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