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"
35 #define to_hid_device(pdev) container_of(pdev, struct hid_device, dev)
37 #define LG4FF_MMODE_IS_MULTIMODE 0
38 #define LG4FF_MMODE_SWITCHED 1
39 #define LG4FF_MMODE_NOT_MULTIMODE 2
41 #define LG4FF_MODE_NATIVE_IDX 0
42 #define LG4FF_MODE_DFEX_IDX 1
43 #define LG4FF_MODE_DFP_IDX 2
44 #define LG4FF_MODE_G25_IDX 3
45 #define LG4FF_MODE_DFGT_IDX 4
46 #define LG4FF_MODE_G27_IDX 5
47 #define LG4FF_MODE_MAX_IDX 6
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)
56 #define LG4FF_DFEX_TAG "DF-EX"
57 #define LG4FF_DFEX_NAME "Driving Force / Formula EX"
58 #define LG4FF_DFP_TAG "DFP"
59 #define LG4FF_DFP_NAME "Driving Force Pro"
60 #define LG4FF_G25_TAG "G25"
61 #define LG4FF_G25_NAME "G25 Racing Wheel"
62 #define LG4FF_G27_TAG "G27"
63 #define LG4FF_G27_NAME "G27 Racing Wheel"
64 #define LG4FF_DFGT_TAG "DFGT"
65 #define LG4FF_DFGT_NAME "Driving Force GT"
67 #define LG4FF_FFEX_REV_MAJ 0x21
68 #define LG4FF_FFEX_REV_MIN 0x00
70 static void hid_lg4ff_set_range_dfp(struct hid_device *hid, u16 range);
71 static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range);
73 struct lg4ff_device_entry {
78 #ifdef CONFIG_LEDS_CLASS
80 struct led_classdev *led[5];
84 const char *real_name;
86 struct list_head list;
87 void (*set_range)(struct hid_device *hid, u16 range);
90 static const signed short lg4ff_wheel_effects[] = {
97 const __u32 product_id;
98 const signed short *ff_effects;
99 const __u16 min_range;
100 const __u16 max_range;
101 void (*set_range)(struct hid_device *hid, u16 range);
104 struct lg4ff_compat_mode_switch {
105 const __u8 cmd_count; /* Number of commands to send */
109 struct lg4ff_wheel_ident_info {
112 const u16 real_product_id;
115 struct lg4ff_wheel_ident_checklist {
117 const struct lg4ff_wheel_ident_info *models[];
120 struct lg4ff_multimode_wheel {
121 const u16 product_id;
122 const u32 alternate_modes;
123 const char *real_tag;
124 const char *real_name;
127 struct lg4ff_alternate_mode {
128 const u16 product_id;
133 static const struct lg4ff_wheel lg4ff_devices[] = {
134 {USB_DEVICE_ID_LOGITECH_WHEEL, lg4ff_wheel_effects, 40, 270, NULL},
135 {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL, lg4ff_wheel_effects, 40, 270, NULL},
136 {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_dfp},
137 {USB_DEVICE_ID_LOGITECH_G25_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
138 {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
139 {USB_DEVICE_ID_LOGITECH_G27_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
140 {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL},
141 {USB_DEVICE_ID_LOGITECH_WII_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}
144 static const struct lg4ff_multimode_wheel lg4ff_multimode_wheels[] = {
145 {USB_DEVICE_ID_LOGITECH_DFP_WHEEL,
146 LG4FF_MODE_NATIVE | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
147 LG4FF_DFP_TAG, LG4FF_DFP_NAME},
148 {USB_DEVICE_ID_LOGITECH_G25_WHEEL,
149 LG4FF_MODE_NATIVE | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
150 LG4FF_G25_TAG, LG4FF_G25_NAME},
151 {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL,
152 LG4FF_MODE_NATIVE | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
153 LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
154 {USB_DEVICE_ID_LOGITECH_G27_WHEEL,
155 LG4FF_MODE_NATIVE | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
156 LG4FF_G27_TAG, LG4FF_G27_NAME},
159 static const struct lg4ff_alternate_mode lg4ff_alternate_modes[] = {
160 [LG4FF_MODE_NATIVE_IDX] = {0, "native", ""},
161 [LG4FF_MODE_DFEX_IDX] = {USB_DEVICE_ID_LOGITECH_WHEEL, LG4FF_DFEX_TAG, LG4FF_DFEX_NAME},
162 [LG4FF_MODE_DFP_IDX] = {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, LG4FF_DFP_TAG, LG4FF_DFP_NAME},
163 [LG4FF_MODE_G25_IDX] = {USB_DEVICE_ID_LOGITECH_G25_WHEEL, LG4FF_G25_TAG, LG4FF_G25_NAME},
164 [LG4FF_MODE_DFGT_IDX] = {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
165 [LG4FF_MODE_G27_IDX] = {USB_DEVICE_ID_LOGITECH_G27_WHEEL, LG4FF_G27_TAG, LG4FF_G27_NAME}
168 /* Multimode wheel identificators */
169 static const struct lg4ff_wheel_ident_info lg4ff_dfp_ident_info = {
172 USB_DEVICE_ID_LOGITECH_DFP_WHEEL
175 static const struct lg4ff_wheel_ident_info lg4ff_g25_ident_info = {
178 USB_DEVICE_ID_LOGITECH_G25_WHEEL
181 static const struct lg4ff_wheel_ident_info lg4ff_g27_ident_info = {
184 USB_DEVICE_ID_LOGITECH_G27_WHEEL
187 static const struct lg4ff_wheel_ident_info lg4ff_dfgt_ident_info = {
190 USB_DEVICE_ID_LOGITECH_DFGT_WHEEL
193 /* Multimode wheel identification checklists */
194 static const struct lg4ff_wheel_ident_checklist lg4ff_main_checklist = {
196 {&lg4ff_dfgt_ident_info,
197 &lg4ff_g27_ident_info,
198 &lg4ff_g25_ident_info,
199 &lg4ff_dfp_ident_info}
202 /* Compatibility mode switching commands */
203 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_dfp = {
205 {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
208 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_dfgt = {
210 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */
211 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* 2nd command */
214 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_g25 = {
216 {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
219 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_g27 = {
221 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */
222 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* 2nd command */
225 /* Recalculates X axis value accordingly to currently selected range */
226 static __s32 lg4ff_adjust_dfp_x_axis(__s32 value, __u16 range)
233 else if (range == 200)
235 else if (range < 200)
240 new_value = 8192 + mult_frac(value - 8192, max_range, range);
243 else if (new_value > 16383)
249 int lg4ff_adjust_input_event(struct hid_device *hid, struct hid_field *field,
250 struct hid_usage *usage, __s32 value, struct lg_drv_data *drv_data)
252 struct lg4ff_device_entry *entry = drv_data->device_props;
256 hid_err(hid, "Device properties not found");
260 switch (entry->product_id) {
261 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
262 switch (usage->code) {
264 new_value = lg4ff_adjust_dfp_x_axis(value, entry->range);
265 input_event(field->hidinput->input, usage->type, usage->code, new_value);
275 static int hid_lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
277 struct hid_device *hid = input_get_drvdata(dev);
278 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
279 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
280 __s32 *value = report->field[0]->value;
283 #define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0)
285 switch (effect->type) {
287 x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */
291 /* De-activate force in slot-1*/
300 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
304 value[0] = 0x11; /* Slot 1 */
312 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
318 /* Sends default autocentering command compatible with
319 * all wheels except Formula Force EX */
320 static void hid_lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude)
322 struct hid_device *hid = input_get_drvdata(dev);
323 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
324 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
325 __s32 *value = report->field[0]->value;
326 __u32 expand_a, expand_b;
327 struct lg4ff_device_entry *entry;
328 struct lg_drv_data *drv_data;
330 drv_data = hid_get_drvdata(hid);
332 hid_err(hid, "Private driver data not found!\n");
336 entry = drv_data->device_props;
338 hid_err(hid, "Device properties not found!\n");
342 /* De-activate Auto-Center */
343 if (magnitude == 0) {
352 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
356 if (magnitude <= 0xaaaa) {
357 expand_a = 0x0c * magnitude;
358 expand_b = 0x80 * magnitude;
360 expand_a = (0x0c * 0xaaaa) + 0x06 * (magnitude - 0xaaaa);
361 expand_b = (0x80 * 0xaaaa) + 0xff * (magnitude - 0xaaaa);
364 /* Adjust for non-MOMO wheels */
365 switch (entry->product_id) {
366 case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL:
367 case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2:
370 expand_a = expand_a >> 1;
376 value[2] = expand_a / 0xaaaa;
377 value[3] = expand_a / 0xaaaa;
378 value[4] = expand_b / 0xaaaa;
382 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
384 /* Activate Auto-Center */
393 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
396 /* Sends autocentering command compatible with Formula Force EX */
397 static void hid_lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude)
399 struct hid_device *hid = input_get_drvdata(dev);
400 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
401 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
402 __s32 *value = report->field[0]->value;
403 magnitude = magnitude * 90 / 65535;
407 value[2] = magnitude >> 14;
408 value[3] = magnitude >> 14;
409 value[4] = magnitude;
413 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
416 /* Sends command to set range compatible with G25/G27/Driving Force GT */
417 static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range)
419 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
420 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
421 __s32 *value = report->field[0]->value;
423 dbg_hid("G25/G27/DFGT: setting range to %u\n", range);
427 value[2] = range & 0x00ff;
428 value[3] = (range & 0xff00) >> 8;
433 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
436 /* Sends commands to set range compatible with Driving Force Pro wheel */
437 static void hid_lg4ff_set_range_dfp(struct hid_device *hid, __u16 range)
439 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
440 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
441 int start_left, start_right, full_range;
442 __s32 *value = report->field[0]->value;
444 dbg_hid("Driving Force Pro: setting range to %u\n", range);
446 /* Prepare "coarse" limit command */
448 value[1] = 0x00; /* Set later */
456 report->field[0]->value[1] = 0x03;
459 report->field[0]->value[1] = 0x02;
462 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
464 /* Prepare "fine" limit command */
473 if (range == 200 || range == 900) { /* Do not apply any fine limit */
474 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
478 /* Construct fine limit command */
479 start_left = (((full_range - range + 1) * 2047) / full_range);
480 start_right = 0xfff - start_left;
482 value[2] = start_left >> 4;
483 value[3] = start_right >> 4;
485 value[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
488 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
491 static int lg4ff_switch_compatibility_mode(struct hid_device *hid, const struct lg4ff_compat_mode_switch *s)
493 struct usb_device *usbdev = hid_to_usb_dev(hid);
494 struct usbhid_device *usbhid = hid->driver_data;
497 for (i = 0; i < s->cmd_count; i++) {
501 memcpy(data, s->cmd + (7*i), 7);
502 ret = usb_interrupt_msg(usbdev, usbhid->urbout->pipe, data, 7, &xferd, USB_CTRL_SET_TIMEOUT);
509 static ssize_t lg4ff_alternate_modes_show(struct device *dev, struct device_attribute *attr, char *buf)
511 struct hid_device *hid = to_hid_device(dev);
512 struct lg4ff_device_entry *entry;
513 struct lg_drv_data *drv_data;
517 drv_data = hid_get_drvdata(hid);
519 hid_err(hid, "Private driver data not found!\n");
523 entry = drv_data->device_props;
525 hid_err(hid, "Device properties not found!\n");
529 if (!entry->real_name) {
530 hid_err(hid, "NULL pointer to string\n");
534 for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
535 if (entry->alternate_modes & BIT(i)) {
536 /* Print tag and full name */
537 count += scnprintf(buf + count, PAGE_SIZE - count, "%s: %s",
538 lg4ff_alternate_modes[i].tag,
539 !lg4ff_alternate_modes[i].product_id ? entry->real_name : lg4ff_alternate_modes[i].name);
540 if (count >= PAGE_SIZE - 1)
543 /* Mark the currently active mode with an asterisk */
544 if (lg4ff_alternate_modes[i].product_id == entry->product_id ||
545 (lg4ff_alternate_modes[i].product_id == 0 && entry->product_id == entry->real_product_id))
546 count += scnprintf(buf + count, PAGE_SIZE - count, " *\n");
548 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
550 if (count >= PAGE_SIZE - 1)
558 static ssize_t lg4ff_alternate_modes_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
562 static DEVICE_ATTR(alternate_modes, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_alternate_modes_show, lg4ff_alternate_modes_store);
564 /* Read current range and display it in terminal */
565 static ssize_t range_show(struct device *dev, struct device_attribute *attr,
568 struct hid_device *hid = to_hid_device(dev);
569 struct lg4ff_device_entry *entry;
570 struct lg_drv_data *drv_data;
573 drv_data = hid_get_drvdata(hid);
575 hid_err(hid, "Private driver data not found!\n");
579 entry = drv_data->device_props;
581 hid_err(hid, "Device properties not found!\n");
585 count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->range);
589 /* Set range to user specified value, call appropriate function
590 * according to the type of the wheel */
591 static ssize_t range_store(struct device *dev, struct device_attribute *attr,
592 const char *buf, size_t count)
594 struct hid_device *hid = to_hid_device(dev);
595 struct lg4ff_device_entry *entry;
596 struct lg_drv_data *drv_data;
597 __u16 range = simple_strtoul(buf, NULL, 10);
599 drv_data = hid_get_drvdata(hid);
601 hid_err(hid, "Private driver data not found!\n");
605 entry = drv_data->device_props;
607 hid_err(hid, "Device properties not found!\n");
612 range = entry->max_range;
614 /* Check if the wheel supports range setting
615 * and that the range is within limits for the wheel */
616 if (entry->set_range != NULL && range >= entry->min_range && range <= entry->max_range) {
617 entry->set_range(hid, range);
618 entry->range = range;
623 static DEVICE_ATTR_RW(range);
625 static ssize_t lg4ff_real_id_show(struct device *dev, struct device_attribute *attr, char *buf)
627 struct hid_device *hid = to_hid_device(dev);
628 struct lg4ff_device_entry *entry;
629 struct lg_drv_data *drv_data;
632 drv_data = hid_get_drvdata(hid);
634 hid_err(hid, "Private driver data not found!\n");
638 entry = drv_data->device_props;
640 hid_err(hid, "Device properties not found!\n");
644 if (!entry->real_tag || !entry->real_name) {
645 hid_err(hid, "NULL pointer to string\n");
649 count = scnprintf(buf, PAGE_SIZE, "%s: %s\n", entry->real_tag, entry->real_name);
653 static ssize_t lg4ff_real_id_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
655 /* Real ID is a read-only value */
658 static DEVICE_ATTR(real_id, S_IRUGO, lg4ff_real_id_show, lg4ff_real_id_store);
660 #ifdef CONFIG_LEDS_CLASS
661 static void lg4ff_set_leds(struct hid_device *hid, __u8 leds)
663 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
664 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
665 __s32 *value = report->field[0]->value;
674 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
677 static void lg4ff_led_set_brightness(struct led_classdev *led_cdev,
678 enum led_brightness value)
680 struct device *dev = led_cdev->dev->parent;
681 struct hid_device *hid = container_of(dev, struct hid_device, dev);
682 struct lg_drv_data *drv_data = hid_get_drvdata(hid);
683 struct lg4ff_device_entry *entry;
687 hid_err(hid, "Device data not found.");
691 entry = (struct lg4ff_device_entry *)drv_data->device_props;
694 hid_err(hid, "Device properties not found.");
698 for (i = 0; i < 5; i++) {
699 if (led_cdev != entry->led[i])
701 state = (entry->led_state >> i) & 1;
702 if (value == LED_OFF && state) {
703 entry->led_state &= ~(1 << i);
704 lg4ff_set_leds(hid, entry->led_state);
705 } else if (value != LED_OFF && !state) {
706 entry->led_state |= 1 << i;
707 lg4ff_set_leds(hid, entry->led_state);
713 static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev)
715 struct device *dev = led_cdev->dev->parent;
716 struct hid_device *hid = container_of(dev, struct hid_device, dev);
717 struct lg_drv_data *drv_data = hid_get_drvdata(hid);
718 struct lg4ff_device_entry *entry;
722 hid_err(hid, "Device data not found.");
726 entry = (struct lg4ff_device_entry *)drv_data->device_props;
729 hid_err(hid, "Device properties not found.");
733 for (i = 0; i < 5; i++)
734 if (led_cdev == entry->led[i]) {
735 value = (entry->led_state >> i) & 1;
739 return value ? LED_FULL : LED_OFF;
743 static u16 lg4ff_identify_multimode_wheel(struct hid_device *hid, const u16 reported_product_id, const u16 bcdDevice)
745 const struct lg4ff_wheel_ident_checklist *checklist;
746 int i, from_idx, to_idx;
748 switch (reported_product_id) {
749 case USB_DEVICE_ID_LOGITECH_WHEEL:
750 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
751 checklist = &lg4ff_main_checklist;
753 to_idx = checklist->count - 1;
755 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
756 checklist = &lg4ff_main_checklist;
758 to_idx = checklist->count - 2; /* End identity check at G25 */
760 case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
761 checklist = &lg4ff_main_checklist;
762 from_idx = 1; /* Start identity check at G27 */
763 to_idx = checklist->count - 3; /* End identity check at G27 */
765 case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
766 checklist = &lg4ff_main_checklist;
768 to_idx = checklist->count - 4; /* End identity check at DFGT */
774 for (i = from_idx; i <= to_idx; i++) {
775 const u16 mask = checklist->models[i]->mask;
776 const u16 result = checklist->models[i]->result;
777 const u16 real_product_id = checklist->models[i]->real_product_id;
779 if ((bcdDevice & mask) == result) {
780 dbg_hid("Found wheel with real PID %X whose reported PID is %X\n", real_product_id, reported_product_id);
781 return real_product_id;
785 /* No match found. This is an unknown wheel model, do not touch it */
786 dbg_hid("Wheel with bcdDevice %X was not recognized as multimode wheel, leaving in its current mode\n", bcdDevice);
790 static int lg4ff_handle_multimode_wheel(struct hid_device *hid, u16 *real_product_id, const u16 bcdDevice)
792 const u16 reported_product_id = hid->product;
795 *real_product_id = lg4ff_identify_multimode_wheel(hid, reported_product_id, bcdDevice);
796 /* Probed wheel is not a multimode wheel */
797 if (!*real_product_id) {
798 *real_product_id = reported_product_id;
799 dbg_hid("Wheel is not a multimode wheel\n");
800 return LG4FF_MMODE_NOT_MULTIMODE;
803 /* Switch from "Driving Force" mode to native mode automatically.
804 * Otherwise keep the wheel in its current mode */
805 if (reported_product_id == USB_DEVICE_ID_LOGITECH_WHEEL &&
806 reported_product_id != *real_product_id) {
807 const struct lg4ff_compat_mode_switch *s;
809 switch (*real_product_id) {
810 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
811 s = &lg4ff_mode_switch_dfp;
813 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
814 s = &lg4ff_mode_switch_g25;
816 case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
817 s = &lg4ff_mode_switch_g27;
819 case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
820 s = &lg4ff_mode_switch_dfgt;
823 hid_err(hid, "Invalid product id %X\n", *real_product_id);
824 return LG4FF_MMODE_NOT_MULTIMODE;
827 ret = lg4ff_switch_compatibility_mode(hid, s);
829 /* Wheel could not have been switched to native mode,
830 * leave it in "Driving Force" mode and continue */
831 hid_err(hid, "Unable to switch wheel mode, errno %d\n", ret);
832 return LG4FF_MMODE_IS_MULTIMODE;
834 return LG4FF_MMODE_SWITCHED;
837 return LG4FF_MMODE_IS_MULTIMODE;
841 int lg4ff_init(struct hid_device *hid)
843 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
844 struct input_dev *dev = hidinput->input;
845 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
846 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
847 struct lg4ff_device_entry *entry;
848 struct lg_drv_data *drv_data;
850 int mmode_ret, mmode_idx = -1;
853 /* Check that the report looks ok */
854 if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
857 /* Check if a multimode wheel has been connected and
858 * handle it appropriately */
859 mmode_ret = lg4ff_handle_multimode_wheel(hid, &real_product_id, bcdDevice);
861 /* Wheel has been told to switch to native mode. There is no point in going on
862 * with the initialization as the wheel will do a USB reset when it switches mode
864 if (mmode_ret == LG4FF_MMODE_SWITCHED)
867 /* Check what wheel has been connected */
868 for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
869 if (hid->product == lg4ff_devices[i].product_id) {
870 dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
875 if (i == ARRAY_SIZE(lg4ff_devices)) {
876 hid_err(hid, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to"
877 "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n");
881 if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
882 for (mmode_idx = 0; mmode_idx < ARRAY_SIZE(lg4ff_multimode_wheels); mmode_idx++) {
883 if (real_product_id == lg4ff_multimode_wheels[mmode_idx].product_id)
887 if (mmode_idx == ARRAY_SIZE(lg4ff_multimode_wheels)) {
888 hid_err(hid, "Device product ID %X is not listed as a multimode wheel", real_product_id);
893 /* Set supported force feedback capabilities */
894 for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
895 set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
897 error = input_ff_create_memless(dev, NULL, hid_lg4ff_play);
902 /* Get private driver data */
903 drv_data = hid_get_drvdata(hid);
905 hid_err(hid, "Cannot add device, private driver data not allocated\n");
909 /* Initialize device properties */
910 entry = kzalloc(sizeof(struct lg4ff_device_entry), GFP_KERNEL);
912 hid_err(hid, "Cannot add device, insufficient memory to allocate device properties.\n");
915 drv_data->device_props = entry;
917 entry->product_id = lg4ff_devices[i].product_id;
918 entry->real_product_id = real_product_id;
919 entry->min_range = lg4ff_devices[i].min_range;
920 entry->max_range = lg4ff_devices[i].max_range;
921 entry->set_range = lg4ff_devices[i].set_range;
922 if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
923 BUG_ON(mmode_idx == -1);
924 entry->alternate_modes = lg4ff_multimode_wheels[mmode_idx].alternate_modes;
925 entry->real_tag = lg4ff_multimode_wheels[mmode_idx].real_tag;
926 entry->real_name = lg4ff_multimode_wheels[mmode_idx].real_name;
929 /* Check if autocentering is available and
930 * set the centering force to zero by default */
931 if (test_bit(FF_AUTOCENTER, dev->ffbit)) {
932 /* Formula Force EX expects different autocentering command */
933 if ((bcdDevice >> 8) == LG4FF_FFEX_REV_MAJ &&
934 (bcdDevice & 0xff) == LG4FF_FFEX_REV_MIN)
935 dev->ff->set_autocenter = hid_lg4ff_set_autocenter_ffex;
937 dev->ff->set_autocenter = hid_lg4ff_set_autocenter_default;
939 dev->ff->set_autocenter(dev, 0);
942 /* Create sysfs interface */
943 error = device_create_file(&hid->dev, &dev_attr_range);
946 if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
947 error = device_create_file(&hid->dev, &dev_attr_real_id);
950 error = device_create_file(&hid->dev, &dev_attr_alternate_modes);
954 dbg_hid("sysfs interface created\n");
956 /* Set the maximum range to start with */
957 entry->range = entry->max_range;
958 if (entry->set_range != NULL)
959 entry->set_range(hid, entry->range);
961 #ifdef CONFIG_LEDS_CLASS
962 /* register led subsystem - G27 only */
963 entry->led_state = 0;
964 for (j = 0; j < 5; j++)
965 entry->led[j] = NULL;
967 if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL) {
968 struct led_classdev *led;
972 lg4ff_set_leds(hid, 0);
974 name_sz = strlen(dev_name(&hid->dev)) + 8;
976 for (j = 0; j < 5; j++) {
977 led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
979 hid_err(hid, "can't allocate memory for LED %d\n", j);
983 name = (void *)(&led[1]);
984 snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1);
987 led->max_brightness = 1;
988 led->brightness_get = lg4ff_led_get_brightness;
989 led->brightness_set = lg4ff_led_set_brightness;
992 error = led_classdev_register(&hid->dev, led);
995 hid_err(hid, "failed to register LED %d. Aborting.\n", j);
997 /* Deregister LEDs (if any) */
998 for (j = 0; j < 5; j++) {
1000 entry->led[j] = NULL;
1003 led_classdev_unregister(led);
1006 goto out; /* Let the driver continue without LEDs */
1012 hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n");
1016 int lg4ff_deinit(struct hid_device *hid)
1018 struct lg4ff_device_entry *entry;
1019 struct lg_drv_data *drv_data;
1021 drv_data = hid_get_drvdata(hid);
1023 hid_err(hid, "Error while deinitializing device, no private driver data.\n");
1026 entry = drv_data->device_props;
1028 goto out; /* Nothing more to do */
1030 device_remove_file(&hid->dev, &dev_attr_range);
1032 /* Multimode devices will have at least the "MODE_NATIVE" bit set */
1033 if (entry->alternate_modes) {
1034 device_remove_file(&hid->dev, &dev_attr_real_id);
1035 device_remove_file(&hid->dev, &dev_attr_alternate_modes);
1038 #ifdef CONFIG_LEDS_CLASS
1041 struct led_classdev *led;
1043 /* Deregister LEDs (if any) */
1044 for (j = 0; j < 5; j++) {
1046 led = entry->led[j];
1047 entry->led[j] = NULL;
1050 led_classdev_unregister(led);
1056 /* Deallocate memory */
1060 dbg_hid("Device successfully unregistered\n");