xref: /linux/drivers/hid/hid-lg4ff.c (revision 7362cd22)
132c88cbcSSimon Wood /*
232c88cbcSSimon Wood  *  Force feedback support for Logitech Speed Force Wireless
332c88cbcSSimon Wood  *
432c88cbcSSimon Wood  *  http://wiibrew.org/wiki/Logitech_USB_steering_wheel
532c88cbcSSimon Wood  *
632c88cbcSSimon Wood  *  Copyright (c) 2010 Simon Wood <simon@mungewell.org>
732c88cbcSSimon Wood  */
832c88cbcSSimon Wood 
932c88cbcSSimon Wood /*
1032c88cbcSSimon Wood  * This program is free software; you can redistribute it and/or modify
1132c88cbcSSimon Wood  * it under the terms of the GNU General Public License as published by
1232c88cbcSSimon Wood  * the Free Software Foundation; either version 2 of the License, or
1332c88cbcSSimon Wood  * (at your option) any later version.
1432c88cbcSSimon Wood  *
1532c88cbcSSimon Wood  * This program is distributed in the hope that it will be useful,
1632c88cbcSSimon Wood  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1732c88cbcSSimon Wood  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1832c88cbcSSimon Wood  * GNU General Public License for more details.
1932c88cbcSSimon Wood  *
2032c88cbcSSimon Wood  * You should have received a copy of the GNU General Public License
2132c88cbcSSimon Wood  * along with this program; if not, write to the Free Software
2232c88cbcSSimon Wood  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2332c88cbcSSimon Wood  */
2432c88cbcSSimon Wood 
2532c88cbcSSimon Wood 
2632c88cbcSSimon Wood #include <linux/input.h>
2732c88cbcSSimon Wood #include <linux/usb.h>
2832c88cbcSSimon Wood #include <linux/hid.h>
2932c88cbcSSimon Wood 
3032c88cbcSSimon Wood #include "usbhid/usbhid.h"
3132c88cbcSSimon Wood #include "hid-lg.h"
32*7362cd22SMichal Malý #include "hid-ids.h"
3332c88cbcSSimon Wood 
34*7362cd22SMichal Malý static const signed short lg4ff_wheel_effects[] = {
3532c88cbcSSimon Wood 	FF_CONSTANT,
3632c88cbcSSimon Wood 	FF_AUTOCENTER,
3732c88cbcSSimon Wood 	-1
3832c88cbcSSimon Wood };
3932c88cbcSSimon Wood 
40*7362cd22SMichal Malý struct lg4ff_wheel {
41*7362cd22SMichal Malý 	const __u32 product_id;
42*7362cd22SMichal Malý 	const signed short *ff_effects;
43*7362cd22SMichal Malý 	const __u16 min_range;
44*7362cd22SMichal Malý 	const __u16 max_range;
45*7362cd22SMichal Malý };
46*7362cd22SMichal Malý 
47*7362cd22SMichal Malý static const struct lg4ff_wheel lg4ff_devices[] = {
48*7362cd22SMichal Malý 	{USB_DEVICE_ID_LOGITECH_WHEEL,       lg4ff_wheel_effects, 40, 270},
49*7362cd22SMichal Malý 	{USB_DEVICE_ID_LOGITECH_MOMO_WHEEL,  lg4ff_wheel_effects, 40, 270},
50*7362cd22SMichal Malý 	{USB_DEVICE_ID_LOGITECH_DFP_WHEEL,   lg4ff_wheel_effects, 40, 900},
51*7362cd22SMichal Malý 	{USB_DEVICE_ID_LOGITECH_G25_WHEEL,   lg4ff_wheel_effects, 40, 900},
52*7362cd22SMichal Malý 	{USB_DEVICE_ID_LOGITECH_DFGT_WHEEL,  lg4ff_wheel_effects, 40, 900},
53*7362cd22SMichal Malý 	{USB_DEVICE_ID_LOGITECH_G27_WHEEL,   lg4ff_wheel_effects, 40, 900},
54*7362cd22SMichal Malý 	{USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270},
55*7362cd22SMichal Malý 	{USB_DEVICE_ID_LOGITECH_WII_WHEEL,   lg4ff_wheel_effects, 40, 270}
56*7362cd22SMichal Malý };
57*7362cd22SMichal Malý 
5832c88cbcSSimon Wood static int hid_lg4ff_play(struct input_dev *dev, void *data,
5932c88cbcSSimon Wood 			 struct ff_effect *effect)
6032c88cbcSSimon Wood {
6132c88cbcSSimon Wood 	struct hid_device *hid = input_get_drvdata(dev);
6232c88cbcSSimon Wood 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
6332c88cbcSSimon Wood 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
6432c88cbcSSimon Wood 	int x;
6532c88cbcSSimon Wood 
6632c88cbcSSimon Wood #define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff
6732c88cbcSSimon Wood 
6832c88cbcSSimon Wood 	switch (effect->type) {
6932c88cbcSSimon Wood 	case FF_CONSTANT:
7032c88cbcSSimon Wood 		x = effect->u.ramp.start_level + 0x80;	/* 0x80 is no force */
7132c88cbcSSimon Wood 		CLAMP(x);
7232c88cbcSSimon Wood 		report->field[0]->value[0] = 0x11;	/* Slot 1 */
73*7362cd22SMichal Malý 		report->field[0]->value[1] = 0x08;
7432c88cbcSSimon Wood 		report->field[0]->value[2] = x;
75*7362cd22SMichal Malý 		report->field[0]->value[3] = 0x80;
7632c88cbcSSimon Wood 		report->field[0]->value[4] = 0x00;
77*7362cd22SMichal Malý 		report->field[0]->value[5] = 0x00;
7832c88cbcSSimon Wood 		report->field[0]->value[6] = 0x00;
7932c88cbcSSimon Wood 
8032c88cbcSSimon Wood 		usbhid_submit_report(hid, report, USB_DIR_OUT);
8132c88cbcSSimon Wood 		break;
8232c88cbcSSimon Wood 	}
8332c88cbcSSimon Wood 	return 0;
8432c88cbcSSimon Wood }
8532c88cbcSSimon Wood 
8632c88cbcSSimon Wood static void hid_lg4ff_set_autocenter(struct input_dev *dev, u16 magnitude)
8732c88cbcSSimon Wood {
8832c88cbcSSimon Wood 	struct hid_device *hid = input_get_drvdata(dev);
8932c88cbcSSimon Wood 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
9032c88cbcSSimon Wood 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
9132c88cbcSSimon Wood 
92*7362cd22SMichal Malý 	report->field[0]->value[0] = 0xfe;
93*7362cd22SMichal Malý 	report->field[0]->value[1] = 0x0d;
94*7362cd22SMichal Malý 	report->field[0]->value[2] = magnitude >> 13;
95*7362cd22SMichal Malý 	report->field[0]->value[3] = magnitude >> 13;
96*7362cd22SMichal Malý 	report->field[0]->value[4] = magnitude >> 8;
97*7362cd22SMichal Malý 	report->field[0]->value[5] = 0x00;
98*7362cd22SMichal Malý 	report->field[0]->value[6] = 0x00;
9932c88cbcSSimon Wood 
10032c88cbcSSimon Wood 	usbhid_submit_report(hid, report, USB_DIR_OUT);
10132c88cbcSSimon Wood }
10232c88cbcSSimon Wood 
10332c88cbcSSimon Wood 
10432c88cbcSSimon Wood int lg4ff_init(struct hid_device *hid)
10532c88cbcSSimon Wood {
10632c88cbcSSimon Wood 	struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
10732c88cbcSSimon Wood 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
10832c88cbcSSimon Wood 	struct input_dev *dev = hidinput->input;
10932c88cbcSSimon Wood 	struct hid_report *report;
11032c88cbcSSimon Wood 	struct hid_field *field;
111*7362cd22SMichal Malý 	int error, i, j;
11232c88cbcSSimon Wood 
11332c88cbcSSimon Wood 	/* Find the report to use */
11432c88cbcSSimon Wood 	if (list_empty(report_list)) {
1154291ee30SJoe Perches 		hid_err(hid, "No output report found\n");
11632c88cbcSSimon Wood 		return -1;
11732c88cbcSSimon Wood 	}
11832c88cbcSSimon Wood 
11932c88cbcSSimon Wood 	/* Check that the report looks ok */
12032c88cbcSSimon Wood 	report = list_entry(report_list->next, struct hid_report, list);
12132c88cbcSSimon Wood 	if (!report) {
1224291ee30SJoe Perches 		hid_err(hid, "NULL output report\n");
12332c88cbcSSimon Wood 		return -1;
12432c88cbcSSimon Wood 	}
12532c88cbcSSimon Wood 
12632c88cbcSSimon Wood 	field = report->field[0];
12732c88cbcSSimon Wood 	if (!field) {
1284291ee30SJoe Perches 		hid_err(hid, "NULL field\n");
12932c88cbcSSimon Wood 		return -1;
13032c88cbcSSimon Wood 	}
13132c88cbcSSimon Wood 
132*7362cd22SMichal Malý 	/* Check what wheel has been connected */
133*7362cd22SMichal Malý 	for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
134*7362cd22SMichal Malý 		if (hid->product == lg4ff_devices[i].product_id) {
135*7362cd22SMichal Malý 			dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
136*7362cd22SMichal Malý 			break;
137*7362cd22SMichal Malý 		}
138*7362cd22SMichal Malý 	}
139*7362cd22SMichal Malý 
140*7362cd22SMichal Malý 	if (i == ARRAY_SIZE(lg4ff_devices)) {
141*7362cd22SMichal Malý 		hid_err(hid, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to"
142*7362cd22SMichal Malý 			     "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n");
143*7362cd22SMichal Malý 		return -1;
144*7362cd22SMichal Malý 	}
145*7362cd22SMichal Malý 
146*7362cd22SMichal Malý 	/* Set supported force feedback capabilities */
147*7362cd22SMichal Malý 	for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
148*7362cd22SMichal Malý 		set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
14932c88cbcSSimon Wood 
15032c88cbcSSimon Wood 	error = input_ff_create_memless(dev, NULL, hid_lg4ff_play);
15132c88cbcSSimon Wood 
15232c88cbcSSimon Wood 	if (error)
15332c88cbcSSimon Wood 		return error;
15432c88cbcSSimon Wood 
15532c88cbcSSimon Wood 	if (test_bit(FF_AUTOCENTER, dev->ffbit))
15632c88cbcSSimon Wood 		dev->ff->set_autocenter = hid_lg4ff_set_autocenter;
15732c88cbcSSimon Wood 
1584291ee30SJoe Perches 	hid_info(hid, "Force feedback for Logitech Speed Force Wireless by Simon Wood <simon@mungewell.org>\n");
15932c88cbcSSimon Wood 	return 0;
16032c88cbcSSimon Wood }
16132c88cbcSSimon Wood 
162