hid-axff.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 "hid-ids.h"
  33. #ifdef CONFIG_HID_ACRUX_FF
  34. #include "usbhid/usbhid.h"
  35. struct axff_device {
  36. struct hid_report *report;
  37. };
  38. static int axff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
  39. {
  40. struct hid_device *hid = input_get_drvdata(dev);
  41. struct axff_device *axff = data;
  42. struct hid_report *report = axff->report;
  43. int field_count = 0;
  44. int left, right;
  45. int i, j;
  46. left = effect->u.rumble.strong_magnitude;
  47. right = effect->u.rumble.weak_magnitude;
  48. dbg_hid("called with 0x%04x 0x%04x", left, right);
  49. left = left * 0xff / 0xffff;
  50. right = right * 0xff / 0xffff;
  51. for (i = 0; i < report->maxfield; i++) {
  52. for (j = 0; j < report->field[i]->report_count; j++) {
  53. report->field[i]->value[j] =
  54. field_count % 2 ? right : left;
  55. field_count++;
  56. }
  57. }
  58. dbg_hid("running with 0x%02x 0x%02x", left, right);
  59. usbhid_submit_report(hid, axff->report, USB_DIR_OUT);
  60. return 0;
  61. }
  62. static int axff_init(struct hid_device *hid)
  63. {
  64. struct axff_device *axff;
  65. struct hid_report *report;
  66. struct hid_input *hidinput = list_first_entry(&hid->inputs, struct hid_input, list);
  67. struct list_head *report_list =&hid->report_enum[HID_OUTPUT_REPORT].report_list;
  68. struct input_dev *dev = hidinput->input;
  69. int field_count = 0;
  70. int i, j;
  71. int error;
  72. if (list_empty(report_list)) {
  73. hid_err(hid, "no output reports found\n");
  74. return -ENODEV;
  75. }
  76. report = list_first_entry(report_list, struct hid_report, list);
  77. for (i = 0; i < report->maxfield; i++) {
  78. for (j = 0; j < report->field[i]->report_count; j++) {
  79. report->field[i]->value[j] = 0x00;
  80. field_count++;
  81. }
  82. }
  83. if (field_count < 4) {
  84. hid_err(hid, "not enough fields in the report: %d\n",
  85. field_count);
  86. return -ENODEV;
  87. }
  88. axff = kzalloc(sizeof(struct axff_device), GFP_KERNEL);
  89. if (!axff)
  90. return -ENOMEM;
  91. set_bit(FF_RUMBLE, dev->ffbit);
  92. error = input_ff_create_memless(dev, axff, axff_play);
  93. if (error)
  94. goto err_free_mem;
  95. axff->report = report;
  96. usbhid_submit_report(hid, axff->report, USB_DIR_OUT);
  97. hid_info(hid, "Force Feedback for ACRUX game controllers by Sergei Kolzun <x0r@dv-life.ru>\n");
  98. return 0;
  99. err_free_mem:
  100. kfree(axff);
  101. return error;
  102. }
  103. #else
  104. static inline int axff_init(struct hid_device *hid)
  105. {
  106. return 0;
  107. }
  108. #endif
  109. static int ax_probe(struct hid_device *hdev, const struct hid_device_id *id)
  110. {
  111. int error;
  112. dev_dbg(&hdev->dev, "ACRUX HID hardware probe...\n");
  113. error = hid_parse(hdev);
  114. if (error) {
  115. hid_err(hdev, "parse failed\n");
  116. return error;
  117. }
  118. error = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  119. if (error) {
  120. hid_err(hdev, "hw start failed\n");
  121. return error;
  122. }
  123. error = axff_init(hdev);
  124. if (error) {
  125. /*
  126. * Do not fail device initialization completely as device
  127. * may still be partially operable, just warn.
  128. */
  129. hid_warn(hdev,
  130. "Failed to enable force feedback support, error: %d\n",
  131. error);
  132. }
  133. /*
  134. * We need to start polling device right away, otherwise
  135. * it will go into a coma.
  136. */
  137. error = hid_hw_open(hdev);
  138. if (error) {
  139. dev_err(&hdev->dev, "hw open failed\n");
  140. hid_hw_stop(hdev);
  141. return error;
  142. }
  143. return 0;
  144. }
  145. static void ax_remove(struct hid_device *hdev)
  146. {
  147. hid_hw_close(hdev);
  148. hid_hw_stop(hdev);
  149. }
  150. static const struct hid_device_id ax_devices[] = {
  151. { HID_USB_DEVICE(USB_VENDOR_ID_ACRUX, 0x0802), },
  152. { }
  153. };
  154. MODULE_DEVICE_TABLE(hid, ax_devices);
  155. static struct hid_driver ax_driver = {
  156. .name = "acrux",
  157. .id_table = ax_devices,
  158. .probe = ax_probe,
  159. .remove = ax_remove,
  160. };
  161. static int __init ax_init(void)
  162. {
  163. return hid_register_driver(&ax_driver);
  164. }
  165. static void __exit ax_exit(void)
  166. {
  167. hid_unregister_driver(&ax_driver);
  168. }
  169. module_init(ax_init);
  170. module_exit(ax_exit);
  171. MODULE_AUTHOR("Sergei Kolzun");
  172. MODULE_DESCRIPTION("Force feedback support for ACRUX game controllers");
  173. MODULE_LICENSE("GPL");