hid-lgff.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Force feedback support for hid-compliant for some of the devices from
  4. * Logitech, namely:
  5. * - WingMan Cordless RumblePad
  6. * - WingMan Force 3D
  7. *
  8. * Copyright (c) 2002-2004 Johann Deneux
  9. * Copyright (c) 2006 Anssi Hannula <[email protected]>
  10. */
  11. /*
  12. *
  13. * Should you need to contact me, the author, you can do so by
  14. * e-mail - mail your message to <[email protected]>
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/input.h>
  18. #include <linux/hid.h>
  19. #include "hid-lg.h"
  20. struct dev_type {
  21. u16 idVendor;
  22. u16 idProduct;
  23. const signed short *ff;
  24. };
  25. static const signed short ff_rumble[] = {
  26. FF_RUMBLE,
  27. -1
  28. };
  29. static const signed short ff_joystick[] = {
  30. FF_CONSTANT,
  31. -1
  32. };
  33. static const signed short ff_joystick_ac[] = {
  34. FF_CONSTANT,
  35. FF_AUTOCENTER,
  36. -1
  37. };
  38. static const struct dev_type devices[] = {
  39. { 0x046d, 0xc211, ff_rumble },
  40. { 0x046d, 0xc219, ff_rumble },
  41. { 0x046d, 0xc283, ff_joystick },
  42. { 0x046d, 0xc286, ff_joystick_ac },
  43. { 0x046d, 0xc287, ff_joystick_ac },
  44. { 0x046d, 0xc293, ff_joystick },
  45. { 0x046d, 0xc295, ff_joystick },
  46. };
  47. static int hid_lgff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
  48. {
  49. struct hid_device *hid = input_get_drvdata(dev);
  50. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  51. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  52. int x, y;
  53. unsigned int left, right;
  54. #define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff
  55. switch (effect->type) {
  56. case FF_CONSTANT:
  57. x = effect->u.ramp.start_level + 0x7f; /* 0x7f is center */
  58. y = effect->u.ramp.end_level + 0x7f;
  59. CLAMP(x);
  60. CLAMP(y);
  61. report->field[0]->value[0] = 0x51;
  62. report->field[0]->value[1] = 0x08;
  63. report->field[0]->value[2] = x;
  64. report->field[0]->value[3] = y;
  65. dbg_hid("(x, y)=(%04x, %04x)\n", x, y);
  66. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  67. break;
  68. case FF_RUMBLE:
  69. right = effect->u.rumble.strong_magnitude;
  70. left = effect->u.rumble.weak_magnitude;
  71. right = right * 0xff / 0xffff;
  72. left = left * 0xff / 0xffff;
  73. CLAMP(left);
  74. CLAMP(right);
  75. report->field[0]->value[0] = 0x42;
  76. report->field[0]->value[1] = 0x00;
  77. report->field[0]->value[2] = left;
  78. report->field[0]->value[3] = right;
  79. dbg_hid("(left, right)=(%04x, %04x)\n", left, right);
  80. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  81. break;
  82. }
  83. return 0;
  84. }
  85. static void hid_lgff_set_autocenter(struct input_dev *dev, u16 magnitude)
  86. {
  87. struct hid_device *hid = input_get_drvdata(dev);
  88. struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  89. struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
  90. __s32 *value = report->field[0]->value;
  91. magnitude = (magnitude >> 12) & 0xf;
  92. *value++ = 0xfe;
  93. *value++ = 0x0d;
  94. *value++ = magnitude; /* clockwise strength */
  95. *value++ = magnitude; /* counter-clockwise strength */
  96. *value++ = 0x80;
  97. *value++ = 0x00;
  98. *value = 0x00;
  99. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  100. }
  101. int lgff_init(struct hid_device* hid)
  102. {
  103. struct hid_input *hidinput;
  104. struct input_dev *dev;
  105. const signed short *ff_bits = ff_joystick;
  106. int error;
  107. int i;
  108. if (list_empty(&hid->inputs)) {
  109. hid_err(hid, "no inputs found\n");
  110. return -ENODEV;
  111. }
  112. hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  113. dev = hidinput->input;
  114. /* Check that the report looks ok */
  115. if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
  116. return -ENODEV;
  117. for (i = 0; i < ARRAY_SIZE(devices); i++) {
  118. if (dev->id.vendor == devices[i].idVendor &&
  119. dev->id.product == devices[i].idProduct) {
  120. ff_bits = devices[i].ff;
  121. break;
  122. }
  123. }
  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_lgff_play);
  127. if (error)
  128. return error;
  129. if ( test_bit(FF_AUTOCENTER, dev->ffbit) )
  130. dev->ff->set_autocenter = hid_lgff_set_autocenter;
  131. pr_info("Force feedback for Logitech force feedback devices by Johann Deneux <[email protected]>\n");
  132. return 0;
  133. }