hid-tmff.c 4.1 KB

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