hid-tmff.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "hid.h"
  32. /* Usages for thrustmaster devices I know about */
  33. #define THRUSTMASTER_USAGE_RUMBLE_LR (HID_UP_GENDESK | 0xbb)
  34. struct tmff_device {
  35. struct hid_report *report;
  36. struct hid_field *rumble;
  37. };
  38. /* Changes values from 0 to 0xffff into values from minimum to maximum */
  39. static inline int hid_tmff_scale(unsigned int in, int minimum, int maximum)
  40. {
  41. int ret;
  42. ret = (in * (maximum - minimum) / 0xffff) + minimum;
  43. if (ret < minimum)
  44. return minimum;
  45. if (ret > maximum)
  46. return maximum;
  47. return ret;
  48. }
  49. static int hid_tmff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
  50. {
  51. struct hid_device *hid = dev->private;
  52. struct tmff_device *tmff = data;
  53. int left, right; /* Rumbling */
  54. left = hid_tmff_scale(effect->u.rumble.weak_magnitude,
  55. tmff->rumble->logical_minimum, tmff->rumble->logical_maximum);
  56. right = hid_tmff_scale(effect->u.rumble.strong_magnitude,
  57. tmff->rumble->logical_minimum, tmff->rumble->logical_maximum);
  58. tmff->rumble->value[0] = left;
  59. tmff->rumble->value[1] = right;
  60. dbg("(left,right)=(%08x, %08x)", left, right);
  61. hid_submit_report(hid, tmff->report, USB_DIR_OUT);
  62. return 0;
  63. }
  64. int hid_tmff_init(struct hid_device *hid)
  65. {
  66. struct tmff_device *tmff;
  67. struct list_head *pos;
  68. struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  69. struct input_dev *input_dev = hidinput->input;
  70. int error;
  71. tmff = kzalloc(sizeof(struct tmff_device), GFP_KERNEL);
  72. if (!tmff)
  73. return -ENOMEM;
  74. /* Find the report to use */
  75. __list_for_each(pos, &hid->report_enum[HID_OUTPUT_REPORT].report_list) {
  76. struct hid_report *report = (struct hid_report *)pos;
  77. int fieldnum;
  78. for (fieldnum = 0; fieldnum < report->maxfield; ++fieldnum) {
  79. struct hid_field *field = report->field[fieldnum];
  80. if (field->maxusage <= 0)
  81. continue;
  82. switch (field->usage[0].hid) {
  83. case THRUSTMASTER_USAGE_RUMBLE_LR:
  84. if (field->report_count < 2) {
  85. warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR with report_count < 2");
  86. continue;
  87. }
  88. if (field->logical_maximum == field->logical_minimum) {
  89. warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR with logical_maximum == logical_minimum");
  90. continue;
  91. }
  92. if (tmff->report && tmff->report != report) {
  93. warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR in other report");
  94. continue;
  95. }
  96. if (tmff->rumble && tmff->rumble != field) {
  97. warn("ignoring duplicate THRUSTMASTER_USAGE_RUMBLE_LR");
  98. continue;
  99. }
  100. tmff->report = report;
  101. tmff->rumble = field;
  102. set_bit(FF_RUMBLE, input_dev->ffbit);
  103. break;
  104. default:
  105. warn("ignoring unknown output usage %08x", field->usage[0].hid);
  106. continue;
  107. }
  108. }
  109. }
  110. error = input_ff_create_memless(input_dev, tmff, hid_tmff_play);
  111. if (error) {
  112. kfree(tmff);
  113. return error;
  114. }
  115. info("Force feedback for ThrustMaster rumble pad devices by Zinx Verituse <zinx@epicsol.org>");
  116. return 0;
  117. }