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

Source: Internet
Author: User
Structbus_type provides two linked lists for the device and driver, and the struct device of the device contains two members: structbus_type * bus and structdevice_driver * driver. Similarly, structdevice_driver represents the drive structure...

In struct bus_type, two linked lists are prepared for the device and the driver. The struct device contains two members, struct bus_type * bus and struct device_driver * driver. Similarly, the struct device_driver, which represents the driver, has two members: struct bus_type * bus and struct list_head devices. The definition of struct device and struct device_driver is the same as that of struct bus_type, in include/linux/device. h. Intuitively, you can know that the bus in the struct device records the bus on which the device is connected, and the driver records the driver used by the device. In turn, the bus in struct device_driver represents the bus to which the driver belongs, and the devices records the devices supported by the driver. That's right, it's devices (plural), not device (singular), because a driver can support one or more devices, and the other is bound to only one driver.

So we want to know how to establish connections between bus, device, and driver? In other words, how are pointers assigned to these three data structures? What is absolutely impossible is that once a struct bus_type data structure is applied for a bus, it will know what its devices linked list and drivers linked list will contain, these things will not be inherent, but can only be entered later.

Specifically, the USB Core is used to complete this task. The USB Core code initializes the entire USB system, such as applying for struct bus_typeusb_bus_type, and then scans the USB bus to see which USB devices are connected online. Or which USB devices are connected to the Root Hub, for example, a USB keyboard, a struct device is prepared for the device. assign a value to the struct device based on the actual situation, and insert it into the devices linked list. For example, if a common Hub is connected to the Root Hub, in addition to preparing a struct device for the Hub itself, you have to continue scanning to see if another device is connected to the Hub, if so, repeat the previous tasks until the entire scan is completed, and finally the devices linked list in usb_bus_type is created.

What about drivers linked list? In this case, you do not need to take the initiative in the bus, and each driver should register on the bus, or register on the board. Specifically to the USB system, each driver of a USB device has a struct usb_driver structure. the code is as follows, from include/linux/usb. h:

833 struct usb_driver {

834 const char * name;

835

836 int (* probe) (struct usb_interface * intf,

837 const struct usb_device_id * id );

838

839 void (* disconnect) (struct usb_interface * intf );

840

841 int (* ioctl) (struct usb_interface * intf, unsigned int code,

842 void * buf );

843

844 int (* suspend) (struct usb_interface * intf, pm_message_t message );

845 int (* resume) (struct usb_interface * intf );

846

847 void (* pre_reset) (struct usb_interface * intf );

848 void (* post_reset) (structusb_interface * intf );

849

850 conststruct usb_device_id * id_table;

851

852 structusb_dynids dynids;

853 struct usbdrv_wrap drvwrap;

854 unsignedint no_dynamic_id: 1;

855 unsignedint supports_autosuspend: 1;

856 };

857 # define to_usb_driver (d )\

Container_of (d, struct usb_driver, drvwrap. driver)

At this moment, we only need to notice the structdevice_driver driver member. USB Core has prepared a function for each device driver to insert the struct device_driver into the drivers linked list in usb_bus_type. This function is the usb_register we have seen before. The corresponding usb_deregister function is engaged in the opposite work, removing this struct from the drivers linked list.

It can be said that USB Core is indeed painstaking, and it has done a lot of work for every USB device driver. because of this, as an actual USB device driver, it has very little to do in the initialization phase, you can simply call usb_register. In fact, no one should do anything for you, but USB Core does. Therefore, anyone who writes a USB driver should remember that a USB driver is not working alone. behind it, it is an obscure and indispensable support provided by USB Core.

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.