现在的位置: 首页 > 综合 > 正文

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

2013年09月15日 ⁄ 综合 ⁄ 共 7204字 ⁄ 字号 评论关闭

一:前言 
在键盘驱动代码分析的笔记中,接触到了input子系统.键盘驱动,键盘驱动将检测到的所有按键都上报给了input子系统。Input子系统是所有I/O设备驱动的中间层,为上层提供了一个统一的界面。例如,在终端系统中,我们不需要去管有多少个键盘,多少个鼠标。它只要从input子系统中去取对应的事件(按键,鼠标移位等)就可以了。今天就对input子系统做一个详尽的分析. 
下面的代码是基于linux kernel 2.6.25.分析的代码主要位于kernel2.6.25/drivers/input下面. 
二:使用input子系统的例子 
在内核自带的文档Documentation/input/input-programming.txt中。有一个使用input子系统的例子,并附带相应的说明。以此为例分析如下: 
#include <linux/input.h> 
#include <linux/module.h> 
#include <linux/init.h> 
  
#include <asm/irq.h> 
#include <asm/io.h> 
  
static void button_interrupt(int irq, void *dummy, struct pt_regs *fp) 

        input_report_key(&button_dev, BTN_1, inb(BUTTON_PORT) & 1); 
        input_sync(&button_dev); 

  
static int __init button_init(void) 

        if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) { 
                printk(KERN_ERR "button.c: Can't allocate irq %d/n", button_irq); 
                return -EBUSY; 
        } 
  
        button_dev.evbit[0] = BIT(EV_KEY); 
        button_dev.keybit[LONG(BTN_0)] = BIT(BTN_0); 
  
        input_register_device(&button_dev); 

  
static void __exit button_exit(void) 

        input_unregister_device(&button_dev); 
        free_irq(BUTTON_IRQ, button_interrupt); 

  
module_init(button_init); 
module_exit(button_exit); 
  
这个示例module代码还是比较简单,在初始化函数里注册了一个中断处理例程。然后注册了一个input device.在中断处理程序里,将接收到的按键上报给input子系统。 
文档的作者在之后的分析里又对这个module作了优化。主要是在注册中断处理的时序上。在修改过后的代码里,为input device定义了open函数,在open的时候再去注册中断处理例程。具体的信息请自行参考这篇文档。在资料缺乏的情况下,kernel自带的文档就是剖析kernel相关知识的最好资料. 
文档的作者还分析了几个api函数。列举如下: 
  
1):set_bit(EV_KEY, button_dev.evbit); 
   set_bit(BTN_0, button_dev.keybit); 
分别用来设置设备所产生的事件以及上报的按键值。Struct iput_dev中有两个成员,一个是evbit.一个是keybit.分别用表示设备所支持的动作和按键类型。 
2): input_register_device(&button_dev); 
用来注册一个input device. 
3): input_report_key() 
用于给上层上报一个按键动作 
4): input_sync() 
用来告诉上层,本次的事件已经完成了. 
5): NBITS(x) - returns the length of a bitfield array in longs for x bits 
    LONG(x)  - returns the index in the array in longs for bit x 
BIT(x)   - returns the index in a long for bit x      
这几个宏在input子系统中经常用到。上面的英文解释已经很清楚了。 
  
三:input设备注册分析. 
Input设备注册的接口为:input_register_device()。代码如下: 
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; 
  
         __set_bit(EV_SYN, dev->evbit); 
  
         /* 
          * 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; 
         } 
在前面的分析中曾分析过。Input_device的evbit表示该设备所支持的事件。在这里将其EV_SYN置位,即所有设备都支持这个事件.如果dev->rep[REP_DELAY]和dev->rep[REP_PERIOD]没有设值,则将其赋默认值。这主要是处理重复按键的. 
  
         if (!dev->getkeycode) 
                   dev->getkeycode = input_default_getkeycode; 
  
         if (!dev->setkeycode) 
                   dev->setkeycode = input_default_setkeycode; 
  
         snprintf(dev->dev.bus_id, sizeof(dev->dev.bus_id), 
                    "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 device没有定义getkeycode和setkeycode.则将其赋默认值。还记得在键盘驱动中的分析吗?这两个操作函数就可以用来取键的扫描码和设置键的扫描码。然后调用device_add()将input_dev中封装的device注册到sysfs 
  
         list_add_tail(&dev->node, &input_dev_list); 
  
         list_for_each_entry(handler, &input_handler_list, node) 
                   input_attach_handler(dev, handler); 
  
         input_wakeup_procfs_readers(); 
  
         mutex_unlock(&input_mutex); 
  
         return 0; 

这里就是重点了。将input device 挂到input_dev_list链表上.然后,对每一个挂在input_handler_list的handler调用input_attach_handler().在这里的情况有好比设备模型中的device和driver的匹配。所有的input device都挂在input_dev_list链上。所有的handle都挂在input_handler_list上。 
看一下这个匹配的详细过程。匹配是在input_attach_handler()中完成的。代码如下: 
static int input_attach_handler(struct input_dev *dev, struct input_handler *handler) 

         const struct input_device_id *id; 
         int error; 
  
         if (handler->blacklist && input_match_device(handler->blacklist, dev)) 
                   return -ENODEV; 
  
         id = input_match_device(handler->id_table, dev); 
         if (!id) 
                   return -ENODEV; 
  
         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; 

如果handle的blacklist被赋值。要先匹配blacklist中的数据跟dev->id的数据是否匹配。匹配成功过后再来匹配handle->id和dev->id中的数据。如果匹配成功,则调用handler->connect(). 
来看一下具体的数据匹配过程,这是在input_match_device()中完成的。代码如下: 
static const struct input_device_id *input_match_device(const struct input_device_id *id, 
                                                                 struct input_dev *dev) 

         int i; 
  
         for (; 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(,, 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); 
  
                   return id; 
         } 
  
         return NULL; 

MATCH_BIT宏的定义如下: 
#define MATCH_BIT(bit, max) 
                   for (i = 0; i < BITS_TO_LONGS(max); i++) 
                            if ((id->bit[i] & dev->bit[i]) != id->bit[i]) 
                                     break; 
                   if (i != BITS_TO_LONGS(max)) 
                            continue; 
  
由此看到。在id->flags中定义了要匹配的项。定义INPUT_DEVICE_ID_MATCH_BUS。则是要比较input device和input handler的总线类型。INPUT_DEVICE_ID_MATCH_VENDOR,INPUT_DEVICE_ID_MATCH_PRODUCT,INPUT_DEVICE_ID_MATCH_VERSION分别要求设备厂商。设备号和设备版本. 
如果id->flags定义的类型匹配成功。或者是id->flags没有定义,就会进入到MATCH_BIT的匹配项了.从MATCH_BIT宏的定义可以看出。只有当iput device和input handler的id成员在evbit, keybit,… swbit项相同才会匹配成功。而且匹配的顺序是从evbit, keybit到swbit.只要有一项不同,就会循环到id中的下一项进行比较. 
简而言之,注册input device的过程就是为input device设置默认值,并将其挂以input_dev_list.与挂载在input_handler_list中的handler相匹配。如果匹配成功,就会调用handler的connect函数. 

抱歉!评论已关闭.