xref: /linux/drivers/hid/hid-lg4ff.c (revision 570f7e331f5febb30f1384817463c7e42b65ca7d)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Force feedback support for Logitech Gaming Wheels
4  *
5  *  Including G27, G25, DFP, DFGT, FFEX, Momo, Momo2 &
6  *  Speed Force Wireless (WiiWheel)
7  *
8  *  Copyright (c) 2010 Simon Wood <simon@mungewell.org>
9  */
10 
11 /*
12  */
13 
14 
15 #include <linux/input.h>
16 #include <linux/usb.h>
17 #include <linux/hid.h>
18 
19 #include "usbhid/usbhid.h"
20 #include "hid-lg.h"
21 #include "hid-lg4ff.h"
22 #include "hid-ids.h"
23 
24 #define LG4FF_MMODE_IS_MULTIMODE 0
25 #define LG4FF_MMODE_SWITCHED 1
26 #define LG4FF_MMODE_NOT_MULTIMODE 2
27 
28 #define LG4FF_MODE_NATIVE_IDX 0
29 #define LG4FF_MODE_DFEX_IDX 1
30 #define LG4FF_MODE_DFP_IDX 2
31 #define LG4FF_MODE_G25_IDX 3
32 #define LG4FF_MODE_DFGT_IDX 4
33 #define LG4FF_MODE_G27_IDX 5
34 #define LG4FF_MODE_G29_IDX 6
35 #define LG4FF_MODE_MAX_IDX 7
36 
37 #define LG4FF_MODE_NATIVE BIT(LG4FF_MODE_NATIVE_IDX)
38 #define LG4FF_MODE_DFEX BIT(LG4FF_MODE_DFEX_IDX)
39 #define LG4FF_MODE_DFP BIT(LG4FF_MODE_DFP_IDX)
40 #define LG4FF_MODE_G25 BIT(LG4FF_MODE_G25_IDX)
41 #define LG4FF_MODE_DFGT BIT(LG4FF_MODE_DFGT_IDX)
42 #define LG4FF_MODE_G27 BIT(LG4FF_MODE_G27_IDX)
43 #define LG4FF_MODE_G29 BIT(LG4FF_MODE_G29_IDX)
44 
45 #define LG4FF_DFEX_TAG "DF-EX"
46 #define LG4FF_DFEX_NAME "Driving Force / Formula EX"
47 #define LG4FF_DFP_TAG "DFP"
48 #define LG4FF_DFP_NAME "Driving Force Pro"
49 #define LG4FF_G25_TAG "G25"
50 #define LG4FF_G25_NAME "G25 Racing Wheel"
51 #define LG4FF_G27_TAG "G27"
52 #define LG4FF_G27_NAME "G27 Racing Wheel"
53 #define LG4FF_G29_TAG "G29"
54 #define LG4FF_G29_NAME "G29 Racing Wheel"
55 #define LG4FF_DFGT_TAG "DFGT"
56 #define LG4FF_DFGT_NAME "Driving Force GT"
57 
58 #define LG4FF_FFEX_REV_MAJ 0x21
59 #define LG4FF_FFEX_REV_MIN 0x00
60 
61 static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range);
62 static void lg4ff_set_range_g25(struct hid_device *hid, u16 range);
63 
64 struct lg4ff_wheel_data {
65 	const u32 product_id;
66 	u16 combine;
67 	u16 range;
68 	const u16 min_range;
69 	const u16 max_range;
70 #ifdef CONFIG_LEDS_CLASS
71 	u8  led_state;
72 	struct led_classdev *led[5];
73 #endif
74 	const u32 alternate_modes;
75 	const char * const real_tag;
76 	const char * const real_name;
77 	const u16 real_product_id;
78 
79 	void (*set_range)(struct hid_device *hid, u16 range);
80 };
81 
82 struct lg4ff_device_entry {
83 	spinlock_t report_lock; /* Protect output HID report */
84 	struct hid_report *report;
85 	struct lg4ff_wheel_data wdata;
86 };
87 
88 static const signed short lg4ff_wheel_effects[] = {
89 	FF_CONSTANT,
90 	FF_AUTOCENTER,
91 	-1
92 };
93 
94 static const signed short no_wheel_effects[] = {
95 	-1
96 };
97 
98 struct lg4ff_wheel {
99 	const u32 product_id;
100 	const signed short *ff_effects;
101 	const u16 min_range;
102 	const u16 max_range;
103 	void (*set_range)(struct hid_device *hid, u16 range);
104 };
105 
106 struct lg4ff_compat_mode_switch {
107 	const u8 cmd_count;	/* Number of commands to send */
108 	const u8 cmd[];
109 };
110 
111 struct lg4ff_wheel_ident_info {
112 	const u32 modes;
113 	const u16 mask;
114 	const u16 result;
115 	const u16 real_product_id;
116 };
117 
118 struct lg4ff_multimode_wheel {
119 	const u16 product_id;
120 	const u32 alternate_modes;
121 	const char *real_tag;
122 	const char *real_name;
123 };
124 
125 struct lg4ff_alternate_mode {
126 	const u16 product_id;
127 	const char *tag;
128 	const char *name;
129 };
130 
131 static const struct lg4ff_wheel lg4ff_devices[] = {
132 	{USB_DEVICE_ID_LOGITECH_WINGMAN_FG,  no_wheel_effects,    40, 180, NULL},
133 	{USB_DEVICE_ID_LOGITECH_WINGMAN_FFG, lg4ff_wheel_effects, 40, 180, NULL},
134 	{USB_DEVICE_ID_LOGITECH_WHEEL,       lg4ff_wheel_effects, 40, 270, NULL},
135 	{USB_DEVICE_ID_LOGITECH_MOMO_WHEEL,  lg4ff_wheel_effects, 40, 270, NULL},
136 	{USB_DEVICE_ID_LOGITECH_DFP_WHEEL,   lg4ff_wheel_effects, 40, 900, lg4ff_set_range_dfp},
137 	{USB_DEVICE_ID_LOGITECH_G25_WHEEL,   lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
138 	{USB_DEVICE_ID_LOGITECH_DFGT_WHEEL,  lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
139 	{USB_DEVICE_ID_LOGITECH_G27_WHEEL,   lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
140 	{USB_DEVICE_ID_LOGITECH_G29_WHEEL,   lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
141 	{USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL},
142 	{USB_DEVICE_ID_LOGITECH_WII_WHEEL,   lg4ff_wheel_effects, 40, 270, NULL}
143 };
144 
145 static const struct lg4ff_multimode_wheel lg4ff_multimode_wheels[] = {
146 	{USB_DEVICE_ID_LOGITECH_DFP_WHEEL,
147 	 LG4FF_MODE_NATIVE | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
148 	 LG4FF_DFP_TAG, LG4FF_DFP_NAME},
149 	{USB_DEVICE_ID_LOGITECH_G25_WHEEL,
150 	 LG4FF_MODE_NATIVE | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
151 	 LG4FF_G25_TAG, LG4FF_G25_NAME},
152 	{USB_DEVICE_ID_LOGITECH_DFGT_WHEEL,
153 	 LG4FF_MODE_NATIVE | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
154 	 LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
155 	{USB_DEVICE_ID_LOGITECH_G27_WHEEL,
156 	 LG4FF_MODE_NATIVE | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
157 	 LG4FF_G27_TAG, LG4FF_G27_NAME},
158 	{USB_DEVICE_ID_LOGITECH_G29_WHEEL,
159 	 LG4FF_MODE_NATIVE | LG4FF_MODE_G29 | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
160 	 LG4FF_G29_TAG, LG4FF_G29_NAME},
161 };
162 
163 static const struct lg4ff_alternate_mode lg4ff_alternate_modes[] = {
164 	[LG4FF_MODE_NATIVE_IDX] = {0, "native", ""},
165 	[LG4FF_MODE_DFEX_IDX] = {USB_DEVICE_ID_LOGITECH_WHEEL, LG4FF_DFEX_TAG, LG4FF_DFEX_NAME},
166 	[LG4FF_MODE_DFP_IDX] = {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, LG4FF_DFP_TAG, LG4FF_DFP_NAME},
167 	[LG4FF_MODE_G25_IDX] = {USB_DEVICE_ID_LOGITECH_G25_WHEEL, LG4FF_G25_TAG, LG4FF_G25_NAME},
168 	[LG4FF_MODE_DFGT_IDX] = {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
169 	[LG4FF_MODE_G27_IDX] = {USB_DEVICE_ID_LOGITECH_G27_WHEEL, LG4FF_G27_TAG, LG4FF_G27_NAME},
170 	[LG4FF_MODE_G29_IDX] = {USB_DEVICE_ID_LOGITECH_G29_WHEEL, LG4FF_G29_TAG, LG4FF_G29_NAME},
171 };
172 
173 /* Multimode wheel identificators */
174 static const struct lg4ff_wheel_ident_info lg4ff_dfp_ident_info = {
175 	LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
176 	0xf000,
177 	0x1000,
178 	USB_DEVICE_ID_LOGITECH_DFP_WHEEL
179 };
180 
181 static const struct lg4ff_wheel_ident_info lg4ff_g25_ident_info = {
182 	LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
183 	0xff00,
184 	0x1200,
185 	USB_DEVICE_ID_LOGITECH_G25_WHEEL
186 };
187 
188 static const struct lg4ff_wheel_ident_info lg4ff_g27_ident_info = {
189 	LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
190 	0xfff0,
191 	0x1230,
192 	USB_DEVICE_ID_LOGITECH_G27_WHEEL
193 };
194 
195 static const struct lg4ff_wheel_ident_info lg4ff_dfgt_ident_info = {
196 	LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
197 	0xff00,
198 	0x1300,
199 	USB_DEVICE_ID_LOGITECH_DFGT_WHEEL
200 };
201 
202 static const struct lg4ff_wheel_ident_info lg4ff_g29_ident_info = {
203 	LG4FF_MODE_G29 | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
204 	0xfff8,
205 	0x1350,
206 	USB_DEVICE_ID_LOGITECH_G29_WHEEL
207 };
208 
209 static const struct lg4ff_wheel_ident_info lg4ff_g29_ident_info2 = {
210 	LG4FF_MODE_G29 | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
211 	0xff00,
212 	0x8900,
213 	USB_DEVICE_ID_LOGITECH_G29_WHEEL
214 };
215 
216 /* Multimode wheel identification checklists */
217 static const struct lg4ff_wheel_ident_info *lg4ff_main_checklist[] = {
218 	&lg4ff_g29_ident_info,
219 	&lg4ff_g29_ident_info2,
220 	&lg4ff_dfgt_ident_info,
221 	&lg4ff_g27_ident_info,
222 	&lg4ff_g25_ident_info,
223 	&lg4ff_dfp_ident_info
224 };
225 
226 /* Compatibility mode switching commands */
227 /* EXT_CMD9 - Understood by G27 and DFGT */
228 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfex = {
229 	2,
230 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* Revert mode upon USB reset */
231 	 0xf8, 0x09, 0x00, 0x01, 0x00, 0x00, 0x00}	/* Switch mode to DF-EX with detach */
232 };
233 
234 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfp = {
235 	2,
236 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* Revert mode upon USB reset */
237 	 0xf8, 0x09, 0x01, 0x01, 0x00, 0x00, 0x00}	/* Switch mode to DFP with detach */
238 };
239 
240 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g25 = {
241 	2,
242 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* Revert mode upon USB reset */
243 	 0xf8, 0x09, 0x02, 0x01, 0x00, 0x00, 0x00}	/* Switch mode to G25 with detach */
244 };
245 
246 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfgt = {
247 	2,
248 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* Revert mode upon USB reset */
249 	 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00}	/* Switch mode to DFGT with detach */
250 };
251 
252 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g27 = {
253 	2,
254 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* Revert mode upon USB reset */
255 	 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00}	/* Switch mode to G27 with detach */
256 };
257 
258 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g29 = {
259 	2,
260 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* Revert mode upon USB reset */
261 	 0xf8, 0x09, 0x05, 0x01, 0x01, 0x00, 0x00}	/* Switch mode to G29 with detach */
262 };
263 
264 /* EXT_CMD1 - Understood by DFP, G25, G27 and DFGT */
265 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext01_dfp = {
266 	1,
267 	{0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
268 };
269 
270 /* EXT_CMD16 - Understood by G25 and G27 */
271 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext16_g25 = {
272 	1,
273 	{0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
274 };
275 
276 /* Recalculates X axis value accordingly to currently selected range */
277 static s32 lg4ff_adjust_dfp_x_axis(s32 value, u16 range)
278 {
279 	u16 max_range;
280 	s32 new_value;
281 
282 	if (range == 900)
283 		return value;
284 	else if (range == 200)
285 		return value;
286 	else if (range < 200)
287 		max_range = 200;
288 	else
289 		max_range = 900;
290 
291 	new_value = 8192 + mult_frac(value - 8192, max_range, range);
292 	if (new_value < 0)
293 		return 0;
294 	else if (new_value > 16383)
295 		return 16383;
296 	else
297 		return new_value;
298 }
299 
300 int lg4ff_adjust_input_event(struct hid_device *hid, struct hid_field *field,
301 			     struct hid_usage *usage, s32 value, struct lg_drv_data *drv_data)
302 {
303 	struct lg4ff_device_entry *entry = drv_data->device_props;
304 	s32 new_value = 0;
305 
306 	if (!entry) {
307 		hid_err(hid, "Device properties not found");
308 		return 0;
309 	}
310 
311 	switch (entry->wdata.product_id) {
312 	case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
313 		switch (usage->code) {
314 		case ABS_X:
315 			new_value = lg4ff_adjust_dfp_x_axis(value, entry->wdata.range);
316 			input_event(field->hidinput->input, usage->type, usage->code, new_value);
317 			return 1;
318 		default:
319 			return 0;
320 		}
321 	default:
322 		return 0;
323 	}
324 }
325 
326 int lg4ff_raw_event(struct hid_device *hdev, struct hid_report *report,
327 		u8 *rd, int size, struct lg_drv_data *drv_data)
328 {
329 	int offset;
330 	struct lg4ff_device_entry *entry = drv_data->device_props;
331 
332 	if (!entry)
333 		return 0;
334 
335 	/* adjust HID report present combined pedals data */
336 	if (entry->wdata.combine) {
337 		switch (entry->wdata.product_id) {
338 		case USB_DEVICE_ID_LOGITECH_WHEEL:
339 			if (size < 7)
340 				return 0;
341 			rd[5] = rd[3];
342 			rd[6] = 0x7F;
343 			return 1;
344 		case USB_DEVICE_ID_LOGITECH_WINGMAN_FG:
345 		case USB_DEVICE_ID_LOGITECH_WINGMAN_FFG:
346 		case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL:
347 		case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2:
348 			if (size < 6)
349 				return 0;
350 			rd[4] = rd[3];
351 			rd[5] = 0x7F;
352 			return 1;
353 		case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
354 			if (size < 7)
355 				return 0;
356 			rd[5] = rd[4];
357 			rd[6] = 0x7F;
358 			return 1;
359 		case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
360 		case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
361 			offset = 5;
362 			break;
363 		case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
364 		case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
365 			offset = 6;
366 			break;
367 		case USB_DEVICE_ID_LOGITECH_WII_WHEEL:
368 			offset = 3;
369 			break;
370 		default:
371 			return 0;
372 		}
373 
374 		/* Compute a combined axis when wheel does not supply it */
375 		if (size <= offset + 1)
376 			return 0;
377 		rd[offset] = (0xFF + rd[offset] - rd[offset+1]) >> 1;
378 		rd[offset+1] = 0x7F;
379 		return 1;
380 	}
381 
382 	return 0;
383 }
384 
385 static void lg4ff_init_wheel_data(struct lg4ff_wheel_data * const wdata, const struct lg4ff_wheel *wheel,
386 				  const struct lg4ff_multimode_wheel *mmode_wheel,
387 				  const u16 real_product_id)
388 {
389 	u32 alternate_modes = 0;
390 	const char *real_tag = NULL;
391 	const char *real_name = NULL;
392 
393 	if (mmode_wheel) {
394 		alternate_modes = mmode_wheel->alternate_modes;
395 		real_tag = mmode_wheel->real_tag;
396 		real_name = mmode_wheel->real_name;
397 	}
398 
399 	{
400 		struct lg4ff_wheel_data t_wdata =  { .product_id = wheel->product_id,
401 						     .real_product_id = real_product_id,
402 						     .combine = 0,
403 						     .min_range = wheel->min_range,
404 						     .max_range = wheel->max_range,
405 						     .set_range = wheel->set_range,
406 						     .alternate_modes = alternate_modes,
407 						     .real_tag = real_tag,
408 						     .real_name = real_name };
409 
410 		memcpy(wdata, &t_wdata, sizeof(t_wdata));
411 	}
412 }
413 
414 static int lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
415 {
416 	struct hid_device *hid = input_get_drvdata(dev);
417 	struct lg4ff_device_entry *entry;
418 	struct lg_drv_data *drv_data;
419 	unsigned long flags;
420 	s32 *value;
421 	int x;
422 
423 	drv_data = hid_get_drvdata(hid);
424 	if (!drv_data) {
425 		hid_err(hid, "Private driver data not found!\n");
426 		return -EINVAL;
427 	}
428 
429 	entry = drv_data->device_props;
430 	if (!entry) {
431 		hid_err(hid, "Device properties not found!\n");
432 		return -EINVAL;
433 	}
434 	value = entry->report->field[0]->value;
435 
436 #define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0)
437 
438 	switch (effect->type) {
439 	case FF_CONSTANT:
440 		x = effect->u.ramp.start_level + 0x80;	/* 0x80 is no force */
441 		CLAMP(x);
442 
443 		spin_lock_irqsave(&entry->report_lock, flags);
444 		if (x == 0x80) {
445 			/* De-activate force in slot-1*/
446 			value[0] = 0x13;
447 			value[1] = 0x00;
448 			value[2] = 0x00;
449 			value[3] = 0x00;
450 			value[4] = 0x00;
451 			value[5] = 0x00;
452 			value[6] = 0x00;
453 
454 			hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
455 			spin_unlock_irqrestore(&entry->report_lock, flags);
456 			return 0;
457 		}
458 
459 		value[0] = 0x11;	/* Slot 1 */
460 		value[1] = 0x08;
461 		value[2] = x;
462 		value[3] = 0x80;
463 		value[4] = 0x00;
464 		value[5] = 0x00;
465 		value[6] = 0x00;
466 
467 		hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
468 		spin_unlock_irqrestore(&entry->report_lock, flags);
469 		break;
470 	}
471 	return 0;
472 }
473 
474 /* Sends default autocentering command compatible with
475  * all wheels except Formula Force EX */
476 static void lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude)
477 {
478 	struct hid_device *hid = input_get_drvdata(dev);
479 	s32 *value;
480 	u32 expand_a, expand_b;
481 	struct lg4ff_device_entry *entry;
482 	struct lg_drv_data *drv_data;
483 	unsigned long flags;
484 
485 	drv_data = hid_get_drvdata(hid);
486 	if (!drv_data) {
487 		hid_err(hid, "Private driver data not found!\n");
488 		return;
489 	}
490 
491 	entry = drv_data->device_props;
492 	if (!entry) {
493 		hid_err(hid, "Device properties not found!\n");
494 		return;
495 	}
496 	value = entry->report->field[0]->value;
497 
498 	/* De-activate Auto-Center */
499 	spin_lock_irqsave(&entry->report_lock, flags);
500 	if (magnitude == 0) {
501 		value[0] = 0xf5;
502 		value[1] = 0x00;
503 		value[2] = 0x00;
504 		value[3] = 0x00;
505 		value[4] = 0x00;
506 		value[5] = 0x00;
507 		value[6] = 0x00;
508 
509 		hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
510 		spin_unlock_irqrestore(&entry->report_lock, flags);
511 		return;
512 	}
513 
514 	if (magnitude <= 0xaaaa) {
515 		expand_a = 0x0c * magnitude;
516 		expand_b = 0x80 * magnitude;
517 	} else {
518 		expand_a = (0x0c * 0xaaaa) + 0x06 * (magnitude - 0xaaaa);
519 		expand_b = (0x80 * 0xaaaa) + 0xff * (magnitude - 0xaaaa);
520 	}
521 
522 	/* Adjust for non-MOMO wheels */
523 	switch (entry->wdata.product_id) {
524 	case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL:
525 	case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2:
526 		break;
527 	default:
528 		expand_a = expand_a >> 1;
529 		break;
530 	}
531 
532 	value[0] = 0xfe;
533 	value[1] = 0x0d;
534 	value[2] = expand_a / 0xaaaa;
535 	value[3] = expand_a / 0xaaaa;
536 	value[4] = expand_b / 0xaaaa;
537 	value[5] = 0x00;
538 	value[6] = 0x00;
539 
540 	hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
541 
542 	/* Activate Auto-Center */
543 	value[0] = 0x14;
544 	value[1] = 0x00;
545 	value[2] = 0x00;
546 	value[3] = 0x00;
547 	value[4] = 0x00;
548 	value[5] = 0x00;
549 	value[6] = 0x00;
550 
551 	hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
552 	spin_unlock_irqrestore(&entry->report_lock, flags);
553 }
554 
555 /* Sends autocentering command compatible with Formula Force EX */
556 static void lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude)
557 {
558 	struct hid_device *hid = input_get_drvdata(dev);
559 	struct lg4ff_device_entry *entry;
560 	struct lg_drv_data *drv_data;
561 	unsigned long flags;
562 	s32 *value;
563 	magnitude = magnitude * 90 / 65535;
564 
565 	drv_data = hid_get_drvdata(hid);
566 	if (!drv_data) {
567 		hid_err(hid, "Private driver data not found!\n");
568 		return;
569 	}
570 
571 	entry = drv_data->device_props;
572 	if (!entry) {
573 		hid_err(hid, "Device properties not found!\n");
574 		return;
575 	}
576 	value = entry->report->field[0]->value;
577 
578 	spin_lock_irqsave(&entry->report_lock, flags);
579 	value[0] = 0xfe;
580 	value[1] = 0x03;
581 	value[2] = magnitude >> 14;
582 	value[3] = magnitude >> 14;
583 	value[4] = magnitude;
584 	value[5] = 0x00;
585 	value[6] = 0x00;
586 
587 	hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
588 	spin_unlock_irqrestore(&entry->report_lock, flags);
589 }
590 
591 /* Sends command to set range compatible with G25/G27/Driving Force GT */
592 static void lg4ff_set_range_g25(struct hid_device *hid, u16 range)
593 {
594 	struct lg4ff_device_entry *entry;
595 	struct lg_drv_data *drv_data;
596 	unsigned long flags;
597 	s32 *value;
598 
599 	drv_data = hid_get_drvdata(hid);
600 	if (!drv_data) {
601 		hid_err(hid, "Private driver data not found!\n");
602 		return;
603 	}
604 
605 	entry = drv_data->device_props;
606 	if (!entry) {
607 		hid_err(hid, "Device properties not found!\n");
608 		return;
609 	}
610 	value = entry->report->field[0]->value;
611 	dbg_hid("G25/G27/DFGT: setting range to %u\n", range);
612 
613 	spin_lock_irqsave(&entry->report_lock, flags);
614 	value[0] = 0xf8;
615 	value[1] = 0x81;
616 	value[2] = range & 0x00ff;
617 	value[3] = (range & 0xff00) >> 8;
618 	value[4] = 0x00;
619 	value[5] = 0x00;
620 	value[6] = 0x00;
621 
622 	hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
623 	spin_unlock_irqrestore(&entry->report_lock, flags);
624 }
625 
626 /* Sends commands to set range compatible with Driving Force Pro wheel */
627 static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range)
628 {
629 	struct lg4ff_device_entry *entry;
630 	struct lg_drv_data *drv_data;
631 	unsigned long flags;
632 	int start_left, start_right, full_range;
633 	s32 *value;
634 
635 	drv_data = hid_get_drvdata(hid);
636 	if (!drv_data) {
637 		hid_err(hid, "Private driver data not found!\n");
638 		return;
639 	}
640 
641 	entry = drv_data->device_props;
642 	if (!entry) {
643 		hid_err(hid, "Device properties not found!\n");
644 		return;
645 	}
646 	value = entry->report->field[0]->value;
647 	dbg_hid("Driving Force Pro: setting range to %u\n", range);
648 
649 	/* Prepare "coarse" limit command */
650 	spin_lock_irqsave(&entry->report_lock, flags);
651 	value[0] = 0xf8;
652 	value[1] = 0x00;	/* Set later */
653 	value[2] = 0x00;
654 	value[3] = 0x00;
655 	value[4] = 0x00;
656 	value[5] = 0x00;
657 	value[6] = 0x00;
658 
659 	if (range > 200) {
660 		value[1] = 0x03;
661 		full_range = 900;
662 	} else {
663 		value[1] = 0x02;
664 		full_range = 200;
665 	}
666 	hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
667 
668 	/* Prepare "fine" limit command */
669 	value[0] = 0x81;
670 	value[1] = 0x0b;
671 	value[2] = 0x00;
672 	value[3] = 0x00;
673 	value[4] = 0x00;
674 	value[5] = 0x00;
675 	value[6] = 0x00;
676 
677 	if (range == 200 || range == 900) {	/* Do not apply any fine limit */
678 		hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
679 		spin_unlock_irqrestore(&entry->report_lock, flags);
680 		return;
681 	}
682 
683 	/* Construct fine limit command */
684 	start_left = (((full_range - range + 1) * 2047) / full_range);
685 	start_right = 0xfff - start_left;
686 
687 	value[2] = start_left >> 4;
688 	value[3] = start_right >> 4;
689 	value[4] = 0xff;
690 	value[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
691 	value[6] = 0xff;
692 
693 	hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
694 	spin_unlock_irqrestore(&entry->report_lock, flags);
695 }
696 
697 static const struct lg4ff_compat_mode_switch *lg4ff_get_mode_switch_command(const u16 real_product_id, const u16 target_product_id)
698 {
699 	switch (real_product_id) {
700 	case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
701 		switch (target_product_id) {
702 		case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
703 			return &lg4ff_mode_switch_ext01_dfp;
704 		/* DFP can only be switched to its native mode */
705 		default:
706 			return NULL;
707 		}
708 		break;
709 	case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
710 		switch (target_product_id) {
711 		case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
712 			return &lg4ff_mode_switch_ext01_dfp;
713 		case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
714 			return &lg4ff_mode_switch_ext16_g25;
715 		/* G25 can only be switched to DFP mode or its native mode */
716 		default:
717 			return NULL;
718 		}
719 		break;
720 	case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
721 		switch (target_product_id) {
722 		case USB_DEVICE_ID_LOGITECH_WHEEL:
723 			return &lg4ff_mode_switch_ext09_dfex;
724 		case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
725 			return &lg4ff_mode_switch_ext09_dfp;
726 		case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
727 			return &lg4ff_mode_switch_ext09_g25;
728 		case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
729 			return &lg4ff_mode_switch_ext09_g27;
730 		/* G27 can only be switched to DF-EX, DFP, G25 or its native mode */
731 		default:
732 			return NULL;
733 		}
734 		break;
735 	case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
736 		switch (target_product_id) {
737 		case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
738 			return &lg4ff_mode_switch_ext09_dfp;
739 		case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
740 			return &lg4ff_mode_switch_ext09_dfgt;
741 		case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
742 			return &lg4ff_mode_switch_ext09_g25;
743 		case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
744 			return &lg4ff_mode_switch_ext09_g27;
745 		case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
746 			return &lg4ff_mode_switch_ext09_g29;
747 		/* G29 can only be switched to DF-EX, DFP, DFGT, G25, G27 or its native mode */
748 		default:
749 			return NULL;
750 		}
751 		break;
752 	case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
753 		switch (target_product_id) {
754 		case USB_DEVICE_ID_LOGITECH_WHEEL:
755 			return &lg4ff_mode_switch_ext09_dfex;
756 		case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
757 			return &lg4ff_mode_switch_ext09_dfp;
758 		case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
759 			return &lg4ff_mode_switch_ext09_dfgt;
760 		/* DFGT can only be switched to DF-EX, DFP or its native mode */
761 		default:
762 			return NULL;
763 		}
764 		break;
765 	/* No other wheels have multiple modes */
766 	default:
767 		return NULL;
768 	}
769 }
770 
771 static int lg4ff_switch_compatibility_mode(struct hid_device *hid, const struct lg4ff_compat_mode_switch *s)
772 {
773 	struct lg4ff_device_entry *entry;
774 	struct lg_drv_data *drv_data;
775 	unsigned long flags;
776 	s32 *value;
777 	u8 i;
778 
779 	drv_data = hid_get_drvdata(hid);
780 	if (!drv_data) {
781 		hid_err(hid, "Private driver data not found!\n");
782 		return -EINVAL;
783 	}
784 
785 	entry = drv_data->device_props;
786 	if (!entry) {
787 		hid_err(hid, "Device properties not found!\n");
788 		return -EINVAL;
789 	}
790 	value = entry->report->field[0]->value;
791 
792 	spin_lock_irqsave(&entry->report_lock, flags);
793 	for (i = 0; i < s->cmd_count; i++) {
794 		u8 j;
795 
796 		for (j = 0; j < 7; j++)
797 			value[j] = s->cmd[j + (7*i)];
798 
799 		hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
800 	}
801 	spin_unlock_irqrestore(&entry->report_lock, flags);
802 	hid_hw_wait(hid);
803 	return 0;
804 }
805 
806 static ssize_t lg4ff_alternate_modes_show(struct device *dev, struct device_attribute *attr, char *buf)
807 {
808 	struct hid_device *hid = to_hid_device(dev);
809 	struct lg4ff_device_entry *entry;
810 	struct lg_drv_data *drv_data;
811 	ssize_t count = 0;
812 	int i;
813 
814 	drv_data = hid_get_drvdata(hid);
815 	if (!drv_data) {
816 		hid_err(hid, "Private driver data not found!\n");
817 		return 0;
818 	}
819 
820 	entry = drv_data->device_props;
821 	if (!entry) {
822 		hid_err(hid, "Device properties not found!\n");
823 		return 0;
824 	}
825 
826 	if (!entry->wdata.real_name) {
827 		hid_err(hid, "NULL pointer to string\n");
828 		return 0;
829 	}
830 
831 	for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
832 		if (entry->wdata.alternate_modes & BIT(i)) {
833 			/* Print tag and full name */
834 			count += sysfs_emit_at(buf, count, "%s: %s",
835 					   lg4ff_alternate_modes[i].tag,
836 					   !lg4ff_alternate_modes[i].product_id ? entry->wdata.real_name : lg4ff_alternate_modes[i].name);
837 			if (count >= PAGE_SIZE - 1)
838 				return count;
839 
840 			/* Mark the currently active mode with an asterisk */
841 			if (lg4ff_alternate_modes[i].product_id == entry->wdata.product_id ||
842 			    (lg4ff_alternate_modes[i].product_id == 0 && entry->wdata.product_id == entry->wdata.real_product_id))
843 				count += sysfs_emit_at(buf, count, " *\n");
844 			else
845 				count += sysfs_emit_at(buf, count, "\n");
846 
847 			if (count >= PAGE_SIZE - 1)
848 				return count;
849 		}
850 	}
851 
852 	return count;
853 }
854 
855 static ssize_t lg4ff_alternate_modes_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
856 {
857 	struct hid_device *hid = to_hid_device(dev);
858 	struct lg4ff_device_entry *entry;
859 	struct lg_drv_data *drv_data;
860 	const struct lg4ff_compat_mode_switch *s;
861 	u16 target_product_id = 0;
862 	int i, ret;
863 	char *lbuf;
864 
865 	drv_data = hid_get_drvdata(hid);
866 	if (!drv_data) {
867 		hid_err(hid, "Private driver data not found!\n");
868 		return -EINVAL;
869 	}
870 
871 	entry = drv_data->device_props;
872 	if (!entry) {
873 		hid_err(hid, "Device properties not found!\n");
874 		return -EINVAL;
875 	}
876 
877 	/* Allow \n at the end of the input parameter */
878 	lbuf = kasprintf(GFP_KERNEL, "%s", buf);
879 	if (!lbuf)
880 		return -ENOMEM;
881 
882 	i = strlen(lbuf);
883 
884 	if (i == 0) {
885 		kfree(lbuf);
886 		return -EINVAL;
887 	}
888 
889 	if (lbuf[i-1] == '\n') {
890 		if (i == 1) {
891 			kfree(lbuf);
892 			return -EINVAL;
893 		}
894 		lbuf[i-1] = '\0';
895 	}
896 
897 	for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
898 		const u16 mode_product_id = lg4ff_alternate_modes[i].product_id;
899 		const char *tag = lg4ff_alternate_modes[i].tag;
900 
901 		if (entry->wdata.alternate_modes & BIT(i)) {
902 			if (!strcmp(tag, lbuf)) {
903 				if (!mode_product_id)
904 					target_product_id = entry->wdata.real_product_id;
905 				else
906 					target_product_id = mode_product_id;
907 				break;
908 			}
909 		}
910 	}
911 
912 	if (i == LG4FF_MODE_MAX_IDX) {
913 		hid_info(hid, "Requested mode \"%s\" is not supported by the device\n", lbuf);
914 		kfree(lbuf);
915 		return -EINVAL;
916 	}
917 	kfree(lbuf); /* Not needed anymore */
918 
919 	if (target_product_id == entry->wdata.product_id) /* Nothing to do */
920 		return count;
921 
922 	/* Automatic switching has to be disabled for the switch to DF-EX mode to work correctly */
923 	if (target_product_id == USB_DEVICE_ID_LOGITECH_WHEEL && !lg4ff_no_autoswitch) {
924 		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",
925 			 entry->wdata.real_name);
926 		return -EINVAL;
927 	}
928 
929 	/* Take care of hardware limitations */
930 	if ((entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_DFP_WHEEL || entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_G25_WHEEL) &&
931 	    entry->wdata.product_id > target_product_id) {
932 		hid_info(hid, "\"%s\" cannot be switched back into \"%s\" mode\n", entry->wdata.real_name, lg4ff_alternate_modes[i].name);
933 		return -EINVAL;
934 	}
935 
936 	s = lg4ff_get_mode_switch_command(entry->wdata.real_product_id, target_product_id);
937 	if (!s) {
938 		hid_err(hid, "Invalid target product ID %X\n", target_product_id);
939 		return -EINVAL;
940 	}
941 
942 	ret = lg4ff_switch_compatibility_mode(hid, s);
943 	return (ret == 0 ? count : ret);
944 }
945 static DEVICE_ATTR(alternate_modes, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_alternate_modes_show, lg4ff_alternate_modes_store);
946 
947 static ssize_t lg4ff_combine_show(struct device *dev, struct device_attribute *attr,
948 				char *buf)
949 {
950 	struct hid_device *hid = to_hid_device(dev);
951 	struct lg4ff_device_entry *entry;
952 	struct lg_drv_data *drv_data;
953 	size_t count;
954 
955 	drv_data = hid_get_drvdata(hid);
956 	if (!drv_data) {
957 		hid_err(hid, "Private driver data not found!\n");
958 		return 0;
959 	}
960 
961 	entry = drv_data->device_props;
962 	if (!entry) {
963 		hid_err(hid, "Device properties not found!\n");
964 		return 0;
965 	}
966 
967 	count = sysfs_emit(buf, "%u\n", entry->wdata.combine);
968 	return count;
969 }
970 
971 static ssize_t lg4ff_combine_store(struct device *dev, struct device_attribute *attr,
972 				 const char *buf, size_t count)
973 {
974 	struct hid_device *hid = to_hid_device(dev);
975 	struct lg4ff_device_entry *entry;
976 	struct lg_drv_data *drv_data;
977 	u16 combine = simple_strtoul(buf, NULL, 10);
978 
979 	drv_data = hid_get_drvdata(hid);
980 	if (!drv_data) {
981 		hid_err(hid, "Private driver data not found!\n");
982 		return -EINVAL;
983 	}
984 
985 	entry = drv_data->device_props;
986 	if (!entry) {
987 		hid_err(hid, "Device properties not found!\n");
988 		return -EINVAL;
989 	}
990 
991 	if (combine > 1)
992 		combine = 1;
993 
994 	entry->wdata.combine = combine;
995 	return count;
996 }
997 static DEVICE_ATTR(combine_pedals, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_combine_show, lg4ff_combine_store);
998 
999 /* Export the currently set range of the wheel */
1000 static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr,
1001 				char *buf)
1002 {
1003 	struct hid_device *hid = to_hid_device(dev);
1004 	struct lg4ff_device_entry *entry;
1005 	struct lg_drv_data *drv_data;
1006 	size_t count;
1007 
1008 	drv_data = hid_get_drvdata(hid);
1009 	if (!drv_data) {
1010 		hid_err(hid, "Private driver data not found!\n");
1011 		return 0;
1012 	}
1013 
1014 	entry = drv_data->device_props;
1015 	if (!entry) {
1016 		hid_err(hid, "Device properties not found!\n");
1017 		return 0;
1018 	}
1019 
1020 	count = sysfs_emit(buf, "%u\n", entry->wdata.range);
1021 	return count;
1022 }
1023 
1024 /* Set range to user specified value, call appropriate function
1025  * according to the type of the wheel */
1026 static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr,
1027 				 const char *buf, size_t count)
1028 {
1029 	struct hid_device *hid = to_hid_device(dev);
1030 	struct lg4ff_device_entry *entry;
1031 	struct lg_drv_data *drv_data;
1032 	u16 range = simple_strtoul(buf, NULL, 10);
1033 
1034 	drv_data = hid_get_drvdata(hid);
1035 	if (!drv_data) {
1036 		hid_err(hid, "Private driver data not found!\n");
1037 		return -EINVAL;
1038 	}
1039 
1040 	entry = drv_data->device_props;
1041 	if (!entry) {
1042 		hid_err(hid, "Device properties not found!\n");
1043 		return -EINVAL;
1044 	}
1045 
1046 	if (range == 0)
1047 		range = entry->wdata.max_range;
1048 
1049 	/* Check if the wheel supports range setting
1050 	 * and that the range is within limits for the wheel */
1051 	if (entry->wdata.set_range && range >= entry->wdata.min_range && range <= entry->wdata.max_range) {
1052 		entry->wdata.set_range(hid, range);
1053 		entry->wdata.range = range;
1054 	}
1055 
1056 	return count;
1057 }
1058 static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_range_show, lg4ff_range_store);
1059 
1060 static ssize_t lg4ff_real_id_show(struct device *dev, struct device_attribute *attr, char *buf)
1061 {
1062 	struct hid_device *hid = to_hid_device(dev);
1063 	struct lg4ff_device_entry *entry;
1064 	struct lg_drv_data *drv_data;
1065 	size_t count;
1066 
1067 	drv_data = hid_get_drvdata(hid);
1068 	if (!drv_data) {
1069 		hid_err(hid, "Private driver data not found!\n");
1070 		return 0;
1071 	}
1072 
1073 	entry = drv_data->device_props;
1074 	if (!entry) {
1075 		hid_err(hid, "Device properties not found!\n");
1076 		return 0;
1077 	}
1078 
1079 	if (!entry->wdata.real_tag || !entry->wdata.real_name) {
1080 		hid_err(hid, "NULL pointer to string\n");
1081 		return 0;
1082 	}
1083 
1084 	count = sysfs_emit(buf, "%s: %s\n", entry->wdata.real_tag, entry->wdata.real_name);
1085 	return count;
1086 }
1087 
1088 static ssize_t lg4ff_real_id_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1089 {
1090 	/* Real ID is a read-only value */
1091 	return -EPERM;
1092 }
1093 static DEVICE_ATTR(real_id, S_IRUGO, lg4ff_real_id_show, lg4ff_real_id_store);
1094 
1095 #ifdef CONFIG_LEDS_CLASS
1096 static void lg4ff_set_leds(struct hid_device *hid, u8 leds)
1097 {
1098 	struct lg_drv_data *drv_data;
1099 	struct lg4ff_device_entry *entry;
1100 	unsigned long flags;
1101 	s32 *value;
1102 
1103 	drv_data = hid_get_drvdata(hid);
1104 	if (!drv_data) {
1105 		hid_err(hid, "Private driver data not found!\n");
1106 		return;
1107 	}
1108 
1109 	entry = drv_data->device_props;
1110 	if (!entry) {
1111 		hid_err(hid, "Device properties not found!\n");
1112 		return;
1113 	}
1114 	value = entry->report->field[0]->value;
1115 
1116 	spin_lock_irqsave(&entry->report_lock, flags);
1117 	value[0] = 0xf8;
1118 	value[1] = 0x12;
1119 	value[2] = leds;
1120 	value[3] = 0x00;
1121 	value[4] = 0x00;
1122 	value[5] = 0x00;
1123 	value[6] = 0x00;
1124 	hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
1125 	spin_unlock_irqrestore(&entry->report_lock, flags);
1126 }
1127 
1128 static void lg4ff_led_set_brightness(struct led_classdev *led_cdev,
1129 			enum led_brightness value)
1130 {
1131 	struct device *dev = led_cdev->dev->parent;
1132 	struct hid_device *hid = to_hid_device(dev);
1133 	struct lg_drv_data *drv_data = hid_get_drvdata(hid);
1134 	struct lg4ff_device_entry *entry;
1135 	int i, state = 0;
1136 
1137 	if (!drv_data) {
1138 		hid_err(hid, "Device data not found.");
1139 		return;
1140 	}
1141 
1142 	entry = drv_data->device_props;
1143 
1144 	if (!entry) {
1145 		hid_err(hid, "Device properties not found.");
1146 		return;
1147 	}
1148 
1149 	for (i = 0; i < 5; i++) {
1150 		if (led_cdev != entry->wdata.led[i])
1151 			continue;
1152 		state = (entry->wdata.led_state >> i) & 1;
1153 		if (value == LED_OFF && state) {
1154 			entry->wdata.led_state &= ~(1 << i);
1155 			lg4ff_set_leds(hid, entry->wdata.led_state);
1156 		} else if (value != LED_OFF && !state) {
1157 			entry->wdata.led_state |= 1 << i;
1158 			lg4ff_set_leds(hid, entry->wdata.led_state);
1159 		}
1160 		break;
1161 	}
1162 }
1163 
1164 static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev)
1165 {
1166 	struct device *dev = led_cdev->dev->parent;
1167 	struct hid_device *hid = to_hid_device(dev);
1168 	struct lg_drv_data *drv_data = hid_get_drvdata(hid);
1169 	struct lg4ff_device_entry *entry;
1170 	int i, value = 0;
1171 
1172 	if (!drv_data) {
1173 		hid_err(hid, "Device data not found.");
1174 		return LED_OFF;
1175 	}
1176 
1177 	entry = drv_data->device_props;
1178 
1179 	if (!entry) {
1180 		hid_err(hid, "Device properties not found.");
1181 		return LED_OFF;
1182 	}
1183 
1184 	for (i = 0; i < 5; i++)
1185 		if (led_cdev == entry->wdata.led[i]) {
1186 			value = (entry->wdata.led_state >> i) & 1;
1187 			break;
1188 		}
1189 
1190 	return value ? LED_FULL : LED_OFF;
1191 }
1192 #endif
1193 
1194 static u16 lg4ff_identify_multimode_wheel(struct hid_device *hid, const u16 reported_product_id, const u16 bcdDevice)
1195 {
1196 	u32 current_mode;
1197 	int i;
1198 
1199 	/* identify current mode from USB PID */
1200 	for (i = 1; i < ARRAY_SIZE(lg4ff_alternate_modes); i++) {
1201 		dbg_hid("Testing whether PID is %X\n", lg4ff_alternate_modes[i].product_id);
1202 		if (reported_product_id == lg4ff_alternate_modes[i].product_id)
1203 			break;
1204 	}
1205 
1206 	if (i == ARRAY_SIZE(lg4ff_alternate_modes))
1207 		return 0;
1208 
1209 	current_mode = BIT(i);
1210 
1211 	for (i = 0; i < ARRAY_SIZE(lg4ff_main_checklist); i++) {
1212 		const u16 mask = lg4ff_main_checklist[i]->mask;
1213 		const u16 result = lg4ff_main_checklist[i]->result;
1214 		const u16 real_product_id = lg4ff_main_checklist[i]->real_product_id;
1215 
1216 		if ((current_mode & lg4ff_main_checklist[i]->modes) && \
1217 				(bcdDevice & mask) == result) {
1218 			dbg_hid("Found wheel with real PID %X whose reported PID is %X\n", real_product_id, reported_product_id);
1219 			return real_product_id;
1220 		}
1221 	}
1222 
1223 	/* No match found. This is either Driving Force or an unknown
1224 	 * wheel model, do not touch it */
1225 	dbg_hid("Wheel with bcdDevice %X was not recognized as multimode wheel, leaving in its current mode\n", bcdDevice);
1226 	return 0;
1227 }
1228 
1229 static int lg4ff_handle_multimode_wheel(struct hid_device *hid, u16 *real_product_id, const u16 bcdDevice)
1230 {
1231 	const u16 reported_product_id = hid->product;
1232 	int ret;
1233 
1234 	*real_product_id = lg4ff_identify_multimode_wheel(hid, reported_product_id, bcdDevice);
1235 	/* Probed wheel is not a multimode wheel */
1236 	if (!*real_product_id) {
1237 		*real_product_id = reported_product_id;
1238 		dbg_hid("Wheel is not a multimode wheel\n");
1239 		return LG4FF_MMODE_NOT_MULTIMODE;
1240 	}
1241 
1242 	/* Switch from "Driving Force" mode to native mode automatically.
1243 	 * Otherwise keep the wheel in its current mode */
1244 	if (reported_product_id == USB_DEVICE_ID_LOGITECH_WHEEL &&
1245 	    reported_product_id != *real_product_id &&
1246 	    !lg4ff_no_autoswitch) {
1247 		const struct lg4ff_compat_mode_switch *s = lg4ff_get_mode_switch_command(*real_product_id, *real_product_id);
1248 
1249 		if (!s) {
1250 			hid_err(hid, "Invalid product id %X\n", *real_product_id);
1251 			return LG4FF_MMODE_NOT_MULTIMODE;
1252 		}
1253 
1254 		ret = lg4ff_switch_compatibility_mode(hid, s);
1255 		if (ret) {
1256 			/* Wheel could not have been switched to native mode,
1257 			 * leave it in "Driving Force" mode and continue */
1258 			hid_err(hid, "Unable to switch wheel mode, errno %d\n", ret);
1259 			return LG4FF_MMODE_IS_MULTIMODE;
1260 		}
1261 		return LG4FF_MMODE_SWITCHED;
1262 	}
1263 
1264 	return LG4FF_MMODE_IS_MULTIMODE;
1265 }
1266 
1267 
1268 int lg4ff_init(struct hid_device *hid)
1269 {
1270 	struct hid_input *hidinput;
1271 	struct input_dev *dev;
1272 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
1273 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
1274 	const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
1275 	const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
1276 	const struct lg4ff_multimode_wheel *mmode_wheel = NULL;
1277 	struct lg4ff_device_entry *entry;
1278 	struct lg_drv_data *drv_data;
1279 	int error, i, j;
1280 	int mmode_ret, mmode_idx = -1;
1281 	u16 real_product_id;
1282 
1283 	if (list_empty(&hid->inputs)) {
1284 		hid_err(hid, "no inputs found\n");
1285 		return -ENODEV;
1286 	}
1287 	hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1288 	dev = hidinput->input;
1289 
1290 	/* Check that the report looks ok */
1291 	if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
1292 		return -1;
1293 
1294 	drv_data = hid_get_drvdata(hid);
1295 	if (!drv_data) {
1296 		hid_err(hid, "Cannot add device, private driver data not allocated\n");
1297 		return -1;
1298 	}
1299 	entry = kzalloc_obj(*entry);
1300 	if (!entry)
1301 		return -ENOMEM;
1302 	spin_lock_init(&entry->report_lock);
1303 	entry->report = report;
1304 	drv_data->device_props = entry;
1305 
1306 	/* Check if a multimode wheel has been connected and
1307 	 * handle it appropriately */
1308 	mmode_ret = lg4ff_handle_multimode_wheel(hid, &real_product_id, bcdDevice);
1309 
1310 	/* Wheel has been told to switch to native mode. There is no point in going on
1311 	 * with the initialization as the wheel will do a USB reset when it switches mode
1312 	 */
1313 	if (mmode_ret == LG4FF_MMODE_SWITCHED)
1314 		return 0;
1315 	else if (mmode_ret < 0) {
1316 		hid_err(hid, "Unable to switch device mode during initialization, errno %d\n", mmode_ret);
1317 		error = mmode_ret;
1318 		goto err_init;
1319 	}
1320 
1321 	/* Check what wheel has been connected */
1322 	for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
1323 		if (hid->product == lg4ff_devices[i].product_id) {
1324 			dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
1325 			break;
1326 		}
1327 	}
1328 
1329 	if (i == ARRAY_SIZE(lg4ff_devices)) {
1330 		hid_err(hid, "This device is flagged to be handled by the lg4ff module but this module does not know how to handle it. "
1331 			     "Please report this as a bug to LKML, Simon Wood <simon@mungewell.org> or "
1332 			     "Michal Maly <madcatxster@devoid-pointer.net>\n");
1333 		error = -1;
1334 		goto err_init;
1335 	}
1336 
1337 	if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1338 		for (mmode_idx = 0; mmode_idx < ARRAY_SIZE(lg4ff_multimode_wheels); mmode_idx++) {
1339 			if (real_product_id == lg4ff_multimode_wheels[mmode_idx].product_id)
1340 				break;
1341 		}
1342 
1343 		if (mmode_idx == ARRAY_SIZE(lg4ff_multimode_wheels)) {
1344 			hid_err(hid, "Device product ID %X is not listed as a multimode wheel", real_product_id);
1345 			error = -1;
1346 			goto err_init;
1347 		}
1348 	}
1349 
1350 	/* Set supported force feedback capabilities */
1351 	for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
1352 		set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
1353 
1354 	error = input_ff_create_memless(dev, NULL, lg4ff_play);
1355 
1356 	if (error)
1357 		goto err_init;
1358 
1359 	/* Initialize device properties */
1360 	if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1361 		if (WARN_ON(mmode_idx == -1))
1362 			return -EINVAL;
1363 		mmode_wheel = &lg4ff_multimode_wheels[mmode_idx];
1364 	}
1365 	lg4ff_init_wheel_data(&entry->wdata, &lg4ff_devices[i], mmode_wheel, real_product_id);
1366 
1367 	/* Check if autocentering is available and
1368 	 * set the centering force to zero by default */
1369 	if (test_bit(FF_AUTOCENTER, dev->ffbit)) {
1370 		/* Formula Force EX expects different autocentering command */
1371 		if ((bcdDevice >> 8) == LG4FF_FFEX_REV_MAJ &&
1372 		    (bcdDevice & 0xff) == LG4FF_FFEX_REV_MIN)
1373 			dev->ff->set_autocenter = lg4ff_set_autocenter_ffex;
1374 		else
1375 			dev->ff->set_autocenter = lg4ff_set_autocenter_default;
1376 
1377 		dev->ff->set_autocenter(dev, 0);
1378 	}
1379 
1380 	/* Create sysfs interface */
1381 	error = device_create_file(&hid->dev, &dev_attr_combine_pedals);
1382 	if (error)
1383 		hid_warn(hid, "Unable to create sysfs interface for \"combine\", errno %d\n", error);
1384 	error = device_create_file(&hid->dev, &dev_attr_range);
1385 	if (error)
1386 		hid_warn(hid, "Unable to create sysfs interface for \"range\", errno %d\n", error);
1387 	if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1388 		error = device_create_file(&hid->dev, &dev_attr_real_id);
1389 		if (error)
1390 			hid_warn(hid, "Unable to create sysfs interface for \"real_id\", errno %d\n", error);
1391 		error = device_create_file(&hid->dev, &dev_attr_alternate_modes);
1392 		if (error)
1393 			hid_warn(hid, "Unable to create sysfs interface for \"alternate_modes\", errno %d\n", error);
1394 	}
1395 	dbg_hid("sysfs interface created\n");
1396 
1397 	/* Set the maximum range to start with */
1398 	entry->wdata.range = entry->wdata.max_range;
1399 	if (entry->wdata.set_range)
1400 		entry->wdata.set_range(hid, entry->wdata.range);
1401 
1402 #ifdef CONFIG_LEDS_CLASS
1403 	/* register led subsystem - G27/G29 only */
1404 	entry->wdata.led_state = 0;
1405 	for (j = 0; j < 5; j++)
1406 		entry->wdata.led[j] = NULL;
1407 
1408 	if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL ||
1409 			lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G29_WHEEL) {
1410 		struct led_classdev *led;
1411 		size_t name_sz;
1412 		char *name;
1413 
1414 		lg4ff_set_leds(hid, 0);
1415 
1416 		name_sz = strlen(dev_name(&hid->dev)) + 8;
1417 
1418 		for (j = 0; j < 5; j++) {
1419 			led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
1420 			if (!led) {
1421 				hid_err(hid, "can't allocate memory for LED %d\n", j);
1422 				goto err_leds;
1423 			}
1424 
1425 			name = (void *)(&led[1]);
1426 			snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1);
1427 			led->name = name;
1428 			led->brightness = 0;
1429 			led->max_brightness = 1;
1430 			led->brightness_get = lg4ff_led_get_brightness;
1431 			led->brightness_set = lg4ff_led_set_brightness;
1432 
1433 			entry->wdata.led[j] = led;
1434 			error = led_classdev_register(&hid->dev, led);
1435 
1436 			if (error) {
1437 				hid_err(hid, "failed to register LED %d. Aborting.\n", j);
1438 err_leds:
1439 				/* Deregister LEDs (if any) */
1440 				for (j = 0; j < 5; j++) {
1441 					led = entry->wdata.led[j];
1442 					entry->wdata.led[j] = NULL;
1443 					if (!led)
1444 						continue;
1445 					led_classdev_unregister(led);
1446 					kfree(led);
1447 				}
1448 				goto out;	/* Let the driver continue without LEDs */
1449 			}
1450 		}
1451 	}
1452 out:
1453 #endif
1454 	hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n");
1455 	return 0;
1456 
1457 err_init:
1458 	drv_data->device_props = NULL;
1459 	kfree(entry);
1460 	return error;
1461 }
1462 
1463 int lg4ff_deinit(struct hid_device *hid)
1464 {
1465 	struct lg4ff_device_entry *entry;
1466 	struct lg_drv_data *drv_data;
1467 
1468 	drv_data = hid_get_drvdata(hid);
1469 	if (!drv_data) {
1470 		hid_err(hid, "Error while deinitializing device, no private driver data.\n");
1471 		return -1;
1472 	}
1473 	entry = drv_data->device_props;
1474 	if (!entry)
1475 		goto out; /* Nothing more to do */
1476 
1477 	/* Multimode devices will have at least the "MODE_NATIVE" bit set */
1478 	if (entry->wdata.alternate_modes) {
1479 		device_remove_file(&hid->dev, &dev_attr_real_id);
1480 		device_remove_file(&hid->dev, &dev_attr_alternate_modes);
1481 	}
1482 
1483 	device_remove_file(&hid->dev, &dev_attr_combine_pedals);
1484 	device_remove_file(&hid->dev, &dev_attr_range);
1485 #ifdef CONFIG_LEDS_CLASS
1486 	{
1487 		int j;
1488 		struct led_classdev *led;
1489 
1490 		/* Deregister LEDs (if any) */
1491 		for (j = 0; j < 5; j++) {
1492 
1493 			led = entry->wdata.led[j];
1494 			entry->wdata.led[j] = NULL;
1495 			if (!led)
1496 				continue;
1497 			led_classdev_unregister(led);
1498 			kfree(led);
1499 		}
1500 	}
1501 #endif
1502 	drv_data->device_props = NULL;
1503 
1504 	kfree(entry);
1505 out:
1506 	dbg_hid("Device successfully unregistered\n");
1507 	return 0;
1508 }
1509