request_irq()——注册中断服务

在 2.4 内核和 2.6内核中都使用request_irq()函数来注册中断服务函数。在 2.4 内核中,需要包含的头文件是 #include <linux/sched.h> ,2.6 内核中需要包含的头文件则是#include <linux/interrupt.h> 。函数原型如下:2.4 内核intrequest_irq(unsignedintirq,void(*handler)(int,void*,structpt_regs*),unsignedlongfrags,constchar*device,void*dev_id);2.6 内核request_irq(unsignedintirq,irq_handler_thandler,unsignedlongflags,constchar*name,void*dev);参数说明:在发生对应于第 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_txxx_interrupt(intirq,void*dev_id){…return(IRQ_HANDLED);}intxxx_open(structinode*inode,structfile*filp){if(!request_irq(XXX_IRQ,xxx_interruppt,IRQF_DISABLED,"xxx",NULL)){/*正常注册*/}return(0);}

============================================================================

内核中的中断处理模型

内核版本: Linux 2.6.19

Kernel中断处理模型结构图如下:

中断处理模型” alt=”内核中的中断处理模型” src=”http://cdn.verydemo.com/upload/2013_01_29/13594688390260.gif” >

下面简单介绍一下:

1. Linux定义了名字为irq_desc的中断例程描述符表:(include/linux/irq.h)

struct irqdesc irq_desc[NR_IRQS];

NR_IRQS表示中断源的数目。

2. irq_desc[]是一个指向irq_desc结构的数组, irq_desc结构是各个设备中断服务例程的描述符。

structirq_desc {irq_flow_handler_thandle_irq;structirq_chip*chip;void*handler_data;void*chip_data;structirqaction*action;unsigned intstatus;

unsigned intdepth;unsigned intwake_depth;unsigned intirq_count;unsigned intirqs_unhandled;spinlock_tlock;#ifdefCONFIG_SMPcpumask_taffinity;unsigned intcpu;#endif#ifdefined(CONFIG_GENERIC_PENDING_IRQ) || defined(CONFIG_IRQBALANCE)cpumask_tpending_mask;#endif#ifdefCONFIG_PROC_FSstructproc_dir_entry*dir;#endifconst char*name;} ____cacheline_aligned;

Irq_desc结构体中的成员action指向该中断号对应的irqaction结构体链表。Irqaction结构体定义如下:

// include/linux/interrupt.hstructirqaction{irq_handler_t handler;// 指向中断服务程序unsignedlongflags;// 中断标志unsignedlongmask;// 中断掩码constchar*name;// I/O设备名

void *dev_id;// 设备标识structirqaction*next;// 指向下一个描述符

intirq;// IRQ线structproc_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.cintrequest_irq(unsignedintirq,irqreturn_t(*handler)(int,void*,structpt_regs*),unsignedlongirqflags,constchar*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标志位进行判断。

在 2.4 内核和 2.6内核中都使用request_irq()函数来注册中断服务函数。在 2.4 内核中,需要包含的头文件是 #include <linux/sched.h> ,2.6 内核中需要包含的头文件则是#include <linux/interrupt.h> 。函数原型如下:2.4 内核intrequest_irq(unsignedintirq,void(*handler)(int,void*,structpt_regs*),unsignedlongfrags,constchar*device,void*dev_id);2.6 内核request_irq(unsignedintirq,irq_handler_thandler,unsignedlongflags,constchar*name,void*dev);参数说明:在发生对应于第 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_txxx_interrupt(intirq,void*dev_id){…return(IRQ_HANDLED);}intxxx_open(structinode*inode,structfile*filp){if(!request_irq(XXX_IRQ,xxx_interruppt,IRQF_DISABLED,"xxx",NULL)){/*正常注册*/}return(0);}

============================================================================

内核中的中断处理模型

内核版本: Linux 2.6.19

Kernel中断处理模型结构图如下:

中断处理模型” alt=”内核中的中断处理模型” src=”http://cdn.verydemo.com/upload/2013_01_29/13594688390260.gif” >

下面简单介绍一下:

1. Linux定义了名字为irq_desc的中断例程描述符表:(include/linux/irq.h)

struct irqdesc irq_desc[NR_IRQS];

NR_IRQS表示中断源的数目。

2. irq_desc[]是一个指向irq_desc结构的数组, irq_desc结构是各个设备中断服务例程的描述符。

structirq_desc {irq_flow_handler_thandle_irq;structirq_chip*chip;void*handler_data;void*chip_data;structirqaction*action;unsigned intstatus;

unsigned intdepth;unsigned intwake_depth;unsigned intirq_count;unsigned intirqs_unhandled;spinlock_tlock;#ifdefCONFIG_SMPcpumask_taffinity;unsigned intcpu;#endif#ifdefined(CONFIG_GENERIC_PENDING_IRQ) || defined(CONFIG_IRQBALANCE)cpumask_tpending_mask;#endif#ifdefCONFIG_PROC_FSstructproc_dir_entry*dir;#endifconst char*name;} ____cacheline_aligned;

Irq_desc结构体中的成员action指向该中断号对应的irqaction结构体链表。Irqaction结构体定义如下:

// include/linux/interrupt.hstructirqaction{irq_handler_t handler;// 指向中断服务程序unsignedlongflags;// 中断标志unsignedlongmask;// 中断掩码constchar*name;// I/O设备名

void *dev_id;// 设备标识structirqaction*next;// 指向下一个描述符

intirq;// IRQ线structproc_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.cintrequest_irq(unsignedintirq,irqreturn_t(*handler)(int,void*,structpt_regs*),unsignedlongirqflags,constchar*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()——注册中断服务

相关文章:

你感兴趣的文章:

标签云: