xref: /plan9/sys/src/9/pc/devusb.c (revision 3468a4915d661daa200976acc4f80f51aae144b2)
1 /*
2  * USB device driver.
3  *
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).
9  *
10  * The interface provided is a violation of the standard:
11  * you're welcome.
12  *
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
21  * never be removed.
22  *
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.
26  *
27  * epN.M/data files permit I/O and are considered DMEXCL.
28  * epN.M/ctl files provide status info and accept control requests.
29  *
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.
33  *
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.
39  *
40  */
41 
42 #include	"u.h"
43 #include	"../port/lib.h"
44 #include	"mem.h"
45 #include	"dat.h"
46 #include	"fns.h"
47 #include	"io.h"
48 #include	"../port/error.h"
49 #include	"usb.h"
50 
51 typedef struct Hcitype Hcitype;
52 
53 enum
54 {
55 	/* Qid numbers */
56 	Qdir = 0,		/* #u */
57 	Qusbdir,			/* #u/usb */
58 	Qctl,			/* #u/usb/ctl - control requests */
59 
60 	Qep0dir,			/* #u/usb/ep0.0 - endpoint 0 dir */
61 	Qep0io,			/* #u/usb/ep0.0/data - endpoint 0 I/O */
62 	Qep0ctl,		/* #u/usb/ep0.0/ctl - endpoint 0 ctl. */
63 	Qep0dummy,		/* give 4 qids to each endpoint */
64 
65 	Qepdir = 0,		/* (qid-qep0dir)&3 is one of these */
66 	Qepio,			/* to identify which file for the endpoint */
67 	Qepctl,
68 
69 	/* ... */
70 
71 	/* Usb ctls. */
72 	CMdebug = 0,		/* debug on|off */
73 	CMdump,			/* dump (data structures for debug) */
74 
75 	/* Ep. ctls */
76 	CMnew = 0,		/* new nb ctl|bulk|intr|iso r|w|rw (endpoint) */
77 	CMnewdev,		/* newdev full|low|high portnb (allocate new devices) */
78 	CMhub,			/* hub (set the device as a hub) */
79 	CMspeed,		/* speed full|low|high|no */
80 	CMmaxpkt,		/* maxpkt size */
81 	CMntds,			/* ntds nb (max nb. of tds per µframe) */
82 	CMclrhalt,		/* clrhalt (halt was cleared on endpoint) */
83 	CMpollival,		/* pollival interval (interrupt/iso) */
84 	CMhz,			/* hz n (samples/sec; iso) */
85 	CMsamplesz,		/* samplesz n (sample size; iso) */
86 	CMinfo,			/* info infostr (ke.ep info for humans) */
87 	CMdetach,		/* detach (abort I/O forever on this ep). */
88 	CMaddress,		/* address (address is assigned) */
89 	CMdebugep,		/* debug n (set/clear debug for this ep) */
90 	CMname,			/* name str (show up as #u/name as well) */
91 	CMtmout,		/* timeout n (activate timeouts for ep) */
92 	CMpreset,		/* reset the port */
93 
94 	/* Hub feature selectors */
95 	Rportenable	= 1,
96 	Rportreset	= 4,
97 
98 };
99 
100 struct Hcitype
101 {
102 	char*	type;
103 	int	(*reset)(Hci*);
104 };
105 
106 #define QID(q)	((int)(q).path)
107 
108 static char Edetach[] = "device is detached";
109 static char Enotconf[] = "endpoint not configured";
110 char Estalled[] = "endpoint stalled";
111 
112 static Cmdtab usbctls[] =
113 {
114 	{CMdebug,	"debug",	2},
115 	{CMdump,	"dump",		1},
116 };
117 
118 static Cmdtab epctls[] =
119 {
120 	{CMnew,		"new",		4},
121 	{CMnewdev,	"newdev",	3},
122 	{CMhub,		"hub",		1},
123 	{CMspeed,	"speed",	2},
124 	{CMmaxpkt,	"maxpkt",	2},
125 	{CMntds,	"ntds",		2},
126 	{CMpollival,	"pollival",	2},
127 	{CMsamplesz,	"samplesz",	2},
128 	{CMhz,		"hz",		2},
129 	{CMinfo,		"info",		0},
130 	{CMdetach,	"detach",	1},
131 	{CMaddress,	"address",	1},
132 	{CMdebugep,	"debug",	2},
133 	{CMclrhalt,	"clrhalt",	1},
134 	{CMname,	"name",		2},
135 	{CMtmout,	"timeout",	2},
136 	{CMpreset,	"reset",	1},
137 };
138 
139 static Dirtab usbdir[] =
140 {
141 	"ctl",		{Qctl},		0,	0666,
142 };
143 
144 char *usbmodename[] =
145 {
146 	[OREAD]	"r",
147 	[OWRITE]	"w",
148 	[ORDWR]	"rw",
149 };
150 
151 static char *ttname[] =
152 {
153 	[Tnone]	"none",
154 	[Tctl]	"control",
155 	[Tiso]	"iso",
156 	[Tintr]	"interrupt",
157 	[Tbulk]	"bulk",
158 };
159 
160 static char *spname[] =
161 {
162 	[Fullspeed]	"full",
163 	[Lowspeed]	"low",
164 	[Highspeed]	"high",
165 	[Nospeed]	"no",
166 };
167 
168 static int	debug;
169 static Hcitype	hcitypes[Nhcis];
170 static Hci*	hcis[Nhcis];
171 static QLock	epslck;		/* add, del, lookup endpoints */
172 static Ep*	eps[Neps];	/* all endpoints known */
173 static int	epmax;		/* 1 + last endpoint index used  */
174 static int	usbidgen;	/* device address generator */
175 
176 /*
177  * Is there something like this in a library? should it be?
178  */
179 char*
180 seprintdata(char *s, char *se, uchar *d, int n)
181 {
182 	int i;
183 	int l;
184 
185 	s = seprint(s, se, " %#p[%d]: ", d, n);
186 	l = n;
187 	if(l > 10)
188 		l = 10;
189 	for(i=0; i<l; i++)
190 		s = seprint(s, se, " %2.2ux", d[i]);
191 	if(l < n)
192 		s = seprint(s, se, "...");
193 	return s;
194 }
195 
196 static int
197 name2speed(char *name)
198 {
199 	int i;
200 
201 	for(i = 0; i < nelem(spname); i++)
202 		if(strcmp(name, spname[i]) == 0)
203 			return i;
204 	return Nospeed;
205 }
206 
207 static int
208 name2ttype(char *name)
209 {
210 	int i;
211 
212 	for(i = 0; i < nelem(ttname); i++)
213 		if(strcmp(name, ttname[i]) == 0)
214 			return i;
215 	/* may be a std. USB ep. type */
216 	i = strtol(name, nil, 0);
217 	switch(i+1){
218 	case Tctl:
219 	case Tiso:
220 	case Tbulk:
221 	case Tintr:
222 		return i+1;
223 	default:
224 		return Tnone;
225 	}
226 }
227 
228 static int
229 name2mode(char *mode)
230 {
231 	int i;
232 
233 	for(i = 0; i < nelem(usbmodename); i++)
234 		if(strcmp(mode, usbmodename[i]) == 0)
235 			return i;
236 	return -1;
237 }
238 
239 static int
240 qid2epidx(int q)
241 {
242 	q = (q-Qep0dir)/4;
243 	if(q < 0 || q >= epmax || eps[q] == nil)
244 		return -1;
245 	return q;
246 }
247 
248 static int
249 isqtype(int q, int type)
250 {
251 	if(q < Qep0dir)
252 		return 0;
253 	q -= Qep0dir;
254 	return (q & 3) == type;
255 }
256 
257 void
258 addhcitype(char* t, int (*r)(Hci*))
259 {
260 	static int ntype;
261 
262 	if(ntype == Nhcis)
263 		panic("too many USB host interface types");
264 	hcitypes[ntype].type = t;
265 	hcitypes[ntype].reset = r;
266 	ntype++;
267 }
268 
269 static char*
270 seprintep(char *s, char *se, Ep *ep, int all)
271 {
272 	static char* dsnames[] = { "config", "enabled", "detached", "reset" };
273 	Udev *d;
274 	int i;
275 	int di;
276 
277 	d = ep->dev;
278 
279 	qlock(ep);
280 	if(waserror()){
281 		qunlock(ep);
282 		nexterror();
283 	}
284 	di = ep->dev->nb;
285 	if(all)
286 		s = seprint(s, se, "dev %d ep %d ", di, ep->nb);
287 	s = seprint(s, se, "%s", dsnames[ep->dev->state]);
288 	s = seprint(s, se, " %s", ttname[ep->ttype]);
289 	assert(ep->mode == OREAD || ep->mode == OWRITE || ep->mode == ORDWR);
290 	s = seprint(s, se, " %s", usbmodename[ep->mode]);
291 	s = seprint(s, se, " speed %s", spname[d->speed]);
292 	s = seprint(s, se, " maxpkt %ld", ep->maxpkt);
293 	s = seprint(s, se, " pollival %ld", ep->pollival);
294 	s = seprint(s, se, " samplesz %ld", ep->samplesz);
295 	s = seprint(s, se, " hz %ld", ep->hz);
296 	s = seprint(s, se, " hub %d", ep->dev->hub);
297 	s = seprint(s, se, " port %d", ep->dev->port);
298 	if(ep->inuse)
299 		s = seprint(s, se, " busy");
300 	else
301 		s = seprint(s, se, " idle");
302 	if(all){
303 		s = seprint(s, se, " load %uld", ep->load);
304 		s = seprint(s, se, " ref %ld addr %#p", ep->ref, ep);
305 		s = seprint(s, se, " idx %d", ep->idx);
306 		if(ep->name != nil)
307 			s = seprint(s, se, " name '%s'", ep->name);
308 		if(ep->tmout != 0)
309 			s = seprint(s, se, " tmout");
310 		if(ep == ep->ep0){
311 			s = seprint(s, se, " ctlrno %#x", ep->hp->ctlrno);
312 			s = seprint(s, se, " eps:");
313 			for(i = 0; i < nelem(d->eps); i++)
314 				if(d->eps[i] != nil)
315 					s = seprint(s, se, " ep%d.%d", di, i);
316 		}
317 	}
318 	if(ep->info != nil)
319 		s = seprint(s, se, "\n%s %s\n", ep->info, ep->hp->type);
320 	else
321 		s = seprint(s, se, "\n");
322 	qunlock(ep);
323 	poperror();
324 	return s;
325 }
326 
327 static Ep*
328 epalloc(Hci *hp)
329 {
330 	Ep *ep;
331 	int i;
332 
333 	ep = mallocz(sizeof(Ep), 1);
334 	ep->ref = 1;
335 	qlock(&epslck);
336 	for(i = 0; i < Neps; i++)
337 		if(eps[i] == nil)
338 			break;
339 	if(i == Neps){
340 		qunlock(&epslck);
341 		free(ep);
342 		print("usb: bug: too few endpoints.\n");
343 		return nil;
344 	}
345 	ep->idx = i;
346 	if(epmax <= i)
347 		epmax = i+1;
348 	eps[i] = ep;
349 	ep->hp = hp;
350 	ep->maxpkt = 8;
351 	ep->ntds = 1;
352 	ep->samplesz = ep->pollival = ep->hz = 0; /* make them void */
353 	qunlock(&epslck);
354 	return ep;
355 }
356 
357 static Ep*
358 getep(int i)
359 {
360 	Ep *ep;
361 
362 	if(i < 0 || i >= epmax || eps[i] == nil)
363 		return nil;
364 	qlock(&epslck);
365 	ep = eps[i];
366 	if(ep != nil)
367 		incref(ep);
368 	qunlock(&epslck);
369 	return ep;
370 }
371 
372 static void
373 putep(Ep *ep)
374 {
375 	Udev *d;
376 
377 	if(ep != nil && decref(ep) == 0){
378 		d = ep->dev;
379 		deprint("usb: ep%d.%d %#p released\n", d->nb, ep->nb, ep);
380 		qlock(&epslck);
381 		eps[ep->idx] = nil;
382 		if(ep->idx == epmax-1)
383 			epmax--;
384 		if(ep == ep->ep0 && ep->dev != nil && ep->dev->nb == usbidgen)
385 			usbidgen--;
386 		qunlock(&epslck);
387 		if(d != nil){
388 			qlock(ep->ep0);
389 			d->eps[ep->nb] = nil;
390 			qunlock(ep->ep0);
391 		}
392 		if(ep->ep0 != ep){
393 			putep(ep->ep0);
394 			ep->ep0 = nil;
395 		}
396 		free(ep->info);
397 		free(ep->name);
398 		free(ep);
399 	}
400 }
401 
402 static void
403 dumpeps(void)
404 {
405 	int i;
406 	static char buf[512];
407 	char *s;
408 	char *e;
409 	Ep *ep;
410 
411 	print("usb dump eps: epmax %d Neps %d (ref=1+ for dump):\n", epmax, Neps);
412 	for(i = 0; i < epmax; i++){
413 		s = buf;
414 		e = buf+sizeof(buf);
415 		ep = getep(i);
416 		if(ep != nil){
417 			if(waserror()){
418 				putep(ep);
419 				nexterror();
420 			}
421 			s = seprint(s, e, "ep%d.%d ", ep->dev->nb, ep->nb);
422 			seprintep(s, e, ep, 1);
423 			print("%s", buf);
424 			ep->hp->seprintep(buf, e, ep);
425 			print("%s", buf);
426 			poperror();
427 			putep(ep);
428 		}
429 	}
430 	print("usb dump hcis:\n");
431 	for(i = 0; i < Nhcis; i++)
432 		if(hcis[i] != nil)
433 			hcis[i]->dump(hcis[i]);
434 }
435 
436 static int
437 newusbid(Hci *)
438 {
439 	int id;
440 
441 	qlock(&epslck);
442 	id = ++usbidgen;
443 	if(id >= 0x7F)
444 		print("#u: too many device addresses; reuse them more\n");
445 	qunlock(&epslck);
446 	return id;
447 }
448 
449 /*
450  * Create endpoint 0 for a new device
451  */
452 static Ep*
453 newdev(Hci *hp, int ishub, int isroot)
454 {
455 	Ep *ep;
456 	Udev *d;
457 
458 	ep = epalloc(hp);
459 	d = ep->dev = mallocz(sizeof(Udev), 1);
460 	d->nb = newusbid(hp);
461 	d->eps[0] = ep;
462 	ep->nb = 0;
463 	ep->toggle[0] = ep->toggle[1] = 0;
464 	d->ishub = ishub;
465 	d->isroot = isroot;
466 	if(hp->highspeed != 0)
467 		d->speed = Highspeed;
468 	else
469 		d->speed = Fullspeed;
470 	d->state = Dconfig;		/* address not yet set */
471 	ep->dev = d;
472 	ep->ep0 = ep;			/* no ref counted here */
473 	ep->ttype = Tctl;
474 	ep->tmout = Xfertmout;
475 	ep->mode = ORDWR;
476 	dprint("newdev %#p ep%d.%d %#p\n", d, d->nb, ep->nb, ep);
477 	return ep;
478 }
479 
480 /*
481  * Create a new endpoint for the device
482  * accessed via the given endpoint 0.
483  */
484 static Ep*
485 newdevep(Ep *ep, int i, int tt, int mode)
486 {
487 	Ep *nep;
488 	Udev *d;
489 
490 	d = ep->dev;
491 	if(d->eps[i] != nil)
492 		error("endpoint already in use");
493 	nep = epalloc(ep->hp);
494 	incref(ep);
495 	d->eps[i] = nep;
496 	nep->nb = i;
497 	nep->toggle[0] = nep->toggle[1] = 0;
498 	nep->ep0 = ep;
499 	nep->dev = ep->dev;
500 	nep->mode = mode;
501 	nep->ttype = tt;
502 	nep->debug = ep->debug;
503 	/* set defaults */
504 	switch(tt){
505 	case Tctl:
506 		nep->tmout = Xfertmout;
507 		break;
508 	case Tintr:
509 		nep->pollival = 10;
510 		break;
511 	case Tiso:
512 		nep->tmout = Xfertmout;
513 		nep->pollival = 10;
514 		nep->samplesz = 4;
515 		nep->hz = 44100;
516 		break;
517 	}
518 	deprint("newdevep ep%d.%d %#p\n", d->nb, nep->nb, nep);
519 	return ep;
520 }
521 
522 static int
523 epdataperm(int mode)
524 {
525 
526 	switch(mode){
527 	case OREAD:
528 		return 0440|DMEXCL;
529 		break;
530 	case OWRITE:
531 		return 0220|DMEXCL;
532 		break;
533 	default:
534 		return 0660|DMEXCL;
535 	}
536 }
537 
538 static int
539 usbgen(Chan *c, char *, Dirtab*, int, int s, Dir *dp)
540 {
541 	Qid q;
542 	Dirtab *dir;
543 	int perm;
544 	char *se;
545 	Ep *ep;
546 	int nb;
547 	int mode;
548 
549 	if(0)ddprint("usbgen q %#x s %d...", QID(c->qid), s);
550 	if(s == DEVDOTDOT){
551 		if(QID(c->qid) <= Qusbdir){
552 			mkqid(&q, Qdir, 0, QTDIR);
553 			devdir(c, q, "#u", 0, eve, 0555, dp);
554 		}else{
555 			mkqid(&q, Qusbdir, 0, QTDIR);
556 			devdir(c, q, "usb", 0, eve, 0555, dp);
557 		}
558 		if(0)ddprint("ok\n");
559 		return 1;
560 	}
561 
562 	switch(QID(c->qid)){
563 	case Qdir:				/* list #u */
564 		if(s == 0){
565 			mkqid(&q, Qusbdir, 0, QTDIR);
566 			devdir(c, q, "usb", 0, eve, 0555, dp);
567 			if(0)ddprint("ok\n");
568 			return 1;
569 		}
570 		s--;
571 		if(s < 0 || s >= epmax)
572 			goto Fail;
573 		ep = getep(s);
574 		if(ep == nil || ep->name == nil){
575 			if(ep != nil)
576 				putep(ep);
577 			if(0)ddprint("skip\n");
578 			return 0;
579 		}
580 		if(waserror()){
581 			putep(ep);
582 			nexterror();
583 		}
584 		mkqid(&q, Qep0io+s*4, 0, QTFILE);
585 		devdir(c, q, ep->name, 0, eve, epdataperm(ep->mode), dp);
586 		putep(ep);
587 		poperror();
588 		if(0)ddprint("ok\n");
589 		return 1;
590 
591 	case Qusbdir:				/* list #u/usb */
592 	Usbdir:
593 		if(s < nelem(usbdir)){
594 			dir = &usbdir[s];
595 			mkqid(&q, dir->qid.path, 0, QTFILE);
596 			devdir(c, q, dir->name, dir->length, eve, dir->perm, dp);
597 			if(0)ddprint("ok\n");
598 			return 1;
599 		}
600 		s -= nelem(usbdir);
601 		if(s < 0 || s >= epmax)
602 			goto Fail;
603 		ep = getep(s);
604 		if(ep == nil){
605 			if(0)ddprint("skip\n");
606 			return 0;
607 		}
608 		if(waserror()){
609 			putep(ep);
610 			nexterror();
611 		}
612 		se = up->genbuf+sizeof(up->genbuf);
613 		seprint(up->genbuf, se, "ep%d.%d", ep->dev->nb, ep->nb);
614 		mkqid(&q, Qep0dir+4*s, 0, QTDIR);
615 		putep(ep);
616 		poperror();
617 		devdir(c, q, up->genbuf, 0, eve, 0755, dp);
618 		if(0)ddprint("ok\n");
619 		return 1;
620 
621 	case Qctl:
622 		s = 0;
623 		goto Usbdir;
624 
625 	default:				/* list #u/usb/epN.M */
626 		nb = qid2epidx(QID(c->qid));
627 		ep = getep(nb);
628 		if(ep == nil)
629 			goto Fail;
630 		mode = ep->mode;
631 		putep(ep);
632 		if(isqtype(QID(c->qid), Qepdir)){
633 		Epdir:
634 			switch(s){
635 			case 0:
636 				mkqid(&q, Qep0io+nb*4, 0, QTFILE);
637 				perm = epdataperm(mode);
638 				devdir(c, q, "data", 0, eve, perm, dp);
639 				break;
640 			case 1:
641 				mkqid(&q, Qep0ctl+nb*4, 0, QTFILE);
642 				devdir(c, q, "ctl", 0, eve, 0664, dp);
643 				break;
644 			default:
645 				goto Fail;
646 			}
647 		}else if(isqtype(QID(c->qid), Qepctl)){
648 			s = 1;
649 			goto Epdir;
650 		}else{
651 			s = 0;
652 			goto Epdir;
653 		}
654 		if(0)ddprint("ok\n");
655 		return 1;
656 	}
657 Fail:
658 	if(0)ddprint("fail\n");
659 	return -1;
660 }
661 
662 static Hci*
663 hciprobe(int cardno, int ctlrno)
664 {
665 	Hci *hp;
666 	char *type;
667 	char name[64];
668 	static int epnb = 1;	/* guess the endpoint nb. for the controller */
669 
670 	ddprint("hciprobe %d %d\n", cardno, ctlrno);
671 	hp = mallocz(sizeof(Hci), 1);
672 	hp->ctlrno = ctlrno;
673 	hp->tbdf = BUSUNKNOWN;
674 
675 	if(cardno < 0){
676 		if(isaconfig("usb", ctlrno, hp) == 0){
677 			free(hp);
678 			return nil;
679 		}
680 		for(cardno = 0; cardno < Nhcis; cardno++){
681 			if(hcitypes[cardno].type == nil)
682 				break;
683 			type = hp->type;
684 			if(type==nil || *type==0)
685 				type = "uhci";
686 			if(cistrcmp(hcitypes[cardno].type, type) == 0)
687 				break;
688 		}
689 	}
690 
691 	if(cardno >= Nhcis || hcitypes[cardno].type == nil){
692 		free(hp);
693 		return nil;
694 	}
695 	dprint("%s...", hcitypes[cardno].type);
696 	if(hcitypes[cardno].reset(hp) < 0){
697 		free(hp);
698 		return nil;
699 	}
700 
701 	/*
702 	 * IRQ2 doesn't really exist, it's used to gang the interrupt
703 	 * controllers together. A device set to IRQ2 will appear on
704 	 * the second interrupt controller as IRQ9.
705 	 */
706 	if(hp->irq == 2)
707 		hp->irq = 9;
708 	snprint(name, sizeof(name), "usb%s", hcitypes[cardno].type);
709 	intrenable(hp->irq, hp->interrupt, hp, hp->tbdf, name);
710 
711 	/*
712 	 * modern machines have too many usb controllers to list on
713 	 * the console.
714 	 */
715 	dprint("#u/usb/ep%d.0: %s: port 0x%luX irq %d\n",
716 		epnb, hcitypes[cardno].type, hp->port, hp->irq);
717 	epnb++;
718 	return hp;
719 }
720 
721 static void
722 usbreset(void)
723 {
724 	int cardno, ctlrno;
725 	Hci *hp;
726 
727 	if(getconf("*nousbprobe"))
728 		return;
729 	dprint("usbreset\n");
730 
731 	for(ctlrno = 0; ctlrno < Nhcis; ctlrno++)
732 		if((hp = hciprobe(-1, ctlrno)) != nil)
733 			hcis[ctlrno] = hp;
734 	cardno = ctlrno = 0;
735 	while(cardno < Nhcis && ctlrno < Nhcis && hcitypes[cardno].type != nil)
736 		if(hcis[ctlrno] != nil)
737 			ctlrno++;
738 		else{
739 			hp = hciprobe(cardno, ctlrno);
740 			if(hp == nil)
741 				cardno++;
742 			hcis[ctlrno++] = hp;
743 		}
744 	if(hcis[Nhcis-1] != nil)
745 		print("usbreset: bug: Nhcis too small\n");
746 }
747 
748 static void
749 usbinit(void)
750 {
751 	Hci *hp;
752 	int ctlrno;
753 	Ep *d;
754 	char info[40];
755 
756 	dprint("usbinit\n");
757 	for(ctlrno = 0; ctlrno < Nhcis; ctlrno++){
758 		hp = hcis[ctlrno];
759 		if(hp != nil){
760 			if(hp->init != nil)
761 				hp->init(hp);
762 			d = newdev(hp, 1, 1);		/* new root hub */
763 			d->dev->state = Denabled;	/* although addr == 0 */
764 			d->maxpkt = 64;
765 			snprint(info, sizeof(info), "ports %d", hp->nports);
766 			kstrdup(&d->info, info);
767 		}
768 	}
769 }
770 
771 static Chan*
772 usbattach(char *spec)
773 {
774 	return devattach(L'u', spec);
775 }
776 
777 static Walkqid*
778 usbwalk(Chan *c, Chan *nc, char **name, int nname)
779 {
780 	return devwalk(c, nc, name, nname, nil, 0, usbgen);
781 }
782 
783 static int
784 usbstat(Chan *c, uchar *db, int n)
785 {
786 	return devstat(c, db, n, nil, 0, usbgen);
787 }
788 
789 /*
790  * µs for the given transfer, for bandwidth allocation.
791  * This is a very rough worst case for what 5.11.3
792  * of the usb 2.0 spec says.
793  * Also, we are using maxpkt and not actual transfer sizes.
794  * Only when we are sure we
795  * are not exceeding b/w might we consider adjusting it.
796  */
797 static ulong
798 usbload(int speed, int maxpkt)
799 {
800 	enum{ Hostns = 1000, Hubns = 333 };
801 	ulong l;
802 	ulong bs;
803 
804 	l = 0;
805 	bs = 10UL * maxpkt;
806 	switch(speed){
807 	case Highspeed:
808 		l = 55*8*2 + 2 * (3 + bs) + Hostns;
809 		break;
810 	case Fullspeed:
811 		l = 9107 + 84 * (4 + bs) + Hostns;
812 		break;
813 	case Lowspeed:
814 		l = 64107 + 2 * Hubns + 667 * (3 + bs) + Hostns;
815 		break;
816 	default:
817 		print("usbload: bad speed %d\n", speed);
818 		/* let it run */
819 	}
820 	return l / 1000UL;	/* in µs */
821 }
822 
823 static Chan*
824 usbopen(Chan *c, int omode)
825 {
826 	int q;
827 	Ep *ep;
828 	int mode;
829 
830 	mode = openmode(omode);
831 	q = QID(c->qid);
832 
833 	if(q >= Qep0dir && qid2epidx(q) < 0)
834 		error(Eio);
835 	if(q < Qep0dir || isqtype(q, Qepctl) || isqtype(q, Qepdir))
836 		return devopen(c, omode, nil, 0, usbgen);
837 
838 	ep = getep(qid2epidx(q));
839 	if(ep == nil)
840 		error(Eio);
841 	deprint("usbopen q %#x fid %d omode %d\n", q, c->fid, mode);
842 	if(waserror()){
843 		putep(ep);
844 		nexterror();
845 	}
846 	qlock(ep);
847 	if(ep->inuse){
848 		qunlock(ep);
849 		error(Einuse);
850 	}
851 	ep->inuse = 1;
852 	qunlock(ep);
853 	if(waserror()){
854 		ep->inuse = 0;
855 		nexterror();
856 	}
857 	if(mode != OREAD && ep->mode == OREAD)
858 		error(Eperm);
859 	if(mode != OWRITE && ep->mode == OWRITE)
860 		error(Eperm);
861 	if(ep->ttype == Tnone)
862 		error(Enotconf);
863 	ep->clrhalt = 0;
864 	ep->rhrepl = -1;
865 	if(ep->load == 0)
866 		ep->load = usbload(ep->dev->speed, ep->maxpkt);
867 	ep->hp->epopen(ep);
868 
869 	poperror();	/* ep->inuse */
870 	poperror();	/* don't putep(): ref kept for fid using the ep. */
871 
872 	c->mode = mode;
873 	c->flag |= COPEN;
874 	c->offset = 0;
875 	c->aux = nil;	/* paranoia */
876 	return c;
877 }
878 
879 static void
880 epclose(Ep *ep)
881 {
882 	qlock(ep);
883 	if(waserror()){
884 		qunlock(ep);
885 		nexterror();
886 	}
887 	if(ep->inuse){
888 		ep->hp->epclose(ep);
889 		ep->inuse = 0;
890 	}
891 	qunlock(ep);
892 	poperror();
893 }
894 
895 static void
896 usbclose(Chan *c)
897 {
898 	int q;
899 	Ep *ep;
900 
901 	q = QID(c->qid);
902 	if(q < Qep0dir || isqtype(q, Qepctl) || isqtype(q, Qepdir))
903 		return;
904 
905 	ep = getep(qid2epidx(q));
906 	if(ep == nil)
907 		return;
908 	deprint("usbclose q %#x fid %d ref %ld\n", q, c->fid, ep->ref);
909 	if(waserror()){
910 		putep(ep);
911 		nexterror();
912 	}
913 	if(c->flag & COPEN){
914 		free(c->aux);
915 		c->aux = nil;
916 		epclose(ep);
917 		putep(ep);	/* release ref kept since usbopen */
918 		c->flag &= ~COPEN;
919 	}
920 	poperror();
921 	putep(ep);
922 }
923 
924 static long
925 ctlread(Chan *c, void *a, long n, vlong offset)
926 {
927 	int q;
928 	char *s;
929 	char *us;
930 	char *se;
931 	Ep *ep;
932 	int i;
933 
934 	q = QID(c->qid);
935 	us = s = smalloc(READSTR);
936 	se = s + READSTR;
937 	if(waserror()){
938 		free(us);
939 		nexterror();
940 	}
941 	if(q == Qctl)
942 		for(i = 0; i < epmax; i++){
943 			ep = getep(i);
944 			if(ep != nil){
945 				if(waserror()){
946 					putep(ep);
947 					nexterror();
948 				}
949 				s = seprint(s, se, "ep%d.%d ", ep->dev->nb, ep->nb);
950 				s = seprintep(s, se, ep, 0);
951 				poperror();
952 			}
953 			putep(ep);
954 		}
955 	else{
956 		ep = getep(qid2epidx(q));
957 		if(ep == nil)
958 			error(Eio);
959 		if(waserror()){
960 			putep(ep);
961 			nexterror();
962 		}
963 		if(c->aux != nil){
964 			/* After a new endpoint request we read
965 			 * the new endpoint name back.
966 			 */
967 			strecpy(s, se, c->aux);
968 			free(c->aux);
969 			c->aux = nil;
970 		}else
971 			seprintep(s, se, ep, 0);
972 		poperror();
973 		putep(ep);
974 	}
975 	n = readstr(offset, a, n, us);
976 	poperror();
977 	free(us);
978 	return n;
979 }
980 
981 /*
982  * Fake root hub emulation.
983  */
984 static long
985 rhubread(Ep *ep, void *a, long n)
986 {
987 	char *b;
988 
989 	if(ep->dev->isroot == 0 || ep->nb != 0 || n < 2)
990 		return -1;
991 	if(ep->rhrepl < 0)
992 		return -1;
993 
994 	b = a;
995 	memset(b, 0, n);
996 	PUT2(b, ep->rhrepl);
997 	ep->rhrepl = -1;
998 	return n;
999 }
1000 
1001 static long
1002 rhubwrite(Ep *ep, void *a, long n)
1003 {
1004 	uchar *s;
1005 	int cmd;
1006 	int feature;
1007 	int port;
1008 	Hci *hp;
1009 
1010 	if(ep->dev == nil || ep->dev->isroot == 0 || ep->nb != 0)
1011 		return -1;
1012 	if(n != Rsetuplen)
1013 		error("root hub is a toy hub");
1014 	ep->rhrepl = -1;
1015 	s = a;
1016 	if(s[Rtype] != (Rh2d|Rclass|Rother) && s[Rtype] != (Rd2h|Rclass|Rother))
1017 		error("root hub is a toy hub");
1018 	hp = ep->hp;
1019 	cmd = s[Rreq];
1020 	feature = GET2(s+Rvalue);
1021 	port = GET2(s+Rindex);
1022 	if(port < 1 || port > hp->nports)
1023 		error("bad hub port number");
1024 	switch(feature){
1025 	case Rportenable:
1026 		ep->rhrepl = hp->portenable(hp, port, cmd == Rsetfeature);
1027 		break;
1028 	case Rportreset:
1029 		ep->rhrepl = hp->portreset(hp, port, cmd == Rsetfeature);
1030 		break;
1031 	case Rgetstatus:
1032 		ep->rhrepl = hp->portstatus(hp, port);
1033 		break;
1034 	default:
1035 		ep->rhrepl = 0;
1036 	}
1037 	return n;
1038 }
1039 
1040 static long
1041 usbread(Chan *c, void *a, long n, vlong offset)
1042 {
1043 	int q;
1044 	Ep *ep;
1045 	int nr;
1046 
1047 	q = QID(c->qid);
1048 
1049 	if(c->qid.type == QTDIR)
1050 		return devdirread(c, a, n, nil, 0, usbgen);
1051 
1052 	if(q == Qctl || isqtype(q, Qepctl))
1053 		return ctlread(c, a, n, offset);
1054 
1055 	ep = getep(qid2epidx(q));
1056 	if(ep == nil)
1057 		error(Eio);
1058 	if(waserror()){
1059 		putep(ep);
1060 		nexterror();
1061 	}
1062 	if(ep->dev->state == Ddetach)
1063 		error(Edetach);
1064 	if(ep->mode == OWRITE || ep->inuse == 0)
1065 		error(Ebadusefd);
1066 	switch(ep->ttype){
1067 	case Tnone:
1068 		error("endpoint not configured");
1069 	case Tctl:
1070 		nr = rhubread(ep, a, n);
1071 		if(nr >= 0){
1072 			n = nr;
1073 			break;
1074 		}
1075 		/* else fall */
1076 	default:
1077 		ddeprint("\nusbread q %#x fid %d cnt %ld off %lld\n",q,c->fid,n,offset);
1078 		n = ep->hp->epread(ep, a, n);
1079 		break;
1080 	}
1081 	poperror();
1082 	putep(ep);
1083 	return n;
1084 }
1085 
1086 static long
1087 pow2(int n)
1088 {
1089 	long v;
1090 
1091 	for(v = 1; n > 0; n--)
1092 		v *= 2;
1093 	return v;
1094 }
1095 
1096 static void
1097 setmaxpkt(Ep *ep, char* s)
1098 {
1099 	long spp;	/* samples per packet */
1100 
1101 	if(ep->dev->speed == Highspeed)
1102 		spp = (ep->hz * ep->pollival * ep->ntds + 7999) / 8000;
1103 	else
1104 		spp = (ep->hz * ep->pollival + 999) / 1000;
1105 	ep->maxpkt = spp * ep->samplesz;
1106 	deprint("usb: %s: setmaxpkt: hz %ld poll %ld"
1107 		" ntds %d %s speed -> spp %ld maxpkt %ld\n", s,
1108 		ep->hz, ep->pollival, ep->ntds, spname[ep->dev->speed],
1109 		spp, ep->maxpkt);
1110 	if(ep->maxpkt > 1024){
1111 		print("usb: %s: maxpkt %ld > 1024. truncating\n", s, ep->maxpkt);
1112 		ep->maxpkt = 1024;
1113 	}
1114 }
1115 
1116 /*
1117  * Many endpoint ctls. simply update the portable representation
1118  * of the endpoint. The actual controller driver will look
1119  * at them to setup the endpoints as dictated.
1120  */
1121 static long
1122 epctl(Ep *ep, Chan *c, void *a, long n)
1123 {
1124 	static char *Info = "info ";
1125 	Ep *nep;
1126 	Udev *d;
1127 	int l;
1128 	char *s;
1129 	char *b;
1130 	int tt;
1131 	int i;
1132 	int mode;
1133 	int nb;
1134 	Cmdtab *ct;
1135 	Cmdbuf *cb;
1136 
1137 	d = ep->dev;
1138 
1139 	cb = parsecmd(a, n);
1140 	if(waserror()){
1141 		free(cb);
1142 		nexterror();
1143 	}
1144 	ct = lookupcmd(cb, epctls, nelem(epctls));
1145 	if(ct == nil)
1146 		error(Ebadctl);
1147 	i = ct->index;
1148 	if(i == CMnew || i == CMspeed || i == CMhub || i == CMpreset)
1149 		if(ep != ep->ep0)
1150 			error("allowed only on a setup endpoint");
1151 	if(i != CMclrhalt && i != CMdetach && i != CMdebugep && i != CMname)
1152 		if(ep != ep->ep0 && ep->inuse != 0)
1153 			error("must configure before using");
1154 	switch(i){
1155 	case CMnew:
1156 		deprint("usb epctl %s\n", cb->f[0]);
1157 		nb = strtol(cb->f[1], nil, 0);
1158 		if(nb < 0 || nb >= Ndeveps)
1159 			error("bad endpoint number");
1160 		tt = name2ttype(cb->f[2]);
1161 		if(tt == Tnone)
1162 			error("unknown endpoint type");
1163 		mode = name2mode(cb->f[3]);
1164 		if(mode < 0)
1165 			error("unknown i/o mode");
1166 		newdevep(ep, nb, tt, mode);
1167 		break;
1168 	case CMnewdev:
1169 		deprint("usb epctl %s\n", cb->f[0]);
1170 		if(ep != ep->ep0 || d->ishub == 0)
1171 			error("not a hub setup endpoint");
1172 		l = name2speed(cb->f[1]);
1173 		if(l == Nospeed)
1174 			error("speed must be full|low|high");
1175 		nep = newdev(ep->hp, 0, 0);
1176 		nep->dev->speed = l;
1177 		if(nep->dev->speed  != Lowspeed)
1178 			nep->maxpkt = 64;	/* assume full speed */
1179 		nep->dev->hub = d->nb;
1180 		nep->dev->port = atoi(cb->f[2]);
1181 		/* next read request will read
1182 		 * the name for the new endpoint
1183 		 */
1184 		l = sizeof(up->genbuf);
1185 		snprint(up->genbuf, l, "ep%d.%d", nep->dev->nb, nep->nb);
1186 		kstrdup(&c->aux, up->genbuf);
1187 		break;
1188 	case CMhub:
1189 		deprint("usb epctl %s\n", cb->f[0]);
1190 		d->ishub = 1;
1191 		break;
1192 	case CMspeed:
1193 		l = name2speed(cb->f[1]);
1194 		deprint("usb epctl %s %d\n", cb->f[0], l);
1195 		if(l == Nospeed)
1196 			error("speed must be full|low|high");
1197 		qlock(ep->ep0);
1198 		d->speed = l;
1199 		qunlock(ep->ep0);
1200 		break;
1201 	case CMmaxpkt:
1202 		l = strtoul(cb->f[1], nil, 0);
1203 		deprint("usb epctl %s %d\n", cb->f[0], l);
1204 		if(l < 1 || l > 1024)
1205 			error("maxpkt not in [1:1024]");
1206 		qlock(ep);
1207 		ep->maxpkt = l;
1208 		qunlock(ep);
1209 		break;
1210 	case CMntds:
1211 		l = strtoul(cb->f[1], nil, 0);
1212 		deprint("usb epctl %s %d\n", cb->f[0], l);
1213 		if(l < 1 || l > 3)
1214 			error("ntds not in [1:3]");
1215 		qlock(ep);
1216 		ep->ntds = l;
1217 		qunlock(ep);
1218 		break;
1219 	case CMpollival:
1220 		if(ep->ttype != Tintr && ep->ttype != Tiso)
1221 			error("not an intr or iso endpoint");
1222 		l = strtoul(cb->f[1], nil, 0);
1223 		deprint("usb epctl %s %d\n", cb->f[0], l);
1224 		if(ep->ttype == Tiso ||
1225 		   (ep->ttype == Tintr && ep->dev->speed == Highspeed)){
1226 			if(l < 1 || l > 16)
1227 				error("pollival power not in [1:16]");
1228 			l = pow2(l-1);
1229 		}else
1230 			if(l < 1 || l > 255)
1231 				error("pollival not in [1:255]");
1232 		qlock(ep);
1233 		ep->pollival = l;
1234 		if(ep->ttype == Tiso)
1235 			setmaxpkt(ep, "pollival");
1236 		qunlock(ep);
1237 		break;
1238 	case CMsamplesz:
1239 		if(ep->ttype != Tiso)
1240 			error("not an iso endpoint");
1241 		l = strtoul(cb->f[1], nil, 0);
1242 		deprint("usb epctl %s %d\n", cb->f[0], l);
1243 		if(l <= 0 || l > 8)
1244 			error("samplesz not in [1:8]");
1245 		qlock(ep);
1246 		ep->samplesz = l;
1247 		setmaxpkt(ep, "samplesz");
1248 		qunlock(ep);
1249 		break;
1250 	case CMhz:
1251 		if(ep->ttype != Tiso)
1252 			error("not an iso endpoint");
1253 		l = strtoul(cb->f[1], nil, 0);
1254 		deprint("usb epctl %s %d\n", cb->f[0], l);
1255 		if(l <= 0 || l > 100000)
1256 			error("hz not in [1:100000]");
1257 		qlock(ep);
1258 		ep->hz = l;
1259 		setmaxpkt(ep, "hz");
1260 		qunlock(ep);
1261 		break;
1262 	case CMclrhalt:
1263 		qlock(ep);
1264 		deprint("usb epctl %s\n", cb->f[0]);
1265 		ep->clrhalt = 1;
1266 		qunlock(ep);
1267 		break;
1268 	case CMinfo:
1269 		deprint("usb epctl %s\n", cb->f[0]);
1270 		l = strlen(Info);
1271 		s = a;
1272 		if(n < l+2 || strncmp(Info, s, l) != 0)
1273 			error(Ebadctl);
1274 		if(n > 1024)
1275 			n = 1024;
1276 		b = smalloc(n);
1277 		memmove(b, s+l, n-l);
1278 		b[n-l] = 0;
1279 		if(b[n-l-1] == '\n')
1280 			b[n-l-1] = 0;
1281 		qlock(ep);
1282 		free(ep->info);
1283 		ep->info = b;
1284 		qunlock(ep);
1285 		break;
1286 	case CMaddress:
1287 		deprint("usb epctl %s\n", cb->f[0]);
1288 		ep->dev->state = Denabled;
1289 		break;
1290 	case CMdetach:
1291 		if(ep->dev->isroot != 0)
1292 			error("can't detach a root hub");
1293 		deprint("usb epctl %s ep%d.%d\n",
1294 			cb->f[0], ep->dev->nb, ep->nb);
1295 		ep->dev->state = Ddetach;
1296 		/* Release file system ref. for its endpoints */
1297 		for(i = 0; i < nelem(ep->dev->eps); i++)
1298 			putep(ep->dev->eps[i]);
1299 		break;
1300 	case CMdebugep:
1301 		if(strcmp(cb->f[1], "on") == 0)
1302 			ep->debug = 1;
1303 		else if(strcmp(cb->f[1], "off") == 0)
1304 			ep->debug = 0;
1305 		else
1306 			ep->debug = strtoul(cb->f[1], nil, 0);
1307 		print("usb: ep%d.%d debug %d\n",
1308 			ep->dev->nb, ep->nb, ep->debug);
1309 		break;
1310 	case CMname:
1311 		deprint("usb epctl %s %s\n", cb->f[0], cb->f[1]);
1312 		validname(cb->f[1], 0);
1313 		kstrdup(&ep->name, cb->f[1]);
1314 		break;
1315 	case CMtmout:
1316 		deprint("usb epctl %s\n", cb->f[0]);
1317 		if(ep->ttype == Tiso || ep->ttype == Tctl)
1318 			error("ctl ignored for this endpoint type");
1319 		ep->tmout = strtoul(cb->f[1], nil, 0);
1320 		if(ep->tmout != 0 && ep->tmout < Xfertmout)
1321 			ep->tmout = Xfertmout;
1322 		break;
1323 	case CMpreset:
1324 		deprint("usb epctl %s\n", cb->f[0]);
1325 		if(ep->ttype != Tctl)
1326 			error("not a control endpoint");
1327 		if(ep->dev->state != Denabled)
1328 			error("forbidden on devices not enabled");
1329 		ep->dev->state = Dreset;
1330 		break;
1331 	default:
1332 		panic("usb: unknown epctl %d", ct->index);
1333 	}
1334 	free(cb);
1335 	poperror();
1336 	return n;
1337 }
1338 
1339 static long
1340 usbctl(void *a, long n)
1341 {
1342 	Cmdtab *ct;
1343 	Cmdbuf *cb;
1344 	Ep *ep;
1345 	int i;
1346 
1347 	cb = parsecmd(a, n);
1348 	if(waserror()){
1349 		free(cb);
1350 		nexterror();
1351 	}
1352 	ct = lookupcmd(cb, usbctls, nelem(usbctls));
1353 	dprint("usb ctl %s\n", cb->f[0]);
1354 	switch(ct->index){
1355 	case CMdebug:
1356 		if(strcmp(cb->f[1], "on") == 0)
1357 			debug = 1;
1358 		else if(strcmp(cb->f[1], "off") == 0)
1359 			debug = 0;
1360 		else
1361 			debug = strtol(cb->f[1], nil, 0);
1362 		print("usb: debug %d\n", debug);
1363 		for(i = 0; i < epmax; i++)
1364 			if((ep = getep(i)) != nil){
1365 				ep->hp->debug(ep->hp, debug);
1366 				putep(ep);
1367 			}
1368 		break;
1369 	case CMdump:
1370 		dumpeps();
1371 		break;
1372 	}
1373 	free(cb);
1374 	poperror();
1375 	return n;
1376 }
1377 
1378 static long
1379 ctlwrite(Chan *c, void *a, long n)
1380 {
1381 	int q;
1382 	Ep *ep;
1383 
1384 	q = QID(c->qid);
1385 	if(q == Qctl)
1386 		return usbctl(a, n);
1387 
1388 	ep = getep(qid2epidx(q));
1389 	if(ep == nil)
1390 		error(Eio);
1391 	if(waserror()){
1392 		putep(ep);
1393 		nexterror();
1394 	}
1395 	if(ep->dev->state == Ddetach)
1396 		error(Edetach);
1397 	if(isqtype(q, Qepctl) && c->aux != nil){
1398 		/* Be sure we don't keep a cloned ep name */
1399 		free(c->aux);
1400 		c->aux = nil;
1401 		error("read, not write, expected");
1402 	}
1403 	n = epctl(ep, c, a, n);
1404 	putep(ep);
1405 	poperror();
1406 	return n;
1407 }
1408 
1409 static long
1410 usbwrite(Chan *c, void *a, long n, vlong off)
1411 {
1412 	int q;
1413 	Ep *ep;
1414 	int nr;
1415 
1416 	if(c->qid.type == QTDIR)
1417 		error(Eisdir);
1418 
1419 	q = QID(c->qid);
1420 
1421 	if(q == Qctl || isqtype(q, Qepctl))
1422 		return ctlwrite(c, a, n);
1423 
1424 	ep = getep(qid2epidx(q));
1425 	if(ep == nil)
1426 		error(Eio);
1427 	if(waserror()){
1428 		putep(ep);
1429 		nexterror();
1430 	}
1431 	if(ep->dev->state == Ddetach)
1432 		error(Edetach);
1433 	if(ep->mode == OREAD || ep->inuse == 0)
1434 		error(Ebadusefd);
1435 
1436 	switch(ep->ttype){
1437 	case Tnone:
1438 		error("endpoint not configured");
1439 	case Tctl:
1440 		nr = rhubwrite(ep, a, n);
1441 		if(nr >= 0){
1442 			n = nr;
1443 			break;
1444 		}
1445 		/* else fall */
1446 	default:
1447 		ddeprint("\nusbwrite q %#x fid %d cnt %ld off %lld\n",q, c->fid, n, off);
1448 		ep->hp->epwrite(ep, a, n);
1449 	}
1450 	putep(ep);
1451 	poperror();
1452 	return n;
1453 }
1454 
1455 void
1456 usbshutdown(void)
1457 {
1458 	Hci *hp;
1459 	int i;
1460 
1461 	for(i = 0; i < Nhcis; i++){
1462 		hp = hcis[i];
1463 		if(hp == nil)
1464 			continue;
1465 		if(hp->shutdown == nil)
1466 			print("#u: no shutdown function for %s\n", hp->type);
1467 		else
1468 			hp->shutdown(hp);
1469 	}
1470 }
1471 
1472 Dev usbdevtab = {
1473 	L'u',
1474 	"usb",
1475 
1476 	usbreset,
1477 	usbinit,
1478 	usbshutdown,
1479 	usbattach,
1480 	usbwalk,
1481 	usbstat,
1482 	usbopen,
1483 	devcreate,
1484 	usbclose,
1485 	usbread,
1486 	devbread,
1487 	usbwrite,
1488 	devbwrite,
1489 	devremove,
1490 	devwstat,
1491 };
1492