hid-tmff.c 7.3 KB

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