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