hid-pl.c 6.0 KB

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