hid-pl.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Force feedback support for PantherLord/GreenAsia based devices
  4. *
  5. * The devices are distributed under various names and the same USB device ID
  6. * can be used in both adapters and actual game controllers.
  7. *
  8. * 0810:0001 "Twin USB Joystick"
  9. * - tested with PantherLord USB/PS2 2in1 Adapter
  10. * - contains two reports, one for each port (HID_QUIRK_MULTI_INPUT)
  11. *
  12. * 0e8f:0003 "GreenAsia Inc. USB Joystick "
  13. * - tested with König Gaming gamepad
  14. *
  15. * 0e8f:0003 "GASIA USB Gamepad"
  16. * - another version of the König gamepad
  17. *
  18. * 0f30:0111 "Saitek Color Rumble Pad"
  19. *
  20. * Copyright (c) 2007, 2009 Anssi Hannula <anssi.hannula@gmail.com>
  21. */
  22. /*
  23. */
  24. /* #define DEBUG */
  25. #define debug(format, arg...) pr_debug("hid-plff: " format "\n" , ## arg)
  26. #include <linux/input.h>
  27. #include <linux/slab.h>
  28. #include <linux/module.h>
  29. #include <linux/hid.h>
  30. #include "hid-ids.h"
  31. #ifdef CONFIG_PANTHERLORD_FF
  32. struct plff_device {
  33. struct hid_report *report;
  34. s32 maxval;
  35. s32 *strong;
  36. s32 *weak;
  37. };
  38. static int hid_plff_play(struct input_dev *dev, void *data,
  39. struct ff_effect *effect)
  40. {
  41. struct hid_device *hid = input_get_drvdata(dev);
  42. struct plff_device *plff = data;
  43. int left, right;
  44. left = effect->u.rumble.strong_magnitude;
  45. right = effect->u.rumble.weak_magnitude;
  46. debug("called with 0x%04x 0x%04x", left, right);
  47. left = left * plff->maxval / 0xffff;
  48. right = right * plff->maxval / 0xffff;
  49. *plff->strong = left;
  50. *plff->weak = right;
  51. debug("running with 0x%02x 0x%02x", left, right);
  52. hid_hw_request(hid, plff->report, HID_REQ_SET_REPORT);
  53. return 0;
  54. }
  55. static int plff_init(struct hid_device *hid)
  56. {
  57. struct plff_device *plff;
  58. struct hid_report *report;
  59. struct hid_input *hidinput;
  60. struct list_head *report_list =
  61. &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  62. struct list_head *report_ptr = report_list;
  63. struct input_dev *dev;
  64. int error;
  65. s32 maxval;
  66. s32 *strong;
  67. s32 *weak;
  68. /* The device contains one output report per physical device, all
  69. containing 1 field, which contains 4 ff00.0002 usages and 4 16bit
  70. absolute values.
  71. The input reports also contain a field which contains
  72. 8 ff00.0001 usages and 8 boolean values. Their meaning is
  73. currently unknown.
  74. A version of the 0e8f:0003 exists that has all the values in
  75. separate fields and misses the extra input field, thus resembling
  76. Zeroplus (hid-zpff) devices.
  77. */
  78. if (list_empty(report_list)) {
  79. hid_err(hid, "no output reports found\n");
  80. return -ENODEV;
  81. }
  82. list_for_each_entry(hidinput, &hid->inputs, list) {
  83. report_ptr = report_ptr->next;
  84. if (report_ptr == report_list) {
  85. hid_err(hid, "required output report is missing\n");
  86. return -ENODEV;
  87. }
  88. report = list_entry(report_ptr, struct hid_report, list);
  89. if (report->maxfield < 1) {
  90. hid_err(hid, "no fields in the report\n");
  91. return -ENODEV;
  92. }
  93. maxval = 0x7f;
  94. if (report->field[0]->report_count >= 4) {
  95. report->field[0]->value[0] = 0x00;
  96. report->field[0]->value[1] = 0x00;
  97. strong = &report->field[0]->value[2];
  98. weak = &report->field[0]->value[3];
  99. debug("detected single-field device");
  100. } else if (report->field[0]->maxusage == 1 &&
  101. report->field[0]->usage[0].hid ==
  102. (HID_UP_LED | 0x43) &&
  103. report->maxfield >= 4 &&
  104. report->field[0]->report_count >= 1 &&
  105. report->field[1]->report_count >= 1 &&
  106. report->field[2]->report_count >= 1 &&
  107. report->field[3]->report_count >= 1) {
  108. report->field[0]->value[0] = 0x00;
  109. report->field[1]->value[0] = 0x00;
  110. strong = &report->field[2]->value[0];
  111. weak = &report->field[3]->value[0];
  112. if (hid->vendor == USB_VENDOR_ID_JESS2)
  113. maxval = 0xff;
  114. debug("detected 4-field device");
  115. } else {
  116. hid_err(hid, "not enough fields or values\n");
  117. return -ENODEV;
  118. }
  119. plff = kzalloc(sizeof(struct plff_device), GFP_KERNEL);
  120. if (!plff)
  121. return -ENOMEM;
  122. dev = hidinput->input;
  123. set_bit(FF_RUMBLE, dev->ffbit);
  124. error = input_ff_create_memless(dev, plff, hid_plff_play);
  125. if (error) {
  126. kfree(plff);
  127. return error;
  128. }
  129. plff->report = report;
  130. plff->strong = strong;
  131. plff->weak = weak;
  132. plff->maxval = maxval;
  133. *strong = 0x00;
  134. *weak = 0x00;
  135. hid_hw_request(hid, plff->report, HID_REQ_SET_REPORT);
  136. }
  137. hid_info(hid, "Force feedback for PantherLord/GreenAsia devices by Anssi Hannula <anssi.hannula@gmail.com>\n");
  138. return 0;
  139. }
  140. #else
  141. static inline int plff_init(struct hid_device *hid)
  142. {
  143. return 0;
  144. }
  145. #endif
  146. static int pl_probe(struct hid_device *hdev, const struct hid_device_id *id)
  147. {
  148. int ret;
  149. if (id->driver_data)
  150. hdev->quirks |= HID_QUIRK_MULTI_INPUT;
  151. ret = hid_parse(hdev);
  152. if (ret) {
  153. hid_err(hdev, "parse failed\n");
  154. goto err;
  155. }
  156. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  157. if (ret) {
  158. hid_err(hdev, "hw start failed\n");
  159. goto err;
  160. }
  161. plff_init(hdev);
  162. return 0;
  163. err:
  164. return ret;
  165. }
  166. static const struct hid_device_id pl_devices[] = {
  167. { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR),
  168. .driver_data = 1 }, /* Twin USB Joystick */
  169. { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PCS_ADAPTOR),
  170. .driver_data = 1 }, /* Twin USB Joystick */
  171. { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0003), },
  172. { HID_USB_DEVICE(USB_VENDOR_ID_JESS2, USB_DEVICE_ID_JESS2_COLOR_RUMBLE_PAD), },
  173. { }
  174. };
  175. MODULE_DEVICE_TABLE(hid, pl_devices);
  176. static struct hid_driver pl_driver = {
  177. .name = "pantherlord",
  178. .id_table = pl_devices,
  179. .probe = pl_probe,
  180. };
  181. module_hid_driver(pl_driver);
  182. MODULE_LICENSE("GPL");