Merge tag '9p-for-5.3' of git://github.com/martinetd/linux
[linux-2.6-block.git] / drivers / hid / hid-dr.c
CommitLineData
1a59d1b8 1// SPDX-License-Identifier: GPL-2.0-or-later
3f866fbd
RW
2/*
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/*
3f866fbd
RW
16 */
17
18#include <linux/input.h>
5a0e3ad6 19#include <linux/slab.h>
3f866fbd 20#include <linux/hid.h>
8f86a2c3 21#include <linux/module.h>
3f866fbd
RW
22
23#include "hid-ids.h"
0f6f4319
JK
24
25#ifdef CONFIG_DRAGONRISE_FF
3f866fbd
RW
26
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;
d8814272 57 hid_hw_request(hid, drff->report, HID_REQ_SET_REPORT);
3f866fbd
RW
58
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);
d8814272 69 hid_hw_request(hid, drff->report, HID_REQ_SET_REPORT);
3f866fbd
RW
70
71 return 0;
72}
73
74static int drff_init(struct hid_device *hid)
75{
76 struct drff_device *drff;
77 struct hid_report *report;
78 struct hid_input *hidinput = list_first_entry(&hid->inputs,
79 struct hid_input, list);
80 struct list_head *report_list =
81 &hid->report_enum[HID_OUTPUT_REPORT].report_list;
82 struct input_dev *dev = hidinput->input;
83 int error;
84
85 if (list_empty(report_list)) {
4291ee30 86 hid_err(hid, "no output reports found\n");
3f866fbd
RW
87 return -ENODEV;
88 }
89
90 report = list_first_entry(report_list, struct hid_report, list);
91 if (report->maxfield < 1) {
4291ee30 92 hid_err(hid, "no fields in the report\n");
3f866fbd
RW
93 return -ENODEV;
94 }
95
96 if (report->field[0]->report_count < 7) {
4291ee30 97 hid_err(hid, "not enough values in the field\n");
3f866fbd
RW
98 return -ENODEV;
99 }
100
101 drff = kzalloc(sizeof(struct drff_device), GFP_KERNEL);
102 if (!drff)
103 return -ENOMEM;
104
105 set_bit(FF_RUMBLE, dev->ffbit);
106
107 error = input_ff_create_memless(dev, drff, drff_play);
108 if (error) {
109 kfree(drff);
110 return error;
111 }
112
113 drff->report = report;
114 drff->report->field[0]->value[0] = 0xf3;
115 drff->report->field[0]->value[1] = 0x00;
116 drff->report->field[0]->value[2] = 0x00;
117 drff->report->field[0]->value[3] = 0x00;
118 drff->report->field[0]->value[4] = 0x00;
119 drff->report->field[0]->value[5] = 0x00;
120 drff->report->field[0]->value[6] = 0x00;
d8814272 121 hid_hw_request(hid, drff->report, HID_REQ_SET_REPORT);
3f866fbd 122
4291ee30
JP
123 hid_info(hid, "Force Feedback for DragonRise Inc. "
124 "game controllers by Richard Walmsley <richwalm@gmail.com>\n");
3f866fbd
RW
125
126 return 0;
127}
0f6f4319
JK
128#else
129static inline int drff_init(struct hid_device *hid)
130{
131 return 0;
132}
133#endif
3f866fbd 134
e05eefb9
NK
135/*
136 * The original descriptor of joystick with PID 0x0011, represented by DVTech PC
137 * JS19. It seems both copied from another device and a result of confusion
138 * either about the specification or about the program used to create the
139 * descriptor. In any case, it's a wonder it works on Windows.
140 *
141 * Usage Page (Desktop), ; Generic desktop controls (01h)
313726ca 142 * Usage (Joystick), ; Joystick (04h, application collection)
e05eefb9
NK
143 * Collection (Application),
144 * Collection (Logical),
145 * Report Size (8),
146 * Report Count (5),
147 * Logical Minimum (0),
148 * Logical Maximum (255),
149 * Physical Minimum (0),
150 * Physical Maximum (255),
151 * Usage (X), ; X (30h, dynamic value)
152 * Usage (X), ; X (30h, dynamic value)
153 * Usage (X), ; X (30h, dynamic value)
154 * Usage (X), ; X (30h, dynamic value)
155 * Usage (Y), ; Y (31h, dynamic value)
156 * Input (Variable),
157 * Report Size (4),
158 * Report Count (1),
159 * Logical Maximum (7),
160 * Physical Maximum (315),
161 * Unit (Degrees),
162 * Usage (00h),
163 * Input (Variable, Null State),
164 * Unit,
165 * Report Size (1),
166 * Report Count (10),
167 * Logical Maximum (1),
168 * Physical Maximum (1),
169 * Usage Page (Button), ; Button (09h)
170 * Usage Minimum (01h),
171 * Usage Maximum (0Ah),
172 * Input (Variable),
173 * Usage Page (FF00h), ; FF00h, vendor-defined
174 * Report Size (1),
175 * Report Count (10),
176 * Logical Maximum (1),
177 * Physical Maximum (1),
178 * Usage (01h),
179 * Input (Variable),
180 * End Collection,
181 * Collection (Logical),
182 * Report Size (8),
183 * Report Count (4),
184 * Physical Maximum (255),
185 * Logical Maximum (255),
186 * Usage (02h),
187 * Output (Variable),
188 * End Collection,
189 * End Collection
190 */
191
192/* Size of the original descriptor of the PID 0x0011 joystick */
193#define PID0011_RDESC_ORIG_SIZE 101
194
195/* Fixed report descriptor for PID 0x011 joystick */
196static __u8 pid0011_rdesc_fixed[] = {
197 0x05, 0x01, /* Usage Page (Desktop), */
313726ca 198 0x09, 0x04, /* Usage (Joystick), */
e05eefb9
NK
199 0xA1, 0x01, /* Collection (Application), */
200 0xA1, 0x02, /* Collection (Logical), */
201 0x14, /* Logical Minimum (0), */
202 0x75, 0x08, /* Report Size (8), */
203 0x95, 0x03, /* Report Count (3), */
204 0x81, 0x01, /* Input (Constant), */
205 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
206 0x95, 0x02, /* Report Count (2), */
207 0x09, 0x30, /* Usage (X), */
208 0x09, 0x31, /* Usage (Y), */
209 0x81, 0x02, /* Input (Variable), */
210 0x75, 0x01, /* Report Size (1), */
211 0x95, 0x04, /* Report Count (4), */
212 0x81, 0x01, /* Input (Constant), */
213 0x25, 0x01, /* Logical Maximum (1), */
214 0x95, 0x0A, /* Report Count (10), */
215 0x05, 0x09, /* Usage Page (Button), */
216 0x19, 0x01, /* Usage Minimum (01h), */
217 0x29, 0x0A, /* Usage Maximum (0Ah), */
218 0x81, 0x02, /* Input (Variable), */
219 0x95, 0x0A, /* Report Count (10), */
220 0x81, 0x01, /* Input (Constant), */
221 0xC0, /* End Collection, */
222 0xC0 /* End Collection */
223};
224
225static __u8 *dr_report_fixup(struct hid_device *hdev, __u8 *rdesc,
226 unsigned int *rsize)
227{
228 switch (hdev->product) {
229 case 0x0011:
230 if (*rsize == PID0011_RDESC_ORIG_SIZE) {
231 rdesc = pid0011_rdesc_fixed;
232 *rsize = sizeof(pid0011_rdesc_fixed);
233 }
234 break;
235 }
236 return rdesc;
237}
238
e1594409
IAR
239#define map_abs(c) hid_map_usage(hi, usage, bit, max, EV_ABS, (c))
240#define map_rel(c) hid_map_usage(hi, usage, bit, max, EV_REL, (c))
241
242static int dr_input_mapping(struct hid_device *hdev, struct hid_input *hi,
243 struct hid_field *field, struct hid_usage *usage,
244 unsigned long **bit, int *max)
245{
246 switch (usage->hid) {
247 /*
248 * revert to the old hid-input behavior where axes
249 * can be randomly assigned when hid->usage is reused.
250 */
251 case HID_GD_X: case HID_GD_Y: case HID_GD_Z:
252 case HID_GD_RX: case HID_GD_RY: case HID_GD_RZ:
253 if (field->flags & HID_MAIN_ITEM_RELATIVE)
254 map_rel(usage->hid & 0xf);
255 else
256 map_abs(usage->hid & 0xf);
257 return 1;
258 }
259
260 return 0;
261}
262
3f866fbd
RW
263static int dr_probe(struct hid_device *hdev, const struct hid_device_id *id)
264{
265 int ret;
266
267 dev_dbg(&hdev->dev, "DragonRise Inc. HID hardware probe...");
268
269 ret = hid_parse(hdev);
270 if (ret) {
4291ee30 271 hid_err(hdev, "parse failed\n");
3f866fbd
RW
272 goto err;
273 }
274
275 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
276 if (ret) {
4291ee30 277 hid_err(hdev, "hw start failed\n");
3f866fbd
RW
278 goto err;
279 }
280
e05eefb9
NK
281 switch (hdev->product) {
282 case 0x0006:
283 ret = drff_init(hdev);
284 if (ret) {
285 dev_err(&hdev->dev, "force feedback init failed\n");
286 hid_hw_stop(hdev);
287 goto err;
288 }
289 break;
290 }
3f866fbd
RW
291
292 return 0;
293err:
294 return ret;
295}
296
297static const struct hid_device_id dr_devices[] = {
298 { HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0006), },
e05eefb9 299 { HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0011), },
3f866fbd
RW
300 { }
301};
302MODULE_DEVICE_TABLE(hid, dr_devices);
303
304static struct hid_driver dr_driver = {
305 .name = "dragonrise",
306 .id_table = dr_devices,
e05eefb9 307 .report_fixup = dr_report_fixup,
3f866fbd 308 .probe = dr_probe,
e1594409 309 .input_mapping = dr_input_mapping,
3f866fbd 310};
f425458e 311module_hid_driver(dr_driver);
3f866fbd 312
3f866fbd 313MODULE_LICENSE("GPL");