ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / Documentation / DocBook / gadget.tmpl
1 <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook V3.1//EN"[]>
2 <book id="USB-Gadget-API">
3   <bookinfo>
4     <title>USB Gadget API for Linux</title>
5     <date>02 June 2003</date>
6     <edition>02 June 2003</edition>
7   
8     <legalnotice>
9        <para>
10          This documentation is free software; you can redistribute
11          it and/or modify it under the terms of the GNU General Public
12          License as published by the Free Software Foundation; either
13          version 2 of the License, or (at your option) any later
14          version.
15        </para>
16           
17        <para>
18          This program is distributed in the hope that it will be
19          useful, but WITHOUT ANY WARRANTY; without even the implied
20          warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21          See the GNU General Public License for more details.
22        </para>
23           
24        <para>
25          You should have received a copy of the GNU General Public
26          License along with this program; if not, write to the Free
27          Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28          MA 02111-1307 USA
29        </para>
30           
31        <para>
32          For more details see the file COPYING in the source
33          distribution of Linux.
34        </para>
35     </legalnotice>
36     <copyright>
37       <year>2003</year>
38       <holder>David Brownell</holder>
39     </copyright>
40
41     <author>
42       <firstname>David</firstname> 
43       <surname>Brownell</surname>
44       <affiliation>
45         <address><email>dbrownell@users.sourceforge.net</email></address>
46       </affiliation>
47     </author>
48   </bookinfo>
49
50 <toc></toc>
51
52 <chapter><title>Introduction</title>
53
54 <para>This document presents a Linux-USB "Gadget"
55 kernel mode
56 API, for use within peripherals and other USB devices
57 that embed Linux.
58 It provides an overview of the API structure,
59 and shows how that fits into a system development project.
60 This is the first such API released on Linux to address
61 a number of important problems, including: </para>
62
63 <itemizedlist>
64     <listitem><para>Supports USB 2.0, for high speed devices which
65         can stream data at several dozen megabytes per second.
66         </para></listitem>
67     <listitem><para>Handles devices with dozens of endpoints just as
68         well as ones with just two fixed-function ones.  Gadget drivers
69         can be written so they're easy to port to new hardware.
70         </para></listitem>
71     <listitem><para>Flexible enough to expose more complex USB device
72         capabilities such as multiple configurations, multiple interfaces,
73         composite devices,
74         and alternate interface settings.
75         </para></listitem>
76     <listitem><para>Sharing data structures and API models with the
77         Linux-USB host side API.  This looks forward to USB "On-The-Go"
78         (OTG) and similar more-symmetric frameworks.
79         </para></listitem>
80     <listitem><para>Minimalist, so it's easier to support new device
81         controller hardware.  I/O processing doesn't imply large
82         demands for memory or CPU resources.
83         </para></listitem>
84 </itemizedlist>
85
86
87 <para>Most Linux developers will not be able to use this API, since they
88 have USB "host" hardware in a PC, workstation, or server.
89 Linux users with embedded systems are more likely to
90 have USB peripheral hardware.
91 To distinguish drivers running inside such hardware from the
92 more familiar Linux "USB device drivers",
93 which are host side proxies for the real USB devices,
94 a different term is used:
95 the drivers inside the peripherals are "USB gadget drivers".
96 In USB protocol interactions, the device driver is the master
97 (or "client driver")
98 and the gadget driver is the slave (or "function driver").
99 </para>
100
101 <para>The gadget API resembles the host side Linux-USB API in that both
102 use queues of request objects to package I/O buffers, and those requests
103 may be submitted or canceled.
104 They share common definitions for the standard USB
105 <emphasis>Chapter 9</emphasis> messages, structures, and constants.
106 Also, both APIs bind and unbind drivers to devices.
107 The APIs differ in detail, since the host side's current
108 URB framework exposes a number of implementation details
109 and assumptions that are inappropriate for a gadget API.
110 While the model for control transfers and configuration
111 management is necessarily different (one side is a hardware-neutral master,
112 the other is a hardware-aware slave), the endpoint I/0 API used here
113 should also be usable for an overhead-reduced host side API.
114 </para>
115
116 </chapter>
117
118 <chapter id="structure"><title>Structure of Gadget Drivers</title>
119
120 <para>A system running inside a USB peripheral
121 normally has at least three layers inside the kernel to handle
122 USB protocol processing, and may have additional layers in
123 user space code.
124 The "gadget" API is used by the middle layer to interact
125 with the lowest level (which directly handles hardware).
126 </para>
127
128 <para>In Linux, from the bottom up, these layers are:
129 </para>
130
131 <variablelist>
132
133     <varlistentry>
134         <term><emphasis>USB Controller Driver</emphasis></term>
135
136         <listitem>
137         <para>This is the lowest software level.
138         It is the only layer that talks to hardware,
139         through registers, fifos, dma, irqs, and the like.
140         The <filename>&lt;linux/usb_gadget.h&gt;</filename> API abstracts
141         the peripheral controller endpoint hardware.
142         That hardware is exposed through endpoint objects, which accept
143         streams of IN/OUT buffers, and through callbacks that interact
144         with gadget drivers.
145         Since normal USB devices only have one upstream
146         port, they only have one of these drivers.
147         The controller driver can support any number of different
148         gadget drivers, but only one of them can be used at a time.
149         </para>
150
151         <para>Examples of such controller hardware include
152         the PCI-based NetChip 2280 USB 2.0 high speed controller,
153         the SA-11x0 or PXA-25x UDC (found within many PDAs),
154         and a variety of other products.
155         </para>
156         </listitem></varlistentry>
157
158     <varlistentry>
159         <term><emphasis>Gadget Driver</emphasis></term>
160
161         <listitem>
162         <para>The lower boundary of this driver implements hardware-neutral
163         USB functions, using calls to the controller driver.
164         Because such hardware varies widely in capabilities and restrictions,
165         the gadget driver is normally configured at compile time
166         to work with endpoints supported by one particular controller.
167         Gadget drivers may be portable to several different controllers,
168         using conditional compilation.
169         Gadget driver responsibilities include:
170         </para>
171         <itemizedlist>
172             <listitem><para>handling setup requests (ep0 protocol responses)
173                 possibly including class-specific functionality
174                 </para></listitem>
175             <listitem><para>returning configuration and string descriptors
176                 </para></listitem>
177             <listitem><para>(re)setting configurations and interface
178                 altsettings, including enabling and configuring endpoints
179                 </para></listitem>
180             <listitem><para>handling life cycle events, such as managing
181                 bindings
182                 to hardware, and disconnection from the USB host.
183                 </para></listitem>
184             <listitem><para>managing IN and OUT transfers on all currently
185                 enabled endpoints
186                 </para></listitem>
187         </itemizedlist>
188
189         <para>
190         Such drivers may be modules of proprietary code, although
191         that approach is discouraged in the Linux community.
192         </para>
193         </listitem></varlistentry>
194
195     <varlistentry>
196         <term><emphasis>Upper Level</emphasis></term>
197
198         <listitem>
199         <para>Most gadget drivers have an upper boundary that connects
200         to some Linux driver or framework in Linux.
201         Through that boundary flows the data which the gadget driver
202         produces and/or consumes through protocol transfers over USB.
203         Examples include:
204         </para>
205         <itemizedlist>
206             <listitem><para>user mode code, using generic (gadgetfs)
207                 or application specific files in
208                 <filename>/dev</filename>
209                 </para></listitem>
210             <listitem><para>networking subsystem (for network gadgets,
211                 like the CDC Ethernet Model gadget driver)
212                 </para></listitem>
213             <listitem><para>data capture drivers, perhaps video4Linux or
214                  a scanner driver; or test and measurement hardware.
215                  </para></listitem>
216             <listitem><para>input subsystem (for HID gadgets)
217                 </para></listitem>
218             <listitem><para>sound subsystem (for audio gadgets)
219                 </para></listitem>
220             <listitem><para>file system (for PTP gadgets)
221                 </para></listitem>
222             <listitem><para>block i/o subsystem (for usb-storage gadgets)
223                 </para></listitem>
224             <listitem><para>... and more </para></listitem>
225         </itemizedlist>
226         </listitem></varlistentry>
227
228     <varlistentry>
229         <term><emphasis>Additional Layers</emphasis></term>
230
231         <listitem>
232         <para>Other layers may exist.
233         These could include kernel layers, such as network protocol stacks,
234         as well as user mode applications building on standard POSIX
235         system call APIs such as
236         <emphasis>open()</emphasis>, <emphasis>close()</emphasis>,
237         <emphasis>read()</emphasis> and <emphasis>write()</emphasis>.
238         On newer systems, POSIX Async I/O calls may be an option.
239         Such user mode code will not necessarily be subject to
240         the GNU General Public License (GPL).
241         </para>
242         </listitem></varlistentry>
243
244
245 </variablelist>
246
247 <para>Over time, reusable utilities should evolve to help make some
248 gadget driver tasks simpler.  An example of particular interest
249 is code implementing standard USB-IF protocols for
250 HID, networking, storage, or audio classes.
251 Some developers are interested in KDB or KGDB hooks, to let
252 target hardware be remotely debugged.
253 Most such USB protocol code doesn't need to be hardware-specific,
254 any more than network protocols like X11, HTTP, or NFS are.
255 Such interface drivers might be combined, to support composite devices.
256 </para>
257
258 </chapter>
259
260
261 <chapter id="api"><title>Kernel Mode Gadget API</title>
262
263 <para>Gadget drivers declare themselves through a
264 <emphasis>struct usb_gadget_driver</emphasis>, which is responsible for
265 most parts of enumeration for a <emphasis>struct usb_gadget</emphasis>.
266 The response to a set_configuration usually involves
267 enabling one or more of the <emphasis>struct usb_ep</emphasis> objects
268 exposed by the gadget, and submitting one or more
269 <emphasis>struct usb_request</emphasis> buffers to transfer data.
270 Understand those four data types, and their operations, and
271 you will understand how this API works.
272 </para> 
273
274 <note><title>Incomplete Data Type Descriptions</title>
275
276 <para>This documentation was prepared using the standard Linux
277 kernel <filename>docproc</filename> tool, which turns text
278 and in-code comments into SGML DocBook and then into usable
279 formats such as HTML or PDF.
280 Other than the "Chapter 9" data types, most of the significant
281 data types and functions are described here.
282 </para>
283
284 <para>However, docproc does not understand all the C constructs
285 that are used, so some relevant information is likely omitted from
286 what you are reading.  
287 One example of such information is several per-request flags.
288 You'll have to read the header file, and use example source
289 code (such as that for "Gadget Zero"), to fully understand the API.
290 </para>
291
292 <para>The part of the API implementing some basic
293 driver capabilities is specific to the version of the
294 Linux kernel that's in use.
295 The 2.5 kernel includes a <emphasis>driver model</emphasis>
296 framework that has no analogue on earlier kernels;
297 so those parts of the gadget API are not fully portable.
298 (They are implemented on 2.4 kernels, but in a different way.)
299 The driver model state is another part of this API that is
300 ignored by the kerneldoc tools.
301 </para>
302 </note>
303
304 <para>The core API does not expose
305 every possible hardware feature, only the most widely available ones.
306 There are significant hardware features, such as device-to-device DMA
307 (without temporary storage in a memory buffer)
308 that would be added using hardware-specific APIs.
309 </para>
310
311 <para>This API expects drivers to use conditional compilation to handle
312 endpoint capabilities of different hardware.
313 Those tend to have arbitrary restrictions, relating to
314 transfer types, addressing, packet sizes, buffering, and availability.
315 As a rule, such differences only matter for "endpoint zero" logic
316 that handles device configuration and management.
317 The API only supports limited run-time
318 detection of capabilities, through naming conventions for endpoints.
319 Although a gadget driver could scan the endpoints available to it and
320 choose to map those capabilities onto driver functionality in some way,
321 few drivers will want to reconfigure themselves at run-time.
322 </para>
323
324 <para>Like the Linux-USB host side API, this API exposes
325 the "chunky" nature of USB messages:  I/O requests are in terms
326 of one or more "packets", and packet boundaries are visible to drivers.
327 Compared to RS-232 serial protocols, USB resembles
328 synchronous protocols like HDLC
329 (N bytes per frame, multipoint addressing, host as the primary
330 station and devices as secondary stations)
331 more than asynchronous ones
332 (tty style:  8 data bits per frame, no parity, one stop bit).
333 So for example the controller drivers won't buffer
334 two single byte writes into a single two-byte USB IN packet,
335 although gadget drivers may do so when they implement
336 protocols where packet boundaries (and "short packets")
337 are not significant.
338 </para>
339
340 <sect1 id="lifecycle"><title>Driver Life Cycle</title>
341
342 <para>Gadget drivers make endpoint I/O requests to hardware without
343 needing to know many details of the hardware, but driver
344 setup/configuration code needs to handle some differences.
345 Use the API like this:
346 </para>
347
348 <orderedlist numeration='arabic'>
349
350 <listitem><para>Register a driver for the particular device side
351 usb controller hardware,
352 such as the net2280 on PCI (USB 2.0),
353 sa11x0 or pxa25x as found in Linux PDAs,
354 and so on.
355 At this point the device is logically in the USB ch9 initial state
356 ("attached"), drawing no power and not usable
357 (since it does not yet support enumeration).
358 </para></listitem>
359
360 <listitem><para>Register a gadget driver that implements some higher level
361 device function.  That will then bind() to a usb_gadget.
362 </para></listitem>
363
364 <listitem><para>The hardware driver can now start enumerating.
365 The steps it handles are to accept USB power and set_address requests.
366 Other steps are handled by the gadget driver.
367 If the gadget driver module is unloaded before the host starts to
368 enumerate, steps before step 7 are skipped.
369 </para></listitem>
370
371 <listitem><para>The gadget driver's setup() call returns usb descriptors,
372 based both on what the bus interface hardware provides and on the
373 functionality being implemented.
374 That can involve alternate settings or configurations,
375 unless the hardware prevents such operation.
376 </para></listitem>
377
378 <listitem><para>The gadget driver handles the last step of enumeration,
379 when the USB host issues a set_configuration call.
380 It enables all endpoints used in that configuration,
381 with all interfaces in their default settings.
382 That involves using a list of the hardware's endpoints, enabling each
383 endpoint according to its descriptor.
384 </para></listitem>
385
386 <listitem><para>Do real work and perform data transfers, possibly involving
387 changes to interface settings or switching to new configurations, until the
388 device is disconnect()ed from the host.
389 Queue any number of transfer requests to each endpoint.
390 The drivers then go back to step 3 (above).
391 </para></listitem>
392
393 <listitem><para>When the gadget driver module is being unloaded,
394 the driver unbind() callback is issued.  That lets the controller
395 driver be unloaded.
396 </para></listitem>
397
398 </orderedlist>
399
400 <para>Drivers will normally be arranged so that just loading the
401 gadget driver module (or statically linking it into a Linux kernel)
402 allows the peripheral device to be enumerated.
403 Note that at this lowest level there are no policies about how
404 ep0 configuration logic is implemented,
405 except that it should obey USB specifications.
406 Such issues are in the domain of gadget drivers,
407 including knowing about implementation constraints
408 imposed by some USB controllers
409 or understanding that composite devices might happen to
410 be built by integrating reusable components.
411 </para>
412
413 </sect1>
414
415 <sect1 id="ch9"><title>USB 2.0 Chapter 9 Types and Constants</title>
416
417 <para>Gadget drivers
418 rely on common USB structures and constants
419 defined in the
420 <filename>&lt;linux/usb_ch9.h&gt;</filename>
421 header file, which is standard in Linux 2.5 kernels.
422 These are the same types and constants used by host
423 side drivers.
424 </para>
425
426 !Iinclude/linux/usb_ch9.h
427 </sect1>
428
429 <sect1 id="core"><title>Core Objects and Methods</title>
430
431 <para>These are declared in
432 <filename>&lt;linux/usb_gadget.h&gt;</filename>,
433 and are used by gadget drivers to interact with
434 USB peripheral controller drivers.
435 </para>
436
437         <!-- yeech, this is ugly in nsgmls PDF output.
438
439              the PDF bookmark and refentry output nesting is wrong,
440              and the member/argument documentation indents ugly.
441
442              plus something (docproc?) adds whitespace before the
443              descriptive paragraph text, so it can't line up right
444              unless the explanations are trivial.
445           -->
446
447 !Iinclude/linux/usb_gadget.h
448 </sect1>
449
450 <sect1 id="utils"><title>Optional Utilities</title>
451
452 <para>The core API is sufficient for writing a USB Gadget Driver,
453 but some optional utilities are provided to simplify common tasks.
454 </para>
455
456 !Edrivers/usb/gadget/usbstring.c
457 !Edrivers/usb/gadget/config.c
458 </sect1>
459
460 </chapter>
461
462 <chapter id="controllers"><title>Peripheral Controller Drivers</title>
463
464 <para>The first hardware supporting this API is the NetChip 2280
465 controller, which supports USB 2.0 high speed and is based on PCI.
466 This is the <filename>net2280</filename> driver module.
467 The driver supports Linux kernel versions 2.4 and 2.5;
468 contact NetChip Technologies for development boards and product
469 information.
470 </para> 
471
472 <para>For users of Intel's PXA 2xx series processors,
473 a <filename>pxa2xx_udc</filename> driver is available.
474 </para>
475
476 <para>At this writing, there are people at work on drivers in
477 this framework for several other USB device controllers,
478 with plans to make many of them be widely available.
479 </para>
480
481 <!-- !Edrivers/usb/gadget/net2280.c -->
482
483 <para>A partial USB simulator,
484 the <filename>dummy_hcd</filename> driver, is available.
485 It can act like a net2280, a pxa25x, or an sa11x0 in terms
486 of available endpoints and device speeds; and it simulates
487 control, bulk, and to some extent interrupt transfers.
488 That lets you develop some parts of a gadget driver on a normal PC,
489 without any special hardware, and perhaps with the assistance
490 of tools such as GDB running with User Mode Linux.
491 At least one person has expressed interest in adapting that
492 approach, hooking it up to a simulator for a microcontroller.
493 Such simulators can help debug subsystems where the runtime hardware
494 is unfriendly to software development, or is not yet available.
495 </para>
496
497 <para>Support for other controllers is expected to be developed
498 and contributed
499 over time, as this driver framework evolves.
500 </para>
501
502 </chapter>
503
504 <chapter id="gadget"><title>Gadget Drivers</title>
505
506 <para>In addition to <emphasis>Gadget Zero</emphasis>
507 (used primarily for testing and development with drivers
508 for usb controller hardware), other gadget drivers exist.
509 </para>
510
511 <para>There's an <emphasis>ethernet</emphasis> gadget
512 driver, which implements one of the most useful
513 <emphasis>Communications Device Class</emphasis> (CDC) models.  
514 One of the standards for cable modem interoperability even
515 specifies the use of this ethernet model as one of two
516 mandatory options.
517 Gadgets using this code look to a USB host as if they're
518 an Ethernet adapter.
519 It provides access to a network where the gadget's CPU is one host,
520 which could easily be bridging, routing, or firewalling
521 access to other networks.
522 Since some hardware can't fully implement the CDC Ethernet
523 requirements, this driver also implements a "good parts only"
524 subset of CDC Ethernet.
525 (That subset doesn't advertise itself as CDC Ethernet,
526 to avoid creating problems.)
527 </para>
528
529 <para>There is also support for user mode gadget drivers,
530 using <emphasis>gadgetfs</emphasis>.
531 This provides a <emphasis>User Mode API</emphasis> that presents
532 each endpoint as a single file descriptor.  I/O is done using
533 normal <emphasis>read()</emphasis> and <emphasis>read()</emphasis> calls.
534 Familiar tools like GDB and pthreads can be used to
535 develop and debug user mode drivers, so that once a robust
536 controller driver is available many applications for it
537 won't require new kernel mode software.
538 </para>
539
540 <para>There's a USB Mass Storage class driver, which provides
541 a different solution for interoperability with systems such
542 as MS-Windows and MacOS.
543 That <emphasis>File-backed Storage</emphasis> driver uses a
544 file or block device as backing store for a drive,
545 like the <filename>loop</filename> driver.
546 The USB host uses the BBB, CB, or CBI versions of the mass
547 storage class specification, using transparent SCSI commands
548 to access the data from the backing store.
549 </para>
550
551 <para>Support for other kinds of gadget is expected to
552 be developed and contributed
553 over time, as this driver framework evolves.
554 </para>
555
556 </chapter>
557
558
559 </book>
560 <!--
561         vim:syntax=sgml:sw=4
562 -->