usb.rst 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. .. _usb-hostside-api:
  2. ===========================
  3. The Linux-USB Host Side API
  4. ===========================
  5. Introduction to USB on Linux
  6. ============================
  7. A Universal Serial Bus (USB) is used to connect a host, such as a PC or
  8. workstation, to a number of peripheral devices. USB uses a tree
  9. structure, with the host as the root (the system's master), hubs as
  10. interior nodes, and peripherals as leaves (and slaves). Modern PCs
  11. support several such trees of USB devices, usually
  12. a few USB 3.0 (5 GBit/s) or USB 3.1 (10 GBit/s) and some legacy
  13. USB 2.0 (480 MBit/s) busses just in case.
  14. That master/slave asymmetry was designed-in for a number of reasons, one
  15. being ease of use. It is not physically possible to mistake upstream and
  16. downstream or it does not matter with a type C plug (or they are built into the
  17. peripheral). Also, the host software doesn't need to deal with
  18. distributed auto-configuration since the pre-designated master node
  19. manages all that.
  20. Kernel developers added USB support to Linux early in the 2.2 kernel
  21. series and have been developing it further since then. Besides support
  22. for each new generation of USB, various host controllers gained support,
  23. new drivers for peripherals have been added and advanced features for latency
  24. measurement and improved power management introduced.
  25. Linux can run inside USB devices as well as on the hosts that control
  26. the devices. But USB device drivers running inside those peripherals
  27. don't do the same things as the ones running inside hosts, so they've
  28. been given a different name: *gadget drivers*. This document does not
  29. cover gadget drivers.
  30. USB Host-Side API Model
  31. =======================
  32. Host-side drivers for USB devices talk to the "usbcore" APIs. There are
  33. two. One is intended for *general-purpose* drivers (exposed through
  34. driver frameworks), and the other is for drivers that are *part of the
  35. core*. Such core drivers include the *hub* driver (which manages trees
  36. of USB devices) and several different kinds of *host controller
  37. drivers*, which control individual busses.
  38. The device model seen by USB drivers is relatively complex.
  39. - USB supports four kinds of data transfers (control, bulk, interrupt,
  40. and isochronous). Two of them (control and bulk) use bandwidth as
  41. it's available, while the other two (interrupt and isochronous) are
  42. scheduled to provide guaranteed bandwidth.
  43. - The device description model includes one or more "configurations"
  44. per device, only one of which is active at a time. Devices are supposed
  45. to be capable of operating at lower than their top
  46. speeds and may provide a BOS descriptor showing the lowest speed they
  47. remain fully operational at.
  48. - From USB 3.0 on configurations have one or more "functions", which
  49. provide a common functionality and are grouped together for purposes
  50. of power management.
  51. - Configurations or functions have one or more "interfaces", each of which may have
  52. "alternate settings". Interfaces may be standardized by USB "Class"
  53. specifications, or may be specific to a vendor or device.
  54. USB device drivers actually bind to interfaces, not devices. Think of
  55. them as "interface drivers", though you may not see many devices
  56. where the distinction is important. *Most USB devices are simple,
  57. with only one function, one configuration, one interface, and one alternate
  58. setting.*
  59. - Interfaces have one or more "endpoints", each of which supports one
  60. type and direction of data transfer such as "bulk out" or "interrupt
  61. in". The entire configuration may have up to sixteen endpoints in
  62. each direction, allocated as needed among all the interfaces.
  63. - Data transfer on USB is packetized; each endpoint has a maximum
  64. packet size. Drivers must often be aware of conventions such as
  65. flagging the end of bulk transfers using "short" (including zero
  66. length) packets.
  67. - The Linux USB API supports synchronous calls for control and bulk
  68. messages. It also supports asynchronous calls for all kinds of data
  69. transfer, using request structures called "URBs" (USB Request
  70. Blocks).
  71. Accordingly, the USB Core API exposed to device drivers covers quite a
  72. lot of territory. You'll probably need to consult the USB 3.0
  73. specification, available online from www.usb.org at no cost, as well as
  74. class or device specifications.
  75. The only host-side drivers that actually touch hardware (reading/writing
  76. registers, handling IRQs, and so on) are the HCDs. In theory, all HCDs
  77. provide the same functionality through the same API. In practice, that's
  78. becoming more true, but there are still differences
  79. that crop up especially with fault handling on the less common controllers.
  80. Different controllers don't
  81. necessarily report the same aspects of failures, and recovery from
  82. faults (including software-induced ones like unlinking an URB) isn't yet
  83. fully consistent. Device driver authors should make a point of doing
  84. disconnect testing (while the device is active) with each different host
  85. controller driver, to make sure drivers don't have bugs of their own as
  86. well as to make sure they aren't relying on some HCD-specific behavior.
  87. .. _usb_chapter9:
  88. USB-Standard Types
  89. ==================
  90. In ``<linux/usb/ch9.h>`` you will find the USB data types defined in
  91. chapter 9 of the USB specification. These data types are used throughout
  92. USB, and in APIs including this host side API, gadget APIs, usb character
  93. devices and debugfs interfaces.
  94. .. kernel-doc:: include/linux/usb/ch9.h
  95. :internal:
  96. .. _usb_header:
  97. Host-Side Data Types and Macros
  98. ===============================
  99. The host side API exposes several layers to drivers, some of which are
  100. more necessary than others. These support lifecycle models for host side
  101. drivers and devices, and support passing buffers through usbcore to some
  102. HCD that performs the I/O for the device driver.
  103. .. kernel-doc:: include/linux/usb.h
  104. :internal:
  105. USB Core APIs
  106. =============
  107. There are two basic I/O models in the USB API. The most elemental one is
  108. asynchronous: drivers submit requests in the form of an URB, and the
  109. URB's completion callback handles the next step. All USB transfer types
  110. support that model, although there are special cases for control URBs
  111. (which always have setup and status stages, but may not have a data
  112. stage) and isochronous URBs (which allow large packets and include
  113. per-packet fault reports). Built on top of that is synchronous API
  114. support, where a driver calls a routine that allocates one or more URBs,
  115. submits them, and waits until they complete. There are synchronous
  116. wrappers for single-buffer control and bulk transfers (which are awkward
  117. to use in some driver disconnect scenarios), and for scatterlist based
  118. streaming i/o (bulk or interrupt).
  119. USB drivers need to provide buffers that can be used for DMA, although
  120. they don't necessarily need to provide the DMA mapping themselves. There
  121. are APIs to use used when allocating DMA buffers, which can prevent use
  122. of bounce buffers on some systems. In some cases, drivers may be able to
  123. rely on 64bit DMA to eliminate another kind of bounce buffer.
  124. .. kernel-doc:: drivers/usb/core/urb.c
  125. :export:
  126. .. kernel-doc:: drivers/usb/core/message.c
  127. :export:
  128. .. kernel-doc:: drivers/usb/core/file.c
  129. :export:
  130. .. kernel-doc:: drivers/usb/core/driver.c
  131. :export:
  132. .. kernel-doc:: drivers/usb/core/usb.c
  133. :export:
  134. .. kernel-doc:: drivers/usb/core/hub.c
  135. :export:
  136. Host Controller APIs
  137. ====================
  138. These APIs are only for use by host controller drivers, most of which
  139. implement standard register interfaces such as XHCI, EHCI, OHCI, or UHCI. UHCI
  140. was one of the first interfaces, designed by Intel and also used by VIA;
  141. it doesn't do much in hardware. OHCI was designed later, to have the
  142. hardware do more work (bigger transfers, tracking protocol state, and so
  143. on). EHCI was designed with USB 2.0; its design has features that
  144. resemble OHCI (hardware does much more work) as well as UHCI (some parts
  145. of ISO support, TD list processing). XHCI was designed with USB 3.0. It
  146. continues to shift support for functionality into hardware.
  147. There are host controllers other than the "big three", although most PCI
  148. based controllers (and a few non-PCI based ones) use one of those
  149. interfaces. Not all host controllers use DMA; some use PIO, and there is
  150. also a simulator and a virtual host controller to pipe USB over the network.
  151. The same basic APIs are available to drivers for all those controllers.
  152. For historical reasons they are in two layers: :c:type:`struct
  153. usb_bus <usb_bus>` is a rather thin layer that became available
  154. in the 2.2 kernels, while :c:type:`struct usb_hcd <usb_hcd>`
  155. is a more featureful layer
  156. that lets HCDs share common code, to shrink driver size and
  157. significantly reduce hcd-specific behaviors.
  158. .. kernel-doc:: drivers/usb/core/hcd.c
  159. :export:
  160. .. kernel-doc:: drivers/usb/core/hcd-pci.c
  161. :export:
  162. .. kernel-doc:: drivers/usb/core/buffer.c
  163. :internal:
  164. The USB character device nodes
  165. ==============================
  166. This chapter presents the Linux character device nodes. You may prefer
  167. to avoid writing new kernel code for your USB driver. User mode device
  168. drivers are usually packaged as applications or libraries, and may use
  169. character devices through some programming library that wraps it.
  170. Such libraries include:
  171. - `libusb <http://libusb.sourceforge.net>`__ for C/C++, and
  172. - `jUSB <http://jUSB.sourceforge.net>`__ for Java.
  173. Some old information about it can be seen at the "USB Device Filesystem"
  174. section of the USB Guide. The latest copy of the USB Guide can be found
  175. at http://www.linux-usb.org/
  176. .. note::
  177. - They were used to be implemented via *usbfs*, but this is not part of
  178. the sysfs debug interface.
  179. - This particular documentation is incomplete, especially with respect
  180. to the asynchronous mode. As of kernel 2.5.66 the code and this
  181. (new) documentation need to be cross-reviewed.
  182. What files are in "devtmpfs"?
  183. -----------------------------
  184. Conventionally mounted at ``/dev/bus/usb/``, usbfs features include:
  185. - ``/dev/bus/usb/BBB/DDD`` ... magic files exposing the each device's
  186. configuration descriptors, and supporting a series of ioctls for
  187. making device requests, including I/O to devices. (Purely for access
  188. by programs.)
  189. Each bus is given a number (``BBB``) based on when it was enumerated; within
  190. each bus, each device is given a similar number (``DDD``). Those ``BBB/DDD``
  191. paths are not "stable" identifiers; expect them to change even if you
  192. always leave the devices plugged in to the same hub port. *Don't even
  193. think of saving these in application configuration files.* Stable
  194. identifiers are available, for user mode applications that want to use
  195. them. HID and networking devices expose these stable IDs, so that for
  196. example you can be sure that you told the right UPS to power down its
  197. second server. Pleast note that it doesn't (yet) expose those IDs.
  198. /dev/bus/usb/BBB/DDD
  199. --------------------
  200. Use these files in one of these basic ways:
  201. - *They can be read,* producing first the device descriptor (18 bytes) and
  202. then the descriptors for the current configuration. See the USB 2.0 spec
  203. for details about those binary data formats. You'll need to convert most
  204. multibyte values from little endian format to your native host byte
  205. order, although a few of the fields in the device descriptor (both of
  206. the BCD-encoded fields, and the vendor and product IDs) will be
  207. byteswapped for you. Note that configuration descriptors include
  208. descriptors for interfaces, altsettings, endpoints, and maybe additional
  209. class descriptors.
  210. - *Perform USB operations* using *ioctl()* requests to make endpoint I/O
  211. requests (synchronously or asynchronously) or manage the device. These
  212. requests need the ``CAP_SYS_RAWIO`` capability, as well as filesystem
  213. access permissions. Only one ioctl request can be made on one of these
  214. device files at a time. This means that if you are synchronously reading
  215. an endpoint from one thread, you won't be able to write to a different
  216. endpoint from another thread until the read completes. This works for
  217. *half duplex* protocols, but otherwise you'd use asynchronous i/o
  218. requests.
  219. Each connected USB device has one file. The ``BBB`` indicates the bus
  220. number. The ``DDD`` indicates the device address on that bus. Both
  221. of these numbers are assigned sequentially, and can be reused, so
  222. you can't rely on them for stable access to devices. For example,
  223. it's relatively common for devices to re-enumerate while they are
  224. still connected (perhaps someone jostled their power supply, hub,
  225. or USB cable), so a device might be ``002/027`` when you first connect
  226. it and ``002/048`` sometime later.
  227. These files can be read as binary data. The binary data consists
  228. of first the device descriptor, then the descriptors for each
  229. configuration of the device. Multi-byte fields in the device descriptor
  230. are converted to host endianness by the kernel. The configuration
  231. descriptors are in bus endian format! The configuration descriptor
  232. are wTotalLength bytes apart. If a device returns less configuration
  233. descriptor data than indicated by wTotalLength there will be a hole in
  234. the file for the missing bytes. This information is also shown
  235. in text form by the ``/sys/kernel/debug/usb/devices`` file, described later.
  236. These files may also be used to write user-level drivers for the USB
  237. devices. You would open the ``/dev/bus/usb/BBB/DDD`` file read/write,
  238. read its descriptors to make sure it's the device you expect, and then
  239. bind to an interface (or perhaps several) using an ioctl call. You
  240. would issue more ioctls to the device to communicate to it using
  241. control, bulk, or other kinds of USB transfers. The IOCTLs are
  242. listed in the ``<linux/usbdevice_fs.h>`` file, and at this writing the
  243. source code (``linux/drivers/usb/core/devio.c``) is the primary reference
  244. for how to access devices through those files.
  245. Note that since by default these ``BBB/DDD`` files are writable only by
  246. root, only root can write such user mode drivers. You can selectively
  247. grant read/write permissions to other users by using ``chmod``. Also,
  248. usbfs mount options such as ``devmode=0666`` may be helpful.
  249. Life Cycle of User Mode Drivers
  250. -------------------------------
  251. Such a driver first needs to find a device file for a device it knows
  252. how to handle. Maybe it was told about it because a ``/sbin/hotplug``
  253. event handling agent chose that driver to handle the new device. Or
  254. maybe it's an application that scans all the ``/dev/bus/usb`` device files,
  255. and ignores most devices. In either case, it should :c:func:`read()`
  256. all the descriptors from the device file, and check them against what it
  257. knows how to handle. It might just reject everything except a particular
  258. vendor and product ID, or need a more complex policy.
  259. Never assume there will only be one such device on the system at a time!
  260. If your code can't handle more than one device at a time, at least
  261. detect when there's more than one, and have your users choose which
  262. device to use.
  263. Once your user mode driver knows what device to use, it interacts with
  264. it in either of two styles. The simple style is to make only control
  265. requests; some devices don't need more complex interactions than those.
  266. (An example might be software using vendor-specific control requests for
  267. some initialization or configuration tasks, with a kernel driver for the
  268. rest.)
  269. More likely, you need a more complex style driver: one using non-control
  270. endpoints, reading or writing data and claiming exclusive use of an
  271. interface. *Bulk* transfers are easiest to use, but only their sibling
  272. *interrupt* transfers work with low speed devices. Both interrupt and
  273. *isochronous* transfers offer service guarantees because their bandwidth
  274. is reserved. Such "periodic" transfers are awkward to use through usbfs,
  275. unless you're using the asynchronous calls. However, interrupt transfers
  276. can also be used in a synchronous "one shot" style.
  277. Your user-mode driver should never need to worry about cleaning up
  278. request state when the device is disconnected, although it should close
  279. its open file descriptors as soon as it starts seeing the ENODEV errors.
  280. The ioctl() Requests
  281. --------------------
  282. To use these ioctls, you need to include the following headers in your
  283. userspace program::
  284. #include <linux/usb.h>
  285. #include <linux/usbdevice_fs.h>
  286. #include <asm/byteorder.h>
  287. The standard USB device model requests, from "Chapter 9" of the USB 2.0
  288. specification, are automatically included from the ``<linux/usb/ch9.h>``
  289. header.
  290. Unless noted otherwise, the ioctl requests described here will update
  291. the modification time on the usbfs file to which they are applied
  292. (unless they fail). A return of zero indicates success; otherwise, a
  293. standard USB error code is returned (These are documented in
  294. :ref:`usb-error-codes`).
  295. Each of these files multiplexes access to several I/O streams, one per
  296. endpoint. Each device has one control endpoint (endpoint zero) which
  297. supports a limited RPC style RPC access. Devices are configured by
  298. hub_wq (in the kernel) setting a device-wide *configuration* that
  299. affects things like power consumption and basic functionality. The
  300. endpoints are part of USB *interfaces*, which may have *altsettings*
  301. affecting things like which endpoints are available. Many devices only
  302. have a single configuration and interface, so drivers for them will
  303. ignore configurations and altsettings.
  304. Management/Status Requests
  305. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  306. A number of usbfs requests don't deal very directly with device I/O.
  307. They mostly relate to device management and status. These are all
  308. synchronous requests.
  309. USBDEVFS_CLAIMINTERFACE
  310. This is used to force usbfs to claim a specific interface, which has
  311. not previously been claimed by usbfs or any other kernel driver. The
  312. ioctl parameter is an integer holding the number of the interface
  313. (bInterfaceNumber from descriptor).
  314. Note that if your driver doesn't claim an interface before trying to
  315. use one of its endpoints, and no other driver has bound to it, then
  316. the interface is automatically claimed by usbfs.
  317. This claim will be released by a RELEASEINTERFACE ioctl, or by
  318. closing the file descriptor. File modification time is not updated
  319. by this request.
  320. USBDEVFS_CONNECTINFO
  321. Says whether the device is lowspeed. The ioctl parameter points to a
  322. structure like this::
  323. struct usbdevfs_connectinfo {
  324. unsigned int devnum;
  325. unsigned char slow;
  326. };
  327. File modification time is not updated by this request.
  328. *You can't tell whether a "not slow" device is connected at high
  329. speed (480 MBit/sec) or just full speed (12 MBit/sec).* You should
  330. know the devnum value already, it's the DDD value of the device file
  331. name.
  332. USBDEVFS_GETDRIVER
  333. Returns the name of the kernel driver bound to a given interface (a
  334. string). Parameter is a pointer to this structure, which is
  335. modified::
  336. struct usbdevfs_getdriver {
  337. unsigned int interface;
  338. char driver[USBDEVFS_MAXDRIVERNAME + 1];
  339. };
  340. File modification time is not updated by this request.
  341. USBDEVFS_IOCTL
  342. Passes a request from userspace through to a kernel driver that has
  343. an ioctl entry in the *struct usb_driver* it registered::
  344. struct usbdevfs_ioctl {
  345. int ifno;
  346. int ioctl_code;
  347. void *data;
  348. };
  349. /* user mode call looks like this.
  350. * 'request' becomes the driver->ioctl() 'code' parameter.
  351. * the size of 'param' is encoded in 'request', and that data
  352. * is copied to or from the driver->ioctl() 'buf' parameter.
  353. */
  354. static int
  355. usbdev_ioctl (int fd, int ifno, unsigned request, void *param)
  356. {
  357. struct usbdevfs_ioctl wrapper;
  358. wrapper.ifno = ifno;
  359. wrapper.ioctl_code = request;
  360. wrapper.data = param;
  361. return ioctl (fd, USBDEVFS_IOCTL, &wrapper);
  362. }
  363. File modification time is not updated by this request.
  364. This request lets kernel drivers talk to user mode code through
  365. filesystem operations even when they don't create a character or
  366. block special device. It's also been used to do things like ask
  367. devices what device special file should be used. Two pre-defined
  368. ioctls are used to disconnect and reconnect kernel drivers, so that
  369. user mode code can completely manage binding and configuration of
  370. devices.
  371. USBDEVFS_RELEASEINTERFACE
  372. This is used to release the claim usbfs made on interface, either
  373. implicitly or because of a USBDEVFS_CLAIMINTERFACE call, before the
  374. file descriptor is closed. The ioctl parameter is an integer holding
  375. the number of the interface (bInterfaceNumber from descriptor); File
  376. modification time is not updated by this request.
  377. .. warning::
  378. *No security check is made to ensure that the task which made
  379. the claim is the one which is releasing it. This means that user
  380. mode driver may interfere other ones.*
  381. USBDEVFS_RESETEP
  382. Resets the data toggle value for an endpoint (bulk or interrupt) to
  383. DATA0. The ioctl parameter is an integer endpoint number (1 to 15,
  384. as identified in the endpoint descriptor), with USB_DIR_IN added
  385. if the device's endpoint sends data to the host.
  386. .. Warning::
  387. *Avoid using this request. It should probably be removed.* Using
  388. it typically means the device and driver will lose toggle
  389. synchronization. If you really lost synchronization, you likely
  390. need to completely handshake with the device, using a request
  391. like CLEAR_HALT or SET_INTERFACE.
  392. USBDEVFS_DROP_PRIVILEGES
  393. This is used to relinquish the ability to do certain operations
  394. which are considered to be privileged on a usbfs file descriptor.
  395. This includes claiming arbitrary interfaces, resetting a device on
  396. which there are currently claimed interfaces from other users, and
  397. issuing USBDEVFS_IOCTL calls. The ioctl parameter is a 32 bit mask
  398. of interfaces the user is allowed to claim on this file descriptor.
  399. You may issue this ioctl more than one time to narrow said mask.
  400. Synchronous I/O Support
  401. ~~~~~~~~~~~~~~~~~~~~~~~
  402. Synchronous requests involve the kernel blocking until the user mode
  403. request completes, either by finishing successfully or by reporting an
  404. error. In most cases this is the simplest way to use usbfs, although as
  405. noted above it does prevent performing I/O to more than one endpoint at
  406. a time.
  407. USBDEVFS_BULK
  408. Issues a bulk read or write request to the device. The ioctl
  409. parameter is a pointer to this structure::
  410. struct usbdevfs_bulktransfer {
  411. unsigned int ep;
  412. unsigned int len;
  413. unsigned int timeout; /* in milliseconds */
  414. void *data;
  415. };
  416. The ``ep`` value identifies a bulk endpoint number (1 to 15, as
  417. identified in an endpoint descriptor), masked with USB_DIR_IN when
  418. referring to an endpoint which sends data to the host from the
  419. device. The length of the data buffer is identified by ``len``; Recent
  420. kernels support requests up to about 128KBytes. *FIXME say how read
  421. length is returned, and how short reads are handled.*.
  422. USBDEVFS_CLEAR_HALT
  423. Clears endpoint halt (stall) and resets the endpoint toggle. This is
  424. only meaningful for bulk or interrupt endpoints. The ioctl parameter
  425. is an integer endpoint number (1 to 15, as identified in an endpoint
  426. descriptor), masked with USB_DIR_IN when referring to an endpoint
  427. which sends data to the host from the device.
  428. Use this on bulk or interrupt endpoints which have stalled,
  429. returning ``-EPIPE`` status to a data transfer request. Do not issue
  430. the control request directly, since that could invalidate the host's
  431. record of the data toggle.
  432. USBDEVFS_CONTROL
  433. Issues a control request to the device. The ioctl parameter points
  434. to a structure like this::
  435. struct usbdevfs_ctrltransfer {
  436. __u8 bRequestType;
  437. __u8 bRequest;
  438. __u16 wValue;
  439. __u16 wIndex;
  440. __u16 wLength;
  441. __u32 timeout; /* in milliseconds */
  442. void *data;
  443. };
  444. The first eight bytes of this structure are the contents of the
  445. SETUP packet to be sent to the device; see the USB 2.0 specification
  446. for details. The bRequestType value is composed by combining a
  447. ``USB_TYPE_*`` value, a ``USB_DIR_*`` value, and a ``USB_RECIP_*``
  448. value (from ``linux/usb.h``). If wLength is nonzero, it describes
  449. the length of the data buffer, which is either written to the device
  450. (USB_DIR_OUT) or read from the device (USB_DIR_IN).
  451. At this writing, you can't transfer more than 4 KBytes of data to or
  452. from a device; usbfs has a limit, and some host controller drivers
  453. have a limit. (That's not usually a problem.) *Also* there's no way
  454. to say it's not OK to get a short read back from the device.
  455. USBDEVFS_RESET
  456. Does a USB level device reset. The ioctl parameter is ignored. After
  457. the reset, this rebinds all device interfaces. File modification
  458. time is not updated by this request.
  459. .. warning::
  460. *Avoid using this call* until some usbcore bugs get fixed, since
  461. it does not fully synchronize device, interface, and driver (not
  462. just usbfs) state.
  463. USBDEVFS_SETINTERFACE
  464. Sets the alternate setting for an interface. The ioctl parameter is
  465. a pointer to a structure like this::
  466. struct usbdevfs_setinterface {
  467. unsigned int interface;
  468. unsigned int altsetting;
  469. };
  470. File modification time is not updated by this request.
  471. Those struct members are from some interface descriptor applying to
  472. the current configuration. The interface number is the
  473. bInterfaceNumber value, and the altsetting number is the
  474. bAlternateSetting value. (This resets each endpoint in the
  475. interface.)
  476. USBDEVFS_SETCONFIGURATION
  477. Issues the :c:func:`usb_set_configuration()` call for the
  478. device. The parameter is an integer holding the number of a
  479. configuration (bConfigurationValue from descriptor). File
  480. modification time is not updated by this request.
  481. .. warning::
  482. *Avoid using this call* until some usbcore bugs get fixed, since
  483. it does not fully synchronize device, interface, and driver (not
  484. just usbfs) state.
  485. Asynchronous I/O Support
  486. ~~~~~~~~~~~~~~~~~~~~~~~~
  487. As mentioned above, there are situations where it may be important to
  488. initiate concurrent operations from user mode code. This is particularly
  489. important for periodic transfers (interrupt and isochronous), but it can
  490. be used for other kinds of USB requests too. In such cases, the
  491. asynchronous requests described here are essential. Rather than
  492. submitting one request and having the kernel block until it completes,
  493. the blocking is separate.
  494. These requests are packaged into a structure that resembles the URB used
  495. by kernel device drivers. (No POSIX Async I/O support here, sorry.) It
  496. identifies the endpoint type (``USBDEVFS_URB_TYPE_*``), endpoint
  497. (number, masked with USB_DIR_IN as appropriate), buffer and length,
  498. and a user "context" value serving to uniquely identify each request.
  499. (It's usually a pointer to per-request data.) Flags can modify requests
  500. (not as many as supported for kernel drivers).
  501. Each request can specify a realtime signal number (between SIGRTMIN and
  502. SIGRTMAX, inclusive) to request a signal be sent when the request
  503. completes.
  504. When usbfs returns these urbs, the status value is updated, and the
  505. buffer may have been modified. Except for isochronous transfers, the
  506. actual_length is updated to say how many bytes were transferred; if the
  507. USBDEVFS_URB_DISABLE_SPD flag is set ("short packets are not OK"), if
  508. fewer bytes were read than were requested then you get an error report::
  509. struct usbdevfs_iso_packet_desc {
  510. unsigned int length;
  511. unsigned int actual_length;
  512. unsigned int status;
  513. };
  514. struct usbdevfs_urb {
  515. unsigned char type;
  516. unsigned char endpoint;
  517. int status;
  518. unsigned int flags;
  519. void *buffer;
  520. int buffer_length;
  521. int actual_length;
  522. int start_frame;
  523. int number_of_packets;
  524. int error_count;
  525. unsigned int signr;
  526. void *usercontext;
  527. struct usbdevfs_iso_packet_desc iso_frame_desc[];
  528. };
  529. For these asynchronous requests, the file modification time reflects
  530. when the request was initiated. This contrasts with their use with the
  531. synchronous requests, where it reflects when requests complete.
  532. USBDEVFS_DISCARDURB
  533. *TBS* File modification time is not updated by this request.
  534. USBDEVFS_DISCSIGNAL
  535. *TBS* File modification time is not updated by this request.
  536. USBDEVFS_REAPURB
  537. *TBS* File modification time is not updated by this request.
  538. USBDEVFS_REAPURBNDELAY
  539. *TBS* File modification time is not updated by this request.
  540. USBDEVFS_SUBMITURB
  541. *TBS*
  542. The USB devices
  543. ===============
  544. The USB devices are now exported via debugfs:
  545. - ``/sys/kernel/debug/usb/devices`` ... a text file showing each of the USB
  546. devices on known to the kernel, and their configuration descriptors.
  547. You can also poll() this to learn about new devices.
  548. /sys/kernel/debug/usb/devices
  549. -----------------------------
  550. This file is handy for status viewing tools in user mode, which can scan
  551. the text format and ignore most of it. More detailed device status
  552. (including class and vendor status) is available from device-specific
  553. files. For information about the current format of this file, see below.
  554. This file, in combination with the poll() system call, can also be used
  555. to detect when devices are added or removed::
  556. int fd;
  557. struct pollfd pfd;
  558. fd = open("/sys/kernel/debug/usb/devices", O_RDONLY);
  559. pfd = { fd, POLLIN, 0 };
  560. for (;;) {
  561. /* The first time through, this call will return immediately. */
  562. poll(&pfd, 1, -1);
  563. /* To see what's changed, compare the file's previous and current
  564. contents or scan the filesystem. (Scanning is more precise.) */
  565. }
  566. Note that this behavior is intended to be used for informational and
  567. debug purposes. It would be more appropriate to use programs such as
  568. udev or HAL to initialize a device or start a user-mode helper program,
  569. for instance.
  570. In this file, each device's output has multiple lines of ASCII output.
  571. I made it ASCII instead of binary on purpose, so that someone
  572. can obtain some useful data from it without the use of an
  573. auxiliary program. However, with an auxiliary program, the numbers
  574. in the first 4 columns of each ``T:`` line (topology info:
  575. Lev, Prnt, Port, Cnt) can be used to build a USB topology diagram.
  576. Each line is tagged with a one-character ID for that line::
  577. T = Topology (etc.)
  578. B = Bandwidth (applies only to USB host controllers, which are
  579. virtualized as root hubs)
  580. D = Device descriptor info.
  581. P = Product ID info. (from Device descriptor, but they won't fit
  582. together on one line)
  583. S = String descriptors.
  584. C = Configuration descriptor info. (* = active configuration)
  585. I = Interface descriptor info.
  586. E = Endpoint descriptor info.
  587. /sys/kernel/debug/usb/devices output format
  588. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  589. Legend::
  590. d = decimal number (may have leading spaces or 0's)
  591. x = hexadecimal number (may have leading spaces or 0's)
  592. s = string
  593. Topology info
  594. ^^^^^^^^^^^^^
  595. ::
  596. T: Bus=dd Lev=dd Prnt=dd Port=dd Cnt=dd Dev#=ddd Spd=dddd MxCh=dd
  597. | | | | | | | | |__MaxChildren
  598. | | | | | | | |__Device Speed in Mbps
  599. | | | | | | |__DeviceNumber
  600. | | | | | |__Count of devices at this level
  601. | | | | |__Connector/Port on Parent for this device
  602. | | | |__Parent DeviceNumber
  603. | | |__Level in topology for this bus
  604. | |__Bus number
  605. |__Topology info tag
  606. Speed may be:
  607. ======= ======================================================
  608. 1.5 Mbit/s for low speed USB
  609. 12 Mbit/s for full speed USB
  610. 480 Mbit/s for high speed USB (added for USB 2.0);
  611. also used for Wireless USB, which has no fixed speed
  612. 5000 Mbit/s for SuperSpeed USB (added for USB 3.0)
  613. ======= ======================================================
  614. For reasons lost in the mists of time, the Port number is always
  615. too low by 1. For example, a device plugged into port 4 will
  616. show up with ``Port=03``.
  617. Bandwidth info
  618. ^^^^^^^^^^^^^^
  619. ::
  620. B: Alloc=ddd/ddd us (xx%), #Int=ddd, #Iso=ddd
  621. | | | |__Number of isochronous requests
  622. | | |__Number of interrupt requests
  623. | |__Total Bandwidth allocated to this bus
  624. |__Bandwidth info tag
  625. Bandwidth allocation is an approximation of how much of one frame
  626. (millisecond) is in use. It reflects only periodic transfers, which
  627. are the only transfers that reserve bandwidth. Control and bulk
  628. transfers use all other bandwidth, including reserved bandwidth that
  629. is not used for transfers (such as for short packets).
  630. The percentage is how much of the "reserved" bandwidth is scheduled by
  631. those transfers. For a low or full speed bus (loosely, "USB 1.1"),
  632. 90% of the bus bandwidth is reserved. For a high speed bus (loosely,
  633. "USB 2.0") 80% is reserved.
  634. Device descriptor info & Product ID info
  635. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  636. ::
  637. D: Ver=x.xx Cls=xx(s) Sub=xx Prot=xx MxPS=dd #Cfgs=dd
  638. P: Vendor=xxxx ProdID=xxxx Rev=xx.xx
  639. where::
  640. D: Ver=x.xx Cls=xx(sssss) Sub=xx Prot=xx MxPS=dd #Cfgs=dd
  641. | | | | | | |__NumberConfigurations
  642. | | | | | |__MaxPacketSize of Default Endpoint
  643. | | | | |__DeviceProtocol
  644. | | | |__DeviceSubClass
  645. | | |__DeviceClass
  646. | |__Device USB version
  647. |__Device info tag #1
  648. where::
  649. P: Vendor=xxxx ProdID=xxxx Rev=xx.xx
  650. | | | |__Product revision number
  651. | | |__Product ID code
  652. | |__Vendor ID code
  653. |__Device info tag #2
  654. String descriptor info
  655. ^^^^^^^^^^^^^^^^^^^^^^
  656. ::
  657. S: Manufacturer=ssss
  658. | |__Manufacturer of this device as read from the device.
  659. | For USB host controller drivers (virtual root hubs) this may
  660. | be omitted, or (for newer drivers) will identify the kernel
  661. | version and the driver which provides this hub emulation.
  662. |__String info tag
  663. S: Product=ssss
  664. | |__Product description of this device as read from the device.
  665. | For older USB host controller drivers (virtual root hubs) this
  666. | indicates the driver; for newer ones, it's a product (and vendor)
  667. | description that often comes from the kernel's PCI ID database.
  668. |__String info tag
  669. S: SerialNumber=ssss
  670. | |__Serial Number of this device as read from the device.
  671. | For USB host controller drivers (virtual root hubs) this is
  672. | some unique ID, normally a bus ID (address or slot name) that
  673. | can't be shared with any other device.
  674. |__String info tag
  675. Configuration descriptor info
  676. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  677. ::
  678. C:* #Ifs=dd Cfg#=dd Atr=xx MPwr=dddmA
  679. | | | | | |__MaxPower in mA
  680. | | | | |__Attributes
  681. | | | |__ConfiguratioNumber
  682. | | |__NumberOfInterfaces
  683. | |__ "*" indicates the active configuration (others are " ")
  684. |__Config info tag
  685. USB devices may have multiple configurations, each of which act
  686. rather differently. For example, a bus-powered configuration
  687. might be much less capable than one that is self-powered. Only
  688. one device configuration can be active at a time; most devices
  689. have only one configuration.
  690. Each configuration consists of one or more interfaces. Each
  691. interface serves a distinct "function", which is typically bound
  692. to a different USB device driver. One common example is a USB
  693. speaker with an audio interface for playback, and a HID interface
  694. for use with software volume control.
  695. Interface descriptor info (can be multiple per Config)
  696. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  697. ::
  698. I:* If#=dd Alt=dd #EPs=dd Cls=xx(sssss) Sub=xx Prot=xx Driver=ssss
  699. | | | | | | | | |__Driver name
  700. | | | | | | | | or "(none)"
  701. | | | | | | | |__InterfaceProtocol
  702. | | | | | | |__InterfaceSubClass
  703. | | | | | |__InterfaceClass
  704. | | | | |__NumberOfEndpoints
  705. | | | |__AlternateSettingNumber
  706. | | |__InterfaceNumber
  707. | |__ "*" indicates the active altsetting (others are " ")
  708. |__Interface info tag
  709. A given interface may have one or more "alternate" settings.
  710. For example, default settings may not use more than a small
  711. amount of periodic bandwidth. To use significant fractions
  712. of bus bandwidth, drivers must select a non-default altsetting.
  713. Only one setting for an interface may be active at a time, and
  714. only one driver may bind to an interface at a time. Most devices
  715. have only one alternate setting per interface.
  716. Endpoint descriptor info (can be multiple per Interface)
  717. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  718. ::
  719. E: Ad=xx(s) Atr=xx(ssss) MxPS=dddd Ivl=dddss
  720. | | | | |__Interval (max) between transfers
  721. | | | |__EndpointMaxPacketSize
  722. | | |__Attributes(EndpointType)
  723. | |__EndpointAddress(I=In,O=Out)
  724. |__Endpoint info tag
  725. The interval is nonzero for all periodic (interrupt or isochronous)
  726. endpoints. For high speed endpoints the transfer interval may be
  727. measured in microseconds rather than milliseconds.
  728. For high speed periodic endpoints, the ``EndpointMaxPacketSize`` reflects
  729. the per-microframe data transfer size. For "high bandwidth"
  730. endpoints, that can reflect two or three packets (for up to
  731. 3KBytes every 125 usec) per endpoint.
  732. With the Linux-USB stack, periodic bandwidth reservations use the
  733. transfer intervals and sizes provided by URBs, which can be less
  734. than those found in endpoint descriptor.
  735. Usage examples
  736. ~~~~~~~~~~~~~~
  737. If a user or script is interested only in Topology info, for
  738. example, use something like ``grep ^T: /sys/kernel/debug/usb/devices``
  739. for only the Topology lines. A command like
  740. ``grep -i ^[tdp]: /sys/kernel/debug/usb/devices`` can be used to list
  741. only the lines that begin with the characters in square brackets,
  742. where the valid characters are TDPCIE. With a slightly more able
  743. script, it can display any selected lines (for example, only T, D,
  744. and P lines) and change their output format. (The ``procusb``
  745. Perl script is the beginning of this idea. It will list only
  746. selected lines [selected from TBDPSCIE] or "All" lines from
  747. ``/sys/kernel/debug/usb/devices``.)
  748. The Topology lines can be used to generate a graphic/pictorial
  749. of the USB devices on a system's root hub. (See more below
  750. on how to do this.)
  751. The Interface lines can be used to determine what driver is
  752. being used for each device, and which altsetting it activated.
  753. The Configuration lines could be used to list maximum power
  754. (in milliamps) that a system's USB devices are using.
  755. For example, ``grep ^C: /sys/kernel/debug/usb/devices``.
  756. Here's an example, from a system which has a UHCI root hub,
  757. an external hub connected to the root hub, and a mouse and
  758. a serial converter connected to the external hub.
  759. ::
  760. T: Bus=00 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
  761. B: Alloc= 28/900 us ( 3%), #Int= 2, #Iso= 0
  762. D: Ver= 1.00 Cls=09(hub ) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1
  763. P: Vendor=0000 ProdID=0000 Rev= 0.00
  764. S: Product=USB UHCI Root Hub
  765. S: SerialNumber=dce0
  766. C:* #Ifs= 1 Cfg#= 1 Atr=40 MxPwr= 0mA
  767. I: If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
  768. E: Ad=81(I) Atr=03(Int.) MxPS= 8 Ivl=255ms
  769. T: Bus=00 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 2 Spd=12 MxCh= 4
  770. D: Ver= 1.00 Cls=09(hub ) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1
  771. P: Vendor=0451 ProdID=1446 Rev= 1.00
  772. C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr=100mA
  773. I: If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
  774. E: Ad=81(I) Atr=03(Int.) MxPS= 1 Ivl=255ms
  775. T: Bus=00 Lev=02 Prnt=02 Port=00 Cnt=01 Dev#= 3 Spd=1.5 MxCh= 0
  776. D: Ver= 1.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1
  777. P: Vendor=04b4 ProdID=0001 Rev= 0.00
  778. C:* #Ifs= 1 Cfg#= 1 Atr=80 MxPwr=100mA
  779. I: If#= 0 Alt= 0 #EPs= 1 Cls=03(HID ) Sub=01 Prot=02 Driver=mouse
  780. E: Ad=81(I) Atr=03(Int.) MxPS= 3 Ivl= 10ms
  781. T: Bus=00 Lev=02 Prnt=02 Port=02 Cnt=02 Dev#= 4 Spd=12 MxCh= 0
  782. D: Ver= 1.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1
  783. P: Vendor=0565 ProdID=0001 Rev= 1.08
  784. S: Manufacturer=Peracom Networks, Inc.
  785. S: Product=Peracom USB to Serial Converter
  786. C:* #Ifs= 1 Cfg#= 1 Atr=a0 MxPwr=100mA
  787. I: If#= 0 Alt= 0 #EPs= 3 Cls=00(>ifc ) Sub=00 Prot=00 Driver=serial
  788. E: Ad=81(I) Atr=02(Bulk) MxPS= 64 Ivl= 16ms
  789. E: Ad=01(O) Atr=02(Bulk) MxPS= 16 Ivl= 16ms
  790. E: Ad=82(I) Atr=03(Int.) MxPS= 8 Ivl= 8ms
  791. Selecting only the ``T:`` and ``I:`` lines from this (for example, by using
  792. ``procusb ti``), we have
  793. ::
  794. T: Bus=00 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
  795. T: Bus=00 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 2 Spd=12 MxCh= 4
  796. I: If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
  797. T: Bus=00 Lev=02 Prnt=02 Port=00 Cnt=01 Dev#= 3 Spd=1.5 MxCh= 0
  798. I: If#= 0 Alt= 0 #EPs= 1 Cls=03(HID ) Sub=01 Prot=02 Driver=mouse
  799. T: Bus=00 Lev=02 Prnt=02 Port=02 Cnt=02 Dev#= 4 Spd=12 MxCh= 0
  800. I: If#= 0 Alt= 0 #EPs= 3 Cls=00(>ifc ) Sub=00 Prot=00 Driver=serial
  801. Physically this looks like (or could be converted to)::
  802. +------------------+
  803. | PC/root_hub (12)| Dev# = 1
  804. +------------------+ (nn) is Mbps.
  805. Level 0 | CN.0 | CN.1 | [CN = connector/port #]
  806. +------------------+
  807. /
  808. /
  809. +-----------------------+
  810. Level 1 | Dev#2: 4-port hub (12)|
  811. +-----------------------+
  812. |CN.0 |CN.1 |CN.2 |CN.3 |
  813. +-----------------------+
  814. \ \____________________
  815. \_____ \
  816. \ \
  817. +--------------------+ +--------------------+
  818. Level 2 | Dev# 3: mouse (1.5)| | Dev# 4: serial (12)|
  819. +--------------------+ +--------------------+
  820. Or, in a more tree-like structure (ports [Connectors] without
  821. connections could be omitted)::
  822. PC: Dev# 1, root hub, 2 ports, 12 Mbps
  823. |_ CN.0: Dev# 2, hub, 4 ports, 12 Mbps
  824. |_ CN.0: Dev #3, mouse, 1.5 Mbps
  825. |_ CN.1:
  826. |_ CN.2: Dev #4, serial, 12 Mbps
  827. |_ CN.3:
  828. |_ CN.1: