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 int lg4ff_raw_event(struct hid_device *hdev, struct hid_report *report,
333 u8 *rd, int size, struct lg_drv_data *drv_data)
335 struct lg4ff_device_entry *entry = drv_data->device_props;
340 /* adjust HID report present combined pedals data */
341 if (entry->wdata.combine) {
342 switch (entry->wdata.product_id) {
343 case USB_DEVICE_ID_LOGITECH_WHEEL:
347 case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL:
348 case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2:
352 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
364 static void lg4ff_init_wheel_data(struct lg4ff_wheel_data * const wdata, const struct lg4ff_wheel *wheel,
365 const struct lg4ff_multimode_wheel *mmode_wheel,
366 const u16 real_product_id)
368 u32 alternate_modes = 0;
369 const char *real_tag = NULL;
370 const char *real_name = NULL;
373 alternate_modes = mmode_wheel->alternate_modes;
374 real_tag = mmode_wheel->real_tag;
375 real_name = mmode_wheel->real_name;
379 struct lg4ff_wheel_data t_wdata = { .product_id = wheel->product_id,
380 .real_product_id = real_product_id,
382 .min_range = wheel->min_range,
383 .max_range = wheel->max_range,
384 .set_range = wheel->set_range,
385 .alternate_modes = alternate_modes,
386 .real_tag = real_tag,
387 .real_name = real_name };
389 memcpy(wdata, &t_wdata, sizeof(t_wdata));
393 static int lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
395 struct hid_device *hid = input_get_drvdata(dev);
396 struct lg4ff_device_entry *entry;
397 struct lg_drv_data *drv_data;
402 drv_data = hid_get_drvdata(hid);
404 hid_err(hid, "Private driver data not found!\n");
408 entry = drv_data->device_props;
410 hid_err(hid, "Device properties not found!\n");
413 value = entry->report->field[0]->value;
415 #define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0)
417 switch (effect->type) {
419 x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */
422 spin_lock_irqsave(&entry->report_lock, flags);
424 /* De-activate force in slot-1*/
433 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
434 spin_unlock_irqrestore(&entry->report_lock, flags);
438 value[0] = 0x11; /* Slot 1 */
446 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
447 spin_unlock_irqrestore(&entry->report_lock, flags);
453 /* Sends default autocentering command compatible with
454 * all wheels except Formula Force EX */
455 static void lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude)
457 struct hid_device *hid = input_get_drvdata(dev);
458 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
459 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
460 s32 *value = report->field[0]->value;
461 u32 expand_a, expand_b;
462 struct lg4ff_device_entry *entry;
463 struct lg_drv_data *drv_data;
466 drv_data = hid_get_drvdata(hid);
468 hid_err(hid, "Private driver data not found!\n");
472 entry = drv_data->device_props;
474 hid_err(hid, "Device properties not found!\n");
477 value = entry->report->field[0]->value;
479 /* De-activate Auto-Center */
480 spin_lock_irqsave(&entry->report_lock, flags);
481 if (magnitude == 0) {
490 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
491 spin_unlock_irqrestore(&entry->report_lock, flags);
495 if (magnitude <= 0xaaaa) {
496 expand_a = 0x0c * magnitude;
497 expand_b = 0x80 * magnitude;
499 expand_a = (0x0c * 0xaaaa) + 0x06 * (magnitude - 0xaaaa);
500 expand_b = (0x80 * 0xaaaa) + 0xff * (magnitude - 0xaaaa);
503 /* Adjust for non-MOMO wheels */
504 switch (entry->wdata.product_id) {
505 case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL:
506 case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2:
509 expand_a = expand_a >> 1;
515 value[2] = expand_a / 0xaaaa;
516 value[3] = expand_a / 0xaaaa;
517 value[4] = expand_b / 0xaaaa;
521 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
523 /* Activate Auto-Center */
532 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
533 spin_unlock_irqrestore(&entry->report_lock, flags);
536 /* Sends autocentering command compatible with Formula Force EX */
537 static void lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude)
539 struct hid_device *hid = input_get_drvdata(dev);
540 struct lg4ff_device_entry *entry;
541 struct lg_drv_data *drv_data;
544 magnitude = magnitude * 90 / 65535;
546 drv_data = hid_get_drvdata(hid);
548 hid_err(hid, "Private driver data not found!\n");
552 entry = drv_data->device_props;
554 hid_err(hid, "Device properties not found!\n");
557 value = entry->report->field[0]->value;
559 spin_lock_irqsave(&entry->report_lock, flags);
562 value[2] = magnitude >> 14;
563 value[3] = magnitude >> 14;
564 value[4] = magnitude;
568 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
569 spin_unlock_irqrestore(&entry->report_lock, flags);
572 /* Sends command to set range compatible with G25/G27/Driving Force GT */
573 static void lg4ff_set_range_g25(struct hid_device *hid, u16 range)
575 struct lg4ff_device_entry *entry;
576 struct lg_drv_data *drv_data;
580 drv_data = hid_get_drvdata(hid);
582 hid_err(hid, "Private driver data not found!\n");
586 entry = drv_data->device_props;
588 hid_err(hid, "Device properties not found!\n");
591 value = entry->report->field[0]->value;
592 dbg_hid("G25/G27/DFGT: setting range to %u\n", range);
594 spin_lock_irqsave(&entry->report_lock, flags);
597 value[2] = range & 0x00ff;
598 value[3] = (range & 0xff00) >> 8;
603 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
604 spin_unlock_irqrestore(&entry->report_lock, flags);
607 /* Sends commands to set range compatible with Driving Force Pro wheel */
608 static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range)
610 struct lg4ff_device_entry *entry;
611 struct lg_drv_data *drv_data;
613 int start_left, start_right, full_range;
616 drv_data = hid_get_drvdata(hid);
618 hid_err(hid, "Private driver data not found!\n");
622 entry = drv_data->device_props;
624 hid_err(hid, "Device properties not found!\n");
627 value = entry->report->field[0]->value;
628 dbg_hid("Driving Force Pro: setting range to %u\n", range);
630 /* Prepare "coarse" limit command */
631 spin_lock_irqsave(&entry->report_lock, flags);
633 value[1] = 0x00; /* Set later */
647 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
649 /* Prepare "fine" limit command */
658 if (range == 200 || range == 900) { /* Do not apply any fine limit */
659 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
660 spin_unlock_irqrestore(&entry->report_lock, flags);
664 /* Construct fine limit command */
665 start_left = (((full_range - range + 1) * 2047) / full_range);
666 start_right = 0xfff - start_left;
668 value[2] = start_left >> 4;
669 value[3] = start_right >> 4;
671 value[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
674 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
675 spin_unlock_irqrestore(&entry->report_lock, flags);
678 static const struct lg4ff_compat_mode_switch *lg4ff_get_mode_switch_command(const u16 real_product_id, const u16 target_product_id)
680 switch (real_product_id) {
681 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
682 switch (target_product_id) {
683 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
684 return &lg4ff_mode_switch_ext01_dfp;
685 /* DFP can only be switched to its native mode */
690 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
691 switch (target_product_id) {
692 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
693 return &lg4ff_mode_switch_ext01_dfp;
694 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
695 return &lg4ff_mode_switch_ext16_g25;
696 /* G25 can only be switched to DFP mode or its native mode */
701 case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
702 switch (target_product_id) {
703 case USB_DEVICE_ID_LOGITECH_WHEEL:
704 return &lg4ff_mode_switch_ext09_dfex;
705 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
706 return &lg4ff_mode_switch_ext09_dfp;
707 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
708 return &lg4ff_mode_switch_ext09_g25;
709 case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
710 return &lg4ff_mode_switch_ext09_g27;
711 /* G27 can only be switched to DF-EX, DFP, G25 or its native mode */
716 case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
717 switch (target_product_id) {
718 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
719 return &lg4ff_mode_switch_ext09_dfp;
720 case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
721 return &lg4ff_mode_switch_ext09_dfgt;
722 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
723 return &lg4ff_mode_switch_ext09_g25;
724 case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
725 return &lg4ff_mode_switch_ext09_g27;
726 case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
727 return &lg4ff_mode_switch_ext09_g29;
728 /* G29 can only be switched to DF-EX, DFP, DFGT, G25, G27 or its native mode */
733 case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
734 switch (target_product_id) {
735 case USB_DEVICE_ID_LOGITECH_WHEEL:
736 return &lg4ff_mode_switch_ext09_dfex;
737 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
738 return &lg4ff_mode_switch_ext09_dfp;
739 case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
740 return &lg4ff_mode_switch_ext09_dfgt;
741 /* DFGT can only be switched to DF-EX, DFP or its native mode */
746 /* No other wheels have multiple modes */
752 static int lg4ff_switch_compatibility_mode(struct hid_device *hid, const struct lg4ff_compat_mode_switch *s)
754 struct lg4ff_device_entry *entry;
755 struct lg_drv_data *drv_data;
760 drv_data = hid_get_drvdata(hid);
762 hid_err(hid, "Private driver data not found!\n");
766 entry = drv_data->device_props;
768 hid_err(hid, "Device properties not found!\n");
771 value = entry->report->field[0]->value;
773 spin_lock_irqsave(&entry->report_lock, flags);
774 for (i = 0; i < s->cmd_count; i++) {
777 for (j = 0; j < 7; j++)
778 value[j] = s->cmd[j + (7*i)];
780 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
782 spin_unlock_irqrestore(&entry->report_lock, flags);
787 static ssize_t lg4ff_alternate_modes_show(struct device *dev, struct device_attribute *attr, char *buf)
789 struct hid_device *hid = to_hid_device(dev);
790 struct lg4ff_device_entry *entry;
791 struct lg_drv_data *drv_data;
795 drv_data = hid_get_drvdata(hid);
797 hid_err(hid, "Private driver data not found!\n");
801 entry = drv_data->device_props;
803 hid_err(hid, "Device properties not found!\n");
807 if (!entry->wdata.real_name) {
808 hid_err(hid, "NULL pointer to string\n");
812 for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
813 if (entry->wdata.alternate_modes & BIT(i)) {
814 /* Print tag and full name */
815 count += scnprintf(buf + count, PAGE_SIZE - count, "%s: %s",
816 lg4ff_alternate_modes[i].tag,
817 !lg4ff_alternate_modes[i].product_id ? entry->wdata.real_name : lg4ff_alternate_modes[i].name);
818 if (count >= PAGE_SIZE - 1)
821 /* Mark the currently active mode with an asterisk */
822 if (lg4ff_alternate_modes[i].product_id == entry->wdata.product_id ||
823 (lg4ff_alternate_modes[i].product_id == 0 && entry->wdata.product_id == entry->wdata.real_product_id))
824 count += scnprintf(buf + count, PAGE_SIZE - count, " *\n");
826 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
828 if (count >= PAGE_SIZE - 1)
836 static ssize_t lg4ff_alternate_modes_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
838 struct hid_device *hid = to_hid_device(dev);
839 struct lg4ff_device_entry *entry;
840 struct lg_drv_data *drv_data;
841 const struct lg4ff_compat_mode_switch *s;
842 u16 target_product_id = 0;
846 drv_data = hid_get_drvdata(hid);
848 hid_err(hid, "Private driver data not found!\n");
852 entry = drv_data->device_props;
854 hid_err(hid, "Device properties not found!\n");
858 /* Allow \n at the end of the input parameter */
859 lbuf = kasprintf(GFP_KERNEL, "%s", buf);
864 if (lbuf[i-1] == '\n') {
872 for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
873 const u16 mode_product_id = lg4ff_alternate_modes[i].product_id;
874 const char *tag = lg4ff_alternate_modes[i].tag;
876 if (entry->wdata.alternate_modes & BIT(i)) {
877 if (!strcmp(tag, lbuf)) {
878 if (!mode_product_id)
879 target_product_id = entry->wdata.real_product_id;
881 target_product_id = mode_product_id;
887 if (i == LG4FF_MODE_MAX_IDX) {
888 hid_info(hid, "Requested mode \"%s\" is not supported by the device\n", lbuf);
892 kfree(lbuf); /* Not needed anymore */
894 if (target_product_id == entry->wdata.product_id) /* Nothing to do */
897 /* Automatic switching has to be disabled for the switch to DF-EX mode to work correctly */
898 if (target_product_id == USB_DEVICE_ID_LOGITECH_WHEEL && !lg4ff_no_autoswitch) {
899 hid_info(hid, "\"%s\" cannot be switched to \"DF-EX\" mode. Load the \"hid_logitech\" module with \"lg4ff_no_autoswitch=1\" parameter set and try again\n",
900 entry->wdata.real_name);
904 /* Take care of hardware limitations */
905 if ((entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_DFP_WHEEL || entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_G25_WHEEL) &&
906 entry->wdata.product_id > target_product_id) {
907 hid_info(hid, "\"%s\" cannot be switched back into \"%s\" mode\n", entry->wdata.real_name, lg4ff_alternate_modes[i].name);
911 s = lg4ff_get_mode_switch_command(entry->wdata.real_product_id, target_product_id);
913 hid_err(hid, "Invalid target product ID %X\n", target_product_id);
917 ret = lg4ff_switch_compatibility_mode(hid, s);
918 return (ret == 0 ? count : ret);
920 static DEVICE_ATTR(alternate_modes, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_alternate_modes_show, lg4ff_alternate_modes_store);
922 static ssize_t lg4ff_combine_show(struct device *dev, struct device_attribute *attr,
925 struct hid_device *hid = to_hid_device(dev);
926 struct lg4ff_device_entry *entry;
927 struct lg_drv_data *drv_data;
930 drv_data = hid_get_drvdata(hid);
932 hid_err(hid, "Private driver data not found!\n");
936 entry = drv_data->device_props;
938 hid_err(hid, "Device properties not found!\n");
942 count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->wdata.combine);
946 static ssize_t lg4ff_combine_store(struct device *dev, struct device_attribute *attr,
947 const char *buf, size_t count)
949 struct hid_device *hid = to_hid_device(dev);
950 struct lg4ff_device_entry *entry;
951 struct lg_drv_data *drv_data;
952 u16 combine = simple_strtoul(buf, NULL, 10);
954 drv_data = hid_get_drvdata(hid);
956 hid_err(hid, "Private driver data not found!\n");
960 entry = drv_data->device_props;
962 hid_err(hid, "Device properties not found!\n");
969 entry->wdata.combine = combine;
972 static DEVICE_ATTR(combine_pedals, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_combine_show, lg4ff_combine_store);
974 /* Export the currently set range of the wheel */
975 static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr,
978 struct hid_device *hid = to_hid_device(dev);
979 struct lg4ff_device_entry *entry;
980 struct lg_drv_data *drv_data;
983 drv_data = hid_get_drvdata(hid);
985 hid_err(hid, "Private driver data not found!\n");
989 entry = drv_data->device_props;
991 hid_err(hid, "Device properties not found!\n");
995 count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->wdata.range);
999 /* Set range to user specified value, call appropriate function
1000 * according to the type of the wheel */
1001 static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr,
1002 const char *buf, size_t count)
1004 struct hid_device *hid = to_hid_device(dev);
1005 struct lg4ff_device_entry *entry;
1006 struct lg_drv_data *drv_data;
1007 u16 range = simple_strtoul(buf, NULL, 10);
1009 drv_data = hid_get_drvdata(hid);
1011 hid_err(hid, "Private driver data not found!\n");
1015 entry = drv_data->device_props;
1017 hid_err(hid, "Device properties not found!\n");
1022 range = entry->wdata.max_range;
1024 /* Check if the wheel supports range setting
1025 * and that the range is within limits for the wheel */
1026 if (entry->wdata.set_range && range >= entry->wdata.min_range && range <= entry->wdata.max_range) {
1027 entry->wdata.set_range(hid, range);
1028 entry->wdata.range = range;
1033 static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_range_show, lg4ff_range_store);
1035 static ssize_t lg4ff_real_id_show(struct device *dev, struct device_attribute *attr, char *buf)
1037 struct hid_device *hid = to_hid_device(dev);
1038 struct lg4ff_device_entry *entry;
1039 struct lg_drv_data *drv_data;
1042 drv_data = hid_get_drvdata(hid);
1044 hid_err(hid, "Private driver data not found!\n");
1048 entry = drv_data->device_props;
1050 hid_err(hid, "Device properties not found!\n");
1054 if (!entry->wdata.real_tag || !entry->wdata.real_name) {
1055 hid_err(hid, "NULL pointer to string\n");
1059 count = scnprintf(buf, PAGE_SIZE, "%s: %s\n", entry->wdata.real_tag, entry->wdata.real_name);
1063 static ssize_t lg4ff_real_id_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1065 /* Real ID is a read-only value */
1068 static DEVICE_ATTR(real_id, S_IRUGO, lg4ff_real_id_show, lg4ff_real_id_store);
1070 #ifdef CONFIG_LEDS_CLASS
1071 static void lg4ff_set_leds(struct hid_device *hid, u8 leds)
1073 struct lg_drv_data *drv_data;
1074 struct lg4ff_device_entry *entry;
1075 unsigned long flags;
1078 drv_data = hid_get_drvdata(hid);
1080 hid_err(hid, "Private driver data not found!\n");
1084 entry = drv_data->device_props;
1086 hid_err(hid, "Device properties not found!\n");
1089 value = entry->report->field[0]->value;
1091 spin_lock_irqsave(&entry->report_lock, flags);
1099 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
1100 spin_unlock_irqrestore(&entry->report_lock, flags);
1103 static void lg4ff_led_set_brightness(struct led_classdev *led_cdev,
1104 enum led_brightness value)
1106 struct device *dev = led_cdev->dev->parent;
1107 struct hid_device *hid = to_hid_device(dev);
1108 struct lg_drv_data *drv_data = hid_get_drvdata(hid);
1109 struct lg4ff_device_entry *entry;
1113 hid_err(hid, "Device data not found.");
1117 entry = drv_data->device_props;
1120 hid_err(hid, "Device properties not found.");
1124 for (i = 0; i < 5; i++) {
1125 if (led_cdev != entry->wdata.led[i])
1127 state = (entry->wdata.led_state >> i) & 1;
1128 if (value == LED_OFF && state) {
1129 entry->wdata.led_state &= ~(1 << i);
1130 lg4ff_set_leds(hid, entry->wdata.led_state);
1131 } else if (value != LED_OFF && !state) {
1132 entry->wdata.led_state |= 1 << i;
1133 lg4ff_set_leds(hid, entry->wdata.led_state);
1139 static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev)
1141 struct device *dev = led_cdev->dev->parent;
1142 struct hid_device *hid = to_hid_device(dev);
1143 struct lg_drv_data *drv_data = hid_get_drvdata(hid);
1144 struct lg4ff_device_entry *entry;
1148 hid_err(hid, "Device data not found.");
1152 entry = drv_data->device_props;
1155 hid_err(hid, "Device properties not found.");
1159 for (i = 0; i < 5; i++)
1160 if (led_cdev == entry->wdata.led[i]) {
1161 value = (entry->wdata.led_state >> i) & 1;
1165 return value ? LED_FULL : LED_OFF;
1169 static u16 lg4ff_identify_multimode_wheel(struct hid_device *hid, const u16 reported_product_id, const u16 bcdDevice)
1174 /* identify current mode from USB PID */
1175 for (i = 1; i < ARRAY_SIZE(lg4ff_alternate_modes); i++) {
1176 dbg_hid("Testing whether PID is %X\n", lg4ff_alternate_modes[i].product_id);
1177 if (reported_product_id == lg4ff_alternate_modes[i].product_id)
1181 if (i == ARRAY_SIZE(lg4ff_alternate_modes))
1184 current_mode = BIT(i);
1186 for (i = 0; i < ARRAY_SIZE(lg4ff_main_checklist); i++) {
1187 const u16 mask = lg4ff_main_checklist[i]->mask;
1188 const u16 result = lg4ff_main_checklist[i]->result;
1189 const u16 real_product_id = lg4ff_main_checklist[i]->real_product_id;
1191 if ((current_mode & lg4ff_main_checklist[i]->modes) && \
1192 (bcdDevice & mask) == result) {
1193 dbg_hid("Found wheel with real PID %X whose reported PID is %X\n", real_product_id, reported_product_id);
1194 return real_product_id;
1198 /* No match found. This is either Driving Force or an unknown
1199 * wheel model, do not touch it */
1200 dbg_hid("Wheel with bcdDevice %X was not recognized as multimode wheel, leaving in its current mode\n", bcdDevice);
1204 static int lg4ff_handle_multimode_wheel(struct hid_device *hid, u16 *real_product_id, const u16 bcdDevice)
1206 const u16 reported_product_id = hid->product;
1209 *real_product_id = lg4ff_identify_multimode_wheel(hid, reported_product_id, bcdDevice);
1210 /* Probed wheel is not a multimode wheel */
1211 if (!*real_product_id) {
1212 *real_product_id = reported_product_id;
1213 dbg_hid("Wheel is not a multimode wheel\n");
1214 return LG4FF_MMODE_NOT_MULTIMODE;
1217 /* Switch from "Driving Force" mode to native mode automatically.
1218 * Otherwise keep the wheel in its current mode */
1219 if (reported_product_id == USB_DEVICE_ID_LOGITECH_WHEEL &&
1220 reported_product_id != *real_product_id &&
1221 !lg4ff_no_autoswitch) {
1222 const struct lg4ff_compat_mode_switch *s = lg4ff_get_mode_switch_command(*real_product_id, *real_product_id);
1225 hid_err(hid, "Invalid product id %X\n", *real_product_id);
1226 return LG4FF_MMODE_NOT_MULTIMODE;
1229 ret = lg4ff_switch_compatibility_mode(hid, s);
1231 /* Wheel could not have been switched to native mode,
1232 * leave it in "Driving Force" mode and continue */
1233 hid_err(hid, "Unable to switch wheel mode, errno %d\n", ret);
1234 return LG4FF_MMODE_IS_MULTIMODE;
1236 return LG4FF_MMODE_SWITCHED;
1239 return LG4FF_MMODE_IS_MULTIMODE;
1243 int lg4ff_init(struct hid_device *hid)
1245 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1246 struct input_dev *dev = hidinput->input;
1247 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
1248 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
1249 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
1250 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
1251 const struct lg4ff_multimode_wheel *mmode_wheel = NULL;
1252 struct lg4ff_device_entry *entry;
1253 struct lg_drv_data *drv_data;
1255 int mmode_ret, mmode_idx = -1;
1256 u16 real_product_id;
1258 /* Check that the report looks ok */
1259 if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
1262 drv_data = hid_get_drvdata(hid);
1264 hid_err(hid, "Cannot add device, private driver data not allocated\n");
1267 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1270 spin_lock_init(&entry->report_lock);
1271 entry->report = report;
1272 drv_data->device_props = entry;
1274 /* Check if a multimode wheel has been connected and
1275 * handle it appropriately */
1276 mmode_ret = lg4ff_handle_multimode_wheel(hid, &real_product_id, bcdDevice);
1278 /* Wheel has been told to switch to native mode. There is no point in going on
1279 * with the initialization as the wheel will do a USB reset when it switches mode
1281 if (mmode_ret == LG4FF_MMODE_SWITCHED)
1283 else if (mmode_ret < 0) {
1284 hid_err(hid, "Unable to switch device mode during initialization, errno %d\n", mmode_ret);
1289 /* Check what wheel has been connected */
1290 for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
1291 if (hid->product == lg4ff_devices[i].product_id) {
1292 dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
1297 if (i == ARRAY_SIZE(lg4ff_devices)) {
1298 hid_err(hid, "This device is flagged to be handled by the lg4ff module but this module does not know how to handle it. "
1299 "Please report this as a bug to LKML, Simon Wood <simon@mungewell.org> or "
1300 "Michal Maly <madcatxster@devoid-pointer.net>\n");
1305 if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1306 for (mmode_idx = 0; mmode_idx < ARRAY_SIZE(lg4ff_multimode_wheels); mmode_idx++) {
1307 if (real_product_id == lg4ff_multimode_wheels[mmode_idx].product_id)
1311 if (mmode_idx == ARRAY_SIZE(lg4ff_multimode_wheels)) {
1312 hid_err(hid, "Device product ID %X is not listed as a multimode wheel", real_product_id);
1318 /* Set supported force feedback capabilities */
1319 for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
1320 set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
1322 error = input_ff_create_memless(dev, NULL, lg4ff_play);
1327 /* Initialize device properties */
1328 if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1329 BUG_ON(mmode_idx == -1);
1330 mmode_wheel = &lg4ff_multimode_wheels[mmode_idx];
1332 lg4ff_init_wheel_data(&entry->wdata, &lg4ff_devices[i], mmode_wheel, real_product_id);
1334 /* Check if autocentering is available and
1335 * set the centering force to zero by default */
1336 if (test_bit(FF_AUTOCENTER, dev->ffbit)) {
1337 /* Formula Force EX expects different autocentering command */
1338 if ((bcdDevice >> 8) == LG4FF_FFEX_REV_MAJ &&
1339 (bcdDevice & 0xff) == LG4FF_FFEX_REV_MIN)
1340 dev->ff->set_autocenter = lg4ff_set_autocenter_ffex;
1342 dev->ff->set_autocenter = lg4ff_set_autocenter_default;
1344 dev->ff->set_autocenter(dev, 0);
1347 /* Create sysfs interface */
1348 error = device_create_file(&hid->dev, &dev_attr_combine_pedals);
1350 hid_warn(hid, "Unable to create sysfs interface for \"combine\", errno %d\n", error);
1351 error = device_create_file(&hid->dev, &dev_attr_range);
1353 hid_warn(hid, "Unable to create sysfs interface for \"range\", errno %d\n", error);
1354 if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1355 error = device_create_file(&hid->dev, &dev_attr_real_id);
1357 hid_warn(hid, "Unable to create sysfs interface for \"real_id\", errno %d\n", error);
1358 error = device_create_file(&hid->dev, &dev_attr_alternate_modes);
1360 hid_warn(hid, "Unable to create sysfs interface for \"alternate_modes\", errno %d\n", error);
1362 dbg_hid("sysfs interface created\n");
1364 /* Set the maximum range to start with */
1365 entry->wdata.range = entry->wdata.max_range;
1366 if (entry->wdata.set_range)
1367 entry->wdata.set_range(hid, entry->wdata.range);
1369 #ifdef CONFIG_LEDS_CLASS
1370 /* register led subsystem - G27/G29 only */
1371 entry->wdata.led_state = 0;
1372 for (j = 0; j < 5; j++)
1373 entry->wdata.led[j] = NULL;
1375 if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL ||
1376 lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G29_WHEEL) {
1377 struct led_classdev *led;
1381 lg4ff_set_leds(hid, 0);
1383 name_sz = strlen(dev_name(&hid->dev)) + 8;
1385 for (j = 0; j < 5; j++) {
1386 led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
1388 hid_err(hid, "can't allocate memory for LED %d\n", j);
1392 name = (void *)(&led[1]);
1393 snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1);
1395 led->brightness = 0;
1396 led->max_brightness = 1;
1397 led->brightness_get = lg4ff_led_get_brightness;
1398 led->brightness_set = lg4ff_led_set_brightness;
1400 entry->wdata.led[j] = led;
1401 error = led_classdev_register(&hid->dev, led);
1404 hid_err(hid, "failed to register LED %d. Aborting.\n", j);
1406 /* Deregister LEDs (if any) */
1407 for (j = 0; j < 5; j++) {
1408 led = entry->wdata.led[j];
1409 entry->wdata.led[j] = NULL;
1412 led_classdev_unregister(led);
1415 goto out; /* Let the driver continue without LEDs */
1421 hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n");
1425 drv_data->device_props = NULL;
1430 int lg4ff_deinit(struct hid_device *hid)
1432 struct lg4ff_device_entry *entry;
1433 struct lg_drv_data *drv_data;
1435 drv_data = hid_get_drvdata(hid);
1437 hid_err(hid, "Error while deinitializing device, no private driver data.\n");
1440 entry = drv_data->device_props;
1442 goto out; /* Nothing more to do */
1444 /* Multimode devices will have at least the "MODE_NATIVE" bit set */
1445 if (entry->wdata.alternate_modes) {
1446 device_remove_file(&hid->dev, &dev_attr_real_id);
1447 device_remove_file(&hid->dev, &dev_attr_alternate_modes);
1450 device_remove_file(&hid->dev, &dev_attr_combine_pedals);
1451 device_remove_file(&hid->dev, &dev_attr_range);
1452 #ifdef CONFIG_LEDS_CLASS
1455 struct led_classdev *led;
1457 /* Deregister LEDs (if any) */
1458 for (j = 0; j < 5; j++) {
1460 led = entry->wdata.led[j];
1461 entry->wdata.led[j] = NULL;
1464 led_classdev_unregister(led);
1470 drv_data->device_props = NULL;
1474 dbg_hid("Device successfully unregistered\n");