xref: /openbmc/linux/drivers/hid/hid-lg4ff.c (revision d881427253da011495f4193663d809d0e9dfa215)
132c88cbcSSimon Wood /*
264013800SSimon Wood  *  Force feedback support for Logitech Gaming Wheels
332c88cbcSSimon Wood  *
464013800SSimon Wood  *  Including G27, G25, DFP, DFGT, FFEX, Momo, Momo2 &
564013800SSimon Wood  *  Speed Force Wireless (WiiWheel)
632c88cbcSSimon Wood  *
732c88cbcSSimon Wood  *  Copyright (c) 2010 Simon Wood <simon@mungewell.org>
832c88cbcSSimon Wood  */
932c88cbcSSimon Wood 
1032c88cbcSSimon Wood /*
1132c88cbcSSimon Wood  * This program is free software; you can redistribute it and/or modify
1232c88cbcSSimon Wood  * it under the terms of the GNU General Public License as published by
1332c88cbcSSimon Wood  * the Free Software Foundation; either version 2 of the License, or
1432c88cbcSSimon Wood  * (at your option) any later version.
1532c88cbcSSimon Wood  *
1632c88cbcSSimon Wood  * This program is distributed in the hope that it will be useful,
1732c88cbcSSimon Wood  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1832c88cbcSSimon Wood  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1932c88cbcSSimon Wood  * GNU General Public License for more details.
2032c88cbcSSimon Wood  *
2132c88cbcSSimon Wood  * You should have received a copy of the GNU General Public License
2232c88cbcSSimon Wood  * along with this program; if not, write to the Free Software
2332c88cbcSSimon Wood  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2432c88cbcSSimon Wood  */
2532c88cbcSSimon Wood 
2632c88cbcSSimon Wood 
2732c88cbcSSimon Wood #include <linux/input.h>
2832c88cbcSSimon Wood #include <linux/usb.h>
2932c88cbcSSimon Wood #include <linux/hid.h>
3032c88cbcSSimon Wood 
3132c88cbcSSimon Wood #include "usbhid/usbhid.h"
3232c88cbcSSimon Wood #include "hid-lg.h"
337362cd22SMichal Malý #include "hid-ids.h"
3432c88cbcSSimon Wood 
3596440c8aSMichal Malý #define DFGT_REV_MAJ 0x13
3696440c8aSMichal Malý #define DFGT_REV_MIN 0x22
3796440c8aSMichal Malý #define DFP_REV_MAJ 0x11
3896440c8aSMichal Malý #define DFP_REV_MIN 0x06
3996440c8aSMichal Malý #define FFEX_REV_MAJ 0x21
4096440c8aSMichal Malý #define FFEX_REV_MIN 0x00
4196440c8aSMichal Malý #define G25_REV_MAJ 0x12
4296440c8aSMichal Malý #define G25_REV_MIN 0x22
4396440c8aSMichal Malý #define G27_REV_MAJ 0x12
4496440c8aSMichal Malý #define G27_REV_MIN 0x38
4596440c8aSMichal Malý 
4630bb75d7SMichal Malý #define to_hid_device(pdev) container_of(pdev, struct hid_device, dev)
4730bb75d7SMichal Malý 
4830bb75d7SMichal Malý static void hid_lg4ff_set_range_dfp(struct hid_device *hid, u16 range);
4930bb75d7SMichal Malý static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range);
5030bb75d7SMichal Malý static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf);
5130bb75d7SMichal Malý static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count);
5230bb75d7SMichal Malý 
5330bb75d7SMichal Malý static DEVICE_ATTR(range, S_IRWXU | S_IRWXG | S_IRWXO, lg4ff_range_show, lg4ff_range_store);
5430bb75d7SMichal Malý 
5530bb75d7SMichal Malý struct lg4ff_device_entry {
562b24a960SMichal Malý 	__u32 product_id;
5730bb75d7SMichal Malý 	__u16 range;
5830bb75d7SMichal Malý 	__u16 min_range;
5930bb75d7SMichal Malý 	__u16 max_range;
6022bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS
6122bcefdcSSimon Wood 	__u8  led_state;
6222bcefdcSSimon Wood 	struct led_classdev *led[5];
6322bcefdcSSimon Wood #endif
6430bb75d7SMichal Malý 	struct list_head list;
6530bb75d7SMichal Malý 	void (*set_range)(struct hid_device *hid, u16 range);
6630bb75d7SMichal Malý };
6730bb75d7SMichal Malý 
687362cd22SMichal Malý static const signed short lg4ff_wheel_effects[] = {
6932c88cbcSSimon Wood 	FF_CONSTANT,
7032c88cbcSSimon Wood 	FF_AUTOCENTER,
7132c88cbcSSimon Wood 	-1
7232c88cbcSSimon Wood };
7332c88cbcSSimon Wood 
747362cd22SMichal Malý struct lg4ff_wheel {
757362cd22SMichal Malý 	const __u32 product_id;
767362cd22SMichal Malý 	const signed short *ff_effects;
777362cd22SMichal Malý 	const __u16 min_range;
787362cd22SMichal Malý 	const __u16 max_range;
7930bb75d7SMichal Malý 	void (*set_range)(struct hid_device *hid, u16 range);
807362cd22SMichal Malý };
817362cd22SMichal Malý 
827362cd22SMichal Malý static const struct lg4ff_wheel lg4ff_devices[] = {
8330bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_WHEEL,       lg4ff_wheel_effects, 40, 270, NULL},
8430bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_MOMO_WHEEL,  lg4ff_wheel_effects, 40, 270, NULL},
8530bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_DFP_WHEEL,   lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_dfp},
8630bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_G25_WHEEL,   lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
8730bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_DFGT_WHEEL,  lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
8830bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_G27_WHEEL,   lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
8930bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL},
9030bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_WII_WHEEL,   lg4ff_wheel_effects, 40, 270, NULL}
917362cd22SMichal Malý };
927362cd22SMichal Malý 
9396440c8aSMichal Malý struct lg4ff_native_cmd {
9496440c8aSMichal Malý 	const __u8 cmd_num;	/* Number of commands to send */
9596440c8aSMichal Malý 	const __u8 cmd[];
9696440c8aSMichal Malý };
9796440c8aSMichal Malý 
9896440c8aSMichal Malý struct lg4ff_usb_revision {
9996440c8aSMichal Malý 	const __u16 rev_maj;
10096440c8aSMichal Malý 	const __u16 rev_min;
10196440c8aSMichal Malý 	const struct lg4ff_native_cmd *command;
10296440c8aSMichal Malý };
10396440c8aSMichal Malý 
10496440c8aSMichal Malý static const struct lg4ff_native_cmd native_dfp = {
10596440c8aSMichal Malý 	1,
10696440c8aSMichal Malý 	{0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
10796440c8aSMichal Malý };
10896440c8aSMichal Malý 
10996440c8aSMichal Malý static const struct lg4ff_native_cmd native_dfgt = {
11096440c8aSMichal Malý 	2,
11196440c8aSMichal Malý 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* 1st command */
11296440c8aSMichal Malý 	 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00}	/* 2nd command */
11396440c8aSMichal Malý };
11496440c8aSMichal Malý 
11596440c8aSMichal Malý static const struct lg4ff_native_cmd native_g25 = {
11696440c8aSMichal Malý 	1,
11796440c8aSMichal Malý 	{0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
11896440c8aSMichal Malý };
11996440c8aSMichal Malý 
12096440c8aSMichal Malý static const struct lg4ff_native_cmd native_g27 = {
12196440c8aSMichal Malý 	2,
12296440c8aSMichal Malý 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* 1st command */
12396440c8aSMichal Malý 	 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00}	/* 2nd command */
12496440c8aSMichal Malý };
12596440c8aSMichal Malý 
12696440c8aSMichal Malý static const struct lg4ff_usb_revision lg4ff_revs[] = {
12796440c8aSMichal Malý 	{DFGT_REV_MAJ, DFGT_REV_MIN, &native_dfgt},	/* Driving Force GT */
12896440c8aSMichal Malý 	{DFP_REV_MAJ,  DFP_REV_MIN,  &native_dfp},	/* Driving Force Pro */
12996440c8aSMichal Malý 	{G25_REV_MAJ,  G25_REV_MIN,  &native_g25},	/* G25 */
13096440c8aSMichal Malý 	{G27_REV_MAJ,  G27_REV_MIN,  &native_g27},	/* G27 */
13196440c8aSMichal Malý };
13296440c8aSMichal Malý 
1332b24a960SMichal Malý /* Recalculates X axis value accordingly to currently selected range */
1342b24a960SMichal Malý static __s32 lg4ff_adjust_dfp_x_axis(__s32 value, __u16 range)
1352b24a960SMichal Malý {
1362b24a960SMichal Malý 	__u16 max_range;
1372b24a960SMichal Malý 	__s32 new_value;
1382b24a960SMichal Malý 
1392b24a960SMichal Malý 	if (range == 900)
1402b24a960SMichal Malý 		return value;
1412b24a960SMichal Malý 	else if (range == 200)
1422b24a960SMichal Malý 		return value;
1432b24a960SMichal Malý 	else if (range < 200)
1442b24a960SMichal Malý 		max_range = 200;
1452b24a960SMichal Malý 	else
1462b24a960SMichal Malý 		max_range = 900;
1472b24a960SMichal Malý 
1482b24a960SMichal Malý 	new_value = 8192 + mult_frac(value - 8192, max_range, range);
1492b24a960SMichal Malý 	if (new_value < 0)
1502b24a960SMichal Malý 		return 0;
1512b24a960SMichal Malý 	else if (new_value > 16383)
1522b24a960SMichal Malý 		return 16383;
1532b24a960SMichal Malý 	else
1542b24a960SMichal Malý 		return new_value;
1552b24a960SMichal Malý }
1562b24a960SMichal Malý 
1572b24a960SMichal Malý int lg4ff_adjust_input_event(struct hid_device *hid, struct hid_field *field,
1582b24a960SMichal Malý 			     struct hid_usage *usage, __s32 value, struct lg_drv_data *drv_data)
1592b24a960SMichal Malý {
1602b24a960SMichal Malý 	struct lg4ff_device_entry *entry = drv_data->device_props;
1612b24a960SMichal Malý 	__s32 new_value = 0;
1622b24a960SMichal Malý 
1632b24a960SMichal Malý 	if (!entry) {
1642b24a960SMichal Malý 		hid_err(hid, "Device properties not found");
1652b24a960SMichal Malý 		return 0;
1662b24a960SMichal Malý 	}
1672b24a960SMichal Malý 
1682b24a960SMichal Malý 	switch (entry->product_id) {
1692b24a960SMichal Malý 	case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
1702b24a960SMichal Malý 		switch (usage->code) {
1712b24a960SMichal Malý 		case ABS_X:
1722b24a960SMichal Malý 			new_value = lg4ff_adjust_dfp_x_axis(value, entry->range);
1732b24a960SMichal Malý 			input_event(field->hidinput->input, usage->type, usage->code, new_value);
1742b24a960SMichal Malý 			return 1;
1752b24a960SMichal Malý 		default:
1762b24a960SMichal Malý 			return 0;
1772b24a960SMichal Malý 		}
1782b24a960SMichal Malý 	default:
1792b24a960SMichal Malý 		return 0;
1802b24a960SMichal Malý 	}
1812b24a960SMichal Malý }
1822b24a960SMichal Malý 
18330bb75d7SMichal Malý static int hid_lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
18432c88cbcSSimon Wood {
18532c88cbcSSimon Wood 	struct hid_device *hid = input_get_drvdata(dev);
18632c88cbcSSimon Wood 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
18732c88cbcSSimon Wood 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
18874479ba8SMichal Malý 	__s32 *value = report->field[0]->value;
18932c88cbcSSimon Wood 	int x;
19032c88cbcSSimon Wood 
191a80fe5d6SMichal Malý #define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0)
19232c88cbcSSimon Wood 
19332c88cbcSSimon Wood 	switch (effect->type) {
19432c88cbcSSimon Wood 	case FF_CONSTANT:
19532c88cbcSSimon Wood 		x = effect->u.ramp.start_level + 0x80;	/* 0x80 is no force */
19632c88cbcSSimon Wood 		CLAMP(x);
19774479ba8SMichal Malý 		value[0] = 0x11;	/* Slot 1 */
19874479ba8SMichal Malý 		value[1] = 0x08;
19974479ba8SMichal Malý 		value[2] = x;
20074479ba8SMichal Malý 		value[3] = 0x80;
20174479ba8SMichal Malý 		value[4] = 0x00;
20274479ba8SMichal Malý 		value[5] = 0x00;
20374479ba8SMichal Malý 		value[6] = 0x00;
20432c88cbcSSimon Wood 
205*d8814272SBenjamin Tissoires 		hid_hw_request(hid, report, HID_REQ_SET_REPORT);
20632c88cbcSSimon Wood 		break;
20732c88cbcSSimon Wood 	}
20832c88cbcSSimon Wood 	return 0;
20932c88cbcSSimon Wood }
21032c88cbcSSimon Wood 
2116e2de8e0SMichal Malý /* Sends default autocentering command compatible with
2126e2de8e0SMichal Malý  * all wheels except Formula Force EX */
2136e2de8e0SMichal Malý static void hid_lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude)
21432c88cbcSSimon Wood {
21532c88cbcSSimon Wood 	struct hid_device *hid = input_get_drvdata(dev);
21632c88cbcSSimon Wood 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
21732c88cbcSSimon Wood 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
21874479ba8SMichal Malý 	__s32 *value = report->field[0]->value;
21932c88cbcSSimon Wood 
22074479ba8SMichal Malý 	value[0] = 0xfe;
22174479ba8SMichal Malý 	value[1] = 0x0d;
22274479ba8SMichal Malý 	value[2] = magnitude >> 13;
22374479ba8SMichal Malý 	value[3] = magnitude >> 13;
22474479ba8SMichal Malý 	value[4] = magnitude >> 8;
22574479ba8SMichal Malý 	value[5] = 0x00;
22674479ba8SMichal Malý 	value[6] = 0x00;
22732c88cbcSSimon Wood 
228*d8814272SBenjamin Tissoires 	hid_hw_request(hid, report, HID_REQ_SET_REPORT);
22932c88cbcSSimon Wood }
23032c88cbcSSimon Wood 
2316e2de8e0SMichal Malý /* Sends autocentering command compatible with Formula Force EX */
2326e2de8e0SMichal Malý static void hid_lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude)
2336e2de8e0SMichal Malý {
2346e2de8e0SMichal Malý 	struct hid_device *hid = input_get_drvdata(dev);
2356e2de8e0SMichal Malý 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
2366e2de8e0SMichal Malý 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
23774479ba8SMichal Malý 	__s32 *value = report->field[0]->value;
2386e2de8e0SMichal Malý 	magnitude = magnitude * 90 / 65535;
2396e2de8e0SMichal Malý 
24074479ba8SMichal Malý 	value[0] = 0xfe;
24174479ba8SMichal Malý 	value[1] = 0x03;
24274479ba8SMichal Malý 	value[2] = magnitude >> 14;
24374479ba8SMichal Malý 	value[3] = magnitude >> 14;
24474479ba8SMichal Malý 	value[4] = magnitude;
24574479ba8SMichal Malý 	value[5] = 0x00;
24674479ba8SMichal Malý 	value[6] = 0x00;
2476e2de8e0SMichal Malý 
248*d8814272SBenjamin Tissoires 	hid_hw_request(hid, report, HID_REQ_SET_REPORT);
2496e2de8e0SMichal Malý }
2506e2de8e0SMichal Malý 
25130bb75d7SMichal Malý /* Sends command to set range compatible with G25/G27/Driving Force GT */
25230bb75d7SMichal Malý static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range)
25330bb75d7SMichal Malý {
25430bb75d7SMichal Malý 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
25530bb75d7SMichal Malý 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
25674479ba8SMichal Malý 	__s32 *value = report->field[0]->value;
25774479ba8SMichal Malý 
25830bb75d7SMichal Malý 	dbg_hid("G25/G27/DFGT: setting range to %u\n", range);
25930bb75d7SMichal Malý 
26074479ba8SMichal Malý 	value[0] = 0xf8;
26174479ba8SMichal Malý 	value[1] = 0x81;
26274479ba8SMichal Malý 	value[2] = range & 0x00ff;
26374479ba8SMichal Malý 	value[3] = (range & 0xff00) >> 8;
26474479ba8SMichal Malý 	value[4] = 0x00;
26574479ba8SMichal Malý 	value[5] = 0x00;
26674479ba8SMichal Malý 	value[6] = 0x00;
26730bb75d7SMichal Malý 
268*d8814272SBenjamin Tissoires 	hid_hw_request(hid, report, HID_REQ_SET_REPORT);
26930bb75d7SMichal Malý }
27030bb75d7SMichal Malý 
27130bb75d7SMichal Malý /* Sends commands to set range compatible with Driving Force Pro wheel */
27230bb75d7SMichal Malý static void hid_lg4ff_set_range_dfp(struct hid_device *hid, __u16 range)
27330bb75d7SMichal Malý {
27430bb75d7SMichal Malý 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
27530bb75d7SMichal Malý 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
27630bb75d7SMichal Malý 	int start_left, start_right, full_range;
27774479ba8SMichal Malý 	__s32 *value = report->field[0]->value;
27874479ba8SMichal Malý 
27930bb75d7SMichal Malý 	dbg_hid("Driving Force Pro: setting range to %u\n", range);
28030bb75d7SMichal Malý 
28130bb75d7SMichal Malý 	/* Prepare "coarse" limit command */
28274479ba8SMichal Malý 	value[0] = 0xf8;
28374479ba8SMichal Malý 	value[1] = 0x00;	/* Set later */
28474479ba8SMichal Malý 	value[2] = 0x00;
28574479ba8SMichal Malý 	value[3] = 0x00;
28674479ba8SMichal Malý 	value[4] = 0x00;
28774479ba8SMichal Malý 	value[5] = 0x00;
28874479ba8SMichal Malý 	value[6] = 0x00;
28930bb75d7SMichal Malý 
29030bb75d7SMichal Malý 	if (range > 200) {
29130bb75d7SMichal Malý 		report->field[0]->value[1] = 0x03;
29230bb75d7SMichal Malý 		full_range = 900;
29330bb75d7SMichal Malý 	} else {
29430bb75d7SMichal Malý 		report->field[0]->value[1] = 0x02;
29530bb75d7SMichal Malý 		full_range = 200;
29630bb75d7SMichal Malý 	}
297*d8814272SBenjamin Tissoires 	hid_hw_request(hid, report, HID_REQ_SET_REPORT);
29830bb75d7SMichal Malý 
29930bb75d7SMichal Malý 	/* Prepare "fine" limit command */
30074479ba8SMichal Malý 	value[0] = 0x81;
30174479ba8SMichal Malý 	value[1] = 0x0b;
30274479ba8SMichal Malý 	value[2] = 0x00;
30374479ba8SMichal Malý 	value[3] = 0x00;
30474479ba8SMichal Malý 	value[4] = 0x00;
30574479ba8SMichal Malý 	value[5] = 0x00;
30674479ba8SMichal Malý 	value[6] = 0x00;
30730bb75d7SMichal Malý 
30830bb75d7SMichal Malý 	if (range == 200 || range == 900) {	/* Do not apply any fine limit */
309*d8814272SBenjamin Tissoires 		hid_hw_request(hid, report, HID_REQ_SET_REPORT);
31030bb75d7SMichal Malý 		return;
31130bb75d7SMichal Malý 	}
31230bb75d7SMichal Malý 
31330bb75d7SMichal Malý 	/* Construct fine limit command */
31430bb75d7SMichal Malý 	start_left = (((full_range - range + 1) * 2047) / full_range);
31530bb75d7SMichal Malý 	start_right = 0xfff - start_left;
31630bb75d7SMichal Malý 
31774479ba8SMichal Malý 	value[2] = start_left >> 4;
31874479ba8SMichal Malý 	value[3] = start_right >> 4;
31974479ba8SMichal Malý 	value[4] = 0xff;
32074479ba8SMichal Malý 	value[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
32174479ba8SMichal Malý 	value[6] = 0xff;
32230bb75d7SMichal Malý 
323*d8814272SBenjamin Tissoires 	hid_hw_request(hid, report, HID_REQ_SET_REPORT);
32430bb75d7SMichal Malý }
32530bb75d7SMichal Malý 
32696440c8aSMichal Malý static void hid_lg4ff_switch_native(struct hid_device *hid, const struct lg4ff_native_cmd *cmd)
32796440c8aSMichal Malý {
32896440c8aSMichal Malý 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
32996440c8aSMichal Malý 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
33096440c8aSMichal Malý 	__u8 i, j;
33196440c8aSMichal Malý 
33296440c8aSMichal Malý 	j = 0;
33396440c8aSMichal Malý 	while (j < 7*cmd->cmd_num) {
33496440c8aSMichal Malý 		for (i = 0; i < 7; i++)
33596440c8aSMichal Malý 			report->field[0]->value[i] = cmd->cmd[j++];
33696440c8aSMichal Malý 
337*d8814272SBenjamin Tissoires 		hid_hw_request(hid, report, HID_REQ_SET_REPORT);
33896440c8aSMichal Malý 	}
33996440c8aSMichal Malý }
34032c88cbcSSimon Wood 
34130bb75d7SMichal Malý /* Read current range and display it in terminal */
34230bb75d7SMichal Malý static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
34330bb75d7SMichal Malý {
34430bb75d7SMichal Malý 	struct hid_device *hid = to_hid_device(dev);
3453b6b17b7SMichal Malý 	struct lg4ff_device_entry *entry;
3463b6b17b7SMichal Malý 	struct lg_drv_data *drv_data;
34730bb75d7SMichal Malý 	size_t count;
34830bb75d7SMichal Malý 
3493b6b17b7SMichal Malý 	drv_data = hid_get_drvdata(hid);
3503b6b17b7SMichal Malý 	if (!drv_data) {
3513b6b17b7SMichal Malý 		hid_err(hid, "Private driver data not found!\n");
3523b6b17b7SMichal Malý 		return 0;
35330bb75d7SMichal Malý 	}
3543b6b17b7SMichal Malý 
3553b6b17b7SMichal Malý 	entry = drv_data->device_props;
3563b6b17b7SMichal Malý 	if (!entry) {
3573b6b17b7SMichal Malý 		hid_err(hid, "Device properties not found!\n");
35830bb75d7SMichal Malý 		return 0;
35930bb75d7SMichal Malý 	}
36030bb75d7SMichal Malý 
36130bb75d7SMichal Malý 	count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->range);
36230bb75d7SMichal Malý 	return count;
36330bb75d7SMichal Malý }
36430bb75d7SMichal Malý 
36530bb75d7SMichal Malý /* Set range to user specified value, call appropriate function
36630bb75d7SMichal Malý  * according to the type of the wheel */
36730bb75d7SMichal Malý static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
36830bb75d7SMichal Malý {
36930bb75d7SMichal Malý 	struct hid_device *hid = to_hid_device(dev);
3703b6b17b7SMichal Malý 	struct lg4ff_device_entry *entry;
3713b6b17b7SMichal Malý 	struct lg_drv_data *drv_data;
37230bb75d7SMichal Malý 	__u16 range = simple_strtoul(buf, NULL, 10);
37330bb75d7SMichal Malý 
3743b6b17b7SMichal Malý 	drv_data = hid_get_drvdata(hid);
3753b6b17b7SMichal Malý 	if (!drv_data) {
3763b6b17b7SMichal Malý 		hid_err(hid, "Private driver data not found!\n");
3773b6b17b7SMichal Malý 		return 0;
37830bb75d7SMichal Malý 	}
3793b6b17b7SMichal Malý 
3803b6b17b7SMichal Malý 	entry = drv_data->device_props;
3813b6b17b7SMichal Malý 	if (!entry) {
3823b6b17b7SMichal Malý 		hid_err(hid, "Device properties not found!\n");
3833b6b17b7SMichal Malý 		return 0;
38430bb75d7SMichal Malý 	}
38530bb75d7SMichal Malý 
38630bb75d7SMichal Malý 	if (range == 0)
38730bb75d7SMichal Malý 		range = entry->max_range;
38830bb75d7SMichal Malý 
38930bb75d7SMichal Malý 	/* Check if the wheel supports range setting
39030bb75d7SMichal Malý 	 * and that the range is within limits for the wheel */
39130bb75d7SMichal Malý 	if (entry->set_range != NULL && range >= entry->min_range && range <= entry->max_range) {
39230bb75d7SMichal Malý 		entry->set_range(hid, range);
39330bb75d7SMichal Malý 		entry->range = range;
39430bb75d7SMichal Malý 	}
39530bb75d7SMichal Malý 
39630bb75d7SMichal Malý 	return count;
39730bb75d7SMichal Malý }
39830bb75d7SMichal Malý 
39922bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS
40022bcefdcSSimon Wood static void lg4ff_set_leds(struct hid_device *hid, __u8 leds)
40122bcefdcSSimon Wood {
40222bcefdcSSimon Wood 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
40322bcefdcSSimon Wood 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
40474479ba8SMichal Malý 	__s32 *value = report->field[0]->value;
40522bcefdcSSimon Wood 
40674479ba8SMichal Malý 	value[0] = 0xf8;
40774479ba8SMichal Malý 	value[1] = 0x12;
40874479ba8SMichal Malý 	value[2] = leds;
40974479ba8SMichal Malý 	value[3] = 0x00;
41074479ba8SMichal Malý 	value[4] = 0x00;
41174479ba8SMichal Malý 	value[5] = 0x00;
41274479ba8SMichal Malý 	value[6] = 0x00;
413*d8814272SBenjamin Tissoires 	hid_hw_request(hid, report, HID_REQ_SET_REPORT);
41422bcefdcSSimon Wood }
41522bcefdcSSimon Wood 
41622bcefdcSSimon Wood static void lg4ff_led_set_brightness(struct led_classdev *led_cdev,
41722bcefdcSSimon Wood 			enum led_brightness value)
41822bcefdcSSimon Wood {
41922bcefdcSSimon Wood 	struct device *dev = led_cdev->dev->parent;
42022bcefdcSSimon Wood 	struct hid_device *hid = container_of(dev, struct hid_device, dev);
4214629fd16SAxel Lin 	struct lg_drv_data *drv_data = hid_get_drvdata(hid);
42222bcefdcSSimon Wood 	struct lg4ff_device_entry *entry;
42322bcefdcSSimon Wood 	int i, state = 0;
42422bcefdcSSimon Wood 
42522bcefdcSSimon Wood 	if (!drv_data) {
42622bcefdcSSimon Wood 		hid_err(hid, "Device data not found.");
42722bcefdcSSimon Wood 		return;
42822bcefdcSSimon Wood 	}
42922bcefdcSSimon Wood 
43022bcefdcSSimon Wood 	entry = (struct lg4ff_device_entry *)drv_data->device_props;
43122bcefdcSSimon Wood 
43222bcefdcSSimon Wood 	if (!entry) {
43322bcefdcSSimon Wood 		hid_err(hid, "Device properties not found.");
43422bcefdcSSimon Wood 		return;
43522bcefdcSSimon Wood 	}
43622bcefdcSSimon Wood 
43722bcefdcSSimon Wood 	for (i = 0; i < 5; i++) {
43822bcefdcSSimon Wood 		if (led_cdev != entry->led[i])
43922bcefdcSSimon Wood 			continue;
44022bcefdcSSimon Wood 		state = (entry->led_state >> i) & 1;
44122bcefdcSSimon Wood 		if (value == LED_OFF && state) {
44222bcefdcSSimon Wood 			entry->led_state &= ~(1 << i);
44322bcefdcSSimon Wood 			lg4ff_set_leds(hid, entry->led_state);
44422bcefdcSSimon Wood 		} else if (value != LED_OFF && !state) {
44522bcefdcSSimon Wood 			entry->led_state |= 1 << i;
44622bcefdcSSimon Wood 			lg4ff_set_leds(hid, entry->led_state);
44722bcefdcSSimon Wood 		}
44822bcefdcSSimon Wood 		break;
44922bcefdcSSimon Wood 	}
45022bcefdcSSimon Wood }
45122bcefdcSSimon Wood 
45222bcefdcSSimon Wood static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev)
45322bcefdcSSimon Wood {
45422bcefdcSSimon Wood 	struct device *dev = led_cdev->dev->parent;
45522bcefdcSSimon Wood 	struct hid_device *hid = container_of(dev, struct hid_device, dev);
4564629fd16SAxel Lin 	struct lg_drv_data *drv_data = hid_get_drvdata(hid);
45722bcefdcSSimon Wood 	struct lg4ff_device_entry *entry;
45822bcefdcSSimon Wood 	int i, value = 0;
45922bcefdcSSimon Wood 
46022bcefdcSSimon Wood 	if (!drv_data) {
46122bcefdcSSimon Wood 		hid_err(hid, "Device data not found.");
46222bcefdcSSimon Wood 		return LED_OFF;
46322bcefdcSSimon Wood 	}
46422bcefdcSSimon Wood 
46522bcefdcSSimon Wood 	entry = (struct lg4ff_device_entry *)drv_data->device_props;
46622bcefdcSSimon Wood 
46722bcefdcSSimon Wood 	if (!entry) {
46822bcefdcSSimon Wood 		hid_err(hid, "Device properties not found.");
46922bcefdcSSimon Wood 		return LED_OFF;
47022bcefdcSSimon Wood 	}
47122bcefdcSSimon Wood 
47222bcefdcSSimon Wood 	for (i = 0; i < 5; i++)
47322bcefdcSSimon Wood 		if (led_cdev == entry->led[i]) {
47422bcefdcSSimon Wood 			value = (entry->led_state >> i) & 1;
47522bcefdcSSimon Wood 			break;
47622bcefdcSSimon Wood 		}
47722bcefdcSSimon Wood 
47822bcefdcSSimon Wood 	return value ? LED_FULL : LED_OFF;
47922bcefdcSSimon Wood }
48022bcefdcSSimon Wood #endif
48122bcefdcSSimon Wood 
48232c88cbcSSimon Wood int lg4ff_init(struct hid_device *hid)
48332c88cbcSSimon Wood {
48432c88cbcSSimon Wood 	struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
48532c88cbcSSimon Wood 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
48632c88cbcSSimon Wood 	struct input_dev *dev = hidinput->input;
48732c88cbcSSimon Wood 	struct hid_report *report;
48832c88cbcSSimon Wood 	struct hid_field *field;
48930bb75d7SMichal Malý 	struct lg4ff_device_entry *entry;
4903b6b17b7SMichal Malý 	struct lg_drv_data *drv_data;
49130bb75d7SMichal Malý 	struct usb_device_descriptor *udesc;
4927362cd22SMichal Malý 	int error, i, j;
49396440c8aSMichal Malý 	__u16 bcdDevice, rev_maj, rev_min;
49432c88cbcSSimon Wood 
49532c88cbcSSimon Wood 	/* Find the report to use */
49632c88cbcSSimon Wood 	if (list_empty(report_list)) {
4974291ee30SJoe Perches 		hid_err(hid, "No output report found\n");
49832c88cbcSSimon Wood 		return -1;
49932c88cbcSSimon Wood 	}
50032c88cbcSSimon Wood 
50132c88cbcSSimon Wood 	/* Check that the report looks ok */
50232c88cbcSSimon Wood 	report = list_entry(report_list->next, struct hid_report, list);
50332c88cbcSSimon Wood 	if (!report) {
5044291ee30SJoe Perches 		hid_err(hid, "NULL output report\n");
50532c88cbcSSimon Wood 		return -1;
50632c88cbcSSimon Wood 	}
50732c88cbcSSimon Wood 
50832c88cbcSSimon Wood 	field = report->field[0];
50932c88cbcSSimon Wood 	if (!field) {
5104291ee30SJoe Perches 		hid_err(hid, "NULL field\n");
51132c88cbcSSimon Wood 		return -1;
51232c88cbcSSimon Wood 	}
51332c88cbcSSimon Wood 
5147362cd22SMichal Malý 	/* Check what wheel has been connected */
5157362cd22SMichal Malý 	for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
5167362cd22SMichal Malý 		if (hid->product == lg4ff_devices[i].product_id) {
5177362cd22SMichal Malý 			dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
5187362cd22SMichal Malý 			break;
5197362cd22SMichal Malý 		}
5207362cd22SMichal Malý 	}
5217362cd22SMichal Malý 
5227362cd22SMichal Malý 	if (i == ARRAY_SIZE(lg4ff_devices)) {
5237362cd22SMichal Malý 		hid_err(hid, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to"
5247362cd22SMichal Malý 			     "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n");
5257362cd22SMichal Malý 		return -1;
5267362cd22SMichal Malý 	}
5277362cd22SMichal Malý 
52896440c8aSMichal Malý 	/* Attempt to switch wheel to native mode when applicable */
52996440c8aSMichal Malý 	udesc = &(hid_to_usb_dev(hid)->descriptor);
53096440c8aSMichal Malý 	if (!udesc) {
53196440c8aSMichal Malý 		hid_err(hid, "NULL USB device descriptor\n");
53296440c8aSMichal Malý 		return -1;
53396440c8aSMichal Malý 	}
53496440c8aSMichal Malý 	bcdDevice = le16_to_cpu(udesc->bcdDevice);
53596440c8aSMichal Malý 	rev_maj = bcdDevice >> 8;
53696440c8aSMichal Malý 	rev_min = bcdDevice & 0xff;
53796440c8aSMichal Malý 
53896440c8aSMichal Malý 	if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_WHEEL) {
53996440c8aSMichal Malý 		dbg_hid("Generic wheel detected, can it do native?\n");
54096440c8aSMichal Malý 		dbg_hid("USB revision: %2x.%02x\n", rev_maj, rev_min);
54196440c8aSMichal Malý 
54296440c8aSMichal Malý 		for (j = 0; j < ARRAY_SIZE(lg4ff_revs); j++) {
54396440c8aSMichal Malý 			if (lg4ff_revs[j].rev_maj == rev_maj && lg4ff_revs[j].rev_min == rev_min) {
54496440c8aSMichal Malý 				hid_lg4ff_switch_native(hid, lg4ff_revs[j].command);
54596440c8aSMichal Malý 				hid_info(hid, "Switched to native mode\n");
54696440c8aSMichal Malý 			}
54796440c8aSMichal Malý 		}
54896440c8aSMichal Malý 	}
54996440c8aSMichal Malý 
5507362cd22SMichal Malý 	/* Set supported force feedback capabilities */
5517362cd22SMichal Malý 	for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
5527362cd22SMichal Malý 		set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
55332c88cbcSSimon Wood 
55432c88cbcSSimon Wood 	error = input_ff_create_memless(dev, NULL, hid_lg4ff_play);
55532c88cbcSSimon Wood 
55632c88cbcSSimon Wood 	if (error)
55732c88cbcSSimon Wood 		return error;
55832c88cbcSSimon Wood 
5596e2de8e0SMichal Malý 	/* Check if autocentering is available and
5606e2de8e0SMichal Malý 	 * set the centering force to zero by default */
5616e2de8e0SMichal Malý 	if (test_bit(FF_AUTOCENTER, dev->ffbit)) {
5626e2de8e0SMichal Malý 		if (rev_maj == FFEX_REV_MAJ && rev_min == FFEX_REV_MIN)	/* Formula Force EX expects different autocentering command */
5636e2de8e0SMichal Malý 			dev->ff->set_autocenter = hid_lg4ff_set_autocenter_ffex;
5646e2de8e0SMichal Malý 		else
5656e2de8e0SMichal Malý 			dev->ff->set_autocenter = hid_lg4ff_set_autocenter_default;
5666e2de8e0SMichal Malý 
5676e2de8e0SMichal Malý 		dev->ff->set_autocenter(dev, 0);
5686e2de8e0SMichal Malý 	}
56932c88cbcSSimon Wood 
5703b6b17b7SMichal Malý 	/* Get private driver data */
5713b6b17b7SMichal Malý 	drv_data = hid_get_drvdata(hid);
5723b6b17b7SMichal Malý 	if (!drv_data) {
5733b6b17b7SMichal Malý 		hid_err(hid, "Cannot add device, private driver data not allocated\n");
5743b6b17b7SMichal Malý 		return -1;
57530bb75d7SMichal Malý 	}
57630bb75d7SMichal Malý 
5773b6b17b7SMichal Malý 	/* Initialize device properties */
5788383c6bfSThomas Meyer 	entry = kzalloc(sizeof(struct lg4ff_device_entry), GFP_KERNEL);
57930bb75d7SMichal Malý 	if (!entry) {
5803b6b17b7SMichal Malý 		hid_err(hid, "Cannot add device, insufficient memory to allocate device properties.\n");
58130bb75d7SMichal Malý 		return -ENOMEM;
58230bb75d7SMichal Malý 	}
5833b6b17b7SMichal Malý 	drv_data->device_props = entry;
5843b6b17b7SMichal Malý 
5852b24a960SMichal Malý 	entry->product_id = lg4ff_devices[i].product_id;
58630bb75d7SMichal Malý 	entry->min_range = lg4ff_devices[i].min_range;
58730bb75d7SMichal Malý 	entry->max_range = lg4ff_devices[i].max_range;
58830bb75d7SMichal Malý 	entry->set_range = lg4ff_devices[i].set_range;
58930bb75d7SMichal Malý 
59030bb75d7SMichal Malý 	/* Create sysfs interface */
59130bb75d7SMichal Malý 	error = device_create_file(&hid->dev, &dev_attr_range);
59230bb75d7SMichal Malý 	if (error)
59330bb75d7SMichal Malý 		return error;
59430bb75d7SMichal Malý 	dbg_hid("sysfs interface created\n");
59530bb75d7SMichal Malý 
59630bb75d7SMichal Malý 	/* Set the maximum range to start with */
59730bb75d7SMichal Malý 	entry->range = entry->max_range;
59830bb75d7SMichal Malý 	if (entry->set_range != NULL)
59930bb75d7SMichal Malý 		entry->set_range(hid, entry->range);
60030bb75d7SMichal Malý 
60122bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS
60222bcefdcSSimon Wood 	/* register led subsystem - G27 only */
60322bcefdcSSimon Wood 	entry->led_state = 0;
60422bcefdcSSimon Wood 	for (j = 0; j < 5; j++)
60522bcefdcSSimon Wood 		entry->led[j] = NULL;
60622bcefdcSSimon Wood 
60722bcefdcSSimon Wood 	if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL) {
60822bcefdcSSimon Wood 		struct led_classdev *led;
60922bcefdcSSimon Wood 		size_t name_sz;
61022bcefdcSSimon Wood 		char *name;
61122bcefdcSSimon Wood 
61222bcefdcSSimon Wood 		lg4ff_set_leds(hid, 0);
61322bcefdcSSimon Wood 
61422bcefdcSSimon Wood 		name_sz = strlen(dev_name(&hid->dev)) + 8;
61522bcefdcSSimon Wood 
61622bcefdcSSimon Wood 		for (j = 0; j < 5; j++) {
61722bcefdcSSimon Wood 			led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
61822bcefdcSSimon Wood 			if (!led) {
61922bcefdcSSimon Wood 				hid_err(hid, "can't allocate memory for LED %d\n", j);
62022bcefdcSSimon Wood 				goto err;
62122bcefdcSSimon Wood 			}
62222bcefdcSSimon Wood 
62322bcefdcSSimon Wood 			name = (void *)(&led[1]);
62422bcefdcSSimon Wood 			snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1);
62522bcefdcSSimon Wood 			led->name = name;
62622bcefdcSSimon Wood 			led->brightness = 0;
62722bcefdcSSimon Wood 			led->max_brightness = 1;
62822bcefdcSSimon Wood 			led->brightness_get = lg4ff_led_get_brightness;
62922bcefdcSSimon Wood 			led->brightness_set = lg4ff_led_set_brightness;
63022bcefdcSSimon Wood 
63122bcefdcSSimon Wood 			entry->led[j] = led;
63222bcefdcSSimon Wood 			error = led_classdev_register(&hid->dev, led);
63322bcefdcSSimon Wood 
63422bcefdcSSimon Wood 			if (error) {
63522bcefdcSSimon Wood 				hid_err(hid, "failed to register LED %d. Aborting.\n", j);
63622bcefdcSSimon Wood err:
63722bcefdcSSimon Wood 				/* Deregister LEDs (if any) */
63822bcefdcSSimon Wood 				for (j = 0; j < 5; j++) {
63922bcefdcSSimon Wood 					led = entry->led[j];
64022bcefdcSSimon Wood 					entry->led[j] = NULL;
64122bcefdcSSimon Wood 					if (!led)
64222bcefdcSSimon Wood 						continue;
64322bcefdcSSimon Wood 					led_classdev_unregister(led);
64422bcefdcSSimon Wood 					kfree(led);
64522bcefdcSSimon Wood 				}
64622bcefdcSSimon Wood 				goto out;	/* Let the driver continue without LEDs */
64722bcefdcSSimon Wood 			}
64822bcefdcSSimon Wood 		}
64922bcefdcSSimon Wood 	}
65022bcefdcSSimon Wood out:
651c6e6dc87SJiri Kosina #endif
65264013800SSimon Wood 	hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n");
65332c88cbcSSimon Wood 	return 0;
65432c88cbcSSimon Wood }
65532c88cbcSSimon Wood 
6562b24a960SMichal Malý 
6572b24a960SMichal Malý 
65830bb75d7SMichal Malý int lg4ff_deinit(struct hid_device *hid)
65930bb75d7SMichal Malý {
66030bb75d7SMichal Malý 	struct lg4ff_device_entry *entry;
6613b6b17b7SMichal Malý 	struct lg_drv_data *drv_data;
6626a2e176bSMichal Malý 
6636a2e176bSMichal Malý 	device_remove_file(&hid->dev, &dev_attr_range);
6646a2e176bSMichal Malý 
6653b6b17b7SMichal Malý 	drv_data = hid_get_drvdata(hid);
6663b6b17b7SMichal Malý 	if (!drv_data) {
6673b6b17b7SMichal Malý 		hid_err(hid, "Error while deinitializing device, no private driver data.\n");
66830bb75d7SMichal Malý 		return -1;
66930bb75d7SMichal Malý 	}
6703b6b17b7SMichal Malý 	entry = drv_data->device_props;
6713b6b17b7SMichal Malý 	if (!entry) {
6723b6b17b7SMichal Malý 		hid_err(hid, "Error while deinitializing device, no device properties data.\n");
6733b6b17b7SMichal Malý 		return -1;
6743b6b17b7SMichal Malý 	}
67522bcefdcSSimon Wood 
67622bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS
67722bcefdcSSimon Wood 	{
67822bcefdcSSimon Wood 		int j;
67922bcefdcSSimon Wood 		struct led_classdev *led;
68022bcefdcSSimon Wood 
68122bcefdcSSimon Wood 		/* Deregister LEDs (if any) */
68222bcefdcSSimon Wood 		for (j = 0; j < 5; j++) {
68322bcefdcSSimon Wood 
68422bcefdcSSimon Wood 			led = entry->led[j];
68522bcefdcSSimon Wood 			entry->led[j] = NULL;
68622bcefdcSSimon Wood 			if (!led)
68722bcefdcSSimon Wood 				continue;
68822bcefdcSSimon Wood 			led_classdev_unregister(led);
68922bcefdcSSimon Wood 			kfree(led);
69022bcefdcSSimon Wood 		}
69122bcefdcSSimon Wood 	}
69222bcefdcSSimon Wood #endif
69322bcefdcSSimon Wood 
6943b6b17b7SMichal Malý 	/* Deallocate memory */
6953b6b17b7SMichal Malý 	kfree(entry);
69630bb75d7SMichal Malý 
69730bb75d7SMichal Malý 	dbg_hid("Device successfully unregistered\n");
69830bb75d7SMichal Malý 	return 0;
69930bb75d7SMichal Malý }
700