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