hid-holtek-kbd.c 8.0 KB

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