blob: a98f78c91ab64f97aa02bd73bc4935a165e496c5 [file] [log] [blame]
Mauro Carvalho Chehabe463c062017-04-05 10:23:10 -03001.. _usb-hostside-api:
2
Oliver Neukumdd0b38d2016-11-14 15:52:43 +01003===========================
4The Linux-USB Host Side API
5===========================
6
7Introduction to USB on Linux
8============================
9
10A Universal Serial Bus (USB) is used to connect a host, such as a PC or
11workstation, to a number of peripheral devices. USB uses a tree
12structure, with the host as the root (the system's master), hubs as
13interior nodes, and peripherals as leaves (and slaves). Modern PCs
14support several such trees of USB devices, usually
15a few USB 3.0 (5 GBit/s) or USB 3.1 (10 GBit/s) and some legacy
16USB 2.0 (480 MBit/s) busses just in case.
17
18That master/slave asymmetry was designed-in for a number of reasons, one
19being ease of use. It is not physically possible to mistake upstream and
20downstream or it does not matter with a type C plug (or they are built into the
21peripheral). Also, the host software doesn't need to deal with
22distributed auto-configuration since the pre-designated master node
23manages all that.
24
25Kernel developers added USB support to Linux early in the 2.2 kernel
26series and have been developing it further since then. Besides support
27for each new generation of USB, various host controllers gained support,
28new drivers for peripherals have been added and advanced features for latency
29measurement and improved power management introduced.
30
31Linux can run inside USB devices as well as on the hosts that control
32the devices. But USB device drivers running inside those peripherals
33don't do the same things as the ones running inside hosts, so they've
34been given a different name: *gadget drivers*. This document does not
35cover gadget drivers.
36
37USB Host-Side API Model
38=======================
39
40Host-side drivers for USB devices talk to the "usbcore" APIs. There are
41two. One is intended for *general-purpose* drivers (exposed through
42driver frameworks), and the other is for drivers that are *part of the
43core*. Such core drivers include the *hub* driver (which manages trees
44of USB devices) and several different kinds of *host controller
45drivers*, which control individual busses.
46
47The device model seen by USB drivers is relatively complex.
48
49- USB supports four kinds of data transfers (control, bulk, interrupt,
50 and isochronous). Two of them (control and bulk) use bandwidth as
51 it's available, while the other two (interrupt and isochronous) are
52 scheduled to provide guaranteed bandwidth.
53
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
57 speeds and may provide a BOS descriptor showing the lowest speed they
58 remain fully operational at.
59
60- From USB 3.0 on configurations have one or more "functions", which
61 provide a common functionality and are grouped together for purposes
62 of power management.
63
64- Configurations or functions have one or more "interfaces", each of which may have
65 "alternate settings". Interfaces may be standardized by USB "Class"
66 specifications, or may be specific to a vendor or device.
67
68 USB device drivers actually bind to interfaces, not devices. Think of
69 them as "interface drivers", though you may not see many devices
70 where the distinction is important. *Most USB devices are simple,
71 with only one function, one configuration, one interface, and one alternate
72 setting.*
73
74- Interfaces have one or more "endpoints", each of which supports one
75 type and direction of data transfer such as "bulk out" or "interrupt
76 in". The entire configuration may have up to sixteen endpoints in
77 each direction, allocated as needed among all the interfaces.
78
79- Data transfer on USB is packetized; each endpoint has a maximum
80 packet size. Drivers must often be aware of conventions such as
81 flagging the end of bulk transfers using "short" (including zero
82 length) packets.
83
84- The Linux USB API supports synchronous calls for control and bulk
85 messages. It also supports asynchronous calls for all kinds of data
86 transfer, using request structures called "URBs" (USB Request
87 Blocks).
88
89Accordingly, the USB Core API exposed to device drivers covers quite a
90lot of territory. You'll probably need to consult the USB 3.0
91specification, available online from www.usb.org at no cost, as well as
92class or device specifications.
93
94The only host-side drivers that actually touch hardware (reading/writing
95registers, handling IRQs, and so on) are the HCDs. In theory, all HCDs
96provide the same functionality through the same API. In practice, that's
97becoming more true, but there are still differences
98that crop up especially with fault handling on the less common controllers.
99Different controllers don't
100necessarily report the same aspects of failures, and recovery from
101faults (including software-induced ones like unlinking an URB) isn't yet
102fully consistent. Device driver authors should make a point of doing
103disconnect testing (while the device is active) with each different host
104controller driver, to make sure drivers don't have bugs of their own as
105well as to make sure they aren't relying on some HCD-specific behavior.
106
Mauro Carvalho Chehab9a3c8b32017-04-05 10:22:58 -0300107.. _usb_chapter9:
108
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100109USB-Standard Types
110==================
111
112In ``<linux/usb/ch9.h>`` you will find the USB data types defined in
113chapter 9 of the USB specification. These data types are used throughout
Mauro Carvalho Chehab8a6a2852017-04-16 21:51:06 -0300114USB, and in APIs including this host side API, gadget APIs, usb character
115devices and debugfs interfaces.
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100116
117.. kernel-doc:: include/linux/usb/ch9.h
118 :internal:
119
Mauro Carvalho Chehab9a3c8b32017-04-05 10:22:58 -0300120.. _usb_header:
121
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100122Host-Side Data Types and Macros
123===============================
124
125The host side API exposes several layers to drivers, some of which are
126more necessary than others. These support lifecycle models for host side
127drivers and devices, and support passing buffers through usbcore to some
128HCD that performs the I/O for the device driver.
129
130.. kernel-doc:: include/linux/usb.h
131 :internal:
132
133USB Core APIs
134=============
135
136There are two basic I/O models in the USB API. The most elemental one is
137asynchronous: drivers submit requests in the form of an URB, and the
138URB's completion callback handles the next step. All USB transfer types
139support that model, although there are special cases for control URBs
140(which always have setup and status stages, but may not have a data
141stage) and isochronous URBs (which allow large packets and include
142per-packet fault reports). Built on top of that is synchronous API
143support, where a driver calls a routine that allocates one or more URBs,
144submits them, and waits until they complete. There are synchronous
145wrappers for single-buffer control and bulk transfers (which are awkward
146to use in some driver disconnect scenarios), and for scatterlist based
147streaming i/o (bulk or interrupt).
148
149USB drivers need to provide buffers that can be used for DMA, although
150they don't necessarily need to provide the DMA mapping themselves. There
151are APIs to use used when allocating DMA buffers, which can prevent use
152of bounce buffers on some systems. In some cases, drivers may be able to
153rely on 64bit DMA to eliminate another kind of bounce buffer.
154
155.. kernel-doc:: drivers/usb/core/urb.c
156 :export:
157
158.. kernel-doc:: drivers/usb/core/message.c
159 :export:
160
161.. kernel-doc:: drivers/usb/core/file.c
162 :export:
163
164.. kernel-doc:: drivers/usb/core/driver.c
165 :export:
166
167.. kernel-doc:: drivers/usb/core/usb.c
168 :export:
169
170.. kernel-doc:: drivers/usb/core/hub.c
171 :export:
172
173Host Controller APIs
174====================
175
176These APIs are only for use by host controller drivers, most of which
177implement standard register interfaces such as XHCI, EHCI, OHCI, or UHCI. UHCI
178was one of the first interfaces, designed by Intel and also used by VIA;
179it doesn't do much in hardware. OHCI was designed later, to have the
180hardware do more work (bigger transfers, tracking protocol state, and so
181on). EHCI was designed with USB 2.0; its design has features that
182resemble OHCI (hardware does much more work) as well as UHCI (some parts
183of ISO support, TD list processing). XHCI was designed with USB 3.0. It
184continues to shift support for functionality into hardware.
185
186There are host controllers other than the "big three", although most PCI
187based controllers (and a few non-PCI based ones) use one of those
188interfaces. Not all host controllers use DMA; some use PIO, and there is
189also a simulator and a virtual host controller to pipe USB over the network.
190
191The same basic APIs are available to drivers for all those controllers.
192For historical reasons they are in two layers: :c:type:`struct
193usb_bus <usb_bus>` is a rather thin layer that became available
194in the 2.2 kernels, while :c:type:`struct usb_hcd <usb_hcd>`
195is a more featureful layer
196that lets HCDs share common code, to shrink driver size and
197significantly reduce hcd-specific behaviors.
198
199.. kernel-doc:: drivers/usb/core/hcd.c
200 :export:
201
202.. kernel-doc:: drivers/usb/core/hcd-pci.c
203 :export:
204
205.. kernel-doc:: drivers/usb/core/buffer.c
206 :internal:
207
Mauro Carvalho Chehab8a6a2852017-04-16 21:51:06 -0300208The USB character device nodes
209==============================
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100210
Mauro Carvalho Chehab8a6a2852017-04-16 21:51:06 -0300211This chapter presents the Linux character device nodes. You may prefer
212to avoid writing new kernel code for your USB driver. User mode device
213drivers are usually packaged as applications or libraries, and may use
214character devices through some programming library that wraps it.
215Such libraries include
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100216`libusb <http://libusb.sourceforge.net>`__ for C/C++, and
217`jUSB <http://jUSB.sourceforge.net>`__ for Java.
218
Mauro Carvalho Chehab9a3c8b32017-04-05 10:22:58 -0300219.. note::
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100220
Mauro Carvalho Chehab8a6a2852017-04-16 21:51:06 -0300221 - They were used to be implemented via *usbfs*, but this is not part of
222 the sysfs debug interface.
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100223
Mauro Carvalho Chehab8a6a2852017-04-16 21:51:06 -0300224 - This particular documentation is incomplete, especially with respect
225 to the asynchronous mode. As of kernel 2.5.66 the code and this
226 (new) documentation need to be cross-reviewed.
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100227
Mauro Carvalho Chehab8a6a2852017-04-16 21:51:06 -0300228What files are in "devtmpfs"?
229-----------------------------
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100230
Mauro Carvalho Chehab8a6a2852017-04-16 21:51:06 -0300231Conventionally mounted at ``/dev/bus/usb/``, usbfs features include:
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100232
Mauro Carvalho Chehab8a6a2852017-04-16 21:51:06 -0300233- ``/dev/bus/usb//BBB/DDD`` ... magic files exposing the each device's
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100234 configuration descriptors, and supporting a series of ioctls for
235 making device requests, including I/O to devices. (Purely for access
236 by programs.)
237
238Each bus is given a number (BBB) based on when it was enumerated; within
239each bus, each device is given a similar number (DDD). Those BBB/DDD
240paths are not "stable" identifiers; expect them to change even if you
241always leave the devices plugged in to the same hub port. *Don't even
242think of saving these in application configuration files.* Stable
243identifiers are available, for user mode applications that want to use
244them. HID and networking devices expose these stable IDs, so that for
245example you can be sure that you told the right UPS to power down its
246second server. "usbfs" doesn't (yet) expose those IDs.
247
Mauro Carvalho Chehab8a6a2852017-04-16 21:51:06 -0300248/dev/bus/usb//BBB/DDD
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100249---------------------
250
251Use these files in one of these basic ways:
252
253*They can be read,* producing first the device descriptor (18 bytes) and
254then the descriptors for the current configuration. See the USB 2.0 spec
255for details about those binary data formats. You'll need to convert most
256multibyte values from little endian format to your native host byte
257order, although a few of the fields in the device descriptor (both of
258the BCD-encoded fields, and the vendor and product IDs) will be
259byteswapped for you. Note that configuration descriptors include
260descriptors for interfaces, altsettings, endpoints, and maybe additional
261class descriptors.
262
263*Perform USB operations* using *ioctl()* requests to make endpoint I/O
264requests (synchronously or asynchronously) or manage the device. These
265requests need the CAP_SYS_RAWIO capability, as well as filesystem
266access permissions. Only one ioctl request can be made on one of these
267device files at a time. This means that if you are synchronously reading
268an endpoint from one thread, you won't be able to write to a different
269endpoint from another thread until the read completes. This works for
270*half duplex* protocols, but otherwise you'd use asynchronous i/o
271requests.
272
273Life Cycle of User Mode Drivers
274-------------------------------
275
276Such a driver first needs to find a device file for a device it knows
277how to handle. Maybe it was told about it because a ``/sbin/hotplug``
278event handling agent chose that driver to handle the new device. Or
Mauro Carvalho Chehab8a6a2852017-04-16 21:51:06 -0300279maybe it's an application that scans all the /dev/bus/usb/ device files,
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100280and ignores most devices. In either case, it should :c:func:`read()`
281all the descriptors from the device file, and check them against what it
282knows how to handle. It might just reject everything except a particular
283vendor and product ID, or need a more complex policy.
284
285Never assume there will only be one such device on the system at a time!
286If your code can't handle more than one device at a time, at least
287detect when there's more than one, and have your users choose which
288device to use.
289
290Once your user mode driver knows what device to use, it interacts with
291it in either of two styles. The simple style is to make only control
292requests; some devices don't need more complex interactions than those.
293(An example might be software using vendor-specific control requests for
294some initialization or configuration tasks, with a kernel driver for the
295rest.)
296
297More likely, you need a more complex style driver: one using non-control
298endpoints, reading or writing data and claiming exclusive use of an
299interface. *Bulk* transfers are easiest to use, but only their sibling
300*interrupt* transfers work with low speed devices. Both interrupt and
301*isochronous* transfers offer service guarantees because their bandwidth
302is reserved. Such "periodic" transfers are awkward to use through usbfs,
303unless you're using the asynchronous calls. However, interrupt transfers
304can also be used in a synchronous "one shot" style.
305
306Your user-mode driver should never need to worry about cleaning up
307request state when the device is disconnected, although it should close
308its open file descriptors as soon as it starts seeing the ENODEV errors.
309
310The ioctl() Requests
311--------------------
312
313To use these ioctls, you need to include the following headers in your
Mauro Carvalho Chehab9a3c8b32017-04-05 10:22:58 -0300314userspace program::
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100315
316 #include <linux/usb.h>
317 #include <linux/usbdevice_fs.h>
318 #include <asm/byteorder.h>
319
320The standard USB device model requests, from "Chapter 9" of the USB 2.0
321specification, are automatically included from the ``<linux/usb/ch9.h>``
322header.
323
324Unless noted otherwise, the ioctl requests described here will update
325the modification time on the usbfs file to which they are applied
326(unless they fail). A return of zero indicates success; otherwise, a
Mauro Carvalho Chehabe1c3e6e2017-04-05 10:23:15 -0300327standard USB error code is returned (These are documented in
328:ref:`usb-error-codes`).
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100329
330Each of these files multiplexes access to several I/O streams, one per
331endpoint. Each device has one control endpoint (endpoint zero) which
332supports a limited RPC style RPC access. Devices are configured by
333hub_wq (in the kernel) setting a device-wide *configuration* that
334affects things like power consumption and basic functionality. The
335endpoints are part of USB *interfaces*, which may have *altsettings*
336affecting things like which endpoints are available. Many devices only
337have a single configuration and interface, so drivers for them will
338ignore configurations and altsettings.
339
340Management/Status Requests
341~~~~~~~~~~~~~~~~~~~~~~~~~~
342
343A number of usbfs requests don't deal very directly with device I/O.
344They mostly relate to device management and status. These are all
345synchronous requests.
346
347USBDEVFS_CLAIMINTERFACE
348 This is used to force usbfs to claim a specific interface, which has
349 not previously been claimed by usbfs or any other kernel driver. The
350 ioctl parameter is an integer holding the number of the interface
351 (bInterfaceNumber from descriptor).
352
353 Note that if your driver doesn't claim an interface before trying to
354 use one of its endpoints, and no other driver has bound to it, then
355 the interface is automatically claimed by usbfs.
356
357 This claim will be released by a RELEASEINTERFACE ioctl, or by
358 closing the file descriptor. File modification time is not updated
359 by this request.
360
361USBDEVFS_CONNECTINFO
362 Says whether the device is lowspeed. The ioctl parameter points to a
Mauro Carvalho Chehab9a3c8b32017-04-05 10:22:58 -0300363 structure like this::
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100364
Mauro Carvalho Chehab4ad4b212017-04-05 10:22:57 -0300365 struct usbdevfs_connectinfo {
366 unsigned int devnum;
367 unsigned char slow;
368 };
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100369
370 File modification time is not updated by this request.
371
372 *You can't tell whether a "not slow" device is connected at high
373 speed (480 MBit/sec) or just full speed (12 MBit/sec).* You should
374 know the devnum value already, it's the DDD value of the device file
375 name.
376
377USBDEVFS_GETDRIVER
378 Returns the name of the kernel driver bound to a given interface (a
379 string). Parameter is a pointer to this structure, which is
Mauro Carvalho Chehab9a3c8b32017-04-05 10:22:58 -0300380 modified::
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100381
Mauro Carvalho Chehab4ad4b212017-04-05 10:22:57 -0300382 struct usbdevfs_getdriver {
383 unsigned int interface;
384 char driver[USBDEVFS_MAXDRIVERNAME + 1];
385 };
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100386
387 File modification time is not updated by this request.
388
389USBDEVFS_IOCTL
390 Passes a request from userspace through to a kernel driver that has
Mauro Carvalho Chehab9a3c8b32017-04-05 10:22:58 -0300391 an ioctl entry in the *struct usb_driver* it registered::
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100392
Mauro Carvalho Chehab4ad4b212017-04-05 10:22:57 -0300393 struct usbdevfs_ioctl {
394 int ifno;
395 int ioctl_code;
396 void *data;
397 };
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100398
Mauro Carvalho Chehab4ad4b212017-04-05 10:22:57 -0300399 /* user mode call looks like this.
400 * 'request' becomes the driver->ioctl() 'code' parameter.
401 * the size of 'param' is encoded in 'request', and that data
402 * is copied to or from the driver->ioctl() 'buf' parameter.
403 */
404 static int
405 usbdev_ioctl (int fd, int ifno, unsigned request, void *param)
406 {
407 struct usbdevfs_ioctl wrapper;
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100408
Mauro Carvalho Chehab4ad4b212017-04-05 10:22:57 -0300409 wrapper.ifno = ifno;
410 wrapper.ioctl_code = request;
411 wrapper.data = param;
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100412
Mauro Carvalho Chehab4ad4b212017-04-05 10:22:57 -0300413 return ioctl (fd, USBDEVFS_IOCTL, &wrapper);
414 }
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100415
416 File modification time is not updated by this request.
417
418 This request lets kernel drivers talk to user mode code through
419 filesystem operations even when they don't create a character or
420 block special device. It's also been used to do things like ask
421 devices what device special file should be used. Two pre-defined
422 ioctls are used to disconnect and reconnect kernel drivers, so that
423 user mode code can completely manage binding and configuration of
424 devices.
425
426USBDEVFS_RELEASEINTERFACE
427 This is used to release the claim usbfs made on interface, either
428 implicitly or because of a USBDEVFS_CLAIMINTERFACE call, before the
429 file descriptor is closed. The ioctl parameter is an integer holding
430 the number of the interface (bInterfaceNumber from descriptor); File
431 modification time is not updated by this request.
432
Mauro Carvalho Chehab9a3c8b32017-04-05 10:22:58 -0300433.. warning::
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100434
Mauro Carvalho Chehab4ad4b212017-04-05 10:22:57 -0300435 *No security check is made to ensure that the task which made
436 the claim is the one which is releasing it. This means that user
437 mode driver may interfere other ones.*
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100438
439USBDEVFS_RESETEP
440 Resets the data toggle value for an endpoint (bulk or interrupt) to
441 DATA0. The ioctl parameter is an integer endpoint number (1 to 15,
442 as identified in the endpoint descriptor), with USB_DIR_IN added
443 if the device's endpoint sends data to the host.
444
Mauro Carvalho Chehab4ad4b212017-04-05 10:22:57 -0300445 **Warning**
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100446
Mauro Carvalho Chehab4ad4b212017-04-05 10:22:57 -0300447 *Avoid using this request. It should probably be removed.* Using
448 it typically means the device and driver will lose toggle
449 synchronization. If you really lost synchronization, you likely
450 need to completely handshake with the device, using a request
451 like CLEAR_HALT or SET_INTERFACE.
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100452
453USBDEVFS_DROP_PRIVILEGES
454 This is used to relinquish the ability to do certain operations
455 which are considered to be privileged on a usbfs file descriptor.
456 This includes claiming arbitrary interfaces, resetting a device on
457 which there are currently claimed interfaces from other users, and
458 issuing USBDEVFS_IOCTL calls. The ioctl parameter is a 32 bit mask
459 of interfaces the user is allowed to claim on this file descriptor.
460 You may issue this ioctl more than one time to narrow said mask.
461
462Synchronous I/O Support
463~~~~~~~~~~~~~~~~~~~~~~~
464
465Synchronous requests involve the kernel blocking until the user mode
466request completes, either by finishing successfully or by reporting an
467error. In most cases this is the simplest way to use usbfs, although as
468noted above it does prevent performing I/O to more than one endpoint at
469a time.
470
471USBDEVFS_BULK
472 Issues a bulk read or write request to the device. The ioctl
Mauro Carvalho Chehab9a3c8b32017-04-05 10:22:58 -0300473 parameter is a pointer to this structure::
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100474
Mauro Carvalho Chehab4ad4b212017-04-05 10:22:57 -0300475 struct usbdevfs_bulktransfer {
476 unsigned int ep;
477 unsigned int len;
478 unsigned int timeout; /* in milliseconds */
479 void *data;
480 };
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100481
482 The "ep" value identifies a bulk endpoint number (1 to 15, as
483 identified in an endpoint descriptor), masked with USB_DIR_IN when
484 referring to an endpoint which sends data to the host from the
485 device. The length of the data buffer is identified by "len"; Recent
486 kernels support requests up to about 128KBytes. *FIXME say how read
487 length is returned, and how short reads are handled.*.
488
489USBDEVFS_CLEAR_HALT
490 Clears endpoint halt (stall) and resets the endpoint toggle. This is
491 only meaningful for bulk or interrupt endpoints. The ioctl parameter
492 is an integer endpoint number (1 to 15, as identified in an endpoint
493 descriptor), masked with USB_DIR_IN when referring to an endpoint
494 which sends data to the host from the device.
495
496 Use this on bulk or interrupt endpoints which have stalled,
497 returning *-EPIPE* status to a data transfer request. Do not issue
498 the control request directly, since that could invalidate the host's
499 record of the data toggle.
500
501USBDEVFS_CONTROL
502 Issues a control request to the device. The ioctl parameter points
Mauro Carvalho Chehab9a3c8b32017-04-05 10:22:58 -0300503 to a structure like this::
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100504
Mauro Carvalho Chehab4ad4b212017-04-05 10:22:57 -0300505 struct usbdevfs_ctrltransfer {
506 __u8 bRequestType;
507 __u8 bRequest;
508 __u16 wValue;
509 __u16 wIndex;
510 __u16 wLength;
511 __u32 timeout; /* in milliseconds */
512 void *data;
513 };
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100514
515 The first eight bytes of this structure are the contents of the
516 SETUP packet to be sent to the device; see the USB 2.0 specification
517 for details. The bRequestType value is composed by combining a
Mauro Carvalho Chehab69966c92017-04-05 10:23:11 -0300518 ``USB_TYPE_*`` value, a ``USB_DIR_*`` value, and a ``USB_RECIP_*``
519 value (from ``linux/usb.h``). If wLength is nonzero, it describes
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100520 the length of the data buffer, which is either written to the device
521 (USB_DIR_OUT) or read from the device (USB_DIR_IN).
522
523 At this writing, you can't transfer more than 4 KBytes of data to or
524 from a device; usbfs has a limit, and some host controller drivers
525 have a limit. (That's not usually a problem.) *Also* there's no way
526 to say it's not OK to get a short read back from the device.
527
528USBDEVFS_RESET
529 Does a USB level device reset. The ioctl parameter is ignored. After
530 the reset, this rebinds all device interfaces. File modification
531 time is not updated by this request.
532
Mauro Carvalho Chehab9a3c8b32017-04-05 10:22:58 -0300533.. warning::
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100534
Mauro Carvalho Chehab4ad4b212017-04-05 10:22:57 -0300535 *Avoid using this call* until some usbcore bugs get fixed, since
536 it does not fully synchronize device, interface, and driver (not
537 just usbfs) state.
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100538
539USBDEVFS_SETINTERFACE
540 Sets the alternate setting for an interface. The ioctl parameter is
Mauro Carvalho Chehab9a3c8b32017-04-05 10:22:58 -0300541 a pointer to a structure like this::
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100542
Mauro Carvalho Chehab4ad4b212017-04-05 10:22:57 -0300543 struct usbdevfs_setinterface {
544 unsigned int interface;
545 unsigned int altsetting;
546 };
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100547
548 File modification time is not updated by this request.
549
550 Those struct members are from some interface descriptor applying to
551 the current configuration. The interface number is the
552 bInterfaceNumber value, and the altsetting number is the
553 bAlternateSetting value. (This resets each endpoint in the
554 interface.)
555
556USBDEVFS_SETCONFIGURATION
557 Issues the :c:func:`usb_set_configuration()` call for the
558 device. The parameter is an integer holding the number of a
559 configuration (bConfigurationValue from descriptor). File
560 modification time is not updated by this request.
561
Mauro Carvalho Chehab9a3c8b32017-04-05 10:22:58 -0300562.. warning::
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100563
Mauro Carvalho Chehab4ad4b212017-04-05 10:22:57 -0300564 *Avoid using this call* until some usbcore bugs get fixed, since
565 it does not fully synchronize device, interface, and driver (not
566 just usbfs) state.
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100567
568Asynchronous I/O Support
569~~~~~~~~~~~~~~~~~~~~~~~~
570
571As mentioned above, there are situations where it may be important to
572initiate concurrent operations from user mode code. This is particularly
573important for periodic transfers (interrupt and isochronous), but it can
574be used for other kinds of USB requests too. In such cases, the
575asynchronous requests described here are essential. Rather than
576submitting one request and having the kernel block until it completes,
577the blocking is separate.
578
579These requests are packaged into a structure that resembles the URB used
580by kernel device drivers. (No POSIX Async I/O support here, sorry.) It
Mauro Carvalho Chehab69966c92017-04-05 10:23:11 -0300581identifies the endpoint type (``USBDEVFS_URB_TYPE_*``), endpoint
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100582(number, masked with USB_DIR_IN as appropriate), buffer and length,
583and a user "context" value serving to uniquely identify each request.
584(It's usually a pointer to per-request data.) Flags can modify requests
585(not as many as supported for kernel drivers).
586
587Each request can specify a realtime signal number (between SIGRTMIN and
588SIGRTMAX, inclusive) to request a signal be sent when the request
589completes.
590
591When usbfs returns these urbs, the status value is updated, and the
592buffer may have been modified. Except for isochronous transfers, the
593actual_length is updated to say how many bytes were transferred; if the
594USBDEVFS_URB_DISABLE_SPD flag is set ("short packets are not OK"), if
Mauro Carvalho Chehab9a3c8b32017-04-05 10:22:58 -0300595fewer bytes were read than were requested then you get an error report::
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100596
597 struct usbdevfs_iso_packet_desc {
Mauro Carvalho Chehab4ad4b212017-04-05 10:22:57 -0300598 unsigned int length;
599 unsigned int actual_length;
600 unsigned int status;
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100601 };
602
603 struct usbdevfs_urb {
Mauro Carvalho Chehab4ad4b212017-04-05 10:22:57 -0300604 unsigned char type;
605 unsigned char endpoint;
606 int status;
607 unsigned int flags;
608 void *buffer;
609 int buffer_length;
610 int actual_length;
611 int start_frame;
612 int number_of_packets;
613 int error_count;
614 unsigned int signr;
615 void *usercontext;
616 struct usbdevfs_iso_packet_desc iso_frame_desc[];
Oliver Neukumdd0b38d2016-11-14 15:52:43 +0100617 };
618
619For these asynchronous requests, the file modification time reflects
620when the request was initiated. This contrasts with their use with the
621synchronous requests, where it reflects when requests complete.
622
623USBDEVFS_DISCARDURB
624 *TBS* File modification time is not updated by this request.
625
626USBDEVFS_DISCSIGNAL
627 *TBS* File modification time is not updated by this request.
628
629USBDEVFS_REAPURB
630 *TBS* File modification time is not updated by this request.
631
632USBDEVFS_REAPURBNDELAY
633 *TBS* File modification time is not updated by this request.
634
635USBDEVFS_SUBMITURB
636 *TBS*
Mauro Carvalho Chehab8a6a2852017-04-16 21:51:06 -0300637
638The USB devices
639===============
640
641The USB devices are now exported via debugfs:
642
643- ``/sys/kernel/debug/usb/devices`` ... a text file showing each of the USB
644 devices on known to the kernel, and their configuration descriptors.
645 You can also poll() this to learn about new devices.
646
647/sys/kernel/debug/usb/devices
648-----------------------------
649
650This file is handy for status viewing tools in user mode, which can scan
651the text format and ignore most of it. More detailed device status
652(including class and vendor status) is available from device-specific
653files. For information about the current format of this file, see the
654``Documentation/usb/proc_usb_info.txt`` file in your Linux kernel
655sources.
656
657This file, in combination with the poll() system call, can also be used
658to detect when devices are added or removed::
659
660 int fd;
661 struct pollfd pfd;
662
663 fd = open("/sys/kernel/debug/usb/devices", O_RDONLY);
664 pfd = { fd, POLLIN, 0 };
665 for (;;) {
666 /* The first time through, this call will return immediately. */
667 poll(&pfd, 1, -1);
668
669 /* To see what's changed, compare the file's previous and current
670 contents or scan the filesystem. (Scanning is more precise.) */
671 }
672
673Note that this behavior is intended to be used for informational and
674debug purposes. It would be more appropriate to use programs such as
675udev or HAL to initialize a device or start a user-mode helper program,
676for instance.