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

2024-02-12 11:32

本文主要是介绍linux中probe函数中传递的参数来源(上),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 linux中probe函数传递参数的寻找(上)

        上一篇中,我们追踪了probe函数在何时调用,知道了满足什么条件会调用probe函数,但probe函数中传递的参数我们并不知道在何时定义,到底是谁定义的,反正不是我们在驱动中定义的(当然,驱动中也不会定义设备的详细信息),但也不是在我们设备信息定义时的结构体。这就相当于武林绝学中只打通了任脉,而督脉还没打通,要想成为武林高手还差一步,本文就致力于打通我们设备驱动probe函数的任督二脉,做到正向逆向全顺畅,当任督二脉全都打通后,。。。,就可以独步武林、指点江山啦,再然后按照武林高手成名后既定的流程,该寂寞地隐去了(好像又再做白日梦了),当然了Linux中值得我们要学的多着呢,除了编写内核的那帮家伙们偶尔会寂寞下外,我们还是没有多少时间留给我们寂寞的(^_^)。

         虽然不知道probe函数的参数怎么来的,但没吃过猪肉,还是见过猪跑的,有点关系就能找到出路。经常听说:先注册设备时,内核会将设备信息挂到设备链上,然后等待命中注定的有缘的设备驱动mm or gg。so,我们可以猜想:应该是设备注册的时候,内核将设备信息挂到上面去的,按照这个猜想,我们应该先从设备注册入手,但是这么多函数到底朝哪个方向努力呀?所以,先从传递的参数入手,查看下,等走不通了在去从设备注册入手,起码有了努力的方向了。

调用probe函数的是:static int really_probe(struct device *dev, struct device_driver*drv),里面有调用ret = dev->bus->probe(dev)和ret =drv->probe(dev)。函数如下:

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

{

         intret = 0;

......

 

         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;

         }

 

......

         returnret;

}

这里的参数dev是上一个函数传递进来的,上一个函数为:int driver_probe_device(struct device_driver *drv, struct device*dev)

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

{

         intret = 0;

  ......

         ret = really_probe(dev, drv);


...... 

         returnret;

}

这里的dev又是上一个函数传递进来的,上一个函数为:static int __driver_attach(struct device *dev, void *data)

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

{

         structdevice_driver *drv = data;

  ......

         device_lock(dev);

         if(!dev->driver)

                   driver_probe_device(drv, dev);

         device_unlock(dev);

        ......

         return0;

}

这里的dev又是上一个函数传递进来的,调用__driver_attach的函数为:int driver_attach(struct device_driver *drv),但本函数没有给__driver_attach传递参数。

int driver_attach(structdevice_driver *drv)

{

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

}

         这里面调用了__driver_attach,对应error =fn(dev, data)。第一个参数dev为while ((dev = next_device(&i)) && !error)产生。即dev有i间接产生。

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;

....

 

         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;

}

之所以是next_device(&i),因为第一个节点为头节点,需要从下一个开始,先看看klist_iter_init_node(&bus->p->klist_devices, &i, (start ? &start->p->knode_bus : NULL))对i干了什么?因为start为NULL,故传递的第三个参数n为NULL。

void klist_iter_init_node(struct klist *k,struct klist_iter *i,

                              struct klist_node *n)

{

         i->i_klist= k;

         i->i_cur= n;

         if(n)

                   kref_get(&n->n_ref);

}

         看来ta没干什么,就是赋了两个值。然后再看最重要的next_device(&i)

static struct device *next_device(struct klist_iter *i)

{

         structklist_node *n = klist_next(i);

         structdevice *dev = NULL;

         structdevice_private *p;

 

         if(n) {

                   p = to_device_private_parent(n);

                   dev = p->device;

         }

         returndev;

}

#define to_device_private_parent(obj)  \

         container_of(obj,struct device_private, knode_parent)

         看到dev由p->device赋值,p为struct device_private,n = i->i_cur为structklist_node 型(后面分析)。为了看懂这个函数,需要补充N多知识,先上几个struct:

struct klist_iter {

         structklist                 *i_klist;

         structklist_node      *i_cur;

};

 

struct klist {

         spinlock_t                  k_lock;

         structlist_head        k_list;

         void                    (*get)(struct klist_node *);

         void                    (*put)(struct klist_node *);

} __attribute__ ((aligned (sizeof(void*))));

 

struct klist_node {

         void                    *n_klist;   /* never access directly */

         structlist_head        n_node;

         structkref                  n_ref;

};

 

struct kref {

         atomic_trefcount;

};

 

         其中的klist_iter_init_node(&bus->p->klist_devices, &i,(start ?&start->p->knode_bus : NULL))作用是定义个klist_iter指向此klist,以便以后直接使用,如图:

 

         再把关键的函数拷到此处,以遍分析:

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

                   error = fn(dev, data);

static struct device *next_device(struct klist_iter *i)

{

         structklist_node *n = klist_next(i);

         structdevice *dev = NULL;

         structdevice_private *p;

 

         if(n) {

                   p = to_device_private_parent(n);

                   dev = p->device;

         }

         returndev;

}

 

/**

 *klist_next - Ante up next node in list.

 *@i: Iterator structure.

 *

 *First grab list lock. Decrement the reference count of the previous

 *node, if there was one. Grab the next node, increment its reference

 *count, drop the lock, and return that next node.

 */

struct klist_node *klist_next(struct klist_iter *i)

{

         void(*put)(struct klist_node *) = i->i_klist->put;

         structklist_node *last = i->i_cur;//NULL

         structklist_node *next;

 

         spin_lock(&i->i_klist->k_lock);

 

         if(last) {

                   next= to_klist_node(last->n_node.next);

                   if(!klist_dec_and_del(last))

                            put= NULL;

         }else

                   next= to_klist_node(i->i_klist->k_list.next);

 

         i->i_cur= NULL;

         while(next != to_klist_node(&i->i_klist->k_list)){

                   if(likely(!knode_dead(next))) {

                            kref_get(&next->n_ref);

                            i->i_cur = next;

                            break;

                   }

                   next= to_klist_node(next->n_node.next);

         }

 

         spin_unlock(&i->i_klist->k_lock);

 

         if(put && last)

                   put(last);

         returni->i_cur;

}

         这里last =i->i_cur;为NULL,然后执行next = to_klist_node(i->i_klist->k_list.next);从这个函数来看,就是取出了包含i->i_klist->k_list.next的n_node指针。不过next所指的和n_node地址偏差一个head指针(list_head包括head和next俩指针)。while循环是从第一个目标to_klist_node(i->i_klist->k_list.next)循环,当再次循环到头节点to_klist_node(&i->i_klist->k_list)时截止(这是个循环链表,总会再次循环回来的)。还一个结束的条件,当循环到knode_dead(next)为真时break,不过,likely说明了next通常不会是dead的,(struct klist_node的第一个成员最后一位做标志dead位,网上还说有指针的作用,我觉得好像做了标志位了就不能做指向头节点的指针了,不过void *n_klist名字起得确实很有迷惑性)。

static struct klist_node*to_klist_node(struct list_head *n)

{

         returncontainer_of(n, struct klist_node, n_node);

}

         还一个i的来源,ta是一切的来源。在klist_iter_init_node(&bus->p->klist_devices,&i,                               (start ? &start->p->knode_bus :NULL))中,       i->i_klist = &bus->p->klist_devices;i->i_cur = NULL;

 

         Klist_iter找到合适的即停止搜索,找到此处的device_private的device,此结构即为传入probe函数的参数。device源于i(i只是暂时用于查找定义的一个临时变量),而i源于bus,bus源于drv->bus,drv源于sdrv->driver,sdrv即为mx25lx_driver,不过mx25lx_driver->driver中的bus,只给赋了一个值,而在后来调用标准的spi函数时,又重新对bus赋了值spi_bus_type,spi_bus_type是spi.c中的struct bus_type定义的全局变量。

 

这篇关于linux中probe函数中传递的参数来源(上)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/702501

相关文章

防止Linux rm命令误操作的多场景防护方案与实践

《防止Linuxrm命令误操作的多场景防护方案与实践》在Linux系统中,rm命令是删除文件和目录的高效工具,但一旦误操作,如执行rm-rf/或rm-rf/*,极易导致系统数据灾难,本文针对不同场景... 目录引言理解 rm 命令及误操作风险rm 命令基础常见误操作案例防护方案使用 rm编程 别名及安全删除

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

使用docker搭建嵌入式Linux开发环境

《使用docker搭建嵌入式Linux开发环境》本文主要介绍了使用docker搭建嵌入式Linux开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1、前言2、安装docker3、编写容器管理脚本4、创建容器1、前言在日常开发全志、rk等不同

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

HTTP 与 SpringBoot 参数提交与接收协议方式

《HTTP与SpringBoot参数提交与接收协议方式》HTTP参数提交方式包括URL查询、表单、JSON/XML、路径变量、头部、Cookie、GraphQL、WebSocket和SSE,依据... 目录HTTP 协议支持多种参数提交方式,主要取决于请求方法(Method)和内容类型(Content-Ty

GO语言中函数命名返回值的使用

《GO语言中函数命名返回值的使用》在Go语言中,函数可以为其返回值指定名称,这被称为命名返回值或命名返回参数,这种特性可以使代码更清晰,特别是在返回多个值时,感兴趣的可以了解一下... 目录基本语法函数命名返回特点代码示例命名特点基本语法func functionName(parameters) (nam

linux系统上安装JDK8全过程

《linux系统上安装JDK8全过程》文章介绍安装JDK的必要性及Linux下JDK8的安装步骤,包括卸载旧版本、下载解压、配置环境变量等,强调开发需JDK,运行可选JRE,现JDK已集成JRE... 目录为什么要安装jdk?1.查看linux系统是否有自带的jdk:2.下载jdk压缩包2.解压3.配置环境

Linux搭建ftp服务器的步骤

《Linux搭建ftp服务器的步骤》本文给大家分享Linux搭建ftp服务器的步骤,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录ftp搭建1:下载vsftpd工具2:下载客户端工具3:进入配置文件目录vsftpd.conf配置文件4:

Python Counter 函数使用案例

《PythonCounter函数使用案例》Counter是collections模块中的一个类,专门用于对可迭代对象中的元素进行计数,接下来通过本文给大家介绍PythonCounter函数使用案例... 目录一、Counter函数概述二、基本使用案例(一)列表元素计数(二)字符串字符计数(三)元组计数三、C