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

2024-01-05 11:58

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

应该是设备注册的时候,内核将设备信息挂到上面去的,按照这个猜想,我们应该先从设备注册入手,但是这么多函数到底朝哪个方向努力呀?所以,先从传递的参数入手,查看下,等走不通了在去从设备注册入手,起码有了努力的方向了。

调用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/572788

相关文章

Linux之systemV共享内存方式

《Linux之systemV共享内存方式》:本文主要介绍Linux之systemV共享内存方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、工作原理二、系统调用接口1、申请共享内存(一)key的获取(二)共享内存的申请2、将共享内存段连接到进程地址空间3、将

快速修复一个Panic的Linux内核的技巧

《快速修复一个Panic的Linux内核的技巧》Linux系统中运行了不当的mkinitcpio操作导致内核文件不能正常工作,重启的时候,内核启动中止于Panic状态,该怎么解决这个问题呢?下面我们就... 感谢China编程(www.chinasem.cn)网友 鸢一雨音 的投稿写这篇文章是有原因的。为了配置完

SpringBoot请求参数接收控制指南分享

《SpringBoot请求参数接收控制指南分享》:本文主要介绍SpringBoot请求参数接收控制指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring Boot 请求参数接收控制指南1. 概述2. 有注解时参数接收方式对比3. 无注解时接收参数默认位置

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

Spring 请求之传递 JSON 数据的操作方法

《Spring请求之传递JSON数据的操作方法》JSON就是一种数据格式,有自己的格式和语法,使用文本表示一个对象或数组的信息,因此JSON本质是字符串,主要负责在不同的语言中数据传递和交换,这... 目录jsON 概念JSON 语法JSON 的语法JSON 的两种结构JSON 字符串和 Java 对象互转

Python正则表达式语法及re模块中的常用函数详解

《Python正则表达式语法及re模块中的常用函数详解》这篇文章主要给大家介绍了关于Python正则表达式语法及re模块中常用函数的相关资料,正则表达式是一种强大的字符串处理工具,可以用于匹配、切分、... 目录概念、作用和步骤语法re模块中的常用函数总结 概念、作用和步骤概念: 本身也是一个字符串,其中

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

Linux命令之firewalld的用法

《Linux命令之firewalld的用法》:本文主要介绍Linux命令之firewalld的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux命令之firewalld1、程序包2、启动firewalld3、配置文件4、firewalld规则定义的九大

Linux之计划任务和调度命令at/cron详解

《Linux之计划任务和调度命令at/cron详解》:本文主要介绍Linux之计划任务和调度命令at/cron的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux计划任务和调度命令at/cron一、计划任务二、命令{at}介绍三、命令语法及功能 :at

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文