blob: ce0644424f587707e68023f066eefecc50048d6b [file] [log] [blame]
Richard Walmsley3f866fb2009-03-04 22:12:04 +13001/*
2 * Force feedback support for DragonRise Inc. game controllers
3 *
4 * From what I have gathered, these devices are mass produced in China and are
5 * distributed under several vendors. They often share the same design as
6 * the original PlayStation DualShock controller.
7 *
8 * 0079:0006 "DragonRise Inc. Generic USB Joystick "
9 * - tested with a Tesun USB-703 game controller.
10 *
11 * Copyright (c) 2009 Richard Walmsley <richwalm@gmail.com>
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Richard Walmsley3f866fb2009-03-04 22:12:04 +130032#include <linux/hid.h>
Paul Gortmaker8f86a2c2011-07-03 13:39:48 -040033#include <linux/module.h>
Richard Walmsley3f866fb2009-03-04 22:12:04 +130034
35#include "hid-ids.h"
Jiri Kosina0f6f4312009-05-15 15:46:44 +020036
37#ifdef CONFIG_DRAGONRISE_FF
Richard Walmsley3f866fb2009-03-04 22:12:04 +130038
39struct drff_device {
40 struct hid_report *report;
41};
42
43static int drff_play(struct input_dev *dev, void *data,
44 struct ff_effect *effect)
45{
46 struct hid_device *hid = input_get_drvdata(dev);
47 struct drff_device *drff = data;
48 int strong, weak;
49
50 strong = effect->u.rumble.strong_magnitude;
51 weak = effect->u.rumble.weak_magnitude;
52
53 dbg_hid("called with 0x%04x 0x%04x", strong, weak);
54
55 if (strong || weak) {
56 strong = strong * 0xff / 0xffff;
57 weak = weak * 0xff / 0xffff;
58
59 /* While reverse engineering this device, I found that when
60 this value is set, it causes the strong rumble to function
61 at a near maximum speed, so we'll bypass it. */
62 if (weak == 0x0a)
63 weak = 0x0b;
64
65 drff->report->field[0]->value[0] = 0x51;
66 drff->report->field[0]->value[1] = 0x00;
67 drff->report->field[0]->value[2] = weak;
68 drff->report->field[0]->value[4] = strong;
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 drff->report->field[0]->value[0] = 0xfa;
72 drff->report->field[0]->value[1] = 0xfe;
73 } else {
74 drff->report->field[0]->value[0] = 0xf3;
75 drff->report->field[0]->value[1] = 0x00;
76 }
77
78 drff->report->field[0]->value[2] = 0x00;
79 drff->report->field[0]->value[4] = 0x00;
80 dbg_hid("running with 0x%02x 0x%02x", strong, weak);
Benjamin Tissoiresd88142722013-02-25 11:31:46 +010081 hid_hw_request(hid, drff->report, HID_REQ_SET_REPORT);
Richard Walmsley3f866fb2009-03-04 22:12:04 +130082
83 return 0;
84}
85
86static int drff_init(struct hid_device *hid)
87{
88 struct drff_device *drff;
89 struct hid_report *report;
90 struct hid_input *hidinput = list_first_entry(&hid->inputs,
91 struct hid_input, list);
92 struct list_head *report_list =
93 &hid->report_enum[HID_OUTPUT_REPORT].report_list;
94 struct input_dev *dev = hidinput->input;
95 int error;
96
97 if (list_empty(report_list)) {
Joe Perches4291ee32010-12-09 19:29:03 -080098 hid_err(hid, "no output reports found\n");
Richard Walmsley3f866fb2009-03-04 22:12:04 +130099 return -ENODEV;
100 }
101
102 report = list_first_entry(report_list, struct hid_report, list);
103 if (report->maxfield < 1) {
Joe Perches4291ee32010-12-09 19:29:03 -0800104 hid_err(hid, "no fields in the report\n");
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300105 return -ENODEV;
106 }
107
108 if (report->field[0]->report_count < 7) {
Joe Perches4291ee32010-12-09 19:29:03 -0800109 hid_err(hid, "not enough values in the field\n");
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300110 return -ENODEV;
111 }
112
113 drff = kzalloc(sizeof(struct drff_device), GFP_KERNEL);
114 if (!drff)
115 return -ENOMEM;
116
117 set_bit(FF_RUMBLE, dev->ffbit);
118
119 error = input_ff_create_memless(dev, drff, drff_play);
120 if (error) {
121 kfree(drff);
122 return error;
123 }
124
125 drff->report = report;
126 drff->report->field[0]->value[0] = 0xf3;
127 drff->report->field[0]->value[1] = 0x00;
128 drff->report->field[0]->value[2] = 0x00;
129 drff->report->field[0]->value[3] = 0x00;
130 drff->report->field[0]->value[4] = 0x00;
131 drff->report->field[0]->value[5] = 0x00;
132 drff->report->field[0]->value[6] = 0x00;
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100133 hid_hw_request(hid, drff->report, HID_REQ_SET_REPORT);
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300134
Joe Perches4291ee32010-12-09 19:29:03 -0800135 hid_info(hid, "Force Feedback for DragonRise Inc. "
136 "game controllers by Richard Walmsley <richwalm@gmail.com>\n");
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300137
138 return 0;
139}
Jiri Kosina0f6f4312009-05-15 15:46:44 +0200140#else
141static inline int drff_init(struct hid_device *hid)
142{
143 return 0;
144}
145#endif
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300146
Nikolai Kondrashove05eefb2011-01-28 14:50:52 +0300147/*
148 * The original descriptor of joystick with PID 0x0011, represented by DVTech PC
149 * JS19. It seems both copied from another device and a result of confusion
150 * either about the specification or about the program used to create the
151 * descriptor. In any case, it's a wonder it works on Windows.
152 *
153 * Usage Page (Desktop), ; Generic desktop controls (01h)
154 * Usage (Joystik), ; Joystik (04h, application collection)
155 * Collection (Application),
156 * Collection (Logical),
157 * Report Size (8),
158 * Report Count (5),
159 * Logical Minimum (0),
160 * Logical Maximum (255),
161 * Physical Minimum (0),
162 * Physical Maximum (255),
163 * Usage (X), ; X (30h, dynamic value)
164 * Usage (X), ; X (30h, dynamic value)
165 * Usage (X), ; X (30h, dynamic value)
166 * Usage (X), ; X (30h, dynamic value)
167 * Usage (Y), ; Y (31h, dynamic value)
168 * Input (Variable),
169 * Report Size (4),
170 * Report Count (1),
171 * Logical Maximum (7),
172 * Physical Maximum (315),
173 * Unit (Degrees),
174 * Usage (00h),
175 * Input (Variable, Null State),
176 * Unit,
177 * Report Size (1),
178 * Report Count (10),
179 * Logical Maximum (1),
180 * Physical Maximum (1),
181 * Usage Page (Button), ; Button (09h)
182 * Usage Minimum (01h),
183 * Usage Maximum (0Ah),
184 * Input (Variable),
185 * Usage Page (FF00h), ; FF00h, vendor-defined
186 * Report Size (1),
187 * Report Count (10),
188 * Logical Maximum (1),
189 * Physical Maximum (1),
190 * Usage (01h),
191 * Input (Variable),
192 * End Collection,
193 * Collection (Logical),
194 * Report Size (8),
195 * Report Count (4),
196 * Physical Maximum (255),
197 * Logical Maximum (255),
198 * Usage (02h),
199 * Output (Variable),
200 * End Collection,
201 * End Collection
202 */
203
204/* Size of the original descriptor of the PID 0x0011 joystick */
205#define PID0011_RDESC_ORIG_SIZE 101
206
207/* Fixed report descriptor for PID 0x011 joystick */
208static __u8 pid0011_rdesc_fixed[] = {
209 0x05, 0x01, /* Usage Page (Desktop), */
210 0x09, 0x04, /* Usage (Joystik), */
211 0xA1, 0x01, /* Collection (Application), */
212 0xA1, 0x02, /* Collection (Logical), */
213 0x14, /* Logical Minimum (0), */
214 0x75, 0x08, /* Report Size (8), */
215 0x95, 0x03, /* Report Count (3), */
216 0x81, 0x01, /* Input (Constant), */
217 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
218 0x95, 0x02, /* Report Count (2), */
219 0x09, 0x30, /* Usage (X), */
220 0x09, 0x31, /* Usage (Y), */
221 0x81, 0x02, /* Input (Variable), */
222 0x75, 0x01, /* Report Size (1), */
223 0x95, 0x04, /* Report Count (4), */
224 0x81, 0x01, /* Input (Constant), */
225 0x25, 0x01, /* Logical Maximum (1), */
226 0x95, 0x0A, /* Report Count (10), */
227 0x05, 0x09, /* Usage Page (Button), */
228 0x19, 0x01, /* Usage Minimum (01h), */
229 0x29, 0x0A, /* Usage Maximum (0Ah), */
230 0x81, 0x02, /* Input (Variable), */
231 0x95, 0x0A, /* Report Count (10), */
232 0x81, 0x01, /* Input (Constant), */
233 0xC0, /* End Collection, */
234 0xC0 /* End Collection */
235};
236
237static __u8 *dr_report_fixup(struct hid_device *hdev, __u8 *rdesc,
238 unsigned int *rsize)
239{
240 switch (hdev->product) {
241 case 0x0011:
242 if (*rsize == PID0011_RDESC_ORIG_SIZE) {
243 rdesc = pid0011_rdesc_fixed;
244 *rsize = sizeof(pid0011_rdesc_fixed);
245 }
246 break;
247 }
248 return rdesc;
249}
250
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300251static int dr_probe(struct hid_device *hdev, const struct hid_device_id *id)
252{
253 int ret;
254
255 dev_dbg(&hdev->dev, "DragonRise Inc. HID hardware probe...");
256
257 ret = hid_parse(hdev);
258 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800259 hid_err(hdev, "parse failed\n");
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300260 goto err;
261 }
262
263 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
264 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800265 hid_err(hdev, "hw start failed\n");
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300266 goto err;
267 }
268
Nikolai Kondrashove05eefb2011-01-28 14:50:52 +0300269 switch (hdev->product) {
270 case 0x0006:
271 ret = drff_init(hdev);
272 if (ret) {
273 dev_err(&hdev->dev, "force feedback init failed\n");
274 hid_hw_stop(hdev);
275 goto err;
276 }
277 break;
278 }
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300279
280 return 0;
281err:
282 return ret;
283}
284
285static const struct hid_device_id dr_devices[] = {
286 { HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0006), },
Nikolai Kondrashove05eefb2011-01-28 14:50:52 +0300287 { HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0011), },
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300288 { }
289};
290MODULE_DEVICE_TABLE(hid, dr_devices);
291
292static struct hid_driver dr_driver = {
293 .name = "dragonrise",
294 .id_table = dr_devices,
Nikolai Kondrashove05eefb2011-01-28 14:50:52 +0300295 .report_fixup = dr_report_fixup,
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300296 .probe = dr_probe,
297};
H Hartley Sweetenf4254582012-12-17 15:28:26 -0700298module_hid_driver(dr_driver);
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300299
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300300MODULE_LICENSE("GPL");