usb.rst 42 KB

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