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 中定义操作这个参数的宏如下:
引用/*
* These flags used only by the kernel as part of the
* irq handling routines.
*
* IRQF_DISABLED - keep irqs disabled when calling the action handler
* IRQF_SAMPLE_RANDOM - irq is used to feed the random generator
* IRQF_SHARED - allow sharing the irq among several devices
* IRQF_PROBE_SHARED - set by callers when they expect sharing mismatches to occur
* IRQF_TIMER - Flag to mark this interrupt as timer interrupt
* IRQF_PERCPU - Interrupt is per cpu
* IRQF_NOBALANCING - Flag to exclude this interrupt from irq balancing
* IRQF_IRQPOLL - Interrupt is used for polling (only the interrupt that is
* registered first in an shared interrupt is considered for
* performance reasons)
*/
#define IRQF_DISABLED 0x00000020
#define IRQF_SAMPLE_RANDOM 0x00000040
#define IRQF_SHARED 0x00000080
#define IRQF_PROBE_SHARED 0x00000100
#define IRQF_TIMER 0x00000200
#define IRQF_PERCPU 0x00000400
#define IRQF_NOBALANCING 0x00000800
#define IRQF_IRQPOLL 0x00001000
早期一点的 2.6 内核这里一般以 SA_ 前缀开头,如:
SA_INTERRUPT 表示禁止其他中断;(对应于 IRQF_DISABLED )
SA_SHIRQ 表示共享相同的中断号 (对应于 IRQF_SHARED )
SA_SAMPLE_RANDOM 此宏会影响到 RANDOM 的处理( 对应于 IRQF_SAMPLE_RANDOM )。
第 4 个参数 name 通常是设备驱动程序的名称。改值用在 /proc/interrupt 系统 (虚拟) 文件上,或内核发生中断错误时使用。
第 5 个参数 dev_id 可作为共享中断时的中断区别参数,也可以用来指定中断服务函数需要参考的数据地址。
返回值 :
函数运行正常时返回 0 ,否则返回对应错误的负值。
示例代码片段 :
引用irqreturn_t xxx_interrupt (int irq , void * dev_id )
{
...
return (IRQ_HANDLED );
}
int xxx_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_SMP
cpumask_t affinity;
unsigned int cpu;
#endif
#if defined(CONFIG_GENERIC_PENDING_IRQ) || defined(CONFIG_IRQBALANCE)
cpumask_t pending_mask;
#endif
#ifdef CONFIG_PROC_FS
struct proc_dir_entry *dir;
#endif
const char *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 * ) ,
unsigned long irqflags,
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标志位进行判 断。