Chinaunix首页 | 论坛 | 博客
  • 博客访问: 635991
  • 博文数量: 113
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 4176
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-15 20:22
个人简介

最大化我的市场价值

文章分类

全部博文(113)

文章存档

2013年(113)

分类: LINUX

2013-02-28 11:05:29

Linux中斷處理過程

与Linux设备驱动中中断处理相关的首先是申请与释放IRQ的API request_irq()和free_irq(),

request_irq()的原型为:

int request_irq(unsigned int irq,
void (*handler)(int irq, void *dev_id, struct pt_regs *regs),
unsigned long irqflags,
const char * devname,
void *dev_id);

irq是要申请的硬件中断号;

handler是向系统登记的中断处理函数,是一个回调函数,中断发生时,系统调用这个函数,dev_id参数将被传递;

irqflags是中断处理的属性,若设置SA_INTERRUPT,标明中断处理程序是快速处理程序,快速处理程序被调用时屏蔽所有中断,慢速处理程 序不屏蔽;若设置SA_SHIRQ,则多个设备共享中断,dev_id在中断共享时会用到,一般设置为这个设备的device结构本身或者NULL。

free_irq()的原型为:

void free_irq(unsigned int irq,void *dev_id);

另外,与Linux中断息息相关的一个重要概念是Linux中断分为两个半部:上半部(tophalf)和下半部(bottom half)。上半部的功能是"登记中断",当一个中断发生时,它进行相应地硬件读写后就把中断例程的下半部挂到该设备的下半部执行队列中去。因此,上半部 执行的速度就会很快,可以服务更多的中断请求。但是,仅有"登记中断"是远远不够的,因为中断的事件可能很复杂。因此,Linux引入了一个下半部,来完 成中断事件的绝大多数使命。下半部和上半部最大的不同是下半部是可中断的,而上半部是不可中断的,下半部几乎做了中断处理程序所有的事情,而且可以被新的 中断打断!下半部则相对来说并不是非常紧急的,通常还是比较耗时的,因此由系统自行安排运行时机,不在中断服务上下文中执行。

Linux实现下半部的机制主要有tasklet和工作队列。

tasklet基于Linux softirq,其使用相当简单,我们只需要定义tasklet及其处理函数并将二者关联:

void my_tasklet_func(unsigned long); //定义一个处理函数:
DECLARE_TASKLET(my_tasklet,my_tasklet_func,data); //定义一个tasklet结构my_tasklet,与my_tasklet_func(data)函数相关联

然后,在需要调度tasklet的时候引用一个简单的API就能使系统在适当的时候进行调度运行:

tasklet_schedule(&my_tasklet);

此外,Linux还提供了另外一些其它的控制tasklet调度与运行的API:

DECLARE_TASKLET_DISABLED(name,function,data); //与DECLARE_TASKLET类似,但等待tasklet被使能
tasklet_enable(struct tasklet_struct *); //使能tasklet 
tasklet_disble(struct tasklet_struct *); //禁用tasklet 
tasklet_init(struct tasklet_struct *,void (*func)(unsigned long),unsigned long); //类似DECLARE_TASKLET() 
tasklet_kill(struct tasklet_struct *); // 清除指定tasklet的可调度位,即不允许调度该tasklet

我们先来看一个tasklet的运行实例,这个实例没有任何实际意义,仅仅为了演示。它的功能是:在globalvar被写入一次后,就调度一个tasklet,函数中输出"tasklet is executing":

 

代码
复制代码
  1 #include <linux/interrupt.h>
  2 … 
  3 //定义与绑定tasklet函数
  4 void test_tasklet_action(unsigned long t);
  5 DECLARE_TASKLET(test_tasklet, test_tasklet_action, 0);
  6 
  7 void test_tasklet_action(unsigned long t)
  8 {
  9     printk("tasklet is executing\n");
 10 }
 11 
 12 
 13 
 14 ssize_t globalvar_write(struct file *filp, const char *buf, size_t len, loff_t *off)
 15 {
 16     …
 17     if (copy_from_user(&global_var, buf, sizeof(int)))
 18     {
 19         return - EFAULT;
 20     
 21 
 22     //调度tasklet执行
 23     tasklet_schedule(&test_tasklet);
 24     return sizeof(int);
 25 
 26 
 27 由于中断与真实的硬件息息相关,脱离硬件而空谈中断是毫无意义的,我们还是来举一个简单的例子。这个例子来源于SAMSUNG S3C2410嵌入式系统实例,看看其中实时钟的驱动中与中断相关的部分:
 28 
 29 static struct fasync_struct *rtc_async_queue;
 30 static int __init rtc_init(void)
 31 {
 32     misc_register(&rtc_dev);
 33     create_proc_read_entry("driver/rtc"00, rtc_read_proc, NULL);
 34 
 35     #if RTC_IRQ
 36     if (rtc_has_irq == 0)
 37         goto no_irq2;
 38 
 39     init_timer(&rtc_irq_timer);
 40     rtc_irq_timer.function = rtc_dropped_irq;
 41     spin_lock_irq(&rtc_lock);
 42     /* Initialize periodic freq. to CMOS reset default, which is 1024Hz */
 43     CMOS_WRITE(((CMOS_READ(RTC_FREQ_SELECT) &0xF0| 0x06), RTC_FREQ_SELECT);
 44     spin_unlock_irq(&rtc_lock);
 45     rtc_freq = 1024;
 46 no_irq2:
 47 #endif
 48 
 49     printk(KERN_INFO "Real Time Clock Driver v" RTC_VERSION "\n");
 50     return 0;
 51 }
 52 
 53 static void __exit rtc_exit(void)
 54 {
 55     remove_proc_entry("driver/rtc", NULL);
 56     misc_deregister(&rtc_dev);
 57 
 58     release_region(RTC_PORT(0), RTC_IO_EXTENT);
 59     if (rtc_has_irq)
 60         free_irq(RTC_IRQ, NULL);
 61 }
 62 static void rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
 63 {
 64 /*
 65 * Can be an alarm interrupt, update complete interrupt,
 66 * or a periodic interrupt. We store the status in the
 67 * low byte and the number of interrupts received since
 68 * the last read in the remainder of rtc_irq_data.
 69 */
 70 
 71 spin_lock(&rtc_lock);
 72 rtc_irq_data += 0x100;
 73 rtc_irq_data &= ~0xff;
 74 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) &0xF0);
 75 
 76 if (rtc_status &RTC_TIMER_ON)
 77 mod_timer(&rtc_irq_timer, jiffies + HZ / rtc_freq + 2 * HZ / 100);
 78 
 79 spin_unlock(&rtc_lock);
 80 
 81 /* Now do the rest of the actions */
 82 wake_up_interruptible(&rtc_wait);
 83 
 84 kill_fasync(&rtc_async_queue, SIGIO, POLL_IN);
 85 }
 86 
 87 static int rtc_fasync (int fd, struct file *filp, int on)
 88 {
 89 return fasync_helper (fd, filp, on, &rtc_async_queue);
 90 }
 91 
 92 static void rtc_dropped_irq(unsigned long data)
 93 {
 94 unsigned long freq;
 95 
 96 spin_lock_irq(&rtc_lock);
 97 
 98 /* Just in case someone disabled the timer from behind our back... */
 99 if (rtc_status &RTC_TIMER_ON)
100 mod_timer(&rtc_irq_timer, jiffies + HZ / rtc_freq + 2 * HZ / 100);
101 
102 rtc_irq_data += ((rtc_freq / HZ) << 8);
103 rtc_irq_data &= ~0xff;
104 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) &0xF0); /* restart */
105 
106 freq = rtc_freq;
107 
108 spin_unlock_irq(&rtc_lock);
109 printk(KERN_WARNING "rtc: lost some interrupts at %ldHz.\n", freq);
110 
111 /* Now we have new data */
112 wake_up_interruptible(&rtc_wait);
113 
114 kill_fasync(&rtc_async_queue, SIGIO, POLL_IN);
115 
116 
117 RTC中断发生后,激发了一个异步信号,因此本驱动程序提供了对第6节异步信号的支持。并不是每个中断都需要一个下半部,如果本身要处理的事情并不复杂,可能只有一个上半部,本例中的RTC驱动就是如此
118 
复制代码

 

tasklet使用模板


//定义tasklet和底半部函数并关联

void xxx_do_tasklet(unsigned long);

DECLARE_TASKLET(xxx_tasklet, xxx_do_tasklet, 0);

 

//中断底半部处理

void xxx_do_tasklet(unsigned long)

{

        ……………..

}

 

//中断处理顶半部

irqreturn_t xxx_interrupt(int irq, void * dev_id, struct pt_regs * regs)

{

         …….

         tasklet_schedule(&xxx_tasklet);

         ……..

}

 

//设备驱动模块加载函数

int __init xxx_init(void)

{

        …………..

       //申请中断

         result = request_irq(xxx_irq, xxx_interrupt, SA_INTERRUPT, “xxx”, NULL);

        …………..

}

 

//设备驱动模块卸载函数

void __exit xxx_exit(void)

{

         …………….

         //释放中断

         free_irq(xxx_irq, xxx_interrupt)

         …………….

}


工作队列使用模板


//定义工作队列和关联函数

struct work_struct xxx_wq;

void xxx_do_work(unsigned long);

 

//中断处理底半部

void xxx_do_work(unsigned long)

{

      ………………

}

 

//中断处理顶半部

irqreturn_t xxx_interrupt( int irq, void * dev_id, struct pt_regs * regs)

{

      …………..

       schedule_work(&xxx_wq);

       ………….

}

 

//设备驱动模块加载函数

int xxx_int(void)

{

        …………

       result = request_irq(xxx_irq, xxx_interrupt, SA_INTERRUPT, “xxx”, NULL);

//初始化工作队列

        INIT_WORK(&xxx_wq, (void (*) (void *)) xxx_do_work, NULL);

      …………….

}

 

//设备驱动模块卸载函数

void xxx_exit(void)

{

       ……………

        free_irq(xxx_irq, xxx_interrupt);

       ………….

}


共享中断编程模板


//中断处理顶半部

irqreturn_t xxx_interrupt(int irq, void * dev_id, struct pt_regs * regs)

{

      ………………

      int status = read_int_status();//获知中断源

      if(!(is_myint(dev_id, status))//判断时候是本设备中断

      {

            return IRQ_NONE;

       }

      ………….

      return IRQ_HANDLED;

}

 

//设备驱动模块加载函数

int xxx_init(void)

{

     ……………

      //申请共享中断

       result = request_irq( sh_irq, xxx_interrupt, SA_SHIRQ, “xxx”, xxx_dev);

      ………………

}

 

//卸载函数

void xxx_exit(void)

{

       ………………

       free_irq(xxx_irq, xxx_interrupt);

       ……………

}


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