以下是一個統計中斷時間間隔的中斷服務程序。
irqreturn_t short_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
static long mytime=0;
static int i=0;
struct net_device *dev=(struct net_device *)dev_id;
if(i==0){
mytime=jiffies;
}else
if(i<20){
mytime =jiffies- mytime;
printk("Request on IRQ %d time %d/n",irq , mytime);
mytime=jiffies;
printk("Interrupt on %s -----%d /n",dev->name,dev->irq);
}
i++;
return IRQ_HANDLED;
}
這個函數實現的只是對兩次發生中斷的時間間隔的統計,時間單位是毫秒
函數參數說明:int irq :在這裏很明顯傳遞過來的是中斷號
void *dev_id :這個傳遞來的是設備的id號,可以根據這個設備id號得到相應設備的數據結構,進而的到相應設備的信息和相關數據。下面以提取網路數據爲例來說明一下。
struct net_device *dev=( struct net_device *)dev_id; (這裏的dev_id的值是註冊中斷的時候宏傳遞過來的,是註冊中斷函數的最後一個參數。特別說明)
在這之後就可以用dev->name; dev->irq;等得到網絡設備的信息了,當然提取ip數據報還得進行一些其它的工作。
struct pt_regs *regs :它指向一個數據結構,此結構保存的是中斷之前處理器的寄存器和狀態。主要用在程序調試。
關於中斷處理函數的返回值:中斷程序的返回值是一個特殊類型—irqreturn_t。但是中斷程序的返回值卻只有兩個—IRQ_NONE和IRQ_HANDLED。
/* irqreturn.h */
#ifndef _LINUX_IRQRETURN_H
#define _LINUX_IRQRETURN_H
typedef int irqreturn_t;
/*
* For 2.4.x compatibility, 2.4.x can use
*
* typedef void irqreturn_t;
* #define IRQ_NONE
* #define IRQ_HANDLED
* #define IRQ_RETVAL(x)
*……此處我刪去了部分關緊要的內容
* To mix old-style and new-style irq handler returns.
*
* IRQ_NONE means we didn't handle it.
* 中斷程序接收到中斷信號後發現這並不是註冊時指定的中斷原發出的中斷信號.
*此時返回次值
* IRQ_HANDLED means that we did have a valid interrupt and handled it.
* 接收到了準確的中斷信號,並且作了相應正確的處理
* IRQ_RETVAL(x) selects on the two depending on x being non-zero (for handled)
*/
#define IRQ_NONE (0)
#define IRQ_HANDLED (1)
#define IRQ_RETVAL(x) ((x) != 0) //這個宏只是返回0或非0
#endif
以上是在linux/irqreturn.h中的內容,我加了一定的註釋.我想是可以說明問題的