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