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