blob: 3bdb4500f95eea6bc7f8b901cc460d3d881ae2b5 [file] [log] [blame]
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +02001/*
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"
Sergei Kolzunb55ebc22011-08-04 00:25:57 -07009 * - tested with an EXEQ EQ-PCU-02090 game controller.
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +020010 *
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
35#include "hid-ids.h"
Dmitry Torokhov0ae43812011-03-11 00:27:34 -080036
37#ifdef CONFIG_HID_ACRUX_FF
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +020038#include "usbhid/usbhid.h"
39
40struct axff_device {
41 struct hid_report *report;
42};
43
44static int axff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
45{
46 struct hid_device *hid = input_get_drvdata(dev);
47 struct axff_device *axff = data;
Sergei Kolzunb55ebc22011-08-04 00:25:57 -070048 struct hid_report *report = axff->report;
49 int field_count = 0;
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +020050 int left, right;
Sergei Kolzunb55ebc22011-08-04 00:25:57 -070051 int i, j;
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +020052
53 left = effect->u.rumble.strong_magnitude;
54 right = effect->u.rumble.weak_magnitude;
55
56 dbg_hid("called with 0x%04x 0x%04x", left, right);
57
58 left = left * 0xff / 0xffff;
59 right = right * 0xff / 0xffff;
60
Sergei Kolzunb55ebc22011-08-04 00:25:57 -070061 for (i = 0; i < report->maxfield; i++) {
62 for (j = 0; j < report->field[i]->report_count; j++) {
63 report->field[i]->value[j] =
64 field_count % 2 ? right : left;
65 field_count++;
66 }
67 }
68
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +020069 dbg_hid("running with 0x%02x 0x%02x", left, right);
70 usbhid_submit_report(hid, axff->report, USB_DIR_OUT);
71
72 return 0;
73}
74
75static int axff_init(struct hid_device *hid)
76{
77 struct axff_device *axff;
78 struct hid_report *report;
79 struct hid_input *hidinput = list_first_entry(&hid->inputs, struct hid_input, list);
80 struct list_head *report_list =&hid->report_enum[HID_OUTPUT_REPORT].report_list;
81 struct input_dev *dev = hidinput->input;
Sergei Kolzunb55ebc22011-08-04 00:25:57 -070082 int field_count = 0;
83 int i, j;
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +020084 int error;
85
86 if (list_empty(report_list)) {
Joe Perches4291ee32010-12-09 19:29:03 -080087 hid_err(hid, "no output reports found\n");
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +020088 return -ENODEV;
89 }
90
91 report = list_first_entry(report_list, struct hid_report, list);
Sergei Kolzunb55ebc22011-08-04 00:25:57 -070092 for (i = 0; i < report->maxfield; i++) {
93 for (j = 0; j < report->field[i]->report_count; j++) {
94 report->field[i]->value[j] = 0x00;
95 field_count++;
96 }
97 }
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +020098
Sergei Kolzunb55ebc22011-08-04 00:25:57 -070099 if (field_count < 4) {
100 hid_err(hid, "not enough fields in the report: %d\n",
101 field_count);
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +0200102 return -ENODEV;
103 }
104
105 axff = kzalloc(sizeof(struct axff_device), GFP_KERNEL);
106 if (!axff)
107 return -ENOMEM;
108
109 set_bit(FF_RUMBLE, dev->ffbit);
110
111 error = input_ff_create_memless(dev, axff, axff_play);
112 if (error)
113 goto err_free_mem;
114
115 axff->report = report;
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +0200116 usbhid_submit_report(hid, axff->report, USB_DIR_OUT);
117
Sergei Kolzunb55ebc22011-08-04 00:25:57 -0700118 hid_info(hid, "Force Feedback for ACRUX game controllers by Sergei Kolzun <x0r@dv-life.ru>\n");
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +0200119
120 return 0;
121
122err_free_mem:
123 kfree(axff);
124 return error;
125}
Dmitry Torokhov0ae43812011-03-11 00:27:34 -0800126#else
127static inline int axff_init(struct hid_device *hid)
128{
129 return 0;
130}
131#endif
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +0200132
133static int ax_probe(struct hid_device *hdev, const struct hid_device_id *id)
134{
135 int error;
136
Joe Perches4291ee32010-12-09 19:29:03 -0800137 dev_dbg(&hdev->dev, "ACRUX HID hardware probe...\n");
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +0200138
139 error = hid_parse(hdev);
140 if (error) {
Joe Perches4291ee32010-12-09 19:29:03 -0800141 hid_err(hdev, "parse failed\n");
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +0200142 return error;
143 }
144
145 error = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
146 if (error) {
Joe Perches4291ee32010-12-09 19:29:03 -0800147 hid_err(hdev, "hw start failed\n");
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +0200148 return error;
149 }
150
151 error = axff_init(hdev);
152 if (error) {
153 /*
154 * Do not fail device initialization completely as device
155 * may still be partially operable, just warn.
156 */
Joe Perches4291ee32010-12-09 19:29:03 -0800157 hid_warn(hdev,
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +0200158 "Failed to enable force feedback support, error: %d\n",
159 error);
160 }
161
Dmitry Torokhov0ae43812011-03-11 00:27:34 -0800162 /*
163 * We need to start polling device right away, otherwise
164 * it will go into a coma.
165 */
166 error = hid_hw_open(hdev);
167 if (error) {
168 dev_err(&hdev->dev, "hw open failed\n");
Axel Linb30d89d2011-07-14 13:07:51 +0800169 hid_hw_stop(hdev);
Dmitry Torokhov0ae43812011-03-11 00:27:34 -0800170 return error;
171 }
172
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +0200173 return 0;
174}
175
Dmitry Torokhov0ae43812011-03-11 00:27:34 -0800176static void ax_remove(struct hid_device *hdev)
177{
178 hid_hw_close(hdev);
179 hid_hw_stop(hdev);
180}
181
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +0200182static const struct hid_device_id ax_devices[] = {
183 { HID_USB_DEVICE(USB_VENDOR_ID_ACRUX, 0x0802), },
184 { }
185};
186MODULE_DEVICE_TABLE(hid, ax_devices);
187
188static struct hid_driver ax_driver = {
Dmitry Torokhov0ae43812011-03-11 00:27:34 -0800189 .name = "acrux",
190 .id_table = ax_devices,
191 .probe = ax_probe,
192 .remove = ax_remove,
Sergei Kolzunc0dbcc32010-07-19 12:13:23 +0200193};
194
195static int __init ax_init(void)
196{
197 return hid_register_driver(&ax_driver);
198}
199
200static void __exit ax_exit(void)
201{
202 hid_unregister_driver(&ax_driver);
203}
204
205module_init(ax_init);
206module_exit(ax_exit);
207
208MODULE_AUTHOR("Sergei Kolzun");
209MODULE_DESCRIPTION("Force feedback support for ACRUX game controllers");
210MODULE_LICENSE("GPL");