1.\" Copyright (c) 1997, 1998 2.\" Nick Hibma <n_hibma@FreeBSD.org>. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. All advertising materials mentioning features or use of this software 13.\" must display the following acknowledgement: 14.\" This product includes software developed by Bill Paul. 15.\" 4. Neither the name of the author nor the names of any co-contributors 16.\" may be used to endorse or promote products derived from this software 17.\" without specific prior written permission. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY NICK HIBMA AND CONTRIBUTORS ``AS IS'' AND 20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22.\" ARE DISCLAIMED. IN NO EVENT SHALL NICK HIBMA OR THE VOICES IN HIS HEAD 23.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 29.\" THE POSSIBILITY OF SUCH DAMAGE. 30.\" 31.\" $FreeBSD$ 32.\" 33.Dd February 21, 1999 34.Dt USB 4 35.Os 36.Sh NAME 37.Nm usb 38.Nd Universal Serial Bus 39.Sh SYNOPSIS 40.Cd "device usb" 41.Pp 42.In dev/usb/usb.h 43.In dev/usb/usbhid.h 44.Sh DESCRIPTION 45.Fx 46provides machine-independent bus support and drivers for 47.Tn USB 48devices. 49.Pp 50The 51.Nm 52driver has three layers: the controller, the bus, and the 53device layer. 54The controller attaches to a physical bus 55(like 56.Xr pci 4 ) . 57The 58.Tn USB 59bus attaches to the controller, and the root hub attaches 60to the controller. 61Any devices attached to the bus will attach to the root hub 62or another hub attached to the 63.Tn USB 64bus. 65.Pp 66The 67.Nm uhub 68device will always be present as it is needed for the 69root hub. 70.Sh INTRODUCTION TO USB 71The 72.Tn USB 73is a 12 Mb/s serial bus (1.5 Mb/s for low speed devices). 74Each 75.Tn USB 76has a host controller that is the master of the bus; 77all other devices on the bus only speak when spoken to. 78.Pp 79There can be up to 127 devices (apart from the host controller) 80on a bus, each with its own address. 81The addresses are assigned 82dynamically by the host when each device is attached to the bus. 83.Pp 84Within each device there can be up to 16 endpoints. 85Each endpoint 86is individually addressed and the addresses are static. 87Each of these endpoints will communicate in one of four different modes: 88.Em control , isochronous , bulk , 89or 90.Em interrupt . 91A device always has at least one endpoint. 92This endpoint has address 0 and is a control 93endpoint and is used to give commands to and extract basic data, 94such as descriptors, from the device. 95Each endpoint, except the control endpoint, is unidirectional. 96.Pp 97The endpoints in a device are grouped into interfaces. 98An interface is a logical unit within a device; e.g.\& 99a compound device with both a keyboard and a trackball would present 100one interface for each. 101An interface can sometimes be set into different modes, 102called alternate settings, which affects how it operates. 103Different alternate settings can have different endpoints 104within it. 105.Pp 106A device may operate in different configurations. 107Depending on the 108configuration, the device may present different sets of endpoints 109and interfaces. 110.Pp 111Each device located on a hub has several 112.Xr config 8 113locators: 114.Bl -tag -compact -width xxxxxx 115.It Cd port 116this is the number of the port on the closest upstream hub. 117.It Cd configuration 118this is the configuration the device must be in for this driver to attach. 119This locator does not set the configuration; it is iterated by the bus 120enumeration. 121.It Cd interface 122this is the interface number within a device that an interface driver 123attaches to. 124.It Cd vendor 125this is the 16 bit vendor id of the device. 126.It Cd product 127this is the 16 bit product id of the device. 128.It Cd release 129this is the 16 bit release (revision) number of the device. 130.El 131The first locator can be used to pin down a particular device 132according to its physical position in the device tree. 133The last three locators can be used to pin down a particular 134device according to what device it actually is. 135.Pp 136The bus enumeration of the 137.Tn USB 138bus proceeds in several steps: 139.Bl -enum 140.It 141Any device specific driver can attach to the device. 142.It 143If none is found, any device class specific driver can attach. 144.It 145If none is found, all configurations are iterated over. 146For each configuration, all the interfaces are iterated over, and interface 147drivers can attach. 148If any interface driver attached in a certain 149configuration, the iteration over configurations is stopped. 150.It 151If still no drivers have been found, the generic 152.Tn USB 153driver can attach. 154.El 155.Sh USB CONTROLLER INTERFACE 156Use the following to get access to the 157.Tn USB 158specific structures and defines. 159.Pp 160.In dev/usb/usb.h 161.Pp 162The 163.Pa /dev/usb Ns Ar N 164can be opened and a few operations can be performed on it. 165The 166.Xr poll 2 167system call will say that I/O is possible on the controller device when a 168.Tn USB 169device has been connected or disconnected to the bus. 170.Pp 171The following 172.Xr ioctl 2 173commands are supported on the controller device: 174.Bl -tag -width xxxxxx 175.It Dv USB_DISCOVER 176This command will cause a complete bus discovery to be initiated. 177If any devices attached or detached from the bus they will be 178processed during this command. 179This is the only way that new devices are found on the bus. 180.It Dv USB_DEVICEINFO Vt "struct usb_device_info" 181This command can be used to retrieve some information about a device 182on the bus. 183The 184.Va addr 185field should be filled before the call and the other fields will 186be filled by information about the device on that address. 187Should no such device exist, an error is reported. 188.Bd -literal 189struct usb_device_info { 190 u_int8_t bus; 191 u_int8_t addr; 192 usb_event_cookie_t cookie; 193 char product[USB_MAX_STRING_LEN]; 194 char vendor[USB_MAX_STRING_LEN]; 195 char release[8]; 196 u_int16_t productNo; 197 u_int16_t vendorNo; 198 u_int16_t releaseNo; 199 u_int8_t class; 200 u_int8_t subclass; 201 u_int8_t protocol; 202 u_int8_t config; 203 u_int8_t lowspeed; 204 int power; 205 int nports; 206 char devnames[USB_MAX_DEVNAMES][USB_MAX_DEVNAMELEN]; 207 u_int8_t ports[16]; 208#define USB_PORT_ENABLED 0xff 209#define USB_PORT_SUSPENDED 0xfe 210#define USB_PORT_POWERED 0xfd 211#define USB_PORT_DISABLED 0xfc 212}; 213.Ed 214.Pp 215.Va bus 216and 217.Va addr 218contain the topological information for the device. 219.Va devnames 220contains the device names of the connected drivers. 221For example, the 222third 223.Tn USB 224Zip drive connected will be 225.Li umass2 . 226The 227.Va product , vendor 228and 229.Va release 230fields contain self-explanatory descriptions of the device. 231.Va productNo , vendorNo , releaseNo , class , subclass 232and 233.Va protocol 234contain the corresponding values from the device descriptors. 235The 236.Va config 237field shows the current configuration of the device. 238.Pp 239.Va lowspeed 240indicates whether the device is a full speed (0) or low speed (1) 241device. 242The 243.Va power 244field shows the power consumption in milli-amps drawn at 5 volts, 245or zero if the device is self powered. 246.Pp 247If the device is a hub, the 248.Va nports 249field is non-zero, and the 250.Va ports 251field contains the addresses of the connected devices. 252If no device is connected to a port, one of the 253.Dv USB_PORT_* 254values indicates its status. 255.It Dv USB_DEVICESTATS Vt "struct usb_device_stats" 256This command retrieves statistics about the controller. 257.Bd -literal 258struct usb_device_stats { 259 u_long requests[4]; 260}; 261.Ed 262.Pp 263The 264.Va requests 265field is indexed by the transfer kind, i.e.\& 266.Dv UE_* , 267and indicates how many transfers of each kind that has been completed 268by the controller. 269.It Dv USB_REQUEST Vt "struct usb_ctl_request" 270This command can be used to execute arbitrary requests on the control pipe. 271This is 272.Em DANGEROUS 273and should be used with great care since it 274can destroy the bus integrity. 275.El 276.Pp 277The include file 278.Aq Pa dev/usb/usb.h 279contains definitions for the types used by the various 280.Xr ioctl 2 281calls. 282The naming convention of the fields for the various 283.Tn USB 284descriptors exactly follows the naming in the 285.Tn USB 286specification. 287Byte sized fields can be accessed directly, but word (16 bit) 288sized fields must be access by the 289.Fn UGETW field 290and 291.Fn USETW field value 292macros to handle byte order and alignment properly. 293.Pp 294The include file 295.Aq Pa dev/usb/usbhid.h 296similarly contains the definitions for 297Human Interface Devices 298.Pq Tn HID . 299.Sh USB EVENT INTERFACE 300All 301.Tn USB 302events are reported via the 303.Pa /dev/usb 304device. 305This devices can be opened for reading and each 306.Xr read 2 307will yield an event record (if something has happened). 308The 309.Xr poll 2 310system call can be used to determine if an event record is available 311for reading. 312.Pp 313The event record has the following definition: 314.Bd -literal 315struct usb_event { 316 int ue_type; 317#define USB_EVENT_CTRLR_ATTACH 1 318#define USB_EVENT_CTRLR_DETACH 2 319#define USB_EVENT_DEVICE_ATTACH 3 320#define USB_EVENT_DEVICE_DETACH 4 321#define USB_EVENT_DRIVER_ATTACH 5 322#define USB_EVENT_DRIVER_DETACH 6 323 struct timespec ue_time; 324 union { 325 struct { 326 int ue_bus; 327 } ue_ctrlr; 328 struct usb_device_info ue_device; 329 struct { 330 usb_event_cookie_t ue_cookie; 331 char ue_devname[16]; 332 } ue_driver; 333 } u; 334}; 335.Ed 336The 337.Va ue_type 338field identifies the type of event that is described. 339The possible events are attach/detach of a host controller, 340a device, or a device driver. 341The union contains information 342pertinent to the different types of events. 343.Pp 344The 345.Va ue_bus 346contains the number of the 347.Tn USB 348bus for host controller events. 349.Pp 350The 351.Va ue_device 352record contains information about the device in a device event event. 353.Pp 354The 355.Va ue_cookie 356is an opaque value that uniquely determines which which 357device a device driver has been attached to (i.e., it equals 358the cookie value in the device that the driver attached to). 359.Pp 360The 361.Va ue_devname 362contains the name of the device (driver) as seen in, e.g., 363kernel messages. 364.Pp 365Note that there is a separation between device and device 366driver events. 367A device event is generated when a physical 368.Tn USB 369device is attached or detached. 370A single 371.Tn USB 372device may 373have zero, one, or many device drivers associated with it. 374.Sh SEE ALSO 375The 376.Tn USB 377specifications can be found at: 378.Pp 379.D1 Pa http://www.usb.org/developers/docs.html 380.Pp 381.Xr usb 3 , 382.Xr aue 4 , 383.Xr cue 4 , 384.Xr kue 4 , 385.Xr ohci 4 , 386.Xr pci 4 , 387.Xr ugen 4 , 388.Xr uhci 4 , 389.Xr uhid 4 , 390.Xr ukbd 4 , 391.Xr umass 4 , 392.Xr ums 4 , 393.Xr urio 4 , 394.Xr uscanner 4 , 395.Xr usbd 8 , 396.Xr usbdevs 8 397.Sh HISTORY 398The 399.Nm 400driver first appeared in 401.Fx 3.0 . 402.Sh AUTHORS 403The 404.Nm 405driver was written by 406.An Lennart Augustsson Aq augustss@carlstedt.se 407for the 408.Nx 409project. 410