hid-lg3ff.c 4.4 KB

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