⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gadget.tmpl

📁 linux 内核源代码
💻 TMPL
📖 第 1 页 / 共 2 页
字号:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"	"http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" []><book id="USB-Gadget-API">  <bookinfo>    <title>USB Gadget API for Linux</title>    <date>20 August 2004</date>    <edition>20 August 2004</edition>      <legalnotice>       <para>	 This documentation is free software; you can redistribute	 it and/or modify it under the terms of the GNU General Public	 License as published by the Free Software Foundation; either	 version 2 of the License, or (at your option) any later	 version.       </para>	         <para>	 This program is distributed in the hope that it will be	 useful, but WITHOUT ANY WARRANTY; without even the implied	 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU General Public License for more details.       </para>	         <para>	 You should have received a copy of the GNU General Public	 License along with this program; if not, write to the Free	 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,	 MA 02111-1307 USA       </para>	         <para>	 For more details see the file COPYING in the source	 distribution of Linux.       </para>    </legalnotice>    <copyright>      <year>2003-2004</year>      <holder>David Brownell</holder>    </copyright>    <author>      <firstname>David</firstname>       <surname>Brownell</surname>      <affiliation>        <address><email>dbrownell@users.sourceforge.net</email></address>      </affiliation>    </author>  </bookinfo><toc></toc><chapter id="intro"><title>Introduction</title><para>This document presents a Linux-USB "Gadget"kernel modeAPI, for use within peripherals and other USB devicesthat embed Linux.It provides an overview of the API structure,and shows how that fits into a system development project.This is the first such API released on Linux to addressa number of important problems, including: </para><itemizedlist>    <listitem><para>Supports USB 2.0, for high speed devices which	can stream data at several dozen megabytes per second.	</para></listitem>    <listitem><para>Handles devices with dozens of endpoints just as	well as ones with just two fixed-function ones.  Gadget drivers	can be written so they're easy to port to new hardware.	</para></listitem>    <listitem><para>Flexible enough to expose more complex USB device	capabilities such as multiple configurations, multiple interfaces,	composite devices,	and alternate interface settings.	</para></listitem>    <listitem><para>USB "On-The-Go" (OTG) support, in conjunction	with updates to the Linux-USB host side.	</para></listitem>    <listitem><para>Sharing data structures and API models with the	Linux-USB host side API.  This helps the OTG support, and	looks forward to more-symmetric frameworks (where the same	I/O model is used by both host and device side drivers).	</para></listitem>    <listitem><para>Minimalist, so it's easier to support new device	controller hardware.  I/O processing doesn't imply large	demands for memory or CPU resources.	</para></listitem></itemizedlist><para>Most Linux developers will not be able to use this API, since theyhave USB "host" hardware in a PC, workstation, or server.Linux users with embedded systems are more likely tohave USB peripheral hardware.To distinguish drivers running inside such hardware from themore familiar Linux "USB device drivers",which are host side proxies for the real USB devices,a different term is used:the drivers inside the peripherals are "USB gadget drivers".In USB protocol interactions, the device driver is the master(or "client driver")and the gadget driver is the slave (or "function driver").</para><para>The gadget API resembles the host side Linux-USB API in that bothuse queues of request objects to package I/O buffers, and those requestsmay be submitted or canceled.They share common definitions for the standard USB<emphasis>Chapter 9</emphasis> messages, structures, and constants.Also, both APIs bind and unbind drivers to devices.The APIs differ in detail, since the host side's currentURB framework exposes a number of implementation detailsand assumptions that are inappropriate for a gadget API.While the model for control transfers and configurationmanagement is necessarily different (one side is a hardware-neutral master,the other is a hardware-aware slave), the endpoint I/0 API used hereshould also be usable for an overhead-reduced host side API.</para></chapter><chapter id="structure"><title>Structure of Gadget Drivers</title><para>A system running inside a USB peripheralnormally has at least three layers inside the kernel to handleUSB protocol processing, and may have additional layers inuser space code.The "gadget" API is used by the middle layer to interactwith the lowest level (which directly handles hardware).</para><para>In Linux, from the bottom up, these layers are:</para><variablelist>    <varlistentry>        <term><emphasis>USB Controller Driver</emphasis></term>	<listitem>	<para>This is the lowest software level.	It is the only layer that talks to hardware,	through registers, fifos, dma, irqs, and the like.	The <filename>&lt;linux/usb/gadget.h&gt;</filename> API abstracts	the peripheral controller endpoint hardware.	That hardware is exposed through endpoint objects, which accept	streams of IN/OUT buffers, and through callbacks that interact	with gadget drivers.	Since normal USB devices only have one upstream	port, they only have one of these drivers.	The controller driver can support any number of different	gadget drivers, but only one of them can be used at a time.	</para>	<para>Examples of such controller hardware include	the PCI-based NetChip 2280 USB 2.0 high speed controller,	the SA-11x0 or PXA-25x UDC (found within many PDAs),	and a variety of other products.	</para>	</listitem></varlistentry>    <varlistentry>	<term><emphasis>Gadget Driver</emphasis></term>	<listitem>	<para>The lower boundary of this driver implements hardware-neutral	USB functions, using calls to the controller driver.	Because such hardware varies widely in capabilities and restrictions,	and is used in embedded environments where space is at a premium,	the gadget driver is often configured at compile time	to work with endpoints supported by one particular controller.	Gadget drivers may be portable to several different controllers,	using conditional compilation.	(Recent kernels substantially simplify the work involved in	supporting new hardware, by <emphasis>autoconfiguring</emphasis>	endpoints automatically for many bulk-oriented drivers.)	Gadget driver responsibilities include:	</para>	<itemizedlist>	    <listitem><para>handling setup requests (ep0 protocol responses)		possibly including class-specific functionality		</para></listitem>	    <listitem><para>returning configuration and string descriptors		</para></listitem>	    <listitem><para>(re)setting configurations and interface		altsettings, including enabling and configuring endpoints		</para></listitem>	    <listitem><para>handling life cycle events, such as managing		bindings to hardware,		USB suspend/resume, remote wakeup,		and disconnection from the USB host.		</para></listitem>	    <listitem><para>managing IN and OUT transfers on all currently		enabled endpoints		</para></listitem>	</itemizedlist>	<para>	Such drivers may be modules of proprietary code, although	that approach is discouraged in the Linux community.	</para>	</listitem></varlistentry>    <varlistentry>	<term><emphasis>Upper Level</emphasis></term>	<listitem>	<para>Most gadget drivers have an upper boundary that connects	to some Linux driver or framework in Linux.	Through that boundary flows the data which the gadget driver	produces and/or consumes through protocol transfers over USB.	Examples include:	</para>	<itemizedlist>	    <listitem><para>user mode code, using generic (gadgetfs)	        or application specific files in		<filename>/dev</filename>		</para></listitem>	    <listitem><para>networking subsystem (for network gadgets,		like the CDC Ethernet Model gadget driver)		</para></listitem>	    <listitem><para>data capture drivers, perhaps video4Linux or		 a scanner driver; or test and measurement hardware.		 </para></listitem>	    <listitem><para>input subsystem (for HID gadgets)		</para></listitem>	    <listitem><para>sound subsystem (for audio gadgets)		</para></listitem>	    <listitem><para>file system (for PTP gadgets)		</para></listitem>	    <listitem><para>block i/o subsystem (for usb-storage gadgets)		</para></listitem>	    <listitem><para>... and more </para></listitem>	</itemizedlist>	</listitem></varlistentry>    <varlistentry>	<term><emphasis>Additional Layers</emphasis></term>	<listitem>	<para>Other layers may exist.	These could include kernel layers, such as network protocol stacks,	as well as user mode applications building on standard POSIX	system call APIs such as	<emphasis>open()</emphasis>, <emphasis>close()</emphasis>,	<emphasis>read()</emphasis> and <emphasis>write()</emphasis>.	On newer systems, POSIX Async I/O calls may be an option.	Such user mode code will not necessarily be subject to	the GNU General Public License (GPL).	</para>	</listitem></varlistentry></variablelist><para>OTG-capable systems will also need to include a standard Linux-USBhost side stack,with <emphasis>usbcore</emphasis>,one or more <emphasis>Host Controller Drivers</emphasis> (HCDs),<emphasis>USB Device Drivers</emphasis> to supportthe OTG "Targeted Peripheral List",and so forth.There will also be an <emphasis>OTG Controller Driver</emphasis>,which is visible to gadget and device driver developers only indirectly.That helps the host and device side USB controllers implement thetwo new OTG protocols (HNP and SRP).Roles switch (host to peripheral, or vice versa) using HNPduring USB suspend processing, and SRP can be viewed as amore battery-friendly kind of device wakeup protocol.</para><para>Over time, reusable utilities are evolving to help make somegadget driver tasks simpler.For example, building configuration descriptors from vectors ofdescriptors for the configurations interfaces and endpoints isnow automated, and many drivers now use autoconfiguration tochoose hardware endpoints and initialize their descriptors.A potential example of particular interestis code implementing standard USB-IF protocols forHID, networking, storage, or audio classes.Some developers are interested in KDB or KGDB hooks, to lettarget hardware be remotely debugged.Most such USB protocol code doesn't need to be hardware-specific,any more than network protocols like X11, HTTP, or NFS are.Such gadget-side interface drivers should eventually be combined,to implement composite devices.</para></chapter><chapter id="api"><title>Kernel Mode Gadget API</title><para>Gadget drivers declare themselves through a<emphasis>struct usb_gadget_driver</emphasis>, which is responsible formost parts of enumeration for a <emphasis>struct usb_gadget</emphasis>.The response to a set_configuration usually involvesenabling one or more of the <emphasis>struct usb_ep</emphasis> objectsexposed by the gadget, and submitting one or more<emphasis>struct usb_request</emphasis> buffers to transfer data.Understand those four data types, and their operations, andyou will understand how this API works.</para> <note><title>Incomplete Data Type Descriptions</title><para>This documentation was prepared using the standard Linuxkernel <filename>docproc</filename> tool, which turns textand in-code comments into SGML DocBook and then into usableformats such as HTML or PDF.Other than the "Chapter 9" data types, most of the significantdata types and functions are described here.</para><para>However, docproc does not understand all the C constructsthat are used, so some relevant information is likely omitted fromwhat you are reading.  One example of such information is endpoint autoconfiguration.You'll have to read the header file, and use example sourcecode (such as that for "Gadget Zero"), to fully understand the API.</para><para>The part of the API implementing some basicdriver capabilities is specific to the version of theLinux kernel that's in use.The 2.6 kernel includes a <emphasis>driver model</emphasis>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 isignored by the kerneldoc tools.</para></note><para>The core API does not exposeevery 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.</para><para>This API allows drivers to use conditional compilation to handleendpoint capabilities of different hardware, but doesn't require that.Hardware tends to have arbitrary restrictions, relating totransfer types, addressing, packet sizes, buffering, and availability.As a rule, such differences only matter for "endpoint zero" logicthat handles device configuration and management.The API supports limited run-timedetection of capabilities, through naming conventions for endpoints.Many drivers will be able to at least partially autoconfigurethemselves.In particular, driver init sections will often have endpointautoconfiguration logic that scans the hardware's list of endpointsto find ones matching the driver requirements(relying on those conventions), to eliminate some of the mostcommon reasons for conditional compilation.</para><para>Like the Linux-USB host side API, this API exposesthe "chunky" nature of USB messages:  I/O requests are in termsof one or more "packets", and packet boundaries are visible to drivers.Compared to RS-232 serial protocols, USB resemblessynchronous protocols like HDLC(N bytes per frame, multipoint addressing, host as the primarystation 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 buffertwo single byte writes into a single two-byte USB IN packet,although gadget drivers may do so when they implementprotocols where packet boundaries (and "short packets")are not significant.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -