7 parsedev(Dev *xd, uchar *b, int n)
17 hd = hexstr(b, Ddevlen);
18 fprint(2, "%s: parsedev %s: %s\n", argv0, xd->dir, hd);
21 if(dd->bLength < Ddevlen){
22 werrstr("short dev descr. (%d < %d)", dd->bLength, Ddevlen);
25 if(dd->bDescriptorType != Ddev){
26 werrstr("%d is not a dev descriptor", dd->bDescriptorType);
29 d->csp = CSP(dd->bDevClass, dd->bDevSubClass, dd->bDevProtocol);
30 d->ep[0]->maxpkt = xd->maxpkt = dd->bMaxPacketSize0;
31 d->class = dd->bDevClass;
32 d->nconf = dd->bNumConfigurations;
34 dprint(2, "%s: %s: no configurations\n", argv0, xd->dir);
35 d->vid = GET2(dd->idVendor);
36 d->did = GET2(dd->idProduct);
37 d->dno = GET2(dd->bcdDev);
38 d->vsid = dd->iManufacturer;
39 d->psid = dd->iProduct;
40 d->ssid = dd->iSerialNumber;
41 if(n > Ddevlen && usbdebug>1)
42 fprint(2, "%s: %s: parsedev: %d bytes left",
43 argv0, xd->dir, n - Ddevlen);
48 parseiface(Usbdev *d, Conf *c, uchar *b, int n, Iface **ipp, Altc **app)
50 int class, subclass, proto;
55 assert(d != nil && c != nil);
57 werrstr("short interface descriptor");
61 ifid = dip->bInterfaceNumber;
62 if(ifid < 0 || ifid >= nelem(c->iface)){
63 werrstr("bad interface number %d", ifid);
66 if(c->iface[ifid] == nil)
67 c->iface[ifid] = emallocz(sizeof(Iface), 1);
69 class = dip->bInterfaceClass;
70 subclass = dip->bInterfaceSubClass;
71 proto = dip->bInterfaceProtocol;
72 ip->csp = CSP(class, subclass, proto);
75 if(d->csp == 0) /* use csp from 1st iface */
76 d->csp = ip->csp; /* if device has none */
80 if(c == d->conf[0] && ifid == 0) /* ep0 was already there */
82 altid = dip->bAlternateSetting;
83 if(altid < 0 || altid >= nelem(ip->altc)){
84 werrstr("bad alternate conf. number %d", altid);
87 if(ip->altc[altid] == nil)
88 ip->altc[altid] = emallocz(sizeof(Altc), 1);
90 *app = ip->altc[altid];
94 extern Ep* mkep(Usbdev *, int);
97 parseendpt(Usbdev *d, Conf *c, Iface *ip, Altc *altc, uchar *b, int n, Ep **epp)
99 int i, dir, epid, type, addr;
103 assert(d != nil && c != nil && ip != nil && altc != nil);
105 werrstr("short endpoint descriptor");
109 altc->attrib = dep->bmAttributes; /* here? */
110 altc->interval = dep->bInterval;
112 type = dep->bmAttributes & 0x03;
113 addr = dep->bEndpointAddress;
118 epid = addr & 0xF; /* default map to 0..15 */
119 assert(epid < nelem(d->ep));
124 }else if((ep->addr & 0x80) != (addr & 0x80)){
129 * resolve conflict when same endpoint number
130 * is used for different input and output types.
131 * map input endpoint to 16..31 and output to 0..15.
133 ep->id = ((ep->addr & 0x80) != 0)<<4 | (ep->addr & 0xF);
135 epid = ep->id ^ 0x10;
140 ep->maxpkt = GET2(dep->wMaxPacketSize);
141 ep->ntds = 1 + ((ep->maxpkt >> 11) & 3);
145 ep->isotype = (dep->bmAttributes>>2) & 0x03;
148 for(i = 0; i < nelem(ip->ep); i++)
151 if(i == nelem(ip->ep)){
152 werrstr("parseendpt: bug: too many end points on interface "
153 "with csp %#lux", ip->csp);
154 fprint(2, "%s: %r\n", argv0);
157 *epp = ip->ep[i] = ep;
165 case Ddev: return "device";
166 case Dconf: return "config";
167 case Dstr: return "string";
168 case Diface: return "interface";
169 case Dep: return "endpoint";
170 case Dreport: return "report";
171 case Dphysical: return "phys";
172 default: return "desc";
177 parsedesc(Usbdev *d, Conf *c, uchar *b, int n)
185 assert(d != nil && c != nil);
190 for(nd = 0; nd < nelem(d->ddesc); nd++)
191 if(d->ddesc[nd] == nil)
194 while(n > 2 && b[0] != 0 && b[0] <= n){
198 fprint(2, "%s:\t\tparsedesc %s %x[%d] %s\n",
199 argv0, dname(b[1]), b[1], b[0], hd);
205 werrstr("unexpected descriptor %d", b[1]);
206 ddprint(2, "%s\tparsedesc: %r", argv0);
209 if(parseiface(d, c, b, n, &ip, &altc) < 0){
210 ddprint(2, "%s\tparsedesc: %r\n", argv0);
215 if(ip == nil || altc == nil){
216 werrstr("unexpected endpoint descriptor");
219 if(parseendpt(d, c, ip, altc, b, n, &ep) < 0){
220 ddprint(2, "%s\tparsedesc: %r\n", argv0);
225 if(nd == nelem(d->ddesc)){
226 fprint(2, "%s: parsedesc: too many "
227 "device-specific descriptors for device"
229 argv0, d->vendor, d->product);
232 d->ddesc[nd] = emallocz(sizeof(Desc)+b[0], 0);
233 d->ddesc[nd]->iface = ip;
234 d->ddesc[nd]->ep = ep;
235 d->ddesc[nd]->altc = altc;
236 d->ddesc[nd]->conf = c;
237 memmove(&d->ddesc[nd]->data, b, len);
248 parseconf(Usbdev *d, Conf *c, uchar *b, int n)
255 assert(d != nil && c != nil);
258 hd = hexstr(b, Dconflen);
259 fprint(2, "%s:\tparseconf %s\n", argv0, hd);
262 if(dc->bLength < Dconflen){
263 werrstr("short configuration descriptor");
266 if(dc->bDescriptorType != Dconf){
267 werrstr("not a configuration descriptor");
270 c->cval = dc->bConfigurationValue;
271 c->attrib = dc->bmAttributes;
272 c->milliamps = dc->MaxPower*2;
273 l = GET2(dc->wTotalLength);
275 werrstr("truncated configuration info");
281 if(n > 0 && (nr=parsedesc(d, c, b, n)) < 0)
284 if(n > 0 && usbdebug>1)
285 fprint(2, "%s:\tparseconf: %d bytes left\n", argv0, n);