led驱动程序是向寄存器写入数据来控制灯的亮和来,而按键驱动则是从寄存器中读出数据,查看某个键是否按下。这是一个不相同的地方。
下面是键盘驱动:
#define DEVICE_NAME "buttons"
struct button_irq_desc {
int irq;
int number;
char *name;
};
static struct button_irq_desc button_irqs [] = {
{IRQ_EINT( 0), 0, "KEY0"},
{IRQ_EINT( 1), 1, "KEY1"},
{IRQ_EINT( 2), 2, "KEY2"},
{IRQ_EINT( 3), 3, "KEY3"},
{IRQ_EINT( 4), 4, "KEY4"},
{IRQ_EINT( 5), 5, "KEY5"},
{IRQ_EINT(19), 6, "KEY6"},
{IRQ_EINT(20), 7, "KEY7"},
};
static volatile char key_values [] = {'0', '0', '0', '0', '0', '0', '0', '0'};
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static volatile int ev_press = 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;
int number;
unsigned tmp;
udelay(0);
number = button_irqs->number;
switch(number) {
case 0: case 1: case 2: case 3: case 4: case 5:
tmp = readl(S3C64XX_GPNDAT);
down = !(tmp & (1<<number));
break;
case 6: case 7:
tmp = readl(S3C64XX_GPLDAT);
down = !(tmp & (1 << (number + 5)));
break;
default:
down = 0;
}
if (down != (key_values[number] & 1)) {
key_values[number] = '0' + down;
ev_press = 1;
wake_up_interruptible(&button_waitq);
}
return IRQ_RETVAL(IRQ_HANDLED);
}
static int s3c64xx_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;
}
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) {
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;
}
static int s3c64xx_buttons_close(struct inode *inode, struct file *file)
{
int i;
for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {
if (button_irqs[i].irq < 0) {
continue;
}
free_irq(button_irqs[i].irq, (void *)&button_irqs[i]);
}
return 0;
}
static int s3c64xx_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
wait_event_interruptible(button_waitq, ev_press);
}
ev_press = 0;
err = copy_to_user((void *)buff, (const void *)(&key_values), min(sizeof(key_values), count));
return err ? -EFAULT : min(sizeof(key_values), count);
}
static unsigned int s3c64xx_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 dev_fops = {
.owner = THIS_MODULE,
.open = s3c64xx_buttons_open,
.release = s3c64xx_buttons_close,
.read = s3c64xx_buttons_read,
.poll = s3c64xx_buttons_poll,
};
static struct miscdevice misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &dev_fops,
};
static int __init dev_init(void)
{
int ret;
ret = misc_register(&misc);
printk (DEVICE_NAME"\tinitialized\n");
return ret;
}
static void __exit dev_exit(void)
{
misc_deregister(&misc);
}
module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FriendlyARM Inc.");
1.中断处理:
request_irq(button_irqs[i].irq, buttons_interrupt, IRQ_TYPE_EDGE_BOTH, button_irqs[i].name, (void *)&button_irqs[i]);
第一个参数irq,要处理的中断号
第二个参数为中断发生时的回调函数
回调函数的三个参数分别为中断号irq,申请是告诉系统的设备标识dev_id,中断发生时寄存器内容regs
第三个参数flag,申请是的选项快速处理程序SA_INTERRUPT,共享此中断SA_SHIRQ
第四个参数区分处理程序dev_id
2.poll and poll_wait
1、对可能引起设备文件状态变化的等待队列调用poll_wait()函数,将对应的等待队列头添加到poll_table;
2、返回表示是否对设备进行无阻塞读、写访问的掩码。 提供给内核信息用来使调用进程睡眠直到 I/O 变为可能
3.测试程序
在测试程序中我们把设备结点打开后,不断去读取状态就可以了。