Back to home page

OSCL-LXR

 
 

    


0001 ========================
0002 USB Gadget API for Linux
0003 ========================
0004 
0005 :Author: David Brownell
0006 :Date:   20 August 2004
0007 
0008 Introduction
0009 ============
0010 
0011 This document presents a Linux-USB "Gadget" kernel mode API, for use
0012 within peripherals and other USB devices that embed Linux. It provides
0013 an overview of the API structure, and shows how that fits into a system
0014 development project. This is the first such API released on Linux to
0015 address a number of important problems, including:
0016 
0017 -  Supports USB 2.0, for high speed devices which can stream data at
0018    several dozen megabytes per second.
0019 
0020 -  Handles devices with dozens of endpoints just as well as ones with
0021    just two fixed-function ones. Gadget drivers can be written so
0022    they're easy to port to new hardware.
0023 
0024 -  Flexible enough to expose more complex USB device capabilities such
0025    as multiple configurations, multiple interfaces, composite devices,
0026    and alternate interface settings.
0027 
0028 -  USB "On-The-Go" (OTG) support, in conjunction with updates to the
0029    Linux-USB host side.
0030 
0031 -  Sharing data structures and API models with the Linux-USB host side
0032    API. This helps the OTG support, and looks forward to more-symmetric
0033    frameworks (where the same I/O model is used by both host and device
0034    side drivers).
0035 
0036 -  Minimalist, so it's easier to support new device controller hardware.
0037    I/O processing doesn't imply large demands for memory or CPU
0038    resources.
0039 
0040 Most Linux developers will not be able to use this API, since they have
0041 USB ``host`` hardware in a PC, workstation, or server. Linux users with
0042 embedded systems are more likely to have USB peripheral hardware. To
0043 distinguish drivers running inside such hardware from the more familiar
0044 Linux "USB device drivers", which are host side proxies for the real USB
0045 devices, a different term is used: the drivers inside the peripherals
0046 are "USB gadget drivers". In USB protocol interactions, the device
0047 driver is the master (or "client driver") and the gadget driver is the
0048 slave (or "function driver").
0049 
0050 The gadget API resembles the host side Linux-USB API in that both use
0051 queues of request objects to package I/O buffers, and those requests may
0052 be submitted or canceled. They share common definitions for the standard
0053 USB *Chapter 9* messages, structures, and constants. Also, both APIs
0054 bind and unbind drivers to devices. The APIs differ in detail, since the
0055 host side's current URB framework exposes a number of implementation
0056 details and assumptions that are inappropriate for a gadget API. While
0057 the model for control transfers and configuration management is
0058 necessarily different (one side is a hardware-neutral master, the other
0059 is a hardware-aware slave), the endpoint I/0 API used here should also
0060 be usable for an overhead-reduced host side API.
0061 
0062 Structure of Gadget Drivers
0063 ===========================
0064 
0065 A system running inside a USB peripheral normally has at least three
0066 layers inside the kernel to handle USB protocol processing, and may have
0067 additional layers in user space code. The ``gadget`` API is used by the
0068 middle layer to interact with the lowest level (which directly handles
0069 hardware).
0070 
0071 In Linux, from the bottom up, these layers are:
0072 
0073 *USB Controller Driver*
0074     This is the lowest software level. It is the only layer that talks
0075     to hardware, through registers, fifos, dma, irqs, and the like. The
0076     ``<linux/usb/gadget.h>`` API abstracts the peripheral controller
0077     endpoint hardware. That hardware is exposed through endpoint
0078     objects, which accept streams of IN/OUT buffers, and through
0079     callbacks that interact with gadget drivers. Since normal USB
0080     devices only have one upstream port, they only have one of these
0081     drivers. The controller driver can support any number of different
0082     gadget drivers, but only one of them can be used at a time.
0083 
0084     Examples of such controller hardware include the PCI-based NetChip
0085     2280 USB 2.0 high speed controller, the SA-11x0 or PXA-25x UDC
0086     (found within many PDAs), and a variety of other products.
0087 
0088 *Gadget Driver*
0089     The lower boundary of this driver implements hardware-neutral USB
0090     functions, using calls to the controller driver. Because such
0091     hardware varies widely in capabilities and restrictions, and is used
0092     in embedded environments where space is at a premium, the gadget
0093     driver is often configured at compile time to work with endpoints
0094     supported by one particular controller. Gadget drivers may be
0095     portable to several different controllers, using conditional
0096     compilation. (Recent kernels substantially simplify the work
0097     involved in supporting new hardware, by *autoconfiguring* endpoints
0098     automatically for many bulk-oriented drivers.) Gadget driver
0099     responsibilities include:
0100 
0101     -  handling setup requests (ep0 protocol responses) possibly
0102        including class-specific functionality
0103 
0104     -  returning configuration and string descriptors
0105 
0106     -  (re)setting configurations and interface altsettings, including
0107        enabling and configuring endpoints
0108 
0109     -  handling life cycle events, such as managing bindings to
0110        hardware, USB suspend/resume, remote wakeup, and disconnection
0111        from the USB host.
0112 
0113     -  managing IN and OUT transfers on all currently enabled endpoints
0114 
0115     Such drivers may be modules of proprietary code, although that
0116     approach is discouraged in the Linux community.
0117 
0118 *Upper Level*
0119     Most gadget drivers have an upper boundary that connects to some
0120     Linux driver or framework in Linux. Through that boundary flows the
0121     data which the gadget driver produces and/or consumes through
0122     protocol transfers over USB. Examples include:
0123 
0124     -  user mode code, using generic (gadgetfs) or application specific
0125        files in ``/dev``
0126 
0127     -  networking subsystem (for network gadgets, like the CDC Ethernet
0128        Model gadget driver)
0129 
0130     -  data capture drivers, perhaps video4Linux or a scanner driver; or
0131        test and measurement hardware.
0132 
0133     -  input subsystem (for HID gadgets)
0134 
0135     -  sound subsystem (for audio gadgets)
0136 
0137     -  file system (for PTP gadgets)
0138 
0139     -  block i/o subsystem (for usb-storage gadgets)
0140 
0141     -  ... and more
0142 
0143 *Additional Layers*
0144     Other layers may exist. These could include kernel layers, such as
0145     network protocol stacks, as well as user mode applications building
0146     on standard POSIX system call APIs such as ``open()``, ``close()``,
0147     ``read()`` and ``write()``. On newer systems, POSIX Async I/O calls may
0148     be an option. Such user mode code will not necessarily be subject to
0149     the GNU General Public License (GPL).
0150 
0151 OTG-capable systems will also need to include a standard Linux-USB host
0152 side stack, with ``usbcore``, one or more *Host Controller Drivers*
0153 (HCDs), *USB Device Drivers* to support the OTG "Targeted Peripheral
0154 List", and so forth. There will also be an *OTG Controller Driver*,
0155 which is visible to gadget and device driver developers only indirectly.
0156 That helps the host and device side USB controllers implement the two
0157 new OTG protocols (HNP and SRP). Roles switch (host to peripheral, or
0158 vice versa) using HNP during USB suspend processing, and SRP can be
0159 viewed as a more battery-friendly kind of device wakeup protocol.
0160 
0161 Over time, reusable utilities are evolving to help make some gadget
0162 driver tasks simpler. For example, building configuration descriptors
0163 from vectors of descriptors for the configurations interfaces and
0164 endpoints is now automated, and many drivers now use autoconfiguration
0165 to choose hardware endpoints and initialize their descriptors. A
0166 potential example of particular interest is code implementing standard
0167 USB-IF protocols for HID, networking, storage, or audio classes. Some
0168 developers are interested in KDB or KGDB hooks, to let target hardware
0169 be remotely debugged. Most such USB protocol code doesn't need to be
0170 hardware-specific, any more than network protocols like X11, HTTP, or
0171 NFS are. Such gadget-side interface drivers should eventually be
0172 combined, to implement composite devices.
0173 
0174 Kernel Mode Gadget API
0175 ======================
0176 
0177 Gadget drivers declare themselves through a struct
0178 :c:type:`usb_gadget_driver`, which is responsible for most parts of enumeration
0179 for a struct usb_gadget. The response to a set_configuration usually
0180 involves enabling one or more of the struct usb_ep objects exposed by
0181 the gadget, and submitting one or more struct usb_request buffers to
0182 transfer data. Understand those four data types, and their operations,
0183 and you will understand how this API works.
0184 
0185 .. Note::
0186 
0187     Other than the "Chapter 9" data types, most of the significant data
0188     types and functions are described here.
0189 
0190     However, some relevant information is likely omitted from what you
0191     are reading. One example of such information is endpoint
0192     autoconfiguration. You'll have to read the header file, and use
0193     example source code (such as that for "Gadget Zero"), to fully
0194     understand the API.
0195 
0196     The part of the API implementing some basic driver capabilities is
0197     specific to the version of the Linux kernel that's in use. The 2.6
0198     and upper kernel versions include a *driver model* framework that has
0199     no analogue on earlier kernels; so those parts of the gadget API are
0200     not fully portable. (They are implemented on 2.4 kernels, but in a
0201     different way.) The driver model state is another part of this API that is
0202     ignored by the kerneldoc tools.
0203 
0204 The core API does not expose every possible hardware feature, only the
0205 most widely available ones. There are significant hardware features,
0206 such as device-to-device DMA (without temporary storage in a memory
0207 buffer) that would be added using hardware-specific APIs.
0208 
0209 This API allows drivers to use conditional compilation to handle
0210 endpoint capabilities of different hardware, but doesn't require that.
0211 Hardware tends to have arbitrary restrictions, relating to transfer
0212 types, addressing, packet sizes, buffering, and availability. As a rule,
0213 such differences only matter for "endpoint zero" logic that handles
0214 device configuration and management. The API supports limited run-time
0215 detection of capabilities, through naming conventions for endpoints.
0216 Many drivers will be able to at least partially autoconfigure
0217 themselves. In particular, driver init sections will often have endpoint
0218 autoconfiguration logic that scans the hardware's list of endpoints to
0219 find ones matching the driver requirements (relying on those
0220 conventions), to eliminate some of the most common reasons for
0221 conditional compilation.
0222 
0223 Like the Linux-USB host side API, this API exposes the "chunky" nature
0224 of USB messages: I/O requests are in terms of one or more "packets", and
0225 packet boundaries are visible to drivers. Compared to RS-232 serial
0226 protocols, USB resembles synchronous protocols like HDLC (N bytes per
0227 frame, multipoint addressing, host as the primary station and devices as
0228 secondary stations) more than asynchronous ones (tty style: 8 data bits
0229 per frame, no parity, one stop bit). So for example the controller
0230 drivers won't buffer two single byte writes into a single two-byte USB
0231 IN packet, although gadget drivers may do so when they implement
0232 protocols where packet boundaries (and "short packets") are not
0233 significant.
0234 
0235 Driver Life Cycle
0236 -----------------
0237 
0238 Gadget drivers make endpoint I/O requests to hardware without needing to
0239 know many details of the hardware, but driver setup/configuration code
0240 needs to handle some differences. Use the API like this:
0241 
0242 1. Register a driver for the particular device side usb controller
0243    hardware, such as the net2280 on PCI (USB 2.0), sa11x0 or pxa25x as
0244    found in Linux PDAs, and so on. At this point the device is logically
0245    in the USB ch9 initial state (``attached``), drawing no power and not
0246    usable (since it does not yet support enumeration). Any host should
0247    not see the device, since it's not activated the data line pullup
0248    used by the host to detect a device, even if VBUS power is available.
0249 
0250 2. Register a gadget driver that implements some higher level device
0251    function. That will then bind() to a :c:type:`usb_gadget`, which activates
0252    the data line pullup sometime after detecting VBUS.
0253 
0254 3. The hardware driver can now start enumerating. The steps it handles
0255    are to accept USB ``power`` and ``set_address`` requests. Other steps are
0256    handled by the gadget driver. If the gadget driver module is unloaded
0257    before the host starts to enumerate, steps before step 7 are skipped.
0258 
0259 4. The gadget driver's ``setup()`` call returns usb descriptors, based both
0260    on what the bus interface hardware provides and on the functionality
0261    being implemented. That can involve alternate settings or
0262    configurations, unless the hardware prevents such operation. For OTG
0263    devices, each configuration descriptor includes an OTG descriptor.
0264 
0265 5. The gadget driver handles the last step of enumeration, when the USB
0266    host issues a ``set_configuration`` call. It enables all endpoints used
0267    in that configuration, with all interfaces in their default settings.
0268    That involves using a list of the hardware's endpoints, enabling each
0269    endpoint according to its descriptor. It may also involve using
0270    ``usb_gadget_vbus_draw`` to let more power be drawn from VBUS, as
0271    allowed by that configuration. For OTG devices, setting a
0272    configuration may also involve reporting HNP capabilities through a
0273    user interface.
0274 
0275 6. Do real work and perform data transfers, possibly involving changes
0276    to interface settings or switching to new configurations, until the
0277    device is disconnect()ed from the host. Queue any number of transfer
0278    requests to each endpoint. It may be suspended and resumed several
0279    times before being disconnected. On disconnect, the drivers go back
0280    to step 3 (above).
0281 
0282 7. When the gadget driver module is being unloaded, the driver unbind()
0283    callback is issued. That lets the controller driver be unloaded.
0284 
0285 Drivers will normally be arranged so that just loading the gadget driver
0286 module (or statically linking it into a Linux kernel) allows the
0287 peripheral device to be enumerated, but some drivers will defer
0288 enumeration until some higher level component (like a user mode daemon)
0289 enables it. Note that at this lowest level there are no policies about
0290 how ep0 configuration logic is implemented, except that it should obey
0291 USB specifications. Such issues are in the domain of gadget drivers,
0292 including knowing about implementation constraints imposed by some USB
0293 controllers or understanding that composite devices might happen to be
0294 built by integrating reusable components.
0295 
0296 Note that the lifecycle above can be slightly different for OTG devices.
0297 Other than providing an additional OTG descriptor in each configuration,
0298 only the HNP-related differences are particularly visible to driver
0299 code. They involve reporting requirements during the ``SET_CONFIGURATION``
0300 request, and the option to invoke HNP during some suspend callbacks.
0301 Also, SRP changes the semantics of ``usb_gadget_wakeup`` slightly.
0302 
0303 USB 2.0 Chapter 9 Types and Constants
0304 -------------------------------------
0305 
0306 Gadget drivers rely on common USB structures and constants defined in
0307 the :ref:`linux/usb/ch9.h <usb_chapter9>` header file, which is standard in
0308 Linux 2.6+ kernels. These are the same types and constants used by host side
0309 drivers (and usbcore).
0310 
0311 Core Objects and Methods
0312 ------------------------
0313 
0314 These are declared in ``<linux/usb/gadget.h>``, and are used by gadget
0315 drivers to interact with USB peripheral controller drivers.
0316 
0317 .. kernel-doc:: include/linux/usb/gadget.h
0318    :internal:
0319 
0320 Optional Utilities
0321 ------------------
0322 
0323 The core API is sufficient for writing a USB Gadget Driver, but some
0324 optional utilities are provided to simplify common tasks. These
0325 utilities include endpoint autoconfiguration.
0326 
0327 .. kernel-doc:: drivers/usb/gadget/usbstring.c
0328    :export:
0329 
0330 .. kernel-doc:: drivers/usb/gadget/config.c
0331    :export:
0332 
0333 Composite Device Framework
0334 --------------------------
0335 
0336 The core API is sufficient for writing drivers for composite USB devices
0337 (with more than one function in a given configuration), and also
0338 multi-configuration devices (also more than one function, but not
0339 necessarily sharing a given configuration). There is however an optional
0340 framework which makes it easier to reuse and combine functions.
0341 
0342 Devices using this framework provide a struct usb_composite_driver,
0343 which in turn provides one or more struct usb_configuration
0344 instances. Each such configuration includes at least one struct
0345 :c:type:`usb_function`, which packages a user visible role such as "network
0346 link" or "mass storage device". Management functions may also exist,
0347 such as "Device Firmware Upgrade".
0348 
0349 .. kernel-doc:: include/linux/usb/composite.h
0350    :internal:
0351 
0352 .. kernel-doc:: drivers/usb/gadget/composite.c
0353    :export:
0354 
0355 Composite Device Functions
0356 --------------------------
0357 
0358 At this writing, a few of the current gadget drivers have been converted
0359 to this framework. Near-term plans include converting all of them,
0360 except for ``gadgetfs``.
0361 
0362 Peripheral Controller Drivers
0363 =============================
0364 
0365 The first hardware supporting this API was the NetChip 2280 controller,
0366 which supports USB 2.0 high speed and is based on PCI. This is the
0367 ``net2280`` driver module. The driver supports Linux kernel versions 2.4
0368 and 2.6; contact NetChip Technologies for development boards and product
0369 information.
0370 
0371 Other hardware working in the ``gadget`` framework includes: Intel's PXA
0372 25x and IXP42x series processors (``pxa2xx_udc``), Toshiba TC86c001
0373 "Goku-S" (``goku_udc``), Renesas SH7705/7727 (``sh_udc``), MediaQ 11xx
0374 (``mq11xx_udc``), Hynix HMS30C7202 (``h7202_udc``), National 9303/4
0375 (``n9604_udc``), Texas Instruments OMAP (``omap_udc``), Sharp LH7A40x
0376 (``lh7a40x_udc``), and more. Most of those are full speed controllers.
0377 
0378 At this writing, there are people at work on drivers in this framework
0379 for several other USB device controllers, with plans to make many of
0380 them be widely available.
0381 
0382 A partial USB simulator, the ``dummy_hcd`` driver, is available. It can
0383 act like a net2280, a pxa25x, or an sa11x0 in terms of available
0384 endpoints and device speeds; and it simulates control, bulk, and to some
0385 extent interrupt transfers. That lets you develop some parts of a gadget
0386 driver on a normal PC, without any special hardware, and perhaps with
0387 the assistance of tools such as GDB running with User Mode Linux. At
0388 least one person has expressed interest in adapting that approach,
0389 hooking it up to a simulator for a microcontroller. Such simulators can
0390 help debug subsystems where the runtime hardware is unfriendly to
0391 software development, or is not yet available.
0392 
0393 Support for other controllers is expected to be developed and
0394 contributed over time, as this driver framework evolves.
0395 
0396 Gadget Drivers
0397 ==============
0398 
0399 In addition to *Gadget Zero* (used primarily for testing and development
0400 with drivers for usb controller hardware), other gadget drivers exist.
0401 
0402 There's an ``ethernet`` gadget driver, which implements one of the most
0403 useful *Communications Device Class* (CDC) models. One of the standards
0404 for cable modem interoperability even specifies the use of this ethernet
0405 model as one of two mandatory options. Gadgets using this code look to a
0406 USB host as if they're an Ethernet adapter. It provides access to a
0407 network where the gadget's CPU is one host, which could easily be
0408 bridging, routing, or firewalling access to other networks. Since some
0409 hardware can't fully implement the CDC Ethernet requirements, this
0410 driver also implements a "good parts only" subset of CDC Ethernet. (That
0411 subset doesn't advertise itself as CDC Ethernet, to avoid creating
0412 problems.)
0413 
0414 Support for Microsoft's ``RNDIS`` protocol has been contributed by
0415 Pengutronix and Auerswald GmbH. This is like CDC Ethernet, but it runs
0416 on more slightly USB hardware (but less than the CDC subset). However,
0417 its main claim to fame is being able to connect directly to recent
0418 versions of Windows, using drivers that Microsoft bundles and supports,
0419 making it much simpler to network with Windows.
0420 
0421 There is also support for user mode gadget drivers, using ``gadgetfs``.
0422 This provides a *User Mode API* that presents each endpoint as a single
0423 file descriptor. I/O is done using normal ``read()`` and ``read()`` calls.
0424 Familiar tools like GDB and pthreads can be used to develop and debug
0425 user mode drivers, so that once a robust controller driver is available
0426 many applications for it won't require new kernel mode software. Linux
0427 2.6 *Async I/O (AIO)* support is available, so that user mode software
0428 can stream data with only slightly more overhead than a kernel driver.
0429 
0430 There's a USB Mass Storage class driver, which provides a different
0431 solution for interoperability with systems such as MS-Windows and MacOS.
0432 That *Mass Storage* driver uses a file or block device as backing store
0433 for a drive, like the ``loop`` driver. The USB host uses the BBB, CB, or
0434 CBI versions of the mass storage class specification, using transparent
0435 SCSI commands to access the data from the backing store.
0436 
0437 There's a "serial line" driver, useful for TTY style operation over USB.
0438 The latest version of that driver supports CDC ACM style operation, like
0439 a USB modem, and so on most hardware it can interoperate easily with
0440 MS-Windows. One interesting use of that driver is in boot firmware (like
0441 a BIOS), which can sometimes use that model with very small systems
0442 without real serial lines.
0443 
0444 Support for other kinds of gadget is expected to be developed and
0445 contributed over time, as this driver framework evolves.
0446 
0447 USB On-The-GO (OTG)
0448 ===================
0449 
0450 USB OTG support on Linux 2.6 was initially developed by Texas
0451 Instruments for `OMAP <http://www.omap.com>`__ 16xx and 17xx series
0452 processors. Other OTG systems should work in similar ways, but the
0453 hardware level details could be very different.
0454 
0455 Systems need specialized hardware support to implement OTG, notably
0456 including a special *Mini-AB* jack and associated transceiver to support
0457 *Dual-Role* operation: they can act either as a host, using the standard
0458 Linux-USB host side driver stack, or as a peripheral, using this
0459 ``gadget`` framework. To do that, the system software relies on small
0460 additions to those programming interfaces, and on a new internal
0461 component (here called an "OTG Controller") affecting which driver stack
0462 connects to the OTG port. In each role, the system can re-use the
0463 existing pool of hardware-neutral drivers, layered on top of the
0464 controller driver interfaces (:c:type:`usb_bus` or :c:type:`usb_gadget`).
0465 Such drivers need at most minor changes, and most of the calls added to
0466 support OTG can also benefit non-OTG products.
0467 
0468 -  Gadget drivers test the ``is_otg`` flag, and use it to determine
0469    whether or not to include an OTG descriptor in each of their
0470    configurations.
0471 
0472 -  Gadget drivers may need changes to support the two new OTG protocols,
0473    exposed in new gadget attributes such as ``b_hnp_enable`` flag. HNP
0474    support should be reported through a user interface (two LEDs could
0475    suffice), and is triggered in some cases when the host suspends the
0476    peripheral. SRP support can be user-initiated just like remote
0477    wakeup, probably by pressing the same button.
0478 
0479 -  On the host side, USB device drivers need to be taught to trigger HNP
0480    at appropriate moments, using ``usb_suspend_device()``. That also
0481    conserves battery power, which is useful even for non-OTG
0482    configurations.
0483 
0484 -  Also on the host side, a driver must support the OTG "Targeted
0485    Peripheral List". That's just a whitelist, used to reject peripherals
0486    not supported with a given Linux OTG host. *This whitelist is
0487    product-specific; each product must modify* ``otg_whitelist.h`` *to
0488    match its interoperability specification.*
0489 
0490    Non-OTG Linux hosts, like PCs and workstations, normally have some
0491    solution for adding drivers, so that peripherals that aren't
0492    recognized can eventually be supported. That approach is unreasonable
0493    for consumer products that may never have their firmware upgraded,
0494    and where it's usually unrealistic to expect traditional
0495    PC/workstation/server kinds of support model to work. For example,
0496    it's often impractical to change device firmware once the product has
0497    been distributed, so driver bugs can't normally be fixed if they're
0498    found after shipment.
0499 
0500 Additional changes are needed below those hardware-neutral :c:type:`usb_bus`
0501 and :c:type:`usb_gadget` driver interfaces; those aren't discussed here in any
0502 detail. Those affect the hardware-specific code for each USB Host or
0503 Peripheral controller, and how the HCD initializes (since OTG can be
0504 active only on a single port). They also involve what may be called an
0505 *OTG Controller Driver*, managing the OTG transceiver and the OTG state
0506 machine logic as well as much of the root hub behavior for the OTG port.
0507 The OTG controller driver needs to activate and deactivate USB
0508 controllers depending on the relevant device role. Some related changes
0509 were needed inside usbcore, so that it can identify OTG-capable devices
0510 and respond appropriately to HNP or SRP protocols.