转自:http://blog.csdn.net/xiafeng1113/article/details/8030248

Linux中 probe函数何时调用的

所以的驱动教程上都说:只有设备和驱动的名字匹配,BUS就会调用驱动的probe函数,但是有时我们要看看probe函数里面到底做了什么,还有传递给probe函数的参数我们就不知道在哪定义(反正不是我们在驱动里定义的),如果不知道传递进的参数,去看probe函数总是感觉不求甚解的样子(你对系统不求甚解,系统也会对你的要求不求甚解的),心里对自己写出的程序没底,保不齐那天来个bug,就悲剧了。

这里以static int__devinit sst25l_probe(struct spi_device *spi)为例看看传递进的参数structspi_device *spi到底是什么,在哪定义,什么时候定义,定义了有什么用…?(本着“five W and H”的原则打破沙锅问到底)。首先struct spi_device *spi不是我们定义的驱动里定义的;其次在read,write等函数里都有struct spi_device *spi的影子,不过不是直接传递进去的,而是通过传递进去struct mtd_info *mtd,然后to_sst25l_flash(mtd),即container_of()出包含mtd的struct sst25l_flash *flash,其中flash里的第一个成员就是structspi_device *spi,而此成员的赋值就是将传递给probe中的struct spi_device *spi赋值给struct sst25l_flash *flash的,有代码为证:

static int __devinit sst25l_probe(structspi_device *spi)

{

structflash_info *flash_info;

structsst25l_flash *flash;

……

flash->spi = spi;// 将structspi_device *spi赋值给struct sst25l_flash *flash

mutex_init(&flash->lock);

dev_set_drvdata(&spi->dev,flash);// &spi->dev ->p->driver_data = flash保持flash

……

}

所以搞清楚structspi_device *spi的来源是搞清楚设备驱动与主控驱动的联系纽带的关键之一,当然要首先搞清楚probe函数什么时候调用才能搞清楚struct spi_device *spi怎么传递的,其重要性不言而喻(虽然言了很多,^-^,有点唐僧了)。我们先从驱动的init开始入手,毕竟这是驱动注册开始的地方,也是一系列后续操作引发的地方:

static int __init sst25l_init(void)

{

returnspi_register_driver(&sst25l_driver);

}

里面只有一个函数,最喜欢这样的函数了:

int spi_register_driver(struct spi_driver*sdrv)

{

sdrv->driver.bus= &spi_bus_type;

if(sdrv->probe)

sdrv->driver.probe= spi_drv_probe;

if(sdrv->remove)

sdrv->driver.remove= spi_drv_remove;

if(sdrv->shutdown)

sdrv->driver.shutdown= spi_drv_shutdown;

return driver_register(&sdrv->driver);

}

前面都是赋值,直接最后一个语句:

int driver_register(struct device_driver*drv)

{

intret;

structdevice_driver *other;

……

ret = bus_add_driver(drv);

if(ret)

returnret;

ret= driver_add_groups(drv, drv->groups);

if(ret)

bus_remove_driver(drv);

returnret;

}

bus_add_driver(drv)看着就像“好人”:

int bus_add_driver(struct device_driver*drv)

{

structbus_type *bus;

structdriver_private *priv;

interror = 0;

……

if(drv->bus->p->drivers_autoprobe) {

error= driver_attach(drv);

if(error)

goto out_unregister;

}

……

}

driver_attach看着也很“友善”(函数名中带get,init的一般都不是,如果里面有几个“友善”的,一首歌中已经告诉了我们解决的办法:“xx就像偶尔拨不通的电话号码,多试几次总会回答,……”,如果网上找不到,只好挨个跟踪了,我就这样找的,笨人只好采取笨办法,也是没有办法的办法了):

int driver_attach(struct device_driver*drv)

{

returnbus_for_each_dev(drv->bus, NULL, drv, __driver_attach);

}

里面只有一个函数,goon:

int bus_for_each_dev(struct bus_type *bus,struct device *start, void *data, int (*fn)(struct device *, void *))

{

structklist_iter i;

structdevice *dev;

interror = 0;

if(!bus)

return -EINVAL;

klist_iter_init_node(&bus->p->klist_devices,&i, (start ? &start->p->knode_bus : NULL));

while((dev = next_device(&i)) && !error)

error = fn(dev,data);

klist_iter_exit(&i);

returnerror;

}

看到这里好像没有我们想要找的attach,只执行了个fn()函数,肿么回事?到回头看看哪里漏了,在bus_for_each_dev中传递了个 __driver_attach,也就是在bus_for_each_dev执行了__driver_attach(dev, data),那么它里面到底执行了什么?

static int __driver_attach(struct device*dev, void *data)

{

structdevice_driver *drv = data;

if (!driver_match_device(drv, dev))

return 0;

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

return0;

}

有个driver_probe_device(drv,dev),继续跟踪:

int driver_probe_device(structdevice_driver *drv, struct device *dev)

{

intret = 0;

……

ret = really_probe(dev, drv);

pm_runtime_put_sync(dev);

returnret;

}

有个really_probe(dev,drv),linux神马的就喜欢这样,经常一个函数传递给另一函数,后一个函数就是在前一个函数前加“do_”、“really_”、“__”,还经常的就是宏定义的或inline的。

static int really_probe(struct device *dev,struct device_driver *drv)

{

intret = 0;

……

if(dev->bus->probe) {

ret = dev->bus->probe(dev);

if(ret)

gotoprobe_failed;

}else if (drv->probe) {

ret = drv->probe(dev);

if(ret)

gotoprobe_failed;

}

……

returnret;

}

这里如果有总线上的probe函数就调用总线的probe函数,如果没有则调用drv的probe函数。

在static int__driver_attach(struct device *dev, void *data)中先调用了driver_match_device(drv,dev),用于匹配,成功才继续执行,否则直接返回了。driver_match_device(drv, dev)中:

static inline intdriver_match_device(struct device_driver *drv,

struct device *dev)

{

returndrv->bus->match ? drv->bus->match(dev, drv) : 1;

}

即如果match函数的指针不为空,则执行此bus的match函数,也就是为什么资料上老是说总线负责匹配设备和驱动了。这里也传递了参数struct device *dev,到底这个dev来自何方,会在下一篇文章中继续跟踪。

本文章参考:http://blog.chinaunix.net/space.php?uid=15887868&do=blog&id=2758294,对原作者表示感谢!

probe函数何时调用的的更多相关文章

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

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

  2. linux中 probe函数的何时调用的?

    点击打开链接 linux中 probe函数何时调用的 所以的驱动教程上都说:只有设备和驱动的名字匹配,BUS就会调用驱动的probe函数,但是有时我们要看看probe函数里面到底做了什么,还有传递给p ...

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

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

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

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

  5. linux中probe函数中传递的参数来源(上)

    点击打开链接 上一篇中,我们追踪了probe函数在何时调用,知道了满足什么条件会调用probe函数,但probe函数中传递的参数我们并不知道在何时定义,到底是谁定义的,反正不是我们在驱动中定义的(当然 ...

  6. platform驱动之probe函数

    驱动注册的probe函数 probe函数在设备驱动注册最后收尾工作,当设备的device 和其对应的driver 在总线上完成配对之后,系统就调用platform设备的probe函数完成驱动注册最后工 ...

  7. Direct3D Draw函数 异步调用原理解析

    概述 在D3D10中,一个基本的渲染流程可分为以下步骤: 清理帧缓存: 执行若干次的绘制: 通过Device API创建所需Buffer: 通过Map/Unmap填充数据到Buffer中: 将Buff ...

  8. Entity Framework 6 Recipes 2nd Edition(11-4)译 -> 在”模型定义”函数里调用另一个”模型定义”函数

    11-4.在”模型定义”函数里调用另一个”模型定义”函数 问题 想要用一个”模型定义”函数去实现另一个”模型定义”函数 解决方案 假设我们已有一个公司合伙人关系连同它们的结构模型,如Figure 11 ...

  9. JS中函数的调用和this的值

    调用每一个函数会暂停当前函数的执行,传递控制权和参数给新函数.除了声明时定义的形式参数,每个函数还接收两个附加的参数:this 和 arguments. 参数this在面向对象编程中非常重要,他的值取 ...

随机推荐

  1. Python super继承详解

    MRO(Method resolution order)是python用来解析方法调用顺序的,mro中记录了一个类的所有基类的类类型序列,super不是简单地调用基类的方法,而是按照MRO中的顺序来调 ...

  2. 有向图寻找(一个)奇环 -- find an oddcycle in directed graph

    /// the original blog is http://www.cnblogs.com/tmzbot/p/5579020.html , automatic crawling without l ...

  3. C#基础语法实例荟萃

    匿名类 action = new UploadHandler(context, new UploadConfig() { AllowExtensions = Config.GetStringList( ...

  4. Python连接MySQL的准备工作

    首先要安装MySQL,64位的win7可以安装64或者32位的MySQL版本,安装之后,python需要一个工具才能连接MySQL,这个工具叫MySQL-python,去这里或者这里下载1.2.3版本 ...

  5. poj 1125 (floyd)

    http://poj.org/problem?id=1125. 题意:在经纪人的圈子里,他们各自都有自己的消息来源,并且也只相信自己的消息来源,他们之间的信息传输也需要一定的时间.现在有一个消息需要传 ...

  6. ios oc 和 swfit 用dispatch_once 创建单例

    网上已经有方法了,我这里就是抄了下,原文链接 http://bj007.blog.51cto.com/1701577/649413 http://blog.csdn.net/u010124617/ar ...

  7. Redis集群的部署

    Redis集群分为主节点Master和从节点Slave,主节点只有1个,而从节点可以有多个,这样从节点和主节点可以进行数据的传输,Redis集群的性能将比单机环境更高,接下来是配置的过程 首先配置Ma ...

  8. 初识 MySQL 5.6 新功能、参数

    摘要: 继上一篇的文章 初识 MySQL 5.5 新功能.参数 之后,现在MySQL5.6 针对 MySQL5.5 各个方面又提升了很多,特别在性能和一些新参数上面,现在看看大致提升了哪些方面(后续不 ...

  9. HDU 3957 Street Fighter(搜索、DLX、重复覆盖+精确覆盖)

    很久以前就看到的一个经典题,一直没做,今天拿来练手.街霸 给n<=25个角色,每个角色有 1 or 2 个版本(可以理解为普通版以及爆发版),每个角色版本可以KO掉若干人. 问最少选多少个角色( ...

  10. 安装/移除Windows服务

    在工作中,涉及到相关Windows的开发时,常常要安装.移除Windows服务程序.一般地,可通过cmd命令行操作来完成~ cd 服务程序所在目录 *.exe -i // 安装服务 *.exe -s ...