blob: 69882a726e99d8cc19b6fda183e2b0f9ca8de240 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Force feedback support for various HID compliant devices by ThrustMaster:
3 * ThrustMaster FireStorm Dual Power 2
4 * and possibly others whose device ids haven't been added.
5 *
6 * Modified to support ThrustMaster devices by Zinx Verituse
7 * on 2003-01-25 from the Logitech force feedback driver,
8 * which is by Johann Deneux.
9 *
10 * Copyright (c) 2003 Zinx Verituse <zinx@epicsol.org>
11 * Copyright (c) 2002 Johann Deneux
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#undef DEBUG
33#include <linux/usb.h>
34
Jiri Kosinadde58452006-12-08 18:40:44 +010035#include <linux/hid.h>
Jiri Kosina4916b3a2006-12-08 18:41:03 +010036#include "usbhid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/* Usages for thrustmaster devices I know about */
Dmitry Torokhovb27c9592007-07-30 14:56:26 +020039#define THRUSTMASTER_USAGE_FF (HID_UP_GENDESK | 0xbb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Dmitry Torokhovb27c9592007-07-30 14:56:26 +020041struct dev_type {
42 u16 idVendor;
43 u16 idProduct;
44 const signed short *ff;
45};
46
47static const signed short ff_rumble[] = {
48 FF_RUMBLE,
49 -1
50};
51
52static const signed short ff_joystick[] = {
53 FF_CONSTANT,
54 -1
55};
56
57static const struct dev_type devices[] = {
58 { 0x44f, 0xb300, ff_rumble },
59 { 0x44f, 0xb304, ff_rumble },
60 { 0x44f, 0xb651, ff_rumble }, /* FGT Rumble Force Wheel */
61 { 0x44f, 0xb654, ff_joystick }, /* FGT Force Feedback Wheel */
62};
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64struct tmff_device {
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 struct hid_report *report;
Dmitry Torokhovb27c9592007-07-30 14:56:26 +020066 struct hid_field *ff_field;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68
Anssi Hannuladc76c912006-07-19 01:40:55 -040069/* Changes values from 0 to 0xffff into values from minimum to maximum */
Dmitry Torokhovb27c9592007-07-30 14:56:26 +020070static inline int hid_tmff_scale_u16(unsigned int in,
71 int minimum, int maximum)
Anssi Hannuladc76c912006-07-19 01:40:55 -040072{
73 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Anssi Hannuladc76c912006-07-19 01:40:55 -040075 ret = (in * (maximum - minimum) / 0xffff) + minimum;
76 if (ret < minimum)
77 return minimum;
78 if (ret > maximum)
79 return maximum;
80 return ret;
81}
82
Dmitry Torokhovb27c9592007-07-30 14:56:26 +020083/* Changes values from -0x80 to 0x7f into values from minimum to maximum */
84static inline int hid_tmff_scale_s8(int in,
85 int minimum, int maximum)
86{
87 int ret;
88
89 ret = (((in + 0x80) * (maximum - minimum)) / 0xff) + minimum;
90 if (ret < minimum)
91 return minimum;
92 if (ret > maximum)
93 return maximum;
94 return ret;
95}
96
Anssi Hannuladc76c912006-07-19 01:40:55 -040097static int hid_tmff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
98{
Dmitry Torokhove0712982007-05-09 10:17:31 +020099 struct hid_device *hid = input_get_drvdata(dev);
Anssi Hannuladc76c912006-07-19 01:40:55 -0400100 struct tmff_device *tmff = data;
Dmitry Torokhovb27c9592007-07-30 14:56:26 +0200101 struct hid_field *ff_field = tmff->ff_field;
102 int x, y;
Anssi Hannuladc76c912006-07-19 01:40:55 -0400103 int left, right; /* Rumbling */
104
Dmitry Torokhovb27c9592007-07-30 14:56:26 +0200105 switch (effect->type) {
106 case FF_CONSTANT:
107 x = hid_tmff_scale_s8(effect->u.ramp.start_level,
108 ff_field->logical_minimum,
109 ff_field->logical_maximum);
110 y = hid_tmff_scale_s8(effect->u.ramp.end_level,
111 ff_field->logical_minimum,
112 ff_field->logical_maximum);
Anssi Hannuladc76c912006-07-19 01:40:55 -0400113
Dmitry Torokhovb27c9592007-07-30 14:56:26 +0200114 dbg_hid("(x, y)=(%04x, %04x)\n", x, y);
115 ff_field->value[0] = x;
116 ff_field->value[1] = y;
117 usbhid_submit_report(hid, tmff->report, USB_DIR_OUT);
118 break;
Anssi Hannuladc76c912006-07-19 01:40:55 -0400119
Dmitry Torokhovb27c9592007-07-30 14:56:26 +0200120 case FF_RUMBLE:
121 left = hid_tmff_scale_u16(effect->u.rumble.weak_magnitude,
122 ff_field->logical_minimum,
123 ff_field->logical_maximum);
124 right = hid_tmff_scale_u16(effect->u.rumble.strong_magnitude,
125 ff_field->logical_minimum,
126 ff_field->logical_maximum);
127
128 dbg_hid("(left,right)=(%08x, %08x)\n", left, right);
129 ff_field->value[0] = left;
130 ff_field->value[1] = right;
131 usbhid_submit_report(hid, tmff->report, USB_DIR_OUT);
132 break;
133 }
Anssi Hannuladc76c912006-07-19 01:40:55 -0400134 return 0;
135}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137int hid_tmff_init(struct hid_device *hid)
138{
Anssi Hannuladc76c912006-07-19 01:40:55 -0400139 struct tmff_device *tmff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 struct list_head *pos;
141 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500142 struct input_dev *input_dev = hidinput->input;
Dmitry Torokhovb27c9592007-07-30 14:56:26 +0200143 const signed short *ff_bits = ff_joystick;
Anssi Hannuladc76c912006-07-19 01:40:55 -0400144 int error;
Dmitry Torokhovb27c9592007-07-30 14:56:26 +0200145 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Anssi Hannuladc76c912006-07-19 01:40:55 -0400147 tmff = kzalloc(sizeof(struct tmff_device), GFP_KERNEL);
148 if (!tmff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return -ENOMEM;
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 /* Find the report to use */
Dmitry Torokhovb27c9592007-07-30 14:56:26 +0200152 list_for_each(pos, &hid->report_enum[HID_OUTPUT_REPORT].report_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 struct hid_report *report = (struct hid_report *)pos;
154 int fieldnum;
155
156 for (fieldnum = 0; fieldnum < report->maxfield; ++fieldnum) {
157 struct hid_field *field = report->field[fieldnum];
158
159 if (field->maxusage <= 0)
160 continue;
161
162 switch (field->usage[0].hid) {
Dmitry Torokhovb27c9592007-07-30 14:56:26 +0200163 case THRUSTMASTER_USAGE_FF:
164 if (field->report_count < 2) {
165 warn("ignoring FF field with report_count < 2");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 continue;
Dmitry Torokhovb27c9592007-07-30 14:56:26 +0200167 }
168
169 if (field->logical_maximum == field->logical_minimum) {
170 warn("ignoring FF field with logical_maximum == logical_minimum");
171 continue;
172 }
173
174 if (tmff->report && tmff->report != report) {
175 warn("ignoring FF field in other report");
176 continue;
177 }
178
179 if (tmff->ff_field && tmff->ff_field != field) {
180 warn("ignoring duplicate FF field");
181 continue;
182 }
183
184 tmff->report = report;
185 tmff->ff_field = field;
186
187 for (i = 0; i < ARRAY_SIZE(devices); i++) {
188 if (input_dev->id.vendor == devices[i].idVendor &&
189 input_dev->id.product == devices[i].idProduct) {
190 ff_bits = devices[i].ff;
191 break;
192 }
193 }
194
195 for (i = 0; ff_bits[i] >= 0; i++)
196 set_bit(ff_bits[i], input_dev->ffbit);
197
198 break;
199
200 default:
201 warn("ignoring unknown output usage %08x", field->usage[0].hid);
202 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205 }
206
Dmitry Torokhovb27c9592007-07-30 14:56:26 +0200207 if (!tmff->report) {
208 err("cant find FF field in output reports\n");
209 error = -ENODEV;
210 goto fail;
Anssi Hannuladc76c912006-07-19 01:40:55 -0400211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Dmitry Torokhovb27c9592007-07-30 14:56:26 +0200213 error = input_ff_create_memless(input_dev, tmff, hid_tmff_play);
214 if (error)
215 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Dmitry Torokhovb27c9592007-07-30 14:56:26 +0200217 info("Force feedback for ThrustMaster devices by Zinx Verituse <zinx@epicsol.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return 0;
Dmitry Torokhovb27c9592007-07-30 14:56:26 +0200219
220 fail:
221 kfree(tmff);
222 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224