|
贴个我写的wgbb 吧,,仅供参考:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
struct pin_desc {
char pin;
char val;
};
struct pin_desc pin[4] = {
{S3C2410_GPF0,0x01},
{S3C2410_GPF1,0x02},
{S3C2410_GPF2,0x03},
{S3C2410_GPF4,0x04},
};
static unsigned int val = 0;
static unsigned int event = 0;
static DECLARE_WAIT_QUEUE_HEAD(key_wait);
static irqreturn_t key_int(int irq, void *dev_id) //中断服务函数
{
unsigned int temp;
struct pin_desc *pinval = NULL;
pinval = (struct pin_desc *)dev_id;
temp = s3c2410_gpio_getpin(pinval->pin);
if(!temp)
{
val = pinval->val;
}
event = 1;
wake_up_interruptible(&key_wait);
return IRQ_RETVAL(IRQ_HANDLED);
}
static int key_open(struct inode *inode,struct file *file)
{
request_irq(IRQ_EINT0,key_int,IRQT_BOTHEDGE,"key0",&pin[0]); //注册中断
request_irq(IRQ_EINT1,key_int,IRQT_BOTHEDGE,"key1",&pin[1]);
request_irq(IRQ_EINT2,key_int,IRQT_BOTHEDGE,"key2",&pin[2]);
request_irq(IRQ_EINT4,key_int,IRQT_BOTHEDGE,"key3",&pin[3]);
return 0;
}
static ssize_t key_read(struct file *file,char __user *userbuf,size_t count,loff_t *off)
{
wait_event_interruptible(key_wait,event);
event = 0;
copy_to_user(userbuf,&val,sizeof(val));
return 0;
}
static int key_close(struct inode *inode,struct file *file) //释放中断
{
free_irq(IRQ_EINT0,&pin[0]);
free_irq(IRQ_EINT1,&pin[1]);
free_irq(IRQ_EINT2,&pin[2]);
free_irq(IRQ_EINT4,&pin[3]);
return 0;
}
static struct file_operations key_intterupt = {
.owner = THIS_MODULE,
.open = key_open,
.read = key_read,
.release = key_close,
};
static int key_init(void)
{
register_chrdev(251,"key_int",&key_intterupt);
return 0;
}
static void key_exit(void)
{
unregister_chrdev(251,"key_int");
}
module_init(key_init);
module_exit(key_exit);
MODULE_LICENSE("GPL"); |
|