注:这篇文章是我在学习的过程中,整理出来的,我也是学习这,不喜勿喷!
一、input子系统基础
1.input子系统的分层
注:一般的驱动框架都分为两层(驱动框架层和驱动设备层),驱动框架层是由内核开发者提供,驱动设备层是由具体的驱动开发工程师来编写。但是input子系统的分层为3层
(1)最上层:输入事件驱动层,在evdev.c,mousedev.c、joydev.c等文件下。注意:不是每种输入事件驱动层都分散在这个三个文件中,而是每一个文件具体包含了一类会发生的事件。比如:鼠标会发生的事件,就在mousedev.c下提供。摇杆类设备发生的事件就在joydev.c下提供。这一套时间发生的机制是纯软件实现的。而evdev.c文件下提供了所有可能发生的事件,它是一个通用事件发生的包含集(分析代码就可以知道)。
(2)中间层:核心层,在input.c文件中
(3)最下层:硬件设备驱动层,在driver/input/xxx目录下
2.各层之间的调用关系
(1)驱动工程师通过调用核心层开放的接口来完成input设备的注册,
(2)输入事件驱动层也是通过调用核心层开放的接口来完成提供可能发生事件的注册(由内核开发者写好的)
二、驱动框架的代码分析
1.相关的结构体
(1)在input.h文件里面
struct input_dev {
void *private;
const char *name;
const char *phys;
const char *uniq;
struct input_id id; //与input_handler匹配用的id
unsigned long evbit[NBITS(EV_MAX)]; //设备支持的事件类型
unsigned long keybit[NBITS(KEY_MAX)]; //按键事件支持的子事件类型
unsigned long relbit[NBITS(REL_MAX)];
unsigned long absbit[NBITS(ABS_MAX)]; //绝对坐标事件支持的子事件类型
unsigned long mscbit[NBITS(MSC_MAX)];
unsigned long ledbit[NBITS(LED_MAX)];
unsigned long sndbit[NBITS(SND_MAX)];
unsigned long ffbit[NBITS(FF_MAX)];
unsigned long swbit[NBITS(SW_MAX)];
int ff_effects_max;
unsigned int keycodemax;
unsigned int keycodesize;
void *keycode;
unsigned int repeat_key;
struct timer_list timer;
struct pt_regs *regs;
int state;
int sync;
int abs[ABS_MAX + 1];
int rep[REP_MAX + 1];
unsigned long key[NBITS(KEY_MAX)];
unsigned long led[NBITS(LED_MAX)];
unsigned long snd[NBITS(SND_MAX)];
unsigned long sw[NBITS(SW_MAX)];
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 (*accept)(struct input_dev *dev, struct file *file);
int (*flush)(struct input_dev *dev, struct file *file);
int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value);
int (*upload_effect)(struct input_dev *dev, struct ff_effect *effect);
int (*erase_effect)(struct input_dev *dev, int effect_id);
struct input_handle *grab; //当前占有该设备的handle
struct mutex mutex; /* serializes open and close operations */
unsigned int users; //打开该设备的用户量
struct class_device cdev;
struct device *dev; /* will be removed soon */
int dynalloc; /* temporarily */
struct list_head h_list; //该链表头用于链接该设备所关联的input_handle
struct list_head node; //该链表头用于将设备链接到input_dev_list
};
struct input_handler {
//私有的数据
void *private;
//向上发报事件的函数指针
void (*event)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
bool (*filter)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
//用来匹配handler和dev的函数指针
bool (*match)(struct input_handler *handler, struct input_dev *dev);
//用来连接handler和dev的函数指针
int (*connect)(struct input_handler *handler, struct input_dev *dev, const struct input_device_id *id);
//用来断开handler和dev的函数指针
void (*disconnect)(struct input_handle *handle);
void (*start)(struct input_handle *handle);
//操作设备文件的函数集
const struct file_operations *fops;
//次设备号
int minor;
//名字
const char *name;
//用来匹配的id_table
const struct input_device_id *id_table;
struct list_head h_list;
struct list_head node;
};
//这个结构体用来关联dev和handler的
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;
};
2.核心层代码分析
(1)核心驱动层框架的注册
struct class input_class = {
.name = "input",
.devnode = input_devnode,
};
static const struct file_operations input_fops = {
.owner = THIS_MODULE,
.open = input_open_file,
};
static int __init input_init(void)
{
int err;
//不懂干什么的
input_init_abs_bypass();
//创建一个类,在/sys/class/下创建一个input目录
err = class_register(&input_class);
if (err) {
printk(KERN_ERR "input: unable to register input_dev class\n");
return err;
}
//proc文件系统相关的
err = input_proc_init();
if (err)
goto fail1;
//注册input子系统,主设备号是INPUT_MAJOR,绑定input_fops操作函数
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;
}
总结:从各种框架的注册来看,都是调用class_register,然后在注册函数进行注册,大体框架都是这样。注册函数都是模式化的,比较简单。
(2)核心层提供的接口函数
//作用:动态创建一个input_dev类型的结构体变量,并且做一些基本的初始化
struct input_dev *input_allocate_device(void)
{
struct input_dev *dev;
dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
if (dev) {
dev->dev.type = &input_dev_type;
dev->dev.class = &input_class;
device_initialize(&dev->dev);
mutex_init(&dev->mutex);
spin_lock_init(&dev->event_lock);
INIT_LIST_HEAD(&dev->h_list);
INIT_LIST_HEAD(&dev->node);
__module_get(THIS_MODULE);
}
return dev;
}
//作用:对input类设备设置可能发生的事件。
void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
{
switch (type) {
case EV_KEY:
__set_bit(code, dev->keybit);
break;
case EV_REL:
__set_bit(code, dev->relbit);
break;
case EV_ABS:
__set_bit(code, dev->absbit);
break;
case EV_MSC:
__set_bit(code, dev->mscbit);
break;
case EV_SW:
__set_bit(code, dev->swbit);
break;
case EV_LED:
__set_bit(code, dev->ledbit);
break;
case EV_SND:
__set_bit(code, dev->sndbit);
break;
case EV_FF:
__set_bit(code, dev->ffbit);
break;
case EV_PWR:
/* do nothing */
break;
default:
printk(KERN_ERR
"input_set_capability: unknown type %u (code %u)\n",
type, code);
dump_stack();
return;
}
__set_bit(type, dev->evbit);
}
//作用:注册我们动态创建的struct input_dev类型的变量。
int input_register_device(struct input_dev *dev)
{
static atomic_t input_no = ATOMIC_INIT(0);
struct input_handler *handler;
const char *path;
int error;
/* Every input device generates EV_SYN/SYN_REPORT events. */
//我们在input_set_capability函数设置了一些位,如果没有设置EV_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);
/*
* If delay and period are pre-set by the driver, then autorepeating
* is handled by the driver itself and we don't do it in input.c.
*/
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;
}
if (!dev->getkeycode)
dev->getkeycode = input_default_getkeycode;
if (!dev->setkeycode)
dev->setkeycode = input_default_setkeycode;
dev_set_name(&dev->dev, "input%ld",
(unsigned long) atomic_inc_return(&input_no) - 1);
//添加类下面的设备
error = device_add(&dev->dev);
if (error)
return error;
path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
printk(KERN_INFO "input: %s as %s\n",
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;
}
//添加到input_dev_list,完成注册
list_add_tail(&dev->node, &input_dev_list);
//遍历整个input_handler_list
list_for_each_entry(handler, &input_handler_list, node)
//试图匹配dev和handler,如果匹配成功则连接它们
input_attach_handler(dev, handler);
input_wakeup_procfs_readers();
mutex_unlock(&input_mutex);
return 0;
}
input_dev_list表示已经注册的设备链表头
input_handler_list表示已经注册的handler链表头(这个链表的添加,在后面会有)
(3)输入事件层的注册和相关的函数
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,
.fasync = evdev_fasync,
.flush = evdev_flush
};
//说明event这个handler是什么都能进行匹配的
static const struct input_device_id evdev_ids[] = {
{ .driver_info = 1 }, /* Matches all devices */
{ }, /* Terminating zero entry */
};
static struct input_handler evdev_handler = {
.event = evdev_event,
.connect = evdev_connect,
.disconnect = evdev_disconnect,
.fops = &evdev_fops,
.minor = EVDEV_MINOR_BASE,
.name = "evdev",
.id_table = evdev_ids,
};
static int __init evdev_init(void)
{
return input_register_handler(&evdev_handler);
}
注册函数调用了input_register_handler函数,注意这个函数的原型在input.c里面,这就证实了输入事件层是调用核心层来实现的。好,接下来我们看input_register_handler函数
//这个指针数组是用来管理handler的,这个指针数组,说明最多只支持8个handler。 为什么是8个,那么少?因为我们Linux内核开发者把现在可能出现的input设备都囊括了,才出现了mouse,joy,event 3种,而且所有设备都可以归到event下。所以定义了8个指针数组是够用的。
static struct input_handler *input_table[8];
int input_register_handler(struct input_handler *handler)
{
struct input_dev *dev;
int retval;
retval = mutex_lock_interruptible(&input_mutex);
if (retval)
return retval;
//初始化handler的链表,为接下来挂接handle做准备
INIT_LIST_HEAD(&handler->h_list);
//显示这个if是成立的,
if (handler->fops != NULL) {
//判断input_table这个位是否为空,如果为空,则将要注册的handler放入handler指针数组中
if (input_table[handler->minor >> 5]) {
retval = -EBUSY;
goto out;
}
input_table[handler->minor >> 5] = handler;
}
//添加handler->node到input_handler_list。
list_add_tail(&handler->node, &input_handler_list);
//这里是用来遍历input_dev_list的,
list_for_each_entry(dev, &input_dev_list, node)
input_attach_handler(dev, handler);
input_wakeup_procfs_readers();
out:
mutex_unlock(&input_mutex);
return retval;
}
注意:在内核启动后,event完成注册后input_handler_list已经不为空了,里面已经添加了evdev_handler。同样的,在驱动工程师调用了input_register_device函数之后input_dev_list链表也不空。注意先后顺序:用脚后跟想想就知道肯定是event先注册了,然后驱动工程师在注册设备,不然input设备怎么找到具体的handler(这里只是我凭经验的自己猜测,哈哈,非礼莫视)。
接下来结合驱动工程师使用的input_register_device和输入事件层注册时调用的input_register_handler来分析两者的联系。重点就是在这两句代码中。
//上面已经说明这里的input_handler_list不为空了
list_for_each_entry(handler, &input_handler_list, node)
input_attach_handler(dev, handler);
具体看代码
//这个宏就是用来遍历整个链表的,里面的list_entry宏也就是container_of宏,可以自己追进去分析
#define list_for_each_entry(pos, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member); \
prefetch(pos->member.next), &pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
{
const struct input_device_id *id;
int error;
//完成dev和handler的匹配工作,代码在下面
id = input_match_device(handler, dev);
if (!id)
return -ENODEV;
//找到匹配的id之后进行dev和handler的连接,连接函数就是在evdev.c注册时候填充的。具体的看上面我提供的结构体
error = handler->connect(handler, dev, id);
if (error && error != -ENODEV)
printk(KERN_ERR
"input: failed to attach handler %s to device %s, "
"error: %d\n",
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;
//要明白handler->id_table跟dev->id,handler->id_table就是在我们handler注册时提供的id_table,而dev的->id是在硬件初始化的时候填充的
for (id = handler->id_table; id->flags || id->driver_info; id++) {
//匹配总线,如果总线不相同,就匹配下一个
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);
//匹配成功后会返回一个id
if (!handler->match || handler->match(handler, dev))
return id;
}
return NULL;
}
#define EVDEV_MINORS 32
static struct evdev *evdev_table[EVDEV_MINORS];
说明,一个handler最多也只能匹配32个dev
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_table指针数组为空的地方,用minor记录下来
for (minor = 0; minor < EVDEV_MINORS; minor++)
if (!evdev_table[minor])
break;
//如果minor等于EVDEV_MINORS,说明evdev_table已经满了,就直接返回错误
if (minor == EVDEV_MINORS) {
printk(KERN_ERR "evdev: no more free evdev devices\n");
return -ENFILE;
}
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);
//设置名字,可以根据ubuntu下面的/sys/class/input结合代码看
dev_set_name(&evdev->dev, "event%d", minor);
evdev->exist = 1;
evdev->minor = minor;
//注意是handle不是handler。handle是用来关联dev和handler的
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, EVDEV_MINOR_BASE + minor);
evdev->dev.class = &input_class;
evdev->dev.parent = &dev->dev;
evdev->dev.release = evdev_free;
//初始化dev,用来创建sysfs下面的文件
device_initialize(&evdev->dev);
//调用核心层函数input_register_handle完成handle的注册,代码在后面
error = input_register_handle(&evdev->handle);
if (error)
goto err_free_evdev;
//把注册的这个evdev填充到evdev_table里面,代码在后面
error = evdev_install_chrdev(evdev);
if (error)
goto err_unregister_handle;
//在添加设备/sys/class/input下添加设备
error = device_add(&evdev->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;
}
int input_register_handle(struct input_handle *handle)
{
struct input_handler *handler = handle->handler;
struct input_dev *dev = handle->dev;
int error;
/*
* We take dev->mutex here to prevent race with
* input_release_device().
*/
error = mutex_lock_interruptible(&dev->mutex);
if (error)
return error;
/*
* Filters go to the head of the list, normal handlers
* to the tail.
*/
if (handler->filter)
list_add_rcu(&handle->d_node, &dev->h_list);
else
//把handle->d_node挂接到dev的h_list上面
list_add_tail_rcu(&handle->d_node, &dev->h_list);
mutex_unlock(&dev->mutex);
/*
* Since we are supposed to be called from ->connect()
* which is mutually exclusive with ->disconnect()
* we can't be racing with input_unregister_handle()
* and so separate lock is not needed here.
*/
//把handle->h_node挂接到dev的h_list上面,两个挂接完成之后,就可以通过handler和dev下面的链表找到这个handle
list_add_tail_rcu(&handle->h_node, &handler->h_list);
if (handler->start)
handler->start(handle);
return 0;
}
static int evdev_install_chrdev(struct evdev *evdev)
{
/*
* No need to do any locking here as calls to connect and
* disconnect are serialized by the input core
*/
evdev_table[evdev->minor] = evdev;
return 0;
}
匹配连接过程到此结束。这里dev和handler都关联在一起了。而实际是通过handle这个结构体来联系dev和handler的。可以通过dev下的h_list找到handle->h_node,也可以通过handler下的h_list找到handle->handle->h_node。然后也通过handle下面的dev找到相应的dev和hanlder。
匹配过程完成之后,整个input子系统的构建也完成了。
三、实例分析流成
1.我在内核中找了一个已经写好的文件/driver/keyboard/gpio_keys.c文件
2.这个按键的框架使用的是platform框架,初始化函数在gpio_keys_probe函数。
调用层次关系:
gpio_keys_probe
gpio_keys_report_event
input_event
//从这一层代码开始就是input.c文件里面的内容了,就是input核心部分的代码了。
input_handle_event
input_pass_event
handle->handler->event(handle, type, code, value);
层次关系就是这样的,具体的细节我也没仔细看。然后我们在分析一下,下面这句代码。
这就是上报发生的事件
handle->handler->event(handle, type, code, value);
在上面我们说过handle可以找到handler,是因为在evdev.c的connect函数里面进行了一系列的初始化。由此我们就知道这个函数调用的是谁了。也就是我们注册event的时候的那个结构体变量绑定好的。不多说上代码
static struct input_handler evdev_handler = {
.event = evdev_event,
.connect = evdev_connect,
.disconnect = evdev_disconnect,
.fops = &evdev_fops,
.minor = EVDEV_MINOR_BASE,
.name = "evdev",
.id_table = evdev_ids,
};
//这个函数就是把event放入了一个buffer当中为read做准备,也就完成了一次事件放生的上报
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;
struct timespec ts;
ktime_get_ts(&ts);
event.time.tv_sec = ts.tv_sec;
event.time.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
event.type = type;
event.code = code;
event.value = value;
rcu_read_lock();
client = rcu_dereference(evdev->grab);
if (client)
//把数据放入buff的具体函数
evdev_pass_event(client, &event);
else
list_for_each_entry_rcu(client, &evdev->client_list, node)
evdev_pass_event(client, &event);
rcu_read_unlock();
wake_up_interruptible(&evdev->wait);
}
输入事件驱动层的其他接口的代码分析,我不写了,详情可以参考网上其他大神所写的博客,整个input子系统大概就是这样工作的。因为我也是学习者,能力有限。谢谢大家的观看