1.\" $NetBSD: usb.4,v 1.111 2020/01/29 18:39:04 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 January 29, 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.It Xr uyap 4 217USB YAP phone firmware loader 218.El 219.Sh INTRODUCTION TO USB 220The USB 1.x is a 12 Mb/s serial bus with 1.5 Mb/s for low speed devices. 221USB 2.x handles 480 Mb/s. 222Each USB has a host controller that is the master of the bus; 223all other devices on the bus only speak when spoken to. 224.Pp 225There can be up to 127 devices (apart from the host controller) 226on a bus, each with its own address. 227The addresses are assigned 228dynamically by the host when each device is attached to the bus. 229.Pp 230Within each device there can be up to 16 endpoints. 231Each endpoint 232is individually addressed and the addresses are static. 233Each of these endpoints will communicate in one of four different modes: 234control, isochronous, bulk, or interrupt. 235A device always has at least one endpoint. 236This endpoint has address 0 and is a control 237endpoint and is used to give commands to and extract basic data, 238such as descriptors, from the device. 239Each endpoint, except the control endpoint, is unidirectional. 240.Pp 241The endpoints in a device are grouped into interfaces. 242An interface is a logical unit within a device; e.g., 243a compound device with both a keyboard and a trackball would present 244one interface for each. 245An interface can sometimes be set into different modes, 246called alternate settings, which affects how it operates. 247Different alternate settings can have different endpoints 248within it. 249.Pp 250A device may operate in different configurations. 251Depending on the 252configuration the device may present different sets of endpoints 253and interfaces. 254.Pp 255Each device located on a hub has several 256.Xr config 1 257locators: 258.Bl -tag -compact -width xxxxxxxxx 259.It Cd port 260this is the number of the port on closest upstream hub. 261.It Cd configuration 262this is the configuration the device must be in for this driver to attach. 263This locator does not set the configuration; it is iterated by the bus 264enumeration. 265.It Cd interface 266this is the interface number within a device that an interface driver 267attaches to. 268.It Cd vendor 269this is the 16 bit vendor id of the device. 270.It Cd product 271this is the 16 bit product id of the device. 272.It Cd release 273this is the 16 bit release (revision) number of the device. 274.El 275The first locator can be used to pin down a particular device 276according to its physical position in the device tree. 277The last three locators can be used to pin down a particular 278device according to what device it actually is. 279.Pp 280The bus enumeration of the USB bus proceeds in several steps: 281.Bl -enum 282.It 283Any device specific driver can attach to the device. 284.It 285If none is found, any device class specific driver can attach. 286.It 287If none is found, all configurations are iterated over. 288For each configuration all the interface are iterated over and interface 289drivers can attach. 290If any interface driver attached in a certain 291configuration the iteration over configurations is stopped. 292.It 293If still no drivers have been found, the generic USB driver can attach. 294.El 295.Sh USB CONTROLLER INTERFACE 296Use the following to get access to the USB specific structures and defines. 297.Bd -literal 298#include <dev/usb/usb.h> 299.Ed 300.Pp 301The 302.Pa /dev/usbN 303can be opened and a few operations can be performed on it. 304The 305.Xr poll 2 306system call will say that I/O is possible on the controller device when a 307USB device has been connected or disconnected to the bus. 308.Pp 309The following 310.Xr ioctl 2 311commands are supported on the controller device: 312.Bl -tag -width xxxxxx 313.\" .It Dv USB_DISCOVER 314.\" This command will cause a complete bus discovery to be initiated. 315.\" If any devices attached or detached from the bus they will be 316.\" processed during this command. 317.\" This is the only way that new devices are found on the bus. 318.It Dv USB_DEVICEINFO Fa "struct usb_device_info" 319This command can be used to retrieve some information about a device 320on the bus. 321The 322.Va addr 323field should be filled before the call and the other fields will 324be filled by information about the device on that address. 325Should no such device exist an error is reported. 326.Bd -literal 327struct usb_device_info { 328 uint8_t udi_bus; 329 uint8_t udi_addr; 330 usb_event_cookie_t udi_cookie; 331 char udi_product[USB_MAX_ENCODED_STRING_LEN]; 332 char udi_vendor[USB_MAX_ENCODED_STRING_LEN]; 333 char udi_release[8]; 334 char udi_serial[USB_MAX_ENCODED_STRING_LEN]; 335 uint16_t udi_productNo; 336 uint16_t udi_vendorNo; 337 uint16_t udi_releaseNo; 338 uint8_t udi_class; 339 uint8_t udi_subclass; 340 uint8_t udi_protocol; 341 uint8_t udi_config; 342 uint8_t udi_speed; 343#define USB_SPEED_LOW 1 344#define USB_SPEED_FULL 2 345#define USB_SPEED_HIGH 3 346 int udi_power; 347 int udi_nports; 348 char udi_devnames[USB_MAX_DEVNAMES][USB_MAX_DEVNAMELEN]; 349 uint8_t udi_ports[16]; 350#define USB_PORT_ENABLED 0xff 351#define USB_PORT_SUSPENDED 0xfe 352#define USB_PORT_POWERED 0xfd 353#define USB_PORT_DISABLED 0xfc 354}; 355.Ed 356.Pp 357The 358.Va product , 359.Va vendor , 360.Va release , 361and 362.Va serial 363fields contain self-explanatory descriptions of the device. 364.Pp 365The 366.Va class 367field contains the device class. 368.Pp 369The 370.Va config 371field shows the current configuration of the device. 372.Pp 373The 374.Va lowspeed 375field 376is set if the device is a USB low speed device. 377.Pp 378The 379.Va power 380field shows the power consumption in milli-amps drawn at 5 volts, 381or zero if the device is self powered. 382.Pp 383If the device is a hub the 384.Va nports 385field is non-zero and the 386.Va ports 387field contains the addresses of the connected devices. 388If no device is connected to a port one of the 389.Va USB_PORT_* 390values indicates its status. 391.It Dv USB_DEVICESTATS Fa "struct usb_device_stats" 392This command retrieves statistics about the controller. 393.Bd -literal 394struct usb_device_stats { 395 u_long uds_requests[4]; 396}; 397.Ed 398.Pp 399The 400.Va requests 401field is indexed by the transfer kind, i.e. 402.Va UE_* , 403and indicates how many transfers of each kind have been completed 404by the controller. 405.It Dv USB_REQUEST Fa "struct usb_ctl_request" 406This command can be used to execute arbitrary requests on the control pipe. 407This is 408.Em DANGEROUS 409and should be used with great care since it 410can destroy the bus integrity. 411.El 412.Pp 413The include file 414.In dev/usb/usb.h 415contains definitions for the types used by the various 416.Xr ioctl 2 417calls. 418The naming convention of the fields for the various USB 419descriptors exactly follows the naming in the USB specification. 420Byte sized fields can be accessed directly, but word (16 bit) 421sized fields must be access by the 422.Fn UGETW field 423and 424.Fn USETW field value 425macros to handle byte order and alignment properly. 426.Pp 427The include file 428.In dev/usb/usbhid.h 429similarly contains the definitions for 430Human Interface Devices (HID). 431.Sh USB EVENT INTERFACE 432All USB events are reported via the 433.Pa /dev/usb 434device. 435This devices can be opened for reading and each 436.Xr read 2 437will yield an event record (if something has happened). 438The 439.Xr poll 2 440system call can be used to determine if an event record is available 441for reading. 442.Pp 443The event record has the following definition: 444.Bd -literal 445struct usb_event { 446 int ue_type; 447#define USB_EVENT_CTRLR_ATTACH 1 448#define USB_EVENT_CTRLR_DETACH 2 449#define USB_EVENT_DEVICE_ATTACH 3 450#define USB_EVENT_DEVICE_DETACH 4 451#define USB_EVENT_DRIVER_ATTACH 5 452#define USB_EVENT_DRIVER_DETACH 6 453 struct timespec ue_time; 454 union { 455 struct { 456 int ue_bus; 457 } ue_ctrlr; 458 struct usb_device_info ue_device; 459 struct { 460 usb_event_cookie_t ue_cookie; 461 char ue_devname[16]; 462 } ue_driver; 463 } u; 464}; 465.Ed 466.Pp 467The 468.Va ue_type 469field identifies the type of event that is described. 470The possible events are attach/detach of a host controller, 471a device, or a device driver. 472The union contains information 473pertinent to the different types of events. 474.Pp 475The 476.Va ue_bus 477contains the number of the USB bus for host controller events. 478.Pp 479The 480.Va ue_device 481record contains information about the device in a device event event. 482.Pp 483The 484.Va ue_cookie 485is an opaque value that uniquely determines which 486device a device driver has been attached to (i.e., it equals 487the cookie value in the device that the driver attached to). 488The 489.Va ue_devname 490contains the name of the device (driver) as seen in, e.g., 491kernel messages. 492.Pp 493Note that there is a separation between device and device 494driver events. 495A device event is generated when a physical 496USB device is attached or detached. 497A single USB device may 498have zero, one, or many device drivers associated with it. 499.Sh KERNEL THREADS 500For each USB bus, i.e., for each host controller, there is 501a kernel thread that handles attach and detach of devices on 502that bus. 503The thread is named 504.Va usbN 505where 506.Va N 507is the bus number. 508.Pp 509In addition there is a kernel thread, 510.Va usbtask , 511which handles various minor tasks that are initiated from 512an interrupt context, but need to sleep, e.g., time-out 513abort of transfers. 514.Sh SEE ALSO 515.Xr usbhidaction 1 , 516.Xr usbhidctl 1 , 517.Xr cardbus 4 , 518.Xr ehci 4 , 519.Xr isa 4 , 520.Xr ohci 4 , 521.Xr pci 4 , 522.Xr pcmcia 4 , 523.Xr slhci 4 , 524.Xr uhci 4 , 525.Xr xhci 4 , 526.Xr usbdevs 8 527.Rs 528.%T Universal Serial Bus Specifications Documents 529.%U http://www.usb.org/developers/docs/ 530.Re 531.Sh HISTORY 532The 533.Nm 534driver 535appeared in 536.Nx 1.4 . 537.Sh BUGS 538There should be a serial number locator, but 539.Nx 540does not have string valued locators. 541