static struct pin_desc *irq_pd;
/* 鍵值: 按下時, 0x01, 0x02, 0x03, 0x04 */
/* 鍵值: 鬆開時, 0x81, 0x82, 0x83, 0x84 */
static unsigned char key_val;
struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
//static unsigned char keyval;
/* 中斷事件標誌, 中斷服務程式將它置1,third_drv_read將它清0 */
static volatile int ev_press = 0;
/* 鍵值: 按下時, 0x01, 0x02, 0x03, 0x04 */
/* 鍵值: 鬆開時, 0x81, 0x82, 0x83, 0x84 */
struct pin_desc pins_desc[4] = {
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPF3, 0x03},
{S3C2410_GPF4, 0x04},
};
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
irq_pd = (struct pin_desc *)dev_id;
mod_timer(&buttons_timer, jiffies+HZ/100);
return IRQ_HANDLED;
}

static int fifth_drv_open(struct inode *inode, struct file *file)
{
  #if 0
if (!atomic_dec_and_test(&canopen))
{
  atomic_inc(&canopen);
  return -EBUSY;
}
#endif
if (file->f_flags & O_NONBLOCK)//非阻塞
{
if (down_trylock(&button_lock))
return -EBUSY;
}
else//阻塞
{
/* 獲取訊號量 */
down(&button_lock);
}

request_irq(IRQ_EINT0,buttons_irq, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "S2", &pins_desc[0]);
request_irq(IRQ_EINT2,buttons_irq, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "S3", &pins_desc[1]);
request_irq(IRQ_EINT3,buttons_irq, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "S4", &pins_desc[2]);
request_irq(IRQ_EINT4,buttons_irq, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "S5", &pins_desc[3]);
return 0;
}

ssize_t fifth_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
if(size!=1)
return EINVAL;
/* 如果有按鍵動作, 返回鍵值 */
if (file->f_flags & O_NONBLOCK)//非阻塞
{
if (!ev_press)
return -EAGAIN;
}
else//阻塞
{
/* 如果沒有按鍵動作, 休眠 */
wait_event_interruptible(button_waitq, ev_press);

}
copy_to_user(buf, &key_val, 1);
ev_press=0;
return 0;
}

int fifth_drv_close(struct inode *inode, struct file *file)
{
//atomic_inc(&canopen);
up(&button_lock);
free_irq(IRQ_EINT0, &pins_desc[0]);
free_irq(IRQ_EINT2, &pins_desc[1]);
free_irq(IRQ_EINT3, &pins_desc[2]);
free_irq(IRQ_EINT4, &pins_desc[3]);
return 0;
}

static int fifth_drv_fasync (int fd, struct file *filp, int on)
{
printk("driver: fifth_drv_fasync\n");
return fasync_helper (fd, filp, on, &button_async);
}

static struct file_operations fifth_drv_fops = {
.owner =THIS_MODULE,/* 這是一個巨集,推向編譯模組時自動建立的__this_module變數 */
.open=fifth_drv_open,
.read=fifth_drv_read,    
.release = fifth_drv_close,
.fasync  = fifth_drv_fasync,
};

static void buttons_timer_function(unsigned long data)  /* 定時器呼叫函式 */
{
struct pin_desc * pindesc = irq_pd;
unsigned int pinval;

if (!pindesc)
return;
pinval = s3c2410_gpio_getpin(pindesc->pin);
if (pinval)
{
/* 鬆開 */
key_val = 0x80 | pindesc->key_val;
}
else
{
/* 按下 */
key_val = pindesc->key_val;
}

ev_press = 1;  /* 表示中斷髮生了 */
wake_up_interruptible(&button_waitq);/* 喚醒休眠的程序 */

//kill_fasync (&button_async, SIGIO, POLL_IN);
}


int major;
static int fifth_drv_init(void)
{
 init_timer(&buttons_timer);
buttons_timer.function = buttons_timer_function;   /* 定時器的一些初始化操作 */
//buttons_timer.expires  = 0;
add_timer(&buttons_timer);

major = register_chrdev(0, DEVICE_NAME, &fifth_drv_fops);
timer_class= class_create(THIS_MODULE, DEVICE_NAME);
device_create(timer_class, NULL, MKDEV(major, 0), NULL, "timer");
return 0;
}

static void fifth_drv_exit(void)
{
unregister_chrdev(major, "signal");
device_destroy(timer_class,MKDEV(major, 0));
class_destroy(timer_class);
}

module_init(fifth_drv_init);
module_exit(fifth_drv_exit);
MODULE_LICENSE("GPL");