blob: afcf3d67eb024683cc43e11f5325184ce8235871 [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/usb.h>
33#include <linux/hid.h>
34
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#include "usbhid/usbhid.h"
39
40struct drff_device {
41 struct hid_report *report;
42};
43
44static int drff_play(struct input_dev *dev, void *data,
45 struct ff_effect *effect)
46{
47 struct hid_device *hid = input_get_drvdata(dev);
48 struct drff_device *drff = data;
49 int strong, weak;
50
51 strong = effect->u.rumble.strong_magnitude;
52 weak = effect->u.rumble.weak_magnitude;
53
54 dbg_hid("called with 0x%04x 0x%04x", strong, weak);
55
56 if (strong || weak) {
57 strong = strong * 0xff / 0xffff;
58 weak = weak * 0xff / 0xffff;
59
60 /* While reverse engineering this device, I found that when
61 this value is set, it causes the strong rumble to function
62 at a near maximum speed, so we'll bypass it. */
63 if (weak == 0x0a)
64 weak = 0x0b;
65
66 drff->report->field[0]->value[0] = 0x51;
67 drff->report->field[0]->value[1] = 0x00;
68 drff->report->field[0]->value[2] = weak;
69 drff->report->field[0]->value[4] = strong;
70 usbhid_submit_report(hid, drff->report, USB_DIR_OUT);
71
72 drff->report->field[0]->value[0] = 0xfa;
73 drff->report->field[0]->value[1] = 0xfe;
74 } else {
75 drff->report->field[0]->value[0] = 0xf3;
76 drff->report->field[0]->value[1] = 0x00;
77 }
78
79 drff->report->field[0]->value[2] = 0x00;
80 drff->report->field[0]->value[4] = 0x00;
81 dbg_hid("running with 0x%02x 0x%02x", strong, weak);
82 usbhid_submit_report(hid, drff->report, USB_DIR_OUT);
83
84 return 0;
85}
86
87static int drff_init(struct hid_device *hid)
88{
89 struct drff_device *drff;
90 struct hid_report *report;
91 struct hid_input *hidinput = list_first_entry(&hid->inputs,
92 struct hid_input, list);
93 struct list_head *report_list =
94 &hid->report_enum[HID_OUTPUT_REPORT].report_list;
95 struct input_dev *dev = hidinput->input;
96 int error;
97
98 if (list_empty(report_list)) {
Joe Perches4291ee32010-12-09 19:29:03 -080099 hid_err(hid, "no output reports found\n");
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300100 return -ENODEV;
101 }
102
103 report = list_first_entry(report_list, struct hid_report, list);
104 if (report->maxfield < 1) {
Joe Perches4291ee32010-12-09 19:29:03 -0800105 hid_err(hid, "no fields in the report\n");
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300106 return -ENODEV;
107 }
108
109 if (report->field[0]->report_count < 7) {
Joe Perches4291ee32010-12-09 19:29:03 -0800110 hid_err(hid, "not enough values in the field\n");
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300111 return -ENODEV;
112 }
113
114 drff = kzalloc(sizeof(struct drff_device), GFP_KERNEL);
115 if (!drff)
116 return -ENOMEM;
117
118 set_bit(FF_RUMBLE, dev->ffbit);
119
120 error = input_ff_create_memless(dev, drff, drff_play);
121 if (error) {
122 kfree(drff);
123 return error;
124 }
125
126 drff->report = report;
127 drff->report->field[0]->value[0] = 0xf3;
128 drff->report->field[0]->value[1] = 0x00;
129 drff->report->field[0]->value[2] = 0x00;
130 drff->report->field[0]->value[3] = 0x00;
131 drff->report->field[0]->value[4] = 0x00;
132 drff->report->field[0]->value[5] = 0x00;
133 drff->report->field[0]->value[6] = 0x00;
134 usbhid_submit_report(hid, drff->report, USB_DIR_OUT);
135
Joe Perches4291ee32010-12-09 19:29:03 -0800136 hid_info(hid, "Force Feedback for DragonRise Inc. "
137 "game controllers by Richard Walmsley <richwalm@gmail.com>\n");
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300138
139 return 0;
140}
Jiri Kosina0f6f4312009-05-15 15:46:44 +0200141#else
142static inline int drff_init(struct hid_device *hid)
143{
144 return 0;
145}
146#endif
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300147
148static int dr_probe(struct hid_device *hdev, const struct hid_device_id *id)
149{
150 int ret;
151
152 dev_dbg(&hdev->dev, "DragonRise Inc. HID hardware probe...");
153
154 ret = hid_parse(hdev);
155 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800156 hid_err(hdev, "parse failed\n");
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300157 goto err;
158 }
159
160 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
161 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800162 hid_err(hdev, "hw start failed\n");
Richard Walmsley3f866fb2009-03-04 22:12:04 +1300163 goto err;
164 }
165
166 drff_init(hdev);
167
168 return 0;
169err:
170 return ret;
171}
172
173static const struct hid_device_id dr_devices[] = {
174 { HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, 0x0006), },
175 { }
176};
177MODULE_DEVICE_TABLE(hid, dr_devices);
178
179static struct hid_driver dr_driver = {
180 .name = "dragonrise",
181 .id_table = dr_devices,
182 .probe = dr_probe,
183};
184
185static int __init dr_init(void)
186{
187 return hid_register_driver(&dr_driver);
188}
189
190static void __exit dr_exit(void)
191{
192 hid_unregister_driver(&dr_driver);
193}
194
195module_init(dr_init);
196module_exit(dr_exit);
197MODULE_LICENSE("GPL");