I am USB in Linux. I am a USB flash drive (9) bus, device, and driver (bottom)

Source: Internet
Author: User
The two linked lists on the bus record every device and driver. how is the connection between the device and the driver? At this moment, the problem must be thrown out. Is there a device or a driver first? In the past, there were first devices. every device to be used was inserted before the computer started... information & n

The two linked lists on the bus record every device and driver. how is the connection between the device and the driver? At this moment, the problem must be thrown out. Is there a device or a driver first?

In the past, there were first devices. each device to be used was plugged in before the computer started, put it in the location where it should be, then the computer started, and then the operating system started initialization, the bus starts scanning the device. Each time a device is found, a struct device structure is applied for it and mounted to the devices linked list in the bus. then, each driver starts initialization and registers its struct device_driver structure, then it goes to the devices linked list of the bus to find (Traverse) each device that has not been bound with a driver, that is, the struct device_driver pointer in the struct device is still null, then it will observe the characteristics of such a device to see if it is a device that he supports. if so, it will call a function called device_bind_driver, and then it will become better for Qin Jin. In other words, point the struct device_driver driver in the struct device to this driver, while the struct device_driver adds the struct device to its struct list_head devices linked list. In this way, the bus, device, and driver, or the two of them, are connected. If you know one of them, you can find the other two. The loss is always high.

But now the situation has changed, and a new term "hot swapping" emerged ". The device can be inserted or pulled out after the computer is started. Therefore, it is difficult to say whether there is a device or a driver first. Because it is possible. The device can appear at any time, and the driver can be loaded at any time. Therefore, whenever a struct device is born, it will go to the drivers linked list of the bus to find its other half. On the contrary, whenever one structdevice_driver is born, it goes to the devices linked list of the bus to find its devices. If appropriate, call device_bind_driver to bind the device. If it cannot be found, it does not matter. wait.

In fact, perfecting this triangular relationship is one of the important missions completed in each device driver initialization phase. Let's go back to the code. the usb_register function is called, but what is the parameter passed to it?

We noticed that the code for calling the usb_register function is as follows:

1073/* registerthe driver, return usb_register return code if error */

1074 retval = usb_register (& usb_storage_driver );

Yes, I passed a guy named usb_storage_driver. What is this? In the same file:

1055 static struct usb_driver usb_storage_driver = {

1056. name = "usb-storage ",

1057. probe = storage_probe,

1058. disconnect = storage_disconnect,

1059 # ifdef CONFIG_PM

1060. suspend = storage_suspend,

1061. resume = storage_resume,

1062 # endif

1063. pre_reset = storage_pre_reset,

1064. post_reset = storage_post_reset,

1065. id_table = storage_usb_ids,

1066 };

We can see that this defines a struct variable of struct usb_driver, usb_storage_driver. we have mentioned about usb_driver in the previous section. at that time, we mainly talked about the member driver, now we will talk about several other members. First, the owner is used to count the module. every module is used in this way, and the value assignment is always THIS_MODULE. The name is the name of this module, and USB Core will process it. Therefore, if this module is loaded normally, you can use the lsmod command to see a module name called usb-storage. CONFIG_PM is related to power management. Next we will focus on the introduction of. probe and. disconnect and this id_table.

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.