hid-tmff.c 5.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*
 * Force feedback support for various HID compliant devices by ThrustMaster:
 *    ThrustMaster FireStorm Dual Power 2
 * and possibly others whose device ids haven't been added.
 *
 *  Modified to support ThrustMaster devices by Zinx Verituse
 *  on 2003-01-25 from the Logitech force feedback driver,
 *  which is by Johann Deneux.
 *
 *  Copyright (c) 2003 Zinx Verituse <zinx@epicsol.org>
 *  Copyright (c) 2002 Johann Deneux
 */

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <linux/input.h>

#undef DEBUG
#include <linux/usb.h>

35
#include <linux/hid.h>
J
Jiri Kosina 已提交
36
#include "usbhid.h"
L
Linus Torvalds 已提交
37 38

/* Usages for thrustmaster devices I know about */
39
#define THRUSTMASTER_USAGE_FF	(HID_UP_GENDESK | 0xbb)
L
Linus Torvalds 已提交
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
struct dev_type {
	u16 idVendor;
	u16 idProduct;
	const signed short *ff;
};

static const signed short ff_rumble[] = {
	FF_RUMBLE,
	-1
};

static const signed short ff_joystick[] = {
	FF_CONSTANT,
	-1
};

static const struct dev_type devices[] = {
	{ 0x44f, 0xb300, ff_rumble },
	{ 0x44f, 0xb304, ff_rumble },
	{ 0x44f, 0xb651, ff_rumble },	/* FGT Rumble Force Wheel */
	{ 0x44f, 0xb654, ff_joystick },	/* FGT Force Feedback Wheel */
};
L
Linus Torvalds 已提交
63 64 65

struct tmff_device {
	struct hid_report *report;
66
	struct hid_field *ff_field;
67
};
L
Linus Torvalds 已提交
68

69
/* Changes values from 0 to 0xffff into values from minimum to maximum */
70 71
static inline int hid_tmff_scale_u16(unsigned int in,
				int minimum, int maximum)
72 73
{
	int ret;
L
Linus Torvalds 已提交
74

75 76 77 78 79 80 81
	ret = (in * (maximum - minimum) / 0xffff) + minimum;
	if (ret < minimum)
		return minimum;
	if (ret > maximum)
		return maximum;
	return ret;
}
L
Linus Torvalds 已提交
82

83 84 85 86 87 88 89 90 91 92 93 94 95 96
/* Changes values from -0x80 to 0x7f into values from minimum to maximum */
static inline int hid_tmff_scale_s8(int in,
				    int minimum, int maximum)
{
	int ret;

	ret = (((in + 0x80) * (maximum - minimum)) / 0xff) + minimum;
	if (ret < minimum)
		return minimum;
	if (ret > maximum)
		return maximum;
	return ret;
}

97 98
static int hid_tmff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
{
99
	struct hid_device *hid = input_get_drvdata(dev);
100
	struct tmff_device *tmff = data;
101 102
	struct hid_field *ff_field = tmff->ff_field;
	int x, y;
103
	int left, right;	/* Rumbling */
L
Linus Torvalds 已提交
104

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
	switch (effect->type) {
	case FF_CONSTANT:
		x = hid_tmff_scale_s8(effect->u.ramp.start_level,
					ff_field->logical_minimum,
					ff_field->logical_maximum);
		y = hid_tmff_scale_s8(effect->u.ramp.end_level,
					ff_field->logical_minimum,
					ff_field->logical_maximum);

		dbg_hid("(x, y)=(%04x, %04x)\n", x, y);
		ff_field->value[0] = x;
		ff_field->value[1] = y;
		usbhid_submit_report(hid, tmff->report, USB_DIR_OUT);
		break;

	case FF_RUMBLE:
		left = hid_tmff_scale_u16(effect->u.rumble.weak_magnitude,
					ff_field->logical_minimum,
					ff_field->logical_maximum);
		right = hid_tmff_scale_u16(effect->u.rumble.strong_magnitude,
					ff_field->logical_minimum,
					ff_field->logical_maximum);

		dbg_hid("(left,right)=(%08x, %08x)\n", left, right);
		ff_field->value[0] = left;
		ff_field->value[1] = right;
		usbhid_submit_report(hid, tmff->report, USB_DIR_OUT);
		break;
	}
134 135
	return 0;
}
L
Linus Torvalds 已提交
136 137 138

int hid_tmff_init(struct hid_device *hid)
{
139
	struct tmff_device *tmff;
140 141
	struct hid_report *report;
	struct list_head *report_list;
L
Linus Torvalds 已提交
142
	struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
143
	struct input_dev *input_dev = hidinput->input;
144
	const signed short *ff_bits = ff_joystick;
145
	int error;
146
	int i;
L
Linus Torvalds 已提交
147

148 149
	tmff = kzalloc(sizeof(struct tmff_device), GFP_KERNEL);
	if (!tmff)
L
Linus Torvalds 已提交
150 151 152
		return -ENOMEM;

	/* Find the report to use */
153 154
	report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
	list_for_each_entry(report, report_list, list) {
L
Linus Torvalds 已提交
155 156 157 158 159 160 161 162 163
		int fieldnum;

		for (fieldnum = 0; fieldnum < report->maxfield; ++fieldnum) {
			struct hid_field *field = report->field[fieldnum];

			if (field->maxusage <= 0)
				continue;

			switch (field->usage[0].hid) {
164 165 166 167 168
			case THRUSTMASTER_USAGE_FF:
				if (field->report_count < 2) {
					warn("ignoring FF field with report_count < 2");
					continue;
				}
L
Linus Torvalds 已提交
169

170 171 172 173
				if (field->logical_maximum == field->logical_minimum) {
					warn("ignoring FF field with logical_maximum == logical_minimum");
					continue;
				}
L
Linus Torvalds 已提交
174

175 176 177 178
				if (tmff->report && tmff->report != report) {
					warn("ignoring FF field in other report");
					continue;
				}
L
Linus Torvalds 已提交
179

180 181 182 183 184 185 186 187 188 189 190 191 192
				if (tmff->ff_field && tmff->ff_field != field) {
					warn("ignoring duplicate FF field");
					continue;
				}

				tmff->report = report;
				tmff->ff_field = field;

				for (i = 0; i < ARRAY_SIZE(devices); i++) {
					if (input_dev->id.vendor == devices[i].idVendor &&
					    input_dev->id.product == devices[i].idProduct) {
						ff_bits = devices[i].ff;
						break;
L
Linus Torvalds 已提交
193
					}
194
				}
L
Linus Torvalds 已提交
195

196 197
				for (i = 0; ff_bits[i] >= 0; i++)
					set_bit(ff_bits[i], input_dev->ffbit);
L
Linus Torvalds 已提交
198

199
				break;
L
Linus Torvalds 已提交
200

201 202 203
			default:
				warn("ignoring unknown output usage %08x", field->usage[0].hid);
				continue;
L
Linus Torvalds 已提交
204 205 206 207
			}
		}
	}

208 209 210 211
	if (!tmff->report) {
		err("cant find FF field in output reports\n");
		error = -ENODEV;
		goto fail;
L
Linus Torvalds 已提交
212 213
	}

214 215 216
	error = input_ff_create_memless(input_dev, tmff, hid_tmff_play);
	if (error)
		goto fail;
L
Linus Torvalds 已提交
217

218
	info("Force feedback for ThrustMaster devices by Zinx Verituse <zinx@epicsol.org>");
L
Linus Torvalds 已提交
219
	return 0;
220 221 222 223

 fail:
	kfree(tmff);
	return error;
L
Linus Torvalds 已提交
224 225
}