hid-tmff.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Force feedback support for various HID compliant devices by ThrustMaster:
  4. * ThrustMaster FireStorm Dual Power 2
  5. * and possibly others whose device ids haven't been added.
  6. *
  7. * Modified to support ThrustMaster devices by Zinx Verituse
  8. * on 2003-01-25 from the Logitech force feedback driver,
  9. * which is by Johann Deneux.
  10. *
  11. * Copyright (c) 2003 Zinx Verituse <zinx@epicsol.org>
  12. * Copyright (c) 2002 Johann Deneux
  13. */
  14. /*
  15. */
  16. #include <linux/hid.h>
  17. #include <linux/input.h>
  18. #include <linux/slab.h>
  19. #include <linux/module.h>
  20. #include "hid-ids.h"
  21. #define THRUSTMASTER_DEVICE_ID_2_IN_1_DT 0xb320
  22. static const signed short ff_rumble[] = {
  23. FF_RUMBLE,
  24. -1
  25. };
  26. static const signed short ff_joystick[] = {
  27. FF_CONSTANT,
  28. -1
  29. };
  30. #ifdef CONFIG_THRUSTMASTER_FF
  31. /* Usages for thrustmaster devices I know about */
  32. #define THRUSTMASTER_USAGE_FF (HID_UP_GENDESK | 0xbb)
  33. struct tmff_device {
  34. struct hid_report *report;
  35. struct hid_field *ff_field;
  36. };
  37. /* Changes values from 0 to 0xffff into values from minimum to maximum */
  38. static inline int tmff_scale_u16(unsigned int in, int minimum, int maximum)
  39. {
  40. int ret;
  41. ret = (in * (maximum - minimum) / 0xffff) + minimum;
  42. if (ret < minimum)
  43. return minimum;
  44. if (ret > maximum)
  45. return maximum;
  46. return ret;
  47. }
  48. /* Changes values from -0x80 to 0x7f into values from minimum to maximum */
  49. static inline int tmff_scale_s8(int in, int minimum, int maximum)
  50. {
  51. int ret;
  52. ret = (((in + 0x80) * (maximum - minimum)) / 0xff) + minimum;
  53. if (ret < minimum)
  54. return minimum;
  55. if (ret > maximum)
  56. return maximum;
  57. return ret;
  58. }
  59. static int tmff_play(struct input_dev *dev, void *data,
  60. struct ff_effect *effect)
  61. {
  62. struct hid_device *hid = input_get_drvdata(dev);
  63. struct tmff_device *tmff = data;
  64. struct hid_field *ff_field = tmff->ff_field;
  65. int x, y;
  66. int left, right; /* Rumbling */
  67. int motor_swap;
  68. switch (effect->type) {
  69. case FF_CONSTANT:
  70. x = tmff_scale_s8(effect->u.ramp.start_level,
  71. ff_field->logical_minimum,
  72. ff_field->logical_maximum);
  73. y = tmff_scale_s8(effect->u.ramp.end_level,
  74. ff_field->logical_minimum,
  75. ff_field->logical_maximum);
  76. dbg_hid("(x, y)=(%04x, %04x)\n", x, y);
  77. ff_field->value[0] = x;
  78. ff_field->value[1] = y;
  79. hid_hw_request(hid, tmff->report, HID_REQ_SET_REPORT);
  80. break;
  81. case FF_RUMBLE:
  82. left = tmff_scale_u16(effect->u.rumble.weak_magnitude,
  83. ff_field->logical_minimum,
  84. ff_field->logical_maximum);
  85. right = tmff_scale_u16(effect->u.rumble.strong_magnitude,
  86. ff_field->logical_minimum,
  87. ff_field->logical_maximum);
  88. /* 2-in-1 strong motor is left */
  89. if (hid->product == THRUSTMASTER_DEVICE_ID_2_IN_1_DT) {
  90. motor_swap = left;
  91. left = right;
  92. right = motor_swap;
  93. }
  94. dbg_hid("(left,right)=(%08x, %08x)\n", left, right);
  95. ff_field->value[0] = left;
  96. ff_field->value[1] = right;
  97. hid_hw_request(hid, tmff->report, HID_REQ_SET_REPORT);
  98. break;
  99. }
  100. return 0;
  101. }
  102. static int tmff_init(struct hid_device *hid, const signed short *ff_bits)
  103. {
  104. struct tmff_device *tmff;
  105. struct hid_report *report;
  106. struct list_head *report_list;
  107. struct hid_input *hidinput;
  108. struct input_dev *input_dev;
  109. int error;
  110. int i;
  111. if (list_empty(&hid->inputs)) {
  112. hid_err(hid, "no inputs found\n");
  113. return -ENODEV;
  114. }
  115. hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  116. input_dev = hidinput->input;
  117. tmff = kzalloc(sizeof(struct tmff_device), GFP_KERNEL);
  118. if (!tmff)
  119. return -ENOMEM;
  120. /* Find the report to use */
  121. report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  122. list_for_each_entry(report, report_list, list) {
  123. int fieldnum;
  124. for (fieldnum = 0; fieldnum < report->maxfield; ++fieldnum) {
  125. struct hid_field *field = report->field[fieldnum];
  126. if (field->maxusage <= 0)
  127. continue;
  128. switch (field->usage[0].hid) {
  129. case THRUSTMASTER_USAGE_FF:
  130. if (field->report_count < 2) {
  131. hid_warn(hid, "ignoring FF field with report_count < 2\n");
  132. continue;
  133. }
  134. if (field->logical_maximum ==
  135. field->logical_minimum) {
  136. hid_warn(hid, "ignoring FF field with logical_maximum == logical_minimum\n");
  137. continue;
  138. }
  139. if (tmff->report && tmff->report != report) {
  140. hid_warn(hid, "ignoring FF field in other report\n");
  141. continue;
  142. }
  143. if (tmff->ff_field && tmff->ff_field != field) {
  144. hid_warn(hid, "ignoring duplicate FF field\n");
  145. continue;
  146. }
  147. tmff->report = report;
  148. tmff->ff_field = field;
  149. for (i = 0; ff_bits[i] >= 0; i++)
  150. set_bit(ff_bits[i], input_dev->ffbit);
  151. break;
  152. default:
  153. hid_warn(hid, "ignoring unknown output usage %08x\n",
  154. field->usage[0].hid);
  155. continue;
  156. }
  157. }
  158. }
  159. if (!tmff->report) {
  160. hid_err(hid, "can't find FF field in output reports\n");
  161. error = -ENODEV;
  162. goto fail;
  163. }
  164. error = input_ff_create_memless(input_dev, tmff, tmff_play);
  165. if (error)
  166. goto fail;
  167. hid_info(hid, "force feedback for ThrustMaster devices by Zinx Verituse <zinx@epicsol.org>\n");
  168. return 0;
  169. fail:
  170. kfree(tmff);
  171. return error;
  172. }
  173. #else
  174. static inline int tmff_init(struct hid_device *hid, const signed short *ff_bits)
  175. {
  176. return 0;
  177. }
  178. #endif
  179. static int tm_probe(struct hid_device *hdev, const struct hid_device_id *id)
  180. {
  181. int ret;
  182. ret = hid_parse(hdev);
  183. if (ret) {
  184. hid_err(hdev, "parse failed\n");
  185. goto err;
  186. }
  187. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  188. if (ret) {
  189. hid_err(hdev, "hw start failed\n");
  190. goto err;
  191. }
  192. tmff_init(hdev, (void *)id->driver_data);
  193. return 0;
  194. err:
  195. return ret;
  196. }
  197. static const struct hid_device_id tm_devices[] = {
  198. { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb300),
  199. .driver_data = (unsigned long)ff_rumble },
  200. { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb304), /* FireStorm Dual Power 2 (and 3) */
  201. .driver_data = (unsigned long)ff_rumble },
  202. { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, THRUSTMASTER_DEVICE_ID_2_IN_1_DT), /* Dual Trigger 2-in-1 */
  203. .driver_data = (unsigned long)ff_rumble },
  204. { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb323), /* Dual Trigger 3-in-1 (PC Mode) */
  205. .driver_data = (unsigned long)ff_rumble },
  206. { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb324), /* Dual Trigger 3-in-1 (PS3 Mode) */
  207. .driver_data = (unsigned long)ff_rumble },
  208. { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb605), /* NASCAR PRO FF2 Wheel */
  209. .driver_data = (unsigned long)ff_joystick },
  210. { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb651), /* FGT Rumble Force Wheel */
  211. .driver_data = (unsigned long)ff_rumble },
  212. { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb653), /* RGT Force Feedback CLUTCH Raging Wheel */
  213. .driver_data = (unsigned long)ff_joystick },
  214. { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb654), /* FGT Force Feedback Wheel */
  215. .driver_data = (unsigned long)ff_joystick },
  216. { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb65a), /* F430 Force Feedback Wheel */
  217. .driver_data = (unsigned long)ff_joystick },
  218. { }
  219. };
  220. MODULE_DEVICE_TABLE(hid, tm_devices);
  221. static struct hid_driver tm_driver = {
  222. .name = "thrustmaster",
  223. .id_table = tm_devices,
  224. .probe = tm_probe,
  225. };
  226. module_hid_driver(tm_driver);
  227. MODULE_LICENSE("GPL");