MINI2440_BUTTON.C
#include
#include
#include
#include
#include
#include
#include
#include interrupt.h>
#include
#include
#include
#define DEVICE_NAME "buttons"
#define BUTTON_MAJOR 232
struct button_irq_desc {
int irq;
int pin;
int pin_setting;
int number;
char *name;
};
static struct button_irq_desc button_irqs [] = {
{IRQ_EINT8, S3C2410_GPG0, S3C2410_GPG0_EINT8, 0, "KEY1"},
{IRQ_EINT11, S3C2410_GPG3, S3C2410_GPG3_EINT11, 1, "KEY2"},
{IRQ_EINT13, S3C2410_GPG5, S3C2410_GPG5_EINT13, 2, "KEY3"},
{IRQ_EINT14, S3C2410_GPG6, S3C2410_GPG6_EINT14, 3, "KEY4"},
{IRQ_EINT15, S3C2410_GPG7, S3C2410_GPG7_EINT15, 4, "KEY5"},
{IRQ_EINT19, S3C2410_GPG11, S3C2410_GPG11_EINT19, 5, "KEY6"},
};
static volatile int key_values [] = {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 up = s3c2410_gpio_getpin(button_irqs->pin);
if (up)
key_values[button_irqs->number] = (button_irqs->number + 1) + 0x80;
else
key_values[button_irqs->number] = (button_irqs->number + 1);
ev_press = 1;
wake_up_interruptible(&button_waitq);
return IRQ_RETVAL(IRQ_HANDLED);
}
static int mini2440_buttons_open(struct inode *inode, struct file *file)
{
int i;
int err;
for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {
// 注册中断处理函数
s3c2410_gpio_cfgpin(button_irqs[i].pin,button_irqs[i].pin_setting);
err = request_irq(button_irqs[i].irq, buttons_interrupt, NULL,
button_irqs[i].name, (void *)&button_irqs[i]);
set_irq_type(button_irqs[i].irq, IRQT_BOTHEDGE);
if (err)
break;
}
if (err) {
// 释放已经注册的中断
i--;
for (; i >= 0; i--) {
disable_irq(button_irqs[i].irq);
free_irq(button_irqs[i].irq, (void *)&button_irqs[i]);
}
return -EBUSY;
}
return 0;
}
static int mini2440_buttons_close(struct inode *inode, struct file *file)
{
int i;
for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {
// 释放已经注册的中断
disable_irq(button_irqs[i].irq);
free_irq(button_irqs[i].irq, (void *)&button_irqs[i]);
}
return 0;
}
static int mini2440_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(buff, (const void *)key_values, min(sizeof(key_values), count));
memset((void *)key_values, 0, sizeof(key_values));
return err ? -EFAULT : min(sizeof(key_values), count);
}
static unsigned int mini2440_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 mini2440_buttons_fops = {
.owner = THIS_MODULE,
.open = mini2440_buttons_open,
.release = mini2440_buttons_close,
.read = mini2440_buttons_read,
.poll = mini2440_buttons_poll,
};
static int __init mini2440_buttons_init(void)
{
int ret;
ret = register_chrdev(BUTTON_MAJOR, DEVICE_NAME, &mini2440_buttons_fops);
if (ret < 0) {
printk(DEVICE_NAME " cant register major number\n");
return ret;
}
devfs_mk_cdev(MKDEV(BUTTON_MAJOR, 0), S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP, DEVICE_NAME);
printk(DEVICE_NAME " initialized\n");
return 0;
}
static void __exit mini2440_buttons_exit(void)
{
devfs_remove(DEVICE_NAME);
unregister_chrdev(BUTTON_MAJOR, DEVICE_NAME);
}
module_init(mini2440_buttons_init);
module_exit(mini2440_buttons_exit);
MODULE_AUTHOR("http://www.arm9.net"); // 驱动程序的作者
MODULE_DESCRIPTION("S3C2410/S3C2440 BUTTON