162306a36Sopenharmony_ci========================
262306a36Sopenharmony_ciUSB Gadget API for Linux
362306a36Sopenharmony_ci========================
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci:Author: David Brownell
662306a36Sopenharmony_ci:Date:   20 August 2004
762306a36Sopenharmony_ci
862306a36Sopenharmony_ciIntroduction
962306a36Sopenharmony_ci============
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ciThis document presents a Linux-USB "Gadget" kernel mode API, for use
1262306a36Sopenharmony_ciwithin peripherals and other USB devices that embed Linux. It provides
1362306a36Sopenharmony_cian overview of the API structure, and shows how that fits into a system
1462306a36Sopenharmony_cidevelopment project. This is the first such API released on Linux to
1562306a36Sopenharmony_ciaddress a number of important problems, including:
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci-  Supports USB 2.0, for high speed devices which can stream data at
1862306a36Sopenharmony_ci   several dozen megabytes per second.
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci-  Handles devices with dozens of endpoints just as well as ones with
2162306a36Sopenharmony_ci   just two fixed-function ones. Gadget drivers can be written so
2262306a36Sopenharmony_ci   they're easy to port to new hardware.
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci-  Flexible enough to expose more complex USB device capabilities such
2562306a36Sopenharmony_ci   as multiple configurations, multiple interfaces, composite devices,
2662306a36Sopenharmony_ci   and alternate interface settings.
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci-  USB "On-The-Go" (OTG) support, in conjunction with updates to the
2962306a36Sopenharmony_ci   Linux-USB host side.
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci-  Sharing data structures and API models with the Linux-USB host side
3262306a36Sopenharmony_ci   API. This helps the OTG support, and looks forward to more-symmetric
3362306a36Sopenharmony_ci   frameworks (where the same I/O model is used by both host and device
3462306a36Sopenharmony_ci   side drivers).
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci-  Minimalist, so it's easier to support new device controller hardware.
3762306a36Sopenharmony_ci   I/O processing doesn't imply large demands for memory or CPU
3862306a36Sopenharmony_ci   resources.
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ciMost Linux developers will not be able to use this API, since they have
4162306a36Sopenharmony_ciUSB ``host`` hardware in a PC, workstation, or server. Linux users with
4262306a36Sopenharmony_ciembedded systems are more likely to have USB peripheral hardware. To
4362306a36Sopenharmony_cidistinguish drivers running inside such hardware from the more familiar
4462306a36Sopenharmony_ciLinux "USB device drivers", which are host side proxies for the real USB
4562306a36Sopenharmony_cidevices, a different term is used: the drivers inside the peripherals
4662306a36Sopenharmony_ciare "USB gadget drivers". In USB protocol interactions, the device
4762306a36Sopenharmony_cidriver is the master (or "client driver") and the gadget driver is the
4862306a36Sopenharmony_cislave (or "function driver").
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ciThe gadget API resembles the host side Linux-USB API in that both use
5162306a36Sopenharmony_ciqueues of request objects to package I/O buffers, and those requests may
5262306a36Sopenharmony_cibe submitted or canceled. They share common definitions for the standard
5362306a36Sopenharmony_ciUSB *Chapter 9* messages, structures, and constants. Also, both APIs
5462306a36Sopenharmony_cibind and unbind drivers to devices. The APIs differ in detail, since the
5562306a36Sopenharmony_cihost side's current URB framework exposes a number of implementation
5662306a36Sopenharmony_cidetails and assumptions that are inappropriate for a gadget API. While
5762306a36Sopenharmony_cithe model for control transfers and configuration management is
5862306a36Sopenharmony_cinecessarily different (one side is a hardware-neutral master, the other
5962306a36Sopenharmony_ciis a hardware-aware slave), the endpoint I/0 API used here should also
6062306a36Sopenharmony_cibe usable for an overhead-reduced host side API.
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ciStructure of Gadget Drivers
6362306a36Sopenharmony_ci===========================
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ciA system running inside a USB peripheral normally has at least three
6662306a36Sopenharmony_cilayers inside the kernel to handle USB protocol processing, and may have
6762306a36Sopenharmony_ciadditional layers in user space code. The ``gadget`` API is used by the
6862306a36Sopenharmony_cimiddle layer to interact with the lowest level (which directly handles
6962306a36Sopenharmony_cihardware).
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ciIn Linux, from the bottom up, these layers are:
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci*USB Controller Driver*
7462306a36Sopenharmony_ci    This is the lowest software level. It is the only layer that talks
7562306a36Sopenharmony_ci    to hardware, through registers, fifos, dma, irqs, and the like. The
7662306a36Sopenharmony_ci    ``<linux/usb/gadget.h>`` API abstracts the peripheral controller
7762306a36Sopenharmony_ci    endpoint hardware. That hardware is exposed through endpoint
7862306a36Sopenharmony_ci    objects, which accept streams of IN/OUT buffers, and through
7962306a36Sopenharmony_ci    callbacks that interact with gadget drivers. Since normal USB
8062306a36Sopenharmony_ci    devices only have one upstream port, they only have one of these
8162306a36Sopenharmony_ci    drivers. The controller driver can support any number of different
8262306a36Sopenharmony_ci    gadget drivers, but only one of them can be used at a time.
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci    Examples of such controller hardware include the PCI-based NetChip
8562306a36Sopenharmony_ci    2280 USB 2.0 high speed controller, the SA-11x0 or PXA-25x UDC
8662306a36Sopenharmony_ci    (found within many PDAs), and a variety of other products.
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci*Gadget Driver*
8962306a36Sopenharmony_ci    The lower boundary of this driver implements hardware-neutral USB
9062306a36Sopenharmony_ci    functions, using calls to the controller driver. Because such
9162306a36Sopenharmony_ci    hardware varies widely in capabilities and restrictions, and is used
9262306a36Sopenharmony_ci    in embedded environments where space is at a premium, the gadget
9362306a36Sopenharmony_ci    driver is often configured at compile time to work with endpoints
9462306a36Sopenharmony_ci    supported by one particular controller. Gadget drivers may be
9562306a36Sopenharmony_ci    portable to several different controllers, using conditional
9662306a36Sopenharmony_ci    compilation. (Recent kernels substantially simplify the work
9762306a36Sopenharmony_ci    involved in supporting new hardware, by *autoconfiguring* endpoints
9862306a36Sopenharmony_ci    automatically for many bulk-oriented drivers.) Gadget driver
9962306a36Sopenharmony_ci    responsibilities include:
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci    -  handling setup requests (ep0 protocol responses) possibly
10262306a36Sopenharmony_ci       including class-specific functionality
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci    -  returning configuration and string descriptors
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci    -  (re)setting configurations and interface altsettings, including
10762306a36Sopenharmony_ci       enabling and configuring endpoints
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci    -  handling life cycle events, such as managing bindings to
11062306a36Sopenharmony_ci       hardware, USB suspend/resume, remote wakeup, and disconnection
11162306a36Sopenharmony_ci       from the USB host.
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci    -  managing IN and OUT transfers on all currently enabled endpoints
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci    Such drivers may be modules of proprietary code, although that
11662306a36Sopenharmony_ci    approach is discouraged in the Linux community.
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci*Upper Level*
11962306a36Sopenharmony_ci    Most gadget drivers have an upper boundary that connects to some
12062306a36Sopenharmony_ci    Linux driver or framework in Linux. Through that boundary flows the
12162306a36Sopenharmony_ci    data which the gadget driver produces and/or consumes through
12262306a36Sopenharmony_ci    protocol transfers over USB. Examples include:
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci    -  user mode code, using generic (gadgetfs) or application specific
12562306a36Sopenharmony_ci       files in ``/dev``
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci    -  networking subsystem (for network gadgets, like the CDC Ethernet
12862306a36Sopenharmony_ci       Model gadget driver)
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci    -  data capture drivers, perhaps video4Linux or a scanner driver; or
13162306a36Sopenharmony_ci       test and measurement hardware.
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci    -  input subsystem (for HID gadgets)
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci    -  sound subsystem (for audio gadgets)
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci    -  file system (for PTP gadgets)
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci    -  block i/o subsystem (for usb-storage gadgets)
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci    -  ... and more
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci*Additional Layers*
14462306a36Sopenharmony_ci    Other layers may exist. These could include kernel layers, such as
14562306a36Sopenharmony_ci    network protocol stacks, as well as user mode applications building
14662306a36Sopenharmony_ci    on standard POSIX system call APIs such as ``open()``, ``close()``,
14762306a36Sopenharmony_ci    ``read()`` and ``write()``. On newer systems, POSIX Async I/O calls may
14862306a36Sopenharmony_ci    be an option. Such user mode code will not necessarily be subject to
14962306a36Sopenharmony_ci    the GNU General Public License (GPL).
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ciOTG-capable systems will also need to include a standard Linux-USB host
15262306a36Sopenharmony_ciside stack, with ``usbcore``, one or more *Host Controller Drivers*
15362306a36Sopenharmony_ci(HCDs), *USB Device Drivers* to support the OTG "Targeted Peripheral
15462306a36Sopenharmony_ciList", and so forth. There will also be an *OTG Controller Driver*,
15562306a36Sopenharmony_ciwhich is visible to gadget and device driver developers only indirectly.
15662306a36Sopenharmony_ciThat helps the host and device side USB controllers implement the two
15762306a36Sopenharmony_cinew OTG protocols (HNP and SRP). Roles switch (host to peripheral, or
15862306a36Sopenharmony_civice versa) using HNP during USB suspend processing, and SRP can be
15962306a36Sopenharmony_civiewed as a more battery-friendly kind of device wakeup protocol.
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ciOver time, reusable utilities are evolving to help make some gadget
16262306a36Sopenharmony_cidriver tasks simpler. For example, building configuration descriptors
16362306a36Sopenharmony_cifrom vectors of descriptors for the configurations interfaces and
16462306a36Sopenharmony_ciendpoints is now automated, and many drivers now use autoconfiguration
16562306a36Sopenharmony_cito choose hardware endpoints and initialize their descriptors. A
16662306a36Sopenharmony_cipotential example of particular interest is code implementing standard
16762306a36Sopenharmony_ciUSB-IF protocols for HID, networking, storage, or audio classes. Some
16862306a36Sopenharmony_cidevelopers are interested in KDB or KGDB hooks, to let target hardware
16962306a36Sopenharmony_cibe remotely debugged. Most such USB protocol code doesn't need to be
17062306a36Sopenharmony_cihardware-specific, any more than network protocols like X11, HTTP, or
17162306a36Sopenharmony_ciNFS are. Such gadget-side interface drivers should eventually be
17262306a36Sopenharmony_cicombined, to implement composite devices.
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ciKernel Mode Gadget API
17562306a36Sopenharmony_ci======================
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ciGadget drivers declare themselves through a struct
17862306a36Sopenharmony_ci:c:type:`usb_gadget_driver`, which is responsible for most parts of enumeration
17962306a36Sopenharmony_cifor a struct usb_gadget. The response to a set_configuration usually
18062306a36Sopenharmony_ciinvolves enabling one or more of the struct usb_ep objects exposed by
18162306a36Sopenharmony_cithe gadget, and submitting one or more struct usb_request buffers to
18262306a36Sopenharmony_citransfer data. Understand those four data types, and their operations,
18362306a36Sopenharmony_ciand you will understand how this API works.
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ci.. Note::
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci    Other than the "Chapter 9" data types, most of the significant data
18862306a36Sopenharmony_ci    types and functions are described here.
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci    However, some relevant information is likely omitted from what you
19162306a36Sopenharmony_ci    are reading. One example of such information is endpoint
19262306a36Sopenharmony_ci    autoconfiguration. You'll have to read the header file, and use
19362306a36Sopenharmony_ci    example source code (such as that for "Gadget Zero"), to fully
19462306a36Sopenharmony_ci    understand the API.
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci    The part of the API implementing some basic driver capabilities is
19762306a36Sopenharmony_ci    specific to the version of the Linux kernel that's in use. The 2.6
19862306a36Sopenharmony_ci    and upper kernel versions include a *driver model* framework that has
19962306a36Sopenharmony_ci    no analogue on earlier kernels; so those parts of the gadget API are
20062306a36Sopenharmony_ci    not fully portable. (They are implemented on 2.4 kernels, but in a
20162306a36Sopenharmony_ci    different way.) The driver model state is another part of this API that is
20262306a36Sopenharmony_ci    ignored by the kerneldoc tools.
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ciThe core API does not expose every possible hardware feature, only the
20562306a36Sopenharmony_cimost widely available ones. There are significant hardware features,
20662306a36Sopenharmony_cisuch as device-to-device DMA (without temporary storage in a memory
20762306a36Sopenharmony_cibuffer) that would be added using hardware-specific APIs.
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ciThis API allows drivers to use conditional compilation to handle
21062306a36Sopenharmony_ciendpoint capabilities of different hardware, but doesn't require that.
21162306a36Sopenharmony_ciHardware tends to have arbitrary restrictions, relating to transfer
21262306a36Sopenharmony_citypes, addressing, packet sizes, buffering, and availability. As a rule,
21362306a36Sopenharmony_cisuch differences only matter for "endpoint zero" logic that handles
21462306a36Sopenharmony_cidevice configuration and management. The API supports limited run-time
21562306a36Sopenharmony_cidetection of capabilities, through naming conventions for endpoints.
21662306a36Sopenharmony_ciMany drivers will be able to at least partially autoconfigure
21762306a36Sopenharmony_cithemselves. In particular, driver init sections will often have endpoint
21862306a36Sopenharmony_ciautoconfiguration logic that scans the hardware's list of endpoints to
21962306a36Sopenharmony_cifind ones matching the driver requirements (relying on those
22062306a36Sopenharmony_ciconventions), to eliminate some of the most common reasons for
22162306a36Sopenharmony_ciconditional compilation.
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ciLike the Linux-USB host side API, this API exposes the "chunky" nature
22462306a36Sopenharmony_ciof USB messages: I/O requests are in terms of one or more "packets", and
22562306a36Sopenharmony_cipacket boundaries are visible to drivers. Compared to RS-232 serial
22662306a36Sopenharmony_ciprotocols, USB resembles synchronous protocols like HDLC (N bytes per
22762306a36Sopenharmony_ciframe, multipoint addressing, host as the primary station and devices as
22862306a36Sopenharmony_cisecondary stations) more than asynchronous ones (tty style: 8 data bits
22962306a36Sopenharmony_ciper frame, no parity, one stop bit). So for example the controller
23062306a36Sopenharmony_cidrivers won't buffer two single byte writes into a single two-byte USB
23162306a36Sopenharmony_ciIN packet, although gadget drivers may do so when they implement
23262306a36Sopenharmony_ciprotocols where packet boundaries (and "short packets") are not
23362306a36Sopenharmony_cisignificant.
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ciDriver Life Cycle
23662306a36Sopenharmony_ci-----------------
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ciGadget drivers make endpoint I/O requests to hardware without needing to
23962306a36Sopenharmony_ciknow many details of the hardware, but driver setup/configuration code
24062306a36Sopenharmony_cineeds to handle some differences. Use the API like this:
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ci1. Register a driver for the particular device side usb controller
24362306a36Sopenharmony_ci   hardware, such as the net2280 on PCI (USB 2.0), sa11x0 or pxa25x as
24462306a36Sopenharmony_ci   found in Linux PDAs, and so on. At this point the device is logically
24562306a36Sopenharmony_ci   in the USB ch9 initial state (``attached``), drawing no power and not
24662306a36Sopenharmony_ci   usable (since it does not yet support enumeration). Any host should
24762306a36Sopenharmony_ci   not see the device, since it's not activated the data line pullup
24862306a36Sopenharmony_ci   used by the host to detect a device, even if VBUS power is available.
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci2. Register a gadget driver that implements some higher level device
25162306a36Sopenharmony_ci   function. That will then bind() to a :c:type:`usb_gadget`, which activates
25262306a36Sopenharmony_ci   the data line pullup sometime after detecting VBUS.
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci3. The hardware driver can now start enumerating. The steps it handles
25562306a36Sopenharmony_ci   are to accept USB ``power`` and ``set_address`` requests. Other steps are
25662306a36Sopenharmony_ci   handled by the gadget driver. If the gadget driver module is unloaded
25762306a36Sopenharmony_ci   before the host starts to enumerate, steps before step 7 are skipped.
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_ci4. The gadget driver's ``setup()`` call returns usb descriptors, based both
26062306a36Sopenharmony_ci   on what the bus interface hardware provides and on the functionality
26162306a36Sopenharmony_ci   being implemented. That can involve alternate settings or
26262306a36Sopenharmony_ci   configurations, unless the hardware prevents such operation. For OTG
26362306a36Sopenharmony_ci   devices, each configuration descriptor includes an OTG descriptor.
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ci5. The gadget driver handles the last step of enumeration, when the USB
26662306a36Sopenharmony_ci   host issues a ``set_configuration`` call. It enables all endpoints used
26762306a36Sopenharmony_ci   in that configuration, with all interfaces in their default settings.
26862306a36Sopenharmony_ci   That involves using a list of the hardware's endpoints, enabling each
26962306a36Sopenharmony_ci   endpoint according to its descriptor. It may also involve using
27062306a36Sopenharmony_ci   ``usb_gadget_vbus_draw`` to let more power be drawn from VBUS, as
27162306a36Sopenharmony_ci   allowed by that configuration. For OTG devices, setting a
27262306a36Sopenharmony_ci   configuration may also involve reporting HNP capabilities through a
27362306a36Sopenharmony_ci   user interface.
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_ci6. Do real work and perform data transfers, possibly involving changes
27662306a36Sopenharmony_ci   to interface settings or switching to new configurations, until the
27762306a36Sopenharmony_ci   device is disconnect()ed from the host. Queue any number of transfer
27862306a36Sopenharmony_ci   requests to each endpoint. It may be suspended and resumed several
27962306a36Sopenharmony_ci   times before being disconnected. On disconnect, the drivers go back
28062306a36Sopenharmony_ci   to step 3 (above).
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ci7. When the gadget driver module is being unloaded, the driver unbind()
28362306a36Sopenharmony_ci   callback is issued. That lets the controller driver be unloaded.
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_ciDrivers will normally be arranged so that just loading the gadget driver
28662306a36Sopenharmony_cimodule (or statically linking it into a Linux kernel) allows the
28762306a36Sopenharmony_ciperipheral device to be enumerated, but some drivers will defer
28862306a36Sopenharmony_cienumeration until some higher level component (like a user mode daemon)
28962306a36Sopenharmony_cienables it. Note that at this lowest level there are no policies about
29062306a36Sopenharmony_cihow ep0 configuration logic is implemented, except that it should obey
29162306a36Sopenharmony_ciUSB specifications. Such issues are in the domain of gadget drivers,
29262306a36Sopenharmony_ciincluding knowing about implementation constraints imposed by some USB
29362306a36Sopenharmony_cicontrollers or understanding that composite devices might happen to be
29462306a36Sopenharmony_cibuilt by integrating reusable components.
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_ciNote that the lifecycle above can be slightly different for OTG devices.
29762306a36Sopenharmony_ciOther than providing an additional OTG descriptor in each configuration,
29862306a36Sopenharmony_cionly the HNP-related differences are particularly visible to driver
29962306a36Sopenharmony_cicode. They involve reporting requirements during the ``SET_CONFIGURATION``
30062306a36Sopenharmony_cirequest, and the option to invoke HNP during some suspend callbacks.
30162306a36Sopenharmony_ciAlso, SRP changes the semantics of ``usb_gadget_wakeup`` slightly.
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ciUSB 2.0 Chapter 9 Types and Constants
30462306a36Sopenharmony_ci-------------------------------------
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_ciGadget drivers rely on common USB structures and constants defined in
30762306a36Sopenharmony_cithe :ref:`linux/usb/ch9.h <usb_chapter9>` header file, which is standard in
30862306a36Sopenharmony_ciLinux 2.6+ kernels. These are the same types and constants used by host side
30962306a36Sopenharmony_cidrivers (and usbcore).
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_ciCore Objects and Methods
31262306a36Sopenharmony_ci------------------------
31362306a36Sopenharmony_ci
31462306a36Sopenharmony_ciThese are declared in ``<linux/usb/gadget.h>``, and are used by gadget
31562306a36Sopenharmony_cidrivers to interact with USB peripheral controller drivers.
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_ci.. kernel-doc:: include/linux/usb/gadget.h
31862306a36Sopenharmony_ci   :internal:
31962306a36Sopenharmony_ci
32062306a36Sopenharmony_ciOptional Utilities
32162306a36Sopenharmony_ci------------------
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_ciThe core API is sufficient for writing a USB Gadget Driver, but some
32462306a36Sopenharmony_cioptional utilities are provided to simplify common tasks. These
32562306a36Sopenharmony_ciutilities include endpoint autoconfiguration.
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_ci.. kernel-doc:: drivers/usb/gadget/usbstring.c
32862306a36Sopenharmony_ci   :export:
32962306a36Sopenharmony_ci
33062306a36Sopenharmony_ci.. kernel-doc:: drivers/usb/gadget/config.c
33162306a36Sopenharmony_ci   :export:
33262306a36Sopenharmony_ci
33362306a36Sopenharmony_ciComposite Device Framework
33462306a36Sopenharmony_ci--------------------------
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_ciThe core API is sufficient for writing drivers for composite USB devices
33762306a36Sopenharmony_ci(with more than one function in a given configuration), and also
33862306a36Sopenharmony_cimulti-configuration devices (also more than one function, but not
33962306a36Sopenharmony_cinecessarily sharing a given configuration). There is however an optional
34062306a36Sopenharmony_ciframework which makes it easier to reuse and combine functions.
34162306a36Sopenharmony_ci
34262306a36Sopenharmony_ciDevices using this framework provide a struct usb_composite_driver,
34362306a36Sopenharmony_ciwhich in turn provides one or more struct usb_configuration
34462306a36Sopenharmony_ciinstances. Each such configuration includes at least one struct
34562306a36Sopenharmony_ci:c:type:`usb_function`, which packages a user visible role such as "network
34662306a36Sopenharmony_cilink" or "mass storage device". Management functions may also exist,
34762306a36Sopenharmony_cisuch as "Device Firmware Upgrade".
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_ci.. kernel-doc:: include/linux/usb/composite.h
35062306a36Sopenharmony_ci   :internal:
35162306a36Sopenharmony_ci
35262306a36Sopenharmony_ci.. kernel-doc:: drivers/usb/gadget/composite.c
35362306a36Sopenharmony_ci   :export:
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ciComposite Device Functions
35662306a36Sopenharmony_ci--------------------------
35762306a36Sopenharmony_ci
35862306a36Sopenharmony_ciAt this writing, a few of the current gadget drivers have been converted
35962306a36Sopenharmony_cito this framework. Near-term plans include converting all of them,
36062306a36Sopenharmony_ciexcept for ``gadgetfs``.
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_ciPeripheral Controller Drivers
36362306a36Sopenharmony_ci=============================
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_ciThe first hardware supporting this API was the NetChip 2280 controller,
36662306a36Sopenharmony_ciwhich supports USB 2.0 high speed and is based on PCI. This is the
36762306a36Sopenharmony_ci``net2280`` driver module. The driver supports Linux kernel versions 2.4
36862306a36Sopenharmony_ciand 2.6; contact NetChip Technologies for development boards and product
36962306a36Sopenharmony_ciinformation.
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_ciOther hardware working in the ``gadget`` framework includes: Intel's PXA
37262306a36Sopenharmony_ci25x and IXP42x series processors (``pxa2xx_udc``), Toshiba TC86c001
37362306a36Sopenharmony_ci"Goku-S" (``goku_udc``), Renesas SH7705/7727 (``sh_udc``), MediaQ 11xx
37462306a36Sopenharmony_ci(``mq11xx_udc``), Hynix HMS30C7202 (``h7202_udc``), National 9303/4
37562306a36Sopenharmony_ci(``n9604_udc``), Texas Instruments OMAP (``omap_udc``), Sharp LH7A40x
37662306a36Sopenharmony_ci(``lh7a40x_udc``), and more. Most of those are full speed controllers.
37762306a36Sopenharmony_ci
37862306a36Sopenharmony_ciAt this writing, there are people at work on drivers in this framework
37962306a36Sopenharmony_cifor several other USB device controllers, with plans to make many of
38062306a36Sopenharmony_cithem be widely available.
38162306a36Sopenharmony_ci
38262306a36Sopenharmony_ciA partial USB simulator, the ``dummy_hcd`` driver, is available. It can
38362306a36Sopenharmony_ciact like a net2280, a pxa25x, or an sa11x0 in terms of available
38462306a36Sopenharmony_ciendpoints and device speeds; and it simulates control, bulk, and to some
38562306a36Sopenharmony_ciextent interrupt transfers. That lets you develop some parts of a gadget
38662306a36Sopenharmony_cidriver on a normal PC, without any special hardware, and perhaps with
38762306a36Sopenharmony_cithe assistance of tools such as GDB running with User Mode Linux. At
38862306a36Sopenharmony_cileast one person has expressed interest in adapting that approach,
38962306a36Sopenharmony_cihooking it up to a simulator for a microcontroller. Such simulators can
39062306a36Sopenharmony_cihelp debug subsystems where the runtime hardware is unfriendly to
39162306a36Sopenharmony_cisoftware development, or is not yet available.
39262306a36Sopenharmony_ci
39362306a36Sopenharmony_ciSupport for other controllers is expected to be developed and
39462306a36Sopenharmony_cicontributed over time, as this driver framework evolves.
39562306a36Sopenharmony_ci
39662306a36Sopenharmony_ciGadget Drivers
39762306a36Sopenharmony_ci==============
39862306a36Sopenharmony_ci
39962306a36Sopenharmony_ciIn addition to *Gadget Zero* (used primarily for testing and development
40062306a36Sopenharmony_ciwith drivers for usb controller hardware), other gadget drivers exist.
40162306a36Sopenharmony_ci
40262306a36Sopenharmony_ciThere's an ``ethernet`` gadget driver, which implements one of the most
40362306a36Sopenharmony_ciuseful *Communications Device Class* (CDC) models. One of the standards
40462306a36Sopenharmony_cifor cable modem interoperability even specifies the use of this ethernet
40562306a36Sopenharmony_cimodel as one of two mandatory options. Gadgets using this code look to a
40662306a36Sopenharmony_ciUSB host as if they're an Ethernet adapter. It provides access to a
40762306a36Sopenharmony_cinetwork where the gadget's CPU is one host, which could easily be
40862306a36Sopenharmony_cibridging, routing, or firewalling access to other networks. Since some
40962306a36Sopenharmony_cihardware can't fully implement the CDC Ethernet requirements, this
41062306a36Sopenharmony_cidriver also implements a "good parts only" subset of CDC Ethernet. (That
41162306a36Sopenharmony_cisubset doesn't advertise itself as CDC Ethernet, to avoid creating
41262306a36Sopenharmony_ciproblems.)
41362306a36Sopenharmony_ci
41462306a36Sopenharmony_ciSupport for Microsoft's ``RNDIS`` protocol has been contributed by
41562306a36Sopenharmony_ciPengutronix and Auerswald GmbH. This is like CDC Ethernet, but it runs
41662306a36Sopenharmony_cion more slightly USB hardware (but less than the CDC subset). However,
41762306a36Sopenharmony_ciits main claim to fame is being able to connect directly to recent
41862306a36Sopenharmony_civersions of Windows, using drivers that Microsoft bundles and supports,
41962306a36Sopenharmony_cimaking it much simpler to network with Windows.
42062306a36Sopenharmony_ci
42162306a36Sopenharmony_ciThere is also support for user mode gadget drivers, using ``gadgetfs``.
42262306a36Sopenharmony_ciThis provides a *User Mode API* that presents each endpoint as a single
42362306a36Sopenharmony_cifile descriptor. I/O is done using normal ``read()`` and ``read()`` calls.
42462306a36Sopenharmony_ciFamiliar tools like GDB and pthreads can be used to develop and debug
42562306a36Sopenharmony_ciuser mode drivers, so that once a robust controller driver is available
42662306a36Sopenharmony_cimany applications for it won't require new kernel mode software. Linux
42762306a36Sopenharmony_ci2.6 *Async I/O (AIO)* support is available, so that user mode software
42862306a36Sopenharmony_cican stream data with only slightly more overhead than a kernel driver.
42962306a36Sopenharmony_ci
43062306a36Sopenharmony_ciThere's a USB Mass Storage class driver, which provides a different
43162306a36Sopenharmony_cisolution for interoperability with systems such as MS-Windows and MacOS.
43262306a36Sopenharmony_ciThat *Mass Storage* driver uses a file or block device as backing store
43362306a36Sopenharmony_cifor a drive, like the ``loop`` driver. The USB host uses the BBB, CB, or
43462306a36Sopenharmony_ciCBI versions of the mass storage class specification, using transparent
43562306a36Sopenharmony_ciSCSI commands to access the data from the backing store.
43662306a36Sopenharmony_ci
43762306a36Sopenharmony_ciThere's a "serial line" driver, useful for TTY style operation over USB.
43862306a36Sopenharmony_ciThe latest version of that driver supports CDC ACM style operation, like
43962306a36Sopenharmony_cia USB modem, and so on most hardware it can interoperate easily with
44062306a36Sopenharmony_ciMS-Windows. One interesting use of that driver is in boot firmware (like
44162306a36Sopenharmony_cia BIOS), which can sometimes use that model with very small systems
44262306a36Sopenharmony_ciwithout real serial lines.
44362306a36Sopenharmony_ci
44462306a36Sopenharmony_ciSupport for other kinds of gadget is expected to be developed and
44562306a36Sopenharmony_cicontributed over time, as this driver framework evolves.
44662306a36Sopenharmony_ci
44762306a36Sopenharmony_ciUSB On-The-GO (OTG)
44862306a36Sopenharmony_ci===================
44962306a36Sopenharmony_ci
45062306a36Sopenharmony_ciUSB OTG support on Linux 2.6 was initially developed by Texas
45162306a36Sopenharmony_ciInstruments for `OMAP <http://www.omap.com>`__ 16xx and 17xx series
45262306a36Sopenharmony_ciprocessors. Other OTG systems should work in similar ways, but the
45362306a36Sopenharmony_cihardware level details could be very different.
45462306a36Sopenharmony_ci
45562306a36Sopenharmony_ciSystems need specialized hardware support to implement OTG, notably
45662306a36Sopenharmony_ciincluding a special *Mini-AB* jack and associated transceiver to support
45762306a36Sopenharmony_ci*Dual-Role* operation: they can act either as a host, using the standard
45862306a36Sopenharmony_ciLinux-USB host side driver stack, or as a peripheral, using this
45962306a36Sopenharmony_ci``gadget`` framework. To do that, the system software relies on small
46062306a36Sopenharmony_ciadditions to those programming interfaces, and on a new internal
46162306a36Sopenharmony_cicomponent (here called an "OTG Controller") affecting which driver stack
46262306a36Sopenharmony_ciconnects to the OTG port. In each role, the system can re-use the
46362306a36Sopenharmony_ciexisting pool of hardware-neutral drivers, layered on top of the
46462306a36Sopenharmony_cicontroller driver interfaces (:c:type:`usb_bus` or :c:type:`usb_gadget`).
46562306a36Sopenharmony_ciSuch drivers need at most minor changes, and most of the calls added to
46662306a36Sopenharmony_cisupport OTG can also benefit non-OTG products.
46762306a36Sopenharmony_ci
46862306a36Sopenharmony_ci-  Gadget drivers test the ``is_otg`` flag, and use it to determine
46962306a36Sopenharmony_ci   whether or not to include an OTG descriptor in each of their
47062306a36Sopenharmony_ci   configurations.
47162306a36Sopenharmony_ci
47262306a36Sopenharmony_ci-  Gadget drivers may need changes to support the two new OTG protocols,
47362306a36Sopenharmony_ci   exposed in new gadget attributes such as ``b_hnp_enable`` flag. HNP
47462306a36Sopenharmony_ci   support should be reported through a user interface (two LEDs could
47562306a36Sopenharmony_ci   suffice), and is triggered in some cases when the host suspends the
47662306a36Sopenharmony_ci   peripheral. SRP support can be user-initiated just like remote
47762306a36Sopenharmony_ci   wakeup, probably by pressing the same button.
47862306a36Sopenharmony_ci
47962306a36Sopenharmony_ci-  On the host side, USB device drivers need to be taught to trigger HNP
48062306a36Sopenharmony_ci   at appropriate moments, using ``usb_suspend_device()``. That also
48162306a36Sopenharmony_ci   conserves battery power, which is useful even for non-OTG
48262306a36Sopenharmony_ci   configurations.
48362306a36Sopenharmony_ci
48462306a36Sopenharmony_ci-  Also on the host side, a driver must support the OTG "Targeted
48562306a36Sopenharmony_ci   Peripheral List". That's just a whitelist, used to reject peripherals
48662306a36Sopenharmony_ci   not supported with a given Linux OTG host. *This whitelist is
48762306a36Sopenharmony_ci   product-specific; each product must modify* ``otg_whitelist.h`` *to
48862306a36Sopenharmony_ci   match its interoperability specification.*
48962306a36Sopenharmony_ci
49062306a36Sopenharmony_ci   Non-OTG Linux hosts, like PCs and workstations, normally have some
49162306a36Sopenharmony_ci   solution for adding drivers, so that peripherals that aren't
49262306a36Sopenharmony_ci   recognized can eventually be supported. That approach is unreasonable
49362306a36Sopenharmony_ci   for consumer products that may never have their firmware upgraded,
49462306a36Sopenharmony_ci   and where it's usually unrealistic to expect traditional
49562306a36Sopenharmony_ci   PC/workstation/server kinds of support model to work. For example,
49662306a36Sopenharmony_ci   it's often impractical to change device firmware once the product has
49762306a36Sopenharmony_ci   been distributed, so driver bugs can't normally be fixed if they're
49862306a36Sopenharmony_ci   found after shipment.
49962306a36Sopenharmony_ci
50062306a36Sopenharmony_ciAdditional changes are needed below those hardware-neutral :c:type:`usb_bus`
50162306a36Sopenharmony_ciand :c:type:`usb_gadget` driver interfaces; those aren't discussed here in any
50262306a36Sopenharmony_cidetail. Those affect the hardware-specific code for each USB Host or
50362306a36Sopenharmony_ciPeripheral controller, and how the HCD initializes (since OTG can be
50462306a36Sopenharmony_ciactive only on a single port). They also involve what may be called an
50562306a36Sopenharmony_ci*OTG Controller Driver*, managing the OTG transceiver and the OTG state
50662306a36Sopenharmony_cimachine logic as well as much of the root hub behavior for the OTG port.
50762306a36Sopenharmony_ciThe OTG controller driver needs to activate and deactivate USB
50862306a36Sopenharmony_cicontrollers depending on the relevant device role. Some related changes
50962306a36Sopenharmony_ciwere needed inside usbcore, so that it can identify OTG-capable devices
51062306a36Sopenharmony_ciand respond appropriately to HNP or SRP protocols.
511