hid-holtekff.c 6.1 KB

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