xref: /openbmc/linux/drivers/hid/hid-lg3ff.c (revision d8814272)
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/hid.h>
2674f292caSGary Stein 
2774f292caSGary Stein #include "hid-lg.h"
2874f292caSGary Stein 
2974f292caSGary Stein /*
3074f292caSGary Stein  * G940 Theory of Operation (from experimentation)
3174f292caSGary Stein  *
3274f292caSGary Stein  * There are 63 fields (only 3 of them currently used)
3374f292caSGary Stein  * 0 - seems to be command field
3474f292caSGary Stein  * 1 - 30 deal with the x axis
3574f292caSGary Stein  * 31 -60 deal with the y axis
3674f292caSGary Stein  *
3774f292caSGary Stein  * Field 1 is x axis constant force
3874f292caSGary Stein  * Field 31 is y axis constant force
3974f292caSGary Stein  *
4074f292caSGary Stein  * other interesting fields 1,2,3,4 on x axis
4174f292caSGary Stein  * (same for 31,32,33,34 on y axis)
4274f292caSGary Stein  *
4374f292caSGary Stein  * 0 0 127 127 makes the joystick autocenter hard
4474f292caSGary Stein  *
4574f292caSGary Stein  * 127 0 127 127 makes the joystick loose on the right,
4674f292caSGary Stein  * but stops all movemnt left
4774f292caSGary Stein  *
4874f292caSGary Stein  * -127 0 -127 -127 makes the joystick loose on the left,
4974f292caSGary Stein  * but stops all movement right
5074f292caSGary Stein  *
5174f292caSGary Stein  * 0 0 -127 -127 makes the joystick rattle very hard
5274f292caSGary Stein  *
5374f292caSGary Stein  * I'm sure these are effects that I don't know enough about them
5474f292caSGary Stein  */
5574f292caSGary Stein 
5674f292caSGary Stein struct lg3ff_device {
5774f292caSGary Stein 	struct hid_report *report;
5874f292caSGary Stein };
5974f292caSGary Stein 
6074f292caSGary Stein static int hid_lg3ff_play(struct input_dev *dev, void *data,
6174f292caSGary Stein 			 struct ff_effect *effect)
6274f292caSGary Stein {
6374f292caSGary Stein 	struct hid_device *hid = input_get_drvdata(dev);
6474f292caSGary Stein 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
6574f292caSGary Stein 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
6674f292caSGary Stein 	int x, y;
6774f292caSGary Stein 
6874f292caSGary Stein /*
6974f292caSGary Stein  * Maxusage should always be 63 (maximum fields)
7074f292caSGary Stein  * likely a better way to ensure this data is clean
7174f292caSGary Stein  */
7274f292caSGary Stein 	memset(report->field[0]->value, 0, sizeof(__s32)*report->field[0]->maxusage);
7374f292caSGary Stein 
7474f292caSGary Stein 	switch (effect->type) {
7574f292caSGary Stein 	case FF_CONSTANT:
7674f292caSGary Stein /*
7774f292caSGary Stein  * Already clamped in ff_memless
7874f292caSGary Stein  * 0 is center (different then other logitech)
7974f292caSGary Stein  */
8074f292caSGary Stein 		x = effect->u.ramp.start_level;
8174f292caSGary Stein 		y = effect->u.ramp.end_level;
8274f292caSGary Stein 
8374f292caSGary Stein 		/* send command byte */
8474f292caSGary Stein 		report->field[0]->value[0] = 0x51;
8574f292caSGary Stein 
8674f292caSGary Stein /*
8774f292caSGary Stein  * Sign backwards from other Force3d pro
8874f292caSGary Stein  * which get recast here in two's complement 8 bits
8974f292caSGary Stein  */
9074f292caSGary Stein 		report->field[0]->value[1] = (unsigned char)(-x);
9174f292caSGary Stein 		report->field[0]->value[31] = (unsigned char)(-y);
9274f292caSGary Stein 
93d8814272SBenjamin Tissoires 		hid_hw_request(hid, report, HID_REQ_SET_REPORT);
9474f292caSGary Stein 		break;
9574f292caSGary Stein 	}
9674f292caSGary Stein 	return 0;
9774f292caSGary Stein }
9874f292caSGary Stein static void hid_lg3ff_set_autocenter(struct input_dev *dev, u16 magnitude)
9974f292caSGary Stein {
10074f292caSGary Stein 	struct hid_device *hid = input_get_drvdata(dev);
10174f292caSGary Stein 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
10274f292caSGary Stein 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
10374f292caSGary Stein 
10474f292caSGary Stein /*
10574f292caSGary Stein  * Auto Centering probed from device
10674f292caSGary Stein  * NOTE: deadman's switch on G940 must be covered
10774f292caSGary Stein  * for effects to work
10874f292caSGary Stein  */
10974f292caSGary Stein 	report->field[0]->value[0] = 0x51;
11074f292caSGary Stein 	report->field[0]->value[1] = 0x00;
11174f292caSGary Stein 	report->field[0]->value[2] = 0x00;
11274f292caSGary Stein 	report->field[0]->value[3] = 0x7F;
11374f292caSGary Stein 	report->field[0]->value[4] = 0x7F;
11474f292caSGary Stein 	report->field[0]->value[31] = 0x00;
11574f292caSGary Stein 	report->field[0]->value[32] = 0x00;
11674f292caSGary Stein 	report->field[0]->value[33] = 0x7F;
11774f292caSGary Stein 	report->field[0]->value[34] = 0x7F;
11874f292caSGary Stein 
119d8814272SBenjamin Tissoires 	hid_hw_request(hid, report, HID_REQ_SET_REPORT);
12074f292caSGary Stein }
12174f292caSGary Stein 
12274f292caSGary Stein 
12374f292caSGary Stein static const signed short ff3_joystick_ac[] = {
12474f292caSGary Stein 	FF_CONSTANT,
12574f292caSGary Stein 	FF_AUTOCENTER,
12674f292caSGary Stein 	-1
12774f292caSGary Stein };
12874f292caSGary Stein 
12974f292caSGary Stein int lg3ff_init(struct hid_device *hid)
13074f292caSGary Stein {
13174f292caSGary Stein 	struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
13274f292caSGary Stein 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
13374f292caSGary Stein 	struct input_dev *dev = hidinput->input;
13474f292caSGary Stein 	struct hid_report *report;
13574f292caSGary Stein 	struct hid_field *field;
13674f292caSGary Stein 	const signed short *ff_bits = ff3_joystick_ac;
13774f292caSGary Stein 	int error;
13874f292caSGary Stein 	int i;
13974f292caSGary Stein 
14074f292caSGary Stein 	/* Find the report to use */
14174f292caSGary Stein 	if (list_empty(report_list)) {
1424291ee30SJoe Perches 		hid_err(hid, "No output report found\n");
14374f292caSGary Stein 		return -1;
14474f292caSGary Stein 	}
14574f292caSGary Stein 
14674f292caSGary Stein 	/* Check that the report looks ok */
14774f292caSGary Stein 	report = list_entry(report_list->next, struct hid_report, list);
14874f292caSGary Stein 	if (!report) {
1494291ee30SJoe Perches 		hid_err(hid, "NULL output report\n");
15074f292caSGary Stein 		return -1;
15174f292caSGary Stein 	}
15274f292caSGary Stein 
15374f292caSGary Stein 	field = report->field[0];
15474f292caSGary Stein 	if (!field) {
1554291ee30SJoe Perches 		hid_err(hid, "NULL field\n");
15674f292caSGary Stein 		return -1;
15774f292caSGary Stein 	}
15874f292caSGary Stein 
15974f292caSGary Stein 	/* Assume single fixed device G940 */
16074f292caSGary Stein 	for (i = 0; ff_bits[i] >= 0; i++)
16174f292caSGary Stein 		set_bit(ff_bits[i], dev->ffbit);
16274f292caSGary Stein 
16374f292caSGary Stein 	error = input_ff_create_memless(dev, NULL, hid_lg3ff_play);
16474f292caSGary Stein 	if (error)
16574f292caSGary Stein 		return error;
16674f292caSGary Stein 
16774f292caSGary Stein 	if (test_bit(FF_AUTOCENTER, dev->ffbit))
16874f292caSGary Stein 		dev->ff->set_autocenter = hid_lg3ff_set_autocenter;
16974f292caSGary Stein 
1704291ee30SJoe Perches 	hid_info(hid, "Force feedback for Logitech Flight System G940 by Gary Stein <LordCnidarian@gmail.com>\n");
17174f292caSGary Stein 	return 0;
17274f292caSGary Stein }
17374f292caSGary Stein 
174