你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux input设备驱动

[日期:2012-12-22] 来源:Linux公社  作者:paomadi [字体:大 中 小]

七. 设备的匹配

Linux input设备驱动

1.在输入设备或者输入处理器注册的时候都会遍历全局链表input_handler_list或input_dev_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;

 id = input_match_device(handler, dev); //匹配input_handler和input_dev获取input_device_id
 if (!id)
  return -ENODEV;

 error = handler->connect(handler, dev, id); //匹配到则调用其input_handler的连接方法
 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;
}

2.input_match_device

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

 for (id = handler->id_table; 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) //判断厂商id
    continue;

  if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
   if (id->product != dev->id.product) //判断产品id
    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);

  if (!handler->match || handler->match(handler, dev)) //若input_handler存在match方法则调用其方法
   return id;
 }

 return NULL;
}

八. 事件的处理

1.设备需要上报事件(一般在中断处理函数中上报)

static inline void input_report_key(struct input_dev *dev, unsigned int code, int value) //上报键盘事件
{
 input_event(dev, EV_KEY, code, !!value);
}

static inline void input_report_rel(struct input_dev *dev, unsigned int code, int value) //上报相对位移事件
{
 input_event(dev, EV_REL, code, value);
}

static inline void input_report_abs(struct input_dev *dev, unsigned int code, int value) //上报绝对位移事件
{
 input_event(dev, EV_ABS, code, value);
}

static inline void input_report_ff_status(struct input_dev *dev, unsigned int code, int value) //上报强制反馈事件
{
 input_event(dev, EV_FF_STATUS, code, value);
}

static inline void input_report_switch(struct input_dev *dev, unsigned int code, int value)  //上报开关事件
{
 input_event(dev, EV_SW, code, !!value);
}

static inline void input_sync(struct input_dev *dev) //上报同步事件----在上报完其他事件后必须上报同步事件通知对应的输入处理器
{
 input_event(dev, EV_SYN, SYN_REPORT, 0);
}

2.所有上报事件都会调用到input_event函数

void input_event(struct input_dev *dev,unsigned int type, unsigned int code, int value)
{
 unsigned long flags;
 if (is_event_supported(type, dev->evbit, EV_MAX)) { //判断对应的input_dev是否支持该事件
  spin_lock_irqsave(&dev->event_lock, flags);
  add_input_randomness(type, code, value);
  input_handle_event(dev, NULL, type, code, value); //输入事件处理句柄
  spin_unlock_irqrestore(&dev->event_lock, flags);
 }
}

3.input_handle_event函数

static void input_handle_event(struct input_dev *dev,struct input_handler *src_handler,unsigned int type, unsigned int code, int value)
{
 int disposition = INPUT_IGNORE_EVENT;
 switch (type) { //判断事件类型
 case EV_SYN: //同步事件
  switch (code) {
  case SYN_CONFIG:
   disposition = INPUT_PASS_TO_ALL; //这里有个宏#define INPUT_PASS_TO_ALL (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE)
   break;

  case SYN_REPORT:
   if (!dev->sync) {
    dev->sync = true;
    disposition = INPUT_PASS_TO_HANDLERS;
   }
   break;
  case SYN_MT_REPORT:
   dev->sync = false;
   disposition = INPUT_PASS_TO_HANDLERS;
   break;
  }
  break;

 case EV_KEY: //键盘事件
  if (is_event_supported(code, dev->keybit, KEY_MAX) && !!test_bit(code, dev->key) != value) {

   if (value != 2) {
    __change_bit(code, dev->key);
    if (value)
     input_start_autorepeat(dev, code);
    else
     input_stop_autorepeat(dev);
   }

   disposition = INPUT_PASS_TO_HANDLERS;
  }
  break;

 case EV_SW: //开关事件
  if (is_event_supported(code, dev->swbit, SW_MAX) &&!!test_bit(code, dev->sw) != value) {
   __change_bit(code, dev->sw);
   disposition = INPUT_PASS_TO_HANDLERS;
  }
  break;

 case EV_ABS: //绝对位移事件
  if (is_event_supported(code, dev->absbit, ABS_MAX))
   disposition = input_handle_abs_event(dev, src_handler,code, &value);

  break;

 case EV_REL: //相对位移事件
  if (is_event_supported(code, dev->relbit, REL_MAX) && value)
   disposition = INPUT_PASS_TO_HANDLERS;

  break;

 case EV_MSC: //杂项事件
  if (is_event_supported(code, dev->mscbit, MSC_MAX))
   disposition = INPUT_PASS_TO_ALL;

  break;

 case EV_LED: //led事件
  if (is_event_supported(code, dev->ledbit, LED_MAX) &&
      !!test_bit(code, dev->led) != value) {

   __change_bit(code, dev->led);
   disposition = INPUT_PASS_TO_ALL;
  }
  break;

 case EV_SND: //声音事件
  if (is_event_supported(code, dev->sndbit, SND_MAX)) {

   if (!!test_bit(code, dev->snd) != !!value)
    __change_bit(code, dev->snd);
   disposition = INPUT_PASS_TO_ALL;
  }
  break;

 case EV_REP: //重复事件
  if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) {
   dev->rep[code] = value;
   disposition = INPUT_PASS_TO_ALL;
  }
  break;

 case EV_FF: //强制反馈事件
  if (value >= 0)
   disposition = INPUT_PASS_TO_ALL;
  break;

 case EV_PWR: //电源事件
  disposition = INPUT_PASS_TO_ALL;
  break;
 }

 if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)
  dev->sync = false;

 if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
  dev->event(dev, type, code, value); //根据事件类型不同,部分事件会先调用到input_dev的事件处理函数

 if (disposition & INPUT_PASS_TO_HANDLERS)
  input_pass_event(dev, src_handler, type, code, value); //直接传递事件给对应事件的输入处理器处理
}

4.input_pass_event

static void input_pass_event(struct input_dev *dev,struct input_handler *src_handler,unsigned int type, unsigned int code, int value)
{
 struct input_handler *handler;
 struct input_handle *handle;
 rcu_read_lock();
 handle = rcu_dereference(dev->grab);
 if (handle)
  handle->handler->event(handle, type, code, value); //调用处理器的event方法
 else {
  bool filtered = false;

  list_for_each_entry_rcu(handle, &dev->h_list, d_node) {
   if (!handle->open)
    continue;

  handler = handle->handler;

  if (handler == src_handler)
   continue;

  if (!handler->filter) {
   if (filtered)
    break;
   handler->event(handle, type, code, value);

  } else if (handler->filter(handle, type, code, value))
    filtered = true;
  }
 }

 rcu_read_unlock();
}

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款