hid-winwing.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * HID driver for WinWing Orion 2 throttle
  4. *
  5. * Copyright (c) 2023 Ivan Gorinov
  6. */
  7. #include <linux/device.h>
  8. #include <linux/hid.h>
  9. #include <linux/hidraw.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/mutex.h>
  13. #define MAX_REPORT 16
  14. struct winwing_led {
  15. struct led_classdev cdev;
  16. struct hid_device *hdev;
  17. int number;
  18. };
  19. struct winwing_led_info {
  20. int number;
  21. int max_brightness;
  22. const char *led_name;
  23. };
  24. static const struct winwing_led_info led_info[3] = {
  25. { 0, 255, "backlight" },
  26. { 1, 1, "a-a" },
  27. { 2, 1, "a-g" },
  28. };
  29. struct winwing_drv_data {
  30. struct hid_device *hdev;
  31. __u8 *report_buf;
  32. struct mutex lock;
  33. unsigned int num_leds;
  34. struct winwing_led leds[];
  35. };
  36. static int winwing_led_write(struct led_classdev *cdev,
  37. enum led_brightness br)
  38. {
  39. struct winwing_led *led = (struct winwing_led *) cdev;
  40. struct winwing_drv_data *data = hid_get_drvdata(led->hdev);
  41. __u8 *buf = data->report_buf;
  42. int ret;
  43. mutex_lock(&data->lock);
  44. buf[0] = 0x02;
  45. buf[1] = 0x60;
  46. buf[2] = 0xbe;
  47. buf[3] = 0x00;
  48. buf[4] = 0x00;
  49. buf[5] = 0x03;
  50. buf[6] = 0x49;
  51. buf[7] = led->number;
  52. buf[8] = br;
  53. buf[9] = 0x00;
  54. buf[10] = 0;
  55. buf[11] = 0;
  56. buf[12] = 0;
  57. buf[13] = 0;
  58. ret = hid_hw_output_report(led->hdev, buf, 14);
  59. mutex_unlock(&data->lock);
  60. return ret;
  61. }
  62. static int winwing_init_led(struct hid_device *hdev,
  63. struct input_dev *input)
  64. {
  65. struct winwing_drv_data *data;
  66. struct winwing_led *led;
  67. int ret;
  68. int i;
  69. size_t data_size = struct_size(data, leds, 3);
  70. data = devm_kzalloc(&hdev->dev, data_size, GFP_KERNEL);
  71. if (!data)
  72. return -ENOMEM;
  73. data->report_buf = devm_kmalloc(&hdev->dev, MAX_REPORT, GFP_KERNEL);
  74. if (!data->report_buf)
  75. return -ENOMEM;
  76. for (i = 0; i < 3; i += 1) {
  77. const struct winwing_led_info *info = &led_info[i];
  78. led = &data->leds[i];
  79. led->hdev = hdev;
  80. led->number = info->number;
  81. led->cdev.max_brightness = info->max_brightness;
  82. led->cdev.brightness_set_blocking = winwing_led_write;
  83. led->cdev.flags = LED_HW_PLUGGABLE;
  84. led->cdev.name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
  85. "%s::%s",
  86. dev_name(&input->dev),
  87. info->led_name);
  88. if (!led->cdev.name)
  89. return -ENOMEM;
  90. ret = devm_led_classdev_register(&hdev->dev, &led->cdev);
  91. if (ret)
  92. return ret;
  93. }
  94. hid_set_drvdata(hdev, data);
  95. return ret;
  96. }
  97. static int winwing_probe(struct hid_device *hdev,
  98. const struct hid_device_id *id)
  99. {
  100. int ret;
  101. ret = hid_parse(hdev);
  102. if (ret) {
  103. hid_err(hdev, "parse failed\n");
  104. return ret;
  105. }
  106. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  107. if (ret) {
  108. hid_err(hdev, "hw start failed\n");
  109. return ret;
  110. }
  111. return 0;
  112. }
  113. static int winwing_input_configured(struct hid_device *hdev,
  114. struct hid_input *hidinput)
  115. {
  116. int ret;
  117. ret = winwing_init_led(hdev, hidinput->input);
  118. if (ret)
  119. hid_err(hdev, "led init failed\n");
  120. return ret;
  121. }
  122. static const __u8 original_rdesc_buttons[] = {
  123. 0x05, 0x09, 0x19, 0x01, 0x29, 0x6F,
  124. 0x15, 0x00, 0x25, 0x01, 0x35, 0x00,
  125. 0x45, 0x01, 0x75, 0x01, 0x95, 0x6F,
  126. 0x81, 0x02, 0x75, 0x01, 0x95, 0x01,
  127. 0x81, 0x01
  128. };
  129. /*
  130. * HID report descriptor shows 111 buttons, which exceeds maximum
  131. * number of buttons (80) supported by Linux kernel HID subsystem.
  132. *
  133. * This module skips numbers 32-63, unused on some throttle grips.
  134. */
  135. static const __u8 *winwing_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  136. unsigned int *rsize)
  137. {
  138. int sig_length = sizeof(original_rdesc_buttons);
  139. int unused_button_numbers = 32;
  140. if (*rsize < 34)
  141. return rdesc;
  142. if (memcmp(rdesc + 8, original_rdesc_buttons, sig_length) == 0) {
  143. /* Usage Maximum */
  144. rdesc[13] -= unused_button_numbers;
  145. /* Report Count for buttons */
  146. rdesc[25] -= unused_button_numbers;
  147. /* Report Count for padding [HID1_11, 6.2.2.9] */
  148. rdesc[31] += unused_button_numbers;
  149. hid_info(hdev, "winwing descriptor fixed\n");
  150. }
  151. return rdesc;
  152. }
  153. static int winwing_raw_event(struct hid_device *hdev,
  154. struct hid_report *report, u8 *raw_data, int size)
  155. {
  156. if (size >= 15) {
  157. /* Skip buttons 32 .. 63 */
  158. memmove(raw_data + 5, raw_data + 9, 6);
  159. /* Clear the padding */
  160. memset(raw_data + 11, 0, 4);
  161. }
  162. return 0;
  163. }
  164. static const struct hid_device_id winwing_devices[] = {
  165. { HID_USB_DEVICE(0x4098, 0xbe62) }, /* TGRIP-18 */
  166. { HID_USB_DEVICE(0x4098, 0xbe68) }, /* TGRIP-16EX */
  167. {}
  168. };
  169. MODULE_DEVICE_TABLE(hid, winwing_devices);
  170. static struct hid_driver winwing_driver = {
  171. .name = "winwing",
  172. .id_table = winwing_devices,
  173. .probe = winwing_probe,
  174. .input_configured = winwing_input_configured,
  175. .report_fixup = winwing_report_fixup,
  176. .raw_event = winwing_raw_event,
  177. };
  178. module_hid_driver(winwing_driver);
  179. MODULE_DESCRIPTION("HID driver for WinWing Orion 2 throttle");
  180. MODULE_LICENSE("GPL");