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