hid-holtekff.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Force feedback support for Holtek On Line Grip based gamepads
  4. *
  5. * These include at least a Brazilian "Clone Joypad Super Power Fire"
  6. * which uses vendor ID 0x1241 and identifies as "HOLTEK On Line Grip".
  7. *
  8. * Copyright (c) 2011 Anssi Hannula <anssi.hannula@iki.fi>
  9. */
  10. /*
  11. */
  12. #include <linux/hid.h>
  13. #include <linux/input.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include "hid-ids.h"
  17. #ifdef CONFIG_HOLTEK_FF
  18. /*
  19. * These commands and parameters are currently known:
  20. *
  21. * byte 0: command id:
  22. * 01 set effect parameters
  23. * 02 play specified effect
  24. * 03 stop specified effect
  25. * 04 stop all effects
  26. * 06 stop all effects
  27. * (the difference between 04 and 06 isn't known; win driver
  28. * sends 06,04 on application init, and 06 otherwise)
  29. *
  30. * Commands 01 and 02 need to be sent as pairs, i.e. you need to send 01
  31. * before each 02.
  32. *
  33. * The rest of the bytes are parameters. Command 01 takes all of them, and
  34. * commands 02,03 take only the effect id.
  35. *
  36. * byte 1:
  37. * bits 0-3: effect id:
  38. * 1: very strong rumble
  39. * 2: periodic rumble, short intervals
  40. * 3: very strong rumble
  41. * 4: periodic rumble, long intervals
  42. * 5: weak periodic rumble, long intervals
  43. * 6: weak periodic rumble, short intervals
  44. * 7: periodic rumble, short intervals
  45. * 8: strong periodic rumble, short intervals
  46. * 9: very strong rumble
  47. * a: causes an error
  48. * b: very strong periodic rumble, very short intervals
  49. * c-f: nothing
  50. * bit 6: right (weak) motor enabled
  51. * bit 7: left (strong) motor enabled
  52. *
  53. * bytes 2-3: time in milliseconds, big-endian
  54. * bytes 5-6: unknown (win driver seems to use at least 10e0 with effect 1
  55. * and 0014 with effect 6)
  56. * byte 7:
  57. * bits 0-3: effect magnitude
  58. */
  59. #define HOLTEKFF_MSG_LENGTH 7
  60. static const u8 start_effect_1[] = { 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
  61. static const u8 stop_all4[] = { 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  62. static const u8 stop_all6[] = { 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  63. struct holtekff_device {
  64. struct hid_field *field;
  65. };
  66. static void holtekff_send(struct holtekff_device *holtekff,
  67. struct hid_device *hid,
  68. const u8 data[HOLTEKFF_MSG_LENGTH])
  69. {
  70. int i;
  71. for (i = 0; i < HOLTEKFF_MSG_LENGTH; i++) {
  72. holtekff->field->value[i] = data[i];
  73. }
  74. dbg_hid("sending %7ph\n", data);
  75. hid_hw_request(hid, holtekff->field->report, HID_REQ_SET_REPORT);
  76. }
  77. static int holtekff_play(struct input_dev *dev, void *data,
  78. struct ff_effect *effect)
  79. {
  80. struct hid_device *hid = input_get_drvdata(dev);
  81. struct holtekff_device *holtekff = data;
  82. int left, right;
  83. /* effect type 1, length 65535 msec */
  84. u8 buf[HOLTEKFF_MSG_LENGTH] =
  85. { 0x01, 0x01, 0xff, 0xff, 0x10, 0xe0, 0x00 };
  86. left = effect->u.rumble.strong_magnitude;
  87. right = effect->u.rumble.weak_magnitude;
  88. dbg_hid("called with 0x%04x 0x%04x\n", left, right);
  89. if (!left && !right) {
  90. holtekff_send(holtekff, hid, stop_all6);
  91. return 0;
  92. }
  93. if (left)
  94. buf[1] |= 0x80;
  95. if (right)
  96. buf[1] |= 0x40;
  97. /* The device takes a single magnitude, so we just sum them up. */
  98. buf[6] = min(0xf, (left >> 12) + (right >> 12));
  99. holtekff_send(holtekff, hid, buf);
  100. holtekff_send(holtekff, hid, start_effect_1);
  101. return 0;
  102. }
  103. static int holtekff_init(struct hid_device *hid)
  104. {
  105. struct holtekff_device *holtekff;
  106. struct hid_report *report;
  107. struct hid_input *hidinput;
  108. struct list_head *report_list =
  109. &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  110. struct input_dev *dev;
  111. int error;
  112. if (list_empty(&hid->inputs)) {
  113. hid_err(hid, "no inputs found\n");
  114. return -ENODEV;
  115. }
  116. hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  117. dev = hidinput->input;
  118. if (list_empty(report_list)) {
  119. hid_err(hid, "no output report found\n");
  120. return -ENODEV;
  121. }
  122. report = list_entry(report_list->next, struct hid_report, list);
  123. if (report->maxfield < 1 || report->field[0]->report_count != 7) {
  124. hid_err(hid, "unexpected output report layout\n");
  125. return -ENODEV;
  126. }
  127. holtekff = kzalloc(sizeof(*holtekff), GFP_KERNEL);
  128. if (!holtekff)
  129. return -ENOMEM;
  130. set_bit(FF_RUMBLE, dev->ffbit);
  131. holtekff->field = report->field[0];
  132. /* initialize the same way as win driver does */
  133. holtekff_send(holtekff, hid, stop_all4);
  134. holtekff_send(holtekff, hid, stop_all6);
  135. error = input_ff_create_memless(dev, holtekff, holtekff_play);
  136. if (error) {
  137. kfree(holtekff);
  138. return error;
  139. }
  140. hid_info(hid, "Force feedback for Holtek On Line Grip based devices by Anssi Hannula <anssi.hannula@iki.fi>\n");
  141. return 0;
  142. }
  143. #else
  144. static inline int holtekff_init(struct hid_device *hid)
  145. {
  146. return 0;
  147. }
  148. #endif
  149. static int holtek_probe(struct hid_device *hdev, const struct hid_device_id *id)
  150. {
  151. int ret;
  152. ret = hid_parse(hdev);
  153. if (ret) {
  154. hid_err(hdev, "parse failed\n");
  155. goto err;
  156. }
  157. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  158. if (ret) {
  159. hid_err(hdev, "hw start failed\n");
  160. goto err;
  161. }
  162. holtekff_init(hdev);
  163. return 0;
  164. err:
  165. return ret;
  166. }
  167. static const struct hid_device_id holtek_devices[] = {
  168. { HID_USB_DEVICE(USB_VENDOR_ID_HOLTEK, USB_DEVICE_ID_HOLTEK_ON_LINE_GRIP) },
  169. { }
  170. };
  171. MODULE_DEVICE_TABLE(hid, holtek_devices);
  172. static struct hid_driver holtek_driver = {
  173. .name = "holtek",
  174. .id_table = holtek_devices,
  175. .probe = holtek_probe,
  176. };
  177. module_hid_driver(holtek_driver);
  178. MODULE_LICENSE("GPL");
  179. MODULE_AUTHOR("Anssi Hannula <anssi.hannula@iki.fi>");
  180. MODULE_DESCRIPTION("Force feedback support for Holtek On Line Grip based devices");