hid-holtekff.c 6.4 KB

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