hid-pl.c 5.9 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/usb.h>
  38. #include <linux/hid.h>
  39. #include "hid-ids.h"
  40. #ifdef CONFIG_PANTHERLORD_FF
  41. #include "usbhid/usbhid.h"
  42. struct plff_device {
  43. struct hid_report *report;
  44. s32 *strong;
  45. s32 *weak;
  46. };
  47. static int hid_plff_play(struct input_dev *dev, void *data,
  48. struct ff_effect *effect)
  49. {
  50. struct hid_device *hid = input_get_drvdata(dev);
  51. struct plff_device *plff = data;
  52. int left, right;
  53. left = effect->u.rumble.strong_magnitude;
  54. right = effect->u.rumble.weak_magnitude;
  55. debug("called with 0x%04x 0x%04x", left, right);
  56. left = left * 0x7f / 0xffff;
  57. right = right * 0x7f / 0xffff;
  58. *plff->strong = left;
  59. *plff->weak = right;
  60. debug("running with 0x%02x 0x%02x", left, right);
  61. usbhid_submit_report(hid, plff->report, USB_DIR_OUT);
  62. return 0;
  63. }
  64. static int plff_init(struct hid_device *hid)
  65. {
  66. struct plff_device *plff;
  67. struct hid_report *report;
  68. struct hid_input *hidinput;
  69. struct list_head *report_list =
  70. &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  71. struct list_head *report_ptr = report_list;
  72. struct input_dev *dev;
  73. int error;
  74. s32 *strong;
  75. s32 *weak;
  76. /* The device contains one output report per physical device, all
  77. containing 1 field, which contains 4 ff00.0002 usages and 4 16bit
  78. absolute values.
  79. The input reports also contain a field which contains
  80. 8 ff00.0001 usages and 8 boolean values. Their meaning is
  81. currently unknown.
  82. A version of the 0e8f:0003 exists that has all the values in
  83. separate fields and misses the extra input field, thus resembling
  84. Zeroplus (hid-zpff) devices.
  85. */
  86. if (list_empty(report_list)) {
  87. dev_err(&hid->dev, "no output reports found\n");
  88. return -ENODEV;
  89. }
  90. list_for_each_entry(hidinput, &hid->inputs, list) {
  91. report_ptr = report_ptr->next;
  92. if (report_ptr == report_list) {
  93. dev_err(&hid->dev, "required output report is "
  94. "missing\n");
  95. return -ENODEV;
  96. }
  97. report = list_entry(report_ptr, struct hid_report, list);
  98. if (report->maxfield < 1) {
  99. dev_err(&hid->dev, "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. dev_err(&hid->dev, "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. dev_info(&hid->dev, "Force feedback for PantherLord/GreenAsia "
  137. "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. dev_err(&hdev->dev, "parse failed\n");
  154. goto err;
  155. }
  156. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  157. if (ret) {
  158. dev_err(&hdev->dev, "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. { }
  173. };
  174. MODULE_DEVICE_TABLE(hid, pl_devices);
  175. static struct hid_driver pl_driver = {
  176. .name = "pantherlord",
  177. .id_table = pl_devices,
  178. .probe = pl_probe,
  179. };
  180. static int pl_init(void)
  181. {
  182. return hid_register_driver(&pl_driver);
  183. }
  184. static void pl_exit(void)
  185. {
  186. hid_unregister_driver(&pl_driver);
  187. }
  188. module_init(pl_init);
  189. module_exit(pl_exit);
  190. MODULE_LICENSE("GPL");