hid-lg4ff.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. struct lg4ff_device {
  29. struct hid_report *report;
  30. };
  31. static const signed short ff4_wheel_ac[] = {
  32. FF_CONSTANT,
  33. FF_AUTOCENTER,
  34. -1
  35. };
  36. static int hid_lg4ff_play(struct input_dev *dev, void *data,
  37. struct ff_effect *effect)
  38. {
  39. struct hid_device *hid = input_get_drvdata(dev);
  40. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  41. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  42. int x;
  43. #define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff
  44. switch (effect->type) {
  45. case FF_CONSTANT:
  46. x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */
  47. CLAMP(x);
  48. report->field[0]->value[0] = 0x11; /* Slot 1 */
  49. report->field[0]->value[1] = 0x10;
  50. report->field[0]->value[2] = x;
  51. report->field[0]->value[3] = 0x00;
  52. report->field[0]->value[4] = 0x00;
  53. report->field[0]->value[5] = 0x08;
  54. report->field[0]->value[6] = 0x00;
  55. dbg_hid("Autocenter, x=0x%02X\n", x);
  56. usbhid_submit_report(hid, report, USB_DIR_OUT);
  57. break;
  58. }
  59. return 0;
  60. }
  61. static void hid_lg4ff_set_autocenter(struct input_dev *dev, u16 magnitude)
  62. {
  63. struct hid_device *hid = input_get_drvdata(dev);
  64. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  65. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  66. __s32 *value = report->field[0]->value;
  67. *value++ = 0xfe;
  68. *value++ = 0x0d;
  69. *value++ = 0x07;
  70. *value++ = 0x07;
  71. *value++ = (magnitude >> 8) & 0xff;
  72. *value++ = 0x00;
  73. *value = 0x00;
  74. usbhid_submit_report(hid, report, USB_DIR_OUT);
  75. }
  76. int lg4ff_init(struct hid_device *hid)
  77. {
  78. struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  79. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  80. struct input_dev *dev = hidinput->input;
  81. struct hid_report *report;
  82. struct hid_field *field;
  83. const signed short *ff_bits = ff4_wheel_ac;
  84. int error;
  85. int i;
  86. /* Find the report to use */
  87. if (list_empty(report_list)) {
  88. hid_err(hid, "No output report found\n");
  89. return -1;
  90. }
  91. /* Check that the report looks ok */
  92. report = list_entry(report_list->next, struct hid_report, list);
  93. if (!report) {
  94. hid_err(hid, "NULL output report\n");
  95. return -1;
  96. }
  97. field = report->field[0];
  98. if (!field) {
  99. hid_err(hid, "NULL field\n");
  100. return -1;
  101. }
  102. for (i = 0; ff_bits[i] >= 0; i++)
  103. set_bit(ff_bits[i], dev->ffbit);
  104. error = input_ff_create_memless(dev, NULL, hid_lg4ff_play);
  105. if (error)
  106. return error;
  107. if (test_bit(FF_AUTOCENTER, dev->ffbit))
  108. dev->ff->set_autocenter = hid_lg4ff_set_autocenter;
  109. hid_info(hid, "Force feedback for Logitech Speed Force Wireless by Simon Wood <simon@mungewell.org>\n");
  110. return 0;
  111. }