在前面我們已經看到,PCI的註冊就是將PCI驅動程序掛載到其所在的總線的drivers鏈,同時掃描PCI設備,將它能夠進行驅動的設備掛載到driver上的devices鏈表上來,這裏,我們將詳細地查看這整個流程的函數調用關係。
pci_register_driver()->__pci_register_driver()
CODE:
/**
* __pci_register_driver - register a new pci driver
* @drv: the driver structure to register
* @owner: owner module of drv
* @mod_name: module name string
*
* Adds the driver structure to the list of registered drivers.
* Returns a negative value on error, otherwise 0.
* If no error occurred, the driver remains registered even if
* no device was claimed during registration.
*/
int __pci_register_driver(struct pci_driver *drv, struct module *owner, const char *mod_name);
在函數中有幾個初始化語句:
drv->driver.name = drv->name;
drv->driver.bus = &pci_bus_type;
drv->driver.owner = owner;
drv->driver.mod_name = mod_name;
即是將PCI設備中的driver變量的總線指向pci_bus_type這個總線描述符,同時設置驅動的名字等。
pci_bus_type定義如下:
CODE:
struct bus_type pci_bus_type = {
.name = "pci",
.match = pci_bus_match,
.uevent = pci_uevent,
.probe = pci_device_probe,
.remove = pci_device_remove,
.suspend = pci_device_suspend,
.suspend_late = pci_device_suspend_late,
.resume_early = pci_device_resume_early,
.resume = pci_device_resume,
.shutdown = pci_device_shutdown,
.dev_attrs = pci_dev_attrs,
};
然後再調用函數driver_register(&drv->driver);通過這個函數將這個PCI驅動中的struct device_driver driver成員變量註冊到系統中去。
pci_register_driver()->__pci_register_driver()->driver_register()
driver_register()代碼如下:
CODE:
/**
* driver_register - register driver with bus
* @drv: driver to register
*
* We pass off most of the work to the bus_add_driver() call,
* since most of the things we have to do deal with the bus
* structures.
*
* The one interesting aspect is that we setup @drv->unloaded
* as a completion that gets complete when the driver reference
* count reaches 0.
*/
int driver_register(struct device_driver * drv)
{
if ((drv->bus->probe && drv->probe) ||
(drv->bus->remove && drv->remove) ||
(drv->bus->shutdown && drv->shutdown)) {
printk(KERN_WARNING "Driver '%s' needs updating - please use bus_type methods\n", drv->name);
}
klist_init(&drv->klist_devices, NULL, NULL);
init_completion(&drv->unloaded);
return bus_add_driver(drv);
}
klist_init()是爲設備驅動的klist_devices成員進行初始化,這個klist_devices是一個對鏈表進行操作的包裹結構,它會鏈接這個驅動能夠支持的那些設備。
最後就調用bus_add_driver()函數。這個函數的功能就是將這個驅動加到其所在的總線的驅動鏈上。
pci_register_driver()->__pci_register_driver()->driver_register()->bus_add_driver()->driver_attach()
在bus_add_driver()函數中,最重要的是調用driver_attach()函數,其定義如下:
CODE:
/**
* driver_attach - try to bind driver to devices.
* @drv: driver.
*
* Walk the list of devices that the bus has on it and try to
* match the driver with each one. If driver_probe_device()
* returns 0 and the @dev->driver is set, we've found a
* compatible pair.
*/
int driver_attach(struct device_driver * drv)
{
return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
}
該函數遍歷這個驅動所在的總線上的所有設備,然後將這些設備與當前驅動進行匹配,以檢測這個驅動是否能夠支持某個設備,也即是將設備與驅動聯繫起來。
bus_for_each_dev函數是掃描在drv->bus這個總線上的所有設備,然後將每個設備以及當前驅動這兩個指針傳遞給__driver_attach函數。
pci_register_driver()->__pci_register_driver()->driver_register()->bus_add_driver()->driver_attach()->__driver_attach()
__driver_attach()函數是將驅動與設備聯繫起來的函數。
CODE:
static int __driver_attach(struct device * dev, void * data)
{
struct device_driver * drv = data;
/*
* Lock device and try to bind to it. We drop the error
* here and always return 0, because we need to keep trying
* to bind to devices and some drivers will return an error
* simply if it didn't support the device.
*
* driver_probe_device() will spit a warning if there
* is an error.
*/
if (dev->parent) /* Needed for USB */
down(&dev->parent->sem);
down(&dev->sem);
if (!dev->driver)
driver_probe_device(drv, dev);
up(&dev->sem);
if (dev->parent)
up(&dev->parent->sem);
return 0;
}
在函數中有兩條語句:
CODE:
if (!dev->driver)
driver_probe_device(drv, dev);
也即是判斷當前設備是否已經註冊了一個驅動,如果沒有註冊驅動,則調用driver_probe_device()函數。
pci_register_driver()->__pci_register_driver()->driver_register()->bus_add_driver()->driver_attach()->__driver_attach()->driver_probe_device()
如下:
CODE:
/**
* driver_probe_device - attempt to bind device & driver together
* @drv: driver to bind a device to
* @dev: device to try to bind to the driver
*
* First, we call the bus's match function, if one present, which should
* compare the device IDs the driver supports with the device IDs of the
* device. Note we don't do this ourselves because we don't know the
* format of the ID structures, nor what is to be considered a match and
* what is not.
*
* This function returns 1 if a match is found, an error if one occurs
* (that is not -ENODEV or -ENXIO), and 0 otherwise.
*
* This function must be called with @dev->sem held. When called for a
* USB interface, @dev->parent->sem must be held as well.
*/
int driver_probe_device(struct device_driver * drv, struct device * dev)
{
struct stupid_thread_structure *data;
struct task_struct *probe_task;
int ret = 0;
if (!device_is_registered(dev))
return -ENODEV;
if (drv->bus->match && !drv->bus->match(dev, drv))
goto done;
pr_debug("%s: Matched Device %s with Driver %s\n",
drv->bus->name, dev->bus_id, drv->name);
data = kmalloc(sizeof(*data), GFP_KERNEL);
if (!data)
return -ENOMEM;
data->drv = drv;
data->dev = dev;
if (drv->multithread_probe) {
probe_task = kthread_run(really_probe, data,
"probe-%s", dev->bus_id);
if (IS_ERR(probe_task))
ret = really_probe(data);
} else
ret = really_probe(data);
done:
return ret;
}
該函數首先會調用總線上的match函數,以判斷當前的PCI驅動能否支持該PCI設備,如果可以,則繼續往後面執行。
drv->bus->match函數也即是pci_bus_type中的match成員變量,它爲pci_bus_match函數。
pci_register_driver()->__pci_register_driver()->driver_register()->bus_add_driver()->driver_attach()->__driver_attach()->driver_probe_device()->pci_bus_match()
CODE:
/**
* pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
* @dev: the PCI device structure to match against
* @drv: the device driver to search for matching PCI device id structures
*
* Used by a driver to check whether a PCI device present in the
* system is in its list of supported devices. Returns the matching
* pci_device_id structure or %NULL if there is no match.
*/
static int pci_bus_match(struct device *dev, struct device_driver *drv)
{
struct pci_dev *pci_dev = to_pci_dev(dev);
struct pci_driver *pci_drv = to_pci_driver(drv);
const struct pci_device_id *found_id;
found_id = pci_match_device(pci_drv, pci_dev);
if (found_id)
return 1;
return 0;
}
pci_bus_match函數的作用就是將PCI設備與PCI驅動進行比較以檢查該驅動是否能夠支持這個設備。在函數的最前面是兩個宏to_pci_dev和to_pci_driver。因爲在函數執行的過程中,雖然最開始傳進來的是pci_driver結構與pci_dev結構,但是在執行的時候卻取了這兩個結構體中的device_driver和device成員變量,所以現在就要通過這兩個成員變量找到之前對應的pci_driver和pci_dev結構的地址。
#define to_pci_dev(n) container_of(n, struct pci_dev, dev)
#define to_pci_driver(drv) container_of(drv,struct pci_driver, driver)
這兩個宏在 3rd書上有相應的講解,這裏也就是找到E100的pci_driver:e100_driver以及該網卡設備的pci_dev結構。現在就要對它們進行比較以看它們之間是否能夠聯繫起來。這是通過函數pci_match_device實現的。
pci_register_driver()->__pci_register_driver()->driver_register()->bus_add_driver()->driver_attach()->__driver_attach()->driver_probe_device()->pci_bus_match()->pci_match_device()
CODE:
/**
* pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
* @drv: the PCI driver to match against
* @dev: the PCI device structure to match against
*
* Used by a driver to check whether a PCI device present in the
* system is in its list of supported devices. Returns the matching
* pci_device_id structure or %NULL if there is no match.
*/
const struct pci_device_id *pci_match_device(struct pci_driver *drv,
struct pci_dev *dev)
{
struct pci_dynid *dynid;
/* Look at the dynamic ids first, before the static ones */
spin_lock(&drv->dynids.lock);
list_for_each_entry(dynid, &drv->dynids.list, node) {
if (pci_match_one_device(&dynid->id, dev)) {
spin_unlock(&drv->dynids.lock);
return &dynid->id;
}
}
spin_unlock(&drv->dynids.lock);
return pci_match_id(drv->id_table, dev);
}
pci_match_one_driver函數的作用是將一個PCI設備與PCI驅動進行比較,以查看它們是否相匹配。如果相匹配,則返回匹配的pci_device_id結構體指針。
此時,如果該PCI驅動已經找到了一個可以想符的PCI設備,則返回,然後再退回到之前的driver_probe_device函數中。在該函數最後將調用really_probe函數。將device_driver與device結構體指針作爲參數傳遞到這個函數中。下面幾行是調用驅動或者總線的probe函數來掃描設備。
pci_register_driver()->__pci_register_driver()->driver_register()->bus_add_driver()->driver_attach()->__driver_attach()->driver_probe_device()->really_probe()
在函數really_probe()中:
CODE:
if (dev->bus->probe) {
ret = dev->bus->probe(dev);
if (ret)
goto probe_failed;
} else if (drv->probe) {
ret = drv->probe(dev);
if (ret)
goto probe_failed;
}
此時的dev->bus爲pci_bus_type,其probe函數則對應爲:pci_device_probe。
pci_register_driver()->__pci_register_driver()->driver_register()->bus_add_driver()->driver_attach()->__driver_attach()->driver_probe_device()->really_probe()->pci_device_probe()
同樣,在該函數中會獲得當前的PCI設備的pci_dev結構體指針以及PCI驅動程序的pci_driver結構體指針。分別使用宏to_pci_dev和to_pci_driver。最後則調用函數__pci_device_probe。在該函數中還會調用函數pci_call_probe,這是最後的函數
pci_register_driver()->__pci_register_driver()->driver_register()->bus_add_driver()->driver_attach()->__driver_attach()->driver_probe_device()->really_probe()->pci_device_probe()->__pci_device_probe()->pci_call_probe()
在函數pci_call_probe裏有一條語句:
CODE:
static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
const struct pci_device_id *id)
{
int error;
/* 省略 */
error = drv->probe(dev, id);
在此處就調用了pci_driver的probe函數,對於這裏的E100驅動來說,它的probe函數是最開始註冊的e100_probe函數,在該函數中會完成對網卡設備net_device的初始化等操作。
CODE:
pci_register_driver()->__pci_register_driver()->driver_register()->bus_add_driver()->driver_attach()->__driver_attach()->driver_probe_device()->really_probe()->pci_device_probe()->__pci_device_probe()->pci_call_probe()->e100_probe()