Note: This article is temporarily not flow chart, if there is a demand to do follow-up.
1. The need to prepare a list of source files:
base section:
kernel\base\core.c
kernel\base\bus.c
kernel\base\dd.c
kernel\base\class.c
kernel\base\driver.c
Header section:
kernel\include\linux\device.h
kernel\include\linux\usb.h
kernel\include\scsi\scsi_host.h
usb core components:
kernel\driver\usb\core\usb.c
kernel\driverusb\core\driver.c
kernel\driverusb\core\hub.c
kernel\driverusb\core\driver.c
kernel\drivers\usb\core\message.c
kernel\drivers\usb\core\generic.c
Part mass:
kernel\driverusb\storage\usb.c
scsi section:
kernel\driverscsi\scsi_scan.c
kernel\driverscsi\scsi_sysfs.c
kernel\driverscsi\sg.c
2. when things happen before a U disk into linux device:
. A hub portion of the beginning of registration:
There is need to focus on the registration drive hub, usb, usb-storage. hub of whether there is used for access detection Eastern OTG usb port, usb Big Brother usb access to all devices, usb-Storage only usb a little brother.
Turn kernel \ driver \ usb \ core \ usb.c source, a hub drive here to register, re-register the usb drive.
Note: The code "..." means to ignore this part of the code, posted only need to focus on the code.
int the __init usb_init static (void) { ... retval = usb_hub_init (); // Sign hub drive ... }
Look at the hub registration process to open the kernel \ driverusb \ core \ hub.c,
static struct usb_driver hub_driver = { .name = "hub", ... }; 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; } ... }
First concern usb_register, later omitted attention again to open the kernel \ include \ linux \ usb.h,
/* use a define to avoid include chaining to get THIS_MODULE & friends */ #define usb_register(driver) \ usb_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
Then enter the kernel \ driverusb \ core \ driver.c in
int usb_register_driver(struct usb_driver *new_driver, struct module *owner, const char *mod_name) { ... new_driver->drvwrap.driver.name = (char *) new_driver->name; new_driver->drvwrap.driver.bus = &usb_bus_type; new_driver->drvwrap.driver.probe = usb_probe_interface; ... retval = driver_register(&new_driver->drvwrap.driver); if (retval) goto out; ... } EXPORT_SYMBOL_GPL(usb_register_driver);
Driver_register achieved in the kernel \ base \ driver.c in
int driver_register(struct device_driver *drv) { ... ret = bus_add_driver(drv); ... }
bus_add_driver implemented in the kernel \ base \ bus.c in
int bus_add_driver(struct device_driver *drv) { ... error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL, "%s", drv->name); ... klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers); ...
module_add_driver(drv->owner, drv);
...
}
This process is probably the hub driver added to a list, because the list is used to do data manipulation, the basic is to add, delete, modify, traverse to find, when used in the follow-up to repeat, this is part of the registration hub a.
. B registered usb part:
Open kernel \ driver \ usb \ core \ usb.c, on-line registration at 3 hub-driven, registered a usb device driver,
static int __init usb_init(void) { ... retval = usb_register_device_driver(&usb_generic_driver, THIS_MODULE); ... }
Then enter the kernel \ driverusb \ core \ driver.c in,
int usb_register_device_driver(struct usb_device_driver *new_udriver, struct module *owner) { ... new_udriver->drvwrap.driver.name = (char *) new_udriver->name; new_udriver->drvwrap.driver.bus = &usb_bus_type; new_udriver->drvwrap.driver.probe = usb_probe_device; ... retval = driver_register(&new_udriver->drvwrap.driver); ... } EXPORT_SYMBOL_GPL(usb_register_device_driver);
Went to the driver_register, eventually it is to add a usb device driver to the list, waiting for the moment to traverse execution.
. C usb-storage part of the registration:
Open usb \ storage \ usb.c, where registered driver usb-storage. This drive is associated with the node U disk.
static struct usb_driver usb_storage_driver = { .name = "usb-storage", ... }; module_usb_driver(usb_storage_driver);
We can look at its implementation open kernel \ include \ linux \ usb.h,
#define module_usb_driver(__usb_driver) \ module_driver(__usb_driver, usb_register, \ usb_deregister)
Can \ device.h see the implementation module_driver in the kernel \ include \ linux,
#define module_driver(__driver, __register, __unregister, ...) \ static int __init __driver##_init(void) \ { \ return __register(&(__driver) , ##__VA_ARGS__); \ } \ module_init(__driver##_init); \ static void __exit __driver##_exit(void) \ { \ __unregister(&(__driver) , ##__VA_ARGS__); \ } \ module_exit(__driver##_exit);
Is a macro, registered with usb_register, reverse registration with usb_deregister, then module_init it will be executed at boot time. As usb_register, eventually usb-storage drive is to add to a linked list, waiting for the moment to traverse execution.
3. When the disk is inserted into a U linux device:
a. the need for a U disk into the thread waits to detect and return kernel \ driverusb \ core \ hub.c,
int usb_hub_init(void) { ... khubd_task = kthread_run(hub_thread, NULL, "khubd"); ... }
static int hub_thread(void *__unused) { ... do { hub_events(); wait_event_freezable(khubd_wait, !list_empty(&hub_event_list) || kthread_should_stop()); } while (!kthread_should_stop() || !list_empty(&hub_event_list)); ... }
hub_events void static (void) { ... the while (1) {
...
HDEV = hub-> HDEV; // Here's a usb drive equipment acquisition process, ignore, because I have not carefully studied
... IF ( connect_change) hub_port_connect_change (Hub, I, portstatus, portchange); } ... }
static void hub_port_connect_change(struct usb_hub *hub, int port1, u16 portstatus, u16 portchange) { ... /* Run it through the hoops (find a driver, etc) */ if (!status) { status = usb_new_device(udev); ... }
int usb_new_device(struct usb_device *udev) { ... err = device_add(&udev->dev); ... }
Into the kernel \ base \ core.c in
int device_add(struct device *dev) { ... bus_probe_device(dev); ... }
Into the kernel \ base \ bus.c in
void bus_probe_device(struct device *dev) { ... ret = device_attach(dev); ... }
Into the kernel \ base \ dd.c in
int device_attach(struct device *dev) {
... ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach); ...
}
int bus_for_each_drv(struct bus_type *bus, struct device_driver *start, void *data, int (*fn)(struct device_driver *, void *)) {
... error = fn(drv, data); ... }
static int __device_attach(struct device_driver *drv, void *data) { ... return driver_probe_device(drv, dev); }
int driver_probe_device(struct device_driver *drv, struct device *dev) { ... ret = really_probe(dev, drv); .... }
static int really_probe(struct device *dev, struct device_driver *drv) { ... } else if (drv->probe) { ret = drv->probe(dev); if (ret) goto probe_failed; } ... }
Prior to insertion of the list usb device driver probe was traversed out at the moment, then called.
Recalling the function pointer is inserted to open the kernel \ driverusb \ core \ driver.c,
int usb_register_device_driver(struct usb_device_driver *new_udriver, struct module *owner) { ... new_udriver->drvwrap.driver.probe = usb_probe_device; ... }
enter
static int usb_probe_device(struct device *dev) { struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); ... error = udriver->probe(udev); ... }
By the kernel \ include \ linux \ usb.h in:
#define to_usb_device_driver(d) container_of(d, struct usb_device_driver, \ drvwrap.driver)
And kernel \ driver \ usb \ core \ usb.c in:
retval = usb_register_device_driver(&usb_generic_driver, THIS_MODULE);
Shows
probe calls the probe usb_generic_driver here, because of the role container_of is to return a pointer to point d, shall be returned pointer & usb_generic_driver.
Open kernel \ drivers \ usb \ core \ generic.c,
struct usb_device_driver usb_generic_driver = { .name = "usb", .probe = generic_probe, ... };
static int generic_probe(struct usb_device *udev) { ... err = usb_set_configuration(udev, c); ... }
Open kernel \ drivers \ usb \ core \ message.c,
int usb_set_configuration(struct usb_device *dev, int configuration) { ... ret = device_add(&intf->dev); ... }
We mentioned before device_add-> bus_probe_device-> device_attach -> __ device_attach-> driver_probe_device-> really_probe-> device driver corresponding incoming probe.
Before usb_set_configuration Or, there must be a process of obtaining information usb-storage drive, in short, this time the probe will enter usb_probe_interface, the drive is previously registered usb-storage.
Open kernel \ driverusb \ core \ driver.c,
static int usb_probe_interface(struct device *dev) {
struct usb_driver *driver = to_usb_driver(dev->driver); ... error = driver->probe(intf, id); ... }
As before container_of returns a pointer to a pointer p analysis, like the pointer returned is & usb_storage_driver.
Open kernel \ driverusb \ storage \ usb.c,
static int storage_probe(struct usb_interface *intf, const struct usb_device_id *id) { ...
result = usb_stor_probe1(&us, intf, id, unusual_dev);
... result = usb_stor_probe2(us); ... } static struct usb_driver usb_storage_driver = { .name = "usb-storage",
.probe = storage_probe, ... };
int usb_stor_probe1(struct us_data **pus, struct usb_interface *intf, const struct usb_device_id *id, struct us_unusual_dev *unusual_dev) { ... INIT_DELAYED_WORK(&us->scan_dwork, usb_stor_scan_dwork); ... }
int usb_stor_probe2(struct us_data *us) { ... queue_delayed_work(system_freezable_wq, &us->scan_dwork, delay_use * HZ); ... }
This is usb_stor_probe1 a delay in registering the job queue, and then wake up the job queue usb_stor_probe2 registered function usb_stor_scan_dwork work.
static void usb_stor_scan_dwork(struct work_struct *work) { ... scsi_scan_host(us_to_host(us)); ... }
The next step is scsi subsystem work.
b. Create sg nodes.
Open kernel \ driverscsi \ scsi_scan.c,
void scsi_scan_host(struct Scsi_Host *shost) { ... async_schedule(do_scan_async, data); ... }
static void do_scan_async(void *_data, async_cookie_t c) { ... scsi_finish_async_scan(data); }
static void scsi_finish_async_scan(struct async_scan_data *data) { ... scsi_sysfs_add_devices(shost); ... }
static void scsi_sysfs_add_devices(struct Scsi_Host *shost) { ... if (!scsi_host_scan_allowed(shost) || scsi_sysfs_add_sdev(sdev) != 0) __scsi_remove_device(sdev); } }
Open kernel \ driverscsi \ scsi_sysfs.c,
int scsi_sysfs_add_sdev(struct scsi_device *sdev) { ... error = device_add(&sdev->sdev_dev); ... }
Note: This biography is & sdev-> sdev_dev, rather than & sdev-> sdev_gendev
Went to the device_add, this is not so simple really_probe go directly show the critical code,
Open kernel \ base \ core.c,
int device_add(struct device *dev) { ... if (class_intf->add_dev) class_intf->add_dev(dev, class_intf); ... }
add_dev which class_interface calls?
Open kernel \ driverscsi \ sg.c
static int __init init_sg(void) { ... rc = scsi_register_interface(&sg_interface); ... }
static struct class_interface sg_interface = { .add_dev = sg_add, .remove_dev = sg_remove, };
Call add_dev is known sg_add, the node sg following code is created.
static int sg_add(struct device *cl_dev, struct class_interface *cl_intf) { ... sdp = sg_alloc(disk, scsidp); ... }
static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp) { ... sprintf(disk->disk_name, "sg%d", k); ... }
Source too much, I spent a lot of time to stroke clear.
In general is, registered a bunch of stuff, bus (usb) ah, the drive device (usb) ah, drive (hub, usb-storage) ah, class (sg_interface) ah, etc., and then run a thread, east to the needs detected after the East, than the registration data specific to the list, and then invoke various probe and registration interfaces such as add_dev and so on.