A brief analysis of Linux driver framework for USB bus driver

Source: Internet
Author: User
Tags goto

A Universal Serial Bus (USB) is a connection between a host and a peripheral device. The USB bus specification has version 1.1 and 2.0, and of course now has 3.0. The USB1.1 supports two transmission speeds: 1.5Mbps at low speeds and 12Mbps at high speeds. The USB2.0 can transmit speeds up to 480Mbps. USB2.0 backwards compatible with USB1.1, the USB1.1 device can be connected to the USB2.0 controller, or the USB2.0 device can be connected to the USB1.1 controller. The s3c2440 USB host controller supports the USB1.1 bus specification.

The topology of the USB bus is as shown in the following: the USB host controller (USB console) is connected to other USB devices through the root hub. The hub also belongs to a USB device, which can extend multiple interfaces on a USB interface. A maximum of 5 root hubs can be stacked outside the root hub. Up to 127 devices can be connected to a USB main line, including, of course, the root hub and other hubs. The entire structure is a star structure, all devices on a USB bus share a data channel to the host, only one device can communicate with the host at the same time.

Manage the external USB device via the USB host controller. USB controller is actually composed of USB controller hardware +USB Controller software. The USB host controller is divided into 3 kinds: UHCI, OHCI, EHCI. UHCI and OHCI Support USB1.1 protocol; EHCI supports USB2.0 protocol. UHCI, its hardware is simple, so the software is relatively complex, and OHCI, its hardware has more performance, contrary to the software to do less things. The s3c2440 USB host controller supports the OHCI host controller. The English language of HCI is all called (Host Controller Interface).

According to the above knowledge can be known in fact, the USB driver can be divided into two categories: USB host Controller driver (host controllers Drivers), USB device drivers (USB devices Drivers). They are in the kernel hierarchy as shown in

USB Host Controller driver provides access to the USB device interface, it is just a data channel, as to what the role of this data, this depends on the upper USB device driver to explain. The USB device driver uses the data interface provided by the lower-level driver to access the USB device without needing to be concerned about the specific transmission details. In Linux2.6, the USB host controller driver has been written for us, the following is a simple analysis of the USB host controller driver.

1, before analyzing the USB host controller driver to look at several issues:

When you connect your Android phone to a USB port on your computer via USB, you will see the following phenomenon

1, the bottom right corner pops up "found Andrido phone".

2. Jump out of a dialog box prompting you to install the driver.

Then the problem comes.

question 1, since there is no "driver", why do you know it is "Andrido phone"
A 1:windows has a USB bus device driver, after access to the USB device, is "bus driver" know you are "Andrido phone", you are prompted to install the "device driver", the USB bus driver is responsible for: Identify the USB device and find the appropriate driver for the USB device

Question 2, There are so many types of USB devices, why is it possible to recognize them when connected to a computer?
Answer 2: PC and USB devices are subject to some specifications.
For example, when the USB device is connected to the computer, the PC will send out "What are you?" "The USB device must answer" I am XXX "and the language of the answer must be Chinese;
The USB bus driver issues certain commands to obtain device information (descriptors), and the USB device must return "descriptor" to the PC.

Question 3, There are so many USB devices on the PC, how can I tell them apart?
Answer 3: each USB device connected to the USB bus has its own number (address), each USB device access to the PC, the USB bus driver will assign it a number, the PC wants to access a USB device, the command to be issued first contains the other person's number (address)

Question 4, When the USB device is just connected to the PC, it doesn't have a number, so how does the PC tell it the "Assigned number"?
Answer 4: The default number of the newly accessed USB device is 0, and the PC uses the 0 number and communicates with it before the new number is assigned.

question 5, why a USB device, the PC can find it (and no interruption)
Answer 5: The USB interface has only 4 lines: 5V, GND, D-, d+. the PC's USB port has a 15K pull-down resistor inside the D-and d+, a low-level USB device, and a USBdevice with a 1.5k pull-up resistor inside the USB port, D-or d+: It will put the PC USB port D (full speed) or d+ (High speed) pull high, from the hardware point of view to inform the PC has new device access.

From the above question and answer can be seen that the role of the USB bus driver can be summarized as: (will be explained in detail later)

1. Identification Equipment
2. Locate and install the corresponding USB device driver
3, provide USB reading and writing function (do not understand the meaning of data)

2, then put forward some of the concept of USB

1, USB is the master-slave structure
All USB transmissions are initiated from the USB host, the USB device does not have the ability to proactively notify the USB host
Example: A USB mouse swipe, immediately generate data, but it does not have the ability to notify the PC to read data, only passively waiting for the PC to be read

2, the transmission type of USB:
A, control transmission: Reliable, time is guaranteed, such as the identification process of USB devices
b, Batch transmission: Reliable, time is not guaranteed, such as: U disk
C, interrupt transmission (or query mode): Reliable, real-time, such as: USB mouse
D, real-time transmission: Data unreliable, real-time, such as: USB camera

3. USB-Transmitted object: Endpoint (Endpoint)
We say "Read U disk", "Write U disk" can be refined to: write the data to the USB flash drive Endpoint 1, the data from the end of the USB stick 2 read the data
In addition to endpoint 0, each endpoint supports only one direction of data transfer
Endpoint 0 is used to control the transmission and can be either output or input

4, each endpoint has only one transmission type, transmission direction

5, the term, the program said input (in), Output (out) "are" based on the position of the USB host said.
For example, the mouse data is passed from the mouse to the PC, the corresponding endpoint is called "Input endpoint"

3, through the previous description for the USB has a general understanding, and then through the program analysis to more in-depth understanding of the USB device identification process:

A USB mouse to the Development Board will pop up the following message:

Usb1-1:NewFull Speed USB DeviceusingS3C2410-OHCI and Address3USB1-1: Configuration #1Chosen from 1CHOICESCSI1:SCSI emulation forUSB Mass Storage devices Unplug USB1-1: USB Disconnect, address3then connect the USB1-1: USB Disconnect, Address 3usb1-1:NewFull Speed USB DeviceusingS3C2410-OHCI and Address4USB1-1: Configuration #1Chosen from 1CHOICESCSI2:SCSI emulation forUSB Mass Storage Devices

Where ADDRESS3,ADDRESS4 is the USB core program to the USB device assigned address. Find the "USB device using" keyword, found in line 2186 of DRIVERS/USB/CORE/HUB.C: "%s%s speed%susb device using%s and address%d\n". It is located under the hub_port_init function, and layer-by-layer lookups can be found that eventually the HUB_IRQ function causes the hub_port_init to be called. HUB_IRQ is not a real interrupt handler, it is just a callback function after the completion of the data receiving of the hub USB device, in fact, DRIVERS/USB/CORE/HUB.C is just a USB device driver.

The DRIVERS/USB/CORE/HUB.C program is first put aside and will be analyzed later. First try to find the driver of the USB controller, DRIVERS\USB\HOST\OHCI-S3C2410.C found the driver, it is the platform device driver as the framework, drivers\usb\host\ OHCI-S3C2410.C belongs to driver layer

Static structPlatform_driver Ohci_hcd_s3c2410_driver ={. Probe= Ohci_hcd_s3c2410_drv_probe,//once the match devices is called. remove =Ohci_hcd_s3c2410_drv_remove,. Shutdown=Usb_hcd_platform_shutdown,/*. Suspend = Ohci_hcd_s3c2410_drv_suspend,*/    /*. Resume = Ohci_hcd_s3c2410_drv_resume,*/. Driver={. Owner=this_module,. Name="S3C2410-OHCI",    },};

Then search for "S3C2410-OHCI", find the devices layer, located in ARCH\ARM\PLAT-S3C24XX\DEVS.C

 struct  platform_device s3c_device_usb = {. Name  =  s3c2410-ohci  "   =-1   = Array_size (S3c_usb_resource),. R esource  = S3c_usb_resource,. Dev  =s3c_device_usb_dmamask,. Coherent_dma_mask = 0xfffffffful  }};  

Here we have not found plug in the USB device after the first run function, we suspect that this function must be an interrupt, the USB controller receives the data will generate an interrupt to the MCU, and then the MCU processing. Now we are going to find this interrupt function, found in DRIVERS/USB/CORE/HUB.C usb_add_hcd function, which is located in usb_hcd_s3c2410_probe function, this function will be called once a USB controller device is matched.

Static int usb_hcd_s3c2410_probe (conststruct hc_driver *driver,                  struct Platform_device *dev) {    ...    ...     = USB_ADD_HCD (HCD, dev->resource[1].start, irqf_disabled); // Register OHCI Controller Interrupt    if 0 )        goto  err_ioremap;    ...    ...}

Keep looking down at the usb_add_hcd function, which is located under DRIVERS\USB\CORE\HCD.C

intUSB_ADD_HCD (structUSB_HCD *HCD, unsignedintIrqnum, unsignedLongirqflags) {    ...    ...        if(retval = Request_irq (Irqnum, &Usb_hcd_irq, Irqflags, HCD->IRQ_DESCR, HCD))! =0) {//Interrupt USB InterruptDev_err (hcd->Self.controller,"Request Interrupt%d failed\n", Irqnum); GotoErr_request_irq; } HCD->IRQ =Irqnum; ...    ...} 

Here you see the call registration Interrupt function, and its interrupt handling callback function is USB_HCD_IRQ, which is also located under DRIVERS\USB\CORE\HCD.C

Irqreturn_t USB_HCD_IRQ (intIrqvoid*__HCD) {    structUSB_HCD *HCD =__HCD; intStart = hcd->State ; if(unlikely (start = = Hc_state_halt | | !test_bit (hcd_flag_hw_accessible, &hcd->flags))) returnIrq_none; if(HCD->DRIVER->IRQ (HCD) = =Irq_none)returnIrq_none; Set_bit (HCD_FLAG_SAW_IRQ,&hcd->flags);//Set Interrupt Flag    if(Unlikely (hcd->state = =Hc_state_halt))    Usb_hc_died (HCD); returnirq_handled;}

By doing so, the USB_HCD_IRQ function will eventually invoke the Hub_irq function under DRIVERS/USB/CORE/HUB.C. Said before that DRIVERS/USB/CORE/HUB.C is actually a USB device driver, it is actually a root hub driver. The procedure for matching USB device drivers is directly listed here.

HUB_IRQ kick_khubd hub_thread hub_events hub_port_connect_change Usb_alloc_dev (Hdev, Hdev-bus, Port1); Dev->dev.type = &Usb_device_type;  Choose_address (Udev); //Assign a number (address) to a new deviceHub_port_init//USB 1-1: New full speed USB device using S3C2410-OHCI and address 3Hub_set_address//tell the USB device the number (address)Usb_get_device_descriptor (Udev,8);//Get Device DescriptorUsb_get_device_descriptor (Udev, usb_dt_device_size);                        Usb_new_device (Udev); Usb_get_configuration (udev);//Read all the descriptors and parse them.usb_parse_configuration Device_add//Put the device into the Usb_bus_type dev list,//Remove the Usb_bus_type from the driver list Usb_driver//Compare the id_table of Usb_interface and Usb_driver//if it matches, call Usb_driver's probe function

Briefly summarize:

1, when connected to the USB mouse, the USB host controller to check the D-or d+ interface to become high know that there is a device access.

2, the USB host controller to the new assigned number to tell the USB device, this time or 0 address communication.

3, the USB host controller to 0 address from the USB device to obtain the first 8 bytes of the device descriptor, from which 8 bytes can know the total size of all device descriptors behind

4, know the remaining device descriptor size, you can read the remaining device descriptors

5. Then put the device into the Usb_bus_type's dev link list

6. Remove the Usb_bus_type from the driver list Usb_driver

7, the Usb_interface and usb_driver of the id_table comparison (for the USB mouse is the ultimate comparison of the interface descriptor under the Binterfaceclass;binterfacesubclass; Binterfaceprotocol; three information, Chinese translation for device type, device subtype, device protocol)

8, if can match, call Usb_driver's probe function

As you can see from the previous summary, the driver for the USB device needs to be initialized for the usb_driver structure.

The relationship between the USB descriptors is as follows, and the specific descriptor structure will be explained in the next section

The above is the USB bus driver framework, USB device is a very complex thing, here is just a simple description, not in-depth understanding. The purpose is to write USB device drivers.

A brief analysis of Linux driver framework for USB bus driver

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.