嵌入式 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生产者,消费者问题

pthread_cond_wait() :用于阻塞当前线程,等待别的线程使用pthread_cond_signal()或pthread_cond_broadcast来唤醒它。 pthread_cond_wait() 必须与pthread_mutex 配套使用。pthread_cond_wait()函数一进入wait状态就会自动release mutex。当其他线程通过pthread

Linux 安装、配置Tomcat 的HTTPS

Linux 安装 、配置Tomcat的HTTPS 安装Tomcat 这里选择的是 tomcat 10.X ,需要Java 11及更高版本 Binary Distributions ->Core->选择 tar.gz包 下载、上传到内网服务器 /opt 目录tar -xzf 解压将解压的根目录改名为 tomat-10 并移动到 /opt 下, 形成个人习惯的路径 /opt/tomcat-10

RedHat运维-Linux文本操作基础-AWK进阶

你不用整理,跟着敲一遍,有个印象,然后把它保存到本地,以后要用再去看,如果有了新东西,你自个再添加。这是我参考牛客上的shell编程专项题,只不过换成了问答的方式而已。不用背,就算是我自己亲自敲,我现在好多也记不住。 1. 输出nowcoder.txt文件第5行的内容 2. 输出nowcoder.txt文件第6行的内容 3. 输出nowcoder.txt文件第7行的内容 4. 输出nowcode

【Linux进阶】UNIX体系结构分解——操作系统,内核,shell

1.什么是操作系统? 从严格意义上说,可将操作系统定义为一种软件,它控制计算机硬件资源,提供程序运行环境。我们通常将这种软件称为内核(kerel),因为它相对较小,而且位于环境的核心。  从广义上说,操作系统包括了内核和一些其他软件,这些软件使得计算机能够发挥作用,并使计算机具有自己的特生。这里所说的其他软件包括系统实用程序(system utility)、应用程序、shell以及公用函数库等

【操作系统】信号Signal超详解|捕捉函数

🔥博客主页: 我要成为C++领域大神🎥系列专栏:【C++核心编程】 【计算机网络】 【Linux编程】 【操作系统】 ❤️感谢大家点赞👍收藏⭐评论✍️ 本博客致力于知识分享,与更多的人进行学习交流 ​ 如何触发信号 信号是Linux下的经典技术,一般操作系统利用信号杀死违规进程,典型进程干预手段,信号除了杀死进程外也可以挂起进程 kill -l 查看系统支持的信号

Windows/macOS/Linux 安装 Redis 和 Redis Desktop Manager 可视化工具

本文所有安装都在macOS High Sierra 10.13.4进行,Windows安装相对容易些,Linux安装与macOS类似,文中会做区分讲解 1. Redis安装 1.下载Redis https://redis.io/download 把下载的源码更名为redis-4.0.9-source,我喜欢跟maven、Tomcat放在一起,就放到/Users/zhan/Documents

ABAP怎么把传入的参数刷新到内表里面呢?

1.在执行相关的功能操作之前,优先执行这一段代码,把输入的数据更新入内表里面 DATA: lo_guid TYPE REF TO cl_gui_alv_grid.CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'IMPORTINGe_grid = lo_guid.CALL METHOD lo_guid->check_changed_data.CALL M

java中查看函数运行时间和cpu运行时间

android开发调查性能问题中有一个现象,函数的运行时间远低于cpu执行时间,因为函数运行期间线程可能包含等待操作。native层可以查看实际的cpu执行时间和函数执行时间。在java中如何实现? 借助AI得到了答案 import java.lang.management.ManagementFactory;import java.lang.management.Threa

Linux系统稳定性的奥秘:探究其背后的机制与哲学

在计算机操作系统的世界里,Linux以其卓越的稳定性和可靠性著称,成为服务器、嵌入式系统乃至个人电脑用户的首选。那么,是什么造就了Linux如此之高的稳定性呢?本文将深入解析Linux系统稳定性的几个关键因素,揭示其背后的技术哲学与实践。 1. 开源协作的力量Linux是一个开源项目,意味着任何人都可以查看、修改和贡献其源代码。这种开放性吸引了全球成千上万的开发者参与到内核的维护与优化中,形成了

SQL Server中,isnull()函数以及null的用法

SQL Serve中的isnull()函数:          isnull(value1,value2)         1、value1与value2的数据类型必须一致。         2、如果value1的值不为null,结果返回value1。         3、如果value1为null,结果返回vaule2的值。vaule2是你设定的值。        如