blob: fbe4e16ab0297f778402eca7d9e44d2621bd1154 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +02002/*
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"
Sergei Kolzunb55ebc22011-08-04 00:25:57 -070010 * - tested with an EXEQ EQ-PCU-02090 game controller.
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +020011 *
12 * Copyright (c) 2010 Sergei Kolzun <x0r@dv-life.ru>
13 */
14
15/*
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +020016 */
17
18#include <linux/input.h>
19#include <linux/slab.h>
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +020020#include <linux/hid.h>
Paul Gortmaker8f86a2c2011-07-03 13:39:48 -040021#include <linux/module.h>
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +020022
23#include "hid-ids.h"
Dmitry Torokhov0ae43812011-03-11 00:27:34 -080024
25#ifdef CONFIG_HID_ACRUX_FF
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +020026
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;
Sergei Kolzunb55ebc22011-08-04 00:25:57 -070035 struct hid_report *report = axff->report;
36 int field_count = 0;
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +020037 int left, right;
Sergei Kolzunb55ebc22011-08-04 00:25:57 -070038 int i, j;
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +020039
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
Sergei Kolzunb55ebc22011-08-04 00:25:57 -070048 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
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +020056 dbg_hid("running with 0x%02x 0x%02x", left, right);
Benjamin Tissoiresd88142722013-02-25 11:31:46 +010057 hid_hw_request(hid, axff->report, HID_REQ_SET_REPORT);
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +020058
59 return 0;
60}
61
62static int axff_init(struct hid_device *hid)
63{
64 struct axff_device *axff;
65 struct hid_report *report;
Alan Sternd9d4b1e2019-10-03 14:53:59 -040066 struct hid_input *hidinput;
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +020067 struct list_head *report_list =&hid->report_enum[HID_OUTPUT_REPORT].report_list;
Alan Sternd9d4b1e2019-10-03 14:53:59 -040068 struct input_dev *dev;
Sergei Kolzunb55ebc22011-08-04 00:25:57 -070069 int field_count = 0;
70 int i, j;
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +020071 int error;
72
Alan Sternd9d4b1e2019-10-03 14:53:59 -040073 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
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +020080 if (list_empty(report_list)) {
Joe Perches4291ee32010-12-09 19:29:03 -080081 hid_err(hid, "no output reports found\n");
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +020082 return -ENODEV;
83 }
84
85 report = list_first_entry(report_list, struct hid_report, list);
Sergei Kolzunb55ebc22011-08-04 00:25:57 -070086 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 }
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +020092
Tristan Ricee17f5d762013-11-12 19:06:23 +010093 if (field_count < 4 && hid->product != 0xf705) {
Sergei Kolzunb55ebc22011-08-04 00:25:57 -070094 hid_err(hid, "not enough fields in the report: %d\n",
95 field_count);
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +020096 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;
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100110 hid_hw_request(hid, axff->report, HID_REQ_SET_REPORT);
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +0200111
Sergei Kolzunb55ebc22011-08-04 00:25:57 -0700112 hid_info(hid, "Force Feedback for ACRUX game controllers by Sergei Kolzun <x0r@dv-life.ru>\n");
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +0200113
114 return 0;
115
116err_free_mem:
117 kfree(axff);
118 return error;
119}
Dmitry Torokhov0ae43812011-03-11 00:27:34 -0800120#else
121static inline int axff_init(struct hid_device *hid)
122{
123 return 0;
124}
125#endif
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +0200126
127static int ax_probe(struct hid_device *hdev, const struct hid_device_id *id)
128{
129 int error;
130
Joe Perches4291ee32010-12-09 19:29:03 -0800131 dev_dbg(&hdev->dev, "ACRUX HID hardware probe...\n");
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +0200132
133 error = hid_parse(hdev);
134 if (error) {
Joe Perches4291ee32010-12-09 19:29:03 -0800135 hid_err(hdev, "parse failed\n");
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +0200136 return error;
137 }
138
139 error = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
140 if (error) {
Joe Perches4291ee32010-12-09 19:29:03 -0800141 hid_err(hdev, "hw start failed\n");
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +0200142 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 */
Joe Perches4291ee32010-12-09 19:29:03 -0800151 hid_warn(hdev,
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +0200152 "Failed to enable force feedback support, error: %d\n",
153 error);
154 }
155
Dmitry Torokhov0ae43812011-03-11 00:27:34 -0800156 /*
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");
Axel Linb30d89d2011-07-14 13:07:51 +0800163 hid_hw_stop(hdev);
Dmitry Torokhov0ae43812011-03-11 00:27:34 -0800164 return error;
165 }
166
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +0200167 return 0;
168}
169
Dmitry Torokhov0ae43812011-03-11 00:27:34 -0800170static void ax_remove(struct hid_device *hdev)
171{
172 hid_hw_close(hdev);
173 hid_hw_stop(hdev);
174}
175
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +0200176static const struct hid_device_id ax_devices[] = {
177 { HID_USB_DEVICE(USB_VENDOR_ID_ACRUX, 0x0802), },
Tristan Ricee17f5d762013-11-12 19:06:23 +0100178 { HID_USB_DEVICE(USB_VENDOR_ID_ACRUX, 0xf705), },
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +0200179 { }
180};
181MODULE_DEVICE_TABLE(hid, ax_devices);
182
183static struct hid_driver ax_driver = {
Dmitry Torokhov0ae43812011-03-11 00:27:34 -0800184 .name = "acrux",
185 .id_table = ax_devices,
186 .probe = ax_probe,
187 .remove = ax_remove,
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +0200188};
H Hartley Sweetenf4254582012-12-17 15:28:26 -0700189module_hid_driver(ax_driver);
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +0200190
191MODULE_AUTHOR("Sergei Kolzun");
192MODULE_DESCRIPTION("Force feedback support for ACRUX game controllers");
193MODULE_LICENSE("GPL");