1.\" $NetBSD: usb.4,v 1.112 2020/05/16 13:46:10 maya Exp $ 2.\" 3.\" Copyright (c) 1999-2014 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.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28.\" POSSIBILITY OF SUCH DAMAGE. 29.\" 30.Dd May 16, 2020 31.Dt USB 4 32.Os 33.Sh NAME 34.Nm usb 35.Nd Universal Serial Bus driver 36.Sh SYNOPSIS 37.Cd "ehci* at cardbus? function ?" 38.Cd "ehci* at pci? dev ? function ?" 39.Cd "ohci* at cardbus? function ?" 40.Cd "ohci* at pci? dev ? function ?" 41.Cd "xhci* at pci? dev ? function ?" 42.Cd "slhci* at isa? port ? irq ?" 43.Cd "slhci* at pcmcia? function ?" 44.Cd "uhci* at cardbus? function ?" 45.Cd "uhci* at pci? dev ? function ?" 46.Cd "usb* at ehci?" 47.Cd "usb* at ohci?" 48.Cd "usb* at uhci?" 49.Cd "usb* at slhci?" 50.Cd "uhub* at usb?" 51.Cd "uhub* at uhub? port ? configuration ? interface ? vendor ? product ? release ?" 52.Cd "XX* at uhub? port ? configuration ? interface ? vendor ? product ? release ?" 53.Pp 54.Cd options USBVERBOSE 55.Pp 56.In dev/usb/usb.h 57.In dev/usb/usbhid.h 58.Sh DESCRIPTION 59.Nx 60provides machine-independent bus support and drivers for USB devices. 61.Pp 62The 63.Nx 64.Nm 65driver has three layers (like 66.Xr scsi 4 67and 68.Xr pcmcia 4 ) : 69the controller, the bus, and the device layer. 70The controller attaches to a physical bus (like 71.Xr pci 4 ) . 72The USB bus attaches to the controller and the root hub attaches 73to the bus. 74Further devices, which may include further hubs, 75attach to other hubs. 76The attachment forms the same tree structure as the physical 77USB device tree. 78For each USB device there may be additional drivers attached to it. 79.Pp 80The 81.Cm uhub 82device controls USB hubs and must always be present since there is 83at least a root hub in any USB system. 84.Pp 85.Nx 86supports the following machine-independent USB drivers: 87.Ss Storage devices 88.Bl -tag -width 12n -offset ind -compact 89.It Xr umass 4 90USB Mass Storage Devices, e.g., external disk drives 91.El 92.Ss Wired network interfaces 93.Bl -tag -width 12n -offset ind -compact 94.It Xr aue 4 95ADMtek AN986/ADM8511 Pegasus family 10/100 USB Ethernet device 96.It Xr axe 4 97ASIX Electronics AX88172/AX88178/AX88772 10/100/Gigabit USB Ethernet device 98.It Xr axen 4 99ASIX Electronics AX88178a/AX88179 10/100/Gigabit USB Ethernet device 100.It Xr cdce 4 101USB Communication Device Class Ethernet device 102.It Xr cue 4 103CATC USB-EL1201A USB Ethernet device 104.It Xr kue 4 105Kawasaki LSI KL5KUSB101B USB Ethernet device 106.It Xr udav 4 107Davicom DM9601 10/100 USB Ethernet device 108.It Xr url 4 109Realtek RTL8150L 10/100 USB Ethernet device 110.It Xr urndis 4 111USB Remote NDIS Ethernet device 112.El 113.Ss Wireless network interfaces 114.Bl -tag -width 12n -offset ind -compact 115.It Xr atu 4 116Atmel AT76C50x IEEE 802.11b wireless network device 117.It Xr ral 4 118Ralink Technology USB IEEE 802.11b/g wireless network device 119.It Xr rum 4 120Ralink Technology USB IEEE 802.11a/b/g wireless network device 121.It Xr run 4 122Ralink Technology USB IEEE 802.11a/b/g/n wireless network device 123.It Xr ubt 4 124USB Bluetooth dongles 125.It Xr upgt 4 126Conexant/Intersil PrismGT SoftMAC USB 802.11b/g wireless network device 127.It Xr urtwn 4 128Realtek RTL8188CU/RTL8192CU USB IEEE 802.11b/g/n wireless network device 129.It Xr zyd 4 130ZyDAS ZD1211/ZD1211B USB IEEE 802.11b/g wireless network device 131.El 132.Ss Serial and parallel interfaces 133.Bl -tag -width 12n -offset ind -compact 134.It Xr ubsa 4 135Belkin USB serial adapter 136.It Xr uchcom 4 137WinChipHead CH341/340 based USB serial adapter 138.It Xr ucom 4 139USB tty support 140.It Xr ucycom 4 141Cypress microcontroller based USB serial adapter 142.It Xr uftdi 4 143FT8U100AX USB serial adapter 144.It Xr ugensa 4 145USB generic serial adapter 146.It Xr uhmodem 4 147USB Huawei 3G wireless modem device 148.It Xr uipaq 4 149iPAQ USB units 150.It Xr ukyopon 4 151USB Kyocera AIR-EDGE PHONE device 152.It Xr ulpt 4 153USB printer support 154.It Xr umct 4 155MCT USB-RS232 USB serial adapter 156.It Xr umodem 4 157USB modem support 158.It Xr uplcom 4 159Prolific PL-2303 USB serial adapter 160.It Xr uslsa 4 161Silicon Laboratories CP2101/CP2102 based USB serial adapter 162.It Xr uvisor 4 163USB Handspring Visor 164.It Xr uvscom 4 165SUNTAC Slipper U VS-10U USB serial adapter 166.El 167.Ss Audio devices 168.Bl -tag -width 12n -offset ind -compact 169.It Xr uaudio 4 170USB audio devices 171.It Xr umidi 4 172USB MIDI devices 173.El 174.Ss Radio receiver devices 175.Bl -tag -width 12n -offset ind -compact 176.It Xr udsbr 4 177D-Link DSB-R100 USB radio device 178.El 179.Ss Human Interface Devices 180.Bl -tag -width 12n -offset ind -compact 181.It Xr uhid 4 182Generic driver for Human Interface Devices 183.It Xr uhidev 4 184Base driver for all Human Interface Devices 185.It Xr ukbd 4 186USB keyboards that follow the boot protocol 187.It Xr ums 4 188USB mouse devices 189.El 190.Ss Miscellaneous devices 191.Bl -tag -width 12n -offset ind -compact 192.It Xr stuirda 4 193Sigmaltel 4116/4220 USB-IrDA bridge 194.It Xr uberry 4 195Battery charging RIM BlackBerry phones via USB 196.\" .It Xr udsir 4 197.\" KingSun/DonShine USB IrDA bridge 198.It Xr uep 4 199USB eGalax touch-panel 200.It Xr ugen 4 201USB generic devices 202.It Xr uipad 4 203Battery charging iOS devices via USB 204.It Xr uirda 4 205USB IrDA bridges 206.It Xr upl 4 207Prolific based host-to-host adapters 208.It Xr uscanner 4 209USB scanner support 210.It Xr usscanner 4 211SCSI-over-USB scanners 212.It Xr ustir 4 213SigmaTel STIr4200 USB IrDA bridges 214.It Xr utoppy 4 215Topfield TF5000PVR range of digital video recorders 216.El 217.Sh INTRODUCTION TO USB 218The USB 1.x is a 12 Mb/s serial bus with 1.5 Mb/s for low speed devices. 219USB 2.x handles 480 Mb/s. 220Each USB has a host controller that is the master of the bus; 221all other devices on the bus only speak when spoken to. 222.Pp 223There can be up to 127 devices (apart from the host controller) 224on a bus, each with its own address. 225The addresses are assigned 226dynamically by the host when each device is attached to the bus. 227.Pp 228Within each device there can be up to 16 endpoints. 229Each endpoint 230is individually addressed and the addresses are static. 231Each of these endpoints will communicate in one of four different modes: 232control, isochronous, bulk, or interrupt. 233A device always has at least one endpoint. 234This endpoint has address 0 and is a control 235endpoint and is used to give commands to and extract basic data, 236such as descriptors, from the device. 237Each endpoint, except the control endpoint, is unidirectional. 238.Pp 239The endpoints in a device are grouped into interfaces. 240An interface is a logical unit within a device; e.g., 241a compound device with both a keyboard and a trackball would present 242one interface for each. 243An interface can sometimes be set into different modes, 244called alternate settings, which affects how it operates. 245Different alternate settings can have different endpoints 246within it. 247.Pp 248A device may operate in different configurations. 249Depending on the 250configuration the device may present different sets of endpoints 251and interfaces. 252.Pp 253Each device located on a hub has several 254.Xr config 1 255locators: 256.Bl -tag -compact -width xxxxxxxxx 257.It Cd port 258this is the number of the port on closest upstream hub. 259.It Cd configuration 260this is the configuration the device must be in for this driver to attach. 261This locator does not set the configuration; it is iterated by the bus 262enumeration. 263.It Cd interface 264this is the interface number within a device that an interface driver 265attaches to. 266.It Cd vendor 267this is the 16 bit vendor id of the device. 268.It Cd product 269this is the 16 bit product id of the device. 270.It Cd release 271this is the 16 bit release (revision) number of the device. 272.El 273The first locator can be used to pin down a particular device 274according to its physical position in the device tree. 275The last three locators can be used to pin down a particular 276device according to what device it actually is. 277.Pp 278The bus enumeration of the USB bus proceeds in several steps: 279.Bl -enum 280.It 281Any device specific driver can attach to the device. 282.It 283If none is found, any device class specific driver can attach. 284.It 285If none is found, all configurations are iterated over. 286For each configuration all the interface are iterated over and interface 287drivers can attach. 288If any interface driver attached in a certain 289configuration the iteration over configurations is stopped. 290.It 291If still no drivers have been found, the generic USB driver can attach. 292.El 293.Sh USB CONTROLLER INTERFACE 294Use the following to get access to the USB specific structures and defines. 295.Bd -literal 296#include <dev/usb/usb.h> 297.Ed 298.Pp 299The 300.Pa /dev/usbN 301can be opened and a few operations can be performed on it. 302The 303.Xr poll 2 304system call will say that I/O is possible on the controller device when a 305USB device has been connected or disconnected to the bus. 306.Pp 307The following 308.Xr ioctl 2 309commands are supported on the controller device: 310.Bl -tag -width xxxxxx 311.\" .It Dv USB_DISCOVER 312.\" This command will cause a complete bus discovery to be initiated. 313.\" If any devices attached or detached from the bus they will be 314.\" processed during this command. 315.\" This is the only way that new devices are found on the bus. 316.It Dv USB_DEVICEINFO Fa "struct usb_device_info" 317This command can be used to retrieve some information about a device 318on the bus. 319The 320.Va addr 321field should be filled before the call and the other fields will 322be filled by information about the device on that address. 323Should no such device exist an error is reported. 324.Bd -literal 325struct usb_device_info { 326 uint8_t udi_bus; 327 uint8_t udi_addr; 328 usb_event_cookie_t udi_cookie; 329 char udi_product[USB_MAX_ENCODED_STRING_LEN]; 330 char udi_vendor[USB_MAX_ENCODED_STRING_LEN]; 331 char udi_release[8]; 332 char udi_serial[USB_MAX_ENCODED_STRING_LEN]; 333 uint16_t udi_productNo; 334 uint16_t udi_vendorNo; 335 uint16_t udi_releaseNo; 336 uint8_t udi_class; 337 uint8_t udi_subclass; 338 uint8_t udi_protocol; 339 uint8_t udi_config; 340 uint8_t udi_speed; 341#define USB_SPEED_LOW 1 342#define USB_SPEED_FULL 2 343#define USB_SPEED_HIGH 3 344 int udi_power; 345 int udi_nports; 346 char udi_devnames[USB_MAX_DEVNAMES][USB_MAX_DEVNAMELEN]; 347 uint8_t udi_ports[16]; 348#define USB_PORT_ENABLED 0xff 349#define USB_PORT_SUSPENDED 0xfe 350#define USB_PORT_POWERED 0xfd 351#define USB_PORT_DISABLED 0xfc 352}; 353.Ed 354.Pp 355The 356.Va product , 357.Va vendor , 358.Va release , 359and 360.Va serial 361fields contain self-explanatory descriptions of the device. 362.Pp 363The 364.Va class 365field contains the device class. 366.Pp 367The 368.Va config 369field shows the current configuration of the device. 370.Pp 371The 372.Va lowspeed 373field 374is set if the device is a USB low speed device. 375.Pp 376The 377.Va power 378field shows the power consumption in milli-amps drawn at 5 volts, 379or zero if the device is self powered. 380.Pp 381If the device is a hub the 382.Va nports 383field is non-zero and the 384.Va ports 385field contains the addresses of the connected devices. 386If no device is connected to a port one of the 387.Va USB_PORT_* 388values indicates its status. 389.It Dv USB_DEVICESTATS Fa "struct usb_device_stats" 390This command retrieves statistics about the controller. 391.Bd -literal 392struct usb_device_stats { 393 u_long uds_requests[4]; 394}; 395.Ed 396.Pp 397The 398.Va requests 399field is indexed by the transfer kind, i.e. 400.Va UE_* , 401and indicates how many transfers of each kind have been completed 402by the controller. 403.It Dv USB_REQUEST Fa "struct usb_ctl_request" 404This command can be used to execute arbitrary requests on the control pipe. 405This is 406.Em DANGEROUS 407and should be used with great care since it 408can destroy the bus integrity. 409.El 410.Pp 411The include file 412.In dev/usb/usb.h 413contains definitions for the types used by the various 414.Xr ioctl 2 415calls. 416The naming convention of the fields for the various USB 417descriptors exactly follows the naming in the USB specification. 418Byte sized fields can be accessed directly, but word (16 bit) 419sized fields must be access by the 420.Fn UGETW field 421and 422.Fn USETW field value 423macros to handle byte order and alignment properly. 424.Pp 425The include file 426.In dev/usb/usbhid.h 427similarly contains the definitions for 428Human Interface Devices (HID). 429.Sh USB EVENT INTERFACE 430All USB events are reported via the 431.Pa /dev/usb 432device. 433This devices can be opened for reading and each 434.Xr read 2 435will yield an event record (if something has happened). 436The 437.Xr poll 2 438system call can be used to determine if an event record is available 439for reading. 440.Pp 441The event record has the following definition: 442.Bd -literal 443struct usb_event { 444 int ue_type; 445#define USB_EVENT_CTRLR_ATTACH 1 446#define USB_EVENT_CTRLR_DETACH 2 447#define USB_EVENT_DEVICE_ATTACH 3 448#define USB_EVENT_DEVICE_DETACH 4 449#define USB_EVENT_DRIVER_ATTACH 5 450#define USB_EVENT_DRIVER_DETACH 6 451 struct timespec ue_time; 452 union { 453 struct { 454 int ue_bus; 455 } ue_ctrlr; 456 struct usb_device_info ue_device; 457 struct { 458 usb_event_cookie_t ue_cookie; 459 char ue_devname[16]; 460 } ue_driver; 461 } u; 462}; 463.Ed 464.Pp 465The 466.Va ue_type 467field identifies the type of event that is described. 468The possible events are attach/detach of a host controller, 469a device, or a device driver. 470The union contains information 471pertinent to the different types of events. 472.Pp 473The 474.Va ue_bus 475contains the number of the USB bus for host controller events. 476.Pp 477The 478.Va ue_device 479record contains information about the device in a device event event. 480.Pp 481The 482.Va ue_cookie 483is an opaque value that uniquely determines which 484device a device driver has been attached to (i.e., it equals 485the cookie value in the device that the driver attached to). 486The 487.Va ue_devname 488contains the name of the device (driver) as seen in, e.g., 489kernel messages. 490.Pp 491Note that there is a separation between device and device 492driver events. 493A device event is generated when a physical 494USB device is attached or detached. 495A single USB device may 496have zero, one, or many device drivers associated with it. 497.Sh KERNEL THREADS 498For each USB bus, i.e., for each host controller, there is 499a kernel thread that handles attach and detach of devices on 500that bus. 501The thread is named 502.Va usbN 503where 504.Va N 505is the bus number. 506.Pp 507In addition there is a kernel thread, 508.Va usbtask , 509which handles various minor tasks that are initiated from 510an interrupt context, but need to sleep, e.g., time-out 511abort of transfers. 512.Sh SEE ALSO 513.Xr usbhidaction 1 , 514.Xr usbhidctl 1 , 515.Xr cardbus 4 , 516.Xr ehci 4 , 517.Xr isa 4 , 518.Xr ohci 4 , 519.Xr pci 4 , 520.Xr pcmcia 4 , 521.Xr slhci 4 , 522.Xr uhci 4 , 523.Xr xhci 4 , 524.Xr usbdevs 8 525.Rs 526.%T Universal Serial Bus Specifications Documents 527.%U http://www.usb.org/developers/docs/ 528.Re 529.Sh HISTORY 530The 531.Nm 532driver 533appeared in 534.Nx 1.4 . 535.Sh BUGS 536There should be a serial number locator, but 537.Nx 538does not have string valued locators. 539