blob: 478bfe16a19d002ddb21a0f22ae1f2271993d291 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" []>
4
5<book id="USB-Gadget-API">
6 <bookinfo>
7 <title>USB Gadget API for Linux</title>
8 <date>20 August 2004</date>
9 <edition>20 August 2004</edition>
10
11 <legalnotice>
12 <para>
13 This documentation is free software; you can redistribute
14 it and/or modify it under the terms of the GNU General Public
15 License as published by the Free Software Foundation; either
16 version 2 of the License, or (at your option) any later
17 version.
18 </para>
19
20 <para>
21 This program is distributed in the hope that it will be
22 useful, but WITHOUT ANY WARRANTY; without even the implied
23 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24 See the GNU General Public License for more details.
25 </para>
26
27 <para>
28 You should have received a copy of the GNU General Public
29 License along with this program; if not, write to the Free
30 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 MA 02111-1307 USA
32 </para>
33
34 <para>
35 For more details see the file COPYING in the source
36 distribution of Linux.
37 </para>
38 </legalnotice>
39 <copyright>
40 <year>2003-2004</year>
41 <holder>David Brownell</holder>
42 </copyright>
43
44 <author>
45 <firstname>David</firstname>
46 <surname>Brownell</surname>
47 <affiliation>
48 <address><email>dbrownell@users.sourceforge.net</email></address>
49 </affiliation>
50 </author>
51 </bookinfo>
52
53<toc></toc>
54
David Brownell741ec4e2007-04-29 19:51:05 -070055<chapter id="intro"><title>Introduction</title>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57<para>This document presents a Linux-USB "Gadget"
58kernel mode
59API, for use within peripherals and other USB devices
60that embed Linux.
61It provides an overview of the API structure,
62and shows how that fits into a system development project.
63This is the first such API released on Linux to address
64a number of important problems, including: </para>
65
66<itemizedlist>
67 <listitem><para>Supports USB 2.0, for high speed devices which
68 can stream data at several dozen megabytes per second.
69 </para></listitem>
70 <listitem><para>Handles devices with dozens of endpoints just as
71 well as ones with just two fixed-function ones. Gadget drivers
72 can be written so they're easy to port to new hardware.
73 </para></listitem>
74 <listitem><para>Flexible enough to expose more complex USB device
75 capabilities such as multiple configurations, multiple interfaces,
76 composite devices,
77 and alternate interface settings.
78 </para></listitem>
79 <listitem><para>USB "On-The-Go" (OTG) support, in conjunction
80 with updates to the Linux-USB host side.
81 </para></listitem>
82 <listitem><para>Sharing data structures and API models with the
83 Linux-USB host side API. This helps the OTG support, and
84 looks forward to more-symmetric frameworks (where the same
85 I/O model is used by both host and device side drivers).
86 </para></listitem>
87 <listitem><para>Minimalist, so it's easier to support new device
88 controller hardware. I/O processing doesn't imply large
89 demands for memory or CPU resources.
90 </para></listitem>
91</itemizedlist>
92
93
94<para>Most Linux developers will not be able to use this API, since they
95have USB "host" hardware in a PC, workstation, or server.
96Linux users with embedded systems are more likely to
97have USB peripheral hardware.
98To distinguish drivers running inside such hardware from the
99more familiar Linux "USB device drivers",
100which are host side proxies for the real USB devices,
101a different term is used:
102the drivers inside the peripherals are "USB gadget drivers".
103In USB protocol interactions, the device driver is the master
104(or "client driver")
105and the gadget driver is the slave (or "function driver").
106</para>
107
108<para>The gadget API resembles the host side Linux-USB API in that both
109use queues of request objects to package I/O buffers, and those requests
110may be submitted or canceled.
111They share common definitions for the standard USB
112<emphasis>Chapter 9</emphasis> messages, structures, and constants.
113Also, both APIs bind and unbind drivers to devices.
114The APIs differ in detail, since the host side's current
115URB framework exposes a number of implementation details
116and assumptions that are inappropriate for a gadget API.
117While the model for control transfers and configuration
118management is necessarily different (one side is a hardware-neutral master,
119the other is a hardware-aware slave), the endpoint I/0 API used here
120should also be usable for an overhead-reduced host side API.
121</para>
122
123</chapter>
124
125<chapter id="structure"><title>Structure of Gadget Drivers</title>
126
127<para>A system running inside a USB peripheral
128normally has at least three layers inside the kernel to handle
129USB protocol processing, and may have additional layers in
130user space code.
131The "gadget" API is used by the middle layer to interact
132with the lowest level (which directly handles hardware).
133</para>
134
135<para>In Linux, from the bottom up, these layers are:
136</para>
137
138<variablelist>
139
140 <varlistentry>
141 <term><emphasis>USB Controller Driver</emphasis></term>
142
143 <listitem>
144 <para>This is the lowest software level.
145 It is the only layer that talks to hardware,
146 through registers, fifos, dma, irqs, and the like.
Rob Landley3f51bed2007-10-16 23:31:31 -0700147 The <filename>&lt;linux/usb/gadget.h&gt;</filename> API abstracts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 the peripheral controller endpoint hardware.
149 That hardware is exposed through endpoint objects, which accept
150 streams of IN/OUT buffers, and through callbacks that interact
151 with gadget drivers.
152 Since normal USB devices only have one upstream
153 port, they only have one of these drivers.
154 The controller driver can support any number of different
155 gadget drivers, but only one of them can be used at a time.
156 </para>
157
158 <para>Examples of such controller hardware include
159 the PCI-based NetChip 2280 USB 2.0 high speed controller,
160 the SA-11x0 or PXA-25x UDC (found within many PDAs),
161 and a variety of other products.
162 </para>
163
164 </listitem></varlistentry>
165
166 <varlistentry>
167 <term><emphasis>Gadget Driver</emphasis></term>
168
169 <listitem>
170 <para>The lower boundary of this driver implements hardware-neutral
171 USB functions, using calls to the controller driver.
172 Because such hardware varies widely in capabilities and restrictions,
173 and is used in embedded environments where space is at a premium,
174 the gadget driver is often configured at compile time
175 to work with endpoints supported by one particular controller.
176 Gadget drivers may be portable to several different controllers,
177 using conditional compilation.
178 (Recent kernels substantially simplify the work involved in
179 supporting new hardware, by <emphasis>autoconfiguring</emphasis>
180 endpoints automatically for many bulk-oriented drivers.)
181 Gadget driver responsibilities include:
182 </para>
183 <itemizedlist>
184 <listitem><para>handling setup requests (ep0 protocol responses)
185 possibly including class-specific functionality
186 </para></listitem>
187 <listitem><para>returning configuration and string descriptors
188 </para></listitem>
189 <listitem><para>(re)setting configurations and interface
190 altsettings, including enabling and configuring endpoints
191 </para></listitem>
192 <listitem><para>handling life cycle events, such as managing
193 bindings to hardware,
194 USB suspend/resume, remote wakeup,
195 and disconnection from the USB host.
196 </para></listitem>
197 <listitem><para>managing IN and OUT transfers on all currently
198 enabled endpoints
199 </para></listitem>
200 </itemizedlist>
201
202 <para>
203 Such drivers may be modules of proprietary code, although
204 that approach is discouraged in the Linux community.
205 </para>
206 </listitem></varlistentry>
207
208 <varlistentry>
209 <term><emphasis>Upper Level</emphasis></term>
210
211 <listitem>
212 <para>Most gadget drivers have an upper boundary that connects
213 to some Linux driver or framework in Linux.
214 Through that boundary flows the data which the gadget driver
215 produces and/or consumes through protocol transfers over USB.
216 Examples include:
217 </para>
218 <itemizedlist>
219 <listitem><para>user mode code, using generic (gadgetfs)
220 or application specific files in
221 <filename>/dev</filename>
222 </para></listitem>
223 <listitem><para>networking subsystem (for network gadgets,
224 like the CDC Ethernet Model gadget driver)
225 </para></listitem>
226 <listitem><para>data capture drivers, perhaps video4Linux or
227 a scanner driver; or test and measurement hardware.
228 </para></listitem>
229 <listitem><para>input subsystem (for HID gadgets)
230 </para></listitem>
231 <listitem><para>sound subsystem (for audio gadgets)
232 </para></listitem>
233 <listitem><para>file system (for PTP gadgets)
234 </para></listitem>
235 <listitem><para>block i/o subsystem (for usb-storage gadgets)
236 </para></listitem>
237 <listitem><para>... and more </para></listitem>
238 </itemizedlist>
239 </listitem></varlistentry>
240
241 <varlistentry>
242 <term><emphasis>Additional Layers</emphasis></term>
243
244 <listitem>
245 <para>Other layers may exist.
246 These could include kernel layers, such as network protocol stacks,
247 as well as user mode applications building on standard POSIX
248 system call APIs such as
249 <emphasis>open()</emphasis>, <emphasis>close()</emphasis>,
250 <emphasis>read()</emphasis> and <emphasis>write()</emphasis>.
251 On newer systems, POSIX Async I/O calls may be an option.
252 Such user mode code will not necessarily be subject to
253 the GNU General Public License (GPL).
254 </para>
255 </listitem></varlistentry>
256
257
258</variablelist>
259
260<para>OTG-capable systems will also need to include a standard Linux-USB
261host side stack,
262with <emphasis>usbcore</emphasis>,
263one or more <emphasis>Host Controller Drivers</emphasis> (HCDs),
264<emphasis>USB Device Drivers</emphasis> to support
265the OTG "Targeted Peripheral List",
266and so forth.
267There will also be an <emphasis>OTG Controller Driver</emphasis>,
268which is visible to gadget and device driver developers only indirectly.
269That helps the host and device side USB controllers implement the
270two new OTG protocols (HNP and SRP).
271Roles switch (host to peripheral, or vice versa) using HNP
272during USB suspend processing, and SRP can be viewed as a
273more battery-friendly kind of device wakeup protocol.
274</para>
275
276<para>Over time, reusable utilities are evolving to help make some
277gadget driver tasks simpler.
278For example, building configuration descriptors from vectors of
279descriptors for the configurations interfaces and endpoints is
280now automated, and many drivers now use autoconfiguration to
281choose hardware endpoints and initialize their descriptors.
282
283A potential example of particular interest
284is code implementing standard USB-IF protocols for
285HID, networking, storage, or audio classes.
286Some developers are interested in KDB or KGDB hooks, to let
287target hardware be remotely debugged.
288Most such USB protocol code doesn't need to be hardware-specific,
289any more than network protocols like X11, HTTP, or NFS are.
290Such gadget-side interface drivers should eventually be combined,
291to implement composite devices.
292</para>
293
294</chapter>
295
296
297<chapter id="api"><title>Kernel Mode Gadget API</title>
298
299<para>Gadget drivers declare themselves through a
300<emphasis>struct usb_gadget_driver</emphasis>, which is responsible for
301most parts of enumeration for a <emphasis>struct usb_gadget</emphasis>.
302The response to a set_configuration usually involves
303enabling one or more of the <emphasis>struct usb_ep</emphasis> objects
304exposed by the gadget, and submitting one or more
305<emphasis>struct usb_request</emphasis> buffers to transfer data.
306Understand those four data types, and their operations, and
307you will understand how this API works.
308</para>
309
310<note><title>Incomplete Data Type Descriptions</title>
311
312<para>This documentation was prepared using the standard Linux
313kernel <filename>docproc</filename> tool, which turns text
314and in-code comments into SGML DocBook and then into usable
315formats such as HTML or PDF.
316Other than the "Chapter 9" data types, most of the significant
317data types and functions are described here.
318</para>
319
320<para>However, docproc does not understand all the C constructs
321that are used, so some relevant information is likely omitted from
322what you are reading.
323One example of such information is endpoint autoconfiguration.
324You'll have to read the header file, and use example source
325code (such as that for "Gadget Zero"), to fully understand the API.
326</para>
327
328<para>The part of the API implementing some basic
329driver capabilities is specific to the version of the
330Linux kernel that's in use.
331The 2.6 kernel includes a <emphasis>driver model</emphasis>
332framework that has no analogue on earlier kernels;
333so those parts of the gadget API are not fully portable.
334(They are implemented on 2.4 kernels, but in a different way.)
335The driver model state is another part of this API that is
336ignored by the kerneldoc tools.
337</para>
338</note>
339
340<para>The core API does not expose
341every possible hardware feature, only the most widely available ones.
342There are significant hardware features, such as device-to-device DMA
343(without temporary storage in a memory buffer)
344that would be added using hardware-specific APIs.
345</para>
346
347<para>This API allows drivers to use conditional compilation to handle
348endpoint capabilities of different hardware, but doesn't require that.
349Hardware tends to have arbitrary restrictions, relating to
350transfer types, addressing, packet sizes, buffering, and availability.
351As a rule, such differences only matter for "endpoint zero" logic
352that handles device configuration and management.
353The API supports limited run-time
354detection of capabilities, through naming conventions for endpoints.
355Many drivers will be able to at least partially autoconfigure
356themselves.
357In particular, driver init sections will often have endpoint
358autoconfiguration logic that scans the hardware's list of endpoints
359to find ones matching the driver requirements
360(relying on those conventions), to eliminate some of the most
361common reasons for conditional compilation.
362</para>
363
364<para>Like the Linux-USB host side API, this API exposes
365the "chunky" nature of USB messages: I/O requests are in terms
366of one or more "packets", and packet boundaries are visible to drivers.
367Compared to RS-232 serial protocols, USB resembles
368synchronous protocols like HDLC
369(N bytes per frame, multipoint addressing, host as the primary
370station and devices as secondary stations)
371more than asynchronous ones
372(tty style: 8 data bits per frame, no parity, one stop bit).
373So for example the controller drivers won't buffer
374two single byte writes into a single two-byte USB IN packet,
375although gadget drivers may do so when they implement
376protocols where packet boundaries (and "short packets")
377are not significant.
378</para>
379
380<sect1 id="lifecycle"><title>Driver Life Cycle</title>
381
382<para>Gadget drivers make endpoint I/O requests to hardware without
383needing to know many details of the hardware, but driver
384setup/configuration code needs to handle some differences.
385Use the API like this:
386</para>
387
388<orderedlist numeration='arabic'>
389
390<listitem><para>Register a driver for the particular device side
391usb controller hardware,
392such as the net2280 on PCI (USB 2.0),
393sa11x0 or pxa25x as found in Linux PDAs,
394and so on.
395At this point the device is logically in the USB ch9 initial state
396("attached"), drawing no power and not usable
397(since it does not yet support enumeration).
398Any host should not see the device, since it's not
399activated the data line pullup used by the host to
400detect a device, even if VBUS power is available.
401</para></listitem>
402
403<listitem><para>Register a gadget driver that implements some higher level
404device function. That will then bind() to a usb_gadget, which
405activates the data line pullup sometime after detecting VBUS.
406</para></listitem>
407
408<listitem><para>The hardware driver can now start enumerating.
409The steps it handles are to accept USB power and set_address requests.
410Other steps are handled by the gadget driver.
411If the gadget driver module is unloaded before the host starts to
412enumerate, steps before step 7 are skipped.
413</para></listitem>
414
415<listitem><para>The gadget driver's setup() call returns usb descriptors,
416based both on what the bus interface hardware provides and on the
417functionality being implemented.
418That can involve alternate settings or configurations,
419unless the hardware prevents such operation.
420For OTG devices, each configuration descriptor includes
421an OTG descriptor.
422</para></listitem>
423
424<listitem><para>The gadget driver handles the last step of enumeration,
425when the USB host issues a set_configuration call.
426It enables all endpoints used in that configuration,
427with all interfaces in their default settings.
428That involves using a list of the hardware's endpoints, enabling each
429endpoint according to its descriptor.
430It may also involve using <function>usb_gadget_vbus_draw</function>
431to let more power be drawn from VBUS, as allowed by that configuration.
432For OTG devices, setting a configuration may also involve reporting
433HNP capabilities through a user interface.
434</para></listitem>
435
436<listitem><para>Do real work and perform data transfers, possibly involving
437changes to interface settings or switching to new configurations, until the
438device is disconnect()ed from the host.
439Queue any number of transfer requests to each endpoint.
440It may be suspended and resumed several times before being disconnected.
441On disconnect, the drivers go back to step 3 (above).
442</para></listitem>
443
444<listitem><para>When the gadget driver module is being unloaded,
445the driver unbind() callback is issued. That lets the controller
446driver be unloaded.
447</para></listitem>
448
449</orderedlist>
450
451<para>Drivers will normally be arranged so that just loading the
452gadget driver module (or statically linking it into a Linux kernel)
453allows the peripheral device to be enumerated, but some drivers
454will defer enumeration until some higher level component (like
455a user mode daemon) enables it.
456Note that at this lowest level there are no policies about how
457ep0 configuration logic is implemented,
458except that it should obey USB specifications.
459Such issues are in the domain of gadget drivers,
460including knowing about implementation constraints
461imposed by some USB controllers
462or understanding that composite devices might happen to
463be built by integrating reusable components.
464</para>
465
466<para>Note that the lifecycle above can be slightly different
467for OTG devices.
468Other than providing an additional OTG descriptor in each
469configuration, only the HNP-related differences are particularly
470visible to driver code.
471They involve reporting requirements during the SET_CONFIGURATION
472request, and the option to invoke HNP during some suspend callbacks.
473Also, SRP changes the semantics of
474<function>usb_gadget_wakeup</function>
475slightly.
476</para>
477
478</sect1>
479
480<sect1 id="ch9"><title>USB 2.0 Chapter 9 Types and Constants</title>
481
482<para>Gadget drivers
483rely on common USB structures and constants
484defined in the
Don Mullisc67687f2007-02-10 01:46:51 -0800485<filename>&lt;linux/usb/ch9.h&gt;</filename>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486header file, which is standard in Linux 2.6 kernels.
487These are the same types and constants used by host
488side drivers (and usbcore).
489</para>
490
Don Mullisc67687f2007-02-10 01:46:51 -0800491!Iinclude/linux/usb/ch9.h
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492</sect1>
493
494<sect1 id="core"><title>Core Objects and Methods</title>
495
496<para>These are declared in
Rob Landley3f51bed2007-10-16 23:31:31 -0700497<filename>&lt;linux/usb/gadget.h&gt;</filename>,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498and are used by gadget drivers to interact with
499USB peripheral controller drivers.
500</para>
501
502 <!-- yeech, this is ugly in nsgmls PDF output.
503
504 the PDF bookmark and refentry output nesting is wrong,
505 and the member/argument documentation indents ugly.
506
507 plus something (docproc?) adds whitespace before the
508 descriptive paragraph text, so it can't line up right
509 unless the explanations are trivial.
510 -->
511
Rob Landley3f51bed2007-10-16 23:31:31 -0700512!Iinclude/linux/usb/gadget.h
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513</sect1>
514
515<sect1 id="utils"><title>Optional Utilities</title>
516
517<para>The core API is sufficient for writing a USB Gadget Driver,
518but some optional utilities are provided to simplify common tasks.
519These utilities include endpoint autoconfiguration.
520</para>
521
522!Edrivers/usb/gadget/usbstring.c
523!Edrivers/usb/gadget/config.c
524<!-- !Edrivers/usb/gadget/epautoconf.c -->
525</sect1>
526
David Brownell40982be2008-06-19 17:52:58 -0700527<sect1 id="composite"><title>Composite Device Framework</title>
528
529<para>The core API is sufficient for writing drivers for composite
530USB devices (with more than one function in a given configuration),
531and also multi-configuration devices (also more than one function,
532but not necessarily sharing a given configuration).
533There is however an optional framework which makes it easier to
534reuse and combine functions.
535</para>
536
537<para>Devices using this framework provide a <emphasis>struct
538usb_composite_driver</emphasis>, which in turn provides one or
539more <emphasis>struct usb_configuration</emphasis> instances.
540Each such configuration includes at least one
541<emphasis>struct usb_function</emphasis>, which packages a user
542visible role such as "network link" or "mass storage device".
543Management functions may also exist, such as "Device Firmware
544Upgrade".
545</para>
546
547!Iinclude/linux/usb/composite.h
548!Edrivers/usb/gadget/composite.c
549
550</sect1>
551
552<sect1 id="functions"><title>Composite Device Functions</title>
553
554<para>At this writing, a few of the current gadget drivers have
555been converted to this framework.
556Near-term plans include converting all of them, except for "gadgetfs".
557</para>
558
559</sect1>
560
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562</chapter>
563
564<chapter id="controllers"><title>Peripheral Controller Drivers</title>
565
566<para>The first hardware supporting this API was the NetChip 2280
567controller, which supports USB 2.0 high speed and is based on PCI.
568This is the <filename>net2280</filename> driver module.
569The driver supports Linux kernel versions 2.4 and 2.6;
570contact NetChip Technologies for development boards and product
571information.
572</para>
573
574<para>Other hardware working in the "gadget" framework includes:
575Intel's PXA 25x and IXP42x series processors
576(<filename>pxa2xx_udc</filename>),
577Toshiba TC86c001 "Goku-S" (<filename>goku_udc</filename>),
578Renesas SH7705/7727 (<filename>sh_udc</filename>),
579MediaQ 11xx (<filename>mq11xx_udc</filename>),
580Hynix HMS30C7202 (<filename>h7202_udc</filename>),
581National 9303/4 (<filename>n9604_udc</filename>),
582Texas Instruments OMAP (<filename>omap_udc</filename>),
583Sharp LH7A40x (<filename>lh7a40x_udc</filename>),
584and more.
585Most of those are full speed controllers.
586</para>
587
588<para>At this writing, there are people at work on drivers in
589this framework for several other USB device controllers,
590with plans to make many of them be widely available.
591</para>
592
593<!-- !Edrivers/usb/gadget/net2280.c -->
594
595<para>A partial USB simulator,
596the <filename>dummy_hcd</filename> driver, is available.
597It can act like a net2280, a pxa25x, or an sa11x0 in terms
598of available endpoints and device speeds; and it simulates
599control, bulk, and to some extent interrupt transfers.
600That lets you develop some parts of a gadget driver on a normal PC,
601without any special hardware, and perhaps with the assistance
602of tools such as GDB running with User Mode Linux.
603At least one person has expressed interest in adapting that
604approach, hooking it up to a simulator for a microcontroller.
605Such simulators can help debug subsystems where the runtime hardware
606is unfriendly to software development, or is not yet available.
607</para>
608
609<para>Support for other controllers is expected to be developed
610and contributed
611over time, as this driver framework evolves.
612</para>
613
614</chapter>
615
616<chapter id="gadget"><title>Gadget Drivers</title>
617
618<para>In addition to <emphasis>Gadget Zero</emphasis>
619(used primarily for testing and development with drivers
620for usb controller hardware), other gadget drivers exist.
621</para>
622
623<para>There's an <emphasis>ethernet</emphasis> gadget
624driver, which implements one of the most useful
625<emphasis>Communications Device Class</emphasis> (CDC) models.
626One of the standards for cable modem interoperability even
627specifies the use of this ethernet model as one of two
628mandatory options.
629Gadgets using this code look to a USB host as if they're
630an Ethernet adapter.
631It provides access to a network where the gadget's CPU is one host,
632which could easily be bridging, routing, or firewalling
633access to other networks.
634Since some hardware can't fully implement the CDC Ethernet
635requirements, this driver also implements a "good parts only"
636subset of CDC Ethernet.
637(That subset doesn't advertise itself as CDC Ethernet,
638to avoid creating problems.)
639</para>
640
641<para>Support for Microsoft's <emphasis>RNDIS</emphasis>
642protocol has been contributed by Pengutronix and Auerswald GmbH.
643This is like CDC Ethernet, but it runs on more slightly USB hardware
644(but less than the CDC subset).
645However, its main claim to fame is being able to connect directly to
646recent versions of Windows, using drivers that Microsoft bundles
647and supports, making it much simpler to network with Windows.
648</para>
649
650<para>There is also support for user mode gadget drivers,
651using <emphasis>gadgetfs</emphasis>.
652This provides a <emphasis>User Mode API</emphasis> that presents
653each endpoint as a single file descriptor. I/O is done using
654normal <emphasis>read()</emphasis> and <emphasis>read()</emphasis> calls.
655Familiar tools like GDB and pthreads can be used to
656develop and debug user mode drivers, so that once a robust
657controller driver is available many applications for it
658won't require new kernel mode software.
659Linux 2.6 <emphasis>Async I/O (AIO)</emphasis>
660support is available, so that user mode software
661can stream data with only slightly more overhead
662than a kernel driver.
663</para>
664
665<para>There's a USB Mass Storage class driver, which provides
666a different solution for interoperability with systems such
667as MS-Windows and MacOS.
668That <emphasis>File-backed Storage</emphasis> driver uses a
669file or block device as backing store for a drive,
670like the <filename>loop</filename> driver.
671The USB host uses the BBB, CB, or CBI versions of the mass
672storage class specification, using transparent SCSI commands
673to access the data from the backing store.
674</para>
675
676<para>There's a "serial line" driver, useful for TTY style
677operation over USB.
678The latest version of that driver supports CDC ACM style
679operation, like a USB modem, and so on most hardware it can
680interoperate easily with MS-Windows.
681One interesting use of that driver is in boot firmware (like a BIOS),
682which can sometimes use that model with very small systems without
683real serial lines.
684</para>
685
686<para>Support for other kinds of gadget is expected to
687be developed and contributed
688over time, as this driver framework evolves.
689</para>
690
691</chapter>
692
693<chapter id="otg"><title>USB On-The-GO (OTG)</title>
694
695<para>USB OTG support on Linux 2.6 was initially developed
696by Texas Instruments for
697<ulink url="http://www.omap.com">OMAP</ulink> 16xx and 17xx
698series processors.
699Other OTG systems should work in similar ways, but the
700hardware level details could be very different.
701</para>
702
703<para>Systems need specialized hardware support to implement OTG,
704notably including a special <emphasis>Mini-AB</emphasis> jack
705and associated transciever to support <emphasis>Dual-Role</emphasis>
706operation:
707they can act either as a host, using the standard
708Linux-USB host side driver stack,
709or as a peripheral, using this "gadget" framework.
710To do that, the system software relies on small additions
711to those programming interfaces,
712and on a new internal component (here called an "OTG Controller")
713affecting which driver stack connects to the OTG port.
714In each role, the system can re-use the existing pool of
715hardware-neutral drivers, layered on top of the controller
716driver interfaces (<emphasis>usb_bus</emphasis> or
717<emphasis>usb_gadget</emphasis>).
718Such drivers need at most minor changes, and most of the calls
719added to support OTG can also benefit non-OTG products.
720</para>
721
722<itemizedlist>
723 <listitem><para>Gadget drivers test the <emphasis>is_otg</emphasis>
724 flag, and use it to determine whether or not to include
725 an OTG descriptor in each of their configurations.
726 </para></listitem>
727 <listitem><para>Gadget drivers may need changes to support the
728 two new OTG protocols, exposed in new gadget attributes
729 such as <emphasis>b_hnp_enable</emphasis> flag.
730 HNP support should be reported through a user interface
731 (two LEDs could suffice), and is triggered in some cases
732 when the host suspends the peripheral.
733 SRP support can be user-initiated just like remote wakeup,
734 probably by pressing the same button.
735 </para></listitem>
736 <listitem><para>On the host side, USB device drivers need
737 to be taught to trigger HNP at appropriate moments, using
738 <function>usb_suspend_device()</function>.
739 That also conserves battery power, which is useful even
740 for non-OTG configurations.
741 </para></listitem>
742 <listitem><para>Also on the host side, a driver must support the
743 OTG "Targeted Peripheral List". That's just a whitelist,
744 used to reject peripherals not supported with a given
745 Linux OTG host.
746 <emphasis>This whitelist is product-specific;
747 each product must modify <filename>otg_whitelist.h</filename>
748 to match its interoperability specification.
749 </emphasis>
750 </para>
751 <para>Non-OTG Linux hosts, like PCs and workstations,
752 normally have some solution for adding drivers, so that
753 peripherals that aren't recognized can eventually be supported.
754 That approach is unreasonable for consumer products that may
755 never have their firmware upgraded, and where it's usually
756 unrealistic to expect traditional PC/workstation/server kinds
757 of support model to work.
758 For example, it's often impractical to change device firmware
759 once the product has been distributed, so driver bugs can't
760 normally be fixed if they're found after shipment.
761 </para></listitem>
762</itemizedlist>
763
764<para>
765Additional changes are needed below those hardware-neutral
766<emphasis>usb_bus</emphasis> and <emphasis>usb_gadget</emphasis>
767driver interfaces; those aren't discussed here in any detail.
768Those affect the hardware-specific code for each USB Host or Peripheral
769controller, and how the HCD initializes (since OTG can be active only
770on a single port).
771They also involve what may be called an <emphasis>OTG Controller
772Driver</emphasis>, managing the OTG transceiver and the OTG state
773machine logic as well as much of the root hub behavior for the
774OTG port.
775The OTG controller driver needs to activate and deactivate USB
776controllers depending on the relevant device role.
777Some related changes were needed inside usbcore, so that it
778can identify OTG-capable devices and respond appropriately
779to HNP or SRP protocols.
780</para>
781
782</chapter>
783
784</book>
785<!--
786 vim:syntax=sgml:sw=4
787-->