1<?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
55<chapter id="intro"><title>Introduction</title>
56
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.
147	The <filename>&lt;linux/usb/gadget.h&gt;</filename> API abstracts
148	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
485<filename>&lt;linux/usb/ch9.h&gt;</filename>
486header 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
491!Iinclude/linux/usb/ch9.h
492</sect1>
493
494<sect1 id="core"><title>Core Objects and Methods</title>
495
496<para>These are declared in
497<filename>&lt;linux/usb/gadget.h&gt;</filename>,
498and 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
512!Iinclude/linux/usb/gadget.h
513</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
527<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!Edrivers/usb/gadget/function/f_acm.c
560!Edrivers/usb/gadget/function/f_ecm.c
561!Edrivers/usb/gadget/function/f_subset.c
562!Edrivers/usb/gadget/function/f_obex.c
563!Edrivers/usb/gadget/function/f_serial.c
564
565</sect1>
566
567
568</chapter>
569
570<chapter id="controllers"><title>Peripheral Controller Drivers</title>
571
572<para>The first hardware supporting this API was the NetChip 2280
573controller, which supports USB 2.0 high speed and is based on PCI.
574This is the <filename>net2280</filename> driver module.
575The driver supports Linux kernel versions 2.4 and 2.6;
576contact NetChip Technologies for development boards and product
577information.
578</para> 
579
580<para>Other hardware working in the "gadget" framework includes:
581Intel's PXA 25x and IXP42x series processors
582(<filename>pxa2xx_udc</filename>),
583Toshiba TC86c001 "Goku-S" (<filename>goku_udc</filename>),
584Renesas SH7705/7727 (<filename>sh_udc</filename>),
585MediaQ 11xx (<filename>mq11xx_udc</filename>),
586Hynix HMS30C7202 (<filename>h7202_udc</filename>),
587National 9303/4 (<filename>n9604_udc</filename>),
588Texas Instruments OMAP (<filename>omap_udc</filename>),
589Sharp LH7A40x (<filename>lh7a40x_udc</filename>),
590and more.
591Most of those are full speed controllers.
592</para>
593
594<para>At this writing, there are people at work on drivers in
595this framework for several other USB device controllers,
596with plans to make many of them be widely available.
597</para>
598
599<!-- !Edrivers/usb/gadget/net2280.c -->
600
601<para>A partial USB simulator,
602the <filename>dummy_hcd</filename> driver, is available.
603It can act like a net2280, a pxa25x, or an sa11x0 in terms
604of available endpoints and device speeds; and it simulates
605control, bulk, and to some extent interrupt transfers.
606That lets you develop some parts of a gadget driver on a normal PC,
607without any special hardware, and perhaps with the assistance
608of tools such as GDB running with User Mode Linux.
609At least one person has expressed interest in adapting that
610approach, hooking it up to a simulator for a microcontroller.
611Such simulators can help debug subsystems where the runtime hardware
612is unfriendly to software development, or is not yet available.
613</para>
614
615<para>Support for other controllers is expected to be developed
616and contributed
617over time, as this driver framework evolves.
618</para>
619
620</chapter>
621
622<chapter id="gadget"><title>Gadget Drivers</title>
623
624<para>In addition to <emphasis>Gadget Zero</emphasis>
625(used primarily for testing and development with drivers
626for usb controller hardware), other gadget drivers exist.
627</para>
628
629<para>There's an <emphasis>ethernet</emphasis> gadget
630driver, which implements one of the most useful
631<emphasis>Communications Device Class</emphasis> (CDC) models.  
632One of the standards for cable modem interoperability even
633specifies the use of this ethernet model as one of two
634mandatory options.
635Gadgets using this code look to a USB host as if they're
636an Ethernet adapter.
637It provides access to a network where the gadget's CPU is one host,
638which could easily be bridging, routing, or firewalling
639access to other networks.
640Since some hardware can't fully implement the CDC Ethernet
641requirements, this driver also implements a "good parts only"
642subset of CDC Ethernet.
643(That subset doesn't advertise itself as CDC Ethernet,
644to avoid creating problems.)
645</para>
646
647<para>Support for Microsoft's <emphasis>RNDIS</emphasis>
648protocol has been contributed by Pengutronix and Auerswald GmbH.
649This is like CDC Ethernet, but it runs on more slightly USB hardware
650(but less than the CDC subset).
651However, its main claim to fame is being able to connect directly to
652recent versions of Windows, using drivers that Microsoft bundles
653and supports, making it much simpler to network with Windows.
654</para>
655
656<para>There is also support for user mode gadget drivers,
657using <emphasis>gadgetfs</emphasis>.
658This provides a <emphasis>User Mode API</emphasis> that presents
659each endpoint as a single file descriptor.  I/O is done using
660normal <emphasis>read()</emphasis> and <emphasis>read()</emphasis> calls.
661Familiar tools like GDB and pthreads can be used to
662develop and debug user mode drivers, so that once a robust
663controller driver is available many applications for it
664won't require new kernel mode software.
665Linux 2.6 <emphasis>Async I/O (AIO)</emphasis>
666support is available, so that user mode software
667can stream data with only slightly more overhead
668than a kernel driver.
669</para>
670
671<para>There's a USB Mass Storage class driver, which provides
672a different solution for interoperability with systems such
673as MS-Windows and MacOS.
674That <emphasis>Mass Storage</emphasis> driver uses a
675file or block device as backing store for a drive,
676like the <filename>loop</filename> driver.
677The USB host uses the BBB, CB, or CBI versions of the mass
678storage class specification, using transparent SCSI commands
679to access the data from the backing store.
680</para>
681
682<para>There's a "serial line" driver, useful for TTY style
683operation over USB.
684The latest version of that driver supports CDC ACM style
685operation, like a USB modem, and so on most hardware it can
686interoperate easily with MS-Windows.
687One interesting use of that driver is in boot firmware (like a BIOS),
688which can sometimes use that model with very small systems without
689real serial lines.
690</para>
691
692<para>Support for other kinds of gadget is expected to
693be developed and contributed
694over time, as this driver framework evolves.
695</para>
696
697</chapter>
698
699<chapter id="otg"><title>USB On-The-GO (OTG)</title>
700
701<para>USB OTG support on Linux 2.6 was initially developed
702by Texas Instruments for
703<ulink url="http://www.omap.com">OMAP</ulink> 16xx and 17xx
704series processors.
705Other OTG systems should work in similar ways, but the
706hardware level details could be very different.
707</para> 
708
709<para>Systems need specialized hardware support to implement OTG,
710notably including a special <emphasis>Mini-AB</emphasis> jack
711and associated transceiver to support <emphasis>Dual-Role</emphasis>
712operation:
713they can act either as a host, using the standard
714Linux-USB host side driver stack,
715or as a peripheral, using this "gadget" framework.
716To do that, the system software relies on small additions
717to those programming interfaces,
718and on a new internal component (here called an "OTG Controller")
719affecting which driver stack connects to the OTG port.
720In each role, the system can re-use the existing pool of
721hardware-neutral drivers, layered on top of the controller
722driver interfaces (<emphasis>usb_bus</emphasis> or
723<emphasis>usb_gadget</emphasis>).
724Such drivers need at most minor changes, and most of the calls
725added to support OTG can also benefit non-OTG products.
726</para>
727
728<itemizedlist>
729    <listitem><para>Gadget drivers test the <emphasis>is_otg</emphasis>
730	flag, and use it to determine whether or not to include
731	an OTG descriptor in each of their configurations.
732	</para></listitem>
733    <listitem><para>Gadget drivers may need changes to support the
734	two new OTG protocols, exposed in new gadget attributes
735	such as <emphasis>b_hnp_enable</emphasis> flag.
736	HNP support should be reported through a user interface
737	(two LEDs could suffice), and is triggered in some cases
738	when the host suspends the peripheral.
739	SRP support can be user-initiated just like remote wakeup,
740	probably by pressing the same button.
741	</para></listitem>
742    <listitem><para>On the host side, USB device drivers need
743	to be taught to trigger HNP at appropriate moments, using
744	<function>usb_suspend_device()</function>.
745	That also conserves battery power, which is useful even
746	for non-OTG configurations.
747	</para></listitem>
748    <listitem><para>Also on the host side, a driver must support the
749	OTG "Targeted Peripheral List".  That's just a whitelist,
750	used to reject peripherals not supported with a given
751	Linux OTG host.
752	<emphasis>This whitelist is product-specific;
753	each product must modify <filename>otg_whitelist.h</filename>
754	to match its interoperability specification.
755	</emphasis>
756	</para>
757	<para>Non-OTG Linux hosts, like PCs and workstations,
758	normally have some solution for adding drivers, so that
759	peripherals that aren't recognized can eventually be supported.
760	That approach is unreasonable for consumer products that may
761	never have their firmware upgraded, and where it's usually
762	unrealistic to expect traditional PC/workstation/server kinds
763	of support model to work.
764	For example, it's often impractical to change device firmware
765	once the product has been distributed, so driver bugs can't
766	normally be fixed if they're found after shipment.
767	</para></listitem>
768</itemizedlist>
769
770<para>
771Additional changes are needed below those hardware-neutral
772<emphasis>usb_bus</emphasis> and <emphasis>usb_gadget</emphasis>
773driver interfaces; those aren't discussed here in any detail.
774Those affect the hardware-specific code for each USB Host or Peripheral
775controller, and how the HCD initializes (since OTG can be active only
776on a single port).
777They also involve what may be called an <emphasis>OTG Controller
778Driver</emphasis>, managing the OTG transceiver and the OTG state
779machine logic as well as much of the root hub behavior for the
780OTG port.
781The OTG controller driver needs to activate and deactivate USB
782controllers depending on the relevant device role.
783Some related changes were needed inside usbcore, so that it
784can identify OTG-capable devices and respond appropriately
785to HNP or SRP protocols.
786</para> 
787
788</chapter>
789
790</book>
791<!--
792	vim:syntax=sgml:sw=4
793-->
794