深入浅出 Linux设备驱动中断处理介绍

分类: 设备驱动   出处:iocblog整理  更新时间:2008-05-24   添加到收藏  

与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":

 

 

#include <linux/interrupt.h> … //定义与绑定tasklet函数 void test_tasklet_action(unsigned long t); declare_tasklet(test_tasklet, test_tasklet_action, 0); void test_tasklet_action(unsigned long t) {  printk("tasklet is executing "); } … ssize_t globalvar_write(struct file *filp, const char *buf, size_t len, loff_t *off) {  …  if (copy_from_user(&global_var, buf, sizeof(int)))  {   return - efault;  }  //调度tasklet执行 tasklet_schedule(&test_tasklet);  return sizeof(int); }

由于中断与真实的硬件息息相关,脱离硬件而空谈中断是毫无意义的,我们还是来举一个简单的例子。这个例子来源于samsung s3c2410嵌入式系统实例,看看其中实时钟的驱动中与中断相关的部分:

 

 (www.iocblog.net 文章来源)

static struct fasync_struct *rtc_async_queue;
static int __init rtc_init(void)
{
 misc_register(&rtc_dev);
 create_proc_read_entry("driver/rtc", 0, 0, rtc_read_proc, null);
 #if rtc_irq
  if (rtc_has_irq == 0)
   goto no_irq2;
  init_timer(&rtc_irq_timer);
  rtc_irq_timer.function = rtc_dropped_irq;
  spin_lock_irq(&rtc_lock);
  /* initialize periodic freq. to cmos reset default, which is 1024hz */
  cmos_write(((cmos_read(rtc_freq_select) &0xf0) | 0x06), rtc_freq_select);
  spin_unlock_irq(&rtc_lock);
  rtc_freq = 1024;
  no_irq2:
 #endif
 printk(kern_info "real time clock driver v" rtc_version "
");
 return 0;
}
static void __exit rtc_exit(void)
{
 remove_proc_entry("driver/rtc", null);
 misc_deregister(&rtc_dev);
 release_region(rtc_port(0), rtc_io_extent);
 if (rtc_has_irq)
  free_irq(rtc_irq, null);
}
static void rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
 /*
 * can be an alarm interrupt, update complete interrupt,
 * or a periodic interrupt. we store the status in the
 * low byte and the number of interrupts received since
 * the last read in the remainder of rtc_irq_data.
 */
 spin_lock(&rtc_lock);
 rtc_irq_data += 0x100;
 rtc_irq_data &= ~0xff;
 rtc_irq_data |= (cmos_read(rtc_intr_flags) &0xf0);
 if (rtc_status &rtc_timer_on)
  mod_timer(&rtc_irq_timer, jiffies + hz / rtc_freq + 2 * hz / 100);
 spin_unlock(&rtc_lock);
 /* now do the rest of the actions */
 wake_up_interruptible(&rtc_wait);
 kill_fasync(&rtc_async_queue, sigio, poll_in);
}
static int rtc_fasync (int fd, struct file *filp, int on)
{
 return fasync_helper (fd, filp, on, &rtc_async_queue);
}
static void rtc_dropped_irq(unsigned long data)
{
 unsigned long freq;
 spin_lock_irq(&rtc_lock);
 /* just in case someone disabled the timer from behind our back... */
 if (rtc_status &rtc_timer_on)
  mod_timer(&rtc_irq_timer, jiffies + hz / rtc_freq + 2 * hz / 100);
 rtc_irq_data += ((rtc_freq / hz) << 8);
 rtc_irq_data &= ~0xff;
 rtc_irq_data |= (cmos_read(rtc_intr_flags) &0xf0); /* restart */
 freq = rtc_freq;
 spin_unlock_irq(&rtc_lock);
 printk(kern_warning "rtc: lost some interrupts at %ldhz.
", freq);
 /* now we have new data */
 wake_up_interruptible(&rtc_wait);
 kill_fasync(&rtc_async_queue, sigio, poll_in);
                        }

 

rtc中断发生后,激发了一个异步信号,因此本驱动程序提供了对第6节异步信号的支持。并不是每个中断都需要一个下半部,如果本身要处理的事情并不复杂,可能只有一个上半部,本例中的rtc驱动就是如此。


Tag: 中断处理