8 parsedev(Dev *xd, uchar *b, int n)
18 hd = hexstr(b, Ddevlen);
19 fprint(2, "%s: parsedev %s: %s\n", argv0, xd->dir, hd);
22 if(dd->bLength < Ddevlen){
23 werrstr("short dev descr. (%d < %d)", dd->bLength, Ddevlen);
26 if(dd->bDescriptorType != Ddev){
27 werrstr("%d is not a dev descriptor", dd->bDescriptorType);
30 d->csp = CSP(dd->bDevClass, dd->bDevSubClass, dd->bDevProtocol);
31 d->ep[0]->maxpkt = xd->maxpkt = dd->bMaxPacketSize0;
32 d->class = dd->bDevClass;
33 d->nconf = dd->bNumConfigurations;
35 dprint(2, "%s: %s: no configurations\n", argv0, xd->dir);
36 d->vid = GET2(dd->idVendor);
37 d->did = GET2(dd->idProduct);
38 d->dno = GET2(dd->bcdDev);
39 d->vsid = dd->iManufacturer;
40 d->psid = dd->iProduct;
41 d->ssid = dd->iSerialNumber;
42 if(n > Ddevlen && usbdebug>1)
43 fprint(2, "%s: %s: parsedev: %d bytes left",
44 argv0, xd->dir, n - Ddevlen);
49 parseiface(Usbdev *d, Conf *c, uchar *b, int n, Iface **ipp, Altc **app)
51 int class, subclass, proto;
56 assert(d != nil && c != nil);
58 werrstr("short interface descriptor");
62 ifid = dip->bInterfaceNumber;
63 if(ifid < 0 || ifid >= nelem(c->iface)){
64 werrstr("bad interface number %d", ifid);
67 if(c->iface[ifid] == nil)
68 c->iface[ifid] = emallocz(sizeof(Iface), 1);
70 class = dip->bInterfaceClass;
71 subclass = dip->bInterfaceSubClass;
72 proto = dip->bInterfaceProtocol;
73 ip->csp = CSP(class, subclass, proto);
76 if(d->csp == 0) /* use csp from 1st iface */
77 d->csp = ip->csp; /* if device has none */
81 if(c == d->conf[0] && ifid == 0) /* ep0 was already there */
83 altid = dip->bAlternateSetting;
84 if(altid < 0 || altid >= nelem(ip->altc)){
85 werrstr("bad alternate conf. number %d", altid);
88 if(ip->altc[altid] == nil)
89 ip->altc[altid] = emallocz(sizeof(Altc), 1);
91 *app = ip->altc[altid];
95 extern Ep* mkep(Usbdev *, int);
98 parseendpt(Usbdev *d, Conf *c, Iface *ip, Altc *altc, uchar *b, int n, Ep **epp)
104 assert(d != nil && c != nil && ip != nil && altc != nil);
106 werrstr("short endpoint descriptor");
110 altc->attrib = dep->bmAttributes; /* here? */
111 altc->interval = dep->bInterval;
113 epid = dep->bEndpointAddress & 0xF;
114 assert(epid < nelem(d->ep));
115 if(dep->bEndpointAddress & 0x80)
123 }else if((ep->addr & 0x80) != (dep->bEndpointAddress & 0x80))
125 ep->maxpkt = GET2(dep->wMaxPacketSize);
126 ep->ntds = 1 + ((ep->maxpkt >> 11) & 3);
128 ep->addr = dep->bEndpointAddress;
129 ep->type = dep->bmAttributes & 0x03;
130 ep->isotype = (dep->bmAttributes>>2) & 0x03;
133 for(i = 0; i < nelem(ip->ep); i++)
136 if(i == nelem(ip->ep)){
137 werrstr("parseendpt: bug: too many end points on interface "
138 "with csp %#lux", ip->csp);
139 fprint(2, "%s: %r\n", argv0);
142 *epp = ip->ep[i] = ep;
150 case Ddev: return "device";
151 case Dconf: return "config";
152 case Dstr: return "string";
153 case Diface: return "interface";
154 case Dep: return "endpoint";
155 case Dreport: return "report";
156 case Dphysical: return "phys";
157 default: return "desc";
162 parsedesc(Usbdev *d, Conf *c, uchar *b, int n)
170 assert(d != nil && c != nil);
175 for(nd = 0; nd < nelem(d->ddesc); nd++)
176 if(d->ddesc[nd] == nil)
179 while(n > 2 && b[0] != 0 && b[0] <= n){
183 fprint(2, "%s:\t\tparsedesc %s %x[%d] %s\n",
184 argv0, dname(b[1]), b[1], b[0], hd);
190 werrstr("unexpected descriptor %d", b[1]);
191 ddprint(2, "%s\tparsedesc: %r", argv0);
194 if(parseiface(d, c, b, n, &ip, &altc) < 0){
195 ddprint(2, "%s\tparsedesc: %r\n", argv0);
200 if(ip == nil || altc == nil){
201 werrstr("unexpected endpoint descriptor");
204 if(parseendpt(d, c, ip, altc, b, n, &ep) < 0){
205 ddprint(2, "%s\tparsedesc: %r\n", argv0);
210 if(nd == nelem(d->ddesc)){
211 fprint(2, "%s: parsedesc: too many "
212 "device-specific descriptors for device"
214 argv0, d->vendor, d->product);
217 d->ddesc[nd] = emallocz(sizeof(Desc)+b[0], 0);
218 d->ddesc[nd]->iface = ip;
219 d->ddesc[nd]->ep = ep;
220 d->ddesc[nd]->altc = altc;
221 d->ddesc[nd]->conf = c;
222 memmove(&d->ddesc[nd]->data, b, len);
233 parseconf(Usbdev *d, Conf *c, uchar *b, int n)
240 assert(d != nil && c != nil);
243 hd = hexstr(b, Dconflen);
244 fprint(2, "%s:\tparseconf %s\n", argv0, hd);
247 if(dc->bLength < Dconflen){
248 werrstr("short configuration descriptor");
251 if(dc->bDescriptorType != Dconf){
252 werrstr("not a configuration descriptor");
255 c->cval = dc->bConfigurationValue;
256 c->attrib = dc->bmAttributes;
257 c->milliamps = dc->MaxPower*2;
258 l = GET2(dc->wTotalLength);
260 werrstr("truncated configuration info");
266 if(n > 0 && (nr=parsedesc(d, c, b, n)) < 0)
269 if(n > 0 && usbdebug>1)
270 fprint(2, "%s:\tparseconf: %d bytes left\n", argv0, n);