2 * USB device driver framework.
4 * This is in charge of providing access to actual HCIs
5 * and providing I/O to the various endpoints of devices.
6 * A separate user program (usbd) is in charge of
7 * enumerating the bus, setting up endpoints and
8 * starting devices (also user programs).
10 * The interface provided is a violation of the standard:
13 * The interface consists of a root directory with several files
14 * plus a directory (epN.M) with two files per endpoint.
15 * A device is represented by its first endpoint, which
16 * is a control endpoint automatically allocated for each device.
17 * Device control endpoints may be used to create new endpoints.
18 * Devices corresponding to hubs may also allocate new devices,
19 * perhaps also hubs. Initially, a hub device is allocated for
20 * each controller present, to represent its root hub. Those can
23 * All endpoints refer to the first endpoint (epN.0) of the device,
24 * which keeps per-device information, and also to the HCI used
25 * to reach them. Although all endpoints cache that information.
27 * epN.M/data files permit I/O and are considered DMEXCL.
28 * epN.M/ctl files provide status info and accept control requests.
30 * Endpoints may be given file names to be listed also at #u,
31 * for those drivers that have nothing to do after configuring the
32 * device and its endpoints.
34 * Drivers for different controllers are kept at usb[oue]hci.c
35 * It's likely we could factor out much from controllers into
36 * a generic controller driver, the problem is that details
37 * regarding how to handle toggles, tokens, Tds, etc. will
38 * get in the way. Thus, code is probably easier the way it is.
42 #include "../port/lib.h"
47 #include "../port/error.h"
48 #include "../port/usb.h"
50 typedef struct Hcitype Hcitype;
57 Qctl, /* #u/usb/ctl - control requests */
59 Qep0dir, /* #u/usb/ep0.0 - endpoint 0 dir */
60 Qep0io, /* #u/usb/ep0.0/data - endpoint 0 I/O */
61 Qep0ctl, /* #u/usb/ep0.0/ctl - endpoint 0 ctl. */
62 Qep0dummy, /* give 4 qids to each endpoint */
64 Qepdir = 0, /* (qid-qep0dir)&3 is one of these */
65 Qepio, /* to identify which file for the endpoint */
71 CMdebug = 0, /* debug on|off */
72 CMdump, /* dump (data structures for debug) */
75 CMnew = 0, /* new nb ctl|bulk|intr|iso r|w|rw (endpoint) */
76 CMnewdev, /* newdev full|low|high|super portnb (allocate new devices) */
77 CMhub, /* hub (set the device as a hub) */
78 CMspeed, /* speed full|low|high|no */
79 CMmaxpkt, /* maxpkt size */
80 CMntds, /* ntds nb (max nb. of tds per µframe) */
81 CMclrhalt, /* clrhalt (halt was cleared on endpoint) */
82 CMpollival, /* pollival interval (interrupt/iso) */
83 CMhz, /* hz n (samples/sec; iso) */
84 CMsamplesz, /* samplesz n (sample size; iso) */
85 CMinfo, /* info infostr (ke.ep info for humans) */
86 CMdetach, /* detach (abort I/O forever on this ep). */
87 CMaddress, /* address (address is assigned) */
88 CMdebugep, /* debug n (set/clear debug for this ep) */
89 CMname, /* name str (show up as #u/name as well) */
90 CMtmout, /* timeout n (activate timeouts for ep) */
91 CMsampledelay, /* maximum delay introduced by buffering (iso) */
92 CMpreset, /* reset the port */
94 /* Hub feature selectors */
106 #define QID(q) ((int)(q).path)
108 static char Edetach[] = "device is detached";
109 static char Enotconf[] = "endpoint not configured";
110 char Estalled[] = "endpoint stalled";
112 static Cmdtab usbctls[] =
114 {CMdebug, "debug", 2},
118 static Cmdtab epctls[] =
121 {CMnewdev, "newdev", 3},
123 {CMspeed, "speed", 2},
124 {CMmaxpkt, "maxpkt", 2},
126 {CMpollival, "pollival", 2},
127 {CMsamplesz, "samplesz", 2},
130 {CMdetach, "detach", 1},
131 {CMaddress, "address", 1},
132 {CMdebugep, "debug", 2},
133 {CMclrhalt, "clrhalt", 1},
135 {CMtmout, "timeout", 2},
136 {CMsampledelay, "sampledelay", 2},
137 {CMpreset, "reset", 1},
140 static Dirtab usbdir[] =
142 "ctl", {Qctl}, 0, 0666,
145 char *usbmodename[] =
152 static char *ttname[] =
161 static char *spname[] =
163 [Superspeed] "super",
171 static Hcitype hcitypes[Nhcis];
172 static Hci* hcis[Nhcis];
173 static QLock epslck; /* add, del, lookup endpoints */
174 static Ep* eps[Neps]; /* all endpoints known */
175 static int epmax; /* 1 + last endpoint index used */
176 static int usbidgen; /* device address generator */
179 * Is there something like this in a library? should it be?
182 seprintdata(char *s, char *se, uchar *d, int n)
186 s = seprint(s, se, " %#p[%d]: ", d, n);
191 s = seprint(s, se, " %2.2ux", d[i]);
193 s = seprint(s, se, "...");
198 name2speed(char *name)
202 for(i = 0; i < nelem(spname); i++)
203 if(strcmp(name, spname[i]) == 0)
209 name2ttype(char *name)
213 for(i = 0; i < nelem(ttname); i++)
214 if(strcmp(name, ttname[i]) == 0)
216 /* may be a std. USB ep. type */
217 i = strtol(name, nil, 0);
230 name2mode(char *mode)
234 for(i = 0; i < nelem(usbmodename); i++)
235 if(strcmp(mode, usbmodename[i]) == 0)
244 if(q < 0 || q >= epmax || eps[q] == nil)
250 isqtype(int q, int type)
255 return (q & 3) == type;
259 addhcitype(char* t, int (*r)(Hci*))
264 panic("too many USB host interface types");
265 hcitypes[ntype].type = t;
266 hcitypes[ntype].reset = r;
271 seprintep(char *s, char *se, Ep *ep, int all)
273 static char* dsnames[] = { "config", "enabled", "detached", "reset" };
287 s = seprint(s, se, "dev %d ep %d ", di, ep->nb);
288 s = seprint(s, se, "%s", dsnames[ep->dev->state]);
289 s = seprint(s, se, " %s", ttname[ep->ttype]);
290 assert(ep->mode == OREAD || ep->mode == OWRITE || ep->mode == ORDWR);
291 s = seprint(s, se, " %s", usbmodename[ep->mode]);
292 s = seprint(s, se, " speed %s", spname[d->speed]);
293 s = seprint(s, se, " maxpkt %ld", ep->maxpkt);
294 s = seprint(s, se, " pollival %ld", ep->pollival);
295 s = seprint(s, se, " samplesz %ld", ep->samplesz);
296 s = seprint(s, se, " hz %ld", ep->hz);
297 s = seprint(s, se, " hub %d", ep->dev->hub);
298 s = seprint(s, se, " port %d", ep->dev->port);
299 s = seprint(s, se, " rootport %d", ep->dev->rootport);
300 s = seprint(s, se, " addr %d", ep->dev->addr);
302 s = seprint(s, se, " busy");
304 s = seprint(s, se, " idle");
306 s = seprint(s, se, " load %uld", ep->load);
307 s = seprint(s, se, " ref %ld addr %#p", ep->ref, ep);
308 s = seprint(s, se, " idx %d", ep->idx);
310 s = seprint(s, se, " name '%s'", ep->name);
312 s = seprint(s, se, " tmout");
314 s = seprint(s, se, " ctlrno %#x", ep->hp->ctlrno);
315 s = seprint(s, se, " eps:");
316 for(i = 0; i < nelem(d->eps); i++)
318 s = seprint(s, se, " ep%d.%d", di, i);
322 s = seprint(s, se, "\n%s %s\n", ep->info, ep->hp->type);
324 s = seprint(s, se, "\n");
336 ep = smalloc(sizeof(Ep));
339 for(i = 0; i < Neps; i++)
345 print("usb: bug: too few endpoints.\n");
355 ep->samplesz = ep->pollival = ep->hz = 0; /* make them void */
365 if(i < 0 || i >= epmax || eps[i] == nil)
380 if(ep == nil || decref(ep) > 0)
383 deprint("usb: ep%d.%d %#p released\n", d->nb, ep->nb, ep);
386 if(ep->idx == epmax-1)
388 if(ep == ep->ep0 && ep->dev != nil && ep->dev->nb == usbidgen)
393 d->eps[ep->nb] = nil;
413 static char buf[512];
418 print("usb dump eps: epmax %d Neps %d (ref=1+ for dump):\n", epmax, Neps);
419 for(i = 0; i < epmax; i++){
428 s = seprint(s, e, "ep%d.%d ", ep->dev->nb, ep->nb);
429 seprintep(s, e, ep, 1);
431 if(ep->hp->seprintep != nil){
432 ep->hp->seprintep(buf, e, ep);
439 print("usb dump hcis:\n");
440 for(i = 0; i < Nhcis; i++)
441 if(hcis[i] != nil && hcis[i]->dump != nil)
442 hcis[i]->dump(hcis[i]);
453 print("#u: too many device addresses; reuse them more\n");
459 * Create endpoint 0 for a new device
462 newdev(Hci *hp, int ishub, int isroot)
468 d = ep->dev = smalloc(sizeof(Udev));
469 d->nb = newusbid(hp);
473 ep->toggle[0] = ep->toggle[1] = 0;
479 d->speed = Fullspeed;
480 d->state = Dconfig; /* address not yet set */
482 ep->ep0 = ep; /* no ref counted here */
484 ep->tmout = Xfertmout;
486 dprint("newdev %#p ep%d.%d %#p\n", d, d->nb, ep->nb, ep);
491 * Create a new endpoint for the device
492 * accessed via the given endpoint 0.
495 newdevep(Ep *ep, int i, int tt, int mode)
502 error("endpoint already in use");
503 nep = epalloc(ep->hp);
507 nep->toggle[0] = nep->toggle[1] = 0;
512 nep->debug = ep->debug;
516 nep->tmout = Xfertmout;
522 nep->tmout = Xfertmout;
528 deprint("newdevep ep%d.%d %#p\n", d->nb, nep->nb, nep);
549 usbgen(Chan *c, char *, Dirtab*, int, int s, Dir *dp)
559 if(0)ddprint("usbgen q %#x s %d...", QID(c->qid), s);
561 if(QID(c->qid) <= Qusbdir){
562 mkqid(&q, Qdir, 0, QTDIR);
563 devdir(c, q, "#u", 0, eve, 0555, dp);
565 mkqid(&q, Qusbdir, 0, QTDIR);
566 devdir(c, q, "usb", 0, eve, 0555, dp);
568 if(0)ddprint("ok\n");
573 case Qdir: /* list #u */
575 mkqid(&q, Qusbdir, 0, QTDIR);
576 devdir(c, q, "usb", 0, eve, 0555, dp);
577 if(0)ddprint("ok\n");
581 if(s < 0 || s >= epmax)
584 if(ep == nil || ep->name == nil){
587 if(0)ddprint("skip\n");
594 mkqid(&q, Qep0io+s*4, 0, QTFILE);
595 devdir(c, q, ep->name, 0, eve, epdataperm(ep->mode), dp);
598 if(0)ddprint("ok\n");
601 case Qusbdir: /* list #u/usb */
603 if(s < nelem(usbdir)){
605 mkqid(&q, dir->qid.path, 0, QTFILE);
606 devdir(c, q, dir->name, dir->length, eve, dir->perm, dp);
607 if(0)ddprint("ok\n");
611 if(s < 0 || s >= epmax)
615 if(0)ddprint("skip\n");
622 se = up->genbuf+sizeof(up->genbuf);
623 seprint(up->genbuf, se, "ep%d.%d", ep->dev->nb, ep->nb);
624 mkqid(&q, Qep0dir+4*s, 0, QTDIR);
627 devdir(c, q, up->genbuf, 0, eve, 0755, dp);
628 if(0)ddprint("ok\n");
635 default: /* list #u/usb/epN.M */
636 nb = qid2epidx(QID(c->qid));
642 if(isqtype(QID(c->qid), Qepdir)){
646 mkqid(&q, Qep0io+nb*4, 0, QTFILE);
647 perm = epdataperm(mode);
648 devdir(c, q, "data", 0, eve, perm, dp);
651 mkqid(&q, Qep0ctl+nb*4, 0, QTFILE);
652 devdir(c, q, "ctl", 0, eve, 0664, dp);
657 }else if(isqtype(QID(c->qid), Qepctl)){
664 if(0)ddprint("ok\n");
668 if(0)ddprint("fail\n");
673 hciprobe(int cardno, int ctlrno)
677 static int epnb = 1; /* guess the endpoint nb. for the controller */
679 ddprint("hciprobe %d %d\n", cardno, ctlrno);
680 hp = smalloc(sizeof(Hci));
682 hp->tbdf = BUSUNKNOWN;
685 if(isaconfig("usb", ctlrno, hp) == 0){
689 for(cardno = 0; cardno < Nhcis; cardno++){
690 if(hcitypes[cardno].type == nil)
693 if(type==nil || *type==0)
695 if(cistrcmp(hcitypes[cardno].type, type) == 0)
700 if(cardno >= Nhcis || hcitypes[cardno].type == nil){
704 dprint("%s...", hcitypes[cardno].type);
705 if(hcitypes[cardno].reset(hp) < 0){
711 * modern machines have too many usb controllers to list on
714 dprint("#u/usb/ep%d.0: %s: port 0x%luX irq %d\n",
715 epnb, hcitypes[cardno].type, hp->port, hp->irq);
726 if(getconf("*nousbprobe"))
728 dprint("usbreset\n");
730 for(ctlrno = 0; ctlrno < Nhcis; ctlrno++)
731 if((hp = hciprobe(-1, ctlrno)) != nil)
734 while(cardno < Nhcis && ctlrno < Nhcis && hcitypes[cardno].type != nil)
735 if(hcis[ctlrno] != nil)
738 hp = hciprobe(cardno, ctlrno);
743 if(hcis[Nhcis-1] != nil)
744 print("usbreset: bug: Nhcis (%d) too small\n", Nhcis);
767 for(ctlrno = 0; ctlrno < Nhcis; ctlrno++){
774 print("usbinit: %s: %s\n", hp->type, up->errstr);
781 hp->superspeed &= (1<<hp->nports)-1;
782 n = hp->nports - numbits(hp->superspeed);
784 d = newdev(hp, 1, 1); /* new LS/FS/HS root hub */
786 if(hp->highspeed != 0)
787 d->dev->speed = Highspeed;
788 d->dev->state = Denabled; /* although addr == 0 */
789 snprint(info, sizeof(info), "roothub ports %d", n);
790 kstrdup(&d->info, info);
792 n = numbits(hp->superspeed);
794 d = newdev(hp, 1, 1); /* new SS root hub */
796 d->dev->speed = Superspeed;
797 d->dev->state = Denabled; /* although addr == 0 */
798 snprint(info, sizeof(info), "roothub ports %d", n);
799 kstrdup(&d->info, info);
806 usbattach(char *spec)
808 return devattach(L'u', spec);
812 usbwalk(Chan *c, Chan *nc, char **name, int nname)
814 return devwalk(c, nc, name, nname, nil, 0, usbgen);
818 usbstat(Chan *c, uchar *db, int n)
820 return devstat(c, db, n, nil, 0, usbgen);
824 * µs for the given transfer, for bandwidth allocation.
825 * This is a very rough worst case for what 5.11.3
826 * of the usb 2.0 spec says.
827 * Also, we are using maxpkt and not actual transfer sizes.
828 * Only when we are sure we
829 * are not exceeding b/w might we consider adjusting it.
832 usbload(int speed, int maxpkt)
834 enum{ Hostns = 1000, Hubns = 333 };
842 l = 55*8*2 + 2 * (3 + bs) + Hostns;
845 l = 9107 + 84 * (4 + bs) + Hostns;
848 l = 64107 + 2 * Hubns + 667 * (3 + bs) + Hostns;
851 print("usbload: bad speed %d\n", speed);
854 return l / 1000UL; /* in µs */
858 usbopen(Chan *c, int omode)
864 mode = openmode(omode);
867 if(q >= Qep0dir && qid2epidx(q) < 0)
869 if(q < Qep0dir || isqtype(q, Qepctl) || isqtype(q, Qepdir))
870 return devopen(c, omode, nil, 0, usbgen);
872 ep = getep(qid2epidx(q));
875 deprint("usbopen q %#x fid %d omode %d\n", q, c->fid, mode);
891 if(mode != OREAD && ep->mode == OREAD)
893 if(mode != OWRITE && ep->mode == OWRITE)
895 if(ep->ttype == Tnone)
899 if(ep->load == 0 && ep->dev->speed != Superspeed)
900 ep->load = usbload(ep->dev->speed, ep->maxpkt);
903 poperror(); /* ep->inuse */
904 poperror(); /* don't putep(): ref kept for fid using the ep. */
909 c->aux = nil; /* paranoia */
936 if(q < Qep0dir || isqtype(q, Qepctl) || isqtype(q, Qepdir))
939 ep = getep(qid2epidx(q));
942 deprint("usbclose q %#x fid %d ref %ld\n", q, c->fid, ep->ref);
951 putep(ep); /* release ref kept since usbopen */
959 ctlread(Chan *c, void *a, long n, vlong offset)
969 us = s = smalloc(READSTR);
976 for(i = 0; i < epmax; i++){
983 s = seprint(s, se, "ep%d.%d ", ep->dev->nb, ep->nb);
984 s = seprintep(s, se, ep, 0);
990 ep = getep(qid2epidx(q));
998 /* After a new endpoint request we read
999 * the new endpoint name back.
1001 strecpy(s, se, c->aux);
1005 seprintep(s, se, ep, 0);
1009 n = readstr(offset, a, n, us);
1016 * Fake root hub emulation.
1019 rhubread(Ep *ep, void *a, long n)
1023 if(ep->dev->isroot == 0 || ep->nb != 0 || n < 2 || ep->rhrepl == -1)
1027 b[1] = ep->rhrepl>>8;
1028 b[2] = ep->rhrepl>>16;
1029 b[3] = ep->rhrepl>>24;
1030 b[4] = ep->rhrepl>>32;
1031 b[5] = ep->rhrepl>>40;
1032 b[6] = ep->rhrepl>>48;
1033 b[7] = ep->rhrepl>>56;
1045 rootport(Ep *ep, int port)
1055 return hub->rootport;
1057 mask = hp->superspeed;
1058 if(hub->speed != Superspeed)
1059 mask = (1<<hp->nports)-1 & ~mask;
1061 for(rootport = 1; mask != 0; rootport++){
1073 rhubwrite(Ep *ep, void *a, long n)
1081 if(ep->dev == nil || ep->dev->isroot == 0 || ep->nb != 0)
1084 error("root hub is a toy hub");
1087 if(s[Rtype] != (Rh2d|Rclass|Rother) && s[Rtype] != (Rd2h|Rclass|Rother))
1088 error("root hub is a toy hub");
1091 feature = GET2(s+Rvalue);
1092 port = rootport(ep, GET2(s+Rindex));
1094 error("bad hub port number");
1097 ep->rhrepl = hp->portenable(hp, port, cmd == Rsetfeature);
1100 ep->rhrepl = hp->portreset(hp, port, cmd == Rsetfeature);
1103 ep->rhrepl = hp->portstatus(hp, port);
1112 usbread(Chan *c, void *a, long n, vlong offset)
1120 if(c->qid.type == QTDIR)
1121 return devdirread(c, a, n, nil, 0, usbgen);
1123 if(q == Qctl || isqtype(q, Qepctl))
1124 return ctlread(c, a, n, offset);
1126 ep = getep(qid2epidx(q));
1133 if(ep->dev->state == Ddetach)
1135 if(ep->mode == OWRITE || ep->inuse == 0)
1139 error("endpoint not configured");
1141 nr = rhubread(ep, a, n);
1148 ddeprint("\nusbread q %#x fid %d cnt %ld off %lld\n",q,c->fid,n,offset);
1149 n = ep->hp->epread(ep, a, n);
1158 setmaxpkt(Ep *ep, char* s)
1160 long spp, max; /* samples per packet */
1162 if(ep->dev->speed == Fullspeed)
1163 spp = (ep->hz * ep->pollival + 999) / 1000;
1165 spp = (ep->hz * ep->pollival * ep->ntds + 7999) / 8000;
1166 ep->maxpkt = spp * ep->samplesz;
1167 deprint("usb: %s: setmaxpkt: hz %ld poll %ld"
1168 " ntds %d %s speed -> spp %ld maxpkt %ld\n", s,
1169 ep->hz, ep->pollival, ep->ntds, spname[ep->dev->speed],
1171 switch(ep->dev->speed){
1184 if(ep->maxpkt*ep->ntds > max){
1185 print("usb: %s: maxpkt %ld > %ld for %s, truncating\n",
1186 s, ep->maxpkt*ep->ntds, max, spname[ep->dev->speed]);
1187 ep->maxpkt = max/ep->ntds;
1192 * Many endpoint ctls. simply update the portable representation
1193 * of the endpoint. The actual controller driver will look
1194 * at them to setup the endpoints as dictated.
1197 epctl(Ep *ep, Chan *c, void *a, long n)
1199 int i, l, mode, nb, tt;
1205 static char *Info = "info ";
1209 cb = parsecmd(a, n);
1214 ct = lookupcmd(cb, epctls, nelem(epctls));
1216 if(i == CMnew || i == CMspeed || i == CMhub || i == CMpreset)
1218 error("allowed only on a setup endpoint");
1219 if(i != CMclrhalt && i != CMdetach && i != CMdebugep && i != CMname)
1220 if(ep != ep->ep0 && ep->inuse != 0)
1221 error("must configure before using");
1224 deprint("usb epctl %s\n", cb->f[0]);
1225 nb = strtol(cb->f[1], nil, 0);
1226 if(nb < 0 || nb >= Ndeveps)
1227 error("bad endpoint number");
1228 tt = name2ttype(cb->f[2]);
1230 error("unknown endpoint type");
1231 mode = name2mode(cb->f[3]);
1233 error("unknown i/o mode");
1234 newdevep(ep, nb, tt, mode);
1237 deprint("usb epctl %s\n", cb->f[0]);
1238 if(ep != ep->ep0 || d->ishub == 0)
1239 error("not a hub setup endpoint");
1240 l = name2speed(cb->f[1]);
1242 error("speed must be full|low|high|super");
1243 if(l != d->speed && (l == Superspeed || d->speed == Superspeed))
1244 error("wrong speed for superspeed hub/device");
1245 nep = newdev(ep->hp, 0, 0);
1246 nep->dev->speed = l;
1249 else if(l != Lowspeed)
1250 nep->maxpkt = 64; /* assume full speed */
1251 nep->dev->hub = d->addr;
1252 nep->dev->port = atoi(cb->f[2]);
1253 nep->dev->depth = d->depth+1;
1254 nep->dev->rootport = rootport(ep, nep->dev->port);
1255 nep->dev->routestr = d->routestr | (((nep->dev->port&15) << 4*nep->dev->depth) >> 4);
1256 /* next read request will read
1257 * the name for the new endpoint
1259 l = sizeof(up->genbuf);
1260 snprint(up->genbuf, l, "ep%d.%d", nep->dev->nb, nep->nb);
1261 kstrdup(&c->aux, up->genbuf);
1264 deprint("usb epctl %s\n", cb->f[0]);
1268 l = name2speed(cb->f[1]);
1269 deprint("usb epctl %s %d\n", cb->f[0], l);
1271 error("speed must be full|low|high|super");
1272 if(l != d->speed && (l == Superspeed || d->speed == Superspeed))
1273 error("cannot change speed on superspeed device");
1279 l = strtoul(cb->f[1], nil, 0);
1280 deprint("usb epctl %s %d\n", cb->f[0], l);
1281 if(l < 1 || l > 1024)
1282 error("maxpkt not in [1:1024]");
1288 l = strtoul(cb->f[1], nil, 0);
1289 deprint("usb epctl %s %d\n", cb->f[0], l);
1291 error("ntds not in [1:3]");
1297 if(ep->ttype != Tintr && ep->ttype != Tiso)
1298 error("not an intr or iso endpoint");
1299 l = strtoul(cb->f[1], nil, 0);
1300 deprint("usb epctl %s %d\n", cb->f[0], l);
1301 if(ep->dev->speed == Fullspeed || ep->dev->speed == Lowspeed){
1302 if(l < 1 || l > 255)
1303 error("pollival not in [1:255]");
1306 error("pollival power not in [1:16]");
1311 if(ep->ttype == Tiso)
1312 setmaxpkt(ep, "pollival");
1316 if(ep->ttype != Tiso)
1317 error("not an iso endpoint");
1318 l = strtoul(cb->f[1], nil, 0);
1319 deprint("usb epctl %s %d\n", cb->f[0], l);
1321 error("samplesz not in [1:8]");
1324 setmaxpkt(ep, "samplesz");
1328 if(ep->ttype != Tiso)
1329 error("not an iso endpoint");
1330 l = strtoul(cb->f[1], nil, 0);
1331 deprint("usb epctl %s %d\n", cb->f[0], l);
1332 if(l <= 0 || l > 100000)
1333 error("hz not in [1:100000]");
1336 setmaxpkt(ep, "hz");
1341 deprint("usb epctl %s\n", cb->f[0]);
1346 deprint("usb epctl %s\n", cb->f[0]);
1349 if(n < l+2 || strncmp(Info, s, l) != 0)
1354 memmove(b, s+l, n-l);
1356 if(b[n-l-1] == '\n')
1364 deprint("usb epctl %s\n", cb->f[0]);
1365 if(ep->dev->addr == 0)
1366 ep->dev->addr = ep->dev->nb;
1367 ep->dev->state = Denabled;
1370 if(ep->dev->isroot != 0)
1371 error("can't detach a root hub");
1372 deprint("usb epctl %s ep%d.%d\n",
1373 cb->f[0], ep->dev->nb, ep->nb);
1374 ep->dev->state = Ddetach;
1375 /* Release file system ref. for its endpoints */
1376 for(i = 0; i < nelem(ep->dev->eps); i++)
1377 putep(ep->dev->eps[i]);
1380 if(strcmp(cb->f[1], "on") == 0)
1382 else if(strcmp(cb->f[1], "off") == 0)
1385 ep->debug = strtoul(cb->f[1], nil, 0);
1386 print("usb: ep%d.%d debug %d\n",
1387 ep->dev->nb, ep->nb, ep->debug);
1390 deprint("usb epctl %s %s\n", cb->f[0], cb->f[1]);
1391 validname(cb->f[1], 0);
1392 kstrdup(&ep->name, cb->f[1]);
1395 deprint("usb epctl %s\n", cb->f[0]);
1396 if(ep->ttype == Tiso || ep->ttype == Tctl)
1397 error("ctl ignored for this endpoint type");
1398 ep->tmout = strtoul(cb->f[1], nil, 0);
1399 if(ep->tmout != 0 && ep->tmout < Xfertmout)
1400 ep->tmout = Xfertmout;
1403 if(ep->ttype != Tiso)
1404 error("ctl ignored for this endpoint type");
1405 ep->sampledelay = strtoul(cb->f[1], nil, 0);
1408 deprint("usb epctl %s\n", cb->f[0]);
1409 if(ep->ttype != Tctl)
1410 error("not a control endpoint");
1411 if(ep->dev->state != Denabled)
1412 error("forbidden on devices not enabled");
1413 ep->dev->state = Dreset;
1416 panic("usb: unknown epctl %d", ct->index);
1424 usbctl(void *a, long n)
1431 cb = parsecmd(a, n);
1436 ct = lookupcmd(cb, usbctls, nelem(usbctls));
1437 dprint("usb ctl %s\n", cb->f[0]);
1440 if(strcmp(cb->f[1], "on") == 0)
1442 else if(strcmp(cb->f[1], "off") == 0)
1445 debug = strtol(cb->f[1], nil, 0);
1446 print("usb: debug %d\n", debug);
1447 for(i = 0; i < epmax; i++)
1448 if((ep = getep(i)) != nil){
1449 if(ep->hp->debug != nil)
1450 ep->hp->debug(ep->hp, debug);
1464 ctlwrite(Chan *c, void *a, long n)
1471 return usbctl(a, n);
1473 ep = getep(qid2epidx(q));
1480 if(ep->dev->state == Ddetach)
1482 if(isqtype(q, Qepctl) && c->aux != nil){
1483 /* Be sure we don't keep a cloned ep name */
1486 error("read, not write, expected");
1488 n = epctl(ep, c, a, n);
1495 usbwrite(Chan *c, void *a, long n, vlong off)
1500 if(c->qid.type == QTDIR)
1505 if(q == Qctl || isqtype(q, Qepctl))
1506 return ctlwrite(c, a, n);
1508 ep = getep(qid2epidx(q));
1515 if(ep->dev->state == Ddetach)
1517 if(ep->mode == OREAD || ep->inuse == 0)
1522 error("endpoint not configured");
1524 nr = rhubwrite(ep, a, n);
1531 ddeprint("\nusbwrite q %#x fid %d cnt %ld off %lld\n",q, c->fid, n, off);
1532 ep->hp->epwrite(ep, a, n);
1545 for(i = 0; i < Nhcis; i++){
1549 if(hp->shutdown == nil)
1550 print("#u: no shutdown function for %s\n", hp->type);