hid-lg4ff.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Force feedback support for Logitech Speed Force Wireless
  3. *
  4. * http://wiibrew.org/wiki/Logitech_USB_steering_wheel
  5. *
  6. * Copyright (c) 2010 Simon Wood <simon@mungewell.org>
  7. */
  8. /*
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/input.h>
  24. #include <linux/usb.h>
  25. #include <linux/hid.h>
  26. #include "usbhid/usbhid.h"
  27. #include "hid-lg.h"
  28. #include "hid-ids.h"
  29. static const signed short lg4ff_wheel_effects[] = {
  30. FF_CONSTANT,
  31. FF_AUTOCENTER,
  32. -1
  33. };
  34. struct lg4ff_wheel {
  35. const __u32 product_id;
  36. const signed short *ff_effects;
  37. const __u16 min_range;
  38. const __u16 max_range;
  39. };
  40. static const struct lg4ff_wheel lg4ff_devices[] = {
  41. {USB_DEVICE_ID_LOGITECH_WHEEL, lg4ff_wheel_effects, 40, 270},
  42. {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL, lg4ff_wheel_effects, 40, 270},
  43. {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, lg4ff_wheel_effects, 40, 900},
  44. {USB_DEVICE_ID_LOGITECH_G25_WHEEL, lg4ff_wheel_effects, 40, 900},
  45. {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, lg4ff_wheel_effects, 40, 900},
  46. {USB_DEVICE_ID_LOGITECH_G27_WHEEL, lg4ff_wheel_effects, 40, 900},
  47. {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270},
  48. {USB_DEVICE_ID_LOGITECH_WII_WHEEL, lg4ff_wheel_effects, 40, 270}
  49. };
  50. static int hid_lg4ff_play(struct input_dev *dev, void *data,
  51. struct ff_effect *effect)
  52. {
  53. struct hid_device *hid = input_get_drvdata(dev);
  54. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  55. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  56. int x;
  57. #define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff
  58. switch (effect->type) {
  59. case FF_CONSTANT:
  60. x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */
  61. CLAMP(x);
  62. report->field[0]->value[0] = 0x11; /* Slot 1 */
  63. report->field[0]->value[1] = 0x08;
  64. report->field[0]->value[2] = x;
  65. report->field[0]->value[3] = 0x80;
  66. report->field[0]->value[4] = 0x00;
  67. report->field[0]->value[5] = 0x00;
  68. report->field[0]->value[6] = 0x00;
  69. usbhid_submit_report(hid, report, USB_DIR_OUT);
  70. break;
  71. }
  72. return 0;
  73. }
  74. static void hid_lg4ff_set_autocenter(struct input_dev *dev, u16 magnitude)
  75. {
  76. struct hid_device *hid = input_get_drvdata(dev);
  77. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  78. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  79. report->field[0]->value[0] = 0xfe;
  80. report->field[0]->value[1] = 0x0d;
  81. report->field[0]->value[2] = magnitude >> 13;
  82. report->field[0]->value[3] = magnitude >> 13;
  83. report->field[0]->value[4] = magnitude >> 8;
  84. report->field[0]->value[5] = 0x00;
  85. report->field[0]->value[6] = 0x00;
  86. usbhid_submit_report(hid, report, USB_DIR_OUT);
  87. }
  88. int lg4ff_init(struct hid_device *hid)
  89. {
  90. struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  91. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  92. struct input_dev *dev = hidinput->input;
  93. struct hid_report *report;
  94. struct hid_field *field;
  95. int error, i, j;
  96. /* Find the report to use */
  97. if (list_empty(report_list)) {
  98. hid_err(hid, "No output report found\n");
  99. return -1;
  100. }
  101. /* Check that the report looks ok */
  102. report = list_entry(report_list->next, struct hid_report, list);
  103. if (!report) {
  104. hid_err(hid, "NULL output report\n");
  105. return -1;
  106. }
  107. field = report->field[0];
  108. if (!field) {
  109. hid_err(hid, "NULL field\n");
  110. return -1;
  111. }
  112. /* Check what wheel has been connected */
  113. for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
  114. if (hid->product == lg4ff_devices[i].product_id) {
  115. dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
  116. break;
  117. }
  118. }
  119. if (i == ARRAY_SIZE(lg4ff_devices)) {
  120. hid_err(hid, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to"
  121. "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n");
  122. return -1;
  123. }
  124. /* Set supported force feedback capabilities */
  125. for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
  126. set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
  127. error = input_ff_create_memless(dev, NULL, hid_lg4ff_play);
  128. if (error)
  129. return error;
  130. if (test_bit(FF_AUTOCENTER, dev->ffbit))
  131. dev->ff->set_autocenter = hid_lg4ff_set_autocenter;
  132. hid_info(hid, "Force feedback for Logitech Speed Force Wireless by Simon Wood <simon@mungewell.org>\n");
  133. return 0;
  134. }