xref: /linux/drivers/hid/hid-lg3ff.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
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 
hid_lg3ff_play(struct input_dev * dev,void * data,struct ff_effect * effect)4474f292caSGary Stein static int hid_lg3ff_play(struct input_dev *dev, void *data,
4574f292caSGary Stein 			 struct ff_effect *effect)
4674f292caSGary Stein {
4774f292caSGary Stein 	struct hid_device *hid = input_get_drvdata(dev);
4874f292caSGary Stein 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
4974f292caSGary Stein 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
5074f292caSGary Stein 	int x, y;
5174f292caSGary Stein 
5274f292caSGary Stein /*
530fb6bd06SKees Cook  * Available values in the field should always be 63, but we only use up to
540fb6bd06SKees Cook  * 35. Instead, clear the entire area, however big it is.
5574f292caSGary Stein  */
560fb6bd06SKees Cook 	memset(report->field[0]->value, 0,
570fb6bd06SKees Cook 	       sizeof(__s32) * report->field[0]->report_count);
5874f292caSGary Stein 
5974f292caSGary Stein 	switch (effect->type) {
6074f292caSGary Stein 	case FF_CONSTANT:
6174f292caSGary Stein /*
6274f292caSGary Stein  * Already clamped in ff_memless
6374f292caSGary Stein  * 0 is center (different then other logitech)
6474f292caSGary Stein  */
6574f292caSGary Stein 		x = effect->u.ramp.start_level;
6674f292caSGary Stein 		y = effect->u.ramp.end_level;
6774f292caSGary Stein 
6874f292caSGary Stein 		/* send command byte */
6974f292caSGary Stein 		report->field[0]->value[0] = 0x51;
7074f292caSGary Stein 
7174f292caSGary Stein /*
7274f292caSGary Stein  * Sign backwards from other Force3d pro
7374f292caSGary Stein  * which get recast here in two's complement 8 bits
7474f292caSGary Stein  */
7574f292caSGary Stein 		report->field[0]->value[1] = (unsigned char)(-x);
7674f292caSGary Stein 		report->field[0]->value[31] = (unsigned char)(-y);
7774f292caSGary Stein 
78d8814272SBenjamin Tissoires 		hid_hw_request(hid, report, HID_REQ_SET_REPORT);
7974f292caSGary Stein 		break;
8074f292caSGary Stein 	}
8174f292caSGary Stein 	return 0;
8274f292caSGary Stein }
hid_lg3ff_set_autocenter(struct input_dev * dev,u16 magnitude)8374f292caSGary Stein static void hid_lg3ff_set_autocenter(struct input_dev *dev, u16 magnitude)
8474f292caSGary Stein {
8574f292caSGary Stein 	struct hid_device *hid = input_get_drvdata(dev);
8674f292caSGary Stein 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
8774f292caSGary Stein 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
8874f292caSGary Stein 
8974f292caSGary Stein /*
9074f292caSGary Stein  * Auto Centering probed from device
9174f292caSGary Stein  * NOTE: deadman's switch on G940 must be covered
9274f292caSGary Stein  * for effects to work
9374f292caSGary Stein  */
9474f292caSGary Stein 	report->field[0]->value[0] = 0x51;
9574f292caSGary Stein 	report->field[0]->value[1] = 0x00;
9674f292caSGary Stein 	report->field[0]->value[2] = 0x00;
9774f292caSGary Stein 	report->field[0]->value[3] = 0x7F;
9874f292caSGary Stein 	report->field[0]->value[4] = 0x7F;
9974f292caSGary Stein 	report->field[0]->value[31] = 0x00;
10074f292caSGary Stein 	report->field[0]->value[32] = 0x00;
10174f292caSGary Stein 	report->field[0]->value[33] = 0x7F;
10274f292caSGary Stein 	report->field[0]->value[34] = 0x7F;
10374f292caSGary Stein 
104d8814272SBenjamin Tissoires 	hid_hw_request(hid, report, HID_REQ_SET_REPORT);
10574f292caSGary Stein }
10674f292caSGary Stein 
10774f292caSGary Stein 
10874f292caSGary Stein static const signed short ff3_joystick_ac[] = {
10974f292caSGary Stein 	FF_CONSTANT,
11074f292caSGary Stein 	FF_AUTOCENTER,
11174f292caSGary Stein 	-1
11274f292caSGary Stein };
11374f292caSGary Stein 
lg3ff_init(struct hid_device * hid)11474f292caSGary Stein int lg3ff_init(struct hid_device *hid)
11574f292caSGary Stein {
116*d9d4b1e4SAlan Stern 	struct hid_input *hidinput;
117*d9d4b1e4SAlan Stern 	struct input_dev *dev;
11874f292caSGary Stein 	const signed short *ff_bits = ff3_joystick_ac;
11974f292caSGary Stein 	int error;
12074f292caSGary Stein 	int i;
12174f292caSGary Stein 
122*d9d4b1e4SAlan Stern 	if (list_empty(&hid->inputs)) {
123*d9d4b1e4SAlan Stern 		hid_err(hid, "no inputs found\n");
124*d9d4b1e4SAlan Stern 		return -ENODEV;
125*d9d4b1e4SAlan Stern 	}
126*d9d4b1e4SAlan Stern 	hidinput = list_entry(hid->inputs.next, struct hid_input, list);
127*d9d4b1e4SAlan Stern 	dev = hidinput->input;
128*d9d4b1e4SAlan Stern 
12974f292caSGary Stein 	/* Check that the report looks ok */
1300fb6bd06SKees Cook 	if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 35))
1310fb6bd06SKees Cook 		return -ENODEV;
13274f292caSGary Stein 
13374f292caSGary Stein 	/* Assume single fixed device G940 */
13474f292caSGary Stein 	for (i = 0; ff_bits[i] >= 0; i++)
13574f292caSGary Stein 		set_bit(ff_bits[i], dev->ffbit);
13674f292caSGary Stein 
13774f292caSGary Stein 	error = input_ff_create_memless(dev, NULL, hid_lg3ff_play);
13874f292caSGary Stein 	if (error)
13974f292caSGary Stein 		return error;
14074f292caSGary Stein 
14174f292caSGary Stein 	if (test_bit(FF_AUTOCENTER, dev->ffbit))
14274f292caSGary Stein 		dev->ff->set_autocenter = hid_lg3ff_set_autocenter;
14374f292caSGary Stein 
1444291ee30SJoe Perches 	hid_info(hid, "Force feedback for Logitech Flight System G940 by Gary Stein <LordCnidarian@gmail.com>\n");
14574f292caSGary Stein 	return 0;
14674f292caSGary Stein }
14774f292caSGary Stein 
148