Driver"); // 一些描述信息
MODULE_LICENSE("GPL"); // 遵循的协议
总结一下,程序框架。
按键的驱动主要是多了中断的处理,关于中断主要的处理函数是
request_irq(button_irqs[i].irq, buttons_interrupt, NULL,
button_irqs[i].name, (void *)&button_irqs[i]);
第二个参数是主要的中断处理函数。
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 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"},
};
struct button_irq_desc {
int irq;
int pin;
int pin_setting;
int number;
char *name;
};
而驱动程序中的最重要的部分,file_operations为
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,
};
程序中其他的任务就是丰满这个机构体,使得应用程序在调用open,close等的时候可以在
驱动中找到对应的项。
这里的request_irq在open函数里调用的。
至于注册的部分跟LEDS那些是一样的,就不多说啦。
再附个应用程序供参考:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(void)
{
int i;
int buttons_fd;
int key_value[4];
buttons_fd = open("/dev/buttons", 0);
if (buttons_fd < 0) {
perror("open device buttons");
exit(1);
}
for (;;) {
fd_set rds;
int ret;
FD_ZERO(&rds);
FD_SET(buttons_fd, &rds);
ret = select(buttons_fd + 1, &rds, NULL, NULL, NULL);
if (ret < 0) {
perror("select");
exit(1);
}
if (ret == 0) {
printf("Timeout.\n");
}
else if (FD_ISSET(buttons_fd, &rds)) {
int ret = read(buttons_fd, key_value, sizeof key_value);
if (ret != sizeof key_value) {
if (errno != EAGAIN)
perror("read buttons\n");
continue;
} else {
for (i = 0;i < sizeof(key_value)/sizeof(key_value[0]); i++)
printf("K%d %s, key value = 0x%02x\n", i+1, (key_value[i] & 0x80) ? "released" : \
key_value[i] ? "pressed down" : "", \
key_value[i]);
}
}
}
close(buttons_fd);
return 0;
}