xref: /openbmc/linux/drivers/hid/hid-lg3ff.c (revision 0fb6bd06)
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 /*
690fb6bd06SKees Cook  * Available values in the field should always be 63, but we only use up to
700fb6bd06SKees Cook  * 35. Instead, clear the entire area, however big it is.
7174f292caSGary Stein  */
720fb6bd06SKees Cook 	memset(report->field[0]->value, 0,
730fb6bd06SKees Cook 	       sizeof(__s32) * report->field[0]->report_count);
7474f292caSGary Stein 
7574f292caSGary Stein 	switch (effect->type) {
7674f292caSGary Stein 	case FF_CONSTANT:
7774f292caSGary Stein /*
7874f292caSGary Stein  * Already clamped in ff_memless
7974f292caSGary Stein  * 0 is center (different then other logitech)
8074f292caSGary Stein  */
8174f292caSGary Stein 		x = effect->u.ramp.start_level;
8274f292caSGary Stein 		y = effect->u.ramp.end_level;
8374f292caSGary Stein 
8474f292caSGary Stein 		/* send command byte */
8574f292caSGary Stein 		report->field[0]->value[0] = 0x51;
8674f292caSGary Stein 
8774f292caSGary Stein /*
8874f292caSGary Stein  * Sign backwards from other Force3d pro
8974f292caSGary Stein  * which get recast here in two's complement 8 bits
9074f292caSGary Stein  */
9174f292caSGary Stein 		report->field[0]->value[1] = (unsigned char)(-x);
9274f292caSGary Stein 		report->field[0]->value[31] = (unsigned char)(-y);
9374f292caSGary Stein 
94d8814272SBenjamin Tissoires 		hid_hw_request(hid, report, HID_REQ_SET_REPORT);
9574f292caSGary Stein 		break;
9674f292caSGary Stein 	}
9774f292caSGary Stein 	return 0;
9874f292caSGary Stein }
9974f292caSGary Stein static void hid_lg3ff_set_autocenter(struct input_dev *dev, u16 magnitude)
10074f292caSGary Stein {
10174f292caSGary Stein 	struct hid_device *hid = input_get_drvdata(dev);
10274f292caSGary Stein 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
10374f292caSGary Stein 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
10474f292caSGary Stein 
10574f292caSGary Stein /*
10674f292caSGary Stein  * Auto Centering probed from device
10774f292caSGary Stein  * NOTE: deadman's switch on G940 must be covered
10874f292caSGary Stein  * for effects to work
10974f292caSGary Stein  */
11074f292caSGary Stein 	report->field[0]->value[0] = 0x51;
11174f292caSGary Stein 	report->field[0]->value[1] = 0x00;
11274f292caSGary Stein 	report->field[0]->value[2] = 0x00;
11374f292caSGary Stein 	report->field[0]->value[3] = 0x7F;
11474f292caSGary Stein 	report->field[0]->value[4] = 0x7F;
11574f292caSGary Stein 	report->field[0]->value[31] = 0x00;
11674f292caSGary Stein 	report->field[0]->value[32] = 0x00;
11774f292caSGary Stein 	report->field[0]->value[33] = 0x7F;
11874f292caSGary Stein 	report->field[0]->value[34] = 0x7F;
11974f292caSGary Stein 
120d8814272SBenjamin Tissoires 	hid_hw_request(hid, report, HID_REQ_SET_REPORT);
12174f292caSGary Stein }
12274f292caSGary Stein 
12374f292caSGary Stein 
12474f292caSGary Stein static const signed short ff3_joystick_ac[] = {
12574f292caSGary Stein 	FF_CONSTANT,
12674f292caSGary Stein 	FF_AUTOCENTER,
12774f292caSGary Stein 	-1
12874f292caSGary Stein };
12974f292caSGary Stein 
13074f292caSGary Stein int lg3ff_init(struct hid_device *hid)
13174f292caSGary Stein {
13274f292caSGary Stein 	struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
13374f292caSGary Stein 	struct input_dev *dev = hidinput->input;
13474f292caSGary Stein 	const signed short *ff_bits = ff3_joystick_ac;
13574f292caSGary Stein 	int error;
13674f292caSGary Stein 	int i;
13774f292caSGary Stein 
13874f292caSGary Stein 	/* Check that the report looks ok */
1390fb6bd06SKees Cook 	if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 35))
1400fb6bd06SKees Cook 		return -ENODEV;
14174f292caSGary Stein 
14274f292caSGary Stein 	/* Assume single fixed device G940 */
14374f292caSGary Stein 	for (i = 0; ff_bits[i] >= 0; i++)
14474f292caSGary Stein 		set_bit(ff_bits[i], dev->ffbit);
14574f292caSGary Stein 
14674f292caSGary Stein 	error = input_ff_create_memless(dev, NULL, hid_lg3ff_play);
14774f292caSGary Stein 	if (error)
14874f292caSGary Stein 		return error;
14974f292caSGary Stein 
15074f292caSGary Stein 	if (test_bit(FF_AUTOCENTER, dev->ffbit))
15174f292caSGary Stein 		dev->ff->set_autocenter = hid_lg3ff_set_autocenter;
15274f292caSGary Stein 
1534291ee30SJoe Perches 	hid_info(hid, "Force feedback for Logitech Flight System G940 by Gary Stein <LordCnidarian@gmail.com>\n");
15474f292caSGary Stein 	return 0;
15574f292caSGary Stein }
15674f292caSGary Stein 
157