hid-axff.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Force feedback support for ACRUX game controllers
  4. *
  5. * From what I have gathered, these devices are mass produced in China
  6. * by several vendors. They often share the same design as the original
  7. * Xbox 360 controller.
  8. *
  9. * 1a34:0802 "ACRUX USB GAMEPAD 8116"
  10. * - tested with an EXEQ EQ-PCU-02090 game controller.
  11. *
  12. * Copyright (c) 2010 Sergei Kolzun <x0r@dv-life.ru>
  13. */
  14. /*
  15. */
  16. #include <linux/input.h>
  17. #include <linux/slab.h>
  18. #include <linux/hid.h>
  19. #include <linux/module.h>
  20. #include "hid-ids.h"
  21. #ifdef CONFIG_HID_ACRUX_FF
  22. struct axff_device {
  23. struct hid_report *report;
  24. };
  25. static int axff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
  26. {
  27. struct hid_device *hid = input_get_drvdata(dev);
  28. struct axff_device *axff = data;
  29. struct hid_report *report = axff->report;
  30. int field_count = 0;
  31. int left, right;
  32. int i, j;
  33. left = effect->u.rumble.strong_magnitude;
  34. right = effect->u.rumble.weak_magnitude;
  35. dbg_hid("called with 0x%04x 0x%04x", left, right);
  36. left = left * 0xff / 0xffff;
  37. right = right * 0xff / 0xffff;
  38. for (i = 0; i < report->maxfield; i++) {
  39. for (j = 0; j < report->field[i]->report_count; j++) {
  40. report->field[i]->value[j] =
  41. field_count % 2 ? right : left;
  42. field_count++;
  43. }
  44. }
  45. dbg_hid("running with 0x%02x 0x%02x", left, right);
  46. hid_hw_request(hid, axff->report, HID_REQ_SET_REPORT);
  47. return 0;
  48. }
  49. static int axff_init(struct hid_device *hid)
  50. {
  51. struct axff_device *axff;
  52. struct hid_report *report;
  53. struct hid_input *hidinput;
  54. struct list_head *report_list =&hid->report_enum[HID_OUTPUT_REPORT].report_list;
  55. struct input_dev *dev;
  56. int field_count = 0;
  57. int i, j;
  58. int error;
  59. if (list_empty(&hid->inputs)) {
  60. hid_err(hid, "no inputs found\n");
  61. return -ENODEV;
  62. }
  63. hidinput = list_first_entry(&hid->inputs, struct hid_input, list);
  64. dev = hidinput->input;
  65. if (list_empty(report_list)) {
  66. hid_err(hid, "no output reports found\n");
  67. return -ENODEV;
  68. }
  69. report = list_first_entry(report_list, struct hid_report, list);
  70. for (i = 0; i < report->maxfield; i++) {
  71. for (j = 0; j < report->field[i]->report_count; j++) {
  72. report->field[i]->value[j] = 0x00;
  73. field_count++;
  74. }
  75. }
  76. if (field_count < 4 && hid->product != 0xf705) {
  77. hid_err(hid, "not enough fields in the report: %d\n",
  78. field_count);
  79. return -ENODEV;
  80. }
  81. axff = kzalloc(sizeof(struct axff_device), GFP_KERNEL);
  82. if (!axff)
  83. return -ENOMEM;
  84. set_bit(FF_RUMBLE, dev->ffbit);
  85. error = input_ff_create_memless(dev, axff, axff_play);
  86. if (error)
  87. goto err_free_mem;
  88. axff->report = report;
  89. hid_hw_request(hid, axff->report, HID_REQ_SET_REPORT);
  90. hid_info(hid, "Force Feedback for ACRUX game controllers by Sergei Kolzun <x0r@dv-life.ru>\n");
  91. return 0;
  92. err_free_mem:
  93. kfree(axff);
  94. return error;
  95. }
  96. #else
  97. static inline int axff_init(struct hid_device *hid)
  98. {
  99. return 0;
  100. }
  101. #endif
  102. static int ax_probe(struct hid_device *hdev, const struct hid_device_id *id)
  103. {
  104. int error;
  105. dev_dbg(&hdev->dev, "ACRUX HID hardware probe...\n");
  106. error = hid_parse(hdev);
  107. if (error) {
  108. hid_err(hdev, "parse failed\n");
  109. return error;
  110. }
  111. error = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  112. if (error) {
  113. hid_err(hdev, "hw start failed\n");
  114. return error;
  115. }
  116. error = axff_init(hdev);
  117. if (error) {
  118. /*
  119. * Do not fail device initialization completely as device
  120. * may still be partially operable, just warn.
  121. */
  122. hid_warn(hdev,
  123. "Failed to enable force feedback support, error: %d\n",
  124. error);
  125. }
  126. /*
  127. * We need to start polling device right away, otherwise
  128. * it will go into a coma.
  129. */
  130. error = hid_hw_open(hdev);
  131. if (error) {
  132. dev_err(&hdev->dev, "hw open failed\n");
  133. hid_hw_stop(hdev);
  134. return error;
  135. }
  136. return 0;
  137. }
  138. static void ax_remove(struct hid_device *hdev)
  139. {
  140. hid_hw_close(hdev);
  141. hid_hw_stop(hdev);
  142. }
  143. static const struct hid_device_id ax_devices[] = {
  144. { HID_USB_DEVICE(USB_VENDOR_ID_ACRUX, 0x0802), },
  145. { HID_USB_DEVICE(USB_VENDOR_ID_ACRUX, 0xf705), },
  146. { }
  147. };
  148. MODULE_DEVICE_TABLE(hid, ax_devices);
  149. static struct hid_driver ax_driver = {
  150. .name = "acrux",
  151. .id_table = ax_devices,
  152. .probe = ax_probe,
  153. .remove = ax_remove,
  154. };
  155. module_hid_driver(ax_driver);
  156. MODULE_AUTHOR("Sergei Kolzun");
  157. MODULE_DESCRIPTION("Force feedback support for ACRUX game controllers");
  158. MODULE_LICENSE("GPL");