xref: /openbmc/linux/drivers/hid/hid-lg4ff.c (revision c28abd8c423b85e2d8be8ed33984a3ad966f1c83)
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"
33a54dc779SMichal Malý #include "hid-lg4ff.h"
347362cd22SMichal Malý #include "hid-ids.h"
3532c88cbcSSimon Wood 
3630bb75d7SMichal Malý #define to_hid_device(pdev) container_of(pdev, struct hid_device, dev)
3730bb75d7SMichal Malý 
38b96d23ecSMichal Malý #define LG4FF_MMODE_IS_MULTIMODE 0
39e7c23449SMichal Malý #define LG4FF_MMODE_SWITCHED 1
40e7c23449SMichal Malý #define LG4FF_MMODE_NOT_MULTIMODE 2
41e7c23449SMichal Malý 
42b96d23ecSMichal Malý #define LG4FF_MODE_NATIVE_IDX 0
43b96d23ecSMichal Malý #define LG4FF_MODE_DFEX_IDX 1
44b96d23ecSMichal Malý #define LG4FF_MODE_DFP_IDX 2
45b96d23ecSMichal Malý #define LG4FF_MODE_G25_IDX 3
46b96d23ecSMichal Malý #define LG4FF_MODE_DFGT_IDX 4
47b96d23ecSMichal Malý #define LG4FF_MODE_G27_IDX 5
48b96d23ecSMichal Malý #define LG4FF_MODE_MAX_IDX 6
49b96d23ecSMichal Malý 
50b96d23ecSMichal Malý #define LG4FF_MODE_NATIVE BIT(LG4FF_MODE_NATIVE_IDX)
51b96d23ecSMichal Malý #define LG4FF_MODE_DFEX BIT(LG4FF_MODE_DFEX_IDX)
52b96d23ecSMichal Malý #define LG4FF_MODE_DFP BIT(LG4FF_MODE_DFP_IDX)
53b96d23ecSMichal Malý #define LG4FF_MODE_G25 BIT(LG4FF_MODE_G25_IDX)
54b96d23ecSMichal Malý #define LG4FF_MODE_DFGT BIT(LG4FF_MODE_DFGT_IDX)
55b96d23ecSMichal Malý #define LG4FF_MODE_G27 BIT(LG4FF_MODE_G27_IDX)
56b96d23ecSMichal Malý 
57b96d23ecSMichal Malý #define LG4FF_DFEX_TAG "DF-EX"
58b96d23ecSMichal Malý #define LG4FF_DFEX_NAME "Driving Force / Formula EX"
59b96d23ecSMichal Malý #define LG4FF_DFP_TAG "DFP"
60b96d23ecSMichal Malý #define LG4FF_DFP_NAME "Driving Force Pro"
61b96d23ecSMichal Malý #define LG4FF_G25_TAG "G25"
62b96d23ecSMichal Malý #define LG4FF_G25_NAME "G25 Racing Wheel"
63b96d23ecSMichal Malý #define LG4FF_G27_TAG "G27"
64b96d23ecSMichal Malý #define LG4FF_G27_NAME "G27 Racing Wheel"
65b96d23ecSMichal Malý #define LG4FF_DFGT_TAG "DFGT"
66b96d23ecSMichal Malý #define LG4FF_DFGT_NAME "Driving Force GT"
67b96d23ecSMichal Malý 
68e7c23449SMichal Malý #define LG4FF_FFEX_REV_MAJ 0x21
69e7c23449SMichal Malý #define LG4FF_FFEX_REV_MIN 0x00
70e7c23449SMichal Malý 
71d0afd848SMichal Malý static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range);
72d0afd848SMichal Malý static void lg4ff_set_range_g25(struct hid_device *hid, u16 range);
7330bb75d7SMichal Malý 
7472529c65SMichal Malý struct lg4ff_wheel_data {
752a552c30SMichal Malý 	u32 product_id;
762a552c30SMichal Malý 	u16 range;
772a552c30SMichal Malý 	u16 min_range;
782a552c30SMichal Malý 	u16 max_range;
7922bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS
802a552c30SMichal Malý 	u8  led_state;
8122bcefdcSSimon Wood 	struct led_classdev *led[5];
8222bcefdcSSimon Wood #endif
83b96d23ecSMichal Malý 	u32 alternate_modes;
84b96d23ecSMichal Malý 	const char *real_tag;
85b96d23ecSMichal Malý 	const char *real_name;
86b96d23ecSMichal Malý 	u16 real_product_id;
8772529c65SMichal Malý 
8830bb75d7SMichal Malý 	void (*set_range)(struct hid_device *hid, u16 range);
8930bb75d7SMichal Malý };
9030bb75d7SMichal Malý 
9172529c65SMichal Malý struct lg4ff_device_entry {
92c918fe78SMichal Malý 	spinlock_t report_lock; /* Protect output HID report */
93*c28abd8cSMichal Malý 	struct hid_report *report;
9472529c65SMichal Malý 	struct lg4ff_wheel_data wdata;
9572529c65SMichal Malý };
9672529c65SMichal Malý 
977362cd22SMichal Malý static const signed short lg4ff_wheel_effects[] = {
9832c88cbcSSimon Wood 	FF_CONSTANT,
9932c88cbcSSimon Wood 	FF_AUTOCENTER,
10032c88cbcSSimon Wood 	-1
10132c88cbcSSimon Wood };
10232c88cbcSSimon Wood 
1037362cd22SMichal Malý struct lg4ff_wheel {
1042a552c30SMichal Malý 	const u32 product_id;
1057362cd22SMichal Malý 	const signed short *ff_effects;
1062a552c30SMichal Malý 	const u16 min_range;
1072a552c30SMichal Malý 	const u16 max_range;
10830bb75d7SMichal Malý 	void (*set_range)(struct hid_device *hid, u16 range);
1097362cd22SMichal Malý };
1107362cd22SMichal Malý 
111e7c23449SMichal Malý struct lg4ff_compat_mode_switch {
1122a552c30SMichal Malý 	const u8 cmd_count;	/* Number of commands to send */
1132a552c30SMichal Malý 	const u8 cmd[];
114e7c23449SMichal Malý };
115e7c23449SMichal Malý 
116e7c23449SMichal Malý struct lg4ff_wheel_ident_info {
117e7c23449SMichal Malý 	const u16 mask;
118e7c23449SMichal Malý 	const u16 result;
119e7c23449SMichal Malý 	const u16 real_product_id;
120e7c23449SMichal Malý };
121e7c23449SMichal Malý 
122e7c23449SMichal Malý struct lg4ff_wheel_ident_checklist {
123e7c23449SMichal Malý 	const u32 count;
124e7c23449SMichal Malý 	const struct lg4ff_wheel_ident_info *models[];
125e7c23449SMichal Malý };
126e7c23449SMichal Malý 
127b96d23ecSMichal Malý struct lg4ff_multimode_wheel {
128b96d23ecSMichal Malý 	const u16 product_id;
129b96d23ecSMichal Malý 	const u32 alternate_modes;
130b96d23ecSMichal Malý 	const char *real_tag;
131b96d23ecSMichal Malý 	const char *real_name;
132b96d23ecSMichal Malý };
133b96d23ecSMichal Malý 
134b96d23ecSMichal Malý struct lg4ff_alternate_mode {
135b96d23ecSMichal Malý 	const u16 product_id;
136b96d23ecSMichal Malý 	const char *tag;
137b96d23ecSMichal Malý 	const char *name;
138b96d23ecSMichal Malý };
139b96d23ecSMichal Malý 
1407362cd22SMichal Malý static const struct lg4ff_wheel lg4ff_devices[] = {
14130bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_WHEEL,       lg4ff_wheel_effects, 40, 270, NULL},
14230bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_MOMO_WHEEL,  lg4ff_wheel_effects, 40, 270, NULL},
143d0afd848SMichal Malý 	{USB_DEVICE_ID_LOGITECH_DFP_WHEEL,   lg4ff_wheel_effects, 40, 900, lg4ff_set_range_dfp},
144d0afd848SMichal Malý 	{USB_DEVICE_ID_LOGITECH_G25_WHEEL,   lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
145d0afd848SMichal Malý 	{USB_DEVICE_ID_LOGITECH_DFGT_WHEEL,  lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
146d0afd848SMichal Malý 	{USB_DEVICE_ID_LOGITECH_G27_WHEEL,   lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
14730bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL},
14830bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_WII_WHEEL,   lg4ff_wheel_effects, 40, 270, NULL}
1497362cd22SMichal Malý };
1507362cd22SMichal Malý 
151b96d23ecSMichal Malý static const struct lg4ff_multimode_wheel lg4ff_multimode_wheels[] = {
152b96d23ecSMichal Malý 	{USB_DEVICE_ID_LOGITECH_DFP_WHEEL,
153b96d23ecSMichal Malý 	 LG4FF_MODE_NATIVE | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
154b96d23ecSMichal Malý 	 LG4FF_DFP_TAG, LG4FF_DFP_NAME},
155b96d23ecSMichal Malý 	{USB_DEVICE_ID_LOGITECH_G25_WHEEL,
156b96d23ecSMichal Malý 	 LG4FF_MODE_NATIVE | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
157b96d23ecSMichal Malý 	 LG4FF_G25_TAG, LG4FF_G25_NAME},
158b96d23ecSMichal Malý 	{USB_DEVICE_ID_LOGITECH_DFGT_WHEEL,
159b96d23ecSMichal Malý 	 LG4FF_MODE_NATIVE | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
160b96d23ecSMichal Malý 	 LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
161b96d23ecSMichal Malý 	{USB_DEVICE_ID_LOGITECH_G27_WHEEL,
162b96d23ecSMichal Malý 	 LG4FF_MODE_NATIVE | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
163b96d23ecSMichal Malý 	 LG4FF_G27_TAG, LG4FF_G27_NAME},
164b96d23ecSMichal Malý };
165b96d23ecSMichal Malý 
166b96d23ecSMichal Malý static const struct lg4ff_alternate_mode lg4ff_alternate_modes[] = {
167b96d23ecSMichal Malý 	[LG4FF_MODE_NATIVE_IDX] = {0, "native", ""},
168b96d23ecSMichal Malý 	[LG4FF_MODE_DFEX_IDX] = {USB_DEVICE_ID_LOGITECH_WHEEL, LG4FF_DFEX_TAG, LG4FF_DFEX_NAME},
169b96d23ecSMichal Malý 	[LG4FF_MODE_DFP_IDX] = {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, LG4FF_DFP_TAG, LG4FF_DFP_NAME},
170b96d23ecSMichal Malý 	[LG4FF_MODE_G25_IDX] = {USB_DEVICE_ID_LOGITECH_G25_WHEEL, LG4FF_G25_TAG, LG4FF_G25_NAME},
171b96d23ecSMichal Malý 	[LG4FF_MODE_DFGT_IDX] = {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
172b96d23ecSMichal Malý 	[LG4FF_MODE_G27_IDX] = {USB_DEVICE_ID_LOGITECH_G27_WHEEL, LG4FF_G27_TAG, LG4FF_G27_NAME}
173b96d23ecSMichal Malý };
174b96d23ecSMichal Malý 
175e7c23449SMichal Malý /* Multimode wheel identificators */
176e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_dfp_ident_info = {
177e7c23449SMichal Malý 	0xf000,
178e7c23449SMichal Malý 	0x1000,
179e7c23449SMichal Malý 	USB_DEVICE_ID_LOGITECH_DFP_WHEEL
18096440c8aSMichal Malý };
18196440c8aSMichal Malý 
182e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_g25_ident_info = {
183e7c23449SMichal Malý 	0xff00,
184e7c23449SMichal Malý 	0x1200,
185e7c23449SMichal Malý 	USB_DEVICE_ID_LOGITECH_G25_WHEEL
18696440c8aSMichal Malý };
18796440c8aSMichal Malý 
188e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_g27_ident_info = {
189e7c23449SMichal Malý 	0xfff0,
190e7c23449SMichal Malý 	0x1230,
191e7c23449SMichal Malý 	USB_DEVICE_ID_LOGITECH_G27_WHEEL
192e7c23449SMichal Malý };
193e7c23449SMichal Malý 
194e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_dfgt_ident_info = {
195e7c23449SMichal Malý 	0xff00,
196e7c23449SMichal Malý 	0x1300,
197e7c23449SMichal Malý 	USB_DEVICE_ID_LOGITECH_DFGT_WHEEL
198e7c23449SMichal Malý };
199e7c23449SMichal Malý 
200e7c23449SMichal Malý /* Multimode wheel identification checklists */
201e7c23449SMichal Malý static const struct lg4ff_wheel_ident_checklist lg4ff_main_checklist = {
202e7c23449SMichal Malý 	4,
203e7c23449SMichal Malý 	{&lg4ff_dfgt_ident_info,
204e7c23449SMichal Malý 	 &lg4ff_g27_ident_info,
205e7c23449SMichal Malý 	 &lg4ff_g25_ident_info,
206e7c23449SMichal Malý 	 &lg4ff_dfp_ident_info}
207e7c23449SMichal Malý };
208e7c23449SMichal Malý 
209e7c23449SMichal Malý /* Compatibility mode switching commands */
210f31a2de3SMichal Malý /* EXT_CMD9 - Understood by G27 and DFGT */
211f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfex = {
212f31a2de3SMichal Malý 	2,
213f31a2de3SMichal Malý 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* Revert mode upon USB reset */
214f31a2de3SMichal Malý 	 0xf8, 0x09, 0x00, 0x01, 0x00, 0x00, 0x00}	/* Switch mode to DF-EX with detach */
215f31a2de3SMichal Malý };
216f31a2de3SMichal Malý 
217f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfp = {
218f31a2de3SMichal Malý 	2,
219f31a2de3SMichal Malý 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* Revert mode upon USB reset */
220f31a2de3SMichal Malý 	 0xf8, 0x09, 0x01, 0x01, 0x00, 0x00, 0x00}	/* Switch mode to DFP with detach */
221f31a2de3SMichal Malý };
222f31a2de3SMichal Malý 
223f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g25 = {
224f31a2de3SMichal Malý 	2,
225f31a2de3SMichal Malý 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* Revert mode upon USB reset */
226f31a2de3SMichal Malý 	 0xf8, 0x09, 0x02, 0x01, 0x00, 0x00, 0x00}	/* Switch mode to G25 with detach */
227f31a2de3SMichal Malý };
228f31a2de3SMichal Malý 
229f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfgt = {
230f31a2de3SMichal Malý 	2,
231f31a2de3SMichal Malý 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* Revert mode upon USB reset */
232f31a2de3SMichal Malý 	 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00}	/* Switch mode to DFGT with detach */
233f31a2de3SMichal Malý };
234f31a2de3SMichal Malý 
235f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g27 = {
236f31a2de3SMichal Malý 	2,
237f31a2de3SMichal Malý 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* Revert mode upon USB reset */
238f31a2de3SMichal Malý 	 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00}	/* Switch mode to G27 with detach */
239f31a2de3SMichal Malý };
240f31a2de3SMichal Malý 
241f31a2de3SMichal Malý /* EXT_CMD1 - Understood by DFP, G25, G27 and DFGT */
242f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext01_dfp = {
24396440c8aSMichal Malý 	1,
24496440c8aSMichal Malý 	{0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
24596440c8aSMichal Malý };
24696440c8aSMichal Malý 
247f31a2de3SMichal Malý /* EXT_CMD16 - Understood by G25 and G27 */
248f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext16_g25 = {
24996440c8aSMichal Malý 	1,
25096440c8aSMichal Malý 	{0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
25196440c8aSMichal Malý };
25296440c8aSMichal Malý 
2532b24a960SMichal Malý /* Recalculates X axis value accordingly to currently selected range */
2542a552c30SMichal Malý static s32 lg4ff_adjust_dfp_x_axis(s32 value, u16 range)
2552b24a960SMichal Malý {
2562a552c30SMichal Malý 	u16 max_range;
2572a552c30SMichal Malý 	s32 new_value;
2582b24a960SMichal Malý 
2592b24a960SMichal Malý 	if (range == 900)
2602b24a960SMichal Malý 		return value;
2612b24a960SMichal Malý 	else if (range == 200)
2622b24a960SMichal Malý 		return value;
2632b24a960SMichal Malý 	else if (range < 200)
2642b24a960SMichal Malý 		max_range = 200;
2652b24a960SMichal Malý 	else
2662b24a960SMichal Malý 		max_range = 900;
2672b24a960SMichal Malý 
2682b24a960SMichal Malý 	new_value = 8192 + mult_frac(value - 8192, max_range, range);
2692b24a960SMichal Malý 	if (new_value < 0)
2702b24a960SMichal Malý 		return 0;
2712b24a960SMichal Malý 	else if (new_value > 16383)
2722b24a960SMichal Malý 		return 16383;
2732b24a960SMichal Malý 	else
2742b24a960SMichal Malý 		return new_value;
2752b24a960SMichal Malý }
2762b24a960SMichal Malý 
2772b24a960SMichal Malý int lg4ff_adjust_input_event(struct hid_device *hid, struct hid_field *field,
2782a552c30SMichal Malý 			     struct hid_usage *usage, s32 value, struct lg_drv_data *drv_data)
2792b24a960SMichal Malý {
2802b24a960SMichal Malý 	struct lg4ff_device_entry *entry = drv_data->device_props;
2812a552c30SMichal Malý 	s32 new_value = 0;
2822b24a960SMichal Malý 
2832b24a960SMichal Malý 	if (!entry) {
2842b24a960SMichal Malý 		hid_err(hid, "Device properties not found");
2852b24a960SMichal Malý 		return 0;
2862b24a960SMichal Malý 	}
2872b24a960SMichal Malý 
28872529c65SMichal Malý 	switch (entry->wdata.product_id) {
2892b24a960SMichal Malý 	case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
2902b24a960SMichal Malý 		switch (usage->code) {
2912b24a960SMichal Malý 		case ABS_X:
29272529c65SMichal Malý 			new_value = lg4ff_adjust_dfp_x_axis(value, entry->wdata.range);
2932b24a960SMichal Malý 			input_event(field->hidinput->input, usage->type, usage->code, new_value);
2942b24a960SMichal Malý 			return 1;
2952b24a960SMichal Malý 		default:
2962b24a960SMichal Malý 			return 0;
2972b24a960SMichal Malý 		}
2982b24a960SMichal Malý 	default:
2992b24a960SMichal Malý 		return 0;
3002b24a960SMichal Malý 	}
3012b24a960SMichal Malý }
3022b24a960SMichal Malý 
303d0afd848SMichal Malý static int lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
30432c88cbcSSimon Wood {
30532c88cbcSSimon Wood 	struct hid_device *hid = input_get_drvdata(dev);
306c918fe78SMichal Malý 	struct lg4ff_device_entry *entry;
307c918fe78SMichal Malý 	struct lg_drv_data *drv_data;
308c918fe78SMichal Malý 	unsigned long flags;
309*c28abd8cSMichal Malý 	s32 *value;
31032c88cbcSSimon Wood 	int x;
31132c88cbcSSimon Wood 
312c918fe78SMichal Malý 	drv_data = hid_get_drvdata(hid);
313c918fe78SMichal Malý 	if (!drv_data) {
314c918fe78SMichal Malý 		hid_err(hid, "Private driver data not found!\n");
315c918fe78SMichal Malý 		return -EINVAL;
316c918fe78SMichal Malý 	}
317c918fe78SMichal Malý 
318c918fe78SMichal Malý 	entry = drv_data->device_props;
319c918fe78SMichal Malý 	if (!entry) {
320c918fe78SMichal Malý 		hid_err(hid, "Device properties not found!\n");
321c918fe78SMichal Malý 		return -EINVAL;
322c918fe78SMichal Malý 	}
323*c28abd8cSMichal Malý 	value = entry->report->field[0]->value;
324c918fe78SMichal Malý 
325a80fe5d6SMichal Malý #define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0)
32632c88cbcSSimon Wood 
32732c88cbcSSimon Wood 	switch (effect->type) {
32832c88cbcSSimon Wood 	case FF_CONSTANT:
32932c88cbcSSimon Wood 		x = effect->u.ramp.start_level + 0x80;	/* 0x80 is no force */
33032c88cbcSSimon Wood 		CLAMP(x);
33156930e7aSSimon Wood 
332c918fe78SMichal Malý 		spin_lock_irqsave(&entry->report_lock, flags);
33356930e7aSSimon Wood 		if (x == 0x80) {
33456930e7aSSimon Wood 			/* De-activate force in slot-1*/
33556930e7aSSimon Wood 			value[0] = 0x13;
33656930e7aSSimon Wood 			value[1] = 0x00;
33756930e7aSSimon Wood 			value[2] = 0x00;
33856930e7aSSimon Wood 			value[3] = 0x00;
33956930e7aSSimon Wood 			value[4] = 0x00;
34056930e7aSSimon Wood 			value[5] = 0x00;
34156930e7aSSimon Wood 			value[6] = 0x00;
34256930e7aSSimon Wood 
343*c28abd8cSMichal Malý 			hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
344c918fe78SMichal Malý 			spin_unlock_irqrestore(&entry->report_lock, flags);
34556930e7aSSimon Wood 			return 0;
34656930e7aSSimon Wood 		}
34756930e7aSSimon Wood 
34874479ba8SMichal Malý 		value[0] = 0x11;	/* Slot 1 */
34974479ba8SMichal Malý 		value[1] = 0x08;
35074479ba8SMichal Malý 		value[2] = x;
35174479ba8SMichal Malý 		value[3] = 0x80;
35274479ba8SMichal Malý 		value[4] = 0x00;
35374479ba8SMichal Malý 		value[5] = 0x00;
35474479ba8SMichal Malý 		value[6] = 0x00;
35532c88cbcSSimon Wood 
356*c28abd8cSMichal Malý 		hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
357c918fe78SMichal Malý 		spin_unlock_irqrestore(&entry->report_lock, flags);
35832c88cbcSSimon Wood 		break;
35932c88cbcSSimon Wood 	}
36032c88cbcSSimon Wood 	return 0;
36132c88cbcSSimon Wood }
36232c88cbcSSimon Wood 
3636e2de8e0SMichal Malý /* Sends default autocentering command compatible with
3646e2de8e0SMichal Malý  * all wheels except Formula Force EX */
365d0afd848SMichal Malý static void lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude)
36632c88cbcSSimon Wood {
36732c88cbcSSimon Wood 	struct hid_device *hid = input_get_drvdata(dev);
36832c88cbcSSimon Wood 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
36932c88cbcSSimon Wood 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
3702a552c30SMichal Malý 	s32 *value = report->field[0]->value;
3712a552c30SMichal Malý 	u32 expand_a, expand_b;
3721859762eSSimon Wood 	struct lg4ff_device_entry *entry;
3731859762eSSimon Wood 	struct lg_drv_data *drv_data;
374c918fe78SMichal Malý 	unsigned long flags;
3751859762eSSimon Wood 
3761859762eSSimon Wood 	drv_data = hid_get_drvdata(hid);
3771859762eSSimon Wood 	if (!drv_data) {
3781859762eSSimon Wood 		hid_err(hid, "Private driver data not found!\n");
3791859762eSSimon Wood 		return;
3801859762eSSimon Wood 	}
3811859762eSSimon Wood 
3821859762eSSimon Wood 	entry = drv_data->device_props;
3831859762eSSimon Wood 	if (!entry) {
3841859762eSSimon Wood 		hid_err(hid, "Device properties not found!\n");
3851859762eSSimon Wood 		return;
3861859762eSSimon Wood 	}
387*c28abd8cSMichal Malý 	value = entry->report->field[0]->value;
388f8c23156SSimon Wood 
389d2c02da5SSimon Wood 	/* De-activate Auto-Center */
390c918fe78SMichal Malý 	spin_lock_irqsave(&entry->report_lock, flags);
391d2c02da5SSimon Wood 	if (magnitude == 0) {
392d2c02da5SSimon Wood 		value[0] = 0xf5;
393d2c02da5SSimon Wood 		value[1] = 0x00;
394d2c02da5SSimon Wood 		value[2] = 0x00;
395d2c02da5SSimon Wood 		value[3] = 0x00;
396d2c02da5SSimon Wood 		value[4] = 0x00;
397d2c02da5SSimon Wood 		value[5] = 0x00;
398d2c02da5SSimon Wood 		value[6] = 0x00;
399d2c02da5SSimon Wood 
400*c28abd8cSMichal Malý 		hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
401c918fe78SMichal Malý 		spin_unlock_irqrestore(&entry->report_lock, flags);
402d2c02da5SSimon Wood 		return;
403d2c02da5SSimon Wood 	}
404d2c02da5SSimon Wood 
405f8c23156SSimon Wood 	if (magnitude <= 0xaaaa) {
406f8c23156SSimon Wood 		expand_a = 0x0c * magnitude;
407f8c23156SSimon Wood 		expand_b = 0x80 * magnitude;
408f8c23156SSimon Wood 	} else {
409f8c23156SSimon Wood 		expand_a = (0x0c * 0xaaaa) + 0x06 * (magnitude - 0xaaaa);
410f8c23156SSimon Wood 		expand_b = (0x80 * 0xaaaa) + 0xff * (magnitude - 0xaaaa);
411f8c23156SSimon Wood 	}
41232c88cbcSSimon Wood 
4131859762eSSimon Wood 	/* Adjust for non-MOMO wheels */
41472529c65SMichal Malý 	switch (entry->wdata.product_id) {
4151859762eSSimon Wood 	case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL:
4161859762eSSimon Wood 	case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2:
4171859762eSSimon Wood 		break;
4181859762eSSimon Wood 	default:
4191859762eSSimon Wood 		expand_a = expand_a >> 1;
4201859762eSSimon Wood 		break;
4211859762eSSimon Wood 	}
4221859762eSSimon Wood 
42374479ba8SMichal Malý 	value[0] = 0xfe;
42474479ba8SMichal Malý 	value[1] = 0x0d;
425f8c23156SSimon Wood 	value[2] = expand_a / 0xaaaa;
426f8c23156SSimon Wood 	value[3] = expand_a / 0xaaaa;
427f8c23156SSimon Wood 	value[4] = expand_b / 0xaaaa;
42874479ba8SMichal Malý 	value[5] = 0x00;
42974479ba8SMichal Malý 	value[6] = 0x00;
43032c88cbcSSimon Wood 
431*c28abd8cSMichal Malý 	hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
432d2c02da5SSimon Wood 
433d2c02da5SSimon Wood 	/* Activate Auto-Center */
434d2c02da5SSimon Wood 	value[0] = 0x14;
435d2c02da5SSimon Wood 	value[1] = 0x00;
436d2c02da5SSimon Wood 	value[2] = 0x00;
437d2c02da5SSimon Wood 	value[3] = 0x00;
438d2c02da5SSimon Wood 	value[4] = 0x00;
439d2c02da5SSimon Wood 	value[5] = 0x00;
440d2c02da5SSimon Wood 	value[6] = 0x00;
441d2c02da5SSimon Wood 
442*c28abd8cSMichal Malý 	hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
443c918fe78SMichal Malý 	spin_unlock_irqrestore(&entry->report_lock, flags);
44432c88cbcSSimon Wood }
44532c88cbcSSimon Wood 
4466e2de8e0SMichal Malý /* Sends autocentering command compatible with Formula Force EX */
447d0afd848SMichal Malý static void lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude)
4486e2de8e0SMichal Malý {
4496e2de8e0SMichal Malý 	struct hid_device *hid = input_get_drvdata(dev);
450c918fe78SMichal Malý 	struct lg4ff_device_entry *entry;
451c918fe78SMichal Malý 	struct lg_drv_data *drv_data;
452c918fe78SMichal Malý 	unsigned long flags;
453*c28abd8cSMichal Malý 	s32 *value;
4546e2de8e0SMichal Malý 	magnitude = magnitude * 90 / 65535;
4556e2de8e0SMichal Malý 
456c918fe78SMichal Malý 	drv_data = hid_get_drvdata(hid);
457c918fe78SMichal Malý 	if (!drv_data) {
458c918fe78SMichal Malý 		hid_err(hid, "Private driver data not found!\n");
459c918fe78SMichal Malý 		return;
460c918fe78SMichal Malý 	}
461c918fe78SMichal Malý 
462c918fe78SMichal Malý 	entry = drv_data->device_props;
463c918fe78SMichal Malý 	if (!entry) {
464c918fe78SMichal Malý 		hid_err(hid, "Device properties not found!\n");
465c918fe78SMichal Malý 		return;
466c918fe78SMichal Malý 	}
467*c28abd8cSMichal Malý 	value = entry->report->field[0]->value;
468c918fe78SMichal Malý 
469c918fe78SMichal Malý 	spin_lock_irqsave(&entry->report_lock, flags);
47074479ba8SMichal Malý 	value[0] = 0xfe;
47174479ba8SMichal Malý 	value[1] = 0x03;
47274479ba8SMichal Malý 	value[2] = magnitude >> 14;
47374479ba8SMichal Malý 	value[3] = magnitude >> 14;
47474479ba8SMichal Malý 	value[4] = magnitude;
47574479ba8SMichal Malý 	value[5] = 0x00;
47674479ba8SMichal Malý 	value[6] = 0x00;
4776e2de8e0SMichal Malý 
478*c28abd8cSMichal Malý 	hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
479c918fe78SMichal Malý 	spin_unlock_irqrestore(&entry->report_lock, flags);
4806e2de8e0SMichal Malý }
4816e2de8e0SMichal Malý 
48230bb75d7SMichal Malý /* Sends command to set range compatible with G25/G27/Driving Force GT */
483d0afd848SMichal Malý static void lg4ff_set_range_g25(struct hid_device *hid, u16 range)
48430bb75d7SMichal Malý {
485c918fe78SMichal Malý 	struct lg4ff_device_entry *entry;
486c918fe78SMichal Malý 	struct lg_drv_data *drv_data;
487c918fe78SMichal Malý 	unsigned long flags;
488*c28abd8cSMichal Malý 	s32 *value;
48974479ba8SMichal Malý 
490c918fe78SMichal Malý 	drv_data = hid_get_drvdata(hid);
491c918fe78SMichal Malý 	if (!drv_data) {
492c918fe78SMichal Malý 		hid_err(hid, "Private driver data not found!\n");
493c918fe78SMichal Malý 		return;
494c918fe78SMichal Malý 	}
495c918fe78SMichal Malý 
496c918fe78SMichal Malý 	entry = drv_data->device_props;
497c918fe78SMichal Malý 	if (!entry) {
498c918fe78SMichal Malý 		hid_err(hid, "Device properties not found!\n");
499c918fe78SMichal Malý 		return;
500c918fe78SMichal Malý 	}
501*c28abd8cSMichal Malý 	value = entry->report->field[0]->value;
50230bb75d7SMichal Malý 	dbg_hid("G25/G27/DFGT: setting range to %u\n", range);
50330bb75d7SMichal Malý 
504c918fe78SMichal Malý 	spin_lock_irqsave(&entry->report_lock, flags);
50574479ba8SMichal Malý 	value[0] = 0xf8;
50674479ba8SMichal Malý 	value[1] = 0x81;
50774479ba8SMichal Malý 	value[2] = range & 0x00ff;
50874479ba8SMichal Malý 	value[3] = (range & 0xff00) >> 8;
50974479ba8SMichal Malý 	value[4] = 0x00;
51074479ba8SMichal Malý 	value[5] = 0x00;
51174479ba8SMichal Malý 	value[6] = 0x00;
51230bb75d7SMichal Malý 
513*c28abd8cSMichal Malý 	hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
514c918fe78SMichal Malý 	spin_unlock_irqrestore(&entry->report_lock, flags);
51530bb75d7SMichal Malý }
51630bb75d7SMichal Malý 
51730bb75d7SMichal Malý /* Sends commands to set range compatible with Driving Force Pro wheel */
518d0afd848SMichal Malý static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range)
51930bb75d7SMichal Malý {
520c918fe78SMichal Malý 	struct lg4ff_device_entry *entry;
521c918fe78SMichal Malý 	struct lg_drv_data *drv_data;
522c918fe78SMichal Malý 	unsigned long flags;
523*c28abd8cSMichal Malý 	int start_left, start_right, full_range;
524*c28abd8cSMichal Malý 	s32 *value;
52574479ba8SMichal Malý 
526c918fe78SMichal Malý 	drv_data = hid_get_drvdata(hid);
527c918fe78SMichal Malý 	if (!drv_data) {
528c918fe78SMichal Malý 		hid_err(hid, "Private driver data not found!\n");
529c918fe78SMichal Malý 		return;
530c918fe78SMichal Malý 	}
531c918fe78SMichal Malý 
532c918fe78SMichal Malý 	entry = drv_data->device_props;
533c918fe78SMichal Malý 	if (!entry) {
534c918fe78SMichal Malý 		hid_err(hid, "Device properties not found!\n");
535c918fe78SMichal Malý 		return;
536c918fe78SMichal Malý 	}
537*c28abd8cSMichal Malý 	value = entry->report->field[0]->value;
53830bb75d7SMichal Malý 	dbg_hid("Driving Force Pro: setting range to %u\n", range);
53930bb75d7SMichal Malý 
54030bb75d7SMichal Malý 	/* Prepare "coarse" limit command */
541c918fe78SMichal Malý 	spin_lock_irqsave(&entry->report_lock, flags);
54274479ba8SMichal Malý 	value[0] = 0xf8;
54374479ba8SMichal Malý 	value[1] = 0x00;	/* Set later */
54474479ba8SMichal Malý 	value[2] = 0x00;
54574479ba8SMichal Malý 	value[3] = 0x00;
54674479ba8SMichal Malý 	value[4] = 0x00;
54774479ba8SMichal Malý 	value[5] = 0x00;
54874479ba8SMichal Malý 	value[6] = 0x00;
54930bb75d7SMichal Malý 
55030bb75d7SMichal Malý 	if (range > 200) {
551*c28abd8cSMichal Malý 		value[1] = 0x03;
55230bb75d7SMichal Malý 		full_range = 900;
55330bb75d7SMichal Malý 	} else {
554*c28abd8cSMichal Malý 		value[1] = 0x02;
55530bb75d7SMichal Malý 		full_range = 200;
55630bb75d7SMichal Malý 	}
557*c28abd8cSMichal Malý 	hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
55830bb75d7SMichal Malý 
55930bb75d7SMichal Malý 	/* Prepare "fine" limit command */
56074479ba8SMichal Malý 	value[0] = 0x81;
56174479ba8SMichal Malý 	value[1] = 0x0b;
56274479ba8SMichal Malý 	value[2] = 0x00;
56374479ba8SMichal Malý 	value[3] = 0x00;
56474479ba8SMichal Malý 	value[4] = 0x00;
56574479ba8SMichal Malý 	value[5] = 0x00;
56674479ba8SMichal Malý 	value[6] = 0x00;
56730bb75d7SMichal Malý 
56830bb75d7SMichal Malý 	if (range == 200 || range == 900) {	/* Do not apply any fine limit */
569*c28abd8cSMichal Malý 		hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
570c918fe78SMichal Malý 		spin_unlock_irqrestore(&entry->report_lock, flags);
57130bb75d7SMichal Malý 		return;
57230bb75d7SMichal Malý 	}
57330bb75d7SMichal Malý 
57430bb75d7SMichal Malý 	/* Construct fine limit command */
57530bb75d7SMichal Malý 	start_left = (((full_range - range + 1) * 2047) / full_range);
57630bb75d7SMichal Malý 	start_right = 0xfff - start_left;
57730bb75d7SMichal Malý 
57874479ba8SMichal Malý 	value[2] = start_left >> 4;
57974479ba8SMichal Malý 	value[3] = start_right >> 4;
58074479ba8SMichal Malý 	value[4] = 0xff;
58174479ba8SMichal Malý 	value[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
58274479ba8SMichal Malý 	value[6] = 0xff;
58330bb75d7SMichal Malý 
584*c28abd8cSMichal Malý 	hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
585c918fe78SMichal Malý 	spin_unlock_irqrestore(&entry->report_lock, flags);
58630bb75d7SMichal Malý }
58730bb75d7SMichal Malý 
588f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch *lg4ff_get_mode_switch_command(const u16 real_product_id, const u16 target_product_id)
589f31a2de3SMichal Malý {
590f31a2de3SMichal Malý 	switch (real_product_id) {
591f31a2de3SMichal Malý 	case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
592f31a2de3SMichal Malý 		switch (target_product_id) {
593f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
594f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext01_dfp;
595f31a2de3SMichal Malý 		/* DFP can only be switched to its native mode */
596f31a2de3SMichal Malý 		default:
597f31a2de3SMichal Malý 			return NULL;
598f31a2de3SMichal Malý 		}
599f31a2de3SMichal Malý 		break;
600f31a2de3SMichal Malý 	case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
601f31a2de3SMichal Malý 		switch (target_product_id) {
602f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
603f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext01_dfp;
604f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
605f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext16_g25;
606f31a2de3SMichal Malý 		/* G25 can only be switched to DFP mode or its native mode */
607f31a2de3SMichal Malý 		default:
608f31a2de3SMichal Malý 			return NULL;
609f31a2de3SMichal Malý 		}
610f31a2de3SMichal Malý 		break;
611f31a2de3SMichal Malý 	case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
612f31a2de3SMichal Malý 		switch (target_product_id) {
613f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_WHEEL:
614f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext09_dfex;
615f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
616f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext09_dfp;
617f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
618f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext09_g25;
619f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
620f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext09_g27;
621f31a2de3SMichal Malý 		/* G27 can only be switched to DF-EX, DFP, G25 or its native mode */
622f31a2de3SMichal Malý 		default:
623f31a2de3SMichal Malý 			return NULL;
624f31a2de3SMichal Malý 		}
625f31a2de3SMichal Malý 		break;
626f31a2de3SMichal Malý 	case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
627f31a2de3SMichal Malý 		switch (target_product_id) {
628f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_WHEEL:
629f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext09_dfex;
630f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
631f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext09_dfp;
632f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
633f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext09_dfgt;
634f31a2de3SMichal Malý 		/* DFGT can only be switched to DF-EX, DFP or its native mode */
635f31a2de3SMichal Malý 		default:
636f31a2de3SMichal Malý 			return NULL;
637f31a2de3SMichal Malý 		}
638f31a2de3SMichal Malý 		break;
639f31a2de3SMichal Malý 	/* No other wheels have multiple modes */
640f31a2de3SMichal Malý 	default:
641f31a2de3SMichal Malý 		return NULL;
642f31a2de3SMichal Malý 	}
643f31a2de3SMichal Malý }
644f31a2de3SMichal Malý 
645e7c23449SMichal Malý static int lg4ff_switch_compatibility_mode(struct hid_device *hid, const struct lg4ff_compat_mode_switch *s)
64696440c8aSMichal Malý {
647c918fe78SMichal Malý 	struct lg4ff_device_entry *entry;
648c918fe78SMichal Malý 	struct lg_drv_data *drv_data;
649c918fe78SMichal Malý 	unsigned long flags;
650*c28abd8cSMichal Malý 	s32 *value;
651e7c23449SMichal Malý 	u8 i;
65296440c8aSMichal Malý 
653c918fe78SMichal Malý 	drv_data = hid_get_drvdata(hid);
654c918fe78SMichal Malý 	if (!drv_data) {
655c918fe78SMichal Malý 		hid_err(hid, "Private driver data not found!\n");
656c918fe78SMichal Malý 		return -EINVAL;
657c918fe78SMichal Malý 	}
658c918fe78SMichal Malý 
659c918fe78SMichal Malý 	entry = drv_data->device_props;
660c918fe78SMichal Malý 	if (!entry) {
661c918fe78SMichal Malý 		hid_err(hid, "Device properties not found!\n");
662c918fe78SMichal Malý 		return -EINVAL;
663c918fe78SMichal Malý 	}
664*c28abd8cSMichal Malý 	value = entry->report->field[0]->value;
665c918fe78SMichal Malý 
666c918fe78SMichal Malý 	spin_lock_irqsave(&entry->report_lock, flags);
667e7c23449SMichal Malý 	for (i = 0; i < s->cmd_count; i++) {
668c1740d13SMichal Malý 		u8 j;
66996440c8aSMichal Malý 
670c1740d13SMichal Malý 		for (j = 0; j < 7; j++)
671c1740d13SMichal Malý 			value[j] = s->cmd[j + (7*i)];
672c1740d13SMichal Malý 
673*c28abd8cSMichal Malý 		hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
67496440c8aSMichal Malý 	}
675c918fe78SMichal Malý 	spin_unlock_irqrestore(&entry->report_lock, flags);
676c1740d13SMichal Malý 	hid_hw_wait(hid);
677e7c23449SMichal Malý 	return 0;
67896440c8aSMichal Malý }
67932c88cbcSSimon Wood 
680b96d23ecSMichal Malý static ssize_t lg4ff_alternate_modes_show(struct device *dev, struct device_attribute *attr, char *buf)
681b96d23ecSMichal Malý {
682b96d23ecSMichal Malý 	struct hid_device *hid = to_hid_device(dev);
683b96d23ecSMichal Malý 	struct lg4ff_device_entry *entry;
684b96d23ecSMichal Malý 	struct lg_drv_data *drv_data;
685b96d23ecSMichal Malý 	ssize_t count = 0;
686b96d23ecSMichal Malý 	int i;
687b96d23ecSMichal Malý 
688b96d23ecSMichal Malý 	drv_data = hid_get_drvdata(hid);
689b96d23ecSMichal Malý 	if (!drv_data) {
690b96d23ecSMichal Malý 		hid_err(hid, "Private driver data not found!\n");
691b96d23ecSMichal Malý 		return 0;
692b96d23ecSMichal Malý 	}
693b96d23ecSMichal Malý 
694b96d23ecSMichal Malý 	entry = drv_data->device_props;
695b96d23ecSMichal Malý 	if (!entry) {
696b96d23ecSMichal Malý 		hid_err(hid, "Device properties not found!\n");
697b96d23ecSMichal Malý 		return 0;
698b96d23ecSMichal Malý 	}
699b96d23ecSMichal Malý 
70072529c65SMichal Malý 	if (!entry->wdata.real_name) {
701b96d23ecSMichal Malý 		hid_err(hid, "NULL pointer to string\n");
702b96d23ecSMichal Malý 		return 0;
703b96d23ecSMichal Malý 	}
704b96d23ecSMichal Malý 
705b96d23ecSMichal Malý 	for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
70672529c65SMichal Malý 		if (entry->wdata.alternate_modes & BIT(i)) {
707b96d23ecSMichal Malý 			/* Print tag and full name */
708b96d23ecSMichal Malý 			count += scnprintf(buf + count, PAGE_SIZE - count, "%s: %s",
709b96d23ecSMichal Malý 					   lg4ff_alternate_modes[i].tag,
71072529c65SMichal Malý 					   !lg4ff_alternate_modes[i].product_id ? entry->wdata.real_name : lg4ff_alternate_modes[i].name);
711b96d23ecSMichal Malý 			if (count >= PAGE_SIZE - 1)
712b96d23ecSMichal Malý 				return count;
713b96d23ecSMichal Malý 
714b96d23ecSMichal Malý 			/* Mark the currently active mode with an asterisk */
71572529c65SMichal Malý 			if (lg4ff_alternate_modes[i].product_id == entry->wdata.product_id ||
71672529c65SMichal Malý 			    (lg4ff_alternate_modes[i].product_id == 0 && entry->wdata.product_id == entry->wdata.real_product_id))
717b96d23ecSMichal Malý 				count += scnprintf(buf + count, PAGE_SIZE - count, " *\n");
718b96d23ecSMichal Malý 			else
719b96d23ecSMichal Malý 				count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
720b96d23ecSMichal Malý 
721b96d23ecSMichal Malý 			if (count >= PAGE_SIZE - 1)
722b96d23ecSMichal Malý 				return count;
723b96d23ecSMichal Malý 		}
724b96d23ecSMichal Malý 	}
725b96d23ecSMichal Malý 
726b96d23ecSMichal Malý 	return count;
727b96d23ecSMichal Malý }
728b96d23ecSMichal Malý 
729b96d23ecSMichal Malý static ssize_t lg4ff_alternate_modes_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
730b96d23ecSMichal Malý {
731f31a2de3SMichal Malý 	struct hid_device *hid = to_hid_device(dev);
732f31a2de3SMichal Malý 	struct lg4ff_device_entry *entry;
733f31a2de3SMichal Malý 	struct lg_drv_data *drv_data;
734f31a2de3SMichal Malý 	const struct lg4ff_compat_mode_switch *s;
735f31a2de3SMichal Malý 	u16 target_product_id = 0;
736f31a2de3SMichal Malý 	int i, ret;
737f31a2de3SMichal Malý 	char *lbuf;
738f31a2de3SMichal Malý 
739f31a2de3SMichal Malý 	drv_data = hid_get_drvdata(hid);
740f31a2de3SMichal Malý 	if (!drv_data) {
741f31a2de3SMichal Malý 		hid_err(hid, "Private driver data not found!\n");
742f31a2de3SMichal Malý 		return -EINVAL;
743f31a2de3SMichal Malý 	}
744f31a2de3SMichal Malý 
745f31a2de3SMichal Malý 	entry = drv_data->device_props;
746f31a2de3SMichal Malý 	if (!entry) {
747f31a2de3SMichal Malý 		hid_err(hid, "Device properties not found!\n");
748f31a2de3SMichal Malý 		return -EINVAL;
749f31a2de3SMichal Malý 	}
750f31a2de3SMichal Malý 
751f31a2de3SMichal Malý 	/* Allow \n at the end of the input parameter */
752f31a2de3SMichal Malý 	lbuf = kasprintf(GFP_KERNEL, "%s", buf);
753f31a2de3SMichal Malý 	if (!lbuf)
754f31a2de3SMichal Malý 		return -ENOMEM;
755f31a2de3SMichal Malý 
756f31a2de3SMichal Malý 	i = strlen(lbuf);
757f31a2de3SMichal Malý 	if (lbuf[i-1] == '\n') {
758f31a2de3SMichal Malý 		if (i == 1) {
759f31a2de3SMichal Malý 			kfree(lbuf);
760f31a2de3SMichal Malý 			return -EINVAL;
761f31a2de3SMichal Malý 		}
762f31a2de3SMichal Malý 		lbuf[i-1] = '\0';
763f31a2de3SMichal Malý 	}
764f31a2de3SMichal Malý 
765f31a2de3SMichal Malý 	for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
766f31a2de3SMichal Malý 		const u16 mode_product_id = lg4ff_alternate_modes[i].product_id;
767f31a2de3SMichal Malý 		const char *tag = lg4ff_alternate_modes[i].tag;
768f31a2de3SMichal Malý 
76972529c65SMichal Malý 		if (entry->wdata.alternate_modes & BIT(i)) {
770f31a2de3SMichal Malý 			if (!strcmp(tag, lbuf)) {
771f31a2de3SMichal Malý 				if (!mode_product_id)
77272529c65SMichal Malý 					target_product_id = entry->wdata.real_product_id;
773f31a2de3SMichal Malý 				else
774f31a2de3SMichal Malý 					target_product_id = mode_product_id;
775f31a2de3SMichal Malý 				break;
776f31a2de3SMichal Malý 			}
777f31a2de3SMichal Malý 		}
778f31a2de3SMichal Malý 	}
779f31a2de3SMichal Malý 
780f31a2de3SMichal Malý 	if (i == LG4FF_MODE_MAX_IDX) {
781f31a2de3SMichal Malý 		hid_info(hid, "Requested mode \"%s\" is not supported by the device\n", lbuf);
782f31a2de3SMichal Malý 		kfree(lbuf);
783f31a2de3SMichal Malý 		return -EINVAL;
784f31a2de3SMichal Malý 	}
785f31a2de3SMichal Malý 	kfree(lbuf); /* Not needed anymore */
786f31a2de3SMichal Malý 
78772529c65SMichal Malý 	if (target_product_id == entry->wdata.product_id) /* Nothing to do */
788f31a2de3SMichal Malý 		return count;
789f31a2de3SMichal Malý 
790f31a2de3SMichal Malý 	/* Automatic switching has to be disabled for the switch to DF-EX mode to work correctly */
791f31a2de3SMichal Malý 	if (target_product_id == USB_DEVICE_ID_LOGITECH_WHEEL && !lg4ff_no_autoswitch) {
792f31a2de3SMichal Malý 		hid_info(hid, "\"%s\" cannot be switched to \"DF-EX\" mode. Load the \"hid_logitech\" module with \"lg4ff_no_autoswitch=1\" parameter set and try again\n",
79372529c65SMichal Malý 			 entry->wdata.real_name);
794f31a2de3SMichal Malý 		return -EINVAL;
795f31a2de3SMichal Malý 	}
796f31a2de3SMichal Malý 
797f31a2de3SMichal Malý 	/* Take care of hardware limitations */
79872529c65SMichal Malý 	if ((entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_DFP_WHEEL || entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_G25_WHEEL) &&
79972529c65SMichal Malý 	    entry->wdata.product_id > target_product_id) {
80072529c65SMichal Malý 		hid_info(hid, "\"%s\" cannot be switched back into \"%s\" mode\n", entry->wdata.real_name, lg4ff_alternate_modes[i].name);
801f31a2de3SMichal Malý 		return -EINVAL;
802f31a2de3SMichal Malý 	}
803f31a2de3SMichal Malý 
80472529c65SMichal Malý 	s = lg4ff_get_mode_switch_command(entry->wdata.real_product_id, target_product_id);
805f31a2de3SMichal Malý 	if (!s) {
806f31a2de3SMichal Malý 		hid_err(hid, "Invalid target product ID %X\n", target_product_id);
807f31a2de3SMichal Malý 		return -EINVAL;
808f31a2de3SMichal Malý 	}
809f31a2de3SMichal Malý 
810f31a2de3SMichal Malý 	ret = lg4ff_switch_compatibility_mode(hid, s);
811f31a2de3SMichal Malý 	return (ret == 0 ? count : ret);
812b96d23ecSMichal Malý }
813b96d23ecSMichal Malý static DEVICE_ATTR(alternate_modes, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_alternate_modes_show, lg4ff_alternate_modes_store);
814b96d23ecSMichal Malý 
815fbf85e2aSMichal Malý /* Export the currently set range of the wheel */
816fbf85e2aSMichal Malý static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr,
8172f1cec32SVivien Didelot 				char *buf)
81830bb75d7SMichal Malý {
81930bb75d7SMichal Malý 	struct hid_device *hid = to_hid_device(dev);
8203b6b17b7SMichal Malý 	struct lg4ff_device_entry *entry;
8213b6b17b7SMichal Malý 	struct lg_drv_data *drv_data;
82230bb75d7SMichal Malý 	size_t count;
82330bb75d7SMichal Malý 
8243b6b17b7SMichal Malý 	drv_data = hid_get_drvdata(hid);
8253b6b17b7SMichal Malý 	if (!drv_data) {
8263b6b17b7SMichal Malý 		hid_err(hid, "Private driver data not found!\n");
8273b6b17b7SMichal Malý 		return 0;
82830bb75d7SMichal Malý 	}
8293b6b17b7SMichal Malý 
8303b6b17b7SMichal Malý 	entry = drv_data->device_props;
8313b6b17b7SMichal Malý 	if (!entry) {
8323b6b17b7SMichal Malý 		hid_err(hid, "Device properties not found!\n");
83330bb75d7SMichal Malý 		return 0;
83430bb75d7SMichal Malý 	}
83530bb75d7SMichal Malý 
83672529c65SMichal Malý 	count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->wdata.range);
83730bb75d7SMichal Malý 	return count;
83830bb75d7SMichal Malý }
83930bb75d7SMichal Malý 
84030bb75d7SMichal Malý /* Set range to user specified value, call appropriate function
84130bb75d7SMichal Malý  * according to the type of the wheel */
842fbf85e2aSMichal Malý static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr,
8432f1cec32SVivien Didelot 				 const char *buf, size_t count)
84430bb75d7SMichal Malý {
84530bb75d7SMichal Malý 	struct hid_device *hid = to_hid_device(dev);
8463b6b17b7SMichal Malý 	struct lg4ff_device_entry *entry;
8473b6b17b7SMichal Malý 	struct lg_drv_data *drv_data;
8482a552c30SMichal Malý 	u16 range = simple_strtoul(buf, NULL, 10);
84930bb75d7SMichal Malý 
8503b6b17b7SMichal Malý 	drv_data = hid_get_drvdata(hid);
8513b6b17b7SMichal Malý 	if (!drv_data) {
8523b6b17b7SMichal Malý 		hid_err(hid, "Private driver data not found!\n");
85329ff6657SSimon Wood 		return -EINVAL;
85430bb75d7SMichal Malý 	}
8553b6b17b7SMichal Malý 
8563b6b17b7SMichal Malý 	entry = drv_data->device_props;
8573b6b17b7SMichal Malý 	if (!entry) {
8583b6b17b7SMichal Malý 		hid_err(hid, "Device properties not found!\n");
85929ff6657SSimon Wood 		return -EINVAL;
86030bb75d7SMichal Malý 	}
86130bb75d7SMichal Malý 
86230bb75d7SMichal Malý 	if (range == 0)
86372529c65SMichal Malý 		range = entry->wdata.max_range;
86430bb75d7SMichal Malý 
86530bb75d7SMichal Malý 	/* Check if the wheel supports range setting
86630bb75d7SMichal Malý 	 * and that the range is within limits for the wheel */
86772529c65SMichal Malý 	if (entry->wdata.set_range && range >= entry->wdata.min_range && range <= entry->wdata.max_range) {
86872529c65SMichal Malý 		entry->wdata.set_range(hid, range);
86972529c65SMichal Malý 		entry->wdata.range = range;
87030bb75d7SMichal Malý 	}
87130bb75d7SMichal Malý 
87230bb75d7SMichal Malý 	return count;
87330bb75d7SMichal Malý }
874fbf85e2aSMichal Malý static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_range_show, lg4ff_range_store);
87530bb75d7SMichal Malý 
876b96d23ecSMichal Malý static ssize_t lg4ff_real_id_show(struct device *dev, struct device_attribute *attr, char *buf)
877b96d23ecSMichal Malý {
878b96d23ecSMichal Malý 	struct hid_device *hid = to_hid_device(dev);
879b96d23ecSMichal Malý 	struct lg4ff_device_entry *entry;
880b96d23ecSMichal Malý 	struct lg_drv_data *drv_data;
881b96d23ecSMichal Malý 	size_t count;
882b96d23ecSMichal Malý 
883b96d23ecSMichal Malý 	drv_data = hid_get_drvdata(hid);
884b96d23ecSMichal Malý 	if (!drv_data) {
885b96d23ecSMichal Malý 		hid_err(hid, "Private driver data not found!\n");
886b96d23ecSMichal Malý 		return 0;
887b96d23ecSMichal Malý 	}
888b96d23ecSMichal Malý 
889b96d23ecSMichal Malý 	entry = drv_data->device_props;
890b96d23ecSMichal Malý 	if (!entry) {
891b96d23ecSMichal Malý 		hid_err(hid, "Device properties not found!\n");
892b96d23ecSMichal Malý 		return 0;
893b96d23ecSMichal Malý 	}
894b96d23ecSMichal Malý 
89572529c65SMichal Malý 	if (!entry->wdata.real_tag || !entry->wdata.real_name) {
896b96d23ecSMichal Malý 		hid_err(hid, "NULL pointer to string\n");
897b96d23ecSMichal Malý 		return 0;
898b96d23ecSMichal Malý 	}
899b96d23ecSMichal Malý 
90072529c65SMichal Malý 	count = scnprintf(buf, PAGE_SIZE, "%s: %s\n", entry->wdata.real_tag, entry->wdata.real_name);
901b96d23ecSMichal Malý 	return count;
902b96d23ecSMichal Malý }
903b96d23ecSMichal Malý 
904b96d23ecSMichal Malý static ssize_t lg4ff_real_id_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
905b96d23ecSMichal Malý {
906b96d23ecSMichal Malý 	/* Real ID is a read-only value */
907b96d23ecSMichal Malý 	return -EPERM;
908b96d23ecSMichal Malý }
909b96d23ecSMichal Malý static DEVICE_ATTR(real_id, S_IRUGO, lg4ff_real_id_show, lg4ff_real_id_store);
910b96d23ecSMichal Malý 
91122bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS
9122a552c30SMichal Malý static void lg4ff_set_leds(struct hid_device *hid, u8 leds)
91322bcefdcSSimon Wood {
914c918fe78SMichal Malý 	struct lg_drv_data *drv_data;
915c918fe78SMichal Malý 	struct lg4ff_device_entry *entry;
916c918fe78SMichal Malý 	unsigned long flags;
917*c28abd8cSMichal Malý 	s32 *value;
91822bcefdcSSimon Wood 
919c918fe78SMichal Malý 	drv_data = hid_get_drvdata(hid);
920c918fe78SMichal Malý 	if (!drv_data) {
921c918fe78SMichal Malý 		hid_err(hid, "Private driver data not found!\n");
922c918fe78SMichal Malý 		return;
923c918fe78SMichal Malý 	}
924c918fe78SMichal Malý 
925c918fe78SMichal Malý 	entry = drv_data->device_props;
926c918fe78SMichal Malý 	if (!entry) {
927c918fe78SMichal Malý 		hid_err(hid, "Device properties not found!\n");
928c918fe78SMichal Malý 		return;
929c918fe78SMichal Malý 	}
930*c28abd8cSMichal Malý 	value = entry->report->field[0]->value;
931c918fe78SMichal Malý 
932c918fe78SMichal Malý 	spin_lock_irqsave(&entry->report_lock, flags);
93374479ba8SMichal Malý 	value[0] = 0xf8;
93474479ba8SMichal Malý 	value[1] = 0x12;
93574479ba8SMichal Malý 	value[2] = leds;
93674479ba8SMichal Malý 	value[3] = 0x00;
93774479ba8SMichal Malý 	value[4] = 0x00;
93874479ba8SMichal Malý 	value[5] = 0x00;
93974479ba8SMichal Malý 	value[6] = 0x00;
940*c28abd8cSMichal Malý 	hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
941c918fe78SMichal Malý 	spin_unlock_irqrestore(&entry->report_lock, flags);
94222bcefdcSSimon Wood }
94322bcefdcSSimon Wood 
94422bcefdcSSimon Wood static void lg4ff_led_set_brightness(struct led_classdev *led_cdev,
94522bcefdcSSimon Wood 			enum led_brightness value)
94622bcefdcSSimon Wood {
94722bcefdcSSimon Wood 	struct device *dev = led_cdev->dev->parent;
94822bcefdcSSimon Wood 	struct hid_device *hid = container_of(dev, struct hid_device, dev);
9494629fd16SAxel Lin 	struct lg_drv_data *drv_data = hid_get_drvdata(hid);
95022bcefdcSSimon Wood 	struct lg4ff_device_entry *entry;
95122bcefdcSSimon Wood 	int i, state = 0;
95222bcefdcSSimon Wood 
95322bcefdcSSimon Wood 	if (!drv_data) {
95422bcefdcSSimon Wood 		hid_err(hid, "Device data not found.");
95522bcefdcSSimon Wood 		return;
95622bcefdcSSimon Wood 	}
95722bcefdcSSimon Wood 
958371a1d9eSMichal Malý 	entry = drv_data->device_props;
95922bcefdcSSimon Wood 
96022bcefdcSSimon Wood 	if (!entry) {
96122bcefdcSSimon Wood 		hid_err(hid, "Device properties not found.");
96222bcefdcSSimon Wood 		return;
96322bcefdcSSimon Wood 	}
96422bcefdcSSimon Wood 
96522bcefdcSSimon Wood 	for (i = 0; i < 5; i++) {
96672529c65SMichal Malý 		if (led_cdev != entry->wdata.led[i])
96722bcefdcSSimon Wood 			continue;
96872529c65SMichal Malý 		state = (entry->wdata.led_state >> i) & 1;
96922bcefdcSSimon Wood 		if (value == LED_OFF && state) {
97072529c65SMichal Malý 			entry->wdata.led_state &= ~(1 << i);
97172529c65SMichal Malý 			lg4ff_set_leds(hid, entry->wdata.led_state);
97222bcefdcSSimon Wood 		} else if (value != LED_OFF && !state) {
97372529c65SMichal Malý 			entry->wdata.led_state |= 1 << i;
97472529c65SMichal Malý 			lg4ff_set_leds(hid, entry->wdata.led_state);
97522bcefdcSSimon Wood 		}
97622bcefdcSSimon Wood 		break;
97722bcefdcSSimon Wood 	}
97822bcefdcSSimon Wood }
97922bcefdcSSimon Wood 
98022bcefdcSSimon Wood static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev)
98122bcefdcSSimon Wood {
98222bcefdcSSimon Wood 	struct device *dev = led_cdev->dev->parent;
98322bcefdcSSimon Wood 	struct hid_device *hid = container_of(dev, struct hid_device, dev);
9844629fd16SAxel Lin 	struct lg_drv_data *drv_data = hid_get_drvdata(hid);
98522bcefdcSSimon Wood 	struct lg4ff_device_entry *entry;
98622bcefdcSSimon Wood 	int i, value = 0;
98722bcefdcSSimon Wood 
98822bcefdcSSimon Wood 	if (!drv_data) {
98922bcefdcSSimon Wood 		hid_err(hid, "Device data not found.");
99022bcefdcSSimon Wood 		return LED_OFF;
99122bcefdcSSimon Wood 	}
99222bcefdcSSimon Wood 
993371a1d9eSMichal Malý 	entry = drv_data->device_props;
99422bcefdcSSimon Wood 
99522bcefdcSSimon Wood 	if (!entry) {
99622bcefdcSSimon Wood 		hid_err(hid, "Device properties not found.");
99722bcefdcSSimon Wood 		return LED_OFF;
99822bcefdcSSimon Wood 	}
99922bcefdcSSimon Wood 
100022bcefdcSSimon Wood 	for (i = 0; i < 5; i++)
100172529c65SMichal Malý 		if (led_cdev == entry->wdata.led[i]) {
100272529c65SMichal Malý 			value = (entry->wdata.led_state >> i) & 1;
100322bcefdcSSimon Wood 			break;
100422bcefdcSSimon Wood 		}
100522bcefdcSSimon Wood 
100622bcefdcSSimon Wood 	return value ? LED_FULL : LED_OFF;
100722bcefdcSSimon Wood }
100822bcefdcSSimon Wood #endif
100922bcefdcSSimon Wood 
1010e7c23449SMichal Malý static u16 lg4ff_identify_multimode_wheel(struct hid_device *hid, const u16 reported_product_id, const u16 bcdDevice)
1011e7c23449SMichal Malý {
1012e7c23449SMichal Malý 	const struct lg4ff_wheel_ident_checklist *checklist;
1013e7c23449SMichal Malý 	int i, from_idx, to_idx;
1014e7c23449SMichal Malý 
1015e7c23449SMichal Malý 	switch (reported_product_id) {
1016e7c23449SMichal Malý 	case USB_DEVICE_ID_LOGITECH_WHEEL:
1017e7c23449SMichal Malý 	case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
1018e7c23449SMichal Malý 		checklist = &lg4ff_main_checklist;
1019e7c23449SMichal Malý 		from_idx = 0;
1020e7c23449SMichal Malý 		to_idx = checklist->count - 1;
1021e7c23449SMichal Malý 		break;
1022e7c23449SMichal Malý 	case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
1023e7c23449SMichal Malý 		checklist = &lg4ff_main_checklist;
1024e7c23449SMichal Malý 		from_idx = 0;
1025e7c23449SMichal Malý 		to_idx = checklist->count - 2; /* End identity check at G25 */
1026e7c23449SMichal Malý 		break;
1027e7c23449SMichal Malý 	case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
1028e7c23449SMichal Malý 		checklist = &lg4ff_main_checklist;
1029e7c23449SMichal Malý 		from_idx = 1; /* Start identity check at G27 */
1030e7c23449SMichal Malý 		to_idx = checklist->count - 3; /* End identity check at G27 */
1031e7c23449SMichal Malý 		break;
1032e7c23449SMichal Malý 	case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
1033e7c23449SMichal Malý 		checklist = &lg4ff_main_checklist;
1034e7c23449SMichal Malý 		from_idx = 0;
1035e7c23449SMichal Malý 		to_idx = checklist->count - 4; /* End identity check at DFGT */
1036e7c23449SMichal Malý 		break;
1037e7c23449SMichal Malý 	default:
1038e7c23449SMichal Malý 		return 0;
1039e7c23449SMichal Malý 	}
1040e7c23449SMichal Malý 
1041e7c23449SMichal Malý 	for (i = from_idx; i <= to_idx; i++) {
1042e7c23449SMichal Malý 		const u16 mask = checklist->models[i]->mask;
1043e7c23449SMichal Malý 		const u16 result = checklist->models[i]->result;
1044e7c23449SMichal Malý 		const u16 real_product_id = checklist->models[i]->real_product_id;
1045e7c23449SMichal Malý 
1046e7c23449SMichal Malý 		if ((bcdDevice & mask) == result) {
1047e7c23449SMichal Malý 			dbg_hid("Found wheel with real PID %X whose reported PID is %X\n", real_product_id, reported_product_id);
1048e7c23449SMichal Malý 			return real_product_id;
1049e7c23449SMichal Malý 		}
1050e7c23449SMichal Malý 	}
1051e7c23449SMichal Malý 
1052f31a2de3SMichal Malý 	/* No match found. This is either Driving Force or an unknown
1053f31a2de3SMichal Malý 	 * wheel model, do not touch it */
1054e7c23449SMichal Malý 	dbg_hid("Wheel with bcdDevice %X was not recognized as multimode wheel, leaving in its current mode\n", bcdDevice);
1055e7c23449SMichal Malý 	return 0;
1056e7c23449SMichal Malý }
1057e7c23449SMichal Malý 
1058e7c23449SMichal Malý static int lg4ff_handle_multimode_wheel(struct hid_device *hid, u16 *real_product_id, const u16 bcdDevice)
1059e7c23449SMichal Malý {
1060e7c23449SMichal Malý 	const u16 reported_product_id = hid->product;
1061e7c23449SMichal Malý 	int ret;
1062e7c23449SMichal Malý 
1063e7c23449SMichal Malý 	*real_product_id = lg4ff_identify_multimode_wheel(hid, reported_product_id, bcdDevice);
1064e7c23449SMichal Malý 	/* Probed wheel is not a multimode wheel */
1065e7c23449SMichal Malý 	if (!*real_product_id) {
1066e7c23449SMichal Malý 		*real_product_id = reported_product_id;
1067e7c23449SMichal Malý 		dbg_hid("Wheel is not a multimode wheel\n");
1068e7c23449SMichal Malý 		return LG4FF_MMODE_NOT_MULTIMODE;
1069e7c23449SMichal Malý 	}
1070e7c23449SMichal Malý 
1071e7c23449SMichal Malý 	/* Switch from "Driving Force" mode to native mode automatically.
1072e7c23449SMichal Malý 	 * Otherwise keep the wheel in its current mode */
1073e7c23449SMichal Malý 	if (reported_product_id == USB_DEVICE_ID_LOGITECH_WHEEL &&
1074a54dc779SMichal Malý 	    reported_product_id != *real_product_id &&
1075a54dc779SMichal Malý 	    !lg4ff_no_autoswitch) {
1076f31a2de3SMichal Malý 		const struct lg4ff_compat_mode_switch *s = lg4ff_get_mode_switch_command(*real_product_id, *real_product_id);
1077e7c23449SMichal Malý 
1078f31a2de3SMichal Malý 		if (!s) {
1079e7c23449SMichal Malý 			hid_err(hid, "Invalid product id %X\n", *real_product_id);
1080b96d23ecSMichal Malý 			return LG4FF_MMODE_NOT_MULTIMODE;
1081e7c23449SMichal Malý 		}
1082e7c23449SMichal Malý 
1083e7c23449SMichal Malý 		ret = lg4ff_switch_compatibility_mode(hid, s);
1084e7c23449SMichal Malý 		if (ret) {
1085e7c23449SMichal Malý 			/* Wheel could not have been switched to native mode,
1086e7c23449SMichal Malý 			 * leave it in "Driving Force" mode and continue */
1087e7c23449SMichal Malý 			hid_err(hid, "Unable to switch wheel mode, errno %d\n", ret);
1088b96d23ecSMichal Malý 			return LG4FF_MMODE_IS_MULTIMODE;
1089e7c23449SMichal Malý 		}
1090e7c23449SMichal Malý 		return LG4FF_MMODE_SWITCHED;
1091e7c23449SMichal Malý 	}
1092e7c23449SMichal Malý 
1093b96d23ecSMichal Malý 	return LG4FF_MMODE_IS_MULTIMODE;
1094e7c23449SMichal Malý }
1095e7c23449SMichal Malý 
1096e7c23449SMichal Malý 
109732c88cbcSSimon Wood int lg4ff_init(struct hid_device *hid)
109832c88cbcSSimon Wood {
109932c88cbcSSimon Wood 	struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
110032c88cbcSSimon Wood 	struct input_dev *dev = hidinput->input;
1101*c28abd8cSMichal Malý 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
1102*c28abd8cSMichal Malý 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
1103e7c23449SMichal Malý 	const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
1104e7c23449SMichal Malý 	const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
110530bb75d7SMichal Malý 	struct lg4ff_device_entry *entry;
11063b6b17b7SMichal Malý 	struct lg_drv_data *drv_data;
1107b96d23ecSMichal Malý 	int error, i, j;
1108b96d23ecSMichal Malý 	int mmode_ret, mmode_idx = -1;
1109e7c23449SMichal Malý 	u16 real_product_id;
111032c88cbcSSimon Wood 
111132c88cbcSSimon Wood 	/* Check that the report looks ok */
11120fb6bd06SKees Cook 	if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
111332c88cbcSSimon Wood 		return -1;
111432c88cbcSSimon Wood 
111572529c65SMichal Malý 	drv_data = hid_get_drvdata(hid);
111672529c65SMichal Malý 	if (!drv_data) {
111772529c65SMichal Malý 		hid_err(hid, "Cannot add device, private driver data not allocated\n");
111872529c65SMichal Malý 		return -1;
111972529c65SMichal Malý 	}
112072529c65SMichal Malý 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
112172529c65SMichal Malý 	if (!entry)
112272529c65SMichal Malý 		return -ENOMEM;
1123c918fe78SMichal Malý 	spin_lock_init(&entry->report_lock);
1124*c28abd8cSMichal Malý 	entry->report = report;
112572529c65SMichal Malý 	drv_data->device_props = entry;
112672529c65SMichal Malý 
1127e7c23449SMichal Malý 	/* Check if a multimode wheel has been connected and
1128e7c23449SMichal Malý 	 * handle it appropriately */
1129b96d23ecSMichal Malý 	mmode_ret = lg4ff_handle_multimode_wheel(hid, &real_product_id, bcdDevice);
1130e7c23449SMichal Malý 
1131e7c23449SMichal Malý 	/* Wheel has been told to switch to native mode. There is no point in going on
1132e7c23449SMichal Malý 	 * with the initialization as the wheel will do a USB reset when it switches mode
1133e7c23449SMichal Malý 	 */
1134b96d23ecSMichal Malý 	if (mmode_ret == LG4FF_MMODE_SWITCHED)
1135e7c23449SMichal Malý 		return 0;
113672529c65SMichal Malý 	else if (mmode_ret < 0) {
113772529c65SMichal Malý 		hid_err(hid, "Unable to switch device mode during initialization, errno %d\n", mmode_ret);
113872529c65SMichal Malý 		error = mmode_ret;
113972529c65SMichal Malý 		goto err_init;
114072529c65SMichal Malý 	}
1141e7c23449SMichal Malý 
11427362cd22SMichal Malý 	/* Check what wheel has been connected */
11437362cd22SMichal Malý 	for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
11447362cd22SMichal Malý 		if (hid->product == lg4ff_devices[i].product_id) {
11457362cd22SMichal Malý 			dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
11467362cd22SMichal Malý 			break;
11477362cd22SMichal Malý 		}
11487362cd22SMichal Malý 	}
11497362cd22SMichal Malý 
11507362cd22SMichal Malý 	if (i == ARRAY_SIZE(lg4ff_devices)) {
11519c2a6bd1SMichal Malý 		hid_err(hid, "This device is flagged to be handled by the lg4ff module but this module does not know how to handle it. "
11529c2a6bd1SMichal Malý 			     "Please report this as a bug to LKML, Simon Wood <simon@mungewell.org> or "
11539c2a6bd1SMichal Malý 			     "Michal Maly <madcatxster@devoid-pointer.net>\n");
115472529c65SMichal Malý 		error = -1;
115572529c65SMichal Malý 		goto err_init;
11567362cd22SMichal Malý 	}
11577362cd22SMichal Malý 
1158b96d23ecSMichal Malý 	if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1159b96d23ecSMichal Malý 		for (mmode_idx = 0; mmode_idx < ARRAY_SIZE(lg4ff_multimode_wheels); mmode_idx++) {
1160b96d23ecSMichal Malý 			if (real_product_id == lg4ff_multimode_wheels[mmode_idx].product_id)
1161b96d23ecSMichal Malý 				break;
1162b96d23ecSMichal Malý 		}
1163b96d23ecSMichal Malý 
1164b96d23ecSMichal Malý 		if (mmode_idx == ARRAY_SIZE(lg4ff_multimode_wheels)) {
1165b96d23ecSMichal Malý 			hid_err(hid, "Device product ID %X is not listed as a multimode wheel", real_product_id);
116672529c65SMichal Malý 			error = -1;
116772529c65SMichal Malý 			goto err_init;
1168b96d23ecSMichal Malý 		}
1169b96d23ecSMichal Malý 	}
1170b96d23ecSMichal Malý 
11717362cd22SMichal Malý 	/* Set supported force feedback capabilities */
11727362cd22SMichal Malý 	for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
11737362cd22SMichal Malý 		set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
117432c88cbcSSimon Wood 
1175d0afd848SMichal Malý 	error = input_ff_create_memless(dev, NULL, lg4ff_play);
117632c88cbcSSimon Wood 
117732c88cbcSSimon Wood 	if (error)
117872529c65SMichal Malý 		goto err_init;
117932c88cbcSSimon Wood 
118072529c65SMichal Malý 	entry->wdata.product_id = lg4ff_devices[i].product_id;
118172529c65SMichal Malý 	entry->wdata.real_product_id = real_product_id;
118272529c65SMichal Malý 	entry->wdata.min_range = lg4ff_devices[i].min_range;
118372529c65SMichal Malý 	entry->wdata.max_range = lg4ff_devices[i].max_range;
118472529c65SMichal Malý 	entry->wdata.set_range = lg4ff_devices[i].set_range;
1185b96d23ecSMichal Malý 	if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1186b96d23ecSMichal Malý 		BUG_ON(mmode_idx == -1);
118772529c65SMichal Malý 		entry->wdata.alternate_modes = lg4ff_multimode_wheels[mmode_idx].alternate_modes;
118872529c65SMichal Malý 		entry->wdata.real_tag = lg4ff_multimode_wheels[mmode_idx].real_tag;
118972529c65SMichal Malý 		entry->wdata.real_name = lg4ff_multimode_wheels[mmode_idx].real_name;
1190b96d23ecSMichal Malý 	}
119130bb75d7SMichal Malý 
1192114a55cfSSimon Wood 	/* Check if autocentering is available and
1193114a55cfSSimon Wood 	 * set the centering force to zero by default */
1194114a55cfSSimon Wood 	if (test_bit(FF_AUTOCENTER, dev->ffbit)) {
1195e7c23449SMichal Malý 		/* Formula Force EX expects different autocentering command */
1196e7c23449SMichal Malý 		if ((bcdDevice >> 8) == LG4FF_FFEX_REV_MAJ &&
1197e7c23449SMichal Malý 		    (bcdDevice & 0xff) == LG4FF_FFEX_REV_MIN)
1198d0afd848SMichal Malý 			dev->ff->set_autocenter = lg4ff_set_autocenter_ffex;
1199114a55cfSSimon Wood 		else
1200d0afd848SMichal Malý 			dev->ff->set_autocenter = lg4ff_set_autocenter_default;
1201114a55cfSSimon Wood 
1202114a55cfSSimon Wood 		dev->ff->set_autocenter(dev, 0);
1203114a55cfSSimon Wood 	}
1204114a55cfSSimon Wood 
120530bb75d7SMichal Malý 	/* Create sysfs interface */
120630bb75d7SMichal Malý 	error = device_create_file(&hid->dev, &dev_attr_range);
120730bb75d7SMichal Malý 	if (error)
120872529c65SMichal Malý 		goto err_init;
1209b96d23ecSMichal Malý 	if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1210b96d23ecSMichal Malý 		error = device_create_file(&hid->dev, &dev_attr_real_id);
1211b96d23ecSMichal Malý 		if (error)
121272529c65SMichal Malý 			goto err_init;
121372529c65SMichal Malý 
1214b96d23ecSMichal Malý 		error = device_create_file(&hid->dev, &dev_attr_alternate_modes);
1215b96d23ecSMichal Malý 		if (error)
121672529c65SMichal Malý 			goto err_init;
1217b96d23ecSMichal Malý 	}
121830bb75d7SMichal Malý 	dbg_hid("sysfs interface created\n");
121930bb75d7SMichal Malý 
122030bb75d7SMichal Malý 	/* Set the maximum range to start with */
122172529c65SMichal Malý 	entry->wdata.range = entry->wdata.max_range;
122272529c65SMichal Malý 	if (entry->wdata.set_range)
122372529c65SMichal Malý 		entry->wdata.set_range(hid, entry->wdata.range);
122430bb75d7SMichal Malý 
122522bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS
122622bcefdcSSimon Wood 	/* register led subsystem - G27 only */
122772529c65SMichal Malý 	entry->wdata.led_state = 0;
122822bcefdcSSimon Wood 	for (j = 0; j < 5; j++)
122972529c65SMichal Malý 		entry->wdata.led[j] = NULL;
123022bcefdcSSimon Wood 
123122bcefdcSSimon Wood 	if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL) {
123222bcefdcSSimon Wood 		struct led_classdev *led;
123322bcefdcSSimon Wood 		size_t name_sz;
123422bcefdcSSimon Wood 		char *name;
123522bcefdcSSimon Wood 
123622bcefdcSSimon Wood 		lg4ff_set_leds(hid, 0);
123722bcefdcSSimon Wood 
123822bcefdcSSimon Wood 		name_sz = strlen(dev_name(&hid->dev)) + 8;
123922bcefdcSSimon Wood 
124022bcefdcSSimon Wood 		for (j = 0; j < 5; j++) {
124122bcefdcSSimon Wood 			led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
124222bcefdcSSimon Wood 			if (!led) {
124322bcefdcSSimon Wood 				hid_err(hid, "can't allocate memory for LED %d\n", j);
124472529c65SMichal Malý 				goto err_leds;
124522bcefdcSSimon Wood 			}
124622bcefdcSSimon Wood 
124722bcefdcSSimon Wood 			name = (void *)(&led[1]);
124822bcefdcSSimon Wood 			snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1);
124922bcefdcSSimon Wood 			led->name = name;
125022bcefdcSSimon Wood 			led->brightness = 0;
125122bcefdcSSimon Wood 			led->max_brightness = 1;
125222bcefdcSSimon Wood 			led->brightness_get = lg4ff_led_get_brightness;
125322bcefdcSSimon Wood 			led->brightness_set = lg4ff_led_set_brightness;
125422bcefdcSSimon Wood 
125572529c65SMichal Malý 			entry->wdata.led[j] = led;
125622bcefdcSSimon Wood 			error = led_classdev_register(&hid->dev, led);
125722bcefdcSSimon Wood 
125822bcefdcSSimon Wood 			if (error) {
125922bcefdcSSimon Wood 				hid_err(hid, "failed to register LED %d. Aborting.\n", j);
126072529c65SMichal Malý err_leds:
126122bcefdcSSimon Wood 				/* Deregister LEDs (if any) */
126222bcefdcSSimon Wood 				for (j = 0; j < 5; j++) {
126372529c65SMichal Malý 					led = entry->wdata.led[j];
126472529c65SMichal Malý 					entry->wdata.led[j] = NULL;
126522bcefdcSSimon Wood 					if (!led)
126622bcefdcSSimon Wood 						continue;
126722bcefdcSSimon Wood 					led_classdev_unregister(led);
126822bcefdcSSimon Wood 					kfree(led);
126922bcefdcSSimon Wood 				}
127022bcefdcSSimon Wood 				goto out;	/* Let the driver continue without LEDs */
127122bcefdcSSimon Wood 			}
127222bcefdcSSimon Wood 		}
127322bcefdcSSimon Wood 	}
127422bcefdcSSimon Wood out:
1275c6e6dc87SJiri Kosina #endif
127664013800SSimon Wood 	hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n");
127732c88cbcSSimon Wood 	return 0;
127872529c65SMichal Malý 
127972529c65SMichal Malý err_init:
128072529c65SMichal Malý 	drv_data->device_props = NULL;
128172529c65SMichal Malý 	kfree(entry);
128272529c65SMichal Malý 	return error;
128332c88cbcSSimon Wood }
128432c88cbcSSimon Wood 
128530bb75d7SMichal Malý int lg4ff_deinit(struct hid_device *hid)
128630bb75d7SMichal Malý {
128730bb75d7SMichal Malý 	struct lg4ff_device_entry *entry;
12883b6b17b7SMichal Malý 	struct lg_drv_data *drv_data;
12896a2e176bSMichal Malý 
12903b6b17b7SMichal Malý 	drv_data = hid_get_drvdata(hid);
12913b6b17b7SMichal Malý 	if (!drv_data) {
12923b6b17b7SMichal Malý 		hid_err(hid, "Error while deinitializing device, no private driver data.\n");
129330bb75d7SMichal Malý 		return -1;
129430bb75d7SMichal Malý 	}
12953b6b17b7SMichal Malý 	entry = drv_data->device_props;
1296e7c23449SMichal Malý 	if (!entry)
1297e7c23449SMichal Malý 		goto out; /* Nothing more to do */
1298e7c23449SMichal Malý 
1299b96d23ecSMichal Malý 	/* Multimode devices will have at least the "MODE_NATIVE" bit set */
130072529c65SMichal Malý 	if (entry->wdata.alternate_modes) {
1301b96d23ecSMichal Malý 		device_remove_file(&hid->dev, &dev_attr_real_id);
1302b96d23ecSMichal Malý 		device_remove_file(&hid->dev, &dev_attr_alternate_modes);
1303b96d23ecSMichal Malý 	}
1304b96d23ecSMichal Malý 
130572529c65SMichal Malý 	device_remove_file(&hid->dev, &dev_attr_range);
130622bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS
130722bcefdcSSimon Wood 	{
130822bcefdcSSimon Wood 		int j;
130922bcefdcSSimon Wood 		struct led_classdev *led;
131022bcefdcSSimon Wood 
131122bcefdcSSimon Wood 		/* Deregister LEDs (if any) */
131222bcefdcSSimon Wood 		for (j = 0; j < 5; j++) {
131322bcefdcSSimon Wood 
131472529c65SMichal Malý 			led = entry->wdata.led[j];
131572529c65SMichal Malý 			entry->wdata.led[j] = NULL;
131622bcefdcSSimon Wood 			if (!led)
131722bcefdcSSimon Wood 				continue;
131822bcefdcSSimon Wood 			led_classdev_unregister(led);
131922bcefdcSSimon Wood 			kfree(led);
132022bcefdcSSimon Wood 		}
132122bcefdcSSimon Wood 	}
132222bcefdcSSimon Wood #endif
1323b211a638SMichal Malý 	hid_hw_stop(hid);
1324b211a638SMichal Malý 	drv_data->device_props = NULL;
132522bcefdcSSimon Wood 
13263b6b17b7SMichal Malý 	kfree(entry);
1327e7c23449SMichal Malý out:
132830bb75d7SMichal Malý 	dbg_hid("Device successfully unregistered\n");
132930bb75d7SMichal Malý 	return 0;
133030bb75d7SMichal Malý }
1331