/***************************************************************************************************
设备驱动中的中断处理--机制篇
编译环境:不需要
实 验 者:张永辉 2012年9月13日
***************************************************************************************************/
机制:
主要函数:
申请与释放IRQ API
int request_irq(unsigned int irq, //硬件中断号
//中断处理回调函数,dev_id 参数将被传递
void (*handler)(int irq, void *dev_id, struct pt_regs *regs),
unsigned long irqflags, //irqflags 是中断处理的属性,
const char * devname,
void *dev_id);
//irqflags若设置为 SA_INTERRUPT,标明中断处理程序是快速处理程序,快速处理程序被调用时屏蔽所有中断,慢速处理程序不屏蔽,
//irqflags若设置为 SA_SHIRQ,则多个设备共享中断,dev_id 在中断共享时会用到,一般设置为这个设备的 device 结构本身或者 NULL。
void free_irq(unsigned int irq,void *dev_id);
linux中断机制:
Linux 中断分为两个半部:
上半部(tophalf) 功能是“登记中断”,当一个中断发生时,它进行相应地硬件读写后就把中断例程挂到该设备的下半部执行队列中去。
它不可中断,速度就会很快,可以服务更多的中断请求。
下半部(bottom half) 来完成中断事件的绝大多数使命。
它做了中断处理程序所有的事情,而且可以被新的中断打断!
下半部的机制主要有 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()关联后,系统在适当的时候会运行: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
实例一:
它的功能是:在 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\n");
}
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 嵌入式系统实例,看其中实时钟的驱动中与中断相关的部分:
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 "\n");
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.\n", freq);
/* Now we have new data */
wake_up_interruptible(&rtc_wait);
kill_fasync(&rtc_async_queue, SIGIO, POLL_IN);
}
/*RTC 中断发生后,激发了一个异步信号,因此本驱动程序提供了对第 6 节异步信号的支持。
并不是每个中断都需要一个下半部,如果本身要处理的事情并不复杂,可能只有一个上半部,本例中的 RTC 驱动就是如此。*/