Chinaunix首页 | 论坛 | 博客
  • 博客访问: 106894
  • 博文数量: 40
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 337
  • 用 户 组: 普通用户
  • 注册时间: 2013-05-09 23:49
文章分类
文章存档

2016年(4)

2015年(2)

2014年(30)

2013年(4)

我的朋友

分类: LINUX

2016-08-23 11:23:44

  我们之前已经说过,input_register_device 的作用就是注册一个 input_device。
 在分析input_register_device 之前,我们先搜索一下,究竟哪里会调用这个函数你呢。可以看到很多文件都调用了,我们看到一个熟悉的文件ft5x.c。可以想象到,是在设
 备初始化时候,要向input子系统注册一个input_dev,因为只有这样才能用input子系统里面的东西啊,不然怎么用input子系统呢。关于ft5x.c这个文件,是一个典型的触
 摸屏驱动程序,我们最后会分析它。
 
 好了,我们将精力集中回到input_register_device 这个函数分析里面来。
 

点击(此处)折叠或打开

  1. /**
  2.  * input_register_device - register device with input core
  3.  * @dev: device to be registered
  4.  *
  5.  * This function registers device with input core. The device must be
  6.  * allocated with input_allocate_device() and all it's capabilities
  7.  * set up before registering.
  8.  * If function fails the device must be freed with input_free_device().
  9.  * Once device has been successfully registered it can be unregistered
  10.  * with input_unregister_device(); input_free_device() should not be
  11.  * called in this case.
  12.  */
  13. int input_register_device(struct input_dev *dev)
  14. {
  15.     static atomic_t input_no = ATOMIC_INIT(0);
  16.     struct input_handler *handler;
  17.     const char *path;
  18.     int error;
  19.     //解析说的很明白,因为每个input device 都会产生EV_SYN/SYN_REPORT 时间,所以就放在一起去设置了。
  20.     /* Every input device generates EV_SYN/SYN_REPORT events. */
  21.     __set_bit(EV_SYN, dev->evbit);

  22.     /* KEY_RESERVED is not supposed to be transmitted to userspace. */
  23.     __clear_bit(KEY_RESERVED, dev->keybit);
  24.     //没有设置的位,保证被清零
  25.     /* Make sure that bitmasks not mentioned in dev->evbit are clean. */
  26.     input_cleanse_bitmasks(dev);
  27.     //这个函数的意义是,获取每个包有多少个event,不太明白
  28.     if (!dev->hint_events_per_packet)
  29.         dev->hint_events_per_packet =
  30.                 input_estimate_events_per_packet(dev);
  31.     //下面这段,不太明白
  32.     /*
  33.      * If delay and period are pre-set by the driver, then autorepeating
  34.      * is handled by the driver itself and we don't do it in input.c.
  35.      */
  36.     init_timer(&dev->timer);
  37.     if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
  38.         dev->timer.data = (long) dev;
  39.         dev->timer.function = input_repeat_key;
  40.         dev->rep[REP_DELAY] = 250;
  41.         dev->rep[REP_PERIOD] = 33;
  42.     }
  43.     //获取按键值默认函数
  44.     if (!dev->getkeycode)
  45.         dev->getkeycode = input_default_getkeycode;
  46.     //设置按键值默认函数
  47.     if (!dev->setkeycode)
  48.         dev->setkeycode = input_default_setkeycode;

  49.     dev_set_name(&dev->dev, "input%ld",
  50.          (unsigned long) atomic_inc_return(&input_no) - 1);
  51.     //其实不太明白,这里创建了什么,因为和自己想像的不一样
  52.     error = device_add(&dev->dev);
  53.     if (error)
  54.         return error;

  55.     path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
  56.     pr_info("%s as %s\n",
  57.         dev->name ? dev->name : "Unspecified device",
  58.         path ? path : "N/A");
  59.     kfree(path);

  60.     error = mutex_lock_interruptible(&input_mutex);
  61.     if (error) {
  62.         device_del(&dev->dev);
  63.         return error;
  64.     }
  65.     //将dev挂入input_dev_list设备链表
  66.     list_add_tail(&dev->node, &input_dev_list);
  67.     //遍历驱动链表input_handler_list,对立面的每一个handler,看是否匹配
  68.     list_for_each_entry(handler, &input_handler_list, node)
  69.         input_attach_handler(dev, handler);

  70.     input_wakeup_procfs_readers();

  71.     mutex_unlock(&input_mutex);

  72.     return 0;
  73. }
  如上面所示,会把dev挂入input_dev_list链表,然后会遍历input_handler_list链表,对链表里面的每个handler 进行匹配,如果匹配成功就调用到input_attach_handler函数
这个该函数之前分析过,就不再次分析了。由此可见,无论是先注册驱动,还是先注册设备,最后都会调用的匹配函数。
  
 
阅读(3253) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~