hid-pl.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. hid_err(hid, "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. hid_err(hid, "required output report is missing\n");
  95. return -ENODEV;
  96. }
  97. report = list_entry(report_ptr, struct hid_report, list);
  98. if (report->maxfield < 1) {
  99. hid_err(hid, "no fields in the report\n");
  100. return -ENODEV;
  101. }
  102. if (report->field[0]->report_count >= 4) {
  103. report->field[0]->value[0] = 0x00;
  104. report->field[0]->value[1] = 0x00;
  105. strong = &report->field[0]->value[2];
  106. weak = &report->field[0]->value[3];
  107. debug("detected single-field device");
  108. } else if (report->maxfield >= 4 && report->field[0]->maxusage == 1 &&
  109. report->field[0]->usage[0].hid == (HID_UP_LED | 0x43)) {
  110. report->field[0]->value[0] = 0x00;
  111. report->field[1]->value[0] = 0x00;
  112. strong = &report->field[2]->value[0];
  113. weak = &report->field[3]->value[0];
  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. *strong = 0x00;
  133. *weak = 0x00;
  134. usbhid_submit_report(hid, plff->report, USB_DIR_OUT);
  135. }
  136. hid_info(hid, "Force feedback for PantherLord/GreenAsia devices by Anssi Hannula <anssi.hannula@gmail.com>\n");
  137. return 0;
  138. }
  139. #else
  140. static inline int plff_init(struct hid_device *hid)
  141. {
  142. return 0;
  143. }
  144. #endif
  145. static int pl_probe(struct hid_device *hdev, const struct hid_device_id *id)
  146. {
  147. int ret;
  148. if (id->driver_data)
  149. hdev->quirks |= HID_QUIRK_MULTI_INPUT;
  150. ret = hid_parse(hdev);
  151. if (ret) {
  152. hid_err(hdev, "parse failed\n");
  153. goto err;
  154. }
  155. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  156. if (ret) {
  157. hid_err(hdev, "hw start failed\n");
  158. goto err;
  159. }
  160. plff_init(hdev);
  161. return 0;
  162. err:
  163. return ret;
  164. }
  165. static const struct hid_device_id pl_devices[] = {
  166. { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR),
  167. .driver_data = 1 }, /* Twin USB Joystick */
  168. { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PCS_ADAPTOR),
  169. .driver_data = 1 }, /* Twin USB Joystick */
  170. { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0003), },
  171. { }
  172. };
  173. MODULE_DEVICE_TABLE(hid, pl_devices);
  174. static struct hid_driver pl_driver = {
  175. .name = "pantherlord",
  176. .id_table = pl_devices,
  177. .probe = pl_probe,
  178. };
  179. static int __init pl_init(void)
  180. {
  181. return hid_register_driver(&pl_driver);
  182. }
  183. static void __exit pl_exit(void)
  184. {
  185. hid_unregister_driver(&pl_driver);
  186. }
  187. module_init(pl_init);
  188. module_exit(pl_exit);
  189. MODULE_LICENSE("GPL");