hid-gaff.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Force feedback support for GreenAsia (Product ID 0x12) based devices
  4. *
  5. * The devices are distributed under various names and the same USB device ID
  6. * can be used in many game controllers.
  7. *
  8. * 0e8f:0012 "GreenAsia Inc. USB Joystick "
  9. * - tested with MANTA Warior MM816 and SpeedLink Strike2 SL-6635.
  10. *
  11. * Copyright (c) 2008 Lukasz Lubojanski <[email protected]>
  12. */
  13. /*
  14. */
  15. #include <linux/input.h>
  16. #include <linux/slab.h>
  17. #include <linux/hid.h>
  18. #include <linux/module.h>
  19. #include "hid-ids.h"
  20. #ifdef CONFIG_GREENASIA_FF
  21. struct gaff_device {
  22. struct hid_report *report;
  23. };
  24. static int hid_gaff_play(struct input_dev *dev, void *data,
  25. struct ff_effect *effect)
  26. {
  27. struct hid_device *hid = input_get_drvdata(dev);
  28. struct gaff_device *gaff = data;
  29. int left, right;
  30. left = effect->u.rumble.strong_magnitude;
  31. right = effect->u.rumble.weak_magnitude;
  32. dbg_hid("called with 0x%04x 0x%04x", left, right);
  33. left = left * 0xfe / 0xffff;
  34. right = right * 0xfe / 0xffff;
  35. gaff->report->field[0]->value[0] = 0x51;
  36. gaff->report->field[0]->value[1] = 0x0;
  37. gaff->report->field[0]->value[2] = right;
  38. gaff->report->field[0]->value[3] = 0;
  39. gaff->report->field[0]->value[4] = left;
  40. gaff->report->field[0]->value[5] = 0;
  41. dbg_hid("running with 0x%02x 0x%02x", left, right);
  42. hid_hw_request(hid, gaff->report, HID_REQ_SET_REPORT);
  43. gaff->report->field[0]->value[0] = 0xfa;
  44. gaff->report->field[0]->value[1] = 0xfe;
  45. gaff->report->field[0]->value[2] = 0x0;
  46. gaff->report->field[0]->value[4] = 0x0;
  47. hid_hw_request(hid, gaff->report, HID_REQ_SET_REPORT);
  48. return 0;
  49. }
  50. static int gaff_init(struct hid_device *hid)
  51. {
  52. struct gaff_device *gaff;
  53. struct hid_report *report;
  54. struct hid_input *hidinput;
  55. struct list_head *report_list =
  56. &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  57. struct list_head *report_ptr = report_list;
  58. struct input_dev *dev;
  59. int error;
  60. if (list_empty(&hid->inputs)) {
  61. hid_err(hid, "no inputs found\n");
  62. return -ENODEV;
  63. }
  64. hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  65. dev = hidinput->input;
  66. if (list_empty(report_list)) {
  67. hid_err(hid, "no output reports found\n");
  68. return -ENODEV;
  69. }
  70. report_ptr = report_ptr->next;
  71. report = list_entry(report_ptr, struct hid_report, list);
  72. if (report->maxfield < 1) {
  73. hid_err(hid, "no fields in the report\n");
  74. return -ENODEV;
  75. }
  76. if (report->field[0]->report_count < 6) {
  77. hid_err(hid, "not enough values in the field\n");
  78. return -ENODEV;
  79. }
  80. gaff = kzalloc(sizeof(struct gaff_device), GFP_KERNEL);
  81. if (!gaff)
  82. return -ENOMEM;
  83. set_bit(FF_RUMBLE, dev->ffbit);
  84. error = input_ff_create_memless(dev, gaff, hid_gaff_play);
  85. if (error) {
  86. kfree(gaff);
  87. return error;
  88. }
  89. gaff->report = report;
  90. gaff->report->field[0]->value[0] = 0x51;
  91. gaff->report->field[0]->value[1] = 0x00;
  92. gaff->report->field[0]->value[2] = 0x00;
  93. gaff->report->field[0]->value[3] = 0x00;
  94. hid_hw_request(hid, gaff->report, HID_REQ_SET_REPORT);
  95. gaff->report->field[0]->value[0] = 0xfa;
  96. gaff->report->field[0]->value[1] = 0xfe;
  97. hid_hw_request(hid, gaff->report, HID_REQ_SET_REPORT);
  98. hid_info(hid, "Force Feedback for GreenAsia 0x12 devices by Lukasz Lubojanski <[email protected]>\n");
  99. return 0;
  100. }
  101. #else
  102. static inline int gaff_init(struct hid_device *hdev)
  103. {
  104. return 0;
  105. }
  106. #endif
  107. static int ga_probe(struct hid_device *hdev, const struct hid_device_id *id)
  108. {
  109. int ret;
  110. dev_dbg(&hdev->dev, "Greenasia HID hardware probe...");
  111. ret = hid_parse(hdev);
  112. if (ret) {
  113. hid_err(hdev, "parse failed\n");
  114. goto err;
  115. }
  116. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  117. if (ret) {
  118. hid_err(hdev, "hw start failed\n");
  119. goto err;
  120. }
  121. gaff_init(hdev);
  122. return 0;
  123. err:
  124. return ret;
  125. }
  126. static const struct hid_device_id ga_devices[] = {
  127. { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0012), },
  128. { }
  129. };
  130. MODULE_DEVICE_TABLE(hid, ga_devices);
  131. static struct hid_driver ga_driver = {
  132. .name = "greenasia",
  133. .id_table = ga_devices,
  134. .probe = ga_probe,
  135. };
  136. module_hid_driver(ga_driver);
  137. MODULE_LICENSE("GPL");