// 以下是利用poll做的按键驱动,逻辑上有些问题,不过可以使用
//驱动
//未加去抖动
//通过poll判断是否可读来得到中断值
#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 <asm/irq.h>
#include <linux/interrupt.h>
#include <asm/uaccess.h>
#include <linux/cdev.h>
#include <linux/irq.h>
#include <mach/board.h>
#include <mach/gpio.h>
#include <mach/irqs.h>
#include <mach/hardware.h>
#include <mach/at91_pio.h>
#define DEVICE_NAME "buttons_my"
#define BUTTON_MAJOR 0 // 定义0使用自动分配设备号
unsigned int button_major = BUTTON_MAJOR;
/*
struct button_irq_desc
{
int irq; //中断号
int pin; //中断控制的寄存器
int pin_setting; //中断的引脚
int number; //编号
char *name; //名称
};
static struct button_irq_desc button_irqs [] =
{
{IRQ_EINT11,S3C2410_GPG3, S3C2410_GPG3_EINT11, 1,"KEY1"},
{IRQ_EINT0, S3C2410_GPF0, S3C2410_GPF0_EINT0, 2,"KEY2"},
{IRQ_EINT19,S3C2410_GPG11,S3C2410_GPG11_EINT19,3,"KEY3"},
{IRQ_EINT2, S3C2410_GPF2, S3C2410_GPF2_EINT2, 4,"KEY4"},
{IRQ_EINT6, S3C2410_GPF6, S3C2410_GPF6_EINT6, 5,"KEY5"},
{IRQ_EINT5, S3C2410_GPF5, S3C2410_GPF5_EINT5, 6,"KEY6"},
};
*/
// 按键硬件资源、键值信息结构体
static struct key_info
{
int irq_no; // 中断号
int irq_type; // 中断类型
unsigned int gpio_port; // GPIO端口
int key_no; // 键值
}key_info_tab[4]=
// 按键所使用的CPU资源
{
{
AT91_PIN_PB0,AT91_AIC_SRCTYPE_FALLING,AT91_PIN_PB0,1
},
{
AT91_PIN_PB1,AT91_AIC_SRCTYPE_FALLING,AT91_PIN_PB1,2
},
{
AT91_PIN_PB2,AT91_AIC_SRCTYPE_FALLING,AT91_PIN_PB2,3
},
{
AT91_PIN_PB3,AT91_AIC_SRCTYPE_FALLING,AT91_PIN_PB3,4
},
};
static volatile int key_values [] = {0,0,0,0};/*key_values数组中是否有数据的标志,0表示无数据可读,1表示有数据可读*/
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static volatile int ev_press = 0; /*初始为0*/
// 初始化
static void at91sam9260_key_io_init(void)
{
at91_set_gpio_input(AT91_PIN_PB0, 1);
at91_set_deglitch(AT91_PIN_PB0, 1);
at91_set_gpio_input(AT91_PIN_PB1, 1);
at91_set_deglitch(AT91_PIN_PB1, 1);
at91_set_gpio_input(AT91_PIN_PB2, 1);
at91_set_deglitch(AT91_PIN_PB2, 1);
at91_set_gpio_input(AT91_PIN_PB3, 1);
at91_set_deglitch(AT91_PIN_PB3, 1);
}
// 中断处理
static irqreturn_t buttons_interrupt(int irq,void *dev_id)
{
// struct key_info *button_irqs = (struct key_info *)dev_id;
int key = (int)dev_id;
//没去抖动
// key_values[button_irqs->key_no - 1] = button_irqs->key_no; //zhh
key_values[key] = key + 1;
ev_press = 1; // 表示数组已经可读了
wake_up_interruptible(&button_waitq); //zhh
return IRQ_RETVAL(IRQ_HANDLED);
}
static int request_irqs(void)
{
struct key_info *k;
int i;
int ret;
for(i = 0; i < sizeof(key_info_tab)/sizeof(key_info_tab[1]); i++)
{
k = key_info_tab + i;
//设置4个IO口为中断下降沿触发方式
set_irq_type(key_info_tab[i].irq_no, key_info_tab[i].irq_type); // 设置中断类型
//申请中断(类型为快速中断,中断服务时屏蔽所有外部中断?)将按键序列号作为参数传入中断服务程序
// if(request_irq(k->irq_no,at91sam9260_enit_key,0,DEVICE_NAME,(void *)i))
ret = request_irq(k->irq_no,buttons_interrupt,0,DEVICE_NAME,(void *)i);
if(ret)
{
printk(KERN_NOTICE "buttons:err is %d\r\n",ret);
break;
}
}
if(ret)
{
i--;
for(;i >=0; i--)
{
disable_irq(key_info_tab[i].irq_no);
free_irq(key_info_tab[i].irq_no,(void *)&key_info_tab[i]);
}
return -EBUSY;
}
return 0;
}
//open 注册中断
static int buttons_open(struct inode *inode,struct file *file)
{
int i;
i = 0;
return 0;
}
//close 释放中断
static int buttons_close(struct inode *inode,struct file *file)
{
int i;
for(i = 0;i < sizeof(key_info_tab)/sizeof(key_info_tab[0]);i++)
{
disable_irq(key_info_tab[i].irq_no);
free_irq(key_info_tab[i].irq_no,(void *)&key_info_tab[i]);
}
return 0;
}
//read
static int buttons_read(struct file *filp,char __user *buff,size_t count,loff_t *offp)
{
unsigned long err;
if(!ev_press) /*标志位为0,即无数据时*/
{
if(filp->f_flags & O_NONBLOCK) //不能以非阻塞读取
{
return -EAGAIN;
}
else
{
wait_event_interruptible(button_waitq,ev_press);
}
}
err = copy_to_user(buff,(const void *)key_values,min(sizeof(key_values),count));
memset((void *)key_values,0,sizeof(key_values));
ev_press = 0;
if(err)
{
return -EFAULT;
}
else
{
return min(sizeof(key_values),count);
}
}
//buttons_poll
static unsigned int buttons_poll(struct file *file,struct poll_table_struct *wait)
{
unsigned int mask = 0;
poll_wait(file,&button_waitq,wait);
if(ev_press)
{
mask |= POLLIN | POLLRDNORM;
}
return mask;
}
static struct file_operations buttons_fops =
{
.owner = THIS_MODULE,
.open = buttons_open, /*open()*/
.release = buttons_close, /*release()*/
.read = buttons_read, /*read()*/
.poll = buttons_poll /*poll()*/
};
struct cdev button_cdev; // cdev结构体
/* 初始化并注册cdev */
static void buttons_setup_cdev(void)
{
int err,devno = MKDEV(button_major,0);
cdev_init(&button_cdev,&buttons_fops);
button_cdev.owner = THIS_MODULE;
button_cdev.ops = &buttons_fops;
err = cdev_add(&button_cdev, devno, 1);
if (err)
printk(KERN_NOTICE "Error %d adding utukey", err);
}
//init
static int __init buttons_init(void)
{
int result;
dev_t devno = MKDEV(button_major,0); // 用主次设备号生成设备号
at91sam9260_key_io_init();
/* 申请设备号 */
if (button_major)
result = register_chrdev_region(devno, 1, DEVICE_NAME);
else /* 动态申请设备号 */
{
result = alloc_chrdev_region(&devno, 0, 1, DEVICE_NAME);
button_major = MAJOR(devno);
printk(KERN_INFO "Todo: mknod /dev/%s c %d 0\n", DEVICE_NAME, button_major);
}
if (result < 0)
return result;
buttons_setup_cdev();
// 注册中断函数
if(-1 == request_irqs())
{
printk(KERN_NOTICE "request_irqs failed!\r\n");
}
else
{
printk(KERN_NOTICE "request_irqs success!\r\n");
}
printk(DEVICE_NAME " initialized\n");
return 0;
}
//buttons_exit()
static void __exit buttons_exit(void)
{
cdev_del(&button_cdev); // 注销cdev
unregister_chrdev_region(MKDEV(button_major, 0), 1); // 释放设备号
}
module_init(buttons_init);
module_exit(buttons_exit);
MODULE_AUTHOR("http://www.xxx.net");
MODULE_DESCRIPTION("S3C2410/S3C2440 BUTTON Driver");
MODULE_LICENSE("GPL"); |