hid-lg2ff.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Force feedback support for Logitech RumblePad and Rumblepad 2
  4. *
  5. * Copyright (c) 2008 Anssi Hannula <[email protected]>
  6. */
  7. /*
  8. */
  9. #include <linux/input.h>
  10. #include <linux/slab.h>
  11. #include <linux/hid.h>
  12. #include "hid-lg.h"
  13. struct lg2ff_device {
  14. struct hid_report *report;
  15. };
  16. static int play_effect(struct input_dev *dev, void *data,
  17. struct ff_effect *effect)
  18. {
  19. struct hid_device *hid = input_get_drvdata(dev);
  20. struct lg2ff_device *lg2ff = data;
  21. int weak, strong;
  22. strong = effect->u.rumble.strong_magnitude;
  23. weak = effect->u.rumble.weak_magnitude;
  24. if (weak || strong) {
  25. weak = weak * 0xff / 0xffff;
  26. strong = strong * 0xff / 0xffff;
  27. lg2ff->report->field[0]->value[0] = 0x51;
  28. lg2ff->report->field[0]->value[2] = weak;
  29. lg2ff->report->field[0]->value[4] = strong;
  30. } else {
  31. lg2ff->report->field[0]->value[0] = 0xf3;
  32. lg2ff->report->field[0]->value[2] = 0x00;
  33. lg2ff->report->field[0]->value[4] = 0x00;
  34. }
  35. hid_hw_request(hid, lg2ff->report, HID_REQ_SET_REPORT);
  36. return 0;
  37. }
  38. int lg2ff_init(struct hid_device *hid)
  39. {
  40. struct lg2ff_device *lg2ff;
  41. struct hid_report *report;
  42. struct hid_input *hidinput;
  43. struct input_dev *dev;
  44. int error;
  45. if (list_empty(&hid->inputs)) {
  46. hid_err(hid, "no inputs found\n");
  47. return -ENODEV;
  48. }
  49. hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  50. dev = hidinput->input;
  51. /* Check that the report looks ok */
  52. report = hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7);
  53. if (!report)
  54. return -ENODEV;
  55. lg2ff = kmalloc(sizeof(struct lg2ff_device), GFP_KERNEL);
  56. if (!lg2ff)
  57. return -ENOMEM;
  58. set_bit(FF_RUMBLE, dev->ffbit);
  59. error = input_ff_create_memless(dev, lg2ff, play_effect);
  60. if (error) {
  61. kfree(lg2ff);
  62. return error;
  63. }
  64. lg2ff->report = report;
  65. report->field[0]->value[0] = 0xf3;
  66. report->field[0]->value[1] = 0x00;
  67. report->field[0]->value[2] = 0x00;
  68. report->field[0]->value[3] = 0x00;
  69. report->field[0]->value[4] = 0x00;
  70. report->field[0]->value[5] = 0x00;
  71. report->field[0]->value[6] = 0x00;
  72. hid_hw_request(hid, report, HID_REQ_SET_REPORT);
  73. hid_info(hid, "Force feedback for Logitech variant 2 rumble devices by Anssi Hannula <[email protected]>\n");
  74. return 0;
  75. }