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