usb.rst 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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_GET_SPEED
  338. Returns the speed of the device. The speed is returned as a
  339. numerical value in accordance with enum usb_device_speed
  340. File modification time is not updated by this request.
  341. USBDEVFS_GETDRIVER
  342. Returns the name of the kernel driver bound to a given interface (a
  343. string). Parameter is a pointer to this structure, which is
  344. modified::
  345. struct usbdevfs_getdriver {
  346. unsigned int interface;
  347. char driver[USBDEVFS_MAXDRIVERNAME + 1];
  348. };
  349. File modification time is not updated by this request.
  350. USBDEVFS_IOCTL
  351. Passes a request from userspace through to a kernel driver that has
  352. an ioctl entry in the *struct usb_driver* it registered::
  353. struct usbdevfs_ioctl {
  354. int ifno;
  355. int ioctl_code;
  356. void *data;
  357. };
  358. /* user mode call looks like this.
  359. * 'request' becomes the driver->ioctl() 'code' parameter.
  360. * the size of 'param' is encoded in 'request', and that data
  361. * is copied to or from the driver->ioctl() 'buf' parameter.
  362. */
  363. static int
  364. usbdev_ioctl (int fd, int ifno, unsigned request, void *param)
  365. {
  366. struct usbdevfs_ioctl wrapper;
  367. wrapper.ifno = ifno;
  368. wrapper.ioctl_code = request;
  369. wrapper.data = param;
  370. return ioctl (fd, USBDEVFS_IOCTL, &wrapper);
  371. }
  372. File modification time is not updated by this request.
  373. This request lets kernel drivers talk to user mode code through
  374. filesystem operations even when they don't create a character or
  375. block special device. It's also been used to do things like ask
  376. devices what device special file should be used. Two pre-defined
  377. ioctls are used to disconnect and reconnect kernel drivers, so that
  378. user mode code can completely manage binding and configuration of
  379. devices.
  380. USBDEVFS_RELEASEINTERFACE
  381. This is used to release the claim usbfs made on interface, either
  382. implicitly or because of a USBDEVFS_CLAIMINTERFACE call, before the
  383. file descriptor is closed. The ioctl parameter is an integer holding
  384. the number of the interface (bInterfaceNumber from descriptor); File
  385. modification time is not updated by this request.
  386. .. warning::
  387. *No security check is made to ensure that the task which made
  388. the claim is the one which is releasing it. This means that user
  389. mode driver may interfere other ones.*
  390. USBDEVFS_RESETEP
  391. Resets the data toggle value for an endpoint (bulk or interrupt) to
  392. DATA0. The ioctl parameter is an integer endpoint number (1 to 15,
  393. as identified in the endpoint descriptor), with USB_DIR_IN added
  394. if the device's endpoint sends data to the host.
  395. .. Warning::
  396. *Avoid using this request. It should probably be removed.* Using
  397. it typically means the device and driver will lose toggle
  398. synchronization. If you really lost synchronization, you likely
  399. need to completely handshake with the device, using a request
  400. like CLEAR_HALT or SET_INTERFACE.
  401. USBDEVFS_DROP_PRIVILEGES
  402. This is used to relinquish the ability to do certain operations
  403. which are considered to be privileged on a usbfs file descriptor.
  404. This includes claiming arbitrary interfaces, resetting a device on
  405. which there are currently claimed interfaces from other users, and
  406. issuing USBDEVFS_IOCTL calls. The ioctl parameter is a 32 bit mask
  407. of interfaces the user is allowed to claim on this file descriptor.
  408. You may issue this ioctl more than one time to narrow said mask.
  409. Synchronous I/O Support
  410. ~~~~~~~~~~~~~~~~~~~~~~~
  411. Synchronous requests involve the kernel blocking until the user mode
  412. request completes, either by finishing successfully or by reporting an
  413. error. In most cases this is the simplest way to use usbfs, although as
  414. noted above it does prevent performing I/O to more than one endpoint at
  415. a time.
  416. USBDEVFS_BULK
  417. Issues a bulk read or write request to the device. The ioctl
  418. parameter is a pointer to this structure::
  419. struct usbdevfs_bulktransfer {
  420. unsigned int ep;
  421. unsigned int len;
  422. unsigned int timeout; /* in milliseconds */
  423. void *data;
  424. };
  425. The ``ep`` value identifies a bulk endpoint number (1 to 15, as
  426. identified in an endpoint descriptor), masked with USB_DIR_IN when
  427. referring to an endpoint which sends data to the host from the
  428. device. The length of the data buffer is identified by ``len``; Recent
  429. kernels support requests up to about 128KBytes. *FIXME say how read
  430. length is returned, and how short reads are handled.*.
  431. USBDEVFS_CLEAR_HALT
  432. Clears endpoint halt (stall) and resets the endpoint toggle. This is
  433. only meaningful for bulk or interrupt endpoints. The ioctl parameter
  434. is an integer endpoint number (1 to 15, as identified in an endpoint
  435. descriptor), masked with USB_DIR_IN when referring to an endpoint
  436. which sends data to the host from the device.
  437. Use this on bulk or interrupt endpoints which have stalled,
  438. returning ``-EPIPE`` status to a data transfer request. Do not issue
  439. the control request directly, since that could invalidate the host's
  440. record of the data toggle.
  441. USBDEVFS_CONTROL
  442. Issues a control request to the device. The ioctl parameter points
  443. to a structure like this::
  444. struct usbdevfs_ctrltransfer {
  445. __u8 bRequestType;
  446. __u8 bRequest;
  447. __u16 wValue;
  448. __u16 wIndex;
  449. __u16 wLength;
  450. __u32 timeout; /* in milliseconds */
  451. void *data;
  452. };
  453. The first eight bytes of this structure are the contents of the
  454. SETUP packet to be sent to the device; see the USB 2.0 specification
  455. for details. The bRequestType value is composed by combining a
  456. ``USB_TYPE_*`` value, a ``USB_DIR_*`` value, and a ``USB_RECIP_*``
  457. value (from ``linux/usb.h``). If wLength is nonzero, it describes
  458. the length of the data buffer, which is either written to the device
  459. (USB_DIR_OUT) or read from the device (USB_DIR_IN).
  460. At this writing, you can't transfer more than 4 KBytes of data to or
  461. from a device; usbfs has a limit, and some host controller drivers
  462. have a limit. (That's not usually a problem.) *Also* there's no way
  463. to say it's not OK to get a short read back from the device.
  464. USBDEVFS_RESET
  465. Does a USB level device reset. The ioctl parameter is ignored. After
  466. the reset, this rebinds all device interfaces. File modification
  467. time is not updated by this request.
  468. .. warning::
  469. *Avoid using this call* until some usbcore bugs get fixed, since
  470. it does not fully synchronize device, interface, and driver (not
  471. just usbfs) state.
  472. USBDEVFS_SETINTERFACE
  473. Sets the alternate setting for an interface. The ioctl parameter is
  474. a pointer to a structure like this::
  475. struct usbdevfs_setinterface {
  476. unsigned int interface;
  477. unsigned int altsetting;
  478. };
  479. File modification time is not updated by this request.
  480. Those struct members are from some interface descriptor applying to
  481. the current configuration. The interface number is the
  482. bInterfaceNumber value, and the altsetting number is the
  483. bAlternateSetting value. (This resets each endpoint in the
  484. interface.)
  485. USBDEVFS_SETCONFIGURATION
  486. Issues the :c:func:`usb_set_configuration()` call for the
  487. device. The parameter is an integer holding the number of a
  488. configuration (bConfigurationValue from descriptor). File
  489. modification time is not updated by this request.
  490. .. warning::
  491. *Avoid using this call* until some usbcore bugs get fixed, since
  492. it does not fully synchronize device, interface, and driver (not
  493. just usbfs) state.
  494. Asynchronous I/O Support
  495. ~~~~~~~~~~~~~~~~~~~~~~~~
  496. As mentioned above, there are situations where it may be important to
  497. initiate concurrent operations from user mode code. This is particularly
  498. important for periodic transfers (interrupt and isochronous), but it can
  499. be used for other kinds of USB requests too. In such cases, the
  500. asynchronous requests described here are essential. Rather than
  501. submitting one request and having the kernel block until it completes,
  502. the blocking is separate.
  503. These requests are packaged into a structure that resembles the URB used
  504. by kernel device drivers. (No POSIX Async I/O support here, sorry.) It
  505. identifies the endpoint type (``USBDEVFS_URB_TYPE_*``), endpoint
  506. (number, masked with USB_DIR_IN as appropriate), buffer and length,
  507. and a user "context" value serving to uniquely identify each request.
  508. (It's usually a pointer to per-request data.) Flags can modify requests
  509. (not as many as supported for kernel drivers).
  510. Each request can specify a realtime signal number (between SIGRTMIN and
  511. SIGRTMAX, inclusive) to request a signal be sent when the request
  512. completes.
  513. When usbfs returns these urbs, the status value is updated, and the
  514. buffer may have been modified. Except for isochronous transfers, the
  515. actual_length is updated to say how many bytes were transferred; if the
  516. USBDEVFS_URB_DISABLE_SPD flag is set ("short packets are not OK"), if
  517. fewer bytes were read than were requested then you get an error report::
  518. struct usbdevfs_iso_packet_desc {
  519. unsigned int length;
  520. unsigned int actual_length;
  521. unsigned int status;
  522. };
  523. struct usbdevfs_urb {
  524. unsigned char type;
  525. unsigned char endpoint;
  526. int status;
  527. unsigned int flags;
  528. void *buffer;
  529. int buffer_length;
  530. int actual_length;
  531. int start_frame;
  532. int number_of_packets;
  533. int error_count;
  534. unsigned int signr;
  535. void *usercontext;
  536. struct usbdevfs_iso_packet_desc iso_frame_desc[];
  537. };
  538. For these asynchronous requests, the file modification time reflects
  539. when the request was initiated. This contrasts with their use with the
  540. synchronous requests, where it reflects when requests complete.
  541. USBDEVFS_DISCARDURB
  542. *TBS* File modification time is not updated by this request.
  543. USBDEVFS_DISCSIGNAL
  544. *TBS* File modification time is not updated by this request.
  545. USBDEVFS_REAPURB
  546. *TBS* File modification time is not updated by this request.
  547. USBDEVFS_REAPURBNDELAY
  548. *TBS* File modification time is not updated by this request.
  549. USBDEVFS_SUBMITURB
  550. *TBS*
  551. The USB devices
  552. ===============
  553. The USB devices are now exported via debugfs:
  554. - ``/sys/kernel/debug/usb/devices`` ... a text file showing each of the USB
  555. devices on known to the kernel, and their configuration descriptors.
  556. You can also poll() this to learn about new devices.
  557. /sys/kernel/debug/usb/devices
  558. -----------------------------
  559. This file is handy for status viewing tools in user mode, which can scan
  560. the text format and ignore most of it. More detailed device status
  561. (including class and vendor status) is available from device-specific
  562. files. For information about the current format of this file, see below.
  563. This file, in combination with the poll() system call, can also be used
  564. to detect when devices are added or removed::
  565. int fd;
  566. struct pollfd pfd;
  567. fd = open("/sys/kernel/debug/usb/devices", O_RDONLY);
  568. pfd = { fd, POLLIN, 0 };
  569. for (;;) {
  570. /* The first time through, this call will return immediately. */
  571. poll(&pfd, 1, -1);
  572. /* To see what's changed, compare the file's previous and current
  573. contents or scan the filesystem. (Scanning is more precise.) */
  574. }
  575. Note that this behavior is intended to be used for informational and
  576. debug purposes. It would be more appropriate to use programs such as
  577. udev or HAL to initialize a device or start a user-mode helper program,
  578. for instance.
  579. In this file, each device's output has multiple lines of ASCII output.
  580. I made it ASCII instead of binary on purpose, so that someone
  581. can obtain some useful data from it without the use of an
  582. auxiliary program. However, with an auxiliary program, the numbers
  583. in the first 4 columns of each ``T:`` line (topology info:
  584. Lev, Prnt, Port, Cnt) can be used to build a USB topology diagram.
  585. Each line is tagged with a one-character ID for that line::
  586. T = Topology (etc.)
  587. B = Bandwidth (applies only to USB host controllers, which are
  588. virtualized as root hubs)
  589. D = Device descriptor info.
  590. P = Product ID info. (from Device descriptor, but they won't fit
  591. together on one line)
  592. S = String descriptors.
  593. C = Configuration descriptor info. (* = active configuration)
  594. I = Interface descriptor info.
  595. E = Endpoint descriptor info.
  596. /sys/kernel/debug/usb/devices output format
  597. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  598. Legend::
  599. d = decimal number (may have leading spaces or 0's)
  600. x = hexadecimal number (may have leading spaces or 0's)
  601. s = string
  602. Topology info
  603. ^^^^^^^^^^^^^
  604. ::
  605. T: Bus=dd Lev=dd Prnt=dd Port=dd Cnt=dd Dev#=ddd Spd=dddd MxCh=dd
  606. | | | | | | | | |__MaxChildren
  607. | | | | | | | |__Device Speed in Mbps
  608. | | | | | | |__DeviceNumber
  609. | | | | | |__Count of devices at this level
  610. | | | | |__Connector/Port on Parent for this device
  611. | | | |__Parent DeviceNumber
  612. | | |__Level in topology for this bus
  613. | |__Bus number
  614. |__Topology info tag
  615. Speed may be:
  616. ======= ======================================================
  617. 1.5 Mbit/s for low speed USB
  618. 12 Mbit/s for full speed USB
  619. 480 Mbit/s for high speed USB (added for USB 2.0)
  620. 5000 Mbit/s for SuperSpeed USB (added for USB 3.0)
  621. ======= ======================================================
  622. For reasons lost in the mists of time, the Port number is always
  623. too low by 1. For example, a device plugged into port 4 will
  624. show up with ``Port=03``.
  625. Bandwidth info
  626. ^^^^^^^^^^^^^^
  627. ::
  628. B: Alloc=ddd/ddd us (xx%), #Int=ddd, #Iso=ddd
  629. | | | |__Number of isochronous requests
  630. | | |__Number of interrupt requests
  631. | |__Total Bandwidth allocated to this bus
  632. |__Bandwidth info tag
  633. Bandwidth allocation is an approximation of how much of one frame
  634. (millisecond) is in use. It reflects only periodic transfers, which
  635. are the only transfers that reserve bandwidth. Control and bulk
  636. transfers use all other bandwidth, including reserved bandwidth that
  637. is not used for transfers (such as for short packets).
  638. The percentage is how much of the "reserved" bandwidth is scheduled by
  639. those transfers. For a low or full speed bus (loosely, "USB 1.1"),
  640. 90% of the bus bandwidth is reserved. For a high speed bus (loosely,
  641. "USB 2.0") 80% is reserved.
  642. Device descriptor info & Product ID info
  643. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  644. ::
  645. D: Ver=x.xx Cls=xx(s) Sub=xx Prot=xx MxPS=dd #Cfgs=dd
  646. P: Vendor=xxxx ProdID=xxxx Rev=xx.xx
  647. where::
  648. D: Ver=x.xx Cls=xx(sssss) Sub=xx Prot=xx MxPS=dd #Cfgs=dd
  649. | | | | | | |__NumberConfigurations
  650. | | | | | |__MaxPacketSize of Default Endpoint
  651. | | | | |__DeviceProtocol
  652. | | | |__DeviceSubClass
  653. | | |__DeviceClass
  654. | |__Device USB version
  655. |__Device info tag #1
  656. where::
  657. P: Vendor=xxxx ProdID=xxxx Rev=xx.xx
  658. | | | |__Product revision number
  659. | | |__Product ID code
  660. | |__Vendor ID code
  661. |__Device info tag #2
  662. String descriptor info
  663. ^^^^^^^^^^^^^^^^^^^^^^
  664. ::
  665. S: Manufacturer=ssss
  666. | |__Manufacturer of this device as read from the device.
  667. | For USB host controller drivers (virtual root hubs) this may
  668. | be omitted, or (for newer drivers) will identify the kernel
  669. | version and the driver which provides this hub emulation.
  670. |__String info tag
  671. S: Product=ssss
  672. | |__Product description of this device as read from the device.
  673. | For older USB host controller drivers (virtual root hubs) this
  674. | indicates the driver; for newer ones, it's a product (and vendor)
  675. | description that often comes from the kernel's PCI ID database.
  676. |__String info tag
  677. S: SerialNumber=ssss
  678. | |__Serial Number of this device as read from the device.
  679. | For USB host controller drivers (virtual root hubs) this is
  680. | some unique ID, normally a bus ID (address or slot name) that
  681. | can't be shared with any other device.
  682. |__String info tag
  683. Configuration descriptor info
  684. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  685. ::
  686. C:* #Ifs=dd Cfg#=dd Atr=xx MPwr=dddmA
  687. | | | | | |__MaxPower in mA
  688. | | | | |__Attributes
  689. | | | |__ConfiguratioNumber
  690. | | |__NumberOfInterfaces
  691. | |__ "*" indicates the active configuration (others are " ")
  692. |__Config info tag
  693. USB devices may have multiple configurations, each of which act
  694. rather differently. For example, a bus-powered configuration
  695. might be much less capable than one that is self-powered. Only
  696. one device configuration can be active at a time; most devices
  697. have only one configuration.
  698. Each configuration consists of one or more interfaces. Each
  699. interface serves a distinct "function", which is typically bound
  700. to a different USB device driver. One common example is a USB
  701. speaker with an audio interface for playback, and a HID interface
  702. for use with software volume control.
  703. Interface descriptor info (can be multiple per Config)
  704. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  705. ::
  706. I:* If#=dd Alt=dd #EPs=dd Cls=xx(sssss) Sub=xx Prot=xx Driver=ssss
  707. | | | | | | | | |__Driver name
  708. | | | | | | | | or "(none)"
  709. | | | | | | | |__InterfaceProtocol
  710. | | | | | | |__InterfaceSubClass
  711. | | | | | |__InterfaceClass
  712. | | | | |__NumberOfEndpoints
  713. | | | |__AlternateSettingNumber
  714. | | |__InterfaceNumber
  715. | |__ "*" indicates the active altsetting (others are " ")
  716. |__Interface info tag
  717. A given interface may have one or more "alternate" settings.
  718. For example, default settings may not use more than a small
  719. amount of periodic bandwidth. To use significant fractions
  720. of bus bandwidth, drivers must select a non-default altsetting.
  721. Only one setting for an interface may be active at a time, and
  722. only one driver may bind to an interface at a time. Most devices
  723. have only one alternate setting per interface.
  724. Endpoint descriptor info (can be multiple per Interface)
  725. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  726. ::
  727. E: Ad=xx(s) Atr=xx(ssss) MxPS=dddd Ivl=dddss
  728. | | | | |__Interval (max) between transfers
  729. | | | |__EndpointMaxPacketSize
  730. | | |__Attributes(EndpointType)
  731. | |__EndpointAddress(I=In,O=Out)
  732. |__Endpoint info tag
  733. The interval is nonzero for all periodic (interrupt or isochronous)
  734. endpoints. For high speed endpoints the transfer interval may be
  735. measured in microseconds rather than milliseconds.
  736. For high speed periodic endpoints, the ``EndpointMaxPacketSize`` reflects
  737. the per-microframe data transfer size. For "high bandwidth"
  738. endpoints, that can reflect two or three packets (for up to
  739. 3KBytes every 125 usec) per endpoint.
  740. With the Linux-USB stack, periodic bandwidth reservations use the
  741. transfer intervals and sizes provided by URBs, which can be less
  742. than those found in endpoint descriptor.
  743. Usage examples
  744. ~~~~~~~~~~~~~~
  745. If a user or script is interested only in Topology info, for
  746. example, use something like ``grep ^T: /sys/kernel/debug/usb/devices``
  747. for only the Topology lines. A command like
  748. ``grep -i ^[tdp]: /sys/kernel/debug/usb/devices`` can be used to list
  749. only the lines that begin with the characters in square brackets,
  750. where the valid characters are TDPCIE. With a slightly more able
  751. script, it can display any selected lines (for example, only T, D,
  752. and P lines) and change their output format. (The ``procusb``
  753. Perl script is the beginning of this idea. It will list only
  754. selected lines [selected from TBDPSCIE] or "All" lines from
  755. ``/sys/kernel/debug/usb/devices``.)
  756. The Topology lines can be used to generate a graphic/pictorial
  757. of the USB devices on a system's root hub. (See more below
  758. on how to do this.)
  759. The Interface lines can be used to determine what driver is
  760. being used for each device, and which altsetting it activated.
  761. The Configuration lines could be used to list maximum power
  762. (in milliamps) that a system's USB devices are using.
  763. For example, ``grep ^C: /sys/kernel/debug/usb/devices``.
  764. Here's an example, from a system which has a UHCI root hub,
  765. an external hub connected to the root hub, and a mouse and
  766. a serial converter connected to the external hub.
  767. ::
  768. T: Bus=00 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
  769. B: Alloc= 28/900 us ( 3%), #Int= 2, #Iso= 0
  770. D: Ver= 1.00 Cls=09(hub ) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1
  771. P: Vendor=0000 ProdID=0000 Rev= 0.00
  772. S: Product=USB UHCI Root Hub
  773. S: SerialNumber=dce0
  774. C:* #Ifs= 1 Cfg#= 1 Atr=40 MxPwr= 0mA
  775. I: If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
  776. E: Ad=81(I) Atr=03(Int.) MxPS= 8 Ivl=255ms
  777. T: Bus=00 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 2 Spd=12 MxCh= 4
  778. D: Ver= 1.00 Cls=09(hub ) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1
  779. P: Vendor=0451 ProdID=1446 Rev= 1.00
  780. C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr=100mA
  781. I: If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
  782. E: Ad=81(I) Atr=03(Int.) MxPS= 1 Ivl=255ms
  783. T: Bus=00 Lev=02 Prnt=02 Port=00 Cnt=01 Dev#= 3 Spd=1.5 MxCh= 0
  784. D: Ver= 1.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1
  785. P: Vendor=04b4 ProdID=0001 Rev= 0.00
  786. C:* #Ifs= 1 Cfg#= 1 Atr=80 MxPwr=100mA
  787. I: If#= 0 Alt= 0 #EPs= 1 Cls=03(HID ) Sub=01 Prot=02 Driver=mouse
  788. E: Ad=81(I) Atr=03(Int.) MxPS= 3 Ivl= 10ms
  789. T: Bus=00 Lev=02 Prnt=02 Port=02 Cnt=02 Dev#= 4 Spd=12 MxCh= 0
  790. D: Ver= 1.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1
  791. P: Vendor=0565 ProdID=0001 Rev= 1.08
  792. S: Manufacturer=Peracom Networks, Inc.
  793. S: Product=Peracom USB to Serial Converter
  794. C:* #Ifs= 1 Cfg#= 1 Atr=a0 MxPwr=100mA
  795. I: If#= 0 Alt= 0 #EPs= 3 Cls=00(>ifc ) Sub=00 Prot=00 Driver=serial
  796. E: Ad=81(I) Atr=02(Bulk) MxPS= 64 Ivl= 16ms
  797. E: Ad=01(O) Atr=02(Bulk) MxPS= 16 Ivl= 16ms
  798. E: Ad=82(I) Atr=03(Int.) MxPS= 8 Ivl= 8ms
  799. Selecting only the ``T:`` and ``I:`` lines from this (for example, by using
  800. ``procusb ti``), we have
  801. ::
  802. T: Bus=00 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
  803. T: Bus=00 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 2 Spd=12 MxCh= 4
  804. I: If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
  805. T: Bus=00 Lev=02 Prnt=02 Port=00 Cnt=01 Dev#= 3 Spd=1.5 MxCh= 0
  806. I: If#= 0 Alt= 0 #EPs= 1 Cls=03(HID ) Sub=01 Prot=02 Driver=mouse
  807. T: Bus=00 Lev=02 Prnt=02 Port=02 Cnt=02 Dev#= 4 Spd=12 MxCh= 0
  808. I: If#= 0 Alt= 0 #EPs= 3 Cls=00(>ifc ) Sub=00 Prot=00 Driver=serial
  809. Physically this looks like (or could be converted to)::
  810. +------------------+
  811. | PC/root_hub (12)| Dev# = 1
  812. +------------------+ (nn) is Mbps.
  813. Level 0 | CN.0 | CN.1 | [CN = connector/port #]
  814. +------------------+
  815. /
  816. /
  817. +-----------------------+
  818. Level 1 | Dev#2: 4-port hub (12)|
  819. +-----------------------+
  820. |CN.0 |CN.1 |CN.2 |CN.3 |
  821. +-----------------------+
  822. \ \____________________
  823. \_____ \
  824. \ \
  825. +--------------------+ +--------------------+
  826. Level 2 | Dev# 3: mouse (1.5)| | Dev# 4: serial (12)|
  827. +--------------------+ +--------------------+
  828. Or, in a more tree-like structure (ports [Connectors] without
  829. connections could be omitted)::
  830. PC: Dev# 1, root hub, 2 ports, 12 Mbps
  831. |_ CN.0: Dev# 2, hub, 4 ports, 12 Mbps
  832. |_ CN.0: Dev #3, mouse, 1.5 Mbps
  833. |_ CN.1:
  834. |_ CN.2: Dev #4, serial, 12 Mbps
  835. |_ CN.3:
  836. |_ CN.1: