免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 23765 | 回复: 6

platform_driver_register,什么时候调用PROBE函数 注册后如何找到驱动匹配的设备 [复制链接]

论坛徽章:
0
发表于 2008-07-31 17:35 |显示全部楼层
platform_driver_register(struct platform_driver *drv)注册后如何找到驱动匹配的设备

struct platform_driver {
        int (*probe)(struct platform_device *);
        int (*remove)(struct platform_device *);
        void (*shutdown)(struct platform_device *);
        int (*suspend)(struct platform_device *, pm_message_t state);
        int (*suspend_late)(struct platform_device *, pm_message_t state);
        int (*resume_early)(struct platform_device *);
        int (*resume)(struct platform_device *);
        struct device_driver driver;
};
struct platform_driver 的结构成员如上,那么注册了一个struct platform_driver *drv,内核怎么知道那个设备是该驱动想驱动的呢,在什么条件下才会调用该platform_driver *drv的PROBE函数呢?struct device_driver {
        const char                * name;
        struct bus_type                * bus;

        struct kobject                kobj;
        struct klist                klist_devices;
        struct klist_node        knode_bus;

        struct module                * owner;
        const char                 * mod_name;        /* used for built-in modules */
        struct module_kobject        * mkobj;

        int        (*probe)        (struct device * dev);
        int        (*remove)        (struct device * dev);
        void        (*shutdown)        (struct device * dev);
        int        (*suspend)        (struct device * dev, pm_message_t state);
        int        (*resume)        (struct device * dev);
};
struct device_driver 里的name成员是设备的名字还是改设备DRIVER的名字?这里有两个PROBE函数,是不是先调用struct device_driver 里的PROBE,然后在调用struct platform_driver里的PROBE函数

论坛徽章:
0
发表于 2008-08-01 11:25 |显示全部楼层
do_basic_setup()->driver_init()->platform_bus_init()->...初始化platform bus(虚拟总线)
设备向内核注册的时候platform_device_register()->platform_device_add()->...内核把设备挂在虚拟的platform bus下
驱动注册的时候platform_driver_register()->driver_register()->bus_add_driver()->driver_attach()->bus_for_each_dev()对每个挂在虚拟的platform bus的设备作__driver_attach()->driver_probe_device()->drv->bus->match()==platform_match()->比较strncmp(pdev->name, drv->name, BUS_ID_SIZE),如果相符就调用platform_drv_probe()->driver->probe(),如果probe成功则绑定该设备到该驱动.

论坛徽章:
0
发表于 2008-08-04 09:24 |显示全部楼层
怎么是通过驱动的名字和设备的名字来比较是否绑定该设备到该驱动,如果驱动换个名字,岂不。。。?

论坛徽章:
0
发表于 2008-08-04 10:54 |显示全部楼层
原帖由 lovesunshine 于 2008-7-31 17:35 发表
platform_driver_register(struct platform_driver *drv)注册后如何找到驱动匹配的设备

struct platform_driver {
        int (*probe)(struct platform_device *);
        int (*remove)(struct platfo ...



看看platform_driver_register()的代码,drivers/base/platform.c
432int platform_driver_register(struct platform_driver *drv)
433{
434        drv->driver.bus = &platform_bus_type;
435        if (drv->probe)
436                drv->driver.probe = platform_drv_probe;
437        if (drv->remove)
438                drv->driver.remove = platform_drv_remove;
439        if (drv->shutdown)
440                drv->driver.shutdown = platform_drv_shutdown;
441        if (drv->suspend)
442                drv->driver.suspend = platform_drv_suspend;
443        if (drv->resume)
444                drv->driver.resume = platform_drv_resume;
445        return driver_register(&drv->driver);
446}

可见platform_driver的一系列函数,在这儿全部赋值给了其成员struct device_driver driver里面的函数指针。系统启动检测时调用的driver->probe()也就是platform_driver的probe()函数

论坛徽章:
0
发表于 2008-08-04 10:55 |显示全部楼层
内核的platform device 和 platform driver应该只是个模板,你可以参照这个对特殊的platform写自己的驱动加载和设备检测机制,很多嵌入式平台都实现了自己的platform driver机制,包括platform bus, platform device, platform driver。这个可以参照powerpc或arm体系的某个platform。

你的实现可以像pci driver一样。
struct pci_driver里就有个id_table,用于设备和驱动匹配的检测。
static inline const struct pci_device_id *
pci_match_one_device(const struct pci_device_id *id, const struct pci_dev *dev)
{
        if ((id->vendor == PCI_ANY_ID || id->vendor == dev->vendor) &&
            (id->device == PCI_ANY_ID || id->device == dev->device) &&
            (id->subvendor == PCI_ANY_ID || id->subvendor == dev->subsystem_vendor) &&
            (id->subdevice == PCI_ANY_ID || id->subdevice == dev->subsystem_device) &&
            !((id->class ^ dev->class) & id->class_mask))
                return id;
        return NULL;
}

这样你就可以不必 通过驱动的名字和设备的名字来比较是否绑定该设备到该驱动 。

虚拟总线驱动->设备在总线上的挂接->驱动attach->总线检测设备->匹配驱动

论坛徽章:
0
发表于 2008-08-04 10:58 |显示全部楼层
pci设备检测是硬件实现的,系统初始化后设备都挂接好的,如果你的platform总线没有自动检测设备机制,你需要软件手工挂接

论坛徽章:
0
发表于 2008-08-04 10:59 |显示全部楼层
原帖由 readkernel 于 2008-8-4 10:55 发表
内核的platform device 和 platform driver应该只是个模板,你可以参照这个对特殊的platform写自己的驱动加载和设备检测机制,很多嵌入式平台都实现了自己的platform driver机制,包括platform bus, platform d ...


是的  驱动程序匹配一般就是看设备的PID/VID. 如果PID/VID符合驱动程序的要求,那么就match上那么这个设备使用这个驱动程序
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP