Chinaunix首页 | 论坛 | 博客
  • 博客访问: 174085
  • 博文数量: 44
  • 博客积分: 627
  • 博客等级: 中士
  • 技术积分: 345
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-20 21:55
文章分类

全部博文(44)

文章存档

2012年(44)

分类: LINUX

2012-08-08 17:04:52

输入子系统设备模型分析  

2010-08-13 11:26:23|  分类: arm linux设备驱 |  标签: |字号 

在文件input.c中定义并初始化了两个链表头:

static LIST_HEAD(input_dev_list);
static LIST_HEAD(input_handler_list);

系统中注册的所有输入设备都挂到链表input_dev_list上,所有注册的input_handler都挂到链表input_handler_list上。

 

input_handler结构体原型如下:

这个结构体的各成员先不讲解,但我们看到了我们最想看到的文件操作函数集const struct file_operations *fops;

struct input_handler {

 void *private;

 void (*event)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
 int (*connect)(struct input_handler *handler, struct input_dev *dev, const struct input_device_id *id);
 void (*disconnect)(struct input_handle *handle);
 void (*start)(struct input_handle *handle);

 const struct file_operations *fops;
 int minor;
 const char *name;

 const struct input_device_id *id_table;
 const struct input_device_id *blacklist;

 struct list_head h_list;
 struct list_head node;
};

 

static struct input_handler *input_table[8];

这表示在整个系统中允许有8个input_handler,这8个input_handler就可以处理所有的输入事件。

每个input_handler可以同时与32个input_dev匹配。因为在文件input.c中又有这样的定义

#define INPUT_DEVICES 256

 

所有的输入设备有一个共同的主设备号INPUT_MAJOR,在文件major.h中有

#define INPUT_MAJOR  13

该设备号的注册是在文件input.c中完成的:

static int __init input_init(void)
{
 int err;

 input_init_abs_bypass();  //后面遇到了再讲

 err = class_register(&input_class); //创建一个类input_class
。。。。。。

 err = input_proc_init();  //在文件系统proc下创建相应目录
 if (err)
  goto fail1;

//注册设备号INPUT_MAJOR,并关联文件操作函数集input_fops。

 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);  
。。。。。。
}

文件操作函数集的实现如下:

static const struct file_operations input_fops = {
 .owner = THIS_MODULE,
 .open = input_open_file,
};

 

static int input_open_file(struct inode *inode, struct file *file)
{
 struct input_handler *handler;
 const struct file_operations *old_fops, *new_fops = NULL;
 int err;

 lock_kernel();
///根据次设备号在结构体数组 input_table[]中找出对应的handler。
 handler = input_table[iminor(inode) >> 5];
 if (!handler || !(new_fops = fops_get(handler->fops))) {
  err = -ENODEV;
  goto out;
 }

。。。。。。
 file->f_op = new_fops;  //切换到handler->fops

 err = new_fops->open(inode, file);  //真正的打开操作。

 。。。。。。
}

 

下图清楚地展示了输入设备模型的建立过程:

输入子系统设备模型分析 - 尘 - 学习点滴
 

 在讲解图中各函数之前先来看一个重要结构体:

struct input_dev {
 const char *name;
 const char *phys;
 const char *uniq;
 struct input_id id;  //与input_handler匹配时会用到

 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)]; // LED灯事件支持的子事件
 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];

// 数组rep[[]有两个元素rep[REP_DELAY ] 和rep[REP_PERIOD] 。这些值跟重复按键有关。比如一个键被按下

//3秒开始算重复按键,3秒之后每隔1秒检测一次如果还被按下就算重复按下。这个3秒就是rep[REP_DELAY ],

//这个1秒就是rep[REP_PERIOD]。
 int rep[REP_MAX + 1];

 unsigned long key[BITS_TO_LONGS(KEY_CNT)]; //按键有两种状态,按下和抬起,这个字段就是记录这两个状态。
 unsigned long led[BITS_TO_LONGS(LED_CNT)]; //同上
 unsigned long snd[BITS_TO_LONGS(SND_CNT)]; //同上
 unsigned long sw[BITS_TO_LONGS(SW_CNT)]; //同上

 int absmax[ABS_MAX + 1]; // 绝对坐标事件value的最大值
 int absmin[ABS_MAX + 1]; // 绝对坐标事件value的最小值

//绝对坐标事件的触发可能存在干扰,或value偏离上一次较大的情况,一下两个字段就是为了滤除干扰

//取出一个合理值而设的。
 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);
 int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value);

//结构体input_handle中成员handler指向了结构体input_handler,一个input_dev 可能匹配多个input_handler,

//这些被input_dev匹配的每一个input_handler都对应一个input_handle挂在input_dev的struct list_head h_list上。
//此处的grab是当前使用的input_handle。

 struct input_handle *grab;

 spinlock_t event_lock;
 struct mutex mutex;

 unsigned int users;
 int going_away;

 struct device dev;

 struct list_head h_list; //用于挂接input_handle。
 struct list_head node;
};

//输入设备支持的事件类型
       //EV_SYN      同步事件

       //EV_KEY       键盘事件

       //EV_REL       相对坐标事件,用于鼠标

       //EV_ABS       绝对坐标事件,用于摇杆

       //EV_MSC      其他事件

      // EV_SW      开关事件

       //EV_LED       LED灯事件

       //EV_SND      声音事件

       //EV_REP       重复按键事件

       //EV_FF         受力事件

       //EV_PWR      电源事件

       //EV_FF_STATUS  受力状态事件

 

输入设备模型的建立过程函数讲解:

输入设备的注册:

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);

 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;  //当按下一个键250个时间单位后算作重复按下该键。
  dev->rep[REP_PERIOD] = 33; //250个时间单位之后,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);  //将input_dev对应的设备添加到内核
 if (error)
  return error;

 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);  //用来获得引起事件的kobject在sysfs中的路径
 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;
 }

 list_add_tail(&dev->node, &input_dev_list);  //将注册的input_dev 添加到链表input_dev_list上。

 //在链表input_handler_list上找一个能匹配新添加的input_dev的handler。

 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_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;
  }

 //根据次设备号将新加入的handler添加到结构体数组input_table[]对应的位置。
  input_table[handler->minor >> 5] = handler; 

 }

 list_add_tail(&handler->node, &input_handler_list);  //将新注册的input_handler添加到链表input_handler_list上。

 list_for_each_entry(dev, &input_dev_list, node) //在链表input_dev_list上找出能和新加入handler匹配的input_dev。
  input_attach_handler(dev, handler);

 input_wakeup_procfs_readers();

 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;

//input_handler有一个黑名单handler->blacklist,如果input_dev 得id列入其中,该input_dev 就不能和handler匹配。

 if (handler->blacklist && input_match_device(handler->blacklist, dev))
  return -ENODEV;

 id = input_match_device(handler->id_table, dev);       【1】 请看下面讲解
 if (!id)
  return -ENODEV;

 error = handler->connect(handler, dev, id);              【2】请看下面讲解
 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;
}

 

【1】

static const struct input_device_id *input_match_device(const struct input_device_id *id, struct input_dev *dev)
{
 int i;

//下面便是input_handler 的id和input_dev的id各字段的匹配过程,比较简单就不详述了。

 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(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);

  return id;
 }

 return NULL;
}

 

【2】

当input_handler 和input_dev匹配之后调用函数 handler->connect(handler, dev, id);。

在此我们以evdev_handler为例。在文件evdev.c中有

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,
};

//我们总算看到了让人兴奋的结构,文件操作函数集evdev_fops。关于它我们后面遇到了再讲。

每一个input_handler 和input_dev的匹配都会导致一个设备节点的创建,EVDEV_MINOR_BASE就是

与evdev_handler匹配的所有input_handler 创建设备节点时以EVDEV_MINOR_BASE为起始递增。

在文件evdev.c中有这样的定义:

#define EVDEV_MINOR_BASE 64   //与evdev_handler相关的设备此设备号的起始值
#define EVDEV_MINORS  32    //evdev_handler支持同时匹配32个input_dev

evdev_handler的id_table定义如下:

static const struct input_device_id evdev_ids[] = {
 { .driver_info = 1 }, /* Matches all devices */
 { },   /* Terminating zero entry */
};

这样定义的id_table与所有input_dev的id都是匹配的。

 

每一个input_dev与evdev_handler 匹配都将创建一个结构体evdev ,这些evdev的指针都放在结构体数组

evdev_table[EVDEV_MINORS]中。

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])    //在evdev_table[minor]中找出一个未被占用的位置。
   break;

 if (minor == EVDEV_MINORS) {
  printk(KERN_ERR "evdev: no more free evdev devices\n");
  return -ENFILE;
 }

 evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);  //为结构体 evdev分配内存。
 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);  //将要创建的设备节点名为"event%d"
 evdev->exist = 1;
 evdev->minor = minor; //决定了此设备号。

//结构体handle连接了input_handler和 input_dev,还有evdev。

 evdev->handle.dev = input_get_device(dev);
 evdev->handle.name = evdev->name;
 evdev->handle.handler = handler;  
 evdev->handle.private = evdev;

 dev_set_name(&evdev->dev, evdev->name);
 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->handle的注册主要完成以下两项工作。

list_add_tail_rcu(&handle->d_node, &dev->h_list);

list_add_tail(&handle->h_node, &handler->h_list);

*/

 error = input_register_handle(&evdev->handle);
 if (error)
  goto err_free_evdev;

/*

以下函数就干了这一件事

evdev_table[evdev->minor] = 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;
}

到此输入子系统的设备模型就建立起来了,上面没讲到的将在下一篇博文

《输入事件的传递过程》中讲到。

阅读(1228) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~