hid-holtek-kbd.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * HID driver for Holtek keyboard
  4. * Copyright (c) 2012 Tom Harwood
  5. */
  6. /*
  7. */
  8. #include <linux/device.h>
  9. #include <linux/hid.h>
  10. #include <linux/module.h>
  11. #include <linux/usb.h>
  12. #include "hid-ids.h"
  13. #include "usbhid/usbhid.h"
  14. /* Holtek based keyboards (USB ID 04d9:a055) have the following issues:
  15. * - The report descriptor specifies an excessively large number of consumer
  16. * usages (2^15), which is more than HID_MAX_USAGES. This prevents proper
  17. * parsing of the report descriptor.
  18. * - The report descriptor reports on caps/scroll/num lock key presses, but
  19. * doesn't have an LED output usage block.
  20. *
  21. * The replacement descriptor below fixes the number of consumer usages,
  22. * and provides an LED output usage block. LED output events are redirected
  23. * to the boot interface.
  24. */
  25. static __u8 holtek_kbd_rdesc_fixed[] = {
  26. /* Original report descriptor, with reduced number of consumer usages */
  27. 0x05, 0x01, /* Usage Page (Desktop), */
  28. 0x09, 0x80, /* Usage (Sys Control), */
  29. 0xA1, 0x01, /* Collection (Application), */
  30. 0x85, 0x01, /* Report ID (1), */
  31. 0x19, 0x81, /* Usage Minimum (Sys Power Down), */
  32. 0x29, 0x83, /* Usage Maximum (Sys Wake Up), */
  33. 0x15, 0x00, /* Logical Minimum (0), */
  34. 0x25, 0x01, /* Logical Maximum (1), */
  35. 0x95, 0x03, /* Report Count (3), */
  36. 0x75, 0x01, /* Report Size (1), */
  37. 0x81, 0x02, /* Input (Variable), */
  38. 0x95, 0x01, /* Report Count (1), */
  39. 0x75, 0x05, /* Report Size (5), */
  40. 0x81, 0x01, /* Input (Constant), */
  41. 0xC0, /* End Collection, */
  42. 0x05, 0x0C, /* Usage Page (Consumer), */
  43. 0x09, 0x01, /* Usage (Consumer Control), */
  44. 0xA1, 0x01, /* Collection (Application), */
  45. 0x85, 0x02, /* Report ID (2), */
  46. 0x19, 0x00, /* Usage Minimum (00h), */
  47. 0x2A, 0xFF, 0x2F, /* Usage Maximum (0x2FFF), previously 0x7FFF */
  48. 0x15, 0x00, /* Logical Minimum (0), */
  49. 0x26, 0xFF, 0x2F, /* Logical Maximum (0x2FFF),previously 0x7FFF*/
  50. 0x95, 0x01, /* Report Count (1), */
  51. 0x75, 0x10, /* Report Size (16), */
  52. 0x81, 0x00, /* Input, */
  53. 0xC0, /* End Collection, */
  54. 0x05, 0x01, /* Usage Page (Desktop), */
  55. 0x09, 0x06, /* Usage (Keyboard), */
  56. 0xA1, 0x01, /* Collection (Application), */
  57. 0x85, 0x03, /* Report ID (3), */
  58. 0x95, 0x38, /* Report Count (56), */
  59. 0x75, 0x01, /* Report Size (1), */
  60. 0x15, 0x00, /* Logical Minimum (0), */
  61. 0x25, 0x01, /* Logical Maximum (1), */
  62. 0x05, 0x07, /* Usage Page (Keyboard), */
  63. 0x19, 0xE0, /* Usage Minimum (KB Leftcontrol), */
  64. 0x29, 0xE7, /* Usage Maximum (KB Right GUI), */
  65. 0x19, 0x00, /* Usage Minimum (None), */
  66. 0x29, 0x2F, /* Usage Maximum (KB Lboxbracket And Lbrace),*/
  67. 0x81, 0x02, /* Input (Variable), */
  68. 0xC0, /* End Collection, */
  69. 0x05, 0x01, /* Usage Page (Desktop), */
  70. 0x09, 0x06, /* Usage (Keyboard), */
  71. 0xA1, 0x01, /* Collection (Application), */
  72. 0x85, 0x04, /* Report ID (4), */
  73. 0x95, 0x38, /* Report Count (56), */
  74. 0x75, 0x01, /* Report Size (1), */
  75. 0x15, 0x00, /* Logical Minimum (0), */
  76. 0x25, 0x01, /* Logical Maximum (1), */
  77. 0x05, 0x07, /* Usage Page (Keyboard), */
  78. 0x19, 0x30, /* Usage Minimum (KB Rboxbracket And Rbrace),*/
  79. 0x29, 0x67, /* Usage Maximum (KP Equals), */
  80. 0x81, 0x02, /* Input (Variable), */
  81. 0xC0, /* End Collection */
  82. /* LED usage for the boot protocol interface */
  83. 0x05, 0x01, /* Usage Page (Desktop), */
  84. 0x09, 0x06, /* Usage (Keyboard), */
  85. 0xA1, 0x01, /* Collection (Application), */
  86. 0x05, 0x08, /* Usage Page (LED), */
  87. 0x19, 0x01, /* Usage Minimum (01h), */
  88. 0x29, 0x03, /* Usage Maximum (03h), */
  89. 0x15, 0x00, /* Logical Minimum (0), */
  90. 0x25, 0x01, /* Logical Maximum (1), */
  91. 0x75, 0x01, /* Report Size (1), */
  92. 0x95, 0x03, /* Report Count (3), */
  93. 0x91, 0x02, /* Output (Variable), */
  94. 0x95, 0x05, /* Report Count (5), */
  95. 0x91, 0x01, /* Output (Constant), */
  96. 0xC0, /* End Collection */
  97. };
  98. static __u8 *holtek_kbd_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  99. unsigned int *rsize)
  100. {
  101. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  102. if (intf->cur_altsetting->desc.bInterfaceNumber == 1) {
  103. rdesc = holtek_kbd_rdesc_fixed;
  104. *rsize = sizeof(holtek_kbd_rdesc_fixed);
  105. }
  106. return rdesc;
  107. }
  108. static int holtek_kbd_input_event(struct input_dev *dev, unsigned int type,
  109. unsigned int code,
  110. int value)
  111. {
  112. struct hid_device *hid = input_get_drvdata(dev);
  113. struct usb_device *usb_dev = hid_to_usb_dev(hid);
  114. /* Locate the boot interface, to receive the LED change events */
  115. struct usb_interface *boot_interface = usb_ifnum_to_if(usb_dev, 0);
  116. struct hid_device *boot_hid;
  117. struct hid_input *boot_hid_input;
  118. if (unlikely(boot_interface == NULL))
  119. return -ENODEV;
  120. boot_hid = usb_get_intfdata(boot_interface);
  121. if (list_empty(&boot_hid->inputs)) {
  122. hid_err(hid, "no inputs found\n");
  123. return -ENODEV;
  124. }
  125. boot_hid_input = list_first_entry(&boot_hid->inputs,
  126. struct hid_input, list);
  127. return boot_hid_input->input->event(boot_hid_input->input, type, code,
  128. value);
  129. }
  130. static int holtek_kbd_probe(struct hid_device *hdev,
  131. const struct hid_device_id *id)
  132. {
  133. struct usb_interface *intf;
  134. int ret;
  135. if (!hid_is_usb(hdev))
  136. return -EINVAL;
  137. ret = hid_parse(hdev);
  138. if (!ret)
  139. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  140. intf = to_usb_interface(hdev->dev.parent);
  141. if (!ret && intf->cur_altsetting->desc.bInterfaceNumber == 1) {
  142. struct hid_input *hidinput;
  143. list_for_each_entry(hidinput, &hdev->inputs, list) {
  144. hidinput->input->event = holtek_kbd_input_event;
  145. }
  146. }
  147. return ret;
  148. }
  149. static const struct hid_device_id holtek_kbd_devices[] = {
  150. { HID_USB_DEVICE(USB_VENDOR_ID_HOLTEK_ALT,
  151. USB_DEVICE_ID_HOLTEK_ALT_KEYBOARD) },
  152. { }
  153. };
  154. MODULE_DEVICE_TABLE(hid, holtek_kbd_devices);
  155. static struct hid_driver holtek_kbd_driver = {
  156. .name = "holtek_kbd",
  157. .id_table = holtek_kbd_devices,
  158. .report_fixup = holtek_kbd_report_fixup,
  159. .probe = holtek_kbd_probe
  160. };
  161. module_hid_driver(holtek_kbd_driver);
  162. MODULE_LICENSE("GPL");