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 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 CMpreset, /* reset the port */
93 /* Hub feature selectors */
105 #define QID(q) ((int)(q).path)
107 static char Edetach[] = "device is detached";
108 static char Enotconf[] = "endpoint not configured";
109 char Estalled[] = "endpoint stalled";
111 static Cmdtab usbctls[] =
113 {CMdebug, "debug", 2},
117 static Cmdtab epctls[] =
120 {CMnewdev, "newdev", 3},
122 {CMspeed, "speed", 2},
123 {CMmaxpkt, "maxpkt", 2},
125 {CMpollival, "pollival", 2},
126 {CMsamplesz, "samplesz", 2},
129 {CMdetach, "detach", 1},
130 {CMaddress, "address", 1},
131 {CMdebugep, "debug", 2},
132 {CMclrhalt, "clrhalt", 1},
134 {CMtmout, "timeout", 2},
135 {CMpreset, "reset", 1},
138 static Dirtab usbdir[] =
140 "ctl", {Qctl}, 0, 0666,
143 char *usbmodename[] =
150 static char *ttname[] =
159 static char *spname[] =
168 static Hcitype hcitypes[Nhcis];
169 static Hci* hcis[Nhcis];
170 static QLock epslck; /* add, del, lookup endpoints */
171 static Ep* eps[Neps]; /* all endpoints known */
172 static int epmax; /* 1 + last endpoint index used */
173 static int usbidgen; /* device address generator */
176 * Is there something like this in a library? should it be?
179 seprintdata(char *s, char *se, uchar *d, int n)
183 s = seprint(s, se, " %#p[%d]: ", d, n);
188 s = seprint(s, se, " %2.2ux", d[i]);
190 s = seprint(s, se, "...");
195 name2speed(char *name)
199 for(i = 0; i < nelem(spname); i++)
200 if(strcmp(name, spname[i]) == 0)
206 name2ttype(char *name)
210 for(i = 0; i < nelem(ttname); i++)
211 if(strcmp(name, ttname[i]) == 0)
213 /* may be a std. USB ep. type */
214 i = strtol(name, nil, 0);
227 name2mode(char *mode)
231 for(i = 0; i < nelem(usbmodename); i++)
232 if(strcmp(mode, usbmodename[i]) == 0)
241 if(q < 0 || q >= epmax || eps[q] == nil)
247 isqtype(int q, int type)
252 return (q & 3) == type;
256 addhcitype(char* t, int (*r)(Hci*))
261 panic("too many USB host interface types");
262 hcitypes[ntype].type = t;
263 hcitypes[ntype].reset = r;
268 seprintep(char *s, char *se, Ep *ep, int all)
270 static char* dsnames[] = { "config", "enabled", "detached", "reset" };
284 s = seprint(s, se, "dev %d ep %d ", di, ep->nb);
285 s = seprint(s, se, "%s", dsnames[ep->dev->state]);
286 s = seprint(s, se, " %s", ttname[ep->ttype]);
287 assert(ep->mode == OREAD || ep->mode == OWRITE || ep->mode == ORDWR);
288 s = seprint(s, se, " %s", usbmodename[ep->mode]);
289 s = seprint(s, se, " speed %s", spname[d->speed]);
290 s = seprint(s, se, " maxpkt %ld", ep->maxpkt);
291 s = seprint(s, se, " pollival %ld", ep->pollival);
292 s = seprint(s, se, " samplesz %ld", ep->samplesz);
293 s = seprint(s, se, " hz %ld", ep->hz);
294 s = seprint(s, se, " hub %d", ep->dev->hub);
295 s = seprint(s, se, " port %d", ep->dev->port);
297 s = seprint(s, se, " busy");
299 s = seprint(s, se, " idle");
301 s = seprint(s, se, " load %uld", ep->load);
302 s = seprint(s, se, " ref %ld addr %#p", ep->ref, ep);
303 s = seprint(s, se, " idx %d", ep->idx);
305 s = seprint(s, se, " name '%s'", ep->name);
307 s = seprint(s, se, " tmout");
309 s = seprint(s, se, " ctlrno %#x", ep->hp->ctlrno);
310 s = seprint(s, se, " eps:");
311 for(i = 0; i < nelem(d->eps); i++)
313 s = seprint(s, se, " ep%d.%d", di, i);
317 s = seprint(s, se, "\n%s %s\n", ep->info, ep->hp->type);
319 s = seprint(s, se, "\n");
331 ep = smalloc(sizeof(Ep));
334 for(i = 0; i < Neps; i++)
340 print("usb: bug: too few endpoints.\n");
350 ep->samplesz = ep->pollival = ep->hz = 0; /* make them void */
360 if(i < 0 || i >= epmax || eps[i] == nil)
375 if(ep != nil && decref(ep) == 0){
377 deprint("usb: ep%d.%d %#p released\n", d->nb, ep->nb, ep);
380 if(ep->idx == epmax-1)
382 if(ep == ep->ep0 && ep->dev != nil && ep->dev->nb == usbidgen)
387 d->eps[ep->nb] = nil;
404 static char buf[512];
409 print("usb dump eps: epmax %d Neps %d (ref=1+ for dump):\n", epmax, Neps);
410 for(i = 0; i < epmax; i++){
419 s = seprint(s, e, "ep%d.%d ", ep->dev->nb, ep->nb);
420 seprintep(s, e, ep, 1);
422 ep->hp->seprintep(buf, e, ep);
428 print("usb dump hcis:\n");
429 for(i = 0; i < Nhcis; i++)
431 hcis[i]->dump(hcis[i]);
442 print("#u: too many device addresses; reuse them more\n");
448 * Create endpoint 0 for a new device
451 newdev(Hci *hp, int ishub, int isroot)
457 d = ep->dev = smalloc(sizeof(Udev));
458 d->nb = newusbid(hp);
461 ep->toggle[0] = ep->toggle[1] = 0;
464 if(hp->highspeed != 0)
465 d->speed = Highspeed;
467 d->speed = Fullspeed;
468 d->state = Dconfig; /* address not yet set */
470 ep->ep0 = ep; /* no ref counted here */
472 ep->tmout = Xfertmout;
474 dprint("newdev %#p ep%d.%d %#p\n", d, d->nb, ep->nb, ep);
479 * Create a new endpoint for the device
480 * accessed via the given endpoint 0.
483 newdevep(Ep *ep, int i, int tt, int mode)
490 error("endpoint already in use");
491 nep = epalloc(ep->hp);
495 nep->toggle[0] = nep->toggle[1] = 0;
500 nep->debug = ep->debug;
504 nep->tmout = Xfertmout;
510 nep->tmout = Xfertmout;
516 deprint("newdevep ep%d.%d %#p\n", d->nb, nep->nb, nep);
537 usbgen(Chan *c, char *, Dirtab*, int, int s, Dir *dp)
547 if(0)ddprint("usbgen q %#x s %d...", QID(c->qid), s);
549 if(QID(c->qid) <= Qusbdir){
550 mkqid(&q, Qdir, 0, QTDIR);
551 devdir(c, q, "#u", 0, eve, 0555, dp);
553 mkqid(&q, Qusbdir, 0, QTDIR);
554 devdir(c, q, "usb", 0, eve, 0555, dp);
556 if(0)ddprint("ok\n");
561 case Qdir: /* list #u */
563 mkqid(&q, Qusbdir, 0, QTDIR);
564 devdir(c, q, "usb", 0, eve, 0555, dp);
565 if(0)ddprint("ok\n");
569 if(s < 0 || s >= epmax)
572 if(ep == nil || ep->name == nil){
575 if(0)ddprint("skip\n");
582 mkqid(&q, Qep0io+s*4, 0, QTFILE);
583 devdir(c, q, ep->name, 0, eve, epdataperm(ep->mode), dp);
586 if(0)ddprint("ok\n");
589 case Qusbdir: /* list #u/usb */
591 if(s < nelem(usbdir)){
593 mkqid(&q, dir->qid.path, 0, QTFILE);
594 devdir(c, q, dir->name, dir->length, eve, dir->perm, dp);
595 if(0)ddprint("ok\n");
599 if(s < 0 || s >= epmax)
603 if(0)ddprint("skip\n");
610 se = up->genbuf+sizeof(up->genbuf);
611 seprint(up->genbuf, se, "ep%d.%d", ep->dev->nb, ep->nb);
612 mkqid(&q, Qep0dir+4*s, 0, QTDIR);
615 devdir(c, q, up->genbuf, 0, eve, 0755, dp);
616 if(0)ddprint("ok\n");
623 default: /* list #u/usb/epN.M */
624 nb = qid2epidx(QID(c->qid));
630 if(isqtype(QID(c->qid), Qepdir)){
634 mkqid(&q, Qep0io+nb*4, 0, QTFILE);
635 perm = epdataperm(mode);
636 devdir(c, q, "data", 0, eve, perm, dp);
639 mkqid(&q, Qep0ctl+nb*4, 0, QTFILE);
640 devdir(c, q, "ctl", 0, eve, 0664, dp);
645 }else if(isqtype(QID(c->qid), Qepctl)){
652 if(0)ddprint("ok\n");
656 if(0)ddprint("fail\n");
661 hciprobe(int cardno, int ctlrno)
666 static int epnb = 1; /* guess the endpoint nb. for the controller */
668 ddprint("hciprobe %d %d\n", cardno, ctlrno);
669 hp = smalloc(sizeof(Hci));
673 for(cardno = 0; cardno < Nhcis; cardno++){
674 if(hcitypes[cardno].type == nil)
677 if(type==nil || *type==0)
679 if(cistrcmp(hcitypes[cardno].type, type) == 0)
683 if(cardno >= Nhcis || hcitypes[cardno].type == nil){
687 dprint("%s...", hcitypes[cardno].type);
688 if(hcitypes[cardno].reset(hp) < 0){
693 snprint(name, sizeof(name), "usb%s", hcitypes[cardno].type);
694 intrenable(hp->irq, hp->interrupt, hp, UNKNOWN, name);
696 print("#u/usb/ep%d.0: %s: port %#luX irq %d\n",
697 epnb, hcitypes[cardno].type, hp->port, hp->irq);
709 dprint("usbreset\n");
711 for(ctlrno = 0; ctlrno < Nhcis; ctlrno++)
712 if((hp = hciprobe(-1, ctlrno)) != nil)
715 while(cardno < Nhcis && ctlrno < Nhcis && hcitypes[cardno].type != nil)
716 if(hcis[ctlrno] != nil)
719 hp = hciprobe(cardno, ctlrno);
724 if(hcis[Nhcis-1] != nil)
725 print("usbreset: bug: Nhcis too small\n");
737 for(ctlrno = 0; ctlrno < Nhcis; ctlrno++){
742 d = newdev(hp, 1, 1); /* new root hub */
743 d->dev->state = Denabled; /* although addr == 0 */
745 snprint(info, sizeof(info), "ports %d", hp->nports);
746 kstrdup(&d->info, info);
752 usbattach(char *spec)
754 return devattach(L'u', spec);
758 usbwalk(Chan *c, Chan *nc, char **name, int nname)
760 return devwalk(c, nc, name, nname, nil, 0, usbgen);
764 usbstat(Chan *c, uchar *db, int n)
766 return devstat(c, db, n, nil, 0, usbgen);
770 * µs for the given transfer, for bandwidth allocation.
771 * This is a very rough worst case for what 5.11.3
772 * of the usb 2.0 spec says.
773 * Also, we are using maxpkt and not actual transfer sizes.
774 * Only when we are sure we
775 * are not exceeding b/w might we consider adjusting it.
778 usbload(int speed, int maxpkt)
780 enum{ Hostns = 1000, Hubns = 333 };
788 l = 55*8*2 + 2 * (3 + bs) + Hostns;
791 l = 9107 + 84 * (4 + bs) + Hostns;
794 l = 64107 + 2 * Hubns + 667 * (3 + bs) + Hostns;
797 print("usbload: bad speed %d\n", speed);
800 return l / 1000UL; /* in µs */
804 usbopen(Chan *c, int omode)
810 mode = openmode(omode);
813 if(q >= Qep0dir && qid2epidx(q) < 0)
815 if(q < Qep0dir || isqtype(q, Qepctl) || isqtype(q, Qepdir))
816 return devopen(c, omode, nil, 0, usbgen);
818 ep = getep(qid2epidx(q));
821 deprint("usbopen q %#x fid %d omode %d\n", q, c->fid, mode);
837 if(mode != OREAD && ep->mode == OREAD)
839 if(mode != OWRITE && ep->mode == OWRITE)
841 if(ep->ttype == Tnone)
846 ep->load = usbload(ep->dev->speed, ep->maxpkt);
849 poperror(); /* ep->inuse */
850 poperror(); /* don't putep(): ref kept for fid using the ep. */
855 c->aux = nil; /* paranoia */
882 if(q < Qep0dir || isqtype(q, Qepctl) || isqtype(q, Qepdir))
885 ep = getep(qid2epidx(q));
888 deprint("usbclose q %#x fid %d ref %ld\n", q, c->fid, ep->ref);
897 putep(ep); /* release ref kept since usbopen */
905 ctlread(Chan *c, void *a, long n, vlong offset)
915 us = s = smalloc(READSTR);
922 for(i = 0; i < epmax; i++){
929 s = seprint(s, se, "ep%d.%d ", ep->dev->nb, ep->nb);
930 s = seprintep(s, se, ep, 0);
936 ep = getep(qid2epidx(q));
944 /* After a new endpoint request we read
945 * the new endpoint name back.
947 strecpy(s, se, c->aux);
951 seprintep(s, se, ep, 0);
955 n = readstr(offset, a, n, us);
962 * Fake root hub emulation.
965 rhubread(Ep *ep, void *a, long n)
969 if(ep->dev->isroot == 0 || ep->nb != 0 || n < 2)
982 rhubwrite(Ep *ep, void *a, long n)
990 if(ep->dev == nil || ep->dev->isroot == 0 || ep->nb != 0)
993 error("root hub is a toy hub");
996 if(s[Rtype] != (Rh2d|Rclass|Rother) && s[Rtype] != (Rd2h|Rclass|Rother))
997 error("root hub is a toy hub");
1000 feature = GET2(s+Rvalue);
1001 port = GET2(s+Rindex);
1002 if(port < 1 || port > hp->nports)
1003 error("bad hub port number");
1006 ep->rhrepl = hp->portenable(hp, port, cmd == Rsetfeature);
1009 ep->rhrepl = hp->portreset(hp, port, cmd == Rsetfeature);
1012 ep->rhrepl = hp->portstatus(hp, port);
1021 usbread(Chan *c, void *a, long n, vlong offset)
1029 if(c->qid.type == QTDIR)
1030 return devdirread(c, a, n, nil, 0, usbgen);
1032 if(q == Qctl || isqtype(q, Qepctl))
1033 return ctlread(c, a, n, offset);
1035 ep = getep(qid2epidx(q));
1042 if(ep->dev->state == Ddetach)
1044 if(ep->mode == OWRITE || ep->inuse == 0)
1048 error("endpoint not configured");
1050 nr = rhubread(ep, a, n);
1057 ddeprint("\nusbread q %#x fid %d cnt %ld off %lld\n",q,c->fid,n,offset);
1058 n = ep->hp->epread(ep, a, n);
1073 setmaxpkt(Ep *ep, char* s)
1075 long spp; /* samples per packet */
1077 if(ep->dev->speed == Highspeed)
1078 spp = (ep->hz * ep->pollival * ep->ntds + 7999) / 8000;
1080 spp = (ep->hz * ep->pollival + 999) / 1000;
1081 ep->maxpkt = spp * ep->samplesz;
1082 deprint("usb: %s: setmaxpkt: hz %ld poll %ld"
1083 " ntds %d %s speed -> spp %ld maxpkt %ld\n", s,
1084 ep->hz, ep->pollival, ep->ntds, spname[ep->dev->speed],
1086 if(ep->maxpkt > 1024){
1087 print("usb: %s: maxpkt %ld > 1024. truncating\n", s, ep->maxpkt);
1093 * Many endpoint ctls. simply update the portable representation
1094 * of the endpoint. The actual controller driver will look
1095 * at them to setup the endpoints as dictated.
1098 epctl(Ep *ep, Chan *c, void *a, long n)
1100 int i, l, mode, nb, tt;
1106 static char *Info = "info ";
1110 cb = parsecmd(a, n);
1115 ct = lookupcmd(cb, epctls, nelem(epctls));
1119 if(i == CMnew || i == CMspeed || i == CMhub || i == CMpreset)
1121 error("allowed only on a setup endpoint");
1122 if(i != CMclrhalt && i != CMdetach && i != CMdebugep && i != CMname)
1123 if(ep != ep->ep0 && ep->inuse != 0)
1124 error("must configure before using");
1127 deprint("usb epctl %s\n", cb->f[0]);
1128 nb = strtol(cb->f[1], nil, 0);
1129 if(nb < 0 || nb >= Ndeveps)
1130 error("bad endpoint number");
1131 tt = name2ttype(cb->f[2]);
1133 error("unknown endpoint type");
1134 mode = name2mode(cb->f[3]);
1136 error("unknown i/o mode");
1137 newdevep(ep, nb, tt, mode);
1140 deprint("usb epctl %s\n", cb->f[0]);
1141 if(ep != ep->ep0 || d->ishub == 0)
1142 error("not a hub setup endpoint");
1143 l = name2speed(cb->f[1]);
1145 error("speed must be full|low|high");
1146 nep = newdev(ep->hp, 0, 0);
1147 nep->dev->speed = l;
1148 if(nep->dev->speed != Lowspeed)
1149 nep->maxpkt = 64; /* assume full speed */
1150 nep->dev->hub = d->nb;
1151 nep->dev->port = atoi(cb->f[2]);
1152 /* next read request will read
1153 * the name for the new endpoint
1155 l = sizeof(up->genbuf);
1156 snprint(up->genbuf, l, "ep%d.%d", nep->dev->nb, nep->nb);
1157 kstrdup(&c->aux, up->genbuf);
1160 deprint("usb epctl %s\n", cb->f[0]);
1164 l = name2speed(cb->f[1]);
1165 deprint("usb epctl %s %d\n", cb->f[0], l);
1167 error("speed must be full|low|high");
1173 l = strtoul(cb->f[1], nil, 0);
1174 deprint("usb epctl %s %d\n", cb->f[0], l);
1175 if(l < 1 || l > 1024)
1176 error("maxpkt not in [1:1024]");
1182 l = strtoul(cb->f[1], nil, 0);
1183 deprint("usb epctl %s %d\n", cb->f[0], l);
1185 error("ntds not in [1:3]");
1191 if(ep->ttype != Tintr && ep->ttype != Tiso)
1192 error("not an intr or iso endpoint");
1193 l = strtoul(cb->f[1], nil, 0);
1194 deprint("usb epctl %s %d\n", cb->f[0], l);
1195 if(ep->ttype == Tiso ||
1196 (ep->ttype == Tintr && ep->dev->speed == Highspeed)){
1198 error("pollival power not in [1:16]");
1201 if(l < 1 || l > 255)
1202 error("pollival not in [1:255]");
1205 if(ep->ttype == Tiso)
1206 setmaxpkt(ep, "pollival");
1210 if(ep->ttype != Tiso)
1211 error("not an iso endpoint");
1212 l = strtoul(cb->f[1], nil, 0);
1213 deprint("usb epctl %s %d\n", cb->f[0], l);
1215 error("samplesz not in [1:8]");
1218 setmaxpkt(ep, "samplesz");
1222 if(ep->ttype != Tiso)
1223 error("not an iso endpoint");
1224 l = strtoul(cb->f[1], nil, 0);
1225 deprint("usb epctl %s %d\n", cb->f[0], l);
1226 if(l <= 0 || l > 100000)
1227 error("hz not in [1:100000]");
1230 setmaxpkt(ep, "hz");
1235 deprint("usb epctl %s\n", cb->f[0]);
1240 deprint("usb epctl %s\n", cb->f[0]);
1243 if(n < l+2 || strncmp(Info, s, l) != 0)
1248 memmove(b, s+l, n-l);
1250 if(b[n-l-1] == '\n')
1258 deprint("usb epctl %s\n", cb->f[0]);
1259 ep->dev->state = Denabled;
1262 if(ep->dev->isroot != 0)
1263 error("can't detach a root hub");
1264 deprint("usb epctl %s ep%d.%d\n",
1265 cb->f[0], ep->dev->nb, ep->nb);
1266 ep->dev->state = Ddetach;
1267 /* Release file system ref. for its endpoints */
1268 for(i = 0; i < nelem(ep->dev->eps); i++)
1269 putep(ep->dev->eps[i]);
1272 if(strcmp(cb->f[1], "on") == 0)
1274 else if(strcmp(cb->f[1], "off") == 0)
1277 ep->debug = strtoul(cb->f[1], nil, 0);
1278 print("usb: ep%d.%d debug %d\n",
1279 ep->dev->nb, ep->nb, ep->debug);
1282 deprint("usb epctl %s %s\n", cb->f[0], cb->f[1]);
1283 validname(cb->f[1], 0);
1284 kstrdup(&ep->name, cb->f[1]);
1287 deprint("usb epctl %s\n", cb->f[0]);
1288 if(ep->ttype == Tiso || ep->ttype == Tctl)
1289 error("ctl ignored for this endpoint type");
1290 ep->tmout = strtoul(cb->f[1], nil, 0);
1291 if(ep->tmout != 0 && ep->tmout < Xfertmout)
1292 ep->tmout = Xfertmout;
1295 deprint("usb epctl %s\n", cb->f[0]);
1296 if(ep->ttype != Tctl)
1297 error("not a control endpoint");
1298 if(ep->dev->state != Denabled)
1299 error("forbidden on devices not enabled");
1300 ep->dev->state = Dreset;
1303 panic("usb: unknown epctl %d", ct->index);
1311 usbctl(void *a, long n)
1318 cb = parsecmd(a, n);
1323 ct = lookupcmd(cb, usbctls, nelem(usbctls));
1324 dprint("usb ctl %s\n", cb->f[0]);
1327 if(strcmp(cb->f[1], "on") == 0)
1329 else if(strcmp(cb->f[1], "off") == 0)
1332 debug = strtol(cb->f[1], nil, 0);
1333 print("usb: debug %d\n", debug);
1334 for(i = 0; i < epmax; i++)
1335 if((ep = getep(i)) != nil){
1336 ep->hp->debug(ep->hp, debug);
1350 ctlwrite(Chan *c, void *a, long n)
1357 return usbctl(a, n);
1359 ep = getep(qid2epidx(q));
1366 if(ep->dev->state == Ddetach)
1368 if(isqtype(q, Qepctl) && c->aux != nil){
1369 /* Be sure we don't keep a cloned ep name */
1372 error("read, not write, expected");
1374 n = epctl(ep, c, a, n);
1381 usbwrite(Chan *c, void *a, long n, vlong off)
1386 if(c->qid.type == QTDIR)
1391 if(q == Qctl || isqtype(q, Qepctl))
1392 return ctlwrite(c, a, n);
1394 ep = getep(qid2epidx(q));
1401 if(ep->dev->state == Ddetach)
1403 if(ep->mode == OREAD || ep->inuse == 0)
1408 error("endpoint not configured");
1410 nr = rhubwrite(ep, a, n);
1417 ddeprint("\nusbwrite q %#x fid %d cnt %ld off %lld\n",q, c->fid, n, off);
1418 ep->hp->epwrite(ep, a, n);
1431 for(i = 0; i < Nhcis; i++){
1435 if(hp->shutdown == nil)
1436 print("#u: no shutdown function for %s\n", hp->type);