gadget.tmpl 30 KB

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