Chapter 3. Kernel Mode Gadget API

Table of Contents

Driver Life Cycle
USB 2.0 Chapter 9 Types and Constants
struct usb_ctrlrequest — SETUP data for a USB device control request
Core Objects and Methods
struct usb_request — describes one i/o request
struct usb_ep — device side representation of USB endpoint
usb_ep_enable — configure endpoint, making it usable
usb_ep_disable — endpoint is no longer usable
usb_ep_alloc_request — allocate a request object to use with this endpoint
usb_ep_free_request — frees a request object
usb_ep_alloc_buffer — allocate an I/O buffer
usb_ep_free_buffer — frees an i/o buffer
usb_ep_queue — queues (submits) an I/O request to an endpoint.
usb_ep_dequeue — dequeues (cancels, unlinks) an I/O request from an endpoint
usb_ep_set_halt — sets the endpoint halt feature.
usb_ep_clear_halt — clears endpoint halt, and resets toggle
usb_ep_fifo_status — returns number of bytes in fifo, or error
usb_ep_fifo_flush — flushes contents of a fifo
struct usb_gadget — represents a usb slave device
usb_gadget_frame_number — returns the current frame number
usb_gadget_wakeup — tries to wake up the host connected to this gadget
usb_gadget_set_selfpowered — sets the device selfpowered feature.
usb_gadget_clear_selfpowered — clear the device selfpowered feature.
usb_gadget_vbus_connect — Notify controller that VBUS is powered
usb_gadget_vbus_draw — constrain controller's VBUS power usage
usb_gadget_vbus_disconnect — notify controller about VBUS session end
usb_gadget_connect — software-controlled connect to USB host
usb_gadget_disconnect — software-controlled disconnect from USB host
struct usb_gadget_driver — driver for usb 'slave' devices
usb_gadget_register_driver — register a gadget driver
usb_gadget_unregister_driver — unregister a gadget driver
struct usb_string — wraps a C string and its USB id
struct usb_gadget_strings — a set of USB strings in a given language
Optional Utilities
usb_gadget_get_string — fill out a string descriptor
usb_descriptor_fillbuf — fill buffer with descriptors
usb_gadget_config_buf — builts a complete configuration descriptor

Gadget drivers declare themselves through a struct usb_gadget_driver, which is responsible for most parts of enumeration for a struct usb_gadget. The response to a set_configuration usually involves enabling one or more of the struct usb_ep objects exposed by the gadget, and submitting one or more struct usb_request buffers to transfer data. Understand those four data types, and their operations, and you will understand how this API works.

Incomplete Data Type Descriptions

This documentation was prepared using the standard Linux kernel docproc 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.

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.

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 driver model 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.

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.

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.

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.