hid-tmff.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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/usb.h>
  31. #include "hid-ids.h"
  32. #include "usbhid/usbhid.h"
  33. /* Usages for thrustmaster devices I know about */
  34. #define THRUSTMASTER_USAGE_FF (HID_UP_GENDESK | 0xbb)
  35. static const signed short ff_rumble[] = {
  36. FF_RUMBLE,
  37. -1
  38. };
  39. static const signed short ff_joystick[] = {
  40. FF_CONSTANT,
  41. -1
  42. };
  43. struct tmff_device {
  44. struct hid_report *report;
  45. struct hid_field *ff_field;
  46. };
  47. /* Changes values from 0 to 0xffff into values from minimum to maximum */
  48. static inline int tmff_scale_u16(unsigned int in, int minimum, int maximum)
  49. {
  50. int ret;
  51. ret = (in * (maximum - minimum) / 0xffff) + minimum;
  52. if (ret < minimum)
  53. return minimum;
  54. if (ret > maximum)
  55. return maximum;
  56. return ret;
  57. }
  58. /* Changes values from -0x80 to 0x7f into values from minimum to maximum */
  59. static inline int tmff_scale_s8(int in, int minimum, int maximum)
  60. {
  61. int ret;
  62. ret = (((in + 0x80) * (maximum - minimum)) / 0xff) + minimum;
  63. if (ret < minimum)
  64. return minimum;
  65. if (ret > maximum)
  66. return maximum;
  67. return ret;
  68. }
  69. static int tmff_play(struct input_dev *dev, void *data,
  70. struct ff_effect *effect)
  71. {
  72. struct hid_device *hid = input_get_drvdata(dev);
  73. struct tmff_device *tmff = data;
  74. struct hid_field *ff_field = tmff->ff_field;
  75. int x, y;
  76. int left, right; /* Rumbling */
  77. switch (effect->type) {
  78. case FF_CONSTANT:
  79. x = tmff_scale_s8(effect->u.ramp.start_level,
  80. ff_field->logical_minimum,
  81. ff_field->logical_maximum);
  82. y = tmff_scale_s8(effect->u.ramp.end_level,
  83. ff_field->logical_minimum,
  84. ff_field->logical_maximum);
  85. dbg_hid("(x, y)=(%04x, %04x)\n", x, y);
  86. ff_field->value[0] = x;
  87. ff_field->value[1] = y;
  88. usbhid_submit_report(hid, tmff->report, USB_DIR_OUT);
  89. break;
  90. case FF_RUMBLE:
  91. left = tmff_scale_u16(effect->u.rumble.weak_magnitude,
  92. ff_field->logical_minimum,
  93. ff_field->logical_maximum);
  94. right = tmff_scale_u16(effect->u.rumble.strong_magnitude,
  95. ff_field->logical_minimum,
  96. ff_field->logical_maximum);
  97. dbg_hid("(left,right)=(%08x, %08x)\n", left, right);
  98. ff_field->value[0] = left;
  99. ff_field->value[1] = right;
  100. usbhid_submit_report(hid, tmff->report, USB_DIR_OUT);
  101. break;
  102. }
  103. return 0;
  104. }
  105. static int tmff_init(struct hid_device *hid, const signed short *ff_bits)
  106. {
  107. struct tmff_device *tmff;
  108. struct hid_report *report;
  109. struct list_head *report_list;
  110. struct hid_input *hidinput = list_entry(hid->inputs.next,
  111. struct hid_input, list);
  112. struct input_dev *input_dev = hidinput->input;
  113. int error;
  114. int i;
  115. tmff = kzalloc(sizeof(struct tmff_device), GFP_KERNEL);
  116. if (!tmff)
  117. return -ENOMEM;
  118. /* Find the report to use */
  119. report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  120. list_for_each_entry(report, report_list, list) {
  121. int fieldnum;
  122. for (fieldnum = 0; fieldnum < report->maxfield; ++fieldnum) {
  123. struct hid_field *field = report->field[fieldnum];
  124. if (field->maxusage <= 0)
  125. continue;
  126. switch (field->usage[0].hid) {
  127. case THRUSTMASTER_USAGE_FF:
  128. if (field->report_count < 2) {
  129. dev_warn(&hid->dev, "ignoring FF field "
  130. "with report_count < 2\n");
  131. continue;
  132. }
  133. if (field->logical_maximum ==
  134. field->logical_minimum) {
  135. dev_warn(&hid->dev, "ignoring FF field "
  136. "with logical_maximum "
  137. "== logical_minimum\n");
  138. continue;
  139. }
  140. if (tmff->report && tmff->report != report) {
  141. dev_warn(&hid->dev, "ignoring FF field "
  142. "in other report\n");
  143. continue;
  144. }
  145. if (tmff->ff_field && tmff->ff_field != field) {
  146. dev_warn(&hid->dev, "ignoring "
  147. "duplicate FF field\n");
  148. continue;
  149. }
  150. tmff->report = report;
  151. tmff->ff_field = field;
  152. for (i = 0; ff_bits[i] >= 0; i++)
  153. set_bit(ff_bits[i], input_dev->ffbit);
  154. break;
  155. default:
  156. dev_warn(&hid->dev, "ignoring unknown output "
  157. "usage %08x\n",
  158. field->usage[0].hid);
  159. continue;
  160. }
  161. }
  162. }
  163. if (!tmff->report) {
  164. dev_err(&hid->dev, "can't find FF field in output reports\n");
  165. error = -ENODEV;
  166. goto fail;
  167. }
  168. error = input_ff_create_memless(input_dev, tmff, tmff_play);
  169. if (error)
  170. goto fail;
  171. dev_info(&hid->dev, "force feedback for ThrustMaster devices by Zinx "
  172. "Verituse <zinx@epicsol.org>");
  173. return 0;
  174. fail:
  175. kfree(tmff);
  176. return error;
  177. }
  178. static int tm_probe(struct hid_device *hdev, const struct hid_device_id *id)
  179. {
  180. int ret;
  181. ret = hid_parse(hdev);
  182. if (ret) {
  183. dev_err(&hdev->dev, "parse failed\n");
  184. goto err;
  185. }
  186. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  187. if (ret) {
  188. dev_err(&hdev->dev, "hw start failed\n");
  189. goto err;
  190. }
  191. tmff_init(hdev, (void *)id->driver_data);
  192. return 0;
  193. err:
  194. return ret;
  195. }
  196. static const struct hid_device_id tm_devices[] = {
  197. { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb300),
  198. .driver_data = (unsigned long)ff_rumble },
  199. { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb304),
  200. .driver_data = (unsigned long)ff_rumble },
  201. { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb651), /* FGT Rumble Force Wheel */
  202. .driver_data = (unsigned long)ff_rumble },
  203. { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb654), /* FGT Force Feedback Wheel */
  204. .driver_data = (unsigned long)ff_joystick },
  205. { }
  206. };
  207. MODULE_DEVICE_TABLE(hid, tm_devices);
  208. static struct hid_driver tm_driver = {
  209. .name = "thrustmaster",
  210. .id_table = tm_devices,
  211. .probe = tm_probe,
  212. };
  213. static int tm_init(void)
  214. {
  215. return hid_register_driver(&tm_driver);
  216. }
  217. static void tm_exit(void)
  218. {
  219. hid_unregister_driver(&tm_driver);
  220. }
  221. module_init(tm_init);
  222. module_exit(tm_exit);
  223. MODULE_LICENSE("GPL");
  224. HID_COMPAT_LOAD_DRIVER(thrustmaster);