Chinaunix首页 | 论坛 | 博客
  • 博客访问: 498712
  • 博文数量: 187
  • 博客积分: 3011
  • 博客等级: 中校
  • 技术积分: 2092
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-28 17:08
文章分类

全部博文(187)

文章存档

2011年(1)

2010年(8)

2009年(178)

我的朋友

分类: LINUX

2009-07-29 22:15:14

在此,我们将编写一个模块,其中有一个中断函数,当内核接收到某个 IRQ 上的一个中断时会调用它。
 
/*======================================================================
    A simple kernel module: "hello world"
        
    The initial developer of the original code is Baohua Song
    <>. All Rights Reserved.
======================================================================*/
#include
#include
#include
MODULE_LICENSE("Dual BSD/GPL");
static int irq;
static char *interface;
module_param(interface,charp,0644);
module_param(irq,int,0644);
static irqreturn_t myinterrupt(int irq,void *dev_id,struct pt_regs *regs)
{
   static int mycount = 0;
   if(mycount<10){
     printk("Interrupt!\n");
     mycount++;
   }
   return IRQ_NONE;
}
 
static int myinterrupt_init(void)
{
  printk(KERN_EMERG "Driver World enter\n");
  if(request_irq(irq,&myinterrupt,IRQF_SHARED,interface,&irq)){
  printk(KERN_ERR "myirqtest: cannot register IRQ %d\n",irq);
  return -EIO;
  }
  printk("%s Request on IRQ %d succeeded\n",interface,irq);
  return 0;
}
static void myinterrupt_exit(void)
{
  printk(KERN_EMERG "Unloading Driver World\n");
  free_irq(irq,&irq);
  printk("Freeing IQR %d\n",irq);
}
module_init(myinterrupt_init);
module_exit(myinterrupt_exit);
MODULE_AUTHOR("Song Baohua");
MODULE_DESCRIPTION("A simple Hello World Module");
MODULE_ALIAS("a simplest module");
这里要说明的是,在插入模块时,可以带两个参数,例如
insmod myirq.ko interface=eth0 irq=9

其中 具体网卡 irq的值可以查看 cat /proc/interrupts
 
 

irqflags是中断处理的属性,若设置了IRQF_DISABLED (老版本中的SA_INTERRUPT,本版zhon已经不支持了),则表示中断处理程序是快速处理程序,快速处理程序被调用时屏蔽所有中断,慢速处理程序不屏蔽;若设置了IRQF_SHARED (老版本中的SA_SHIRQ),则表示多个设备共享中断,若设置了IRQF_SAMPLE_RANDOM(老版本中的SA_SAMPLE_RANDOM),表示对系统熵有贡献,对系统获取随机数有好处。(这几个flag是可以通过或的方式同时使用的)

 
 
阅读(904) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~