hid-pl.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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/hid.h>
  42. #include "hid-ids.h"
  43. #ifdef CONFIG_PANTHERLORD_FF
  44. struct plff_device {
  45. struct hid_report *report;
  46. s32 maxval;
  47. s32 *strong;
  48. s32 *weak;
  49. };
  50. static int hid_plff_play(struct input_dev *dev, void *data,
  51. struct ff_effect *effect)
  52. {
  53. struct hid_device *hid = input_get_drvdata(dev);
  54. struct plff_device *plff = data;
  55. int left, right;
  56. left = effect->u.rumble.strong_magnitude;
  57. right = effect->u.rumble.weak_magnitude;
  58. debug("called with 0x%04x 0x%04x", left, right);
  59. left = left * plff->maxval / 0xffff;
  60. right = right * plff->maxval / 0xffff;
  61. *plff->strong = left;
  62. *plff->weak = right;
  63. debug("running with 0x%02x 0x%02x", left, right);
  64. hid_hw_request(hid, plff->report, HID_REQ_SET_REPORT);
  65. return 0;
  66. }
  67. static int plff_init(struct hid_device *hid)
  68. {
  69. struct plff_device *plff;
  70. struct hid_report *report;
  71. struct hid_input *hidinput;
  72. struct list_head *report_list =
  73. &hid->report_enum[HID_OUTPUT_REPORT].report_list;
  74. struct list_head *report_ptr = report_list;
  75. struct input_dev *dev;
  76. int error;
  77. s32 maxval;
  78. s32 *strong;
  79. s32 *weak;
  80. /* The device contains one output report per physical device, all
  81. containing 1 field, which contains 4 ff00.0002 usages and 4 16bit
  82. absolute values.
  83. The input reports also contain a field which contains
  84. 8 ff00.0001 usages and 8 boolean values. Their meaning is
  85. currently unknown.
  86. A version of the 0e8f:0003 exists that has all the values in
  87. separate fields and misses the extra input field, thus resembling
  88. Zeroplus (hid-zpff) devices.
  89. */
  90. if (list_empty(report_list)) {
  91. hid_err(hid, "no output reports found\n");
  92. return -ENODEV;
  93. }
  94. list_for_each_entry(hidinput, &hid->inputs, list) {
  95. report_ptr = report_ptr->next;
  96. if (report_ptr == report_list) {
  97. hid_err(hid, "required output report is missing\n");
  98. return -ENODEV;
  99. }
  100. report = list_entry(report_ptr, struct hid_report, list);
  101. if (report->maxfield < 1) {
  102. hid_err(hid, "no fields in the report\n");
  103. return -ENODEV;
  104. }
  105. maxval = 0x7f;
  106. if (report->field[0]->report_count >= 4) {
  107. report->field[0]->value[0] = 0x00;
  108. report->field[0]->value[1] = 0x00;
  109. strong = &report->field[0]->value[2];
  110. weak = &report->field[0]->value[3];
  111. debug("detected single-field device");
  112. } else if (report->field[0]->maxusage == 1 &&
  113. report->field[0]->usage[0].hid ==
  114. (HID_UP_LED | 0x43) &&
  115. report->maxfield >= 4 &&
  116. report->field[0]->report_count >= 1 &&
  117. report->field[1]->report_count >= 1 &&
  118. report->field[2]->report_count >= 1 &&
  119. report->field[3]->report_count >= 1) {
  120. report->field[0]->value[0] = 0x00;
  121. report->field[1]->value[0] = 0x00;
  122. strong = &report->field[2]->value[0];
  123. weak = &report->field[3]->value[0];
  124. if (hid->vendor == USB_VENDOR_ID_JESS2)
  125. maxval = 0xff;
  126. debug("detected 4-field device");
  127. } else {
  128. hid_err(hid, "not enough fields or values\n");
  129. return -ENODEV;
  130. }
  131. plff = kzalloc(sizeof(struct plff_device), GFP_KERNEL);
  132. if (!plff)
  133. return -ENOMEM;
  134. dev = hidinput->input;
  135. set_bit(FF_RUMBLE, dev->ffbit);
  136. error = input_ff_create_memless(dev, plff, hid_plff_play);
  137. if (error) {
  138. kfree(plff);
  139. return error;
  140. }
  141. plff->report = report;
  142. plff->strong = strong;
  143. plff->weak = weak;
  144. plff->maxval = maxval;
  145. *strong = 0x00;
  146. *weak = 0x00;
  147. hid_hw_request(hid, plff->report, HID_REQ_SET_REPORT);
  148. }
  149. hid_info(hid, "Force feedback for PantherLord/GreenAsia devices by Anssi Hannula <anssi.hannula@gmail.com>\n");
  150. return 0;
  151. }
  152. #else
  153. static inline int plff_init(struct hid_device *hid)
  154. {
  155. return 0;
  156. }
  157. #endif
  158. static int pl_probe(struct hid_device *hdev, const struct hid_device_id *id)
  159. {
  160. int ret;
  161. if (id->driver_data)
  162. hdev->quirks |= HID_QUIRK_MULTI_INPUT;
  163. ret = hid_parse(hdev);
  164. if (ret) {
  165. hid_err(hdev, "parse failed\n");
  166. goto err;
  167. }
  168. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  169. if (ret) {
  170. hid_err(hdev, "hw start failed\n");
  171. goto err;
  172. }
  173. plff_init(hdev);
  174. return 0;
  175. err:
  176. return ret;
  177. }
  178. static const struct hid_device_id pl_devices[] = {
  179. { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR),
  180. .driver_data = 1 }, /* Twin USB Joystick */
  181. { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PCS_ADAPTOR),
  182. .driver_data = 1 }, /* Twin USB Joystick */
  183. { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0003), },
  184. { HID_USB_DEVICE(USB_VENDOR_ID_JESS2, USB_DEVICE_ID_JESS2_COLOR_RUMBLE_PAD), },
  185. { }
  186. };
  187. MODULE_DEVICE_TABLE(hid, pl_devices);
  188. static struct hid_driver pl_driver = {
  189. .name = "pantherlord",
  190. .id_table = pl_devices,
  191. .probe = pl_probe,
  192. };
  193. module_hid_driver(pl_driver);
  194. MODULE_LICENSE("GPL");