USB device driver 3: Root hub daemon 2

Source: Internet
Author: User

After the hub is working properly, the master controller regularly queries whether the hub is interrupted. When a device is inserted or removed on the hub port, the hub sends a urb request to the master controller, that is, to tell the master controller about the change of the hub port, this is done through the urb request. After the host finishes processing the urb, it will call the completion function provided by urb, to call the hub interrupt function, that is, hub_irq.

 

Hub_irq is the interrupt processing function of the hub. The Handler first checks the status of the result of the master controller processing the urb. If the status is OK, the processing continues.

1. scan all the ports of the hub to determine which port has changed. A port is represented by a bitmap. A long data can represent 32 hub ports (each representing a port, the 8-bit is represented by one byte. Therefore, the last bit is converted to a byte. For example, if a hub has 18 ports, it must be represented by 18 bits, however, a single byte only has 8 bits. Therefore, it must be 3 bytes to complete.

2. Call the kick_khubd function, add the current hub to the Hub driver queue hub_event_list, and then wake up the hub daemon wake_up (khubd_wait) to start parsing what happened to the hub.

3. After hub_thread is awakened, It will be executed and hub_events will also be executed.

4. hub_events is the main function used to analyze hub events. The content of hub analysis is executed here. This function is a large endless loop.

5. hub_events processing process

5.1 first retrieve the hub node to be processed from hub_event_list, and delete the hub node from the original queue so that it is independent from any linked list, because after processing the event, the Hub struct must be deleted, so it cannot be retained in any queue. The hub can be a root hub or a sub-hub on the Root hub. However, no matter What hub, the same daemon process is used.

5. 2. through macro conversion, find the hub structure of the hub node pair, and then obtain the USB structure corresponding to the hub structure and the hub interface structure usb_intfdev. The key to processing this function is to obtain these three structures.

5. 3. lock the current hub tree because the hub has only one daemon, and all the hubs use this daemon. The daemon can only serve one hub at a time. Therefore, as long as one hub is using this daemon, it needs to be locked to prevent other hubs from being used.

5. 4. perform reset or resume operations on each port of the hub by bit. Note that the reset or resume operations are performed from 1. Bit 0 indicates the entire hub. If the current port is performing reset or resume operations, skip the check on this port. Otherwise, test whether the status of this port has changed. If the status is not changed, skip this port.

5. If the status of the port changes, determine the status of the port.

# Define usb_port_stat_c_connection 0x0001

# Define usb_port_stat_c_enable 0x0002

# Define USB _port_stat_c_suspend 0x0004

# Define USB _port_stat_c_overcurrent 0x0008

# Define usb_port_stat_c_reset 0x0010

 

5. 6. after the status change is confirmed, further processing is started for the status of the changed port. At this time, processing is performed separately based on the various statuses just analyzed.

5. 7. first, you must determine whether the port has a device. If yes, delete the device because there are two statuses on the hub port, 1 indicates that the port is in the status from no device to a device. At this time, the device is still being authenticated. Therefore, the port should not have a device. 2 indicates that the port is switched from a device to no device, A device is detected, indicating that the device has not been removed. Therefore, you need to disable the device directly.

5. 8. to disable a device, a bounce check is required. A device must be in a period of at least ms to indicate that the status is stable. Therefore, wait ms and then judge whether the port is disable.

5. 9. when judging whether the device is in a state of no device, you need to check whether the port power supply is disable. If yes, you need to enable the port power supply. If the power supply is enabled, it indicates that the device is incorrect, end the judgment and return to the upper layer.

5. 10. after the above judgment, it can be executed here, indicating that the port has been inserted by a device. In this case, you need to analyze the port status. For port analysis, you can try it four times, it is mainly used to eliminate various interference.

5. 11. allocate space (struct usb_device) for the coming device. Set the connection status for the device structure of the coming device, the speed is unknown, and the power supply is allocated to the hub, these are default statuses.

5. 12. prepare to assign an address to the device. In this case, the sub-system software operation is to check which one is 0 in the 128-bit diagram on the bus, the method of searching is to start from the next digit recorded last time. If the number exceeds 128, it is not found, and then start from 0, that is, if the USB device is frequently plugged in, even if there is only one device, the device address will be increased by 1 each time it is inserted until 128 is reached and starts from 0. The device address at this time is not the real address of the device, because it has not been sent to the device.

5. 13. reset the device to enable the device. If the device is successfully reset, the status of the device changes to usb_state_default, the master controller can get the device descriptor through the control port. By parsing the device descriptor, the speed of the device can be obtained, so that the space of the port can be controlled based on the speed.

. After obtaining the control endpoint size, you are ready to send an urb request to the control endpoint to obtain the real device descriptor of the device.

. Two modes are used to obtain the device descriptor: the new mode and the old mode. Each mode can be tried twice at most, and a maximum of three ports can be read at a time. For the new mode, you need to allocate a 64-byte space to receive the device descriptor returned from the device. The device descriptor obtained is used to determine whether the port category is indeed a device category. If yes, you only need to read the port once to determine whether it is a device descriptor, the space of the port recorded in the descriptor is assigned to the corresponding byte of the space just applied.

200. Reset the device. Prepare to set the address selected by the system but not set for the device. You can try setting the address for up to two times, with a length of Ms.

5. 17. once the configuration is successful, the device status should be changed to the address status (usb_state_address), and the control port 0 should be dropped to disable. In this way, the urb linked list on this port will be cleared.

5. 18. if the new mode is available, it indicates that the device descriptor is successfully obtained in the new mode. Therefore, do not try the old mode, in the old mode, the device descriptor is obtained twice. The first request is to send an 8-byte urb request, you can know the port size of the device, and then obtain the real device descriptor of the port according to the obtained port size.

5. 19. Next, add the device to the device mode (usb_new_device ).

. First, you need to obtain the configuration descriptor of the device. Note that only the device has configuration, interfaces, ports are not configured, and interfaces are configured. How many configurations are allocated to the device for receiving and how many configurations have been obtained in the device descriptor, in this case, you only need to read from the device.

5. 21. Set the configuration descriptor pointer. How many pointers are configured?

. A Configuration descriptor has nine bytes. In this case, you need to allocate this space to receive the configuration descriptor read back from the device.

5. 23. read the configuration descriptor of the device cyclically and place the read-back data in the corresponding location of the device structure. Then, obtain the number of interfaces, interface settings, and endpoints under the interface from the configuration, these functions are analyzed here.

5. 24. next, add the device to the device model. This is done by the device model, including adding the device linked list and searching for the device driver, this is the driver of the device. The USB system has only one device driver and is ready for use. When the interface in the device is added, we compile the driver, it is called here to determine whether the driver conforms to the device.

. Scan all configurations of the device, select the optimal current configuration, and then use the selected configuration to set the device.

. Now, the device configuration, settings, and everything are properly configured, and the system can use the device normally. 

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.