hid-huion.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * HID driver for Huion devices not fully compliant with HID standard
  3. *
  4. * Copyright (c) 2013 Martin Rusko
  5. */
  6. /*
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. */
  12. #include <linux/device.h>
  13. #include <linux/hid.h>
  14. #include <linux/module.h>
  15. #include <linux/usb.h>
  16. #include "usbhid/usbhid.h"
  17. #include "hid-ids.h"
  18. /* Original Huion 580 report descriptor size */
  19. #define HUION_580_RDESC_ORIG_SIZE 177
  20. /* Fixed Huion 580 report descriptor */
  21. static __u8 huion_580_rdesc_fixed[] = {
  22. 0x05, 0x0D, /* Usage Page (Digitizer), */
  23. 0x09, 0x02, /* Usage (Pen), */
  24. 0xA1, 0x01, /* Collection (Application), */
  25. 0x85, 0x07, /* Report ID (7), */
  26. 0x09, 0x20, /* Usage (Stylus), */
  27. 0xA0, /* Collection (Physical), */
  28. 0x14, /* Logical Minimum (0), */
  29. 0x25, 0x01, /* Logical Maximum (1), */
  30. 0x75, 0x01, /* Report Size (1), */
  31. 0x09, 0x42, /* Usage (Tip Switch), */
  32. 0x09, 0x44, /* Usage (Barrel Switch), */
  33. 0x09, 0x46, /* Usage (Tablet Pick), */
  34. 0x95, 0x03, /* Report Count (3), */
  35. 0x81, 0x02, /* Input (Variable), */
  36. 0x95, 0x03, /* Report Count (3), */
  37. 0x81, 0x03, /* Input (Constant, Variable), */
  38. 0x09, 0x32, /* Usage (In Range), */
  39. 0x95, 0x01, /* Report Count (1), */
  40. 0x81, 0x02, /* Input (Variable), */
  41. 0x95, 0x01, /* Report Count (1), */
  42. 0x81, 0x03, /* Input (Constant, Variable), */
  43. 0x75, 0x10, /* Report Size (16), */
  44. 0x95, 0x01, /* Report Count (1), */
  45. 0xA4, /* Push, */
  46. 0x05, 0x01, /* Usage Page (Desktop), */
  47. 0x65, 0x13, /* Unit (Inch), */
  48. 0x55, 0xFD, /* Unit Exponent (-3), */
  49. 0x34, /* Physical Minimum (0), */
  50. 0x09, 0x30, /* Usage (X), */
  51. 0x46, 0x40, 0x1F, /* Physical Maximum (8000), */
  52. 0x26, 0x00, 0x7D, /* Logical Maximum (32000), */
  53. 0x81, 0x02, /* Input (Variable), */
  54. 0x09, 0x31, /* Usage (Y), */
  55. 0x46, 0x88, 0x13, /* Physical Maximum (5000), */
  56. 0x26, 0x20, 0x4E, /* Logical Maximum (20000), */
  57. 0x81, 0x02, /* Input (Variable), */
  58. 0xB4, /* Pop, */
  59. 0x09, 0x30, /* Usage (Tip Pressure), */
  60. 0x26, 0xFF, 0x07, /* Logical Maximum (2047), */
  61. 0x81, 0x02, /* Input (Variable), */
  62. 0xC0, /* End Collection, */
  63. 0xC0 /* End Collection */
  64. };
  65. static __u8 *huion_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  66. unsigned int *rsize)
  67. {
  68. switch (hdev->product) {
  69. case USB_DEVICE_ID_HUION_580:
  70. if (*rsize == HUION_580_RDESC_ORIG_SIZE) {
  71. rdesc = huion_580_rdesc_fixed;
  72. *rsize = sizeof(huion_580_rdesc_fixed);
  73. }
  74. break;
  75. }
  76. return rdesc;
  77. }
  78. /**
  79. * Enable fully-functional tablet mode by reading special string
  80. * descriptor.
  81. *
  82. * @hdev: HID device
  83. *
  84. * The specific string descriptor and data were discovered by sniffing
  85. * the Windows driver traffic.
  86. */
  87. static int huion_tablet_enable(struct hid_device *hdev)
  88. {
  89. int rc;
  90. char buf[22];
  91. rc = usb_string(hid_to_usb_dev(hdev), 0x64, buf, sizeof(buf));
  92. if (rc < 0)
  93. return rc;
  94. return 0;
  95. }
  96. static int huion_probe(struct hid_device *hdev, const struct hid_device_id *id)
  97. {
  98. int ret;
  99. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  100. /* Ignore interfaces 1 (mouse) and 2 (keyboard) for Huion 580 tablet,
  101. * as they are not used
  102. */
  103. switch (id->product) {
  104. case USB_DEVICE_ID_HUION_580:
  105. if (intf->cur_altsetting->desc.bInterfaceNumber != 0x00)
  106. return -ENODEV;
  107. break;
  108. }
  109. ret = hid_parse(hdev);
  110. if (ret) {
  111. hid_err(hdev, "parse failed\n");
  112. goto err;
  113. }
  114. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  115. if (ret) {
  116. hid_err(hdev, "hw start failed\n");
  117. goto err;
  118. }
  119. switch (id->product) {
  120. case USB_DEVICE_ID_HUION_580:
  121. ret = huion_tablet_enable(hdev);
  122. if (ret) {
  123. hid_err(hdev, "tablet enabling failed\n");
  124. goto enabling_err;
  125. }
  126. break;
  127. }
  128. return 0;
  129. enabling_err:
  130. hid_hw_stop(hdev);
  131. err:
  132. return ret;
  133. }
  134. static int huion_raw_event(struct hid_device *hdev, struct hid_report *report,
  135. u8 *data, int size)
  136. {
  137. /* If this is a pen input report then invert the in-range bit */
  138. if (report->type == HID_INPUT_REPORT && report->id == 0x07 && size >= 2)
  139. data[1] ^= 0x40;
  140. return 0;
  141. }
  142. static const struct hid_device_id huion_devices[] = {
  143. { HID_USB_DEVICE(USB_VENDOR_ID_HUION, USB_DEVICE_ID_HUION_580) },
  144. { }
  145. };
  146. MODULE_DEVICE_TABLE(hid, huion_devices);
  147. static struct hid_driver huion_driver = {
  148. .name = "huion",
  149. .id_table = huion_devices,
  150. .probe = huion_probe,
  151. .report_fixup = huion_report_fixup,
  152. .raw_event = huion_raw_event,
  153. };
  154. module_hid_driver(huion_driver);
  155. MODULE_AUTHOR("Martin Rusko");
  156. MODULE_DESCRIPTION("Huion HID driver");
  157. MODULE_LICENSE("GPL");