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定义的全局变量。

 


<think>首先,用户的问题是:“我想在Linux系统中编写中断函数时,查找确定中断号的方法”。意思是他们在编写中断函数时,想知道如何查找并确定中断号。 用户的指令是基于之前的系统级指令,包括使用$...$和$$...$$格式、中文回答、生成相关问题等。 用户提供了三个引用: - [^1]: 关于中断处理程序的描述。 - [^2]: 关于中断号的解释,包括基本概念。 - [^3]: 关于中断使能和禁用函数的原型。 用户还说:“上述引用仅供参考,并不是用户真正的需求,请尽可能多的参考用户上一次的问题和你的回答。” 但在这个对话中,这是第一个用户消息,所以没有“上一次的问题”。系统提示说“参考用户上一次的问题和你的回答”,但在这个上下文中,起始点是示例对话。所以,我应该基于当前问题回答。 用户的核心需求是:在Linux编写中断函数时,如何查找并确定中断号。 从引用[^2]中,我知道中断号是一个唯一的整数标识符,用于每个中断请求(IRQ)。 在Linux中,中断号通常由硬件决定。硬件设备会有一个特定的中断线(如IRQ line),操作系统需要知道这个中断号来注册中断处理程序。 常见的方法包括: 1. **设备树(Device Tree)**:在嵌入式系统中,中断号通过设备树描述文件定义。设备树指定了设备的中断属性。 2. **PCI设备**:对于PCI设备,中断号可以通过PCI配置空间读取。 3. **ACPI**:在一些系统中,使用ACPI表定义中断。 4. **手动指定**:在某些驱动中,中断号可以硬编码或通过模块参数传递,但不推荐,因为中断号可能在不同系统中变动。 在Linux驱动开发中,注册中断处理程序使用`request_irq`函数,该函数需要一个中断号作为参数。所以,关键是获取这个中断号。 来自引用[^3],有函数如`enable_irq`等,这些函数也需要中断号。 所以,回答应该包括: - 解释中断号的重要性。 - 描述如何获取中断号: - 对于平台设备(如嵌入式),使用设备树。 - 对于PCI设备,使用PCI接口获取。 - 其他方法。 结构化回答: 1. **介绍中断号和其在中断函数中的作用**:简要解释什么是中断号,为什么需要它。 2. **获取中断号的方法**: - 设备树方法:在设备树中定义,驱动中解析。 - PCI方法:使用`pci_enable_device`等函数获取IRQ。 - GPIO中断:使用GPIO中断号。 - 动态探测:在一些系统中,中断号可能动态分配。 3. **示例代码**:提供一个简单的代码片段,展示如何获取中断号和注册中断处理程序。 4. **注意事项**:中断号是系统依赖的,不要硬编码。 系统指令要求使用行内数学表达式用$...$,但这个问题是关于Linux中断的,可能没有数学公式。如果有数学相关的,比如计算中断延迟,但这里主要是代码和概念。所以,可能不需要LaTeX数学。但系统说“所有行内数学表达式必须使用$...$格式”,所以如果有数学内容就用。 生成相关问题:在回答结束时,添加几个相关问题。 回答用中文。 最后,添加引用标识在引用的段落末尾。用户提供了引用,所以我应该在相关内容中引用它们。 例如: - 引用[^1]描述中断处理程序,可以在介绍时引用。 - 引用[^2]解释中断号,可以在解释中断号时引用。 - 引用[^3]给出中断函数,可以在讨论函数时引用。 完整回答结构: - 开头简要概述。 - 详细方法解释。 - 代码示例。 - 结尾总结。 - 生成相关问题。 </think>### Linux编写中断函数时确定中断号的方法 在Linux系统中编写中断函数(即中断处理程序)时,中断号(Interrupt Number)是一个关键参数,用于唯一标识硬件设备的中断请求(IRQ)。中断号帮助内核区分不同设备的中断源,以便调用正确的处理函数。每个设备的中断号通常是硬件定义的,但开发者需要通过软件方式查找或获取该值。以下是确定中断号的常用方法,基于Linux驱动的标准实践。我将逐步解释方法、提供示例代码,并说明注意事项。方法主要包括硬件依赖的设备树(Device Tree)、PCI接口探测等[^2]。 #### 1. **理解中断号的重要性** - 中断号是一个整数标识符,用于关联硬件中断线和内核中断处理函数。在注册中断时(如使用`request_irq`函数),必须提供正确的中断号。 - 中断处理程序运行于中断上下文中,代码不可阻塞,因此正确获取中断号是避免系统崩溃或资源冲突的关键[^1]。 - 中断号由中断控制器(如GIC)管理,不同硬件平台(如ARM、x86)的获取方式可能不同[^2]。 #### 2. **确定中断号的主要方法** 根据设备类型和系统架构,以下是常见的查找和确定中断号的途径: - **设备树(Device Tree)方法(适用于嵌入式平台)**: - 在ARM、PowerPC等嵌入式系统中,中断号通常在设备树描述文件(`.dts`)中定义。设备树描述了硬件资源配置,包括中断信号线和中断号。 - 驱动代码中,使用Linux API解析设备树节点来获取中断号。 - 步骤: 1. 在设备树文件中定义设备节点及其中断属性,例如: ```dts my_device { compatible = "vendor,my-device"; interrupts = <0 42 4>; // 中断号42,基于中断控制器索引 }; ``` 2. 在驱动代码中,使用`platform_get_irq`函数提取中断号: ```c #include <linux/interrupt.h> #include <linux/platform_device.h> static irqreturn_t my_interrupt_handler(int irq, void *dev_id) { // 中断处理函数实现 return IRQ_HANDLED; } static int my_probe(struct platform_device *pdev) { int irq_number; // 从设备树节点获取中断号 irq_number = platform_get_irq(pdev, 0); // 第一个中断资源 if (irq_number < 0) { return irq_number; // 错误处理 } // 注册中断处理程序 int ret = request_irq(irq_number, my_interrupt_handler, IRQF_SHARED, "my_device", pdev); if (ret) { pr_err("Failed to request IRQ %d\n", irq_number); return ret; } return 0; } ``` - **优点**:动态、可移植性强,无需硬编码中断号。 - **缺点**:仅适用于设备树支持的平台(如嵌入式Linux)[^2]。 - **PCI设备方法(适用于x86或PCI设备)**: - 对于PCI设备(如网卡、显卡),中断号由PCI总线动态分配,可通过PCI配置空间读取。 - 驱动代码中,使用`pci_irq_vector`或`pci_alloc_irq_vectors`函数获取中断号。 - 步骤: 1. 在驱动初始化中,启用PCI设备并请求中断资源。 2. 获取中断号: ```c #include <linux/pci.h> #include <linux/interrupt.h> static irqreturn_t pci_interrupt_handler(int irq, void *dev_id) { // 中断处理函数 return IRQ_HANDLED; } static int pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) { int irq_number; int ret; // 启用PCI设备 ret = pci_enable_device(pdev); if (ret) return ret; // 获取中断号(MSI或传统中断) irq_number = pci_irq_vector(pdev, 0); // 第一个中断向量 if (irq_number < 0) { pci_disable_device(pdev); return irq_number; } // 注册中断处理程序 ret = request_irq(irq_number, pci_interrupt_handler, IRQF_SHARED, "pci_device", pdev); if (ret) { pci_disable_device(pdev); pr_err("Failed to request IRQ %d\n", irq_number); return ret; } return 0; } ``` - **优点**:自动处理硬件差异,适合桌面或服务器环境。 - **缺点**:如果中断号无效或冲突,可能导致注册失败[^3]。 - **其他方法**: - **GPIO中断**:对于GPIO设备,使用`gpiod_to_irq`函数从GPIO描述符获取中断号: ```c struct gpio_desc *gpio = gpiod_get(dev, "interrupt", GPIOD_IN); int irq_number = gpiod_to_irq(gpio); ``` - **手动指定(不推荐)**:在简单调试中,可以通过内核模块参数传递中断号,但易出错: ```c static int irq = 10; // 硬编码中断号 module_param(irq, int, S_IRUGO); // 然后在模块初始化中使用request_irq ``` - **系统文件接口**:运行时可通过`/proc/interrupts`文件查看当前中断分配: ```bash cat /proc/interrupts # 列出所有中断号和对应设备 ``` #### 3. **注意事项和最佳实践** - **中断号冲突**:确保中断号不与其他驱动共享,除非使用`IRQF_SHARED`标志。共享中断需在handle函数中区分设备。 - **中断上下文限制**:中断处理程序不可调用可能阻塞的函数(如`kmalloc`),运行速度快以避免系统延迟[^1]。 - **动态性**:中断号可能因系统重启或硬件变动而改变,因此不要硬编码。优先使用设备树或PCI探测方法。 - **错误处理**:获取中断号后,务必检查返回值(负数表示错误),并使用`enable_irq`或`disable_irq`管理中断使能状态[^3]。 - **调试工具**:利用`dmesg`检查内核日志,或使用`devm_request_irq`自动释放资源减少泄漏风险。 #### 4. **示例总结** 以下是一个简化的驱动代码片段,展示在嵌入式设备中通过设备树获取中断号并注册处理函数的完整流程: ```c #include <linux/module.h> #include <linux/interrupt.h> #include <linux/platform_device.h> static irqreturn_t my_irq_handler(int irq, void *dev_id) { printk(KERN_INFO "Interrupt %d handled\n", irq); return IRQ_HANDLED; } static int my_driver_probe(struct platform_device *pdev) { int irq = platform_get_irq(pdev, 0); // 从设备树获取中断号 if (irq < 0) { dev_err(&pdev->dev, "IRQ not found: %d\n", irq); return irq; } int ret = devm_request_irq(&pdev->dev, irq, my_irq_handler, IRQF_TRIGGER_RISING, "my_device", NULL); if (ret) { dev_err(&pdev->dev, "Failed to request IRQ %d: %d\n", irq, ret); return ret; } enable_irq(irq); // 使能中断 return 0; } static struct platform_driver my_driver = { .probe = my_driver_probe, .driver = { .name = "my_interrupt_driver", }, }; module_platform_driver(my_driver); MODULE_LICENSE("GPL"); ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值