hid-lg3ff.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/hid.h>
  23. #include "hid-lg.h"
  24. /*
  25. * G940 Theory of Operation (from experimentation)
  26. *
  27. * There are 63 fields (only 3 of them currently used)
  28. * 0 - seems to be command field
  29. * 1 - 30 deal with the x axis
  30. * 31 -60 deal with the y axis
  31. *
  32. * Field 1 is x axis constant force
  33. * Field 31 is y axis constant force
  34. *
  35. * other interesting fields 1,2,3,4 on x axis
  36. * (same for 31,32,33,34 on y axis)
  37. *
  38. * 0 0 127 127 makes the joystick autocenter hard
  39. *
  40. * 127 0 127 127 makes the joystick loose on the right,
  41. * but stops all movemnt left
  42. *
  43. * -127 0 -127 -127 makes the joystick loose on the left,
  44. * but stops all movement right
  45. *
  46. * 0 0 -127 -127 makes the joystick rattle very hard
  47. *
  48. * I'm sure these are effects that I don't know enough about them
  49. */
  50. struct lg3ff_device {
  51. struct hid_report *report;
  52. };
  53. static int hid_lg3ff_play(struct input_dev *dev, void *data,
  54. struct ff_effect *effect)
  55. {
  56. struct hid_device *hid = input_get_drvdata(dev);
  57. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  58. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  59. int x, y;
  60. /*
  61. * Maxusage should always be 63 (maximum fields)
  62. * likely a better way to ensure this data is clean
  63. */
  64. memset(report->field[0]->value, 0, sizeof(__s32)*report->field[0]->maxusage);
  65. switch (effect->type) {
  66. case FF_CONSTANT:
  67. /*
  68. * Already clamped in ff_memless
  69. * 0 is center (different then other logitech)
  70. */
  71. x = effect->u.ramp.start_level;
  72. y = effect->u.ramp.end_level;
  73. /* send command byte */
  74. report->field[0]->value[0] = 0x51;
  75. /*
  76. * Sign backwards from other Force3d pro
  77. * which get recast here in two's complement 8 bits
  78. */
  79. report->field[0]->value[1] = (unsigned char)(-x);
  80. report->field[0]->value[31] = (unsigned char)(-y);
  81. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  82. break;
  83. }
  84. return 0;
  85. }
  86. static void hid_lg3ff_set_autocenter(struct input_dev *dev, u16 magnitude)
  87. {
  88. struct hid_device *hid = input_get_drvdata(dev);
  89. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  90. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  91. /*
  92. * Auto Centering probed from device
  93. * NOTE: deadman's switch on G940 must be covered
  94. * for effects to work
  95. */
  96. report->field[0]->value[0] = 0x51;
  97. report->field[0]->value[1] = 0x00;
  98. report->field[0]->value[2] = 0x00;
  99. report->field[0]->value[3] = 0x7F;
  100. report->field[0]->value[4] = 0x7F;
  101. report->field[0]->value[31] = 0x00;
  102. report->field[0]->value[32] = 0x00;
  103. report->field[0]->value[33] = 0x7F;
  104. report->field[0]->value[34] = 0x7F;
  105. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  106. }
  107. static const signed short ff3_joystick_ac[] = {
  108. FF_CONSTANT,
  109. FF_AUTOCENTER,
  110. -1
  111. };
  112. int lg3ff_init(struct hid_device *hid)
  113. {
  114. struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  115. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  116. struct input_dev *dev = hidinput->input;
  117. struct hid_report *report;
  118. struct hid_field *field;
  119. const signed short *ff_bits = ff3_joystick_ac;
  120. int error;
  121. int i;
  122. /* Find the report to use */
  123. if (list_empty(report_list)) {
  124. hid_err(hid, "No output report found\n");
  125. return -1;
  126. }
  127. /* Check that the report looks ok */
  128. report = list_entry(report_list->next, struct hid_report, list);
  129. if (!report) {
  130. hid_err(hid, "NULL output report\n");
  131. return -1;
  132. }
  133. field = report->field[0];
  134. if (!field) {
  135. hid_err(hid, "NULL field\n");
  136. return -1;
  137. }
  138. /* Assume single fixed device G940 */
  139. for (i = 0; ff_bits[i] >= 0; i++)
  140. set_bit(ff_bits[i], dev->ffbit);
  141. error = input_ff_create_memless(dev, NULL, hid_lg3ff_play);
  142. if (error)
  143. return error;
  144. if (test_bit(FF_AUTOCENTER, dev->ffbit))
  145. dev->ff->set_autocenter = hid_lg3ff_set_autocenter;
  146. hid_info(hid, "Force feedback for Logitech Flight System G940 by Gary Stein <LordCnidarian@gmail.com>\n");
  147. return 0;
  148. }