Linux那些事儿之我是EHCI(2) 套路 – fudan

子曰:按套路出牌。的确,什么东西都有套路,泡妞有泡妞的套路,花前月下不如花钱日下。打麻将有打麻将的套路,少吃少碰少放炮,多摸多杠多发财。星际有星际的套路,linux也有linux的套路。刘涛姐姐的故事再一次告诉我们,年龄不是问题,身高不是距离,有cai就行。

我们不妨看看 modprobe ehci-hcd 之后发生了什么事情。ehci-hcd是一个驱动程序,不知您记不记得我在sysfs中谈论过设备模型。有两个重要的链表挂在bus上,一个是设备device链表,一个是驱动driver链表。

每当我们向一根bus注册一个驱动driver时,套路是这样的:

driver_register(struct device_driver * drv) -> bus_add_driver() -> driver_attach() ->

bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);

bus_for_each_dev遍历该总线上所有的device,执行一次__driver_attach(),看能不能将驱动关联(attach)到某个设备上去。

__driver_attach()

->driver_probe_device()

->drv->bus->match(dev, drv), // 调用bus的match函数,看device和driver匹不匹配。如果匹配上,

继续执行really_probe()。

->really_probe()

->driver->probe()。(如果bus->probe非空,则调用bus->probe)

而每当我们向一根bus添加一个硬件时时,套路是这样的:

device_add()

// device_add 中有很多操作kobject,注册sysfs,形成硬件hiberarchy结构的代码。

如果您忘记了,先回头去参考参考"我是sysfs"

->bus_attach_device() -> device_attach() ->bus_for_each_drv()

bus_for_each_drv与bus_for_each_dev类似,遍历该总线上所有的driver,执行一次__device_attach(),看能不能将设备关联(attach)到某个已登记的驱动上去。

__device_attach()

->driver_probe_device() //后面与上面一样

总结一些,一句话,注册一个某个bus的驱动就是先把驱动自己链入到bus驱动链表中去,在从bus的设备链表中一一寻找,看有没有自己可以关联上的设备。找到就probe,再把二者bind起来。反之,添加设备道理也是一样的。

好吧,我们还是看看modprobe ehci-hcd后的事情。一切从此开始,

module_init(ehci_hcd_init);

我们把不必要的预编译代码去掉后,ehci_hcd_init 如下:

staticint__initehci_hcd_init(void)…{intretval=0;pr_debug("%s:blocksizes:qh%Zdqtd%Zditd%Zdsitd%Zd ",hcd_name,sizeof(structehci_qh),sizeof(structehci_qtd),sizeof(structehci_itd),sizeof(structehci_sitd));retval=pci_register_driver(&PCI_DRIVER);if(retval<0)…{returnretval;}if(retval<0)…{platform_driver_unregister(&PLATFORM_DRIVER);pci_unregister_driver(&PCI_DRIVER);returnretval;}}

PCI_DRIVER是一个宏,#definePCI_DRIVERehci_pci_driver。

staticstructpci_driverehci_pci_driver=…{.name=(char*)hcd_name,.id_table=pci_ids,.probe=usb_hcd_pci_probe,.remove=usb_hcd_pci_remove,#ifdefCONFIG_PM.suspend=usb_hcd_pci_suspend,.resume=usb_hcd_pci_resume,#endif.shutdown=usb_hcd_pci_shutdown,};

ehci_hcd_init 很简单就是调用了pci_register_driver(),就是__pci_register_driver()。

int__pci_register_driver(structpci_driver*drv,structmodule*owner,constchar*mod_name)…{interror;/**//*initializecommondriverfields*/drv->driver.name=drv->name;drv->driver.bus=&pci_bus_type;drv->driver.owner=owner;drv->driver.mod_name=mod_name;drv->driver.kobj.ktype=&pci_driver_kobj_type;spin_lock_init(&drv->dynids.lock);INIT_LIST_HEAD(&drv->dynids.list);/**//*registerwithcore*/error=driver_register(&drv->driver);if(error)returnerror;error=pci_create_newid_file(drv);if(error)driver_unregister(&drv->driver);returnerror;}

driver_register(struct device_driver * drv)就是前面讲过了,就是linux的套路。那我们看看pci总线的match, probe函数是什么样的吧。

让我们将事前的忧虑,换为事前的思考和计划吧!

Linux那些事儿之我是EHCI(2) 套路 – fudan

相关文章:

你感兴趣的文章:

标签云: