input子系统——架构、驱动、应用

一、input子系统简介

1、Input驱动程序是linux输入设备的驱动程序,分成游戏杆(joystick)、鼠标(mousemice)、事件设备(event)。其中事件驱动程序是目前通用的驱动程序,可支持键盘、鼠标、触摸屏等多种输入设备。

2、input驱动程序的主设备号是13、次设备号的分布如下:

joystick游戏杆:0~16

mouse鼠标: 32~62

mice鼠标: 63

事件设备: 64~95

3、主要的结构体

Input_device:代表着具体的输入设备,他直接从硬件中读取数据,并以事件的形式转发

Handler:代表接收某一类事件的上层接口,对应于一类事件设备文件

Handle:用于将input_devicehandler连接起来,对应于某个具体的设备文件。

Client:对应于用户程序对文件的访问接口,每open一次事件驱动,就创建一个client



Handlerstruct input_handler *input_table[8],最多有8input驱动,比如/dev/input/eventX/dev/input/mouseX就是两种常用的input驱动。

Handle:以evdev.c为例,根据次设备号取值范围64-95,可以分别生成input/event0input/event1,一直到input/event3132个设备文件。每个设备文件对应一个handle

Client:每个设备文件又可以同时对应多个client,当有多个应用程序同时调用设备文件时,他们会从不同的client中取数据。

input子系统维护着两条重要的链表:input_dev_list,input_handler_list

以上部分摘自:http://blog.csdn.net/zhangxizhicn/article/details/6642062


二、input子系统架构

input子系统由驱动层drivers,输入子系统核心层input core,事件处理层event handler组成。

驱动层并不创建文件节点,他只负责将采集到的数据通过input.c提供的函数input_event向上一层汇报。而各个事件驱动则分别将他们感兴趣的事件信息提取出来,通过文件节点传给用户空间。

一个输入事件,通过输入设备发给系统如鼠标移动,键盘按键按下等通过device driver->input core(handler->event函数)->event handler->user space的顺序到达用户空间传给应用程序。

一个输出事件,通过系统发给输入设备,通过user space->event handler->input core(dev->event函数)->device driver

1、驱动功能层:负责和底层的硬件设备打交道,将底层硬件设备对用户输入的响应转换为标准的输入事件以后再向上发送给输入系统核心层

2、Input系统核心层:由driver/input/input.c及相关头文件实现,他对下提供了设备驱动层的接口,对上提供了事件处理层的变成接口。

3、事件处理层将硬件设备上报的事件分发到用户空间和内核。


结构图如下:



三、编写input驱动需要的函数

1)包含头文件<linux/input.h>,他是input子系统的接口,提供了必要的定义消息

2)Input_allocate_device()

分配了一个Input device的结构,设置他的bit field来告诉input子系统他能产生或者接收什么事件。

3)input_register_device(struct input_dev *dev)

将dev结构体添加到input_dev_list全局链表中去

通过input_attach_handler(struct input_dev *dev, struct input_handler *handler)来查找对应的handler

input_attach_handler里面实际调用了input_match_device(const struct input_device_id *id,struct input_dev *dev)

一旦input_attach_handler找到了对应的handler,就执行handler->connect

4)input_report_key(struct input_dev *dev, unsigned int code, int value)

5)input_sync(struct input_dev *dev)

告诉事件的接收者,到此为止为一次完整的消息。比如我们在touch screen上获得了xy的值,要使作为一次事件,那么将input_sync加在report xy值得后面。

6)其他的事件type,输出事件处理

其他的事件有:

EV_LED:用作键盘的LED

EV_SND:用作键盘的蜂鸣器

他和键盘事件很相似,只不过键盘事件是INPUT_PASS_TO_DEVICE,而输出事件是INPUT_PASS_TO_HANDLERS,从系统到输入设备的驱动程序,如果你的驱动程序要处理这些事件,必须设置evbit中相应位,而且要实现一个回调函数。

struct input_dev *button_dev;

button_dev->event = button_event;这个便是处理输出事件的回调函数



四、普通按键实现input驱动例子

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /* 
  2. drivers->input core->event handler 
  3. function: this file is button driver 
  4. date: 20150101 
  5. author: lei_wang 
  6. */  
  7.   
  8. #include <linux/module.h>  
  9. #include <linux/kernel.h>  
  10. #include <linux/init.h>  
  11. #include <linux/irq.h>  
  12. #include <asm/irq.h>  
  13. #include <asm/io.h>  
  14. #include <mach/regs-gpio.h>  
  15. #include <mach/hardware.h>  
  16. #include <linux/interrupt.h>  
  17. #include <linux/gpio.h>  
  18. #include <linux/input.h>  
  19.   
  20. static struct input_dev *button_dev;  
  21.   
  22. static irqreturn_t button_intr(int irq, void *dev_id)  
  23. {  
  24.     int val;  
  25.     val = s3c2410_gpio_getpin(S3C2410_GPG(0));  
  26. //  printk(KERN_INFO "key value is %d\n", val);  
  27.   
  28.     input_report_key(button_dev, BTN_0, val);  
  29.     input_sync(button_dev);  
  30.       
  31.     return IRQ_RETVAL(IRQ_HANDLED);  
  32. }  
  33.   
  34. static int __init button_init(void)  
  35. {  
  36.     int ret;  
  37.     ret = request_irq(IRQ_EINT8, button_intr, IRQ_TYPE_EDGE_BOTH, "button0", NULL);  
  38.     if (ret) {  
  39.         printk(KERN_ERR "%s request failed\n", __func__);  
  40.         return -ENODEV;  
  41.     }  
  42.   
  43.     button_dev = input_allocate_device();  
  44.     if (!button_dev) {  
  45.         printk(KERN_ERR "button.c: Not enough memory\n");  
  46.         free_irq(IRQ_EINT8, NULL);  
  47.         return -ENOMEM;  
  48.     }  
  49.   
  50.     button_dev->name = "button0";  
  51.     button_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY);  
  52.     button_dev->keybit[BIT_WORD(BTN_0)] = BIT_MASK(BTN_0);  
  53.   
  54.     ret = input_register_device(button_dev);  
  55.     if (ret) {  
  56.         printk(KERN_ERR "button.c: Failed to register device\n");  
  57.         input_free_device(button_dev);  
  58.         free_irq(IRQ_EINT8, NULL);  
  59.         return -ENODEV;  
  60.     }  
  61.   
  62.     printk(KERN_INFO "button init ok!\n");  
  63.     return 0;  
  64. }  
  65.   
  66. static void __exit button_exit(void)  
  67. {  
  68.     input_unregister_device(button_dev);  
  69.     input_free_device(button_dev);  
  70.     free_irq(IRQ_EINT8, NULL);  
  71.       
  72.     printk(KERN_INFO "button exit ok!\n");  
  73. }  
  74.   
  75. module_init(button_init);  
  76. module_exit(button_exit);  
  77. MODULE_LICENSE("GPL");  
  78. MODULE_AUTHOR("Realsil Luckywang");  


Makefile如下:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. obj-m = button.o  
  2. KERNELDIR ?=/home/lei/linux-2.6.32.2  
  3. modules:  
  4.     $(MAKE) -C $(KERNELDIR) M=$(shell pwd) modules  
  5. clean:  
  6.     rm -rf *.o *.mod.c *.order *.symvers  


Include/linux/bitops.h中定义了

#define BIT(nr) (1UL << (nr))

#define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))

#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)

#define BTN_0 0x100

button_dev->evbit[0] = BIT_MASK(EV_KEY);

button_dev->keybit[BIT_WORD(BTN_0)] = BIT_MASK(BTN_0);

说明:

1)上面的0x100表示BTN_0这个bit在所有的bit中是0x100(bit 256)位,那么

BIT_WORD(BTN_0)代表bit 256keybit这个数组的第几个数组(第8个)

BIT_MASK(BTN_0)代表bit 256keybit这个数组的第几个数组里面的值(第8个数组的bit0)

2)事件类型type——编码code——值value

evbit是事件数组,evbit这个事件数组里面可以放很多事件类型,比如keyabs

事件key里面又有很多具体编码BTN_0BTN_TOUCH

事件abs里面也有很多具体编码ABS_XABS_Y

不同编码有不同的值


另外http://blog.csdn.net/ylyuanlu/article/details/6704744 这篇博客对以下说的挺详细的

1)input子系统的struct input_dev、struct handler的注册

2)struct input_dev与struct input_handler怎么互相匹配(类似于device和driver匹配)

3)事件处理过程



五、例子对应的应用程序

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /* 
  2. 20150101 
  3. just a simple input test code 
  4. lei_wang 
  5. */  
  6.   
  7. #include <stdio.h>  
  8. #include <stdlib.h>  
  9. #include <unistd.h>  
  10. #include <fcntl.h>  
  11. #include <string.h>  
  12. #include <linux/input.h>  
  13.   
  14. int main()  
  15. {  
  16.     int fd;  
  17.     int version;  
  18.     int ret;  
  19.     struct input_event ev;  
  20.       
  21.     fd = open("/dev/input/event1", O_RDONLY);  
  22.     if (fd < 0) {  
  23.         printf("open file failed\n");  
  24.         exit(1);  
  25.     }  
  26.   
  27.     ioctl(fd, EVIOCGVERSION, &version);  
  28.     printf("evdev driver version is 0x%x: %d.%d.%d\n",  
  29.                     version, version>>16, (version>>8) & 0xff, version & 0xff);  
  30.   
  31.     while (1) {  
  32.         ret = read(fd, &ev, sizeof(struct input_event));  
  33.         if (ret < 0) {  
  34.             printf("read event error!\n");  
  35.             exit(1);  
  36.         }  
  37.           
  38.         if (ev.type == EV_KEY)  
  39.             printf("type %d,code %d, value %d\n", ev.type, ev.code, ev.value);  
  40.     }  
  41.       
  42.     return 0;  
  43. }  

以上只是一个简单的应用程序测试。当你按下K1的时候,串口终端会有显示的input dev上报的按键的消息。



另外,编写应用程序的时候如何确定是哪个eventX呢,cat /proc/bus/input/devices,输出打印消息如下:


这里插入了鼠标,通过比较VID、PID来找到对应的usb mouse设备,然后找到对应的mouse0、event1

其实也可以不写应用程序,直接通过cat /dev/input/mouse0 | hexdump来获取鼠标的数据。


另外还有很多里面ioctl调用的内容没有实验,具体可以参考这篇博客http://www.cnblogs.com/leaven/archive/2011/02/12/1952793.html,对ioctl的每个case以及read调用都试一遍,找到自己的体会。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值