转载--request_irq() | 注册…
来源:互联网 发布:java获取12时间戳 编辑:程序博客网 时间:2024/05/12 04:40
request_irq() | 注册中断服务
#include <linux/interrupt.h>。函数原型如下:
- 2.4 内核
- 2.6 内核
参数说明:
在发生对应于第 1个参数 irq 的中断时,则调用第 2 个参数handler 指定的中断服务函数(也就是把handler() 中断服务函数注册到内核中 )。
第 3 个参数 flags指定了快速中断或中断共享等中断处理属性。在 2.6 教新的内核里(我的是 2.6.27 ~ 2.6.31 ),在linux/interrupt.h 中定义操作这个参数的宏如下:
引用
#defineIRQF_DISABLED0x00000020
#defineIRQF_SAMPLE_RANDOM0x00000040
#defineIRQF_SHARED0x00000080
#defineIRQF_PROBE_SHARED0x00000100
#defineIRQF_TIMER0x00000200
#defineIRQF_PERCPU0x00000400
#defineIRQF_NOBALANCING0x00000800
#defineIRQF_IRQPOLL0x00001000
早期一点的 2.6 内核这里一般以 SA_ 前缀开头,如:
SA_INTERRUPT
SA_SHIRQ
SA_SAMPLE_RANDOM
第 4 个参数 name通常是设备驱动程序的名称。改值用在 /proc/interrupt 系统 (虚拟) 文件上,或内核发生中断错误时使用。
第 5 个参数 dev_id可作为共享中断时的中断区别参数,也可以用来指定中断服务函数需要参考的数据地址。
返回值:
函数运行正常时返回 0 ,否则返回对应错误的负值。
示例代码片段:
引用irqreturn_txxx_interrupt(intirq, void *dev_id)
{
...
return (IRQ_HANDLED);
}
intxxx_open(struct inode *inode, struct file *filp)
{
if(!request_irq (XXX_IRQ, xxx_interruppt, IRQF_DISABLED, "xxx", NULL)) {
}
return (0);
}
============================================================================
内核中的中断处理模型
内核版本: Linux 2.6.19
Kernel中断处理模型结构图如下:
下面简单介绍一下:
1.Linux定义了名字为irq_desc的中断例程描述符表:(include/linux/irq.h)
struct irqdesc irq_desc[NR_IRQS]; NR_IRQS表示中断源的数目。
2.irq_desc[]是一个指向irq_desc结构的数组, irq_desc结构是各个设备中断服务例程的描述符。
struct irq_desc {
irq_flow_handler_t handle_irq; struct irq_chip *chip; void *handler_data; void *chip_data; struct irqaction *action; unsigned int status;
unsigned int depth; unsigned int wake_depth; unsigned int irq_count; unsigned int irqs_unhandled; spinlock_t lock;
#ifdef CONFIG_SMPcpumask_t affinity; unsigned int cpu;
#endif
#ifdefined(CONFIG_GENERIC_PENDING_IRQ) ||defined(CONFIG_IRQBALANCE)cpumask_t pending_mask;
#endif
#ifdef CONFIG_PROC_FSstruct proc_dir_entry *dir;
#endifconstchar *name;
} ____cacheline_aligned;
Irq_desc结构体中的成员action指向该中断号对应的irqaction结构体链表。Irqaction结构体定义如下:
//include/linux/interrupt.h
struct irqaction{irq_handler_t handler; // 指向中断服务程序 unsigned long flags; // 中断标志 unsigned long mask; // 中断掩码 const char *name; //I/O设备名
void *dev_id; //设备标识 struct irqaction *next; //指向下一个描述符
int irq; // IRQ线 struct proc_dir_entry *dir; //指向IRQn相关的/proc/irq/n目录的描述符
};
其中关键的handler成员指向了该设备的中断服务程序,由执行request_irq时建立。
3.
在驱动程序初始化时,若使用到中断,通常调用函数request_irq()建立该驱动程序对应的irqaction结构体,并把它登记到irq_desc[irq_num]->action链表中。Iqr_num为驱动程序申请的中断号。 request_irq()函数的原型如下:
//kernel/irq/manage.c
int request_irq(unsigned int irq,irqreturn_t (*handler)(int, void *, struct pt_regs *), unsignedlongirqflags, const char *devname, void *dev_id);
参数irq是设备中断求号,在向irq_desc[]数组登记时,它做为数组的下标。把中断号为irq的irqaction结构体的首地址写入irq_desc[irq]->action。这样就把设备的中断请求号与该设备的中断服务例程irqaction联系在一起了。
这样当CPU接收到中断请求后,就可以根据中断号通过irq_desc[]找到该设备的中断服务程序。流程如上图所示。
4. 关于共享中断
共享中断的不同设备的iqraction结构体都会添加进该中断号对应的irq_desc结构体的action成员所指向的irqaction链表内。当内核发生中断时,它会依次调用该链表内所有的handler函数。因此,若驱动程序需要使用共享中断机制,其中断处理函数必须有能力识别是否是自己的硬件产生了中断。通常是通过读取该硬件设备提供的中断flag标志位进行判断。
- 转载--request_irq() | 注册…
- 转载--request_irq() | 注册…
- Why ASMLIB and why not? <转载…
- 当 今 中 国 的 深 层 悲 剧(转载…
- 【转载】pthread_mutex_init …
- 【转载】linux C …
- 【转载】 linux-python …
- 【转载】Sublime Text 2…
- 【转载】 linux Python&…
- 【转载】python -- 15个…
- 【转载】cannot be cast…
- (转载)IplImage, CvMat,&nbsp…
- 转载: java teln…
- em的错误 <转载>
- 新公司注册流程 2、注册之…
- 转载:Xlib: No protocol sp…
- 【转载】Quartus II 外部编辑器 No…
- (转载) 关于nor flash,nand flash…
- s3c2410时钟信号:FCLK、HCL…
- 自旋锁与信号量(转载)
- 自旋锁与信号量(转载)
- 主函数main中变量(int argc…
- 主函数main中变量(int argc…
- 转载--request_irq() | 注册…
- 转载--request_irq() | 注册…
- 深入分析request_irq的dev_i…
- 深入分析request_irq的dev_i…
- wait_event_interruptible()
- wait_event_interruptible()
- linux中断底层硬件操作方法…
- linux中断底层硬件操作方法…
- 系统处理 IRQ_EINT0 IRQ_EIN…