hid-lg3ff.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Force feedback support for Logitech Flight System G940
  3. *
  4. * Copyright (c) 2009 Gary Stein <LordCnidarian@gmail.com>
  5. */
  6. /*
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/input.h>
  22. #include <linux/usb.h>
  23. #include <linux/hid.h>
  24. #include "usbhid/usbhid.h"
  25. #include "hid-lg.h"
  26. /*
  27. * G940 Theory of Operation (from experimentation)
  28. *
  29. * There are 63 fields (only 3 of them currently used)
  30. * 0 - seems to be command field
  31. * 1 - 30 deal with the x axis
  32. * 31 -60 deal with the y axis
  33. *
  34. * Field 1 is x axis constant force
  35. * Field 31 is y axis constant force
  36. *
  37. * other interesting fields 1,2,3,4 on x axis
  38. * (same for 31,32,33,34 on y axis)
  39. *
  40. * 0 0 127 127 makes the joystick autocenter hard
  41. *
  42. * 127 0 127 127 makes the joystick loose on the right,
  43. * but stops all movemnt left
  44. *
  45. * -127 0 -127 -127 makes the joystick loose on the left,
  46. * but stops all movement right
  47. *
  48. * 0 0 -127 -127 makes the joystick rattle very hard
  49. *
  50. * I'm sure these are effects that I don't know enough about them
  51. */
  52. struct lg3ff_device {
  53. struct hid_report *report;
  54. };
  55. static int hid_lg3ff_play(struct input_dev *dev, void *data,
  56. struct ff_effect *effect)
  57. {
  58. struct hid_device *hid = input_get_drvdata(dev);
  59. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  60. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  61. int x, y;
  62. /*
  63. * Available values in the field should always be 63, but we only use up to
  64. * 35. Instead, clear the entire area, however big it is.
  65. */
  66. memset(report->field[0]->value, 0,
  67. sizeof(__s32) * report->field[0]->report_count);
  68. switch (effect->type) {
  69. case FF_CONSTANT:
  70. /*
  71. * Already clamped in ff_memless
  72. * 0 is center (different then other logitech)
  73. */
  74. x = effect->u.ramp.start_level;
  75. y = effect->u.ramp.end_level;
  76. /* send command byte */
  77. report->field[0]->value[0] = 0x51;
  78. /*
  79. * Sign backwards from other Force3d pro
  80. * which get recast here in two's complement 8 bits
  81. */
  82. report->field[0]->value[1] = (unsigned char)(-x);
  83. report->field[0]->value[31] = (unsigned char)(-y);
  84. usbhid_submit_report(hid, report, USB_DIR_OUT);
  85. break;
  86. }
  87. return 0;
  88. }
  89. static void hid_lg3ff_set_autocenter(struct input_dev *dev, u16 magnitude)
  90. {
  91. struct hid_device *hid = input_get_drvdata(dev);
  92. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  93. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  94. /*
  95. * Auto Centering probed from device
  96. * NOTE: deadman's switch on G940 must be covered
  97. * for effects to work
  98. */
  99. report->field[0]->value[0] = 0x51;
  100. report->field[0]->value[1] = 0x00;
  101. report->field[0]->value[2] = 0x00;
  102. report->field[0]->value[3] = 0x7F;
  103. report->field[0]->value[4] = 0x7F;
  104. report->field[0]->value[31] = 0x00;
  105. report->field[0]->value[32] = 0x00;
  106. report->field[0]->value[33] = 0x7F;
  107. report->field[0]->value[34] = 0x7F;
  108. usbhid_submit_report(hid, report, USB_DIR_OUT);
  109. }
  110. static const signed short ff3_joystick_ac[] = {
  111. FF_CONSTANT,
  112. FF_AUTOCENTER,
  113. -1
  114. };
  115. int lg3ff_init(struct hid_device *hid)
  116. {
  117. struct hid_input *hidinput;
  118. struct input_dev *dev;
  119. const signed short *ff_bits = ff3_joystick_ac;
  120. int error;
  121. int i;
  122. if (list_empty(&hid->inputs)) {
  123. hid_err(hid, "no inputs found\n");
  124. return -ENODEV;
  125. }
  126. hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  127. dev = hidinput->input;
  128. /* Check that the report looks ok */
  129. if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 35))
  130. return -ENODEV;
  131. /* Assume single fixed device G940 */
  132. for (i = 0; ff_bits[i] >= 0; i++)
  133. set_bit(ff_bits[i], dev->ffbit);
  134. error = input_ff_create_memless(dev, NULL, hid_lg3ff_play);
  135. if (error)
  136. return error;
  137. if (test_bit(FF_AUTOCENTER, dev->ffbit))
  138. dev->ff->set_autocenter = hid_lg3ff_set_autocenter;
  139. hid_info(hid, "Force feedback for Logitech Flight System G940 by Gary Stein <LordCnidarian@gmail.com>\n");
  140. return 0;
  141. }