Learn some USB hardware knowledge before learning USB Hot swap:
First, the USB basic concept
1, hardware knowledge (USB socket and plug)
In the initial standard, the USB connector has 4 lines: power supply, d-,d+, ground . Let's call this a standard USB connector for the moment. Later, OTG appeared and added the MINIUSB connector. And the MINIUSB Connector has 5 lines, more than one ID line, used to identify the identity. Standard USB ports are available in type A and type B. Each type is divided into plugs and sockets, such as a-type plug, a-type receptacle. The kind of socket we use on our computer is called a-type USB socket, and the corresponding plug is called a-type plug, such as a USB stick. And like the printer above that outlet, is a B-type socket (compared to the Quartet, no computer above the kind of flat), the corresponding plug, is the B-type plug. Perhaps you have seen a square a flat USB cable, yes, the flat of the head is called a-type plug, and the side of the head, is called the B-type plug, and the corresponding plug in the two sockets, is a-type receptacle and type B socket. Type a plugs are not plugged into a type B socket and vice versa.
miniUSB is also divided into a type, B type, but added an AB type (not blood type Ah, do not make mistake, no O-type ^_^). Since it is called MINIUSB, then of course it is very small, mainly for portable devices, such as MP3, mobile phones, digital cameras and so on. USB is a master multi-slave structure that can only have one host at a time. Like a PC is a host, the other can only be devices, so the two devices can not communicate directly between. The advent of USB OTG (on the Go) solves this contradiction: a device can change identities in some situations and appear as a host. Thus there is an AB type of MINIUSB socket, whether it is a-type MINIUSB plug, or B-type miniUSB plug, can be inserted in, and the more out of the ID line to identify its identity: is the host or from the machine. This allows the two USB devices to be connected directly for data transfer. Like our MP3 on the MINIUSB socket, is the B-type MINIUSB socket (note that there is a kind of miniusb socket, it does not seem to be the USB specification inside, because the MINIUSB connector should have 5 lines, and this socket only 4 lines). Because USB is hot-swappable, it also has a corresponding measure in the design of the connector. The ground and power pins of the USB plug are longer, while the two data pins are shorter, so that when plugged into the socket, power and ground are plugged in and then two data cables are connected. This ensures that the power supply is plugged in before the data cable, preventing the latch from occurring. As for the USB cable, usually we do not care, buy ready-made on the line, unless you are the production of USB cable. Shielded twisted pair cables are required in full speed mode, while low-speed mode mode can be used without shielding and twisting. In addition, the USB protocol stipulates that the length of the USB low speed cable must not exceed 3 meters, and the full speed cable length must not exceed 5 meters. This is because there is a delay in the transmission of the cable and it is not possible to delay too much to ensure that it responds correctly. The USB standard specifies the color of the inside signal line, where Vbus is red, D is white, d+ is green, and GND is black. However, I have seen a lot of USB cable does not follow the standard, so you should be careful when using the table to measure the more reliable.
2, the hub to the USB Device connection report to the USB host controller
First, the d+ and D-on each downstream port of the USB hub are respectively connected to the ground by a 15K ohm pull-down resistor. This way, when the hub port is dangling, the two pull-down resistors are pulled to the low level. On the USB device side, a 1.5K ohm pull-up resistor is connected to the d+ or D. For full speed and high speed devices, the pull-up resistor is connected to the d+, while the low-speed device is the pull-up resistor on the D-top. In this way, when the device is plugged into the hub, it is divided by a 1.5K pull-up resistor and a 15K pull-down resistor, resulting in a higher bar in the differential data line . when the hub detects this state, it reports to the USB host controller (or to the USB host controller via its previous layer of hub), which detects the insertion of the device . The USB high-speed device is first recognized as a full-speed device and then switched to high-speed mode by confirming between host and device. In the high-speed mode, is the current transmission mode, at this time will d+ on the upper pull power block open.
Second, what is hot-swappable
1. Basic concept
Hot-swappable (hot-plugging, hot swap) is plug -and-unplug, which allows the user to remove and replace damaged hard drives, power supplies, or board parts without shutting down the system, without disconnecting the power supply. This improves the system's ability to restore disaster in a timely manner, scalability and flexibility, for example, some disk mirroring systems for high-end applications can provide disk hot-swappable functionality. Specifically, the academic argument is: Hot replacement, hot expansion, and hot upgrade.
2. Hot Plug Benefits
The benefits of adding hot swapping to the system include:
The damaged modules are removed when the system is powered on, and can be updated or scaled up without affecting system operation.
Due to the increased reliability of hot-swappable parts, they can also be used as broken appliances, and because hot-swappable can be automatically restored, there are many hot-swappable chips to provide the system with the signal of line power, so that the system to do fault analysis, thus reducing the cost.
Third, Linux under the USB hot-swap processing
1, Linux under the USB hub driver implementation and analysis:
When the system initializes, the usb_hub_init function is called in the usb_init function , and the hub initialization is entered.
registered hub drivers are completed in the Usb_hub_init function , and a kernel thread is created using the function Kthread_run . This thread is used to manage the status of the monitor hub , and all the cases are reported through the threads.
USB device is hot plug, which is different from the PCI device, the PCI device is fixed when the system starts, so the PCI device only need to initialize the enumeration can be, using recursive algorithm. USB devices require hot-swapping, so call the hub_configure function in the hub_probe function To configure the hub, where the function usb_alloc_urb function is used primarily To allocate a URB, use usb_fill_int_urb to initialize the URB structure, including the hub's interrupt service program HUB_IRQ, the query cycle, and so on.
whenever a device is connected to a USB port, theUSB bus will trigger the hub's Interrupt service program HUB_IRQ when querying the hub status information, using KICK_KHUBD to pass the hub structure through EVENT_ The list is added to the KHUBD queue hub_event_list, and then the KHUBD is awakened. Enter the Hub_events function, which is used to process each usb_hub data structure in the KHUBD event queue from KHUBD hub_event_list. The function first determines if the hub is faulted, and then detects the status information for each port through a for loop. Use usb_port_status to get the port information, and call the hub_port_connect_change function to configure the port if any changes occur.
2. Software Level Analysis
Here we first talk about the processing of USB hot plug events. ---khubd daemon.
-KHUBD Daemon It is a daemon that checks the USB port for event notification HCD and USB core, and then does the appropriate processing.
Driver Catalog drivers/usb/*
Usb/serial USB Serial device driver (e.g. USB 3G card, Bluetooth, etc.)
Usb/storage USB Large Storage disk drive (U-disk)
Usb/host USB Host Controller driver (embedded OTG:DWC_OTG)
Usb/core USB Core Some processing code, all the driver-related processing is here, also registered to it.
USB/USB-SKELETON.C Classic USB Customer-driven framework, you can refer to
Of course there are others here that are no longer explained.
The overall drive frame of the USB is posted below:
Here we mainly analyze the working principle of Khub: The hardware level is the work of the hub, how to communicate with host and its devices and corresponding events
[USB/CORE/HUB.C]
int Usb_hub_init (void) {if (Usb_register (&hub_driver) < 0) {PRINTK (kern_err "%s:can ' t Register hub driver\n", Usbcore_name); return-1;} Khubd_task = Kthread_run (Hub_thread, NULL, "KHUBD"), if (!is_err (khubd_task)) return 0;/* Fall through if Kernel_thread FAI LED */usb_deregister (&hub_driver);p rintk (kern_err "%s:can ' t start khubd\n", usbcore_name); return-1;}
Here we only care about Kthread_run (Hub_thread, NULL, "khubd"); And then we look at the Hub_thread function
static int hub_thread (void *__unused) {do {hub_events (); Wait_event_interruptible (Khubd_wait,!list_empty (&hub_ Event_list) | | Kthread_should_stop ()); Try_to_freeze ();} while (!kthread_should_stop () | |!list_empty (&hub_event_list));p r_debug ("%s:khubd exiting\n", usbcore_name); return 0;}
Here we see the hub_events () function. Then set the run state and add hub_event_list if there is an event. Since then the KHUBD has run.
Here we also posted its function call flowchart (here too lazy to draw, cut a ^ ^)
Through the flowchart we can clearly understand, when the USB device plugged into the USB interface, KHUBD run, it detects the port status changes, call Hub_port_connect_change (), if it is a new device so Usb_allco_dev, Then call Usb_new_device to configure the USB device to work properly . Detailed process please see source code.
Iv. The enumeration process of USB
The kernel worker thread khubd is used to monitor all the ports that are connected to the hub, and normally the thread is dormant, and when the hub driver detects a change in the state of the USB port, the kernel line Cheng Lima wake up .
USB Enumeration Process: The USB enumeration process is the starting step of a hot-swappable USB device, during which the host controller obtains information about the device and configures the device, and the hub driver is responsible for the enumeration process .
The enumeration process is mainly divided into the following steps:
STEP1: The root hub reports a change in the port current caused by the insertion device, and after the hub driver detects this state change, wakes up the KHUBD thread.
The STEP2:KHUBD identifies the port on which the current changes.
STEP3:KHUBD by sending control URB to control endpoint 0 to select a number from 1-127 as the bulk endpoint of the insertion device
The STEP4:KHUBD uses the control URB used by port 0 to obtain the device descriptor from the inserted device, then obtains the configuration descriptor and selects a suitable one.
STEP5:KHUBD requests the USB core to hook the corresponding customer driver with the USB device .
Linux USB Driver Development (iv)--hot swap that thing