Lines Matching +full:one +full:- +full:to +full:- +full:one
1 .. _usb-hostside-api:
4 The Linux-USB Host Side API
7 Introduction to USB on Linux
10 A Universal Serial Bus (USB) is used to connect a host, such as a PC or
11 workstation, to a number of peripheral devices. USB uses a tree
18 That master/slave asymmetry was designed-in for a number of reasons, one
19 being ease of use. It is not physically possible to mistake upstream and
21 peripheral). Also, the host software doesn't need to deal with
22 distributed auto-configuration since the pre-designated master node
25 Kernel developers added USB support to Linux early in the 2.2 kernel
37 USB Host-Side API Model
40 Host-side drivers for USB devices talk to the "usbcore" APIs. There are
41 two. One is intended for *general-purpose* drivers (exposed through
49 - USB supports four kinds of data transfers (control, bulk, interrupt,
52 scheduled to provide guaranteed bandwidth.
54 - The device description model includes one or more "configurations"
55 per device, only one of which is active at a time. Devices are supposed
56 to be capable of operating at lower than their top
60 - From USB 3.0 on configurations have one or more "functions", which
64 - Configurations or functions have one or more "interfaces", each of which may have
66 specifications, or may be specific to a vendor or device.
68 USB device drivers actually bind to interfaces, not devices. Think of
71 with only one function, one configuration, one interface, and one alternate
74 - Interfaces have one or more "endpoints", each of which supports one
76 in". The entire configuration may have up to sixteen endpoints in
79 - Data transfer on USB is packetized; each endpoint has a maximum
84 - The Linux USB API supports synchronous calls for control and bulk
89 Accordingly, the USB Core API exposed to device drivers covers quite a
90 lot of territory. You'll probably need to consult the USB 3.0
94 The only host-side drivers that actually touch hardware (reading/writing
101 faults (including software-induced ones like unlinking an URB) isn't yet
104 controller driver, to make sure drivers don't have bugs of their own as
105 well as to make sure they aren't relying on some HCD-specific behavior.
109 USB-Standard Types
117 .. kernel-doc:: include/linux/usb/ch9.h
122 Host-Side Data Types and Macros
125 The host side API exposes several layers to drivers, some of which are
127 drivers and devices, and support passing buffers through usbcore to some
130 .. kernel-doc:: include/linux/usb.h
136 There are two basic I/O models in the USB API. The most elemental one is
142 per-packet fault reports). Built on top of that is synchronous API
143 support, where a driver calls a routine that allocates one or more URBs,
145 wrappers for single-buffer control and bulk transfers (which are awkward
146 to use in some driver disconnect scenarios), and for scatterlist based
149 USB drivers need to provide buffers that can be used for DMA, although
150 they don't necessarily need to provide the DMA mapping themselves. There
151 are APIs to use used when allocating DMA buffers, which can prevent use
152 of bounce buffers on some systems. In some cases, drivers may be able to
153 rely on 64bit DMA to eliminate another kind of bounce buffer.
155 .. kernel-doc:: drivers/usb/core/urb.c
158 .. kernel-doc:: drivers/usb/core/message.c
161 .. kernel-doc:: drivers/usb/core/file.c
164 .. kernel-doc:: drivers/usb/core/driver.c
167 .. kernel-doc:: drivers/usb/core/usb.c
170 .. kernel-doc:: drivers/usb/core/hub.c
178 was one of the first interfaces, designed by Intel and also used by VIA;
179 it doesn't do much in hardware. OHCI was designed later, to have the
184 continues to shift support for functionality into hardware.
187 based controllers (and a few non-PCI based ones) use one of those
189 also a simulator and a virtual host controller to pipe USB over the network.
191 The same basic APIs are available to drivers for all those controllers.
196 that lets HCDs share common code, to shrink driver size and
197 significantly reduce hcd-specific behaviors.
199 .. kernel-doc:: drivers/usb/core/hcd.c
202 .. kernel-doc:: drivers/usb/core/hcd-pci.c
205 .. kernel-doc:: drivers/usb/core/buffer.c
212 to avoid writing new kernel code for your USB driver. User mode device
217 - `libusb <http://libusb.sourceforge.net>`__ for C/C++, and
218 - `jUSB <http://jUSB.sourceforge.net>`__ for Java.
222 at http://www.linux-usb.org/
226 - They were used to be implemented via *usbfs*, but this is not part of
229 - This particular documentation is incomplete, especially with respect
230 to the asynchronous mode. As of kernel 2.5.66 the code and this
231 (new) documentation need to be cross-reviewed.
234 -----------------------------
238 - ``/dev/bus/usb/BBB/DDD`` ... magic files exposing the each device's
240 making device requests, including I/O to devices. (Purely for access
245 paths are not "stable" identifiers; expect them to change even if you
246 always leave the devices plugged in to the same hub port. *Don't even
248 identifiers are available, for user mode applications that want to use
250 example you can be sure that you told the right UPS to power down its
254 --------------------
256 Use these files in one of these basic ways:
258 - *They can be read,* producing first the device descriptor (18 bytes) and
260 for details about those binary data formats. You'll need to convert most
261 multibyte values from little endian format to your native host byte
263 the BCD-encoded fields, and the vendor and product IDs) will be
268 - *Perform USB operations* using *ioctl()* requests to make endpoint I/O
271 access permissions. Only one ioctl request can be made on one of these
273 an endpoint from one thread, you won't be able to write to a different
278 Each connected USB device has one file. The ``BBB`` indicates the bus
281 you can't rely on them for stable access to devices. For example,
282 it's relatively common for devices to re-enumerate while they are
289 configuration of the device. Multi-byte fields in the device descriptor
290 are converted to host endianness by the kernel. The configuration
297 These files may also be used to write user-level drivers for the USB
299 read its descriptors to make sure it's the device you expect, and then
300 bind to an interface (or perhaps several) using an ioctl call. You
301 would issue more ioctls to the device to communicate to it using
305 for how to access devices through those files.
309 grant read/write permissions to other users by using ``chmod``. Also,
314 -------------------------------
316 Such a driver first needs to find a device file for a device it knows
317 how to handle. Maybe it was told about it because a ``/sbin/hotplug``
318 event handling agent chose that driver to handle the new device. Or
322 knows how to handle. It might just reject everything except a particular
325 Never assume there will only be one such device on the system at a time!
326 If your code can't handle more than one device at a time, at least
327 detect when there's more than one, and have your users choose which
328 device to use.
330 Once your user mode driver knows what device to use, it interacts with
331 it in either of two styles. The simple style is to make only control
333 (An example might be software using vendor-specific control requests for
337 More likely, you need a more complex style driver: one using non-control
339 interface. *Bulk* transfers are easiest to use, but only their sibling
342 is reserved. Such "periodic" transfers are awkward to use through usbfs,
344 can also be used in a synchronous "one shot" style.
346 Your user-mode driver should never need to worry about cleaning up
351 --------------------
353 To use these ioctls, you need to include the following headers in your
365 the modification time on the usbfs file to which they are applied
368 :ref:`usb-error-codes`).
370 Each of these files multiplexes access to several I/O streams, one per
371 endpoint. Each device has one control endpoint (endpoint zero) which
373 hub_wq (in the kernel) setting a device-wide *configuration* that
384 They mostly relate to device management and status. These are all
388 This is used to force usbfs to claim a specific interface, which has
393 Note that if your driver doesn't claim an interface before trying to
394 use one of its endpoints, and no other driver has bound to it, then
402 Says whether the device is lowspeed. The ioctl parameter points to a
418 Returns the name of the kernel driver bound to a given interface (a
419 string). Parameter is a pointer to this structure, which is
430 Passes a request from userspace through to a kernel driver that has
440 * 'request' becomes the driver->ioctl() 'code' parameter.
442 * is copied to or from the driver->ioctl() 'buf' parameter.
458 This request lets kernel drivers talk to user mode code through
460 block special device. It's also been used to do things like ask
461 devices what device special file should be used. Two pre-defined
462 ioctls are used to disconnect and reconnect kernel drivers, so that
467 This is used to release the claim usbfs made on interface, either
475 *No security check is made to ensure that the task which made
476 the claim is the one which is releasing it. This means that user
480 Resets the data toggle value for an endpoint (bulk or interrupt) to
481 DATA0. The ioctl parameter is an integer endpoint number (1 to 15,
483 if the device's endpoint sends data to the host.
490 need to completely handshake with the device, using a request
494 This is used to relinquish the ability to do certain operations
495 which are considered to be privileged on a usbfs file descriptor.
499 of interfaces the user is allowed to claim on this file descriptor.
500 You may issue this ioctl more than one time to narrow said mask.
507 error. In most cases this is the simplest way to use usbfs, although as
508 noted above it does prevent performing I/O to more than one endpoint at
512 Issues a bulk read or write request to the device. The ioctl
513 parameter is a pointer to this structure::
522 The ``ep`` value identifies a bulk endpoint number (1 to 15, as
524 referring to an endpoint which sends data to the host from the
526 kernels support requests up to about 128KBytes. *FIXME say how read
532 is an integer endpoint number (1 to 15, as identified in an endpoint
533 descriptor), masked with USB_DIR_IN when referring to an endpoint
534 which sends data to the host from the device.
537 returning ``-EPIPE`` status to a data transfer request. Do not issue
542 Issues a control request to the device. The ioctl parameter points
543 to a structure like this::
556 SETUP packet to be sent to the device; see the USB 2.0 specification
560 the length of the data buffer, which is either written to the device
563 At this writing, you can't transfer more than 4 KBytes of data to or
566 to say it's not OK to get a short read back from the device.
581 a pointer to a structure like this::
590 Those struct members are from some interface descriptor applying to
611 As mentioned above, there are situations where it may be important to
616 submitting one request and having the kernel block until it completes,
623 and a user "context" value serving to uniquely identify each request.
624 (It's usually a pointer to per-request data.) Flags can modify requests
628 SIGRTMAX, inclusive) to request a signal be sent when the request
633 actual_length is updated to say how many bytes were transferred; if the
683 - ``/sys/kernel/debug/usb/devices`` ... a text file showing each of the USB
684 devices on known to the kernel, and their configuration descriptors.
685 You can also poll() this to learn about new devices.
688 -----------------------------
692 (including class and vendor status) is available from device-specific
696 to detect when devices are added or removed::
705 poll(&pfd, 1, -1);
707 /* To see what's changed, compare the file's previous and current
711 Note that this behavior is intended to be used for informational and
712 debug purposes. It would be more appropriate to use programs such as
713 udev or HAL to initialize a device or start a user-mode helper program,
722 Lev, Prnt, Port, Cnt) can be used to build a USB topology diagram.
724 Each line is tagged with a one-character ID for that line::
727 B = Bandwidth (applies only to USB host controllers, which are
731 together on one line)
785 | |__Total Bandwidth allocated to this bus
788 Bandwidth allocation is an approximation of how much of one frame
868 rather differently. For example, a bus-powered configuration
869 might be much less capable than one that is self-powered. Only
870 one device configuration can be active at a time; most devices
871 have only one configuration.
873 Each configuration consists of one or more interfaces. Each
875 to a different USB device driver. One common example is a USB
895 A given interface may have one or more "alternate" settings.
897 amount of periodic bandwidth. To use significant fractions
898 of bus bandwidth, drivers must select a non-default altsetting.
900 Only one setting for an interface may be active at a time, and
901 only one driver may bind to an interface at a time. Most devices
902 have only one alternate setting per interface.
922 the per-microframe data transfer size. For "high bandwidth"
923 endpoints, that can reflect two or three packets (for up to
926 With the Linux-USB stack, periodic bandwidth reservations use the
936 ``grep -i ^[tdp]: /sys/kernel/debug/usb/devices`` can be used to list
945 The Topology lines can be used to generate a graphic/pictorial
947 on how to do this.)
949 The Interface lines can be used to determine what driver is
952 The Configuration lines could be used to list maximum power
958 an external hub connected to the root hub, and a mouse and
959 a serial converter connected to the external hub.
991 S: Product=Peracom USB to Serial Converter
1013 Physically this looks like (or could be converted to)::
1015 +------------------+
1017 +------------------+ (nn) is Mbps.
1019 +------------------+
1022 +-----------------------+
1023 Level 1 | Dev#2: 4-port hub (12)|
1024 +-----------------------+
1026 +-----------------------+
1030 +--------------------+ +--------------------+
1032 +--------------------+ +--------------------+
1036 Or, in a more tree-like structure (ports [Connectors] without