亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問板塊 發(fā)新帖
查看: 21157 | 回復(fù): 9
打印 上一主題 下一主題

【轉(zhuǎn)】I2C設(shè)備與驅(qū)動(dòng)的關(guān)聯(lián) [復(fù)制鏈接]

論壇徽章:
3
金牛座
日期:2014-06-14 22:04:062015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:45
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2008-11-25 17:46 |只看該作者 |倒序?yàn)g覽
I2C設(shè)備與驅(qū)動(dòng)的關(guān)聯(lián)

    在Linux操作系統(tǒng)中,驅(qū)動(dòng)程序的加載分為兩種:內(nèi)核啟動(dòng)時(shí)自動(dòng)加載和用戶手動(dòng)加載;硬件設(shè)備也可以采用兩種方式添加到系統(tǒng)中:在系統(tǒng)啟動(dòng)前及系統(tǒng)運(yùn)行時(shí)的熱插撥。下面,我們以arm體系結(jié)構(gòu)下的at91處理器中的I2C控制器為例,介紹一下硬件設(shè)備及相關(guān)的驅(qū)動(dòng)程序是如何綁定及松綁的。
1.        平臺(tái)驅(qū)動(dòng)注冊(cè)過程
1.1 at91_i2c_init()函數(shù)
在文件drivers/i2c/busses/i2c-at91.c中,定義了結(jié)構(gòu)體struct platform_driver并進(jìn)行了初始化,通過使用module_init()宏進(jìn)行聲明,當(dāng)模塊被加載到內(nèi)核時(shí)會(huì)調(diào)用 at91_i2c_init()函數(shù)。在此函數(shù)中,調(diào)用了platform_driver_register()函數(shù)來(lái)完成注冊(cè)。

static struct platform_driver at91_i2c_driver = {
        .probe                = at91_i2c_probe,
        .remove                = __devexit_p(at91_i2c_remove),
        .suspend        = at91_i2c_suspend,
        .resume                = at91_i2c_resume,
        .driver                = {
                .name        = "at91_i2c",
                .owner        = THIS_MODULE,
        },
};

static int __init at91_i2c_init(void)
{
        return platform_driver_register(&at91_i2c_driver);
}
1.2 platform_driver_register()函數(shù)
在文件drivers/base/platform.c中,實(shí)現(xiàn)并導(dǎo)出了platform_driver_register()函數(shù),以便使其他模塊中的函數(shù)可以調(diào)用此函數(shù)。它在完成簡(jiǎn)單的包裝后,調(diào)用了driver_register()函數(shù),完成了從平臺(tái)實(shí)現(xiàn)到Linux內(nèi)核實(shí)現(xiàn)的過渡。
    在此,我們需要關(guān)注一下platform_match()和platform_drv_probe()函數(shù)。platform_match() 函數(shù)確定驅(qū)動(dòng)與設(shè)備的關(guān)聯(lián),而platform_drv_probe()函數(shù)會(huì)在隨后介紹的函數(shù)中被調(diào)用。
//比較驅(qū)動(dòng)信息中的name與設(shè)備信息中的name兩者是否一致
static int platform_match(struct device * dev, struct device_driver * drv)
{
struct platform_device *pdev = container_of(dev, struct platform_device, dev);

        return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);
}

struct bus_type platform_bus_type = {
        .name                = "platform",
        .dev_attrs        = platform_dev_attrs,
        .match                = platform_match,
        .uevent                = platform_uevent,
        .suspend        = platform_suspend,
        .suspend_late        = platform_suspend_late,
        .resume_early        = platform_resume_early,
        .resume                = platform_resume,
};
EXPORT_SYMBOL_GPL(platform_bus_type);

/**
*        platform_driver_register
*        @drv: platform driver structure
*/
int platform_driver_register(struct platform_driver *drv)
{
        drv->driver.bus = &platform_bus_type;
        //在really_probe函數(shù)中,回調(diào)了platform_drv_probe函數(shù)
        if (drv->probe)
                drv->driver.probe = platform_drv_probe;
        if (drv->remove)
                drv->driver.remove = platform_drv_remove;
        if (drv->shutdown)
                drv->driver.shutdown = platform_drv_shutdown;
        if (drv->suspend)
                drv->driver.suspend = platform_drv_suspend;
        if (drv->resume)
                drv->driver.resume = platform_drv_resume;
        return driver_register(&drv->driver);
}
EXPORT_SYMBOL_GPL(platform_driver_register);

1.3 driver_register()函數(shù)
在文件drivers/base/driver.c中,實(shí)現(xiàn)了driver_register()函數(shù)。在此函數(shù)中,初始化結(jié)構(gòu)體struct device_driver中的klist_device和unloaded字段,通過klist_device字段,可以保存此驅(qū)動(dòng)支持的設(shè)備鏈表,通過“完成”接口機(jī)制,完成線程間的同步。鏈表和“完成”接口的詳細(xì)信息可以參考文獻(xiàn)[1]。返回bus_add_driver()函數(shù)的運(yùn)行結(jié)果。

/**
*        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);
}

1.4 bus_add_driver()函數(shù)
在文件drivers/base/bus.c中實(shí)現(xiàn)了bus_add_driver()函數(shù),它通過語(yǔ)句klist_add_tail(&drv->knode_bus, &bus->klist_drivers); 將驅(qū)動(dòng)信息保存到總線結(jié)構(gòu)中,在設(shè)備注冊(cè)過程中,我們就可以明白此語(yǔ)句的作用了。在此語(yǔ)句之前,調(diào)用了driver_attach()函數(shù)。
/**
*        bus_add_driver - Add a driver to the bus.
*        @drv:        driver.
*
*/
int bus_add_driver(struct device_driver *drv)
{
        struct bus_type * bus = get_bus(drv->bus);
        int error = 0;

        if (!bus)
                return 0;

        pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
        error = kobject_set_name(&drv->kobj, "%s", drv->name);
        if (error)
                goto out_put_bus;
        drv->kobj.kset = &bus->drivers;
        if ((error = kobject_register(&drv->kobj)))
                goto out_put_bus;

        error = driver_attach(drv);
        if (error)
                goto out_unregister;
        klist_add_tail(&drv->knode_bus, &bus->klist_drivers);
        module_add_driver(drv->owner, drv);

        error = driver_add_attrs(bus, drv);
        if (error) {
                /* How the hell do we get out of this pickle? Give up */
                printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
                        __FUNCTION__, drv->name);
        }
        error = add_bind_files(drv);
        if (error) {
                /* Ditto */
                printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
                        __FUNCTION__, drv->name);
        }

        return error;
out_unregister:
        kobject_unregister(&drv->kobj);
out_put_bus:
        put_bus(bus);
        return error;
}

1.5 dd.c文件
在文件drivers/base/dd.c中,實(shí)現(xiàn)了設(shè)備與驅(qū)動(dòng)交互的核心函數(shù)。
1.5.1 driver_attach()函數(shù)
函數(shù)driver_attach()返回bus_for_each_dev()函數(shù)的運(yùn)行結(jié)果。bus_for_each_dev()函數(shù)的原型如下:
int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data,
        int (*fn) (struct device *, void *));
該函數(shù)迭代了在總線上的每個(gè)設(shè)備,將相關(guān)的device結(jié)構(gòu)傳遞給fn,同時(shí)傳遞data值。如果start是NULL,將從總線上的第一個(gè)設(shè)備開始迭代;否則將從start后的第一個(gè)設(shè)備開始迭代。如果fn返回一個(gè)非零值,將停止迭代,而這個(gè)值也會(huì)從該函數(shù)返回(摘自<<Linux設(shè)備驅(qū)動(dòng)程序>>第三版)。
該函數(shù)是如何知道總線上的每個(gè)設(shè)備的呢?在設(shè)備注冊(cè)過程中,我會(huì)詳細(xì)介紹。
/*
*        drivers/base/dd.c - The core device/driver interactions.
*
*         This file contains the (sometimes tricky) code that controls the
*        interactions between devices and drivers, which primarily includes
*        driver binding and unbinding.
*/

/**
*        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);
}
1.5.2 __driver_attach()函數(shù)
函數(shù)__driver_attach()在調(diào)用driver_probe_device()函數(shù)前,需要進(jìn)行線程間的互斥處理。
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;
}
1.5.3 driver_probe_device()函數(shù)
在driver_probe_device()函數(shù)中,調(diào)用了match函數(shù)platform_match(),如果它返回0,表示驅(qū)動(dòng)與設(shè)備不一致,函數(shù)返回;否則,調(diào)用really_probe()函數(shù)。
/**
* 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;
}

struct stupid_thread_structure {
        struct device_driver *drv;
        struct device *dev;
};

1.5.4 really_probe()函數(shù)
在really_probe()函數(shù)中,實(shí)現(xiàn)了設(shè)備與驅(qū)動(dòng)的綁定。語(yǔ)句如下:dev->driver = drv;和
ret = drv->probe(dev); probe()函數(shù)的實(shí)現(xiàn)如下:
include/linux/platform_device.h

#define to_platform_device(x) container_of((x), struct platform_device, dev)

drivers/base/platform.c

#define to_platform_driver(drv)        (container_of((drv), struct platform_driver, driver))


static int platform_drv_probe(struct device *_dev)
{
        struct platform_driver *drv = to_platform_driver(_dev->driver);
        struct platform_device *dev = to_platform_device(_dev);

        return drv->probe(dev);
}
在此函數(shù)中,回調(diào)了我們?cè)趇2c-at91.c文件中實(shí)現(xiàn)的探測(cè)函數(shù)at91_i2c_probe(),至此,平臺(tái)驅(qū)動(dòng)的注冊(cè)過程結(jié)束。

static atomic_t probe_count = ATOMIC_INIT(0);
static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);

static int really_probe(void *void_data)
{
        struct stupid_thread_structure *data = void_data;
        struct device_driver *drv = data->drv;
        struct device *dev = data->dev;
        int ret = 0;

        atomic_inc(&probe_count);
        pr_debug("%s: Probing driver %s with device %s\n",
                 drv->bus->name, drv->name, dev->bus_id);
        WARN_ON(!list_empty(&dev->devres_head));

        dev->driver = drv;
        if (driver_sysfs_add(dev)) {
                printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n",
                        __FUNCTION__, dev->bus_id);
                goto probe_failed;
        }

        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;
        }
        //設(shè)備與驅(qū)動(dòng)綁定后,對(duì)系統(tǒng)中已注冊(cè)的組件進(jìn)行事件通知。
        driver_bound(dev);
        ret = 1;
        pr_debug("%s: Bound Device %s to Driver %s\n",
                 drv->bus->name, dev->bus_id, drv->name);
        goto done;

probe_failed:
        devres_release_all(dev);
        driver_sysfs_remove(dev);
        dev->driver = NULL;

        if (ret != -ENODEV && ret != -ENXIO) {
                /* driver matched but the probe failed */
                printk(KERN_WARNING
                       "%s: probe of %s failed with error %d\n",
                       drv->name, dev->bus_id, ret);
        }
        /*
         * Ignore errors returned by ->probe so that the next driver can try
         * its luck.
         */
        ret = 0;
done:
        kfree(data);
        atomic_dec(&probe_count);
        wake_up(&probe_waitqueue);
        return ret;
}


















2.        平臺(tái)驅(qū)動(dòng)卸載過程
平臺(tái)驅(qū)動(dòng)卸載過程是注冊(cè)的逆過程,詳細(xì)信息可參考注冊(cè)過程進(jìn)行分析。
2.1 at91_i2c_exit()函數(shù)
static void __exit at91_i2c_exit(void)
{
        platform_driver_unregister(&at91_i2c_driver);
}

2.2 platform_driver_unregister()函數(shù)

/**
*        platform_driver_unregister
*        @drv: platform driver structure
*/
void platform_driver_unregister(struct platform_driver *drv)
{
        driver_unregister(&drv->driver);
}
EXPORT_SYMBOL_GPL(platform_driver_unregister);

2.3 driver_unregister()函數(shù)

/**
*        driver_unregister - remove driver from system.
*        @drv:        driver.
*
*        Again, we pass off most of the work to the bus-level call.
*
*        Though, once that is done, we wait until @drv->unloaded is completed.
*        This will block until the driver refcount reaches 0, and it is
*        released. Only modular drivers will call this function, and we
*        have to guarantee that it won't complete, letting the driver
*        unload until all references are gone.
*/

void driver_unregister(struct device_driver * drv)
{
        bus_remove_driver(drv);
        /*
         * If the driver is a module, we are probably in
         * the module unload path, and we want to wait
         * for everything to unload before we can actually
         * finish the unload.
         */
        if (drv->owner)
                wait_for_completion(&drv->unloaded);
}

2.4 bus_remove_driver()函數(shù)
/**
*        bus_remove_driver - delete driver from bus's knowledge.
*        @drv:        driver.
*
*        Detach the driver from the devices it controls, and remove
*        it from its bus's list of drivers. Finally, we drop the reference
*        to the bus we took in bus_add_driver().
*/

void bus_remove_driver(struct device_driver * drv)
{
        if (!drv->bus)
                return;

        remove_bind_files(drv);
        driver_remove_attrs(drv->bus, drv);
        klist_remove(&drv->knode_bus);
        pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
        driver_detach(drv);
        module_remove_driver(drv);
        kobject_unregister(&drv->kobj);
        put_bus(drv->bus);
}

2.5 driver_detach()函數(shù)
/**
* driver_detach - detach driver from all devices it controls.
* @drv: driver.
*/
void driver_detach(struct device_driver * drv)
{
        struct device * dev;

        for (;;) {
                spin_lock(&drv->klist_devices.k_lock);
                if (list_empty(&drv->klist_devices.k_list)) {
                        spin_unlock(&drv->klist_devices.k_lock);
                        break;
                }
                dev = list_entry(drv->klist_devices.k_list.prev,
                                struct device, knode_driver.n_node);
                get_device(dev);
                spin_unlock(&drv->klist_devices.k_lock);

                if (dev->parent)        /* Needed for USB */
                        down(&dev->parent->sem);
                down(&dev->sem);
                if (dev->driver == drv)
                        __device_release_driver(dev);
                up(&dev->sem);
                if (dev->parent)
                        up(&dev->parent->sem);
                put_device(dev);
        }
}

2.6 __device_release_driver()函數(shù)
/**
*        device_release_driver - manually detach device from driver.
*        @dev:        device.
*
*        Manually detach device from driver.
*
*        __device_release_driver() must be called with @dev->sem held.
*        When called for a USB interface, @dev->parent->sem must be held
*        as well.
*/

static void __device_release_driver(struct device * dev)
{
        struct device_driver * drv;

        drv = dev->driver;
        if (drv) {
                get_driver(drv);
                driver_sysfs_remove(dev);
                sysfs_remove_link(&dev->kobj, "driver");
                klist_remove(&dev->knode_driver);

                if (dev->bus)
                        blocking_notifier_call_chain(&dev->bus->bus_notifier,
                                                     BUS_NOTIFY_UNBIND_DRIVER,
                                                     dev);

                if (dev->bus && dev->bus->remove)
                        dev->bus->remove(dev);
                else if (drv->remove)
                        drv->remove(dev);
                devres_release_all(dev);
                dev->driver = NULL;
                put_driver(drv);
        }
}

2.7 platform_drv_remove()函數(shù)
static int platform_drv_remove(struct device *_dev)
{
        struct platform_driver *drv = to_platform_driver(_dev->driver);
        struct platform_device *dev = to_platform_device(_dev);

        return drv->remove(dev);
}

在此函數(shù)中,回調(diào)了我們?cè)趇2c-at91.c文件中實(shí)現(xiàn)的移除函數(shù)at91_i2c_remove(),至此,平臺(tái)驅(qū)動(dòng)的卸載過程結(jié)束。

在注冊(cè)和卸載過程中,Linux采用了一些變量來(lái)保存相關(guān)的信息,比如引用計(jì)數(shù)、通知鏈等,感興趣的人員,可以詳細(xì)的閱讀此部分內(nèi)容。

論壇徽章:
3
金牛座
日期:2014-06-14 22:04:062015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:45
2 [報(bào)告]
發(fā)表于 2008-11-25 17:47 |只看該作者
3.        設(shè)備注冊(cè)過程
3.1 at91_add_device_i2c()函數(shù)
在文件arch/arm/mach-at91/at91sam9263_devices.c中,對(duì)I2C設(shè)備進(jìn)行了定義。
/* --------------------------------------------------------------------
*  TWI (i2c)
* -------------------------------------------------------------------- */

#if defined(CONFIG_I2C_AT91) || defined(CONFIG_I2C_AT91_MODULE)

static struct resource twi_resources[] = {
        [0] = {
                .start        = AT91SAM9263_BASE_TWI,
                .end        = AT91SAM9263_BASE_TWI + SZ_16K - 1,
                .flags        = IORESOURCE_MEM,
        },
        [1] = {
                .start        = AT91SAM9263_ID_TWI,
                .end        = AT91SAM9263_ID_TWI,
                .flags        = IORESOURCE_IRQ,
        },
};

static struct platform_device at91sam9263_twi_device = {
        //此名字與驅(qū)動(dòng)信息中的名字"at91_i2c"是一致的,在match()函數(shù)調(diào)用中,會(huì)返回1。
        .name                = "at91_i2c",
        .id                = -1,
        .resource        = twi_resources,
        .num_resources        = ARRAY_SIZE(twi_resources),
};

void __init at91_add_device_i2c(void)
{
        /* pins used for TWI interface */
        at91_set_A_periph(AT91_PIN_PB4, 0);                /* TWD */
        at91_set_multi_drive(AT91_PIN_PB4, 1);

        at91_set_A_periph(AT91_PIN_PB5, 0);                /* TWCK */
        at91_set_multi_drive(AT91_PIN_PB5, 1);

        //對(duì)設(shè)備進(jìn)行注冊(cè)
        platform_device_register(&at91sam9263_twi_device);
}
#else
void __init at91_add_device_i2c(void) {}
#endif
3.2 platform.c文件
在文件drivers/base/platform.c中,實(shí)現(xiàn)了下面的函數(shù)。
3.2.1 platform_device_register()函數(shù)
/**
*        platform_device_register - add a platform-level device
*        @pdev:        platform device we're adding
*
*/
int platform_device_register(struct platform_device * pdev)
{
        device_initialize(&pdev->dev);
        return platform_device_add(pdev);
}
EXPORT_SYMBOL_GPL(platform_device_register);
3.2.2 platform_device_add()函數(shù)
/**
*        platform_device_add - add a platform device to device hierarchy
*        @pdev:        platform device we're adding
*
*        This is part 2 of platform_device_register(), though may be called
*        separately _iff_ pdev was allocated by platform_device_alloc().
*/
int platform_device_add(struct platform_device *pdev)
{
        int i, ret = 0;

        if (!pdev)
                return -EINVAL;

        if (!pdev->dev.parent)
                pdev->dev.parent = &platform_bus;

        pdev->dev.bus = &platform_bus_type;

        if (pdev->id != -1)
                snprintf(pdev->dev.bus_id, BUS_ID_SIZE, "%s.%u", pdev->name, pdev->id);
        else
                strlcpy(pdev->dev.bus_id, pdev->name, BUS_ID_SIZE);

        for (i = 0; i < pdev->num_resources; i++) {
                struct resource *p, *r = &pdev->resource;

                if (r->name == NULL)
                        r->name = pdev->dev.bus_id;

                p = r->parent;
                if (!p) {
                        if (r->flags & IORESOURCE_MEM)
                                p = &iomem_resource;
                        else if (r->flags & IORESOURCE_IO)
                                p = &ioport_resource;
                }

                if (p && insert_resource(p, r)) {
                        printk(KERN_ERR
                               "%s: failed to claim resource %d\n",
                               pdev->dev.bus_id, i);
                        ret = -EBUSY;
                        goto failed;
                }
        }

        pr_debug("Registering platform device '%s'. Parent at %s\n",
                 pdev->dev.bus_id, pdev->dev.parent->bus_id);

        ret = device_add(&pdev->dev);
        if (ret == 0)
                return ret;

failed:
        while (--i >= 0)
                if (pdev->resource.flags & (IORESOURCE_MEM|IORESOURCE_IO))
                        release_resource(&pdev->resource);
        return ret;
}
EXPORT_SYMBOL_GPL(platform_device_add);

3.3 device_add()函數(shù)
在文件drivers/base/core.c中實(shí)現(xiàn)了device_add()函數(shù)。
/**
*        device_add - add device to device hierarchy.
*        @dev:        device.
*
*        This is part 2 of device_register(), though may be called
*        separately _iff_ device_initialize() has been called separately.
*
*        This adds it to the kobject hierarchy via kobject_add(), adds it
*        to the global and sibling lists for the device, then
*        adds it to the other relevant subsystems of the driver model.
*/
int device_add(struct device *dev)
{
        struct device *parent = NULL;
        char *class_name = NULL;
        struct class_interface *class_intf;
        int error = -EINVAL;

        dev = get_device(dev);
        if (!dev || !strlen(dev->bus_id))
                goto Error;

        pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);

        parent = get_device(dev->parent);

        error = setup_parent(dev, parent);
        if (error)
                goto Error;

        /* first, register with generic layer. */
        kobject_set_name(&dev->kobj, "%s", dev->bus_id);
        error = kobject_add(&dev->kobj);
        if (error)
                goto Error;

        /* notify platform of device entry */
        if (platform_notify)
                platform_notify(dev);

        /* notify clients of device entry (new way) */
        if (dev->bus)
                blocking_notifier_call_chain(&dev->bus->bus_notifier,
                                             BUS_NOTIFY_ADD_DEVICE, dev);

        dev->uevent_attr.attr.name = "uevent";
        dev->uevent_attr.attr.mode = S_IWUSR;
        if (dev->driver)
                dev->uevent_attr.attr.owner = dev->driver->owner;
        dev->uevent_attr.store = store_uevent;
        error = device_create_file(dev, &dev->uevent_attr);
        if (error)
                goto attrError;

        if (MAJOR(dev->devt)) {
                struct device_attribute *attr;
                attr = kzalloc(sizeof(*attr), GFP_KERNEL);
                if (!attr) {
                        error = -ENOMEM;
                        goto ueventattrError;
                }
                attr->attr.name = "dev";
                attr->attr.mode = S_IRUGO;
                if (dev->driver)
                        attr->attr.owner = dev->driver->owner;
                attr->show = show_dev;
                error = device_create_file(dev, attr);
                if (error) {
                        kfree(attr);
                        goto ueventattrError;
                }

                dev->devt_attr = attr;
        }

        if (dev->class) {
                sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
                                  "subsystem");
                /* If this is not a "fake" compatible device, then create the
                 * symlink from the class to the device. */
                if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
                        sysfs_create_link(&dev->class->subsys.kset.kobj,
                                          &dev->kobj, dev->bus_id);
                if (parent) {
                        sysfs_create_link(&dev->kobj, &dev->parent->kobj,
                                                        "device");
#ifdef CONFIG_SYSFS_DEPRECATED
                        class_name = make_class_name(dev->class->name,
                                                        &dev->kobj);
                        if (class_name)
                                sysfs_create_link(&dev->parent->kobj,
                                                  &dev->kobj, class_name);
#endif
                }
        }

        if ((error = device_add_attrs(dev)))
                goto AttrsError;
        if ((error = device_add_groups(dev)))
                goto GroupError;
        if ((error = device_pm_add(dev)))
                goto PMError;
        if ((error = bus_add_device(dev)))
                goto BusError;
        if (!dev->uevent_suppress)
                kobject_uevent(&dev->kobj, KOBJ_ADD);
        if ((error = bus_attach_device(dev)))
                goto AttachError;
        if (parent)
                klist_add_tail(&dev->knode_parent, &parent->klist_children);

        if (dev->class) {
                down(&dev->class->sem);
                /* tie the class to the device */
                list_add_tail(&dev->node, &dev->class->devices);

                /* notify any interfaces that the device is here */
                list_for_each_entry(class_intf, &dev->class->interfaces, node)
                        if (class_intf->add_dev)
                                class_intf->add_dev(dev, class_intf);
                up(&dev->class->sem);
        }
Done:
        kfree(class_name);
        put_device(dev);
        return error;
AttachError:
        bus_remove_device(dev);
BusError:
        device_pm_remove(dev);
PMError:
        if (dev->bus)
                blocking_notifier_call_chain(&dev->bus->bus_notifier,
                                             BUS_NOTIFY_DEL_DEVICE, dev);
        device_remove_groups(dev);
GroupError:
        device_remove_attrs(dev);
AttrsError:
        if (dev->devt_attr) {
                device_remove_file(dev, dev->devt_attr);
                kfree(dev->devt_attr);
        }

        if (dev->class) {
                sysfs_remove_link(&dev->kobj, "subsystem");
                /* If this is not a "fake" compatible device, remove the
                 * symlink from the class to the device. */
                if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
                        sysfs_remove_link(&dev->class->subsys.kset.kobj,
                                          dev->bus_id);
                if (parent) {
#ifdef CONFIG_SYSFS_DEPRECATED
                        char *class_name = make_class_name(dev->class->name,
                                                           &dev->kobj);
                        if (class_name)
                                sysfs_remove_link(&dev->parent->kobj,
                                                  class_name);
                        kfree(class_name);
#endif
                        sysfs_remove_link(&dev->kobj, "device");
                }

                down(&dev->class->sem);
                /* notify any interfaces that the device is now gone */
                list_for_each_entry(class_intf, &dev->class->interfaces, node)
                        if (class_intf->remove_dev)
                                class_intf->remove_dev(dev, class_intf);
                /* remove the device from the class list */
                list_del_init(&dev->node);
                up(&dev->class->sem);
        }
ueventattrError:
        device_remove_file(dev, &dev->uevent_attr);
attrError:
        kobject_uevent(&dev->kobj, KOBJ_REMOVE);
        kobject_del(&dev->kobj);
Error:
        if (parent)
                put_device(parent);
        goto Done;
}
3.4 bus_attach_device()函數(shù)
在文件drivers/base/bus.c中實(shí)現(xiàn)了bus_attach_device()函數(shù)。

/**
*        bus_attach_device - add device to bus
*        @dev:        device tried to attach to a driver
*
*        - Add device to bus's list of devices.
*        - Try to attach to driver.
*/
int bus_attach_device(struct device * dev)
{
        struct bus_type *bus = dev->bus;
        int ret = 0;

        if (bus) {
                dev->is_registered = 1;
                ret = device_attach(dev);
                if (ret >= 0) {
//將設(shè)備信息存儲(chǔ)在bus結(jié)構(gòu)中。在驅(qū)動(dòng)注冊(cè)后,利用bus_for_each_dev()函數(shù)可以得到所有設(shè)備的
//信息,完成驅(qū)動(dòng)與設(shè)備之間的綁定。                       
                        klist_add_tail(&dev->knode_bus, &bus->klist_devices);
                        ret = 0;
                } else
                        dev->is_registered = 0;
        }
        return ret;
}
3.5 dd.c文件
在文件drivers/base/dd.c中實(shí)現(xiàn)了device_attach()函數(shù)。
3.5.1 device_attach()函數(shù)
/**
*        device_attach - try to attach device to a driver.
*        @dev:        device.
*
*        Walk the list of drivers that the bus has and call
*        driver_probe_device() for each pair. If a compatible
*        pair is found, break out and return.
*
*        Returns 1 if the device was bound to a driver;
*        0 if no matching device was found; error code otherwise.
*
*        When called for a USB interface, @dev->parent->sem must be held.
*/
int device_attach(struct device * dev)
{
        int ret = 0;

        down(&dev->sem);
        if (dev->driver) {
                ret = device_bind_driver(dev);
                if (ret == 0)
                        ret = 1;
        } else
                ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach);
        up(&dev->sem);
        return ret;
}

static int __device_attach(struct device_driver * drv, void * data)
{
        struct device * dev = data;
        //在此之后,利用驅(qū)動(dòng)信息來(lái)探測(cè)設(shè)備,參考第一部分的介紹
        return driver_probe_device(drv, dev);
}
3.5.2 device_bind_driver()函數(shù)
/**
*        device_bind_driver - bind a driver to one device.
*        @dev:        device.
*
*        Allow manual attachment of a driver to a device.
*        Caller must have already set @dev->driver.
*
*        Note that this does not modify the bus reference count
*        nor take the bus's rwsem. Please verify those are accounted
*        for before calling this. (It is ok to call with no other effort
*        from a driver's probe() method.)
*
*        This function must be called with @dev->sem held.
*/
int device_bind_driver(struct device *dev)
{
        int ret;

        ret = driver_sysfs_add(dev);
        if (!ret)
                driver_bound(dev); //通知鏈
        return ret;
}

到此,設(shè)備與驅(qū)動(dòng)的兩種綁定方式:在設(shè)備注冊(cè)時(shí)進(jìn)行綁定及在驅(qū)動(dòng)注冊(cè)時(shí)進(jìn)行綁定就介紹完了。



參考文獻(xiàn)
[1]Linux設(shè)備驅(qū)動(dòng)程序 第三版
[2]Linux 2.6.21.5內(nèi)核原代碼

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2008-11-25 17:48 |只看該作者
這個(gè)需要收藏。

論壇徽章:
0
4 [報(bào)告]
發(fā)表于 2010-01-06 09:50 |只看該作者
收藏了,謝謝版主~~

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 2010-11-25 14:29 |只看該作者
太給力了,必須的。

論壇徽章:
0
6 [報(bào)告]
發(fā)表于 2012-05-22 20:07 |只看該作者
好文章,收藏了,慢慢研究。

論壇徽章:
0
7 [報(bào)告]
發(fā)表于 2012-08-15 22:34 |只看該作者
看了晚上,現(xiàn)在終于看完,收獲很大,主要是對(duì)platform設(shè)備理解更深了,多謝樓主!
不過脖子有點(diǎn)痛啊!

論壇徽章:
0
8 [報(bào)告]
發(fā)表于 2012-10-09 17:03 |只看該作者
鼓勵(lì)一下,帖子非常好

論壇徽章:
0
9 [報(bào)告]
發(fā)表于 2013-01-22 23:46 |只看該作者
樓主厲害!頂

論壇徽章:
0
10 [報(bào)告]
發(fā)表于 2013-05-08 16:37 |只看該作者
樓主是否有空看看這個(gè)問題:http://72891.cn/thread-4080259-1-1.html
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP