xref: /openbmc/linux/drivers/hid/hid-lg3ff.c (revision d9d4b1e4)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
274f292caSGary Stein /*
374f292caSGary Stein  *  Force feedback support for Logitech Flight System G940
474f292caSGary Stein  *
574f292caSGary Stein  *  Copyright (c) 2009 Gary Stein <LordCnidarian@gmail.com>
674f292caSGary Stein  */
774f292caSGary Stein 
874f292caSGary Stein /*
974f292caSGary Stein  */
1074f292caSGary Stein 
1174f292caSGary Stein 
1274f292caSGary Stein #include <linux/input.h>
1374f292caSGary Stein #include <linux/hid.h>
1474f292caSGary Stein 
1574f292caSGary Stein #include "hid-lg.h"
1674f292caSGary Stein 
1774f292caSGary Stein /*
1874f292caSGary Stein  * G940 Theory of Operation (from experimentation)
1974f292caSGary Stein  *
2074f292caSGary Stein  * There are 63 fields (only 3 of them currently used)
2174f292caSGary Stein  * 0 - seems to be command field
2274f292caSGary Stein  * 1 - 30 deal with the x axis
2374f292caSGary Stein  * 31 -60 deal with the y axis
2474f292caSGary Stein  *
2574f292caSGary Stein  * Field 1 is x axis constant force
2674f292caSGary Stein  * Field 31 is y axis constant force
2774f292caSGary Stein  *
2874f292caSGary Stein  * other interesting fields 1,2,3,4 on x axis
2974f292caSGary Stein  * (same for 31,32,33,34 on y axis)
3074f292caSGary Stein  *
3174f292caSGary Stein  * 0 0 127 127 makes the joystick autocenter hard
3274f292caSGary Stein  *
3374f292caSGary Stein  * 127 0 127 127 makes the joystick loose on the right,
3474f292caSGary Stein  * but stops all movemnt left
3574f292caSGary Stein  *
3674f292caSGary Stein  * -127 0 -127 -127 makes the joystick loose on the left,
3774f292caSGary Stein  * but stops all movement right
3874f292caSGary Stein  *
3974f292caSGary Stein  * 0 0 -127 -127 makes the joystick rattle very hard
4074f292caSGary Stein  *
4174f292caSGary Stein  * I'm sure these are effects that I don't know enough about them
4274f292caSGary Stein  */
4374f292caSGary Stein 
4474f292caSGary Stein struct lg3ff_device {
4574f292caSGary Stein 	struct hid_report *report;
4674f292caSGary Stein };
4774f292caSGary Stein 
hid_lg3ff_play(struct input_dev * dev,void * data,struct ff_effect * effect)4874f292caSGary Stein static int hid_lg3ff_play(struct input_dev *dev, void *data,
4974f292caSGary Stein 			 struct ff_effect *effect)
5074f292caSGary Stein {
5174f292caSGary Stein 	struct hid_device *hid = input_get_drvdata(dev);
5274f292caSGary Stein 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
5374f292caSGary Stein 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
5474f292caSGary Stein 	int x, y;
5574f292caSGary Stein 
5674f292caSGary Stein /*
570fb6bd06SKees Cook  * Available values in the field should always be 63, but we only use up to
580fb6bd06SKees Cook  * 35. Instead, clear the entire area, however big it is.
5974f292caSGary Stein  */
600fb6bd06SKees Cook 	memset(report->field[0]->value, 0,
610fb6bd06SKees Cook 	       sizeof(__s32) * report->field[0]->report_count);
6274f292caSGary Stein 
6374f292caSGary Stein 	switch (effect->type) {
6474f292caSGary Stein 	case FF_CONSTANT:
6574f292caSGary Stein /*
6674f292caSGary Stein  * Already clamped in ff_memless
6774f292caSGary Stein  * 0 is center (different then other logitech)
6874f292caSGary Stein  */
6974f292caSGary Stein 		x = effect->u.ramp.start_level;
7074f292caSGary Stein 		y = effect->u.ramp.end_level;
7174f292caSGary Stein 
7274f292caSGary Stein 		/* send command byte */
7374f292caSGary Stein 		report->field[0]->value[0] = 0x51;
7474f292caSGary Stein 
7574f292caSGary Stein /*
7674f292caSGary Stein  * Sign backwards from other Force3d pro
7774f292caSGary Stein  * which get recast here in two's complement 8 bits
7874f292caSGary Stein  */
7974f292caSGary Stein 		report->field[0]->value[1] = (unsigned char)(-x);
8074f292caSGary Stein 		report->field[0]->value[31] = (unsigned char)(-y);
8174f292caSGary Stein 
82d8814272SBenjamin Tissoires 		hid_hw_request(hid, report, HID_REQ_SET_REPORT);
8374f292caSGary Stein 		break;
8474f292caSGary Stein 	}
8574f292caSGary Stein 	return 0;
8674f292caSGary Stein }
hid_lg3ff_set_autocenter(struct input_dev * dev,u16 magnitude)8774f292caSGary Stein static void hid_lg3ff_set_autocenter(struct input_dev *dev, u16 magnitude)
8874f292caSGary Stein {
8974f292caSGary Stein 	struct hid_device *hid = input_get_drvdata(dev);
9074f292caSGary Stein 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
9174f292caSGary Stein 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
9274f292caSGary Stein 
9374f292caSGary Stein /*
9474f292caSGary Stein  * Auto Centering probed from device
9574f292caSGary Stein  * NOTE: deadman's switch on G940 must be covered
9674f292caSGary Stein  * for effects to work
9774f292caSGary Stein  */
9874f292caSGary Stein 	report->field[0]->value[0] = 0x51;
9974f292caSGary Stein 	report->field[0]->value[1] = 0x00;
10074f292caSGary Stein 	report->field[0]->value[2] = 0x00;
10174f292caSGary Stein 	report->field[0]->value[3] = 0x7F;
10274f292caSGary Stein 	report->field[0]->value[4] = 0x7F;
10374f292caSGary Stein 	report->field[0]->value[31] = 0x00;
10474f292caSGary Stein 	report->field[0]->value[32] = 0x00;
10574f292caSGary Stein 	report->field[0]->value[33] = 0x7F;
10674f292caSGary Stein 	report->field[0]->value[34] = 0x7F;
10774f292caSGary Stein 
108d8814272SBenjamin Tissoires 	hid_hw_request(hid, report, HID_REQ_SET_REPORT);
10974f292caSGary Stein }
11074f292caSGary Stein 
11174f292caSGary Stein 
11274f292caSGary Stein static const signed short ff3_joystick_ac[] = {
11374f292caSGary Stein 	FF_CONSTANT,
11474f292caSGary Stein 	FF_AUTOCENTER,
11574f292caSGary Stein 	-1
11674f292caSGary Stein };
11774f292caSGary Stein 
lg3ff_init(struct hid_device * hid)11874f292caSGary Stein int lg3ff_init(struct hid_device *hid)
11974f292caSGary Stein {
120d9d4b1e4SAlan Stern 	struct hid_input *hidinput;
121d9d4b1e4SAlan Stern 	struct input_dev *dev;
12274f292caSGary Stein 	const signed short *ff_bits = ff3_joystick_ac;
12374f292caSGary Stein 	int error;
12474f292caSGary Stein 	int i;
12574f292caSGary Stein 
126d9d4b1e4SAlan Stern 	if (list_empty(&hid->inputs)) {
127d9d4b1e4SAlan Stern 		hid_err(hid, "no inputs found\n");
128d9d4b1e4SAlan Stern 		return -ENODEV;
129d9d4b1e4SAlan Stern 	}
130d9d4b1e4SAlan Stern 	hidinput = list_entry(hid->inputs.next, struct hid_input, list);
131d9d4b1e4SAlan Stern 	dev = hidinput->input;
132d9d4b1e4SAlan Stern 
13374f292caSGary Stein 	/* Check that the report looks ok */
1340fb6bd06SKees Cook 	if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 35))
1350fb6bd06SKees Cook 		return -ENODEV;
13674f292caSGary Stein 
13774f292caSGary Stein 	/* Assume single fixed device G940 */
13874f292caSGary Stein 	for (i = 0; ff_bits[i] >= 0; i++)
13974f292caSGary Stein 		set_bit(ff_bits[i], dev->ffbit);
14074f292caSGary Stein 
14174f292caSGary Stein 	error = input_ff_create_memless(dev, NULL, hid_lg3ff_play);
14274f292caSGary Stein 	if (error)
14374f292caSGary Stein 		return error;
14474f292caSGary Stein 
14574f292caSGary Stein 	if (test_bit(FF_AUTOCENTER, dev->ffbit))
14674f292caSGary Stein 		dev->ff->set_autocenter = hid_lg3ff_set_autocenter;
14774f292caSGary Stein 
1484291ee30SJoe Perches 	hid_info(hid, "Force feedback for Logitech Flight System G940 by Gary Stein <LordCnidarian@gmail.com>\n");
14974f292caSGary Stein 	return 0;
15074f292caSGary Stein }
15174f292caSGary Stein 
152