hid-pl.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Force feedback support for PantherLord/GreenAsia based devices
  3. *
  4. * The devices are distributed under various names and the same USB device ID
  5. * can be used in both adapters and actual game controllers.
  6. *
  7. * 0810:0001 "Twin USB Joystick"
  8. * - tested with PantherLord USB/PS2 2in1 Adapter
  9. * - contains two reports, one for each port (HID_QUIRK_MULTI_INPUT)
  10. *
  11. * 0e8f:0003 "GreenAsia Inc. USB Joystick "
  12. * - tested with König Gaming gamepad
  13. *
  14. * 0e8f:0003 "GASIA USB Gamepad"
  15. * - another version of the König gamepad
  16. *
  17. * Copyright (c) 2007, 2009 Anssi Hannula <anssi.hannula@gmail.com>
  18. */
  19. /*
  20. * This program is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation; either version 2 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with this program; if not, write to the Free Software
  32. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  33. */
  34. /* #define DEBUG */
  35. #define debug(format, arg...) pr_debug("hid-plff: " format "\n" , ## arg)
  36. #include <linux/input.h>
  37. #include <linux/slab.h>
  38. #include <linux/usb.h>
  39. #include <linux/hid.h>
  40. #include "hid-ids.h"
  41. #ifdef CONFIG_PANTHERLORD_FF
  42. #include "usbhid/usbhid.h"
  43. struct plff_device {
  44. struct hid_report *report;
  45. s32 *strong;
  46. s32 *weak;
  47. };
  48. static int hid_plff_play(struct input_dev *dev, void *data,
  49. struct ff_effect *effect)
  50. {
  51. struct hid_device *hid = input_get_drvdata(dev);
  52. struct plff_device *plff = data;
  53. int left, right;
  54. left = effect->u.rumble.strong_magnitude;
  55. right = effect->u.rumble.weak_magnitude;
  56. debug("called with 0x%04x 0x%04x", left, right);
  57. left = left * 0x7f / 0xffff;
  58. right = right * 0x7f / 0xffff;
  59. *plff->strong = left;
  60. *plff->weak = right;
  61. debug("running with 0x%02x 0x%02x", left, right);
  62. usbhid_submit_report(hid, plff->report, USB_DIR_OUT);
  63. return 0;
  64. }
  65. static int plff_init(struct hid_device *hid)
  66. {
  67. struct plff_device *plff;
  68. struct hid_report *report;
  69. struct hid_input *hidinput;
  70. struct list_head *report_list =
  71. &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  72. struct list_head *report_ptr = report_list;
  73. struct input_dev *dev;
  74. int error;
  75. s32 *strong;
  76. s32 *weak;
  77. /* The device contains one output report per physical device, all
  78. containing 1 field, which contains 4 ff00.0002 usages and 4 16bit
  79. absolute values.
  80. The input reports also contain a field which contains
  81. 8 ff00.0001 usages and 8 boolean values. Their meaning is
  82. currently unknown.
  83. A version of the 0e8f:0003 exists that has all the values in
  84. separate fields and misses the extra input field, thus resembling
  85. Zeroplus (hid-zpff) devices.
  86. */
  87. if (list_empty(report_list)) {
  88. dev_err(&hid->dev, "no output reports found\n");
  89. return -ENODEV;
  90. }
  91. list_for_each_entry(hidinput, &hid->inputs, list) {
  92. report_ptr = report_ptr->next;
  93. if (report_ptr == report_list) {
  94. dev_err(&hid->dev, "required output report is "
  95. "missing\n");
  96. return -ENODEV;
  97. }
  98. report = list_entry(report_ptr, struct hid_report, list);
  99. if (report->maxfield < 1) {
  100. dev_err(&hid->dev, "no fields in the report\n");
  101. return -ENODEV;
  102. }
  103. if (report->field[0]->report_count >= 4) {
  104. report->field[0]->value[0] = 0x00;
  105. report->field[0]->value[1] = 0x00;
  106. strong = &report->field[0]->value[2];
  107. weak = &report->field[0]->value[3];
  108. debug("detected single-field device");
  109. } else if (report->maxfield >= 4 && report->field[0]->maxusage == 1 &&
  110. report->field[0]->usage[0].hid == (HID_UP_LED | 0x43)) {
  111. report->field[0]->value[0] = 0x00;
  112. report->field[1]->value[0] = 0x00;
  113. strong = &report->field[2]->value[0];
  114. weak = &report->field[3]->value[0];
  115. debug("detected 4-field device");
  116. } else {
  117. dev_err(&hid->dev, "not enough fields or values\n");
  118. return -ENODEV;
  119. }
  120. plff = kzalloc(sizeof(struct plff_device), GFP_KERNEL);
  121. if (!plff)
  122. return -ENOMEM;
  123. dev = hidinput->input;
  124. set_bit(FF_RUMBLE, dev->ffbit);
  125. error = input_ff_create_memless(dev, plff, hid_plff_play);
  126. if (error) {
  127. kfree(plff);
  128. return error;
  129. }
  130. plff->report = report;
  131. plff->strong = strong;
  132. plff->weak = weak;
  133. *strong = 0x00;
  134. *weak = 0x00;
  135. usbhid_submit_report(hid, plff->report, USB_DIR_OUT);
  136. }
  137. dev_info(&hid->dev, "Force feedback for PantherLord/GreenAsia "
  138. "devices by Anssi Hannula <anssi.hannula@gmail.com>\n");
  139. return 0;
  140. }
  141. #else
  142. static inline int plff_init(struct hid_device *hid)
  143. {
  144. return 0;
  145. }
  146. #endif
  147. static int pl_probe(struct hid_device *hdev, const struct hid_device_id *id)
  148. {
  149. int ret;
  150. if (id->driver_data)
  151. hdev->quirks |= HID_QUIRK_MULTI_INPUT;
  152. ret = hid_parse(hdev);
  153. if (ret) {
  154. dev_err(&hdev->dev, "parse failed\n");
  155. goto err;
  156. }
  157. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  158. if (ret) {
  159. dev_err(&hdev->dev, "hw start failed\n");
  160. goto err;
  161. }
  162. plff_init(hdev);
  163. return 0;
  164. err:
  165. return ret;
  166. }
  167. static const struct hid_device_id pl_devices[] = {
  168. { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR),
  169. .driver_data = 1 }, /* Twin USB Joystick */
  170. { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PCS_ADAPTOR),
  171. .driver_data = 1 }, /* Twin USB Joystick */
  172. { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0003), },
  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. static int __init pl_init(void)
  182. {
  183. return hid_register_driver(&pl_driver);
  184. }
  185. static void __exit pl_exit(void)
  186. {
  187. hid_unregister_driver(&pl_driver);
  188. }
  189. module_init(pl_init);
  190. module_exit(pl_exit);
  191. MODULE_LICENSE("GPL");