#include #include#include#include#include#include#include#include#include#include#include#include
static struct timer_list buttons_timer; //定义定时器结构体
struct pin_desc *irq_dev_id ; //定义全局变量获取dev_id
static struct class *sixthdrv_class;static struct class_device *sixthdrv_class_devs;/*定义互斥锁button_lock,被用来后面的down()和up()使用*/
staticDECLARE_MUTEX(button_lock);/*声明等待队列类型中断 button_wait*/
staticDECLARE_WAIT_QUEUE_HEAD(button_wait);/*异步信号结构体变量*/
static struct fasync_struct *button_async;/** 定义中断事件标志
* 0:进入等待队列 1:退出等待队列*/
static int even_press=0;/** 定义全局变量key_val,保存key状态*/
static int key_val=0;/**引脚描述结构体*/
structpin_desc{
unsignedintpin;
unsignedintpin_status;
};/**key初始状态(没有按下): 0x81,0x82,0x83,0x84
*key状态(按下): 0x01,0x02,0x03,0x04*/
struct pin_desc pins_desc[4]={
{S3C2410_GPF0,0x01},
{S3C2410_GPF2,0x02},
{S3C2410_GPG3,0x03},
{S3C2410_GPG11,0x04},
} ;int sixth_drv_class(struct inode *inode, struct file *file) //卸载中断{
free_irq(IRQ_EINT0,&pins_desc[0]);
free_irq(IRQ_EINT2,&pins_desc[1]);
free_irq(IRQ_EINT11,&pins_desc[2]);
free_irq(IRQ_EINT19,&pins_desc[3]);/*释放信号量*/up(&button_lock);return 0;
}/** 确定是上升沿还是下降沿*/
static irqreturn_t buttons_irq (int irq, void *dev_id) //中断服务函数{
irq_dev_id=(struct pin_desc *)dev_id; //获取引脚描述结构体
/*每产生一次中断,则更新定时器10ms超时*/mod_timer(&buttons_timer, jiffies+HZ/100);returnIRQ_RETVAL(IRQ_HANDLED);
}static int sixth_drv_open(struct inode *inode, struct file *file)
{if( file->f_flags & O_NONBLOCK ) //非阻塞操作,获取不到则退出{if(down_trylock(&button_lock) )return -1;
}else //阻塞操作,获取不到则进入休眠{
down(&button_lock);
}
request_irq(IRQ_EINT0,buttons_irq,IRQT_BOTHEDGE,"S1",&pins_desc[0]);
request_irq(IRQ_EINT2, buttons_irq,IRQT_BOTHEDGE,"S2", &pins_desc[1]);
request_irq(IRQ_EINT11, buttons_irq,IRQT_BOTHEDGE,"S3", &pins_desc[2]);
request_irq(IRQ_EINT19, buttons_irq,IRQT_BOTHEDGE,"S4", &pins_desc[3]);return 0;
}static int sixth_drv_read(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
{if( file->f_flags & O_NONBLOCK ) //非阻塞操作,获取不到则退出{if(!even_press ) //没有按键按下
return -1;
}/*阻塞操作,则直接进入休眠状态,直到有按键按下为止*/
/*进程 进入等待队列(休眠状态)*/wait_event_interruptible(button_wait, even_press);/*有按键按下,退出等待队列,上传key_val 给用户层*/
if(copy_to_user(buf,&key_val,sizeof(key_val)))returnEFAULT;
even_press=0;return 0;
}static unsigned sixth_poll(struct file *file, poll_table *wait)
{
unsignedint mask = 0;
poll_wait(file,&button_wait, wait); //不会立即休眠
if(even_press)
mask|= POLLIN |POLLRDNORM;returnmask;
}static int sixth_fasync (int fd, struct file *file, inton)
{return fasync_helper(fd, file, on, & button_async); //初始化button_async结构体,就能使用kill_fasync()了}static struct file_operations sixth_drv_fops={
.owner=THIS_MODULE,
.open=sixth_drv_open,
.read=sixth_drv_read,
.release=sixth_drv_class, //里面添加free_irq函数,来释放中断服务函数.poll=sixth_poll,
.fasync= sixth_fasync, //初始化异步信号函数};static void buttons_timer_function(unsigned long data) //定时器超时函数{
unsignedint pin_val=0;if(!irq_dev_id) //定时器.expires成员=0,会进入一次,若irq_dev_id为0则退出
{printk("expires: timer out\n");return; }
pin_val=s3c2410_gpio_getpin(irq_dev_id->pin);if(pin_val)
{/*按下 (下降沿),清除0x80*/key_val=irq_dev_id->pin_status&0xef;
}else{/*没有按下(上升沿),加上0x80*/key_val=irq_dev_id->pin_status|0x80;
}
even_press=1; //退出等待队列wake_up_interruptible(&button_wait); //唤醒 中断kill_fasync(&button_async, SIGIO, POLL_IN); //发送SIGIO信号给应用层}volatile intsixth_major;static int sixth_drv_init(void)
{
init_timer(&buttons_timer); //初始化定时器buttons_timer. function= buttons_timer_function; //定时器超时函数add_timer(&buttons_timer); //添加到内核中
sixth_major=register_chrdev(0,"sixth_drv",&sixth_drv_fops); //创建驱动sixthdrv_class=class_create(THIS_MODULE,"sixth_dev"); //创建类名sixthdrv_class_devs=class_device_create(sixthdrv_class, NULL, MKDEV(sixth_major,0), NULL,"buttons");return 0;
}static int sixth_drv_exit(void)
{
unregister_chrdev(sixth_major,"sixth_drv"); //卸载驱动class_device_unregister(sixthdrv_class_devs);//卸载类设class_destroy(sixthdrv_class);//卸载类
return 0;
}
module_init(sixth_drv_init);
module_exit(sixth_drv_exit);
MODULE_LICENSE("GPL v2");
}