hid-lg3ff.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. * Maxusage should always be 63 (maximum fields)
  64. * likely a better way to ensure this data is clean
  65. */
  66. memset(report->field[0]->value, 0, sizeof(__s32)*report->field[0]->maxusage);
  67. switch (effect->type) {
  68. case FF_CONSTANT:
  69. /*
  70. * Already clamped in ff_memless
  71. * 0 is center (different then other logitech)
  72. */
  73. x = effect->u.ramp.start_level;
  74. y = effect->u.ramp.end_level;
  75. /* send command byte */
  76. report->field[0]->value[0] = 0x51;
  77. /*
  78. * Sign backwards from other Force3d pro
  79. * which get recast here in two's complement 8 bits
  80. */
  81. report->field[0]->value[1] = (unsigned char)(-x);
  82. report->field[0]->value[31] = (unsigned char)(-y);
  83. usbhid_submit_report(hid, report, USB_DIR_OUT);
  84. break;
  85. }
  86. return 0;
  87. }
  88. static void hid_lg3ff_set_autocenter(struct input_dev *dev, u16 magnitude)
  89. {
  90. struct hid_device *hid = input_get_drvdata(dev);
  91. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  92. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  93. /*
  94. * Auto Centering probed from device
  95. * NOTE: deadman's switch on G940 must be covered
  96. * for effects to work
  97. */
  98. report->field[0]->value[0] = 0x51;
  99. report->field[0]->value[1] = 0x00;
  100. report->field[0]->value[2] = 0x00;
  101. report->field[0]->value[3] = 0x7F;
  102. report->field[0]->value[4] = 0x7F;
  103. report->field[0]->value[31] = 0x00;
  104. report->field[0]->value[32] = 0x00;
  105. report->field[0]->value[33] = 0x7F;
  106. report->field[0]->value[34] = 0x7F;
  107. usbhid_submit_report(hid, report, USB_DIR_OUT);
  108. }
  109. static const signed short ff3_joystick_ac[] = {
  110. FF_CONSTANT,
  111. FF_AUTOCENTER,
  112. -1
  113. };
  114. int lg3ff_init(struct hid_device *hid)
  115. {
  116. struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  117. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  118. struct input_dev *dev = hidinput->input;
  119. struct hid_report *report;
  120. struct hid_field *field;
  121. const signed short *ff_bits = ff3_joystick_ac;
  122. int error;
  123. int i;
  124. /* Find the report to use */
  125. if (list_empty(report_list)) {
  126. hid_err(hid, "No output report found\n");
  127. return -1;
  128. }
  129. /* Check that the report looks ok */
  130. report = list_entry(report_list->next, struct hid_report, list);
  131. if (!report) {
  132. hid_err(hid, "NULL output report\n");
  133. return -1;
  134. }
  135. field = report->field[0];
  136. if (!field) {
  137. hid_err(hid, "NULL field\n");
  138. return -1;
  139. }
  140. /* Assume single fixed device G940 */
  141. for (i = 0; ff_bits[i] >= 0; i++)
  142. set_bit(ff_bits[i], dev->ffbit);
  143. error = input_ff_create_memless(dev, NULL, hid_lg3ff_play);
  144. if (error)
  145. return error;
  146. if (test_bit(FF_AUTOCENTER, dev->ffbit))
  147. dev->ff->set_autocenter = hid_lg3ff_set_autocenter;
  148. hid_info(hid, "Force feedback for Logitech Flight System G940 by Gary Stein <LordCnidarian@gmail.com>\n");
  149. return 0;
  150. }