Browse Source

Plan 9 from Bell Labs 2008-02-13

David du Colombier 12 years ago
parent
commit
dd3db5b03d

+ 15 - 11
dist/replica/_plan9.db

@@ -342,12 +342,12 @@
 386/bin/mug - 775 sys sys 1197732786 176443
 386/bin/mv - 775 sys sys 1168402336 66629
 386/bin/ndb - 20000000775 sys sys 985743147 0
-386/bin/ndb/cs - 775 sys sys 1201753355 149092
+386/bin/ndb/cs - 775 sys sys 1202849452 149183
 386/bin/ndb/csquery - 775 sys sys 1176488418 61652
-386/bin/ndb/dns - 775 sys sys 1201985554 297116
-386/bin/ndb/dnsdebug - 775 sys sys 1201985560 271051
+386/bin/ndb/dns - 775 sys sys 1202849458 297488
+386/bin/ndb/dnsdebug - 775 sys sys 1202849465 271160
 386/bin/ndb/dnsquery - 775 sys sys 1185334136 64350
-386/bin/ndb/dnstcp - 775 sys sys 1201985564 268932
+386/bin/ndb/dnstcp - 775 sys sys 1202849469 269041
 386/bin/ndb/ipquery - 775 sys sys 1178568296 95524
 386/bin/ndb/mkdb - 775 sys sys 1168402340 64211
 386/bin/ndb/mkhash - 775 sys sys 1178568296 83312
@@ -7753,8 +7753,8 @@ sys/man/4/telco - 664 sys sys 1196638944 4359
 sys/man/4/u9fs - 664 sys sys 1196638944 4748
 sys/man/4/upasfs - 664 sys sys 1196638944 6212
 sys/man/4/usb - 664 sys sys 1196638944 4682
-sys/man/4/usbd - 664 sys sys 1196638944 712
-sys/man/4/usbdisk - 664 sys sys 1202256707 3025
+sys/man/4/usbd - 664 sys sys 1202846392 1035
+sys/man/4/usbdisk - 664 sys sys 1202847388 3041
 sys/man/4/vacfs - 664 sys sys 1196638944 1545
 sys/man/4/webcookies - 664 sys sys 1196638944 3525
 sys/man/4/webfs - 664 sys sys 1196638944 6518
@@ -8092,7 +8092,7 @@ sys/src/9/pc/devlpt.c - 664 sys sys 1184469951 4423
 sys/src/9/pc/devpccard.c - 664 sys sys 1184469910 39336
 sys/src/9/pc/devrtc.c - 664 sys sys 1015014515 7167
 sys/src/9/pc/devtv.c - 664 sys sys 1184469620 45423
-sys/src/9/pc/devusb.c - 664 sys sys 1202251756 20662
+sys/src/9/pc/devusb.c - 664 sys sys 1202856294 20831
 sys/src/9/pc/devvga.c - 664 sys sys 1181083003 9334
 sys/src/9/pc/dma.c - 664 sys sys 1142966373 5332
 sys/src/9/pc/ether2000.c - 664 sys sys 1173287379 4901
@@ -8180,7 +8180,7 @@ sys/src/9/pc/uartaxp.i - 664 sys sys 1155243528 111326
 sys/src/9/pc/uarti8250.c - 664 sys sys 1177676872 13957
 sys/src/9/pc/uartisa.c - 664 sys sys 1127126907 1777
 sys/src/9/pc/uartpci.c - 664 sys sys 1190235592 4685
-sys/src/9/pc/usb.h - 664 sys sys 1202251665 4590
+sys/src/9/pc/usb.h - 664 sys sys 1202865355 4630
 sys/src/9/pc/usbohci.c - 664 sys sys 1202323008 47570
 sys/src/9/pc/usbuhci.c - 664 sys sys 1202252076 31839
 sys/src/9/pc/vga.c - 664 sys sys 1131290595 5148
@@ -14269,8 +14269,8 @@ sys/src/cmd/usb/disk - 20000000775 sys sys 1193181450 0
 sys/src/cmd/usb/disk/disk.c - 664 sys sys 1202256113 20245
 sys/src/cmd/usb/disk/mkfile - 664 sys sys 1193181450 359
 sys/src/cmd/usb/lib - 20000000775 sys sys 1091204979 0
-sys/src/cmd/usb/lib/device.c - 664 sys sys 1202255312 3552
-sys/src/cmd/usb/lib/dump.c - 664 sys sys 1202255256 13075
+sys/src/cmd/usb/lib/device.c - 664 sys sys 1202846662 3549
+sys/src/cmd/usb/lib/dump.c - 664 sys sys 1202846653 13581
 sys/src/cmd/usb/lib/fmt.c - 664 sys sys 1091204978 291
 sys/src/cmd/usb/lib/mkfile - 664 sys sys 1091204979 204
 sys/src/cmd/usb/lib/setup.c - 664 sys sys 1202255323 1933
@@ -14288,7 +14288,7 @@ sys/src/cmd/usb/usbd/fns.h - 664 sys sys 1091204979 495
 sys/src/cmd/usb/usbd/hub.c - 664 sys sys 1202255474 4292
 sys/src/cmd/usb/usbd/mkfile - 664 sys sys 1091204979 304
 sys/src/cmd/usb/usbd/setup.c - 664 sys sys 1202255463 1351
-sys/src/cmd/usb/usbd/usbd.c - 664 sys sys 1202255449 5327
+sys/src/cmd/usb/usbd/usbd.c - 664 sys sys 1202846486 5467
 sys/src/cmd/va - 20000000775 sys sys 1039727598 0
 sys/src/cmd/va/a.h - 664 sys sys 1089299166 2915
 sys/src/cmd/va/a.y - 664 sys sys 944961340 7211
@@ -15860,3 +15860,7 @@ usr/glenda/lib/profile - 664 glenda glenda 1105128663 890
 usr/glenda/readme.acme - 664 glenda glenda 1019860628 4753
 usr/glenda/readme.rio - 664 glenda glenda 1019860628 6370
 usr/glenda/tmp - 20000000775 glenda glenda 1018802620 0
+386/bin/usb/disk - 775 sys sys 1202876959 188101
+386/bin/usb/usbaudio - 775 sys sys 1202876962 188937
+386/bin/usb/usbd - 775 sys sys 1202876965 132270
+386/bin/usb/usbmouse - 775 sys sys 1202876967 129429

+ 15 - 15
dist/replica/plan9.db

@@ -342,12 +342,12 @@
 386/bin/mug - 775 sys sys 1197732786 176443
 386/bin/mv - 775 sys sys 1168402336 66629
 386/bin/ndb - 20000000775 sys sys 985743147 0
-386/bin/ndb/cs - 775 sys sys 1201753355 149092
+386/bin/ndb/cs - 775 sys sys 1202849452 149183
 386/bin/ndb/csquery - 775 sys sys 1176488418 61652
-386/bin/ndb/dns - 775 sys sys 1201985554 297116
-386/bin/ndb/dnsdebug - 775 sys sys 1201985560 271051
+386/bin/ndb/dns - 775 sys sys 1202849458 297488
+386/bin/ndb/dnsdebug - 775 sys sys 1202849465 271160
 386/bin/ndb/dnsquery - 775 sys sys 1185334136 64350
-386/bin/ndb/dnstcp - 775 sys sys 1201985564 268932
+386/bin/ndb/dnstcp - 775 sys sys 1202849469 269041
 386/bin/ndb/ipquery - 775 sys sys 1178568296 95524
 386/bin/ndb/mkdb - 775 sys sys 1168402340 64211
 386/bin/ndb/mkhash - 775 sys sys 1178568296 83312
@@ -483,10 +483,10 @@
 386/bin/upas/unspam - 775 sys sys 1064598367 38
 386/bin/upas/vf - 775 sys sys 1181704900 97444
 386/bin/usb - 20000000775 sys sys 1019538890 0
-386/bin/usb/disk - 775 sys sys 1202272000 187347
-386/bin/usb/usbaudio - 775 sys sys 1202272003 188183
-386/bin/usb/usbd - 775 sys sys 1202272005 131144
-386/bin/usb/usbmouse - 775 sys sys 1202760908 128469
+386/bin/usb/disk - 775 sys sys 1202876959 188101
+386/bin/usb/usbaudio - 775 sys sys 1202876962 188937
+386/bin/usb/usbd - 775 sys sys 1202876965 132270
+386/bin/usb/usbmouse - 775 sys sys 1202876967 129429
 386/bin/usb/usbprint - 775 sys sys 1196537245 314
 386/bin/usb/usbprobe - 775 sys sys 1196537245 173
 386/bin/vac - 775 sys sys 1196742539 171330
@@ -7753,8 +7753,8 @@ sys/man/4/telco - 664 sys sys 1196638944 4359
 sys/man/4/u9fs - 664 sys sys 1196638944 4748
 sys/man/4/upasfs - 664 sys sys 1196638944 6212
 sys/man/4/usb - 664 sys sys 1196638944 4682
-sys/man/4/usbd - 664 sys sys 1196638944 712
-sys/man/4/usbdisk - 664 sys sys 1202256707 3025
+sys/man/4/usbd - 664 sys sys 1202846392 1035
+sys/man/4/usbdisk - 664 sys sys 1202847388 3041
 sys/man/4/vacfs - 664 sys sys 1196638944 1545
 sys/man/4/webcookies - 664 sys sys 1196638944 3525
 sys/man/4/webfs - 664 sys sys 1196638944 6518
@@ -8092,7 +8092,7 @@ sys/src/9/pc/devlpt.c - 664 sys sys 1184469951 4423
 sys/src/9/pc/devpccard.c - 664 sys sys 1184469910 39336
 sys/src/9/pc/devrtc.c - 664 sys sys 1015014515 7167
 sys/src/9/pc/devtv.c - 664 sys sys 1184469620 45423
-sys/src/9/pc/devusb.c - 664 sys sys 1202251756 20662
+sys/src/9/pc/devusb.c - 664 sys sys 1202856294 20831
 sys/src/9/pc/devvga.c - 664 sys sys 1181083003 9334
 sys/src/9/pc/dma.c - 664 sys sys 1142966373 5332
 sys/src/9/pc/ether2000.c - 664 sys sys 1173287379 4901
@@ -8180,7 +8180,7 @@ sys/src/9/pc/uartaxp.i - 664 sys sys 1155243528 111326
 sys/src/9/pc/uarti8250.c - 664 sys sys 1177676872 13957
 sys/src/9/pc/uartisa.c - 664 sys sys 1127126907 1777
 sys/src/9/pc/uartpci.c - 664 sys sys 1190235592 4685
-sys/src/9/pc/usb.h - 664 sys sys 1202251665 4590
+sys/src/9/pc/usb.h - 664 sys sys 1202865355 4630
 sys/src/9/pc/usbohci.c - 664 sys sys 1202323008 47570
 sys/src/9/pc/usbuhci.c - 664 sys sys 1202252076 31839
 sys/src/9/pc/vga.c - 664 sys sys 1131290595 5148
@@ -14269,8 +14269,8 @@ sys/src/cmd/usb/disk - 20000000775 sys sys 1193181450 0
 sys/src/cmd/usb/disk/disk.c - 664 sys sys 1202256113 20245
 sys/src/cmd/usb/disk/mkfile - 664 sys sys 1193181450 359
 sys/src/cmd/usb/lib - 20000000775 sys sys 1091204979 0
-sys/src/cmd/usb/lib/device.c - 664 sys sys 1202255312 3552
-sys/src/cmd/usb/lib/dump.c - 664 sys sys 1202255256 13075
+sys/src/cmd/usb/lib/device.c - 664 sys sys 1202846662 3549
+sys/src/cmd/usb/lib/dump.c - 664 sys sys 1202846653 13581
 sys/src/cmd/usb/lib/fmt.c - 664 sys sys 1091204978 291
 sys/src/cmd/usb/lib/mkfile - 664 sys sys 1091204979 204
 sys/src/cmd/usb/lib/setup.c - 664 sys sys 1202255323 1933
@@ -14288,7 +14288,7 @@ sys/src/cmd/usb/usbd/fns.h - 664 sys sys 1091204979 495
 sys/src/cmd/usb/usbd/hub.c - 664 sys sys 1202255474 4292
 sys/src/cmd/usb/usbd/mkfile - 664 sys sys 1091204979 304
 sys/src/cmd/usb/usbd/setup.c - 664 sys sys 1202255463 1351
-sys/src/cmd/usb/usbd/usbd.c - 664 sys sys 1202255449 5327
+sys/src/cmd/usb/usbd/usbd.c - 664 sys sys 1202846486 5467
 sys/src/cmd/va - 20000000775 sys sys 1039727598 0
 sys/src/cmd/va/a.h - 664 sys sys 1089299166 2915
 sys/src/cmd/va/a.y - 664 sys sys 944961340 7211

+ 15 - 0
dist/replica/plan9.log

@@ -18470,3 +18470,18 @@
 1202761804 8 c sys/src/cmd/ndb/dnstcp.c - 664 sys sys 1202761262 7535
 1202761804 9 c sys/src/cmd/ndb/dnudpserver.c - 664 sys sys 1202761274 5828
 1202774403 0 c rc/bin/usbfat: - 775 sys sys 1202773132 818
+1202846405 0 c sys/man/4/usbd - 664 sys sys 1202846392 1035
+1202846405 1 c sys/src/cmd/usb/lib/device.c - 664 sys sys 1202846662 3549
+1202846405 2 c sys/src/cmd/usb/lib/dump.c - 664 sys sys 1202846653 13581
+1202846405 3 c sys/src/cmd/usb/usbd/usbd.c - 664 sys sys 1202846486 5467
+1202848206 0 c sys/man/4/usbdisk - 664 sys sys 1202847388 3041
+1202850004 0 c 386/bin/ndb/cs - 775 sys sys 1202849452 149183
+1202850004 1 c 386/bin/ndb/dns - 775 sys sys 1202849458 297488
+1202850004 2 c 386/bin/ndb/dnsdebug - 775 sys sys 1202849465 271160
+1202850004 3 c 386/bin/ndb/dnstcp - 775 sys sys 1202849469 269041
+1202857204 0 c sys/src/9/pc/devusb.c - 664 sys sys 1202856294 20831
+1202866203 0 c sys/src/9/pc/usb.h - 664 sys sys 1202865355 4630
+1202877003 0 c 386/bin/usb/disk - 775 sys sys 1202876959 188101
+1202877003 1 c 386/bin/usb/usbaudio - 775 sys sys 1202876962 188937
+1202877003 2 c 386/bin/usb/usbd - 775 sys sys 1202876965 132270
+1202877003 3 c 386/bin/usb/usbmouse - 775 sys sys 1202876967 129429

+ 27 - 2
sys/man/4/usbd

@@ -4,7 +4,13 @@ usbd \- Universal Serial Bus daemon
 .SH SYNOPSIS
 .B usbd
 [
-.B -dv
+.B -DfV
+] [
+.B -d
+.I bitmask
+] [
+.B -u
+root-hub-num
 ]
 .SH DESCRIPTION
 .I Usbd
@@ -19,9 +25,28 @@ unconfiguring them when they are pulled out or switched off.
 recognizes the following options:
 .TP
 .B d
+Set USB library debugging option
+.IR bitmask .
+A value of 1 sets
+.BI Dbginfo ,
+2 sets
+.BI Dbgfs ,
+4 sets
+.BI Dbgproc ,
+and 8 sets
+.BI Dbgcontrol ;
+they may be added to set multiple options.
+.TP
+.B D
 Debug; print the bytes in each message sent or received.
 .TP
-.B v
+.B f
+Don't fork.
+.TP
+.B u
+Specifies the controller number of the root hub.
+.TP
+.B V
 Verbose; print configuration information and device status as they change.
 .SH SOURCE
 .B /sys/src/cmd/usb/usbd

+ 1 - 0
sys/man/4/usbdisk

@@ -143,6 +143,7 @@ FAT file system service
 .IR fs (3),
 .IR sd (3),
 .IR usb (3),
+.IR partfs (8),
 .IR prep (8),
 .IR scuzz (8)
 .SH BUGS

+ 17 - 6
sys/src/9/pc/devusb.c

@@ -34,6 +34,7 @@ enum
 	Qtopdir = 0,
 	Q2nd,
 	Qnew,
+	Qdebug,
 	Qport,
 	Q3rd,
 	Qctl,
@@ -68,6 +69,7 @@ enum {
 
 static Dirtab usbdir2[] = {
 	"new",		{Qnew},			0,	0666,
+	"debug",	{Qdebug},		0,	0444,
 	"port",		{Qport},		0,	0666,
 };
 
@@ -689,6 +691,11 @@ usbread(Chan *c, void *a, long n, vlong offset)
 		nexterror();
 	}
 	switch(t){
+	case Qdebug:
+		s[0] = '\0';
+		if(uh->debug)
+			uh->debug(uh, s, se);
+		break;
 	case Qport:
 		uh->portinfo(uh, s, se);
 		break;
@@ -786,7 +793,7 @@ usbwrite(Chan *c, void *a, long n, vlong offset)
 			break;
 		case CMclass:
 			if (cb->nf != 5 && cb->nf != 7)
-				cmderror(cb, Ebadusbmsg);
+				cmderror(cb, Ecmdargs);
 			/*
 			 * class #ifc ept csp
 			 * (== class subclass proto) [vendor product]
@@ -874,6 +881,8 @@ usbwrite(Chan *c, void *a, long n, vlong offset)
 			 * Isomode:	ep n period mode samplesize Hz	OR
 			 * Intrmode:	ep n period mode maxpkt
 			 */
+			if(cb->nf < 3)
+				error(Ecmdargs);
 			i = strtoul(cb->f[1], nil, 0);
 			if(i < 0 || i >= nelem(d->ep)) {
 				XPRINT("field 1: 0 <= %d < %d\n",
@@ -885,17 +894,19 @@ usbwrite(Chan *c, void *a, long n, vlong offset)
 				e = devendpt(d, i, 1);
 			}
 			qlock(uh);
+			if(e->active){
+				qunlock(uh);
+				error(Eperm);
+			}
 			if(waserror()){
 				freept(e);
 				qunlock(uh);
 				nexterror();
 			}
-			if(e->active)
-				error(Eperm);
 			if(strcmp(cb->f[2], "bulk") == 0){
 				/* ep n `bulk' mode maxpkt nbuf */
 				if(cb->nf != 6)
-					error("arg count");
+					error(Ecmdargs);
 				i = strtoul(cb->f[4], nil, 0);
 				if(i < 1 || i > 1023){
 					XPRINT("maxpkt: 1 <= %d < 1024\n", i);
@@ -912,7 +923,7 @@ usbwrite(Chan *c, void *a, long n, vlong offset)
 			}else if(strcmp(cb->f[2], "ctl") == 0){
 				/* ep n `ctl' mode maxpkt nbuf */
 				if(cb->nf != 6)
-					error("arg count");
+					error(Ecmdargs);
 				i = strtoul(cb->f[4], nil, 0);
 				if(i < 8 || i > 1023){
 					XPRINT("maxpkt: 8 <= %d < 1024\n", i);
@@ -973,7 +984,7 @@ usbwrite(Chan *c, void *a, long n, vlong offset)
 					e->samplesz;
 				e->epnewmode = Isomode;
 			}else
-				error("arg count");
+				error(Ecmdargs);
 			e->mode = strcmp(cb->f[3],"r") == 0? OREAD:
 				  strcmp(cb->f[3],"w") == 0? OWRITE: ORDWR;
 			uh->epmode(uh, e);

+ 1 - 0
sys/src/9/pc/usb.h

@@ -188,6 +188,7 @@ struct Usbhost
 	void	(*init)(Usbhost*);
 	void	(*interrupt)(Ureg*, void*);
 
+	void	(*debug)(Usbhost*, char*, char*);
 	void	(*portinfo)(Usbhost*, char*, char*);
 	void	(*portreset)(Usbhost*, int);
 	void	(*portenable)(Usbhost*, int, int);

+ 2 - 2
sys/src/cmd/usb/lib/device.c

@@ -36,7 +36,7 @@ opendev(int ctlrno, int id)
 	if(id == -1) {
 		sprint(name, "/dev/usb%d/new", ctlrno);
 		if((d->ctl = open(name, ORDWR)) < 0){
-		Error0:
+Error0:
 			werrstr("open %s: %r", name);
 			if(d->ctl >= 0)
 				close(d->ctl);
@@ -58,7 +58,7 @@ opendev(int ctlrno, int id)
 
 	strcpy(p, "setup");
 	if((d->setup = open(name, ORDWR)) < 0){
-	Error1:
+Error1:
 		if(d->setup >= 0)
 			close(d->setup);
 		goto Error0;

+ 53 - 33
sys/src/cmd/usb/lib/dump.c

@@ -35,37 +35,34 @@ static Classes	classname[] = {
 static	void	pflag(Flags*, uint);
 
 char *
-sclass(ulong csp)
+sclass(char *p, char *e, ulong csp)
 {
 	Classes *cs;
-	int n;
-	static char buf[64];
-	int c, s, p;
+	int c, s, pr;
 
 	c = Class(csp);
 	s = Subclass(csp);
-	p = Proto(csp);
-	if(c < 0 || c >= nelem(classname) || (cs = &classname[c])->name == nil){
-		sprint(buf, "%d.%d.%d", c, s, p);
-		return buf;
-	}
-	n = sprint(buf, "%s.", cs->name);
+	pr = Proto(csp);
+	if(c < 0 || c >= nelem(classname) || (cs = &classname[c])->name == nil)
+		return seprint(p, e, "%d.%d.%d", c, s, pr);
+	p = seprint(p, e, "%s.", cs->name);
 	if(s < 0 || s >= nelem(cs->subclass) || cs->subclass[s].name == nil)
-		sprint(buf+n, "%d.%d", s, p);
+		p = seprint(p, e, "%d.%d", s, pr);
 	else{
-		n += sprint(buf+n, "%s.", cs->subclass[s].name);
-		if(p < 0 || p >= nelem(cs->subclass[s].proto) || cs->subclass[s].proto[p] == nil)
-			sprint(buf+n, "%d", p);
+		p = seprint(p, e, "%s.", cs->subclass[s].name);
+		if(pr < 0 || pr >= nelem(cs->subclass[s].proto) || cs->subclass[s].proto[pr] == nil)
+			p = seprint(p, e, "%d", pr);
 		else
-			sprint(buf+n, "%s", cs->subclass[s].proto[p]);
+			p = seprint(p, e, "%s", cs->subclass[s].proto[pr]);
 	}
-	return buf;
+	return p;
 }
 
 void
 pdevice(Device *, int, ulong, void *b, int n)
 {
 	DDevice *d;
+	char scbuf[64];
 
 	if(n < DDEVLEN)
 		return;
@@ -74,9 +71,11 @@ pdevice(Device *, int, ulong, void *b, int n)
 		fprint(2, "usb (bcd)%c%c%c%c",
 				'0'+((d->bcdUSB[1]>>4)&0xf), '0'+(d->bcdUSB[1]&0xf),
 				'0'+((d->bcdUSB[0]>>4)&0xf), '0'+(d->bcdUSB[0]&0xf));
+		sclass(scbuf, scbuf + sizeof scbuf,
+			CSP(d->bDeviceClass, d->bDeviceSubClass, d->bDeviceProtocol)),
 		fprint(2, " class %d subclass %d proto %d [%s] max0 %d",
 			d->bDeviceClass, d->bDeviceSubClass, d->bDeviceProtocol,
-			sclass(CSP(d->bDeviceClass, d->bDeviceSubClass, d->bDeviceProtocol)),
+			scbuf,
 			d->bMaxPacketSize0);
 		fprint(2, " vendor %#x product %#x device (bcd)%c%c%c%c",
 			GET2(d->idVendor), GET2(d->idProduct),
@@ -399,16 +398,18 @@ pcs_interface(Device *, int n, ulong, void *bb, int nb)
 void
 pdesc(Device *d, int c, ulong csp, byte *b, int n)
 {
-	int class, subclass, proto, dalt = -1, i, ep, ifc = -1;
+	int class, subclass, proto, dalt, i, ep, ifc, len;
 	DConfig *dc;
 	DEndpoint *de;
 	DInterface *di;
 	Dinf *dif;
 	Endpt *dep;
 	void (*f)(Device *, int, ulong, void*, int);
+	char scbuf[64];
 
 	class = Class(csp);
-
+	dalt = -1;
+	ifc = -1;
 	if (c >= nelem(d->config)) {
 		fprint(2, "Too many interfaces (%d of %d)\n",
 			c, nelem(d->config));
@@ -416,13 +417,17 @@ pdesc(Device *d, int c, ulong csp, byte *b, int n)
 	}
 	if(debug & Dbginfo)
 		fprint(2, "pdesc %d.%d [%d]\n", d->id, c, n);
-	for(; n > 2 && b[0] && b[0] <= n; b += b[0]){
+	len = -1;
+	while(n > 2 && b[0] && b[0] <= n){
 		if (debug & Dbginfo)
 			fprint(2, "desc %d.%d [%d] %#2.2x: ", d->id, c, b[0], b[1]);
 		switch (b[1]) {
 		case CONFIGURATION:
-			if(b[0] < DCONFLEN)
+			if(b[0] < DCONFLEN){
+				if(debug & Dbginfo)
+					fprint(2, "short config %d < %d", b[0], DCONFLEN);
 				return;
+			}
 			dc = (DConfig*)b;
 			d->config[c]->nif = dc->bNumInterfaces;
 			d->config[c]->cval = dc->bConfigurationValue;
@@ -437,19 +442,24 @@ pdesc(Device *d, int c, ulong csp, byte *b, int n)
 					dc->bmAttributes, dc->MaxPower*2);
 			break;
 		case INTERFACE:
-			if(n < DINTERLEN)
+			if(n < DINTERLEN){
+				if(debug & Dbginfo)
+					fprint(2, "short interface %d < %d", b[0], DINTERLEN);
 				return;
+			}
 			di = (DInterface *)b;
 			class = di->bInterfaceClass;
 			subclass = di->bInterfaceSubClass;
 			proto = di->bInterfaceProtocol;
 			csp = CSP(class, subclass, proto);
-			if (debug & Dbginfo)
+			if(debug & Dbginfo){
+				sclass(scbuf, scbuf + sizeof scbuf, csp);
 				fprint(2, "interface %d: alt %d nept %d class %#x subclass %#x proto %d [%s] iinterface %d\n",
 					di->bInterfaceNumber,
 					di->bAlternateSetting,
 					di->bNumEndpoints, class, subclass,
-					proto, sclass(csp), di->iInterface);
+					proto, scbuf, di->iInterface);
+			}
 			if (c < 0) {
 				fprint(2, "Unexpected INTERFACE message\n");
 				return;
@@ -541,9 +551,6 @@ pdesc(Device *d, int c, ulong csp, byte *b, int n)
 			}else if ((dep->addr&0x80) !=
 			    (de->bEndpointAddress&0x80))
 				dep->dir = Eboth;
-			else if(debug)
-				fprint(2, "%s: endpoint %d already in use!\n",
-					argv0, ep); // DEBUG
 			dep->maxpkt = GET2(de->wMaxPacketSize);
 			dep->addr = de->bEndpointAddress;
 			dep->type = de->bmAttributes & 0x03;
@@ -564,23 +571,36 @@ pdesc(Device *d, int c, ulong csp, byte *b, int n)
 		default:
 			assert(nelem(dprinter) == 0x100);
 			f = dprinter[b[1]];
-			if(f != nil) {
+			if(f != nil){
 				(*f)(d, c, dalt<<24 | ifc<<16 | (csp&0xffff),
 					b, b[0]);
 				if(debug & Dbginfo)
 					fprint(2, "\n");
-			} else
-				if (verbose) {
+			}else
+				if(verbose){
 					int i;
 
-					fprint(2, "(unknown type)");
+					switch(b[1]){
+					case DEVICE:
+						fprint(2, "(device)");
+						break;
+					case STRING:
+						fprint(2, "(string)");
+						break;
+					default:
+						fprint(2, "(unknown type)");
+					}
 					for(i=1; i<b[0]; i++)
 						fprint(2, " %.2x", b[i]);
 					fprint(2, "\n");
-				} else if(debug & Dbginfo)
+				}else if(debug & Dbginfo)
 					fprint(2, "\n");
 			break;
 		}
-		n -= b[0];
+		len = b[0];
+		n -= len;
+		b += len;
 	}
+	if(n)
+		fprint(2, "pdesc: %d bytes left unparsed, b[0]=%d, b[-len]=%d, len=%d\n", n, b[0], b[-len], len);	
 }

+ 32 - 25
sys/src/cmd/usb/usbd/usbd.c

@@ -30,7 +30,7 @@ void (*dprinter[])(Device *, int, ulong, void *b, int n) = {
 static void
 usage(void)
 {
-	fprint(2, "usage: usbd\n");
+	fprint(2, "usage: usbd [-DfV] [-d mask] [-u root-hub]\n");
 	threadexitsall("usage");
 }
 
@@ -42,7 +42,7 @@ work(void *a)
 	Enum *arg;
 
 	hub = a;
-	for (port = 1; port <= hub->nport; port++) {
+	for(port = 1; port <= hub->nport; port++){
 		if (debug)
 			fprint(2, "enumerate port %H.%d\n", hub, port);
 		arg = emallocz(sizeof(Enum), 1);
@@ -65,37 +65,44 @@ threadmain(int argc, char **argv)
 {
 	int i;
 	Hub *h;
+	int usbnum;
 
+	usbnum = -1;
 	ARGBEGIN{
-	case 'f':
-		dontfork=1;
+	case 'd':
+		debug = atoi(EARGF(usage()));
 		break;
 	case 'D':
 		debugdebug++;
 		break;
-	case 'd':
-		debug = atoi(EARGF(usage()));
+	case 'f':
+		dontfork = 1;
+		break;
+	case 'u':
+		usbnum = atoi(EARGF(usage()));
 		break;
 	case 'V':
 		verbose = 1;
 		break;
 	}ARGEND
-	if (argc)
+	if(argc)
 		usage();
-	if (access("/dev/usb0", 0) < 0 && bind("#U", "/dev", MBEFORE) < 0)
+	if(access("/dev/usb0", 0) < 0 && bind("#U", "/dev", MBEFORE) < 0)
 		sysfatal("%s: can't bind -b #U /dev: %r\n", argv0);
 
 	usbfmtinit();
 	fmtinstall('H', Hfmt);
 
-	/* always fork off usb[1—n] */
-	for(i=1; (h = roothub(i)) != nil; i++) {
-		incref(&busy);
-		proccreate(work, h, STACKSIZE);
+	if(usbnum < 0){
+		/* always fork off usb[1—n] */
+		for(i=1; (h = roothub(i)) != nil; i++) {
+			incref(&busy);
+			proccreate(work, h, STACKSIZE);
+		}
+		usbnum = 0;
 	}
-
 	/* usb0 might be handled in this proc */
-	if((h = roothub(0)) != nil){
+	if((h = roothub(usbnum)) != nil){
 		incref(&busy);
 		if (dontfork) {
 			work(h);
@@ -129,30 +136,30 @@ enumerate(void *v)
 	port = arg->port;
 	free(arg);
 
-	for (;;) {
-		if((portstatus(h, port) & (1<<PORT_CONNECTION)) == 0) {
+	for(;;){
+		if((portstatus(h, port) & (1<<PORT_CONNECTION)) == 0){
 			decref(&busy);
-			if (verbose)
+			if(verbose)
 				fprint(2, "usbd: %H: port %d empty\n", h, port);
-			do {
+			do{
 				yield();
-				if (debugdebug)
+				if(debugdebug)
 					fprint(2, "usbd: probing %H.%d\n",
 						h, port);
 				sleep(500);
-			} while((portstatus(h, port) & (1<<PORT_CONNECTION)) == 0);
+			}while((portstatus(h, port) & (1<<PORT_CONNECTION)) == 0);
 			incref(&busy);
 		}
 		if(verbose)
 			fprint(2, "usbd: %H: port %d attached\n", h, port);
 		d = configure(h, port);
-		if(d == nil) {
+		if(d == nil){
 			if(verbose)
 				fprint(2, "usbd: can't configure port %H.%d\n", h, port);
 			decref(&busy);
 			threadexits("configure");
 		}
-		if(d->class == Hubclass) {
+		if(d->class == Hubclass){
 			if(debug)
 				fprint(2, "usbd: %H.%d: hub %d attached\n",
 					h, port, d->id);
@@ -235,7 +242,7 @@ configure(Hub *h, int port)
 	maxpkt = getmaxpkt(h->dev0);
 	if(debugdebug)
 		fprint(2, "%H.%d maxpkt: %d\n", h, port, maxpkt);
-	if(maxpkt < 0) {
+	if(maxpkt < 0){
 Error0:
 		portenable(h, port, 0);
 		return nil;
@@ -244,7 +251,7 @@ Error0:
 	d->ls = ls;
 	d->state = Enabled;
 	d->ep[0]->maxpkt = maxpkt;
-	if(fprint(d->ctl, "maxpkt 0 %d", maxpkt) < 0) {
+	if(fprint(d->ctl, "maxpkt 0 %d", maxpkt) < 0){
 Error1:
 		closedev(d);
 		goto Error0;
@@ -257,7 +264,7 @@ Error1:
 		goto Error1;
 
 	/* read configurations 0 to n */
-	for(i=0; i<d->nconf; i++) {
+	for(i=0; i<d->nconf; i++){
 		if(d->config[i] == nil)
 			d->config[i] = mallocz(sizeof(*d->config[i]),1);
 		loadconfig(d, i);