Sophie

Sophie

distrib > CentOS > 6 > i386 > by-pkgid > cf93d8a8acdcc6fe2225039da0502495 > files > 1359

kernel-doc-2.6.32-131.17.1.el6.centos.plus.noarch.rpm

<?xml version="1.0" encoding="ANSI_X3.4-1968" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968" /><title>Chapter&#160;3.&#160;Kernel Mode Gadget API</title><meta name="generator" content="DocBook XSL Stylesheets V1.75.2" /><link rel="home" href="index.html" title="USB Gadget API for Linux" /><link rel="up" href="index.html" title="USB Gadget API for Linux" /><link rel="prev" href="ch02.html" title="Chapter&#160;2.&#160;Structure of Gadget Drivers" /><link rel="next" href="ch03s02.html" title="USB 2.0 Chapter 9 Types and Constants" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter&#160;3.&#160;Kernel Mode Gadget API</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch02.html">Prev</a>&#160;</td><th width="60%" align="center">&#160;</th><td width="20%" align="right">&#160;<a accesskey="n" href="ch03s02.html">Next</a></td></tr></table><hr /></div><div class="chapter" title="Chapter&#160;3.&#160;Kernel Mode Gadget API"><div class="titlepage"><div><div><h2 class="title"><a id="api"></a>Chapter&#160;3.&#160;Kernel Mode Gadget API</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="ch03.html#lifecycle">Driver Life Cycle</a></span></dt><dt><span class="sect1"><a href="ch03s02.html">USB 2.0 Chapter 9 Types and Constants</a></span></dt><dt><span class="sect1"><a href="ch03s03.html">Core Objects and Methods</a></span></dt><dt><span class="sect1"><a href="ch03s04.html">Optional Utilities</a></span></dt><dt><span class="sect1"><a href="ch03s05.html">Composite Device Framework</a></span></dt><dt><span class="sect1"><a href="ch03s06.html">Composite Device Functions</a></span></dt></dl></div><p>Gadget drivers declare themselves through a
<span class="emphasis"><em>struct usb_gadget_driver</em></span>, which is responsible for
most parts of enumeration for a <span class="emphasis"><em>struct usb_gadget</em></span>.
The response to a set_configuration usually involves
enabling one or more of the <span class="emphasis"><em>struct usb_ep</em></span> objects
exposed by the gadget, and submitting one or more
<span class="emphasis"><em>struct usb_request</em></span> buffers to transfer data.
Understand those four data types, and their operations, and
you will understand how this API works.
</p><div class="note" title="Incomplete Data Type Descriptions" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Incomplete Data Type Descriptions</h3><p>This documentation was prepared using the standard Linux
kernel <code class="filename">docproc</code> tool, which turns text
and in-code comments into SGML DocBook and then into usable
formats such as HTML or PDF.
Other than the "Chapter 9" data types, most of the significant
data types and functions are described here.
</p><p>However, docproc does not understand all the C constructs
that are used, so some relevant information is likely omitted from
what you are reading.  
One example of such information is endpoint autoconfiguration.
You'll have to read the header file, and use example source
code (such as that for "Gadget Zero"), to fully understand the API.
</p><p>The part of the API implementing some basic
driver capabilities is specific to the version of the
Linux kernel that's in use.
The 2.6 kernel includes a <span class="emphasis"><em>driver model</em></span>
framework that has no analogue on earlier kernels;
so those parts of the gadget API are not fully portable.
(They are implemented on 2.4 kernels, but in a different way.)
The driver model state is another part of this API that is
ignored by the kerneldoc tools.
</p></div><p>The core API does not expose
every possible hardware feature, only the most widely available ones.
There are significant hardware features, such as device-to-device DMA
(without temporary storage in a memory buffer)
that would be added using hardware-specific APIs.
</p><p>This API allows drivers to use conditional compilation to handle
endpoint capabilities of different hardware, but doesn't require that.
Hardware tends to have arbitrary restrictions, relating to
transfer types, addressing, packet sizes, buffering, and availability.
As a rule, such differences only matter for "endpoint zero" logic
that handles device configuration and management.
The API supports limited run-time
detection of capabilities, through naming conventions for endpoints.
Many drivers will be able to at least partially autoconfigure
themselves.
In particular, driver init sections will often have endpoint
autoconfiguration logic that scans the hardware's list of endpoints
to find ones matching the driver requirements
(relying on those conventions), to eliminate some of the most
common reasons for conditional compilation.
</p><p>Like the Linux-USB host side API, this API exposes
the "chunky" nature of USB messages:  I/O requests are in terms
of one or more "packets", and packet boundaries are visible to drivers.
Compared to RS-232 serial protocols, USB resembles
synchronous protocols like HDLC
(N bytes per frame, multipoint addressing, host as the primary
station and devices as secondary stations)
more than asynchronous ones
(tty style:  8 data bits per frame, no parity, one stop bit).
So for example the controller drivers won't buffer
two single byte writes into a single two-byte USB IN packet,
although gadget drivers may do so when they implement
protocols where packet boundaries (and "short packets")
are not significant.
</p><div class="sect1" title="Driver Life Cycle"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="lifecycle"></a>Driver Life Cycle</h2></div></div></div><p>Gadget drivers make endpoint I/O requests to hardware without
needing to know many details of the hardware, but driver
setup/configuration code needs to handle some differences.
Use the API like this:
</p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p>Register a driver for the particular device side
usb controller hardware,
such as the net2280 on PCI (USB 2.0),
sa11x0 or pxa25x as found in Linux PDAs,
and so on.
At this point the device is logically in the USB ch9 initial state
("attached"), drawing no power and not usable
(since it does not yet support enumeration).
Any host should not see the device, since it's not
activated the data line pullup used by the host to
detect a device, even if VBUS power is available.
</p></li><li class="listitem"><p>Register a gadget driver that implements some higher level
device function.  That will then bind() to a usb_gadget, which
activates the data line pullup sometime after detecting VBUS.
</p></li><li class="listitem"><p>The hardware driver can now start enumerating.
The steps it handles are to accept USB power and set_address requests.
Other steps are handled by the gadget driver.
If the gadget driver module is unloaded before the host starts to
enumerate, steps before step 7 are skipped.
</p></li><li class="listitem"><p>The gadget driver's setup() call returns usb descriptors,
based both on what the bus interface hardware provides and on the
functionality being implemented.
That can involve alternate settings or configurations,
unless the hardware prevents such operation.
For OTG devices, each configuration descriptor includes
an OTG descriptor.
</p></li><li class="listitem"><p>The gadget driver handles the last step of enumeration,
when the USB host issues a set_configuration call.
It enables all endpoints used in that configuration,
with all interfaces in their default settings.
That involves using a list of the hardware's endpoints, enabling each
endpoint according to its descriptor.
It may also involve using <code class="function">usb_gadget_vbus_draw</code>
to let more power be drawn from VBUS, as allowed by that configuration.
For OTG devices, setting a configuration may also involve reporting
HNP capabilities through a user interface.
</p></li><li class="listitem"><p>Do real work and perform data transfers, possibly involving
changes to interface settings or switching to new configurations, until the
device is disconnect()ed from the host.
Queue any number of transfer requests to each endpoint.
It may be suspended and resumed several times before being disconnected.
On disconnect, the drivers go back to step 3 (above).
</p></li><li class="listitem"><p>When the gadget driver module is being unloaded,
the driver unbind() callback is issued.  That lets the controller
driver be unloaded.
</p></li></ol></div><p>Drivers will normally be arranged so that just loading the
gadget driver module (or statically linking it into a Linux kernel)
allows the peripheral device to be enumerated, but some drivers
will defer enumeration until some higher level component (like
a user mode daemon) enables it.
Note that at this lowest level there are no policies about how
ep0 configuration logic is implemented,
except that it should obey USB specifications.
Such issues are in the domain of gadget drivers,
including knowing about implementation constraints
imposed by some USB controllers
or understanding that composite devices might happen to
be built by integrating reusable components.
</p><p>Note that the lifecycle above can be slightly different
for OTG devices.
Other than providing an additional OTG descriptor in each
configuration, only the HNP-related differences are particularly
visible to driver code.
They involve reporting requirements during the SET_CONFIGURATION
request, and the option to invoke HNP during some suspend callbacks.
Also, SRP changes the semantics of
<code class="function">usb_gadget_wakeup</code>
slightly.
</p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch02.html">Prev</a>&#160;</td><td width="20%" align="center">&#160;</td><td width="40%" align="right">&#160;<a accesskey="n" href="ch03s02.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter&#160;2.&#160;Structure of Gadget Drivers&#160;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&#160;USB 2.0 Chapter 9 Types and Constants</td></tr></table></div></body></html>