linux下PCI设备的注册及初始化流程分析

        在前面我们已经看到,PCI的注册就是将PCI驱动程序挂载到其所在的总线的drivers链,同时扫描PCI设备,将它能够进行驱动的设备挂载到driver上的devices链表上来,这里,我们将详细地查看这整个流程的函数调用关系。
        pci_register_driver()->__pci_register_driver()
 

[Copy to clipboard] [ - ]

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定义如下:
 

[Copy to clipboard] [ - ]

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()代码如下:
 

[Copy to clipboard] [ - ]

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()函数,其定义如下:
 

[Copy to clipboard] [ - ]

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()函数是将驱动与设备联系起来的函数。
 

[Copy to clipboard] [ - ]

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;
}


在函数中有两条语句:
 

[Copy to clipboard] [ - ]

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()
        如下:
 

[Copy to clipboard] [ - ]

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()
 

[Copy to clipboard] [ - ]

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()
 

[Copy to clipboard] [ - ]

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()中:
 

[Copy to clipboard] [ - ]

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里有一条语句:
 

[Copy to clipboard] [ - ]

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的初始化等操作。
 

[Copy to clipboard] [ - ]

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()

 

 

转自:http://blog.chinaunix.net/uid-618506-id-4113167.html

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值