blob: 947f19f8685f8668ea6e5568d3dad821156c3fa3 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Richard Walmsley3f866fb2009-03-04 22:12:04 +13002/*
3 * Force feedback support for DragonRise Inc. game controllers
4 *
5 * From what I have gathered, these devices are mass produced in China and are
6 * distributed under several vendors. They often share the same design as
7 * the original PlayStation DualShock controller.
8 *
9 * 0079:0006 "DragonRise Inc. Generic USB Joystick "
10 * - tested with a Tesun USB-703 game controller.
11 *
12 * Copyright (c) 2009 Richard Walmsley <richwalm@gmail.com>
13 */
14
15/*
Richard Walmsley3f866fb2009-03-04 22:12:04 +130016 */
17
18#include <linux/input.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Richard Walmsley3f866fb2009-03-04 22:12:04 +130020#include <linux/hid.h>
Paul Gortmaker8f86a2c2011-07-03 13:39:48 -040021#include <linux/module.h>
Richard Walmsley3f866fb2009-03-04 22:12:04 +130022
23#include "hid-ids.h"
Jiri Kosina0f6f4312009-05-15 15:46:44 +020024
25#ifdef CONFIG_DRAGONRISE_FF
Richard Walmsley3f866fb2009-03-04 22:12:04 +130026
27struct drff_device {
28 struct hid_report *report;
29};
30
31static int drff_play(struct input_dev *dev, void *data,
32 struct ff_effect *effect)
33{
34 struct hid_device *hid = input_get_drvdata(dev);
35 struct drff_device *drff = data;
36 int strong, weak;
37
38 strong = effect->u.rumble.strong_magnitude;
39 weak = effect->u.rumble.weak_magnitude;
40
41 dbg_hid("called with 0x%04x 0x%04x", strong, weak);
42
43 if (strong || weak) {
44 strong = strong * 0xff / 0xffff;
45 weak = weak * 0xff / 0xffff;
46
47 /* While reverse engineering this device, I found that when
48 this value is set, it causes the strong rumble to function
49 at a near maximum speed, so we'll bypass it. */
50 if (weak == 0x0a)
51 weak = 0x0b;
52
53 drff->report->field[0]->value[0] = 0x51;
54 drff->report->field[0]->value[1] = 0x00;
55 drff->report->field[0]->value[2] = weak;
56 drff->report->field[0]->value[4] = strong;
Benjamin Tissoiresd88142722013-02-25 11:31:46 +010057 hid_hw_request(hid, drff->report, HID_REQ_SET_REPORT);
Richard Walmsley3f866fb2009-03-04 22:12:04 +130058
59 drff->report->field[0]->value[0] = 0xfa;
60 drff->report->field[0]->value[1] = 0xfe;
61 } else {
62 drff->report->field[0]->value[0] = 0xf3;
63 drff->report->field[0]->value[1] = 0x00;
64 }
65
66 drff->report->field[0]->value[2] = 0x00;
67 drff->report->field[0]->value[4] = 0x00;
68 dbg_hid("running with 0x%02x 0x%02x", strong, weak);
Benjamin Tissoiresd88142722013-02-25 11:31:46 +010069 hid_hw_request(hid, drff->report, HID_REQ_SET_REPORT);
Richard Walmsley3f866fb2009-03-04 22:12:04 +130070
71 return 0;
72}
73
74static int drff_init(struct hid_device *hid)
75{
76 struct drff_device *drff;
77 struct hid_report *report;
Alan Sternd9d4b1e2019-10-03 14:53:59 -040078 struct hid_input *hidinput;
Richard Walmsley3f866fb2009-03-04 22:12:04 +130079 struct list_head *report_list =
80 &hid->report_enum[HID_OUTPUT_REPORT].report_list;
Alan Sternd9d4b1e2019-10-03 14:53:59 -040081 struct input_dev *dev;
Richard Walmsley3f866fb2009-03-04 22:12:04 +130082 int error;
83
Alan Sternd9d4b1e2019-10-03 14:53:59 -040084 if (list_empty(&hid->inputs)) {
85 hid_err(hid, "no inputs found\n");
86 return -ENODEV;
87 }
88 hidinput = list_first_entry(&hid->inputs, struct hid_input, list);
89 dev = hidinput->input;
90
Richard Walmsley3f866fb2009-03-04 22:12:04 +130091 if (list_empty(report_list)) {
Joe Perches4291ee32010-12-09 19:29:03 -080092 hid_err(hid, "no output reports found\n");
Richard Walmsley3f866fb2009-03-04 22:12:04 +130093 return -ENODEV;
94 }
95
96 report = list_first_entry(report_list, struct hid_report, list);
97 if (report->maxfield < 1) {
Joe Perches4291ee32010-12-09 19:29:03 -080098 hid_err(hid, "no fields in the report\n");
Richard Walmsley3f866fb2009-03-04 22:12:04 +130099 return -ENODEV;
100 }
101
102 if (report->field[0]->report_count < 7) {
Joe Perches4291ee32010-12-09 19:29:03 -0800103 hid_err(hid, "not enough values in the field\n");
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300104 return -ENODEV;
105 }
106
107 drff = kzalloc(sizeof(struct drff_device), GFP_KERNEL);
108 if (!drff)
109 return -ENOMEM;
110
111 set_bit(FF_RUMBLE, dev->ffbit);
112
113 error = input_ff_create_memless(dev, drff, drff_play);
114 if (error) {
115 kfree(drff);
116 return error;
117 }
118
119 drff->report = report;
120 drff->report->field[0]->value[0] = 0xf3;
121 drff->report->field[0]->value[1] = 0x00;
122 drff->report->field[0]->value[2] = 0x00;
123 drff->report->field[0]->value[3] = 0x00;
124 drff->report->field[0]->value[4] = 0x00;
125 drff->report->field[0]->value[5] = 0x00;
126 drff->report->field[0]->value[6] = 0x00;
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100127 hid_hw_request(hid, drff->report, HID_REQ_SET_REPORT);
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300128
Joe Perches4291ee32010-12-09 19:29:03 -0800129 hid_info(hid, "Force Feedback for DragonRise Inc. "
130 "game controllers by Richard Walmsley <richwalm@gmail.com>\n");
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300131
132 return 0;
133}
Jiri Kosina0f6f4312009-05-15 15:46:44 +0200134#else
135static inline int drff_init(struct hid_device *hid)
136{
137 return 0;
138}
139#endif
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300140
Nikolai Kondrashove05eefb2011-01-28 14:50:52 +0300141/*
142 * The original descriptor of joystick with PID 0x0011, represented by DVTech PC
143 * JS19. It seems both copied from another device and a result of confusion
144 * either about the specification or about the program used to create the
145 * descriptor. In any case, it's a wonder it works on Windows.
146 *
147 * Usage Page (Desktop), ; Generic desktop controls (01h)
Antonio Ospite313726c2016-01-28 18:23:41 +0100148 * Usage (Joystick), ; Joystick (04h, application collection)
Nikolai Kondrashove05eefb2011-01-28 14:50:52 +0300149 * Collection (Application),
150 * Collection (Logical),
151 * Report Size (8),
152 * Report Count (5),
153 * Logical Minimum (0),
154 * Logical Maximum (255),
155 * Physical Minimum (0),
156 * Physical Maximum (255),
157 * Usage (X), ; X (30h, dynamic value)
158 * Usage (X), ; X (30h, dynamic value)
159 * Usage (X), ; X (30h, dynamic value)
160 * Usage (X), ; X (30h, dynamic value)
161 * Usage (Y), ; Y (31h, dynamic value)
162 * Input (Variable),
163 * Report Size (4),
164 * Report Count (1),
165 * Logical Maximum (7),
166 * Physical Maximum (315),
167 * Unit (Degrees),
168 * Usage (00h),
169 * Input (Variable, Null State),
170 * Unit,
171 * Report Size (1),
172 * Report Count (10),
173 * Logical Maximum (1),
174 * Physical Maximum (1),
175 * Usage Page (Button), ; Button (09h)
176 * Usage Minimum (01h),
177 * Usage Maximum (0Ah),
178 * Input (Variable),
179 * Usage Page (FF00h), ; FF00h, vendor-defined
180 * Report Size (1),
181 * Report Count (10),
182 * Logical Maximum (1),
183 * Physical Maximum (1),
184 * Usage (01h),
185 * Input (Variable),
186 * End Collection,
187 * Collection (Logical),
188 * Report Size (8),
189 * Report Count (4),
190 * Physical Maximum (255),
191 * Logical Maximum (255),
192 * Usage (02h),
193 * Output (Variable),
194 * End Collection,
195 * End Collection
196 */
197
198/* Size of the original descriptor of the PID 0x0011 joystick */
199#define PID0011_RDESC_ORIG_SIZE 101
200
201/* Fixed report descriptor for PID 0x011 joystick */
202static __u8 pid0011_rdesc_fixed[] = {
203 0x05, 0x01, /* Usage Page (Desktop), */
Antonio Ospite313726c2016-01-28 18:23:41 +0100204 0x09, 0x04, /* Usage (Joystick), */
Nikolai Kondrashove05eefb2011-01-28 14:50:52 +0300205 0xA1, 0x01, /* Collection (Application), */
206 0xA1, 0x02, /* Collection (Logical), */
207 0x14, /* Logical Minimum (0), */
208 0x75, 0x08, /* Report Size (8), */
209 0x95, 0x03, /* Report Count (3), */
210 0x81, 0x01, /* Input (Constant), */
211 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
212 0x95, 0x02, /* Report Count (2), */
213 0x09, 0x30, /* Usage (X), */
214 0x09, 0x31, /* Usage (Y), */
215 0x81, 0x02, /* Input (Variable), */
216 0x75, 0x01, /* Report Size (1), */
217 0x95, 0x04, /* Report Count (4), */
218 0x81, 0x01, /* Input (Constant), */
219 0x25, 0x01, /* Logical Maximum (1), */
220 0x95, 0x0A, /* Report Count (10), */
221 0x05, 0x09, /* Usage Page (Button), */
222 0x19, 0x01, /* Usage Minimum (01h), */
223 0x29, 0x0A, /* Usage Maximum (0Ah), */
224 0x81, 0x02, /* Input (Variable), */
225 0x95, 0x0A, /* Report Count (10), */
226 0x81, 0x01, /* Input (Constant), */
227 0xC0, /* End Collection, */
228 0xC0 /* End Collection */
229};
230
231static __u8 *dr_report_fixup(struct hid_device *hdev, __u8 *rdesc,
232 unsigned int *rsize)
233{
234 switch (hdev->product) {
235 case 0x0011:
236 if (*rsize == PID0011_RDESC_ORIG_SIZE) {
237 rdesc = pid0011_rdesc_fixed;
238 *rsize = sizeof(pid0011_rdesc_fixed);
239 }
240 break;
241 }
242 return rdesc;
243}
244
Ioan-Adrian Ratiue1594402016-09-27 21:41:38 +0300245#define map_abs(c) hid_map_usage(hi, usage, bit, max, EV_ABS, (c))
246#define map_rel(c) hid_map_usage(hi, usage, bit, max, EV_REL, (c))
247
248static int dr_input_mapping(struct hid_device *hdev, struct hid_input *hi,
249 struct hid_field *field, struct hid_usage *usage,
250 unsigned long **bit, int *max)
251{
252 switch (usage->hid) {
253 /*
254 * revert to the old hid-input behavior where axes
255 * can be randomly assigned when hid->usage is reused.
256 */
257 case HID_GD_X: case HID_GD_Y: case HID_GD_Z:
258 case HID_GD_RX: case HID_GD_RY: case HID_GD_RZ:
259 if (field->flags & HID_MAIN_ITEM_RELATIVE)
260 map_rel(usage->hid & 0xf);
261 else
262 map_abs(usage->hid & 0xf);
263 return 1;
264 }
265
266 return 0;
267}
268
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300269static int dr_probe(struct hid_device *hdev, const struct hid_device_id *id)
270{
271 int ret;
272
273 dev_dbg(&hdev->dev, "DragonRise Inc. HID hardware probe...");
274
275 ret = hid_parse(hdev);
276 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800277 hid_err(hdev, "parse failed\n");
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300278 goto err;
279 }
280
281 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
282 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800283 hid_err(hdev, "hw start failed\n");
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300284 goto err;
285 }
286
Nikolai Kondrashove05eefb2011-01-28 14:50:52 +0300287 switch (hdev->product) {
288 case 0x0006:
289 ret = drff_init(hdev);
290 if (ret) {
291 dev_err(&hdev->dev, "force feedback init failed\n");
292 hid_hw_stop(hdev);
293 goto err;
294 }
295 break;
296 }
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300297
298 return 0;
299err:
300 return ret;
301}
302
303static const struct hid_device_id dr_devices[] = {
304 { HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0006), },
Nikolai Kondrashove05eefb2011-01-28 14:50:52 +0300305 { HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0011), },
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300306 { }
307};
308MODULE_DEVICE_TABLE(hid, dr_devices);
309
310static struct hid_driver dr_driver = {
311 .name = "dragonrise",
312 .id_table = dr_devices,
Nikolai Kondrashove05eefb2011-01-28 14:50:52 +0300313 .report_fixup = dr_report_fixup,
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300314 .probe = dr_probe,
Ioan-Adrian Ratiue1594402016-09-27 21:41:38 +0300315 .input_mapping = dr_input_mapping,
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300316};
H Hartley Sweetenf4254582012-12-17 15:28:26 -0700317module_hid_driver(dr_driver);
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300318
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300319MODULE_LICENSE("GPL");