Basic USB driver for centos

Source: Internet
Author: User

USB is a connection between the host and peripheral devices. USB was initially designed to replace low-speed bus with a variety of different interfaces. (For example, serial port, parallel port, and keyboard connection ). It connects various types of devices with a single type of bus.

The USB topology is not in the bus mode. It is a tree composed of several point-to-point connections. The connection line consists of four cables (power supply, ground line, and two data lines)

The Host Controller is responsible for querying whether data needs to be sent from each USB device. That is to say, a USB device cannot send data without the requirements of the master controller.

The USB Protocol Specification defines a setStandards that can be followed by any specific type of device. If a device follows this device, a special driver is not required. These differentA specific type is called a class, for example, a storage device, a keyboard, a mouse, a game lever, or a network device. For other devices that do not conform to these types. You need to write a specific driver for this device.



USB device composition:
Linux Kernel provides USB Core to handle most of the complexity of USB. Writing a USB driver, Sam thinks it is to communicate the USB hardware device with the USB Core.

The USB Protocol outlines a hardware USB device with the following definitions.

Concept 1. USB endpoint)
The USB endpoint can only transmit data to one direction. From the host to the device (output Endpoint) or from the device to the host (input Endpoint ). An Endpoint can be seen as a one-way pipeline.

There are four types of endpoints. The difference is that the data transmission method:
Control Endpoint:
Used to control access to different parts of the USB device. They are usually used to configure devices, get device information, send commands to devices, or get device status reports. Each USB device has a control Endpoint named Endpoint0. USB Core uses this Endpoint0 to configure the device during insertion.

Interrupted Endpoint:
When the USB master controller requires a device to transmit data, the interrupted Endpoint transmits a small amount of data at a fixed rate.
USB Keyboard and Mouse usually use an interrupted Endpoint.
Note that the interrupted Endpoint cannot actively send data to the USB master controller because it is different from the interrupted Endpoint. Second, you need to wait for the USB master controller to poll.

Batch Endpoint:
The Bulk Endpoint is used to transmit large volumes of data. The USB specification does not guarantee that data can be transmitted within the specified time. However, data integrity is ensured. Usually used by printers, storage devices, and network devices.

Wait for the Endpoint:
It is used to transmit large batches of data, but it cannot be guaranteed whether the data can be reached.
Usually used by data collection devices.

Sam thinks, in fact, how many and what type of endpoints a device has. In fact, hardware equipment has been set up in the manufacturing phase. USB Core only reads the information and sends the information to the USB driver.

In Linux Kernel, struct usb_host_endpoint is used to describe the USB Endpoint. But in fact, the structUsb_endpoint_descriptor is the true description of the Endpoint.
Struct usb_endpoint_descriptor
{
_ U8 bLength;
_ U8 bDescriptorType;
_ U8BEndpointAddress;
_ U8BmAttributes;
_ Le16WMaxPacketSize;
_ U8BInterval;
_ U8 bRefresh;
_ U8 bSynchAddress;
} _ Attribute _ (packed ));

BEndpointAddress:
// This Endpoint USB address. It also contains Endpoint information. Determine whether the output Endpoint or the input Endpoint by mask USB_DIR_OUT and USB_DIR_IN.

BmAttributes;
Endpoint Type. You can also use the mask: USB_ENDPOINT_XFER_ISOC to determine whether the Endpoint is interrupted or batch Endpoint.

WMaxPacketSize;
The maximum number of bytes that an Endpoint can process at a time. Although the driver can transmit larger data, it is still split into smaller values during actual transmission.

BInterval:
If the Endpoint is interrupted, it is the Endpoint interval setting. That is, the request interruption interval. In milliseconds.


Concept 2: Interface)
Multiple endpoints are bundled into a USB Interface.
A usb Interface corresponds to only one logical connection, such as the mouse, keyboard, or audio stream. A USB device can correspond to multiple interfaces. For example, a device with mouse and keyboard that Sam has seen has two interfaces, one keyboard and one mouse.
In addition, some USB speakers have two interfaces, one keyboard and one audio stream.

Note: Each USB drver only processes one USB Interface. Therefore, a device may correspond to multiple drivers.
Therefore, when processing USB device insertion, USB Core will wake up the appropriate driver for different interfaces. The interface is passed to the drver as a parameter.

Linux Kernel uses struct usb_interface to describe the USB Interface. However, the Interface parameter is usb_interface_descriptor In the example.

Struct usb_interface
{
Struct usb_host_interface * altsetting;
Struct usb_host_interface * cur_altsetting;
Unsigned num_altsetting;
Struct usb_interface_assoc_descriptor * intf_assoc;
Int minor;
Enum usb_interface_condition condition;
Unsigned is_active: 1;
Unsigned needs_remote_wakeup: 1;
Struct device dev;
Struct device * usb_dev;
Int pm_usage_cnt;
};

Struct usb_host_interface * altsetting;
The Interface struct array contains all optional configurations that may be used for this Interface.
Struct usb_host_interface * cur_altsetting;
Number of optional configurations
Unsigned num_altsetting;
PointAltsetting pointer. The current Active settings.

Usb_interface_descriptor: Interface Descriptor

Struct usb_interface_descriptor {
_ U8BLength;

// The length of the descriptor in bytes. The Protocol stipulates that each descriptor must start with one byte to indicate the length of the descriptor. The bLength of the interface descriptor should be 9

_ U8BDescriptorType;

// Type of the descriptor. All types of descriptors are in ch9.h, * Descriptor types... USB 2.0 spec table 9.5

_ U8BInterfaceNumber;

// Interface number. Each configuration can contain multiple interfaces. This value is their index value.

_ U8BAlternateSetting;

_ U8BNumEndpoints;

// Number of endpoints owned by the interface. The endpoint 0 is not included here.

_ U8BInterfaceClass;

// Class to which the Interface belongs. Example: HID = 0x03

_ U8BInterfaceSubClass;

// This value is based on bInterfaceClass. Indicates the sub-class in an Interface class. For example, there are: USB_INTERFACE_SUBCLASS_BOOT in HID.

_ U8BInterfaceProtocol;

// Same as above, there is USB_INTERFACE_PROTOCOL_MOUSE in HID.

_ U8IInterface; // string descriptor that contains descriptive vendor information.


} _ Attribute _ (packed ));// _ Attribute __, which means to tell the compiler that the elements of this structure are 1-byte aligned and do not add any filling bits.



Concept 3: Configuration
One or moreThe USB Interface is bundled as configuration. A USB device can have multiple configurations and can be switched between multiple configurations.
Configuration: struct usb_host_config
USB device: usb_device.

To sum up:
1One or more USB devicesConfiguration
1Configure one or moreInterface
OneOne or more interfacesSet
There are no or more interfacesEndpoint



USB URB
The USB code in Linux Kernel communicates with all USB devices through urb (fast USB request.
Urb is used to receive data asynchronously from a specific USB Endpoint of a specific USB device or send data to a specific USB Endpoint of a specific USB device.

Urb is created by the USB driver. And assign it to a specific Endpoint of a specific USB device. And submitted to the USB Core by the USB driver.

1. Create urb.
Urb cannot be statically defined in the driver. This will damage the USB Core's urb counting mechanism. Therefore, you must use:
The usb_alloc_urb function.
Struct urb *Usb_alloc_urb (int iso_packets, gfp_t mem_flags)
The first parameter is the number of equal data packets.

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.