Browse Source

Plan 9 from Bell Labs 2009-05-27

David du Colombier 10 years ago
parent
commit
3a65aae13f
88 changed files with 20576 additions and 8672 deletions
  1. 0 12
      386/bin/usb/print
  2. 18 9
      386/bin/usb/probe
  3. 59 0
      386/bin/usb/usbfat:
  4. 0 13
      386/bin/usb/usbprint
  5. 0 9
      386/bin/usb/usbprobe
  6. 1 1
      rc/bin/termrc
  7. 42 25
      rc/bin/usbfat:
  8. 11 2
      rc/bin/usbstart
  9. 458 0
      sys/man/2/usb
  10. 341 0
      sys/man/2/usbfs
  11. 471 213
      sys/man/3/usb
  12. 204 150
      sys/man/4/usb
  13. 229 39
      sys/man/4/usbd
  14. 2 11
      sys/src/9/boot/boot.c
  15. 1215 817
      sys/src/9/pc/devusb.c
  16. 4 2
      sys/src/9/pc/pc
  17. 10 2
      sys/src/9/pc/pcauth
  18. 4 4
      sys/src/9/pc/pccd
  19. 6 2
      sys/src/9/pc/pccpu
  20. 4 0
      sys/src/9/pc/pccpuf
  21. 5 2
      sys/src/9/pc/pcdisk
  22. 5 1
      sys/src/9/pc/pcf
  23. 8 3
      sys/src/9/pc/pcfl
  24. 11 2
      sys/src/9/pc/pcflop
  25. 155 183
      sys/src/9/pc/usb.h
  26. 3456 0
      sys/src/9/pc/usbehci.c
  27. 2092 1854
      sys/src/9/pc/usbohci.c
  28. 1992 1263
      sys/src/9/pc/usbuhci.c
  29. 2 0
      sys/src/boot/pc/dat.h
  30. 2 1
      sys/src/boot/pc/devbios.c
  31. 149 173
      sys/src/cmd/usb/audio/usbaudio.c
  32. 14 4
      sys/src/cmd/usb/audio/usbaudio.h
  33. 205 231
      sys/src/cmd/usb/audio/usbaudioctl.c
  34. 0 0
      sys/src/cmd/usb/audio/audioctl.h
  35. 106 148
      sys/src/cmd/usb/audio/audiofs.c
  36. 134 107
      sys/src/cmd/usb/audio/audiosub.c
  37. 5 5
      sys/src/cmd/usb/audio/mkfile
  38. 481 788
      sys/src/cmd/usb/disk/disk.c
  39. 70 0
      sys/src/cmd/usb/disk/main.c
  40. 30 10
      sys/src/cmd/usb/disk/mkfile
  41. 32 0
      sys/src/cmd/usb/disk/mkscsierrs
  42. 949 0
      sys/src/cmd/usb/disk/scsireq.c
  43. 223 0
      sys/src/cmd/usb/disk/scsireq.h
  44. 96 0
      sys/src/cmd/usb/disk/ums.h
  45. 484 0
      sys/src/cmd/usb/ether/asix.c
  46. 60 0
      sys/src/cmd/usb/ether/cdc.c
  47. 993 0
      sys/src/cmd/usb/ether/clether.c
  48. 1139 0
      sys/src/cmd/usb/ether/ether.c
  49. 115 0
      sys/src/cmd/usb/ether/ether.h
  50. 89 0
      sys/src/cmd/usb/ether/main.c
  51. 25 0
      sys/src/cmd/usb/ether/mkfile
  52. 30 27
      sys/src/cmd/usb/kb/hid.h
  53. 301 370
      sys/src/cmd/usb/kb/kb.c
  54. 64 0
      sys/src/cmd/usb/kb/main.c
  55. 13 9
      sys/src/cmd/usb/kb/mkfile
  56. 471 0
      sys/src/cmd/usb/lib/dev.c
  57. 0 185
      sys/src/cmd/usb/lib/device.c
  58. 167 0
      sys/src/cmd/usb/lib/devs.c
  59. 136 564
      sys/src/cmd/usb/lib/dump.c
  60. 0 21
      sys/src/cmd/usb/lib/fmt.c
  61. 672 0
      sys/src/cmd/usb/lib/fs.c
  62. 418 0
      sys/src/cmd/usb/lib/fsdir.c
  63. 11 4
      sys/src/cmd/usb/lib/mkfile
  64. 269 0
      sys/src/cmd/usb/lib/parse.c
  65. 0 101
      sys/src/cmd/usb/lib/setup.c
  66. 269 305
      sys/src/cmd/usb/lib/usb.h
  67. 61 0
      sys/src/cmd/usb/lib/usbfs.h
  68. 0 41
      sys/src/cmd/usb/lib/util.c
  69. 0 32
      sys/src/cmd/usb/misc/mkfile
  70. 0 12
      sys/src/cmd/usb/misc/print
  71. 0 12
      sys/src/cmd/usb/misc/probe
  72. 0 273
      sys/src/cmd/usb/misc/usbmouse.c
  73. 15 3
      sys/src/cmd/usb/mkfile
  74. 47 0
      sys/src/cmd/usb/print/main.c
  75. 22 0
      sys/src/cmd/usb/print/mkfile
  76. 86 0
      sys/src/cmd/usb/print/print.c
  77. 21 0
      sys/src/cmd/usb/probe
  78. 0 30
      sys/src/cmd/usb/usbd/dat.h
  79. 252 0
      sys/src/cmd/usb/usbd/dev.c
  80. 0 21
      sys/src/cmd/usb/usbd/fns.h
  81. 0 229
      sys/src/cmd/usb/usbd/hub.c
  82. 113 0
      sys/src/cmd/usb/usbd/mkdev
  83. 15 6
      sys/src/cmd/usb/usbd/mkfile
  84. 0 63
      sys/src/cmd/usb/usbd/setup.c
  85. 725 278
      sys/src/cmd/usb/usbd/usbd.c
  86. 127 0
      sys/src/cmd/usb/usbd/usbd.h
  87. 6 0
      sys/src/cmd/usb/usbd/usbdb
  88. 59 0
      sys/src/cmd/usb/usbfat:

+ 0 - 12
386/bin/usb/print

@@ -1,12 +0,0 @@
-#!/bin/rc
-# usbprint - bind usb printer endpoint to /dev/lp
-rfork e
-for (id in /dev/usb[0-9]*/[0-9]*)
-	if (grep -s 'Enabled 0x020107' $id/status >[2]/dev/null){
-		echo -n 'ep 2 bulk w 64 32' >$id/ctl
-		aux/stub /dev/lp
-		bind $id/ep2data /dev/lp
-		exit ''
-	}
-echo $0: no usb printer found >[1=2]
-exit 'no printer'

+ 18 - 9
386/bin/usb/probe

@@ -1,12 +1,21 @@
 #!/bin/rc
-# list all usb devices
 rfork e
-if(! test -r '#U'/usb0)
-	exit no-usb
-if(! test -r /dev/usb0)
-	bind -a '#U' /dev
-for (id in /dev/usb[0-9]*/[0-9]*/status)
-	if (test -e $id) {
-		echo $id | sed 's;/status$;:	;' | tr -d '\12'
-		grep '^[A-Z]' $id
+test -e /dev/usb || bind -a '#u' /dev || {
+	echo no '#u/usb' >[1=2]
+	exit nousb
+}
+
+awk 'BEGIN{ep="";}
+	$1 ~ /ep[0-9]+\.0/ && $2 == "enabled" && $NF ~ /busy|idle/ {
+		ep=$1;
+		next;
 	}
+	{
+		if(ep != ""){
+			printf("%s %s\n", ep, $0);
+			ep="";
+		}
+	}
+' /dev/usb/ctl
+
+exit ''

+ 59 - 0
386/bin/usb/usbfat:

@@ -0,0 +1,59 @@
+#!/bin/rc
+# usbfat: [disk [mtpt]] - mount a USB disk's MS FAT file system
+rfork e
+disk = ()
+mtpt = /n/usb
+
+test -e /dev/usb || bind -a '#u' /dev || {
+	echo no '#u/usb' >[1=2]
+	exit nousb
+}
+test -e /dev/usbdctl || mount -a /srv/usb /dev || {
+	echo cannot mount /srv/usb >[1=2]
+	exit nousbd
+}
+
+disks=()
+mtpt=()
+switch ($#*) {
+case 0
+	;
+case 1
+	disks = $1
+case 2
+	disks = $1
+	mtpt = $2
+case *
+	echo usage: $0 ' [disk [mtpt]]' >[1=2]
+	exit usage
+}
+
+if (~ $#disks 0){
+	if(! test -e /dev/sdU*/data){
+		echo no usb disks >[1=2]
+		exit nodisk
+	}
+	disks = `{echo /dev/sdU*/data}
+}
+for(d in $disks){
+	if(~ $d sdU*.[0-9]*)
+		d=/dev/$d/data
+	if(test -e $d){
+		name=`{echo $d | sed 's/.*(sdU[0-9]+\.[0-9]+).*/\1/'}
+		if(~ $#mtpt 0)
+			mnt=/n/$name
+		if not
+			mnt=$mtpt
+		# don't mount it if it seems to be already mounted.
+		if(! test -e $mnt/*)
+		if(grep -s geometry /dev/$name/ctl){
+			blk = `{disk/fdisk -p $d | awk '/^part dos / {print $3}'}
+			if (! ~ $#blk 0 &&  ~ $blk [0-9]*)
+				d=$d:$blk
+			mount -c <{dossrv -sf $d >[2]/dev/null} $mnt && echo $mnt
+		}
+	}
+	if not
+		echo $d does not exist
+}
+exit ''

+ 0 - 13
386/bin/usb/usbprint

@@ -1,13 +0,0 @@
-#!/bin/rc
-# usbprint - bind usb printer endpoint to /dev/lp
-rfork e
-echo warning: use usb/print instead of usb/usbprint >[1=2]
-for (id in /dev/usb[0-9]*/[0-9]*)
-	if (grep -s 'Enabled 0x020107' $id/status >[2]/dev/null){
-		echo -n 'ep 2 bulk w 64 32' >$id/ctl
-		aux/stub /dev/lp
-		bind $id/ep2data /dev/lp
-		exit ''
-	}
-echo $0: no usb printer found >[1=2]
-exit 'no printer'

+ 0 - 9
386/bin/usb/usbprobe

@@ -1,9 +0,0 @@
-#!/bin/rc
-# list all usb devices
-rfork e
-echo warning: use usb/probe instead of usb/usbprobe >[1=2]
-for (id in /dev/usb[0-9]*/[0-9]*/status)
-	if (test -e $id) {
-		echo $id | sed 's;/status$;:	;' | tr -d '\12'
-		grep '^[A-Z]' $id
-	}

+ 1 - 1
rc/bin/termrc

@@ -6,7 +6,7 @@ NDBFILE=/lib/ndb/local
 mntgen -s slashn && chmod 666 /srv/slashn
 
 # bind all likely devices (#S was bound in boot)
-for(i in f t m v L P U '$' Σ κ)
+for(i in f t m v L P u U '$' Σ κ)
 	/bin/bind -a '#'^$i /dev >/dev/null >[2=1]
 
 # set up any partitions

+ 42 - 25
rc/bin/usbfat:

@@ -1,42 +1,59 @@
 #!/bin/rc
-# usbfat: [-fl] [disk [mtpt]] - mount a USB disk's MS FAT file system
+# usbfat: [disk [mtpt]] - mount a USB disk's MS FAT file system
 rfork e
-opts=()
-while (! ~ $#* 0 && ~ $1 -*) {
-	switch ($1) {
-	case -f -l -lf -fl
-		opts=($opts $1)
-	case -*
-		echo usage: $0 '[-fl] [disk [mtpt]]' >[1=2]
-		exit usage
-	}
-	shift
-}
-disk = /n/disk/0/data
+disk = ()
 mtpt = /n/usb
 
+test -e /dev/usb || bind -a '#u' /dev || {
+	echo no '#u/usb' >[1=2]
+	exit nousb
+}
+test -e /dev/usbdctl || mount -a /srv/usb /dev || {
+	echo cannot mount /srv/usb >[1=2]
+	exit nousbd
+}
+
+disks=()
+mtpt=()
 switch ($#*) {
 case 0
 	;
 case 1
-	disk = $1
+	disks = $1
 case 2
-	disk = $1
+	disks = $1
 	mtpt = $2
 case *
-	echo usage: $0 '[-fl] [disk [mtpt]]' >[1=2]
+	echo usage: $0 ' [disk [mtpt]]' >[1=2]
 	exit usage
 }
 
-if (! test -f /srv/usbfat.$user) {
-	if (! test -e $disk)
-		usb/disk $opts || exit 'no disk'
-	blk = `{disk/fdisk -p $disk | awk '/^part dos / {print $3}'}
-	if (~ $#blk 0 || ! ~ $blk [0-9]*) {
-		echo $0: warning: no fdisk dos partition found... >[1=2]
-		dossrv -f $disk usbfat.$user || exit dossrv
+if (~ $#disks 0){
+	if(! test -e /dev/sdU*/data){
+		echo no usb disks >[1=2]
+		exit nodisk
+	}
+	disks = `{echo /dev/sdU*/data}
+}
+for(d in $disks){
+	if(~ $d sdU*.[0-9]*)
+		d=/dev/$d/data
+	if(test -e $d){
+		name=`{echo $d | sed 's/.*(sdU[0-9]+\.[0-9]+).*/\1/'}
+		if(~ $#mtpt 0)
+			mnt=/n/$name
+		if not
+			mnt=$mtpt
+		# don't mount it if it seems to be already mounted.
+		if(! test -e $mnt/*)
+		if(grep -s geometry /dev/$name/ctl){
+			blk = `{disk/fdisk -p $d | awk '/^part dos / {print $3}'}
+			if (! ~ $#blk 0 &&  ~ $blk [0-9]*)
+				d=$d:$blk
+			mount -c <{dossrv -sf $d >[2]/dev/null} $mnt && echo $mnt
+		}
 	}
 	if not
-		dossrv -f $disk:$blk usbfat.$user || exit dossrv
+		echo $d does not exist
 }
-mount -c /srv/usbfat.$user $mtpt
+exit ''

+ 11 - 2
rc/bin/usbstart

@@ -1,15 +1,24 @@
 #!/bin/rc
+# usbstart - start appropriate usb flavour
+if(test -r '#u'/usb) {
+	if(! test -r /dev/usb)
+		bind -a '#u' /dev
 
-if(test -r '#U'/usb0) {
+	# /boot/boot may have started usbd, which starts all usb drivers
+	if (! ps | grep -s ' usbd$')
+		usb/usbd
+}
+if not if(test -r '#U'/usb0) {
 	if(! test -r /dev/usb0)
 		bind -a '#U' /dev
+
 	# /boot/boot may have started usbd, usb/kb or usb/disk
 	if (! ps | grep -s ' usbd$')
 		usb/usbd
 	usb/usbmouse -a 2
 	if (! ps | grep -s ' kb$')
 		usb/kb -k
-	usb/usbaudio -s usbaudio.$sysname # -V
+	usb/usbaudio -s usbaudio.$sysname -V
 	# usb/print
 }
 exit ''

+ 458 - 0
sys/man/2/usb

@@ -0,0 +1,458 @@
+.TH USB 2
+.SH NAME
+usbcmd,
+classname,
+closedev,
+configdev,
+devctl,
+finddevs,
+loaddevstr,
+matchdevcsp,
+opendev,
+opendevdata,
+openep,
+startdevs,
+unstall,
+class,
+subclass,
+proto,
+CSP \- USB device driver library
+.SH SYNOPSIS
+.EX
+.ta 8n +8n +8n +8n +8n +8n +8n
+#include <u.h>
+#include <libc.h>
+#include <thread.h>
+#include "../lib/usb.h"
+.sp 0.3v
+struct Dev {
+	Ref;
+	char*	dir;		/* path for the endpoint dir */
+	int	id;		/* usb id for device or ep. number */
+	int	dfd;		/* descriptor for the data file */
+	int	cfd;		/* descriptor for the control file */
+	int	maxpkt;		/* cached from usb description */
+	Usbdev*	usb;		/* USB description */
+	void*	aux;		/* for the device driver */
+	void	(*free)(void*);	/* idem. to release aux */
+};
+.sp 0.3v
+struct Usbdev {
+	ulong	csp;		/* USB class/subclass/proto */
+	int	vid;		/* vendor id */
+	int	did;		/* product (device) id */
+	char*	vendor;
+	char*	product;
+	char*	serial;
+	int	ls;		/* low speed */
+	int	class;		/* from descriptor */
+	int	nconf;		/* from descriptor */
+	Conf*	conf[Nconf];	/* configurations */
+	Ep*	ep[Nep];	/* all endpoints in device */
+	Desc*	ddesc[Nddesc];	/* (raw) device specific descriptors */
+};
+.sp 0.3v
+struct Ep {
+	uchar	addr;		/* endpt address */
+	uchar	dir;		/* direction, Ein/Eout */
+	uchar	type;		/* Econtrol, Eiso, Ebulk, Eintr */
+	uchar	isotype;	/* Eunknown, Easync, Eadapt, Esync */
+	int	id;
+	int	maxpkt;		/* max. packet size */
+	Conf*	conf;		/* the endpoint belongs to */
+	Iface*	iface;		/* the endpoint belongs to */
+};
+.sp 0.3v
+struct Altc {
+	int	attrib;
+	int	interval;
+	void*	aux;		/* for the driver program */
+};
+.sp 0.3v
+struct Iface {
+	int 	id;		/* interface number */
+	ulong	csp;		/* USB class/subclass/proto */
+	Altc*	altc[Naltc];
+	Ep*	ep[Nep];
+	void*	aux;		/* for the driver program */
+};
+.sp 0.3v
+struct Conf {
+	int	cval;		/* value for set configuration */
+	int	attrib;
+	int	milliamps;	/* maximum power in this config. */
+	Iface*	iface[Niface];	/* up to 16 interfaces */
+};
+.sp 0.3v
+struct Desc {
+	Conf*	conf;		/* where this descriptor was read */
+	Iface*	iface;		/* last iface before desc in conf. */
+	Ep*	ep;		/* last endpt before desc in conf. */
+	Altc*	altc;		/* last alt.c. before desc in conf. */
+	DDesc	data;		/* unparsed standard USB descriptor */
+};
+.sp 0.3v
+struct DDesc {
+	uchar	bLength;
+	uchar	bDescriptorType;
+	uchar	bbytes[1];
+	/* extra bytes allocated here to keep the rest of it */
+};
+.sp 0.3v
+#define Class(csp)	((csp)&0xff)
+#define Subclass(csp)	(((csp)>>8)&0xff)
+#define Proto(csp)	(((csp)>>16)&0xff)
+#define CSP(c, s, p)	((c) | ((s)<<8) | ((p)<<16))
+#define	GET2(p)		...
+#define	PUT2(p,v)	...
+#define	GET4(p)		...
+#define	PUT4(p,v)	...
+#define dprint	 if(usbdebug)fprint
+#define ddprint if(usbdebug > 1)fprint
+.sp 0.3v
+int	Ufmt(Fmt *f);
+char*	classname(int c);
+void	closedev(Dev *d);
+int	configdev(Dev *d);
+int	devctl(Dev *dev, char *fmt, ...);
+void*	emallocz(ulong size, int zero);
+char*	estrdup(char *s);
+int	finddevs(int (*matchf)(char*,void*), void *farg, char** dirs, int ndirs);
+char*	hexstr(void *a, int n);
+char*	loaddevstr(Dev *d, int sid);
+int	matchdevcsp(char *info, void *a);
+Dev*	opendev(char *fn);
+int	opendevdata(Dev *d, int mode);
+Dev*	openep(Dev *d, int id);
+void	startdevs(char *args, char *argv[], int argc,
+		int (*mf)(char*,void*), void*ma, int (*df)(Dev*,int,char**));
+int	unstall(Dev *dev, Dev *ep, int dir);
+int	usbcmd(Dev *d, int type, int req,
+		int value, int index, uchar *data, int count);
+.sp 0.3v
+extern int usbdebug;	/* more messages for bigger values */
+.EE
+.SH DESCRIPTION
+This library provides convenience structures and functions to write
+USB device drivers.
+It is not intended for user programs using USB devices.
+See
+.IR usb (3)
+for a description of the interfaces provided for that purpose.
+For drivers that provide a file system and may be embedded into
+.IR usbd ,
+the library includes a file system implementation toolkit described in
+.IR usbfs (2).
+.PP
+Usb drivers rely on
+.IR usb (3)
+to perform I/O through USB as well as on
+.IR usbd (4)
+to perform the initial configuration for the device's setup endpoint.
+The rest of the work is up to the driver and is where this library may help.
+.PP
+In most cases, a driver locates the devices of interest and configures them
+by calling
+.I startdevs
+and
+then sets up additional endpoints as needed (by calling
+.IR openep )
+to finally perform I/O by reading and writing the
+data files for the endpoints.
+.PP
+An endpoint as provided by
+.IR usb (3)
+is represented by a
+.B Dev
+data structure.
+The setup endpoint for a
+device represents the USB device, because it is the means to
+configure and operate the device.
+This structure is reference counted.
+Functions creating
+.B Devs
+adjust the number of references to one, initially.
+The driver is free to call
+.IR incref
+(in
+.IR lock (2))
+to add references and
+.I closedev
+to drop references (and release resources when the last one vanishes).
+As an aid to the driver, the field
+.B aux
+may keep driver-specific data and the function
+.B free
+will be called (if not null) to release the
+.B aux
+structure when the reference count goes down to zero.
+.PP
+.I Dev.dir
+holds the path for the endpoint's directory.
+.PP
+The field
+.B id
+keeps the device number for setup endpoints and the endpoint number
+for all other endpoints.
+For example, it would be
+.B 3
+for
+.B /dev/usb/ep3.0
+and
+.B 1
+for
+.BR /dev/usb/ep3.1 .
+It is easy to remember this because the former is created to operate
+on the device, while the later has been created as a particular endpoint
+to perform I/O.
+.PP
+Fields
+.B dfd
+and
+.B cfd
+keep the data and
+control file descriptors, respectively.
+When a
+.B Dev
+is created the control file is open, initially.
+Opening the data
+file requires calling
+.I opendevdata
+with the appropriate mode.
+.PP
+When the device configuration information has been loaded (see below),
+.B maxpkt
+holds the maximum packet size (in bytes) for the endpoint and
+.B usb
+keeps the rest of the USB information.
+.PP
+Most of the information in
+.B usb
+comes from parsing
+various device and configuration descriptors provided by the device,
+by calling one of the functions described later.
+Only descriptors unknown
+to the library are kept unparsed at
+.B usb.ddesc
+as an aid for the driver
+(which should know how to parse them and what to do with the information).
+.SS Configuration
+.I Startdevs
+is a wrapper that locates devices of interest, loads their configuration
+information, and starts a
+.IR thread (2)'s
+.I proc
+for each device located so that it executes
+.I f
+as its main entry point. The entry point is called with a pointer to
+the
+.B Dev
+for the device it has to process,
+.BR argc ,
+and
+.BR argv .
+Devices are located either from the arguments (after options) in
+.IR argv ,
+if any,
+or by calling the helper function
+.I mf
+with the argument
+.I ma
+to determine (for each device available) if the device belongs to
+the driver or not. If the function returns -1 then the device is not for us.
+.PP
+In many cases,
+.I matchdevcsp
+may be supplied as
+.I mf
+along with a (null terminated) vector of CSP values supplied as
+.IR ma .
+This function returns 0 for any device with a CSP matching one in the
+vector supplied as an argument and -1 otherwise.
+In other cases (eg., when a particular vendor and device ids are the
+ones identifying the device) the driver must include its own function
+and supply it as an argument to
+.IR startdevs .
+The first argument of the function corresponds to the information
+known about the device (the second line in its
+.B ctl
+file).
+.I Openep
+creates the endpoint number
+.I id
+for the device
+.I d
+and returns a
+.B Dev
+structure to operate on it (with just the control file open).
+.PP
+.I Opendev
+creates a
+.B Dev
+for the endpoint with directory
+.IR fn .
+Usually, the endpoint is a setup endpoint representing a device. The endpoint
+control file is open, but the data file is not. The USB description is void.
+In most cases drivers call
+.I startdevs
+and
+.I openep
+and do not call this function directly.
+.PP
+.I Configdev
+opens the data file for the device supplied and
+loads and parses its configuration information.
+After calling it, the device is ready for I/O and the USB description in
+.B Dev.usb
+is valid.
+When using
+.IR startdevs
+it is not desirable to call this function (because
+.IR startdevs
+already calls it).
+.PP
+Control requests for an endpoint may be written by calling
+.I devctl
+in the style of
+.IR print (2).
+It is better not to call
+.I print
+directly because the control request should be issued as a single
+.IR write (2).
+See
+.IR usb (3)
+for a list of available control requests (not to be confused with
+USB control transfers performed on a control endpoint).
+.SS Input/Output
+.I Opendevdata
+opens the data file for the device according to the given
+.IR mode .
+The mode must match that of the endpoint, doing otherwise is considered
+an error.
+Actual I/O is performed by reading/writing the descriptor kept in the
+.B dfd
+field of
+.BR Dev .
+.PP
+For control endpoints,
+it is not necessary to call
+.I read
+and
+.I write
+directly.
+Instead,
+.I usbcmd
+issues a USB control request to the device
+.I d
+(not to be confused with a
+.IR usb (3)
+control request sent to its control file).
+.I Usbcmd
+retries the control request several times upon failure because some devices
+require it.
+The format of requests is fixed per the USB standard:
+.I type
+is the type of request and
+.I req
+identifies the request. Arguments
+.I value
+and
+.I index
+are parameters to the request and the last two arguments,
+.I data
+and
+.IR count ,
+are similar to
+.I read
+and
+.I write
+arguments.
+However,
+.I data
+may be
+.B nil
+if no transfer (other than the control request) has to take place.
+The library header file includes numerous symbols defined to help writing
+the type and arguments for a request.
+.PP
+The return value from
+.I usbcmd
+is the number of bytes transferred, zero to indicate a stall and -1
+to indicate an error.
+.PP
+A common request is to unstall an endpoint that has been stalled
+due to some reason by the device (eg., when read or write indicate
+a count of zero bytes read or written on the endpoint). The function
+.I unstall
+does this.
+It is given the device that stalled the endpoint,
+.IR dev ,
+the
+stalled endpoint,
+.IR ep ,
+and the direction of the stall (one of
+.B Ein
+or
+.BR Eout ).
+The function takes care of notifying the device of the unstall as well
+as notifying the kernel.
+.SS Tools
+.I Class
+returns the class part of the number given, representing a CSP.
+.I Subclass
+does the same for the device subclass and
+.I Proto
+for the protocol.
+The counterpart is
+.IR CSP ,
+which builds a CSP from the device class, subclass, and protocol.
+For some classes,
+.I classname
+knows the name (for those with constants in the library header file).
+.PP
+The macros
+.I GET2
+and
+.I PUT2
+get and put a (little-endian) two-byte value and are useful to
+parse descriptors and replies for control requests.
+.PP
+Functions
+.I emallocz
+and
+.I estrdup
+are similar to
+.I mallocz
+and
+.I strdup
+but abort program operation upon failure.
+.PP
+The function
+.I Ufmt
+is a format routine suitable for
+.IR fmtinstall (2)
+to print a
+.B Dev
+data structure.
+The auxiliary
+.I hexstr
+returns a string representing a dump (in hexadecimal) of
+.I n
+bytes starting at
+.IR a .
+The string is allocated using
+.IR malloc (2)
+and memory must be released by the caller.
+.PP
+.I Loaddevstr
+returns the string obtained by reading the device string descriptor number
+.IR sid .
+.SH SOURCE
+.B /sys/src/cmd/usb/lib
+.SH "SEE ALSO"
+.IR usbfs (2),
+.IR usb (3),
+.IR usb (4),
+.IR usbd (4).
+.SH BUGS
+Not heavily exercised yet.

+ 341 - 0
sys/man/2/usbfs

@@ -0,0 +1,341 @@
+.TH USBFS 2
+.SH NAME
+usbreadbuf,
+usbfsadd,
+usbfsdel,
+usbdirread,
+usbfsinit,
+usbdirfs,
+usbfs \- USB device driver file system library
+.SH SYNOPSIS
+.EX
+.ta 8n +8n +8n +8n +8n +8n +8n
+#include <u.h>
+#include <libc.h>
+#include <thread.h>
+#include "../lib/usb.h"
+#include "../lib/usbfs.h"
+.sp 0.3v
+enum {
+	Hdrsize	= 128,		/* plenty of room for headers */
+	Msgsize	= 8 * 1024,
+	Bufsize	= Hdrsize + Msgsize,
+	Namesz = 40,
+	Errmax = 128,
+	ONONE = ~0,		/* omode in Fid when not open */
+};
+.sp 0.3v
+struct Fid {
+	int	fid;
+	Qid	qid;
+	int	omode;
+	Fid*	next;
+	void*	aux;
+};
+.sp 0.3v
+struct Usbfs {
+	char	name[Namesz];
+	uvlong	qid;
+	Dev*	dev;
+	void*	aux;
+.sp 0.3v
+	int	(*walk)(Usbfs *fs, Fid *f, char *name);
+	void	(*clone)(Usbfs *fs, Fid *of, Fid *nf);
+	void	(*clunk)(Usbfs *fs, Fid *f);
+	int	(*open)(Usbfs *fs, Fid *f, int mode);
+	long	(*read)(Usbfs *fs, Fid *f,
+			void *data, long count, vlong offset);
+	long	(*write)(Usbfs *fs, Fid*f,
+			void *data, long count, vlong offset);
+	int	(*stat)(Usbfs *fs, Qid q, Dir *d);
+	void	(*end)(Usbfs *fs);
+};
+.sp 0.3v
+typedef int (*Dirgen)(Usbfs*, Qid, int, Dir*, void*);
+.sp 0.3v
+long	usbreadbuf(void *data, long count,
+		vlong offset, void *buf, long n);
+void	usbfsadd(Usbfs *dfs);
+void	usbfsdel(Usbfs *dfs);
+int	usbdirread(Usbfs*f, Qid q, char *data, long cnt,
+		vlong off, Dirgen gen, void *arg);
+void	usbfsinit(char* srv, char *mnt, Usbfs *f, int flag);
+void	usbfsdirdump(void);
+.sp 0.3v
+extern char Enotfound[], Etoosmall[], Eio[], Eperm[], Ebadcall[],
+	Ebadfid[], Einuse[], Eisopen[], Ebadctl[];
+.sp 0.3v
+extern Usbfs usbdirfs;
+extern int usbfsdebug;
+.EE
+.SH DESCRIPTION
+This library provides an alternative to
+.IR 9p (2)
+for implementing a file server within a USB driver.
+Drivers using this
+library may be embedded into
+.IR usbd (4).
+It may be also desirable to use this library when drivers are
+not embedded because it is tailored to work well with the
+library for handling USB devices.
+.PP
+A USB file system is described by a
+.I Usbfs
+structure.
+In most cases, the driver is not responsible for the root of the
+file tree.
+It is customary that a driver creates a file server
+for each device handled and links all of them to a root directory
+implemented by the
+.I usbdirfs
+file system implemented by the library.
+This root directory is bound to
+.B /dev
+in most cases.
+.PP
+.I Usbdirfs
+implements a root directory populated by named file trees,
+each one described by a
+.B Usbfs
+structure.
+.PP
+The field
+.B Usbfs.name
+contains the name for the root directory of the file system, usually
+a directory seen at
+.BI /dev/ name
+when the driver is embedded.
+.PP
+.B Usbfs.qid
+maintains a value used to decorate qids for the file tree.
+This may be ignored when
+.I usbdirfs
+is not used.
+Otherwise,
+.I usbdirfs
+assigns a unique value kept at the high 32 bits of
+.B Qid.path
+for all files on each file tree bound to it.
+Each
+.I Usbfs
+server must bitwise OR
+.B Usbfs.qid
+to all
+.B Qid.path
+values returned by its functions.
+In the same way,
+functions usually clear bits in
+.B Usbfs.qid
+before processing
+.B Qid.path
+values supplied as input.
+.PP
+The USB device handled by a file tree is referenced from
+.B Usbfs.dev
+(and a reference must be counted for it).
+This permits the following functions to quickly locate the device of
+interest, and also permits releasing the device when
+no request is outstanding.
+.PP
+The field
+.B Usbfs.aux
+is for the device to use.
+The rest of the fields implement the 9P protocol for the device.
+Not all the operations need be implemented.
+Only
+.IR walk ,
+.IR open ,
+.IR read ,
+.IR write ,
+and
+.IR stat ,
+must be implemented (and their corresponding fields in
+.B Usbfs
+may never be
+.BR nil ).
+These functions must return -1 upon failure
+and set the error string to reflect the cause of a failure.
+.PP
+In all the functions, a 9P fid is represented by a
+.B Fid
+structure.
+It contains the 9P
+.IR fid ,
+the corresponding
+.IR qid ,
+and an auxiliary pointer for the driver to use.
+Open
+.IR fid s
+have a valid open mode in
+.I omode
+while others have
+.B ONONE
+to indicate that the
+.I fid
+is not open.
+The library takes care of which
+fids
+exist and which ones do not.
+.PP
+.I Walk
+must walk
+.I f
+to
+.I name
+(a single name, not a file path)
+in the supplied
+.IR fs .
+Its implementation should update the qid in
+.I f
+to reflect the walk.
+This function must bitwise OR any returned Qid with
+.B Usbfs.qid ,
+if
+.I usbdirfs
+is used.
+.PP
+.I Clone
+must clone fid
+.I of
+onto
+.I nf
+so that,
+upon successful completion,
+.I nf
+also refers to the file that
+.I f
+refers to.
+An implementation must update the Qid of the cloned
+fid.
+If this function is not supplied, the library copies the
+.I aux
+field to the cloned fid.
+.PP
+.I Clunk
+clunks
+.IR f .
+It usually releases data kept in the
+.I aux
+field, but may be
+set to
+.B nil
+otherwise.
+.PP
+.I Open
+prepares the fid
+.I f
+for I/O according to
+.IR mode .
+The open mode in the fid is updated by the library upon return.
+The library checks trivial cases like opening already-open fids.
+The implementation performs most permission checking.
+.PP
+.I Read
+reads up to
+.I count
+bytes into
+.I data
+starting at
+.I offset
+in the file referenced by
+.IR f .
+.I Write
+is the counterpart.
+To read from directories,
+the function
+.I usbdirread
+may be called.
+It returns the return value of
+.I read
+or -1.
+.I usbdirread
+calls
+.I gen
+to iterate through files as needed.
+The
+.B Dirgen
+function will be called with index values of 0
+and up to ask for the first file and following files.
+To read from data already in buffers, the function
+.I usbreadbuf
+may help.
+It must be given the arguments supplied
+by the user, plus the buffer and buffer size.
+.PP
+.I Stat
+must fill
+.I d
+with the directory entry for the file identified by
+.IR q.
+As an aid,
+.I d
+is initialized to fake access and modification times,
+and user and group ids.
+Also, the field
+.B name
+in
+.I d
+is initialized to point to a 40-byte buffer.
+If the file name fits,
+it may be copied directly into
+.B d->name
+without allocating memory for that purpose.
+Otherwise
+.B d->name
+must be initialized to point to static memory.
+.PP
+The function
+.I end
+is called upon termination of the file tree to
+release resources.
+.PP
+Calling
+.I usbfsinit
+starts a file server for
+.I f
+that mounts itself at
+.I mnt
+and posts
+.I srv
+at
+.IR srv (3).
+In most cases, the file system supplied is
+.IR usbdirfs .
+The
+.I flag
+is used for
+.IR mount (2).
+Once
+.I usbdirfs
+is started, calls to
+.IR usbfsadd
+add a file tree implemented by
+.I dfs
+to the root directory of
+.I usbdirfs
+and
+calls to
+.I usbfsdel
+remove that binding (and release resources including
+the reference to the USB device).
+.PP
+Various error strings are declared as an aid.
+The global
+.B usbfsdebug
+may be set to trigger diagnostics and protocol tracing.
+.SH EXAMPLE
+See
+.B /sys/src/cmd/usb/disk
+for an example driver that uses this library.
+Looking at an example is strongly suggested
+to see how reference counts for the USB device
+and the file system are handled.
+.SH SOURCE
+.B /sys/src/cmd/usb/lib
+.SH "SEE ALSO"
+.IR usb (2),
+.IR usb (3),
+.IR usb (4),
+.IR usbd (4).
+.SH BUGS
+Not heavily exercised yet.

+ 471 - 213
sys/man/3/usb

@@ -1,264 +1,522 @@
-.TH USB 3 
+.EQ
+delim $$
+.EN
+.TH USB 3
 .SH NAME
 usb \- USB Host Controller Interface
 .SH SYNOPSIS
 .nf
-.B bind -a #U /dev
+.B bind -a #u /dev
 .PP
 .nf
-.BI /dev/usb m
-.BI /dev/usb m /new
-.BI /dev/usb m /port
-.BI /dev/usb m / n 
-.BI /dev/usb m / n /ctl
-.BI /dev/usb m / n /status
-.BI /dev/usb m / n /setup
-.BI /dev/usb m / n /ep k\fLdata
+.B /dev/usb
+.B /dev/usb/ctl
+.BI /dev/usb/ep N . M
+.BI /dev/usb/ep N . M /data
+.BI /dev/usb/ep N . M /ctl
 \&...
 .fi
 .SH DESCRIPTION
 The Universal Serial Bus is a complex yet popular bus
-for connecting devices, such as mice, keyboards, printers, scanners,
-and (eventually with USB 2) disks to a PC.  It is
-a four-wire tree-shaped bus that provides both communication and (limited)
-power to devices.  Branching points in the tree are provided by devices
-called
+for connecting all kind of devices to a computer.
+It is a four-wire tree-shaped bus that provides both communication and (limited)
+power to devices.
+Branching points in the tree are provided by devices called
 .IR hubs .
+Hubs provide ports where USB devices (also hubs) can be attached.
 .PP
-Most PCs have a two-slot hub built in, accommodating two USB devices.  To
-attach more devices, one or more hubs have to be plugged in to the USB
-slots of the PC.  The topology of the network is a tree with at most
+Most PCs have one or more USB controllers called
+.I host
+controllers.
+Each one has a built-in hub called a
+.I "root hub"
+providing several ports.
+In some cases, more hubs are built-in
+and attached to a root hub port.
+The topology of the network is a tree with at most
 127 nodes, counting both internal and leaf nodes.
 .PP
+Host controllers come in four flavours:
+UHCI and OHCI for USB 1 (up to 12 Mb/s),
+EHCI for USB 2 (up to 480 Mb/s)
+and
+XHCI for USB 3 (up to 5 Gb/s).
+We currently support all but XHCI, which is still quite new.
+.PP
 The USB bus is fully controlled by the host; all devices are polled.
 Hubs are passive in the sense that they do not poll the devices attached
-to them.  The host polls those devices and the hubs merely route the
-messages.
+to them.
+The host polls those devices and the hubs merely route the messages.
 .PP
 Devices may be added to or removed from the bus at any time.
-When a device is attached, the host queries it to determine its type
-and its speed.  The querying process is standardized.  The first level
-of querying is the same for all devices, the next is somewhat specialized
+When a device is attached, the host queries it to determine its type and speed.
+The querying process is standardized.
+The first level of querying is the same for all devices,
+the next is somewhat specialized
 for particular classes of devices (such as mice, keyboards, or audio devices).
 Specialization continues as subclasses and subsubclasses are explored.
-.SS Discovery
-For each connected device there is a directory in
-.BI #U/usb n\fR.
-Reading
-.BI #U/usb n /*/status
-yields the state, class/subclass/proto, vendor-id and product-id of each device in the
-first line.  The remaining lines give the state of each of the
-interfaces.
-.PP
-To find a mouse, for example, scan the status files for the line beginning with
-.IP
-.EX
-.B "Enabled 0x020103"
-.EE
 .PP
-A mouse belongs to class 3 (in the least significant byte),
-.IR "human interface device" ,
-subclass 1,
-.IR boot ,
-proto 2,
-.I mouse
-(proto 1 would be the keyboard).
-USB class, subclass and proto codes can be found on
-.BR www.usb.org .
-.SS Device Control
-The control interface for each device is
-.I "``endpoint 0''"
-and is named
-.BI #U/usb n /*/setup \fR.
-The control interface of the device is accessed by reading and writing
-this file.
+Enumeration of the bus and initial configuration of devices is done
+by a user level program,
+.IR usbd (4).
+Device drivers are implemented by separate user programs, although
+some of them may be statically linked into
+.IR usbd .
 .PP
-There is a separate
-.I "control interface
-named
-.BI #U/usb n /*/ctl
-which is used to configure the USB device
-.IR driver .
-By writing to this
-file, driver endpoints can be created and configured for communication with a
-device's data streams.  A mouse, for example, has one control interface
-and one data interface.  By communicating with the control interface,
-one can find out the device type (i.e., `mouse'), power consumption, number
-on interfaces, etc.
-.IR Usbd (4)
-will extract all this information and, in verbose mode, print it.
+The kernel device described in this page is responsible for providing
+I/O for using the devices through so called
+.IR endpoints .
+Access to the host controller is hidden from user programs, which see
+just a set of endpoints.
+After system initialization, some endpoints
+are created by the device to permit I/O to root hubs.
+All other devices must be configured by
+.IR usbd .
+.SS Devices and Endpoints
+A device includes one or more functions (e.g., audio output,
+volume control buttons, mouse input, etc.)
+Communication with device functions is performed
+by some combination of
+issuing control requests to, sending data to, and receiving data from
+device
+.IR endpoints .
+Endpoints can be understood as addresses in the bus.
+There are several types:
+.TF "\fIIsochronous
+.TP
+.I Control
+Their main use is to configure devices.
+Writing a message with a specific format
+(specified in the USB specification)
+issues a request to the device.
+If the request implies a reply,
+a read can be made next to retrieve the requested data (if the write succeeded).
+.TP
+.I Interrupt
+Used to send and receive messages to or from a specific device function
+(e.g., to read events from a mouse).
+.TP
+.I Bulk
+Used to send and receive larger amounts of data through streams
+(e.g., to write blocks to a disk).
+.TP
+.I Isochronous
+Used to send and receive data in a timely manner
+(e.g., to write audio samples to a speaker).
+.PD
 .PP
-By sending an `endpoint message' to the
-.I ctl
-file, new driver endpoints can be created.  The syntax of these messages
-is
-.IP
-.B ep
-.I n
-.B ctl
-.I "mode maxpkt nbuf
+All USB devices include at least
+a control endpoint to perform device configuration.
+This is called the
+.I setup
+endpoint or
+.IR "endpoint zero" .
+After configuring a device, other endpoints may be created
+as dictated by the device to perform actual I/O.
+.SS Operation
+Bus enumeration and device configuration is performed by
+.IR usbd (8)
+and not by this driver.
+The driver provides an interface
+to access existing endpoints (initially those for the built-in root hubs),
+to create and configure other ones, and to perform I/O through them.
 .PP
-or
-.IP
-.B ep
-.I n
-.B bulk
-.I "mode maxpkt nbuf
+Each directory
+.BI /dev/usb/ep N . M
+represents an endpoint, where
+.I N
+is a number identifying a device and
+.I M
+is a number identifying one of its endpoints.
 .PP
-or
-.IP
-.B ep
-.I "n period mode maxpkt
+For each device attached to the bus, and configured by
+.IR usbd (8),
+an endpoint zero (a
+.I setup
+endpoint)
+is provided at
+.BI /dev/usb/ep N .0
+for configuring the device.
+This is always a control endpoint and represents the device itself.
 .PP
-or
-.IP
-.B ep
-.I "n period mode samplesize hz
+The device driver may use the setup endpoint
+to issue control requests and perhaps to create more endpoints for the device.
+Each new endpoint created has its own directory as said above.
+For example, if the driver for the device
+.BI /dev/usb/ep N . 0
+creates the endpoint number 3 for that device, a directory
+.BI /dev/usb/ep N .3
+will be available to access that endpoint.
 .PP
-There are four forms for, respectively, Control, Bulk, Interrupt and
-Isochronous traffic (see USB specs for what that means).
-In all forms,
-.I n
-is the endpoint to be configured, and
-.I mode
-is
-.B r
-for read only,
-.B w
-for write only, or
-.B rw
-for reading and writing.
-.I Maxpkt
-is the maximum packet size to be used (between 8 and 1023),
+All endpoint directories contain two files:
+.B data
 and
-.I nbuf
-is the number of buffers to be allocated by the driver.
+.BR ctl .
+The former has mode bit
+.B DMEXCL
+set and can be open by only one process at a time.
+.SS data
 .PP
-.I Period
-is the number of milliseconds between packets (iso) or polls (interrupt).
-This number is usually dictated by the device.  It must be between 1 and 1000.
 The
-.I samplesize
-is the size in bytes of the data units making up packets, and
-.I hz
-is the number of data units transmitted or received per second.
+.B data
+file is used to perform actual I/O.
+In general, reading from it retrieves
+data from the endpoint and writing into it sends data to the endpoint.
+For control endpoints, writing to this file issues a control request
+(which may include data); if the request retrieves data from the device,
+a following read on the file will provide such data.
 .PP
-The data rate for an isochronous channel is
-.IR hz × samplesize
-bytes per second, and the number of samples in a packet
-will be 
-.RI ( period × hz )/1000,
-rounded up or down.
-Packets do not contain fractional samples.
-A 16-bit stereo 44.1 KHz audio stream will thus have 44100 4-byte samples
-per second, typically in a 1ms period.  Ove a 10 ms period, this yields 9
-packets of 176 bytes followed by a 180-byte packet (the driver figures it
-out for you).
+USB errors reported by the endpoint upon I/O failures
+are passed to the user process through the error string.
+I/O stalls not resulting from an error, usually
+an indication from the device, are reported by indicating that the
+number of bytes transferred has been zero.
+In most cases, the correct course of action after noticing the stall
+is for the device driver to issue a `clear halt' request (see
+.I unstall
+in
+.IR usb (2))
+to resume I/O.
+The most common error is
+.L crc/timeout
+indicating problems in communication with the device (eg., a physical
+detach of the device or a wiring problem).
 .PP
-The mouse, which produces 3-byte samples, is configured with
-.BR "ep 1 ctl r 3 32" :
-endpoint 1 is configured for non-real-time read-only 3-byte messages
-and allows 32 of them to be outstanding.
-.PP
-A usb audio output device at 44.1 KHz, 2 channels, 16-bit samples, on endpoint
-4 will be configured with
-.BR "ep 4 1 w 4 44100" .
-.PP
-If the configuration is successful, a file named
-.BI ep n data
-is created which can be read and/or written depending on
-configuration.  Configuration is not allowed when the data endpoint
-is open.
-.SS Isochronous Streams
-Forward
-.I seek
-operations on isochronous endpoints
-can be used to start the I/O at a specific time.
-The usb status file provides information that can be used to map file
-offsets to points in time:  For each endpoint, the status file produces a line
-of the form:
-.IP
-.B "4 0x000201 \f2nnn\fP bytes \f2nnn\fP blocks
+For control, bulk, and isochronous transfers, there is an implicit
+timeout performed by the kernel and it is not necessary for applications
+to place their own timers.
+For interrupt transfers, the kernel will not time out any operation.
+.SS "ctl and status"
 .PP
-The fields are, from left to right,
-endpoint number, class/subclass/proto (as a six-digit hex number with class in the
-least significant byte), number of bytes read/written, number of blocks read/written.
+The
+.B ctl
+file can be read to learn about the endpoint.
+It contains information that can be used
+to locate a particular device (or endpoint).
+It also accepts writes with textual control requests described later.
 .PP
-For isochronous devices only, an additional line is produced of the
-form:
+This may result from the read of an endpoint control file:
 .IP
-.B "bufsize \f2s\fP buffered \f2b\fP offset \f2o\fP time \f2t\fP
-.PP
-.I S
-is the size of the DMA operations on the device (i.e., the minimum useful size
-for reads and writes),
-.I b
-is the number of bytes currently buffered for input or output, and
-.I o
+.EX
+.I "(the first line is wrapped to make it fit here)"
+.ft L
+enabled control rw speed full maxpkt 64 pollival 0
+	samplesz 0 hz 0 hub 1 port 3 busy
+storage csp 0x500608 vid 0x951 did 0x1613 Kingston 'DT 101 II'
+.ft
+.EE
+.LP
+The first line contains status information.
+The rest is information supplied by
+.IR usbd(8)
+as an aid to locate devices.
+The status information includes:
+.TF "\fREndpoint mode
+.PD
+.TP
+Device state
+One of
+.BR config ,
+.BR enabled ,
 and
-.I t
-should be interpreted to mean that byte offset
-.I o
-was/will be reached at time
-.I t
-(nanoseconds since the epoch).
-.PP
-To play or record samples exactly at some predetermined time, use
-.I o
+.BR detached .
+An endpoint starts in the
+.B config
+state, and accepts control commands written to its
+.B ctl
+file to configure the endpoint.
+When configured, the
+state is
+.B enabled
+and the
+.B data
+file is used as described above (several control requests can still
+be issued to its
+.B ctl
+file, but most will not be accepted from now on).
+Upon severe errors, perhaps a physical
+detachment from the bus, the endpoint enters the
+.B detached
+state and no further I/O is accepted on it.
+Files for an endpoint (including its directory)
+vanish when the device is detached and its files are no longer open.
+Root hubs may not be detached.
+.TP
+Endpoint type
+.BR control ,
+.BR iso ,
+.BR interrupt ,
+or
+.BR bulk ,
+indicating the type of transfer supported by the endpoint.
+.TP
+Endpoint mode
+One of
+.BR r ,
+.BR w ,
+or
+.BR rw ,
+depending on the direction of the endpoint (in, out, or inout).
+.TP
+Speed
+.BR low
+(1.5 Mb/s),
+.BR full
+(12 Mb/s),
+or
+.BR high
+(480 Mb/s).
+.TP
+Maximum packet size
+Used when performing I/O on the data file.
+.TP
+Polling interval
+The polling period expressed as a number of µframes
+(for high-speed endpoints) or frames (for low- and full-speed endpoints).
+Note that a µframe takes 125 µs while a frame takes 1 ms.
+This is only of relevance for interrupt and isochronous endpoints.
+This value determines how often I/O happens.
+Note that the control request adjusting the polling interval does
+.I not
+use these units, to make things easier for USB device drivers.
+.TP
+Sample size
+Number of bytes per I/O sample (isochronous endpoints only).
+.TP
+Frequency
+Number of samples per second (Hertz).
+.TP
+Hub address
+Device address of the hub where the device is attached.
+.TP
+Port number
+Port number (in the hub) where the device is attached.
+.TP
+Usage
+.L busy
+while the data file is open and
+.L idle
+otherwise.
+This is useful to avoid disturbing endpoints already run
+by a device driver.
+.LP
+The second line contains information describing the device:
+.TF "\fRDevice strings
+.PD
+.TP
+Class name
+As provided by the device itself.
+.TP
+CSP
+Class, Subclass, and Protocol for the device.
+If the device contains different functions and has more CSPs,
+all of them will be listed.
+The first one is that of the device itself.
+For example,
+a mouse and keyboard combo may identify itself as a keyboard but
+then include two CSPs, one for the keyboard and another one for the mouse.
+.TP
+Vid and Did
+Vendor and device identifiers.
+.TP
+Device strings
+Provided by the device and identifying the manufacturer and type of device.
+.LP
+For example, to find a mouse not yet in use by a driver, scan the
+.B ctl
+files for
+.BR enabled ,
+.BR idle ,
 and
-.I t
-with the sampling rate to calculate the offset to seek to.
+.BR "csp 0x020103" .
+A mouse belongs to class 3 (in the least significant byte),
+.IR "human interface device" ,
+subclass 1,
+.IR boot ,
+protocol 2,
+.I mouse
+(protocol 1 would be the keyboard).
+USB class, subclass and proto codes can be found at
+.BR http://www.usb.org .
+.SS Control requests
+Endpoint control files accept the following requests.
+In most cases
+the driver does not issue them, leaving the task to either
+.IR usbd (8)
+or the usb driver library documented in
+.IR usb (2).
+.TF "\fLsamplehz\fI n
+.TP
+.B detach
+Prevent further I/O on the device (delete the endpoint)
+and remove its file interface as soon as no process is using their files.
+.TP
+.BI maxpkt " n"
+Set the maximum packet size to
+.I n
+bytes.
+.TP
+.BI pollival " n"
+Only for interrupt and isochronous endpoints.
+Set the polling interval as a function of the value
+.I n
+given by the endpoint descriptor.
+The interval value used is the period
+.I n
+in bus time units for low- and full-speed interrupt endpoints.
+Otherwise, the actual interval is
+$2 sup n$
+and not
+.IR n .
+Bus time units are 1 ms for low- and full-speed endpoints and 125 µs for
+high-speed endpoints.
+In most cases, the device driver may ignore
+all this and issue the control request supplying the
+polling interval value as found
+in the endpoint descriptor.
+The kernel adjusts the value according
+to the endpoint configuration and converts it into the number of
+frames or µframes between two consecutive polls.
+.TP
+.BI samplesz " n"
+Use
+.I n
+as the number of bytes per sample.
+.TP
+.BI hz " n"
+Use
+.I n
+as the number of samples per second.
+.TP
+.BI ntds " n"
+Use
+.I n
+as the number of transactions per frame (or µframe), as reported
+by the descriptor.
+.TP
+.B clrhalt
+Clear the halt condition for an endpoint.
+Used to recover from a stall caused by a device to signal its driver
+(usually due to an unknown request or a failure to complete one).
+.TP
+.BI info " string"
+Replaces description information in
+.B ctl
+with
+.IR string .
+.IR Usbd (8)
+uses this to add device descriptions.
+.TP
+.B address
+Tell this driver that the device has been given an address,
+which causes the device to enter the
+.I enabled
+state.
+.TP
+.BI name " str"
+Generates an additional file name,
+.I str ,
+for the
+.B data
+file of the endpoint.
+This file name appears in the root directory of the
+.L #u
+tree.
+For example, this is used by the audio device
+driver to make the
+.B data
+file also available as
+.BR /dev/audio .
+.TP
+.BI debug " n"
+Enable debugging of the endpoint.
+.I N
+is an integer;
+larger values make diagnostics more verbose.
+.L 0
+stops debugging diagnostics.
+.L 1
+causes just problem reports.
+Bigger values report almost everything.
+.PD
+.LP
+Setup endpoints
+(those represented by
+.BI ep N .0
+directories)
+also accept the following requests:
+.TP
+.BI new " n type mode"
+Creates a new endpoint with number
+.I n
+of the given
+.IR type
+(\c
+.BR ctl ,
+.BR bulk ,
+.BR intr ,
+or
+.BR iso ).
+.I Mode
+may be
+.BR r ,
+.BR w ,
+or
+.BR rw ,
+which creates, respectively, an input, output, or input/output endpoint.
+.TP
+.B "speed {low|full|high}
+Set the endpoint speed to full, low, or high, respectively.
+.TP
+.B hub
+Tell this driver that the endpoint corresponds to a hub device.
+.PD
+.PP
+Setup endpoints for hub devices also accept his request:
+.TP
+.B "newdev {low|full|high} \fIport\fP
+Create a new setup endpoint to represent a new device.
+The first argument is the device speed.
+.I Port
+is the port number where the device is attached
+(the hub is implied by the endpoint where the control request is issued).
+.PD
 .PP
-The number of bytes buffered can also be obtained using
-.IR stat (2)
-on the endpoint file.  See also
-.IR audio (3).
+The file
+.B /dev/usb/ctl
+provides all the information provided by the various
+.B ctl
+files when read.
+It accepts several requests that refer to
+the entire driver and not to particular endpoints:
+.TF "\fLdebug \fIn"
+.TP
+.B "debug \fIn\fP
+Sets the global debug flag to
+.IR n .
+.TP
+.B dump
+Dumps data structures for inspection.
 .SH FILES
-.TF "#U/usb n /*/status"
-.TP
-.BI #U/usb n /port
-USB port status file; for each port, space separated: port number, hexadecimal port status, port status string
-.TP
-.BI #U/usb n /*/status
-USB device status file; class/subclass/proto, vendor-id and product-id are found in line one
-.TP
-.BI #U/usb n /*/ctl
-USB
-.I driver
-control file, used to create driver endpoints, control debugging, etc.
-.TP
-.BI #U/usb n /*/setup
-USB
-.I device
-control file, used to exchange messages with a device's control channel.
-.B setup
-may be viewed as a preconfigured
-.B ep0data
-file.
-.TP
-.BI #U/usb n /*/ep k data
-USB device data channel for the
-.IR k 'th
-configuration.
+.TF #u/usb
+.TP
+.B #u/usb
+root of the USB interface
 .SH SOURCE
 .B /sys/src/9/pc/usb.h
 .br
 .B /sys/src/9/pc/devusb.c
 .br
-.B /sys/src/9/pc/usb[ou]hci.c
+.B /sys/src/9/pc/usb?hci.c
 .SH "SEE ALSO"
+.IR usb (2),
 .IR usb (4),
 .IR usbd (4),
 .IR plan9.ini (8)
 .SH BUGS
-EHCI USB 2 controllers are not yet supported.
-.PP
-The interface for configuring endpoints is at variance with the standard.
+Isochronous input streams are not implemented for OHCI.
 .PP
-The notion that the endpoints themselves have a class and subclass
-is a distortion of the standard.
-It would be better to leave all handling of the notions of class to the
-user-level support programs, and remove it from the driver.
+Some EHCI controllers drop completion interrupts and so must
+be polled, which hurts throughput.
 .PP
-There may be a timing bug that makes disk accesses via UHCI much
-slower than necessary.
+Not heavily exercised yet.

+ 204 - 150
sys/man/4/usb

@@ -1,99 +1,152 @@
 .TH USB 4
 .SH NAME
-usbmouse,
+audio,
+disk,
+ether,
 kb,
-usbaudio,
-print
-\- Universal Serial Bus user-level device drivers
+print,
+probe,
+usbfat:
+\- Universal Serial Bus device drivers
 .SH SYNOPSIS
-.B usb/usbmouse
+.B usb/kb
 [
-.B -fsv
+.B -dkm
 ] [
 .B -a
 .I accel
 ] [
-.I ctrlno
-.I n
+.I dev ...
 ]
 .PP
-.B usb/kb
+.B usb/disk
 [
-.B -dkmn
-] [
-.B -a
-.I n
-] [
-.I ctlrno
-.I n
+.B -Dd
+]
+[
+.B -m
+.I mnt
+]
+[
+.B -s
+.I srv
+]
+[
+.I dev ...
+]
+.PP
+.B usbfat:
+[
+.I disk ...
 ]
 .PP
-.B usb/usbaudio
+.B usb/audio
 [
-.B -pV
+.B -dpV
 ] [
 .B -m
-.I mountpoint
+.I mnt
 ] [
 .B -s
-.I srvname
+.I srv
 ] [
 .B -v
-.I volume
+.I vol
 ] [
-.I ctrlno
-.I n
+.I dev
+]
+.PP
+.B usb/ether
+[
+.B -Dd
+]
+[
+.B -m
+.I mnt
+]
+[
+.B -s
+.I srv
+]
+[
+.I dev ...
 ]
 .PP
 .B usb/print
+[
+.B -d
+]
+[
+.I dev ...
+]
+.PP
+.B usb/probe
 .SH DESCRIPTION
-These programs implement support for specific USB device classes.
-They should be run after
+These programs drive USB devices of specific classes via
+.IR usb (3).
+Usually they are started by
 .IR usbd (4)
-has had a chance to locate the devices in question and provide
-them with device addresses and minimal configuration.
-Dynamic handling of device insertion and removal is currently not supported.
-.SS Mice
-.I Usbmouse
-sends mouse events from a USB mouse to
-.B /dev/mousein
-where the Plan 9 kernel processes them like other mice, but see
+upon attachment of the device to the bus.
+Less often, users start them manually, depending on
+.IR usbd (4)'s
+configuration.
+Usually,
 .I kb
-below.
+and
+.I disk
+are started by
+.I usbd
+and other programs are started by hand.
 .PP
-Without arguments, it scans the USB status files to find a mouse
-and uses the first one it finds.  A pair of numeric arguments overrides this search
-with a specific USB controller and device.  The options are
-.TF "-a ac"
-.TP
-.BI -a " accel"
-Accelerate mouse movements.
-.TP
-.BI -f
-Run usbmouse in foreground.
-.TP
-.BI -s
-Use the scrollwheel.
-.TP
-.BI -v
-Verbose mode.
+Without arguments, the drivers handle all the devices (of
+the appropriate USB class) found on the bus.
+To make a driver handle only certain devices, supply as arguments
+the paths for the directories of the devices
+(actually of their zero endpoints).
+.PP
+Drivers that provide file systems accept options
+.B -s
+and
+.B -m
+to instruct them to post a 9P connection at
+.IR srv (3)
+with the given name and/or to mount themselves at
+.IR mnt .
+When embedded into
+.IR usbd
+these options may not be used.
+In this case,
+the file tree supplied by the device driver is
+available through the file system provided by
+.IR usbd ,
+usually mounted at
+.B /dev
+and reachable through the 9P connection posted at
+.BR /srv/usb .
+.PP
+Options
+.B -d
+and
+.B -D
+present on most drivers trigger debug diagnostics and
+file system debugging diagnostics.
+Repeating any one of these may increase verbosity.
+.PP
+To help locate devices of interest,
+.I probe
+lists all the USB devices available,
+including those with no driver started.
 .SS Keyboards and mice
 .I Kb
 supports USB keyboards and mice either as separate USB devices
-or as a single combined USB device. Scan codes from the keyboard
-are sent to
+or as a single combined USB device.
+Scan codes from the keyboard are sent to
 .B /dev/kbin
 to let the kernel process them.
 Mouse events are sent to
 .B /dev/mousein
 in the same way.
 .PP
-Without arguments it handles the keyboard and mouse devices found
-on the bus.
-Otherwise it uses the one attached to controller
-.I ctrlno
-with device number
-.IR n .
 The following options are understood:
 .TF -k
 .TP
@@ -102,25 +155,69 @@ Accelerate the mouse to level
 .I n
 (similar to the kernel mouse driver acceleration).
 .TP
-.B \-d
-Activate debug diagnostics. Repeating the flag one or more times
-increases the verbosity.
-.TP
 .B \-k
 Serve just the keyboard (and not the mouse).
 .TP
 .B \-m
 Serve just the mouse (and not the keyboard).
-.TP
-.B \-n
-Dry run. Do not send any events to the kernel for processing.
+.SS Disks
+.I Disk
+configures and manages USB mass storage devices. It
+provides a file system (usually seen at
+.BR /dev )
+that includes one directory per storage device, named
+.BI sdU N . M
+in correspondence with the usb device number and the storage
+unit number (or LUN).
+For example, LUN number 2 on
+.B /dev/usb/ep3.0
+can be accessed through
+.BR /dev/sdU3.2 .
+.PP
+The storage device directory contains the usual files
+served by
+.IR sd (3):
+.BR data ,
+.BR raw ,
+and
+.BR ctl .
+.PP
+The
+.B ctl
+file supplies the device
+geometry when read.
+.PP
+The convenience script
+.B usbfat:
+mounts the FAT file system in the DOS partition of the named
+.IR disk s;
+if none, it mounts those file systems found at
+.BR /dev/sdU*.*/data .
 .SS Printers
 .I Print
-is a script that mounts a USB printer on
-.BR /dev/lp .
+provides a single file can be written to print on a USB printer.
+Options are similar to those of
+.IR disk .
+The file is also bound at
+.B /dev/lp
+as is customary.
+.SS Ethernet adapters
+.I Ether
+provides a file interface similar to that of
+.IR ether (3)
+for each USB Ethernet adapter found.
+The name of an Ethernet device is
+.BI etherU N
+where
+.I N
+is the device name.
+When started manually, the file interface is mounted at
+.B /net
+as is customary.
 .SS Audio devices
 .I Usbaudio
-configures and manages a USB audio device.  It implements a file system,
+configures and manages a USB audio device.
+It implements a file system,
 normally mounted on
 .BI /dev ,
 but this can be changed with the
@@ -135,11 +232,12 @@ The names
 .B volume
 and
 .B audio
-maintain backward compatibility with the soundblaster driver.
+maintain backward compatibility with the Soundblaster driver.
 .PP
 The
 .B \-V
-option (verbose) causes usbaudio to print information about the device on startup.
+option (verbose)
+causes usbaudio to print information about the device on startup.
 The
 .B \-s
 option specifies a name for a file descriptor to be posted in
@@ -153,10 +251,11 @@ Reading
 .B volume
 or
 .B audioctl
-yields the device's settings.  The data format of
+yields the device's settings.
+The data format of
 .B volume
-is compatible with the soundblaster and
-produces something like
+is compatible with the Soundblaster and produces output in this
+format:
 .IP
 .EX
 audio out 65
@@ -165,9 +264,11 @@ bass out 0
 speed out 44100
 .EE
 .PP
-This file can be written using the same syntax.  The keyword
-.I out
-may be omitted.  Settings are given as percentages of the range,
+This file can be written using the same syntax.
+The keyword
+.L out
+may be omitted.
+Settings are given as percentages of the range,
 except for speed which is in Hz.
 .PP
 The file
@@ -187,13 +288,17 @@ There are 3, 5, or 6 columns present.
 Maxima and resolution are omitted when they are not available or not applicable.
 The resolution for
 .I speed
-is reported as 1 (one) if the sampling frequency is continuously variable.  It is absent
-if it is settable at a fixed number of discrete values only.
+is reported as 1 (one) if the sampling frequency is continuously variable.
+It is absent if it is settable at a fixed number of discrete values only.
 .PP
 When all values from
 .B audioctl
-have been read, a zero-sized buffer is returned (the usual end-of-file indication).
-A new read will then block until one of the settings changes and then report its new value.
+have been read, a zero-length buffer is returned
+(the usual end-of-file indication).
+A new
+.I read
+will then block until one of the settings changes,
+then report its new value.
 .PP
 The file
 .B audioctl
@@ -204,78 +309,27 @@ Audio data is written to
 .B audio
 and read from
 .BR audioin .
-The data format is little endian, samples ordered primarily by time and
-secondarily by channel.  Samples occupy the minimum integral number
-of bytes.  Read and write operations of arbitrary size are allowed.
-.SH EXAMPLE
-To use a USB mouse and audio device, put the following in your profile
-(replace
-.I x
-with your favorite initial volume setting):
-.IP
-.EX
-.ta 6n
-if (test -r '#U'/usb0) {
-	usb/usbd
-	usb/usbmouse -a 2
-	usb/usbaudio -v \fIx\fP
-	usb/print
-}
-.EE
-.PP
-Alternatively, just put
-.B usbstart
-in your profile.
+The data format is little-endian,
+samples ordered primarily by time and
+secondarily by channel.
+Samples occupy the minimum integral number of bytes.
+Read and write operations of arbitrary size are allowed.
 .SH SOURCE
 .B /sys/src/cmd/usb
 .SH "SEE ALSO"
+.IR kbin (3),
+.IR mouse (3),
+.IR sd (3),
 .IR usb (3),
-.IR usbd (4),
-.IR usbdisk (4)
+.IR usbd (4)
 .SH BUGS
-.I Usbaudio
-only works for certain audio devices.
-This is the list of devices known to work with
-.IR usbaudio :
-.IP "" 3
-.RS
-.TF "Edirol U"
-.TP
-Xitel AN1
-Output only.
-Marginally enough to drive headphones.
-Has mute, volume, bass, treble controls.
-.TP
-Philips USB speakers, model DSS 370/17
-.I Usbaudio
-acts on the volume
-.L +
-and
-.L -
-buttons.
-.TP
-Edirol UA-3
-Playback and record.
-Playback only at 44.1 KHz, record at 32, 44.1 or 48 KHz.
-Playback volume control and mute control.
-The device only has analog (slider controlled)
-input volume control.
-.TP
-Edirol UA-1X
-Playback and record.
-Playback only at 32, 44.1 or 48 KHz, record at 8, 16, 22.05, 32, 44.1 or 48 KHz.
-Playback volume control and mute control
-(haven't tested recording, but I believe it'll work).
-.TP
-Xitel Pro HiFi-Link
-Playback only.
-48 KHz only.
-There is a volume control but it isn't connected to the output, so does nothing.
-.TP
-Onkyo WAVIO series MA-500U
-Includes three optical digital interfaces, two analog, and an
-amplifier (15W + 15W).
-.TP
-Turtle Beach Audio Advantage micro
-Headset and S/Pdif out, volume and mute controls.
-.RE
+The various device drivers are generic USB drivers and
+may work only for certain devices on each class.
+The Ethernet device works only for certain ASIX-based cards and for CDC devices.
+ATA storage devices are not supported.
+Both the Ethernet and printer drivers have not
+been tested and it is likely they will fail.
+.PP
+Not heavily exercised yet.
+The entire set of drivers is new and therefore potentially unreliable.
+A list of working devices must be compiled.

+ 229 - 39
sys/man/4/usbd

@@ -4,52 +4,242 @@ usbd \- Universal Serial Bus daemon
 .SH SYNOPSIS
 .B usbd
 [
-.B -DfV
-] [
-.B -d
-.I bitmask
-] [
-.B -u
-.I root-hub-num
+.B -Dd
+]
+[
+.B -s
+.I srv
+]
+[
+.B -m
+.I mnt
+]
+[
+.I hub...
 ]
 .SH DESCRIPTION
 .I Usbd
-manages the USB infrastructure, polls all ports, configures hubs and
-provides access to USB devices through a file system in
-.BR #U .
-It monitors all ports, active or inactive and acts on state changes
-by configuring devices when they are plugged in or turned on and
-unconfiguring them when they are pulled out or switched off.
-.PP
-.B Usbd
+complements
+.IR usb (3)
+to provide USB I/O for device drivers.
+It enumerates the bus, polling
+hub ports to detect device attachments and detachments, performs
+initial configuration of setup endpoints, and writes extra information into
+.IR usb (3)
+endpoint control files, to ease device location.
+.PP
+By default,
+.I usbd
+opens all setup endpoints found at
+.B #u/usb
+(which correspond to built-in hubs initialized by the kernel during boot).
+Paths to directories representing setup endpoints for hubs can be given
+as arguments to restrict
+.I usbd
+operation to such hubs.
+.PP
+When a device is attached,
+depending upon a configuration file compiled into
+.I usbd ,
+the appropriate device driver may be started without
+user intervention.
+This mechanism can be used to statically link some USB device drivers into
+.I usbd
+itself.
+Initial configuration for setup endpoints is performed independently
+of this configuration.
+.PP
+.I Usbd
+provides a file interface used to change debugging flags, and also used by
+USB device drivers statically linked into
+.IR usbd .
+By default, the file system is mounted (after) at
+.B /dev
+and a 9P connection is posted at
+.BR /srv/usb .
+.PP
+Besides files provided by device drivers, the file
+.B usbdctl
+is always present in the file interface.
+It accepts these control requests:
+.TF "fsdebug\fI n
+.TP
+.BI debug " n"
+Sets the debugging level to
+.IR n .
+.TP
+.BI fsdebug " n"
+Sets the file system debugging level to
+.IR n .
+.TP
+.B dump
+Prints the list of devices and file systems known by
+.IR usbd .
+.PD
+.PP
+.I Usbd
 recognizes the following options:
+.TF "-m\fI mnt
+.TP
+.B -d
+Print debugging diagnostics.
+Repeating the option increases verbosity.
+.TP
+.B -D
+Print debugging diagnostics for the file system interface.
 .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 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.
+.BI -m " mnt"
+Mount the served file system at
+.IR mnt .
+.TP
+.BI -s " srv"
+Post a 9P connection at
+.BI #s/ srv.
+.PD
+.SS Configuration
+.PP
+.I Usbd
+can be configured to start drivers for devices matching one or more CSPs
+(hex representation of USB class, subclass and protocol), class,
+subclass, protocol, vendor id, or device id.
+When a new device is attached,
+.I usbd
+scans the configuration and, if an entry matches the device descriptor, starts
+the driver.
+If no driver is configured, the setup endpoint for the device is left
+configured to let the user start the driver by hand.
+.PP
+Configuration is via compilation
+because one of the options is to embed (link) the driver into the
+.I usbd
+binary.
+If the driver is embedded,
+.I usbd
+creates a process for it and calls its main entry point.
+Otherwise,
+.I usbd
+tries to locate the driver binary in
+.B /bin/usb
+and creates a process to execute it.
+.PP
+The configuration file,
+.BR usbdb ,
+has two sections:
+.B embed
+and
+.BR auto .
+Each section includes lines to configure particular drivers.
+A driver may have more than one line if necessary.
+Each line includes the name of the
+driver (the base name of the binary) and one or more attributes of the form
+.IP
+.IR name = value
+.PP
+The following attributes exist:
+.TF subclass
+.TP
+.B class
+.I Value
+may be the name of the class
+or a number identifying the device class (using C syntax).
+The following class names are known:
+.BR audio ,
+.BR comms ,
+.BR hid ,
+.BR printer ,
+.BR storage ,
+.BR hub ,
+and
+.BR data .
+.TP
+.B subclass
+.I Value
+is the number of the device subclass.
+.TP
+.B proto
+.I Value
+is the number of the device protocol.
+.TP
+.B csp
+.I Value
+is the hexadecimal number describing the CSP for the device.
+.TP
+.B vid
+.I Value
+is the vendor id.
+.TP
+.B did
+.I Value
+is the device id.
+.TP
+.B args
+This must be the last field.
+The value is the rest of the line,
+and is supplied as arguments to the driver process.
+.PD
+.LP
+Several environment variables can be used to alter the behaviour of
+.IR usbd ,
+for example, for use in
+.IR plan9.ini (8).
+.B usbdebug
+sets a debug level (zero for no diagnostics and positive
+values for increasing verbosity).
+.B kbargs
+overrides the keyboard arguments as specified by the configuration file.
+.B diskargs
+overrides the disk arguments in the same way.
+.SH EXAMPLE
+This configuration file links
+.B usb/kb
+into
+.I usbd
+when it is compiled.
+It arranges for the driver's entry point,
+.B kbmain
+in this case,
+to be called for any device with CSPs matching either
+.B 0x010103
+or
+.BR 0x020103 .
+Option
+.B -d
+will be supplied as command line arguments for
+.BR kbmain .
+This configuration also arranges for
+.B /bin/usb/disk
+to start (with no arguments) whenever a device of class
+.B storage
+is attached.
+.IP
+.EX
+embed
+	kb	csp=0x010103 csp=0x020103	args=-d
+auto
+	disk	class=storage	args=
+.EE
+.SH FILES
+.TF /srv/usb
+.TP
+.B /srv/usb
+9P connection to the driver file system.
+.TP
+.B /dev
+mount point for the driver file system.
+.TP
+.B /sys/src/cmd/usb/usbd/usbdb
+Configuration file deciding which devices are included into
+.I usbd
+and which ones are started automatically.
 .SH SOURCE
 .B /sys/src/cmd/usb/usbd
 .SH "SEE ALSO"
+.IR usb (2),
 .IR usb (3),
 .IR usb (4)
+.SH BUGS
+.I Usbd
+is not supposed to be restarted.
+This is arguable.
+.PP
+Not heavily exercised yet.

+ 2 - 11
sys/src/9/boot/boot.c

@@ -325,18 +325,9 @@ static void
 usbinit(void)
 {
 	static char *darg[] = { "/boot/usbd", nil };
-	static char *kbarg[] = { "/boot/kb", "-a2", nil };
-	static char *dskarg[] = {
-		"/boot/disk", "-l", "-s", "usbdisk", "-m", "/mnt", nil
-	};
 
-	if(bind("#U", "/dev", MAFTER) < 0 || access("/dev/usb0", 0) < 0)
-		return;
-	run("usbd", darg);
-	if(access("#m/mouse", 0) < 0)	/* no mouse driver? */
-		kbarg[1] = "-k";
-	run("kb", kbarg);
-	run("disk", dskarg);		/* mounts on /mnt/<lun> */
+	if(bind("#u", "/dev", MAFTER) >= 0 && access("/dev/usb", 0) >= 0)
+		run("usbd", darg);
 }
 
 static void

File diff suppressed because it is too large
+ 1215 - 817
sys/src/9/pc/devusb.c


+ 4 - 2
sys/src/9/pc/pc

@@ -53,12 +53,14 @@ link
 	ether82563	pci
 	ether82557	pci
 	ether83815	pci
+	etherdp83820	pci
 	etherec2t	ether8390
 	etherelnk3	pci
 	etherga620	pci
 	etherigbe	pci ethermii
 	ethervgbe	pci ethermii
 	ethervt6102	pci ethermii
+	ethervt6105m	pci ethermii
 	ethersink
 	ethersmc	devi82365 cis
 	etherwavelan	wavelan devi82365 cis pci
@@ -70,6 +72,7 @@ link
 	loopbackmedium
 	usbuhci
 	usbohci
+	usbehci
 
 misc
 	archmp		mp apic
@@ -115,16 +118,15 @@ ip
 	gre
 	ipmux
 	esp
-#	il
 
 port
 	int cpuserver = 0;
 
 boot
 	tcp
-#	il
 
 bootdir
 	bootpc.out boot
 	/386/bin/ip/ipconfig
 	/386/bin/auth/factotum
+	/386/bin/usb/usbd

+ 10 - 2
sys/src/9/pc/pcauth

@@ -1,4 +1,4 @@
-# pcauth - pc kernel for our auth servers
+# pcauth - pccpuf specialised for our auth servers
 dev
 	root
 	cons
@@ -28,14 +28,21 @@ dev
 	floppy		dma
 
 	uart
+	usb
+	kbin
 
 link
 	apm		apmjump
+	etherdp83820	pci
 	ether82557	pci
 	ethervt6102	pci ethermii
+	ethervt6105m	pci ethermii
 	ethermedium
 	netdevmedium
 	loopbackmedium
+	usbuhci
+	usbohci
+	usbehci
 
 misc
 	realmode
@@ -50,6 +57,7 @@ misc
 ip
 	tcp
 	udp
+	rudp
 	ipifc
 	icmp
 	icmp6
@@ -70,4 +78,4 @@ bootdir
 	/386/bin/auth/factotum
 	/386/bin/fossil/fossil
 	/386/bin/venti/venti
-#	/386/bin/disk/kfs
+	/386/bin/usb/usbd

+ 4 - 4
sys/src/9/pc/pccd

@@ -1,4 +1,4 @@
-# small kernel used to install from cd
+# pccd - small kernel used to install from cd
 dev
 	root
 	cons
@@ -60,6 +60,7 @@ link
 	etherigbe	pci ethermii
 	ethervgbe	pci ethermii
 	ethervt6102	pci ethermii
+	ethervt6105m	pci ethermii
 	ethersink
 	ethersmc	devi82365 cis
 	etherwavelan	wavelan devi82365 cis pci
@@ -70,6 +71,7 @@ link
 	netdevmedium
 	usbuhci
 	usbohci
+	usbehci
 
 misc
 	archmp		mp apic
@@ -112,18 +114,16 @@ ip
 	ipifc
 	icmp
 	icmp6
-#	il
 
 port
 	int cpuserver = 0;
 
 boot boot #S/sdD0/data
 	tcp
-#	il
 	local
 
 bootdir
 	bootpccd.out boot
 	/386/bin/ip/ipconfig ipconfig
 	/386/bin/9660srv kfs
-
+	/386/bin/usb/usbd

+ 6 - 2
sys/src/9/pc/pccpu

@@ -1,3 +1,4 @@
+# pccpu - cpu server kernel
 dev
 	root
 	cons
@@ -28,6 +29,7 @@ dev
 
 	uart
 	usb
+	kbin
 	audio
 
 link
@@ -42,11 +44,13 @@ link
 	ether82563	pci
 	ether82557	pci
 	ether83815	pci
+	etherdp83820	pci
 	etherelnk3	pci
 	etherga620	pci
 	etherigbe	pci ethermii
 	ethervgbe	pci ethermii
 	ethervt6102	pci ethermii
+	ethervt6105m	pci ethermii
 #	etherm10g	pci ethermii
 	ether82598	pci
 	ethersink
@@ -54,6 +58,7 @@ link
 	loopbackmedium
 	usbuhci
 	usbohci
+	usbehci
 
 misc
 	archmp		mp apic
@@ -79,16 +84,15 @@ ip
 	ipmux
 	esp
 	rudp
-#	il
 
 port
 	int cpuserver = 1;
 
 boot cpu
 	tcp
-#	il
 
 bootdir
 	bootpccpu.out boot
 	/386/bin/ip/ipconfig ipconfig
 	/386/bin/auth/factotum
+	/386/bin/usb/usbd

+ 4 - 0
sys/src/9/pc/pccpuf

@@ -32,6 +32,7 @@ dev
 
 	uart
 	usb
+	kbin
 
 link
 	realmode
@@ -55,6 +56,7 @@ link
 	etherigbe	pci ethermii
 	ethervgbe	pci ethermii
 	ethervt6102	pci ethermii
+	ethervt6105m	pci ethermii
 #	etherm10g	pci
 	ethersink
 	ethersmc	devi82365 cis
@@ -64,6 +66,7 @@ link
 	loopbackmedium
 	usbuhci
 	usbohci
+	usbehci
 
 misc
 	archmp		mp apic
@@ -125,3 +128,4 @@ bootdir
 #	/386/bin/disk/kfs
 	/386/bin/fossil/fossil
 	/386/bin/venti/venti
+	/386/bin/usb/usbd

+ 5 - 2
sys/src/9/pc/pcdisk

@@ -59,6 +59,7 @@ link
 	etherigbe	pci ethermii
 	ethervgbe	pci ethermii
 	ethervt6102	pci ethermii
+	ethervt6105m	pci ethermii
 	ethersink
 	ethersmc	devi82365 cis
 	etherwavelan	wavelan devi82365 cis pci
@@ -67,6 +68,8 @@ link
 	netdevmedium
 	loopbackmedium
 	usbuhci
+	usbohci
+	usbehci
 
 misc
 	archmp		mp apic
@@ -106,20 +109,19 @@ misc
 ip
 	tcp
 	udp
+	rudp
 	ipifc
 	icmp
 	icmp6
 	gre
 	ipmux
 	esp
-#	il
 
 port
 	int cpuserver = 0;
 
 boot boot #S/sdC0/
 	tcp
-#	il
 	local
 
 bootdir
@@ -128,3 +130,4 @@ bootdir
 	/386/bin/auth/factotum
 	/386/bin/disk/kfs
 	/386/bin/cfs
+	/386/bin/usb/usbd

+ 5 - 1
sys/src/9/pc/pcf

@@ -59,6 +59,7 @@ link
 	etherigbe	pci ethermii
 	ethervgbe	pci ethermii
 	ethervt6102	pci ethermii
+	ethervt6105m	pci ethermii
 	ethersink
 	ethersmc	devi82365 cis
 	etherwavelan	wavelan devi82365 cis pci
@@ -67,6 +68,8 @@ link
 	netdevmedium
 	loopbackmedium
 	usbuhci
+	usbohci
+	usbehci
 
 misc
 	archmp		mp apic
@@ -105,6 +108,7 @@ misc
 ip
 	tcp
 	udp
+	rudp
 	ipifc
 	icmp
 	icmp6
@@ -123,6 +127,6 @@ bootdir
 	bootpcf.out boot
 	/386/bin/ip/ipconfig