hid-holtekff.c 6.0 KB

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