hid-tmff.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/input.h>
  29. #undef DEBUG
  30. #include <linux/usb.h>
  31. #include <linux/hid.h>
  32. #include "usbhid.h"
  33. /* Usages for thrustmaster devices I know about */
  34. #define THRUSTMASTER_USAGE_FF (HID_UP_GENDESK | 0xbb)
  35. struct dev_type {
  36. u16 idVendor;
  37. u16 idProduct;
  38. const signed short *ff;
  39. };
  40. static const signed short ff_rumble[] = {
  41. FF_RUMBLE,
  42. -1
  43. };
  44. static const signed short ff_joystick[] = {
  45. FF_CONSTANT,
  46. -1
  47. };
  48. static const struct dev_type devices[] = {
  49. { 0x44f, 0xb300, ff_rumble },
  50. { 0x44f, 0xb304, ff_rumble },
  51. { 0x44f, 0xb651, ff_rumble }, /* FGT Rumble Force Wheel */
  52. { 0x44f, 0xb654, ff_joystick }, /* FGT Force Feedback Wheel */
  53. };
  54. struct tmff_device {
  55. struct hid_report *report;
  56. struct hid_field *ff_field;
  57. };
  58. /* Changes values from 0 to 0xffff into values from minimum to maximum */
  59. static inline int hid_tmff_scale_u16(unsigned int in,
  60. int minimum, int maximum)
  61. {
  62. int ret;
  63. ret = (in * (maximum - minimum) / 0xffff) + minimum;
  64. if (ret < minimum)
  65. return minimum;
  66. if (ret > maximum)
  67. return maximum;
  68. return ret;
  69. }
  70. /* Changes values from -0x80 to 0x7f into values from minimum to maximum */
  71. static inline int hid_tmff_scale_s8(int in,
  72. int minimum, int maximum)
  73. {
  74. int ret;
  75. ret = (((in + 0x80) * (maximum - minimum)) / 0xff) + minimum;
  76. if (ret < minimum)
  77. return minimum;
  78. if (ret > maximum)
  79. return maximum;
  80. return ret;
  81. }
  82. static int hid_tmff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
  83. {
  84. struct hid_device *hid = input_get_drvdata(dev);
  85. struct tmff_device *tmff = data;
  86. struct hid_field *ff_field = tmff->ff_field;
  87. int x, y;
  88. int left, right; /* Rumbling */
  89. switch (effect->type) {
  90. case FF_CONSTANT:
  91. x = hid_tmff_scale_s8(effect->u.ramp.start_level,
  92. ff_field->logical_minimum,
  93. ff_field->logical_maximum);
  94. y = hid_tmff_scale_s8(effect->u.ramp.end_level,
  95. ff_field->logical_minimum,
  96. ff_field->logical_maximum);
  97. dbg_hid("(x, y)=(%04x, %04x)\n", x, y);
  98. ff_field->value[0] = x;
  99. ff_field->value[1] = y;
  100. usbhid_submit_report(hid, tmff->report, USB_DIR_OUT);
  101. break;
  102. case FF_RUMBLE:
  103. left = hid_tmff_scale_u16(effect->u.rumble.weak_magnitude,
  104. ff_field->logical_minimum,
  105. ff_field->logical_maximum);
  106. right = hid_tmff_scale_u16(effect->u.rumble.strong_magnitude,
  107. ff_field->logical_minimum,
  108. ff_field->logical_maximum);
  109. dbg_hid("(left,right)=(%08x, %08x)\n", left, right);
  110. ff_field->value[0] = left;
  111. ff_field->value[1] = right;
  112. usbhid_submit_report(hid, tmff->report, USB_DIR_OUT);
  113. break;
  114. }
  115. return 0;
  116. }
  117. int hid_tmff_init(struct hid_device *hid)
  118. {
  119. struct tmff_device *tmff;
  120. struct list_head *pos;
  121. struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  122. struct input_dev *input_dev = hidinput->input;
  123. const signed short *ff_bits = ff_joystick;
  124. int error;
  125. int i;
  126. tmff = kzalloc(sizeof(struct tmff_device), GFP_KERNEL);
  127. if (!tmff)
  128. return -ENOMEM;
  129. /* Find the report to use */
  130. list_for_each(pos, &hid->report_enum[HID_OUTPUT_REPORT].report_list) {
  131. struct hid_report *report = (struct hid_report *)pos;
  132. int fieldnum;
  133. for (fieldnum = 0; fieldnum < report->maxfield; ++fieldnum) {
  134. struct hid_field *field = report->field[fieldnum];
  135. if (field->maxusage <= 0)
  136. continue;
  137. switch (field->usage[0].hid) {
  138. case THRUSTMASTER_USAGE_FF:
  139. if (field->report_count < 2) {
  140. warn("ignoring FF field with report_count < 2");
  141. continue;
  142. }
  143. if (field->logical_maximum == field->logical_minimum) {
  144. warn("ignoring FF field with logical_maximum == logical_minimum");
  145. continue;
  146. }
  147. if (tmff->report && tmff->report != report) {
  148. warn("ignoring FF field in other report");
  149. continue;
  150. }
  151. if (tmff->ff_field && tmff->ff_field != field) {
  152. warn("ignoring duplicate FF field");
  153. continue;
  154. }
  155. tmff->report = report;
  156. tmff->ff_field = field;
  157. for (i = 0; i < ARRAY_SIZE(devices); i++) {
  158. if (input_dev->id.vendor == devices[i].idVendor &&
  159. input_dev->id.product == devices[i].idProduct) {
  160. ff_bits = devices[i].ff;
  161. break;
  162. }
  163. }
  164. for (i = 0; ff_bits[i] >= 0; i++)
  165. set_bit(ff_bits[i], input_dev->ffbit);
  166. break;
  167. default:
  168. warn("ignoring unknown output usage %08x", field->usage[0].hid);
  169. continue;
  170. }
  171. }
  172. }
  173. if (!tmff->report) {
  174. err("cant find FF field in output reports\n");
  175. error = -ENODEV;
  176. goto fail;
  177. }
  178. error = input_ff_create_memless(input_dev, tmff, hid_tmff_play);
  179. if (error)
  180. goto fail;
  181. info("Force feedback for ThrustMaster devices by Zinx Verituse <zinx@epicsol.org>");
  182. return 0;
  183. fail:
  184. kfree(tmff);
  185. return error;
  186. }