linux输入子系统概念介绍
在此文章之前,我们讲解的都是简单的字符驱动,涉及的内容有字符驱动的框架、自动创建设备节点、linux中断、poll机制、异步通知、同步互斥、非阻塞、定时器去抖动。
上一节文章链接:http://blog.csdn.net/lwj103862095/article/details/17589311
在这一节里,我们要引入linux的分离分层的概念,linux输入子系统是一个很好的代表,在讲解如何编写input子系统的驱动之前,我们理所当然的要先好好认识一下input子系统的框架。
一、linux输入子系统的框架(摘自作者:刘洪涛,华清远见嵌入式学院讲师。)
下图是input输入子系统框架,输入子系统由输入子系统核心层(Input Core),驱动层和事件处理层(Event Handler)
三部份组成。一个输入事件,如鼠标移动,键盘按键按下,joystick的移动等等通过
input driver -> Input core -> Event handler -> userspace 到达用户空间传给应用程序。
二、drivers/input/input.c:
入口函数input_init > err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
plain?
- static int __init input_init(void)
- {
- int err;
- ...
- /* 创建类 */
- err = class_register(&input_class);
- ...
- /* 注册一个字符驱动,主设备号为13 */
- err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
- ...
- return 0;
- }
只有一个open函数,其他read,write函数呢?
plain?
- static const struct file_operations input_fops = {
- .owner = THIS_MODULE,
- .open = input_open_file,
- };
input_open_file函数
plain?
- static int input_open_file(struct inode *inode, struct file *file)
- {
- struct input_handler *handler;
- const struct file_operations *old_fops, *new_fops = NULL;
- int err;
- ...
- /* 以次设备号为下标,在input_table数组找到一项handler */
- handler = input_table[iminor(inode) >> 5];
- /* 通过handler找到一个新的fops */
- new_fops = fops_get(handler->fops);
- ...
- old_fops = file->f_op;
- /* 从此file->f_op = new_fops */
- file->f_op = new_fops;
- ...
- /* 用新的new_fops的打开函数 */
- err = new_fops->open(inode, file);
- ...
- return err;
- }
input_handlerj结构体成员
plain?
- struct input_handler {
- void *private;
- void (*event)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
- int (*connect)(struct input_handler *handler, struct input_dev *dev, const struct input_device_id *id);
- void (*disconnect)(struct input_handle *handle);
- void (*start)(struct input_handle *handle);
- const struct file_operations *fops;
- int minor;
- const char *name;
- const struct input_device_id *id_table;
- const struct input_device_id *blacklist;
- struct list_head h_list;
- struct list_head node;
- };
问:怎么读按键?
APP:read > ... > file->f_op->read
问:input_table数组由谁构造?
答:input_register_handler
三、input_register_handler函数(注册input_handler)
plain?
- int input_register_handler(struct input_handler *handler)
- {
- struct input_dev *dev;
- ...
- INIT_LIST_HEAD(&handler->h_list);
- ...
- /* 将handler放入input_table数组 */
- input_table[handler->minor >> 5] = handler;
- ...
- /* 将handler放入input_handler_list链表 */
- list_add_tail(&handler->node, &input_handler_list);
- ...
- /* 对于每个input_dev,调用input_attach_handler
- * 根据input_handler的id_table判断能否支持这个input_dev
- */
- list_for_each_entry(dev, &input_dev_list, node)
- input_attach_handler(dev, handler);
- ...
- }
四、input_register_device函数(注册input_dev)
plain?
- int input_register_device(struct input_dev *dev)
- {
- ...
- struct input_handler *handler;
- ...
- device_add(&dev->dev);
- ...
- /* 把input_dev放入input_dev_list链表 */
- list_add_tail(&dev->node, &input_dev_list);
- ...
- /* 对于每一个input_handler,都调用input_attach_handler
- * 根据input_handler的id_table判断能否支持这个input_dev
- */
- list_for_each_entry(handler, &input_handler_list, node)
- input_attach_handler(dev, handler);
- ...
- }
五、input_attach_handler函数
plain?
- static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
- {
- const struct input_device_id *id;
- ...
- /* 根据input_handler的id_table判断能否支持这个input_dev */
- input_match_device(handler->id_table, dev);
- ...
- /* 若支持,则调用handler的connect函数,建立连接 */
- handler->connect(handler, dev, id);
- ...
- }
小总结:
注册input_dev或input_handler时,会两两比较左边的input_dev和右边的input_handler,根据input_handler的id_table判断这个input_handler能否支持这个input_dev,如果能支持,则调用input_handler的connect函数建立"连接"。
问:如何建立连接connect?
答:举例,evdev_connect函数
plain?
- static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
- const struct input_device_id *id)
- {
- struct evdev *evdev;
- ...
- /* 分配一个input_handle */
- evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
- ...
- snprintf(evdev->name, sizeof(evdev->name), "event%d", minor);
- evdev->exist = 1;
- evdev->minor = minor;
- evdev->handle.dev = input_get_device(dev); // 指向左边的input_dev
- evdev->handle.name = evdev->name;
- evdev->handle.handler = handler; // 指向右边的input_handler
- evdev->handle.private = evdev;
- /* 设置dev结构体成员 */
- dev_set_name(&evdev->dev, evdev->name);
- evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
- evdev->dev.class = &input_class;
- evdev->dev.parent = &dev->dev;
- evdev->dev.release = evdev_free;
- device_initialize(&evdev->dev);
- /* 注册 */
- input_register_handle(&evdev->handle);
- ...
- }
input_handle结构体成员
plain?
- struct input_handle {
- void *private;
- int open;
- const char *name;
- struct input_dev *dev;
- struct input_handler *handler;
- struct list_head d_node;
- struct list_head h_node;
- };
问:input_register_handle如何注册?
plain?
- int input_register_handle(struct input_handle *handle)
- {
- struct input_handler *handler = handle->handler;
- struct input_dev *dev = handle->dev;
- ...
- /* 把handle->d_node添加到dev->h_list
- * 这样,就可以从dev->h_list找到handle,进而找到handler
- */
- list_add_tail_rcu(&handle->d_node, &dev->h_list);
- ...
- /* 把handle->h_node添加到handler->h_list
- * 这样,就可以从handler->h_list找到handle,进而找到dev
- */
- list_add_tail(&handle->h_node, &handler->h_list);
- ...
- return 0;
- }
小总结:
怎么建立连接connect?
1. 分配一个input_handle结构体
2.
input_handle.dev = input_dev; // 指向左边的input_dev
input_handle.handler = input_handler; // 指向右边的input_handler
3. 注册:
input_handler->h_list = &input_handle;
inpu_dev->h_list = &input_handle;
六、怎么读按键?
答:举例,evdev_read
plain?
- static ssize_t evdev_read(struct file *file, char __user *buffer,
- size_t count, loff_t *ppos)
- {
- struct evdev_client *client = file->private_data;
- struct evdev *evdev = client->evdev;
- struct input_event event;
- ...
- /* 无数据并且是非阻塞方式打开,则立刻返回 */
- if (client->head == client->tail && evdev->exist &&
- (file->f_flags & O_NONBLOCK))
- return -EAGAIN;
- /* 否则休眠 */
- retval = wait_event_interruptible(evdev->wait,
- client->head != client->tail || !evdev->exist);
- ...
- }
问:谁来唤醒?
搜索evdev->wait发现是evdev_event唤醒的
plain?
- static void evdev_event(struct input_handle *handle,
- unsigned int type, unsigned int code, int value)
- {
- struct evdev *evdev = handle->private;
- struct evdev_client *client;
- struct input_event event;
- ...
- /* 唤醒 */
- wake_up_interruptible(&evdev->wait);
- }
问:evdev_event被谁调用?
答:应该是硬件相关的代码,input_dev那层调用的在设备的中断服务程序里,确定事件是什么,然后调用相应的input_handler的event处理函数。
举例,在drivers/input/keyboard/gpio_keys.c里的gpio_keys_isr函数
plain?
- static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
- {
- struct gpio_button_data *bdata = dev_id;
- struct gpio_keys_button *button = bdata->button;
- ...
- /* 上报事件 */
- gpio_keys_report_event(bdata);
- return IRQ_HANDLED;
- }</span>
gpio_keys_report_event函数
plain?
- static void gpio_keys_report_event(struct gpio_button_data *bdata)
- {
- struct gpio_keys_button *button = bdata->button;
- struct input_dev *input = bdata->input;
- unsigned int type = button->type ?: EV_KEY;
- int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
- /* 上报事件 */
- input_event(input, type, button->code, !!state);
- input_sync(input);
- }</span>
问:input_event函数如何上报事件
答:
plain?
- input_event-->input_handle_event-->input_pass_event
- list_for_each_entry_rcu(handle, &dev->h_list, d_node)
- if (handle->open)
- handle->handler->event(handle,
- type, code, value);
怎么写符合输入子系统框架的驱动程序?
1. 分配一个input_dev结构体
2. 设置
3. 注册
4. 硬件相关的代码,比如在中断服务程序里上报事件
linux输入子系统概念介绍的更多相关文章
- linux输入子系统
linux输入子系统(linux input subsystem)从上到下由三层实现,分别为:输入子系统事件处理层(EventHandler).输入子系统核心层(InputCore)和输入子系统设备驱 ...
- linux输入子系统(input subsystem)之evdev.c事件处理过程
1.代码 input_subsys.drv.c 在linux输入子系统(input subsystem)之按键输入和LED控制的基础上有小改动,input_subsys_test.c不变. input ...
- Linux输入子系统(转)
Linux输入子系统(Input Subsystem) 1.1.input子系统概述 输入设备(如按键,键盘,触摸屏,鼠标等)是典型的字符设备,其一般的工作机制是低层在按键,触摸等动作发生时产生一个中 ...
- Linux输入子系统(Input Subsystem)
Linux输入子系统(Input Subsystem) http://blog.csdn.net/lbmygf/article/details/7360084 input子系统分析 http://b ...
- Linux输入子系统框架分析(1)
在Linux下的输入设备键盘.触摸屏.鼠标等都能够用输入子系统来实现驱动.输入子系统分为三层,核心层和设备驱动层.事件层.核心层和事件层由Linux输入子系统本身实现,设备驱动层由我们实现.我们在设备 ...
- Linux输入子系统详解
input输入子系统框架 linux输入子系统(linux input subsystem)从上到下由三层实现,分别为:输入子系统事件处理层(EventHandler).输入子系统核心层(Input ...
- linux输入子系统简述【转】
本文转载自:http://blog.csdn.net/xubin341719/article/details/7678035 1,linux输入子系统简述 其实驱动这部分大多还是转载别人的,linux ...
- 7.Linux 输入子系统分析
为什么要引入输入子系统? 在前面我们写了一些简单的字符设备的驱动程序,我们是怎么样打开一个设备并操作的呢? 一般都是在执行应用程序时,open一个特定的设备文件,如:/dev/buttons .... ...
- Android底层开发之Linux输入子系统要不要推断系统休眠状态上报键值
Android底层开发之Linux输入子系统要不要推断系统休眠状态上报键值 题外话:一个问题研究到最后,那边记录文档的前半部分基本上都是没用的,甚至是错误的. 重点在最后,前边不过一些假想猜測. ht ...
随机推荐
- java-给微信推送消息 利用企业微信
目的:给关注用户推送消息 场景:自动化测试,运维监控,接口访问等报错预警.例如线上接口报错,发送提醒消息 准备工作: 1:注册企业号(为什么不用公众号呢?) 企业号注册 2:常用参数介绍: 1:COR ...
- 【C#基本功 控件的用法】 委托
接触C#这段时间,很多内容容易理解,但也也有很多内容总是无法踏过门槛,就像Delegate 委托,这种新的机制是Labview不具备的,他的一个用法,也让我们这些从labview跨越过来的coder, ...
- linux上安装mysql以后root不能登录的解决办法
今天心血来潮在linux上装了mysql,装完以后用命令mysql -uroot -p登录时提示 解决办法: 使用ps aux|grep mysql 查出pid 使用kill pid结束mysql进程 ...
- lambda表达式----使用
List<THealthKnowledgeEntity> tHealthKnowledgeList = tHealthKnowledgeService.queryList(query);L ...
- 查看SQLServer的最大连接数
如何查看SQLServer的最大连接数?相信很多人对个很有兴趣,一下就给出两种方法: 1. 查询服务器属性 默认服务设置为0(表示不受限制). 2. SQL查看最大连接数 这里的32767就是服务器的 ...
- 远程登录MySQL
mysql 远程连接数据库的二种方法 一.连接远程数据库: 1.显示密码 如:MySQL 连接远程数据库(192.168.5.116),端口“3306”,用户名为“root”,密码“123456” ...
- LeetCode OJ:Sqrt(x)(平方根)
Implement int sqrt(int x). Compute and return the square root of x. 简单的二分法,注意mid应该选为long,否则容易溢出: cla ...
- Java中的深拷贝(深复制)和浅拷贝(浅复制)
深拷贝(深复制)和浅拷贝(浅复制)是两个比较通用的概念,尤其在C++语言中,若不弄懂,则会在delete的时候出问题,但是我们在这幸好用的是Java.虽然java自动管理对象的回收,但对于深拷贝(深复 ...
- Qt5学习笔记——QRadioButton与QbuttonGroup
[我是小标题:使用QToolButton实现radio button功能.] QRadioButton是什么? 下图是Windows系统中典型的radio button显示效果. QRadio ...
- eclipse javaw.exe in your current path问题
问题: 第一次运行eclipse的时候,可能会提醒找不到javaw.exe ******等的问题 很坑的! 解决方案: 无法启动Eclipe,因找不到javaw.exe 还是环境变量的问题!!! 注意 ...