blob: ab5ba6ef891cb756fd531119baeabef7211c6dcd [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 */
39#define THRUSTMASTER_USAGE_RUMBLE_LR (HID_UP_GENDESK | 0xbb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42struct tmff_device {
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 struct hid_report *report;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 struct hid_field *rumble;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045};
46
Anssi Hannuladc76c912006-07-19 01:40:55 -040047/* Changes values from 0 to 0xffff into values from minimum to maximum */
48static inline int hid_tmff_scale(unsigned int in, int minimum, int maximum)
49{
50 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Anssi Hannuladc76c912006-07-19 01:40:55 -040052 ret = (in * (maximum - minimum) / 0xffff) + minimum;
53 if (ret < minimum)
54 return minimum;
55 if (ret > maximum)
56 return maximum;
57 return ret;
58}
59
60static int hid_tmff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
61{
Dmitry Torokhove0712982007-05-09 10:17:31 +020062 struct hid_device *hid = input_get_drvdata(dev);
Anssi Hannuladc76c912006-07-19 01:40:55 -040063 struct tmff_device *tmff = data;
64 int left, right; /* Rumbling */
65
66 left = hid_tmff_scale(effect->u.rumble.weak_magnitude,
67 tmff->rumble->logical_minimum, tmff->rumble->logical_maximum);
68 right = hid_tmff_scale(effect->u.rumble.strong_magnitude,
69 tmff->rumble->logical_minimum, tmff->rumble->logical_maximum);
70
71 tmff->rumble->value[0] = left;
72 tmff->rumble->value[1] = right;
73 dbg("(left,right)=(%08x, %08x)", left, right);
Jiri Kosina4916b3a2006-12-08 18:41:03 +010074 usbhid_submit_report(hid, tmff->report, USB_DIR_OUT);
Anssi Hannuladc76c912006-07-19 01:40:55 -040075
76 return 0;
77}
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79int hid_tmff_init(struct hid_device *hid)
80{
Anssi Hannuladc76c912006-07-19 01:40:55 -040081 struct tmff_device *tmff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 struct list_head *pos;
83 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -050084 struct input_dev *input_dev = hidinput->input;
Anssi Hannuladc76c912006-07-19 01:40:55 -040085 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Anssi Hannuladc76c912006-07-19 01:40:55 -040087 tmff = kzalloc(sizeof(struct tmff_device), GFP_KERNEL);
88 if (!tmff)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 return -ENOMEM;
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 /* Find the report to use */
92 __list_for_each(pos, &hid->report_enum[HID_OUTPUT_REPORT].report_list) {
93 struct hid_report *report = (struct hid_report *)pos;
94 int fieldnum;
95
96 for (fieldnum = 0; fieldnum < report->maxfield; ++fieldnum) {
97 struct hid_field *field = report->field[fieldnum];
98
99 if (field->maxusage <= 0)
100 continue;
101
102 switch (field->usage[0].hid) {
103 case THRUSTMASTER_USAGE_RUMBLE_LR:
104 if (field->report_count < 2) {
105 warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR with report_count < 2");
106 continue;
107 }
108
109 if (field->logical_maximum == field->logical_minimum) {
110 warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR with logical_maximum == logical_minimum");
111 continue;
112 }
113
Anssi Hannuladc76c912006-07-19 01:40:55 -0400114 if (tmff->report && tmff->report != report) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR in other report");
116 continue;
117 }
118
Anssi Hannuladc76c912006-07-19 01:40:55 -0400119 if (tmff->rumble && tmff->rumble != field) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 warn("ignoring duplicate THRUSTMASTER_USAGE_RUMBLE_LR");
121 continue;
122 }
123
Anssi Hannuladc76c912006-07-19 01:40:55 -0400124 tmff->report = report;
125 tmff->rumble = field;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500127 set_bit(FF_RUMBLE, input_dev->ffbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 break;
129
130 default:
131 warn("ignoring unknown output usage %08x", field->usage[0].hid);
132 continue;
133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135 }
136
Anssi Hannuladc76c912006-07-19 01:40:55 -0400137 error = input_ff_create_memless(input_dev, tmff, hid_tmff_play);
138 if (error) {
139 kfree(tmff);
140 return error;
141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 info("Force feedback for ThrustMaster rumble pad devices by Zinx Verituse <zinx@epicsol.org>");
144
145 return 0;
146}
147