input子系统编写按键驱动
思路:1、分配一个input_dev结构 2、设置 3、注册 4、硬件操作
1.分配input_dev结构体
kp->input = input_allocate_device();
if (kp->input == NULL)
{
TCA_DB("xxxx_keys_probe: Failed to allocate input device\n");
return false;
}
2.设置按键类事件并注册
set_bit(EV_KEY, kp->input->evbit);
for(i = 0; i < MAX_KEY_NUM; i++)//有MAX_KEY_NUM个键,所以上报MAX_KEY_NUM个
set_bit(key_array0[i], kp->input->keybit);
kp->input->name = xxxx_keys_name;//input事件名称
kp->input->id.bustype = BUS_I2C;
kp->input->dev.parent = &client->dev;
input_set_drvdata(kp->input, kp);//保存data结构到input_dev里面
ret = input_register_device(kp->input);//注册
if (ret) {
dev_err(&kp->client->dev,"Probe: Unable to register %s input device\n", kp->input->name);
input_free_device(kp->input);
return false;
}
TCA_DB("Register input device successfully!\n");
在probe函数判断是否成功:
if (false == init_input_dev(client, kp)) {
kfree(kp);
return -1;
}
3.硬件处理
注册中断:
request_irq(kp->irq, xxxx_keys_irq_handler, xxxx_eint_type,"xxxx-eint", NULL)
初始化定时器:
init_timer(&buttons_timer);
buttons_timer.function = buttons_timer_function;//定时器到时操作函数
add_timer(&buttons_timer);
mod_timer(&buttons_timer, jiffies + (HZ /20));//定时器50ms操作
按键上报操作:
input_event(kp->input, EV_KEY, key_array0[i], 1);//按下上报
input_sync(kp->input);//上报结束
input_event(kp->input, EV_KEY, key_array0[i], 0);//抬起上报
input_sync(kp->input);
查看按键事件的打印信息:cat /dev/input/event0
查看input 事件 :cd /dev/input
查看平台驱动 : cd /sys/bus/platform/drivers
查看INPUT设备 : cat /proc/ bus/input/devices