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