当前位置:   article > 正文

USB驱动之Android usb鼠标驱动_安卓鼠标驱动

安卓鼠标驱动

1. 前言

        HID是Human Interface Devices的缩写,翻译成中文即为人机交互设备。这里的人机交互设备是一个宏观上面的概念,任何设备只要符合HID spec都可以称之为HID设备,常见的HID设备有鼠标键盘,游戏操纵杆等等。

        usb鼠标在android代码中没有使用linux中常用的drivers/hid/usbhid/usbmouse.c驱动,而是使用了hid-generic驱动【注:从内核配置可知,内核选项配置了CONFIG_HID,CONFIG_USB_HID,CONFIG_HID_GENERIC,但是没有配置CONFIG_USB_KBD,CONFIG_USB_MOUSE选项】。

        注意有两个hid-core.c文件,分别为hid/hid-core.c和hid/usbhid/hid-core.c文件。前者注册hid总线,后者注册hid device。

2. hid bus

        在内核启动时,注册了hid总线驱动,在drivers/hid/hid-core.c中注册了一个名为hid_bus_type的hid总线。

  1. static int __init hid_init(void)
  2. {
  3. int ret;
  4. ret = bus_register(&hid_bus_type);//---->
  5. ret = hidraw_init();
  6. hid_debug_init();
  7. return 0;
  8. }
  9. static struct bus_type hid_bus_type = {
  10. .name = "hid",
  11. .dev_groups = hid_dev_groups,
  12. .drv_groups = hid_drv_groups,
  13. .match = hid_bus_match,
  14. .probe = hid_device_probe,
  15. .remove = hid_device_remove,
  16. .uevent = hid_uevent,
  17. };

3. hid driver

        在hid-generic.c中定义了module_hid_driver(hid_generic),这个宏实际上是调用__hid_register_driver(drivers/hid/)接口注册一个hid_driver,并把它挂接在hid_bus_type总线驱动链表上。

  1. static const struct hid_device_id hid_table[] = {
  2. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_GENERIC, HID_ANY_ID, HID_ANY_ID) },
  3. { }
  4. };
  5. static struct hid_driver hid_generic = {
  6. .name = "hid-generic",
  7. .id_table = hid_table,
  8. };
  9. module_hid_driver(hid_generic);
  10. //usb鼠标将和hid_generic匹配

4. hid device

        在drivers/hid/usbhid/hid-core.c中定义了hid_driver,它是个usb_driver,并且调用usb_register_driver接口注册到了usb总线(usb_bus_type)上。

  1. static int __init hid_init(void)
  2. {
  3. int retval = -ENOMEM;
  4. retval = usbhid_quirks_init(quirks_param);
  5. retval = usb_register(&hid_driver); //---->
  6. return 0;
  7. }
  8. static struct usb_driver hid_driver = {
  9. .name = "usbhid",
  10. .probe = usbhid_probe,
  11. .disconnect = usbhid_disconnect,
  12. #ifdef CONFIG_PM
  13. .suspend = hid_suspend,
  14. .resume = hid_resume,
  15. .reset_resume = hid_reset_resume,
  16. #endif
  17. .pre_reset = hid_pre_reset,
  18. .post_reset = hid_post_reset,
  19. .id_table = hid_usb_ids,
  20. .supports_autosuspend = 1,
  21. };
  22. static const struct usb_device_id hid_usb_ids[] = {
  23. { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
  24. .bInterfaceClass = USB_INTERFACE_CLASS_HID },//所有ClassID为USB_INTERFACE_CLASS_HID的设备都会被这个驱动所匹配.所以,所有USB HID设备都会由这个module来驱动。
  25. { } /* Terminating entry */
  26. };

        当hid设备插入usb口以后会调用usb_driver的probe函数,这一点由《USB驱动》可知。

  1. static int usbhid_probe(struct usb_interface *intf, const struct usb_device_id *id)
  2. {
  3. struct usb_host_interface *interface = intf->cur_altsetting;
  4. struct usb_device *dev = interface_to_usbdev(intf);
  5. struct usbhid_device *usbhid;
  6. struct hid_device *hid;
  7. unsigned int n, has_in = 0;
  8. size_t len;
  9. int ret;
  10. dbg_hid("HID probe called for ifnum %d\n",
  11. intf->altsetting->desc.bInterfaceNumber);
  12. for (n = 0; n < interface->desc.bNumEndpoints; n++)
  13. if (usb_endpoint_is_int_in(&interface->endpoint[n].desc))
  14. has_in++;
  15. if (!has_in) {
  16. hid_err(intf, "couldn't find an input interrupt endpoint\n");
  17. return -ENODEV;
  18. }
  19. //分配一个struct hid_device设备
  20. hid = hid_allocate_device();
  21. //把这个接口和hid设备关联
  22. usb_set_intfdata(intf, hid);
  23. //安装hid底层驱动,其实是个回调usb hid的回调驱动函数集,具体硬件操作依靠它来实现,hid core层(而不是usb hid core层)回调它。
  24. hid->ll_driver = &usb_hid_driver;
  25. hid->ff_init = hid_pidff_init;
  26. #ifdef CONFIG_USB_HIDDEV
  27. hid->hiddev_connect = hiddev_connect;
  28. hid->hiddev_disconnect = hiddev_disconnect;
  29. hid->hiddev_hid_event = hiddev_hid_event;
  30. hid->hiddev_report_event = hiddev_report_event;
  31. #endif
  32. hid->dev.parent = &intf->dev;
  33. hid->bus = BUS_USB;
  34. hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
  35. hid->product = le16_to_cpu(dev->descriptor.idProduct);
  36. hid->name[0] = 0;
  37. hid->quirks = usbhid_lookup_quirk(hid->vendor, hid->product);
  38. if (intf->cur_altsetting->desc.bInterfaceProtocol ==
  39. USB_INTERFACE_PROTOCOL_MOUSE)
  40. hid->type = HID_TYPE_USBMOUSE;
  41. else if (intf->cur_altsetting->desc.bInterfaceProtocol == 0)
  42. hid->type = HID_TYPE_USBNONE;
  43. if (dev->manufacturer)
  44. strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
  45. if (dev->product) {
  46. if (dev->manufacturer)
  47. strlcat(hid->name, " ", sizeof(hid->name));
  48. strlcat(hid->name, dev->product, sizeof(hid->name));
  49. }
  50. if (!strlen(hid->name))
  51. snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
  52. le16_to_cpu(dev->descriptor.idVendor),
  53. le16_to_cpu(dev->descriptor.idProduct));
  54. usb_make_path(dev, hid->phys, sizeof(hid->phys));
  55. strlcat(hid->phys, "/input", sizeof(hid->phys));
  56. len = strlen(hid->phys);
  57. if (len < sizeof(hid->phys) - 1)
  58. snprintf(hid->phys + len, sizeof(hid->phys) - len,
  59. "%d", intf->altsetting[0].desc.bInterfaceNumber);
  60. if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
  61. hid->uniq[0] = 0;
  62. //分配一个usbhid设备,同时也是一个hid_device,继承hid_device
  63. usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL);
  64. hid->driver_data = usbhid;
  65. usbhid->hid = hid;
  66. usbhid->intf = intf;
  67. usbhid->ifnum = interface->desc.bInterfaceNumber;
  68. init_waitqueue_head(&usbhid->wait);
  69. INIT_WORK(&usbhid->reset_work, hid_reset);
  70. setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
  71. spin_lock_init(&usbhid->lock);
  72. //向hid核心层添加一个hid_device设备,同时也会获取HID report报告描述符
  73. ret = hid_add_device(hid);
  74. return 0;
  75. err_free:
  76. kfree(usbhid);
  77. err:
  78. hid_destroy_device(hid);
  79. return ret;
  80. }
  81. int hid_add_device(struct hid_device *hdev)
  82. {
  83. static atomic_t id = ATOMIC_INIT(0);
  84. int ret;
  85. if (WARN_ON(hdev->status & HID_STAT_ADDED))
  86. return -EBUSY;
  87. /* we need to kill them here, otherwise they will stay allocated to
  88. * wait for coming driver */
  89. if (hid_ignore(hdev))
  90. return -ENODEV;
  91. /*
  92. * Check for the mandatory transport channel.
  93. */
  94. if (!hdev->ll_driver->raw_request) {
  95. hid_err(hdev, "transport driver missing .raw_request()\n");
  96. return -EINVAL;
  97. }
  98. /*
  99. * Read the device report descriptor once and use as template
  100. * for the driver-specific modifications.
  101. */
  102. ret = hdev->ll_driver->parse(hdev);
  103. if (ret)
  104. return ret;
  105. if (!hdev->dev_rdesc)
  106. return -ENODEV;
  107. /*
  108. * Scan generic devices for group information
  109. */
  110. if (hid_ignore_special_drivers) {
  111. hdev->group = HID_GROUP_GENERIC;
  112. } else if (!hdev->group &&
  113. !hid_match_id(hdev, hid_have_special_driver)) {
  114. ret = hid_scan_report(hdev);
  115. if (ret)
  116. hid_warn(hdev, "bad device descriptor (%d)\n", ret);
  117. }
  118. /* XXX hack, any other cleaner solution after the driver core
  119. * is converted to allow more than 20 bytes as the device name? */
  120. dev_set_name(&hdev->dev, "%04X:%04X:%04X.%04X", hdev->bus,
  121. hdev->vendor, hdev->product, atomic_inc_return(&id));
  122. hid_debug_register(hdev, dev_name(&hdev->dev));
  123. //这里会调用hid_bus的hid_device_probe函数
  124. ret = device_add(&hdev->dev);
  125. if (!ret)
  126. hdev->status |= HID_STAT_ADDED;
  127. else
  128. hid_debug_unregister(hdev);
  129. return ret;
  130. }
  131. static int hid_device_probe(struct device *dev)
  132. {
  133. struct hid_driver *hdrv = to_hid_driver(dev->driver);
  134. struct hid_device *hdev = to_hid_device(dev);
  135. const struct hid_device_id *id;
  136. int ret = 0;
  137. if (down_interruptible(&hdev->driver_input_lock)) {
  138. ret = -EINTR;
  139. goto end;
  140. }
  141. hdev->io_started = false;
  142. //第1次添加hid_device时,一定是为空 
  143. if (!hdev->driver) {
  144. //在注册hid_device时就会调用hid_bus_type总线的match函数,这里再调用一次
  145. id = hid_match_device(hdev, hdrv);
  146. if (id == NULL) {
  147. ret = -ENODEV;
  148. goto unlock;
  149. }
  150. hdev->driver = hdrv;
  151. //hid_generic驱动没有probe函数
  152. if (hdrv->probe) {
  153. ret = hdrv->probe(hdev, id);
  154. } else { /* default probe 会调用这个函数,这里会对hid_add_device函数中获取到的HID report描述符进行解析*/
  155. ret = hid_open_report(hdev);
  156. if (!ret)
  157. //调用hid_ll_driver相关接口
  158. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); //---->
  159. }
  160. if (ret) {
  161. hid_close_report(hdev);
  162. hdev->driver = NULL;
  163. }
  164. }
  165. unlock:
  166. if (!hdev->io_started)
  167. up(&hdev->driver_input_lock);
  168. end:
  169. return ret;
  170. }
  1. int hid_hw_start(struct hid_device *hdev, unsigned int connect_mask)
  2. {
  3. int error;
  4. error = hdev->ll_driver->start(hdev); //启动设备,
  5. if (error)
  6. return error;
  7. if (connect_mask) {
  8. error = hid_connect(hdev, connect_mask); //将设备与HID框架关联起来---->
  9. if (error) {
  10. hdev->ll_driver->stop(hdev);
  11. return error;
  12. }
  13. }
  14. return 0;
  15. }
  16. int hid_connect(struct hid_device *hdev, unsigned int connect_mask)
  17. {
  18. static const char *types[] = { "Device", "Pointer", "Mouse", "Device",
  19. "Joystick", "Gamepad", "Keyboard", "Keypad",
  20. "Multi-Axis Controller"
  21. };
  22. const char *type, *bus;
  23. char buf[64] = "";
  24. unsigned int i;
  25. int len;
  26. int ret;
  27. if (hdev->quirks & HID_QUIRK_HIDDEV_FORCE)
  28. connect_mask |= (HID_CONNECT_HIDDEV_FORCE | HID_CONNECT_HIDDEV);
  29. if (hdev->quirks & HID_QUIRK_HIDINPUT_FORCE)
  30. connect_mask |= HID_CONNECT_HIDINPUT_FORCE;
  31. if (hdev->bus != BUS_USB) //如果不是USB总线,那么去掉HID_CONNECT_HIDDEV标记
  32. connect_mask &= ~HID_CONNECT_HIDDEV;
  33. if (hid_hiddev(hdev)) //匹配某些特定vendorID和productID
  34. connect_mask |= HID_CONNECT_HIDDEV_FORCE;
  35. if ((connect_mask & HID_CONNECT_HIDINPUT) && !hidinput_connect(hdev, //input设备--->
  36. connect_mask & HID_CONNECT_HIDINPUT_FORCE))
  37. hdev->claimed |= HID_CLAIMED_INPUT;
  38. if ((connect_mask & HID_CONNECT_HIDDEV) && hdev->hiddev_connect &&
  39. !hdev->hiddev_connect(hdev, //hiddev设备
  40. connect_mask & HID_CONNECT_HIDDEV_FORCE))
  41. hdev->claimed |= HID_CLAIMED_HIDDEV;
  42. if ((connect_mask & HID_CONNECT_HIDRAW) && !hidraw_connect(hdev)) //hidraw设备
  43. hdev->claimed |= HID_CLAIMED_HIDRAW;
  44. if (connect_mask & HID_CONNECT_DRIVER)
  45. hdev->claimed |= HID_CLAIMED_DRIVER;
  46. /* Drivers with the ->raw_event callback set are not required to connect
  47. * to any other listener. */
  48. if (!hdev->claimed && !hdev->driver->raw_event) {
  49. hid_err(hdev, "device has no listeners, quitting\n");
  50. return -ENODEV;
  51. }
  52. if ((hdev->claimed & HID_CLAIMED_INPUT) &&
  53. (connect_mask & HID_CONNECT_FF) && hdev->ff_init)
  54. hdev->ff_init(hdev);
  55. len = 0;
  56. if (hdev->claimed & HID_CLAIMED_INPUT)
  57. len += sprintf(buf + len, "input");
  58. if (hdev->claimed & HID_CLAIMED_HIDDEV)
  59. len += sprintf(buf + len, "%shiddev%d", len ? "," : "",
  60. ((struct hiddev *)hdev->hiddev)->minor);
  61. if (hdev->claimed & HID_CLAIMED_HIDRAW)
  62. len += sprintf(buf + len, "%shidraw%d", len ? "," : "",
  63. ((struct hidraw *)hdev->hidraw)->minor);
  64. type = "Device";
  65. for (i = 0; i < hdev->maxcollection; i++) {
  66. struct hid_collection *col = &hdev->collection[i];
  67. if (col->type == HID_COLLECTION_APPLICATION &&
  68. (col->usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
  69. (col->usage & 0xffff) < ARRAY_SIZE(types)) {
  70. type = types[col->usage & 0xffff];
  71. break;
  72. }
  73. }
  74. switch (hdev->bus) {
  75. case BUS_USB:
  76. bus = "USB";
  77. break;
  78. case BUS_BLUETOOTH:
  79. bus = "BLUETOOTH";
  80. break;
  81. case BUS_I2C:
  82. bus = "I2C";
  83. break;
  84. default:
  85. bus = "<UNKNOWN>";
  86. }
  87. ret = device_create_file(&hdev->dev, &dev_attr_country);
  88. if (ret)
  89. hid_warn(hdev,
  90. "can't create sysfs country code attribute err: %d\n", ret);
  91. hid_info(hdev, "%s: %s HID v%x.%02x %s [%s] on %s\n",
  92. buf, bus, hdev->version >> 8, hdev->version & 0xff,
  93. type, hdev->name, hdev->phys);
  94. return 0;
  95. }

        HID中最常用的是input设备,使用hidinput_connect登记到系统。hidinput_connect的主要作用是对hiddev中的每一个report,都建立一个input_dev设备,并登记到input框架中。

  1. int hidinput_connect(struct hid_device *hid, unsigned int force)
  2. {
  3. struct hid_driver *drv = hid->driver;
  4. struct hid_report *report;
  5. struct hid_input *next, *hidinput = NULL;
  6. int i, k;
  7. INIT_LIST_HEAD(&hid->inputs);
  8. INIT_WORK(&hid->led_work, hidinput_led_worker);
  9. if (!force) {
  10. for (i = 0; i < hid->maxcollection; i++) {
  11. struct hid_collection *col = &hid->collection[i];
  12. if (col->type == HID_COLLECTION_APPLICATION ||
  13. col->type == HID_COLLECTION_PHYSICAL)
  14. if (IS_INPUT_APPLICATION(col->usage))
  15. break;
  16. }
  17. if (i == hid->maxcollection)
  18. return -1;
  19. }
  20. report_features(hid);
  21. //对每一个report,建立一个input设备
  22. for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
  23. if (k == HID_OUTPUT_REPORT &&
  24. hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORTS)
  25. continue;
  26. list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
  27. if (!report->maxfield)
  28. continue;
  29. /*
  30. * Find the previous hidinput report attached
  31. * to this report id.
  32. */
  33. if (hid->quirks & HID_QUIRK_MULTI_INPUT)
  34. hidinput = hidinput_match(report);
  35. if (!hidinput) {
  36. hidinput = hidinput_allocate(hid);
  37. if (!hidinput)
  38. goto out_unwind;
  39. }
  40. hidinput_configure_usages(hidinput, report); //---->
  41. if (hid->quirks & HID_QUIRK_MULTI_INPUT)
  42. hidinput->report = report;
  43. }
  44. }
  45. list_for_each_entry_safe(hidinput, next, &hid->inputs, list) {
  46. if ((hid->quirks & HID_QUIRK_NO_EMPTY_INPUT) &&
  47. !hidinput_has_been_populated(hidinput)) {
  48. /* no need to register an input device not populated */
  49. hidinput_cleanup_hidinput(hid, hidinput);
  50. continue;
  51. }
  52. if (drv->input_configured &&
  53. drv->input_configured(hid, hidinput))
  54. goto out_unwind;
  55. if (input_register_device(hidinput->input)) //将设备注册到Input子系统
  56. goto out_unwind;
  57. hidinput->registered = true;
  58. }
  59. if (list_empty(&hid->inputs)) {
  60. hid_err(hid, "No inputs registered, leaving\n");
  61. goto out_unwind;
  62. }
  63. return 0;
  64. out_unwind:
  65. /* unwind the ones we already registered */
  66. hidinput_disconnect(hid);
  67. return -1;
  68. }

        hidinput是一个将hid数据转为input格式的协议转换者,转换完毕之后,将数据发送给hidinput->input 这个真正的连接到mousedev_handler驱动上input_dev设备, 这样当hid设备上传数据,引发irq中断

        drivers/hid/usbhid/hid-core.c

  1. //hid->ll_driver = &usb_hid_driver
  2. struct hid_ll_driver usb_hid_driver = {
  3. .parse = usbhid_parse,
  4. .start = usbhid_start,
  5. .stop = usbhid_stop,
  6. .open = usbhid_open,
  7. .close = usbhid_close,
  8. .power = usbhid_power,
  9. .request = usbhid_request,
  10. .wait = usbhid_wait_io,
  11. .raw_request = usbhid_raw_request,
  12. .output_report = usbhid_output_report,
  13. .idle = usbhid_idle,
  14. };
  15. //启动HID设备
  16. static int usbhid_start(struct hid_device *hid)
  17. {
  18. struct usb_interface *intf = to_usb_interface(hid->dev.parent);
  19. struct usb_host_interface *interface = intf->cur_altsetting;
  20. struct usb_device *dev = interface_to_usbdev(intf);
  21. struct usbhid_device *usbhid = hid->driver_data;
  22. unsigned int n, insize = 0;
  23. int ret;
  24. clear_bit(HID_DISCONNECTED, &usbhid->iofl);
  25. usbhid->bufsize = HID_MIN_BUFFER_SIZE;
  26. hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
  27. hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
  28. hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
  29. if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
  30. usbhid->bufsize = HID_MAX_BUFFER_SIZE;
  31. hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
  32. if (insize > HID_MAX_BUFFER_SIZE)
  33. insize = HID_MAX_BUFFER_SIZE;
  34. if (hid_alloc_buffers(dev, hid)) {
  35. ret = -ENOMEM;
  36. goto fail;
  37. }
  38. for (n = 0; n < interface->desc.bNumEndpoints; n++) {
  39. struct usb_endpoint_descriptor *endpoint;
  40. int pipe;
  41. int interval;
  42. endpoint = &interface->endpoint[n].desc;
  43. if (!usb_endpoint_xfer_int(endpoint))
  44. continue;
  45. interval = endpoint->bInterval;
  46. /* Some vendors give fullspeed interval on highspeed devides */
  47. if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
  48. dev->speed == USB_SPEED_HIGH) {
  49. interval = fls(endpoint->bInterval*8);
  50. pr_info("%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
  51. hid->name, endpoint->bInterval, interval);
  52. }
  53. /* Change the polling interval of mice and joysticks. */
  54. switch (hid->collection->usage) {
  55. case HID_GD_MOUSE:
  56. if (hid_mousepoll_interval > 0)
  57. interval = hid_mousepoll_interval;
  58. break;
  59. case HID_GD_JOYSTICK:
  60. if (hid_jspoll_interval > 0)
  61. interval = hid_jspoll_interval;
  62. break;
  63. }
  64. ret = -ENOMEM;
  65. if (usb_endpoint_dir_in(endpoint)) {
  66. if (usbhid->urbin)
  67. continue;
  68. if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
  69. goto fail;
  70. pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
  71. usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
  72. hid_irq_in, hid, interval); //中断函数hid_irq_in
  73. usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
  74. usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  75. } else {
  76. if (usbhid->urbout)
  77. continue;
  78. if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
  79. goto fail;
  80. pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
  81. usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
  82. hid_irq_out, hid, interval);
  83. usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
  84. usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  85. }
  86. }
  87. usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
  88. if (!usbhid->urbctrl) {
  89. ret = -ENOMEM;
  90. goto fail;
  91. }
  92. usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
  93. usbhid->ctrlbuf, 1, hid_ctrl, hid);
  94. usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
  95. usbhid->urbctrl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  96. set_bit(HID_STARTED, &usbhid->iofl);
  97. if (hid->quirks & HID_QUIRK_ALWAYS_POLL) {
  98. ret = usb_autopm_get_interface(usbhid->intf);
  99. if (ret)
  100. goto fail;
  101. set_bit(HID_IN_POLLING, &usbhid->iofl);
  102. usbhid->intf->needs_remote_wakeup = 1;
  103. ret = hid_start_in(hid);
  104. if (ret) {
  105. dev_err(&hid->dev,
  106. "failed to start in urb: %d\n", ret);
  107. }
  108. usb_autopm_put_interface(usbhid->intf);
  109. }
  110. /* Some keyboards don't work until their LEDs have been set.
  111. * Since BIOSes do set the LEDs, it must be safe for any device
  112. * that supports the keyboard boot protocol.
  113. * In addition, enable remote wakeup by default for all keyboard
  114. * devices supporting the boot protocol.
  115. */
  116. if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT &&
  117. interface->desc.bInterfaceProtocol ==
  118. USB_INTERFACE_PROTOCOL_KEYBOARD) {
  119. usbhid_set_leds(hid);
  120. device_set_wakeup_enable(&dev->dev, 1);
  121. }
  122. return 0;
  123. fail:
  124. usb_free_urb(usbhid->urbin);
  125. usb_free_urb(usbhid->urbout);
  126. usb_free_urb(usbhid->urbctrl);
  127. usbhid->urbin = NULL;
  128. usbhid->urbout = NULL;
  129. usbhid->urbctrl = NULL;
  130. hid_free_buffers(dev, hid);
  131. return ret;
  132. }
  133. /*
  134. * Input interrupt completion handler.
  135. */
  136. static void hid_irq_in(struct urb *urb)
  137. {
  138. struct hid_device *hid = urb->context;
  139. struct usbhid_device *usbhid = hid->driver_data;
  140. int status;
  141. switch (urb->status) {
  142. case 0: /* success */
  143. usbhid->retry_delay = 0;
  144. if (!test_bit(HID_OPENED, &usbhid->iofl))
  145. break;
  146. usbhid_mark_busy(usbhid);
  147. if (!test_bit(HID_RESUME_RUNNING, &usbhid->iofl)) {
  148. //hid_input_report--->
  149. hid_input_report(urb->context, HID_INPUT_REPORT,
  150. urb->transfer_buffer,
  151. urb->actual_length, 1);
  152. /*
  153. * autosuspend refused while keys are pressed
  154. * because most keyboards don't wake up when
  155. * a key is released
  156. */
  157. if (hid_check_keys_pressed(hid))
  158. set_bit(HID_KEYS_PRESSED, &usbhid->iofl);
  159. else
  160. clear_bit(HID_KEYS_PRESSED, &usbhid->iofl);
  161. }
  162. break;
  163. case -EPIPE: /* stall */
  164. usbhid_mark_busy(usbhid);
  165. clear_bit(HID_IN_RUNNING, &usbhid->iofl);
  166. set_bit(HID_CLEAR_HALT, &usbhid->iofl);
  167. schedule_work(&usbhid->reset_work);
  168. return;
  169. case -ECONNRESET: /* unlink */
  170. case -ENOENT:
  171. case -ESHUTDOWN: /* unplug */
  172. clear_bit(HID_IN_RUNNING, &usbhid->iofl);
  173. return;
  174. case -EILSEQ: /* protocol error or unplug */
  175. case -EPROTO: /* protocol error or unplug */
  176. case -ETIME: /* protocol error or unplug */
  177. case -ETIMEDOUT: /* Should never happen, but... */
  178. usbhid_mark_busy(usbhid);
  179. clear_bit(HID_IN_RUNNING, &usbhid->iofl);
  180. hid_io_error(hid);
  181. return;
  182. default: /* error */
  183. hid_warn(urb->dev, "input irq status %d received\n",
  184. urb->status);
  185. }
  186. status = usb_submit_urb(urb, GFP_ATOMIC);
  187. if (status) {
  188. clear_bit(HID_IN_RUNNING, &usbhid->iofl);
  189. if (status != -EPERM) {
  190. hid_err(hid, "can't resubmit intr, %s-%s/input%d, status %d\n",
  191. hid_to_usb_dev(hid)->bus->bus_name,
  192. hid_to_usb_dev(hid)->devpath,
  193. usbhid->ifnum, status);
  194. hid_io_error(hid);
  195. }
  196. }
  197. }
  198. int hid_input_report(struct hid_device *hid, int type, u8 *data, u32 size, int interrupt)
  199. {
  200. struct hid_report_enum *report_enum;
  201. struct hid_driver *hdrv;
  202. struct hid_report *report;
  203. int ret = 0;
  204. if (!hid)
  205. return -ENODEV;
  206. if (down_trylock(&hid->driver_input_lock))
  207. return -EBUSY;
  208. if (!hid->driver) {
  209. ret = -ENODEV;
  210. goto unlock;
  211. }
  212. report_enum = hid->report_enum + type;
  213. hdrv = hid->driver;
  214. if (!size) {
  215. dbg_hid("empty report\n");
  216. ret = -1;
  217. goto unlock;
  218. }
  219. /* Avoid unnecessary overhead if debugfs is disabled */
  220. if (!list_empty(&hid->debug_list))
  221. hid_dump_report(hid, type, data, size);
  222. report = hid_get_report(report_enum, data);
  223. if (!report) {
  224. ret = -1;
  225. goto unlock;
  226. }
  227. if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
  228. ret = hdrv->raw_event(hid, report, data, size);
  229. if (ret < 0)
  230. goto unlock;
  231. }
  232. ret = hid_report_raw_event(hid, type, data, size, interrupt);//--->
  233. unlock:
  234. up(&hid->driver_input_lock);
  235. return ret;
  236. }
  237. int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, u32 size,
  238. int interrupt)
  239. {
  240. struct hid_report_enum *report_enum = hid->report_enum + type;
  241. struct hid_report *report;
  242. struct hid_driver *hdrv;
  243. unsigned int a;
  244. u32 rsize, csize = size;
  245. u8 *cdata = data;
  246. int ret = 0;
  247. report = hid_get_report(report_enum, data);
  248. if (!report)
  249. goto out;
  250. if (report_enum->numbered) {
  251. cdata++;
  252. csize--;
  253. }
  254. rsize = ((report->size - 1) >> 3) + 1;
  255. if (rsize > HID_MAX_BUFFER_SIZE)
  256. rsize = HID_MAX_BUFFER_SIZE;
  257. if (csize < rsize) {
  258. dbg_hid("report %d is too short, (%d < %d)\n", report->id,
  259. csize, rsize);
  260. memset(cdata + csize, 0, rsize - csize);
  261. }
  262. if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
  263. hid->hiddev_report_event(hid, report);
  264. if (hid->claimed & HID_CLAIMED_HIDRAW) {
  265. ret = hidraw_report_event(hid, data, size);
  266. if (ret)
  267. goto out;
  268. }
  269. if (hid->claimed != HID_CLAIMED_HIDRAW && report->maxfield) {
  270. for (a = 0; a < report->maxfield; a++)
  271. hid_input_field(hid, report->field[a], cdata, interrupt);
  272. hdrv = hid->driver;
  273. if (hdrv && hdrv->report)
  274. hdrv->report(hid, report);
  275. }
  276. if (hid->claimed & HID_CLAIMED_INPUT)
  277. hidinput_report_event(hid, report); //--->
  278. out:
  279. return ret;
  280. }
  281. void hidinput_report_event(struct hid_device *hid, struct hid_report *report)
  282. {
  283. struct hid_input *hidinput;
  284. if (hid->quirks & HID_QUIRK_NO_INPUT_SYNC)
  285. return;
  286. list_for_each_entry(hidinput, &hid->inputs, list)
  287. input_sync(hidinput->input); //上报数据至input-core层
  288. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/327219?site
推荐阅读
相关标签
  

闽ICP备14008679号