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事件驱动的更多相关文章

  1. input子系统学习笔记六 按键驱动实例分析下【转】

    转自:http://blog.chinaunix.net/uid-20776117-id-3212095.html 本文接着input子系统学习笔记五 按键驱动实例分析上接续分析这个按键驱动实例! i ...

  2. input子系统分析之二:数据结构

    内核版本:3.9.5 1. input_dev,用来标识输入设备 struct input_dev { const char *name; const char *phys; const char * ...

  3. Linux时间子系统之(二):软件架构

    专题文档汇总目录 Notes:从框架上讲解了时间子系统,从底向上包括CPU Local TImer.Global Counter.Clock Souce/Clock Events模块管理.Tick D ...

  4. Linux Input子系统浅析(二)-- 模拟tp上报键值【转】

    转自:https://blog.csdn.net/xiaopangzi313/article/details/52383226 版权声明:本文为博主原创文章,未经博主允许不得转载. https://b ...

  5. linux输入子系统(6)-input子系统介绍及结构图

    注:本系列转自: http://www.ourunix.org/post/290.html input子系统介绍         输入设备(如按键,键盘,触摸屏,鼠标,蜂鸣器等)是典型的字符设备,其一 ...

  6. linux 输入子系统(4)---- input子系统的初始化

    Input子系统的初始化函数为input_init(),如下: static int __init input_init(void) { int err; input_init_abs_bypass( ...

  7. Linux input子系统学习总结(三)----Input设备驱动

    Input 设备驱动 ---操作硬件获取硬件寄存器中设备输入的数据,并把数据交给核心层: 一 .设备驱动的注册步骤: 1.分配一个struct  input_dev :          struct ...

  8. Linux input子系统学习总结(一)---- 三个重要的结构体

    一 . 总体架构 图 上层是图形界面和应用程序,通过监听设备节点,获取用户相应的输入事件,根据输入事件来做出相应的反应:eventX (X从0开始)表示 按键事件,mice 表示鼠标事件 Input ...

  9. Linux System Programming 学习笔记(二) 文件I/O

    1.每个Linux进程都有一个最大打开文件数,默认情况下,最大值是1024 文件描述符不仅可以引用普通文件,也可以引用套接字socket,目录,管道(everything is a file) 默认情 ...

随机推荐

  1. LintCode "Subarray Sum II"

    Sliding window doesn't work. So it is a typical partial_sum base solution. As below. However if you ...

  2. bzoj4716 假摔

    Description [题目背景] 小Q最近喜欢上了一款游戏,名为<舰队connection>,在游戏中,小Q指挥强大的舰队南征北战,从而成为了一名 dalao.在游戏关卡的攻略中,可能 ...

  3. 已跳过 'cache' -- 节点处于冲突状态

    svn resolved ./cache ./cache 为冲突文件路径“cache”的冲突状态已解决

  4. ultraedit15.00.0.1046注册码

      ultraedit注册码,版本:15.00.0.1043·········· 用户名 MAYBELOVE 注册码 LFKKM-KIMMX-OSFEB-PMISO-ELILS-IIIHO-KKHLR ...

  5. 【Linux】之shell特殊变量整理

    目录 1. 特殊变量列表 2. 特殊说明 在shell中变量名只能包含数字.字母和下划线,因为某些包含其他字符的变量有特殊含义,这样的变量被称为特殊变量. 例如,$ 表示当前Shell进程的ID,即p ...

  6. Android权限之sharedUserId和签名

    转自:http://blog.csdn.net/hmg25/article/details/6447067 最近在做个东西,巧合碰到了sharedUserId的问题,所以收集了一些资料,存存档备份. ...

  7. 纯c++实现之滚动窗口

    别在MFC了,先分析下,上图 我们以左上角为坐标原点,用position_width和position_height来保存当前显示坐标. 根据msdn说明,滚动条默认情况下的值在0~100之间. 根据 ...

  8. CF 445A 简单DP

    今天早上找一道题的bug,还是找不出来,下午刷了几道水题,晚上准备回家的事, 然后本来想打CF的,一看,数学场,不打了. 这道题的题意: 给出一个序列,每次你可以从这个序列里面选择一个数ak,删除,然 ...

  9. GDI+ 中发生一般性错误。

    GDI+ 中发生一般性错误. “/wechat”应用程序中的服务器错误. GDI+ 中发生一般性错误. 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及 ...

  10. pragma mark - 合成图

    #pragma mark - 合成图 - (UIImage *)getShareImageShell:(UIImage *)shareImage { if (shareImage) { CGSize ...