输入(Input)子系统是分层架构的,总共分为 3 层,从上到下分别是:事件处理层(Event Handler)、输入子系统核心层(Input Core)、硬件驱动层(Input Driver) 。
驱动根据CORE提供的接口,向上报告发生的按键动作。然后CORE根据驱动的类型,分派这个报告给对应的事件处理层进行处理。事件处理层把数据变化反应到设备模型的文件中(事件缓冲区)。并通知在这些设备模型文件上等待的进程。
input子系统框架:
(1) 硬件驱动层负责操作具体的硬件设备,这层的代码是针对具体的驱动程序的,比如你的设备是触摸输入设备,还是鼠标输入设备,还是键盘输入设备,这些不同的设备,自然有不同的硬件操作,驱动工程师往往只需要完成这层的代码编写。
(2) 输入子系统核心层是链接其他两层之间的纽带与桥梁,向下提供硬件驱动层的接口,向上提供事件处理层的接口。
(3) 事件处理厂负责与用户程序打交道,将硬件驱动层传来的事件报告给用户程序。
各层之间通信的基本单位就是事件,任何一个输入设备的动作都可以抽象成一种事件,如键盘的按下,触摸屏的按下,鼠标的移动等。事件有三种属性:类型(type),编码(code),值(value), Input 子系统支持的所有事件都定义在 input.h中,包括所有支持的类型,所属类型支持的编码等。事件传送的方向是 硬件驱动层-->子系统核心-->事件处理层-->用户空间。
一、主要input通用数据结构
1. 输入设备 input_dev,这是input设备基本的设备结构,每个input驱动程序中都必须分配初始化这样一个结构,成员比较多:
struct input_dev {
const char *name;
const char *phys;
const char *uniq;
struct input_id id; //与input_handler匹配用的id,包括
/*
struct input_id {
__u16 bustype; //总线类型
__u16 vendor; //生产厂商
__u16 product; //产品类型
__u16 version; //版本
}; */
/*
#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 //声音输出,如蜂鸣器
#define EV_REP 0x14 //允许按键自重复
#define EV_FF 0x15 //力反馈
#define EV_PWR 0x16 //电源管理事件
*/
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)];
unsigned long sndbit[BITS_TO_LONGS(SND_CNT)];
unsigned long ffbit[BITS_TO_LONGS(FF_CNT)];
unsigned long swbit[BITS_TO_LONGS(SW_CNT)];
unsigned int keycodemax;
unsigned int keycodesize;
void *keycode;
int (*setkeycode)(struct input_dev *dev, int scancode, int keycode);
int (*getkeycode)(struct input_dev *dev, int scancode, int *keycode);
struct ff_device *ff;
unsigned int repeat_key; //最近一次的按键值
struct timer_list timer;
int sync;
int abs[ABS_MAX + 1];
int rep[REP_MAX + 1];
unsigned long key[BITS_TO_LONGS(KEY_CNT)]; //反应设备当前的按键状态
unsigned long led[BITS_TO_LONGS(LED_CNT)]; //反应设备当前的led状态
unsigned long snd[BITS_TO_LONGS(SND_CNT)]; //反应设备当前的声音输入状态
unsigned long sw[BITS_TO_LONGS(SW_CNT)]; //反应设备当前的开关状态
int absmax[ABS_MAX + 1]; //来自绝对坐标事件的最大键值
int absmin[ABS_MAX + 1]; //来自绝对坐标事件的最小键值
int absfuzz[ABS_MAX + 1];
int absflat[ABS_MAX + 1];
int (*open)(struct input_dev *dev); //第一次打开设备时调用,初始化设备用
void (*close)(struct input_dev *dev); //最后一个应用程序释放设备时用,关闭设备
int (*flush)(struct input_dev *dev, struct file *file);
/*用于处理传递给设备的事件,如LED事件和声音事件*/
int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value);
struct input_handle *grab; //当前占有该设备的input_handle
spinlock_t event_lock;
struct mutex mutex;
unsigned int users; //打开该设备的用户数量(input handlers)
int going_away;
struct device dev;
struct list_head h_list;//该链表头用于链接此设备所关联的input_handle
struct list_head node; //用于将此设备链接到input_dev_list(链接了所有注册到内核的事件处理器)
}
2. input_handler 这是事件处理器的数据结构,代表一个事件处理器。
struct input_handler {
void *private;
/* 当事件处理器接收到来自Input设备传来的事件时调用的处理函数,
event用于处理事件 */
void (*event)(struct input_handle *handle,
unsigned int type, unsigned int code, int value);
/* connect用于建立intput_handler和input_dev的联系,
当一个Input设备注册到内核的时候被调用,将输入设备与事件处理器联结起来 */
int (*connect)(struct input_handler *handler, struct input_dev *dev,
const struct input_device_id *id);
/* disconnect用于解除handler和device的联系 */
void (*disconnect)(struct input_handle *handle);
void (*start)(struct input_handle *handle);
<span style="color:#ff0000;">/* 事件处理器作为一种设备,具备文件读写接口 */
const struct file_operations *fops;
</span> int minor; //次设备号
const char *name; //设备名
const struct input_device_id *id_table;//用于和device匹配 ,这个是事件处理器所支持的input设备
const struct input_device_id *blacklist;//匹配黑名单,这个是事件处理器应该忽略的input设备
struct list_head h_list;//这个链表用来链接他所支持的input_handle结构,input_dev与input_handler配对之后就会生成一个input_handle结构
struct list_head node; //链接到input_handler_list,这个链表链接了所有注册到内核的事件处理器
};
- 3.input_handle 结构体代表一个成功配对的input_dev和input_handler。
-
4.三个数据结构之间的关系struct input_handle { /* 每个配对的事件处理器都会分配一个对应的设备结构,如evdev事件处理器的evdev结构, 注意这个结构与设备驱动层的input_dev不同,初始化handle时,保存到这里。 */ void *private; /* 打开标志,每个input_handle 打开后才能操作, 这个一般通过事件处理器的open方法间接设置 */ int open; const char *name; /* 指向Input_dev结构实体 */ struct input_dev *dev; /* 指向Input_Hander结构实体 */ struct input_handler *handler; //input_handle通过d_node连接到了input_dev上的h_list链表上 struct list_head d_node; //input_handle通过h_node连接到了input_handler的h_list链表上 struct list_head h_node; };
- input_dev: 是硬件驱动层,代表一个input设备。
input_handler: 是事件处理层,代表一个事件处理器。
input_handle: 属于核心层,代表一个配对的input设备与input事件处理器。
input_dev 通过全局的input_dev_list链接在一起。设备注册的时候实现这个操作。
input_handler 通过全局的input_handler_list链接在一起。事件处理器注册的时候实现这个操作(事件处理器一般内核自带,一般不需要我们来写)
input_hande 没有一个全局的链表,它注册的时候将自己分别挂在了input_dev 和 input_handler 的h_list上了。通过input_dev 和input_handler就可以找到input_handle在设备注册和事件处理器,注册的时候都要进行配对工作,配对后就会实现链接。通过input_handle也可以找到input_dev和input_handler。 - 我们可以看到,input_device和input_handler中都有一个h_list,而input_handle拥有指向input_dev和input_handler的指针,也就是说input_handle是用来关联input_dev和input_handler的。
那么为什么一个input_device和input_handler中拥有的是h_list而不是一个handle呢?因为一个device可能对应多个handler,而一个handler也不能只处理一个device,比如说一个鼠标,它可以对应even handler,也可以对应mouse handler,因此当其注册时与系统中的handler进行匹配,就有可能产生两个实例,一个是evdev,另一个是mousedev,而任何一个实例中都只有一个handle。至于以何种方式来传递事件,就由用户程序打开哪个实例来决定。后面一个情况很容易理解,一个事件驱动不能只为一个甚至一种设备服务,系统中可能有多种设备都能使用这类handler,比如event handler就可以匹配所有的设备。在input子系统中,有8种事件驱动,每种事件驱动最多可以对应32个设备,因此dev实例总数最多可以达到256个。
- 二、输入子系统驱动层分析(以tps6507x为例)
1.platform device的注册
2.platform driver注册
static struct platform_driver tps6507x_ts_driver = {
.driver = {
.name = "tps6507x-ts",
.owner = THIS_MODULE,
},
.probe = tps6507x_ts_probe,
.remove = __devexit_p(tps6507x_ts_remove),
};
// 平台设备注册
static int __init tps6507x_ts_init(void)
{
return platform_driver_register(&tps6507x_ts_driver);
}
//tps6507x触摸屏封装的设备结构
struct tps6507x_ts {
struct input_dev *input_dev; // input设备
struct device *dev; //
char phys[32];
struct delayed_work work; // 延迟工作
unsigned polling; /* polling is active */
struct ts_event tc; // 触摸事件
struct tps6507x_dev *mfd;
u16 model;
unsigned pendown; // 按下标志
int irq; // 中断号
void (*clear_penirq)(void); // 清除中断函数
unsigned long poll_period; /* ms */
u16 min_pressure;
int vref; /* non-zero to leave vref on */
};
//与platform device匹配成功后会调用tps6507x_ts_probe
static int tps6507x_ts_probe(struct platform_device *pdev)
{
int error;
struct tps6507x_ts *tsc;
struct tps6507x_dev *tps6507x_dev = dev_get_drvdata(pdev->dev.parent);
struct touchscreen_init_data *init_data;
struct input_dev *input_dev;
struct tps6507x_board *tps_board;
int schd;
//找到tps6507x platform data
tps_board = (struct tps6507x_board *)tps6507x_dev->dev->platform_data;
if (!tps_board) {
dev_err(tps6507x_dev->dev,"Could not find tps6507x platform datan");
return -EIO;
}
//得到触摸屏的一些初始化信息,如厂商信息等
init_data = tps_board->tps6507x_ts_init_data;
//分配tps6507x_ts结构体
tsc = kzalloc(sizeof(struct tps6507x_ts), GFP_KERNEL);
if (!tsc) {
dev_err(tps6507x_dev->dev, "failed to allocate driver datan");
error = -ENOMEM;
goto err0;
}
tps6507x_dev->ts = tsc;
tsc->mfd = tps6507x_dev;
tsc->dev = tps6507x_dev->dev;
//分配一个input_dev接口,并初始化一些基本的成员
input_dev = input_allocate_device();
if (!input_dev) {
dev_err(tsc->dev, "Failed to allocate input device.n");
error = -ENOMEM;
goto err1;
}
//设备支持的事件类型为按键事件和绝对坐标事件
input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
//按键事件支持的子事件类型
input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
//MAX_10BIT=0x3FF,设置ABS_X编码值范围为0-0x3ff,因为mini2440的AD转换出的数据最大为10位,所以不会超过0x3ff
input_set_abs_params(input_dev, ABS_X, 0, MAX_10BIT, 0, 0);//这个是设置ad转换的x坐标
input_set_abs_params(input_dev, ABS_Y, 0, MAX_10BIT, 0, 0);//这个是设置ad转换的y坐标
input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_10BIT, 0, 0);//这个是设置触摸屏是否按下的标志
input_dev->name = "TPS6507x Touchscreen";
input_dev->id.bustype = BUS_I2C; //总线类型是I2C
input_dev->dev.parent = tsc->dev; //父设备
snprintf(tsc->phys, sizeof(tsc->phys),"%s/input0", dev_name(tsc->dev));
input_dev->phys = tsc->phys;
dev_dbg(tsc->dev, "device: %sn", input_dev->phys);
input_set_drvdata(input_dev, tsc); //保存tsc结构到input_dev中
tsc->input_dev = input_dev; //tsc结构指向初始化过的input_dev设备
INIT_DELAYED_WORK(&tsc->work, tps6507x_ts_handler);
tsc->wq = create_workqueue("TPS6507x Touchscreen");
if (init_data) {
tsc->poll_period = init_data->poll_period;//触摸屏采样时间30ms
tsc->vref = init_data->vref;//turn off vref when not using A/D
tsc->min_pressure = init_data->min_pressure;//触摸屏最小压力0x30
input_dev->id.vendor = init_data->vendor;//0
input_dev->id.product = init_data->product;//65070
input_dev->id.version = init_data->version;//0x100
} else {
tsc->poll_period = TSC_DEFAULT_POLL_PERIOD;
tsc->min_pressure = TPS_DEFAULT_MIN_PRESSURE;
}
//设置设备standby状态
error = tps6507x_adc_standby(tsc);
if (error)
goto err2;
//注册一个input设备
error = input_register_device(input_dev);
if (error)
goto err2;
schd = queue_delayed_work(tsc->wq, &tsc->work,msecs_to_jiffies(tsc->poll_period));
if (schd)
tsc->polling = 1;
else {
tsc->polling = 0;
dev_err(tsc->dev, "schedule failed");
goto err2;
}
platform_set_drvdata(pdev, tps6507x_dev);
return 0;
err2:
cancel_delayed_work_sync(&tsc->work);
destroy_workqueue(tsc->wq);
input_free_device(input_dev);
err1:
kfree(tsc);
tps6507x_dev->ts = NULL;
err0:
return error;
}
int input_register_device(struct input_dev *dev)
{
//这个原子变量,代表总共注册的input设备,每注册一个加1,因为是静态变量,所以每次调用都不会清零的
static atomic_t input_no = ATOMIC_INIT(0);
struct input_handler *handler;
const char *path;
int error;
//EN_SYN这个是设备都要支持的事件类型,所以要设置
__set_bit(EV_SYN, dev->evbit);
/* KEY_RESERVED is not supposed to be transmitted to userspace. */
__clear_bit(KEY_RESERVED, dev->keybit);
/* Make sure that bitmasks not mentioned in dev->evbit are clean. */
input_cleanse_bitmasks(dev);
//这个内核定时器是为了重复按键而设置的
//rep主要是处理重复按键,如果没有定义dev->rep[REP_DELAY]和dev->rep[REP_PERIOD],
//则将其赋值为默认值。dev->rep[REP_DELAY]是指第一次按下多久算一次,这里是250ms,
//dev->rep[REP_PERIOD]指如果按键没有被抬起,每33ms算一次。
init_timer(&dev->timer);
if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
dev->timer.data = (long) dev;
dev->timer.function = input_repeat_key;
dev->rep[REP_DELAY] = 250;
dev->rep[REP_PERIOD] = 33;
//如果没有定义有关重复按键的相关值,就用内核默认的
}
/*如果dev没有定义getkeycode和setkeycode,则赋默认值。他们的作用一个是获得键的扫描码,一个是设置键的扫描码*/
if (!dev->getkeycode && !dev->getkeycode_new)
dev->getkeycode_new = input_default_getkeycode;
if (!dev->setkeycode && !dev->setkeycode_new)
dev->setkeycode_new = input_default_setkeycode;
//设置input_dev中device的名字,这个名字会在/class/input中出现
dev_set_name(&dev->dev, "input%ld",(unsigned long) atomic_inc_return(&input_no) - 1);
error = device_add(&dev->dev);//添加input设备,注册到linux设备模型中,生成一系列的sys相关文件,udev会根据dev文件生成设备节点
if (error)
return error;
path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
printk(KERN_INFO "input: %s as %sn",dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
kfree(path);
error = mutex_lock_interruptible(&input_mutex);
if (error) {
device_del(&dev->dev);
return error;
}
list_add_tail(&dev->node, &input_dev_list);//将新分配的input设备连接到input_dev_list链表上
list_for_each_entry(handler, &input_handler_list, node)//遍历input_handler_list链表,配对input_dev和input_handler
input_attach_handler(dev, handler);
input_wakeup_procfs_readers();//与proc文件系统有关
mutex_unlock(&input_mutex);
return 0;
}
- 三、输入子系统核心分析
static const struct file_operations input_fops = {
.owner = THIS_MODULE,
.open = input_open_file,
.llseek = noop_llseek,
};
struct class input_class = {
.name = "input",
.devnode = input_devnode,
};
static int __init input_init(void)
{
int err;
//向内核注册一个类,用于linux设备模型。注册后会在/sys/class下面出现input目录
err = class_register(&input_class);
if (err) {
printk(KERN_ERR "input: unable to register input_dev classn");
return err;
}
//和proc文件系统有关,暂时不管
err = input_proc_init();
if (err)
goto fail1;
//注册字符设备,以主设备号INPUT_MAJOR,次设备号0-255,注册266个设备,说明input设备最大只能有255个
err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
if (err) {
printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
goto fail2;
}
return 0;
fail2: input_proc_exit();
fail1: class_unregister(&input_class);
return err;
}
- 四. 事件处理层分析(以evdev事件处理器为例)
- 1.主要数据结构
- (1) evdev设备结构
struct evdev {
int exist;
int open; //打开标志
int minor; //次设备号
struct input_handle handle; //关联的input_handle
wait_queue_head_t wait; //等待队列,当进程读取设备,而没有事件产生的时候,进程就会睡在其上面
struct evdev_client *grab; //强制绑定的evdev_client结构,这个结构后面再分析
struct list_head client_list; //evdev_client 链表,这说明一个evdev设备可以处理多个evdev_client,可以有多个进程访问evdev设备
spinlock_t client_lock; /* protects client_list */
struct mutex mutex;
struct device dev; //device结构,说明这是一个设备结构
};
evdev结构体在配对成功的时候生成,由handler->connect生成,对应设备文件为/class/input/event(n),如触摸屏驱动的event0,这个设备是用户空间要访问的设备,可以理解它是一个虚拟设备,因为没有对应的硬件,但是通过handle->dev 就可以找到input_dev结构,而它对应着触摸屏,设备文件为/class/input/input0。这个设备结构生成之后保存在evdev_table中,索引值是minor。
- (2)evdev用户端结构
struct evdev_client {
//这个是一个input_event数据结构的数组,input_event代表一个事件,基本成员:类型(type),编码(code),值(value)
struct input_event buffer[EVDEV_BUFFER_SIZE];
int head; //针对buffer数组的索引
int tail; //针对buffer数组的索引,当head与tail相等的时候,说明没有事件
spinlock_t buffer_lock; /* protects access to buffer, head and tail */
struct fasync_struct *fasync; //异步通知函数
struct evdev *evdev; //evdev设备
struct list_head node; //evdev_client 链表项
};
- 这个结构在进程打开event0设备的时候调用evdev的open方法,在open中创建这个结构,并初始化。在关闭设备文件的时候释放这个结构。
- (3)input_event结构
-
struct input_event { struct timeval time; //事件发生的时间 __u16 type; //事件类型 __u16 code; //子事件 __s32 value; //事件的value };
- 2.事件处理层与用户程序和输入子系统核心打交道,是他们两层的桥梁。一般内核有好几个事件处理器,像evdev mousedev jotdev。evdev事件处理器可以处理所有的事件,触摸屏驱动就是用的这个,所以下面分析这个事件处理器的实现。它也是作为模块注册到内核中的,首先分析它的模块初始化函数。
// Input_dev成员 static const struct file_operations evdev_fops = { .owner = THIS_MODULE, .read = evdev_read, .write = evdev_write, .poll = evdev_poll, .open = evdev_open, .release = evdev_release, .unlocked_ioctl = evdev_ioctl, #ifdef CONFIG_COMPAT .compat_ioctl = evdev_ioctl_compat, #endif .fasync = evdev_fasync, .flush = evdev_flush, .llseek = no_llseek, }; // Input_dev成员 static const struct input_device_id evdev_ids[] = { { .driver_info = 1 }, //适合所有的类型的设备 { }, /* Terminating zero entry */ }; // evdev处理函数 static struct input_handler evdev_handler = { .event = evdev_event, //向系统报告input事件,系统通过read方法读取 .connect = evdev_connect, //和input_dev匹配后调用connect构建 .disconnect = evdev_disconnect, .fops = &evdev_fops, //event设备文件的操作方法 .minor = EVDEV_MINOR_BASE, //次设备号基准值 .name = "evdev", .id_table = evdev_ids, //匹配规则 }; static int __init evdev_init(void) { //模块初始化函数就调用一个注册handler函数,将evdev_handler注册到系统中。 return input_register_handler(&evdev_handler); } int input_register_handler(struct input_handler *handler) { struct input_dev *dev; int retval; retval = mutex_lock_interruptible(&input_mutex); if (retval) return retval; INIT_LIST_HEAD(&handler->h_list); if (handler->fops != NULL) { if (input_table[handler->minor >> 5]) { retval = -EBUSY; goto out; } input_table[handler->minor >> 5] = handler;//添加到全局数组中 //input_table,每个注册的handler都会将自己保存到这里,索引值为handler->minor右移5为,也就是除以32 //为什么会这样呢,因为每个handler都会处理最大32个input_dev,所以要以minor的32为倍数对齐,这个minor是传进来的handler的MINOR_BASE //每一个handler都有一个这一个MINOR_BASE,以evdev为例,EVDEV_MINOR_BASE = 64,可以看出系统总共可以注册8个handler } //连接到input_handler_list链表中 list_add_tail(&handler->node, &input_handler_list); //配对,遍历input_dev_list链表中的input_dev设备,与对应的input_handler结构配对,和注册input_dev过程一样的 list_for_each_entry(dev, &input_dev_list, node) input_attach_handler(dev, handler); input_wakeup_procfs_readers();//与proc文件系统有关 out: mutex_unlock(&input_mutex); return retval; } 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);//这个是主要的配对函数,主要比较id中的各项 if (!id) return -ENODEV; //配对成功调用handler的connect函数,这个函数在事件处理器中定义,主要生成一个input_handle结构,并初始化,还生成一个事件处理器相关的设备结构, error = handler->connect(handler, dev, id);//调用evdev_connect if (error && error != -ENODEV) printk(KERN_ERR"input: failed to attach handler %s to device %s, ""error: %dn",handler->name, kobject_name(&dev->dev.kobj), error); return error; } static const struct input_device_id *input_match_device(struct input_handler *handler,struct input_dev *dev) { const struct input_device_id *id; int i; for (id = handler->id_table; id->flags || id->driver_info; id++) {//id->driver_info=1,表示可以配对所有 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS) if (id->bustype != dev->id.bustype) continue; if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR) if (id->vendor != dev->id.vendor) continue; if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT) if (id->product != dev->id.product) continue; if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION) if (id->version != dev->id.version) continue; //配对成功,进入下面的宏 MATCH_BIT(evbit, EV_MAX); MATCH_BIT(keybit, KEY_MAX); MATCH_BIT(relbit, REL_MAX); MATCH_BIT(absbit, ABS_MAX); MATCH_BIT(mscbit, MSC_MAX); MATCH_BIT(ledbit, LED_MAX); MATCH_BIT(sndbit, SND_MAX); MATCH_BIT(ffbit, FF_MAX); MATCH_BIT(swbit, SW_MAX); //没有match函数 if (!handler->match || handler->match(handler, dev)) return id; } return NULL; }
如果匹配上了就会创建一个evdev,它里边封装了一个handle,会把input_dev和input_handler关联到一起。关系如下:
- 2.事件处理层与用户程序和输入子系统核心打交道,是他们两层的桥梁。一般内核有好几个事件处理器,像evdev mousedev jotdev。evdev事件处理器可以处理所有的事件,触摸屏驱动就是用的这个,所以下面分析这个事件处理器的实现。它也是作为模块注册到内核中的,首先分析它的模块初始化函数。
static int evdev_connect(struct input_handler *handler, struct input_dev *dev,const struct input_device_id *id)
{
struct evdev *evdev;
int minor;
int error;
//EVDEV_MINORS为32,说明evdev这个handler可以同时有32个输入设备和他配对,evdev_table中以minor(非次设备号,但是有一个换算关系)存放evdev结构体
for (minor = 0; minor < EVDEV_MINORS; minor++)
if (!evdev_table[minor])
break;
if (minor == EVDEV_MINORS) {//这个说明32个位置全都被占用了,连接失败
printk(KERN_ERR "evdev: no more free evdev devicesn");
return -ENFILE;
}
//分配一个evdev结构体,这个结构体是evdev事件处理器特有的
evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
if (!evdev)
return -ENOMEM;
//初始化结构体的一些成员
INIT_LIST_HEAD(&evdev->client_list);
spin_lock_init(&evdev->client_lock);
mutex_init(&evdev->mutex);
init_waitqueue_head(&evdev->wait);
//这个是设置evdev中device的名字,他将出现在/class/input中。
//前面也有一个device是input_dev的,名字是input(n),注意与他的不同
//这个结构是配对后的虚拟设备结构,没有对应的硬件,但是通过它可以找到相关的硬件
dev_set_name(&evdev->dev, "event%d", minor);
evdev->exist = true;
evdev->minor = minor;
//因为evdev中包含handle了,所以初始化它就可以了,这样就连接了input_handler与input_dev
evdev->handle.dev = input_get_device(dev);
evdev->handle.name = dev_name(&evdev->dev);
evdev->handle.handler = handler;
evdev->handle.private = evdev;
//注意:这个minor不是真正的次设备号,还要加上EVDEV_MINOR_BASE
evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
evdev->dev.class = &input_class;
evdev->dev.parent = &dev->dev; //配对生成的device,父设备是与他相关连的input_dev
evdev->dev.release = evdev_free;
device_initialize(&evdev->dev); //做一些初始化device结构
error = input_register_handle(&evdev->handle);// 注册一个input_handle结构体
if (error)
goto err_free_evdev;
//这个函数只做了一件事,就是把evdev结构保存到evdev_table中,这个数组也minor为索引,evdev_table[evdev->minor] = evdev;
error = evdev_install_chrdev(evdev);
if (error)
goto err_unregister_handle;
error = device_add(&evdev->dev);//注册到linux设备模型中,生成一系列的sys相关文件,udev会根据dev文件生成设备节点
if (error)
goto err_cleanup_evdev;
return 0;
err_cleanup_evdev:
evdev_cleanup(evdev);
err_unregister_handle:
input_unregister_handle(&evdev->handle);
err_free_evdev:
put_device(&evdev->dev);
return error;
}
//这个函数基本没做什么事,就是把一个handle结构体通过d_node链表项,
//分别链接到input_dev的h_list,input_handler的h_list上。
//以后通过这个h_list就可以遍历相关的input_handle了。
int input_register_handle(struct input_handle *handle)
{
struct input_handler *handler = handle->handler;
struct input_dev *dev = handle->dev;
int error;
error = mutex_lock_interruptible(&dev->mutex);
if (error)
return error;
list_add_tail_rcu(&handle->d_node, &dev->h_list); //将handle的d_node,链接到其相关的input_dev的h_list链表中
mutex_unlock(&dev->mutex);
list_add_tail(&handle->h_node, &handler->h_list); //将handle的h_node,链接到其相关的input_handler的h_list链表中
if (handler->start)//字段为空
handler->start(handle);
return 0;
}
- 3.evdev设备结点的open()操作
- 我们知道.对主设备号为INPUT_MAJOR的设备节点进行操作,会将操作集转换成handler的操作集.在evdev中,这个操作集就是evdev_fops.对应的open函数如下示:
static int evdev_open(struct inode *inode, struct file *file)
{
struct evdev *evdev;
struct evdev_client *client;
int i = iminor(inode) - EVDEV_MINOR_BASE;//就得到了在evdev_table[ ]中的序号
unsigned int bufsize;
int error;
if (i >= EVDEV_MINORS)
return -ENODEV;
error = mutex_lock_interruptible(&evdev_table_mutex);
if (error)
return error;
evdev = evdev_table[i];//将数组中对应的evdev取出.
if (evdev)
get_device(&evdev->dev);//递增devdev中device的引用计数.
mutex_unlock(&evdev_table_mutex);
if (!evdev)
return -ENODEV;
//evdev_client的buffer大小
bufsize = evdev_compute_buffer_size(evdev->handle.dev);
//打开的时候创建一个evdev_client
client = kzalloc(sizeof(struct evdev_client) +bufsize * sizeof(struct input_event),GFP_KERNEL);
if (!client) {
error = -ENOMEM;
goto err_put_evdev;
}
client->bufsize = bufsize;//buffer size
spin_lock_init(&client->buffer_lock);
client->evdev = evdev;//指向evdev结构,将evdev和client绑定到一起
evdev_attach_client(evdev, client);
error = evdev_open_device(evdev);//调用打开真正的底层设备函数
if (error)
goto err_free_client;
file->private_data = client;//将file->private_data指向刚刚建的client,后边会用到的
nonseekable_open(inode, file);
return 0;
err_free_client:
evdev_detach_client(evdev, client);
kfree(client);
err_put_evdev:
put_device(&evdev->dev);
return error;
}
static int evdev_open_device(struct evdev *evdev)
{
int retval;
retval = mutex_lock_interruptible(&evdev->mutex);
if (retval)
return retval;
if (!evdev->exist)/*如果设备不存在,返回错误*/
retval = -ENODEV;
else if (!evdev->open++) {//递增打开计数
retval = input_open_device(&evdev->handle);//如果是被第一次打开,则调用input_open_device
if (retval)
evdev->open--;
}
mutex_unlock(&evdev->mutex);
return retval;
}
int input_open_device(struct input_handle *handle)
{
struct input_dev *dev = handle->dev;//根据input_handle找到对应的input_dev设备
int retval;
retval = mutex_lock_interruptible(&dev->mutex);
if (retval)
return retval;
if (dev->going_away) {
retval = -ENODEV;
goto out;
}
handle->open++;//递增handle的打开计数
if (!dev->users++ && dev->open)//如果是第一次打开.则调用input device的open()函数
retval = dev->open(dev);
if (retval) {
dev->users--;
if (!--handle->open) {
synchronize_rcu();
}
}
out:
mutex_unlock(&dev->mutex);
return retval;
}
- 4.用户进程读取event的底层实现
static ssize_t evdev_read(struct file *file, char __user *buffer,size_t count, loff_t *ppos)
{
struct evdev_client *client = file->private_data;//就是刚才在open函数中保存的evdev_client
struct evdev *evdev = client->evdev;
struct input_event event;
int retval;
if (count < input_event_size())//count小于input_event结构的size,则返回
return -EINVAL;
//如果client的环形缓冲区中没有数据并且是非阻塞的,那么返回-EAGAIN,也就是try again
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);
if (retval)
return retval;
if (!evdev->exist)
return -ENODEV;
//如果获得了数据则取出来,调用evdev_fetch_next_event
while (retval + input_event_size() <= count && evdev_fetch_next_event(client, &event)) {
if (input_event_to_user(buffer + retval, &event))//input_event_to_user调用copy_to_user传入用户程序中,这样读取完成
return -EFAULT;
retval += input_event_size();
}
return retval;
}
static int evdev_fetch_next_event(struct evdev_client *client, struct input_event *event)
{
int have_event;
spin_lock_irq(&client->buffer_lock);
/*先判断一下是否有数据*/
have_event = client->head != client->tail;
/*如果有就从环形缓冲区的取出来,记得是从head存储,tail取出*/
if (have_event) {
*event = client->buffer[client->tail++];
client->tail &= EVDEV_BUFFER_SIZE - 1;
}
spin_unlock_irq(&client->buffer_lock);
return have_event;
}
int input_event_to_user(char __user *buffer, const struct input_event *event)
{
/*如果设置了标志INPUT_COMPAT_TEST就将事件event包装成结构体compat_event*/
if (INPUT_COMPAT_TEST) {
struct input_event_compat compat_event;
compat_event.time.tv_sec = event->time.tv_sec;
compat_event.time.tv_usec = event->time.tv_usec;
compat_event.type = event->type;
compat_event.code = event->code;
compat_event.value = event->value;
/*将包装成的compat_event拷贝到用户空间*/
if (copy_to_user(buffer, &compat_event, sizeof(struct input_event_compat)))
return -EFAULT;
} else {
/*否则,将event拷贝到用户空间*/
if (copy_to_user(buffer, event, sizeof(struct input_event)))
return -EFAULT;
}
return 0;
}
- 五. 事件传递过程
- 1. 事件产生
- 当按下触摸屏时,进入触摸屏按下中断,开始ad转换,ad转换完成进入ad完成中断,在这个终端中将事件发送出去,调用:
- input_report_abs(dev, ABS_X, xp);
- input_report_abs(dev, ABS_Y, yp);
- 这两个函数调用了 input_event(dev, EV_ABS, code, value) 所有的事件报告函数都调用这个函数。
- 2. 事件报告
- 1)input_event 函数分析,这个函数定义在input.c中
void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) { unsigned long flags; //判断是否支持此种事件类型和事件类型中的编码类型 if (is_event_supported(type, dev->evbit, EV_MAX)) { spin_lock_irqsave(&dev->event_lock, flags); //对系统随机熵池有贡献,因为这个也是一个随机过程 add_input_randomness(type, code, value); //这个函数是事件处理的关键函数 input_handle_event(dev, type, code, value); spin_unlock_irqrestore(&dev->event_lock, flags); } }
<span style="font-size:18px;">2)input_handle_event 函数分析,这个函数定义在input.c中</span>
这个函数主要是根据事件类型的不同,做相应的处理。这里之关心EV_KEY类型,其他函数和事件传递关系不大,只要关心disposition这个是事件处理的方式,默认的是INPUT_IGNORE_EVENT,忽略这个事件,如果是INPUT_PASS_TO_HANDLERS则是传递给事件处理器,如果是INPUT_PASS_TO_DEVICE,则是传递给设备处理,触摸屏驱动没有定义这个。下面分析input_pass_event函数。static void input_handle_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) { int disposition = INPUT_IGNORE_EVENT; //默认的操作:忽略 switch (type) { ...... case EV_KEY: 检查按键是否为驱动所支持,只有之前注册过的按键才会继续传递;检查报告的按键状态是否和上次相同,如果连续多次报告按键按下,则只处理第一次 if (is_event_supported(code, dev->keybit, KEY_MAX) && !!test_bit(code, dev->key) != value) { if (value != 2) {//如果不是连击事件 __change_bit(code, dev->key);//翻转按键的当前状态(按下和释放) if (value)//如果是按下,则开始连击计时 input_start_autorepeat(dev, code); else input_stop_autorepeat(dev); } disposition = INPUT_PASS_TO_HANDLERS;//标记消息传递方向 } break; ...... if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN) dev->sync = 0; if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event) dev->event(dev, type, code, value); if (disposition & INPUT_PASS_TO_HANDLERS) input_pass_event(dev, type, code, value); }
static void input_pass_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) { struct input_handle *handle; rcu_read_lock(); //获取独占设备的handle的指针。如果有独占设备的handle,则仅仅将事件传给独占的handle对应的handler handle = rcu_dereference(dev->grab); if (handle) //这里直接调用了handler事件驱动对应的XX_event函数,这个XX_event函数把事件数据包传递给了handler,当应用程序使用XX_read时就可以读取到这些数据包 handle->handler->event(handle, type, code, value); else //如果没有绑定,则遍历dev的h_list链表,寻找handle,如果handle已经打开,说明有进程读取设备关联的evdev。 list_for_each_entry_rcu(handle, &dev->h_list, d_node) if (handle->open) handle->handler->event(handle, type, code, value);//调用相关的事件处理器的event函数,进行事件的处理,即evdev_event rcu_read_unlock(); } 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; //将传过来的事件,赋值给input_event结构 do_gettimeofday(&event.time);//获取事件时间 event.type = type; event.code = code; event.value = value; rcu_read_lock(); client = rcu_dereference(evdev->grab); //如果evdev绑定了client那么,处理这个客户端,触摸屏驱动没有绑定 if (client) evdev_pass_event(client, &event); else //遍历client链表,调用evdev_pass_event函数 list_for_each_entry_rcu(client, &evdev->client_list, node) evdev_pass_event(client, &event); rcu_read_unlock(); wake_up_interruptible(&evdev->wait); //唤醒等待的进程 } static void evdev_pass_event(struct evdev_client *client, struct input_event *event) { spin_lock(&client->buffer_lock); client->buffer[client->head++] = *event;//将事件赋值给客户端的input_event 数组 client->head &= EVDEV_BUFFER_SIZE - 1; spin_unlock(&client->buffer_lock); kill_fasync(&client->fasync, SIGIO, POLL_IN); }
- 总结一下事件的传递过程:首先在驱动层中,调用inport_report_abs,然后他调用了input core层的input_event,input_event调用了input_handle_event对事件进行分派,调用input_pass_event,在这里他会把事件传递给具体的handler层,然后在相应handler的event处理函数中,封装一个event,然后把它投入evdev的那个client_list上的client的事件buffer中,等待用户空间来读取。
- input_report_abs(dev, ABS_X, xp);