Linux input子系统学习总结(二)----Input事件驱动
Input 事件驱动: (主要文件 :drivers/input/evdev.c 、 drivers/input/input.h)基于kernel 4.0
一、 关键函数调用顺序:
1、input_register_handler(&evdev_handler); ///注册 evdev_handler 这个input事件驱evdev.c
2、input_attach_handler(dev, handler);////input 设备和 input 事件进行匹配 input.h
3、handler->connect(handler, dev, id);///调用evdev_handler 的 connect 函数(.connect = evdev_connect)
4、evdev_connect(struct input_handler *handler, struct input_dev *dev,
const struct input_device_id *id)
5、cdev_init(&evdev->cdev, &evdev_fops);//// 初始化一个 cdev
6、device_add(&evdev->dev);///把初始化好的 evdev 添加到内核

在系统启动时系统会注册input事件驱动 evdev_handler,通过遍历系统中已经存在input设备,并与之进行匹配,匹配成功即条用connect函数
创建evdev设备,即input设备节点,初始化完成之后,上层应用程序通过evdev_fops对输入设备节点进行open/write/read/ioctrl等一系列操作,
从而完成input输入子系统的整个功能实现;
二、关键代码段
static struct input_handler evdev_handler = {
.event = evdev_event,
.events = evdev_events,
.connect = evdev_connect,
.disconnect = evdev_disconnect,
.legacy_minors = true,
.minor = EVDEV_MINOR_BASE,///次设备号从64开始
.name = "evdev",
.id_table = evdev_ids,
};
static int __init evdev_init(void)
{
return input_register_handler(&evdev_handler); ///注册 evdev_handler 这个input事件驱动
}
int input_register_handler(struct input_handler *handler)///把input 事件驱动注册到内核
{
struct input_dev *dev;
int error; error = mutex_lock_interruptible(&input_mutex);
if (error)
return error; INIT_LIST_HEAD(&handler->h_list);///初始化链表头,把链表的前和后都指向它自己 list_add_tail(&handler->node, &input_handler_list);///把 handler的 node 加到 input_handler_list这个双向链表,之后就可以通过这个链表访问所有的input_handler list_for_each_entry(dev, &input_dev_list, node)
input_attach_handler(dev, handler);////inout 设备和 input 事件进行匹配 input_wakeup_procfs_readers(); mutex_unlock(&input_mutex);
return ;
}
static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
{
const struct input_device_id *id;
int error; id = input_match_device(handler, dev);///input_dev 和 input_handler 通过id_table进行匹配
if (!id)
return -ENODEV; error = handler->connect(handler, dev, id);///如果返回id不为空就执行handler 的 connect ---> 调用 evdev.c 的 connect 函数
if (error && error != -ENODEV)
pr_err("failed to attach handler %s to device %s, error: %d\n",
handler->name, kobject_name(&dev->dev.kobj), error); return error;
}
/*
* Create new evdev device. Note that input core serializes calls
* to connect and disconnect.
*/
static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
const struct input_device_id *id)
{
struct evdev *evdev;
int minor;
int dev_no;
int error; minor = input_get_new_minor(EVDEV_MINOR_BASE, EVDEV_MINORS, true);//动态分配一个新的设备号minor
if (minor < ) {
error = minor;
pr_err("failed to reserve new minor: %d\n", error);
return error;
} evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);///初始化evdev ,为evdev分配空间
if (!evdev) {
error = -ENOMEM;
goto err_free_minor;
} INIT_LIST_HEAD(&evdev->client_list);///初始化队列
spin_lock_init(&evdev->client_lock);
mutex_init(&evdev->mutex);
init_waitqueue_head(&evdev->wait);///初始化等待队列
evdev->exist = true; dev_no = minor;
/* Normalize device number if it falls into legacy range */
if (dev_no < EVDEV_MINOR_BASE + EVDEV_MINORS)
dev_no -= EVDEV_MINOR_BASE;
dev_set_name(&evdev->dev, "event%d", dev_no);///给设备设置名字(event0、event1、...) evdev->handle.dev = input_get_device(dev);
evdev->handle.name = dev_name(&evdev->dev);
evdev->handle.handler = handler;
evdev->handle.private = evdev; evdev->dev.devt = MKDEV(INPUT_MAJOR, minor);////根据主设备号(主设备号都是13)和次设备号生成一个设备号(次设备号从64开始)
evdev->dev.class = &input_class;
evdev->dev.parent = &dev->dev;
evdev->dev.release = evdev_free;
device_initialize(&evdev->dev);///对设备进行初始化 error = input_register_handle(&evdev->handle);///注册 handle,handle 用来关联 input_dev 和 input_handler
if (error)
goto err_free_evdev; cdev_init(&evdev->cdev, &evdev_fops);//// 初始化一个 cdev
evdev->cdev.kobj.parent = &evdev->dev.kobj;
error = cdev_add(&evdev->cdev, evdev->dev.devt, );
if (error)
goto err_unregister_handle; error = device_add(&evdev->dev);///把初始化好的 evdev 添加到内核
if (error)
goto err_cleanup_evdev; return ; err_cleanup_evdev:
evdev_cleanup(evdev);
err_unregister_handle:
input_unregister_handle(&evdev->handle);
err_free_evdev:
put_device(&evdev->dev);
err_free_minor:
input_free_minor(minor);
return error;
}
如下图 ,在linux 系统上 /dev/input这个路径下可以看到已经注册好的input设备节点,input设备的主设备号都是13,其中
按键设备的次设备号从64~95,鼠标设备的次设备号从32~63。

Linux input子系统学习总结(二)----Input事件驱动的更多相关文章
- input子系统学习笔记六 按键驱动实例分析下【转】
转自:http://blog.chinaunix.net/uid-20776117-id-3212095.html 本文接着input子系统学习笔记五 按键驱动实例分析上接续分析这个按键驱动实例! i ...
- input子系统分析之二:数据结构
内核版本:3.9.5 1. input_dev,用来标识输入设备 struct input_dev { const char *name; const char *phys; const char * ...
- Linux时间子系统之(二):软件架构
专题文档汇总目录 Notes:从框架上讲解了时间子系统,从底向上包括CPU Local TImer.Global Counter.Clock Souce/Clock Events模块管理.Tick D ...
- Linux Input子系统浅析(二)-- 模拟tp上报键值【转】
转自:https://blog.csdn.net/xiaopangzi313/article/details/52383226 版权声明:本文为博主原创文章,未经博主允许不得转载. https://b ...
- linux输入子系统(6)-input子系统介绍及结构图
注:本系列转自: http://www.ourunix.org/post/290.html input子系统介绍 输入设备(如按键,键盘,触摸屏,鼠标,蜂鸣器等)是典型的字符设备,其一 ...
- linux 输入子系统(4)---- input子系统的初始化
Input子系统的初始化函数为input_init(),如下: static int __init input_init(void) { int err; input_init_abs_bypass( ...
- Linux input子系统学习总结(三)----Input设备驱动
Input 设备驱动 ---操作硬件获取硬件寄存器中设备输入的数据,并把数据交给核心层: 一 .设备驱动的注册步骤: 1.分配一个struct input_dev : struct ...
- Linux input子系统学习总结(一)---- 三个重要的结构体
一 . 总体架构 图 上层是图形界面和应用程序,通过监听设备节点,获取用户相应的输入事件,根据输入事件来做出相应的反应:eventX (X从0开始)表示 按键事件,mice 表示鼠标事件 Input ...
- Linux System Programming 学习笔记(二) 文件I/O
1.每个Linux进程都有一个最大打开文件数,默认情况下,最大值是1024 文件描述符不仅可以引用普通文件,也可以引用套接字socket,目录,管道(everything is a file) 默认情 ...
随机推荐
- Python基础教程【读书笔记】 - 2016/7/24
希望通过博客园持续的更新,分享和记录Python基础知识到高级应用的点点滴滴! 第九波:第9章 魔法方法.属性和迭代器 在Python中,有的名称会在前面和后面都加上两个下划线,这种写法很特别.已 ...
- IntelliJ IDEA以不同格式导出数据库的数据
在数据表内容上点击右键,弹出窗口中先选择Data Extractor SQL Inserts,二级菜单会列出导出数据的类型,这里选择SQL Inserts 然后选择Dump Data菜单中的To Fi ...
- bzoj2592: [Usaco2012 Feb]Symmetry
Description After taking a modern art class, Farmer John has become interested in finding geometric ...
- Android Please ensure that adb is correctly located at问题解决
转载于:http://breezylee.iteye.com/blog/2032588 遇到问题描述: 运行android程序控制台输出 [2012-07-18 16:18:26 - ] The co ...
- C# Winform中WndProc 函数作用
http://blog.csdn.net/xochenlin/article/details/4328954 C# Winform中WndProc 函数作用: 主要用在拦截并处理系统消息和自定义消息 ...
- 黄聪:深入理解PHP Opcode缓存原理
什么是opcode缓存? 当解释器完成对脚本代码的分析后,便将它们生成可以直接运行的中间代码,也称为操作码(Operate Code,opcode).Opcode cache的目地是避免重复编译,减少 ...
- C++库大全(转)
基础类1. Dinkumware C++ Library 参考站点:http://www.dinkumware.com P.J. Plauger编写的高品质的标准库.P.J. Plauger博士是Dr ...
- de.greenrobot.event.EventBusException: Subscriber class dji.midware.a.e already registered to event class
java.lang.RuntimeException: Unable to create application com.android.tools.fd.runtime.BootstrapAppli ...
- BestCoder Round #86 部分题解
Price List 题意: 有n件商品,每天只能买一件,并且会记录账本,问有多少次一定记多了? 题解: 就是求和,最后如果大于和就输出1,否则0. 代码: #include <bits/std ...
- 深入理解Javascript
http://www.cnblogs.com/TomXu/archive/2011/12/15/2288411.html 1.编写高质量JavaScript代码的基本要点 2.Javascript函数 ...