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