四、Linux input 子系统学习之input_register_device 分析

我们之前已经说过,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'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函数
这个该函数之前分析过,就不再次分析了。由此可见,无论是先注册驱动,还是先注册设备,最后都会调用的匹配函数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值