hid-axff.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Force feedback support for ACRUX game controllers
  3. *
  4. * From what I have gathered, these devices are mass produced in China
  5. * by several vendors. They often share the same design as the original
  6. * Xbox 360 controller.
  7. *
  8. * 1a34:0802 "ACRUX USB GAMEPAD 8116"
  9. * - tested with an EXEQ EQ-PCU-02090 game controller.
  10. *
  11. * Copyright (c) 2010 Sergei Kolzun <x0r@dv-life.ru>
  12. */
  13. /*
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. */
  28. #include <linux/input.h>
  29. #include <linux/slab.h>
  30. #include <linux/usb.h>
  31. #include <linux/hid.h>
  32. #include <linux/module.h>
  33. #include "hid-ids.h"
  34. #ifdef CONFIG_HID_ACRUX_FF
  35. #include "usbhid/usbhid.h"
  36. struct axff_device {
  37. struct hid_report *report;
  38. };
  39. static int axff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
  40. {
  41. struct hid_device *hid = input_get_drvdata(dev);
  42. struct axff_device *axff = data;
  43. struct hid_report *report = axff->report;
  44. int field_count = 0;
  45. int left, right;
  46. int i, j;
  47. left = effect->u.rumble.strong_magnitude;
  48. right = effect->u.rumble.weak_magnitude;
  49. dbg_hid("called with 0x%04x 0x%04x", left, right);
  50. left = left * 0xff / 0xffff;
  51. right = right * 0xff / 0xffff;
  52. for (i = 0; i < report->maxfield; i++) {
  53. for (j = 0; j < report->field[i]->report_count; j++) {
  54. report->field[i]->value[j] =
  55. field_count % 2 ? right : left;
  56. field_count++;
  57. }
  58. }
  59. dbg_hid("running with 0x%02x 0x%02x", left, right);
  60. usbhid_submit_report(hid, axff->report, USB_DIR_OUT);
  61. return 0;
  62. }
  63. static int axff_init(struct hid_device *hid)
  64. {
  65. struct axff_device *axff;
  66. struct hid_report *report;
  67. struct hid_input *hidinput;
  68. struct list_head *report_list =&hid->report_enum[HID_OUTPUT_REPORT].report_list;
  69. struct input_dev *dev;
  70. int field_count = 0;
  71. int i, j;
  72. int error;
  73. if (list_empty(&hid->inputs)) {
  74. hid_err(hid, "no inputs found\n");
  75. return -ENODEV;
  76. }
  77. hidinput = list_first_entry(&hid->inputs, struct hid_input, list);
  78. dev = hidinput->input;
  79. if (list_empty(report_list)) {
  80. hid_err(hid, "no output reports found\n");
  81. return -ENODEV;
  82. }
  83. report = list_first_entry(report_list, struct hid_report, list);
  84. for (i = 0; i < report->maxfield; i++) {
  85. for (j = 0; j < report->field[i]->report_count; j++) {
  86. report->field[i]->value[j] = 0x00;
  87. field_count++;
  88. }
  89. }
  90. if (field_count < 4) {
  91. hid_err(hid, "not enough fields in the report: %d\n",
  92. field_count);
  93. return -ENODEV;
  94. }
  95. axff = kzalloc(sizeof(struct axff_device), GFP_KERNEL);
  96. if (!axff)
  97. return -ENOMEM;
  98. set_bit(FF_RUMBLE, dev->ffbit);
  99. error = input_ff_create_memless(dev, axff, axff_play);
  100. if (error)
  101. goto err_free_mem;
  102. axff->report = report;
  103. usbhid_submit_report(hid, axff->report, USB_DIR_OUT);
  104. hid_info(hid, "Force Feedback for ACRUX game controllers by Sergei Kolzun <x0r@dv-life.ru>\n");
  105. return 0;
  106. err_free_mem:
  107. kfree(axff);
  108. return error;
  109. }
  110. #else
  111. static inline int axff_init(struct hid_device *hid)
  112. {
  113. return 0;
  114. }
  115. #endif
  116. static int ax_probe(struct hid_device *hdev, const struct hid_device_id *id)
  117. {
  118. int error;
  119. dev_dbg(&hdev->dev, "ACRUX HID hardware probe...\n");
  120. error = hid_parse(hdev);
  121. if (error) {
  122. hid_err(hdev, "parse failed\n");
  123. return error;
  124. }
  125. error = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  126. if (error) {
  127. hid_err(hdev, "hw start failed\n");
  128. return error;
  129. }
  130. error = axff_init(hdev);
  131. if (error) {
  132. /*
  133. * Do not fail device initialization completely as device
  134. * may still be partially operable, just warn.
  135. */
  136. hid_warn(hdev,
  137. "Failed to enable force feedback support, error: %d\n",
  138. error);
  139. }
  140. /*
  141. * We need to start polling device right away, otherwise
  142. * it will go into a coma.
  143. */
  144. error = hid_hw_open(hdev);
  145. if (error) {
  146. dev_err(&hdev->dev, "hw open failed\n");
  147. hid_hw_stop(hdev);
  148. return error;
  149. }
  150. return 0;
  151. }
  152. static void ax_remove(struct hid_device *hdev)
  153. {
  154. hid_hw_close(hdev);
  155. hid_hw_stop(hdev);
  156. }
  157. static const struct hid_device_id ax_devices[] = {
  158. { HID_USB_DEVICE(USB_VENDOR_ID_ACRUX, 0x0802), },
  159. { }
  160. };
  161. MODULE_DEVICE_TABLE(hid, ax_devices);
  162. static struct hid_driver ax_driver = {
  163. .name = "acrux",
  164. .id_table = ax_devices,
  165. .probe = ax_probe,
  166. .remove = ax_remove,
  167. };
  168. static int __init ax_init(void)
  169. {
  170. return hid_register_driver(&ax_driver);
  171. }
  172. static void __exit ax_exit(void)
  173. {
  174. hid_unregister_driver(&ax_driver);
  175. }
  176. module_init(ax_init);
  177. module_exit(ax_exit);
  178. MODULE_AUTHOR("Sergei Kolzun");
  179. MODULE_DESCRIPTION("Force feedback support for ACRUX game controllers");
  180. MODULE_LICENSE("GPL");