xref: /openbmc/linux/drivers/hid/hid-lg4ff.c (revision 72529c65a5399be02c619ed8592e38a3e5f049f3)
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ý 
74*72529c65SMichal 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;
87*72529c65SMichal Malý 
8830bb75d7SMichal Malý 	void (*set_range)(struct hid_device *hid, u16 range);
8930bb75d7SMichal Malý };
9030bb75d7SMichal Malý 
91*72529c65SMichal Malý struct lg4ff_device_entry {
92*72529c65SMichal Malý 	struct lg4ff_wheel_data wdata;
93*72529c65SMichal Malý };
94*72529c65SMichal Malý 
957362cd22SMichal Malý static const signed short lg4ff_wheel_effects[] = {
9632c88cbcSSimon Wood 	FF_CONSTANT,
9732c88cbcSSimon Wood 	FF_AUTOCENTER,
9832c88cbcSSimon Wood 	-1
9932c88cbcSSimon Wood };
10032c88cbcSSimon Wood 
1017362cd22SMichal Malý struct lg4ff_wheel {
1022a552c30SMichal Malý 	const u32 product_id;
1037362cd22SMichal Malý 	const signed short *ff_effects;
1042a552c30SMichal Malý 	const u16 min_range;
1052a552c30SMichal Malý 	const u16 max_range;
10630bb75d7SMichal Malý 	void (*set_range)(struct hid_device *hid, u16 range);
1077362cd22SMichal Malý };
1087362cd22SMichal Malý 
109e7c23449SMichal Malý struct lg4ff_compat_mode_switch {
1102a552c30SMichal Malý 	const u8 cmd_count;	/* Number of commands to send */
1112a552c30SMichal Malý 	const u8 cmd[];
112e7c23449SMichal Malý };
113e7c23449SMichal Malý 
114e7c23449SMichal Malý struct lg4ff_wheel_ident_info {
115e7c23449SMichal Malý 	const u16 mask;
116e7c23449SMichal Malý 	const u16 result;
117e7c23449SMichal Malý 	const u16 real_product_id;
118e7c23449SMichal Malý };
119e7c23449SMichal Malý 
120e7c23449SMichal Malý struct lg4ff_wheel_ident_checklist {
121e7c23449SMichal Malý 	const u32 count;
122e7c23449SMichal Malý 	const struct lg4ff_wheel_ident_info *models[];
123e7c23449SMichal Malý };
124e7c23449SMichal Malý 
125b96d23ecSMichal Malý struct lg4ff_multimode_wheel {
126b96d23ecSMichal Malý 	const u16 product_id;
127b96d23ecSMichal Malý 	const u32 alternate_modes;
128b96d23ecSMichal Malý 	const char *real_tag;
129b96d23ecSMichal Malý 	const char *real_name;
130b96d23ecSMichal Malý };
131b96d23ecSMichal Malý 
132b96d23ecSMichal Malý struct lg4ff_alternate_mode {
133b96d23ecSMichal Malý 	const u16 product_id;
134b96d23ecSMichal Malý 	const char *tag;
135b96d23ecSMichal Malý 	const char *name;
136b96d23ecSMichal Malý };
137b96d23ecSMichal Malý 
1387362cd22SMichal Malý static const struct lg4ff_wheel lg4ff_devices[] = {
13930bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_WHEEL,       lg4ff_wheel_effects, 40, 270, NULL},
14030bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_MOMO_WHEEL,  lg4ff_wheel_effects, 40, 270, NULL},
141d0afd848SMichal Malý 	{USB_DEVICE_ID_LOGITECH_DFP_WHEEL,   lg4ff_wheel_effects, 40, 900, lg4ff_set_range_dfp},
142d0afd848SMichal Malý 	{USB_DEVICE_ID_LOGITECH_G25_WHEEL,   lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
143d0afd848SMichal Malý 	{USB_DEVICE_ID_LOGITECH_DFGT_WHEEL,  lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
144d0afd848SMichal Malý 	{USB_DEVICE_ID_LOGITECH_G27_WHEEL,   lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
14530bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL},
14630bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_WII_WHEEL,   lg4ff_wheel_effects, 40, 270, NULL}
1477362cd22SMichal Malý };
1487362cd22SMichal Malý 
149b96d23ecSMichal Malý static const struct lg4ff_multimode_wheel lg4ff_multimode_wheels[] = {
150b96d23ecSMichal Malý 	{USB_DEVICE_ID_LOGITECH_DFP_WHEEL,
151b96d23ecSMichal Malý 	 LG4FF_MODE_NATIVE | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
152b96d23ecSMichal Malý 	 LG4FF_DFP_TAG, LG4FF_DFP_NAME},
153b96d23ecSMichal Malý 	{USB_DEVICE_ID_LOGITECH_G25_WHEEL,
154b96d23ecSMichal Malý 	 LG4FF_MODE_NATIVE | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
155b96d23ecSMichal Malý 	 LG4FF_G25_TAG, LG4FF_G25_NAME},
156b96d23ecSMichal Malý 	{USB_DEVICE_ID_LOGITECH_DFGT_WHEEL,
157b96d23ecSMichal Malý 	 LG4FF_MODE_NATIVE | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
158b96d23ecSMichal Malý 	 LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
159b96d23ecSMichal Malý 	{USB_DEVICE_ID_LOGITECH_G27_WHEEL,
160b96d23ecSMichal Malý 	 LG4FF_MODE_NATIVE | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
161b96d23ecSMichal Malý 	 LG4FF_G27_TAG, LG4FF_G27_NAME},
162b96d23ecSMichal Malý };
163b96d23ecSMichal Malý 
164b96d23ecSMichal Malý static const struct lg4ff_alternate_mode lg4ff_alternate_modes[] = {
165b96d23ecSMichal Malý 	[LG4FF_MODE_NATIVE_IDX] = {0, "native", ""},
166b96d23ecSMichal Malý 	[LG4FF_MODE_DFEX_IDX] = {USB_DEVICE_ID_LOGITECH_WHEEL, LG4FF_DFEX_TAG, LG4FF_DFEX_NAME},
167b96d23ecSMichal Malý 	[LG4FF_MODE_DFP_IDX] = {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, LG4FF_DFP_TAG, LG4FF_DFP_NAME},
168b96d23ecSMichal Malý 	[LG4FF_MODE_G25_IDX] = {USB_DEVICE_ID_LOGITECH_G25_WHEEL, LG4FF_G25_TAG, LG4FF_G25_NAME},
169b96d23ecSMichal Malý 	[LG4FF_MODE_DFGT_IDX] = {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
170b96d23ecSMichal Malý 	[LG4FF_MODE_G27_IDX] = {USB_DEVICE_ID_LOGITECH_G27_WHEEL, LG4FF_G27_TAG, LG4FF_G27_NAME}
171b96d23ecSMichal Malý };
172b96d23ecSMichal Malý 
173e7c23449SMichal Malý /* Multimode wheel identificators */
174e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_dfp_ident_info = {
175e7c23449SMichal Malý 	0xf000,
176e7c23449SMichal Malý 	0x1000,
177e7c23449SMichal Malý 	USB_DEVICE_ID_LOGITECH_DFP_WHEEL
17896440c8aSMichal Malý };
17996440c8aSMichal Malý 
180e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_g25_ident_info = {
181e7c23449SMichal Malý 	0xff00,
182e7c23449SMichal Malý 	0x1200,
183e7c23449SMichal Malý 	USB_DEVICE_ID_LOGITECH_G25_WHEEL
18496440c8aSMichal Malý };
18596440c8aSMichal Malý 
186e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_g27_ident_info = {
187e7c23449SMichal Malý 	0xfff0,
188e7c23449SMichal Malý 	0x1230,
189e7c23449SMichal Malý 	USB_DEVICE_ID_LOGITECH_G27_WHEEL
190e7c23449SMichal Malý };
191e7c23449SMichal Malý 
192e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_dfgt_ident_info = {
193e7c23449SMichal Malý 	0xff00,
194e7c23449SMichal Malý 	0x1300,
195e7c23449SMichal Malý 	USB_DEVICE_ID_LOGITECH_DFGT_WHEEL
196e7c23449SMichal Malý };
197e7c23449SMichal Malý 
198e7c23449SMichal Malý /* Multimode wheel identification checklists */
199e7c23449SMichal Malý static const struct lg4ff_wheel_ident_checklist lg4ff_main_checklist = {
200e7c23449SMichal Malý 	4,
201e7c23449SMichal Malý 	{&lg4ff_dfgt_ident_info,
202e7c23449SMichal Malý 	 &lg4ff_g27_ident_info,
203e7c23449SMichal Malý 	 &lg4ff_g25_ident_info,
204e7c23449SMichal Malý 	 &lg4ff_dfp_ident_info}
205e7c23449SMichal Malý };
206e7c23449SMichal Malý 
207e7c23449SMichal Malý /* Compatibility mode switching commands */
208f31a2de3SMichal Malý /* EXT_CMD9 - Understood by G27 and DFGT */
209f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfex = {
210f31a2de3SMichal Malý 	2,
211f31a2de3SMichal Malý 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* Revert mode upon USB reset */
212f31a2de3SMichal Malý 	 0xf8, 0x09, 0x00, 0x01, 0x00, 0x00, 0x00}	/* Switch mode to DF-EX with detach */
213f31a2de3SMichal Malý };
214f31a2de3SMichal Malý 
215f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfp = {
216f31a2de3SMichal Malý 	2,
217f31a2de3SMichal Malý 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* Revert mode upon USB reset */
218f31a2de3SMichal Malý 	 0xf8, 0x09, 0x01, 0x01, 0x00, 0x00, 0x00}	/* Switch mode to DFP with detach */
219f31a2de3SMichal Malý };
220f31a2de3SMichal Malý 
221f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g25 = {
222f31a2de3SMichal Malý 	2,
223f31a2de3SMichal Malý 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* Revert mode upon USB reset */
224f31a2de3SMichal Malý 	 0xf8, 0x09, 0x02, 0x01, 0x00, 0x00, 0x00}	/* Switch mode to G25 with detach */
225f31a2de3SMichal Malý };
226f31a2de3SMichal Malý 
227f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfgt = {
228f31a2de3SMichal Malý 	2,
229f31a2de3SMichal Malý 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* Revert mode upon USB reset */
230f31a2de3SMichal Malý 	 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00}	/* Switch mode to DFGT with detach */
231f31a2de3SMichal Malý };
232f31a2de3SMichal Malý 
233f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g27 = {
234f31a2de3SMichal Malý 	2,
235f31a2de3SMichal Malý 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* Revert mode upon USB reset */
236f31a2de3SMichal Malý 	 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00}	/* Switch mode to G27 with detach */
237f31a2de3SMichal Malý };
238f31a2de3SMichal Malý 
239f31a2de3SMichal Malý /* EXT_CMD1 - Understood by DFP, G25, G27 and DFGT */
240f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext01_dfp = {
24196440c8aSMichal Malý 	1,
24296440c8aSMichal Malý 	{0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
24396440c8aSMichal Malý };
24496440c8aSMichal Malý 
245f31a2de3SMichal Malý /* EXT_CMD16 - Understood by G25 and G27 */
246f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext16_g25 = {
24796440c8aSMichal Malý 	1,
24896440c8aSMichal Malý 	{0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
24996440c8aSMichal Malý };
25096440c8aSMichal Malý 
2512b24a960SMichal Malý /* Recalculates X axis value accordingly to currently selected range */
2522a552c30SMichal Malý static s32 lg4ff_adjust_dfp_x_axis(s32 value, u16 range)
2532b24a960SMichal Malý {
2542a552c30SMichal Malý 	u16 max_range;
2552a552c30SMichal Malý 	s32 new_value;
2562b24a960SMichal Malý 
2572b24a960SMichal Malý 	if (range == 900)
2582b24a960SMichal Malý 		return value;
2592b24a960SMichal Malý 	else if (range == 200)
2602b24a960SMichal Malý 		return value;
2612b24a960SMichal Malý 	else if (range < 200)
2622b24a960SMichal Malý 		max_range = 200;
2632b24a960SMichal Malý 	else
2642b24a960SMichal Malý 		max_range = 900;
2652b24a960SMichal Malý 
2662b24a960SMichal Malý 	new_value = 8192 + mult_frac(value - 8192, max_range, range);
2672b24a960SMichal Malý 	if (new_value < 0)
2682b24a960SMichal Malý 		return 0;
2692b24a960SMichal Malý 	else if (new_value > 16383)
2702b24a960SMichal Malý 		return 16383;
2712b24a960SMichal Malý 	else
2722b24a960SMichal Malý 		return new_value;
2732b24a960SMichal Malý }
2742b24a960SMichal Malý 
2752b24a960SMichal Malý int lg4ff_adjust_input_event(struct hid_device *hid, struct hid_field *field,
2762a552c30SMichal Malý 			     struct hid_usage *usage, s32 value, struct lg_drv_data *drv_data)
2772b24a960SMichal Malý {
2782b24a960SMichal Malý 	struct lg4ff_device_entry *entry = drv_data->device_props;
2792a552c30SMichal Malý 	s32 new_value = 0;
2802b24a960SMichal Malý 
2812b24a960SMichal Malý 	if (!entry) {
2822b24a960SMichal Malý 		hid_err(hid, "Device properties not found");
2832b24a960SMichal Malý 		return 0;
2842b24a960SMichal Malý 	}
2852b24a960SMichal Malý 
286*72529c65SMichal Malý 	switch (entry->wdata.product_id) {
2872b24a960SMichal Malý 	case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
2882b24a960SMichal Malý 		switch (usage->code) {
2892b24a960SMichal Malý 		case ABS_X:
290*72529c65SMichal Malý 			new_value = lg4ff_adjust_dfp_x_axis(value, entry->wdata.range);
2912b24a960SMichal Malý 			input_event(field->hidinput->input, usage->type, usage->code, new_value);
2922b24a960SMichal Malý 			return 1;
2932b24a960SMichal Malý 		default:
2942b24a960SMichal Malý 			return 0;
2952b24a960SMichal Malý 		}
2962b24a960SMichal Malý 	default:
2972b24a960SMichal Malý 		return 0;
2982b24a960SMichal Malý 	}
2992b24a960SMichal Malý }
3002b24a960SMichal Malý 
301d0afd848SMichal Malý static int lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
30232c88cbcSSimon Wood {
30332c88cbcSSimon Wood 	struct hid_device *hid = input_get_drvdata(dev);
30432c88cbcSSimon Wood 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
30532c88cbcSSimon Wood 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
3062a552c30SMichal Malý 	s32 *value = report->field[0]->value;
30732c88cbcSSimon Wood 	int x;
30832c88cbcSSimon Wood 
309a80fe5d6SMichal Malý #define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0)
31032c88cbcSSimon Wood 
31132c88cbcSSimon Wood 	switch (effect->type) {
31232c88cbcSSimon Wood 	case FF_CONSTANT:
31332c88cbcSSimon Wood 		x = effect->u.ramp.start_level + 0x80;	/* 0x80 is no force */
31432c88cbcSSimon Wood 		CLAMP(x);
31556930e7aSSimon Wood 
31656930e7aSSimon Wood 		if (x == 0x80) {
31756930e7aSSimon Wood 			/* De-activate force in slot-1*/
31856930e7aSSimon Wood 			value[0] = 0x13;
31956930e7aSSimon Wood 			value[1] = 0x00;
32056930e7aSSimon Wood 			value[2] = 0x00;
32156930e7aSSimon Wood 			value[3] = 0x00;
32256930e7aSSimon Wood 			value[4] = 0x00;
32356930e7aSSimon Wood 			value[5] = 0x00;
32456930e7aSSimon Wood 			value[6] = 0x00;
32556930e7aSSimon Wood 
32656930e7aSSimon Wood 			hid_hw_request(hid, report, HID_REQ_SET_REPORT);
32756930e7aSSimon Wood 			return 0;
32856930e7aSSimon Wood 		}
32956930e7aSSimon Wood 
33074479ba8SMichal Malý 		value[0] = 0x11;	/* Slot 1 */
33174479ba8SMichal Malý 		value[1] = 0x08;
33274479ba8SMichal Malý 		value[2] = x;
33374479ba8SMichal Malý 		value[3] = 0x80;
33474479ba8SMichal Malý 		value[4] = 0x00;
33574479ba8SMichal Malý 		value[5] = 0x00;
33674479ba8SMichal Malý 		value[6] = 0x00;
33732c88cbcSSimon Wood 
338d8814272SBenjamin Tissoires 		hid_hw_request(hid, report, HID_REQ_SET_REPORT);
33932c88cbcSSimon Wood 		break;
34032c88cbcSSimon Wood 	}
34132c88cbcSSimon Wood 	return 0;
34232c88cbcSSimon Wood }
34332c88cbcSSimon Wood 
3446e2de8e0SMichal Malý /* Sends default autocentering command compatible with
3456e2de8e0SMichal Malý  * all wheels except Formula Force EX */
346d0afd848SMichal Malý static void lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude)
34732c88cbcSSimon Wood {
34832c88cbcSSimon Wood 	struct hid_device *hid = input_get_drvdata(dev);
34932c88cbcSSimon Wood 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
35032c88cbcSSimon Wood 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
3512a552c30SMichal Malý 	s32 *value = report->field[0]->value;
3522a552c30SMichal Malý 	u32 expand_a, expand_b;
3531859762eSSimon Wood 	struct lg4ff_device_entry *entry;
3541859762eSSimon Wood 	struct lg_drv_data *drv_data;
3551859762eSSimon Wood 
3561859762eSSimon Wood 	drv_data = hid_get_drvdata(hid);
3571859762eSSimon Wood 	if (!drv_data) {
3581859762eSSimon Wood 		hid_err(hid, "Private driver data not found!\n");
3591859762eSSimon Wood 		return;
3601859762eSSimon Wood 	}
3611859762eSSimon Wood 
3621859762eSSimon Wood 	entry = drv_data->device_props;
3631859762eSSimon Wood 	if (!entry) {
3641859762eSSimon Wood 		hid_err(hid, "Device properties not found!\n");
3651859762eSSimon Wood 		return;
3661859762eSSimon Wood 	}
367f8c23156SSimon Wood 
368d2c02da5SSimon Wood 	/* De-activate Auto-Center */
369d2c02da5SSimon Wood 	if (magnitude == 0) {
370d2c02da5SSimon Wood 		value[0] = 0xf5;
371d2c02da5SSimon Wood 		value[1] = 0x00;
372d2c02da5SSimon Wood 		value[2] = 0x00;
373d2c02da5SSimon Wood 		value[3] = 0x00;
374d2c02da5SSimon Wood 		value[4] = 0x00;
375d2c02da5SSimon Wood 		value[5] = 0x00;
376d2c02da5SSimon Wood 		value[6] = 0x00;
377d2c02da5SSimon Wood 
378d2c02da5SSimon Wood 		hid_hw_request(hid, report, HID_REQ_SET_REPORT);
379d2c02da5SSimon Wood 		return;
380d2c02da5SSimon Wood 	}
381d2c02da5SSimon Wood 
382f8c23156SSimon Wood 	if (magnitude <= 0xaaaa) {
383f8c23156SSimon Wood 		expand_a = 0x0c * magnitude;
384f8c23156SSimon Wood 		expand_b = 0x80 * magnitude;
385f8c23156SSimon Wood 	} else {
386f8c23156SSimon Wood 		expand_a = (0x0c * 0xaaaa) + 0x06 * (magnitude - 0xaaaa);
387f8c23156SSimon Wood 		expand_b = (0x80 * 0xaaaa) + 0xff * (magnitude - 0xaaaa);
388f8c23156SSimon Wood 	}
38932c88cbcSSimon Wood 
3901859762eSSimon Wood 	/* Adjust for non-MOMO wheels */
391*72529c65SMichal Malý 	switch (entry->wdata.product_id) {
3921859762eSSimon Wood 	case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL:
3931859762eSSimon Wood 	case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2:
3941859762eSSimon Wood 		break;
3951859762eSSimon Wood 	default:
3961859762eSSimon Wood 		expand_a = expand_a >> 1;
3971859762eSSimon Wood 		break;
3981859762eSSimon Wood 	}
3991859762eSSimon Wood 
40074479ba8SMichal Malý 	value[0] = 0xfe;
40174479ba8SMichal Malý 	value[1] = 0x0d;
402f8c23156SSimon Wood 	value[2] = expand_a / 0xaaaa;
403f8c23156SSimon Wood 	value[3] = expand_a / 0xaaaa;
404f8c23156SSimon Wood 	value[4] = expand_b / 0xaaaa;
40574479ba8SMichal Malý 	value[5] = 0x00;
40674479ba8SMichal Malý 	value[6] = 0x00;
40732c88cbcSSimon Wood 
408d8814272SBenjamin Tissoires 	hid_hw_request(hid, report, HID_REQ_SET_REPORT);
409d2c02da5SSimon Wood 
410d2c02da5SSimon Wood 	/* Activate Auto-Center */
411d2c02da5SSimon Wood 	value[0] = 0x14;
412d2c02da5SSimon Wood 	value[1] = 0x00;
413d2c02da5SSimon Wood 	value[2] = 0x00;
414d2c02da5SSimon Wood 	value[3] = 0x00;
415d2c02da5SSimon Wood 	value[4] = 0x00;
416d2c02da5SSimon Wood 	value[5] = 0x00;
417d2c02da5SSimon Wood 	value[6] = 0x00;
418d2c02da5SSimon Wood 
419d2c02da5SSimon Wood 	hid_hw_request(hid, report, HID_REQ_SET_REPORT);
42032c88cbcSSimon Wood }
42132c88cbcSSimon Wood 
4226e2de8e0SMichal Malý /* Sends autocentering command compatible with Formula Force EX */
423d0afd848SMichal Malý static void lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude)
4246e2de8e0SMichal Malý {
4256e2de8e0SMichal Malý 	struct hid_device *hid = input_get_drvdata(dev);
4266e2de8e0SMichal Malý 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
4276e2de8e0SMichal Malý 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
4282a552c30SMichal Malý 	s32 *value = report->field[0]->value;
4296e2de8e0SMichal Malý 	magnitude = magnitude * 90 / 65535;
4306e2de8e0SMichal Malý 
43174479ba8SMichal Malý 	value[0] = 0xfe;
43274479ba8SMichal Malý 	value[1] = 0x03;
43374479ba8SMichal Malý 	value[2] = magnitude >> 14;
43474479ba8SMichal Malý 	value[3] = magnitude >> 14;
43574479ba8SMichal Malý 	value[4] = magnitude;
43674479ba8SMichal Malý 	value[5] = 0x00;
43774479ba8SMichal Malý 	value[6] = 0x00;
4386e2de8e0SMichal Malý 
439d8814272SBenjamin Tissoires 	hid_hw_request(hid, report, HID_REQ_SET_REPORT);
4406e2de8e0SMichal Malý }
4416e2de8e0SMichal Malý 
44230bb75d7SMichal Malý /* Sends command to set range compatible with G25/G27/Driving Force GT */
443d0afd848SMichal Malý static void lg4ff_set_range_g25(struct hid_device *hid, u16 range)
44430bb75d7SMichal Malý {
44530bb75d7SMichal Malý 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
44630bb75d7SMichal Malý 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
4472a552c30SMichal Malý 	s32 *value = report->field[0]->value;
44874479ba8SMichal Malý 
44930bb75d7SMichal Malý 	dbg_hid("G25/G27/DFGT: setting range to %u\n", range);
45030bb75d7SMichal Malý 
45174479ba8SMichal Malý 	value[0] = 0xf8;
45274479ba8SMichal Malý 	value[1] = 0x81;
45374479ba8SMichal Malý 	value[2] = range & 0x00ff;
45474479ba8SMichal Malý 	value[3] = (range & 0xff00) >> 8;
45574479ba8SMichal Malý 	value[4] = 0x00;
45674479ba8SMichal Malý 	value[5] = 0x00;
45774479ba8SMichal Malý 	value[6] = 0x00;
45830bb75d7SMichal Malý 
459d8814272SBenjamin Tissoires 	hid_hw_request(hid, report, HID_REQ_SET_REPORT);
46030bb75d7SMichal Malý }
46130bb75d7SMichal Malý 
46230bb75d7SMichal Malý /* Sends commands to set range compatible with Driving Force Pro wheel */
463d0afd848SMichal Malý static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range)
46430bb75d7SMichal Malý {
46530bb75d7SMichal Malý 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
46630bb75d7SMichal Malý 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
46730bb75d7SMichal Malý 	int start_left, start_right, full_range;
4682a552c30SMichal Malý 	s32 *value = report->field[0]->value;
46974479ba8SMichal Malý 
47030bb75d7SMichal Malý 	dbg_hid("Driving Force Pro: setting range to %u\n", range);
47130bb75d7SMichal Malý 
47230bb75d7SMichal Malý 	/* Prepare "coarse" limit command */
47374479ba8SMichal Malý 	value[0] = 0xf8;
47474479ba8SMichal Malý 	value[1] = 0x00;	/* Set later */
47574479ba8SMichal Malý 	value[2] = 0x00;
47674479ba8SMichal Malý 	value[3] = 0x00;
47774479ba8SMichal Malý 	value[4] = 0x00;
47874479ba8SMichal Malý 	value[5] = 0x00;
47974479ba8SMichal Malý 	value[6] = 0x00;
48030bb75d7SMichal Malý 
48130bb75d7SMichal Malý 	if (range > 200) {
48230bb75d7SMichal Malý 		report->field[0]->value[1] = 0x03;
48330bb75d7SMichal Malý 		full_range = 900;
48430bb75d7SMichal Malý 	} else {
48530bb75d7SMichal Malý 		report->field[0]->value[1] = 0x02;
48630bb75d7SMichal Malý 		full_range = 200;
48730bb75d7SMichal Malý 	}
488d8814272SBenjamin Tissoires 	hid_hw_request(hid, report, HID_REQ_SET_REPORT);
48930bb75d7SMichal Malý 
49030bb75d7SMichal Malý 	/* Prepare "fine" limit command */
49174479ba8SMichal Malý 	value[0] = 0x81;
49274479ba8SMichal Malý 	value[1] = 0x0b;
49374479ba8SMichal Malý 	value[2] = 0x00;
49474479ba8SMichal Malý 	value[3] = 0x00;
49574479ba8SMichal Malý 	value[4] = 0x00;
49674479ba8SMichal Malý 	value[5] = 0x00;
49774479ba8SMichal Malý 	value[6] = 0x00;
49830bb75d7SMichal Malý 
49930bb75d7SMichal Malý 	if (range == 200 || range == 900) {	/* Do not apply any fine limit */
500d8814272SBenjamin Tissoires 		hid_hw_request(hid, report, HID_REQ_SET_REPORT);
50130bb75d7SMichal Malý 		return;
50230bb75d7SMichal Malý 	}
50330bb75d7SMichal Malý 
50430bb75d7SMichal Malý 	/* Construct fine limit command */
50530bb75d7SMichal Malý 	start_left = (((full_range - range + 1) * 2047) / full_range);
50630bb75d7SMichal Malý 	start_right = 0xfff - start_left;
50730bb75d7SMichal Malý 
50874479ba8SMichal Malý 	value[2] = start_left >> 4;
50974479ba8SMichal Malý 	value[3] = start_right >> 4;
51074479ba8SMichal Malý 	value[4] = 0xff;
51174479ba8SMichal Malý 	value[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
51274479ba8SMichal Malý 	value[6] = 0xff;
51330bb75d7SMichal Malý 
514d8814272SBenjamin Tissoires 	hid_hw_request(hid, report, HID_REQ_SET_REPORT);
51530bb75d7SMichal Malý }
51630bb75d7SMichal Malý 
517f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch *lg4ff_get_mode_switch_command(const u16 real_product_id, const u16 target_product_id)
518f31a2de3SMichal Malý {
519f31a2de3SMichal Malý 	switch (real_product_id) {
520f31a2de3SMichal Malý 	case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
521f31a2de3SMichal Malý 		switch (target_product_id) {
522f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
523f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext01_dfp;
524f31a2de3SMichal Malý 		/* DFP can only be switched to its native mode */
525f31a2de3SMichal Malý 		default:
526f31a2de3SMichal Malý 			return NULL;
527f31a2de3SMichal Malý 		}
528f31a2de3SMichal Malý 		break;
529f31a2de3SMichal Malý 	case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
530f31a2de3SMichal Malý 		switch (target_product_id) {
531f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
532f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext01_dfp;
533f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
534f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext16_g25;
535f31a2de3SMichal Malý 		/* G25 can only be switched to DFP mode or its native mode */
536f31a2de3SMichal Malý 		default:
537f31a2de3SMichal Malý 			return NULL;
538f31a2de3SMichal Malý 		}
539f31a2de3SMichal Malý 		break;
540f31a2de3SMichal Malý 	case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
541f31a2de3SMichal Malý 		switch (target_product_id) {
542f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_WHEEL:
543f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext09_dfex;
544f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
545f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext09_dfp;
546f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
547f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext09_g25;
548f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
549f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext09_g27;
550f31a2de3SMichal Malý 		/* G27 can only be switched to DF-EX, DFP, G25 or its native mode */
551f31a2de3SMichal Malý 		default:
552f31a2de3SMichal Malý 			return NULL;
553f31a2de3SMichal Malý 		}
554f31a2de3SMichal Malý 		break;
555f31a2de3SMichal Malý 	case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
556f31a2de3SMichal Malý 		switch (target_product_id) {
557f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_WHEEL:
558f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext09_dfex;
559f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
560f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext09_dfp;
561f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
562f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext09_dfgt;
563f31a2de3SMichal Malý 		/* DFGT can only be switched to DF-EX, DFP or its native mode */
564f31a2de3SMichal Malý 		default:
565f31a2de3SMichal Malý 			return NULL;
566f31a2de3SMichal Malý 		}
567f31a2de3SMichal Malý 		break;
568f31a2de3SMichal Malý 	/* No other wheels have multiple modes */
569f31a2de3SMichal Malý 	default:
570f31a2de3SMichal Malý 		return NULL;
571f31a2de3SMichal Malý 	}
572f31a2de3SMichal Malý }
573f31a2de3SMichal Malý 
574e7c23449SMichal Malý static int lg4ff_switch_compatibility_mode(struct hid_device *hid, const struct lg4ff_compat_mode_switch *s)
57596440c8aSMichal Malý {
576c1740d13SMichal Malý 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
577c1740d13SMichal Malý 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
5782a552c30SMichal Malý 	s32 *value = report->field[0]->value;
579e7c23449SMichal Malý 	u8 i;
58096440c8aSMichal Malý 
581e7c23449SMichal Malý 	for (i = 0; i < s->cmd_count; i++) {
582c1740d13SMichal Malý 		u8 j;
58396440c8aSMichal Malý 
584c1740d13SMichal Malý 		for (j = 0; j < 7; j++)
585c1740d13SMichal Malý 			value[j] = s->cmd[j + (7*i)];
586c1740d13SMichal Malý 
587c1740d13SMichal Malý 		hid_hw_request(hid, report, HID_REQ_SET_REPORT);
58896440c8aSMichal Malý 	}
589c1740d13SMichal Malý 	hid_hw_wait(hid);
590e7c23449SMichal Malý 	return 0;
59196440c8aSMichal Malý }
59232c88cbcSSimon Wood 
593b96d23ecSMichal Malý static ssize_t lg4ff_alternate_modes_show(struct device *dev, struct device_attribute *attr, char *buf)
594b96d23ecSMichal Malý {
595b96d23ecSMichal Malý 	struct hid_device *hid = to_hid_device(dev);
596b96d23ecSMichal Malý 	struct lg4ff_device_entry *entry;
597b96d23ecSMichal Malý 	struct lg_drv_data *drv_data;
598b96d23ecSMichal Malý 	ssize_t count = 0;
599b96d23ecSMichal Malý 	int i;
600b96d23ecSMichal Malý 
601b96d23ecSMichal Malý 	drv_data = hid_get_drvdata(hid);
602b96d23ecSMichal Malý 	if (!drv_data) {
603b96d23ecSMichal Malý 		hid_err(hid, "Private driver data not found!\n");
604b96d23ecSMichal Malý 		return 0;
605b96d23ecSMichal Malý 	}
606b96d23ecSMichal Malý 
607b96d23ecSMichal Malý 	entry = drv_data->device_props;
608b96d23ecSMichal Malý 	if (!entry) {
609b96d23ecSMichal Malý 		hid_err(hid, "Device properties not found!\n");
610b96d23ecSMichal Malý 		return 0;
611b96d23ecSMichal Malý 	}
612b96d23ecSMichal Malý 
613*72529c65SMichal Malý 	if (!entry->wdata.real_name) {
614b96d23ecSMichal Malý 		hid_err(hid, "NULL pointer to string\n");
615b96d23ecSMichal Malý 		return 0;
616b96d23ecSMichal Malý 	}
617b96d23ecSMichal Malý 
618b96d23ecSMichal Malý 	for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
619*72529c65SMichal Malý 		if (entry->wdata.alternate_modes & BIT(i)) {
620b96d23ecSMichal Malý 			/* Print tag and full name */
621b96d23ecSMichal Malý 			count += scnprintf(buf + count, PAGE_SIZE - count, "%s: %s",
622b96d23ecSMichal Malý 					   lg4ff_alternate_modes[i].tag,
623*72529c65SMichal Malý 					   !lg4ff_alternate_modes[i].product_id ? entry->wdata.real_name : lg4ff_alternate_modes[i].name);
624b96d23ecSMichal Malý 			if (count >= PAGE_SIZE - 1)
625b96d23ecSMichal Malý 				return count;
626b96d23ecSMichal Malý 
627b96d23ecSMichal Malý 			/* Mark the currently active mode with an asterisk */
628*72529c65SMichal Malý 			if (lg4ff_alternate_modes[i].product_id == entry->wdata.product_id ||
629*72529c65SMichal Malý 			    (lg4ff_alternate_modes[i].product_id == 0 && entry->wdata.product_id == entry->wdata.real_product_id))
630b96d23ecSMichal Malý 				count += scnprintf(buf + count, PAGE_SIZE - count, " *\n");
631b96d23ecSMichal Malý 			else
632b96d23ecSMichal Malý 				count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
633b96d23ecSMichal Malý 
634b96d23ecSMichal Malý 			if (count >= PAGE_SIZE - 1)
635b96d23ecSMichal Malý 				return count;
636b96d23ecSMichal Malý 		}
637b96d23ecSMichal Malý 	}
638b96d23ecSMichal Malý 
639b96d23ecSMichal Malý 	return count;
640b96d23ecSMichal Malý }
641b96d23ecSMichal Malý 
642b96d23ecSMichal Malý static ssize_t lg4ff_alternate_modes_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
643b96d23ecSMichal Malý {
644f31a2de3SMichal Malý 	struct hid_device *hid = to_hid_device(dev);
645f31a2de3SMichal Malý 	struct lg4ff_device_entry *entry;
646f31a2de3SMichal Malý 	struct lg_drv_data *drv_data;
647f31a2de3SMichal Malý 	const struct lg4ff_compat_mode_switch *s;
648f31a2de3SMichal Malý 	u16 target_product_id = 0;
649f31a2de3SMichal Malý 	int i, ret;
650f31a2de3SMichal Malý 	char *lbuf;
651f31a2de3SMichal Malý 
652f31a2de3SMichal Malý 	drv_data = hid_get_drvdata(hid);
653f31a2de3SMichal Malý 	if (!drv_data) {
654f31a2de3SMichal Malý 		hid_err(hid, "Private driver data not found!\n");
655f31a2de3SMichal Malý 		return -EINVAL;
656f31a2de3SMichal Malý 	}
657f31a2de3SMichal Malý 
658f31a2de3SMichal Malý 	entry = drv_data->device_props;
659f31a2de3SMichal Malý 	if (!entry) {
660f31a2de3SMichal Malý 		hid_err(hid, "Device properties not found!\n");
661f31a2de3SMichal Malý 		return -EINVAL;
662f31a2de3SMichal Malý 	}
663f31a2de3SMichal Malý 
664f31a2de3SMichal Malý 	/* Allow \n at the end of the input parameter */
665f31a2de3SMichal Malý 	lbuf = kasprintf(GFP_KERNEL, "%s", buf);
666f31a2de3SMichal Malý 	if (!lbuf)
667f31a2de3SMichal Malý 		return -ENOMEM;
668f31a2de3SMichal Malý 
669f31a2de3SMichal Malý 	i = strlen(lbuf);
670f31a2de3SMichal Malý 	if (lbuf[i-1] == '\n') {
671f31a2de3SMichal Malý 		if (i == 1) {
672f31a2de3SMichal Malý 			kfree(lbuf);
673f31a2de3SMichal Malý 			return -EINVAL;
674f31a2de3SMichal Malý 		}
675f31a2de3SMichal Malý 		lbuf[i-1] = '\0';
676f31a2de3SMichal Malý 	}
677f31a2de3SMichal Malý 
678f31a2de3SMichal Malý 	for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
679f31a2de3SMichal Malý 		const u16 mode_product_id = lg4ff_alternate_modes[i].product_id;
680f31a2de3SMichal Malý 		const char *tag = lg4ff_alternate_modes[i].tag;
681f31a2de3SMichal Malý 
682*72529c65SMichal Malý 		if (entry->wdata.alternate_modes & BIT(i)) {
683f31a2de3SMichal Malý 			if (!strcmp(tag, lbuf)) {
684f31a2de3SMichal Malý 				if (!mode_product_id)
685*72529c65SMichal Malý 					target_product_id = entry->wdata.real_product_id;
686f31a2de3SMichal Malý 				else
687f31a2de3SMichal Malý 					target_product_id = mode_product_id;
688f31a2de3SMichal Malý 				break;
689f31a2de3SMichal Malý 			}
690f31a2de3SMichal Malý 		}
691f31a2de3SMichal Malý 	}
692f31a2de3SMichal Malý 
693f31a2de3SMichal Malý 	if (i == LG4FF_MODE_MAX_IDX) {
694f31a2de3SMichal Malý 		hid_info(hid, "Requested mode \"%s\" is not supported by the device\n", lbuf);
695f31a2de3SMichal Malý 		kfree(lbuf);
696f31a2de3SMichal Malý 		return -EINVAL;
697f31a2de3SMichal Malý 	}
698f31a2de3SMichal Malý 	kfree(lbuf); /* Not needed anymore */
699f31a2de3SMichal Malý 
700*72529c65SMichal Malý 	if (target_product_id == entry->wdata.product_id) /* Nothing to do */
701f31a2de3SMichal Malý 		return count;
702f31a2de3SMichal Malý 
703f31a2de3SMichal Malý 	/* Automatic switching has to be disabled for the switch to DF-EX mode to work correctly */
704f31a2de3SMichal Malý 	if (target_product_id == USB_DEVICE_ID_LOGITECH_WHEEL && !lg4ff_no_autoswitch) {
705f31a2de3SMichal 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",
706*72529c65SMichal Malý 			 entry->wdata.real_name);
707f31a2de3SMichal Malý 		return -EINVAL;
708f31a2de3SMichal Malý 	}
709f31a2de3SMichal Malý 
710f31a2de3SMichal Malý 	/* Take care of hardware limitations */
711*72529c65SMichal Malý 	if ((entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_DFP_WHEEL || entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_G25_WHEEL) &&
712*72529c65SMichal Malý 	    entry->wdata.product_id > target_product_id) {
713*72529c65SMichal Malý 		hid_info(hid, "\"%s\" cannot be switched back into \"%s\" mode\n", entry->wdata.real_name, lg4ff_alternate_modes[i].name);
714f31a2de3SMichal Malý 		return -EINVAL;
715f31a2de3SMichal Malý 	}
716f31a2de3SMichal Malý 
717*72529c65SMichal Malý 	s = lg4ff_get_mode_switch_command(entry->wdata.real_product_id, target_product_id);
718f31a2de3SMichal Malý 	if (!s) {
719f31a2de3SMichal Malý 		hid_err(hid, "Invalid target product ID %X\n", target_product_id);
720f31a2de3SMichal Malý 		return -EINVAL;
721f31a2de3SMichal Malý 	}
722f31a2de3SMichal Malý 
723f31a2de3SMichal Malý 	ret = lg4ff_switch_compatibility_mode(hid, s);
724f31a2de3SMichal Malý 	return (ret == 0 ? count : ret);
725b96d23ecSMichal Malý }
726b96d23ecSMichal Malý static DEVICE_ATTR(alternate_modes, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_alternate_modes_show, lg4ff_alternate_modes_store);
727b96d23ecSMichal Malý 
728fbf85e2aSMichal Malý /* Export the currently set range of the wheel */
729fbf85e2aSMichal Malý static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr,
7302f1cec32SVivien Didelot 				char *buf)
73130bb75d7SMichal Malý {
73230bb75d7SMichal Malý 	struct hid_device *hid = to_hid_device(dev);
7333b6b17b7SMichal Malý 	struct lg4ff_device_entry *entry;
7343b6b17b7SMichal Malý 	struct lg_drv_data *drv_data;
73530bb75d7SMichal Malý 	size_t count;
73630bb75d7SMichal Malý 
7373b6b17b7SMichal Malý 	drv_data = hid_get_drvdata(hid);
7383b6b17b7SMichal Malý 	if (!drv_data) {
7393b6b17b7SMichal Malý 		hid_err(hid, "Private driver data not found!\n");
7403b6b17b7SMichal Malý 		return 0;
74130bb75d7SMichal Malý 	}
7423b6b17b7SMichal Malý 
7433b6b17b7SMichal Malý 	entry = drv_data->device_props;
7443b6b17b7SMichal Malý 	if (!entry) {
7453b6b17b7SMichal Malý 		hid_err(hid, "Device properties not found!\n");
74630bb75d7SMichal Malý 		return 0;
74730bb75d7SMichal Malý 	}
74830bb75d7SMichal Malý 
749*72529c65SMichal Malý 	count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->wdata.range);
75030bb75d7SMichal Malý 	return count;
75130bb75d7SMichal Malý }
75230bb75d7SMichal Malý 
75330bb75d7SMichal Malý /* Set range to user specified value, call appropriate function
75430bb75d7SMichal Malý  * according to the type of the wheel */
755fbf85e2aSMichal Malý static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr,
7562f1cec32SVivien Didelot 				 const char *buf, size_t count)
75730bb75d7SMichal Malý {
75830bb75d7SMichal Malý 	struct hid_device *hid = to_hid_device(dev);
7593b6b17b7SMichal Malý 	struct lg4ff_device_entry *entry;
7603b6b17b7SMichal Malý 	struct lg_drv_data *drv_data;
7612a552c30SMichal Malý 	u16 range = simple_strtoul(buf, NULL, 10);
76230bb75d7SMichal Malý 
7633b6b17b7SMichal Malý 	drv_data = hid_get_drvdata(hid);
7643b6b17b7SMichal Malý 	if (!drv_data) {
7653b6b17b7SMichal Malý 		hid_err(hid, "Private driver data not found!\n");
76629ff6657SSimon Wood 		return -EINVAL;
76730bb75d7SMichal Malý 	}
7683b6b17b7SMichal Malý 
7693b6b17b7SMichal Malý 	entry = drv_data->device_props;
7703b6b17b7SMichal Malý 	if (!entry) {
7713b6b17b7SMichal Malý 		hid_err(hid, "Device properties not found!\n");
77229ff6657SSimon Wood 		return -EINVAL;
77330bb75d7SMichal Malý 	}
77430bb75d7SMichal Malý 
77530bb75d7SMichal Malý 	if (range == 0)
776*72529c65SMichal Malý 		range = entry->wdata.max_range;
77730bb75d7SMichal Malý 
77830bb75d7SMichal Malý 	/* Check if the wheel supports range setting
77930bb75d7SMichal Malý 	 * and that the range is within limits for the wheel */
780*72529c65SMichal Malý 	if (entry->wdata.set_range && range >= entry->wdata.min_range && range <= entry->wdata.max_range) {
781*72529c65SMichal Malý 		entry->wdata.set_range(hid, range);
782*72529c65SMichal Malý 		entry->wdata.range = range;
78330bb75d7SMichal Malý 	}
78430bb75d7SMichal Malý 
78530bb75d7SMichal Malý 	return count;
78630bb75d7SMichal Malý }
787fbf85e2aSMichal Malý static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_range_show, lg4ff_range_store);
78830bb75d7SMichal Malý 
789b96d23ecSMichal Malý static ssize_t lg4ff_real_id_show(struct device *dev, struct device_attribute *attr, char *buf)
790b96d23ecSMichal Malý {
791b96d23ecSMichal Malý 	struct hid_device *hid = to_hid_device(dev);
792b96d23ecSMichal Malý 	struct lg4ff_device_entry *entry;
793b96d23ecSMichal Malý 	struct lg_drv_data *drv_data;
794b96d23ecSMichal Malý 	size_t count;
795b96d23ecSMichal Malý 
796b96d23ecSMichal Malý 	drv_data = hid_get_drvdata(hid);
797b96d23ecSMichal Malý 	if (!drv_data) {
798b96d23ecSMichal Malý 		hid_err(hid, "Private driver data not found!\n");
799b96d23ecSMichal Malý 		return 0;
800b96d23ecSMichal Malý 	}
801b96d23ecSMichal Malý 
802b96d23ecSMichal Malý 	entry = drv_data->device_props;
803b96d23ecSMichal Malý 	if (!entry) {
804b96d23ecSMichal Malý 		hid_err(hid, "Device properties not found!\n");
805b96d23ecSMichal Malý 		return 0;
806b96d23ecSMichal Malý 	}
807b96d23ecSMichal Malý 
808*72529c65SMichal Malý 	if (!entry->wdata.real_tag || !entry->wdata.real_name) {
809b96d23ecSMichal Malý 		hid_err(hid, "NULL pointer to string\n");
810b96d23ecSMichal Malý 		return 0;
811b96d23ecSMichal Malý 	}
812b96d23ecSMichal Malý 
813*72529c65SMichal Malý 	count = scnprintf(buf, PAGE_SIZE, "%s: %s\n", entry->wdata.real_tag, entry->wdata.real_name);
814b96d23ecSMichal Malý 	return count;
815b96d23ecSMichal Malý }
816b96d23ecSMichal Malý 
817b96d23ecSMichal Malý static ssize_t lg4ff_real_id_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
818b96d23ecSMichal Malý {
819b96d23ecSMichal Malý 	/* Real ID is a read-only value */
820b96d23ecSMichal Malý 	return -EPERM;
821b96d23ecSMichal Malý }
822b96d23ecSMichal Malý static DEVICE_ATTR(real_id, S_IRUGO, lg4ff_real_id_show, lg4ff_real_id_store);
823b96d23ecSMichal Malý 
82422bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS
8252a552c30SMichal Malý static void lg4ff_set_leds(struct hid_device *hid, u8 leds)
82622bcefdcSSimon Wood {
82722bcefdcSSimon Wood 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
82822bcefdcSSimon Wood 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
8292a552c30SMichal Malý 	s32 *value = report->field[0]->value;
83022bcefdcSSimon Wood 
83174479ba8SMichal Malý 	value[0] = 0xf8;
83274479ba8SMichal Malý 	value[1] = 0x12;
83374479ba8SMichal Malý 	value[2] = leds;
83474479ba8SMichal Malý 	value[3] = 0x00;
83574479ba8SMichal Malý 	value[4] = 0x00;
83674479ba8SMichal Malý 	value[5] = 0x00;
83774479ba8SMichal Malý 	value[6] = 0x00;
838d8814272SBenjamin Tissoires 	hid_hw_request(hid, report, HID_REQ_SET_REPORT);
83922bcefdcSSimon Wood }
84022bcefdcSSimon Wood 
84122bcefdcSSimon Wood static void lg4ff_led_set_brightness(struct led_classdev *led_cdev,
84222bcefdcSSimon Wood 			enum led_brightness value)
84322bcefdcSSimon Wood {
84422bcefdcSSimon Wood 	struct device *dev = led_cdev->dev->parent;
84522bcefdcSSimon Wood 	struct hid_device *hid = container_of(dev, struct hid_device, dev);
8464629fd16SAxel Lin 	struct lg_drv_data *drv_data = hid_get_drvdata(hid);
84722bcefdcSSimon Wood 	struct lg4ff_device_entry *entry;
84822bcefdcSSimon Wood 	int i, state = 0;
84922bcefdcSSimon Wood 
85022bcefdcSSimon Wood 	if (!drv_data) {
85122bcefdcSSimon Wood 		hid_err(hid, "Device data not found.");
85222bcefdcSSimon Wood 		return;
85322bcefdcSSimon Wood 	}
85422bcefdcSSimon Wood 
855371a1d9eSMichal Malý 	entry = drv_data->device_props;
85622bcefdcSSimon Wood 
85722bcefdcSSimon Wood 	if (!entry) {
85822bcefdcSSimon Wood 		hid_err(hid, "Device properties not found.");
85922bcefdcSSimon Wood 		return;
86022bcefdcSSimon Wood 	}
86122bcefdcSSimon Wood 
86222bcefdcSSimon Wood 	for (i = 0; i < 5; i++) {
863*72529c65SMichal Malý 		if (led_cdev != entry->wdata.led[i])
86422bcefdcSSimon Wood 			continue;
865*72529c65SMichal Malý 		state = (entry->wdata.led_state >> i) & 1;
86622bcefdcSSimon Wood 		if (value == LED_OFF && state) {
867*72529c65SMichal Malý 			entry->wdata.led_state &= ~(1 << i);
868*72529c65SMichal Malý 			lg4ff_set_leds(hid, entry->wdata.led_state);
86922bcefdcSSimon Wood 		} else if (value != LED_OFF && !state) {
870*72529c65SMichal Malý 			entry->wdata.led_state |= 1 << i;
871*72529c65SMichal Malý 			lg4ff_set_leds(hid, entry->wdata.led_state);
87222bcefdcSSimon Wood 		}
87322bcefdcSSimon Wood 		break;
87422bcefdcSSimon Wood 	}
87522bcefdcSSimon Wood }
87622bcefdcSSimon Wood 
87722bcefdcSSimon Wood static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev)
87822bcefdcSSimon Wood {
87922bcefdcSSimon Wood 	struct device *dev = led_cdev->dev->parent;
88022bcefdcSSimon Wood 	struct hid_device *hid = container_of(dev, struct hid_device, dev);
8814629fd16SAxel Lin 	struct lg_drv_data *drv_data = hid_get_drvdata(hid);
88222bcefdcSSimon Wood 	struct lg4ff_device_entry *entry;
88322bcefdcSSimon Wood 	int i, value = 0;
88422bcefdcSSimon Wood 
88522bcefdcSSimon Wood 	if (!drv_data) {
88622bcefdcSSimon Wood 		hid_err(hid, "Device data not found.");
88722bcefdcSSimon Wood 		return LED_OFF;
88822bcefdcSSimon Wood 	}
88922bcefdcSSimon Wood 
890371a1d9eSMichal Malý 	entry = drv_data->device_props;
89122bcefdcSSimon Wood 
89222bcefdcSSimon Wood 	if (!entry) {
89322bcefdcSSimon Wood 		hid_err(hid, "Device properties not found.");
89422bcefdcSSimon Wood 		return LED_OFF;
89522bcefdcSSimon Wood 	}
89622bcefdcSSimon Wood 
89722bcefdcSSimon Wood 	for (i = 0; i < 5; i++)
898*72529c65SMichal Malý 		if (led_cdev == entry->wdata.led[i]) {
899*72529c65SMichal Malý 			value = (entry->wdata.led_state >> i) & 1;
90022bcefdcSSimon Wood 			break;
90122bcefdcSSimon Wood 		}
90222bcefdcSSimon Wood 
90322bcefdcSSimon Wood 	return value ? LED_FULL : LED_OFF;
90422bcefdcSSimon Wood }
90522bcefdcSSimon Wood #endif
90622bcefdcSSimon Wood 
907e7c23449SMichal Malý static u16 lg4ff_identify_multimode_wheel(struct hid_device *hid, const u16 reported_product_id, const u16 bcdDevice)
908e7c23449SMichal Malý {
909e7c23449SMichal Malý 	const struct lg4ff_wheel_ident_checklist *checklist;
910e7c23449SMichal Malý 	int i, from_idx, to_idx;
911e7c23449SMichal Malý 
912e7c23449SMichal Malý 	switch (reported_product_id) {
913e7c23449SMichal Malý 	case USB_DEVICE_ID_LOGITECH_WHEEL:
914e7c23449SMichal Malý 	case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
915e7c23449SMichal Malý 		checklist = &lg4ff_main_checklist;
916e7c23449SMichal Malý 		from_idx = 0;
917e7c23449SMichal Malý 		to_idx = checklist->count - 1;
918e7c23449SMichal Malý 		break;
919e7c23449SMichal Malý 	case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
920e7c23449SMichal Malý 		checklist = &lg4ff_main_checklist;
921e7c23449SMichal Malý 		from_idx = 0;
922e7c23449SMichal Malý 		to_idx = checklist->count - 2; /* End identity check at G25 */
923e7c23449SMichal Malý 		break;
924e7c23449SMichal Malý 	case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
925e7c23449SMichal Malý 		checklist = &lg4ff_main_checklist;
926e7c23449SMichal Malý 		from_idx = 1; /* Start identity check at G27 */
927e7c23449SMichal Malý 		to_idx = checklist->count - 3; /* End identity check at G27 */
928e7c23449SMichal Malý 		break;
929e7c23449SMichal Malý 	case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
930e7c23449SMichal Malý 		checklist = &lg4ff_main_checklist;
931e7c23449SMichal Malý 		from_idx = 0;
932e7c23449SMichal Malý 		to_idx = checklist->count - 4; /* End identity check at DFGT */
933e7c23449SMichal Malý 		break;
934e7c23449SMichal Malý 	default:
935e7c23449SMichal Malý 		return 0;
936e7c23449SMichal Malý 	}
937e7c23449SMichal Malý 
938e7c23449SMichal Malý 	for (i = from_idx; i <= to_idx; i++) {
939e7c23449SMichal Malý 		const u16 mask = checklist->models[i]->mask;
940e7c23449SMichal Malý 		const u16 result = checklist->models[i]->result;
941e7c23449SMichal Malý 		const u16 real_product_id = checklist->models[i]->real_product_id;
942e7c23449SMichal Malý 
943e7c23449SMichal Malý 		if ((bcdDevice & mask) == result) {
944e7c23449SMichal Malý 			dbg_hid("Found wheel with real PID %X whose reported PID is %X\n", real_product_id, reported_product_id);
945e7c23449SMichal Malý 			return real_product_id;
946e7c23449SMichal Malý 		}
947e7c23449SMichal Malý 	}
948e7c23449SMichal Malý 
949f31a2de3SMichal Malý 	/* No match found. This is either Driving Force or an unknown
950f31a2de3SMichal Malý 	 * wheel model, do not touch it */
951e7c23449SMichal Malý 	dbg_hid("Wheel with bcdDevice %X was not recognized as multimode wheel, leaving in its current mode\n", bcdDevice);
952e7c23449SMichal Malý 	return 0;
953e7c23449SMichal Malý }
954e7c23449SMichal Malý 
955e7c23449SMichal Malý static int lg4ff_handle_multimode_wheel(struct hid_device *hid, u16 *real_product_id, const u16 bcdDevice)
956e7c23449SMichal Malý {
957e7c23449SMichal Malý 	const u16 reported_product_id = hid->product;
958e7c23449SMichal Malý 	int ret;
959e7c23449SMichal Malý 
960e7c23449SMichal Malý 	*real_product_id = lg4ff_identify_multimode_wheel(hid, reported_product_id, bcdDevice);
961e7c23449SMichal Malý 	/* Probed wheel is not a multimode wheel */
962e7c23449SMichal Malý 	if (!*real_product_id) {
963e7c23449SMichal Malý 		*real_product_id = reported_product_id;
964e7c23449SMichal Malý 		dbg_hid("Wheel is not a multimode wheel\n");
965e7c23449SMichal Malý 		return LG4FF_MMODE_NOT_MULTIMODE;
966e7c23449SMichal Malý 	}
967e7c23449SMichal Malý 
968e7c23449SMichal Malý 	/* Switch from "Driving Force" mode to native mode automatically.
969e7c23449SMichal Malý 	 * Otherwise keep the wheel in its current mode */
970e7c23449SMichal Malý 	if (reported_product_id == USB_DEVICE_ID_LOGITECH_WHEEL &&
971a54dc779SMichal Malý 	    reported_product_id != *real_product_id &&
972a54dc779SMichal Malý 	    !lg4ff_no_autoswitch) {
973f31a2de3SMichal Malý 		const struct lg4ff_compat_mode_switch *s = lg4ff_get_mode_switch_command(*real_product_id, *real_product_id);
974e7c23449SMichal Malý 
975f31a2de3SMichal Malý 		if (!s) {
976e7c23449SMichal Malý 			hid_err(hid, "Invalid product id %X\n", *real_product_id);
977b96d23ecSMichal Malý 			return LG4FF_MMODE_NOT_MULTIMODE;
978e7c23449SMichal Malý 		}
979e7c23449SMichal Malý 
980e7c23449SMichal Malý 		ret = lg4ff_switch_compatibility_mode(hid, s);
981e7c23449SMichal Malý 		if (ret) {
982e7c23449SMichal Malý 			/* Wheel could not have been switched to native mode,
983e7c23449SMichal Malý 			 * leave it in "Driving Force" mode and continue */
984e7c23449SMichal Malý 			hid_err(hid, "Unable to switch wheel mode, errno %d\n", ret);
985b96d23ecSMichal Malý 			return LG4FF_MMODE_IS_MULTIMODE;
986e7c23449SMichal Malý 		}
987e7c23449SMichal Malý 		return LG4FF_MMODE_SWITCHED;
988e7c23449SMichal Malý 	}
989e7c23449SMichal Malý 
990b96d23ecSMichal Malý 	return LG4FF_MMODE_IS_MULTIMODE;
991e7c23449SMichal Malý }
992e7c23449SMichal Malý 
993e7c23449SMichal Malý 
99432c88cbcSSimon Wood int lg4ff_init(struct hid_device *hid)
99532c88cbcSSimon Wood {
99632c88cbcSSimon Wood 	struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
99732c88cbcSSimon Wood 	struct input_dev *dev = hidinput->input;
998e7c23449SMichal Malý 	const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
999e7c23449SMichal Malý 	const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
100030bb75d7SMichal Malý 	struct lg4ff_device_entry *entry;
10013b6b17b7SMichal Malý 	struct lg_drv_data *drv_data;
1002b96d23ecSMichal Malý 	int error, i, j;
1003b96d23ecSMichal Malý 	int mmode_ret, mmode_idx = -1;
1004e7c23449SMichal Malý 	u16 real_product_id;
100532c88cbcSSimon Wood 
100632c88cbcSSimon Wood 	/* Check that the report looks ok */
10070fb6bd06SKees Cook 	if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
100832c88cbcSSimon Wood 		return -1;
100932c88cbcSSimon Wood 
1010*72529c65SMichal Malý 	drv_data = hid_get_drvdata(hid);
1011*72529c65SMichal Malý 	if (!drv_data) {
1012*72529c65SMichal Malý 		hid_err(hid, "Cannot add device, private driver data not allocated\n");
1013*72529c65SMichal Malý 		return -1;
1014*72529c65SMichal Malý 	}
1015*72529c65SMichal Malý 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1016*72529c65SMichal Malý 	if (!entry)
1017*72529c65SMichal Malý 		return -ENOMEM;
1018*72529c65SMichal Malý 	drv_data->device_props = entry;
1019*72529c65SMichal Malý 
1020e7c23449SMichal Malý 	/* Check if a multimode wheel has been connected and
1021e7c23449SMichal Malý 	 * handle it appropriately */
1022b96d23ecSMichal Malý 	mmode_ret = lg4ff_handle_multimode_wheel(hid, &real_product_id, bcdDevice);
1023e7c23449SMichal Malý 
1024e7c23449SMichal Malý 	/* Wheel has been told to switch to native mode. There is no point in going on
1025e7c23449SMichal Malý 	 * with the initialization as the wheel will do a USB reset when it switches mode
1026e7c23449SMichal Malý 	 */
1027b96d23ecSMichal Malý 	if (mmode_ret == LG4FF_MMODE_SWITCHED)
1028e7c23449SMichal Malý 		return 0;
1029*72529c65SMichal Malý 	else if (mmode_ret < 0) {
1030*72529c65SMichal Malý 		hid_err(hid, "Unable to switch device mode during initialization, errno %d\n", mmode_ret);
1031*72529c65SMichal Malý 		error = mmode_ret;
1032*72529c65SMichal Malý 		goto err_init;
1033*72529c65SMichal Malý 	}
1034e7c23449SMichal Malý 
10357362cd22SMichal Malý 	/* Check what wheel has been connected */
10367362cd22SMichal Malý 	for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
10377362cd22SMichal Malý 		if (hid->product == lg4ff_devices[i].product_id) {
10387362cd22SMichal Malý 			dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
10397362cd22SMichal Malý 			break;
10407362cd22SMichal Malý 		}
10417362cd22SMichal Malý 	}
10427362cd22SMichal Malý 
10437362cd22SMichal Malý 	if (i == ARRAY_SIZE(lg4ff_devices)) {
10449c2a6bd1SMichal 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. "
10459c2a6bd1SMichal Malý 			     "Please report this as a bug to LKML, Simon Wood <simon@mungewell.org> or "
10469c2a6bd1SMichal Malý 			     "Michal Maly <madcatxster@devoid-pointer.net>\n");
1047*72529c65SMichal Malý 		error = -1;
1048*72529c65SMichal Malý 		goto err_init;
10497362cd22SMichal Malý 	}
10507362cd22SMichal Malý 
1051b96d23ecSMichal Malý 	if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1052b96d23ecSMichal Malý 		for (mmode_idx = 0; mmode_idx < ARRAY_SIZE(lg4ff_multimode_wheels); mmode_idx++) {
1053b96d23ecSMichal Malý 			if (real_product_id == lg4ff_multimode_wheels[mmode_idx].product_id)
1054b96d23ecSMichal Malý 				break;
1055b96d23ecSMichal Malý 		}
1056b96d23ecSMichal Malý 
1057b96d23ecSMichal Malý 		if (mmode_idx == ARRAY_SIZE(lg4ff_multimode_wheels)) {
1058b96d23ecSMichal Malý 			hid_err(hid, "Device product ID %X is not listed as a multimode wheel", real_product_id);
1059*72529c65SMichal Malý 			error = -1;
1060*72529c65SMichal Malý 			goto err_init;
1061b96d23ecSMichal Malý 		}
1062b96d23ecSMichal Malý 	}
1063b96d23ecSMichal Malý 
10647362cd22SMichal Malý 	/* Set supported force feedback capabilities */
10657362cd22SMichal Malý 	for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
10667362cd22SMichal Malý 		set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
106732c88cbcSSimon Wood 
1068d0afd848SMichal Malý 	error = input_ff_create_memless(dev, NULL, lg4ff_play);
106932c88cbcSSimon Wood 
107032c88cbcSSimon Wood 	if (error)
1071*72529c65SMichal Malý 		goto err_init;
107232c88cbcSSimon Wood 
1073*72529c65SMichal Malý 	entry->wdata.product_id = lg4ff_devices[i].product_id;
1074*72529c65SMichal Malý 	entry->wdata.real_product_id = real_product_id;
1075*72529c65SMichal Malý 	entry->wdata.min_range = lg4ff_devices[i].min_range;
1076*72529c65SMichal Malý 	entry->wdata.max_range = lg4ff_devices[i].max_range;
1077*72529c65SMichal Malý 	entry->wdata.set_range = lg4ff_devices[i].set_range;
1078b96d23ecSMichal Malý 	if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1079b96d23ecSMichal Malý 		BUG_ON(mmode_idx == -1);
1080*72529c65SMichal Malý 		entry->wdata.alternate_modes = lg4ff_multimode_wheels[mmode_idx].alternate_modes;
1081*72529c65SMichal Malý 		entry->wdata.real_tag = lg4ff_multimode_wheels[mmode_idx].real_tag;
1082*72529c65SMichal Malý 		entry->wdata.real_name = lg4ff_multimode_wheels[mmode_idx].real_name;
1083b96d23ecSMichal Malý 	}
108430bb75d7SMichal Malý 
1085114a55cfSSimon Wood 	/* Check if autocentering is available and
1086114a55cfSSimon Wood 	 * set the centering force to zero by default */
1087114a55cfSSimon Wood 	if (test_bit(FF_AUTOCENTER, dev->ffbit)) {
1088e7c23449SMichal Malý 		/* Formula Force EX expects different autocentering command */
1089e7c23449SMichal Malý 		if ((bcdDevice >> 8) == LG4FF_FFEX_REV_MAJ &&
1090e7c23449SMichal Malý 		    (bcdDevice & 0xff) == LG4FF_FFEX_REV_MIN)
1091d0afd848SMichal Malý 			dev->ff->set_autocenter = lg4ff_set_autocenter_ffex;
1092114a55cfSSimon Wood 		else
1093d0afd848SMichal Malý 			dev->ff->set_autocenter = lg4ff_set_autocenter_default;
1094114a55cfSSimon Wood 
1095114a55cfSSimon Wood 		dev->ff->set_autocenter(dev, 0);
1096114a55cfSSimon Wood 	}
1097114a55cfSSimon Wood 
109830bb75d7SMichal Malý 	/* Create sysfs interface */
109930bb75d7SMichal Malý 	error = device_create_file(&hid->dev, &dev_attr_range);
110030bb75d7SMichal Malý 	if (error)
1101*72529c65SMichal Malý 		goto err_init;
1102b96d23ecSMichal Malý 	if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1103b96d23ecSMichal Malý 		error = device_create_file(&hid->dev, &dev_attr_real_id);
1104b96d23ecSMichal Malý 		if (error)
1105*72529c65SMichal Malý 			goto err_init;
1106*72529c65SMichal Malý 
1107b96d23ecSMichal Malý 		error = device_create_file(&hid->dev, &dev_attr_alternate_modes);
1108b96d23ecSMichal Malý 		if (error)
1109*72529c65SMichal Malý 			goto err_init;
1110b96d23ecSMichal Malý 	}
111130bb75d7SMichal Malý 	dbg_hid("sysfs interface created\n");
111230bb75d7SMichal Malý 
111330bb75d7SMichal Malý 	/* Set the maximum range to start with */
1114*72529c65SMichal Malý 	entry->wdata.range = entry->wdata.max_range;
1115*72529c65SMichal Malý 	if (entry->wdata.set_range)
1116*72529c65SMichal Malý 		entry->wdata.set_range(hid, entry->wdata.range);
111730bb75d7SMichal Malý 
111822bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS
111922bcefdcSSimon Wood 	/* register led subsystem - G27 only */
1120*72529c65SMichal Malý 	entry->wdata.led_state = 0;
112122bcefdcSSimon Wood 	for (j = 0; j < 5; j++)
1122*72529c65SMichal Malý 		entry->wdata.led[j] = NULL;
112322bcefdcSSimon Wood 
112422bcefdcSSimon Wood 	if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL) {
112522bcefdcSSimon Wood 		struct led_classdev *led;
112622bcefdcSSimon Wood 		size_t name_sz;
112722bcefdcSSimon Wood 		char *name;
112822bcefdcSSimon Wood 
112922bcefdcSSimon Wood 		lg4ff_set_leds(hid, 0);
113022bcefdcSSimon Wood 
113122bcefdcSSimon Wood 		name_sz = strlen(dev_name(&hid->dev)) + 8;
113222bcefdcSSimon Wood 
113322bcefdcSSimon Wood 		for (j = 0; j < 5; j++) {
113422bcefdcSSimon Wood 			led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
113522bcefdcSSimon Wood 			if (!led) {
113622bcefdcSSimon Wood 				hid_err(hid, "can't allocate memory for LED %d\n", j);
1137*72529c65SMichal Malý 				goto err_leds;
113822bcefdcSSimon Wood 			}
113922bcefdcSSimon Wood 
114022bcefdcSSimon Wood 			name = (void *)(&led[1]);
114122bcefdcSSimon Wood 			snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1);
114222bcefdcSSimon Wood 			led->name = name;
114322bcefdcSSimon Wood 			led->brightness = 0;
114422bcefdcSSimon Wood 			led->max_brightness = 1;
114522bcefdcSSimon Wood 			led->brightness_get = lg4ff_led_get_brightness;
114622bcefdcSSimon Wood 			led->brightness_set = lg4ff_led_set_brightness;
114722bcefdcSSimon Wood 
1148*72529c65SMichal Malý 			entry->wdata.led[j] = led;
114922bcefdcSSimon Wood 			error = led_classdev_register(&hid->dev, led);
115022bcefdcSSimon Wood 
115122bcefdcSSimon Wood 			if (error) {
115222bcefdcSSimon Wood 				hid_err(hid, "failed to register LED %d. Aborting.\n", j);
1153*72529c65SMichal Malý err_leds:
115422bcefdcSSimon Wood 				/* Deregister LEDs (if any) */
115522bcefdcSSimon Wood 				for (j = 0; j < 5; j++) {
1156*72529c65SMichal Malý 					led = entry->wdata.led[j];
1157*72529c65SMichal Malý 					entry->wdata.led[j] = NULL;
115822bcefdcSSimon Wood 					if (!led)
115922bcefdcSSimon Wood 						continue;
116022bcefdcSSimon Wood 					led_classdev_unregister(led);
116122bcefdcSSimon Wood 					kfree(led);
116222bcefdcSSimon Wood 				}
116322bcefdcSSimon Wood 				goto out;	/* Let the driver continue without LEDs */
116422bcefdcSSimon Wood 			}
116522bcefdcSSimon Wood 		}
116622bcefdcSSimon Wood 	}
116722bcefdcSSimon Wood out:
1168c6e6dc87SJiri Kosina #endif
116964013800SSimon Wood 	hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n");
117032c88cbcSSimon Wood 	return 0;
1171*72529c65SMichal Malý 
1172*72529c65SMichal Malý err_init:
1173*72529c65SMichal Malý 	drv_data->device_props = NULL;
1174*72529c65SMichal Malý 	kfree(entry);
1175*72529c65SMichal Malý 	return error;
117632c88cbcSSimon Wood }
117732c88cbcSSimon Wood 
117830bb75d7SMichal Malý int lg4ff_deinit(struct hid_device *hid)
117930bb75d7SMichal Malý {
118030bb75d7SMichal Malý 	struct lg4ff_device_entry *entry;
11813b6b17b7SMichal Malý 	struct lg_drv_data *drv_data;
11826a2e176bSMichal Malý 
11833b6b17b7SMichal Malý 	drv_data = hid_get_drvdata(hid);
11843b6b17b7SMichal Malý 	if (!drv_data) {
11853b6b17b7SMichal Malý 		hid_err(hid, "Error while deinitializing device, no private driver data.\n");
118630bb75d7SMichal Malý 		return -1;
118730bb75d7SMichal Malý 	}
11883b6b17b7SMichal Malý 	entry = drv_data->device_props;
1189e7c23449SMichal Malý 	if (!entry)
1190e7c23449SMichal Malý 		goto out; /* Nothing more to do */
1191e7c23449SMichal Malý 
1192b96d23ecSMichal Malý 	/* Multimode devices will have at least the "MODE_NATIVE" bit set */
1193*72529c65SMichal Malý 	if (entry->wdata.alternate_modes) {
1194b96d23ecSMichal Malý 		device_remove_file(&hid->dev, &dev_attr_real_id);
1195b96d23ecSMichal Malý 		device_remove_file(&hid->dev, &dev_attr_alternate_modes);
1196b96d23ecSMichal Malý 	}
1197b96d23ecSMichal Malý 
1198*72529c65SMichal Malý 	device_remove_file(&hid->dev, &dev_attr_range);
119922bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS
120022bcefdcSSimon Wood 	{
120122bcefdcSSimon Wood 		int j;
120222bcefdcSSimon Wood 		struct led_classdev *led;
120322bcefdcSSimon Wood 
120422bcefdcSSimon Wood 		/* Deregister LEDs (if any) */
120522bcefdcSSimon Wood 		for (j = 0; j < 5; j++) {
120622bcefdcSSimon Wood 
1207*72529c65SMichal Malý 			led = entry->wdata.led[j];
1208*72529c65SMichal Malý 			entry->wdata.led[j] = NULL;
120922bcefdcSSimon Wood 			if (!led)
121022bcefdcSSimon Wood 				continue;
121122bcefdcSSimon Wood 			led_classdev_unregister(led);
121222bcefdcSSimon Wood 			kfree(led);
121322bcefdcSSimon Wood 		}
121422bcefdcSSimon Wood 	}
121522bcefdcSSimon Wood #endif
121622bcefdcSSimon Wood 
12173b6b17b7SMichal Malý 	kfree(entry);
1218e7c23449SMichal Malý out:
121930bb75d7SMichal Malý 	dbg_hid("Device successfully unregistered\n");
122030bb75d7SMichal Malý 	return 0;
122130bb75d7SMichal Malý }
1222