18c2ecf20Sopenharmony_ci.. _usb-hostside-api:
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci===========================
48c2ecf20Sopenharmony_ciThe Linux-USB Host Side API
58c2ecf20Sopenharmony_ci===========================
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ciIntroduction to USB on Linux
88c2ecf20Sopenharmony_ci============================
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ciA Universal Serial Bus (USB) is used to connect a host, such as a PC or
118c2ecf20Sopenharmony_ciworkstation, to a number of peripheral devices. USB uses a tree
128c2ecf20Sopenharmony_cistructure, with the host as the root (the system's master), hubs as
138c2ecf20Sopenharmony_ciinterior nodes, and peripherals as leaves (and slaves). Modern PCs
148c2ecf20Sopenharmony_cisupport several such trees of USB devices, usually
158c2ecf20Sopenharmony_cia few USB 3.0 (5 GBit/s) or USB 3.1 (10 GBit/s) and some legacy
168c2ecf20Sopenharmony_ciUSB 2.0 (480 MBit/s) busses just in case.
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ciThat master/slave asymmetry was designed-in for a number of reasons, one
198c2ecf20Sopenharmony_cibeing ease of use. It is not physically possible to mistake upstream and
208c2ecf20Sopenharmony_cidownstream or it does not matter with a type C plug (or they are built into the
218c2ecf20Sopenharmony_ciperipheral). Also, the host software doesn't need to deal with
228c2ecf20Sopenharmony_cidistributed auto-configuration since the pre-designated master node
238c2ecf20Sopenharmony_cimanages all that.
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ciKernel developers added USB support to Linux early in the 2.2 kernel
268c2ecf20Sopenharmony_ciseries and have been developing it further since then. Besides support
278c2ecf20Sopenharmony_cifor each new generation of USB, various host controllers gained support,
288c2ecf20Sopenharmony_cinew drivers for peripherals have been added and advanced features for latency
298c2ecf20Sopenharmony_cimeasurement and improved power management introduced.
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ciLinux can run inside USB devices as well as on the hosts that control
328c2ecf20Sopenharmony_cithe devices. But USB device drivers running inside those peripherals
338c2ecf20Sopenharmony_cidon't do the same things as the ones running inside hosts, so they've
348c2ecf20Sopenharmony_cibeen given a different name: *gadget drivers*. This document does not
358c2ecf20Sopenharmony_cicover gadget drivers.
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ciUSB Host-Side API Model
388c2ecf20Sopenharmony_ci=======================
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ciHost-side drivers for USB devices talk to the "usbcore" APIs. There are
418c2ecf20Sopenharmony_citwo. One is intended for *general-purpose* drivers (exposed through
428c2ecf20Sopenharmony_cidriver frameworks), and the other is for drivers that are *part of the
438c2ecf20Sopenharmony_cicore*. Such core drivers include the *hub* driver (which manages trees
448c2ecf20Sopenharmony_ciof USB devices) and several different kinds of *host controller
458c2ecf20Sopenharmony_cidrivers*, which control individual busses.
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ciThe device model seen by USB drivers is relatively complex.
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci-  USB supports four kinds of data transfers (control, bulk, interrupt,
508c2ecf20Sopenharmony_ci   and isochronous). Two of them (control and bulk) use bandwidth as
518c2ecf20Sopenharmony_ci   it's available, while the other two (interrupt and isochronous) are
528c2ecf20Sopenharmony_ci   scheduled to provide guaranteed bandwidth.
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci-  The device description model includes one or more "configurations"
558c2ecf20Sopenharmony_ci   per device, only one of which is active at a time. Devices are supposed
568c2ecf20Sopenharmony_ci   to be capable of operating at lower than their top
578c2ecf20Sopenharmony_ci   speeds and may provide a BOS descriptor showing the lowest speed they
588c2ecf20Sopenharmony_ci   remain fully operational at.
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci-  From USB 3.0 on configurations have one or more "functions", which
618c2ecf20Sopenharmony_ci   provide a common functionality and are grouped together for purposes
628c2ecf20Sopenharmony_ci   of power management.
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci-  Configurations or functions have one or more "interfaces", each of which may have
658c2ecf20Sopenharmony_ci   "alternate settings". Interfaces may be standardized by USB "Class"
668c2ecf20Sopenharmony_ci   specifications, or may be specific to a vendor or device.
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci   USB device drivers actually bind to interfaces, not devices. Think of
698c2ecf20Sopenharmony_ci   them as "interface drivers", though you may not see many devices
708c2ecf20Sopenharmony_ci   where the distinction is important. *Most USB devices are simple,
718c2ecf20Sopenharmony_ci   with only one function, one configuration, one interface, and one alternate
728c2ecf20Sopenharmony_ci   setting.*
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci-  Interfaces have one or more "endpoints", each of which supports one
758c2ecf20Sopenharmony_ci   type and direction of data transfer such as "bulk out" or "interrupt
768c2ecf20Sopenharmony_ci   in". The entire configuration may have up to sixteen endpoints in
778c2ecf20Sopenharmony_ci   each direction, allocated as needed among all the interfaces.
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci-  Data transfer on USB is packetized; each endpoint has a maximum
808c2ecf20Sopenharmony_ci   packet size. Drivers must often be aware of conventions such as
818c2ecf20Sopenharmony_ci   flagging the end of bulk transfers using "short" (including zero
828c2ecf20Sopenharmony_ci   length) packets.
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci-  The Linux USB API supports synchronous calls for control and bulk
858c2ecf20Sopenharmony_ci   messages. It also supports asynchronous calls for all kinds of data
868c2ecf20Sopenharmony_ci   transfer, using request structures called "URBs" (USB Request
878c2ecf20Sopenharmony_ci   Blocks).
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ciAccordingly, the USB Core API exposed to device drivers covers quite a
908c2ecf20Sopenharmony_cilot of territory. You'll probably need to consult the USB 3.0
918c2ecf20Sopenharmony_cispecification, available online from www.usb.org at no cost, as well as
928c2ecf20Sopenharmony_ciclass or device specifications.
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ciThe only host-side drivers that actually touch hardware (reading/writing
958c2ecf20Sopenharmony_ciregisters, handling IRQs, and so on) are the HCDs. In theory, all HCDs
968c2ecf20Sopenharmony_ciprovide the same functionality through the same API. In practice, that's
978c2ecf20Sopenharmony_cibecoming more true, but there are still differences
988c2ecf20Sopenharmony_cithat crop up especially with fault handling on the less common controllers.
998c2ecf20Sopenharmony_ciDifferent controllers don't
1008c2ecf20Sopenharmony_cinecessarily report the same aspects of failures, and recovery from
1018c2ecf20Sopenharmony_cifaults (including software-induced ones like unlinking an URB) isn't yet
1028c2ecf20Sopenharmony_cifully consistent. Device driver authors should make a point of doing
1038c2ecf20Sopenharmony_cidisconnect testing (while the device is active) with each different host
1048c2ecf20Sopenharmony_cicontroller driver, to make sure drivers don't have bugs of their own as
1058c2ecf20Sopenharmony_ciwell as to make sure they aren't relying on some HCD-specific behavior.
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci.. _usb_chapter9:
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ciUSB-Standard Types
1108c2ecf20Sopenharmony_ci==================
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ciIn ``<linux/usb/ch9.h>`` you will find the USB data types defined in
1138c2ecf20Sopenharmony_cichapter 9 of the USB specification. These data types are used throughout
1148c2ecf20Sopenharmony_ciUSB, and in APIs including this host side API, gadget APIs, usb character
1158c2ecf20Sopenharmony_cidevices and debugfs interfaces.
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci.. kernel-doc:: include/linux/usb/ch9.h
1188c2ecf20Sopenharmony_ci   :internal:
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci.. _usb_header:
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ciHost-Side Data Types and Macros
1238c2ecf20Sopenharmony_ci===============================
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ciThe host side API exposes several layers to drivers, some of which are
1268c2ecf20Sopenharmony_cimore necessary than others. These support lifecycle models for host side
1278c2ecf20Sopenharmony_cidrivers and devices, and support passing buffers through usbcore to some
1288c2ecf20Sopenharmony_ciHCD that performs the I/O for the device driver.
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci.. kernel-doc:: include/linux/usb.h
1318c2ecf20Sopenharmony_ci   :internal:
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ciUSB Core APIs
1348c2ecf20Sopenharmony_ci=============
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ciThere are two basic I/O models in the USB API. The most elemental one is
1378c2ecf20Sopenharmony_ciasynchronous: drivers submit requests in the form of an URB, and the
1388c2ecf20Sopenharmony_ciURB's completion callback handles the next step. All USB transfer types
1398c2ecf20Sopenharmony_cisupport that model, although there are special cases for control URBs
1408c2ecf20Sopenharmony_ci(which always have setup and status stages, but may not have a data
1418c2ecf20Sopenharmony_cistage) and isochronous URBs (which allow large packets and include
1428c2ecf20Sopenharmony_ciper-packet fault reports). Built on top of that is synchronous API
1438c2ecf20Sopenharmony_cisupport, where a driver calls a routine that allocates one or more URBs,
1448c2ecf20Sopenharmony_cisubmits them, and waits until they complete. There are synchronous
1458c2ecf20Sopenharmony_ciwrappers for single-buffer control and bulk transfers (which are awkward
1468c2ecf20Sopenharmony_cito use in some driver disconnect scenarios), and for scatterlist based
1478c2ecf20Sopenharmony_cistreaming i/o (bulk or interrupt).
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ciUSB drivers need to provide buffers that can be used for DMA, although
1508c2ecf20Sopenharmony_cithey don't necessarily need to provide the DMA mapping themselves. There
1518c2ecf20Sopenharmony_ciare APIs to use used when allocating DMA buffers, which can prevent use
1528c2ecf20Sopenharmony_ciof bounce buffers on some systems. In some cases, drivers may be able to
1538c2ecf20Sopenharmony_cirely on 64bit DMA to eliminate another kind of bounce buffer.
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci.. kernel-doc:: drivers/usb/core/urb.c
1568c2ecf20Sopenharmony_ci   :export:
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci.. kernel-doc:: drivers/usb/core/message.c
1598c2ecf20Sopenharmony_ci   :export:
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci.. kernel-doc:: drivers/usb/core/file.c
1628c2ecf20Sopenharmony_ci   :export:
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci.. kernel-doc:: drivers/usb/core/driver.c
1658c2ecf20Sopenharmony_ci   :export:
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci.. kernel-doc:: drivers/usb/core/usb.c
1688c2ecf20Sopenharmony_ci   :export:
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci.. kernel-doc:: drivers/usb/core/hub.c
1718c2ecf20Sopenharmony_ci   :export:
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ciHost Controller APIs
1748c2ecf20Sopenharmony_ci====================
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ciThese APIs are only for use by host controller drivers, most of which
1778c2ecf20Sopenharmony_ciimplement standard register interfaces such as XHCI, EHCI, OHCI, or UHCI. UHCI
1788c2ecf20Sopenharmony_ciwas one of the first interfaces, designed by Intel and also used by VIA;
1798c2ecf20Sopenharmony_ciit doesn't do much in hardware. OHCI was designed later, to have the
1808c2ecf20Sopenharmony_cihardware do more work (bigger transfers, tracking protocol state, and so
1818c2ecf20Sopenharmony_cion). EHCI was designed with USB 2.0; its design has features that
1828c2ecf20Sopenharmony_ciresemble OHCI (hardware does much more work) as well as UHCI (some parts
1838c2ecf20Sopenharmony_ciof ISO support, TD list processing). XHCI was designed with USB 3.0. It
1848c2ecf20Sopenharmony_cicontinues to shift support for functionality into hardware.
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ciThere are host controllers other than the "big three", although most PCI
1878c2ecf20Sopenharmony_cibased controllers (and a few non-PCI based ones) use one of those
1888c2ecf20Sopenharmony_ciinterfaces. Not all host controllers use DMA; some use PIO, and there is
1898c2ecf20Sopenharmony_cialso a simulator and a virtual host controller to pipe USB over the network.
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ciThe same basic APIs are available to drivers for all those controllers.
1928c2ecf20Sopenharmony_ciFor historical reasons they are in two layers: :c:type:`struct
1938c2ecf20Sopenharmony_ciusb_bus <usb_bus>` is a rather thin layer that became available
1948c2ecf20Sopenharmony_ciin the 2.2 kernels, while :c:type:`struct usb_hcd <usb_hcd>`
1958c2ecf20Sopenharmony_ciis a more featureful layer
1968c2ecf20Sopenharmony_cithat lets HCDs share common code, to shrink driver size and
1978c2ecf20Sopenharmony_cisignificantly reduce hcd-specific behaviors.
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci.. kernel-doc:: drivers/usb/core/hcd.c
2008c2ecf20Sopenharmony_ci   :export:
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci.. kernel-doc:: drivers/usb/core/hcd-pci.c
2038c2ecf20Sopenharmony_ci   :export:
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci.. kernel-doc:: drivers/usb/core/buffer.c
2068c2ecf20Sopenharmony_ci   :internal:
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ciThe USB character device nodes
2098c2ecf20Sopenharmony_ci==============================
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ciThis chapter presents the Linux character device nodes. You may prefer
2128c2ecf20Sopenharmony_cito avoid writing new kernel code for your USB driver. User mode device
2138c2ecf20Sopenharmony_cidrivers are usually packaged as applications or libraries, and may use
2148c2ecf20Sopenharmony_cicharacter devices through some programming library that wraps it.
2158c2ecf20Sopenharmony_ciSuch libraries include:
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci - `libusb <http://libusb.sourceforge.net>`__ for C/C++, and
2188c2ecf20Sopenharmony_ci - `jUSB <http://jUSB.sourceforge.net>`__ for Java.
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ciSome old information about it can be seen at the "USB Device Filesystem"
2218c2ecf20Sopenharmony_cisection of the USB Guide. The latest copy of the USB Guide can be found
2228c2ecf20Sopenharmony_ciat http://www.linux-usb.org/
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci.. note::
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci  - They were used to be implemented via *usbfs*, but this is not part of
2278c2ecf20Sopenharmony_ci    the sysfs debug interface.
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci   - This particular documentation is incomplete, especially with respect
2308c2ecf20Sopenharmony_ci     to the asynchronous mode. As of kernel 2.5.66 the code and this
2318c2ecf20Sopenharmony_ci     (new) documentation need to be cross-reviewed.
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ciWhat files are in "devtmpfs"?
2348c2ecf20Sopenharmony_ci-----------------------------
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ciConventionally mounted at ``/dev/bus/usb/``, usbfs features include:
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci-  ``/dev/bus/usb/BBB/DDD`` ... magic files exposing the each device's
2398c2ecf20Sopenharmony_ci   configuration descriptors, and supporting a series of ioctls for
2408c2ecf20Sopenharmony_ci   making device requests, including I/O to devices. (Purely for access
2418c2ecf20Sopenharmony_ci   by programs.)
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ciEach bus is given a number (``BBB``) based on when it was enumerated; within
2448c2ecf20Sopenharmony_cieach bus, each device is given a similar number (``DDD``). Those ``BBB/DDD``
2458c2ecf20Sopenharmony_cipaths are not "stable" identifiers; expect them to change even if you
2468c2ecf20Sopenharmony_cialways leave the devices plugged in to the same hub port. *Don't even
2478c2ecf20Sopenharmony_cithink of saving these in application configuration files.* Stable
2488c2ecf20Sopenharmony_ciidentifiers are available, for user mode applications that want to use
2498c2ecf20Sopenharmony_cithem. HID and networking devices expose these stable IDs, so that for
2508c2ecf20Sopenharmony_ciexample you can be sure that you told the right UPS to power down its
2518c2ecf20Sopenharmony_cisecond server. Pleast note that it doesn't (yet) expose those IDs.
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci/dev/bus/usb/BBB/DDD
2548c2ecf20Sopenharmony_ci--------------------
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ciUse these files in one of these basic ways:
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci- *They can be read,* producing first the device descriptor (18 bytes) and
2598c2ecf20Sopenharmony_ci  then the descriptors for the current configuration. See the USB 2.0 spec
2608c2ecf20Sopenharmony_ci  for details about those binary data formats. You'll need to convert most
2618c2ecf20Sopenharmony_ci  multibyte values from little endian format to your native host byte
2628c2ecf20Sopenharmony_ci  order, although a few of the fields in the device descriptor (both of
2638c2ecf20Sopenharmony_ci  the BCD-encoded fields, and the vendor and product IDs) will be
2648c2ecf20Sopenharmony_ci  byteswapped for you. Note that configuration descriptors include
2658c2ecf20Sopenharmony_ci  descriptors for interfaces, altsettings, endpoints, and maybe additional
2668c2ecf20Sopenharmony_ci  class descriptors.
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci- *Perform USB operations* using *ioctl()* requests to make endpoint I/O
2698c2ecf20Sopenharmony_ci  requests (synchronously or asynchronously) or manage the device. These
2708c2ecf20Sopenharmony_ci  requests need the ``CAP_SYS_RAWIO`` capability, as well as filesystem
2718c2ecf20Sopenharmony_ci  access permissions. Only one ioctl request can be made on one of these
2728c2ecf20Sopenharmony_ci  device files at a time. This means that if you are synchronously reading
2738c2ecf20Sopenharmony_ci  an endpoint from one thread, you won't be able to write to a different
2748c2ecf20Sopenharmony_ci  endpoint from another thread until the read completes. This works for
2758c2ecf20Sopenharmony_ci  *half duplex* protocols, but otherwise you'd use asynchronous i/o
2768c2ecf20Sopenharmony_ci  requests.
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ciEach connected USB device has one file.  The ``BBB`` indicates the bus
2798c2ecf20Sopenharmony_cinumber.  The ``DDD`` indicates the device address on that bus.  Both
2808c2ecf20Sopenharmony_ciof these numbers are assigned sequentially, and can be reused, so
2818c2ecf20Sopenharmony_ciyou can't rely on them for stable access to devices.  For example,
2828c2ecf20Sopenharmony_ciit's relatively common for devices to re-enumerate while they are
2838c2ecf20Sopenharmony_cistill connected (perhaps someone jostled their power supply, hub,
2848c2ecf20Sopenharmony_cior USB cable), so a device might be ``002/027`` when you first connect
2858c2ecf20Sopenharmony_ciit and ``002/048`` sometime later.
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ciThese files can be read as binary data.  The binary data consists
2888c2ecf20Sopenharmony_ciof first the device descriptor, then the descriptors for each
2898c2ecf20Sopenharmony_ciconfiguration of the device.  Multi-byte fields in the device descriptor
2908c2ecf20Sopenharmony_ciare converted to host endianness by the kernel.  The configuration
2918c2ecf20Sopenharmony_cidescriptors are in bus endian format! The configuration descriptor
2928c2ecf20Sopenharmony_ciare wTotalLength bytes apart. If a device returns less configuration
2938c2ecf20Sopenharmony_cidescriptor data than indicated by wTotalLength there will be a hole in
2948c2ecf20Sopenharmony_cithe file for the missing bytes.  This information is also shown
2958c2ecf20Sopenharmony_ciin text form by the ``/sys/kernel/debug/usb/devices`` file, described later.
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ciThese files may also be used to write user-level drivers for the USB
2988c2ecf20Sopenharmony_cidevices.  You would open the ``/dev/bus/usb/BBB/DDD`` file read/write,
2998c2ecf20Sopenharmony_ciread its descriptors to make sure it's the device you expect, and then
3008c2ecf20Sopenharmony_cibind to an interface (or perhaps several) using an ioctl call.  You
3018c2ecf20Sopenharmony_ciwould issue more ioctls to the device to communicate to it using
3028c2ecf20Sopenharmony_cicontrol, bulk, or other kinds of USB transfers.  The IOCTLs are
3038c2ecf20Sopenharmony_cilisted in the ``<linux/usbdevice_fs.h>`` file, and at this writing the
3048c2ecf20Sopenharmony_cisource code (``linux/drivers/usb/core/devio.c``) is the primary reference
3058c2ecf20Sopenharmony_cifor how to access devices through those files.
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ciNote that since by default these ``BBB/DDD`` files are writable only by
3088c2ecf20Sopenharmony_ciroot, only root can write such user mode drivers.  You can selectively
3098c2ecf20Sopenharmony_cigrant read/write permissions to other users by using ``chmod``.  Also,
3108c2ecf20Sopenharmony_ciusbfs mount options such as ``devmode=0666`` may be helpful.
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ciLife Cycle of User Mode Drivers
3148c2ecf20Sopenharmony_ci-------------------------------
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ciSuch a driver first needs to find a device file for a device it knows
3178c2ecf20Sopenharmony_cihow to handle. Maybe it was told about it because a ``/sbin/hotplug``
3188c2ecf20Sopenharmony_cievent handling agent chose that driver to handle the new device. Or
3198c2ecf20Sopenharmony_cimaybe it's an application that scans all the ``/dev/bus/usb`` device files,
3208c2ecf20Sopenharmony_ciand ignores most devices. In either case, it should :c:func:`read()`
3218c2ecf20Sopenharmony_ciall the descriptors from the device file, and check them against what it
3228c2ecf20Sopenharmony_ciknows how to handle. It might just reject everything except a particular
3238c2ecf20Sopenharmony_civendor and product ID, or need a more complex policy.
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ciNever assume there will only be one such device on the system at a time!
3268c2ecf20Sopenharmony_ciIf your code can't handle more than one device at a time, at least
3278c2ecf20Sopenharmony_cidetect when there's more than one, and have your users choose which
3288c2ecf20Sopenharmony_cidevice to use.
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ciOnce your user mode driver knows what device to use, it interacts with
3318c2ecf20Sopenharmony_ciit in either of two styles. The simple style is to make only control
3328c2ecf20Sopenharmony_cirequests; some devices don't need more complex interactions than those.
3338c2ecf20Sopenharmony_ci(An example might be software using vendor-specific control requests for
3348c2ecf20Sopenharmony_cisome initialization or configuration tasks, with a kernel driver for the
3358c2ecf20Sopenharmony_cirest.)
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ciMore likely, you need a more complex style driver: one using non-control
3388c2ecf20Sopenharmony_ciendpoints, reading or writing data and claiming exclusive use of an
3398c2ecf20Sopenharmony_ciinterface. *Bulk* transfers are easiest to use, but only their sibling
3408c2ecf20Sopenharmony_ci*interrupt* transfers work with low speed devices. Both interrupt and
3418c2ecf20Sopenharmony_ci*isochronous* transfers offer service guarantees because their bandwidth
3428c2ecf20Sopenharmony_ciis reserved. Such "periodic" transfers are awkward to use through usbfs,
3438c2ecf20Sopenharmony_ciunless you're using the asynchronous calls. However, interrupt transfers
3448c2ecf20Sopenharmony_cican also be used in a synchronous "one shot" style.
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ciYour user-mode driver should never need to worry about cleaning up
3478c2ecf20Sopenharmony_cirequest state when the device is disconnected, although it should close
3488c2ecf20Sopenharmony_ciits open file descriptors as soon as it starts seeing the ENODEV errors.
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ciThe ioctl() Requests
3518c2ecf20Sopenharmony_ci--------------------
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ciTo use these ioctls, you need to include the following headers in your
3548c2ecf20Sopenharmony_ciuserspace program::
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci    #include <linux/usb.h>
3578c2ecf20Sopenharmony_ci    #include <linux/usbdevice_fs.h>
3588c2ecf20Sopenharmony_ci    #include <asm/byteorder.h>
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ciThe standard USB device model requests, from "Chapter 9" of the USB 2.0
3618c2ecf20Sopenharmony_cispecification, are automatically included from the ``<linux/usb/ch9.h>``
3628c2ecf20Sopenharmony_ciheader.
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ciUnless noted otherwise, the ioctl requests described here will update
3658c2ecf20Sopenharmony_cithe modification time on the usbfs file to which they are applied
3668c2ecf20Sopenharmony_ci(unless they fail). A return of zero indicates success; otherwise, a
3678c2ecf20Sopenharmony_cistandard USB error code is returned (These are documented in
3688c2ecf20Sopenharmony_ci:ref:`usb-error-codes`).
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ciEach of these files multiplexes access to several I/O streams, one per
3718c2ecf20Sopenharmony_ciendpoint. Each device has one control endpoint (endpoint zero) which
3728c2ecf20Sopenharmony_cisupports a limited RPC style RPC access. Devices are configured by
3738c2ecf20Sopenharmony_cihub_wq (in the kernel) setting a device-wide *configuration* that
3748c2ecf20Sopenharmony_ciaffects things like power consumption and basic functionality. The
3758c2ecf20Sopenharmony_ciendpoints are part of USB *interfaces*, which may have *altsettings*
3768c2ecf20Sopenharmony_ciaffecting things like which endpoints are available. Many devices only
3778c2ecf20Sopenharmony_cihave a single configuration and interface, so drivers for them will
3788c2ecf20Sopenharmony_ciignore configurations and altsettings.
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ciManagement/Status Requests
3818c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ciA number of usbfs requests don't deal very directly with device I/O.
3848c2ecf20Sopenharmony_ciThey mostly relate to device management and status. These are all
3858c2ecf20Sopenharmony_cisynchronous requests.
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ciUSBDEVFS_CLAIMINTERFACE
3888c2ecf20Sopenharmony_ci    This is used to force usbfs to claim a specific interface, which has
3898c2ecf20Sopenharmony_ci    not previously been claimed by usbfs or any other kernel driver. The
3908c2ecf20Sopenharmony_ci    ioctl parameter is an integer holding the number of the interface
3918c2ecf20Sopenharmony_ci    (bInterfaceNumber from descriptor).
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci    Note that if your driver doesn't claim an interface before trying to
3948c2ecf20Sopenharmony_ci    use one of its endpoints, and no other driver has bound to it, then
3958c2ecf20Sopenharmony_ci    the interface is automatically claimed by usbfs.
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci    This claim will be released by a RELEASEINTERFACE ioctl, or by
3988c2ecf20Sopenharmony_ci    closing the file descriptor. File modification time is not updated
3998c2ecf20Sopenharmony_ci    by this request.
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ciUSBDEVFS_CONNECTINFO
4028c2ecf20Sopenharmony_ci    Says whether the device is lowspeed. The ioctl parameter points to a
4038c2ecf20Sopenharmony_ci    structure like this::
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	struct usbdevfs_connectinfo {
4068c2ecf20Sopenharmony_ci		unsigned int   devnum;
4078c2ecf20Sopenharmony_ci		unsigned char  slow;
4088c2ecf20Sopenharmony_ci	};
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci    File modification time is not updated by this request.
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci    *You can't tell whether a "not slow" device is connected at high
4138c2ecf20Sopenharmony_ci    speed (480 MBit/sec) or just full speed (12 MBit/sec).* You should
4148c2ecf20Sopenharmony_ci    know the devnum value already, it's the DDD value of the device file
4158c2ecf20Sopenharmony_ci    name.
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ciUSBDEVFS_GETDRIVER
4188c2ecf20Sopenharmony_ci    Returns the name of the kernel driver bound to a given interface (a
4198c2ecf20Sopenharmony_ci    string). Parameter is a pointer to this structure, which is
4208c2ecf20Sopenharmony_ci    modified::
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	struct usbdevfs_getdriver {
4238c2ecf20Sopenharmony_ci		unsigned int  interface;
4248c2ecf20Sopenharmony_ci		char          driver[USBDEVFS_MAXDRIVERNAME + 1];
4258c2ecf20Sopenharmony_ci	};
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci    File modification time is not updated by this request.
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ciUSBDEVFS_IOCTL
4308c2ecf20Sopenharmony_ci    Passes a request from userspace through to a kernel driver that has
4318c2ecf20Sopenharmony_ci    an ioctl entry in the *struct usb_driver* it registered::
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	struct usbdevfs_ioctl {
4348c2ecf20Sopenharmony_ci		int     ifno;
4358c2ecf20Sopenharmony_ci		int     ioctl_code;
4368c2ecf20Sopenharmony_ci		void    *data;
4378c2ecf20Sopenharmony_ci	};
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	/* user mode call looks like this.
4408c2ecf20Sopenharmony_ci	 * 'request' becomes the driver->ioctl() 'code' parameter.
4418c2ecf20Sopenharmony_ci	 * the size of 'param' is encoded in 'request', and that data
4428c2ecf20Sopenharmony_ci	 * is copied to or from the driver->ioctl() 'buf' parameter.
4438c2ecf20Sopenharmony_ci	 */
4448c2ecf20Sopenharmony_ci	static int
4458c2ecf20Sopenharmony_ci	usbdev_ioctl (int fd, int ifno, unsigned request, void *param)
4468c2ecf20Sopenharmony_ci	{
4478c2ecf20Sopenharmony_ci		struct usbdevfs_ioctl   wrapper;
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci		wrapper.ifno = ifno;
4508c2ecf20Sopenharmony_ci		wrapper.ioctl_code = request;
4518c2ecf20Sopenharmony_ci		wrapper.data = param;
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci		return ioctl (fd, USBDEVFS_IOCTL, &wrapper);
4548c2ecf20Sopenharmony_ci	}
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci    File modification time is not updated by this request.
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci    This request lets kernel drivers talk to user mode code through
4598c2ecf20Sopenharmony_ci    filesystem operations even when they don't create a character or
4608c2ecf20Sopenharmony_ci    block special device. It's also been used to do things like ask
4618c2ecf20Sopenharmony_ci    devices what device special file should be used. Two pre-defined
4628c2ecf20Sopenharmony_ci    ioctls are used to disconnect and reconnect kernel drivers, so that
4638c2ecf20Sopenharmony_ci    user mode code can completely manage binding and configuration of
4648c2ecf20Sopenharmony_ci    devices.
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ciUSBDEVFS_RELEASEINTERFACE
4678c2ecf20Sopenharmony_ci    This is used to release the claim usbfs made on interface, either
4688c2ecf20Sopenharmony_ci    implicitly or because of a USBDEVFS_CLAIMINTERFACE call, before the
4698c2ecf20Sopenharmony_ci    file descriptor is closed. The ioctl parameter is an integer holding
4708c2ecf20Sopenharmony_ci    the number of the interface (bInterfaceNumber from descriptor); File
4718c2ecf20Sopenharmony_ci    modification time is not updated by this request.
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci    .. warning::
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	*No security check is made to ensure that the task which made
4768c2ecf20Sopenharmony_ci	the claim is the one which is releasing it. This means that user
4778c2ecf20Sopenharmony_ci	mode driver may interfere other ones.*
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ciUSBDEVFS_RESETEP
4808c2ecf20Sopenharmony_ci    Resets the data toggle value for an endpoint (bulk or interrupt) to
4818c2ecf20Sopenharmony_ci    DATA0. The ioctl parameter is an integer endpoint number (1 to 15,
4828c2ecf20Sopenharmony_ci    as identified in the endpoint descriptor), with USB_DIR_IN added
4838c2ecf20Sopenharmony_ci    if the device's endpoint sends data to the host.
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci    .. Warning::
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	*Avoid using this request. It should probably be removed.* Using
4888c2ecf20Sopenharmony_ci	it typically means the device and driver will lose toggle
4898c2ecf20Sopenharmony_ci	synchronization. If you really lost synchronization, you likely
4908c2ecf20Sopenharmony_ci	need to completely handshake with the device, using a request
4918c2ecf20Sopenharmony_ci	like CLEAR_HALT or SET_INTERFACE.
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ciUSBDEVFS_DROP_PRIVILEGES
4948c2ecf20Sopenharmony_ci    This is used to relinquish the ability to do certain operations
4958c2ecf20Sopenharmony_ci    which are considered to be privileged on a usbfs file descriptor.
4968c2ecf20Sopenharmony_ci    This includes claiming arbitrary interfaces, resetting a device on
4978c2ecf20Sopenharmony_ci    which there are currently claimed interfaces from other users, and
4988c2ecf20Sopenharmony_ci    issuing USBDEVFS_IOCTL calls. The ioctl parameter is a 32 bit mask
4998c2ecf20Sopenharmony_ci    of interfaces the user is allowed to claim on this file descriptor.
5008c2ecf20Sopenharmony_ci    You may issue this ioctl more than one time to narrow said mask.
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ciSynchronous I/O Support
5038c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ciSynchronous requests involve the kernel blocking until the user mode
5068c2ecf20Sopenharmony_cirequest completes, either by finishing successfully or by reporting an
5078c2ecf20Sopenharmony_cierror. In most cases this is the simplest way to use usbfs, although as
5088c2ecf20Sopenharmony_cinoted above it does prevent performing I/O to more than one endpoint at
5098c2ecf20Sopenharmony_cia time.
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ciUSBDEVFS_BULK
5128c2ecf20Sopenharmony_ci    Issues a bulk read or write request to the device. The ioctl
5138c2ecf20Sopenharmony_ci    parameter is a pointer to this structure::
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	struct usbdevfs_bulktransfer {
5168c2ecf20Sopenharmony_ci		unsigned int  ep;
5178c2ecf20Sopenharmony_ci		unsigned int  len;
5188c2ecf20Sopenharmony_ci		unsigned int  timeout; /* in milliseconds */
5198c2ecf20Sopenharmony_ci		void          *data;
5208c2ecf20Sopenharmony_ci	};
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci    The ``ep`` value identifies a bulk endpoint number (1 to 15, as
5238c2ecf20Sopenharmony_ci    identified in an endpoint descriptor), masked with USB_DIR_IN when
5248c2ecf20Sopenharmony_ci    referring to an endpoint which sends data to the host from the
5258c2ecf20Sopenharmony_ci    device. The length of the data buffer is identified by ``len``; Recent
5268c2ecf20Sopenharmony_ci    kernels support requests up to about 128KBytes. *FIXME say how read
5278c2ecf20Sopenharmony_ci    length is returned, and how short reads are handled.*.
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ciUSBDEVFS_CLEAR_HALT
5308c2ecf20Sopenharmony_ci    Clears endpoint halt (stall) and resets the endpoint toggle. This is
5318c2ecf20Sopenharmony_ci    only meaningful for bulk or interrupt endpoints. The ioctl parameter
5328c2ecf20Sopenharmony_ci    is an integer endpoint number (1 to 15, as identified in an endpoint
5338c2ecf20Sopenharmony_ci    descriptor), masked with USB_DIR_IN when referring to an endpoint
5348c2ecf20Sopenharmony_ci    which sends data to the host from the device.
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci    Use this on bulk or interrupt endpoints which have stalled,
5378c2ecf20Sopenharmony_ci    returning ``-EPIPE`` status to a data transfer request. Do not issue
5388c2ecf20Sopenharmony_ci    the control request directly, since that could invalidate the host's
5398c2ecf20Sopenharmony_ci    record of the data toggle.
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ciUSBDEVFS_CONTROL
5428c2ecf20Sopenharmony_ci    Issues a control request to the device. The ioctl parameter points
5438c2ecf20Sopenharmony_ci    to a structure like this::
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	struct usbdevfs_ctrltransfer {
5468c2ecf20Sopenharmony_ci		__u8   bRequestType;
5478c2ecf20Sopenharmony_ci		__u8   bRequest;
5488c2ecf20Sopenharmony_ci		__u16  wValue;
5498c2ecf20Sopenharmony_ci		__u16  wIndex;
5508c2ecf20Sopenharmony_ci		__u16  wLength;
5518c2ecf20Sopenharmony_ci		__u32  timeout;  /* in milliseconds */
5528c2ecf20Sopenharmony_ci		void   *data;
5538c2ecf20Sopenharmony_ci	};
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci    The first eight bytes of this structure are the contents of the
5568c2ecf20Sopenharmony_ci    SETUP packet to be sent to the device; see the USB 2.0 specification
5578c2ecf20Sopenharmony_ci    for details. The bRequestType value is composed by combining a
5588c2ecf20Sopenharmony_ci    ``USB_TYPE_*`` value, a ``USB_DIR_*`` value, and a ``USB_RECIP_*``
5598c2ecf20Sopenharmony_ci    value (from ``linux/usb.h``). If wLength is nonzero, it describes
5608c2ecf20Sopenharmony_ci    the length of the data buffer, which is either written to the device
5618c2ecf20Sopenharmony_ci    (USB_DIR_OUT) or read from the device (USB_DIR_IN).
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci    At this writing, you can't transfer more than 4 KBytes of data to or
5648c2ecf20Sopenharmony_ci    from a device; usbfs has a limit, and some host controller drivers
5658c2ecf20Sopenharmony_ci    have a limit. (That's not usually a problem.) *Also* there's no way
5668c2ecf20Sopenharmony_ci    to say it's not OK to get a short read back from the device.
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ciUSBDEVFS_RESET
5698c2ecf20Sopenharmony_ci    Does a USB level device reset. The ioctl parameter is ignored. After
5708c2ecf20Sopenharmony_ci    the reset, this rebinds all device interfaces. File modification
5718c2ecf20Sopenharmony_ci    time is not updated by this request.
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci.. warning::
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci	*Avoid using this call* until some usbcore bugs get fixed, since
5768c2ecf20Sopenharmony_ci	it does not fully synchronize device, interface, and driver (not
5778c2ecf20Sopenharmony_ci	just usbfs) state.
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ciUSBDEVFS_SETINTERFACE
5808c2ecf20Sopenharmony_ci    Sets the alternate setting for an interface. The ioctl parameter is
5818c2ecf20Sopenharmony_ci    a pointer to a structure like this::
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	struct usbdevfs_setinterface {
5848c2ecf20Sopenharmony_ci		unsigned int  interface;
5858c2ecf20Sopenharmony_ci		unsigned int  altsetting;
5868c2ecf20Sopenharmony_ci	};
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci    File modification time is not updated by this request.
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci    Those struct members are from some interface descriptor applying to
5918c2ecf20Sopenharmony_ci    the current configuration. The interface number is the
5928c2ecf20Sopenharmony_ci    bInterfaceNumber value, and the altsetting number is the
5938c2ecf20Sopenharmony_ci    bAlternateSetting value. (This resets each endpoint in the
5948c2ecf20Sopenharmony_ci    interface.)
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ciUSBDEVFS_SETCONFIGURATION
5978c2ecf20Sopenharmony_ci    Issues the :c:func:`usb_set_configuration()` call for the
5988c2ecf20Sopenharmony_ci    device. The parameter is an integer holding the number of a
5998c2ecf20Sopenharmony_ci    configuration (bConfigurationValue from descriptor). File
6008c2ecf20Sopenharmony_ci    modification time is not updated by this request.
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci.. warning::
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	*Avoid using this call* until some usbcore bugs get fixed, since
6058c2ecf20Sopenharmony_ci	it does not fully synchronize device, interface, and driver (not
6068c2ecf20Sopenharmony_ci	just usbfs) state.
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ciAsynchronous I/O Support
6098c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ciAs mentioned above, there are situations where it may be important to
6128c2ecf20Sopenharmony_ciinitiate concurrent operations from user mode code. This is particularly
6138c2ecf20Sopenharmony_ciimportant for periodic transfers (interrupt and isochronous), but it can
6148c2ecf20Sopenharmony_cibe used for other kinds of USB requests too. In such cases, the
6158c2ecf20Sopenharmony_ciasynchronous requests described here are essential. Rather than
6168c2ecf20Sopenharmony_cisubmitting one request and having the kernel block until it completes,
6178c2ecf20Sopenharmony_cithe blocking is separate.
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ciThese requests are packaged into a structure that resembles the URB used
6208c2ecf20Sopenharmony_ciby kernel device drivers. (No POSIX Async I/O support here, sorry.) It
6218c2ecf20Sopenharmony_ciidentifies the endpoint type (``USBDEVFS_URB_TYPE_*``), endpoint
6228c2ecf20Sopenharmony_ci(number, masked with USB_DIR_IN as appropriate), buffer and length,
6238c2ecf20Sopenharmony_ciand a user "context" value serving to uniquely identify each request.
6248c2ecf20Sopenharmony_ci(It's usually a pointer to per-request data.) Flags can modify requests
6258c2ecf20Sopenharmony_ci(not as many as supported for kernel drivers).
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ciEach request can specify a realtime signal number (between SIGRTMIN and
6288c2ecf20Sopenharmony_ciSIGRTMAX, inclusive) to request a signal be sent when the request
6298c2ecf20Sopenharmony_cicompletes.
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ciWhen usbfs returns these urbs, the status value is updated, and the
6328c2ecf20Sopenharmony_cibuffer may have been modified. Except for isochronous transfers, the
6338c2ecf20Sopenharmony_ciactual_length is updated to say how many bytes were transferred; if the
6348c2ecf20Sopenharmony_ciUSBDEVFS_URB_DISABLE_SPD flag is set ("short packets are not OK"), if
6358c2ecf20Sopenharmony_cifewer bytes were read than were requested then you get an error report::
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci    struct usbdevfs_iso_packet_desc {
6388c2ecf20Sopenharmony_ci	    unsigned int                     length;
6398c2ecf20Sopenharmony_ci	    unsigned int                     actual_length;
6408c2ecf20Sopenharmony_ci	    unsigned int                     status;
6418c2ecf20Sopenharmony_ci    };
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci    struct usbdevfs_urb {
6448c2ecf20Sopenharmony_ci	    unsigned char                    type;
6458c2ecf20Sopenharmony_ci	    unsigned char                    endpoint;
6468c2ecf20Sopenharmony_ci	    int                              status;
6478c2ecf20Sopenharmony_ci	    unsigned int                     flags;
6488c2ecf20Sopenharmony_ci	    void                             *buffer;
6498c2ecf20Sopenharmony_ci	    int                              buffer_length;
6508c2ecf20Sopenharmony_ci	    int                              actual_length;
6518c2ecf20Sopenharmony_ci	    int                              start_frame;
6528c2ecf20Sopenharmony_ci	    int                              number_of_packets;
6538c2ecf20Sopenharmony_ci	    int                              error_count;
6548c2ecf20Sopenharmony_ci	    unsigned int                     signr;
6558c2ecf20Sopenharmony_ci	    void                             *usercontext;
6568c2ecf20Sopenharmony_ci	    struct usbdevfs_iso_packet_desc  iso_frame_desc[];
6578c2ecf20Sopenharmony_ci    };
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ciFor these asynchronous requests, the file modification time reflects
6608c2ecf20Sopenharmony_ciwhen the request was initiated. This contrasts with their use with the
6618c2ecf20Sopenharmony_cisynchronous requests, where it reflects when requests complete.
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ciUSBDEVFS_DISCARDURB
6648c2ecf20Sopenharmony_ci    *TBS* File modification time is not updated by this request.
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ciUSBDEVFS_DISCSIGNAL
6678c2ecf20Sopenharmony_ci    *TBS* File modification time is not updated by this request.
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ciUSBDEVFS_REAPURB
6708c2ecf20Sopenharmony_ci    *TBS* File modification time is not updated by this request.
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ciUSBDEVFS_REAPURBNDELAY
6738c2ecf20Sopenharmony_ci    *TBS* File modification time is not updated by this request.
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ciUSBDEVFS_SUBMITURB
6768c2ecf20Sopenharmony_ci    *TBS*
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ciThe USB devices
6798c2ecf20Sopenharmony_ci===============
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ciThe USB devices are now exported via debugfs:
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci-  ``/sys/kernel/debug/usb/devices`` ... a text file showing each of the USB
6848c2ecf20Sopenharmony_ci   devices on known to the kernel, and their configuration descriptors.
6858c2ecf20Sopenharmony_ci   You can also poll() this to learn about new devices.
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci/sys/kernel/debug/usb/devices
6888c2ecf20Sopenharmony_ci-----------------------------
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ciThis file is handy for status viewing tools in user mode, which can scan
6918c2ecf20Sopenharmony_cithe text format and ignore most of it. More detailed device status
6928c2ecf20Sopenharmony_ci(including class and vendor status) is available from device-specific
6938c2ecf20Sopenharmony_cifiles. For information about the current format of this file, see below.
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ciThis file, in combination with the poll() system call, can also be used
6968c2ecf20Sopenharmony_cito detect when devices are added or removed::
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci    int fd;
6998c2ecf20Sopenharmony_ci    struct pollfd pfd;
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci    fd = open("/sys/kernel/debug/usb/devices", O_RDONLY);
7028c2ecf20Sopenharmony_ci    pfd = { fd, POLLIN, 0 };
7038c2ecf20Sopenharmony_ci    for (;;) {
7048c2ecf20Sopenharmony_ci	/* The first time through, this call will return immediately. */
7058c2ecf20Sopenharmony_ci	poll(&pfd, 1, -1);
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci	/* To see what's changed, compare the file's previous and current
7088c2ecf20Sopenharmony_ci	   contents or scan the filesystem.  (Scanning is more precise.) */
7098c2ecf20Sopenharmony_ci    }
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ciNote that this behavior is intended to be used for informational and
7128c2ecf20Sopenharmony_cidebug purposes. It would be more appropriate to use programs such as
7138c2ecf20Sopenharmony_ciudev or HAL to initialize a device or start a user-mode helper program,
7148c2ecf20Sopenharmony_cifor instance.
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_ciIn this file, each device's output has multiple lines of ASCII output.
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ciI made it ASCII instead of binary on purpose, so that someone
7198c2ecf20Sopenharmony_cican obtain some useful data from it without the use of an
7208c2ecf20Sopenharmony_ciauxiliary program.  However, with an auxiliary program, the numbers
7218c2ecf20Sopenharmony_ciin the first 4 columns of each ``T:`` line (topology info:
7228c2ecf20Sopenharmony_ciLev, Prnt, Port, Cnt) can be used to build a USB topology diagram.
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ciEach line is tagged with a one-character ID for that line::
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci	T = Topology (etc.)
7278c2ecf20Sopenharmony_ci	B = Bandwidth (applies only to USB host controllers, which are
7288c2ecf20Sopenharmony_ci	virtualized as root hubs)
7298c2ecf20Sopenharmony_ci	D = Device descriptor info.
7308c2ecf20Sopenharmony_ci	P = Product ID info. (from Device descriptor, but they won't fit
7318c2ecf20Sopenharmony_ci	together on one line)
7328c2ecf20Sopenharmony_ci	S = String descriptors.
7338c2ecf20Sopenharmony_ci	C = Configuration descriptor info. (* = active configuration)
7348c2ecf20Sopenharmony_ci	I = Interface descriptor info.
7358c2ecf20Sopenharmony_ci	E = Endpoint descriptor info.
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci/sys/kernel/debug/usb/devices output format
7388c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ciLegend::
7418c2ecf20Sopenharmony_ci  d = decimal number (may have leading spaces or 0's)
7428c2ecf20Sopenharmony_ci  x = hexadecimal number (may have leading spaces or 0's)
7438c2ecf20Sopenharmony_ci  s = string
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ciTopology info
7488c2ecf20Sopenharmony_ci^^^^^^^^^^^^^
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci::
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci	T:  Bus=dd Lev=dd Prnt=dd Port=dd Cnt=dd Dev#=ddd Spd=dddd MxCh=dd
7538c2ecf20Sopenharmony_ci	|   |      |      |       |       |      |        |        |__MaxChildren
7548c2ecf20Sopenharmony_ci	|   |      |      |       |       |      |        |__Device Speed in Mbps
7558c2ecf20Sopenharmony_ci	|   |      |      |       |       |      |__DeviceNumber
7568c2ecf20Sopenharmony_ci	|   |      |      |       |       |__Count of devices at this level
7578c2ecf20Sopenharmony_ci	|   |      |      |       |__Connector/Port on Parent for this device
7588c2ecf20Sopenharmony_ci	|   |      |      |__Parent DeviceNumber
7598c2ecf20Sopenharmony_ci	|   |      |__Level in topology for this bus
7608c2ecf20Sopenharmony_ci	|   |__Bus number
7618c2ecf20Sopenharmony_ci	|__Topology info tag
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ciSpeed may be:
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ci	======= ======================================================
7668c2ecf20Sopenharmony_ci	1.5	Mbit/s for low speed USB
7678c2ecf20Sopenharmony_ci	12	Mbit/s for full speed USB
7688c2ecf20Sopenharmony_ci	480	Mbit/s for high speed USB (added for USB 2.0);
7698c2ecf20Sopenharmony_ci		also used for Wireless USB, which has no fixed speed
7708c2ecf20Sopenharmony_ci	5000	Mbit/s for SuperSpeed USB (added for USB 3.0)
7718c2ecf20Sopenharmony_ci	======= ======================================================
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ciFor reasons lost in the mists of time, the Port number is always
7748c2ecf20Sopenharmony_citoo low by 1.  For example, a device plugged into port 4 will
7758c2ecf20Sopenharmony_cishow up with ``Port=03``.
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ciBandwidth info
7788c2ecf20Sopenharmony_ci^^^^^^^^^^^^^^
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci::
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci	B:  Alloc=ddd/ddd us (xx%), #Int=ddd, #Iso=ddd
7838c2ecf20Sopenharmony_ci	|   |                       |         |__Number of isochronous requests
7848c2ecf20Sopenharmony_ci	|   |                       |__Number of interrupt requests
7858c2ecf20Sopenharmony_ci	|   |__Total Bandwidth allocated to this bus
7868c2ecf20Sopenharmony_ci	|__Bandwidth info tag
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ciBandwidth allocation is an approximation of how much of one frame
7898c2ecf20Sopenharmony_ci(millisecond) is in use.  It reflects only periodic transfers, which
7908c2ecf20Sopenharmony_ciare the only transfers that reserve bandwidth.  Control and bulk
7918c2ecf20Sopenharmony_citransfers use all other bandwidth, including reserved bandwidth that
7928c2ecf20Sopenharmony_ciis not used for transfers (such as for short packets).
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ciThe percentage is how much of the "reserved" bandwidth is scheduled by
7958c2ecf20Sopenharmony_cithose transfers.  For a low or full speed bus (loosely, "USB 1.1"),
7968c2ecf20Sopenharmony_ci90% of the bus bandwidth is reserved.  For a high speed bus (loosely,
7978c2ecf20Sopenharmony_ci"USB 2.0") 80% is reserved.
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ciDevice descriptor info & Product ID info
8018c2ecf20Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci::
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_ci	D:  Ver=x.xx Cls=xx(s) Sub=xx Prot=xx MxPS=dd #Cfgs=dd
8068c2ecf20Sopenharmony_ci	P:  Vendor=xxxx ProdID=xxxx Rev=xx.xx
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ciwhere::
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci	D:  Ver=x.xx Cls=xx(sssss) Sub=xx Prot=xx MxPS=dd #Cfgs=dd
8118c2ecf20Sopenharmony_ci	|   |        |             |      |       |       |__NumberConfigurations
8128c2ecf20Sopenharmony_ci	|   |        |             |      |       |__MaxPacketSize of Default Endpoint
8138c2ecf20Sopenharmony_ci	|   |        |             |      |__DeviceProtocol
8148c2ecf20Sopenharmony_ci	|   |        |             |__DeviceSubClass
8158c2ecf20Sopenharmony_ci	|   |        |__DeviceClass
8168c2ecf20Sopenharmony_ci	|   |__Device USB version
8178c2ecf20Sopenharmony_ci	|__Device info tag #1
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ciwhere::
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	P:  Vendor=xxxx ProdID=xxxx Rev=xx.xx
8228c2ecf20Sopenharmony_ci	|   |           |           |__Product revision number
8238c2ecf20Sopenharmony_ci	|   |           |__Product ID code
8248c2ecf20Sopenharmony_ci	|   |__Vendor ID code
8258c2ecf20Sopenharmony_ci	|__Device info tag #2
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_ciString descriptor info
8298c2ecf20Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^
8308c2ecf20Sopenharmony_ci::
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_ci	S:  Manufacturer=ssss
8338c2ecf20Sopenharmony_ci	|   |__Manufacturer of this device as read from the device.
8348c2ecf20Sopenharmony_ci	|      For USB host controller drivers (virtual root hubs) this may
8358c2ecf20Sopenharmony_ci	|      be omitted, or (for newer drivers) will identify the kernel
8368c2ecf20Sopenharmony_ci	|      version and the driver which provides this hub emulation.
8378c2ecf20Sopenharmony_ci	|__String info tag
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci	S:  Product=ssss
8408c2ecf20Sopenharmony_ci	|   |__Product description of this device as read from the device.
8418c2ecf20Sopenharmony_ci	|      For older USB host controller drivers (virtual root hubs) this
8428c2ecf20Sopenharmony_ci	|      indicates the driver; for newer ones, it's a product (and vendor)
8438c2ecf20Sopenharmony_ci	|      description that often comes from the kernel's PCI ID database.
8448c2ecf20Sopenharmony_ci	|__String info tag
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci	S:  SerialNumber=ssss
8478c2ecf20Sopenharmony_ci	|   |__Serial Number of this device as read from the device.
8488c2ecf20Sopenharmony_ci	|      For USB host controller drivers (virtual root hubs) this is
8498c2ecf20Sopenharmony_ci	|      some unique ID, normally a bus ID (address or slot name) that
8508c2ecf20Sopenharmony_ci	|      can't be shared with any other device.
8518c2ecf20Sopenharmony_ci	|__String info tag
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_ciConfiguration descriptor info
8568c2ecf20Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8578c2ecf20Sopenharmony_ci::
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_ci	C:* #Ifs=dd Cfg#=dd Atr=xx MPwr=dddmA
8608c2ecf20Sopenharmony_ci	| | |       |       |      |__MaxPower in mA
8618c2ecf20Sopenharmony_ci	| | |       |       |__Attributes
8628c2ecf20Sopenharmony_ci	| | |       |__ConfiguratioNumber
8638c2ecf20Sopenharmony_ci	| | |__NumberOfInterfaces
8648c2ecf20Sopenharmony_ci	| |__ "*" indicates the active configuration (others are " ")
8658c2ecf20Sopenharmony_ci	|__Config info tag
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_ciUSB devices may have multiple configurations, each of which act
8688c2ecf20Sopenharmony_cirather differently.  For example, a bus-powered configuration
8698c2ecf20Sopenharmony_cimight be much less capable than one that is self-powered.  Only
8708c2ecf20Sopenharmony_cione device configuration can be active at a time; most devices
8718c2ecf20Sopenharmony_cihave only one configuration.
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_ciEach configuration consists of one or more interfaces.  Each
8748c2ecf20Sopenharmony_ciinterface serves a distinct "function", which is typically bound
8758c2ecf20Sopenharmony_cito a different USB device driver.  One common example is a USB
8768c2ecf20Sopenharmony_cispeaker with an audio interface for playback, and a HID interface
8778c2ecf20Sopenharmony_cifor use with software volume control.
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ciInterface descriptor info (can be multiple per Config)
8808c2ecf20Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8818c2ecf20Sopenharmony_ci::
8828c2ecf20Sopenharmony_ci
8838c2ecf20Sopenharmony_ci	I:* If#=dd Alt=dd #EPs=dd Cls=xx(sssss) Sub=xx Prot=xx Driver=ssss
8848c2ecf20Sopenharmony_ci	| | |      |      |       |             |      |       |__Driver name
8858c2ecf20Sopenharmony_ci	| | |      |      |       |             |      |          or "(none)"
8868c2ecf20Sopenharmony_ci	| | |      |      |       |             |      |__InterfaceProtocol
8878c2ecf20Sopenharmony_ci	| | |      |      |       |             |__InterfaceSubClass
8888c2ecf20Sopenharmony_ci	| | |      |      |       |__InterfaceClass
8898c2ecf20Sopenharmony_ci	| | |      |      |__NumberOfEndpoints
8908c2ecf20Sopenharmony_ci	| | |      |__AlternateSettingNumber
8918c2ecf20Sopenharmony_ci	| | |__InterfaceNumber
8928c2ecf20Sopenharmony_ci	| |__ "*" indicates the active altsetting (others are " ")
8938c2ecf20Sopenharmony_ci	|__Interface info tag
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ciA given interface may have one or more "alternate" settings.
8968c2ecf20Sopenharmony_ciFor example, default settings may not use more than a small
8978c2ecf20Sopenharmony_ciamount of periodic bandwidth.  To use significant fractions
8988c2ecf20Sopenharmony_ciof bus bandwidth, drivers must select a non-default altsetting.
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ciOnly one setting for an interface may be active at a time, and
9018c2ecf20Sopenharmony_cionly one driver may bind to an interface at a time.  Most devices
9028c2ecf20Sopenharmony_cihave only one alternate setting per interface.
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ciEndpoint descriptor info (can be multiple per Interface)
9068c2ecf20Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_ci::
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_ci	E:  Ad=xx(s) Atr=xx(ssss) MxPS=dddd Ivl=dddss
9118c2ecf20Sopenharmony_ci	|   |        |            |         |__Interval (max) between transfers
9128c2ecf20Sopenharmony_ci	|   |        |            |__EndpointMaxPacketSize
9138c2ecf20Sopenharmony_ci	|   |        |__Attributes(EndpointType)
9148c2ecf20Sopenharmony_ci	|   |__EndpointAddress(I=In,O=Out)
9158c2ecf20Sopenharmony_ci	|__Endpoint info tag
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_ciThe interval is nonzero for all periodic (interrupt or isochronous)
9188c2ecf20Sopenharmony_ciendpoints.  For high speed endpoints the transfer interval may be
9198c2ecf20Sopenharmony_cimeasured in microseconds rather than milliseconds.
9208c2ecf20Sopenharmony_ci
9218c2ecf20Sopenharmony_ciFor high speed periodic endpoints, the ``EndpointMaxPacketSize`` reflects
9228c2ecf20Sopenharmony_cithe per-microframe data transfer size.  For "high bandwidth"
9238c2ecf20Sopenharmony_ciendpoints, that can reflect two or three packets (for up to
9248c2ecf20Sopenharmony_ci3KBytes every 125 usec) per endpoint.
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_ciWith the Linux-USB stack, periodic bandwidth reservations use the
9278c2ecf20Sopenharmony_citransfer intervals and sizes provided by URBs, which can be less
9288c2ecf20Sopenharmony_cithan those found in endpoint descriptor.
9298c2ecf20Sopenharmony_ci
9308c2ecf20Sopenharmony_ciUsage examples
9318c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~
9328c2ecf20Sopenharmony_ci
9338c2ecf20Sopenharmony_ciIf a user or script is interested only in Topology info, for
9348c2ecf20Sopenharmony_ciexample, use something like ``grep ^T: /sys/kernel/debug/usb/devices``
9358c2ecf20Sopenharmony_cifor only the Topology lines.  A command like
9368c2ecf20Sopenharmony_ci``grep -i ^[tdp]: /sys/kernel/debug/usb/devices`` can be used to list
9378c2ecf20Sopenharmony_cionly the lines that begin with the characters in square brackets,
9388c2ecf20Sopenharmony_ciwhere the valid characters are TDPCIE.  With a slightly more able
9398c2ecf20Sopenharmony_ciscript, it can display any selected lines (for example, only T, D,
9408c2ecf20Sopenharmony_ciand P lines) and change their output format.  (The ``procusb``
9418c2ecf20Sopenharmony_ciPerl script is the beginning of this idea.  It will list only
9428c2ecf20Sopenharmony_ciselected lines [selected from TBDPSCIE] or "All" lines from
9438c2ecf20Sopenharmony_ci``/sys/kernel/debug/usb/devices``.)
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ciThe Topology lines can be used to generate a graphic/pictorial
9468c2ecf20Sopenharmony_ciof the USB devices on a system's root hub.  (See more below
9478c2ecf20Sopenharmony_cion how to do this.)
9488c2ecf20Sopenharmony_ci
9498c2ecf20Sopenharmony_ciThe Interface lines can be used to determine what driver is
9508c2ecf20Sopenharmony_cibeing used for each device, and which altsetting it activated.
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ciThe Configuration lines could be used to list maximum power
9538c2ecf20Sopenharmony_ci(in milliamps) that a system's USB devices are using.
9548c2ecf20Sopenharmony_ciFor example, ``grep ^C: /sys/kernel/debug/usb/devices``.
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_ciHere's an example, from a system which has a UHCI root hub,
9588c2ecf20Sopenharmony_cian external hub connected to the root hub, and a mouse and
9598c2ecf20Sopenharmony_cia serial converter connected to the external hub.
9608c2ecf20Sopenharmony_ci
9618c2ecf20Sopenharmony_ci::
9628c2ecf20Sopenharmony_ci
9638c2ecf20Sopenharmony_ci	T:  Bus=00 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#=  1 Spd=12   MxCh= 2
9648c2ecf20Sopenharmony_ci	B:  Alloc= 28/900 us ( 3%), #Int=  2, #Iso=  0
9658c2ecf20Sopenharmony_ci	D:  Ver= 1.00 Cls=09(hub  ) Sub=00 Prot=00 MxPS= 8 #Cfgs=  1
9668c2ecf20Sopenharmony_ci	P:  Vendor=0000 ProdID=0000 Rev= 0.00
9678c2ecf20Sopenharmony_ci	S:  Product=USB UHCI Root Hub
9688c2ecf20Sopenharmony_ci	S:  SerialNumber=dce0
9698c2ecf20Sopenharmony_ci	C:* #Ifs= 1 Cfg#= 1 Atr=40 MxPwr=  0mA
9708c2ecf20Sopenharmony_ci	I:  If#= 0 Alt= 0 #EPs= 1 Cls=09(hub  ) Sub=00 Prot=00 Driver=hub
9718c2ecf20Sopenharmony_ci	E:  Ad=81(I) Atr=03(Int.) MxPS=   8 Ivl=255ms
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_ci	T:  Bus=00 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#=  2 Spd=12   MxCh= 4
9748c2ecf20Sopenharmony_ci	D:  Ver= 1.00 Cls=09(hub  ) Sub=00 Prot=00 MxPS= 8 #Cfgs=  1
9758c2ecf20Sopenharmony_ci	P:  Vendor=0451 ProdID=1446 Rev= 1.00
9768c2ecf20Sopenharmony_ci	C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr=100mA
9778c2ecf20Sopenharmony_ci	I:  If#= 0 Alt= 0 #EPs= 1 Cls=09(hub  ) Sub=00 Prot=00 Driver=hub
9788c2ecf20Sopenharmony_ci	E:  Ad=81(I) Atr=03(Int.) MxPS=   1 Ivl=255ms
9798c2ecf20Sopenharmony_ci
9808c2ecf20Sopenharmony_ci	T:  Bus=00 Lev=02 Prnt=02 Port=00 Cnt=01 Dev#=  3 Spd=1.5  MxCh= 0
9818c2ecf20Sopenharmony_ci	D:  Ver= 1.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 8 #Cfgs=  1
9828c2ecf20Sopenharmony_ci	P:  Vendor=04b4 ProdID=0001 Rev= 0.00
9838c2ecf20Sopenharmony_ci	C:* #Ifs= 1 Cfg#= 1 Atr=80 MxPwr=100mA
9848c2ecf20Sopenharmony_ci	I:  If#= 0 Alt= 0 #EPs= 1 Cls=03(HID  ) Sub=01 Prot=02 Driver=mouse
9858c2ecf20Sopenharmony_ci	E:  Ad=81(I) Atr=03(Int.) MxPS=   3 Ivl= 10ms
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ci	T:  Bus=00 Lev=02 Prnt=02 Port=02 Cnt=02 Dev#=  4 Spd=12   MxCh= 0
9888c2ecf20Sopenharmony_ci	D:  Ver= 1.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 8 #Cfgs=  1
9898c2ecf20Sopenharmony_ci	P:  Vendor=0565 ProdID=0001 Rev= 1.08
9908c2ecf20Sopenharmony_ci	S:  Manufacturer=Peracom Networks, Inc.
9918c2ecf20Sopenharmony_ci	S:  Product=Peracom USB to Serial Converter
9928c2ecf20Sopenharmony_ci	C:* #Ifs= 1 Cfg#= 1 Atr=a0 MxPwr=100mA
9938c2ecf20Sopenharmony_ci	I:  If#= 0 Alt= 0 #EPs= 3 Cls=00(>ifc ) Sub=00 Prot=00 Driver=serial
9948c2ecf20Sopenharmony_ci	E:  Ad=81(I) Atr=02(Bulk) MxPS=  64 Ivl= 16ms
9958c2ecf20Sopenharmony_ci	E:  Ad=01(O) Atr=02(Bulk) MxPS=  16 Ivl= 16ms
9968c2ecf20Sopenharmony_ci	E:  Ad=82(I) Atr=03(Int.) MxPS=   8 Ivl=  8ms
9978c2ecf20Sopenharmony_ci
9988c2ecf20Sopenharmony_ci
9998c2ecf20Sopenharmony_ciSelecting only the ``T:`` and ``I:`` lines from this (for example, by using
10008c2ecf20Sopenharmony_ci``procusb ti``), we have
10018c2ecf20Sopenharmony_ci
10028c2ecf20Sopenharmony_ci::
10038c2ecf20Sopenharmony_ci
10048c2ecf20Sopenharmony_ci	T:  Bus=00 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#=  1 Spd=12   MxCh= 2
10058c2ecf20Sopenharmony_ci	T:  Bus=00 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#=  2 Spd=12   MxCh= 4
10068c2ecf20Sopenharmony_ci	I:  If#= 0 Alt= 0 #EPs= 1 Cls=09(hub  ) Sub=00 Prot=00 Driver=hub
10078c2ecf20Sopenharmony_ci	T:  Bus=00 Lev=02 Prnt=02 Port=00 Cnt=01 Dev#=  3 Spd=1.5  MxCh= 0
10088c2ecf20Sopenharmony_ci	I:  If#= 0 Alt= 0 #EPs= 1 Cls=03(HID  ) Sub=01 Prot=02 Driver=mouse
10098c2ecf20Sopenharmony_ci	T:  Bus=00 Lev=02 Prnt=02 Port=02 Cnt=02 Dev#=  4 Spd=12   MxCh= 0
10108c2ecf20Sopenharmony_ci	I:  If#= 0 Alt= 0 #EPs= 3 Cls=00(>ifc ) Sub=00 Prot=00 Driver=serial
10118c2ecf20Sopenharmony_ci
10128c2ecf20Sopenharmony_ci
10138c2ecf20Sopenharmony_ciPhysically this looks like (or could be converted to)::
10148c2ecf20Sopenharmony_ci
10158c2ecf20Sopenharmony_ci                      +------------------+
10168c2ecf20Sopenharmony_ci                      |  PC/root_hub (12)|   Dev# = 1
10178c2ecf20Sopenharmony_ci                      +------------------+   (nn) is Mbps.
10188c2ecf20Sopenharmony_ci    Level 0           |  CN.0   |  CN.1  |   [CN = connector/port #]
10198c2ecf20Sopenharmony_ci                      +------------------+
10208c2ecf20Sopenharmony_ci                          /
10218c2ecf20Sopenharmony_ci                         /
10228c2ecf20Sopenharmony_ci            +-----------------------+
10238c2ecf20Sopenharmony_ci  Level 1   | Dev#2: 4-port hub (12)|
10248c2ecf20Sopenharmony_ci            +-----------------------+
10258c2ecf20Sopenharmony_ci            |CN.0 |CN.1 |CN.2 |CN.3 |
10268c2ecf20Sopenharmony_ci            +-----------------------+
10278c2ecf20Sopenharmony_ci                \           \____________________
10288c2ecf20Sopenharmony_ci                 \_____                          \
10298c2ecf20Sopenharmony_ci                       \                          \
10308c2ecf20Sopenharmony_ci               +--------------------+      +--------------------+
10318c2ecf20Sopenharmony_ci  Level 2      | Dev# 3: mouse (1.5)|      | Dev# 4: serial (12)|
10328c2ecf20Sopenharmony_ci               +--------------------+      +--------------------+
10338c2ecf20Sopenharmony_ci
10348c2ecf20Sopenharmony_ci
10358c2ecf20Sopenharmony_ci
10368c2ecf20Sopenharmony_ciOr, in a more tree-like structure (ports [Connectors] without
10378c2ecf20Sopenharmony_ciconnections could be omitted)::
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_ci	PC:  Dev# 1, root hub, 2 ports, 12 Mbps
10408c2ecf20Sopenharmony_ci	|_ CN.0:  Dev# 2, hub, 4 ports, 12 Mbps
10418c2ecf20Sopenharmony_ci	     |_ CN.0:  Dev #3, mouse, 1.5 Mbps
10428c2ecf20Sopenharmony_ci	     |_ CN.1:
10438c2ecf20Sopenharmony_ci	     |_ CN.2:  Dev #4, serial, 12 Mbps
10448c2ecf20Sopenharmony_ci	     |_ CN.3:
10458c2ecf20Sopenharmony_ci	|_ CN.1:
1046