]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/nusb/ether/cdc.c
usbd: intoruce /env/usbbusy
[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
9 #include "usb.h"
10 #include "dat.h"
11
12 #include <ip.h>
13
14 static int
15 cdcreceive(Dev *ep)
16 {
17         Block *b;
18         int n;
19
20         b = allocb(Maxpkt);
21         if((n = read(ep->dfd, b->wp, b->lim - b->base)) < 0){
22                 freeb(b);
23                 return -1;
24         }
25         b->wp += n;
26         etheriq(b, 1);
27         return 0;
28 }
29
30 static void
31 cdctransmit(Dev *ep, Block *b)
32 {
33         int n;
34
35         n = BLEN(b);
36         if(write(ep->dfd, b->rp, n) < 0){
37                 freeb(b);
38                 return;
39         }
40         freeb(b);
41
42         /*
43          * this may not work with all CDC devices. the
44          * linux driver sends one more random byte
45          * instead of a zero byte transaction. maybe we
46          * should do the same?
47          */
48         if((n % ep->maxpkt) == 0)
49                 write(ep->dfd, "", 0);
50 }
51
52 int
53 cdcinit(Dev *d)
54 {
55         int i;
56         Usbdev *ud;
57         uchar *b;
58         Desc *dd;
59         char *mac;
60
61         ud = d->usb;
62         for(i = 0; i < nelem(ud->ddesc); i++)
63                 if((dd = ud->ddesc[i]) != nil){
64                         b = (uchar*)&dd->data;
65                         if(b[1] == Dfunction && b[2] == Fnether){
66                                 mac = loaddevstr(d, b[3]);
67                                 if(mac != nil && strlen(mac) != 12){
68                                         free(mac);
69                                         mac = nil;
70                                 }
71                                 if(mac != nil){
72                                         parseether(macaddr, mac);
73                                         free(mac);
74
75                                         epreceive = cdcreceive;
76                                         eptransmit = cdctransmit;
77                                         return 0;
78                                 }
79                         }
80                 }
81         return -1;
82 }