Merge branch 'for-2.6.31' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[linux-2.6] / drivers / hid / hid-drff.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/usb.h>
32 #include <linux/hid.h>
33
34 #include "hid-ids.h"
35
36 #ifdef CONFIG_DRAGONRISE_FF
37 #include "usbhid/usbhid.h"
38
39 struct drff_device {
40         struct hid_report *report;
41 };
42
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                 usbhid_submit_report(hid, drff->report, USB_DIR_OUT);
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         usbhid_submit_report(hid, drff->report, USB_DIR_OUT);
82
83         return 0;
84 }
85
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 = 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)) {
98                 dev_err(&hid->dev, "no output reports found\n");
99                 return -ENODEV;
100         }
101
102         report = list_first_entry(report_list, struct hid_report, list);
103         if (report->maxfield < 1) {
104                 dev_err(&hid->dev, "no fields in the report\n");
105                 return -ENODEV;
106         }
107
108         if (report->field[0]->report_count < 7) {
109                 dev_err(&hid->dev, "not enough values in the field\n");
110                 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;
133         usbhid_submit_report(hid, drff->report, USB_DIR_OUT);
134
135         dev_info(&hid->dev, "Force Feedback for DragonRise Inc. game "
136                "controllers by Richard Walmsley <richwalm@gmail.com>\n");
137
138         return 0;
139 }
140 #else
141 static inline int drff_init(struct hid_device *hid)
142 {
143         return 0;
144 }
145 #endif
146
147 static int dr_probe(struct hid_device *hdev, const struct hid_device_id *id)
148 {
149         int ret;
150
151         dev_dbg(&hdev->dev, "DragonRise Inc. HID hardware probe...");
152
153         ret = hid_parse(hdev);
154         if (ret) {
155                 dev_err(&hdev->dev, "parse failed\n");
156                 goto err;
157         }
158
159         ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
160         if (ret) {
161                 dev_err(&hdev->dev, "hw start failed\n");
162                 goto err;
163         }
164
165         drff_init(hdev);
166
167         return 0;
168 err:
169         return ret;
170 }
171
172 static const struct hid_device_id dr_devices[] = {
173         { HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0006),  },
174         { }
175 };
176 MODULE_DEVICE_TABLE(hid, dr_devices);
177
178 static struct hid_driver dr_driver = {
179         .name = "dragonrise",
180         .id_table = dr_devices,
181         .probe = dr_probe,
182 };
183
184 static int __init dr_init(void)
185 {
186         return hid_register_driver(&dr_driver);
187 }
188
189 static void __exit dr_exit(void)
190 {
191         hid_unregister_driver(&dr_driver);
192 }
193
194 module_init(dr_init);
195 module_exit(dr_exit);
196 MODULE_LICENSE("GPL");