hid-axff.c 4.8 KB

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