xref: /openbmc/linux/drivers/hid/hid-tmff.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
210e41a71SJiri Slaby /*
310e41a71SJiri Slaby  * Force feedback support for various HID compliant devices by ThrustMaster:
410e41a71SJiri Slaby  *    ThrustMaster FireStorm Dual Power 2
510e41a71SJiri Slaby  * and possibly others whose device ids haven't been added.
610e41a71SJiri Slaby  *
710e41a71SJiri Slaby  *  Modified to support ThrustMaster devices by Zinx Verituse
810e41a71SJiri Slaby  *  on 2003-01-25 from the Logitech force feedback driver,
910e41a71SJiri Slaby  *  which is by Johann Deneux.
1010e41a71SJiri Slaby  *
1110e41a71SJiri Slaby  *  Copyright (c) 2003 Zinx Verituse <zinx@epicsol.org>
1210e41a71SJiri Slaby  *  Copyright (c) 2002 Johann Deneux
1310e41a71SJiri Slaby  */
1410e41a71SJiri Slaby 
1510e41a71SJiri Slaby /*
1610e41a71SJiri Slaby  */
1710e41a71SJiri Slaby 
1810e41a71SJiri Slaby #include <linux/hid.h>
1910e41a71SJiri Slaby #include <linux/input.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
218f86a2c3SPaul Gortmaker #include <linux/module.h>
2210e41a71SJiri Slaby 
2310e41a71SJiri Slaby #include "hid-ids.h"
2410e41a71SJiri Slaby 
2565f11c72SIlya Trukhanov #define THRUSTMASTER_DEVICE_ID_2_IN_1_DT	0xb320
2665f11c72SIlya Trukhanov 
2710e41a71SJiri Slaby static const signed short ff_rumble[] = {
2810e41a71SJiri Slaby 	FF_RUMBLE,
2910e41a71SJiri Slaby 	-1
3010e41a71SJiri Slaby };
3110e41a71SJiri Slaby 
3210e41a71SJiri Slaby static const signed short ff_joystick[] = {
3310e41a71SJiri Slaby 	FF_CONSTANT,
3410e41a71SJiri Slaby 	-1
3510e41a71SJiri Slaby };
3610e41a71SJiri Slaby 
370f6f4319SJiri Kosina #ifdef CONFIG_THRUSTMASTER_FF
380f6f4319SJiri Kosina 
390f6f4319SJiri Kosina /* Usages for thrustmaster devices I know about */
400f6f4319SJiri Kosina #define THRUSTMASTER_USAGE_FF	(HID_UP_GENDESK | 0xbb)
410f6f4319SJiri Kosina 
4210e41a71SJiri Slaby struct tmff_device {
4310e41a71SJiri Slaby 	struct hid_report *report;
4410e41a71SJiri Slaby 	struct hid_field *ff_field;
4510e41a71SJiri Slaby };
4610e41a71SJiri Slaby 
4710e41a71SJiri Slaby /* Changes values from 0 to 0xffff into values from minimum to maximum */
tmff_scale_u16(unsigned int in,int minimum,int maximum)4810e41a71SJiri Slaby static inline int tmff_scale_u16(unsigned int in, int minimum, int maximum)
4910e41a71SJiri Slaby {
5010e41a71SJiri Slaby 	int ret;
5110e41a71SJiri Slaby 
5210e41a71SJiri Slaby 	ret = (in * (maximum - minimum) / 0xffff) + minimum;
5310e41a71SJiri Slaby 	if (ret < minimum)
5410e41a71SJiri Slaby 		return minimum;
5510e41a71SJiri Slaby 	if (ret > maximum)
5610e41a71SJiri Slaby 		return maximum;
5710e41a71SJiri Slaby 	return ret;
5810e41a71SJiri Slaby }
5910e41a71SJiri Slaby 
6010e41a71SJiri Slaby /* Changes values from -0x80 to 0x7f into values from minimum to maximum */
tmff_scale_s8(int in,int minimum,int maximum)6110e41a71SJiri Slaby static inline int tmff_scale_s8(int in, int minimum, int maximum)
6210e41a71SJiri Slaby {
6310e41a71SJiri Slaby 	int ret;
6410e41a71SJiri Slaby 
6510e41a71SJiri Slaby 	ret = (((in + 0x80) * (maximum - minimum)) / 0xff) + minimum;
6610e41a71SJiri Slaby 	if (ret < minimum)
6710e41a71SJiri Slaby 		return minimum;
6810e41a71SJiri Slaby 	if (ret > maximum)
6910e41a71SJiri Slaby 		return maximum;
7010e41a71SJiri Slaby 	return ret;
7110e41a71SJiri Slaby }
7210e41a71SJiri Slaby 
tmff_play(struct input_dev * dev,void * data,struct ff_effect * effect)7310e41a71SJiri Slaby static int tmff_play(struct input_dev *dev, void *data,
7410e41a71SJiri Slaby 		struct ff_effect *effect)
7510e41a71SJiri Slaby {
7610e41a71SJiri Slaby 	struct hid_device *hid = input_get_drvdata(dev);
7710e41a71SJiri Slaby 	struct tmff_device *tmff = data;
7810e41a71SJiri Slaby 	struct hid_field *ff_field = tmff->ff_field;
7910e41a71SJiri Slaby 	int x, y;
8010e41a71SJiri Slaby 	int left, right;	/* Rumbling */
8110e41a71SJiri Slaby 
8210e41a71SJiri Slaby 	switch (effect->type) {
8310e41a71SJiri Slaby 	case FF_CONSTANT:
8410e41a71SJiri Slaby 		x = tmff_scale_s8(effect->u.ramp.start_level,
8510e41a71SJiri Slaby 					ff_field->logical_minimum,
8610e41a71SJiri Slaby 					ff_field->logical_maximum);
8710e41a71SJiri Slaby 		y = tmff_scale_s8(effect->u.ramp.end_level,
8810e41a71SJiri Slaby 					ff_field->logical_minimum,
8910e41a71SJiri Slaby 					ff_field->logical_maximum);
9010e41a71SJiri Slaby 
9110e41a71SJiri Slaby 		dbg_hid("(x, y)=(%04x, %04x)\n", x, y);
9210e41a71SJiri Slaby 		ff_field->value[0] = x;
9310e41a71SJiri Slaby 		ff_field->value[1] = y;
94d8814272SBenjamin Tissoires 		hid_hw_request(hid, tmff->report, HID_REQ_SET_REPORT);
9510e41a71SJiri Slaby 		break;
9610e41a71SJiri Slaby 
9710e41a71SJiri Slaby 	case FF_RUMBLE:
9810e41a71SJiri Slaby 		left = tmff_scale_u16(effect->u.rumble.weak_magnitude,
9910e41a71SJiri Slaby 					ff_field->logical_minimum,
10010e41a71SJiri Slaby 					ff_field->logical_maximum);
10110e41a71SJiri Slaby 		right = tmff_scale_u16(effect->u.rumble.strong_magnitude,
10210e41a71SJiri Slaby 					ff_field->logical_minimum,
10310e41a71SJiri Slaby 					ff_field->logical_maximum);
10410e41a71SJiri Slaby 
10565f11c72SIlya Trukhanov 		/* 2-in-1 strong motor is left */
106*415e701cSchiminghao 		if (hid->product == THRUSTMASTER_DEVICE_ID_2_IN_1_DT)
107*415e701cSchiminghao 			swap(left, right);
10865f11c72SIlya Trukhanov 
10910e41a71SJiri Slaby 		dbg_hid("(left,right)=(%08x, %08x)\n", left, right);
11010e41a71SJiri Slaby 		ff_field->value[0] = left;
11110e41a71SJiri Slaby 		ff_field->value[1] = right;
112d8814272SBenjamin Tissoires 		hid_hw_request(hid, tmff->report, HID_REQ_SET_REPORT);
11310e41a71SJiri Slaby 		break;
11410e41a71SJiri Slaby 	}
11510e41a71SJiri Slaby 	return 0;
11610e41a71SJiri Slaby }
11710e41a71SJiri Slaby 
tmff_init(struct hid_device * hid,const signed short * ff_bits)11810e41a71SJiri Slaby static int tmff_init(struct hid_device *hid, const signed short *ff_bits)
11910e41a71SJiri Slaby {
12010e41a71SJiri Slaby 	struct tmff_device *tmff;
12110e41a71SJiri Slaby 	struct hid_report *report;
12210e41a71SJiri Slaby 	struct list_head *report_list;
123d9d4b1e4SAlan Stern 	struct hid_input *hidinput;
124d9d4b1e4SAlan Stern 	struct input_dev *input_dev;
12510e41a71SJiri Slaby 	int error;
12610e41a71SJiri Slaby 	int i;
12710e41a71SJiri Slaby 
128d9d4b1e4SAlan Stern 	if (list_empty(&hid->inputs)) {
129d9d4b1e4SAlan Stern 		hid_err(hid, "no inputs found\n");
130d9d4b1e4SAlan Stern 		return -ENODEV;
131d9d4b1e4SAlan Stern 	}
132d9d4b1e4SAlan Stern 	hidinput = list_entry(hid->inputs.next, struct hid_input, list);
133d9d4b1e4SAlan Stern 	input_dev = hidinput->input;
134d9d4b1e4SAlan Stern 
13510e41a71SJiri Slaby 	tmff = kzalloc(sizeof(struct tmff_device), GFP_KERNEL);
13610e41a71SJiri Slaby 	if (!tmff)
13710e41a71SJiri Slaby 		return -ENOMEM;
13810e41a71SJiri Slaby 
13910e41a71SJiri Slaby 	/* Find the report to use */
14010e41a71SJiri Slaby 	report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
14110e41a71SJiri Slaby 	list_for_each_entry(report, report_list, list) {
14210e41a71SJiri Slaby 		int fieldnum;
14310e41a71SJiri Slaby 
14410e41a71SJiri Slaby 		for (fieldnum = 0; fieldnum < report->maxfield; ++fieldnum) {
14510e41a71SJiri Slaby 			struct hid_field *field = report->field[fieldnum];
14610e41a71SJiri Slaby 
14710e41a71SJiri Slaby 			if (field->maxusage <= 0)
14810e41a71SJiri Slaby 				continue;
14910e41a71SJiri Slaby 
15010e41a71SJiri Slaby 			switch (field->usage[0].hid) {
15110e41a71SJiri Slaby 			case THRUSTMASTER_USAGE_FF:
15210e41a71SJiri Slaby 				if (field->report_count < 2) {
1534291ee30SJoe Perches 					hid_warn(hid, "ignoring FF field with report_count < 2\n");
15410e41a71SJiri Slaby 					continue;
15510e41a71SJiri Slaby 				}
15610e41a71SJiri Slaby 
15710e41a71SJiri Slaby 				if (field->logical_maximum ==
15810e41a71SJiri Slaby 						field->logical_minimum) {
1594291ee30SJoe Perches 					hid_warn(hid, "ignoring FF field with logical_maximum == logical_minimum\n");
16010e41a71SJiri Slaby 					continue;
16110e41a71SJiri Slaby 				}
16210e41a71SJiri Slaby 
16310e41a71SJiri Slaby 				if (tmff->report && tmff->report != report) {
1644291ee30SJoe Perches 					hid_warn(hid, "ignoring FF field in other report\n");
16510e41a71SJiri Slaby 					continue;
16610e41a71SJiri Slaby 				}
16710e41a71SJiri Slaby 
16810e41a71SJiri Slaby 				if (tmff->ff_field && tmff->ff_field != field) {
1694291ee30SJoe Perches 					hid_warn(hid, "ignoring duplicate FF field\n");
17010e41a71SJiri Slaby 					continue;
17110e41a71SJiri Slaby 				}
17210e41a71SJiri Slaby 
17310e41a71SJiri Slaby 				tmff->report = report;
17410e41a71SJiri Slaby 				tmff->ff_field = field;
17510e41a71SJiri Slaby 
17610e41a71SJiri Slaby 				for (i = 0; ff_bits[i] >= 0; i++)
17710e41a71SJiri Slaby 					set_bit(ff_bits[i], input_dev->ffbit);
17810e41a71SJiri Slaby 
17910e41a71SJiri Slaby 				break;
18010e41a71SJiri Slaby 
18110e41a71SJiri Slaby 			default:
1824291ee30SJoe Perches 				hid_warn(hid, "ignoring unknown output usage %08x\n",
18310e41a71SJiri Slaby 					 field->usage[0].hid);
18410e41a71SJiri Slaby 				continue;
18510e41a71SJiri Slaby 			}
18610e41a71SJiri Slaby 		}
18710e41a71SJiri Slaby 	}
18810e41a71SJiri Slaby 
18910e41a71SJiri Slaby 	if (!tmff->report) {
1904291ee30SJoe Perches 		hid_err(hid, "can't find FF field in output reports\n");
19110e41a71SJiri Slaby 		error = -ENODEV;
19210e41a71SJiri Slaby 		goto fail;
19310e41a71SJiri Slaby 	}
19410e41a71SJiri Slaby 
19510e41a71SJiri Slaby 	error = input_ff_create_memless(input_dev, tmff, tmff_play);
19610e41a71SJiri Slaby 	if (error)
19710e41a71SJiri Slaby 		goto fail;
19810e41a71SJiri Slaby 
1994291ee30SJoe Perches 	hid_info(hid, "force feedback for ThrustMaster devices by Zinx Verituse <zinx@epicsol.org>\n");
20010e41a71SJiri Slaby 	return 0;
20110e41a71SJiri Slaby 
20210e41a71SJiri Slaby fail:
20310e41a71SJiri Slaby 	kfree(tmff);
20410e41a71SJiri Slaby 	return error;
20510e41a71SJiri Slaby }
2060f6f4319SJiri Kosina #else
tmff_init(struct hid_device * hid,const signed short * ff_bits)2070f6f4319SJiri Kosina static inline int tmff_init(struct hid_device *hid, const signed short *ff_bits)
2080f6f4319SJiri Kosina {
2090f6f4319SJiri Kosina 	return 0;
2100f6f4319SJiri Kosina }
2110f6f4319SJiri Kosina #endif
21210e41a71SJiri Slaby 
tm_probe(struct hid_device * hdev,const struct hid_device_id * id)21310e41a71SJiri Slaby static int tm_probe(struct hid_device *hdev, const struct hid_device_id *id)
21410e41a71SJiri Slaby {
21510e41a71SJiri Slaby 	int ret;
21610e41a71SJiri Slaby 
21710e41a71SJiri Slaby 	ret = hid_parse(hdev);
21810e41a71SJiri Slaby 	if (ret) {
2194291ee30SJoe Perches 		hid_err(hdev, "parse failed\n");
22010e41a71SJiri Slaby 		goto err;
22110e41a71SJiri Slaby 	}
22210e41a71SJiri Slaby 
22310e41a71SJiri Slaby 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
22410e41a71SJiri Slaby 	if (ret) {
2254291ee30SJoe Perches 		hid_err(hdev, "hw start failed\n");
22610e41a71SJiri Slaby 		goto err;
22710e41a71SJiri Slaby 	}
22810e41a71SJiri Slaby 
22910e41a71SJiri Slaby 	tmff_init(hdev, (void *)id->driver_data);
23010e41a71SJiri Slaby 
23110e41a71SJiri Slaby 	return 0;
23210e41a71SJiri Slaby err:
23310e41a71SJiri Slaby 	return ret;
23410e41a71SJiri Slaby }
23510e41a71SJiri Slaby 
23610e41a71SJiri Slaby static const struct hid_device_id tm_devices[] = {
23710e41a71SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb300),
23810e41a71SJiri Slaby 		.driver_data = (unsigned long)ff_rumble },
2397a84b133SRuben Aos Garralda 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb304),   /* FireStorm Dual Power 2 (and 3) */
2407a84b133SRuben Aos Garralda 		.driver_data = (unsigned long)ff_rumble },
24165f11c72SIlya Trukhanov 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, THRUSTMASTER_DEVICE_ID_2_IN_1_DT),   /* Dual Trigger 2-in-1 */
24265f11c72SIlya Trukhanov 		.driver_data = (unsigned long)ff_rumble },
2437a84b133SRuben Aos Garralda 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb323),   /* Dual Trigger 3-in-1 (PC Mode) */
2447a84b133SRuben Aos Garralda 		.driver_data = (unsigned long)ff_rumble },
2457a84b133SRuben Aos Garralda 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb324),   /* Dual Trigger 3-in-1 (PS3 Mode) */
24610e41a71SJiri Slaby 		.driver_data = (unsigned long)ff_rumble },
2471477edb4SViktor Chapliev 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb605),   /* NASCAR PRO FF2 Wheel */
2481477edb4SViktor Chapliev 		.driver_data = (unsigned long)ff_joystick },
24910e41a71SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb651),	/* FGT Rumble Force Wheel */
25010e41a71SJiri Slaby 		.driver_data = (unsigned long)ff_rumble },
2513ee8f0a2SMarkus Rathgeb 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb653),	/* RGT Force Feedback CLUTCH Raging Wheel */
2523ee8f0a2SMarkus Rathgeb 		.driver_data = (unsigned long)ff_joystick },
25310e41a71SJiri Slaby 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb654),	/* FGT Force Feedback Wheel */
25410e41a71SJiri Slaby 		.driver_data = (unsigned long)ff_joystick },
255d65c3768SSimon Wood 	{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb65a),	/* F430 Force Feedback Wheel */
256d65c3768SSimon Wood 		.driver_data = (unsigned long)ff_joystick },
25710e41a71SJiri Slaby 	{ }
25810e41a71SJiri Slaby };
25910e41a71SJiri Slaby MODULE_DEVICE_TABLE(hid, tm_devices);
26010e41a71SJiri Slaby 
26110e41a71SJiri Slaby static struct hid_driver tm_driver = {
26210e41a71SJiri Slaby 	.name = "thrustmaster",
26310e41a71SJiri Slaby 	.id_table = tm_devices,
26410e41a71SJiri Slaby 	.probe = tm_probe,
26510e41a71SJiri Slaby };
266f425458eSH Hartley Sweeten module_hid_driver(tm_driver);
26710e41a71SJiri Slaby 
26810e41a71SJiri Slaby MODULE_LICENSE("GPL");
269