xref: /openbmc/linux/drivers/hid/hid-lg4ff.c (revision 6cb6d98abdb06de867e34fde92912fdb89477897)
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 
36b96d23ecSMichal Malý #define LG4FF_MMODE_IS_MULTIMODE 0
37e7c23449SMichal Malý #define LG4FF_MMODE_SWITCHED 1
38e7c23449SMichal Malý #define LG4FF_MMODE_NOT_MULTIMODE 2
39e7c23449SMichal Malý 
40b96d23ecSMichal Malý #define LG4FF_MODE_NATIVE_IDX 0
41b96d23ecSMichal Malý #define LG4FF_MODE_DFEX_IDX 1
42b96d23ecSMichal Malý #define LG4FF_MODE_DFP_IDX 2
43b96d23ecSMichal Malý #define LG4FF_MODE_G25_IDX 3
44b96d23ecSMichal Malý #define LG4FF_MODE_DFGT_IDX 4
45b96d23ecSMichal Malý #define LG4FF_MODE_G27_IDX 5
4629fae1c8SSimon Wood #define LG4FF_MODE_G29_IDX 6
4729fae1c8SSimon Wood #define LG4FF_MODE_MAX_IDX 7
48b96d23ecSMichal Malý 
49b96d23ecSMichal Malý #define LG4FF_MODE_NATIVE BIT(LG4FF_MODE_NATIVE_IDX)
50b96d23ecSMichal Malý #define LG4FF_MODE_DFEX BIT(LG4FF_MODE_DFEX_IDX)
51b96d23ecSMichal Malý #define LG4FF_MODE_DFP BIT(LG4FF_MODE_DFP_IDX)
52b96d23ecSMichal Malý #define LG4FF_MODE_G25 BIT(LG4FF_MODE_G25_IDX)
53b96d23ecSMichal Malý #define LG4FF_MODE_DFGT BIT(LG4FF_MODE_DFGT_IDX)
54b96d23ecSMichal Malý #define LG4FF_MODE_G27 BIT(LG4FF_MODE_G27_IDX)
5529fae1c8SSimon Wood #define LG4FF_MODE_G29 BIT(LG4FF_MODE_G29_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"
6529fae1c8SSimon Wood #define LG4FF_G29_TAG "G29"
6629fae1c8SSimon Wood #define LG4FF_G29_NAME "G29 Racing Wheel"
67b96d23ecSMichal Malý #define LG4FF_DFGT_TAG "DFGT"
68b96d23ecSMichal Malý #define LG4FF_DFGT_NAME "Driving Force GT"
69b96d23ecSMichal Malý 
70e7c23449SMichal Malý #define LG4FF_FFEX_REV_MAJ 0x21
71e7c23449SMichal Malý #define LG4FF_FFEX_REV_MIN 0x00
72e7c23449SMichal Malý 
73d0afd848SMichal Malý static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range);
74d0afd848SMichal Malý static void lg4ff_set_range_g25(struct hid_device *hid, u16 range);
7530bb75d7SMichal Malý 
7672529c65SMichal Malý struct lg4ff_wheel_data {
775d9d60adSMichal Malý 	const u32 product_id;
78961af46fSSimon Wood 	u16 combine;
792a552c30SMichal Malý 	u16 range;
805d9d60adSMichal Malý 	const u16 min_range;
815d9d60adSMichal Malý 	const u16 max_range;
8222bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS
832a552c30SMichal Malý 	u8  led_state;
8422bcefdcSSimon Wood 	struct led_classdev *led[5];
8522bcefdcSSimon Wood #endif
865d9d60adSMichal Malý 	const u32 alternate_modes;
875d9d60adSMichal Malý 	const char * const real_tag;
885d9d60adSMichal Malý 	const char * const real_name;
895d9d60adSMichal Malý 	const u16 real_product_id;
9072529c65SMichal Malý 
9130bb75d7SMichal Malý 	void (*set_range)(struct hid_device *hid, u16 range);
9230bb75d7SMichal Malý };
9330bb75d7SMichal Malý 
9472529c65SMichal Malý struct lg4ff_device_entry {
95c918fe78SMichal Malý 	spinlock_t report_lock; /* Protect output HID report */
96c28abd8cSMichal Malý 	struct hid_report *report;
9772529c65SMichal Malý 	struct lg4ff_wheel_data wdata;
9872529c65SMichal Malý };
9972529c65SMichal Malý 
1007362cd22SMichal Malý static const signed short lg4ff_wheel_effects[] = {
10132c88cbcSSimon Wood 	FF_CONSTANT,
10232c88cbcSSimon Wood 	FF_AUTOCENTER,
10332c88cbcSSimon Wood 	-1
10432c88cbcSSimon Wood };
10532c88cbcSSimon Wood 
1067362cd22SMichal Malý struct lg4ff_wheel {
1072a552c30SMichal Malý 	const u32 product_id;
1087362cd22SMichal Malý 	const signed short *ff_effects;
1092a552c30SMichal Malý 	const u16 min_range;
1102a552c30SMichal Malý 	const u16 max_range;
11130bb75d7SMichal Malý 	void (*set_range)(struct hid_device *hid, u16 range);
1127362cd22SMichal Malý };
1137362cd22SMichal Malý 
114e7c23449SMichal Malý struct lg4ff_compat_mode_switch {
1152a552c30SMichal Malý 	const u8 cmd_count;	/* Number of commands to send */
1162a552c30SMichal Malý 	const u8 cmd[];
117e7c23449SMichal Malý };
118e7c23449SMichal Malý 
119e7c23449SMichal Malý struct lg4ff_wheel_ident_info {
120bbec1bd0SSimon Wood 	const u32 modes;
121e7c23449SMichal Malý 	const u16 mask;
122e7c23449SMichal Malý 	const u16 result;
123e7c23449SMichal Malý 	const u16 real_product_id;
124e7c23449SMichal Malý };
125e7c23449SMichal Malý 
126b96d23ecSMichal Malý struct lg4ff_multimode_wheel {
127b96d23ecSMichal Malý 	const u16 product_id;
128b96d23ecSMichal Malý 	const u32 alternate_modes;
129b96d23ecSMichal Malý 	const char *real_tag;
130b96d23ecSMichal Malý 	const char *real_name;
131b96d23ecSMichal Malý };
132b96d23ecSMichal Malý 
133b96d23ecSMichal Malý struct lg4ff_alternate_mode {
134b96d23ecSMichal Malý 	const u16 product_id;
135b96d23ecSMichal Malý 	const char *tag;
136b96d23ecSMichal Malý 	const char *name;
137b96d23ecSMichal Malý };
138b96d23ecSMichal Malý 
1397362cd22SMichal Malý static const struct lg4ff_wheel lg4ff_devices[] = {
140560bea30SSimon Wood 	{USB_DEVICE_ID_LOGITECH_WINGMAN_FFG, lg4ff_wheel_effects, 40, 180, NULL},
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},
14729fae1c8SSimon Wood 	{USB_DEVICE_ID_LOGITECH_G29_WHEEL,   lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
14830bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL},
14930bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_WII_WHEEL,   lg4ff_wheel_effects, 40, 270, NULL}
1507362cd22SMichal Malý };
1517362cd22SMichal Malý 
152b96d23ecSMichal Malý static const struct lg4ff_multimode_wheel lg4ff_multimode_wheels[] = {
153b96d23ecSMichal Malý 	{USB_DEVICE_ID_LOGITECH_DFP_WHEEL,
154b96d23ecSMichal Malý 	 LG4FF_MODE_NATIVE | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
155b96d23ecSMichal Malý 	 LG4FF_DFP_TAG, LG4FF_DFP_NAME},
156b96d23ecSMichal Malý 	{USB_DEVICE_ID_LOGITECH_G25_WHEEL,
157b96d23ecSMichal Malý 	 LG4FF_MODE_NATIVE | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
158b96d23ecSMichal Malý 	 LG4FF_G25_TAG, LG4FF_G25_NAME},
159b96d23ecSMichal Malý 	{USB_DEVICE_ID_LOGITECH_DFGT_WHEEL,
160b96d23ecSMichal Malý 	 LG4FF_MODE_NATIVE | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
161b96d23ecSMichal Malý 	 LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
162b96d23ecSMichal Malý 	{USB_DEVICE_ID_LOGITECH_G27_WHEEL,
163b96d23ecSMichal Malý 	 LG4FF_MODE_NATIVE | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
164b96d23ecSMichal Malý 	 LG4FF_G27_TAG, LG4FF_G27_NAME},
16529fae1c8SSimon Wood 	{USB_DEVICE_ID_LOGITECH_G29_WHEEL,
16629fae1c8SSimon Wood 	 LG4FF_MODE_NATIVE | LG4FF_MODE_G29 | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
16729fae1c8SSimon Wood 	 LG4FF_G29_TAG, LG4FF_G29_NAME},
168b96d23ecSMichal Malý };
169b96d23ecSMichal Malý 
170b96d23ecSMichal Malý static const struct lg4ff_alternate_mode lg4ff_alternate_modes[] = {
171b96d23ecSMichal Malý 	[LG4FF_MODE_NATIVE_IDX] = {0, "native", ""},
172b96d23ecSMichal Malý 	[LG4FF_MODE_DFEX_IDX] = {USB_DEVICE_ID_LOGITECH_WHEEL, LG4FF_DFEX_TAG, LG4FF_DFEX_NAME},
173b96d23ecSMichal Malý 	[LG4FF_MODE_DFP_IDX] = {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, LG4FF_DFP_TAG, LG4FF_DFP_NAME},
174b96d23ecSMichal Malý 	[LG4FF_MODE_G25_IDX] = {USB_DEVICE_ID_LOGITECH_G25_WHEEL, LG4FF_G25_TAG, LG4FF_G25_NAME},
175b96d23ecSMichal Malý 	[LG4FF_MODE_DFGT_IDX] = {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
17629fae1c8SSimon Wood 	[LG4FF_MODE_G27_IDX] = {USB_DEVICE_ID_LOGITECH_G27_WHEEL, LG4FF_G27_TAG, LG4FF_G27_NAME},
17729fae1c8SSimon Wood 	[LG4FF_MODE_G29_IDX] = {USB_DEVICE_ID_LOGITECH_G29_WHEEL, LG4FF_G29_TAG, LG4FF_G29_NAME},
178b96d23ecSMichal Malý };
179b96d23ecSMichal Malý 
180e7c23449SMichal Malý /* Multimode wheel identificators */
181e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_dfp_ident_info = {
182bbec1bd0SSimon Wood 	LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
183e7c23449SMichal Malý 	0xf000,
184e7c23449SMichal Malý 	0x1000,
185e7c23449SMichal Malý 	USB_DEVICE_ID_LOGITECH_DFP_WHEEL
18696440c8aSMichal Malý };
18796440c8aSMichal Malý 
188e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_g25_ident_info = {
189bbec1bd0SSimon Wood 	LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
190e7c23449SMichal Malý 	0xff00,
191e7c23449SMichal Malý 	0x1200,
192e7c23449SMichal Malý 	USB_DEVICE_ID_LOGITECH_G25_WHEEL
19396440c8aSMichal Malý };
19496440c8aSMichal Malý 
195e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_g27_ident_info = {
196bbec1bd0SSimon Wood 	LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
197e7c23449SMichal Malý 	0xfff0,
198e7c23449SMichal Malý 	0x1230,
199e7c23449SMichal Malý 	USB_DEVICE_ID_LOGITECH_G27_WHEEL
200e7c23449SMichal Malý };
201e7c23449SMichal Malý 
202e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_dfgt_ident_info = {
203bbec1bd0SSimon Wood 	LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
204e7c23449SMichal Malý 	0xff00,
205e7c23449SMichal Malý 	0x1300,
206e7c23449SMichal Malý 	USB_DEVICE_ID_LOGITECH_DFGT_WHEEL
207e7c23449SMichal Malý };
208e7c23449SMichal Malý 
20929fae1c8SSimon Wood static const struct lg4ff_wheel_ident_info lg4ff_g29_ident_info = {
21029fae1c8SSimon Wood 	LG4FF_MODE_G29 | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
21129fae1c8SSimon Wood 	0xfff8,
21229fae1c8SSimon Wood 	0x1350,
21329fae1c8SSimon Wood 	USB_DEVICE_ID_LOGITECH_G29_WHEEL
21429fae1c8SSimon Wood };
21529fae1c8SSimon Wood 
21629fae1c8SSimon Wood static const struct lg4ff_wheel_ident_info lg4ff_g29_ident_info2 = {
21729fae1c8SSimon Wood 	LG4FF_MODE_G29 | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
21829fae1c8SSimon Wood 	0xff00,
21929fae1c8SSimon Wood 	0x8900,
22029fae1c8SSimon Wood 	USB_DEVICE_ID_LOGITECH_G29_WHEEL
22129fae1c8SSimon Wood };
22229fae1c8SSimon Wood 
223e7c23449SMichal Malý /* Multimode wheel identification checklists */
224bbec1bd0SSimon Wood static const struct lg4ff_wheel_ident_info *lg4ff_main_checklist[] = {
22529fae1c8SSimon Wood 	&lg4ff_g29_ident_info,
22629fae1c8SSimon Wood 	&lg4ff_g29_ident_info2,
227bbec1bd0SSimon Wood 	&lg4ff_dfgt_ident_info,
228e7c23449SMichal Malý 	&lg4ff_g27_ident_info,
229e7c23449SMichal Malý 	&lg4ff_g25_ident_info,
230bbec1bd0SSimon Wood 	&lg4ff_dfp_ident_info
231e7c23449SMichal Malý };
232e7c23449SMichal Malý 
233e7c23449SMichal Malý /* Compatibility mode switching commands */
234f31a2de3SMichal Malý /* EXT_CMD9 - Understood by G27 and DFGT */
235f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfex = {
236f31a2de3SMichal Malý 	2,
237f31a2de3SMichal Malý 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* Revert mode upon USB reset */
238f31a2de3SMichal Malý 	 0xf8, 0x09, 0x00, 0x01, 0x00, 0x00, 0x00}	/* Switch mode to DF-EX with detach */
239f31a2de3SMichal Malý };
240f31a2de3SMichal Malý 
241f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfp = {
242f31a2de3SMichal Malý 	2,
243f31a2de3SMichal Malý 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* Revert mode upon USB reset */
244f31a2de3SMichal Malý 	 0xf8, 0x09, 0x01, 0x01, 0x00, 0x00, 0x00}	/* Switch mode to DFP with detach */
245f31a2de3SMichal Malý };
246f31a2de3SMichal Malý 
247f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g25 = {
248f31a2de3SMichal Malý 	2,
249f31a2de3SMichal Malý 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* Revert mode upon USB reset */
250f31a2de3SMichal Malý 	 0xf8, 0x09, 0x02, 0x01, 0x00, 0x00, 0x00}	/* Switch mode to G25 with detach */
251f31a2de3SMichal Malý };
252f31a2de3SMichal Malý 
253f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfgt = {
254f31a2de3SMichal Malý 	2,
255f31a2de3SMichal Malý 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* Revert mode upon USB reset */
256f31a2de3SMichal Malý 	 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00}	/* Switch mode to DFGT with detach */
257f31a2de3SMichal Malý };
258f31a2de3SMichal Malý 
259f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g27 = {
260f31a2de3SMichal Malý 	2,
261f31a2de3SMichal Malý 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* Revert mode upon USB reset */
262f31a2de3SMichal Malý 	 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00}	/* Switch mode to G27 with detach */
263f31a2de3SMichal Malý };
264f31a2de3SMichal Malý 
26529fae1c8SSimon Wood static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g29 = {
26629fae1c8SSimon Wood 	2,
26729fae1c8SSimon Wood 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* Revert mode upon USB reset */
26829fae1c8SSimon Wood 	 0xf8, 0x09, 0x05, 0x01, 0x01, 0x00, 0x00}	/* Switch mode to G29 with detach */
26929fae1c8SSimon Wood };
27029fae1c8SSimon Wood 
271f31a2de3SMichal Malý /* EXT_CMD1 - Understood by DFP, G25, G27 and DFGT */
272f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext01_dfp = {
27396440c8aSMichal Malý 	1,
27496440c8aSMichal Malý 	{0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
27596440c8aSMichal Malý };
27696440c8aSMichal Malý 
277f31a2de3SMichal Malý /* EXT_CMD16 - Understood by G25 and G27 */
278f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext16_g25 = {
27996440c8aSMichal Malý 	1,
28096440c8aSMichal Malý 	{0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
28196440c8aSMichal Malý };
28296440c8aSMichal Malý 
2832b24a960SMichal Malý /* Recalculates X axis value accordingly to currently selected range */
2842a552c30SMichal Malý static s32 lg4ff_adjust_dfp_x_axis(s32 value, u16 range)
2852b24a960SMichal Malý {
2862a552c30SMichal Malý 	u16 max_range;
2872a552c30SMichal Malý 	s32 new_value;
2882b24a960SMichal Malý 
2892b24a960SMichal Malý 	if (range == 900)
2902b24a960SMichal Malý 		return value;
2912b24a960SMichal Malý 	else if (range == 200)
2922b24a960SMichal Malý 		return value;
2932b24a960SMichal Malý 	else if (range < 200)
2942b24a960SMichal Malý 		max_range = 200;
2952b24a960SMichal Malý 	else
2962b24a960SMichal Malý 		max_range = 900;
2972b24a960SMichal Malý 
2982b24a960SMichal Malý 	new_value = 8192 + mult_frac(value - 8192, max_range, range);
2992b24a960SMichal Malý 	if (new_value < 0)
3002b24a960SMichal Malý 		return 0;
3012b24a960SMichal Malý 	else if (new_value > 16383)
3022b24a960SMichal Malý 		return 16383;
3032b24a960SMichal Malý 	else
3042b24a960SMichal Malý 		return new_value;
3052b24a960SMichal Malý }
3062b24a960SMichal Malý 
3072b24a960SMichal Malý int lg4ff_adjust_input_event(struct hid_device *hid, struct hid_field *field,
3082a552c30SMichal Malý 			     struct hid_usage *usage, s32 value, struct lg_drv_data *drv_data)
3092b24a960SMichal Malý {
3102b24a960SMichal Malý 	struct lg4ff_device_entry *entry = drv_data->device_props;
3112a552c30SMichal Malý 	s32 new_value = 0;
3122b24a960SMichal Malý 
3132b24a960SMichal Malý 	if (!entry) {
3142b24a960SMichal Malý 		hid_err(hid, "Device properties not found");
3152b24a960SMichal Malý 		return 0;
3162b24a960SMichal Malý 	}
3172b24a960SMichal Malý 
31872529c65SMichal Malý 	switch (entry->wdata.product_id) {
3192b24a960SMichal Malý 	case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
3202b24a960SMichal Malý 		switch (usage->code) {
3212b24a960SMichal Malý 		case ABS_X:
32272529c65SMichal Malý 			new_value = lg4ff_adjust_dfp_x_axis(value, entry->wdata.range);
3232b24a960SMichal Malý 			input_event(field->hidinput->input, usage->type, usage->code, new_value);
3242b24a960SMichal Malý 			return 1;
3252b24a960SMichal Malý 		default:
3262b24a960SMichal Malý 			return 0;
3272b24a960SMichal Malý 		}
3282b24a960SMichal Malý 	default:
3292b24a960SMichal Malý 		return 0;
3302b24a960SMichal Malý 	}
3312b24a960SMichal Malý }
3322b24a960SMichal Malý 
333c832f86eSSimon Wood int lg4ff_raw_event(struct hid_device *hdev, struct hid_report *report,
334c832f86eSSimon Wood 		u8 *rd, int size, struct lg_drv_data *drv_data)
335c832f86eSSimon Wood {
336b456634bSSimon Wood 	int offset;
337c832f86eSSimon Wood 	struct lg4ff_device_entry *entry = drv_data->device_props;
338c832f86eSSimon Wood 
339c832f86eSSimon Wood 	if (!entry)
340c832f86eSSimon Wood 		return 0;
341c832f86eSSimon Wood 
342c832f86eSSimon Wood 	/* adjust HID report present combined pedals data */
343c832f86eSSimon Wood 	if (entry->wdata.combine) {
344c832f86eSSimon Wood 		switch (entry->wdata.product_id) {
345c832f86eSSimon Wood 		case USB_DEVICE_ID_LOGITECH_WHEEL:
346c832f86eSSimon Wood 			rd[5] = rd[3];
347c832f86eSSimon Wood 			rd[6] = 0x7F;
348c832f86eSSimon Wood 			return 1;
349560bea30SSimon Wood 		case USB_DEVICE_ID_LOGITECH_WINGMAN_FFG:
350c832f86eSSimon Wood 		case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL:
351c832f86eSSimon Wood 		case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2:
352c832f86eSSimon Wood 			rd[4] = rd[3];
353c832f86eSSimon Wood 			rd[5] = 0x7F;
354c832f86eSSimon Wood 			return 1;
355c832f86eSSimon Wood 		case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
356c832f86eSSimon Wood 			rd[5] = rd[4];
357c832f86eSSimon Wood 			rd[6] = 0x7F;
358c832f86eSSimon Wood 			return 1;
359b456634bSSimon Wood 		case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
360b456634bSSimon Wood 		case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
361b456634bSSimon Wood 			offset = 5;
362b456634bSSimon Wood 			break;
363b456634bSSimon Wood 		case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
364b456634bSSimon Wood 		case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
365b456634bSSimon Wood 			offset = 6;
366b456634bSSimon Wood 			break;
367b456634bSSimon Wood 		case USB_DEVICE_ID_LOGITECH_WII_WHEEL:
368b456634bSSimon Wood 			offset = 3;
369b456634bSSimon Wood 			break;
370c832f86eSSimon Wood 		default:
371c832f86eSSimon Wood 			return 0;
372c832f86eSSimon Wood 		}
373b456634bSSimon Wood 
374b456634bSSimon Wood 		/* Compute a combined axis when wheel does not supply it */
375b456634bSSimon Wood 		rd[offset] = (0xFF + rd[offset] - rd[offset+1]) >> 1;
376b456634bSSimon Wood 		rd[offset+1] = 0x7F;
377b456634bSSimon Wood 		return 1;
378c832f86eSSimon Wood 	}
379c832f86eSSimon Wood 
380c832f86eSSimon Wood 	return 0;
381c832f86eSSimon Wood }
382c832f86eSSimon Wood 
3835d9d60adSMichal Malý static void lg4ff_init_wheel_data(struct lg4ff_wheel_data * const wdata, const struct lg4ff_wheel *wheel,
3845d9d60adSMichal Malý 				  const struct lg4ff_multimode_wheel *mmode_wheel,
3855d9d60adSMichal Malý 				  const u16 real_product_id)
3865d9d60adSMichal Malý {
3875d9d60adSMichal Malý 	u32 alternate_modes = 0;
3885d9d60adSMichal Malý 	const char *real_tag = NULL;
3895d9d60adSMichal Malý 	const char *real_name = NULL;
3905d9d60adSMichal Malý 
3915d9d60adSMichal Malý 	if (mmode_wheel) {
3925d9d60adSMichal Malý 		alternate_modes = mmode_wheel->alternate_modes;
3935d9d60adSMichal Malý 		real_tag = mmode_wheel->real_tag;
3945d9d60adSMichal Malý 		real_name = mmode_wheel->real_name;
3955d9d60adSMichal Malý 	}
3965d9d60adSMichal Malý 
3975d9d60adSMichal Malý 	{
3985d9d60adSMichal Malý 		struct lg4ff_wheel_data t_wdata =  { .product_id = wheel->product_id,
3995d9d60adSMichal Malý 						     .real_product_id = real_product_id,
400961af46fSSimon Wood 						     .combine = 0,
4015d9d60adSMichal Malý 						     .min_range = wheel->min_range,
4025d9d60adSMichal Malý 						     .max_range = wheel->max_range,
4035d9d60adSMichal Malý 						     .set_range = wheel->set_range,
4045d9d60adSMichal Malý 						     .alternate_modes = alternate_modes,
4055d9d60adSMichal Malý 						     .real_tag = real_tag,
4065d9d60adSMichal Malý 						     .real_name = real_name };
4075d9d60adSMichal Malý 
4085d9d60adSMichal Malý 		memcpy(wdata, &t_wdata, sizeof(t_wdata));
4095d9d60adSMichal Malý 	}
4105d9d60adSMichal Malý }
4115d9d60adSMichal Malý 
412d0afd848SMichal Malý static int lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
41332c88cbcSSimon Wood {
41432c88cbcSSimon Wood 	struct hid_device *hid = input_get_drvdata(dev);
415c918fe78SMichal Malý 	struct lg4ff_device_entry *entry;
416c918fe78SMichal Malý 	struct lg_drv_data *drv_data;
417c918fe78SMichal Malý 	unsigned long flags;
418c28abd8cSMichal Malý 	s32 *value;
41932c88cbcSSimon Wood 	int x;
42032c88cbcSSimon Wood 
421c918fe78SMichal Malý 	drv_data = hid_get_drvdata(hid);
422c918fe78SMichal Malý 	if (!drv_data) {
423c918fe78SMichal Malý 		hid_err(hid, "Private driver data not found!\n");
424c918fe78SMichal Malý 		return -EINVAL;
425c918fe78SMichal Malý 	}
426c918fe78SMichal Malý 
427c918fe78SMichal Malý 	entry = drv_data->device_props;
428c918fe78SMichal Malý 	if (!entry) {
429c918fe78SMichal Malý 		hid_err(hid, "Device properties not found!\n");
430c918fe78SMichal Malý 		return -EINVAL;
431c918fe78SMichal Malý 	}
432c28abd8cSMichal Malý 	value = entry->report->field[0]->value;
433c918fe78SMichal Malý 
434a80fe5d6SMichal Malý #define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0)
43532c88cbcSSimon Wood 
43632c88cbcSSimon Wood 	switch (effect->type) {
43732c88cbcSSimon Wood 	case FF_CONSTANT:
43832c88cbcSSimon Wood 		x = effect->u.ramp.start_level + 0x80;	/* 0x80 is no force */
43932c88cbcSSimon Wood 		CLAMP(x);
44056930e7aSSimon Wood 
441c918fe78SMichal Malý 		spin_lock_irqsave(&entry->report_lock, flags);
44256930e7aSSimon Wood 		if (x == 0x80) {
44356930e7aSSimon Wood 			/* De-activate force in slot-1*/
44456930e7aSSimon Wood 			value[0] = 0x13;
44556930e7aSSimon Wood 			value[1] = 0x00;
44656930e7aSSimon Wood 			value[2] = 0x00;
44756930e7aSSimon Wood 			value[3] = 0x00;
44856930e7aSSimon Wood 			value[4] = 0x00;
44956930e7aSSimon Wood 			value[5] = 0x00;
45056930e7aSSimon Wood 			value[6] = 0x00;
45156930e7aSSimon Wood 
452c28abd8cSMichal Malý 			hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
453c918fe78SMichal Malý 			spin_unlock_irqrestore(&entry->report_lock, flags);
45456930e7aSSimon Wood 			return 0;
45556930e7aSSimon Wood 		}
45656930e7aSSimon Wood 
45774479ba8SMichal Malý 		value[0] = 0x11;	/* Slot 1 */
45874479ba8SMichal Malý 		value[1] = 0x08;
45974479ba8SMichal Malý 		value[2] = x;
46074479ba8SMichal Malý 		value[3] = 0x80;
46174479ba8SMichal Malý 		value[4] = 0x00;
46274479ba8SMichal Malý 		value[5] = 0x00;
46374479ba8SMichal Malý 		value[6] = 0x00;
46432c88cbcSSimon Wood 
465c28abd8cSMichal Malý 		hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
466c918fe78SMichal Malý 		spin_unlock_irqrestore(&entry->report_lock, flags);
46732c88cbcSSimon Wood 		break;
46832c88cbcSSimon Wood 	}
46932c88cbcSSimon Wood 	return 0;
47032c88cbcSSimon Wood }
47132c88cbcSSimon Wood 
4726e2de8e0SMichal Malý /* Sends default autocentering command compatible with
4736e2de8e0SMichal Malý  * all wheels except Formula Force EX */
474d0afd848SMichal Malý static void lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude)
47532c88cbcSSimon Wood {
47632c88cbcSSimon Wood 	struct hid_device *hid = input_get_drvdata(dev);
477*6cb6d98aSColin Ian King 	s32 *value;
4782a552c30SMichal Malý 	u32 expand_a, expand_b;
4791859762eSSimon Wood 	struct lg4ff_device_entry *entry;
4801859762eSSimon Wood 	struct lg_drv_data *drv_data;
481c918fe78SMichal Malý 	unsigned long flags;
4821859762eSSimon Wood 
4831859762eSSimon Wood 	drv_data = hid_get_drvdata(hid);
4841859762eSSimon Wood 	if (!drv_data) {
4851859762eSSimon Wood 		hid_err(hid, "Private driver data not found!\n");
4861859762eSSimon Wood 		return;
4871859762eSSimon Wood 	}
4881859762eSSimon Wood 
4891859762eSSimon Wood 	entry = drv_data->device_props;
4901859762eSSimon Wood 	if (!entry) {
4911859762eSSimon Wood 		hid_err(hid, "Device properties not found!\n");
4921859762eSSimon Wood 		return;
4931859762eSSimon Wood 	}
494c28abd8cSMichal Malý 	value = entry->report->field[0]->value;
495f8c23156SSimon Wood 
496d2c02da5SSimon Wood 	/* De-activate Auto-Center */
497c918fe78SMichal Malý 	spin_lock_irqsave(&entry->report_lock, flags);
498d2c02da5SSimon Wood 	if (magnitude == 0) {
499d2c02da5SSimon Wood 		value[0] = 0xf5;
500d2c02da5SSimon Wood 		value[1] = 0x00;
501d2c02da5SSimon Wood 		value[2] = 0x00;
502d2c02da5SSimon Wood 		value[3] = 0x00;
503d2c02da5SSimon Wood 		value[4] = 0x00;
504d2c02da5SSimon Wood 		value[5] = 0x00;
505d2c02da5SSimon Wood 		value[6] = 0x00;
506d2c02da5SSimon Wood 
507c28abd8cSMichal Malý 		hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
508c918fe78SMichal Malý 		spin_unlock_irqrestore(&entry->report_lock, flags);
509d2c02da5SSimon Wood 		return;
510d2c02da5SSimon Wood 	}
511d2c02da5SSimon Wood 
512f8c23156SSimon Wood 	if (magnitude <= 0xaaaa) {
513f8c23156SSimon Wood 		expand_a = 0x0c * magnitude;
514f8c23156SSimon Wood 		expand_b = 0x80 * magnitude;
515f8c23156SSimon Wood 	} else {
516f8c23156SSimon Wood 		expand_a = (0x0c * 0xaaaa) + 0x06 * (magnitude - 0xaaaa);
517f8c23156SSimon Wood 		expand_b = (0x80 * 0xaaaa) + 0xff * (magnitude - 0xaaaa);
518f8c23156SSimon Wood 	}
51932c88cbcSSimon Wood 
5201859762eSSimon Wood 	/* Adjust for non-MOMO wheels */
52172529c65SMichal Malý 	switch (entry->wdata.product_id) {
5221859762eSSimon Wood 	case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL:
5231859762eSSimon Wood 	case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2:
5241859762eSSimon Wood 		break;
5251859762eSSimon Wood 	default:
5261859762eSSimon Wood 		expand_a = expand_a >> 1;
5271859762eSSimon Wood 		break;
5281859762eSSimon Wood 	}
5291859762eSSimon Wood 
53074479ba8SMichal Malý 	value[0] = 0xfe;
53174479ba8SMichal Malý 	value[1] = 0x0d;
532f8c23156SSimon Wood 	value[2] = expand_a / 0xaaaa;
533f8c23156SSimon Wood 	value[3] = expand_a / 0xaaaa;
534f8c23156SSimon Wood 	value[4] = expand_b / 0xaaaa;
53574479ba8SMichal Malý 	value[5] = 0x00;
53674479ba8SMichal Malý 	value[6] = 0x00;
53732c88cbcSSimon Wood 
538c28abd8cSMichal Malý 	hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
539d2c02da5SSimon Wood 
540d2c02da5SSimon Wood 	/* Activate Auto-Center */
541d2c02da5SSimon Wood 	value[0] = 0x14;
542d2c02da5SSimon Wood 	value[1] = 0x00;
543d2c02da5SSimon Wood 	value[2] = 0x00;
544d2c02da5SSimon Wood 	value[3] = 0x00;
545d2c02da5SSimon Wood 	value[4] = 0x00;
546d2c02da5SSimon Wood 	value[5] = 0x00;
547d2c02da5SSimon Wood 	value[6] = 0x00;
548d2c02da5SSimon Wood 
549c28abd8cSMichal Malý 	hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
550c918fe78SMichal Malý 	spin_unlock_irqrestore(&entry->report_lock, flags);
55132c88cbcSSimon Wood }
55232c88cbcSSimon Wood 
5536e2de8e0SMichal Malý /* Sends autocentering command compatible with Formula Force EX */
554d0afd848SMichal Malý static void lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude)
5556e2de8e0SMichal Malý {
5566e2de8e0SMichal Malý 	struct hid_device *hid = input_get_drvdata(dev);
557c918fe78SMichal Malý 	struct lg4ff_device_entry *entry;
558c918fe78SMichal Malý 	struct lg_drv_data *drv_data;
559c918fe78SMichal Malý 	unsigned long flags;
560c28abd8cSMichal Malý 	s32 *value;
5616e2de8e0SMichal Malý 	magnitude = magnitude * 90 / 65535;
5626e2de8e0SMichal Malý 
563c918fe78SMichal Malý 	drv_data = hid_get_drvdata(hid);
564c918fe78SMichal Malý 	if (!drv_data) {
565c918fe78SMichal Malý 		hid_err(hid, "Private driver data not found!\n");
566c918fe78SMichal Malý 		return;
567c918fe78SMichal Malý 	}
568c918fe78SMichal Malý 
569c918fe78SMichal Malý 	entry = drv_data->device_props;
570c918fe78SMichal Malý 	if (!entry) {
571c918fe78SMichal Malý 		hid_err(hid, "Device properties not found!\n");
572c918fe78SMichal Malý 		return;
573c918fe78SMichal Malý 	}
574c28abd8cSMichal Malý 	value = entry->report->field[0]->value;
575c918fe78SMichal Malý 
576c918fe78SMichal Malý 	spin_lock_irqsave(&entry->report_lock, flags);
57774479ba8SMichal Malý 	value[0] = 0xfe;
57874479ba8SMichal Malý 	value[1] = 0x03;
57974479ba8SMichal Malý 	value[2] = magnitude >> 14;
58074479ba8SMichal Malý 	value[3] = magnitude >> 14;
58174479ba8SMichal Malý 	value[4] = magnitude;
58274479ba8SMichal Malý 	value[5] = 0x00;
58374479ba8SMichal Malý 	value[6] = 0x00;
5846e2de8e0SMichal Malý 
585c28abd8cSMichal Malý 	hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
586c918fe78SMichal Malý 	spin_unlock_irqrestore(&entry->report_lock, flags);
5876e2de8e0SMichal Malý }
5886e2de8e0SMichal Malý 
58930bb75d7SMichal Malý /* Sends command to set range compatible with G25/G27/Driving Force GT */
590d0afd848SMichal Malý static void lg4ff_set_range_g25(struct hid_device *hid, u16 range)
59130bb75d7SMichal Malý {
592c918fe78SMichal Malý 	struct lg4ff_device_entry *entry;
593c918fe78SMichal Malý 	struct lg_drv_data *drv_data;
594c918fe78SMichal Malý 	unsigned long flags;
595c28abd8cSMichal Malý 	s32 *value;
59674479ba8SMichal Malý 
597c918fe78SMichal Malý 	drv_data = hid_get_drvdata(hid);
598c918fe78SMichal Malý 	if (!drv_data) {
599c918fe78SMichal Malý 		hid_err(hid, "Private driver data not found!\n");
600c918fe78SMichal Malý 		return;
601c918fe78SMichal Malý 	}
602c918fe78SMichal Malý 
603c918fe78SMichal Malý 	entry = drv_data->device_props;
604c918fe78SMichal Malý 	if (!entry) {
605c918fe78SMichal Malý 		hid_err(hid, "Device properties not found!\n");
606c918fe78SMichal Malý 		return;
607c918fe78SMichal Malý 	}
608c28abd8cSMichal Malý 	value = entry->report->field[0]->value;
60930bb75d7SMichal Malý 	dbg_hid("G25/G27/DFGT: setting range to %u\n", range);
61030bb75d7SMichal Malý 
611c918fe78SMichal Malý 	spin_lock_irqsave(&entry->report_lock, flags);
61274479ba8SMichal Malý 	value[0] = 0xf8;
61374479ba8SMichal Malý 	value[1] = 0x81;
61474479ba8SMichal Malý 	value[2] = range & 0x00ff;
61574479ba8SMichal Malý 	value[3] = (range & 0xff00) >> 8;
61674479ba8SMichal Malý 	value[4] = 0x00;
61774479ba8SMichal Malý 	value[5] = 0x00;
61874479ba8SMichal Malý 	value[6] = 0x00;
61930bb75d7SMichal Malý 
620c28abd8cSMichal Malý 	hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
621c918fe78SMichal Malý 	spin_unlock_irqrestore(&entry->report_lock, flags);
62230bb75d7SMichal Malý }
62330bb75d7SMichal Malý 
62430bb75d7SMichal Malý /* Sends commands to set range compatible with Driving Force Pro wheel */
625d0afd848SMichal Malý static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range)
62630bb75d7SMichal Malý {
627c918fe78SMichal Malý 	struct lg4ff_device_entry *entry;
628c918fe78SMichal Malý 	struct lg_drv_data *drv_data;
629c918fe78SMichal Malý 	unsigned long flags;
630c28abd8cSMichal Malý 	int start_left, start_right, full_range;
631c28abd8cSMichal Malý 	s32 *value;
63274479ba8SMichal Malý 
633c918fe78SMichal Malý 	drv_data = hid_get_drvdata(hid);
634c918fe78SMichal Malý 	if (!drv_data) {
635c918fe78SMichal Malý 		hid_err(hid, "Private driver data not found!\n");
636c918fe78SMichal Malý 		return;
637c918fe78SMichal Malý 	}
638c918fe78SMichal Malý 
639c918fe78SMichal Malý 	entry = drv_data->device_props;
640c918fe78SMichal Malý 	if (!entry) {
641c918fe78SMichal Malý 		hid_err(hid, "Device properties not found!\n");
642c918fe78SMichal Malý 		return;
643c918fe78SMichal Malý 	}
644c28abd8cSMichal Malý 	value = entry->report->field[0]->value;
64530bb75d7SMichal Malý 	dbg_hid("Driving Force Pro: setting range to %u\n", range);
64630bb75d7SMichal Malý 
64730bb75d7SMichal Malý 	/* Prepare "coarse" limit command */
648c918fe78SMichal Malý 	spin_lock_irqsave(&entry->report_lock, flags);
64974479ba8SMichal Malý 	value[0] = 0xf8;
65074479ba8SMichal Malý 	value[1] = 0x00;	/* Set later */
65174479ba8SMichal Malý 	value[2] = 0x00;
65274479ba8SMichal Malý 	value[3] = 0x00;
65374479ba8SMichal Malý 	value[4] = 0x00;
65474479ba8SMichal Malý 	value[5] = 0x00;
65574479ba8SMichal Malý 	value[6] = 0x00;
65630bb75d7SMichal Malý 
65730bb75d7SMichal Malý 	if (range > 200) {
658c28abd8cSMichal Malý 		value[1] = 0x03;
65930bb75d7SMichal Malý 		full_range = 900;
66030bb75d7SMichal Malý 	} else {
661c28abd8cSMichal Malý 		value[1] = 0x02;
66230bb75d7SMichal Malý 		full_range = 200;
66330bb75d7SMichal Malý 	}
664c28abd8cSMichal Malý 	hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
66530bb75d7SMichal Malý 
66630bb75d7SMichal Malý 	/* Prepare "fine" limit command */
66774479ba8SMichal Malý 	value[0] = 0x81;
66874479ba8SMichal Malý 	value[1] = 0x0b;
66974479ba8SMichal Malý 	value[2] = 0x00;
67074479ba8SMichal Malý 	value[3] = 0x00;
67174479ba8SMichal Malý 	value[4] = 0x00;
67274479ba8SMichal Malý 	value[5] = 0x00;
67374479ba8SMichal Malý 	value[6] = 0x00;
67430bb75d7SMichal Malý 
67530bb75d7SMichal Malý 	if (range == 200 || range == 900) {	/* Do not apply any fine limit */
676c28abd8cSMichal Malý 		hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
677c918fe78SMichal Malý 		spin_unlock_irqrestore(&entry->report_lock, flags);
67830bb75d7SMichal Malý 		return;
67930bb75d7SMichal Malý 	}
68030bb75d7SMichal Malý 
68130bb75d7SMichal Malý 	/* Construct fine limit command */
68230bb75d7SMichal Malý 	start_left = (((full_range - range + 1) * 2047) / full_range);
68330bb75d7SMichal Malý 	start_right = 0xfff - start_left;
68430bb75d7SMichal Malý 
68574479ba8SMichal Malý 	value[2] = start_left >> 4;
68674479ba8SMichal Malý 	value[3] = start_right >> 4;
68774479ba8SMichal Malý 	value[4] = 0xff;
68874479ba8SMichal Malý 	value[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
68974479ba8SMichal Malý 	value[6] = 0xff;
69030bb75d7SMichal Malý 
691c28abd8cSMichal Malý 	hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
692c918fe78SMichal Malý 	spin_unlock_irqrestore(&entry->report_lock, flags);
69330bb75d7SMichal Malý }
69430bb75d7SMichal Malý 
695f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch *lg4ff_get_mode_switch_command(const u16 real_product_id, const u16 target_product_id)
696f31a2de3SMichal Malý {
697f31a2de3SMichal Malý 	switch (real_product_id) {
698f31a2de3SMichal Malý 	case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
699f31a2de3SMichal Malý 		switch (target_product_id) {
700f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
701f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext01_dfp;
702f31a2de3SMichal Malý 		/* DFP can only be switched to its native mode */
703f31a2de3SMichal Malý 		default:
704f31a2de3SMichal Malý 			return NULL;
705f31a2de3SMichal Malý 		}
706f31a2de3SMichal Malý 		break;
707f31a2de3SMichal Malý 	case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
708f31a2de3SMichal Malý 		switch (target_product_id) {
709f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
710f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext01_dfp;
711f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
712f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext16_g25;
713f31a2de3SMichal Malý 		/* G25 can only be switched to DFP mode or its native mode */
714f31a2de3SMichal Malý 		default:
715f31a2de3SMichal Malý 			return NULL;
716f31a2de3SMichal Malý 		}
717f31a2de3SMichal Malý 		break;
718f31a2de3SMichal Malý 	case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
719f31a2de3SMichal Malý 		switch (target_product_id) {
720f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_WHEEL:
721f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext09_dfex;
722f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
723f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext09_dfp;
724f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
725f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext09_g25;
726f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
727f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext09_g27;
728f31a2de3SMichal Malý 		/* G27 can only be switched to DF-EX, DFP, G25 or its native mode */
729f31a2de3SMichal Malý 		default:
730f31a2de3SMichal Malý 			return NULL;
731f31a2de3SMichal Malý 		}
732f31a2de3SMichal Malý 		break;
73329fae1c8SSimon Wood 	case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
73429fae1c8SSimon Wood 		switch (target_product_id) {
73529fae1c8SSimon Wood 		case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
73629fae1c8SSimon Wood 			return &lg4ff_mode_switch_ext09_dfp;
73729fae1c8SSimon Wood 		case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
73829fae1c8SSimon Wood 			return &lg4ff_mode_switch_ext09_dfgt;
73929fae1c8SSimon Wood 		case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
74029fae1c8SSimon Wood 			return &lg4ff_mode_switch_ext09_g25;
74129fae1c8SSimon Wood 		case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
74229fae1c8SSimon Wood 			return &lg4ff_mode_switch_ext09_g27;
74329fae1c8SSimon Wood 		case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
74429fae1c8SSimon Wood 			return &lg4ff_mode_switch_ext09_g29;
74529fae1c8SSimon Wood 		/* G29 can only be switched to DF-EX, DFP, DFGT, G25, G27 or its native mode */
74629fae1c8SSimon Wood 		default:
74729fae1c8SSimon Wood 			return NULL;
74829fae1c8SSimon Wood 		}
74929fae1c8SSimon Wood 		break;
750f31a2de3SMichal Malý 	case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
751f31a2de3SMichal Malý 		switch (target_product_id) {
752f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_WHEEL:
753f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext09_dfex;
754f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
755f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext09_dfp;
756f31a2de3SMichal Malý 		case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
757f31a2de3SMichal Malý 			return &lg4ff_mode_switch_ext09_dfgt;
758f31a2de3SMichal Malý 		/* DFGT can only be switched to DF-EX, DFP or its native mode */
759f31a2de3SMichal Malý 		default:
760f31a2de3SMichal Malý 			return NULL;
761f31a2de3SMichal Malý 		}
762f31a2de3SMichal Malý 		break;
763f31a2de3SMichal Malý 	/* No other wheels have multiple modes */
764f31a2de3SMichal Malý 	default:
765f31a2de3SMichal Malý 		return NULL;
766f31a2de3SMichal Malý 	}
767f31a2de3SMichal Malý }
768f31a2de3SMichal Malý 
769e7c23449SMichal Malý static int lg4ff_switch_compatibility_mode(struct hid_device *hid, const struct lg4ff_compat_mode_switch *s)
77096440c8aSMichal Malý {
771c918fe78SMichal Malý 	struct lg4ff_device_entry *entry;
772c918fe78SMichal Malý 	struct lg_drv_data *drv_data;
773c918fe78SMichal Malý 	unsigned long flags;
774c28abd8cSMichal Malý 	s32 *value;
775e7c23449SMichal Malý 	u8 i;
77696440c8aSMichal Malý 
777c918fe78SMichal Malý 	drv_data = hid_get_drvdata(hid);
778c918fe78SMichal Malý 	if (!drv_data) {
779c918fe78SMichal Malý 		hid_err(hid, "Private driver data not found!\n");
780c918fe78SMichal Malý 		return -EINVAL;
781c918fe78SMichal Malý 	}
782c918fe78SMichal Malý 
783c918fe78SMichal Malý 	entry = drv_data->device_props;
784c918fe78SMichal Malý 	if (!entry) {
785c918fe78SMichal Malý 		hid_err(hid, "Device properties not found!\n");
786c918fe78SMichal Malý 		return -EINVAL;
787c918fe78SMichal Malý 	}
788c28abd8cSMichal Malý 	value = entry->report->field[0]->value;
789c918fe78SMichal Malý 
790c918fe78SMichal Malý 	spin_lock_irqsave(&entry->report_lock, flags);
791e7c23449SMichal Malý 	for (i = 0; i < s->cmd_count; i++) {
792c1740d13SMichal Malý 		u8 j;
79396440c8aSMichal Malý 
794c1740d13SMichal Malý 		for (j = 0; j < 7; j++)
795c1740d13SMichal Malý 			value[j] = s->cmd[j + (7*i)];
796c1740d13SMichal Malý 
797c28abd8cSMichal Malý 		hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
79896440c8aSMichal Malý 	}
799c918fe78SMichal Malý 	spin_unlock_irqrestore(&entry->report_lock, flags);
800c1740d13SMichal Malý 	hid_hw_wait(hid);
801e7c23449SMichal Malý 	return 0;
80296440c8aSMichal Malý }
80332c88cbcSSimon Wood 
804b96d23ecSMichal Malý static ssize_t lg4ff_alternate_modes_show(struct device *dev, struct device_attribute *attr, char *buf)
805b96d23ecSMichal Malý {
806b96d23ecSMichal Malý 	struct hid_device *hid = to_hid_device(dev);
807b96d23ecSMichal Malý 	struct lg4ff_device_entry *entry;
808b96d23ecSMichal Malý 	struct lg_drv_data *drv_data;
809b96d23ecSMichal Malý 	ssize_t count = 0;
810b96d23ecSMichal Malý 	int i;
811b96d23ecSMichal Malý 
812b96d23ecSMichal Malý 	drv_data = hid_get_drvdata(hid);
813b96d23ecSMichal Malý 	if (!drv_data) {
814b96d23ecSMichal Malý 		hid_err(hid, "Private driver data not found!\n");
815b96d23ecSMichal Malý 		return 0;
816b96d23ecSMichal Malý 	}
817b96d23ecSMichal Malý 
818b96d23ecSMichal Malý 	entry = drv_data->device_props;
819b96d23ecSMichal Malý 	if (!entry) {
820b96d23ecSMichal Malý 		hid_err(hid, "Device properties not found!\n");
821b96d23ecSMichal Malý 		return 0;
822b96d23ecSMichal Malý 	}
823b96d23ecSMichal Malý 
82472529c65SMichal Malý 	if (!entry->wdata.real_name) {
825b96d23ecSMichal Malý 		hid_err(hid, "NULL pointer to string\n");
826b96d23ecSMichal Malý 		return 0;
827b96d23ecSMichal Malý 	}
828b96d23ecSMichal Malý 
829b96d23ecSMichal Malý 	for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
83072529c65SMichal Malý 		if (entry->wdata.alternate_modes & BIT(i)) {
831b96d23ecSMichal Malý 			/* Print tag and full name */
832b96d23ecSMichal Malý 			count += scnprintf(buf + count, PAGE_SIZE - count, "%s: %s",
833b96d23ecSMichal Malý 					   lg4ff_alternate_modes[i].tag,
83472529c65SMichal Malý 					   !lg4ff_alternate_modes[i].product_id ? entry->wdata.real_name : lg4ff_alternate_modes[i].name);
835b96d23ecSMichal Malý 			if (count >= PAGE_SIZE - 1)
836b96d23ecSMichal Malý 				return count;
837b96d23ecSMichal Malý 
838b96d23ecSMichal Malý 			/* Mark the currently active mode with an asterisk */
83972529c65SMichal Malý 			if (lg4ff_alternate_modes[i].product_id == entry->wdata.product_id ||
84072529c65SMichal Malý 			    (lg4ff_alternate_modes[i].product_id == 0 && entry->wdata.product_id == entry->wdata.real_product_id))
841b96d23ecSMichal Malý 				count += scnprintf(buf + count, PAGE_SIZE - count, " *\n");
842b96d23ecSMichal Malý 			else
843b96d23ecSMichal Malý 				count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
844b96d23ecSMichal Malý 
845b96d23ecSMichal Malý 			if (count >= PAGE_SIZE - 1)
846b96d23ecSMichal Malý 				return count;
847b96d23ecSMichal Malý 		}
848b96d23ecSMichal Malý 	}
849b96d23ecSMichal Malý 
850b96d23ecSMichal Malý 	return count;
851b96d23ecSMichal Malý }
852b96d23ecSMichal Malý 
853b96d23ecSMichal Malý static ssize_t lg4ff_alternate_modes_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
854b96d23ecSMichal Malý {
855f31a2de3SMichal Malý 	struct hid_device *hid = to_hid_device(dev);
856f31a2de3SMichal Malý 	struct lg4ff_device_entry *entry;
857f31a2de3SMichal Malý 	struct lg_drv_data *drv_data;
858f31a2de3SMichal Malý 	const struct lg4ff_compat_mode_switch *s;
859f31a2de3SMichal Malý 	u16 target_product_id = 0;
860f31a2de3SMichal Malý 	int i, ret;
861f31a2de3SMichal Malý 	char *lbuf;
862f31a2de3SMichal Malý 
863f31a2de3SMichal Malý 	drv_data = hid_get_drvdata(hid);
864f31a2de3SMichal Malý 	if (!drv_data) {
865f31a2de3SMichal Malý 		hid_err(hid, "Private driver data not found!\n");
866f31a2de3SMichal Malý 		return -EINVAL;
867f31a2de3SMichal Malý 	}
868f31a2de3SMichal Malý 
869f31a2de3SMichal Malý 	entry = drv_data->device_props;
870f31a2de3SMichal Malý 	if (!entry) {
871f31a2de3SMichal Malý 		hid_err(hid, "Device properties not found!\n");
872f31a2de3SMichal Malý 		return -EINVAL;
873f31a2de3SMichal Malý 	}
874f31a2de3SMichal Malý 
875f31a2de3SMichal Malý 	/* Allow \n at the end of the input parameter */
876f31a2de3SMichal Malý 	lbuf = kasprintf(GFP_KERNEL, "%s", buf);
877f31a2de3SMichal Malý 	if (!lbuf)
878f31a2de3SMichal Malý 		return -ENOMEM;
879f31a2de3SMichal Malý 
880f31a2de3SMichal Malý 	i = strlen(lbuf);
881f31a2de3SMichal Malý 	if (lbuf[i-1] == '\n') {
882f31a2de3SMichal Malý 		if (i == 1) {
883f31a2de3SMichal Malý 			kfree(lbuf);
884f31a2de3SMichal Malý 			return -EINVAL;
885f31a2de3SMichal Malý 		}
886f31a2de3SMichal Malý 		lbuf[i-1] = '\0';
887f31a2de3SMichal Malý 	}
888f31a2de3SMichal Malý 
889f31a2de3SMichal Malý 	for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
890f31a2de3SMichal Malý 		const u16 mode_product_id = lg4ff_alternate_modes[i].product_id;
891f31a2de3SMichal Malý 		const char *tag = lg4ff_alternate_modes[i].tag;
892f31a2de3SMichal Malý 
89372529c65SMichal Malý 		if (entry->wdata.alternate_modes & BIT(i)) {
894f31a2de3SMichal Malý 			if (!strcmp(tag, lbuf)) {
895f31a2de3SMichal Malý 				if (!mode_product_id)
89672529c65SMichal Malý 					target_product_id = entry->wdata.real_product_id;
897f31a2de3SMichal Malý 				else
898f31a2de3SMichal Malý 					target_product_id = mode_product_id;
899f31a2de3SMichal Malý 				break;
900f31a2de3SMichal Malý 			}
901f31a2de3SMichal Malý 		}
902f31a2de3SMichal Malý 	}
903f31a2de3SMichal Malý 
904f31a2de3SMichal Malý 	if (i == LG4FF_MODE_MAX_IDX) {
905f31a2de3SMichal Malý 		hid_info(hid, "Requested mode \"%s\" is not supported by the device\n", lbuf);
906f31a2de3SMichal Malý 		kfree(lbuf);
907f31a2de3SMichal Malý 		return -EINVAL;
908f31a2de3SMichal Malý 	}
909f31a2de3SMichal Malý 	kfree(lbuf); /* Not needed anymore */
910f31a2de3SMichal Malý 
91172529c65SMichal Malý 	if (target_product_id == entry->wdata.product_id) /* Nothing to do */
912f31a2de3SMichal Malý 		return count;
913f31a2de3SMichal Malý 
914f31a2de3SMichal Malý 	/* Automatic switching has to be disabled for the switch to DF-EX mode to work correctly */
915f31a2de3SMichal Malý 	if (target_product_id == USB_DEVICE_ID_LOGITECH_WHEEL && !lg4ff_no_autoswitch) {
916f31a2de3SMichal 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",
91772529c65SMichal Malý 			 entry->wdata.real_name);
918f31a2de3SMichal Malý 		return -EINVAL;
919f31a2de3SMichal Malý 	}
920f31a2de3SMichal Malý 
921f31a2de3SMichal Malý 	/* Take care of hardware limitations */
92272529c65SMichal Malý 	if ((entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_DFP_WHEEL || entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_G25_WHEEL) &&
92372529c65SMichal Malý 	    entry->wdata.product_id > target_product_id) {
92472529c65SMichal Malý 		hid_info(hid, "\"%s\" cannot be switched back into \"%s\" mode\n", entry->wdata.real_name, lg4ff_alternate_modes[i].name);
925f31a2de3SMichal Malý 		return -EINVAL;
926f31a2de3SMichal Malý 	}
927f31a2de3SMichal Malý 
92872529c65SMichal Malý 	s = lg4ff_get_mode_switch_command(entry->wdata.real_product_id, target_product_id);
929f31a2de3SMichal Malý 	if (!s) {
930f31a2de3SMichal Malý 		hid_err(hid, "Invalid target product ID %X\n", target_product_id);
931f31a2de3SMichal Malý 		return -EINVAL;
932f31a2de3SMichal Malý 	}
933f31a2de3SMichal Malý 
934f31a2de3SMichal Malý 	ret = lg4ff_switch_compatibility_mode(hid, s);
935f31a2de3SMichal Malý 	return (ret == 0 ? count : ret);
936b96d23ecSMichal Malý }
937b96d23ecSMichal Malý static DEVICE_ATTR(alternate_modes, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_alternate_modes_show, lg4ff_alternate_modes_store);
938b96d23ecSMichal Malý 
939961af46fSSimon Wood static ssize_t lg4ff_combine_show(struct device *dev, struct device_attribute *attr,
940961af46fSSimon Wood 				char *buf)
941961af46fSSimon Wood {
942961af46fSSimon Wood 	struct hid_device *hid = to_hid_device(dev);
943961af46fSSimon Wood 	struct lg4ff_device_entry *entry;
944961af46fSSimon Wood 	struct lg_drv_data *drv_data;
945961af46fSSimon Wood 	size_t count;
946961af46fSSimon Wood 
947961af46fSSimon Wood 	drv_data = hid_get_drvdata(hid);
948961af46fSSimon Wood 	if (!drv_data) {
949961af46fSSimon Wood 		hid_err(hid, "Private driver data not found!\n");
950961af46fSSimon Wood 		return 0;
951961af46fSSimon Wood 	}
952961af46fSSimon Wood 
953961af46fSSimon Wood 	entry = drv_data->device_props;
954961af46fSSimon Wood 	if (!entry) {
955961af46fSSimon Wood 		hid_err(hid, "Device properties not found!\n");
956961af46fSSimon Wood 		return 0;
957961af46fSSimon Wood 	}
958961af46fSSimon Wood 
959961af46fSSimon Wood 	count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->wdata.combine);
960961af46fSSimon Wood 	return count;
961961af46fSSimon Wood }
962961af46fSSimon Wood 
963961af46fSSimon Wood static ssize_t lg4ff_combine_store(struct device *dev, struct device_attribute *attr,
964961af46fSSimon Wood 				 const char *buf, size_t count)
965961af46fSSimon Wood {
966961af46fSSimon Wood 	struct hid_device *hid = to_hid_device(dev);
967961af46fSSimon Wood 	struct lg4ff_device_entry *entry;
968961af46fSSimon Wood 	struct lg_drv_data *drv_data;
969961af46fSSimon Wood 	u16 combine = simple_strtoul(buf, NULL, 10);
970961af46fSSimon Wood 
971961af46fSSimon Wood 	drv_data = hid_get_drvdata(hid);
972961af46fSSimon Wood 	if (!drv_data) {
973961af46fSSimon Wood 		hid_err(hid, "Private driver data not found!\n");
974961af46fSSimon Wood 		return -EINVAL;
975961af46fSSimon Wood 	}
976961af46fSSimon Wood 
977961af46fSSimon Wood 	entry = drv_data->device_props;
978961af46fSSimon Wood 	if (!entry) {
979961af46fSSimon Wood 		hid_err(hid, "Device properties not found!\n");
980961af46fSSimon Wood 		return -EINVAL;
981961af46fSSimon Wood 	}
982961af46fSSimon Wood 
983961af46fSSimon Wood 	if (combine > 1)
984961af46fSSimon Wood 		combine = 1;
985961af46fSSimon Wood 
986961af46fSSimon Wood 	entry->wdata.combine = combine;
987961af46fSSimon Wood 	return count;
988961af46fSSimon Wood }
989961af46fSSimon Wood static DEVICE_ATTR(combine_pedals, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_combine_show, lg4ff_combine_store);
990961af46fSSimon Wood 
991fbf85e2aSMichal Malý /* Export the currently set range of the wheel */
992fbf85e2aSMichal Malý static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr,
9932f1cec32SVivien Didelot 				char *buf)
99430bb75d7SMichal Malý {
99530bb75d7SMichal Malý 	struct hid_device *hid = to_hid_device(dev);
9963b6b17b7SMichal Malý 	struct lg4ff_device_entry *entry;
9973b6b17b7SMichal Malý 	struct lg_drv_data *drv_data;
99830bb75d7SMichal Malý 	size_t count;
99930bb75d7SMichal Malý 
10003b6b17b7SMichal Malý 	drv_data = hid_get_drvdata(hid);
10013b6b17b7SMichal Malý 	if (!drv_data) {
10023b6b17b7SMichal Malý 		hid_err(hid, "Private driver data not found!\n");
10033b6b17b7SMichal Malý 		return 0;
100430bb75d7SMichal Malý 	}
10053b6b17b7SMichal Malý 
10063b6b17b7SMichal Malý 	entry = drv_data->device_props;
10073b6b17b7SMichal Malý 	if (!entry) {
10083b6b17b7SMichal Malý 		hid_err(hid, "Device properties not found!\n");
100930bb75d7SMichal Malý 		return 0;
101030bb75d7SMichal Malý 	}
101130bb75d7SMichal Malý 
101272529c65SMichal Malý 	count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->wdata.range);
101330bb75d7SMichal Malý 	return count;
101430bb75d7SMichal Malý }
101530bb75d7SMichal Malý 
101630bb75d7SMichal Malý /* Set range to user specified value, call appropriate function
101730bb75d7SMichal Malý  * according to the type of the wheel */
1018fbf85e2aSMichal Malý static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr,
10192f1cec32SVivien Didelot 				 const char *buf, size_t count)
102030bb75d7SMichal Malý {
102130bb75d7SMichal Malý 	struct hid_device *hid = to_hid_device(dev);
10223b6b17b7SMichal Malý 	struct lg4ff_device_entry *entry;
10233b6b17b7SMichal Malý 	struct lg_drv_data *drv_data;
10242a552c30SMichal Malý 	u16 range = simple_strtoul(buf, NULL, 10);
102530bb75d7SMichal Malý 
10263b6b17b7SMichal Malý 	drv_data = hid_get_drvdata(hid);
10273b6b17b7SMichal Malý 	if (!drv_data) {
10283b6b17b7SMichal Malý 		hid_err(hid, "Private driver data not found!\n");
102929ff6657SSimon Wood 		return -EINVAL;
103030bb75d7SMichal Malý 	}
10313b6b17b7SMichal Malý 
10323b6b17b7SMichal Malý 	entry = drv_data->device_props;
10333b6b17b7SMichal Malý 	if (!entry) {
10343b6b17b7SMichal Malý 		hid_err(hid, "Device properties not found!\n");
103529ff6657SSimon Wood 		return -EINVAL;
103630bb75d7SMichal Malý 	}
103730bb75d7SMichal Malý 
103830bb75d7SMichal Malý 	if (range == 0)
103972529c65SMichal Malý 		range = entry->wdata.max_range;
104030bb75d7SMichal Malý 
104130bb75d7SMichal Malý 	/* Check if the wheel supports range setting
104230bb75d7SMichal Malý 	 * and that the range is within limits for the wheel */
104372529c65SMichal Malý 	if (entry->wdata.set_range && range >= entry->wdata.min_range && range <= entry->wdata.max_range) {
104472529c65SMichal Malý 		entry->wdata.set_range(hid, range);
104572529c65SMichal Malý 		entry->wdata.range = range;
104630bb75d7SMichal Malý 	}
104730bb75d7SMichal Malý 
104830bb75d7SMichal Malý 	return count;
104930bb75d7SMichal Malý }
1050fbf85e2aSMichal Malý static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_range_show, lg4ff_range_store);
105130bb75d7SMichal Malý 
1052b96d23ecSMichal Malý static ssize_t lg4ff_real_id_show(struct device *dev, struct device_attribute *attr, char *buf)
1053b96d23ecSMichal Malý {
1054b96d23ecSMichal Malý 	struct hid_device *hid = to_hid_device(dev);
1055b96d23ecSMichal Malý 	struct lg4ff_device_entry *entry;
1056b96d23ecSMichal Malý 	struct lg_drv_data *drv_data;
1057b96d23ecSMichal Malý 	size_t count;
1058b96d23ecSMichal Malý 
1059b96d23ecSMichal Malý 	drv_data = hid_get_drvdata(hid);
1060b96d23ecSMichal Malý 	if (!drv_data) {
1061b96d23ecSMichal Malý 		hid_err(hid, "Private driver data not found!\n");
1062b96d23ecSMichal Malý 		return 0;
1063b96d23ecSMichal Malý 	}
1064b96d23ecSMichal Malý 
1065b96d23ecSMichal Malý 	entry = drv_data->device_props;
1066b96d23ecSMichal Malý 	if (!entry) {
1067b96d23ecSMichal Malý 		hid_err(hid, "Device properties not found!\n");
1068b96d23ecSMichal Malý 		return 0;
1069b96d23ecSMichal Malý 	}
1070b96d23ecSMichal Malý 
107172529c65SMichal Malý 	if (!entry->wdata.real_tag || !entry->wdata.real_name) {
1072b96d23ecSMichal Malý 		hid_err(hid, "NULL pointer to string\n");
1073b96d23ecSMichal Malý 		return 0;
1074b96d23ecSMichal Malý 	}
1075b96d23ecSMichal Malý 
107672529c65SMichal Malý 	count = scnprintf(buf, PAGE_SIZE, "%s: %s\n", entry->wdata.real_tag, entry->wdata.real_name);
1077b96d23ecSMichal Malý 	return count;
1078b96d23ecSMichal Malý }
1079b96d23ecSMichal Malý 
1080b96d23ecSMichal Malý static ssize_t lg4ff_real_id_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1081b96d23ecSMichal Malý {
1082b96d23ecSMichal Malý 	/* Real ID is a read-only value */
1083b96d23ecSMichal Malý 	return -EPERM;
1084b96d23ecSMichal Malý }
1085b96d23ecSMichal Malý static DEVICE_ATTR(real_id, S_IRUGO, lg4ff_real_id_show, lg4ff_real_id_store);
1086b96d23ecSMichal Malý 
108722bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS
10882a552c30SMichal Malý static void lg4ff_set_leds(struct hid_device *hid, u8 leds)
108922bcefdcSSimon Wood {
1090c918fe78SMichal Malý 	struct lg_drv_data *drv_data;
1091c918fe78SMichal Malý 	struct lg4ff_device_entry *entry;
1092c918fe78SMichal Malý 	unsigned long flags;
1093c28abd8cSMichal Malý 	s32 *value;
109422bcefdcSSimon Wood 
1095c918fe78SMichal Malý 	drv_data = hid_get_drvdata(hid);
1096c918fe78SMichal Malý 	if (!drv_data) {
1097c918fe78SMichal Malý 		hid_err(hid, "Private driver data not found!\n");
1098c918fe78SMichal Malý 		return;
1099c918fe78SMichal Malý 	}
1100c918fe78SMichal Malý 
1101c918fe78SMichal Malý 	entry = drv_data->device_props;
1102c918fe78SMichal Malý 	if (!entry) {
1103c918fe78SMichal Malý 		hid_err(hid, "Device properties not found!\n");
1104c918fe78SMichal Malý 		return;
1105c918fe78SMichal Malý 	}
1106c28abd8cSMichal Malý 	value = entry->report->field[0]->value;
1107c918fe78SMichal Malý 
1108c918fe78SMichal Malý 	spin_lock_irqsave(&entry->report_lock, flags);
110974479ba8SMichal Malý 	value[0] = 0xf8;
111074479ba8SMichal Malý 	value[1] = 0x12;
111174479ba8SMichal Malý 	value[2] = leds;
111274479ba8SMichal Malý 	value[3] = 0x00;
111374479ba8SMichal Malý 	value[4] = 0x00;
111474479ba8SMichal Malý 	value[5] = 0x00;
111574479ba8SMichal Malý 	value[6] = 0x00;
1116c28abd8cSMichal Malý 	hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
1117c918fe78SMichal Malý 	spin_unlock_irqrestore(&entry->report_lock, flags);
111822bcefdcSSimon Wood }
111922bcefdcSSimon Wood 
112022bcefdcSSimon Wood static void lg4ff_led_set_brightness(struct led_classdev *led_cdev,
112122bcefdcSSimon Wood 			enum led_brightness value)
112222bcefdcSSimon Wood {
112322bcefdcSSimon Wood 	struct device *dev = led_cdev->dev->parent;
1124ee79a8f8SGeliang Tang 	struct hid_device *hid = to_hid_device(dev);
11254629fd16SAxel Lin 	struct lg_drv_data *drv_data = hid_get_drvdata(hid);
112622bcefdcSSimon Wood 	struct lg4ff_device_entry *entry;
112722bcefdcSSimon Wood 	int i, state = 0;
112822bcefdcSSimon Wood 
112922bcefdcSSimon Wood 	if (!drv_data) {
113022bcefdcSSimon Wood 		hid_err(hid, "Device data not found.");
113122bcefdcSSimon Wood 		return;
113222bcefdcSSimon Wood 	}
113322bcefdcSSimon Wood 
1134371a1d9eSMichal Malý 	entry = drv_data->device_props;
113522bcefdcSSimon Wood 
113622bcefdcSSimon Wood 	if (!entry) {
113722bcefdcSSimon Wood 		hid_err(hid, "Device properties not found.");
113822bcefdcSSimon Wood 		return;
113922bcefdcSSimon Wood 	}
114022bcefdcSSimon Wood 
114122bcefdcSSimon Wood 	for (i = 0; i < 5; i++) {
114272529c65SMichal Malý 		if (led_cdev != entry->wdata.led[i])
114322bcefdcSSimon Wood 			continue;
114472529c65SMichal Malý 		state = (entry->wdata.led_state >> i) & 1;
114522bcefdcSSimon Wood 		if (value == LED_OFF && state) {
114672529c65SMichal Malý 			entry->wdata.led_state &= ~(1 << i);
114772529c65SMichal Malý 			lg4ff_set_leds(hid, entry->wdata.led_state);
114822bcefdcSSimon Wood 		} else if (value != LED_OFF && !state) {
114972529c65SMichal Malý 			entry->wdata.led_state |= 1 << i;
115072529c65SMichal Malý 			lg4ff_set_leds(hid, entry->wdata.led_state);
115122bcefdcSSimon Wood 		}
115222bcefdcSSimon Wood 		break;
115322bcefdcSSimon Wood 	}
115422bcefdcSSimon Wood }
115522bcefdcSSimon Wood 
115622bcefdcSSimon Wood static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev)
115722bcefdcSSimon Wood {
115822bcefdcSSimon Wood 	struct device *dev = led_cdev->dev->parent;
1159ee79a8f8SGeliang Tang 	struct hid_device *hid = to_hid_device(dev);
11604629fd16SAxel Lin 	struct lg_drv_data *drv_data = hid_get_drvdata(hid);
116122bcefdcSSimon Wood 	struct lg4ff_device_entry *entry;
116222bcefdcSSimon Wood 	int i, value = 0;
116322bcefdcSSimon Wood 
116422bcefdcSSimon Wood 	if (!drv_data) {
116522bcefdcSSimon Wood 		hid_err(hid, "Device data not found.");
116622bcefdcSSimon Wood 		return LED_OFF;
116722bcefdcSSimon Wood 	}
116822bcefdcSSimon Wood 
1169371a1d9eSMichal Malý 	entry = drv_data->device_props;
117022bcefdcSSimon Wood 
117122bcefdcSSimon Wood 	if (!entry) {
117222bcefdcSSimon Wood 		hid_err(hid, "Device properties not found.");
117322bcefdcSSimon Wood 		return LED_OFF;
117422bcefdcSSimon Wood 	}
117522bcefdcSSimon Wood 
117622bcefdcSSimon Wood 	for (i = 0; i < 5; i++)
117772529c65SMichal Malý 		if (led_cdev == entry->wdata.led[i]) {
117872529c65SMichal Malý 			value = (entry->wdata.led_state >> i) & 1;
117922bcefdcSSimon Wood 			break;
118022bcefdcSSimon Wood 		}
118122bcefdcSSimon Wood 
118222bcefdcSSimon Wood 	return value ? LED_FULL : LED_OFF;
118322bcefdcSSimon Wood }
118422bcefdcSSimon Wood #endif
118522bcefdcSSimon Wood 
1186e7c23449SMichal Malý static u16 lg4ff_identify_multimode_wheel(struct hid_device *hid, const u16 reported_product_id, const u16 bcdDevice)
1187e7c23449SMichal Malý {
1188bbec1bd0SSimon Wood 	u32 current_mode;
1189bbec1bd0SSimon Wood 	int i;
1190e7c23449SMichal Malý 
1191bbec1bd0SSimon Wood 	/* identify current mode from USB PID */
1192bbec1bd0SSimon Wood 	for (i = 1; i < ARRAY_SIZE(lg4ff_alternate_modes); i++) {
1193bbec1bd0SSimon Wood 		dbg_hid("Testing whether PID is %X\n", lg4ff_alternate_modes[i].product_id);
1194bbec1bd0SSimon Wood 		if (reported_product_id == lg4ff_alternate_modes[i].product_id)
1195e7c23449SMichal Malý 			break;
1196e7c23449SMichal Malý 	}
1197e7c23449SMichal Malý 
1198bbec1bd0SSimon Wood 	if (i == ARRAY_SIZE(lg4ff_alternate_modes))
1199bbec1bd0SSimon Wood 		return 0;
1200e7c23449SMichal Malý 
1201bbec1bd0SSimon Wood 	current_mode = BIT(i);
1202bbec1bd0SSimon Wood 
1203bbec1bd0SSimon Wood 	for (i = 0; i < ARRAY_SIZE(lg4ff_main_checklist); i++) {
1204bbec1bd0SSimon Wood 		const u16 mask = lg4ff_main_checklist[i]->mask;
1205bbec1bd0SSimon Wood 		const u16 result = lg4ff_main_checklist[i]->result;
1206bbec1bd0SSimon Wood 		const u16 real_product_id = lg4ff_main_checklist[i]->real_product_id;
1207bbec1bd0SSimon Wood 
1208bbec1bd0SSimon Wood 		if ((current_mode & lg4ff_main_checklist[i]->modes) && \
1209bbec1bd0SSimon Wood 				(bcdDevice & mask) == result) {
1210e7c23449SMichal Malý 			dbg_hid("Found wheel with real PID %X whose reported PID is %X\n", real_product_id, reported_product_id);
1211e7c23449SMichal Malý 			return real_product_id;
1212e7c23449SMichal Malý 		}
1213e7c23449SMichal Malý 	}
1214e7c23449SMichal Malý 
1215f31a2de3SMichal Malý 	/* No match found. This is either Driving Force or an unknown
1216f31a2de3SMichal Malý 	 * wheel model, do not touch it */
1217e7c23449SMichal Malý 	dbg_hid("Wheel with bcdDevice %X was not recognized as multimode wheel, leaving in its current mode\n", bcdDevice);
1218e7c23449SMichal Malý 	return 0;
1219e7c23449SMichal Malý }
1220e7c23449SMichal Malý 
1221e7c23449SMichal Malý static int lg4ff_handle_multimode_wheel(struct hid_device *hid, u16 *real_product_id, const u16 bcdDevice)
1222e7c23449SMichal Malý {
1223e7c23449SMichal Malý 	const u16 reported_product_id = hid->product;
1224e7c23449SMichal Malý 	int ret;
1225e7c23449SMichal Malý 
1226e7c23449SMichal Malý 	*real_product_id = lg4ff_identify_multimode_wheel(hid, reported_product_id, bcdDevice);
1227e7c23449SMichal Malý 	/* Probed wheel is not a multimode wheel */
1228e7c23449SMichal Malý 	if (!*real_product_id) {
1229e7c23449SMichal Malý 		*real_product_id = reported_product_id;
1230e7c23449SMichal Malý 		dbg_hid("Wheel is not a multimode wheel\n");
1231e7c23449SMichal Malý 		return LG4FF_MMODE_NOT_MULTIMODE;
1232e7c23449SMichal Malý 	}
1233e7c23449SMichal Malý 
1234e7c23449SMichal Malý 	/* Switch from "Driving Force" mode to native mode automatically.
1235e7c23449SMichal Malý 	 * Otherwise keep the wheel in its current mode */
1236e7c23449SMichal Malý 	if (reported_product_id == USB_DEVICE_ID_LOGITECH_WHEEL &&
1237a54dc779SMichal Malý 	    reported_product_id != *real_product_id &&
1238a54dc779SMichal Malý 	    !lg4ff_no_autoswitch) {
1239f31a2de3SMichal Malý 		const struct lg4ff_compat_mode_switch *s = lg4ff_get_mode_switch_command(*real_product_id, *real_product_id);
1240e7c23449SMichal Malý 
1241f31a2de3SMichal Malý 		if (!s) {
1242e7c23449SMichal Malý 			hid_err(hid, "Invalid product id %X\n", *real_product_id);
1243b96d23ecSMichal Malý 			return LG4FF_MMODE_NOT_MULTIMODE;
1244e7c23449SMichal Malý 		}
1245e7c23449SMichal Malý 
1246e7c23449SMichal Malý 		ret = lg4ff_switch_compatibility_mode(hid, s);
1247e7c23449SMichal Malý 		if (ret) {
1248e7c23449SMichal Malý 			/* Wheel could not have been switched to native mode,
1249e7c23449SMichal Malý 			 * leave it in "Driving Force" mode and continue */
1250e7c23449SMichal Malý 			hid_err(hid, "Unable to switch wheel mode, errno %d\n", ret);
1251b96d23ecSMichal Malý 			return LG4FF_MMODE_IS_MULTIMODE;
1252e7c23449SMichal Malý 		}
1253e7c23449SMichal Malý 		return LG4FF_MMODE_SWITCHED;
1254e7c23449SMichal Malý 	}
1255e7c23449SMichal Malý 
1256b96d23ecSMichal Malý 	return LG4FF_MMODE_IS_MULTIMODE;
1257e7c23449SMichal Malý }
1258e7c23449SMichal Malý 
1259e7c23449SMichal Malý 
126032c88cbcSSimon Wood int lg4ff_init(struct hid_device *hid)
126132c88cbcSSimon Wood {
126232c88cbcSSimon Wood 	struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
126332c88cbcSSimon Wood 	struct input_dev *dev = hidinput->input;
1264c28abd8cSMichal Malý 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
1265c28abd8cSMichal Malý 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
1266e7c23449SMichal Malý 	const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
1267e7c23449SMichal Malý 	const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
12685d9d60adSMichal Malý 	const struct lg4ff_multimode_wheel *mmode_wheel = NULL;
126930bb75d7SMichal Malý 	struct lg4ff_device_entry *entry;
12703b6b17b7SMichal Malý 	struct lg_drv_data *drv_data;
1271b96d23ecSMichal Malý 	int error, i, j;
1272b96d23ecSMichal Malý 	int mmode_ret, mmode_idx = -1;
1273e7c23449SMichal Malý 	u16 real_product_id;
127432c88cbcSSimon Wood 
127532c88cbcSSimon Wood 	/* Check that the report looks ok */
12760fb6bd06SKees Cook 	if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
127732c88cbcSSimon Wood 		return -1;
127832c88cbcSSimon Wood 
127972529c65SMichal Malý 	drv_data = hid_get_drvdata(hid);
128072529c65SMichal Malý 	if (!drv_data) {
128172529c65SMichal Malý 		hid_err(hid, "Cannot add device, private driver data not allocated\n");
128272529c65SMichal Malý 		return -1;
128372529c65SMichal Malý 	}
128472529c65SMichal Malý 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
128572529c65SMichal Malý 	if (!entry)
128672529c65SMichal Malý 		return -ENOMEM;
1287c918fe78SMichal Malý 	spin_lock_init(&entry->report_lock);
1288c28abd8cSMichal Malý 	entry->report = report;
128972529c65SMichal Malý 	drv_data->device_props = entry;
129072529c65SMichal Malý 
1291e7c23449SMichal Malý 	/* Check if a multimode wheel has been connected and
1292e7c23449SMichal Malý 	 * handle it appropriately */
1293b96d23ecSMichal Malý 	mmode_ret = lg4ff_handle_multimode_wheel(hid, &real_product_id, bcdDevice);
1294e7c23449SMichal Malý 
1295e7c23449SMichal Malý 	/* Wheel has been told to switch to native mode. There is no point in going on
1296e7c23449SMichal Malý 	 * with the initialization as the wheel will do a USB reset when it switches mode
1297e7c23449SMichal Malý 	 */
1298b96d23ecSMichal Malý 	if (mmode_ret == LG4FF_MMODE_SWITCHED)
1299e7c23449SMichal Malý 		return 0;
130072529c65SMichal Malý 	else if (mmode_ret < 0) {
130172529c65SMichal Malý 		hid_err(hid, "Unable to switch device mode during initialization, errno %d\n", mmode_ret);
130272529c65SMichal Malý 		error = mmode_ret;
130372529c65SMichal Malý 		goto err_init;
130472529c65SMichal Malý 	}
1305e7c23449SMichal Malý 
13067362cd22SMichal Malý 	/* Check what wheel has been connected */
13077362cd22SMichal Malý 	for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
13087362cd22SMichal Malý 		if (hid->product == lg4ff_devices[i].product_id) {
13097362cd22SMichal Malý 			dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
13107362cd22SMichal Malý 			break;
13117362cd22SMichal Malý 		}
13127362cd22SMichal Malý 	}
13137362cd22SMichal Malý 
13147362cd22SMichal Malý 	if (i == ARRAY_SIZE(lg4ff_devices)) {
13159c2a6bd1SMichal 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. "
13169c2a6bd1SMichal Malý 			     "Please report this as a bug to LKML, Simon Wood <simon@mungewell.org> or "
13179c2a6bd1SMichal Malý 			     "Michal Maly <madcatxster@devoid-pointer.net>\n");
131872529c65SMichal Malý 		error = -1;
131972529c65SMichal Malý 		goto err_init;
13207362cd22SMichal Malý 	}
13217362cd22SMichal Malý 
1322b96d23ecSMichal Malý 	if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1323b96d23ecSMichal Malý 		for (mmode_idx = 0; mmode_idx < ARRAY_SIZE(lg4ff_multimode_wheels); mmode_idx++) {
1324b96d23ecSMichal Malý 			if (real_product_id == lg4ff_multimode_wheels[mmode_idx].product_id)
1325b96d23ecSMichal Malý 				break;
1326b96d23ecSMichal Malý 		}
1327b96d23ecSMichal Malý 
1328b96d23ecSMichal Malý 		if (mmode_idx == ARRAY_SIZE(lg4ff_multimode_wheels)) {
1329b96d23ecSMichal Malý 			hid_err(hid, "Device product ID %X is not listed as a multimode wheel", real_product_id);
133072529c65SMichal Malý 			error = -1;
133172529c65SMichal Malý 			goto err_init;
1332b96d23ecSMichal Malý 		}
1333b96d23ecSMichal Malý 	}
1334b96d23ecSMichal Malý 
13357362cd22SMichal Malý 	/* Set supported force feedback capabilities */
13367362cd22SMichal Malý 	for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
13377362cd22SMichal Malý 		set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
133832c88cbcSSimon Wood 
1339d0afd848SMichal Malý 	error = input_ff_create_memless(dev, NULL, lg4ff_play);
134032c88cbcSSimon Wood 
134132c88cbcSSimon Wood 	if (error)
134272529c65SMichal Malý 		goto err_init;
134332c88cbcSSimon Wood 
13445d9d60adSMichal Malý 	/* Initialize device properties */
1345b96d23ecSMichal Malý 	if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1346b96d23ecSMichal Malý 		BUG_ON(mmode_idx == -1);
13475d9d60adSMichal Malý 		mmode_wheel = &lg4ff_multimode_wheels[mmode_idx];
1348b96d23ecSMichal Malý 	}
13495d9d60adSMichal Malý 	lg4ff_init_wheel_data(&entry->wdata, &lg4ff_devices[i], mmode_wheel, real_product_id);
135030bb75d7SMichal Malý 
1351114a55cfSSimon Wood 	/* Check if autocentering is available and
1352114a55cfSSimon Wood 	 * set the centering force to zero by default */
1353114a55cfSSimon Wood 	if (test_bit(FF_AUTOCENTER, dev->ffbit)) {
1354e7c23449SMichal Malý 		/* Formula Force EX expects different autocentering command */
1355e7c23449SMichal Malý 		if ((bcdDevice >> 8) == LG4FF_FFEX_REV_MAJ &&
1356e7c23449SMichal Malý 		    (bcdDevice & 0xff) == LG4FF_FFEX_REV_MIN)
1357d0afd848SMichal Malý 			dev->ff->set_autocenter = lg4ff_set_autocenter_ffex;
1358114a55cfSSimon Wood 		else
1359d0afd848SMichal Malý 			dev->ff->set_autocenter = lg4ff_set_autocenter_default;
1360114a55cfSSimon Wood 
1361114a55cfSSimon Wood 		dev->ff->set_autocenter(dev, 0);
1362114a55cfSSimon Wood 	}
1363114a55cfSSimon Wood 
136430bb75d7SMichal Malý 	/* Create sysfs interface */
1365961af46fSSimon Wood 	error = device_create_file(&hid->dev, &dev_attr_combine_pedals);
1366961af46fSSimon Wood 	if (error)
1367961af46fSSimon Wood 		hid_warn(hid, "Unable to create sysfs interface for \"combine\", errno %d\n", error);
136830bb75d7SMichal Malý 	error = device_create_file(&hid->dev, &dev_attr_range);
136930bb75d7SMichal Malý 	if (error)
1370d61a70ecSMichal Malý 		hid_warn(hid, "Unable to create sysfs interface for \"range\", errno %d\n", error);
1371b96d23ecSMichal Malý 	if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1372b96d23ecSMichal Malý 		error = device_create_file(&hid->dev, &dev_attr_real_id);
1373b96d23ecSMichal Malý 		if (error)
1374d61a70ecSMichal Malý 			hid_warn(hid, "Unable to create sysfs interface for \"real_id\", errno %d\n", error);
1375b96d23ecSMichal Malý 		error = device_create_file(&hid->dev, &dev_attr_alternate_modes);
1376b96d23ecSMichal Malý 		if (error)
1377d61a70ecSMichal Malý 			hid_warn(hid, "Unable to create sysfs interface for \"alternate_modes\", errno %d\n", error);
1378b96d23ecSMichal Malý 	}
137930bb75d7SMichal Malý 	dbg_hid("sysfs interface created\n");
138030bb75d7SMichal Malý 
138130bb75d7SMichal Malý 	/* Set the maximum range to start with */
138272529c65SMichal Malý 	entry->wdata.range = entry->wdata.max_range;
138372529c65SMichal Malý 	if (entry->wdata.set_range)
138472529c65SMichal Malý 		entry->wdata.set_range(hid, entry->wdata.range);
138530bb75d7SMichal Malý 
138622bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS
138729fae1c8SSimon Wood 	/* register led subsystem - G27/G29 only */
138872529c65SMichal Malý 	entry->wdata.led_state = 0;
138922bcefdcSSimon Wood 	for (j = 0; j < 5; j++)
139072529c65SMichal Malý 		entry->wdata.led[j] = NULL;
139122bcefdcSSimon Wood 
139229fae1c8SSimon Wood 	if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL ||
139329fae1c8SSimon Wood 			lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G29_WHEEL) {
139422bcefdcSSimon Wood 		struct led_classdev *led;
139522bcefdcSSimon Wood 		size_t name_sz;
139622bcefdcSSimon Wood 		char *name;
139722bcefdcSSimon Wood 
139822bcefdcSSimon Wood 		lg4ff_set_leds(hid, 0);
139922bcefdcSSimon Wood 
140022bcefdcSSimon Wood 		name_sz = strlen(dev_name(&hid->dev)) + 8;
140122bcefdcSSimon Wood 
140222bcefdcSSimon Wood 		for (j = 0; j < 5; j++) {
140322bcefdcSSimon Wood 			led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
140422bcefdcSSimon Wood 			if (!led) {
140522bcefdcSSimon Wood 				hid_err(hid, "can't allocate memory for LED %d\n", j);
140672529c65SMichal Malý 				goto err_leds;
140722bcefdcSSimon Wood 			}
140822bcefdcSSimon Wood 
140922bcefdcSSimon Wood 			name = (void *)(&led[1]);
141022bcefdcSSimon Wood 			snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1);
141122bcefdcSSimon Wood 			led->name = name;
141222bcefdcSSimon Wood 			led->brightness = 0;
141322bcefdcSSimon Wood 			led->max_brightness = 1;
141422bcefdcSSimon Wood 			led->brightness_get = lg4ff_led_get_brightness;
141522bcefdcSSimon Wood 			led->brightness_set = lg4ff_led_set_brightness;
141622bcefdcSSimon Wood 
141772529c65SMichal Malý 			entry->wdata.led[j] = led;
141822bcefdcSSimon Wood 			error = led_classdev_register(&hid->dev, led);
141922bcefdcSSimon Wood 
142022bcefdcSSimon Wood 			if (error) {
142122bcefdcSSimon Wood 				hid_err(hid, "failed to register LED %d. Aborting.\n", j);
142272529c65SMichal Malý err_leds:
142322bcefdcSSimon Wood 				/* Deregister LEDs (if any) */
142422bcefdcSSimon Wood 				for (j = 0; j < 5; j++) {
142572529c65SMichal Malý 					led = entry->wdata.led[j];
142672529c65SMichal Malý 					entry->wdata.led[j] = NULL;
142722bcefdcSSimon Wood 					if (!led)
142822bcefdcSSimon Wood 						continue;
142922bcefdcSSimon Wood 					led_classdev_unregister(led);
143022bcefdcSSimon Wood 					kfree(led);
143122bcefdcSSimon Wood 				}
143222bcefdcSSimon Wood 				goto out;	/* Let the driver continue without LEDs */
143322bcefdcSSimon Wood 			}
143422bcefdcSSimon Wood 		}
143522bcefdcSSimon Wood 	}
143622bcefdcSSimon Wood out:
1437c6e6dc87SJiri Kosina #endif
143864013800SSimon Wood 	hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n");
143932c88cbcSSimon Wood 	return 0;
144072529c65SMichal Malý 
144172529c65SMichal Malý err_init:
144272529c65SMichal Malý 	drv_data->device_props = NULL;
144372529c65SMichal Malý 	kfree(entry);
144472529c65SMichal Malý 	return error;
144532c88cbcSSimon Wood }
144632c88cbcSSimon Wood 
144730bb75d7SMichal Malý int lg4ff_deinit(struct hid_device *hid)
144830bb75d7SMichal Malý {
144930bb75d7SMichal Malý 	struct lg4ff_device_entry *entry;
14503b6b17b7SMichal Malý 	struct lg_drv_data *drv_data;
14516a2e176bSMichal Malý 
14523b6b17b7SMichal Malý 	drv_data = hid_get_drvdata(hid);
14533b6b17b7SMichal Malý 	if (!drv_data) {
14543b6b17b7SMichal Malý 		hid_err(hid, "Error while deinitializing device, no private driver data.\n");
145530bb75d7SMichal Malý 		return -1;
145630bb75d7SMichal Malý 	}
14573b6b17b7SMichal Malý 	entry = drv_data->device_props;
1458e7c23449SMichal Malý 	if (!entry)
1459e7c23449SMichal Malý 		goto out; /* Nothing more to do */
1460e7c23449SMichal Malý 
1461b96d23ecSMichal Malý 	/* Multimode devices will have at least the "MODE_NATIVE" bit set */
146272529c65SMichal Malý 	if (entry->wdata.alternate_modes) {
1463b96d23ecSMichal Malý 		device_remove_file(&hid->dev, &dev_attr_real_id);
1464b96d23ecSMichal Malý 		device_remove_file(&hid->dev, &dev_attr_alternate_modes);
1465b96d23ecSMichal Malý 	}
1466b96d23ecSMichal Malý 
1467961af46fSSimon Wood 	device_remove_file(&hid->dev, &dev_attr_combine_pedals);
146872529c65SMichal Malý 	device_remove_file(&hid->dev, &dev_attr_range);
146922bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS
147022bcefdcSSimon Wood 	{
147122bcefdcSSimon Wood 		int j;
147222bcefdcSSimon Wood 		struct led_classdev *led;
147322bcefdcSSimon Wood 
147422bcefdcSSimon Wood 		/* Deregister LEDs (if any) */
147522bcefdcSSimon Wood 		for (j = 0; j < 5; j++) {
147622bcefdcSSimon Wood 
147772529c65SMichal Malý 			led = entry->wdata.led[j];
147872529c65SMichal Malý 			entry->wdata.led[j] = NULL;
147922bcefdcSSimon Wood 			if (!led)
148022bcefdcSSimon Wood 				continue;
148122bcefdcSSimon Wood 			led_classdev_unregister(led);
148222bcefdcSSimon Wood 			kfree(led);
148322bcefdcSSimon Wood 		}
148422bcefdcSSimon Wood 	}
148522bcefdcSSimon Wood #endif
1486b211a638SMichal Malý 	hid_hw_stop(hid);
1487b211a638SMichal Malý 	drv_data->device_props = NULL;
148822bcefdcSSimon Wood 
14893b6b17b7SMichal Malý 	kfree(entry);
1490e7c23449SMichal Malý out:
149130bb75d7SMichal Malý 	dbg_hid("Device successfully unregistered\n");
149230bb75d7SMichal Malý 	return 0;
149330bb75d7SMichal Malý }
1494