深圳英文网站建设公司,网站快备案,军事新闻最新消息军事新闻,网站建设需要什么东西目录
一、input 子系统简介
二、input 驱动编写流程 1、注册 input_dev 2、上报输入事件
三、input_event 结构体 按键、鼠标、键盘、触摸屏等都属于输入(input)设备#xff0c; Linux 内核为此专门做了一个叫做 input子系统的框架来处理输入事件。输入设备本质上还是字符设…目录
一、input 子系统简介
二、input 驱动编写流程 1、注册 input_dev 2、上报输入事件
三、input_event 结构体 按键、鼠标、键盘、触摸屏等都属于输入(input)设备 Linux 内核为此专门做了一个叫做 input子系统的框架来处理输入事件。输入设备本质上还是字符设备只是在此基础上套上了 input 框架用户只需要负责上报输入事件比如按键值、坐标等信息 input 核心层负责处理这些事件。本章就来学习一下 Linux 内核中的 input 子系统框架。
一、input 子系统简介 input 就是输入的意思因此 input 子系统就是管理输入的子系统和 pinctrl、 gpio 子系统一样都是 Linux 内核针对某一类设备而创建的框架。比如按键输入、键盘、鼠标、触摸屏等等这些都属于输入设备不同的输入设备所代表的含义不同按键和键盘就是代表按键信息鼠标和触摸屏代表坐标信息因此在应用层的处理就不同对于驱动编写者而言不需要去关心应用层的事情只需要按照要求上报这些输入事件即可。 为此 input 子系统分为 input 驱动层、 input 核心层、 input 事件处理层最终给用户空间提供可访问的设备节点 input 子系统框架如下图所示 上图中左边就是最底层的具体设备比如按键、 USB 键盘/鼠标等中间部分属于Linux 内核空间分为驱动层、核心层和事件层最右边的就是用户空间所有的输入设备以文件的形式供用户应用程序使用。可以看出 input 子系统用到了前面讲解的驱动分层模型编写驱动程序的时候只需要关注中间的驱动层、核心层和事件层这三个层的分工如下 驱动层输入设备的具体驱动程序比如按键驱动程序向内核层报告输入内容。 核心层承上启下为驱动层提供输入设备注册和操作接口。通知事件层对输入事件进行处理。 事件层主要和用户空间进行交互。
二、input 驱动编写流程 input 核心层会向 Linux 内核注册一个字符设备大家找到 drivers/input/input.c 这个文件input.c 就是 input 输入子系统的核心层此文件里面有如下所示代码
struct class input_class {.name input,.devnode input_devnode,
};
EXPORT_SYMBOL_GPL(input_class);
...
static int __init input_init(void)
{int err;err class_register(input_class);if (err) {pr_err(unable to register input_dev class\n);return err;}err input_proc_init();if (err)goto fail1;err register_chrdev_region(MKDEV(INPUT_MAJOR, 0),INPUT_MAX_CHAR_DEVICES, input);if (err) {pr_err(unable to register char major %d, INPUT_MAJOR);goto fail2;}return 0;fail2: input_proc_exit();fail1: class_unregister(input_class);return err;
}第 11 行注册一个 input 类这样系统启动以后就会在/sys/class 目录下有一个 input 子目录如下图所示 第 21~22 行注册一个字符设备主设备号为 INPUT_MAJORINPUT_MAJOR 定义在 include/uapi/linux/major.h 文件中定义如下 因此 input 子系统的所有设备主设备号都为 13我们在使用 input 子系统处理输入设备的时候就不需要去注册字符设备了我们只需要向系统注册一个 input_device 即可。
1、注册 input_dev 在使用 input 子系统的时候我们只需要注册一个 input 设备即可input_dev 结构体表示 input设备此结构体定义在 include/linux/input.h 文件中定义如下(有省略)
struct input_dev {const char *name;const char *phys;const char *uniq;struct input_id id;unsigned long propbit[BITS_TO_LONGS(INPUT_PROP_CNT)];unsigned long evbit[BITS_TO_LONGS(EV_CNT)]; /* 事件类型的位图 */unsigned long keybit[BITS_TO_LONGS(KEY_CNT)]; /* 按键值的位图 */ unsigned long relbit[BITS_TO_LONGS(REL_CNT)]; /* 相对坐标的位图 */unsigned long absbit[BITS_TO_LONGS(ABS_CNT)]; /* 绝对坐标的位图 */unsigned long mscbit[BITS_TO_LONGS(MSC_CNT)]; /* 杂项事件的位图 */unsigned long ledbit[BITS_TO_LONGS(LED_CNT)]; /*LED 相关的位图 */unsigned long sndbit[BITS_TO_LONGS(SND_CNT)]; /* sound 有关的位图 */unsigned long ffbit[BITS_TO_LONGS(FF_CNT)]; /* 压力反馈的位图 */unsigned long swbit[BITS_TO_LONGS(SW_CNT)]; /*开关状态的位图 */unsigned int hint_events_per_packet;unsigned int keycodemax;unsigned int keycodesize;void *keycode;......unsigned long key[BITS_TO_LONGS(KEY_CNT)];unsigned long led[BITS_TO_LONGS(LED_CNT)];unsigned long snd[BITS_TO_LONGS(SND_CNT)];unsigned long sw[BITS_TO_LONGS(SW_CNT)];......bool inhibited;
};第 9 行 evbit 表示输入事件类型可选的事件类型定义在 include/uapi/linux/input-event-codes.h 文件中事件类型如下
/** Event types*/#define EV_SYN 0x00 /* 同步事件 */
#define EV_KEY 0x01 /* 按键事件 */
#define EV_REL 0x02 /* 相对坐标事件 */
#define EV_ABS 0x03 /* 绝对坐标事件 */
#define EV_MSC 0x04 /* 杂项(其他)事件 */
#define EV_SW 0x05 /* 开关事件 */
#define EV_LED 0x11 /* LED */
#define EV_SND 0x12 /* sound(声音) */
#define EV_REP 0x14 /* 重复事件 */
#define EV_FF 0x15 /* 压力事件 */
#define EV_PWR 0x16 /* 电源事件 */
#define EV_FF_STATUS 0x17 /* 压力状态事件 */
#define EV_MAX 0x1f
#define EV_CNT (EV_MAX1)比如我们要使用到按键那么就需要注册 EV_KEY 事件如果要使用连按功能的话还需要注册 EV_REP 事件。 继续回到示例代码中第 9 行~17 行的 evbit、 keybit、 relbit 等等都是存放不同事件对应的值。比如我们本章要使用按键事件因此要用到 keybit keybit 就是按键事件使用的位图 Linux 内核定义了很多按键值这些按键值定义在 include/uapi/linux/input-event-codes.h 文件中按键值如下
#define KEY_RESERVED 0
#define KEY_ESC 1
#define KEY_1 2
#define KEY_2 3
#define KEY_3 4
#define KEY_4 5
#define KEY_5 6
#define KEY_6 7
#define KEY_7 8
#define KEY_8 9
#define KEY_9 10
#define KEY_0 11
#define KEY_MINUS 12
......
#define BTN_TRIGGER_HAPPY39 0x2e6
#define BTN_TRIGGER_HAPPY40 0x2e7我们可以将开发板上的按键值设置为上述宏定义中的任意一个比如我们本章实验会将 I.MX6U-ALPHA 开发板上的 KEY 按键值设置为 KEY_0。在编写 input 设备驱动的时候我们需要先申请一个 input_dev 结构体变量使用input_allocate_device 函数来申请一个 input_dev此函数原型如下所示
struct input_dev *input_allocate_device(void)
函数参数和返回值含义如下
- 参数无
- 返回值 申请到的 input_dev如果要注销的 input 设备的话需要使用 input_free_device 函数来释放掉前面申请到的input_dev input_free_device 函数原型如下
void input_free_device(struct input_dev *dev)
函数参数和返回值含义如下
- dev需要释放的 input_dev
- 返回值 无申请好一个 input_dev 以后就需要初始化这个 input_dev需要初始化的内容主要为事件类型(evbit)和事件值(keybit)这两种。 input_dev 初始化完成以后就需要向 Linux 内核注册 input_dev了需要用到 input_register_device 函数此函数原型如下
int input_register_device(struct input_dev *dev)
函数参数和返回值含义如下
- dev要注册的 input_dev
- 返回值 0 input_dev 注册成功负值 input_dev 注册失败同样的注销 input 驱动的时候也需要使用 input_unregister_device 函数来注销掉前面注册的 input_dev input_unregister_device 函数原型如下
void input_unregister_device(struct input_dev *dev)
函数参数和返回值含义如下
- dev要注销的 input_dev
- 返回值 无综上所述 input_dev 注册过程如下 ①、使用 input_allocate_device 函数申请一个 input_dev。 ②、初始化 input_dev 的事件类型以及事件值。 ③、使用 input_register_device 函数向 Linux 系统注册前面初始化好的 input_dev。 ④、卸载 input驱动的时候需要先使用 input_unregister_device 函数注销掉注册的 input_dev然后使用 input_free_device 函数释放掉前面申请的 input_dev。 input_dev 注册过程示例代码如下所示
struct input_dev *inputdev; /* input 结构体变量 *//* 驱动入口函数 */
static int __init xxx_init(void)
{.....nputdev input_allocate_device(); /* 申请 input_dev */nputdev-name test_inputdev; /* 设置 input_dev 名字 *//*********第一种设置事件和事件值的方法***********/__set_bit(EV_KEY, inputdev-evbit); /* 设置产生按键事件 */__set_bit(EV_REP, inputdev-evbit); /* 重复事件 */__set_bit(KEY_0, inputdev-keybit); /*设置产生哪些按键值 *//************************************************//*********第二种设置事件和事件值的方法***********/keyinputdev.inputdev-evbit[0] BIT_MASK(EV_KEY) |BIT_MASK(EV_REP);keyinputdev.inputdev-keybit[BIT_WORD(KEY_0)] |BIT_MASK(KEY_0);/************************************************//*********第三种设置事件和事件值的方法***********/keyinputdev.inputdev-evbit[0] BIT_MASK(EV_KEY) |BIT_MASK(EV_REP);input_set_capability(keyinputdev.inputdev, EV_KEY, KEY_0);/************************************************//* 注册 input_dev */input_register_device(inputdev);......return 0;
}/* 驱动出口函数 */
static void __exit xxx_exit(void)
{input_unregister_device(inputdev); /* 注销 input_dev */input_free_device(inputdev); /* 删除 input_dev */
}第 1 行定义一个 input_dev 结构体指针变量。 第 4~30 行驱动入口函数在此函数中完成 input_dev 的申请、设置、注册等工作。第 7行调用 input_allocate_device 函数申请一个 input_dev。第 10~23 行都是设置 input 设备事件和按键值这里用了三种方法来设置事件和按键值。第 27 行调用 input_register_device 函数向 Linux内核注册 inputdev。 第 33~37 行驱动出口函数第 35 行调用 input_unregister_device 函数注销前面注册的input_dev第 36 行调用 input_free_device 函数删除前面申请的 input_dev。
2、上报输入事件 当向 Linux 内核注册好 input_dev 以后还不能高枕无忧的使用 input 设备 input 设备都是具有输入功能的但是具体是什么样的输入值 Linux 内核是不知道的需要获取到具体的输入值或者说是输入事件然后将输入事件上报给 Linux 内核。 比如按键需要在按键中断处理函数或者消抖定时器中断函数中将按键值上报给 Linux 内核这样 Linux 内核才能获取到正确的输入值。不同的事件其上报事件的 API 函数不同依次来看一下一些常用的事件上报 API 函数。 首先是 input_event 函数此函数用于上报指定的事件以及对应的值函数原型如下
void input_event(struct input_dev *dev,unsigned int type, unsigned int code, int value)
函数参数和返回值含义如下
- dev需要上报的 input_dev
- type: 上报的事件类型比如 EV_KEY
- code 事件码也就是我们注册的按键值比如 KEY_0、 KEY_1 等等
- value事件值比如 1 表示按键按下 0 表示按键松开。返回值 无input_event 函数可以上报所有的事件类型和事件值 Linux 内核也提供了其他的针对具体事件的上报函数这些函数其实都用到了 input_event 函数。比如上报按键所使用的input_report_key 函数此函数内容如下
static inline void input_report_key(struct input_dev *dev, unsigned int code, int value)
{input_event(dev, EV_KEY, code, !!value);
}从上面函数定义可以看出 input_report_key 函数的本质就是 input_event 函数如果要上报按键事件的话还是建议大家使用 input_report_key 函数。同样的还有一些其他的事件上报函数这些函数如下所示
static inline void input_report_rel(struct input_dev *dev, unsigned int code, int value)
static inline void input_report_abs(struct input_dev *dev, unsigned int code, int value)
static inline void input_report_ff_status(struct input_dev *dev, unsigned int code, int value)
static inline void input_report_switch(struct input_dev *dev, unsigned int code, int value)
static inline void input_sync(struct input_dev *dev)
static inline void input_mt_sync(struct input_dev *dev)当我们上报事件以后还需要使用 input_sync 函数来告诉 Linux 内核 input 子系统上报结束input_sync 函数本质是上报一个同步事件此函数原型如下所示
static inline void input_sync(struct input_dev *dev)
{input_event(dev, EV_SYN, SYN_REPORT, 0);
}
函数参数和返回值含义如下
- dev需要上报同步事件的 input_dev。
- 返回值 无。综上所述按键的上报事件的参考代码如下所示
/* 用于按键消抖的定时器服务函数 */
void timer_function(unsigned long arg)
{unsigned char value;value gpio_get_value(keydesc-gpio); /* 读取 IO 值 */if(value 0){ /* 按下按键 *//* 上报按键值 */input_report_key(inputdev, KEY_0, 1); /* 最后一个参数 1 按下 */input_sync(inputdev); /* 同步事件 */} else { /* 按键松开 */input_report_key(inputdev, KEY_0, 0); /* 最后一个参数 0 松开 */input_sync(inputdev); /* 同步事件 */}
}第 6 行获取按键值判断按键是否按下。 第 9~10 行如果按键值为 0 那么表示按键被按下了如果按键按下的话就要使用input_report_key 函数向 Linux 系统上报按键值比如向 Linux 系统通知 KEY_0 这个按键按下了。 第 12~13 行如果按键值为 1 的话就表示按键没有按下是松开的。向 Linux 系统通知KEY_0 这个按键没有按下或松开了。
三、input_event 结构体 Linux 内核使用 input_event 这个结构体来表示所有的输入事件 input_envent 结构体定义在include/uapi/linux/input.h 文件中结构体内容如下
struct input_event {
#if (__BITS_PER_LONG ! 32 || !defined(__USE_TIME_BITS64)) !defined(__KERNEL__)struct timeval time;
#define input_event_sec time.tv_sec
#define input_event_usec time.tv_usec
#else__kernel_ulong_t __sec;
#if defined(__sparc__) defined(__arch64__)unsigned int __usec;unsigned int __pad;
#else__kernel_ulong_t __usec;
#endif
#define input_event_sec __sec
#define input_event_usec __usec
#endif__u16 type;__u16 code;__s32 value;
};依次来看一下 input_event 结构体中的各个成员变量 time时间也就是此事件发生的时间为 timeval 结构体类型 timeval 结构体定义如下
typedef long __kernel_long_t;
typedef __kernel_long_t __kernel_time_t;
typedef __kernel_long_t __kernel_suseconds_t;struct timeval {__kernel_time_t tv_sec; /* 秒 */__kernel_suseconds_t tv_usec; /* 微秒 */
};从上面可以看出 tv_sec 和 tv_usec 这两个成员变量都为 long 类型也就是 32位这个一定要记住后面我们分析 event 事件上报数据的时候要用到。 type 事件类型比如 EV_KEY表示此次事件为按键事件此成员变量为 16 位。 code 事件码比如在 EV_KEY 事件中 code 就表示具体的按键码如 KEY_0、 KEY_1等等这些按键。此成员变量为 16 位。 value 值比如 EV_KEY 事件中 value 就是按键值表示按键有没有被按下如果为 1 的话说明按键按下如果为 0 的话说明按键没有被按下或者按键松开了。 input_envent 这个结构体非常重要因为所有的输入设备最终都是按照 input_event 结构体呈现给用户的用户应用程序可以通过 input_event 来获取到具体的输入事件或相关的值比如按键值等。关于 input 子系统就讲解到这里x下一章以开发板上的 KEY0 按键为例讲解一下如何编写 input 驱动。 关于更多嵌入式C语言、FreeRTOS、RT-Thread、Linux应用编程、linux驱动等相关知识关注公众号【嵌入式Linux知识共享】后续精彩内容及时收看了解。 文章转载自: http://www.morning.qsy40.cn.gov.cn.qsy40.cn http://www.morning.dmcxh.cn.gov.cn.dmcxh.cn http://www.morning.rnrwq.cn.gov.cn.rnrwq.cn http://www.morning.kdbbm.cn.gov.cn.kdbbm.cn http://www.morning.cmdfh.cn.gov.cn.cmdfh.cn http://www.morning.ssjtr.cn.gov.cn.ssjtr.cn http://www.morning.dbsch.cn.gov.cn.dbsch.cn http://www.morning.27asw.cn.gov.cn.27asw.cn http://www.morning.zybdj.cn.gov.cn.zybdj.cn http://www.morning.zlnkq.cn.gov.cn.zlnkq.cn http://www.morning.frsbf.cn.gov.cn.frsbf.cn http://www.morning.knrgb.cn.gov.cn.knrgb.cn http://www.morning.qzqfq.cn.gov.cn.qzqfq.cn http://www.morning.gdljq.cn.gov.cn.gdljq.cn http://www.morning.gqjzp.cn.gov.cn.gqjzp.cn http://www.morning.zwsgl.cn.gov.cn.zwsgl.cn http://www.morning.rdnkx.cn.gov.cn.rdnkx.cn http://www.morning.ghgck.cn.gov.cn.ghgck.cn http://www.morning.jsrnf.cn.gov.cn.jsrnf.cn http://www.morning.xcbnc.cn.gov.cn.xcbnc.cn http://www.morning.dqpd.cn.gov.cn.dqpd.cn http://www.morning.thbnt.cn.gov.cn.thbnt.cn http://www.morning.jbmbj.cn.gov.cn.jbmbj.cn http://www.morning.wxlzr.cn.gov.cn.wxlzr.cn http://www.morning.rgqnt.cn.gov.cn.rgqnt.cn http://www.morning.pbsqr.cn.gov.cn.pbsqr.cn http://www.morning.nmyrg.cn.gov.cn.nmyrg.cn http://www.morning.nfnxp.cn.gov.cn.nfnxp.cn http://www.morning.bktzr.cn.gov.cn.bktzr.cn http://www.morning.tzcr.cn.gov.cn.tzcr.cn http://www.morning.wcghr.cn.gov.cn.wcghr.cn http://www.morning.ryxbz.cn.gov.cn.ryxbz.cn http://www.morning.fylsz.cn.gov.cn.fylsz.cn http://www.morning.mgtmm.cn.gov.cn.mgtmm.cn http://www.morning.rdbj.cn.gov.cn.rdbj.cn http://www.morning.gcdzp.cn.gov.cn.gcdzp.cn http://www.morning.fktlr.cn.gov.cn.fktlr.cn http://www.morning.fwkq.cn.gov.cn.fwkq.cn http://www.morning.ysllp.cn.gov.cn.ysllp.cn http://www.morning.yqsr.cn.gov.cn.yqsr.cn http://www.morning.rgrys.cn.gov.cn.rgrys.cn http://www.morning.sbrxm.cn.gov.cn.sbrxm.cn http://www.morning.gctgc.cn.gov.cn.gctgc.cn http://www.morning.bkkgt.cn.gov.cn.bkkgt.cn http://www.morning.kpypy.cn.gov.cn.kpypy.cn http://www.morning.geledi.com.gov.cn.geledi.com http://www.morning.jwwfk.cn.gov.cn.jwwfk.cn http://www.morning.lyhry.cn.gov.cn.lyhry.cn http://www.morning.zpjhh.cn.gov.cn.zpjhh.cn http://www.morning.nwfpl.cn.gov.cn.nwfpl.cn http://www.morning.ywpwq.cn.gov.cn.ywpwq.cn http://www.morning.27asw.cn.gov.cn.27asw.cn http://www.morning.srwny.cn.gov.cn.srwny.cn http://www.morning.yldgw.cn.gov.cn.yldgw.cn http://www.morning.nwfxp.cn.gov.cn.nwfxp.cn http://www.morning.lfttb.cn.gov.cn.lfttb.cn http://www.morning.darwallet.cn.gov.cn.darwallet.cn http://www.morning.pwhjr.cn.gov.cn.pwhjr.cn http://www.morning.wqmpd.cn.gov.cn.wqmpd.cn http://www.morning.frsbf.cn.gov.cn.frsbf.cn http://www.morning.tsmcc.cn.gov.cn.tsmcc.cn http://www.morning.nwqyq.cn.gov.cn.nwqyq.cn http://www.morning.wbnsf.cn.gov.cn.wbnsf.cn http://www.morning.dzzjq.cn.gov.cn.dzzjq.cn http://www.morning.rnds.cn.gov.cn.rnds.cn http://www.morning.jzykq.cn.gov.cn.jzykq.cn http://www.morning.mingjiangds.com.gov.cn.mingjiangds.com http://www.morning.mxdhy.cn.gov.cn.mxdhy.cn http://www.morning.tgyqq.cn.gov.cn.tgyqq.cn http://www.morning.zcncb.cn.gov.cn.zcncb.cn http://www.morning.ogzjf.cn.gov.cn.ogzjf.cn http://www.morning.dnbhd.cn.gov.cn.dnbhd.cn http://www.morning.lhgqc.cn.gov.cn.lhgqc.cn http://www.morning.kaylyea.com.gov.cn.kaylyea.com http://www.morning.demoux.com.gov.cn.demoux.com http://www.morning.rhgtc.cn.gov.cn.rhgtc.cn http://www.morning.rfgkf.cn.gov.cn.rfgkf.cn http://www.morning.tkhyk.cn.gov.cn.tkhyk.cn http://www.morning.xhhzn.cn.gov.cn.xhhzn.cn http://www.morning.dhwyl.cn.gov.cn.dhwyl.cn