1*4882a593Smuzhiyun======================== 2*4882a593SmuzhiyunUSB Gadget API for Linux 3*4882a593Smuzhiyun======================== 4*4882a593Smuzhiyun 5*4882a593Smuzhiyun:Author: David Brownell 6*4882a593Smuzhiyun:Date: 20 August 2004 7*4882a593Smuzhiyun 8*4882a593SmuzhiyunIntroduction 9*4882a593Smuzhiyun============ 10*4882a593Smuzhiyun 11*4882a593SmuzhiyunThis document presents a Linux-USB "Gadget" kernel mode API, for use 12*4882a593Smuzhiyunwithin peripherals and other USB devices that embed Linux. It provides 13*4882a593Smuzhiyunan overview of the API structure, and shows how that fits into a system 14*4882a593Smuzhiyundevelopment project. This is the first such API released on Linux to 15*4882a593Smuzhiyunaddress a number of important problems, including: 16*4882a593Smuzhiyun 17*4882a593Smuzhiyun- Supports USB 2.0, for high speed devices which can stream data at 18*4882a593Smuzhiyun several dozen megabytes per second. 19*4882a593Smuzhiyun 20*4882a593Smuzhiyun- Handles devices with dozens of endpoints just as well as ones with 21*4882a593Smuzhiyun just two fixed-function ones. Gadget drivers can be written so 22*4882a593Smuzhiyun they're easy to port to new hardware. 23*4882a593Smuzhiyun 24*4882a593Smuzhiyun- Flexible enough to expose more complex USB device capabilities such 25*4882a593Smuzhiyun as multiple configurations, multiple interfaces, composite devices, 26*4882a593Smuzhiyun and alternate interface settings. 27*4882a593Smuzhiyun 28*4882a593Smuzhiyun- USB "On-The-Go" (OTG) support, in conjunction with updates to the 29*4882a593Smuzhiyun Linux-USB host side. 30*4882a593Smuzhiyun 31*4882a593Smuzhiyun- Sharing data structures and API models with the Linux-USB host side 32*4882a593Smuzhiyun API. This helps the OTG support, and looks forward to more-symmetric 33*4882a593Smuzhiyun frameworks (where the same I/O model is used by both host and device 34*4882a593Smuzhiyun side drivers). 35*4882a593Smuzhiyun 36*4882a593Smuzhiyun- Minimalist, so it's easier to support new device controller hardware. 37*4882a593Smuzhiyun I/O processing doesn't imply large demands for memory or CPU 38*4882a593Smuzhiyun resources. 39*4882a593Smuzhiyun 40*4882a593SmuzhiyunMost Linux developers will not be able to use this API, since they have 41*4882a593SmuzhiyunUSB ``host`` hardware in a PC, workstation, or server. Linux users with 42*4882a593Smuzhiyunembedded systems are more likely to have USB peripheral hardware. To 43*4882a593Smuzhiyundistinguish drivers running inside such hardware from the more familiar 44*4882a593SmuzhiyunLinux "USB device drivers", which are host side proxies for the real USB 45*4882a593Smuzhiyundevices, a different term is used: the drivers inside the peripherals 46*4882a593Smuzhiyunare "USB gadget drivers". In USB protocol interactions, the device 47*4882a593Smuzhiyundriver is the master (or "client driver") and the gadget driver is the 48*4882a593Smuzhiyunslave (or "function driver"). 49*4882a593Smuzhiyun 50*4882a593SmuzhiyunThe gadget API resembles the host side Linux-USB API in that both use 51*4882a593Smuzhiyunqueues of request objects to package I/O buffers, and those requests may 52*4882a593Smuzhiyunbe submitted or canceled. They share common definitions for the standard 53*4882a593SmuzhiyunUSB *Chapter 9* messages, structures, and constants. Also, both APIs 54*4882a593Smuzhiyunbind and unbind drivers to devices. The APIs differ in detail, since the 55*4882a593Smuzhiyunhost side's current URB framework exposes a number of implementation 56*4882a593Smuzhiyundetails and assumptions that are inappropriate for a gadget API. While 57*4882a593Smuzhiyunthe model for control transfers and configuration management is 58*4882a593Smuzhiyunnecessarily different (one side is a hardware-neutral master, the other 59*4882a593Smuzhiyunis a hardware-aware slave), the endpoint I/0 API used here should also 60*4882a593Smuzhiyunbe usable for an overhead-reduced host side API. 61*4882a593Smuzhiyun 62*4882a593SmuzhiyunStructure of Gadget Drivers 63*4882a593Smuzhiyun=========================== 64*4882a593Smuzhiyun 65*4882a593SmuzhiyunA system running inside a USB peripheral normally has at least three 66*4882a593Smuzhiyunlayers inside the kernel to handle USB protocol processing, and may have 67*4882a593Smuzhiyunadditional layers in user space code. The ``gadget`` API is used by the 68*4882a593Smuzhiyunmiddle layer to interact with the lowest level (which directly handles 69*4882a593Smuzhiyunhardware). 70*4882a593Smuzhiyun 71*4882a593SmuzhiyunIn Linux, from the bottom up, these layers are: 72*4882a593Smuzhiyun 73*4882a593Smuzhiyun*USB Controller Driver* 74*4882a593Smuzhiyun This is the lowest software level. It is the only layer that talks 75*4882a593Smuzhiyun to hardware, through registers, fifos, dma, irqs, and the like. The 76*4882a593Smuzhiyun ``<linux/usb/gadget.h>`` API abstracts the peripheral controller 77*4882a593Smuzhiyun endpoint hardware. That hardware is exposed through endpoint 78*4882a593Smuzhiyun objects, which accept streams of IN/OUT buffers, and through 79*4882a593Smuzhiyun callbacks that interact with gadget drivers. Since normal USB 80*4882a593Smuzhiyun devices only have one upstream port, they only have one of these 81*4882a593Smuzhiyun drivers. The controller driver can support any number of different 82*4882a593Smuzhiyun gadget drivers, but only one of them can be used at a time. 83*4882a593Smuzhiyun 84*4882a593Smuzhiyun Examples of such controller hardware include the PCI-based NetChip 85*4882a593Smuzhiyun 2280 USB 2.0 high speed controller, the SA-11x0 or PXA-25x UDC 86*4882a593Smuzhiyun (found within many PDAs), and a variety of other products. 87*4882a593Smuzhiyun 88*4882a593Smuzhiyun*Gadget Driver* 89*4882a593Smuzhiyun The lower boundary of this driver implements hardware-neutral USB 90*4882a593Smuzhiyun functions, using calls to the controller driver. Because such 91*4882a593Smuzhiyun hardware varies widely in capabilities and restrictions, and is used 92*4882a593Smuzhiyun in embedded environments where space is at a premium, the gadget 93*4882a593Smuzhiyun driver is often configured at compile time to work with endpoints 94*4882a593Smuzhiyun supported by one particular controller. Gadget drivers may be 95*4882a593Smuzhiyun portable to several different controllers, using conditional 96*4882a593Smuzhiyun compilation. (Recent kernels substantially simplify the work 97*4882a593Smuzhiyun involved in supporting new hardware, by *autoconfiguring* endpoints 98*4882a593Smuzhiyun automatically for many bulk-oriented drivers.) Gadget driver 99*4882a593Smuzhiyun responsibilities include: 100*4882a593Smuzhiyun 101*4882a593Smuzhiyun - handling setup requests (ep0 protocol responses) possibly 102*4882a593Smuzhiyun including class-specific functionality 103*4882a593Smuzhiyun 104*4882a593Smuzhiyun - returning configuration and string descriptors 105*4882a593Smuzhiyun 106*4882a593Smuzhiyun - (re)setting configurations and interface altsettings, including 107*4882a593Smuzhiyun enabling and configuring endpoints 108*4882a593Smuzhiyun 109*4882a593Smuzhiyun - handling life cycle events, such as managing bindings to 110*4882a593Smuzhiyun hardware, USB suspend/resume, remote wakeup, and disconnection 111*4882a593Smuzhiyun from the USB host. 112*4882a593Smuzhiyun 113*4882a593Smuzhiyun - managing IN and OUT transfers on all currently enabled endpoints 114*4882a593Smuzhiyun 115*4882a593Smuzhiyun Such drivers may be modules of proprietary code, although that 116*4882a593Smuzhiyun approach is discouraged in the Linux community. 117*4882a593Smuzhiyun 118*4882a593Smuzhiyun*Upper Level* 119*4882a593Smuzhiyun Most gadget drivers have an upper boundary that connects to some 120*4882a593Smuzhiyun Linux driver or framework in Linux. Through that boundary flows the 121*4882a593Smuzhiyun data which the gadget driver produces and/or consumes through 122*4882a593Smuzhiyun protocol transfers over USB. Examples include: 123*4882a593Smuzhiyun 124*4882a593Smuzhiyun - user mode code, using generic (gadgetfs) or application specific 125*4882a593Smuzhiyun files in ``/dev`` 126*4882a593Smuzhiyun 127*4882a593Smuzhiyun - networking subsystem (for network gadgets, like the CDC Ethernet 128*4882a593Smuzhiyun Model gadget driver) 129*4882a593Smuzhiyun 130*4882a593Smuzhiyun - data capture drivers, perhaps video4Linux or a scanner driver; or 131*4882a593Smuzhiyun test and measurement hardware. 132*4882a593Smuzhiyun 133*4882a593Smuzhiyun - input subsystem (for HID gadgets) 134*4882a593Smuzhiyun 135*4882a593Smuzhiyun - sound subsystem (for audio gadgets) 136*4882a593Smuzhiyun 137*4882a593Smuzhiyun - file system (for PTP gadgets) 138*4882a593Smuzhiyun 139*4882a593Smuzhiyun - block i/o subsystem (for usb-storage gadgets) 140*4882a593Smuzhiyun 141*4882a593Smuzhiyun - ... and more 142*4882a593Smuzhiyun 143*4882a593Smuzhiyun*Additional Layers* 144*4882a593Smuzhiyun Other layers may exist. These could include kernel layers, such as 145*4882a593Smuzhiyun network protocol stacks, as well as user mode applications building 146*4882a593Smuzhiyun on standard POSIX system call APIs such as ``open()``, ``close()``, 147*4882a593Smuzhiyun ``read()`` and ``write()``. On newer systems, POSIX Async I/O calls may 148*4882a593Smuzhiyun be an option. Such user mode code will not necessarily be subject to 149*4882a593Smuzhiyun the GNU General Public License (GPL). 150*4882a593Smuzhiyun 151*4882a593SmuzhiyunOTG-capable systems will also need to include a standard Linux-USB host 152*4882a593Smuzhiyunside stack, with ``usbcore``, one or more *Host Controller Drivers* 153*4882a593Smuzhiyun(HCDs), *USB Device Drivers* to support the OTG "Targeted Peripheral 154*4882a593SmuzhiyunList", and so forth. There will also be an *OTG Controller Driver*, 155*4882a593Smuzhiyunwhich is visible to gadget and device driver developers only indirectly. 156*4882a593SmuzhiyunThat helps the host and device side USB controllers implement the two 157*4882a593Smuzhiyunnew OTG protocols (HNP and SRP). Roles switch (host to peripheral, or 158*4882a593Smuzhiyunvice versa) using HNP during USB suspend processing, and SRP can be 159*4882a593Smuzhiyunviewed as a more battery-friendly kind of device wakeup protocol. 160*4882a593Smuzhiyun 161*4882a593SmuzhiyunOver time, reusable utilities are evolving to help make some gadget 162*4882a593Smuzhiyundriver tasks simpler. For example, building configuration descriptors 163*4882a593Smuzhiyunfrom vectors of descriptors for the configurations interfaces and 164*4882a593Smuzhiyunendpoints is now automated, and many drivers now use autoconfiguration 165*4882a593Smuzhiyunto choose hardware endpoints and initialize their descriptors. A 166*4882a593Smuzhiyunpotential example of particular interest is code implementing standard 167*4882a593SmuzhiyunUSB-IF protocols for HID, networking, storage, or audio classes. Some 168*4882a593Smuzhiyundevelopers are interested in KDB or KGDB hooks, to let target hardware 169*4882a593Smuzhiyunbe remotely debugged. Most such USB protocol code doesn't need to be 170*4882a593Smuzhiyunhardware-specific, any more than network protocols like X11, HTTP, or 171*4882a593SmuzhiyunNFS are. Such gadget-side interface drivers should eventually be 172*4882a593Smuzhiyuncombined, to implement composite devices. 173*4882a593Smuzhiyun 174*4882a593SmuzhiyunKernel Mode Gadget API 175*4882a593Smuzhiyun====================== 176*4882a593Smuzhiyun 177*4882a593SmuzhiyunGadget drivers declare themselves through a struct 178*4882a593Smuzhiyun:c:type:`usb_gadget_driver`, which is responsible for most parts of enumeration 179*4882a593Smuzhiyunfor a struct usb_gadget. The response to a set_configuration usually 180*4882a593Smuzhiyuninvolves enabling one or more of the struct usb_ep objects exposed by 181*4882a593Smuzhiyunthe gadget, and submitting one or more struct usb_request buffers to 182*4882a593Smuzhiyuntransfer data. Understand those four data types, and their operations, 183*4882a593Smuzhiyunand you will understand how this API works. 184*4882a593Smuzhiyun 185*4882a593Smuzhiyun.. Note:: 186*4882a593Smuzhiyun 187*4882a593Smuzhiyun Other than the "Chapter 9" data types, most of the significant data 188*4882a593Smuzhiyun types and functions are described here. 189*4882a593Smuzhiyun 190*4882a593Smuzhiyun However, some relevant information is likely omitted from what you 191*4882a593Smuzhiyun are reading. One example of such information is endpoint 192*4882a593Smuzhiyun autoconfiguration. You'll have to read the header file, and use 193*4882a593Smuzhiyun example source code (such as that for "Gadget Zero"), to fully 194*4882a593Smuzhiyun understand the API. 195*4882a593Smuzhiyun 196*4882a593Smuzhiyun The part of the API implementing some basic driver capabilities is 197*4882a593Smuzhiyun specific to the version of the Linux kernel that's in use. The 2.6 198*4882a593Smuzhiyun and upper kernel versions include a *driver model* framework that has 199*4882a593Smuzhiyun no analogue on earlier kernels; so those parts of the gadget API are 200*4882a593Smuzhiyun not fully portable. (They are implemented on 2.4 kernels, but in a 201*4882a593Smuzhiyun different way.) The driver model state is another part of this API that is 202*4882a593Smuzhiyun ignored by the kerneldoc tools. 203*4882a593Smuzhiyun 204*4882a593SmuzhiyunThe core API does not expose every possible hardware feature, only the 205*4882a593Smuzhiyunmost widely available ones. There are significant hardware features, 206*4882a593Smuzhiyunsuch as device-to-device DMA (without temporary storage in a memory 207*4882a593Smuzhiyunbuffer) that would be added using hardware-specific APIs. 208*4882a593Smuzhiyun 209*4882a593SmuzhiyunThis API allows drivers to use conditional compilation to handle 210*4882a593Smuzhiyunendpoint capabilities of different hardware, but doesn't require that. 211*4882a593SmuzhiyunHardware tends to have arbitrary restrictions, relating to transfer 212*4882a593Smuzhiyuntypes, addressing, packet sizes, buffering, and availability. As a rule, 213*4882a593Smuzhiyunsuch differences only matter for "endpoint zero" logic that handles 214*4882a593Smuzhiyundevice configuration and management. The API supports limited run-time 215*4882a593Smuzhiyundetection of capabilities, through naming conventions for endpoints. 216*4882a593SmuzhiyunMany drivers will be able to at least partially autoconfigure 217*4882a593Smuzhiyunthemselves. In particular, driver init sections will often have endpoint 218*4882a593Smuzhiyunautoconfiguration logic that scans the hardware's list of endpoints to 219*4882a593Smuzhiyunfind ones matching the driver requirements (relying on those 220*4882a593Smuzhiyunconventions), to eliminate some of the most common reasons for 221*4882a593Smuzhiyunconditional compilation. 222*4882a593Smuzhiyun 223*4882a593SmuzhiyunLike the Linux-USB host side API, this API exposes the "chunky" nature 224*4882a593Smuzhiyunof USB messages: I/O requests are in terms of one or more "packets", and 225*4882a593Smuzhiyunpacket boundaries are visible to drivers. Compared to RS-232 serial 226*4882a593Smuzhiyunprotocols, USB resembles synchronous protocols like HDLC (N bytes per 227*4882a593Smuzhiyunframe, multipoint addressing, host as the primary station and devices as 228*4882a593Smuzhiyunsecondary stations) more than asynchronous ones (tty style: 8 data bits 229*4882a593Smuzhiyunper frame, no parity, one stop bit). So for example the controller 230*4882a593Smuzhiyundrivers won't buffer two single byte writes into a single two-byte USB 231*4882a593SmuzhiyunIN packet, although gadget drivers may do so when they implement 232*4882a593Smuzhiyunprotocols where packet boundaries (and "short packets") are not 233*4882a593Smuzhiyunsignificant. 234*4882a593Smuzhiyun 235*4882a593SmuzhiyunDriver Life Cycle 236*4882a593Smuzhiyun----------------- 237*4882a593Smuzhiyun 238*4882a593SmuzhiyunGadget drivers make endpoint I/O requests to hardware without needing to 239*4882a593Smuzhiyunknow many details of the hardware, but driver setup/configuration code 240*4882a593Smuzhiyunneeds to handle some differences. Use the API like this: 241*4882a593Smuzhiyun 242*4882a593Smuzhiyun1. Register a driver for the particular device side usb controller 243*4882a593Smuzhiyun hardware, such as the net2280 on PCI (USB 2.0), sa11x0 or pxa25x as 244*4882a593Smuzhiyun found in Linux PDAs, and so on. At this point the device is logically 245*4882a593Smuzhiyun in the USB ch9 initial state (``attached``), drawing no power and not 246*4882a593Smuzhiyun usable (since it does not yet support enumeration). Any host should 247*4882a593Smuzhiyun not see the device, since it's not activated the data line pullup 248*4882a593Smuzhiyun used by the host to detect a device, even if VBUS power is available. 249*4882a593Smuzhiyun 250*4882a593Smuzhiyun2. Register a gadget driver that implements some higher level device 251*4882a593Smuzhiyun function. That will then bind() to a :c:type:`usb_gadget`, which activates 252*4882a593Smuzhiyun the data line pullup sometime after detecting VBUS. 253*4882a593Smuzhiyun 254*4882a593Smuzhiyun3. The hardware driver can now start enumerating. The steps it handles 255*4882a593Smuzhiyun are to accept USB ``power`` and ``set_address`` requests. Other steps are 256*4882a593Smuzhiyun handled by the gadget driver. If the gadget driver module is unloaded 257*4882a593Smuzhiyun before the host starts to enumerate, steps before step 7 are skipped. 258*4882a593Smuzhiyun 259*4882a593Smuzhiyun4. The gadget driver's ``setup()`` call returns usb descriptors, based both 260*4882a593Smuzhiyun on what the bus interface hardware provides and on the functionality 261*4882a593Smuzhiyun being implemented. That can involve alternate settings or 262*4882a593Smuzhiyun configurations, unless the hardware prevents such operation. For OTG 263*4882a593Smuzhiyun devices, each configuration descriptor includes an OTG descriptor. 264*4882a593Smuzhiyun 265*4882a593Smuzhiyun5. The gadget driver handles the last step of enumeration, when the USB 266*4882a593Smuzhiyun host issues a ``set_configuration`` call. It enables all endpoints used 267*4882a593Smuzhiyun in that configuration, with all interfaces in their default settings. 268*4882a593Smuzhiyun That involves using a list of the hardware's endpoints, enabling each 269*4882a593Smuzhiyun endpoint according to its descriptor. It may also involve using 270*4882a593Smuzhiyun ``usb_gadget_vbus_draw`` to let more power be drawn from VBUS, as 271*4882a593Smuzhiyun allowed by that configuration. For OTG devices, setting a 272*4882a593Smuzhiyun configuration may also involve reporting HNP capabilities through a 273*4882a593Smuzhiyun user interface. 274*4882a593Smuzhiyun 275*4882a593Smuzhiyun6. Do real work and perform data transfers, possibly involving changes 276*4882a593Smuzhiyun to interface settings or switching to new configurations, until the 277*4882a593Smuzhiyun device is disconnect()ed from the host. Queue any number of transfer 278*4882a593Smuzhiyun requests to each endpoint. It may be suspended and resumed several 279*4882a593Smuzhiyun times before being disconnected. On disconnect, the drivers go back 280*4882a593Smuzhiyun to step 3 (above). 281*4882a593Smuzhiyun 282*4882a593Smuzhiyun7. When the gadget driver module is being unloaded, the driver unbind() 283*4882a593Smuzhiyun callback is issued. That lets the controller driver be unloaded. 284*4882a593Smuzhiyun 285*4882a593SmuzhiyunDrivers will normally be arranged so that just loading the gadget driver 286*4882a593Smuzhiyunmodule (or statically linking it into a Linux kernel) allows the 287*4882a593Smuzhiyunperipheral device to be enumerated, but some drivers will defer 288*4882a593Smuzhiyunenumeration until some higher level component (like a user mode daemon) 289*4882a593Smuzhiyunenables it. Note that at this lowest level there are no policies about 290*4882a593Smuzhiyunhow ep0 configuration logic is implemented, except that it should obey 291*4882a593SmuzhiyunUSB specifications. Such issues are in the domain of gadget drivers, 292*4882a593Smuzhiyunincluding knowing about implementation constraints imposed by some USB 293*4882a593Smuzhiyuncontrollers or understanding that composite devices might happen to be 294*4882a593Smuzhiyunbuilt by integrating reusable components. 295*4882a593Smuzhiyun 296*4882a593SmuzhiyunNote that the lifecycle above can be slightly different for OTG devices. 297*4882a593SmuzhiyunOther than providing an additional OTG descriptor in each configuration, 298*4882a593Smuzhiyunonly the HNP-related differences are particularly visible to driver 299*4882a593Smuzhiyuncode. They involve reporting requirements during the ``SET_CONFIGURATION`` 300*4882a593Smuzhiyunrequest, and the option to invoke HNP during some suspend callbacks. 301*4882a593SmuzhiyunAlso, SRP changes the semantics of ``usb_gadget_wakeup`` slightly. 302*4882a593Smuzhiyun 303*4882a593SmuzhiyunUSB 2.0 Chapter 9 Types and Constants 304*4882a593Smuzhiyun------------------------------------- 305*4882a593Smuzhiyun 306*4882a593SmuzhiyunGadget drivers rely on common USB structures and constants defined in 307*4882a593Smuzhiyunthe :ref:`linux/usb/ch9.h <usb_chapter9>` header file, which is standard in 308*4882a593SmuzhiyunLinux 2.6+ kernels. These are the same types and constants used by host side 309*4882a593Smuzhiyundrivers (and usbcore). 310*4882a593Smuzhiyun 311*4882a593SmuzhiyunCore Objects and Methods 312*4882a593Smuzhiyun------------------------ 313*4882a593Smuzhiyun 314*4882a593SmuzhiyunThese are declared in ``<linux/usb/gadget.h>``, and are used by gadget 315*4882a593Smuzhiyundrivers to interact with USB peripheral controller drivers. 316*4882a593Smuzhiyun 317*4882a593Smuzhiyun.. kernel-doc:: include/linux/usb/gadget.h 318*4882a593Smuzhiyun :internal: 319*4882a593Smuzhiyun 320*4882a593SmuzhiyunOptional Utilities 321*4882a593Smuzhiyun------------------ 322*4882a593Smuzhiyun 323*4882a593SmuzhiyunThe core API is sufficient for writing a USB Gadget Driver, but some 324*4882a593Smuzhiyunoptional utilities are provided to simplify common tasks. These 325*4882a593Smuzhiyunutilities include endpoint autoconfiguration. 326*4882a593Smuzhiyun 327*4882a593Smuzhiyun.. kernel-doc:: drivers/usb/gadget/usbstring.c 328*4882a593Smuzhiyun :export: 329*4882a593Smuzhiyun 330*4882a593Smuzhiyun.. kernel-doc:: drivers/usb/gadget/config.c 331*4882a593Smuzhiyun :export: 332*4882a593Smuzhiyun 333*4882a593SmuzhiyunComposite Device Framework 334*4882a593Smuzhiyun-------------------------- 335*4882a593Smuzhiyun 336*4882a593SmuzhiyunThe core API is sufficient for writing drivers for composite USB devices 337*4882a593Smuzhiyun(with more than one function in a given configuration), and also 338*4882a593Smuzhiyunmulti-configuration devices (also more than one function, but not 339*4882a593Smuzhiyunnecessarily sharing a given configuration). There is however an optional 340*4882a593Smuzhiyunframework which makes it easier to reuse and combine functions. 341*4882a593Smuzhiyun 342*4882a593SmuzhiyunDevices using this framework provide a struct usb_composite_driver, 343*4882a593Smuzhiyunwhich in turn provides one or more struct usb_configuration 344*4882a593Smuzhiyuninstances. Each such configuration includes at least one struct 345*4882a593Smuzhiyun:c:type:`usb_function`, which packages a user visible role such as "network 346*4882a593Smuzhiyunlink" or "mass storage device". Management functions may also exist, 347*4882a593Smuzhiyunsuch as "Device Firmware Upgrade". 348*4882a593Smuzhiyun 349*4882a593Smuzhiyun.. kernel-doc:: include/linux/usb/composite.h 350*4882a593Smuzhiyun :internal: 351*4882a593Smuzhiyun 352*4882a593Smuzhiyun.. kernel-doc:: drivers/usb/gadget/composite.c 353*4882a593Smuzhiyun :export: 354*4882a593Smuzhiyun 355*4882a593SmuzhiyunComposite Device Functions 356*4882a593Smuzhiyun-------------------------- 357*4882a593Smuzhiyun 358*4882a593SmuzhiyunAt this writing, a few of the current gadget drivers have been converted 359*4882a593Smuzhiyunto this framework. Near-term plans include converting all of them, 360*4882a593Smuzhiyunexcept for ``gadgetfs``. 361*4882a593Smuzhiyun 362*4882a593SmuzhiyunPeripheral Controller Drivers 363*4882a593Smuzhiyun============================= 364*4882a593Smuzhiyun 365*4882a593SmuzhiyunThe first hardware supporting this API was the NetChip 2280 controller, 366*4882a593Smuzhiyunwhich supports USB 2.0 high speed and is based on PCI. This is the 367*4882a593Smuzhiyun``net2280`` driver module. The driver supports Linux kernel versions 2.4 368*4882a593Smuzhiyunand 2.6; contact NetChip Technologies for development boards and product 369*4882a593Smuzhiyuninformation. 370*4882a593Smuzhiyun 371*4882a593SmuzhiyunOther hardware working in the ``gadget`` framework includes: Intel's PXA 372*4882a593Smuzhiyun25x and IXP42x series processors (``pxa2xx_udc``), Toshiba TC86c001 373*4882a593Smuzhiyun"Goku-S" (``goku_udc``), Renesas SH7705/7727 (``sh_udc``), MediaQ 11xx 374*4882a593Smuzhiyun(``mq11xx_udc``), Hynix HMS30C7202 (``h7202_udc``), National 9303/4 375*4882a593Smuzhiyun(``n9604_udc``), Texas Instruments OMAP (``omap_udc``), Sharp LH7A40x 376*4882a593Smuzhiyun(``lh7a40x_udc``), and more. Most of those are full speed controllers. 377*4882a593Smuzhiyun 378*4882a593SmuzhiyunAt this writing, there are people at work on drivers in this framework 379*4882a593Smuzhiyunfor several other USB device controllers, with plans to make many of 380*4882a593Smuzhiyunthem be widely available. 381*4882a593Smuzhiyun 382*4882a593SmuzhiyunA partial USB simulator, the ``dummy_hcd`` driver, is available. It can 383*4882a593Smuzhiyunact like a net2280, a pxa25x, or an sa11x0 in terms of available 384*4882a593Smuzhiyunendpoints and device speeds; and it simulates control, bulk, and to some 385*4882a593Smuzhiyunextent interrupt transfers. That lets you develop some parts of a gadget 386*4882a593Smuzhiyundriver on a normal PC, without any special hardware, and perhaps with 387*4882a593Smuzhiyunthe assistance of tools such as GDB running with User Mode Linux. At 388*4882a593Smuzhiyunleast one person has expressed interest in adapting that approach, 389*4882a593Smuzhiyunhooking it up to a simulator for a microcontroller. Such simulators can 390*4882a593Smuzhiyunhelp debug subsystems where the runtime hardware is unfriendly to 391*4882a593Smuzhiyunsoftware development, or is not yet available. 392*4882a593Smuzhiyun 393*4882a593SmuzhiyunSupport for other controllers is expected to be developed and 394*4882a593Smuzhiyuncontributed over time, as this driver framework evolves. 395*4882a593Smuzhiyun 396*4882a593SmuzhiyunGadget Drivers 397*4882a593Smuzhiyun============== 398*4882a593Smuzhiyun 399*4882a593SmuzhiyunIn addition to *Gadget Zero* (used primarily for testing and development 400*4882a593Smuzhiyunwith drivers for usb controller hardware), other gadget drivers exist. 401*4882a593Smuzhiyun 402*4882a593SmuzhiyunThere's an ``ethernet`` gadget driver, which implements one of the most 403*4882a593Smuzhiyunuseful *Communications Device Class* (CDC) models. One of the standards 404*4882a593Smuzhiyunfor cable modem interoperability even specifies the use of this ethernet 405*4882a593Smuzhiyunmodel as one of two mandatory options. Gadgets using this code look to a 406*4882a593SmuzhiyunUSB host as if they're an Ethernet adapter. It provides access to a 407*4882a593Smuzhiyunnetwork where the gadget's CPU is one host, which could easily be 408*4882a593Smuzhiyunbridging, routing, or firewalling access to other networks. Since some 409*4882a593Smuzhiyunhardware can't fully implement the CDC Ethernet requirements, this 410*4882a593Smuzhiyundriver also implements a "good parts only" subset of CDC Ethernet. (That 411*4882a593Smuzhiyunsubset doesn't advertise itself as CDC Ethernet, to avoid creating 412*4882a593Smuzhiyunproblems.) 413*4882a593Smuzhiyun 414*4882a593SmuzhiyunSupport for Microsoft's ``RNDIS`` protocol has been contributed by 415*4882a593SmuzhiyunPengutronix and Auerswald GmbH. This is like CDC Ethernet, but it runs 416*4882a593Smuzhiyunon more slightly USB hardware (but less than the CDC subset). However, 417*4882a593Smuzhiyunits main claim to fame is being able to connect directly to recent 418*4882a593Smuzhiyunversions of Windows, using drivers that Microsoft bundles and supports, 419*4882a593Smuzhiyunmaking it much simpler to network with Windows. 420*4882a593Smuzhiyun 421*4882a593SmuzhiyunThere is also support for user mode gadget drivers, using ``gadgetfs``. 422*4882a593SmuzhiyunThis provides a *User Mode API* that presents each endpoint as a single 423*4882a593Smuzhiyunfile descriptor. I/O is done using normal ``read()`` and ``read()`` calls. 424*4882a593SmuzhiyunFamiliar tools like GDB and pthreads can be used to develop and debug 425*4882a593Smuzhiyunuser mode drivers, so that once a robust controller driver is available 426*4882a593Smuzhiyunmany applications for it won't require new kernel mode software. Linux 427*4882a593Smuzhiyun2.6 *Async I/O (AIO)* support is available, so that user mode software 428*4882a593Smuzhiyuncan stream data with only slightly more overhead than a kernel driver. 429*4882a593Smuzhiyun 430*4882a593SmuzhiyunThere's a USB Mass Storage class driver, which provides a different 431*4882a593Smuzhiyunsolution for interoperability with systems such as MS-Windows and MacOS. 432*4882a593SmuzhiyunThat *Mass Storage* driver uses a file or block device as backing store 433*4882a593Smuzhiyunfor a drive, like the ``loop`` driver. The USB host uses the BBB, CB, or 434*4882a593SmuzhiyunCBI versions of the mass storage class specification, using transparent 435*4882a593SmuzhiyunSCSI commands to access the data from the backing store. 436*4882a593Smuzhiyun 437*4882a593SmuzhiyunThere's a "serial line" driver, useful for TTY style operation over USB. 438*4882a593SmuzhiyunThe latest version of that driver supports CDC ACM style operation, like 439*4882a593Smuzhiyuna USB modem, and so on most hardware it can interoperate easily with 440*4882a593SmuzhiyunMS-Windows. One interesting use of that driver is in boot firmware (like 441*4882a593Smuzhiyuna BIOS), which can sometimes use that model with very small systems 442*4882a593Smuzhiyunwithout real serial lines. 443*4882a593Smuzhiyun 444*4882a593SmuzhiyunSupport for other kinds of gadget is expected to be developed and 445*4882a593Smuzhiyuncontributed over time, as this driver framework evolves. 446*4882a593Smuzhiyun 447*4882a593SmuzhiyunUSB On-The-GO (OTG) 448*4882a593Smuzhiyun=================== 449*4882a593Smuzhiyun 450*4882a593SmuzhiyunUSB OTG support on Linux 2.6 was initially developed by Texas 451*4882a593SmuzhiyunInstruments for `OMAP <http://www.omap.com>`__ 16xx and 17xx series 452*4882a593Smuzhiyunprocessors. Other OTG systems should work in similar ways, but the 453*4882a593Smuzhiyunhardware level details could be very different. 454*4882a593Smuzhiyun 455*4882a593SmuzhiyunSystems need specialized hardware support to implement OTG, notably 456*4882a593Smuzhiyunincluding a special *Mini-AB* jack and associated transceiver to support 457*4882a593Smuzhiyun*Dual-Role* operation: they can act either as a host, using the standard 458*4882a593SmuzhiyunLinux-USB host side driver stack, or as a peripheral, using this 459*4882a593Smuzhiyun``gadget`` framework. To do that, the system software relies on small 460*4882a593Smuzhiyunadditions to those programming interfaces, and on a new internal 461*4882a593Smuzhiyuncomponent (here called an "OTG Controller") affecting which driver stack 462*4882a593Smuzhiyunconnects to the OTG port. In each role, the system can re-use the 463*4882a593Smuzhiyunexisting pool of hardware-neutral drivers, layered on top of the 464*4882a593Smuzhiyuncontroller driver interfaces (:c:type:`usb_bus` or :c:type:`usb_gadget`). 465*4882a593SmuzhiyunSuch drivers need at most minor changes, and most of the calls added to 466*4882a593Smuzhiyunsupport OTG can also benefit non-OTG products. 467*4882a593Smuzhiyun 468*4882a593Smuzhiyun- Gadget drivers test the ``is_otg`` flag, and use it to determine 469*4882a593Smuzhiyun whether or not to include an OTG descriptor in each of their 470*4882a593Smuzhiyun configurations. 471*4882a593Smuzhiyun 472*4882a593Smuzhiyun- Gadget drivers may need changes to support the two new OTG protocols, 473*4882a593Smuzhiyun exposed in new gadget attributes such as ``b_hnp_enable`` flag. HNP 474*4882a593Smuzhiyun support should be reported through a user interface (two LEDs could 475*4882a593Smuzhiyun suffice), and is triggered in some cases when the host suspends the 476*4882a593Smuzhiyun peripheral. SRP support can be user-initiated just like remote 477*4882a593Smuzhiyun wakeup, probably by pressing the same button. 478*4882a593Smuzhiyun 479*4882a593Smuzhiyun- On the host side, USB device drivers need to be taught to trigger HNP 480*4882a593Smuzhiyun at appropriate moments, using ``usb_suspend_device()``. That also 481*4882a593Smuzhiyun conserves battery power, which is useful even for non-OTG 482*4882a593Smuzhiyun configurations. 483*4882a593Smuzhiyun 484*4882a593Smuzhiyun- Also on the host side, a driver must support the OTG "Targeted 485*4882a593Smuzhiyun Peripheral List". That's just a whitelist, used to reject peripherals 486*4882a593Smuzhiyun not supported with a given Linux OTG host. *This whitelist is 487*4882a593Smuzhiyun product-specific; each product must modify* ``otg_whitelist.h`` *to 488*4882a593Smuzhiyun match its interoperability specification.* 489*4882a593Smuzhiyun 490*4882a593Smuzhiyun Non-OTG Linux hosts, like PCs and workstations, normally have some 491*4882a593Smuzhiyun solution for adding drivers, so that peripherals that aren't 492*4882a593Smuzhiyun recognized can eventually be supported. That approach is unreasonable 493*4882a593Smuzhiyun for consumer products that may never have their firmware upgraded, 494*4882a593Smuzhiyun and where it's usually unrealistic to expect traditional 495*4882a593Smuzhiyun PC/workstation/server kinds of support model to work. For example, 496*4882a593Smuzhiyun it's often impractical to change device firmware once the product has 497*4882a593Smuzhiyun been distributed, so driver bugs can't normally be fixed if they're 498*4882a593Smuzhiyun found after shipment. 499*4882a593Smuzhiyun 500*4882a593SmuzhiyunAdditional changes are needed below those hardware-neutral :c:type:`usb_bus` 501*4882a593Smuzhiyunand :c:type:`usb_gadget` driver interfaces; those aren't discussed here in any 502*4882a593Smuzhiyundetail. Those affect the hardware-specific code for each USB Host or 503*4882a593SmuzhiyunPeripheral controller, and how the HCD initializes (since OTG can be 504*4882a593Smuzhiyunactive only on a single port). They also involve what may be called an 505*4882a593Smuzhiyun*OTG Controller Driver*, managing the OTG transceiver and the OTG state 506*4882a593Smuzhiyunmachine logic as well as much of the root hub behavior for the OTG port. 507*4882a593SmuzhiyunThe OTG controller driver needs to activate and deactivate USB 508*4882a593Smuzhiyuncontrollers depending on the relevant device role. Some related changes 509*4882a593Smuzhiyunwere needed inside usbcore, so that it can identify OTG-capable devices 510*4882a593Smuzhiyunand respond appropriately to HNP or SRP protocols. 511