找回密码
 立即注册
首页 业界区 安全 Linux驱动---按键

Linux驱动---按键

嶝扁 2025-6-11 13:27:24
目录

  • 一、Input子系统

    • 1.1、简介
    • 1.2、Input子系统构成
    • 1.3、input_dev结构体

  • 二、输入设备驱动开发流程

    • 2.1、分配和初始化输入设备
    • 2.2、注册设备
    • 2.3、事件上报
    • 2.4、释放和注销设备

  • 三、事件同步与事件队列
  • 四、按键消抖

    • 4.1、按键抖动
    • 4.2、延时去抖动
    • 4.3、轮询去抖动

  • 五、实现按键驱动

    • 5.1、硬件原理图
    • 5.2、设备树修改
    • 5.3、编写按键驱动代码
    • 5.4、按键驱动测试


一、Input子系统

1.1、简介

在前面的LED驱动文章中,我们知道Linux为了方便GPIO操作设计了GPIO子系统。那么对于键盘、鼠标、触摸屏、游戏控制器这一类的输入设备呢?为了给这一类的输入设备提供统一的接口和管理,Linux设计了Input子系统,设计该系统的主要目的就是将输入设备驱动中的共性部分提取出来,形成一个通用的框架,开发者只需关注差异化的部分。这样,不仅降低了驱动开发的难度,也提高了驱动的通用性和兼容性。

Input子系统为各种输入设备提供了统一的接口,将输入事件转化为统一的事件格式,并通过input接口传递给应用空间程序,应用程序可以通过这些统一的接口来访问和操作输入设备,而不需要关心设备的具体实现。
1.png

1.2、Input子系统构成

输入子系统主要由三部分构成:

(1)设备驱动层(struct input_dev):通过获取设备树中硬件的信息,对硬件各寄存器的读写访问和将底层硬件的状态变化转换为标准的输入事件,将相应事件上报。

(2)核心层:用于将设备驱动层和事件处理层进行匹配,由内核完成。

(3)事件处理层(struct input_handler):将核心层生成的输入事件传递给系统的高层应用,并确保这些事件被正确处理。

我们本篇文章属于驱动开发,所以主要整理设备驱动层。
1.3、input_dev结构体

input_dev 是 Linux Input 子系统中用于描述输入设备的核心结构体,它的定义如下:
点击查看代码
  1. struct input_dev {
  2.         const char *name;  // 设备名称,例如 "Keyboard" 或 "Mouse"
  3.         const char *phys;  // 设备在系统中的物理路径,例如 "usb-0000:00:14.0-1/input0"
  4.         const char *uniq;  // 设备的唯一标识符,通常用于匹配特定硬件
  5.         struct input_id id; // 包含设备识别信息的结构体(例如供应商ID、产品ID、版本号)
  6.         // 属性位图,用于表示设备支持的属性类型
  7.         unsigned long propbit[BITS_TO_LONGS(INPUT_PROP_CNT)];
  8.         // 事件位图,用于表示设备支持的事件类型
  9.         unsigned long evbit[BITS_TO_LONGS(EV_CNT)];
  10.         // 键位图,用于表示设备支持的按键类型
  11.         unsigned long keybit[BITS_TO_LONGS(KEY_CNT)];
  12.         // 相对位图,用于表示设备支持的相对轴事件。 例如鼠标
  13.         unsigned long relbit[BITS_TO_LONGS(REL_CNT)];
  14.         // 绝对位图,用于表示设备支持的绝对轴事件,例如触摸屏
  15.         unsigned long absbit[BITS_TO_LONGS(ABS_CNT)];
  16.         // 杂项位图,用于表示设备支持的其他事件类型
  17.         unsigned long mscbit[BITS_TO_LONGS(MSC_CNT)];
  18.         // 指示灯位图,用于表示设备支持的LED灯类型
  19.         unsigned long ledbit[BITS_TO_LONGS(LED_CNT)];
  20.         // 声音位图,用于表示设备支持的声音类型
  21.         unsigned long sndbit[BITS_TO_LONGS(SND_CNT)];
  22.         // 力反馈位图,用于表示设备支持的力反馈事件
  23.         unsigned long ffbit[BITS_TO_LONGS(FF_CNT)];
  24.         // 开关位图,用于表示设备支持的开关类型
  25.         unsigned long swbit[BITS_TO_LONGS(SW_CNT)];
  26.         unsigned int hint_events_per_packet; // 每个数据包中的建议事件数量
  27.         unsigned int keycodemax;   // 最大按键码数量
  28.         unsigned int keycodesize;  // 每个按键码的大小
  29.         void *keycode;             // 指向按键码数据的指针
  30.         // 设置按键码的函数指针
  31.         int (*setkeycode)(struct input_dev *dev,
  32.                           const struct input_keymap_entry *ke,
  33.                           unsigned int *old_keycode);
  34.         // 获取按键码的函数指针
  35.         int (*getkeycode)(struct input_dev *dev,
  36.                           struct input_keymap_entry *ke);
  37.         struct ff_device *ff;  // 力反馈设备的指针
  38.         unsigned int repeat_key;  // 重复按键
  39.         struct timer_list timer;  // 用于处理重复按键的定时器
  40.        
  41.         int rep[REP_CNT];  // 用于存储重复延迟和重复率
  42.         struct input_mt *mt;  // 多点触控相关信息的指针
  43.         struct input_absinfo *absinfo;  // 绝对轴相关信息的指针
  44.         // 当前设备状态的位图(按键、指示灯、声音、开关)
  45.         unsigned long key[BITS_TO_LONGS(KEY_CNT)];
  46.         unsigned long led[BITS_TO_LONGS(LED_CNT)];
  47.         unsigned long snd[BITS_TO_LONGS(SND_CNT)];
  48.         unsigned long sw[BITS_TO_LONGS(SW_CNT)];
  49.         // 打开设备的函数指针
  50.         int (*open)(struct input_dev *dev);
  51.         // 关闭设备的函数指针
  52.         void (*close)(struct input_dev *dev);
  53.         // 刷新设备的函数指针
  54.         int (*flush)(struct input_dev *dev, struct file *file);
  55.         // 处理事件的函数指针
  56.         int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value);
  57.         struct input_handle __rcu *grab;  // 用于处理独占设备的指针
  58.         spinlock_t event_lock;  // 用于保护事件处理的自旋锁
  59.         struct mutex mutex;     // 设备访问的互斥锁
  60.         unsigned int users;  // 使用该设备的用户数量
  61.         bool going_away;     // 标志设备是否正在关闭
  62.         struct device dev;  // 设备的基础信息
  63.         struct list_head h_list;  // 处理句柄的链表
  64.         struct list_head node;    // 设备的链表节点
  65.         unsigned int num_vals;  // 当前输入值的数量
  66.         unsigned int max_vals;  // 最大输入值的数量
  67.         struct input_value *vals;  // 输入值数组的指针
  68.         bool devres_managed;  // 标志设备资源是否由设备资源管理器管理
  69. };
复制代码
其中,evbit为事件类型,常见的有以下几种:
  1. EV_KEY    键盘按键事件
  2. EV_REL    鼠标事件
  3. EV_ABS    触摸屏事件
复制代码
二、输入设备驱动开发流程

2.1、分配和初始化输入设备

输入设备驱动首先需要分配一个input_de结构体,并设置它的基本属性,如设备名称、事件类型、支持的按键等。
  1. struct input_dev *input_device;
  2. input_device = input_allocate_device();
  3. if (!input_device) {
  4.     pr_err("Failed to allocate input device\n");
  5.     return -ENOMEM;
  6. }
  7. input_device->name = "my_key_device";
  8. input_device->evbit[0] = BIT_MASK(EV_KEY);  // 设置支持按键事件
复制代码

EV_KEY 是一个枚举值,定义在  中,通常值为 1。BIT_MASK(EV_KEY) 展开后相当于 1 evbit);[/code]2.2、注册设备

设置好设备的属性后,调用input_register_device()函数来注册输入设备,使其可以开始接收并处理事件。
  1. input_device->evbit[0] = 0x02;
复制代码
2.3、事件上报

输入设备需要在状态发生变化时,通过input_report_key()向Input子系统报告事件。
  1. __set_bit(EV_KEY, input_device->evbit);
复制代码
该函数将按键按下的事件报告给系统,用户空间应用程序可以通过evdev等接口读取到这些事件。在驱动中,我们往往需要监听GPIO引脚上按键的状态变化,这通常需要通过硬件中断(IRQ)来触发。按键状态的改变将会触发相应的中断处理函数,在中断处理函数中再通过input_report_key()来报告事件。
2.4、释放和注销设备

在驱动退出时,需释放资源,并通过input_unregister_device()注销输入设备。
  1. ret = input_register_device(input_device);
  2. if (ret) {
  3.     pr_err("Failed to register input device\n");
  4.     return ret;
  5. }
复制代码
三、事件同步与事件队列

在 2.3事件上报 时,我们调用了input_sync函数,为什么要进行该操作呢?这就要聊一下事件同步与事件队列了。

  • 事件同步:在报告完事件后,驱动需要调用input_sync来同步事件,确保事件被正确地传递到Input子系统中。
  • 事件队列:输入子系统通过事件队列的方式管理输入事件,驱动程序负责将事件传递到队列中,用户空间程序通过evdev等接口从队列中读取事件。
四、按键消抖

4.1、按键抖动

按键通常是由两个金属点组成,当按键按下或释放时,这些触点会发生接触或断开。由于物理原因,触点在短时间内可能会发生多次闭合和断开,而不是单次稳定地触发,这种现象称为“抖动”。

如果没有进行去抖动处理,一个按键的按下或释放被多次记录,硬件中断可能会频繁触发,增加系统的处理负担。

通常可以通过软件或硬件方法消除按键抖动。硬件去抖动通常是在按键硬件的设计中加以改进,例如使用RC滤波器、专用的去抖动IC或使用晶振稳定信号,这些方法能在硬件层面消除抖动,无需依赖软件。

接下来,将主要为大家介绍两种软件去抖动的方法。
4.2、延时去抖动

此方法的思路是等待按键接触或断开后的一段时间(例如10~50ms),在检测一次按键状态,已确定状态变化是否稳定。此方法实现简单,但是可能导致响应时间较长,不能非常精准的去抖动。
  1. input_report_key(input_device, KEY_ENTER, 1);  // 报告按下事件
  2. input_sync(input_device);  // 同步事件
复制代码
4.3、轮询去抖动

这种方法是对按键状态进行多次连续检查,只有在按键状态一致时才认为状态已稳定。通常在硬件中断中进行,读取按键状态并检查是否稳定。这种方法可以更可靠地过滤抖动,适合处理快速的按键状态变化。但是它增加了额外的处理复杂度,需要做更多的状态检测和计数。
  1. input_unregister_device(input_device);
  2. input_free_device(input_device);
复制代码
五、实现按键驱动

5.1、硬件原理图

下面是按键的原理图,从中我们可以看到该按键连接到了NAND_nCE1这个引脚上,通过设备树的头文件我们可以查到它使用MX6UL_PAD_NAND_CE1_B__GPIO4_IO14这个引脚。如果没有按下按键时,左侧的上拉电阻R25将该GPIO引脚拉成高电平;而一旦按键按下,则该引脚与GND导通变成低电平。由此可见,我们应该将GPIO4_14中断设置成下降沿触发。
2.png

5.2、设备树修改

接下来,我们需要修改DTS文件中关于按键的配置,因为BSP默认已经使能了该设备和Linux内核自带的按键驱动,这里只需将compatible修改成我们自己即将编写的按键驱动“my,keys”即可,别的都不需要修改。
  1. #define DEBOUNCE_DELAY_MS 20  // 延时 20ms
  2. static irqreturn_t gpio_key_irq(int irq, void *arg)
  3. {
  4.     struct keys_desc *key = arg;
  5.     static unsigned long last_irq_time = 0;
  6.     unsigned long now = jiffies;
  7.     // 检查抖动延迟
  8.     if (time_after(now, last_irq_time + msecs_to_jiffies(DEBOUNCE_DELAY_MS))) {
  9.         int value = gpio_get_value(key->gpio);
  10.         if (value == 0) {
  11.             input_report_key(input_device, key->key_code, 1);  // 按下事件
  12.         } else {
  13.             input_report_key(input_device, key->key_code, 0);  // 释放事件
  14.         }
  15.         input_sync(input_device);
  16.         last_irq_time = now;
  17.     }
  18.     return IRQ_HANDLED;
  19. }
复制代码
我们的按键使用的是GPIO4_14引脚,并且低电平有效GPIO_ACTIVE_LOW。我们设置该按键的键值linux.code为回车KEY_ENTER,按下该按键即相当于按下了回车。

修改完设备树之后,我们重新编译成.dtb文件,Makefile文件如下:
点击查看代码
  1. #define DEBOUNCE_COUNT 5  // 检查连续的 5 次状态
  2. static irqreturn_t gpio_key_irq(int irq, void *arg)
  3. {
  4.     struct keys_desc *key = arg;
  5.     static int stable_state = -1;
  6.     static int count = 0;
  7.     int value = gpio_get_value(key->gpio);
  8.     if (stable_state == value) {
  9.         count++;
  10.         if (count > DEBOUNCE_COUNT) {
  11.             // 状态稳定,报告事件
  12.             if (value == 0) {
  13.                 input_report_key(input_device, key->key_code, 1);  // 按下事件
  14.             } else {
  15.                 input_report_key(input_device, key->key_code, 0);  // 释放事件
  16.             }
  17.             input_sync(input_device);
  18.             count = 0;
  19.         }
  20.     } else {
  21.         stable_state = value;
  22.         count = 0;
  23.     }
  24.     return IRQ_HANDLED;
  25. }
复制代码
5.3、编写按键驱动代码

接下来我们通过Linux内核定时器实现按键消抖,编写代码如下:
  1.     keys {
  2.         compatible = "my,keys";
  3.         pinctrl-names = "default";
  4.         pinctrl-0 = <&pinctrl_gpio_keys>;
  5.         autorepeat;
  6.         status = "okay";
  7.         key_user {
  8.             lable = "key_user";
  9.             gpios = <&gpio4 14 GPIO_ACTIVE_LOW>;
  10.             linux,code = <KEY_ENTER>;
  11.         };
  12.     };
  13. ... ...
  14. &iomuxc {
  15.     pinctrl-names = "default";
  16. ... ...
  17.     pinctrl_gpio_keys: gpio-keys {
  18.         fsl,pins = <
  19.             MX6UL_PAD_NAND_CE1_B__GPIO4_IO14        0x17059 /* gpio key */
  20.         >;
  21.     };
  22. ... ...
  23. };
复制代码
在key_probe()中,我们为每个按键初始化了一个定时器。当GPIO引脚发生变化时,gpio_key_irq函数会被调用,触发定时器的启动。定时器的延时设为50毫秒,到达时间后,定时器回调函数debounce_timer_func()处理按键消抖并报告按键事件。在key_remove中,我们删除了所有按键的定时器,以确保在驱动移除时不会发生定时器回调。

接下来,进行编译,Makefile文件如下:
点击查看代码
  1. ARCH ?= arm
  2. KERNAL_DIR ?= ${HOME}/igkboard-imx6ull/bsp/kernel/linux-imx
  3. CPP_CFLAGS=-Wp,-MD,.x.pre.tmp -nostdinc -undef -D__DTS__ -x assembler-with-cpp
  4. CPP_CFLAGS+= -I ${KERNAL_DIR}/arch/${ARCH}/boot/dts -I ${KERNAL_DIR}/include/
  5. DTC=${KERNAL_DIR}/scripts/dtc/dtc
  6. DTC_FLAGS=-q -@ -I dts -O dtb
  7. DTS_NAME=igkboard-imx6ull
  8. all:
  9.         @cpp ${CPP_CFLAGS} ${DTS_NAME}.dts -o .${DTS_NAME}.dts.tmp
  10.         ${DTC} ${DTC_FLAGS} .${DTS_NAME}.dts.tmp -o ${DTS_NAME}.dtb
  11.         @rm -f .*.tmp
  12. decompile:
  13.         ${DTC} -q -I dtb -O dts ${DTS_NAME}.dtb -o decompile.dts
  14. clean:
  15.         rm -f *.dtb decompile.dts
复制代码
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/errno.h>
  4. #include <linux/init.h>
  5. #include <linux/gpio.h>
  6. #include <linux/input.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/of.h>
  10. #include <linux/of_gpio.h>
  11. #include <linux/of_device.h>
  12. #include <linux/jiffies.h>
  13. #include <linux/delay.h>
  14. struct keys_desc {
  15.     const char         *lable;     /* Key name */
  16.     unsigned int        key_code;  /* Key code */
  17.     int                 gpio;      /* GPIO number */
  18.     unsigned int        irq;       /* IRQ number */
  19.     struct timer_list   timer;     /* Timer for debounce */
  20.     int                 last_value;/* Last key value */
  21. };
  22. struct key_priv {
  23.     int                 nkeys; /* number of keys */
  24.     struct keys_desc   *keys;  /* keys array */
  25. };
  26. struct input_dev *input_device;
  27. struct key_priv *priv;
  28. /* Timer callback function for debounce */
  29. static void debounce_timer_func(struct timer_list *t)
  30. {
  31.     struct keys_desc *key = from_timer(key, t, timer);
  32.     int value = gpio_get_value(key->gpio);
  33.     if (value != key->last_value) {
  34.         key->last_value = value;
  35.         if (value == 0) {
  36.             input_report_key(input_device, key->key_code, 1);  /* Key press event */
  37.         } else {
  38.             input_report_key(input_device, key->key_code, 0);  /* Key release event */
  39.         }
  40.         input_sync(input_device);
  41.     }
  42. }
  43. /* GPIO IRQ handler */
  44. static irqreturn_t gpio_key_irq(int irq, void *arg)
  45. {
  46.     struct keys_desc *key = arg;
  47.     /* start debounce timer(20ms) to delay event processing */
  48.     mod_timer(&key->timer, jiffies + msecs_to_jiffies(20));
  49.     return IRQ_HANDLED;
  50. }
  51. static int key_probe(struct platform_device *pdev) {
  52.     struct device *dev = &pdev->dev;
  53.     struct device_node *np = pdev->dev.of_node;
  54.     struct device_node *key_node;
  55.     int ret, i=0;
  56.     /* allocate memory for private data structure */
  57.     priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  58.     if (!priv)
  59.         return -ENOMEM;
  60.     /* parser the number of keys from the device tree */
  61.     priv->nkeys = device_get_child_node_count(dev);
  62.     if ( priv->nkeys < 1) {
  63.         dev_err(dev, "Failed to read keys gpio from device tree\n");
  64.         return -EINVAL;
  65.     }
  66.     dev_info(dev, "gpio keys driver probe for %d keys from device tree\n", priv->nkeys);
  67.     /* allocate memory for all the keys */
  68.     priv->keys = devm_kzalloc(dev, priv->nkeys*sizeof(*priv->keys), GFP_KERNEL);
  69.     if (!priv->keys )
  70.         return -ENOMEM;
  71.     /* traval all the keys child node */
  72.     for_each_child_of_node(np, key_node) {
  73.         /* read lable information */
  74.         if (of_property_read_string(key_node, "lable", &priv->keys[i].lable)) {
  75.             dev_err(dev, "Failed to read lable from key node\n");
  76.             continue;
  77.         };
  78.         /* read gpio information */
  79.         priv->keys[i].gpio = of_get_named_gpio(key_node, "gpios", 0);
  80.         if( priv->keys[i].gpio < 0 ) {
  81.             dev_err(dev, "Failed to read lable from key node\n");
  82.             continue;
  83.         }
  84.         /* read key code value */
  85.         if (of_property_read_u32(key_node, "linux,code", &priv->keys[i].key_code)) {
  86.             dev_err(dev, "Failed to read linux,code for key %s\n", priv->keys[i].lable);
  87.             continue;
  88.         }
  89.         /* request gpio for this key */
  90.         ret = devm_gpio_request(dev, priv->keys[i].gpio, priv->keys[i].lable);
  91.         if (ret) {
  92.             dev_err(dev, "Failed to request GPIO for key %s\n", priv->keys[i].lable);
  93.             continue;
  94.         }
  95.         /* request interrupt for this key */
  96.         priv->keys[i].irq = gpio_to_irq(priv->keys[i].gpio);
  97.         ret = devm_request_irq(dev, priv->keys[i].irq, gpio_key_irq, IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, priv->keys[i].lable, &priv->keys[i]);
  98.         if (ret) {
  99.             dev_err(dev, "Failed to request IRQ for key %s\n", priv->keys[i].lable);
  100.             continue;
  101.         }
  102.         /* initialize debounce timer */
  103.         timer_setup(&priv->keys[i].timer, debounce_timer_func, 0);
  104.         priv->keys[i].last_value = gpio_get_value(priv->keys[i].gpio);
  105.         /* increase to next key */
  106.         i++;
  107.     }
  108.     priv->nkeys = i; /* update valid keys number */
  109.     /* alloc input device */
  110.     input_device = devm_input_allocate_device(dev);
  111.     if (!input_device) {
  112.         dev_err(dev, "failed to allocate input device\n");
  113.         return -ENOMEM;
  114.     }
  115.     /* set input deivce information */
  116.     input_device->name = "mykeys";
  117.     input_device->evbit[0] = BIT_MASK(EV_KEY); /* key event */
  118.     for ( i=0; i<priv->nkeys; i++) {
  119.         set_bit(priv->keys[i].key_code, input_device->keybit);
  120.     }
  121.     /* register input device */
  122.     ret = input_register_device(input_device);
  123.     if (ret) {
  124.         pr_err("Failed to register input device\n");
  125.         return ret;
  126.     }
  127.     return 0;
  128. }
  129. static int key_remove(struct platform_device *pdev)
  130. {
  131.     input_unregister_device(input_device);
  132.     dev_info(&pdev->dev, "gpio keys driver removed.\n");
  133.     return 0;
  134. }
  135. static const struct of_device_id key_of_match[] = {
  136.     { .compatible = "my,keys", },
  137.     { /* sentinel */ },
  138. };
  139. MODULE_DEVICE_TABLE(of, key_of_match);
  140. static struct platform_driver key_driver = {
  141.     .probe = key_probe,
  142.     .remove = key_remove,
  143.     .driver = {
  144.         .name = "keys",
  145.         .of_match_table = key_of_match,
  146.     },
  147. };
  148. module_platform_driver(key_driver);
  149. MODULE_LICENSE("GPL");
复制代码
5.4、按键驱动测试

首先,在开发板上更新我们的设备树文件。
  1. ARCH ?= arm
  2. CROSS_COMPILE ?= /opt/gcc-aarch32-10.3-2021.07/bin/arm-none-linux-gnueabihf-
  3. KERNAL_DIR ?= ~/igkboard-imx6ull/bsp/kernel/linux-imx/
  4. PWD :=$(shell pwd)
  5. obj-m += keys.o
  6. modules:
  7.     $(MAKE) ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} -C $(KERNAL_DIR) M=$(PWD) modules
  8.     @make clear
  9. clear:
  10.     @rm -f *.o *.cmd *.mod *.mod.c
  11.     @rm -rf *~ core .depend .tmp_versions Module.symvers modules.order -f
  12.     @rm -f .*ko.cmd .*.o.cmd .*.o.d
  13.     @rm -f *.unsigned
  14. clean:
  15.     @rm -f *.ko
复制代码
再将按键驱动文件拷贝到开发板上。并通过insmod安装按键驱动,输入设备的设备文件都在/dev/input路径下。
  1. make
复制代码
接下来使用 evtest 命令测试我们编写的驱动如下:
3.png


来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册