Linux USB slave Development

Source: Internet
Author: User

Http://hi.baidu.com/romashell/blog/item/0faf1e007a249e82e850cd10.html

Recently, I am working on the driver of the USB slave device in Linux. The program is almost written. Let's make a summary. Welcome to the discussion. please correct me if you have any mistakes. Thank you! -- Jason

1. Introduction to USB drive

There are three main types of devices on the USB Bus: Host Controller (such)
EHCI, uhci, OHCI), hub, and device ). Host
The Controller (HC) is responsible for bus management. It is the bus commander. All transmission on the bus is initiated by HC, and devices supporting OTG can also initiate transmission. The Hub is the bus's
Nodes are used to expand the number of devices that can be connected to the bus, which is transparent to the driver. device is a variety of devices and each device has its own functions, such as USB flash disks and USB cameras.
.

There are a lot of materials written by the device driver of the host USB device in Linux, and ldd3 has a corresponding introduction. Mainly through the USB
The core module interacts with the device. This article only describes how to write an embedded USB driver running Linux. Because the host already uses the device
To avoid confusion, use the USB Gadget driver to indicate the driver from the device at one time. In USB protocol
Interactions, the device driver is the master (or client driver) and
The Gadget driver is the slave (or function driver ).

The Linux USB Gadget driver API defines a general interface for the gadget driver.
The driver communicates with the underlying USB controller driver through APIS. This API shields the underlying hardware from being different, making the gadget
The driver focuses on the implementation of functions and tries its best to be unrelated to the hardware. Shows the architecture of the slave system:

 

 

Related information about USB Gadget API layer:

1. http://www.linux-usb.org/gadget/

2. Files in the/kerneldoc/gadget/directory of the kernel

3. The most important file is the/include/Linux/usb_gadget.h file, which contains detailed definitions and comments of the data structure and method of the gadget API interface.

Example of a gadget DRIVER:

4. Some examples of gadget driver are available in the kernel/Drivers/USB/gadget/directory. Zero. c is the simplest example. file_storage.c is the gadget driver of the storage device.

3. Gadget API

To understand the gadget API, you only need to understand several important data structures in the header file (usb_gadget.h. For more information about the fields, see the description of the H file.

(1)

Struct usb_gadget {

Const struct usb_gadget_ops * OPS;

Struct usb_ep * ep0;

Struct list_head ep_list;

Enum usb_device_speed speed;

Unsigned is_dualspeed: 1;

Unsigned is_otg: 1;

Unsigned is_a_peripheral: 1;

Unsigned B _hnp_enable: 1;

Unsigned a_hnp_support: 1;

Unsigned a_alt_hnp_support: 1;

Const char * Name;

Struct device dev;

};

This structure indicates a USB device controller (UDC), which is initialized in the UDC driver and is read-only for the gadget driver.

(2)

Struct usb_gadget_driver {

Char * function;

Enum usb_device_speed speed;

INT (* bind) (struct usb_gadget *);

Void (* unbind) (struct usb_gadget *);

INT (* setup) (struct usb_gadget *,

Const struct usb_ctrlrequest *);

Void (* disconnect) (struct usb_gadget *);

Void (* suspend) (struct usb_gadget *);

Void (* resume) (struct usb_gadget *);

Struct device_driver driver;

};

This structure indicates our gadget driver. The functions must be implemented in the gadget driver, which is one of the key points of coding. For specific implementation, refer to the zero. c file. The following describes the functions of each function:

In bind, the main command is to execute gadget.
Driver initialization, which will be called by the usb_gadget_register_driver function, while usb_gadget_register_driver
It is generally called during module initialization. After executing usb_gadget_register_driver, the device can be detected by the host.

In contrast to bind, unbind is called by usb_gadget_unregister_driver.

Setup processes the requests sent from the host, such as reading decriptors and configuring configuration. Therefore, the setup function generally contains the switch and case statements.

Disconnect is called when the device is disconnected from the host.

(3)

Struct usb_request {

Void * Buf;

Unsigned length;

Dma_addr_t DMA;

Unsigned no_interrupt: 1;

Unsigned zero: 1;

Unsigned short_not_ OK: 1;

Void (* Complete) (struct usb_ep * EP,

Struct usb_request * req );

Void * context;

Struct list_head list;

Int status;

Unsigned actual;

};

Usb_request indicates a transmission request, which is similar to the URL of the host. It has a complete field that specifies the callback function when the request is complete.

(4)

Struct usb_ep {

Void * driver_data;

Const char * Name;

Const struct usb_ep_ops * OPS;

Struct list_head ep_list;

Unsigned maxpacket: 16;

};

Struct usb_ep indicates an endpoint (EP), and usb_gadget contains all EP lists.

(5)

Struct usb_ep_ops {

INT (* enable) (struct usb_ep * EP,

Const struct usb_endpoint_descriptor * DESC );

INT (* disable) (struct usb_ep * EP );

Struct usb_request * (* alloc_request) (struct usb_ep * EP,

Gfp_t gfp_flags );

Void (* free_request) (struct usb_ep * EP, struct usb_request * req );

Void * (* alloc_buffer) (struct usb_ep * EP, unsigned bytes,

Dma_addr_t * DMA, gfp_t gfp_flags );

Void (* free_buffer) (struct usb_ep * EP, void * Buf, dma_addr_t DMA,

Unsigned bytes );

// Note: On 2.6, drivers may also use dma_map () and

// Dma_sync_single _ * () to directly manage DMA overhead.

INT (* Queue) (struct usb_ep * EP, struct usb_request * req,

Gfp_t gfp_flags );

INT (* dequeue) (struct usb_ep * EP, struct usb_request * req );

INT (* set_halt) (struct usb_ep * EP, int value );

INT (* initialize o_status) (struct usb_ep * EP );

Void (* kerbero_flush) (struct usb_ep * EP );

};

Struct usb_ep_ops indicates the operation of the endpoint. The queue function submits a usb_request to an endpoint, which is a key function for data transmission.

Iv. Summary

This article is a summary of how to learn to use the gadget API. It should be recorded in case you forget it after a long time.

Source:

Http://www.linuxidc.com/Linux/2007-12/10106p2.htm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.