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");
48 snprint(name, sizeof(name), "/dev/usb/ep%d.%d", d->id, id);
49 if(access(name, AEXIST) == 0){
50 dprint(2, "%s: %s already exists; trying to open\n", argv0, name);
54 epd->maxpkt = ep->maxpkt; /* guess */
63 if(devctl(d, "new %d %d %s", id, ep->type, mode) < 0){
64 dprint(2, "%s: %s: new: %r\n", argv0, d->dir);
71 if(devctl(epd, "maxpkt %d", ep->maxpkt) < 0)
72 fprint(2, "%s: %s: openep: maxpkt: %r\n", argv0, epd->dir);
74 dprint(2, "%s: %s: maxpkt %d\n", argv0, epd->dir, ep->maxpkt);
75 epd->maxpkt = ep->maxpkt;
76 ac = ep->iface->altc[0];
77 if(ep->ntds > 1 && devctl(epd, "ntds %d", ep->ntds) < 0)
78 fprint(2, "%s: %s: openep: ntds: %r\n", argv0, epd->dir);
80 dprint(2, "%s: %s: ntds %d\n", argv0, epd->dir, ep->ntds);
82 if(ac != nil && (ep->type == Eintr || ep->type == Eiso) && ac->interval != 0)
83 if(devctl(epd, "pollival %d", ac->interval) < 0)
84 fprint(2, "%s: %s: openep: pollival: %r\n", argv0, epd->dir);
94 if(access("/dev/usb", AEXIST) < 0 && bind("#u", "/dev", MBEFORE) < 0)
96 d = emallocz(sizeof(Dev), 1);
102 * +30 to allocate extra size to concat "/<epfilename>"
103 * we should probably remove that feature from the manual
104 * and from the code after checking out that nobody relies on
107 d->dir = emallocz(l + 30, 0);
109 strcpy(d->dir+l, "/ctl");
110 d->cfd = open(d->dir, ORDWR|OCEXEC);
114 werrstr("can't open endpoint %s: %r", d->dir);
120 dprint(2, "%s: opendev %#p %s\n", argv0, d, fn);
125 opendevdata(Dev *d, int mode)
127 char buf[80]; /* more than enough for a usb path */
129 seprint(buf, buf+sizeof(buf), "%s/data", d->dir);
130 d->dfd = open(buf, mode|OCEXEC);
137 * Max device conf is also limited by max control request size as
138 * limited by Maxctllen in the kernel usb.h (both limits are arbitrary).
139 * Some devices ignore the high byte of control transfer reads so keep
140 * the low byte all ones. asking for 16K kills Newsham's disk.
142 Maxdevconf = 4*1024 - 1,
146 loaddevconf(Dev *d, int n)
152 if(n >= nelem(d->usb->conf)){
153 werrstr("loaddevconf: bug: out of configurations in device");
154 fprint(2, "%s: %r\n", argv0);
157 buf = emallocz(Maxdevconf, 0);
158 type = Rd2h|Rstd|Rdev;
159 nr = usbcmd(d, type, Rgetdesc, Dconf<<8|n, 0, buf, Maxdevconf);
164 if(d->usb->conf[n] == nil)
165 d->usb->conf[n] = emallocz(sizeof(Conf), 1);
166 nr = parseconf(d->usb, d->usb->conf[n], buf, nr);
172 mkep(Usbdev *d, int id)
176 d->ep[id] = ep = emallocz(sizeof(Ep), 1);
182 mkstr(uchar *b, int n)
188 if(n > 0 && n > b[0])
190 if(n <= 2 || (n & 1) != 0)
191 return strdup("none");
194 us = s = emallocz(n*UTFmax+1, 0);
195 for(; --n >= 0; b += 2){
197 s += runetochar(s, &r);
204 loaddevstr(Dev *d, int sid)
206 uchar buf[256-2]; /* keep size < 256 */
212 return estrdup("none");
213 type = Rd2h|Rstd|Rdev;
216 * there are devices which do not return a string if used
217 * with invalid language id, so at least try to use the first
218 * one and choose english if failed
220 nr=usbcmd(d, type, Rgetdesc, Dstr<<8, 0, buf, sizeof(buf));
222 langid = 0x0409; // english
224 langid = buf[3]<<8|buf[2];
226 nr=usbcmd(d, type, Rgetdesc, Dstr<<8|sid, langid, buf, sizeof(buf));
227 return mkstr(buf, nr);
238 type = Rd2h|Rstd|Rdev;
241 if((nr=usbcmd(d, type, Rgetdesc, Ddev<<8|0, 0, buf, nr)) < 0)
244 * Several hubs are returning descriptors of 17 bytes, not 18.
245 * We accept them and leave number of configurations as zero.
246 * (a get configuration descriptor also fails for them!)
249 print("%s: %s: warning: device with short descriptor\n",
252 werrstr("short device descriptor (%d bytes)", nr);
256 d->usb = emallocz(sizeof(Usbdev), 1);
257 ep0 = mkep(d->usb, 0);
259 ep0->type = Econtrol;
260 ep0->maxpkt = d->maxpkt = 8; /* a default */
261 nr = parsedev(d, buf, nr);
263 d->usb->vendor = loaddevstr(d, d->usb->vsid);
264 if(strcmp(d->usb->vendor, "none") != 0){
265 d->usb->product = loaddevstr(d, d->usb->psid);
266 d->usb->serial = loaddevstr(d, d->usb->ssid);
278 opendevdata(d, ORDWR);
281 if(loaddevdesc(d) < 0)
283 for(i = 0; i < d->usb->nconf; i++)
284 if(loaddevconf(d, i) < 0)
297 for(i = 0; i < nelem(c->iface); i++)
298 if(c->iface[i] != nil){
299 for(a = 0; a < nelem(c->iface[i]->altc); a++)
300 free(c->iface[i]->altc[a]);
312 if(d==nil || decref(d) != 0)
314 dprint(2, "%s: closedev %#p %s\n", argv0, d, d->dir);
319 d->cfd = d->dfd = -1;
330 for(i = 0; i < nelem(ud->ep); i++)
332 for(i = 0; i < nelem(ud->ddesc); i++)
335 for(i = 0; i < nelem(ud->conf); i++)
336 closeconf(ud->conf[i]);
343 reqstr(int type, int req)
346 static char* ds[] = { "dev", "if", "ep", "oth" };
350 s = seprint(buf, buf+sizeof(buf), "d2h");
352 s = seprint(buf, buf+sizeof(buf), "h2d");
354 s = seprint(s, buf+sizeof(buf), "|cls");
355 else if(type&Rvendor)
356 s = seprint(s, buf+sizeof(buf), "|vnd");
358 s = seprint(s, buf+sizeof(buf), "|std");
359 s = seprint(s, buf+sizeof(buf), "|%s", ds[type&3]);
362 case Rgetstatus: s = seprint(s, buf+sizeof(buf), " getsts"); break;
363 case Rclearfeature: s = seprint(s, buf+sizeof(buf), " clrfeat"); break;
364 case Rsetfeature: s = seprint(s, buf+sizeof(buf), " setfeat"); break;
365 case Rsetaddress: s = seprint(s, buf+sizeof(buf), " setaddr"); break;
366 case Rgetdesc: s = seprint(s, buf+sizeof(buf), " getdesc"); break;
367 case Rsetdesc: s = seprint(s, buf+sizeof(buf), " setdesc"); break;
368 case Rgetconf: s = seprint(s, buf+sizeof(buf), " getcnf"); break;
369 case Rsetconf: s = seprint(s, buf+sizeof(buf), " setcnf"); break;
370 case Rgetiface: s = seprint(s, buf+sizeof(buf), " getif"); break;
371 case Rsetiface: s = seprint(s, buf+sizeof(buf), " setif"); break;
378 cmdreq(Dev *d, int type, int req, int value, int index, uchar *data, int count)
391 wp = emallocz(8+ndata, 0);
392 memmove(wp+8, data, ndata);
400 hd = hexstr(wp, ndata+8);
401 rs = reqstr(type, req);
402 fprint(2, "%s: %s val %d|%d idx %d cnt %d out[%d] %s\n",
403 d->dir, rs, value>>8, value&0xFF,
404 index, count, ndata+8, hd);
407 n = write(d->dfd, wp, 8+ndata);
413 dprint(2, "%s: cmd: short write: %d\n", argv0, n);
420 cmdrep(Dev *d, void *buf, int nb)
424 nb = read(d->dfd, buf, nb);
425 if(nb > 0 && usbdebug > 2){
426 hd = hexstr(buf, nb);
427 fprint(2, "%s: in[%d] %s\n", d->dir, nb, hd);
434 usbcmd(Dev *d, int type, int req, int value, int index, uchar *data, int count)
440 * Some devices do not respond to commands some times.
441 * Others even report errors but later work just fine. Retry.
445 for(i = nerr = 0; i < Uctries; i++){
447 r = cmdreq(d, type, req, value, index, nil, count);
449 r = cmdreq(d, type, req, value, index, data, count);
451 if((type & Rd2h) == 0)
453 r = cmdrep(d, data, count);
457 werrstr("no data from device");
461 rerrstr(err, sizeof(err));
465 /* let the user know the device is not in good shape */
466 fprint(2, "%s: usbcmd: %s: required %d attempts (%s)\n",
467 argv0, d->dir, i, err);
472 unstall(Dev *dev, Dev *ep, int dir)
481 if(usbcmd(dev, r, Rclearfeature, Fhalt, (ep->id&0xF)|dir, nil, 0)<0){
482 werrstr("unstall: %s: %r", ep->dir);
485 if(devctl(ep, "clrhalt") < 0){
486 werrstr("clrhalt: %s: %r", ep->dir);
493 * To be sure it uses a single write.
496 devctl(Dev *dev, char *fmt, ...)
503 e = vseprint(buf, buf+sizeof(buf), fmt, arg);
505 return write(dev->cfd, buf, e-buf);
514 if(devid[0] == '/' || devid[0] == '#'){
515 snprint(buf, sizeof buf, "%s", devid);
516 p = strrchr(buf, '/');
518 p = strrchr(buf, ':');
524 snprint(buf, sizeof buf, "/dev/usb/ep%ld.0", strtol(devid, &p, 10));
532 if(configdev(d) < 0){
538 snprint(buf, sizeof buf, ":%d", d->id);
541 d->hname = strdup(p+1);