Linux设备驱动剖析之Input(三)

00000518     /* get current state of buttons */
00000519     for (i = 0; i < pdata->nbuttons; i++)
00000520         gpio_keys_report_event(&ddata->data[i]);
00000521     input_sync(input);
00000522 
00000523     device_init_wakeup(&pdev->dev, wakeup);
00000524 
00000525     return 0;
00000526 
00000527  fail3:
00000528     sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
00000529  fail2:
00000530     while (--i >= 0) {
00000531         free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
00000532         if (ddata->data[i].timer_debounce)
00000533             del_timer_sync(&ddata->data[i].timer);
00000534         cancel_work_sync(&ddata->data[i].work);
00000535         gpio_free(pdata->buttons[i].gpio);
00000536     }
00000537 
00000538     platform_set_drvdata(pdev, NULL);
00000539  fail1:
00000540     input_free_device(input);
00000541     kfree(ddata);
00000542 
00000543     return error;
00000544 }

520、521行,为了避免与后面的内容重复,为了讲解的连贯性,把它们放到讲完事件驱动程序之后再讲。现在Input设备已经注册进input core了,剩下就是将handler也注册进input core,这样Input事件就能够通过input core到达事件驱动程序,最后到达用户空间,这也是我们的最终目的。

好了,下面开始讲事件驱动程序,以drivers/input/evdev.c为例,这是一个通用的事件驱动程序,可以支持所有的Input设备。好了,“态度决定一切,从初始化函数开始。”

00000914 static int __init evdev_init(void)
00000915 {
00000916     return input_register_handler(&evdev_handler);
00000917 }

先看916行input_register_handler函数参数evdev_handler的定义:

00000904 static struct input_handler evdev_handler = {
00000905     .event        = evdev_event,
00000906     .connect    = evdev_connect,
00000907     .disconnect    = evdev_disconnect,
00000908     .fops        = &evdev_fops,
00000909     .minor        = EVDEV_MINOR_BASE,
00000910     .name        = "evdev",
00000911     .id_table    = evdev_ids,
00000912 };

先有个印象,后面用到的时候再细讲。

916行,调用input_register_handler函数注册handler,这是input core里的函数,定义如下:

00001837 int input_register_handler(struct input_handler *handler)
00001838 {
00001839     struct input_dev *dev;
00001840     int retval;
00001841 
00001842     retval = mutex_lock_interruptible(&input_mutex);
00001843     if (retval)
00001844         return retval;
00001845 
00001846     INIT_LIST_HEAD(&handler->h_list);
00001847 
00001848     if (handler->fops != NULL) {
00001849         if (input_table[handler->minor >> 5]) {
00001850             retval = -EBUSY;
00001851             goto out;
00001852         }
00001853         input_table[handler->minor >> 5] = handler;
00001854     }
00001855 
00001856     list_add_tail(&handler->node, &input_handler_list);
00001857 
00001858     list_for_each_entry(dev, &input_dev_list, node)
00001859         input_attach_handler(dev, handler);
00001860 
00001861     input_wakeup_procfs_readers();
00001862 
00001863  out:
00001864     mutex_unlock(&input_mutex);
00001865     return retval;
00001866 }

1846行,初始化用来连接handle的链表。

1848行,if条件成立,1849行,handler->minor的值为EVDEV_MINOR_BASE,即64。input_table是一个struct input_handler类型的指针数组,定义如下:

00000047 static struct input_handler *input_table[8];

handler->minor右移5位后的值为2,因此这里判断input_table[2]的值是否以0,因为是第一次进来,显然是为0的。

1853行,将input_table[2]指向此handler。

1856行,将handler加入到input_handler_list链表的尾部。

1858、1859行,很眼熟是吧,没错,之前说过的,只不过这里是遍历的是input_dev_list这条链表,而在input_register_device函数中遍历的是input_handler_list这条链表。这表明一个Input设备可以有多个handler,一个handler也可以依附多个Input设备。这里有必要把input_attach_handler函数的代码再贴一遍:

00000840 static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
00000841 {
00000842     const struct input_device_id *id;
00000843     int error;
00000844 
00000845     id = input_match_device(handler, dev);
00000846     if (!id)
00000847         return -ENODEV;
00000848 
00000849     error = handler->connect(handler, dev, id);
00000850     if (error && error != -ENODEV)
00000851         printk(KERN_ERR
00000852             "input: failed to attach handler %s to device %s, "
00000853             "error: %d
",
00000854             handler->name, kobject_name(&dev->dev.kobj), error);
00000855 
00000856     return error;
00000857 }

因为evdev.c能够匹配使有的Input设备,至于为什么可以,在讲input_match_device函数时已经讲过了,忘记了的话可以回去看看,所以会执行849行的connect函数,实质上就是evdev.c里的evdev_connect函数,下面看它的定义:

00000824 static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
00000825              const struct input_device_id *id)
00000826 {
00000827     struct evdev *evdev;
00000828     int minor;
00000829     int error;
00000830 
00000831     for (minor = 0; minor < EVDEV_MINORS; minor++)
00000832         if (!evdev_table[minor])
00000833             break;
00000834 
00000835     if (minor == EVDEV_MINORS) {
00000836         printk(KERN_ERR "evdev: no more free evdev devices
");
00000837         return -ENFILE;
00000838     }
00000839 
00000840     evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
00000841     if (!evdev)
00000842         return -ENOMEM;
00000843 
00000844     INIT_LIST_HEAD(&evdev->client_list);
00000845     spin_lock_init(&evdev->client_lock);
00000846     mutex_init(&evdev->mutex);
00000847     init_waitqueue_head(&evdev->wait);
00000848 
00000849     dev_set_name(&evdev->dev, "event%d", minor);
00000850     evdev->exist = true;
00000851     evdev->minor = minor;
00000852 
00000853     evdev->handle.dev = input_get_device(dev);
00000854     evdev->handle.name = dev_name(&evdev->dev);
00000855     evdev->handle.handler = handler;
00000856     evdev->handle.private = evdev;
00000857 
00000858     evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
00000859     evdev->dev.class = &input_class;
00000860     evdev->dev.parent = &dev->dev;
00000861     evdev->dev.release = evdev_free;
00000862     device_initialize(&evdev->dev);
00000863 
00000864     error = input_register_handle(&evdev->handle);
00000865     if (error)
00000866         goto err_free_evdev;
00000867 
00000868     error = evdev_install_chrdev(evdev);
00000869     if (error)
00000870         goto err_unregister_handle;
00000871 
00000872     error = device_add(&evdev->dev);
00000873     if (error)
00000874         goto err_cleanup_evdev;
00000875 
00000876     return 0;
00000877 
00000878  err_cleanup_evdev:
00000879     evdev_cleanup(evdev);
00000880  err_unregister_handle:
00000881     input_unregister_handle(&evdev->handle);
00000882  err_free_evdev:
00000883     put_device(&evdev->dev);
00000884     return error;
00000885 }

827行,struct evdev的定义:

00000026 struct evdev {
00000027     int open;
00000028     int minor;
00000029     struct input_handle handle;
00000030     wait_queue_head_t wait;
00000031     struct evdev_client *grab;
00000032     struct list_head client_list;
00000033     spinlock_t client_lock; /* protects client_list */
00000034     struct mutex mutex;
00000035     struct device dev;
00000036     bool exist;
00000037 };

27行,open,当用户open此设备时,open的值加1。

28行,minor,次设备号,主设备都一样,用次设备号来区别不同的设备。

29行,handle,很熟悉了。

30行,wait,等待队列。

31行,grab,可以为evdev实例指定一个struct evdev_client实例,这样在传递Input消息时就只会传递给这一个struct evdev_client实例,而不会传递给所有的struct evdev_client实例。每open一次就会生成一个struct evdev_client实例。

32行,client_list,用来把所有的struct client_list实例连接在一起。

33、34行,同步相关的锁。

35行,dev,用来嵌入到设备模型中。

36行,exist,struct evdev被成功实例化后,exist的值就为true。

下面看struct evdev_client的定义:

00000039 struct evdev_client {
00000040     int head;
00000041     int tail;
00000042     spinlock_t buffer_lock; /* protects access to buffer, head and tail */
00000043     struct fasync_struct *fasync;
00000044     struct evdev *evdev;
00000045     struct list_head node;
00000046     int bufsize;
00000047     struct input_event buffer[];
00000048 };

40、41行,作为环形缓冲区的索引值,环形缓冲区就是用来存储Input事件消息的。

42行,buffer_lock,缓冲区的锁。

43行,fasync,异步通知相关的。

44行,evdev,所属的struct evdev实例。

45行,node,作为节点链入struct evdev实例所形成的链表中。

46行,bufsize,环形缓冲区的长度。

47行,buffer,struct input_event类型的数组,也就是环形缓冲区,数组长度可变。

下面看struct input_event,在include/linux/input.h中定义的:

00000026 struct input_event {
00000027     struct timeval time;
00000028     __u16 type;
00000029     __u16 code;
00000030     __s32 value;
00000031 };

27行,time,消息产生的时间。

28行,type,消息的类型。

29行,code,消息的值,对于按键事件的话,就对应键盘上的按键。

30行,value,对于本文来说,表示IO电平的取反值。

回到evdev_connect函数,831至833行,evdev_table是一个struct evdev类型的数组,定义如下:

00000050 static struct evdev *evdev_table[EVDEV_MINORS];

其中EVDEV_MINORS的值为32。

那么831至833行的意思就很明显了,就是从evdev_table数组中找到第一个值为0的元素,这样minor就表示这个元素在evdev_table数组中的索引值,或者说下标值。

835至838行,如果minor等于EVDEV_MINORS,就表示evdev_table数组溢出了,不能往下走了,返回出错吧。

840至842行,为struct evdev实例分配内存。

844至847行,一些初始化。

850行,evdev->exist = true,和之前说的一致。

851行,记录下次设备号。

853至862行,注意一下858行,生成设备号,主设备号为INPUT_MAJOR,即13,次设备号为EVDEV_MINOR_BASE + minor,EVDEV_MINOR_BASE的值为64,就是说次设备号是从64开始往上递增的。

864行,注册handle,input_register_handle函数的定义为:

00001940 int input_register_handle(struct input_handle *handle)
00001941 {
00001942     struct input_handler *handler = handle->handler;
00001943     struct input_dev *dev = handle->dev;
00001944     int error;
00001945 
00001946     /*
00001947      * We take dev->mutex here to prevent race with
00001948      * input_release_device().
00001949      */
00001950     error = mutex_lock_interruptible(&dev->mutex);
00001951     if (error)
00001952         return error;
00001953 
00001954     /*
00001955      * Filters go to the head of the list, normal handlers
00001956      * to the tail.
00001957      */
00001958     if (handler->filter)
00001959         list_add_rcu(&handle->d_node, &dev->h_list);
00001960     else
00001961         list_add_tail_rcu(&handle->d_node, &dev->h_list);
00001962 
00001963     mutex_unlock(&dev->mutex);
00001964 
00001965     /*
00001966      * Since we are supposed to be called from ->connect()
00001967      * which is mutually exclusive with ->disconnect()
00001968      * we can't be racing with input_unregister_handle()
00001969      * and so separate lock is not needed here.
00001970      */
00001971     list_add_tail_rcu(&handle->h_node, &handler->h_list);
00001972 
00001973     if (handler->start)
00001974         handler->start(handle);
00001975 
00001976     return 0;
00001977 }

1958行,if条件不成立,所以执行1961行,将handle加入到Input设备的h_list链表的尾部。

1971行,将handle加入到handler的h_list链表的尾部。

1973行,如果handler有定义start函数,那么就调用之,显然,对于evdev.c这个handler是没有定义start函数的。

回到evdev_connect函数,868至870行,调用evdev_install_chrdev函数,定义如下:

00000773 static int evdev_install_chrdev(struct evdev *evdev)
00000774 {
00000775     /*
00000776      * No need to do any locking here as calls to connect and
00000777      * disconnect are serialized by the input core
00000778      */
00000779     evdev_table[evdev->minor] = evdev;
00000780     return 0;
00000781 }

很简单,就是将evdev_table数组对应的元素指向该struct evdev实例。

872行,将dev加入到设备模型中。

      至此evdev.c的初始化过程完毕。现在系统里已经具备Input设备和Input事件驱动程序,Input消息产生后应该就能到达事件驱动程序的缓冲区,等着用户程序将Input消息读取出来。下面就分析这一个过程。

首先应该知道Input消息产生的源头是按键的按下,按键按下后会触发中断,然后进入中断处理程序,所以应该从按键的中断处理程序看起,就是drivers/input/keyboard/gpio_keys.c里的gpio_keys_isr函数,它的定义如下:

00000346 static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
00000347 {
00000348     struct gpio_button_data *bdata = dev_id;
00000349     struct gpio_keys_button *button = bdata->button;
00000350 
00000351     BUG_ON(irq != gpio_to_irq(button->gpio));
00000352 
00000353     if (bdata->timer_debounce)
00000354         mod_timer(&bdata->timer,
00000355             jiffies + msecs_to_jiffies(bdata->timer_debounce));
00000356     else
00000357         schedule_work(&bdata->work);
00000358 
00000359     return IRQ_HANDLED;
00000360 }

353行,如果按键需要延时消抖的话,那么就启动定时器,否则调用357行的schedule_work函数,将work投入到工作队列里,这样,在一段时间过后work指定的函数将会被执行,这里假设是第一种情况,那么下面看定时器的超时处理函数gpio_keys_timer的定义:

00000339 static void gpio_keys_timer(unsigned long _data)
00000340 {
00000341     struct gpio_button_data *data = (struct gpio_button_data *)_data;
00000342 
00000343     schedule_work(&data->work);
00000344 }

同样是调用schedule_work函数。接下来要看的是work指定的函数gpio_keys_work_func。

00000331 static void gpio_keys_work_func(struct work_struct *work)
00000332 {
00000333     struct gpio_button_data *bdata =
00000334         container_of(work, struct gpio_button_data, work);
00000335 
00000336     gpio_keys_report_event(bdata);
00000337 }

336行,gpio_keys_report_event函数,前面说这个函数放到后面再说,现在就放到这里说,看内核代码需要保持一颗清醒的头脑哈。下面看它的定义:

00000320 static void gpio_keys_report_event(struct gpio_button_data *bdata)
00000321 {
00000322     struct gpio_keys_button *button = bdata->button;
00000323     struct input_dev *input = bdata->input;
00000324     unsigned int type = button->type ?: EV_KEY;
00000325     int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
00000326 
00000327     input_event(input, type, button->code, !!state);
00000328     input_sync(input);
00000329 }

325行,调用gpio_get_value函数获取IO口的电平并将它异或上active_low,假设active_low的值为1,按键按下时IO的电平一般来说是低电平,即0,那么此时state的值就为1(异或:同为0,异为1)。

327行,调用input_event函数,向input core报告按键事件,在drivers/input/input.c中定义,如下:

00000346 void input_event(struct input_dev *dev,
00000347          unsigned int type, unsigned int code, int value)
00000348 {
00000349     unsigned long flags;
00000350 
00000351     if (is_event_supported(type, dev->evbit, EV_MAX)) {
00000352 
00000353         spin_lock_irqsave(&dev->event_lock, flags);
00000354         add_input_randomness(type, code, value);
00000355         input_handle_event(dev, type, code, value);
00000356         spin_unlock_irqrestore(&dev->event_lock, flags);
00000357     }
00000358 }

351行,is_event_supported函数的定义:

00000049 static inline int is_event_supported(unsigned int code,
00000050                      unsigned long *bm, unsigned int max)
00000051 {
00000052     return code <= max && test_bit(code, bm);
00000053 }

函数返回1的条件是:事件类型的值不能大于input core支持的最大值并且Input设备的事件位图中相应的位已经置1。

      回到input_event函数,假设if的条件成立,354行,随机数相关,没怎么了解并且对后面的分析没有影响,因此飘过。

355行,input_handle_event,这函数比较重要,看它的定义:

00000215 static void input_handle_event(struct input_dev *dev,
00000216                    unsigned int type, unsigned int code, int value)
00000217 {
00000218     int disposition = INPUT_IGNORE_EVENT;
00000219 
00000220     switch (type) {
00000221 
00000222     case EV_SYN:
00000223         switch (code) {
00000224         case SYN_CONFIG:
00000225             disposition = INPUT_PASS_TO_ALL;
00000226             break;
00000227 
00000228         case SYN_REPORT:
00000229             if (!dev->sync) {
00000230                 dev->sync = true;
00000231                 disposition = INPUT_PASS_TO_HANDLERS;
00000232             }
00000233             break;
00000234         case SYN_MT_REPORT:
00000235             dev->sync = false;
00000236             disposition = INPUT_PASS_TO_HANDLERS;
00000237             break;
00000238         }
00000239         break;
00000240 
00000241     case EV_KEY:
00000242         if (is_event_supported(code, dev->keybit, KEY_MAX) &&
00000243             !!test_bit(code, dev->key) != value) {
00000244 
00000245             if (value != 2) {
00000246                 __change_bit(code, dev->key);
00000247                 if (value)
00000248                     input_start_autorepeat(dev, code);
00000249                 else
00000250                     input_stop_autorepeat(dev);
00000251             }
00000252 
00000253             disposition = INPUT_PASS_TO_HANDLERS;
00000254         }
00000255         break;
00000256 
00000257     case EV_SW:
00000258         if (is_event_supported(code, dev->swbit, SW_MAX) &&
00000259             !!test_bit(code, dev->sw) != value) {
00000260 
00000261             __change_bit(code, dev->sw);
00000262             disposition = INPUT_PASS_TO_HANDLERS;
00000263         }
00000264         break;
00000265 
00000266     case EV_ABS:
00000267         if (is_event_supported(code, dev->absbit, ABS_MAX))
00000268             disposition = input_handle_abs_event(dev, code, &value);
00000269 
00000270         break;
00000271 
00000272     case EV_REL:
00000273         if (is_event_supported(code, dev->relbit, REL_MAX) && value)
00000274             disposition = INPUT_PASS_TO_HANDLERS;
00000275 
00000276         break;
00000277 
00000278     case EV_MSC:
00000279         if (is_event_supported(code, dev->mscbit, MSC_MAX))
00000280             disposition = INPUT_PASS_TO_ALL;
00000281 
00000282         break;
00000283 
00000284     case EV_LED:
00000285         if (is_event_supported(code, dev->ledbit, LED_MAX) &&
00000286             !!test_bit(code, dev->led) != value) {
00000287 
00000288             __change_bit(code, dev->led);
00000289             disposition = INPUT_PASS_TO_ALL;
00000290         }
00000291         break;
00000292 
00000293     case EV_SND:
00000294         if (is_event_supported(code, dev->sndbit, SND_MAX)) {
00000295 
00000296             if (!!test_bit(code, dev->snd) != !!value)
00000297                 __change_bit(code, dev->snd);
00000298             disposition = INPUT_PASS_TO_ALL;
00000299         }
00000300         break;
00000301 
00000302     case EV_REP:
00000303         if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) {
00000304             dev->rep[code] = value;
00000305             disposition = INPUT_PASS_TO_ALL;
00000306         }
00000307         break;
00000308 
00000309     case EV_FF:
00000310         if (value >= 0)
00000311             disposition = INPUT_PASS_TO_ALL;
00000312         break;
00000313 
00000314     case EV_PWR:
00000315         disposition = INPUT_PASS_TO_ALL;
00000316         break;
00000317     }
00000318 
00000319     if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)
00000320         dev->sync = false;
00000321 
00000322     if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
00000323         dev->event(dev, type, code, value);
00000324 
00000325     if (disposition & INPUT_PASS_TO_HANDLERS)
00000326         input_pass_event(dev, type, code, value);
00000327 }

220至317行,又是一个排比句,根据不同的事件类型执行相应的处理。这里由于type的值为EV_KEY,所以会执行242至255行之间的代码。

242行,if的第一个条件肯定是满足的。

243行,test_bit就是检查dev->key这个数组里的某一位的值,因为在初始化Input设备时这个数组全部被清0,所以test_bit的返回值为0,而value的值为1,因此第二个条件也满足,执行245行,if条件也满足,246行,__change_bit改变dev->key数组里对应位的值,即原来为1的话现在就为0,原来为0的话现在就为1。

247至250行,autorepeat相关的,这里没有用到这个功能,略过。

253行,disposition = INPUT_PASS_TO_HANDLERS,然后就跳出switch,到达319行,很明显,319、322行的if条件都不满足,而325行的if条件满足,所以执行326行的input_pass_event函数。