Mauro Carvalho Chehab | e463c06 | 2017-04-05 10:23:10 -0300 | [diff] [blame] | 1 | .. _usb-hostside-api: |
| 2 | |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 3 | =========================== |
| 4 | The Linux-USB Host Side API |
| 5 | =========================== |
| 6 | |
| 7 | Introduction to USB on Linux |
| 8 | ============================ |
| 9 | |
| 10 | A Universal Serial Bus (USB) is used to connect a host, such as a PC or |
| 11 | workstation, to a number of peripheral devices. USB uses a tree |
| 12 | structure, with the host as the root (the system's master), hubs as |
| 13 | interior nodes, and peripherals as leaves (and slaves). Modern PCs |
| 14 | support several such trees of USB devices, usually |
| 15 | a few USB 3.0 (5 GBit/s) or USB 3.1 (10 GBit/s) and some legacy |
| 16 | USB 2.0 (480 MBit/s) busses just in case. |
| 17 | |
| 18 | That master/slave asymmetry was designed-in for a number of reasons, one |
| 19 | being ease of use. It is not physically possible to mistake upstream and |
| 20 | downstream or it does not matter with a type C plug (or they are built into the |
| 21 | peripheral). Also, the host software doesn't need to deal with |
| 22 | distributed auto-configuration since the pre-designated master node |
| 23 | manages all that. |
| 24 | |
| 25 | Kernel developers added USB support to Linux early in the 2.2 kernel |
| 26 | series and have been developing it further since then. Besides support |
| 27 | for each new generation of USB, various host controllers gained support, |
| 28 | new drivers for peripherals have been added and advanced features for latency |
| 29 | measurement and improved power management introduced. |
| 30 | |
| 31 | Linux can run inside USB devices as well as on the hosts that control |
| 32 | the devices. But USB device drivers running inside those peripherals |
| 33 | don't do the same things as the ones running inside hosts, so they've |
| 34 | been given a different name: *gadget drivers*. This document does not |
| 35 | cover gadget drivers. |
| 36 | |
| 37 | USB Host-Side API Model |
| 38 | ======================= |
| 39 | |
| 40 | Host-side drivers for USB devices talk to the "usbcore" APIs. There are |
| 41 | two. One is intended for *general-purpose* drivers (exposed through |
| 42 | driver frameworks), and the other is for drivers that are *part of the |
| 43 | core*. Such core drivers include the *hub* driver (which manages trees |
| 44 | of USB devices) and several different kinds of *host controller |
| 45 | drivers*, which control individual busses. |
| 46 | |
| 47 | The device model seen by USB drivers is relatively complex. |
| 48 | |
| 49 | - USB supports four kinds of data transfers (control, bulk, interrupt, |
| 50 | and isochronous). Two of them (control and bulk) use bandwidth as |
| 51 | it's available, while the other two (interrupt and isochronous) are |
| 52 | scheduled to provide guaranteed bandwidth. |
| 53 | |
| 54 | - The device description model includes one or more "configurations" |
| 55 | per device, only one of which is active at a time. Devices are supposed |
| 56 | to be capable of operating at lower than their top |
| 57 | speeds and may provide a BOS descriptor showing the lowest speed they |
| 58 | remain fully operational at. |
| 59 | |
| 60 | - From USB 3.0 on configurations have one or more "functions", which |
| 61 | provide a common functionality and are grouped together for purposes |
| 62 | of power management. |
| 63 | |
| 64 | - Configurations or functions have one or more "interfaces", each of which may have |
| 65 | "alternate settings". Interfaces may be standardized by USB "Class" |
| 66 | specifications, or may be specific to a vendor or device. |
| 67 | |
| 68 | USB device drivers actually bind to interfaces, not devices. Think of |
| 69 | them as "interface drivers", though you may not see many devices |
| 70 | where the distinction is important. *Most USB devices are simple, |
| 71 | with only one function, one configuration, one interface, and one alternate |
| 72 | setting.* |
| 73 | |
| 74 | - Interfaces have one or more "endpoints", each of which supports one |
| 75 | type and direction of data transfer such as "bulk out" or "interrupt |
| 76 | in". The entire configuration may have up to sixteen endpoints in |
| 77 | each direction, allocated as needed among all the interfaces. |
| 78 | |
| 79 | - Data transfer on USB is packetized; each endpoint has a maximum |
| 80 | packet size. Drivers must often be aware of conventions such as |
| 81 | flagging the end of bulk transfers using "short" (including zero |
| 82 | length) packets. |
| 83 | |
| 84 | - The Linux USB API supports synchronous calls for control and bulk |
| 85 | messages. It also supports asynchronous calls for all kinds of data |
| 86 | transfer, using request structures called "URBs" (USB Request |
| 87 | Blocks). |
| 88 | |
| 89 | Accordingly, the USB Core API exposed to device drivers covers quite a |
| 90 | lot of territory. You'll probably need to consult the USB 3.0 |
| 91 | specification, available online from www.usb.org at no cost, as well as |
| 92 | class or device specifications. |
| 93 | |
| 94 | The only host-side drivers that actually touch hardware (reading/writing |
| 95 | registers, handling IRQs, and so on) are the HCDs. In theory, all HCDs |
| 96 | provide the same functionality through the same API. In practice, that's |
| 97 | becoming more true, but there are still differences |
| 98 | that crop up especially with fault handling on the less common controllers. |
| 99 | Different controllers don't |
| 100 | necessarily report the same aspects of failures, and recovery from |
| 101 | faults (including software-induced ones like unlinking an URB) isn't yet |
| 102 | fully consistent. Device driver authors should make a point of doing |
| 103 | disconnect testing (while the device is active) with each different host |
| 104 | controller driver, to make sure drivers don't have bugs of their own as |
| 105 | well as to make sure they aren't relying on some HCD-specific behavior. |
| 106 | |
Mauro Carvalho Chehab | 9a3c8b3 | 2017-04-05 10:22:58 -0300 | [diff] [blame] | 107 | .. _usb_chapter9: |
| 108 | |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 109 | USB-Standard Types |
| 110 | ================== |
| 111 | |
| 112 | In ``<linux/usb/ch9.h>`` you will find the USB data types defined in |
| 113 | chapter 9 of the USB specification. These data types are used throughout |
Mauro Carvalho Chehab | 8a6a285 | 2017-04-16 21:51:06 -0300 | [diff] [blame] | 114 | USB, and in APIs including this host side API, gadget APIs, usb character |
| 115 | devices and debugfs interfaces. |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 116 | |
| 117 | .. kernel-doc:: include/linux/usb/ch9.h |
| 118 | :internal: |
| 119 | |
Mauro Carvalho Chehab | 9a3c8b3 | 2017-04-05 10:22:58 -0300 | [diff] [blame] | 120 | .. _usb_header: |
| 121 | |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 122 | Host-Side Data Types and Macros |
| 123 | =============================== |
| 124 | |
| 125 | The host side API exposes several layers to drivers, some of which are |
| 126 | more necessary than others. These support lifecycle models for host side |
| 127 | drivers and devices, and support passing buffers through usbcore to some |
| 128 | HCD that performs the I/O for the device driver. |
| 129 | |
| 130 | .. kernel-doc:: include/linux/usb.h |
| 131 | :internal: |
| 132 | |
| 133 | USB Core APIs |
| 134 | ============= |
| 135 | |
| 136 | There are two basic I/O models in the USB API. The most elemental one is |
| 137 | asynchronous: drivers submit requests in the form of an URB, and the |
| 138 | URB's completion callback handles the next step. All USB transfer types |
| 139 | support that model, although there are special cases for control URBs |
| 140 | (which always have setup and status stages, but may not have a data |
| 141 | stage) and isochronous URBs (which allow large packets and include |
| 142 | per-packet fault reports). Built on top of that is synchronous API |
| 143 | support, where a driver calls a routine that allocates one or more URBs, |
| 144 | submits them, and waits until they complete. There are synchronous |
| 145 | wrappers for single-buffer control and bulk transfers (which are awkward |
| 146 | to use in some driver disconnect scenarios), and for scatterlist based |
| 147 | streaming i/o (bulk or interrupt). |
| 148 | |
| 149 | USB drivers need to provide buffers that can be used for DMA, although |
| 150 | they don't necessarily need to provide the DMA mapping themselves. There |
| 151 | are APIs to use used when allocating DMA buffers, which can prevent use |
| 152 | of bounce buffers on some systems. In some cases, drivers may be able to |
| 153 | rely on 64bit DMA to eliminate another kind of bounce buffer. |
| 154 | |
| 155 | .. kernel-doc:: drivers/usb/core/urb.c |
| 156 | :export: |
| 157 | |
| 158 | .. kernel-doc:: drivers/usb/core/message.c |
| 159 | :export: |
| 160 | |
| 161 | .. kernel-doc:: drivers/usb/core/file.c |
| 162 | :export: |
| 163 | |
| 164 | .. kernel-doc:: drivers/usb/core/driver.c |
| 165 | :export: |
| 166 | |
| 167 | .. kernel-doc:: drivers/usb/core/usb.c |
| 168 | :export: |
| 169 | |
| 170 | .. kernel-doc:: drivers/usb/core/hub.c |
| 171 | :export: |
| 172 | |
| 173 | Host Controller APIs |
| 174 | ==================== |
| 175 | |
| 176 | These APIs are only for use by host controller drivers, most of which |
| 177 | implement standard register interfaces such as XHCI, EHCI, OHCI, or UHCI. UHCI |
| 178 | was one of the first interfaces, designed by Intel and also used by VIA; |
| 179 | it doesn't do much in hardware. OHCI was designed later, to have the |
| 180 | hardware do more work (bigger transfers, tracking protocol state, and so |
| 181 | on). EHCI was designed with USB 2.0; its design has features that |
| 182 | resemble OHCI (hardware does much more work) as well as UHCI (some parts |
| 183 | of ISO support, TD list processing). XHCI was designed with USB 3.0. It |
| 184 | continues to shift support for functionality into hardware. |
| 185 | |
| 186 | There are host controllers other than the "big three", although most PCI |
| 187 | based controllers (and a few non-PCI based ones) use one of those |
| 188 | interfaces. Not all host controllers use DMA; some use PIO, and there is |
| 189 | also a simulator and a virtual host controller to pipe USB over the network. |
| 190 | |
| 191 | The same basic APIs are available to drivers for all those controllers. |
| 192 | For historical reasons they are in two layers: :c:type:`struct |
| 193 | usb_bus <usb_bus>` is a rather thin layer that became available |
| 194 | in the 2.2 kernels, while :c:type:`struct usb_hcd <usb_hcd>` |
| 195 | is a more featureful layer |
| 196 | that lets HCDs share common code, to shrink driver size and |
| 197 | significantly reduce hcd-specific behaviors. |
| 198 | |
| 199 | .. kernel-doc:: drivers/usb/core/hcd.c |
| 200 | :export: |
| 201 | |
| 202 | .. kernel-doc:: drivers/usb/core/hcd-pci.c |
| 203 | :export: |
| 204 | |
| 205 | .. kernel-doc:: drivers/usb/core/buffer.c |
| 206 | :internal: |
| 207 | |
Mauro Carvalho Chehab | 8a6a285 | 2017-04-16 21:51:06 -0300 | [diff] [blame] | 208 | The USB character device nodes |
| 209 | ============================== |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 210 | |
Mauro Carvalho Chehab | 8a6a285 | 2017-04-16 21:51:06 -0300 | [diff] [blame] | 211 | This chapter presents the Linux character device nodes. You may prefer |
| 212 | to avoid writing new kernel code for your USB driver. User mode device |
| 213 | drivers are usually packaged as applications or libraries, and may use |
| 214 | character devices through some programming library that wraps it. |
| 215 | Such libraries include |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 216 | `libusb <http://libusb.sourceforge.net>`__ for C/C++, and |
| 217 | `jUSB <http://jUSB.sourceforge.net>`__ for Java. |
| 218 | |
Mauro Carvalho Chehab | 9a3c8b3 | 2017-04-05 10:22:58 -0300 | [diff] [blame] | 219 | .. note:: |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 220 | |
Mauro Carvalho Chehab | 8a6a285 | 2017-04-16 21:51:06 -0300 | [diff] [blame] | 221 | - They were used to be implemented via *usbfs*, but this is not part of |
| 222 | the sysfs debug interface. |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 223 | |
Mauro Carvalho Chehab | 8a6a285 | 2017-04-16 21:51:06 -0300 | [diff] [blame] | 224 | - This particular documentation is incomplete, especially with respect |
| 225 | to the asynchronous mode. As of kernel 2.5.66 the code and this |
| 226 | (new) documentation need to be cross-reviewed. |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 227 | |
Mauro Carvalho Chehab | 8a6a285 | 2017-04-16 21:51:06 -0300 | [diff] [blame] | 228 | What files are in "devtmpfs"? |
| 229 | ----------------------------- |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 230 | |
Mauro Carvalho Chehab | 8a6a285 | 2017-04-16 21:51:06 -0300 | [diff] [blame] | 231 | Conventionally mounted at ``/dev/bus/usb/``, usbfs features include: |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 232 | |
Mauro Carvalho Chehab | 8a6a285 | 2017-04-16 21:51:06 -0300 | [diff] [blame] | 233 | - ``/dev/bus/usb//BBB/DDD`` ... magic files exposing the each device's |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 234 | configuration descriptors, and supporting a series of ioctls for |
| 235 | making device requests, including I/O to devices. (Purely for access |
| 236 | by programs.) |
| 237 | |
| 238 | Each bus is given a number (BBB) based on when it was enumerated; within |
| 239 | each bus, each device is given a similar number (DDD). Those BBB/DDD |
| 240 | paths are not "stable" identifiers; expect them to change even if you |
| 241 | always leave the devices plugged in to the same hub port. *Don't even |
| 242 | think of saving these in application configuration files.* Stable |
| 243 | identifiers are available, for user mode applications that want to use |
| 244 | them. HID and networking devices expose these stable IDs, so that for |
| 245 | example you can be sure that you told the right UPS to power down its |
| 246 | second server. "usbfs" doesn't (yet) expose those IDs. |
| 247 | |
Mauro Carvalho Chehab | 8a6a285 | 2017-04-16 21:51:06 -0300 | [diff] [blame] | 248 | /dev/bus/usb//BBB/DDD |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 249 | --------------------- |
| 250 | |
| 251 | Use these files in one of these basic ways: |
| 252 | |
| 253 | *They can be read,* producing first the device descriptor (18 bytes) and |
| 254 | then the descriptors for the current configuration. See the USB 2.0 spec |
| 255 | for details about those binary data formats. You'll need to convert most |
| 256 | multibyte values from little endian format to your native host byte |
| 257 | order, although a few of the fields in the device descriptor (both of |
| 258 | the BCD-encoded fields, and the vendor and product IDs) will be |
| 259 | byteswapped for you. Note that configuration descriptors include |
| 260 | descriptors for interfaces, altsettings, endpoints, and maybe additional |
| 261 | class descriptors. |
| 262 | |
| 263 | *Perform USB operations* using *ioctl()* requests to make endpoint I/O |
| 264 | requests (synchronously or asynchronously) or manage the device. These |
| 265 | requests need the CAP_SYS_RAWIO capability, as well as filesystem |
| 266 | access permissions. Only one ioctl request can be made on one of these |
| 267 | device files at a time. This means that if you are synchronously reading |
| 268 | an endpoint from one thread, you won't be able to write to a different |
| 269 | endpoint from another thread until the read completes. This works for |
| 270 | *half duplex* protocols, but otherwise you'd use asynchronous i/o |
| 271 | requests. |
| 272 | |
| 273 | Life Cycle of User Mode Drivers |
| 274 | ------------------------------- |
| 275 | |
| 276 | Such a driver first needs to find a device file for a device it knows |
| 277 | how to handle. Maybe it was told about it because a ``/sbin/hotplug`` |
| 278 | event handling agent chose that driver to handle the new device. Or |
Mauro Carvalho Chehab | 8a6a285 | 2017-04-16 21:51:06 -0300 | [diff] [blame] | 279 | maybe it's an application that scans all the /dev/bus/usb/ device files, |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 280 | and ignores most devices. In either case, it should :c:func:`read()` |
| 281 | all the descriptors from the device file, and check them against what it |
| 282 | knows how to handle. It might just reject everything except a particular |
| 283 | vendor and product ID, or need a more complex policy. |
| 284 | |
| 285 | Never assume there will only be one such device on the system at a time! |
| 286 | If your code can't handle more than one device at a time, at least |
| 287 | detect when there's more than one, and have your users choose which |
| 288 | device to use. |
| 289 | |
| 290 | Once your user mode driver knows what device to use, it interacts with |
| 291 | it in either of two styles. The simple style is to make only control |
| 292 | requests; some devices don't need more complex interactions than those. |
| 293 | (An example might be software using vendor-specific control requests for |
| 294 | some initialization or configuration tasks, with a kernel driver for the |
| 295 | rest.) |
| 296 | |
| 297 | More likely, you need a more complex style driver: one using non-control |
| 298 | endpoints, reading or writing data and claiming exclusive use of an |
| 299 | interface. *Bulk* transfers are easiest to use, but only their sibling |
| 300 | *interrupt* transfers work with low speed devices. Both interrupt and |
| 301 | *isochronous* transfers offer service guarantees because their bandwidth |
| 302 | is reserved. Such "periodic" transfers are awkward to use through usbfs, |
| 303 | unless you're using the asynchronous calls. However, interrupt transfers |
| 304 | can also be used in a synchronous "one shot" style. |
| 305 | |
| 306 | Your user-mode driver should never need to worry about cleaning up |
| 307 | request state when the device is disconnected, although it should close |
| 308 | its open file descriptors as soon as it starts seeing the ENODEV errors. |
| 309 | |
| 310 | The ioctl() Requests |
| 311 | -------------------- |
| 312 | |
| 313 | To use these ioctls, you need to include the following headers in your |
Mauro Carvalho Chehab | 9a3c8b3 | 2017-04-05 10:22:58 -0300 | [diff] [blame] | 314 | userspace program:: |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 315 | |
| 316 | #include <linux/usb.h> |
| 317 | #include <linux/usbdevice_fs.h> |
| 318 | #include <asm/byteorder.h> |
| 319 | |
| 320 | The standard USB device model requests, from "Chapter 9" of the USB 2.0 |
| 321 | specification, are automatically included from the ``<linux/usb/ch9.h>`` |
| 322 | header. |
| 323 | |
| 324 | Unless noted otherwise, the ioctl requests described here will update |
| 325 | the modification time on the usbfs file to which they are applied |
| 326 | (unless they fail). A return of zero indicates success; otherwise, a |
Mauro Carvalho Chehab | e1c3e6e | 2017-04-05 10:23:15 -0300 | [diff] [blame] | 327 | standard USB error code is returned (These are documented in |
| 328 | :ref:`usb-error-codes`). |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 329 | |
| 330 | Each of these files multiplexes access to several I/O streams, one per |
| 331 | endpoint. Each device has one control endpoint (endpoint zero) which |
| 332 | supports a limited RPC style RPC access. Devices are configured by |
| 333 | hub_wq (in the kernel) setting a device-wide *configuration* that |
| 334 | affects things like power consumption and basic functionality. The |
| 335 | endpoints are part of USB *interfaces*, which may have *altsettings* |
| 336 | affecting things like which endpoints are available. Many devices only |
| 337 | have a single configuration and interface, so drivers for them will |
| 338 | ignore configurations and altsettings. |
| 339 | |
| 340 | Management/Status Requests |
| 341 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 342 | |
| 343 | A number of usbfs requests don't deal very directly with device I/O. |
| 344 | They mostly relate to device management and status. These are all |
| 345 | synchronous requests. |
| 346 | |
| 347 | USBDEVFS_CLAIMINTERFACE |
| 348 | This is used to force usbfs to claim a specific interface, which has |
| 349 | not previously been claimed by usbfs or any other kernel driver. The |
| 350 | ioctl parameter is an integer holding the number of the interface |
| 351 | (bInterfaceNumber from descriptor). |
| 352 | |
| 353 | Note that if your driver doesn't claim an interface before trying to |
| 354 | use one of its endpoints, and no other driver has bound to it, then |
| 355 | the interface is automatically claimed by usbfs. |
| 356 | |
| 357 | This claim will be released by a RELEASEINTERFACE ioctl, or by |
| 358 | closing the file descriptor. File modification time is not updated |
| 359 | by this request. |
| 360 | |
| 361 | USBDEVFS_CONNECTINFO |
| 362 | Says whether the device is lowspeed. The ioctl parameter points to a |
Mauro Carvalho Chehab | 9a3c8b3 | 2017-04-05 10:22:58 -0300 | [diff] [blame] | 363 | structure like this:: |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 364 | |
Mauro Carvalho Chehab | 4ad4b21 | 2017-04-05 10:22:57 -0300 | [diff] [blame] | 365 | struct usbdevfs_connectinfo { |
| 366 | unsigned int devnum; |
| 367 | unsigned char slow; |
| 368 | }; |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 369 | |
| 370 | File modification time is not updated by this request. |
| 371 | |
| 372 | *You can't tell whether a "not slow" device is connected at high |
| 373 | speed (480 MBit/sec) or just full speed (12 MBit/sec).* You should |
| 374 | know the devnum value already, it's the DDD value of the device file |
| 375 | name. |
| 376 | |
| 377 | USBDEVFS_GETDRIVER |
| 378 | Returns the name of the kernel driver bound to a given interface (a |
| 379 | string). Parameter is a pointer to this structure, which is |
Mauro Carvalho Chehab | 9a3c8b3 | 2017-04-05 10:22:58 -0300 | [diff] [blame] | 380 | modified:: |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 381 | |
Mauro Carvalho Chehab | 4ad4b21 | 2017-04-05 10:22:57 -0300 | [diff] [blame] | 382 | struct usbdevfs_getdriver { |
| 383 | unsigned int interface; |
| 384 | char driver[USBDEVFS_MAXDRIVERNAME + 1]; |
| 385 | }; |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 386 | |
| 387 | File modification time is not updated by this request. |
| 388 | |
| 389 | USBDEVFS_IOCTL |
| 390 | Passes a request from userspace through to a kernel driver that has |
Mauro Carvalho Chehab | 9a3c8b3 | 2017-04-05 10:22:58 -0300 | [diff] [blame] | 391 | an ioctl entry in the *struct usb_driver* it registered:: |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 392 | |
Mauro Carvalho Chehab | 4ad4b21 | 2017-04-05 10:22:57 -0300 | [diff] [blame] | 393 | struct usbdevfs_ioctl { |
| 394 | int ifno; |
| 395 | int ioctl_code; |
| 396 | void *data; |
| 397 | }; |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 398 | |
Mauro Carvalho Chehab | 4ad4b21 | 2017-04-05 10:22:57 -0300 | [diff] [blame] | 399 | /* user mode call looks like this. |
| 400 | * 'request' becomes the driver->ioctl() 'code' parameter. |
| 401 | * the size of 'param' is encoded in 'request', and that data |
| 402 | * is copied to or from the driver->ioctl() 'buf' parameter. |
| 403 | */ |
| 404 | static int |
| 405 | usbdev_ioctl (int fd, int ifno, unsigned request, void *param) |
| 406 | { |
| 407 | struct usbdevfs_ioctl wrapper; |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 408 | |
Mauro Carvalho Chehab | 4ad4b21 | 2017-04-05 10:22:57 -0300 | [diff] [blame] | 409 | wrapper.ifno = ifno; |
| 410 | wrapper.ioctl_code = request; |
| 411 | wrapper.data = param; |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 412 | |
Mauro Carvalho Chehab | 4ad4b21 | 2017-04-05 10:22:57 -0300 | [diff] [blame] | 413 | return ioctl (fd, USBDEVFS_IOCTL, &wrapper); |
| 414 | } |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 415 | |
| 416 | File modification time is not updated by this request. |
| 417 | |
| 418 | This request lets kernel drivers talk to user mode code through |
| 419 | filesystem operations even when they don't create a character or |
| 420 | block special device. It's also been used to do things like ask |
| 421 | devices what device special file should be used. Two pre-defined |
| 422 | ioctls are used to disconnect and reconnect kernel drivers, so that |
| 423 | user mode code can completely manage binding and configuration of |
| 424 | devices. |
| 425 | |
| 426 | USBDEVFS_RELEASEINTERFACE |
| 427 | This is used to release the claim usbfs made on interface, either |
| 428 | implicitly or because of a USBDEVFS_CLAIMINTERFACE call, before the |
| 429 | file descriptor is closed. The ioctl parameter is an integer holding |
| 430 | the number of the interface (bInterfaceNumber from descriptor); File |
| 431 | modification time is not updated by this request. |
| 432 | |
Mauro Carvalho Chehab | 9a3c8b3 | 2017-04-05 10:22:58 -0300 | [diff] [blame] | 433 | .. warning:: |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 434 | |
Mauro Carvalho Chehab | 4ad4b21 | 2017-04-05 10:22:57 -0300 | [diff] [blame] | 435 | *No security check is made to ensure that the task which made |
| 436 | the claim is the one which is releasing it. This means that user |
| 437 | mode driver may interfere other ones.* |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 438 | |
| 439 | USBDEVFS_RESETEP |
| 440 | Resets the data toggle value for an endpoint (bulk or interrupt) to |
| 441 | DATA0. The ioctl parameter is an integer endpoint number (1 to 15, |
| 442 | as identified in the endpoint descriptor), with USB_DIR_IN added |
| 443 | if the device's endpoint sends data to the host. |
| 444 | |
Mauro Carvalho Chehab | 4ad4b21 | 2017-04-05 10:22:57 -0300 | [diff] [blame] | 445 | **Warning** |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 446 | |
Mauro Carvalho Chehab | 4ad4b21 | 2017-04-05 10:22:57 -0300 | [diff] [blame] | 447 | *Avoid using this request. It should probably be removed.* Using |
| 448 | it typically means the device and driver will lose toggle |
| 449 | synchronization. If you really lost synchronization, you likely |
| 450 | need to completely handshake with the device, using a request |
| 451 | like CLEAR_HALT or SET_INTERFACE. |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 452 | |
| 453 | USBDEVFS_DROP_PRIVILEGES |
| 454 | This is used to relinquish the ability to do certain operations |
| 455 | which are considered to be privileged on a usbfs file descriptor. |
| 456 | This includes claiming arbitrary interfaces, resetting a device on |
| 457 | which there are currently claimed interfaces from other users, and |
| 458 | issuing USBDEVFS_IOCTL calls. The ioctl parameter is a 32 bit mask |
| 459 | of interfaces the user is allowed to claim on this file descriptor. |
| 460 | You may issue this ioctl more than one time to narrow said mask. |
| 461 | |
| 462 | Synchronous I/O Support |
| 463 | ~~~~~~~~~~~~~~~~~~~~~~~ |
| 464 | |
| 465 | Synchronous requests involve the kernel blocking until the user mode |
| 466 | request completes, either by finishing successfully or by reporting an |
| 467 | error. In most cases this is the simplest way to use usbfs, although as |
| 468 | noted above it does prevent performing I/O to more than one endpoint at |
| 469 | a time. |
| 470 | |
| 471 | USBDEVFS_BULK |
| 472 | Issues a bulk read or write request to the device. The ioctl |
Mauro Carvalho Chehab | 9a3c8b3 | 2017-04-05 10:22:58 -0300 | [diff] [blame] | 473 | parameter is a pointer to this structure:: |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 474 | |
Mauro Carvalho Chehab | 4ad4b21 | 2017-04-05 10:22:57 -0300 | [diff] [blame] | 475 | struct usbdevfs_bulktransfer { |
| 476 | unsigned int ep; |
| 477 | unsigned int len; |
| 478 | unsigned int timeout; /* in milliseconds */ |
| 479 | void *data; |
| 480 | }; |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 481 | |
| 482 | The "ep" value identifies a bulk endpoint number (1 to 15, as |
| 483 | identified in an endpoint descriptor), masked with USB_DIR_IN when |
| 484 | referring to an endpoint which sends data to the host from the |
| 485 | device. The length of the data buffer is identified by "len"; Recent |
| 486 | kernels support requests up to about 128KBytes. *FIXME say how read |
| 487 | length is returned, and how short reads are handled.*. |
| 488 | |
| 489 | USBDEVFS_CLEAR_HALT |
| 490 | Clears endpoint halt (stall) and resets the endpoint toggle. This is |
| 491 | only meaningful for bulk or interrupt endpoints. The ioctl parameter |
| 492 | is an integer endpoint number (1 to 15, as identified in an endpoint |
| 493 | descriptor), masked with USB_DIR_IN when referring to an endpoint |
| 494 | which sends data to the host from the device. |
| 495 | |
| 496 | Use this on bulk or interrupt endpoints which have stalled, |
| 497 | returning *-EPIPE* status to a data transfer request. Do not issue |
| 498 | the control request directly, since that could invalidate the host's |
| 499 | record of the data toggle. |
| 500 | |
| 501 | USBDEVFS_CONTROL |
| 502 | Issues a control request to the device. The ioctl parameter points |
Mauro Carvalho Chehab | 9a3c8b3 | 2017-04-05 10:22:58 -0300 | [diff] [blame] | 503 | to a structure like this:: |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 504 | |
Mauro Carvalho Chehab | 4ad4b21 | 2017-04-05 10:22:57 -0300 | [diff] [blame] | 505 | struct usbdevfs_ctrltransfer { |
| 506 | __u8 bRequestType; |
| 507 | __u8 bRequest; |
| 508 | __u16 wValue; |
| 509 | __u16 wIndex; |
| 510 | __u16 wLength; |
| 511 | __u32 timeout; /* in milliseconds */ |
| 512 | void *data; |
| 513 | }; |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 514 | |
| 515 | The first eight bytes of this structure are the contents of the |
| 516 | SETUP packet to be sent to the device; see the USB 2.0 specification |
| 517 | for details. The bRequestType value is composed by combining a |
Mauro Carvalho Chehab | 69966c9 | 2017-04-05 10:23:11 -0300 | [diff] [blame] | 518 | ``USB_TYPE_*`` value, a ``USB_DIR_*`` value, and a ``USB_RECIP_*`` |
| 519 | value (from ``linux/usb.h``). If wLength is nonzero, it describes |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 520 | the length of the data buffer, which is either written to the device |
| 521 | (USB_DIR_OUT) or read from the device (USB_DIR_IN). |
| 522 | |
| 523 | At this writing, you can't transfer more than 4 KBytes of data to or |
| 524 | from a device; usbfs has a limit, and some host controller drivers |
| 525 | have a limit. (That's not usually a problem.) *Also* there's no way |
| 526 | to say it's not OK to get a short read back from the device. |
| 527 | |
| 528 | USBDEVFS_RESET |
| 529 | Does a USB level device reset. The ioctl parameter is ignored. After |
| 530 | the reset, this rebinds all device interfaces. File modification |
| 531 | time is not updated by this request. |
| 532 | |
Mauro Carvalho Chehab | 9a3c8b3 | 2017-04-05 10:22:58 -0300 | [diff] [blame] | 533 | .. warning:: |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 534 | |
Mauro Carvalho Chehab | 4ad4b21 | 2017-04-05 10:22:57 -0300 | [diff] [blame] | 535 | *Avoid using this call* until some usbcore bugs get fixed, since |
| 536 | it does not fully synchronize device, interface, and driver (not |
| 537 | just usbfs) state. |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 538 | |
| 539 | USBDEVFS_SETINTERFACE |
| 540 | Sets the alternate setting for an interface. The ioctl parameter is |
Mauro Carvalho Chehab | 9a3c8b3 | 2017-04-05 10:22:58 -0300 | [diff] [blame] | 541 | a pointer to a structure like this:: |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 542 | |
Mauro Carvalho Chehab | 4ad4b21 | 2017-04-05 10:22:57 -0300 | [diff] [blame] | 543 | struct usbdevfs_setinterface { |
| 544 | unsigned int interface; |
| 545 | unsigned int altsetting; |
| 546 | }; |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 547 | |
| 548 | File modification time is not updated by this request. |
| 549 | |
| 550 | Those struct members are from some interface descriptor applying to |
| 551 | the current configuration. The interface number is the |
| 552 | bInterfaceNumber value, and the altsetting number is the |
| 553 | bAlternateSetting value. (This resets each endpoint in the |
| 554 | interface.) |
| 555 | |
| 556 | USBDEVFS_SETCONFIGURATION |
| 557 | Issues the :c:func:`usb_set_configuration()` call for the |
| 558 | device. The parameter is an integer holding the number of a |
| 559 | configuration (bConfigurationValue from descriptor). File |
| 560 | modification time is not updated by this request. |
| 561 | |
Mauro Carvalho Chehab | 9a3c8b3 | 2017-04-05 10:22:58 -0300 | [diff] [blame] | 562 | .. warning:: |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 563 | |
Mauro Carvalho Chehab | 4ad4b21 | 2017-04-05 10:22:57 -0300 | [diff] [blame] | 564 | *Avoid using this call* until some usbcore bugs get fixed, since |
| 565 | it does not fully synchronize device, interface, and driver (not |
| 566 | just usbfs) state. |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 567 | |
| 568 | Asynchronous I/O Support |
| 569 | ~~~~~~~~~~~~~~~~~~~~~~~~ |
| 570 | |
| 571 | As mentioned above, there are situations where it may be important to |
| 572 | initiate concurrent operations from user mode code. This is particularly |
| 573 | important for periodic transfers (interrupt and isochronous), but it can |
| 574 | be used for other kinds of USB requests too. In such cases, the |
| 575 | asynchronous requests described here are essential. Rather than |
| 576 | submitting one request and having the kernel block until it completes, |
| 577 | the blocking is separate. |
| 578 | |
| 579 | These requests are packaged into a structure that resembles the URB used |
| 580 | by kernel device drivers. (No POSIX Async I/O support here, sorry.) It |
Mauro Carvalho Chehab | 69966c9 | 2017-04-05 10:23:11 -0300 | [diff] [blame] | 581 | identifies the endpoint type (``USBDEVFS_URB_TYPE_*``), endpoint |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 582 | (number, masked with USB_DIR_IN as appropriate), buffer and length, |
| 583 | and a user "context" value serving to uniquely identify each request. |
| 584 | (It's usually a pointer to per-request data.) Flags can modify requests |
| 585 | (not as many as supported for kernel drivers). |
| 586 | |
| 587 | Each request can specify a realtime signal number (between SIGRTMIN and |
| 588 | SIGRTMAX, inclusive) to request a signal be sent when the request |
| 589 | completes. |
| 590 | |
| 591 | When usbfs returns these urbs, the status value is updated, and the |
| 592 | buffer may have been modified. Except for isochronous transfers, the |
| 593 | actual_length is updated to say how many bytes were transferred; if the |
| 594 | USBDEVFS_URB_DISABLE_SPD flag is set ("short packets are not OK"), if |
Mauro Carvalho Chehab | 9a3c8b3 | 2017-04-05 10:22:58 -0300 | [diff] [blame] | 595 | fewer bytes were read than were requested then you get an error report:: |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 596 | |
| 597 | struct usbdevfs_iso_packet_desc { |
Mauro Carvalho Chehab | 4ad4b21 | 2017-04-05 10:22:57 -0300 | [diff] [blame] | 598 | unsigned int length; |
| 599 | unsigned int actual_length; |
| 600 | unsigned int status; |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 601 | }; |
| 602 | |
| 603 | struct usbdevfs_urb { |
Mauro Carvalho Chehab | 4ad4b21 | 2017-04-05 10:22:57 -0300 | [diff] [blame] | 604 | unsigned char type; |
| 605 | unsigned char endpoint; |
| 606 | int status; |
| 607 | unsigned int flags; |
| 608 | void *buffer; |
| 609 | int buffer_length; |
| 610 | int actual_length; |
| 611 | int start_frame; |
| 612 | int number_of_packets; |
| 613 | int error_count; |
| 614 | unsigned int signr; |
| 615 | void *usercontext; |
| 616 | struct usbdevfs_iso_packet_desc iso_frame_desc[]; |
Oliver Neukum | dd0b38d | 2016-11-14 15:52:43 +0100 | [diff] [blame] | 617 | }; |
| 618 | |
| 619 | For these asynchronous requests, the file modification time reflects |
| 620 | when the request was initiated. This contrasts with their use with the |
| 621 | synchronous requests, where it reflects when requests complete. |
| 622 | |
| 623 | USBDEVFS_DISCARDURB |
| 624 | *TBS* File modification time is not updated by this request. |
| 625 | |
| 626 | USBDEVFS_DISCSIGNAL |
| 627 | *TBS* File modification time is not updated by this request. |
| 628 | |
| 629 | USBDEVFS_REAPURB |
| 630 | *TBS* File modification time is not updated by this request. |
| 631 | |
| 632 | USBDEVFS_REAPURBNDELAY |
| 633 | *TBS* File modification time is not updated by this request. |
| 634 | |
| 635 | USBDEVFS_SUBMITURB |
| 636 | *TBS* |
Mauro Carvalho Chehab | 8a6a285 | 2017-04-16 21:51:06 -0300 | [diff] [blame] | 637 | |
| 638 | The USB devices |
| 639 | =============== |
| 640 | |
| 641 | The USB devices are now exported via debugfs: |
| 642 | |
| 643 | - ``/sys/kernel/debug/usb/devices`` ... a text file showing each of the USB |
| 644 | devices on known to the kernel, and their configuration descriptors. |
| 645 | You can also poll() this to learn about new devices. |
| 646 | |
| 647 | /sys/kernel/debug/usb/devices |
| 648 | ----------------------------- |
| 649 | |
| 650 | This file is handy for status viewing tools in user mode, which can scan |
| 651 | the text format and ignore most of it. More detailed device status |
| 652 | (including class and vendor status) is available from device-specific |
| 653 | files. For information about the current format of this file, see the |
| 654 | ``Documentation/usb/proc_usb_info.txt`` file in your Linux kernel |
| 655 | sources. |
| 656 | |
| 657 | This file, in combination with the poll() system call, can also be used |
| 658 | to detect when devices are added or removed:: |
| 659 | |
| 660 | int fd; |
| 661 | struct pollfd pfd; |
| 662 | |
| 663 | fd = open("/sys/kernel/debug/usb/devices", O_RDONLY); |
| 664 | pfd = { fd, POLLIN, 0 }; |
| 665 | for (;;) { |
| 666 | /* The first time through, this call will return immediately. */ |
| 667 | poll(&pfd, 1, -1); |
| 668 | |
| 669 | /* To see what's changed, compare the file's previous and current |
| 670 | contents or scan the filesystem. (Scanning is more precise.) */ |
| 671 | } |
| 672 | |
| 673 | Note that this behavior is intended to be used for informational and |
| 674 | debug purposes. It would be more appropriate to use programs such as |
| 675 | udev or HAL to initialize a device or start a user-mode helper program, |
| 676 | for instance. |