/{
key@26{
compatible = "xj4412,key";
interrupt-parent = <&gpx1>;
interrupts = <2 2>;
};
};
static struct input_dev *button_dev;
static int button_irq;
static int irqflags;
static irqreturn_t button_interrupt(int irq, void *dummy)
{
input_report_key(button_dev, BTN_0,
0);
input_report_key(button_dev, BTN_0,
1);
input_sync(button_dev);
return IRQ_HANDLED;
}
static int button_init(void)
{
request_irq(button_irq, button_interrupt,irqflags,
"button",
NULL)) ;
button_dev = input_allocate_device();
button_dev->name =
"button";
button_dev->evbit[
0] = BIT_MASK(EV_KEY);
button_dev->keybit[BIT_WORD(BTN_0)] = BIT_MASK(BTN_0);
input_register_device(button_dev);
return 0;
}
static int button_exit(void)
{
input_free_device(button_dev);
free_irq(button_irq, button_interrupt);
return 0;
}
static int key_probe(struct platform_device *pdev)
{
struct resource *irq_res;
irq_res = platform_get_resource(pdev, IORESOURCE_IRQ,
0);
if(irq_res){
button_irq = irq_res->start;
irqflags = irq_res->flags & IRQF_TRIGGER_MASK;
}
else{
return -EINVAL;
}
return button_init();
}
static int key_remove(struct platform_device *dev)
{
return button_exit();
}
struct of_device_id of_tbl[] = {
{.compatible =
"xj4412,key",},
{},
};
MODULE_DEVICE_TABLE(of, of_tbl);
struct platform_driver key_drv = {
.probe = key_probe,
.remove = key_remove,
.driver.name =
"keydrv",
.driver.of_match_table = of_tbl,
};
module_platform_driver_register(key_drv);
MODULE_LICENSE(
"GPL");
应用层获取键值
#include <linux/input.h>
struct input_event {
struct timeval time;
unsigned
short type;
unsigned
short code;
int value;
};
int main(int argc, char * const argv[])
{
int fd =
0;
struct input_event
event[
3] = {
0};
//3!!!,驱动上传了2个事件,第三个用来装空元素
int ret =
0;
fd = open(argv[
1],O_RDONLY);
while(
1){
ret = read(fd,&
event,
sizeof(
event));
printf(
"ret:%d,val0:%d,val1:%d,val12:%d\n",ret,
event[
0].
value,
event[
1].
value,
event[
2].
value);
//2!!!,最后一个是空
sleep(
1);
}
return 0;
}
本文永久更新链接地址: