hid-axff.c 4.9 KB

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