18 strecpy(nm, nm+sizeof(nm), r+1);
27 openep(Dev *d, int id)
29 char *mode; /* How many modes? */
36 if(access("/dev/usb", AEXIST) < 0 && bind("#u", "/dev", MBEFORE) < 0)
38 if(d->cfd < 0 || d->usb == nil){
39 werrstr("device not configured");
43 if(id < 0 || id >= nelem(ud->ep) || ud->ep[id] == nil){
44 werrstr("bad enpoint number");
53 snprint(name, sizeof(name), "/dev/usb/ep%d.%d", d->id, id);
54 if(access(name, AEXIST) == 0){
55 dprint(2, "%s: %s already exists; trying to open\n", argv0, name);
58 epd->maxpkt = ep->maxpkt; /* guess */
61 if(devctl(d, "new %d %d %s", id, ep->type, mode) < 0){
62 dprint(2, "%s: %s: new: %r\n", argv0, d->dir);
69 if(devctl(epd, "maxpkt %d", ep->maxpkt) < 0)
70 fprint(2, "%s: %s: openep: maxpkt: %r\n", argv0, epd->dir);
72 dprint(2, "%s: %s: maxpkt %d\n", argv0, epd->dir, ep->maxpkt);
73 epd->maxpkt = ep->maxpkt;
74 ac = ep->iface->altc[0];
75 if(ep->ntds > 1 && devctl(epd, "ntds %d", ep->ntds) < 0)
76 fprint(2, "%s: %s: openep: ntds: %r\n", argv0, epd->dir);
78 dprint(2, "%s: %s: ntds %d\n", argv0, epd->dir, ep->ntds);
81 * For iso endpoints and high speed interrupt endpoints the pollival is
82 * actually 2ⁿ and not n.
83 * The kernel usb driver must take that into account.
84 * It's simpler this way.
87 if(ac != nil && (ep->type == Eintr || ep->type == Eiso) && ac->interval != 0)
88 if(devctl(epd, "pollival %d", ac->interval) < 0)
89 fprint(2, "%s: %s: openep: pollival: %r\n", argv0, epd->dir);
99 if(access("/dev/usb", AEXIST) < 0 && bind("#u", "/dev", MBEFORE) < 0)
101 d = emallocz(sizeof(Dev), 1);
107 * +30 to allocate extra size to concat "/<epfilename>"
108 * we should probably remove that feature from the manual
109 * and from the code after checking out that nobody relies on
112 d->dir = emallocz(l + 30, 0);
114 strcpy(d->dir+l, "/ctl");
115 d->cfd = open(d->dir, ORDWR|OCEXEC);
119 werrstr("can't open endpoint %s: %r", d->dir);
124 dprint(2, "%s: opendev %#p %s\n", argv0, d, fn);
129 opendevdata(Dev *d, int mode)
131 char buf[80]; /* more than enough for a usb path */
133 seprint(buf, buf+sizeof(buf), "%s/data", d->dir);
134 d->dfd = open(buf, mode|OCEXEC);
141 * Max device conf is also limited by max control request size as
142 * limited by Maxctllen in the kernel usb.h (both limits are arbitrary).
144 Maxdevconf = 4 * 1024, /* asking for 16K kills Newsham's disk */
148 loaddevconf(Dev *d, int n)
154 if(n >= nelem(d->usb->conf)){
155 werrstr("loaddevconf: bug: out of configurations in device");
156 fprint(2, "%s: %r\n", argv0);
159 buf = emallocz(Maxdevconf, 0);
160 type = Rd2h|Rstd|Rdev;
161 nr = usbcmd(d, type, Rgetdesc, Dconf<<8|n, 0, buf, Maxdevconf);
166 if(d->usb->conf[n] == nil)
167 d->usb->conf[n] = emallocz(sizeof(Conf), 1);
168 nr = parseconf(d->usb, d->usb->conf[n], buf, nr);
174 mkep(Usbdev *d, int id)
178 d->ep[id] = ep = emallocz(sizeof(Ep), 1);
184 mkstr(uchar *b, int n)
191 if(n <= 2 || (n & 1) != 0)
192 return strdup("none");
195 us = s = emallocz(n*UTFmax+1, 0);
197 for(; --n >= 0; b += 2){
199 s = seprint(s, e, "%C", r);
205 loaddevstr(Dev *d, int sid)
213 return estrdup("none");
214 type = Rd2h|Rstd|Rdev;
217 * there are devices which do not return a string if used
218 * with invalid language id, so at least try to use the first
219 * one and choose english if failed
221 nr=usbcmd(d, type, Rgetdesc, Dstr<<8, 0, buf, sizeof(buf));
223 langid = 0x0409; // english
225 langid = buf[3]<<8|buf[2];
227 nr=usbcmd(d, type, Rgetdesc, Dstr<<8|sid, langid, buf, sizeof(buf));
228 return mkstr(buf, nr);
239 type = Rd2h|Rstd|Rdev;
242 if((nr=usbcmd(d, type, Rgetdesc, Ddev<<8|0, 0, buf, nr)) < 0)
245 * Several hubs are returning descriptors of 17 bytes, not 18.
246 * We accept them and leave number of configurations as zero.
247 * (a get configuration descriptor also fails for them!)
250 print("%s: %s: warning: device with short descriptor\n",
253 werrstr("short device descriptor (%d bytes)", nr);
257 d->usb = emallocz(sizeof(Usbdev), 1);
258 ep0 = mkep(d->usb, 0);
260 ep0->type = Econtrol;
261 ep0->maxpkt = d->maxpkt = 8; /* a default */
262 nr = parsedev(d, buf, nr);
264 d->usb->vendor = loaddevstr(d, d->usb->vsid);
265 if(strcmp(d->usb->vendor, "none") != 0){
266 d->usb->product = loaddevstr(d, d->usb->psid);
267 d->usb->serial = loaddevstr(d, d->usb->ssid);
279 opendevdata(d, ORDWR);
282 if(loaddevdesc(d) < 0)
284 for(i = 0; i < d->usb->nconf; i++)
285 if(loaddevconf(d, i) < 0)
298 for(i = 0; i < nelem(c->iface); i++)
299 if(c->iface[i] != nil){
300 for(a = 0; a < nelem(c->iface[i]->altc); a++)
301 free(c->iface[i]->altc[a]);
313 if(d==nil || decref(d) != 0)
315 dprint(2, "%s: closedev %#p %s\n", argv0, d, d->dir);
322 d->cfd = d->dfd = -1;
331 for(i = 0; i < nelem(ud->ep); i++)
333 for(i = 0; i < nelem(ud->ddesc); i++)
336 for(i = 0; i < nelem(ud->conf); i++)
337 closeconf(ud->conf[i]);
344 reqstr(int type, int req)
347 static char* ds[] = { "dev", "if", "ep", "oth" };
351 s = seprint(buf, buf+sizeof(buf), "d2h");
353 s = seprint(buf, buf+sizeof(buf), "h2d");
355 s = seprint(s, buf+sizeof(buf), "|cls");
356 else if(type&Rvendor)
357 s = seprint(s, buf+sizeof(buf), "|vnd");
359 s = seprint(s, buf+sizeof(buf), "|std");
360 s = seprint(s, buf+sizeof(buf), "|%s", ds[type&3]);
363 case Rgetstatus: s = seprint(s, buf+sizeof(buf), " getsts"); break;
364 case Rclearfeature: s = seprint(s, buf+sizeof(buf), " clrfeat"); break;
365 case Rsetfeature: s = seprint(s, buf+sizeof(buf), " setfeat"); break;
366 case Rsetaddress: s = seprint(s, buf+sizeof(buf), " setaddr"); break;
367 case Rgetdesc: s = seprint(s, buf+sizeof(buf), " getdesc"); break;
368 case Rsetdesc: s = seprint(s, buf+sizeof(buf), " setdesc"); break;
369 case Rgetconf: s = seprint(s, buf+sizeof(buf), " getcnf"); break;
370 case Rsetconf: s = seprint(s, buf+sizeof(buf), " setcnf"); break;
371 case Rgetiface: s = seprint(s, buf+sizeof(buf), " getif"); break;
372 case Rsetiface: s = seprint(s, buf+sizeof(buf), " setif"); break;
379 cmdreq(Dev *d, int type, int req, int value, int index, uchar *data, int count)
392 wp = emallocz(8+ndata, 0);
400 memmove(wp+8, data, ndata);
402 hd = hexstr(wp, ndata+8);
403 rs = reqstr(type, req);
404 fprint(2, "%s: %s val %d|%d idx %d cnt %d out[%d] %s\n",
405 d->dir, rs, value>>8, value&0xFF,
406 index, count, ndata+8, hd);
409 n = write(d->dfd, wp, 8+ndata);
415 dprint(2, "%s: cmd: short write: %d\n", argv0, n);
422 cmdrep(Dev *d, void *buf, int nb)
426 nb = read(d->dfd, buf, nb);
427 if(nb >0 && usbdebug > 2){
428 hd = hexstr(buf, nb);
429 fprint(2, "%s: in[%d] %s\n", d->dir, nb, hd);
436 usbcmd(Dev *d, int type, int req, int value, int index, uchar *data, int count)
442 * Some devices do not respond to commands some times.
443 * Others even report errors but later work just fine. Retry.
447 for(i = nerr = 0; i < Uctries; i++){
449 r = cmdreq(d, type, req, value, index, nil, count);
451 r = cmdreq(d, type, req, value, index, data, count);
453 if((type & Rd2h) == 0)
455 r = cmdrep(d, data, count);
459 werrstr("no data from device");
463 rerrstr(err, sizeof(err));
467 /* let the user know the device is not in good shape */
468 fprint(2, "%s: usbcmd: %s: required %d attempts (%s)\n",
469 argv0, d->dir, i, err);
474 unstall(Dev *dev, Dev *ep, int dir)
483 if(usbcmd(dev, r, Rclearfeature, Fhalt, ep->id|dir, nil, 0)<0){
484 werrstr("unstall: %s: %r", ep->dir);
487 if(devctl(ep, "clrhalt") < 0){
488 werrstr("clrhalt: %s: %r", ep->dir);
495 * To be sure it uses a single write.
498 devctl(Dev *dev, char *fmt, ...)
505 e = vseprint(buf, buf+sizeof(buf), fmt, arg);
507 return write(dev->cfd, buf, e-buf);
516 snprint(buf, sizeof buf, "/dev/usb/ep%d.0", id);
520 if(configdev(d) < 0){