xref: /linux/drivers/hid/hid-lg3ff.c (revision 4291ee305e9bb0699504a66f0e2b7aefcf0512a5)
174f292caSGary Stein /*
274f292caSGary Stein  *  Force feedback support for Logitech Flight System G940
374f292caSGary Stein  *
474f292caSGary Stein  *  Copyright (c) 2009 Gary Stein <LordCnidarian@gmail.com>
574f292caSGary Stein  */
674f292caSGary Stein 
774f292caSGary Stein /*
874f292caSGary Stein  * This program is free software; you can redistribute it and/or modify
974f292caSGary Stein  * it under the terms of the GNU General Public License as published by
1074f292caSGary Stein  * the Free Software Foundation; either version 2 of the License, or
1174f292caSGary Stein  * (at your option) any later version.
1274f292caSGary Stein  *
1374f292caSGary Stein  * This program is distributed in the hope that it will be useful,
1474f292caSGary Stein  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1574f292caSGary Stein  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1674f292caSGary Stein  * GNU General Public License for more details.
1774f292caSGary Stein  *
1874f292caSGary Stein  * You should have received a copy of the GNU General Public License
1974f292caSGary Stein  * along with this program; if not, write to the Free Software
2074f292caSGary Stein  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2174f292caSGary Stein  */
2274f292caSGary Stein 
2374f292caSGary Stein 
2474f292caSGary Stein #include <linux/input.h>
2574f292caSGary Stein #include <linux/usb.h>
2674f292caSGary Stein #include <linux/hid.h>
2774f292caSGary Stein 
2874f292caSGary Stein #include "usbhid/usbhid.h"
2974f292caSGary Stein #include "hid-lg.h"
3074f292caSGary Stein 
3174f292caSGary Stein /*
3274f292caSGary Stein  * G940 Theory of Operation (from experimentation)
3374f292caSGary Stein  *
3474f292caSGary Stein  * There are 63 fields (only 3 of them currently used)
3574f292caSGary Stein  * 0 - seems to be command field
3674f292caSGary Stein  * 1 - 30 deal with the x axis
3774f292caSGary Stein  * 31 -60 deal with the y axis
3874f292caSGary Stein  *
3974f292caSGary Stein  * Field 1 is x axis constant force
4074f292caSGary Stein  * Field 31 is y axis constant force
4174f292caSGary Stein  *
4274f292caSGary Stein  * other interesting fields 1,2,3,4 on x axis
4374f292caSGary Stein  * (same for 31,32,33,34 on y axis)
4474f292caSGary Stein  *
4574f292caSGary Stein  * 0 0 127 127 makes the joystick autocenter hard
4674f292caSGary Stein  *
4774f292caSGary Stein  * 127 0 127 127 makes the joystick loose on the right,
4874f292caSGary Stein  * but stops all movemnt left
4974f292caSGary Stein  *
5074f292caSGary Stein  * -127 0 -127 -127 makes the joystick loose on the left,
5174f292caSGary Stein  * but stops all movement right
5274f292caSGary Stein  *
5374f292caSGary Stein  * 0 0 -127 -127 makes the joystick rattle very hard
5474f292caSGary Stein  *
5574f292caSGary Stein  * I'm sure these are effects that I don't know enough about them
5674f292caSGary Stein  */
5774f292caSGary Stein 
5874f292caSGary Stein struct lg3ff_device {
5974f292caSGary Stein 	struct hid_report *report;
6074f292caSGary Stein };
6174f292caSGary Stein 
6274f292caSGary Stein static int hid_lg3ff_play(struct input_dev *dev, void *data,
6374f292caSGary Stein 			 struct ff_effect *effect)
6474f292caSGary Stein {
6574f292caSGary Stein 	struct hid_device *hid = input_get_drvdata(dev);
6674f292caSGary Stein 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
6774f292caSGary Stein 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
6874f292caSGary Stein 	int x, y;
6974f292caSGary Stein 
7074f292caSGary Stein /*
7174f292caSGary Stein  * Maxusage should always be 63 (maximum fields)
7274f292caSGary Stein  * likely a better way to ensure this data is clean
7374f292caSGary Stein  */
7474f292caSGary Stein 	memset(report->field[0]->value, 0, sizeof(__s32)*report->field[0]->maxusage);
7574f292caSGary Stein 
7674f292caSGary Stein 	switch (effect->type) {
7774f292caSGary Stein 	case FF_CONSTANT:
7874f292caSGary Stein /*
7974f292caSGary Stein  * Already clamped in ff_memless
8074f292caSGary Stein  * 0 is center (different then other logitech)
8174f292caSGary Stein  */
8274f292caSGary Stein 		x = effect->u.ramp.start_level;
8374f292caSGary Stein 		y = effect->u.ramp.end_level;
8474f292caSGary Stein 
8574f292caSGary Stein 		/* send command byte */
8674f292caSGary Stein 		report->field[0]->value[0] = 0x51;
8774f292caSGary Stein 
8874f292caSGary Stein /*
8974f292caSGary Stein  * Sign backwards from other Force3d pro
9074f292caSGary Stein  * which get recast here in two's complement 8 bits
9174f292caSGary Stein  */
9274f292caSGary Stein 		report->field[0]->value[1] = (unsigned char)(-x);
9374f292caSGary Stein 		report->field[0]->value[31] = (unsigned char)(-y);
9474f292caSGary Stein 
9574f292caSGary Stein 		usbhid_submit_report(hid, report, USB_DIR_OUT);
9674f292caSGary Stein 		break;
9774f292caSGary Stein 	}
9874f292caSGary Stein 	return 0;
9974f292caSGary Stein }
10074f292caSGary Stein static void hid_lg3ff_set_autocenter(struct input_dev *dev, u16 magnitude)
10174f292caSGary Stein {
10274f292caSGary Stein 	struct hid_device *hid = input_get_drvdata(dev);
10374f292caSGary Stein 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
10474f292caSGary Stein 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
10574f292caSGary Stein 
10674f292caSGary Stein /*
10774f292caSGary Stein  * Auto Centering probed from device
10874f292caSGary Stein  * NOTE: deadman's switch on G940 must be covered
10974f292caSGary Stein  * for effects to work
11074f292caSGary Stein  */
11174f292caSGary Stein 	report->field[0]->value[0] = 0x51;
11274f292caSGary Stein 	report->field[0]->value[1] = 0x00;
11374f292caSGary Stein 	report->field[0]->value[2] = 0x00;
11474f292caSGary Stein 	report->field[0]->value[3] = 0x7F;
11574f292caSGary Stein 	report->field[0]->value[4] = 0x7F;
11674f292caSGary Stein 	report->field[0]->value[31] = 0x00;
11774f292caSGary Stein 	report->field[0]->value[32] = 0x00;
11874f292caSGary Stein 	report->field[0]->value[33] = 0x7F;
11974f292caSGary Stein 	report->field[0]->value[34] = 0x7F;
12074f292caSGary Stein 
12174f292caSGary Stein 	usbhid_submit_report(hid, report, USB_DIR_OUT);
12274f292caSGary Stein }
12374f292caSGary Stein 
12474f292caSGary Stein 
12574f292caSGary Stein static const signed short ff3_joystick_ac[] = {
12674f292caSGary Stein 	FF_CONSTANT,
12774f292caSGary Stein 	FF_AUTOCENTER,
12874f292caSGary Stein 	-1
12974f292caSGary Stein };
13074f292caSGary Stein 
13174f292caSGary Stein int lg3ff_init(struct hid_device *hid)
13274f292caSGary Stein {
13374f292caSGary Stein 	struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
13474f292caSGary Stein 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
13574f292caSGary Stein 	struct input_dev *dev = hidinput->input;
13674f292caSGary Stein 	struct hid_report *report;
13774f292caSGary Stein 	struct hid_field *field;
13874f292caSGary Stein 	const signed short *ff_bits = ff3_joystick_ac;
13974f292caSGary Stein 	int error;
14074f292caSGary Stein 	int i;
14174f292caSGary Stein 
14274f292caSGary Stein 	/* Find the report to use */
14374f292caSGary Stein 	if (list_empty(report_list)) {
144*4291ee30SJoe Perches 		hid_err(hid, "No output report found\n");
14574f292caSGary Stein 		return -1;
14674f292caSGary Stein 	}
14774f292caSGary Stein 
14874f292caSGary Stein 	/* Check that the report looks ok */
14974f292caSGary Stein 	report = list_entry(report_list->next, struct hid_report, list);
15074f292caSGary Stein 	if (!report) {
151*4291ee30SJoe Perches 		hid_err(hid, "NULL output report\n");
15274f292caSGary Stein 		return -1;
15374f292caSGary Stein 	}
15474f292caSGary Stein 
15574f292caSGary Stein 	field = report->field[0];
15674f292caSGary Stein 	if (!field) {
157*4291ee30SJoe Perches 		hid_err(hid, "NULL field\n");
15874f292caSGary Stein 		return -1;
15974f292caSGary Stein 	}
16074f292caSGary Stein 
16174f292caSGary Stein 	/* Assume single fixed device G940 */
16274f292caSGary Stein 	for (i = 0; ff_bits[i] >= 0; i++)
16374f292caSGary Stein 		set_bit(ff_bits[i], dev->ffbit);
16474f292caSGary Stein 
16574f292caSGary Stein 	error = input_ff_create_memless(dev, NULL, hid_lg3ff_play);
16674f292caSGary Stein 	if (error)
16774f292caSGary Stein 		return error;
16874f292caSGary Stein 
16974f292caSGary Stein 	if (test_bit(FF_AUTOCENTER, dev->ffbit))
17074f292caSGary Stein 		dev->ff->set_autocenter = hid_lg3ff_set_autocenter;
17174f292caSGary Stein 
172*4291ee30SJoe Perches 	hid_info(hid, "Force feedback for Logitech Flight System G940 by Gary Stein <LordCnidarian@gmail.com>\n");
17374f292caSGary Stein 	return 0;
17474f292caSGary Stein }
17574f292caSGary Stein 
176