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