isa: Call isa_bus_init before dependent ISA bus drivers register
[pandora-kernel.git] / drivers / hid / hid-dr.c
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/usb.h>
33 #include <linux/hid.h>
34 #include <linux/module.h>
35
36 #include "hid-ids.h"
37
38 #ifdef CONFIG_DRAGONRISE_FF
39 #include "usbhid/usbhid.h"
40
41 struct drff_device {
42         struct hid_report *report;
43 };
44
45 static int drff_play(struct input_dev *dev, void *data,
46                                  struct ff_effect *effect)
47 {
48         struct hid_device *hid = input_get_drvdata(dev);
49         struct drff_device *drff = data;
50         int strong, weak;
51
52         strong = effect->u.rumble.strong_magnitude;
53         weak = effect->u.rumble.weak_magnitude;
54
55         dbg_hid("called with 0x%04x 0x%04x", strong, weak);
56
57         if (strong || weak) {
58                 strong = strong * 0xff / 0xffff;
59                 weak = weak * 0xff / 0xffff;
60
61                 /* While reverse engineering this device, I found that when
62                    this value is set, it causes the strong rumble to function
63                    at a near maximum speed, so we'll bypass it. */
64                 if (weak == 0x0a)
65                         weak = 0x0b;
66
67                 drff->report->field[0]->value[0] = 0x51;
68                 drff->report->field[0]->value[1] = 0x00;
69                 drff->report->field[0]->value[2] = weak;
70                 drff->report->field[0]->value[4] = strong;
71                 usbhid_submit_report(hid, drff->report, USB_DIR_OUT);
72
73                 drff->report->field[0]->value[0] = 0xfa;
74                 drff->report->field[0]->value[1] = 0xfe;
75         } else {
76                 drff->report->field[0]->value[0] = 0xf3;
77                 drff->report->field[0]->value[1] = 0x00;
78         }
79
80         drff->report->field[0]->value[2] = 0x00;
81         drff->report->field[0]->value[4] = 0x00;
82         dbg_hid("running with 0x%02x 0x%02x", strong, weak);
83         usbhid_submit_report(hid, drff->report, USB_DIR_OUT);
84
85         return 0;
86 }
87
88 static int drff_init(struct hid_device *hid)
89 {
90         struct drff_device *drff;
91         struct hid_report *report;
92         struct hid_input *hidinput = list_first_entry(&hid->inputs,
93                                                 struct hid_input, list);
94         struct list_head *report_list =
95                         &hid->report_enum[HID_OUTPUT_REPORT].report_list;
96         struct input_dev *dev = hidinput->input;
97         int error;
98
99         if (list_empty(report_list)) {
100                 hid_err(hid, "no output reports found\n");
101                 return -ENODEV;
102         }
103
104         report = list_first_entry(report_list, struct hid_report, list);
105         if (report->maxfield < 1) {
106                 hid_err(hid, "no fields in the report\n");
107                 return -ENODEV;
108         }
109
110         if (report->field[0]->report_count < 7) {
111                 hid_err(hid, "not enough values in the field\n");
112                 return -ENODEV;
113         }
114
115         drff = kzalloc(sizeof(struct drff_device), GFP_KERNEL);
116         if (!drff)
117                 return -ENOMEM;
118
119         set_bit(FF_RUMBLE, dev->ffbit);
120
121         error = input_ff_create_memless(dev, drff, drff_play);
122         if (error) {
123                 kfree(drff);
124                 return error;
125         }
126
127         drff->report = report;
128         drff->report->field[0]->value[0] = 0xf3;
129         drff->report->field[0]->value[1] = 0x00;
130         drff->report->field[0]->value[2] = 0x00;
131         drff->report->field[0]->value[3] = 0x00;
132         drff->report->field[0]->value[4] = 0x00;
133         drff->report->field[0]->value[5] = 0x00;
134         drff->report->field[0]->value[6] = 0x00;
135         usbhid_submit_report(hid, drff->report, USB_DIR_OUT);
136
137         hid_info(hid, "Force Feedback for DragonRise Inc. "
138                  "game controllers by Richard Walmsley <richwalm@gmail.com>\n");
139
140         return 0;
141 }
142 #else
143 static inline int drff_init(struct hid_device *hid)
144 {
145         return 0;
146 }
147 #endif
148
149 /*
150  * The original descriptor of joystick with PID 0x0011, represented by DVTech PC
151  * JS19. It seems both copied from another device and a result of confusion
152  * either about the specification or about the program used to create the
153  * descriptor. In any case, it's a wonder it works on Windows.
154  *
155  *  Usage Page (Desktop),             ; Generic desktop controls (01h)
156  *  Usage (Joystik),                  ; Joystik (04h, application collection)
157  *  Collection (Application),
158  *    Collection (Logical),
159  *      Report Size (8),
160  *      Report Count (5),
161  *      Logical Minimum (0),
162  *      Logical Maximum (255),
163  *      Physical Minimum (0),
164  *      Physical Maximum (255),
165  *      Usage (X),                    ; X (30h, dynamic value)
166  *      Usage (X),                    ; X (30h, dynamic value)
167  *      Usage (X),                    ; X (30h, dynamic value)
168  *      Usage (X),                    ; X (30h, dynamic value)
169  *      Usage (Y),                    ; Y (31h, dynamic value)
170  *      Input (Variable),
171  *      Report Size (4),
172  *      Report Count (1),
173  *      Logical Maximum (7),
174  *      Physical Maximum (315),
175  *      Unit (Degrees),
176  *      Usage (00h),
177  *      Input (Variable, Null State),
178  *      Unit,
179  *      Report Size (1),
180  *      Report Count (10),
181  *      Logical Maximum (1),
182  *      Physical Maximum (1),
183  *      Usage Page (Button),          ; Button (09h)
184  *      Usage Minimum (01h),
185  *      Usage Maximum (0Ah),
186  *      Input (Variable),
187  *      Usage Page (FF00h),           ; FF00h, vendor-defined
188  *      Report Size (1),
189  *      Report Count (10),
190  *      Logical Maximum (1),
191  *      Physical Maximum (1),
192  *      Usage (01h),
193  *      Input (Variable),
194  *    End Collection,
195  *    Collection (Logical),
196  *      Report Size (8),
197  *      Report Count (4),
198  *      Physical Maximum (255),
199  *      Logical Maximum (255),
200  *      Usage (02h),
201  *      Output (Variable),
202  *    End Collection,
203  *  End Collection
204  */
205
206 /* Size of the original descriptor of the PID 0x0011 joystick */
207 #define PID0011_RDESC_ORIG_SIZE 101
208
209 /* Fixed report descriptor for PID 0x011 joystick */
210 static __u8 pid0011_rdesc_fixed[] = {
211         0x05, 0x01,         /*  Usage Page (Desktop),           */
212         0x09, 0x04,         /*  Usage (Joystik),                */
213         0xA1, 0x01,         /*  Collection (Application),       */
214         0xA1, 0x02,         /*      Collection (Logical),       */
215         0x14,               /*          Logical Minimum (0),    */
216         0x75, 0x08,         /*          Report Size (8),        */
217         0x95, 0x03,         /*          Report Count (3),       */
218         0x81, 0x01,         /*          Input (Constant),       */
219         0x26, 0xFF, 0x00,   /*          Logical Maximum (255),  */
220         0x95, 0x02,         /*          Report Count (2),       */
221         0x09, 0x30,         /*          Usage (X),              */
222         0x09, 0x31,         /*          Usage (Y),              */
223         0x81, 0x02,         /*          Input (Variable),       */
224         0x75, 0x01,         /*          Report Size (1),        */
225         0x95, 0x04,         /*          Report Count (4),       */
226         0x81, 0x01,         /*          Input (Constant),       */
227         0x25, 0x01,         /*          Logical Maximum (1),    */
228         0x95, 0x0A,         /*          Report Count (10),      */
229         0x05, 0x09,         /*          Usage Page (Button),    */
230         0x19, 0x01,         /*          Usage Minimum (01h),    */
231         0x29, 0x0A,         /*          Usage Maximum (0Ah),    */
232         0x81, 0x02,         /*          Input (Variable),       */
233         0x95, 0x0A,         /*          Report Count (10),      */
234         0x81, 0x01,         /*          Input (Constant),       */
235         0xC0,               /*      End Collection,             */
236         0xC0                /*  End Collection                  */
237 };
238
239 static __u8 pid0006_rdesc_fixed[] = {
240         0x05, 0x01,        /* Usage Page (Generic Desktop)      */
241         0x09, 0x04,        /* Usage (Joystick)                  */
242         0xA1, 0x01,        /* Collection (Application)          */
243         0xA1, 0x02,        /*   Collection (Logical)            */
244         0x75, 0x08,        /*     Report Size (8)               */
245         0x95, 0x05,        /*     Report Count (5)              */
246         0x15, 0x00,        /*     Logical Minimum (0)           */
247         0x26, 0xFF, 0x00,  /*     Logical Maximum (255)         */
248         0x35, 0x00,        /*     Physical Minimum (0)          */
249         0x46, 0xFF, 0x00,  /*     Physical Maximum (255)        */
250         0x09, 0x30,        /*     Usage (X)                     */
251         0x09, 0x33,        /*     Usage (Ry)                    */
252         0x09, 0x32,        /*     Usage (Z)                     */
253         0x09, 0x31,        /*     Usage (Y)                     */
254         0x09, 0x34,        /*     Usage (Ry)                    */
255         0x81, 0x02,        /*     Input (Variable)              */
256         0x75, 0x04,        /*     Report Size (4)               */
257         0x95, 0x01,        /*     Report Count (1)              */
258         0x25, 0x07,        /*     Logical Maximum (7)           */
259         0x46, 0x3B, 0x01,  /*     Physical Maximum (315)        */
260         0x65, 0x14,        /*     Unit (Centimeter)             */
261         0x09, 0x39,        /*     Usage (Hat switch)            */
262         0x81, 0x42,        /*     Input (Variable)              */
263         0x65, 0x00,        /*     Unit (None)                   */
264         0x75, 0x01,        /*     Report Size (1)               */
265         0x95, 0x0C,        /*     Report Count (12)             */
266         0x25, 0x01,        /*     Logical Maximum (1)           */
267         0x45, 0x01,        /*     Physical Maximum (1)          */
268         0x05, 0x09,        /*     Usage Page (Button)           */
269         0x19, 0x01,        /*     Usage Minimum (0x01)          */
270         0x29, 0x0C,        /*     Usage Maximum (0x0C)          */
271         0x81, 0x02,        /*     Input (Variable)              */
272         0x06, 0x00, 0xFF,  /*     Usage Page (Vendor Defined)   */
273         0x75, 0x01,        /*     Report Size (1)               */
274         0x95, 0x08,        /*     Report Count (8)              */
275         0x25, 0x01,        /*     Logical Maximum (1)           */
276         0x45, 0x01,        /*     Physical Maximum (1)          */
277         0x09, 0x01,        /*     Usage (0x01)                  */
278         0x81, 0x02,        /*     Input (Variable)              */
279         0xC0,              /*   End Collection                  */
280         0xA1, 0x02,        /*   Collection (Logical)            */
281         0x75, 0x08,        /*     Report Size (8)               */
282         0x95, 0x07,        /*     Report Count (7)              */
283         0x46, 0xFF, 0x00,  /*     Physical Maximum (255)        */
284         0x26, 0xFF, 0x00,  /*     Logical Maximum (255)         */
285         0x09, 0x02,        /*     Usage (0x02)                  */
286         0x91, 0x02,        /*     Output (Variable)             */
287         0xC0,              /*   End Collection                  */
288         0xC0               /* End Collection                    */
289 };
290
291 static __u8 *dr_report_fixup(struct hid_device *hdev, __u8 *rdesc,
292                                 unsigned int *rsize)
293 {
294         switch (hdev->product) {
295         case 0x0011:
296                 if (*rsize == PID0011_RDESC_ORIG_SIZE) {
297                         rdesc = pid0011_rdesc_fixed;
298                         *rsize = sizeof(pid0011_rdesc_fixed);
299                 }
300                 break;
301         case 0x0006:
302                 if (*rsize == sizeof(pid0006_rdesc_fixed)) {
303                         rdesc = pid0006_rdesc_fixed;
304                         *rsize = sizeof(pid0006_rdesc_fixed);
305                 }
306                 break;
307         }
308         return rdesc;
309 }
310
311 static int dr_probe(struct hid_device *hdev, const struct hid_device_id *id)
312 {
313         int ret;
314
315         dev_dbg(&hdev->dev, "DragonRise Inc. HID hardware probe...");
316
317         ret = hid_parse(hdev);
318         if (ret) {
319                 hid_err(hdev, "parse failed\n");
320                 goto err;
321         }
322
323         ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
324         if (ret) {
325                 hid_err(hdev, "hw start failed\n");
326                 goto err;
327         }
328
329         switch (hdev->product) {
330         case 0x0006:
331                 ret = drff_init(hdev);
332                 if (ret) {
333                         dev_err(&hdev->dev, "force feedback init failed\n");
334                         hid_hw_stop(hdev);
335                         goto err;
336                 }
337                 break;
338         }
339
340         return 0;
341 err:
342         return ret;
343 }
344
345 static const struct hid_device_id dr_devices[] = {
346         { HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0006),  },
347         { HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0011),  },
348         { }
349 };
350 MODULE_DEVICE_TABLE(hid, dr_devices);
351
352 static struct hid_driver dr_driver = {
353         .name = "dragonrise",
354         .id_table = dr_devices,
355         .report_fixup = dr_report_fixup,
356         .probe = dr_probe,
357 };
358
359 static int __init dr_init(void)
360 {
361         return hid_register_driver(&dr_driver);
362 }
363
364 static void __exit dr_exit(void)
365 {
366         hid_unregister_driver(&dr_driver);
367 }
368
369 module_init(dr_init);
370 module_exit(dr_exit);
371 MODULE_LICENSE("GPL");