#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/irq.h>
#include <asm/irq.h>
#include <linux/interrupt.h> //中断有关的头文件,request_irq()函数定义在此
#include <asm/uaccess.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <linux/platform_device.h>
#include <linux/cdev.h>
#include <linux/miscdevice.h>
#define DEVICE_NAME "buttons"
struct button_irq_desc { //定义按键中断结构体
int irq;
int pin;
int pin_setting;
int number;
char *name;
};
#if !defined (CONFIG_QQ2440_BUTTONS)
static struct button_irq_desc button_irqs [] = {
{IRQ_EINT8 , S3C2410_GPG0 , S3C2410_GPG0_EINT8 , 0, "KEY0"},
{IRQ_EINT11, S3C2410_GPG3 , S3C2410_GPG3_EINT11 , 1, "KEY1"},
{IRQ_EINT13, S3C2410_GPG5 , S3C2410_GPG5_EINT13 , 2, "KEY2"},
{IRQ_EINT14, S3C2410_GPG6 , S3C2410_GPG6_EINT14 , 3, "KEY3"},
{IRQ_EINT15, S3C2410_GPG7 , S3C2410_GPG7_EINT15 , 4, "KEY4"},
{IRQ_EINT19, S3C2410_GPG11, S3C2410_GPG11_EINT19, 5, "KEY5"},
};
#else /* means QQ */
static struct button_irq_desc button_irqs [] = {
{IRQ_EINT19, S3C2410_GPG11, S3C2410_GPG11_EINT19, 0, "KEY0"},
{IRQ_EINT11, S3C2410_GPG3, S3C2410_GPG3_EINT11, 1, "KEY1"},
{IRQ_EINT2, S3C2410_GPF2, S3C2410_GPF2_EINT2, 2, "KEY2"},
{IRQ_EINT0, S3C2410_GPF0, S3C2410_GPF0_EINT0, 3, "KEY3"},
{ -1, -1, -1, 4, "KEY4"},
{ -1, -1, -1, 5, "KEY5"},
};
#endif
static volatile char key_values [] = {'0', '0', '0', '0', '0', '0'};
static DECLARE_WAIT_QUEUE_HEAD(button_waitq); //声明一个名为button_waitq的等待队列头
static volatile int ev_press = 0; /*中断事件标志,中断服务程序将它置1,tq2440_buttons_read将它清0 */
static irqreturn_t buttons_interrupt(int irq, void *dev_id) //按键中断服务程序
{
struct button_irq_desc *button_irqs = (struct button_irq_desc *)dev_id;
int down;
// udelay(0);
down = !s3c2410_gpio_getpin(button_irqs->pin); //得到该pin的状态0/1
if (down != (key_values[button_irqs->number] & 1)) { // Changed
key_values[button_irqs->number] = '0' + down;
ev_press = 1;
wake_up_interruptible(&button_waitq); //唤醒休眠的进程
}
return IRQ_RETVAL(IRQ_HANDLED);
}
/*应用程序执行open("/dev/buttons",...)系统调用时,s3c24xx_buttons_open函数将
*被调用,它用来注册6个按键的中断处理程序*/
static int s3c24xx_buttons_open(struct inode *inode, struct file *file)
{
int i;
int err = 0;
for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {
if (button_irqs[i].irq < 0) {
continue;
}
//注册中断服务程序 irq中断号 buttons_interrupt中断服务程序 IRQ_TYPE_EDGE_BOTH中断触发方式 name 中断名字
err = request_irq(button_irqs[i].irq, buttons_interrupt, IRQ_TYPE_EDGE_BOTH,button_irqs[i].name, (void *)&button_irqs[i]);
if (err)
break;
}
if (err) { //如果发生错误,free掉所有的中断服务程序
i--;
for (; i >= 0; i--) {
if (button_irqs[i].irq < 0) {
continue;
}
disable_irq(button_irqs[i].irq);
free_irq(button_irqs[i].irq, (void *)&button_irqs[i]);
}
return -EBUSY;
}
ev_press = 1;//标志中断发生了
return 0;
}
/*应用程序对设备文件/dev/buttons执行close(...)时,
*就会调用s3c24xx_buttons_close函数*/
static int s3c24xx_buttons_close(struct inode *inode, struct file *file)
{
int i;
for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) { //设备关闭实际上就是free掉中断服务程序
if (button_irqs[i].irq < 0) {
continue;
}
free_irq(button_irqs[i].irq, (void *)&button_irqs[i]);
}
return 0;
}
/*应用程序对设备文件/dev/buttons执行read(...)时,
*就会调用s3c24xx_buttons_read函数
*/
static int s3c24xx_buttons_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
unsigned long err;
if (!ev_press) {
if (filp->f_flags & O_NONBLOCK)
return -EAGAIN;
else