点击打开链接

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,对原作者表示感谢!

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

  1. linux中probe函数传递参数的寻找(下)

    点击打开链接 linux中probe函数传递参数的寻找(下) 通过追寻driver的脚步,我们有了努力的方向:只有找到spi_bus_type的填充device即可,下面该从device去打通,当两个 ...

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

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

  3. 【转】linux 中fork()函数详解

    在看多线程的时候看到了这个函数,于是学习了下,下面文章写的通俗易懂,于是就开心的看完了,最后还是很愉快的算出了他最后一个问题. linux 中fork()函数详解 一.fork入门知识 一个进程,包括 ...

  4. open()函数 linux中open函数使用

    来源:http://www.cnblogs.com/songfeixiang/p/3733855.html   linux中open函数使用 open函数用来打开一个设备,他返回的是一个整型变量,如果 ...

  5. 有关linux中,<math.h>的调用方法

    h{font-weight:bold;color:green;font-size:105%} p{font-size:100%} linux下C语言程序中,若要用到math.h中的函数(如:sin() ...

  6. Linux中fork()函数详解(转载)

    linux中fork()函数详解 一.fork入门知识 一个进程,包括代码.数据和分配给进程的资源.fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程,也就是两个进程可以做完全相同的事, ...

  7. Linux 中write()函数的出错情况及处理

    write函数首先将进程需要发送的数据先放在进程缓冲区中,然后向socket的发送缓冲区进行拷贝,在此,可能出现这样情况,即当进程缓冲区中的数据量大于此时发送缓冲区中所能接受的数据量时,若此时处于阻塞 ...

  8. Linux中open函数以及退出进程的函数

    open函数的flag详解1 读写权限:O_RDONLY O_WRONLY O_RDWR (1)linux中文件有读写权限,我们在open打开文件时也可以附带一定的权限说明 (譬如O_RDONLY就表 ...

  9. 4.C++中的函数重载,C++调用C代码,new/delete关键字,namespace(命名空间)

    本章主要内容: 1)函数重载 2)C++调用C代码 3)new/delete关键字实现动态内存分配 4)namespace命名空间 大家都知道,在生活中,动词和不同的名词搭配一起,意义都会大有不同,比 ...

随机推荐

  1. jsonViewer json格式化工具

    以前一直以来都觉得xml个可读性要比json的可读性好,后来使用了JSON Viewer这个小工具之后,发现自己错了.之前认为json的可读性差,完全是因为没有很好的查看工具.JSON Viewer这 ...

  2. 20 个 Laravel Eloquent 必备的实用技巧

    Eloquent ORM 看起来是一个简单的机制,但是在底层,有很多半隐藏的函数和鲜为人知的方式来实现更多功能.在这篇文章中,我将演示几个小技巧. 1. 递增和递减 要代替以下实现: $article ...

  3. Java数据类型与SQL数据类型的映射

    Java数据类型与SQL数据类型的映射 SQL Data Type Java Data Type char/varchar/longvarchar String numeric/decimal jav ...

  4. Docker入门之--定制镜像

    1. 首先定制一个Web 服务器为例 1.1 启动镜像 执行下面命令 docker run --name webserver -d -p 80:80 nginx 1.2 查看容器和镜像状态 然后执行下 ...

  5. 生物分子gene

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAswAAAHoCAYAAABdBLmnAAAgAElEQVR4nOzdB5gsRfU+/kZyTiIgCJ

  6. 计蒜客NOIP2017提高组模拟赛(五)day2-蚂蚁搬家

    传送门 这题可以用线段树来维护 #include<cstdio> #include<cstdlib> #include<algorithm> #include< ...

  7. [BZOJ]4650 优秀的拆分(Noi2016)

    比较有意思的一道后缀数组题.(小C最近是和后缀数组淦上了?) 放在NOI的考场上.O(n^3)暴力80分,O(n^2)暴力95分…… 即使想把它作为一道签到题也不要这么随便啊摔(╯‵□′)╯︵┻━┻ ...

  8. JS中三种字符串连接方式及其性能比较

    工作中经常会碰到要把2个或多个字符串连接成一个字符串的问题,在JS中处理这类问题一般有三种方法,这里将它们一一列出顺便也对它们的性能做个具体的比较. 第一种方法  用连接符“+”把要连接的字符串连起来 ...

  9. 【Git】Git工具常用命令

    GitHub使用指南 一.把本地代码上传到GitHub 0. 提前配置好上传地址 [git config --global user.name "username"] [git c ...

  10. Linux学习之CentOS(十四)----磁盘管理之 硬连接与软件连接(转)

    前言 在 Linux 底下的连结档有两种,一种是类似 Windows 的快捷方式功能的文件,可以让你快速的链接到目标文件(或目录),这种是软链接: 另一种则是透过文件系统的 inode 连结来产生新档 ...