Linux设备模型之input子系统详解(二)

来源:互联网 发布:网络利大于弊的总结词 编辑:程序博客网 时间:2024/05/20 14:44
六:event事件的处理
在开始的时候曾以linux kernel文档中自带的代码作分析.提出了几个事件上报的API. 这些API其实都是input_event()的封装.代码如下:
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);
         }
}
首先,先判断设备产生的这个事件是否合法.如果合法,流程转入到input_handle_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_SYN:
                   switch (code) {
                   case SYN_CONFIG:
                            disposition = INPUT_PASS_TO_ALL;
                            break;
                   case SYN_REPORT:
                            if (!dev->sync) {
                                     dev->sync = 1;
                                     disposition = INPUT_PASS_TO_HANDLERS;
                            }
                            break;
                   }
                   break;
         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);
                            }
                            disposition = INPUT_PASS_TO_HANDLERS;
                   }
                   break;
         case EV_SW:
                   if (is_event_supported(code, dev->swbit, SW_MAX) &&
                       !!test_bit(code, dev->sw) != value) {
                            __change_bit(code, dev->sw);
                            disposition = INPUT_PASS_TO_HANDLERS;
                   }
                   break;
         case EV_ABS:
                   if (is_event_supported(code, dev->absbit, ABS_MAX)) {
                            value = input_defuzz_abs_event(value,
                                               dev->abs[code], dev->absfuzz[code]);
                            if (dev->abs[code] != value) {
                                     dev->abs[code] = value;
                                     disposition = INPUT_PASS_TO_HANDLERS;
                            }
                   }
                   break;
         case EV_REL:
                   if (is_event_supported(code, dev->relbit, REL_MAX) && value)
                            disposition = INPUT_PASS_TO_HANDLERS;
                   break;
         case EV_MSC:
                   if (is_event_supported(code, dev->mscbit, MSC_MAX))
                            disposition = INPUT_PASS_TO_ALL;
                   break;
         case EV_LED:
                   if (is_event_supported(code, dev->ledbit, LED_MAX) &&
                       !!test_bit(code, dev->led) != value) {
                            __change_bit(code, dev->led);
                            disposition = INPUT_PASS_TO_ALL;
                   }
                   break;
         case EV_SND:
                   if (is_event_supported(code, dev->sndbit, SND_MAX)) {
                            if (!!test_bit(code, dev->snd) != !!value)
                                     __change_bit(code, dev->snd);
                            disposition = INPUT_PASS_TO_ALL;
                   }
                   break;
         case EV_REP:
                   if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) {
                            dev->rep[code] = value;
                            disposition = INPUT_PASS_TO_ALL;
                   }
                   break;
         case EV_FF:
                   if (value >= 0)
                            disposition = INPUT_PASS_TO_ALL;
                   break;
         case EV_PWR:
                   disposition = INPUT_PASS_TO_ALL;
                   break;
         }
         if (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);
}
在这里, 忽略掉具体事件的处理. 到最后,如果该事件需要input device来完成的,就会将disposition设置成INPUT_PASS_TO_DEVICE.如果需要handler来完成的,就将 dispostion设为INPUT_PASS_TO_HANDLERS.如果需要两者都参与,将disposition设置为 INPUT_PASS_TO_ALL.
需要输入设备参与的,回调设备的event函数.如果需要handler参与的.调用input_pass_event().代码如下:
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 = rcu_dereference(dev->grab);
         if (handle)
                   handle->handler->event(handle, type, code, value);
         else
                   list_for_each_entry_rcu(handle, &dev->h_list, d_node)
                            if (handle->open)
                                     handle->handler->event(handle,
                                                                 type, code, value);
         rcu_read_unlock();
}
如果input device被强制指定了handler,则调用该handler的event函数.
结合handle注册的分析.我们知道.会将handle挂到input device的h_list链表上.
如果没有为input device强制指定handler.就会遍历input device->h_list上的handle成员.如果该handle被打开,则调用与输入设备对应的handler的event()函数.注意,只有在handle被打开的情况下才会接收到事件.

另外,输入设备的handler强制设置一般是用带EVIOCGRAB标志的ioctl来完成的.如下是发图的方示总结evnet的处理过程:

到此,已经分析了input device,handler和handle的注册过程以及事件的上报和处理.下面以evdev为实例做分析.来贯穿理解一下整个过程.
七:evdev概述
Evdev对应的设备节点一般位于/dev/input/event0 ~ /dev/input/event4.理论上可以对应32个设备节点.分别代表被handler匹配的32个input device.

可以用cat /dev/input/event0.然后移动鼠标或者键盘按键就会有数据输出(两者之间只能选一.因为一个设备文件只能关能一个输入设备).还可以往这个文件里写数据,使其产生特定的事件.这个过程之后再详细分析.
为了分析这一过程,必须从input子系统的初始化说起.
八:input子系统的初始化
Input子系统的初始化函数为input_init().代码如下:
static int __init input_init(void)
{
         int err;
         err = class_register(&input_class);
         if (err) {
                   printk(KERN_ERR "input: unable to register input_dev class\n");
                   return err;
         }
         err = input_proc_init();
         if (err)
                   goto fail1;
         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;
}
在这个初始化函数里,先注册了一个名为”input”的类.所有input device都属于这个类.在sysfs中表现就是.所有input device所代表的目录都位于/dev/class/input下面.
然后调用input_proc_init()在/proc下面建立相关的交互文件.
最后调用register_chrdev()注册了主设备号为INPUT_MAJOR(13).次设备号为0~255的字符设备.它的操作指针为input_fops.
这里可以看到.所有主设备号13的字符设备的操作最终都会转入到input_fops中.在前面分析的/dev/input/event0~/dev/input/event4的主设备号为13.操作也不例外的落在了input_fops中.
Input_fops定义如下:
static const struct file_operations input_fops = {
         .owner = THIS_MODULE,
         .open = input_open_file,
};
打开文件所对应的操作函数为input_open_file.代码如下示:
static int input_open_file(struct inode *inode, struct file *file)
{
         struct input_handler *handler = input_table[iminor(inode) >> 5];
         const struct file_operations *old_fops, *new_fops = NULL;
         int err;
         /* No load-on-demand here? */
         if (!handler || !(new_fops = fops_get(handler->fops)))
                   return -ENODEV;
iminor(inode)为打开文件所对应的次设备号.input_table是一个struct input_handler全局数组.先将设备结点的次设备号右移5位做为索引值到input_table中取对应项.这里可以看到.一 个handler代表1<<5个设备节点(因为在input_table中取值是以次备号右移5位为索引的.即低5位相同的次备号对应的是同一 个索引).终于看到了input_talbe大显身手的地方了.input_talbe[ ]中取值和input_talbe[ ]的赋值,这两个过程是相对应的.
在input_table中找到对应的handler之后,就会检验这个handler是否存,是否带有fops文件操作集.如果没有.则返回一个设备不存在的错误.
         /*
         * That's _really_ odd. Usually NULL ->open means "nothing special",
         * not "no device". Oh, well...
         */
         if (!new_fops->open) {
                   fops_put(new_fops);
                   return -ENODEV;
         }
         old_fops = file->f_op;
         file->f_op = new_fops;
         err = new_fops->open(inode, file);
         if (err) {
                   fops_put(file->f_op);
                   file->f_op = fops_get(old_fops);
         }
         fops_put(old_fops);
         return err;
}
然后将handler中的fops替换掉当前file的fops.如果handler的fops中有open()函数,则调用它.(如果以evdev为例的话,这里的open()函数应该就是evdev_open()函数)
九:evdev的初始化
Evdev的模块初始化函数为evdev_init().代码如下:
static int __init evdev_init(void)
{
         return input_register_handler(&evdev_handler);
}
它调用了input_register_handler注册了一个handler.
注意到,在这里evdev_handler中定义的minor为EVDEV_MINOR_BASE(64).也就是说evdev_handler所表示的设备文件范围为(13,64)~(13,64+32).

从之前的分析知道.匹配成功的关键在于handler中的blacklist和id_talbe. Evdev_handler的id_table定义如下:
static const struct input_device_id evdev_ids[] = {
         { .driver_info = 1 },     /* Matches all devices */
         { },                       /* Terminating zero entry */
};
它没有定义flags.也没有定义匹配属性值.这个handler是匹配所有input device的.匹配成功之后会调用handler->connect函数.
在Evdev_handler中,该成员函数如下所示:
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;
         for (minor = 0; minor < EVDEV_MINORS; minor++)
                   if (!evdev_table[minor])
                            break;
         if (minor == EVDEV_MINORS) {
                   printk(KERN_ERR "evdev: no more free evdev devices\n");
                   return -ENFILE;
         }
EVDEV_MINORS定义为32.表示evdev_handler所表示的32个设备文件.evdev_talbe是一个struct evdev类型的数组.struct evdev是模块使用的封装结构.在接下来的代码中可以看到这个结构的使用.
这一段代码的在evdev_talbe找到为空的那一项.minor就是数组中第一项为空的序号.
         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);
         snprintf(evdev->name, sizeof(evdev->name), "event%d", minor);
         evdev->exist = 1;
         evdev->minor = minor;
         evdev->handle.dev = input_get_device(dev);
         evdev->handle.name = evdev->name;
         evdev->handle.handler = handler;
         evdev->handle.private = evdev;
接下来,分配了一个evdev结构,并对这个结构进行初始化.这个结构封装了一个handle结构,这结构与之前所讨论的handler是不相同的.注意有一个字母的差别哦.可以把handle看成是handler和input device的信息集合体.在这个结构里集合了匹配成功的handler和input device
         strlcpy(evdev->dev.bus_id, evdev->name, sizeof(evdev->dev.bus_id));
         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);
在这段代码里主要完成evdev封装的device的初始化.注意在这里,使它所属的类指向input_class.这样在/sysfs中创建的设备目录就会在/sys/class/input/下面显示.
         error = input_register_handle(&evdev->handle);
         if (error)
                   goto err_free_evdev;
         error = evdev_install_chrdev(evdev);
         if (error)
                   goto err_unregister_handle;
         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;
}
注册handle,如果成功,那么调用evdev_install_chrdev将evdev_table的minor项指向evdev. 然后将evdev->device注册到sysfs.如果失败,则进行相关的错误处理.
万事俱备了,但是要接收事件,还得要等”东风”.这个”东风”就是要打开相应的handle.这个打开过程是在文件的open()中完成的.