註:本篇文章暫時不做流程圖,如果有需求後續補做。 1. 需要準備的源碼文件列表: base部分: kernel\base\core.c kernel\base\bus.c kernel\base\dd.c kernel\base\class.c kernel\base\driver.c 頭文件部分: ...
註:本篇文章暫時不做流程圖,如果有需求後續補做。
1. 需要準備的源碼文件列表:
base部分:
kernel\base\core.c
kernel\base\bus.c
kernel\base\dd.c
kernel\base\class.c
kernel\base\driver.c
頭文件部分:
kernel\include\linux\device.h
kernel\include\linux\usb.h
kernel\include\scsi\scsi_host.h
usb核心部分:
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
大容量設備部分:
kernel\driverusb\storage\usb.c
scsi部分:
kernel\driverscsi\scsi_scan.c
kernel\driverscsi\scsi_sysfs.c
kernel\driverscsi\sg.c
2. 當一個U盤插入linux設備前發生的事情:
a. 最開始註冊hub部分:
需要關註註冊驅動的有hub, usb, usb-storage。hub中用來做檢測usb口是否有OTG的東東接入,usb是所有usb接入設備的老大哥,usb-storage只是usb的一個小老弟。
翻到 kernel\driver\usb\core\usb.c 源碼,這裡先註冊了hub驅動,再註冊了usb驅動。
註:代碼中“...”表示忽略這部分的代碼,只需要關註貼出來的代碼即可。
static int __init usb_init(void) { ... retval = usb_hub_init();//註冊hub驅動 ... }
先看hub註冊過程,打開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; } ... }
先關註usb_register,省略的部分後面再關註,打開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)
然後進入kernel\driverusb\core\driver.c中的
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的實現在kernel\base\driver.c中,
int driver_register(struct device_driver *drv) { ... ret = bus_add_driver(drv); ... }
bus_add_driver的實現在kernel\base\bus.c中,
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);
...
}
這段處理大概就是把hub驅動加入到了一個鏈表中,因為鏈表就是拿來做數據操作,基本就是增加,刪除,修改,遍歷查找的,後續用到的時候再講即可,hub註冊部分就是這樣了。
b. 註冊usb部分:
打開kernel\driver\usb\core\usb.c,就在註冊hub驅動的下3行,註冊了usb設備驅動,
static int __init usb_init(void) { ... retval = usb_register_device_driver(&usb_generic_driver, THIS_MODULE); ... }
然後進入kernel\driverusb\core\driver.c中的,
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);
又到了driver_register,最後也就是把usb設備驅動添加到一個鏈表中,等待著遍歷執行的時刻。
c. 註冊usb-storage部分:
打開usb\storage\usb.c,這裡註冊了usb-storage的驅動,這個驅動就是與U盤節點有關的。
static struct usb_driver usb_storage_driver = { .name = "usb-storage", ... }; module_usb_driver(usb_storage_driver);
可以看看它的實現,打開kernel\include\linux\usb.h,
#define module_usb_driver(__usb_driver) \ module_driver(__usb_driver, usb_register, \ usb_deregister)
可以在kernel\include\linux\device.h查看module_driver的實現,
#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);
就是一個巨集,註冊用usb_register,反向註冊用usb_deregister,然後再module_init它,就會在開機的時候執行了。至於usb_register,最後也就是把usb-storage驅動添加到一個鏈表中,等待著遍歷執行的時刻。
3. 當一個U盤插入linux設備後:
a. 需要有一個線程等待檢測U盤插入,重新回到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)); ... }
static void hub_events(void) { ... while (1) {
...
hdev = hub->hdev; //這裡有一段獲取usb驅動設備過程,忽略,因為我還沒仔細研究過
... 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); ... }
進入到kernel\base\core.c中,
int device_add(struct device *dev) { ... bus_probe_device(dev); ... }
進入到kernel\base\bus.c中,
void bus_probe_device(struct device *dev) { ... ret = device_attach(dev); ... }
進入到kernel\base\dd.c中,
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; } ... }
之前鏈表插入的usb設備驅動的probe就在此刻被遍歷出來,然後調用。
回顧插入的函數指針,打開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; ... }
進入
static int usb_probe_device(struct device *dev) { struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); ... error = udriver->probe(udev); ... }
由kernel\include\linux\usb.h中:
#define to_usb_device_driver(d) container_of(d, struct usb_device_driver, \ drvwrap.driver)
和kernel\driver\usb\core\usb.c中:
retval = usb_register_device_driver(&usb_generic_driver, THIS_MODULE);
可知
這裡的probe會調用usb_generic_driver的probe,因為container_of的作用就是把指向d的指針返回,返回的指針即為&usb_generic_driver。
打開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); ... }
打開kernel\drivers\usb\core\message.c,
int usb_set_configuration(struct usb_device *dev, int configuration) { ... ret = device_add(&intf->dev); ... }
之前提到過device_add->bus_probe_device->device_attach->__device_attach->driver_probe_device->really_probe->傳入的設備對應的驅動probe。
在usb_set_configuration或者之前,肯定有一個獲取usb-storage驅動信息的過程,總之這次的probe會進入usb_probe_interface,驅動就是之前註冊的usb-storage。
打開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); ... }
同之前container_of返回指向p的指針分析的一樣,這次返回的指針是&usb_storage_driver。
打開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); ... }
這段就是usb_stor_probe1中註冊了一個延時的工作隊列,然後usb_stor_probe2喚醒這個工作隊列註冊的函數usb_stor_scan_dwork工作。
static void usb_stor_scan_dwork(struct work_struct *work) { ... scsi_scan_host(us_to_host(us)); ... }
下一步就是scsi子系統的工作了。
b. sg節點的創建。
打開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); } }
打開kernel\driverscsi\scsi_sysfs.c,
int scsi_sysfs_add_sdev(struct scsi_device *sdev) { ... error = device_add(&sdev->sdev_dev); ... }
註:這裡傳的是&sdev->sdev_dev,而不是&sdev->sdev_gendev
又到了device_add,這次可不是走really_probe那麼簡單了,直接show出關鍵代碼,
打開kernel\base\core.c,
int device_add(struct device *dev) { ... if (class_intf->add_dev) class_intf->add_dev(dev, class_intf); ... }
add_dev會調用哪個class_interface?
打開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, };
可知調用的add_dev就是sg_add,所以節點sg就是以下代碼創建的。
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); ... }
源碼太多,花了我大把時間才捋清。
大體就是,註冊一堆東東,匯流排(usb)啊,驅動設備(usb)啊,驅動(hub,usb-storage)啊,class(sg_interface)啊等等,然後跑一個線程,檢測到需要的東東後,比對註冊到特定鏈表的數據,然後就調用各種probe和註冊的介面如add_dev等。