]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/nusb/ether/cdc.c
merge
[plan9front.git] / sys / src / cmd / nusb / ether / cdc.c
1 /*
2  * generic CDC
3  */
4
5 #include <u.h>
6 #include <libc.h>
7 #include <thread.h>
8 #include <bio.h>
9 #include <auth.h>
10 #include <fcall.h>
11 #include <9p.h>
12 #include <ip.h>
13
14 #include "usb.h"
15 #include "dat.h"
16
17 static int
18 cdcread(Dev *ep, uchar *p, int n)
19 {
20         return read(ep->dfd, p, n);
21 }
22
23 static void
24 cdcwrite(Dev *ep, uchar *p, int n)
25 {
26         if(write(ep->dfd, p, n) < 0){
27                 fprint(2, "cdcwrite: %r\n");
28         } else {
29                 /*
30                  * this may not work with all CDC devices. the
31                  * linux driver sends one more random byte
32                  * instead of a zero byte transaction. maybe we
33                  * should do the same?
34                  */
35                 if(n % ep->maxpkt == 0)
36                         write(ep->dfd, "", 0);
37         }
38 }
39 int
40 cdcinit(Dev *d)
41 {
42         int i;
43         Usbdev *ud;
44         uchar *b;
45         Desc *dd;
46         char *mac;
47
48         ud = d->usb;
49         for(i = 0; i < nelem(ud->ddesc); i++)
50                 if((dd = ud->ddesc[i]) != nil){
51                         b = (uchar*)&dd->data;
52                         if(b[1] == Dfunction && b[2] == Fnether){
53                                 mac = loaddevstr(d, b[3]);
54                                 if(mac != nil && strlen(mac) != 12){
55                                         free(mac);
56                                         mac = nil;
57                                 }
58                                 if(mac != nil){
59                                         parseether(macaddr, mac);
60                                         free(mac);
61
62                                         epread = cdcread;
63                                         epwrite = cdcwrite;
64                                         return 0;
65                                 }
66                         }
67                 }
68         return -1;
69 }