2 * Force feedback support for Logitech Gaming Wheels
4 * Including G27, G25, DFP, DFGT, FFEX, Momo, Momo2 &
5 * Speed Force Wireless (WiiWheel)
7 * Copyright (c) 2010 Simon Wood <simon@mungewell.org>
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.
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.
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
27 #include <linux/input.h>
28 #include <linux/usb.h>
29 #include <linux/hid.h>
31 #include "usbhid/usbhid.h"
33 #include "hid-lg4ff.h"
36 #define LG4FF_MMODE_IS_MULTIMODE 0
37 #define LG4FF_MMODE_SWITCHED 1
38 #define LG4FF_MMODE_NOT_MULTIMODE 2
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
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)
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"
70 #define LG4FF_FFEX_REV_MAJ 0x21
71 #define LG4FF_FFEX_REV_MIN 0x00
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);
76 struct lg4ff_wheel_data {
82 #ifdef CONFIG_LEDS_CLASS
84 struct led_classdev *led[5];
86 const u32 alternate_modes;
87 const char * const real_tag;
88 const char * const real_name;
89 const u16 real_product_id;
91 void (*set_range)(struct hid_device *hid, u16 range);
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;
100 static const signed short lg4ff_wheel_effects[] = {
107 const u32 product_id;
108 const signed short *ff_effects;
111 void (*set_range)(struct hid_device *hid, u16 range);
114 struct lg4ff_compat_mode_switch {
115 const u8 cmd_count; /* Number of commands to send */
119 struct lg4ff_wheel_ident_info {
123 const u16 real_product_id;
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;
133 struct lg4ff_alternate_mode {
134 const u16 product_id;
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}
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},
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},
179 /* Multimode wheel identificators */
180 static const struct lg4ff_wheel_ident_info lg4ff_dfp_ident_info = {
181 LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
184 USB_DEVICE_ID_LOGITECH_DFP_WHEEL
187 static const struct lg4ff_wheel_ident_info lg4ff_g25_ident_info = {
188 LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
191 USB_DEVICE_ID_LOGITECH_G25_WHEEL
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,
198 USB_DEVICE_ID_LOGITECH_G27_WHEEL
201 static const struct lg4ff_wheel_ident_info lg4ff_dfgt_ident_info = {
202 LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
205 USB_DEVICE_ID_LOGITECH_DFGT_WHEEL
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,
212 USB_DEVICE_ID_LOGITECH_G29_WHEEL
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,
219 USB_DEVICE_ID_LOGITECH_G29_WHEEL
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
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 = {
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 */
240 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfp = {
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 */
246 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g25 = {
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 */
252 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfgt = {
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 */
258 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g27 = {
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 */
264 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g29 = {
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 */
270 /* EXT_CMD1 - Understood by DFP, G25, G27 and DFGT */
271 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext01_dfp = {
273 {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
276 /* EXT_CMD16 - Understood by G25 and G27 */
277 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext16_g25 = {
279 {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
282 /* Recalculates X axis value accordingly to currently selected range */
283 static s32 lg4ff_adjust_dfp_x_axis(s32 value, u16 range)
290 else if (range == 200)
292 else if (range < 200)
297 new_value = 8192 + mult_frac(value - 8192, max_range, range);
300 else if (new_value > 16383)
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)
309 struct lg4ff_device_entry *entry = drv_data->device_props;
313 hid_err(hid, "Device properties not found");
317 switch (entry->wdata.product_id) {
318 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
319 switch (usage->code) {
321 new_value = lg4ff_adjust_dfp_x_axis(value, entry->wdata.range);
322 input_event(field->hidinput->input, usage->type, usage->code, new_value);
332 static void lg4ff_init_wheel_data(struct lg4ff_wheel_data * const wdata, const struct lg4ff_wheel *wheel,
333 const struct lg4ff_multimode_wheel *mmode_wheel,
334 const u16 real_product_id)
336 u32 alternate_modes = 0;
337 const char *real_tag = NULL;
338 const char *real_name = NULL;
341 alternate_modes = mmode_wheel->alternate_modes;
342 real_tag = mmode_wheel->real_tag;
343 real_name = mmode_wheel->real_name;
347 struct lg4ff_wheel_data t_wdata = { .product_id = wheel->product_id,
348 .real_product_id = real_product_id,
350 .min_range = wheel->min_range,
351 .max_range = wheel->max_range,
352 .set_range = wheel->set_range,
353 .alternate_modes = alternate_modes,
354 .real_tag = real_tag,
355 .real_name = real_name };
357 memcpy(wdata, &t_wdata, sizeof(t_wdata));
361 static int lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
363 struct hid_device *hid = input_get_drvdata(dev);
364 struct lg4ff_device_entry *entry;
365 struct lg_drv_data *drv_data;
370 drv_data = hid_get_drvdata(hid);
372 hid_err(hid, "Private driver data not found!\n");
376 entry = drv_data->device_props;
378 hid_err(hid, "Device properties not found!\n");
381 value = entry->report->field[0]->value;
383 #define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0)
385 switch (effect->type) {
387 x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */
390 spin_lock_irqsave(&entry->report_lock, flags);
392 /* De-activate force in slot-1*/
401 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
402 spin_unlock_irqrestore(&entry->report_lock, flags);
406 value[0] = 0x11; /* Slot 1 */
414 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
415 spin_unlock_irqrestore(&entry->report_lock, flags);
421 /* Sends default autocentering command compatible with
422 * all wheels except Formula Force EX */
423 static void lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude)
425 struct hid_device *hid = input_get_drvdata(dev);
426 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
427 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
428 s32 *value = report->field[0]->value;
429 u32 expand_a, expand_b;
430 struct lg4ff_device_entry *entry;
431 struct lg_drv_data *drv_data;
434 drv_data = hid_get_drvdata(hid);
436 hid_err(hid, "Private driver data not found!\n");
440 entry = drv_data->device_props;
442 hid_err(hid, "Device properties not found!\n");
445 value = entry->report->field[0]->value;
447 /* De-activate Auto-Center */
448 spin_lock_irqsave(&entry->report_lock, flags);
449 if (magnitude == 0) {
458 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
459 spin_unlock_irqrestore(&entry->report_lock, flags);
463 if (magnitude <= 0xaaaa) {
464 expand_a = 0x0c * magnitude;
465 expand_b = 0x80 * magnitude;
467 expand_a = (0x0c * 0xaaaa) + 0x06 * (magnitude - 0xaaaa);
468 expand_b = (0x80 * 0xaaaa) + 0xff * (magnitude - 0xaaaa);
471 /* Adjust for non-MOMO wheels */
472 switch (entry->wdata.product_id) {
473 case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL:
474 case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2:
477 expand_a = expand_a >> 1;
483 value[2] = expand_a / 0xaaaa;
484 value[3] = expand_a / 0xaaaa;
485 value[4] = expand_b / 0xaaaa;
489 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
491 /* Activate Auto-Center */
500 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
501 spin_unlock_irqrestore(&entry->report_lock, flags);
504 /* Sends autocentering command compatible with Formula Force EX */
505 static void lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude)
507 struct hid_device *hid = input_get_drvdata(dev);
508 struct lg4ff_device_entry *entry;
509 struct lg_drv_data *drv_data;
512 magnitude = magnitude * 90 / 65535;
514 drv_data = hid_get_drvdata(hid);
516 hid_err(hid, "Private driver data not found!\n");
520 entry = drv_data->device_props;
522 hid_err(hid, "Device properties not found!\n");
525 value = entry->report->field[0]->value;
527 spin_lock_irqsave(&entry->report_lock, flags);
530 value[2] = magnitude >> 14;
531 value[3] = magnitude >> 14;
532 value[4] = magnitude;
536 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
537 spin_unlock_irqrestore(&entry->report_lock, flags);
540 /* Sends command to set range compatible with G25/G27/Driving Force GT */
541 static void lg4ff_set_range_g25(struct hid_device *hid, u16 range)
543 struct lg4ff_device_entry *entry;
544 struct lg_drv_data *drv_data;
548 drv_data = hid_get_drvdata(hid);
550 hid_err(hid, "Private driver data not found!\n");
554 entry = drv_data->device_props;
556 hid_err(hid, "Device properties not found!\n");
559 value = entry->report->field[0]->value;
560 dbg_hid("G25/G27/DFGT: setting range to %u\n", range);
562 spin_lock_irqsave(&entry->report_lock, flags);
565 value[2] = range & 0x00ff;
566 value[3] = (range & 0xff00) >> 8;
571 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
572 spin_unlock_irqrestore(&entry->report_lock, flags);
575 /* Sends commands to set range compatible with Driving Force Pro wheel */
576 static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range)
578 struct lg4ff_device_entry *entry;
579 struct lg_drv_data *drv_data;
581 int start_left, start_right, full_range;
584 drv_data = hid_get_drvdata(hid);
586 hid_err(hid, "Private driver data not found!\n");
590 entry = drv_data->device_props;
592 hid_err(hid, "Device properties not found!\n");
595 value = entry->report->field[0]->value;
596 dbg_hid("Driving Force Pro: setting range to %u\n", range);
598 /* Prepare "coarse" limit command */
599 spin_lock_irqsave(&entry->report_lock, flags);
601 value[1] = 0x00; /* Set later */
615 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
617 /* Prepare "fine" limit command */
626 if (range == 200 || range == 900) { /* Do not apply any fine limit */
627 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
628 spin_unlock_irqrestore(&entry->report_lock, flags);
632 /* Construct fine limit command */
633 start_left = (((full_range - range + 1) * 2047) / full_range);
634 start_right = 0xfff - start_left;
636 value[2] = start_left >> 4;
637 value[3] = start_right >> 4;
639 value[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
642 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
643 spin_unlock_irqrestore(&entry->report_lock, flags);
646 static const struct lg4ff_compat_mode_switch *lg4ff_get_mode_switch_command(const u16 real_product_id, const u16 target_product_id)
648 switch (real_product_id) {
649 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
650 switch (target_product_id) {
651 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
652 return &lg4ff_mode_switch_ext01_dfp;
653 /* DFP can only be switched to its native mode */
658 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
659 switch (target_product_id) {
660 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
661 return &lg4ff_mode_switch_ext01_dfp;
662 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
663 return &lg4ff_mode_switch_ext16_g25;
664 /* G25 can only be switched to DFP mode or its native mode */
669 case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
670 switch (target_product_id) {
671 case USB_DEVICE_ID_LOGITECH_WHEEL:
672 return &lg4ff_mode_switch_ext09_dfex;
673 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
674 return &lg4ff_mode_switch_ext09_dfp;
675 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
676 return &lg4ff_mode_switch_ext09_g25;
677 case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
678 return &lg4ff_mode_switch_ext09_g27;
679 /* G27 can only be switched to DF-EX, DFP, G25 or its native mode */
684 case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
685 switch (target_product_id) {
686 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
687 return &lg4ff_mode_switch_ext09_dfp;
688 case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
689 return &lg4ff_mode_switch_ext09_dfgt;
690 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
691 return &lg4ff_mode_switch_ext09_g25;
692 case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
693 return &lg4ff_mode_switch_ext09_g27;
694 case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
695 return &lg4ff_mode_switch_ext09_g29;
696 /* G29 can only be switched to DF-EX, DFP, DFGT, G25, G27 or its native mode */
701 case USB_DEVICE_ID_LOGITECH_DFGT_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_DFGT_WHEEL:
708 return &lg4ff_mode_switch_ext09_dfgt;
709 /* DFGT can only be switched to DF-EX, DFP or its native mode */
714 /* No other wheels have multiple modes */
720 static int lg4ff_switch_compatibility_mode(struct hid_device *hid, const struct lg4ff_compat_mode_switch *s)
722 struct lg4ff_device_entry *entry;
723 struct lg_drv_data *drv_data;
728 drv_data = hid_get_drvdata(hid);
730 hid_err(hid, "Private driver data not found!\n");
734 entry = drv_data->device_props;
736 hid_err(hid, "Device properties not found!\n");
739 value = entry->report->field[0]->value;
741 spin_lock_irqsave(&entry->report_lock, flags);
742 for (i = 0; i < s->cmd_count; i++) {
745 for (j = 0; j < 7; j++)
746 value[j] = s->cmd[j + (7*i)];
748 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
750 spin_unlock_irqrestore(&entry->report_lock, flags);
755 static ssize_t lg4ff_alternate_modes_show(struct device *dev, struct device_attribute *attr, char *buf)
757 struct hid_device *hid = to_hid_device(dev);
758 struct lg4ff_device_entry *entry;
759 struct lg_drv_data *drv_data;
763 drv_data = hid_get_drvdata(hid);
765 hid_err(hid, "Private driver data not found!\n");
769 entry = drv_data->device_props;
771 hid_err(hid, "Device properties not found!\n");
775 if (!entry->wdata.real_name) {
776 hid_err(hid, "NULL pointer to string\n");
780 for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
781 if (entry->wdata.alternate_modes & BIT(i)) {
782 /* Print tag and full name */
783 count += scnprintf(buf + count, PAGE_SIZE - count, "%s: %s",
784 lg4ff_alternate_modes[i].tag,
785 !lg4ff_alternate_modes[i].product_id ? entry->wdata.real_name : lg4ff_alternate_modes[i].name);
786 if (count >= PAGE_SIZE - 1)
789 /* Mark the currently active mode with an asterisk */
790 if (lg4ff_alternate_modes[i].product_id == entry->wdata.product_id ||
791 (lg4ff_alternate_modes[i].product_id == 0 && entry->wdata.product_id == entry->wdata.real_product_id))
792 count += scnprintf(buf + count, PAGE_SIZE - count, " *\n");
794 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
796 if (count >= PAGE_SIZE - 1)
804 static ssize_t lg4ff_alternate_modes_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
806 struct hid_device *hid = to_hid_device(dev);
807 struct lg4ff_device_entry *entry;
808 struct lg_drv_data *drv_data;
809 const struct lg4ff_compat_mode_switch *s;
810 u16 target_product_id = 0;
814 drv_data = hid_get_drvdata(hid);
816 hid_err(hid, "Private driver data not found!\n");
820 entry = drv_data->device_props;
822 hid_err(hid, "Device properties not found!\n");
826 /* Allow \n at the end of the input parameter */
827 lbuf = kasprintf(GFP_KERNEL, "%s", buf);
832 if (lbuf[i-1] == '\n') {
840 for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
841 const u16 mode_product_id = lg4ff_alternate_modes[i].product_id;
842 const char *tag = lg4ff_alternate_modes[i].tag;
844 if (entry->wdata.alternate_modes & BIT(i)) {
845 if (!strcmp(tag, lbuf)) {
846 if (!mode_product_id)
847 target_product_id = entry->wdata.real_product_id;
849 target_product_id = mode_product_id;
855 if (i == LG4FF_MODE_MAX_IDX) {
856 hid_info(hid, "Requested mode \"%s\" is not supported by the device\n", lbuf);
860 kfree(lbuf); /* Not needed anymore */
862 if (target_product_id == entry->wdata.product_id) /* Nothing to do */
865 /* Automatic switching has to be disabled for the switch to DF-EX mode to work correctly */
866 if (target_product_id == USB_DEVICE_ID_LOGITECH_WHEEL && !lg4ff_no_autoswitch) {
867 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",
868 entry->wdata.real_name);
872 /* Take care of hardware limitations */
873 if ((entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_DFP_WHEEL || entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_G25_WHEEL) &&
874 entry->wdata.product_id > target_product_id) {
875 hid_info(hid, "\"%s\" cannot be switched back into \"%s\" mode\n", entry->wdata.real_name, lg4ff_alternate_modes[i].name);
879 s = lg4ff_get_mode_switch_command(entry->wdata.real_product_id, target_product_id);
881 hid_err(hid, "Invalid target product ID %X\n", target_product_id);
885 ret = lg4ff_switch_compatibility_mode(hid, s);
886 return (ret == 0 ? count : ret);
888 static DEVICE_ATTR(alternate_modes, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_alternate_modes_show, lg4ff_alternate_modes_store);
890 static ssize_t lg4ff_combine_show(struct device *dev, struct device_attribute *attr,
893 struct hid_device *hid = to_hid_device(dev);
894 struct lg4ff_device_entry *entry;
895 struct lg_drv_data *drv_data;
898 drv_data = hid_get_drvdata(hid);
900 hid_err(hid, "Private driver data not found!\n");
904 entry = drv_data->device_props;
906 hid_err(hid, "Device properties not found!\n");
910 count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->wdata.combine);
914 static ssize_t lg4ff_combine_store(struct device *dev, struct device_attribute *attr,
915 const char *buf, size_t count)
917 struct hid_device *hid = to_hid_device(dev);
918 struct lg4ff_device_entry *entry;
919 struct lg_drv_data *drv_data;
920 u16 combine = simple_strtoul(buf, NULL, 10);
922 drv_data = hid_get_drvdata(hid);
924 hid_err(hid, "Private driver data not found!\n");
928 entry = drv_data->device_props;
930 hid_err(hid, "Device properties not found!\n");
937 entry->wdata.combine = combine;
940 static DEVICE_ATTR(combine_pedals, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_combine_show, lg4ff_combine_store);
942 /* Export the currently set range of the wheel */
943 static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr,
946 struct hid_device *hid = to_hid_device(dev);
947 struct lg4ff_device_entry *entry;
948 struct lg_drv_data *drv_data;
951 drv_data = hid_get_drvdata(hid);
953 hid_err(hid, "Private driver data not found!\n");
957 entry = drv_data->device_props;
959 hid_err(hid, "Device properties not found!\n");
963 count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->wdata.range);
967 /* Set range to user specified value, call appropriate function
968 * according to the type of the wheel */
969 static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr,
970 const char *buf, size_t count)
972 struct hid_device *hid = to_hid_device(dev);
973 struct lg4ff_device_entry *entry;
974 struct lg_drv_data *drv_data;
975 u16 range = simple_strtoul(buf, NULL, 10);
977 drv_data = hid_get_drvdata(hid);
979 hid_err(hid, "Private driver data not found!\n");
983 entry = drv_data->device_props;
985 hid_err(hid, "Device properties not found!\n");
990 range = entry->wdata.max_range;
992 /* Check if the wheel supports range setting
993 * and that the range is within limits for the wheel */
994 if (entry->wdata.set_range && range >= entry->wdata.min_range && range <= entry->wdata.max_range) {
995 entry->wdata.set_range(hid, range);
996 entry->wdata.range = range;
1001 static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_range_show, lg4ff_range_store);
1003 static ssize_t lg4ff_real_id_show(struct device *dev, struct device_attribute *attr, char *buf)
1005 struct hid_device *hid = to_hid_device(dev);
1006 struct lg4ff_device_entry *entry;
1007 struct lg_drv_data *drv_data;
1010 drv_data = hid_get_drvdata(hid);
1012 hid_err(hid, "Private driver data not found!\n");
1016 entry = drv_data->device_props;
1018 hid_err(hid, "Device properties not found!\n");
1022 if (!entry->wdata.real_tag || !entry->wdata.real_name) {
1023 hid_err(hid, "NULL pointer to string\n");
1027 count = scnprintf(buf, PAGE_SIZE, "%s: %s\n", entry->wdata.real_tag, entry->wdata.real_name);
1031 static ssize_t lg4ff_real_id_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1033 /* Real ID is a read-only value */
1036 static DEVICE_ATTR(real_id, S_IRUGO, lg4ff_real_id_show, lg4ff_real_id_store);
1038 #ifdef CONFIG_LEDS_CLASS
1039 static void lg4ff_set_leds(struct hid_device *hid, u8 leds)
1041 struct lg_drv_data *drv_data;
1042 struct lg4ff_device_entry *entry;
1043 unsigned long flags;
1046 drv_data = hid_get_drvdata(hid);
1048 hid_err(hid, "Private driver data not found!\n");
1052 entry = drv_data->device_props;
1054 hid_err(hid, "Device properties not found!\n");
1057 value = entry->report->field[0]->value;
1059 spin_lock_irqsave(&entry->report_lock, flags);
1067 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
1068 spin_unlock_irqrestore(&entry->report_lock, flags);
1071 static void lg4ff_led_set_brightness(struct led_classdev *led_cdev,
1072 enum led_brightness value)
1074 struct device *dev = led_cdev->dev->parent;
1075 struct hid_device *hid = to_hid_device(dev);
1076 struct lg_drv_data *drv_data = hid_get_drvdata(hid);
1077 struct lg4ff_device_entry *entry;
1081 hid_err(hid, "Device data not found.");
1085 entry = drv_data->device_props;
1088 hid_err(hid, "Device properties not found.");
1092 for (i = 0; i < 5; i++) {
1093 if (led_cdev != entry->wdata.led[i])
1095 state = (entry->wdata.led_state >> i) & 1;
1096 if (value == LED_OFF && state) {
1097 entry->wdata.led_state &= ~(1 << i);
1098 lg4ff_set_leds(hid, entry->wdata.led_state);
1099 } else if (value != LED_OFF && !state) {
1100 entry->wdata.led_state |= 1 << i;
1101 lg4ff_set_leds(hid, entry->wdata.led_state);
1107 static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev)
1109 struct device *dev = led_cdev->dev->parent;
1110 struct hid_device *hid = to_hid_device(dev);
1111 struct lg_drv_data *drv_data = hid_get_drvdata(hid);
1112 struct lg4ff_device_entry *entry;
1116 hid_err(hid, "Device data not found.");
1120 entry = drv_data->device_props;
1123 hid_err(hid, "Device properties not found.");
1127 for (i = 0; i < 5; i++)
1128 if (led_cdev == entry->wdata.led[i]) {
1129 value = (entry->wdata.led_state >> i) & 1;
1133 return value ? LED_FULL : LED_OFF;
1137 static u16 lg4ff_identify_multimode_wheel(struct hid_device *hid, const u16 reported_product_id, const u16 bcdDevice)
1142 /* identify current mode from USB PID */
1143 for (i = 1; i < ARRAY_SIZE(lg4ff_alternate_modes); i++) {
1144 dbg_hid("Testing whether PID is %X\n", lg4ff_alternate_modes[i].product_id);
1145 if (reported_product_id == lg4ff_alternate_modes[i].product_id)
1149 if (i == ARRAY_SIZE(lg4ff_alternate_modes))
1152 current_mode = BIT(i);
1154 for (i = 0; i < ARRAY_SIZE(lg4ff_main_checklist); i++) {
1155 const u16 mask = lg4ff_main_checklist[i]->mask;
1156 const u16 result = lg4ff_main_checklist[i]->result;
1157 const u16 real_product_id = lg4ff_main_checklist[i]->real_product_id;
1159 if ((current_mode & lg4ff_main_checklist[i]->modes) && \
1160 (bcdDevice & mask) == result) {
1161 dbg_hid("Found wheel with real PID %X whose reported PID is %X\n", real_product_id, reported_product_id);
1162 return real_product_id;
1166 /* No match found. This is either Driving Force or an unknown
1167 * wheel model, do not touch it */
1168 dbg_hid("Wheel with bcdDevice %X was not recognized as multimode wheel, leaving in its current mode\n", bcdDevice);
1172 static int lg4ff_handle_multimode_wheel(struct hid_device *hid, u16 *real_product_id, const u16 bcdDevice)
1174 const u16 reported_product_id = hid->product;
1177 *real_product_id = lg4ff_identify_multimode_wheel(hid, reported_product_id, bcdDevice);
1178 /* Probed wheel is not a multimode wheel */
1179 if (!*real_product_id) {
1180 *real_product_id = reported_product_id;
1181 dbg_hid("Wheel is not a multimode wheel\n");
1182 return LG4FF_MMODE_NOT_MULTIMODE;
1185 /* Switch from "Driving Force" mode to native mode automatically.
1186 * Otherwise keep the wheel in its current mode */
1187 if (reported_product_id == USB_DEVICE_ID_LOGITECH_WHEEL &&
1188 reported_product_id != *real_product_id &&
1189 !lg4ff_no_autoswitch) {
1190 const struct lg4ff_compat_mode_switch *s = lg4ff_get_mode_switch_command(*real_product_id, *real_product_id);
1193 hid_err(hid, "Invalid product id %X\n", *real_product_id);
1194 return LG4FF_MMODE_NOT_MULTIMODE;
1197 ret = lg4ff_switch_compatibility_mode(hid, s);
1199 /* Wheel could not have been switched to native mode,
1200 * leave it in "Driving Force" mode and continue */
1201 hid_err(hid, "Unable to switch wheel mode, errno %d\n", ret);
1202 return LG4FF_MMODE_IS_MULTIMODE;
1204 return LG4FF_MMODE_SWITCHED;
1207 return LG4FF_MMODE_IS_MULTIMODE;
1211 int lg4ff_init(struct hid_device *hid)
1213 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1214 struct input_dev *dev = hidinput->input;
1215 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
1216 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
1217 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
1218 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
1219 const struct lg4ff_multimode_wheel *mmode_wheel = NULL;
1220 struct lg4ff_device_entry *entry;
1221 struct lg_drv_data *drv_data;
1223 int mmode_ret, mmode_idx = -1;
1224 u16 real_product_id;
1226 /* Check that the report looks ok */
1227 if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
1230 drv_data = hid_get_drvdata(hid);
1232 hid_err(hid, "Cannot add device, private driver data not allocated\n");
1235 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1238 spin_lock_init(&entry->report_lock);
1239 entry->report = report;
1240 drv_data->device_props = entry;
1242 /* Check if a multimode wheel has been connected and
1243 * handle it appropriately */
1244 mmode_ret = lg4ff_handle_multimode_wheel(hid, &real_product_id, bcdDevice);
1246 /* Wheel has been told to switch to native mode. There is no point in going on
1247 * with the initialization as the wheel will do a USB reset when it switches mode
1249 if (mmode_ret == LG4FF_MMODE_SWITCHED)
1251 else if (mmode_ret < 0) {
1252 hid_err(hid, "Unable to switch device mode during initialization, errno %d\n", mmode_ret);
1257 /* Check what wheel has been connected */
1258 for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
1259 if (hid->product == lg4ff_devices[i].product_id) {
1260 dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
1265 if (i == ARRAY_SIZE(lg4ff_devices)) {
1266 hid_err(hid, "This device is flagged to be handled by the lg4ff module but this module does not know how to handle it. "
1267 "Please report this as a bug to LKML, Simon Wood <simon@mungewell.org> or "
1268 "Michal Maly <madcatxster@devoid-pointer.net>\n");
1273 if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1274 for (mmode_idx = 0; mmode_idx < ARRAY_SIZE(lg4ff_multimode_wheels); mmode_idx++) {
1275 if (real_product_id == lg4ff_multimode_wheels[mmode_idx].product_id)
1279 if (mmode_idx == ARRAY_SIZE(lg4ff_multimode_wheels)) {
1280 hid_err(hid, "Device product ID %X is not listed as a multimode wheel", real_product_id);
1286 /* Set supported force feedback capabilities */
1287 for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
1288 set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
1290 error = input_ff_create_memless(dev, NULL, lg4ff_play);
1295 /* Initialize device properties */
1296 if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1297 BUG_ON(mmode_idx == -1);
1298 mmode_wheel = &lg4ff_multimode_wheels[mmode_idx];
1300 lg4ff_init_wheel_data(&entry->wdata, &lg4ff_devices[i], mmode_wheel, real_product_id);
1302 /* Check if autocentering is available and
1303 * set the centering force to zero by default */
1304 if (test_bit(FF_AUTOCENTER, dev->ffbit)) {
1305 /* Formula Force EX expects different autocentering command */
1306 if ((bcdDevice >> 8) == LG4FF_FFEX_REV_MAJ &&
1307 (bcdDevice & 0xff) == LG4FF_FFEX_REV_MIN)
1308 dev->ff->set_autocenter = lg4ff_set_autocenter_ffex;
1310 dev->ff->set_autocenter = lg4ff_set_autocenter_default;
1312 dev->ff->set_autocenter(dev, 0);
1315 /* Create sysfs interface */
1316 error = device_create_file(&hid->dev, &dev_attr_combine_pedals);
1318 hid_warn(hid, "Unable to create sysfs interface for \"combine\", errno %d\n", error);
1319 error = device_create_file(&hid->dev, &dev_attr_range);
1321 hid_warn(hid, "Unable to create sysfs interface for \"range\", errno %d\n", error);
1322 if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1323 error = device_create_file(&hid->dev, &dev_attr_real_id);
1325 hid_warn(hid, "Unable to create sysfs interface for \"real_id\", errno %d\n", error);
1326 error = device_create_file(&hid->dev, &dev_attr_alternate_modes);
1328 hid_warn(hid, "Unable to create sysfs interface for \"alternate_modes\", errno %d\n", error);
1330 dbg_hid("sysfs interface created\n");
1332 /* Set the maximum range to start with */
1333 entry->wdata.range = entry->wdata.max_range;
1334 if (entry->wdata.set_range)
1335 entry->wdata.set_range(hid, entry->wdata.range);
1337 #ifdef CONFIG_LEDS_CLASS
1338 /* register led subsystem - G27/G29 only */
1339 entry->wdata.led_state = 0;
1340 for (j = 0; j < 5; j++)
1341 entry->wdata.led[j] = NULL;
1343 if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL ||
1344 lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G29_WHEEL) {
1345 struct led_classdev *led;
1349 lg4ff_set_leds(hid, 0);
1351 name_sz = strlen(dev_name(&hid->dev)) + 8;
1353 for (j = 0; j < 5; j++) {
1354 led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
1356 hid_err(hid, "can't allocate memory for LED %d\n", j);
1360 name = (void *)(&led[1]);
1361 snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1);
1363 led->brightness = 0;
1364 led->max_brightness = 1;
1365 led->brightness_get = lg4ff_led_get_brightness;
1366 led->brightness_set = lg4ff_led_set_brightness;
1368 entry->wdata.led[j] = led;
1369 error = led_classdev_register(&hid->dev, led);
1372 hid_err(hid, "failed to register LED %d. Aborting.\n", j);
1374 /* Deregister LEDs (if any) */
1375 for (j = 0; j < 5; j++) {
1376 led = entry->wdata.led[j];
1377 entry->wdata.led[j] = NULL;
1380 led_classdev_unregister(led);
1383 goto out; /* Let the driver continue without LEDs */
1389 hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n");
1393 drv_data->device_props = NULL;
1398 int lg4ff_deinit(struct hid_device *hid)
1400 struct lg4ff_device_entry *entry;
1401 struct lg_drv_data *drv_data;
1403 drv_data = hid_get_drvdata(hid);
1405 hid_err(hid, "Error while deinitializing device, no private driver data.\n");
1408 entry = drv_data->device_props;
1410 goto out; /* Nothing more to do */
1412 /* Multimode devices will have at least the "MODE_NATIVE" bit set */
1413 if (entry->wdata.alternate_modes) {
1414 device_remove_file(&hid->dev, &dev_attr_real_id);
1415 device_remove_file(&hid->dev, &dev_attr_alternate_modes);
1418 device_remove_file(&hid->dev, &dev_attr_combine_pedals);
1419 device_remove_file(&hid->dev, &dev_attr_range);
1420 #ifdef CONFIG_LEDS_CLASS
1423 struct led_classdev *led;
1425 /* Deregister LEDs (if any) */
1426 for (j = 0; j < 5; j++) {
1428 led = entry->wdata.led[j];
1429 entry->wdata.led[j] = NULL;
1432 led_classdev_unregister(led);
1438 drv_data->device_props = NULL;
1442 dbg_hid("Device successfully unregistered\n");