int platform_driver_register(struct platform_driver *drv)
{
drv->driver.bus = &platform_bus_type;/*关联总线*/
/*关联driver的设备方法*/
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; return driver_register(&drv->driver);/*注册驱动*/
} /******************************************************************************/
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 (*resume)(struct platform_device *);
struct device_driver driver;
const struct platform_device_id *id_table;
}; struct bus_type platform_bus_type = {
.name = "platform",
.dev_attrs = platform_dev_attrs,
.match = platform_match,
.uevent = platform_uevent,
.pm = &platform_dev_pm_ops,
};
/********************************************************************************/ int driver_register(struct device_driver *drv)
{
int ret;
struct device_driver *other; BUG_ON(!drv->bus->p); 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); other = driver_find(drv->name, drv->bus);
if (other) {
put_driver(other);
printk(KERN_ERR "Error: Driver '%s' is already registered, "
"aborting...\n", drv->name);
return -EBUSY;
} ret = bus_add_driver(drv);
if (ret)
return ret;
ret = driver_add_groups(drv, drv->groups);
if (ret)
bus_remove_driver(drv);
return ret;
} int bus_add_driver(struct device_driver *drv)
{
struct bus_type *bus;
struct driver_private *priv;
int error = ; bus = bus_get(drv->bus);
if (!bus)
return -EINVAL; pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name); priv = kzalloc(sizeof(*priv), GFP_KERNEL);
if (!priv) {
error = -ENOMEM;
goto out_put_bus;
}
klist_init(&priv->klist_devices, NULL, NULL);
priv->driver = drv;
drv->p = priv;
priv->kobj.kset = bus->p->drivers_kset;
error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
"%s", drv->name);
if (error)
goto out_unregister; if (drv->bus->p->drivers_autoprobe) {
error = driver_attach(drv);
if (error)
goto out_unregister;
}
klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
module_add_driver(drv->owner, drv); error = driver_create_file(drv, &driver_attr_uevent);
if (error) {
printk(KERN_ERR "%s: uevent attr (%s) failed\n",
__func__, drv->name);
}
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",
__func__, drv->name);
} if (!drv->suppress_bind_attrs) {
error = add_bind_files(drv);
if (error) {
/* Ditto */
printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
__func__, drv->name);
}
} kobject_uevent(&priv->kobj, KOBJ_ADD);
return ; out_unregister:
kobject_put(&priv->kobj);
kfree(drv->p);
drv->p = NULL;
out_put_bus:
bus_put(bus);
return error;
} int driver_attach(struct device_driver *drv)
{
/*对总线上的每一个设备都调用__driver_attach*/
return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
} 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 (!driver_match_device(drv, dev))
return ; if (dev->parent) /* Needed for USB */
device_lock(dev->parent);
device_lock(dev);
if (!dev->driver)
driver_probe_device(drv, dev);
device_unlock(dev);
if (dev->parent)
device_unlock(dev->parent); return ;
} static inline int driver_match_device(struct device_driver *drv,
struct device *dev)
{
/*调用总线的match去匹配设备和驱动*/
return drv->bus->match ? drv->bus->match(dev, drv) : ;
} int driver_probe_device(struct device_driver *drv, struct device *dev)
{
int ret = ; if (!device_is_registered(dev))
return -ENODEV; pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
drv->bus->name, __func__, dev_name(dev), drv->name); pm_runtime_get_noresume(dev);
pm_runtime_barrier(dev);
ret = really_probe(dev, drv);
pm_runtime_put_sync(dev); return ret;
}
static int really_probe(struct device *dev, struct device_driver *drv)
{
int ret = ; atomic_inc(&probe_count);
pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
drv->bus->name, __func__, drv->name, dev_name(dev));
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",
__func__, dev_name(dev));
goto probe_failed;
}
/**********************************************************************************/
if (dev->bus->probe) {/*首先看总线有没有probe函数,若有则调用,而平台总线没有probe*/
ret = dev->bus->probe(dev);
if (ret)
goto probe_failed;
} else if (drv->probe) {/*然后看驱动有没有probe函数,若有则调用,*/
ret = drv->probe(dev);
if (ret)
goto probe_failed;
}
/************************************************************************************/ driver_bound(dev);
ret = ;
pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
drv->bus->name, __func__, dev_name(dev), 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_name(dev), ret);
}
/*
* Ignore errors returned by ->probe so that the next driver can try
* its luck.
*/
ret = ;
done:
atomic_dec(&probe_count);
wake_up(&probe_waitqueue);
return ret;
} /*平台总线的match逻辑*/
static int platform_match(struct device *dev, struct device_driver *drv)
{
struct platform_device *pdev = to_platform_device(dev);
struct platform_driver *pdrv = to_platform_driver(drv); /* match against the id table first */
if (pdrv->id_table)
return platform_match_id(pdrv->id_table, pdev) != NULL; /* fall-back to driver name match */
return (strcmp(pdev->name, drv->name) == );/*驱动名字与设备名字要匹配*/
}

总结一下:

platform_driver_register

  driver_register

    bus_add_driver

      driver_attach

        __driver_attach

          driver_probe_device

            really_probe

               if (dev->bus->probe) {/*首先看总线有没有probe函数,若有则调用,而平台总线没有probe*/
                                                ret = dev->bus->probe(dev);
                                        if (ret)
                                                goto probe_failed;
                                         } else if (drv->probe) {/*然后看驱动有没有probe函数,若有则调用,*/
                                                 ret = drv->probe(dev);
                                         if (ret)
                                                goto probe_failed;
                                        }

platform_driver_register()--如何match之后调用probe的更多相关文章

  1. (转)platform_driver_register,什么时候调用PROBE函数 注册后如何找到驱动匹配的设备

     platform_driver_register,什么时候调用PROBE函数 注册后如何找到驱动匹配的设备 2011-10-24 19:47:07 分类: LINUX   kernel_init中d ...

  2. platform_driver_register,什么时候调用PROBE函数 注册后如何找到驱动匹配的设备【转】

    转自:http://blog.chinaunix.net/uid-25508271-id-2979412.html kernel_init中do_basic_setup()->driver_in ...

  3. 注册驱动时如何调用probe函数 ?

    platform_driver_register       driver_register             bus_add_driver    //把驱动放入总线的驱动链表里         ...

  4. platform_device和platform_driver的注册过程,及probe函数何时调用的分析 ⭐⭐⭐

    add  platform_device之后,需要注意的一个地方是这里,add是通过系统初始化里边调用platform_add_devices把所有放置在板级platform_device数组中的所有 ...

  5. linux设备驱动归纳总结(八):2.match.probe.remove

    linux设备驱动归纳总结(八):2.总线.设备和驱动的关系 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...

  6. [驱动注册]platform_driver_register()与platform_device_register()

    [驱动注册]platform_driver_register()与platform_device_register()      设备与驱动的两种绑定方式:在设备注册时进行绑定及在驱动注册时进行绑定. ...

  7. (linux)platform_driver_probe与platform_driver_register的区别

      [驱动注册]platform_driver_register()与platform_device_register()          设备与驱动的两种绑定方式:在设备注册时进行绑定及在驱动注册 ...

  8. Linux+I2C总线分析(主要是probe的方式)

    Linux I2C 总线浅析 ㈠ Overview Linux的I2C体系结构分为3个组成部分: ·I2C核心: I2C核心提供了I2C总线驱动和设备驱动的注册.注销方法,I2C通信方法(即“algo ...

  9. I2C驱动程序框架probe道路

    基于Linux的I2C驱动器.采纳probe道路.根据这个框架,如下面就可以写任何支持I2C总线设备Linux驱动器. I2C设备连接到cpu具体i2c接口.被安装在cpu的i2c适配器.i2c设备和 ...

随机推荐

  1. 学习笔记之APACHE ANT

    http://baike.baidu.com/link?url=KkOWkH_nMVJRbd4oj-aIHMVL4HR-s7fqm3i2brUcZzBinwUXjZgzPcYIWz5qFNNvjait ...

  2. JavaScript网站设计实践(四)编写about.html页面,利用JavaScript和DOM,选择性的显示和隐藏DIV元素

    一.现在我们在网站设计(三)的基础上,来编写about.html页面. 这个页面要用到的知识点是利用JavaScript和DOM实现选择性地显示和隐藏某些DIV about.html页面在前面我们为了 ...

  3. c++ (P10—46)

    1 signed unsigned short long 四个修饰符.对int全部适用,对char只有signed和unsigned适用,long适用于double. 2 long int(占4个字节 ...

  4. 工作中遇到的UIScrollview的问题及解决方案

    用scrollview的时候,记得创建scrollview后再在scrollview上放一个view,将其他子空间和子view都放在这个view上. 目前遇到的问题: ImagePlayerView( ...

  5. NekoHTML

    1.如何使用NekoHTML? 必须在 Java Build Path里加入 nekohtml.jar , xercesImpl.jar 以及xalan.jar.下载的NekoHTML目录中并没有xe ...

  6. ORM之PetaPoco入门(一)--Petapoco简介

    1. ORM概括 1.1. ORM简介 ORM 对象-关系映射(Object/Relation Mapping,简称ORM),是随着面向对象的软件开发方法发展而产生的.面向对象的开发方法是当今企业级应 ...

  7. [置顶] 运算符重载,浅拷贝(logical copy) ,vs, 深拷贝(physical copy),三大件(bigthree problem)

    一般的我们喜欢这样对对象赋值: Person p1;Person p2=p1; classT object(another_object), or    A a(b); classT object = ...

  8. /lib /usr/lib /usr/local/lib 区别

    简单说,/lib是内核级的,/usr/lib是系统级的,/usr/local/lib是用户级的. /lib/ — 包含许多被 /bin/ 和 /sbin/ 中的程序使用的库文件.目录 /usr/lib ...

  9. Ubuntu 14.04 & ant: Unable to locate tools.jar. Expected to find it in /usr/lib/jvm/java-7-openjdk-i386/lib/tools.jar

    当我在vagrant + ubuntu 14.04,jenkins ant执行项目的build.xml时,提示: [workspace] $ ant -file build.xml Unable to ...

  10. 揭开CSS3媒体查询迷雾(min-width和max-width)

    本文参考MichelleKlann的Media Queries Demystified: Min-Width and Max-Width 媒体查询(media queries)是响应式设计(Respo ...