【Linux input子系统二】驱动程序的编写
本文主要以分析通用GPIO按键驱动:gpio_keys.C文件为主,将其中牵涉到Linux驱动架构流程,以及架构设计思想进行分析。
【概述】gpio_keys是基于input架构实现的一个通用gpio按键驱动。该驱动基于pladform_driver架构,实现了驱动和设备分离,减少了程序的耦合性,同时提高程序的可移植性。符合Linux设备驱动模型的设计思想。工程中一般都会基于gpio_keys来写。下面我们来理清程序的整体流程思路。
【驱动程序的分析】
step1:根据Linux驱动模型,驱动文件中该有的flow给他加上
step2:当驱动driver模块加载成功,根据compatible属性(name)与device 匹配match成功之后,进入驱动的主入口函数probe
具体程序如下:
- static int __devinit gpio_keys_probe(structplatform_device *pdev)
- {
- structgpio_keys_platform_data *pdata = pdev->dev.platform_data;
- structgpio_keys_drvdata *ddata;
- structinput_dev *input;
- inti, error;
- intwakeup = 0;
- /*kzalloc 对kmalloc的封装,会清0分配的空间*/
- ddata= kzalloc(sizeof(struct gpio_keys_drvdata) +
- pdata->nbuttons* sizeof(struct gpio_button_data),
- GFP_KERNEL);
- /*分配一个input设备*/
- input= input_allocate_device();
- if(!ddata || !input) {
- error= -ENOMEM;
- gotofail1;
- }
- platform_set_drvdata(pdev,ddata);
- /* 设置input设备属性 */
- input->name= pdev->name;
- input->phys= "gpio-keys/input0";
- input->dev.parent= &pdev->dev;
- input->id.bustype= BUS_HOST;
- input->id.vendor= 0x0001;
- input->id.product= 0x0001;
- input->id.version= 0x0100;
- /*Enable auto repeat feature of Linux input subsystem */
- if(pdata->rep)
- __set_bit(EV_REP,input->evbit);
- ddata->input= input;
- for(i = 0; i < pdata->nbuttons; i++) {
- structgpio_keys_button *button = &pdata->buttons[i];
- structgpio_button_data *bdata = &ddata->data[i];
- intirq;
- unsignedint type = button->type ?: EV_KEY;
- bdata->input= input;
- bdata->button= button;
- setup_timer(&bdata->timer,
- gpio_keys_timer, (unsigned long)bdata);
- /*初始化工作队列 */
- INIT_WORK(&bdata->work,gpio_keys_report_event);
- /*申请GPIO口*/
- error = gpio_request(button->gpio, button->desc ?:"gpio_keys");
- if(error < 0) {
- pr_err("gpio-keys:failed to request GPIO %d,"
- "error %d\n", button->gpio, error);
- gotofail2;
- }
- /* 把GPIO设为输入 */
- error= gpio_direction_input(button->gpio);
- if(error < 0) {
- pr_err("gpio-keys:failed to configure input"
- "direction for GPIO %d, error %d\n",
- button->gpio,error);
- gpio_free(button->gpio);
- gotofail2;
- }
- /*获取GPIO对应的中断*/
- irq= gpio_to_irq(button->gpio);
- if(irq < 0) {
- error= irq;
- pr_err("gpio-keys:Unable to get irq number"
- "for GPIO %d, error %d\n",
- button->gpio,error);
- gpio_free(button->gpio);
- gotofail2;
- }
- /*注册中断 */
- error= request_irq(irq, gpio_keys_isr,
- IRQF_SHARED |
- IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
- button->desc ? button->desc :"gpio_keys",
- bdata);
- if(error) {
- pr_err("gpio-keys:Unable to claim irq %d; error %d\n",
- irq,error);
- gpio_free(button->gpio);
- gotofail2;
- }
- if(button->wakeup)
- wakeup= 1; 后续讨论
- /*设置设备对事件的支持,比如设置对键1和键2的支持*/
- input_set_capability(input,type, button->code);
- }
- /*注册input设备*/
- error= input_register_device(input);
- if(error) {
- pr_err("gpio-keys:Unable to register input device, "
- "error:%d\n", error);
- gotofail2;
- }
- device_init_wakeup(&pdev->dev,wakeup);
- return0;
- ……
- returnerror;
- }
本文参考博文链接:
1、https://blog.****.net/RadianceBlau/article/details/56290469
2、https://www.linuxidc.com/Linux/2011-11/47650p3.htm
【最终实现】还需要设置具体的platform device的实现:高通和MTK平台是在DTSI里面配置的
结构体中标绿色的项是读取DTS文件属性配置,其他的,如value,active_low等在代码中设置。反正都是需要设置完成才能使用。
struct gpio_keys_button {
unsigned int code;int gpio;
int active_low;低电平有效1
const char *desc;
unsigned int type;EV_KEY
int wakeup;
int debounce_interval;
bool can_disable;
int value;0按下 1松开
unsigned int irq;中断号
struct gpio_desc *gpiod;description
};
如下是参考:
- static struct gpio_keys_button sc_buttons[]= {
- {
- .gpio = S3C2410_GPG(0), /* K1 */
- .code = KEY_1,
- .desc = "KEY1",
- .active_low = 1,
- },