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 to_hid_device(pdev) container_of(pdev, struct hid_device, dev)
38 #define LG4FF_MMODE_IS_MULTIMODE 0
39 #define LG4FF_MMODE_SWITCHED 1
40 #define LG4FF_MMODE_NOT_MULTIMODE 2
42 #define LG4FF_MODE_NATIVE_IDX 0
43 #define LG4FF_MODE_DFEX_IDX 1
44 #define LG4FF_MODE_DFP_IDX 2
45 #define LG4FF_MODE_G25_IDX 3
46 #define LG4FF_MODE_DFGT_IDX 4
47 #define LG4FF_MODE_G27_IDX 5
48 #define LG4FF_MODE_MAX_IDX 6
50 #define LG4FF_MODE_NATIVE BIT(LG4FF_MODE_NATIVE_IDX)
51 #define LG4FF_MODE_DFEX BIT(LG4FF_MODE_DFEX_IDX)
52 #define LG4FF_MODE_DFP BIT(LG4FF_MODE_DFP_IDX)
53 #define LG4FF_MODE_G25 BIT(LG4FF_MODE_G25_IDX)
54 #define LG4FF_MODE_DFGT BIT(LG4FF_MODE_DFGT_IDX)
55 #define LG4FF_MODE_G27 BIT(LG4FF_MODE_G27_IDX)
57 #define LG4FF_DFEX_TAG "DF-EX"
58 #define LG4FF_DFEX_NAME "Driving Force / Formula EX"
59 #define LG4FF_DFP_TAG "DFP"
60 #define LG4FF_DFP_NAME "Driving Force Pro"
61 #define LG4FF_G25_TAG "G25"
62 #define LG4FF_G25_NAME "G25 Racing Wheel"
63 #define LG4FF_G27_TAG "G27"
64 #define LG4FF_G27_NAME "G27 Racing Wheel"
65 #define LG4FF_DFGT_TAG "DFGT"
66 #define LG4FF_DFGT_NAME "Driving Force GT"
68 #define LG4FF_FFEX_REV_MAJ 0x21
69 #define LG4FF_FFEX_REV_MIN 0x00
71 static void hid_lg4ff_set_range_dfp(struct hid_device
*hid
, u16 range
);
72 static void hid_lg4ff_set_range_g25(struct hid_device
*hid
, u16 range
);
74 struct lg4ff_device_entry
{
79 #ifdef CONFIG_LEDS_CLASS
81 struct led_classdev
*led
[5];
85 const char *real_name
;
87 struct list_head list
;
88 void (*set_range
)(struct hid_device
*hid
, u16 range
);
91 static const signed short lg4ff_wheel_effects
[] = {
98 const __u32 product_id
;
99 const signed short *ff_effects
;
100 const __u16 min_range
;
101 const __u16 max_range
;
102 void (*set_range
)(struct hid_device
*hid
, u16 range
);
105 struct lg4ff_compat_mode_switch
{
106 const __u8 cmd_count
; /* Number of commands to send */
110 struct lg4ff_wheel_ident_info
{
113 const u16 real_product_id
;
116 struct lg4ff_wheel_ident_checklist
{
118 const struct lg4ff_wheel_ident_info
*models
[];
121 struct lg4ff_multimode_wheel
{
122 const u16 product_id
;
123 const u32 alternate_modes
;
124 const char *real_tag
;
125 const char *real_name
;
128 struct lg4ff_alternate_mode
{
129 const u16 product_id
;
134 static const struct lg4ff_wheel lg4ff_devices
[] = {
135 {USB_DEVICE_ID_LOGITECH_WHEEL
, lg4ff_wheel_effects
, 40, 270, NULL
},
136 {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL
, lg4ff_wheel_effects
, 40, 270, NULL
},
137 {USB_DEVICE_ID_LOGITECH_DFP_WHEEL
, lg4ff_wheel_effects
, 40, 900, hid_lg4ff_set_range_dfp
},
138 {USB_DEVICE_ID_LOGITECH_G25_WHEEL
, lg4ff_wheel_effects
, 40, 900, hid_lg4ff_set_range_g25
},
139 {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL
, lg4ff_wheel_effects
, 40, 900, hid_lg4ff_set_range_g25
},
140 {USB_DEVICE_ID_LOGITECH_G27_WHEEL
, lg4ff_wheel_effects
, 40, 900, hid_lg4ff_set_range_g25
},
141 {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2
, lg4ff_wheel_effects
, 40, 270, NULL
},
142 {USB_DEVICE_ID_LOGITECH_WII_WHEEL
, lg4ff_wheel_effects
, 40, 270, NULL
}
145 static const struct lg4ff_multimode_wheel lg4ff_multimode_wheels
[] = {
146 {USB_DEVICE_ID_LOGITECH_DFP_WHEEL
,
147 LG4FF_MODE_NATIVE
| LG4FF_MODE_DFP
| LG4FF_MODE_DFEX
,
148 LG4FF_DFP_TAG
, LG4FF_DFP_NAME
},
149 {USB_DEVICE_ID_LOGITECH_G25_WHEEL
,
150 LG4FF_MODE_NATIVE
| LG4FF_MODE_G25
| LG4FF_MODE_DFP
| LG4FF_MODE_DFEX
,
151 LG4FF_G25_TAG
, LG4FF_G25_NAME
},
152 {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL
,
153 LG4FF_MODE_NATIVE
| LG4FF_MODE_DFGT
| LG4FF_MODE_DFP
| LG4FF_MODE_DFEX
,
154 LG4FF_DFGT_TAG
, LG4FF_DFGT_NAME
},
155 {USB_DEVICE_ID_LOGITECH_G27_WHEEL
,
156 LG4FF_MODE_NATIVE
| LG4FF_MODE_G27
| LG4FF_MODE_G25
| LG4FF_MODE_DFP
| LG4FF_MODE_DFEX
,
157 LG4FF_G27_TAG
, LG4FF_G27_NAME
},
160 static const struct lg4ff_alternate_mode lg4ff_alternate_modes
[] = {
161 [LG4FF_MODE_NATIVE_IDX
] = {0, "native", ""},
162 [LG4FF_MODE_DFEX_IDX
] = {USB_DEVICE_ID_LOGITECH_WHEEL
, LG4FF_DFEX_TAG
, LG4FF_DFEX_NAME
},
163 [LG4FF_MODE_DFP_IDX
] = {USB_DEVICE_ID_LOGITECH_DFP_WHEEL
, LG4FF_DFP_TAG
, LG4FF_DFP_NAME
},
164 [LG4FF_MODE_G25_IDX
] = {USB_DEVICE_ID_LOGITECH_G25_WHEEL
, LG4FF_G25_TAG
, LG4FF_G25_NAME
},
165 [LG4FF_MODE_DFGT_IDX
] = {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL
, LG4FF_DFGT_TAG
, LG4FF_DFGT_NAME
},
166 [LG4FF_MODE_G27_IDX
] = {USB_DEVICE_ID_LOGITECH_G27_WHEEL
, LG4FF_G27_TAG
, LG4FF_G27_NAME
}
169 /* Multimode wheel identificators */
170 static const struct lg4ff_wheel_ident_info lg4ff_dfp_ident_info
= {
173 USB_DEVICE_ID_LOGITECH_DFP_WHEEL
176 static const struct lg4ff_wheel_ident_info lg4ff_g25_ident_info
= {
179 USB_DEVICE_ID_LOGITECH_G25_WHEEL
182 static const struct lg4ff_wheel_ident_info lg4ff_g27_ident_info
= {
185 USB_DEVICE_ID_LOGITECH_G27_WHEEL
188 static const struct lg4ff_wheel_ident_info lg4ff_dfgt_ident_info
= {
191 USB_DEVICE_ID_LOGITECH_DFGT_WHEEL
194 /* Multimode wheel identification checklists */
195 static const struct lg4ff_wheel_ident_checklist lg4ff_main_checklist
= {
197 {&lg4ff_dfgt_ident_info
,
198 &lg4ff_g27_ident_info
,
199 &lg4ff_g25_ident_info
,
200 &lg4ff_dfp_ident_info
}
203 /* Compatibility mode switching commands */
204 /* EXT_CMD9 - Understood by G27 and DFGT */
205 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfex
= {
207 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
208 0xf8, 0x09, 0x00, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DF-EX with detach */
211 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfp
= {
213 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
214 0xf8, 0x09, 0x01, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DFP with detach */
217 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g25
= {
219 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
220 0xf8, 0x09, 0x02, 0x01, 0x00, 0x00, 0x00} /* Switch mode to G25 with detach */
223 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfgt
= {
225 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
226 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DFGT with detach */
229 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g27
= {
231 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
232 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* Switch mode to G27 with detach */
235 /* EXT_CMD1 - Understood by DFP, G25, G27 and DFGT */
236 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext01_dfp
= {
238 {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
241 /* EXT_CMD16 - Understood by G25 and G27 */
242 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext16_g25
= {
244 {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
247 /* Recalculates X axis value accordingly to currently selected range */
248 static __s32
lg4ff_adjust_dfp_x_axis(__s32 value
, __u16 range
)
255 else if (range
== 200)
257 else if (range
< 200)
262 new_value
= 8192 + mult_frac(value
- 8192, max_range
, range
);
265 else if (new_value
> 16383)
271 int lg4ff_adjust_input_event(struct hid_device
*hid
, struct hid_field
*field
,
272 struct hid_usage
*usage
, __s32 value
, struct lg_drv_data
*drv_data
)
274 struct lg4ff_device_entry
*entry
= drv_data
->device_props
;
278 hid_err(hid
, "Device properties not found");
282 switch (entry
->product_id
) {
283 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL
:
284 switch (usage
->code
) {
286 new_value
= lg4ff_adjust_dfp_x_axis(value
, entry
->range
);
287 input_event(field
->hidinput
->input
, usage
->type
, usage
->code
, new_value
);
297 static int hid_lg4ff_play(struct input_dev
*dev
, void *data
, struct ff_effect
*effect
)
299 struct hid_device
*hid
= input_get_drvdata(dev
);
300 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
301 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
302 __s32
*value
= report
->field
[0]->value
;
305 #define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0)
307 switch (effect
->type
) {
309 x
= effect
->u
.ramp
.start_level
+ 0x80; /* 0x80 is no force */
313 /* De-activate force in slot-1*/
322 hid_hw_request(hid
, report
, HID_REQ_SET_REPORT
);
326 value
[0] = 0x11; /* Slot 1 */
334 hid_hw_request(hid
, report
, HID_REQ_SET_REPORT
);
340 /* Sends default autocentering command compatible with
341 * all wheels except Formula Force EX */
342 static void hid_lg4ff_set_autocenter_default(struct input_dev
*dev
, u16 magnitude
)
344 struct hid_device
*hid
= input_get_drvdata(dev
);
345 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
346 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
347 __s32
*value
= report
->field
[0]->value
;
348 __u32 expand_a
, expand_b
;
349 struct lg4ff_device_entry
*entry
;
350 struct lg_drv_data
*drv_data
;
352 drv_data
= hid_get_drvdata(hid
);
354 hid_err(hid
, "Private driver data not found!\n");
358 entry
= drv_data
->device_props
;
360 hid_err(hid
, "Device properties not found!\n");
364 /* De-activate Auto-Center */
365 if (magnitude
== 0) {
374 hid_hw_request(hid
, report
, HID_REQ_SET_REPORT
);
378 if (magnitude
<= 0xaaaa) {
379 expand_a
= 0x0c * magnitude
;
380 expand_b
= 0x80 * magnitude
;
382 expand_a
= (0x0c * 0xaaaa) + 0x06 * (magnitude
- 0xaaaa);
383 expand_b
= (0x80 * 0xaaaa) + 0xff * (magnitude
- 0xaaaa);
386 /* Adjust for non-MOMO wheels */
387 switch (entry
->product_id
) {
388 case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL
:
389 case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2
:
392 expand_a
= expand_a
>> 1;
398 value
[2] = expand_a
/ 0xaaaa;
399 value
[3] = expand_a
/ 0xaaaa;
400 value
[4] = expand_b
/ 0xaaaa;
404 hid_hw_request(hid
, report
, HID_REQ_SET_REPORT
);
406 /* Activate Auto-Center */
415 hid_hw_request(hid
, report
, HID_REQ_SET_REPORT
);
418 /* Sends autocentering command compatible with Formula Force EX */
419 static void hid_lg4ff_set_autocenter_ffex(struct input_dev
*dev
, u16 magnitude
)
421 struct hid_device
*hid
= input_get_drvdata(dev
);
422 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
423 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
424 __s32
*value
= report
->field
[0]->value
;
425 magnitude
= magnitude
* 90 / 65535;
429 value
[2] = magnitude
>> 14;
430 value
[3] = magnitude
>> 14;
431 value
[4] = magnitude
;
435 hid_hw_request(hid
, report
, HID_REQ_SET_REPORT
);
438 /* Sends command to set range compatible with G25/G27/Driving Force GT */
439 static void hid_lg4ff_set_range_g25(struct hid_device
*hid
, u16 range
)
441 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
442 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
443 __s32
*value
= report
->field
[0]->value
;
445 dbg_hid("G25/G27/DFGT: setting range to %u\n", range
);
449 value
[2] = range
& 0x00ff;
450 value
[3] = (range
& 0xff00) >> 8;
455 hid_hw_request(hid
, report
, HID_REQ_SET_REPORT
);
458 /* Sends commands to set range compatible with Driving Force Pro wheel */
459 static void hid_lg4ff_set_range_dfp(struct hid_device
*hid
, __u16 range
)
461 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
462 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
463 int start_left
, start_right
, full_range
;
464 __s32
*value
= report
->field
[0]->value
;
466 dbg_hid("Driving Force Pro: setting range to %u\n", range
);
468 /* Prepare "coarse" limit command */
470 value
[1] = 0x00; /* Set later */
478 report
->field
[0]->value
[1] = 0x03;
481 report
->field
[0]->value
[1] = 0x02;
484 hid_hw_request(hid
, report
, HID_REQ_SET_REPORT
);
486 /* Prepare "fine" limit command */
495 if (range
== 200 || range
== 900) { /* Do not apply any fine limit */
496 hid_hw_request(hid
, report
, HID_REQ_SET_REPORT
);
500 /* Construct fine limit command */
501 start_left
= (((full_range
- range
+ 1) * 2047) / full_range
);
502 start_right
= 0xfff - start_left
;
504 value
[2] = start_left
>> 4;
505 value
[3] = start_right
>> 4;
507 value
[5] = (start_right
& 0xe) << 4 | (start_left
& 0xe);
510 hid_hw_request(hid
, report
, HID_REQ_SET_REPORT
);
513 static const struct lg4ff_compat_mode_switch
*lg4ff_get_mode_switch_command(const u16 real_product_id
, const u16 target_product_id
)
515 switch (real_product_id
) {
516 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL
:
517 switch (target_product_id
) {
518 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL
:
519 return &lg4ff_mode_switch_ext01_dfp
;
520 /* DFP can only be switched to its native mode */
525 case USB_DEVICE_ID_LOGITECH_G25_WHEEL
:
526 switch (target_product_id
) {
527 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL
:
528 return &lg4ff_mode_switch_ext01_dfp
;
529 case USB_DEVICE_ID_LOGITECH_G25_WHEEL
:
530 return &lg4ff_mode_switch_ext16_g25
;
531 /* G25 can only be switched to DFP mode or its native mode */
536 case USB_DEVICE_ID_LOGITECH_G27_WHEEL
:
537 switch (target_product_id
) {
538 case USB_DEVICE_ID_LOGITECH_WHEEL
:
539 return &lg4ff_mode_switch_ext09_dfex
;
540 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL
:
541 return &lg4ff_mode_switch_ext09_dfp
;
542 case USB_DEVICE_ID_LOGITECH_G25_WHEEL
:
543 return &lg4ff_mode_switch_ext09_g25
;
544 case USB_DEVICE_ID_LOGITECH_G27_WHEEL
:
545 return &lg4ff_mode_switch_ext09_g27
;
546 /* G27 can only be switched to DF-EX, DFP, G25 or its native mode */
551 case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL
:
552 switch (target_product_id
) {
553 case USB_DEVICE_ID_LOGITECH_WHEEL
:
554 return &lg4ff_mode_switch_ext09_dfex
;
555 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL
:
556 return &lg4ff_mode_switch_ext09_dfp
;
557 case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL
:
558 return &lg4ff_mode_switch_ext09_dfgt
;
559 /* DFGT can only be switched to DF-EX, DFP or its native mode */
564 /* No other wheels have multiple modes */
570 static int lg4ff_switch_compatibility_mode(struct hid_device
*hid
, const struct lg4ff_compat_mode_switch
*s
)
572 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
573 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
574 __s32
*value
= report
->field
[0]->value
;
577 for (i
= 0; i
< s
->cmd_count
; i
++) {
580 for (j
= 0; j
< 7; j
++)
581 value
[j
] = s
->cmd
[j
+ (7*i
)];
583 hid_hw_request(hid
, report
, HID_REQ_SET_REPORT
);
589 static ssize_t
lg4ff_alternate_modes_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
591 struct hid_device
*hid
= to_hid_device(dev
);
592 struct lg4ff_device_entry
*entry
;
593 struct lg_drv_data
*drv_data
;
597 drv_data
= hid_get_drvdata(hid
);
599 hid_err(hid
, "Private driver data not found!\n");
603 entry
= drv_data
->device_props
;
605 hid_err(hid
, "Device properties not found!\n");
609 if (!entry
->real_name
) {
610 hid_err(hid
, "NULL pointer to string\n");
614 for (i
= 0; i
< LG4FF_MODE_MAX_IDX
; i
++) {
615 if (entry
->alternate_modes
& BIT(i
)) {
616 /* Print tag and full name */
617 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
, "%s: %s",
618 lg4ff_alternate_modes
[i
].tag
,
619 !lg4ff_alternate_modes
[i
].product_id
? entry
->real_name
: lg4ff_alternate_modes
[i
].name
);
620 if (count
>= PAGE_SIZE
- 1)
623 /* Mark the currently active mode with an asterisk */
624 if (lg4ff_alternate_modes
[i
].product_id
== entry
->product_id
||
625 (lg4ff_alternate_modes
[i
].product_id
== 0 && entry
->product_id
== entry
->real_product_id
))
626 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
, " *\n");
628 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
, "\n");
630 if (count
>= PAGE_SIZE
- 1)
638 static ssize_t
lg4ff_alternate_modes_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
640 struct hid_device
*hid
= to_hid_device(dev
);
641 struct lg4ff_device_entry
*entry
;
642 struct lg_drv_data
*drv_data
;
643 const struct lg4ff_compat_mode_switch
*s
;
644 u16 target_product_id
= 0;
648 drv_data
= hid_get_drvdata(hid
);
650 hid_err(hid
, "Private driver data not found!\n");
654 entry
= drv_data
->device_props
;
656 hid_err(hid
, "Device properties not found!\n");
660 /* Allow \n at the end of the input parameter */
661 lbuf
= kasprintf(GFP_KERNEL
, "%s", buf
);
666 if (lbuf
[i
-1] == '\n') {
674 for (i
= 0; i
< LG4FF_MODE_MAX_IDX
; i
++) {
675 const u16 mode_product_id
= lg4ff_alternate_modes
[i
].product_id
;
676 const char *tag
= lg4ff_alternate_modes
[i
].tag
;
678 if (entry
->alternate_modes
& BIT(i
)) {
679 if (!strcmp(tag
, lbuf
)) {
680 if (!mode_product_id
)
681 target_product_id
= entry
->real_product_id
;
683 target_product_id
= mode_product_id
;
689 if (i
== LG4FF_MODE_MAX_IDX
) {
690 hid_info(hid
, "Requested mode \"%s\" is not supported by the device\n", lbuf
);
694 kfree(lbuf
); /* Not needed anymore */
696 if (target_product_id
== entry
->product_id
) /* Nothing to do */
699 /* Automatic switching has to be disabled for the switch to DF-EX mode to work correctly */
700 if (target_product_id
== USB_DEVICE_ID_LOGITECH_WHEEL
&& !lg4ff_no_autoswitch
) {
701 hid_info(hid
, "\"%s\" cannot be switched to \"DF-EX\" mode. Load the \"hid_logitech\" module with \"lg4ff_no_autoswitch=1\" parameter set and try again\n",
706 /* Take care of hardware limitations */
707 if ((entry
->real_product_id
== USB_DEVICE_ID_LOGITECH_DFP_WHEEL
|| entry
->real_product_id
== USB_DEVICE_ID_LOGITECH_G25_WHEEL
) &&
708 entry
->product_id
> target_product_id
) {
709 hid_info(hid
, "\"%s\" cannot be switched back into \"%s\" mode\n", entry
->real_name
, lg4ff_alternate_modes
[i
].name
);
713 s
= lg4ff_get_mode_switch_command(entry
->real_product_id
, target_product_id
);
715 hid_err(hid
, "Invalid target product ID %X\n", target_product_id
);
719 ret
= lg4ff_switch_compatibility_mode(hid
, s
);
720 return (ret
== 0 ? count
: ret
);
722 static DEVICE_ATTR(alternate_modes
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
, lg4ff_alternate_modes_show
, lg4ff_alternate_modes_store
);
724 /* Read current range and display it in terminal */
725 static ssize_t
range_show(struct device
*dev
, struct device_attribute
*attr
,
728 struct hid_device
*hid
= to_hid_device(dev
);
729 struct lg4ff_device_entry
*entry
;
730 struct lg_drv_data
*drv_data
;
733 drv_data
= hid_get_drvdata(hid
);
735 hid_err(hid
, "Private driver data not found!\n");
739 entry
= drv_data
->device_props
;
741 hid_err(hid
, "Device properties not found!\n");
745 count
= scnprintf(buf
, PAGE_SIZE
, "%u\n", entry
->range
);
749 /* Set range to user specified value, call appropriate function
750 * according to the type of the wheel */
751 static ssize_t
range_store(struct device
*dev
, struct device_attribute
*attr
,
752 const char *buf
, size_t count
)
754 struct hid_device
*hid
= to_hid_device(dev
);
755 struct lg4ff_device_entry
*entry
;
756 struct lg_drv_data
*drv_data
;
757 __u16 range
= simple_strtoul(buf
, NULL
, 10);
759 drv_data
= hid_get_drvdata(hid
);
761 hid_err(hid
, "Private driver data not found!\n");
765 entry
= drv_data
->device_props
;
767 hid_err(hid
, "Device properties not found!\n");
772 range
= entry
->max_range
;
774 /* Check if the wheel supports range setting
775 * and that the range is within limits for the wheel */
776 if (entry
->set_range
!= NULL
&& range
>= entry
->min_range
&& range
<= entry
->max_range
) {
777 entry
->set_range(hid
, range
);
778 entry
->range
= range
;
783 static DEVICE_ATTR_RW(range
);
785 static ssize_t
lg4ff_real_id_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
787 struct hid_device
*hid
= to_hid_device(dev
);
788 struct lg4ff_device_entry
*entry
;
789 struct lg_drv_data
*drv_data
;
792 drv_data
= hid_get_drvdata(hid
);
794 hid_err(hid
, "Private driver data not found!\n");
798 entry
= drv_data
->device_props
;
800 hid_err(hid
, "Device properties not found!\n");
804 if (!entry
->real_tag
|| !entry
->real_name
) {
805 hid_err(hid
, "NULL pointer to string\n");
809 count
= scnprintf(buf
, PAGE_SIZE
, "%s: %s\n", entry
->real_tag
, entry
->real_name
);
813 static ssize_t
lg4ff_real_id_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
815 /* Real ID is a read-only value */
818 static DEVICE_ATTR(real_id
, S_IRUGO
, lg4ff_real_id_show
, lg4ff_real_id_store
);
820 #ifdef CONFIG_LEDS_CLASS
821 static void lg4ff_set_leds(struct hid_device
*hid
, __u8 leds
)
823 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
824 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
825 __s32
*value
= report
->field
[0]->value
;
834 hid_hw_request(hid
, report
, HID_REQ_SET_REPORT
);
837 static void lg4ff_led_set_brightness(struct led_classdev
*led_cdev
,
838 enum led_brightness value
)
840 struct device
*dev
= led_cdev
->dev
->parent
;
841 struct hid_device
*hid
= container_of(dev
, struct hid_device
, dev
);
842 struct lg_drv_data
*drv_data
= hid_get_drvdata(hid
);
843 struct lg4ff_device_entry
*entry
;
847 hid_err(hid
, "Device data not found.");
851 entry
= (struct lg4ff_device_entry
*)drv_data
->device_props
;
854 hid_err(hid
, "Device properties not found.");
858 for (i
= 0; i
< 5; i
++) {
859 if (led_cdev
!= entry
->led
[i
])
861 state
= (entry
->led_state
>> i
) & 1;
862 if (value
== LED_OFF
&& state
) {
863 entry
->led_state
&= ~(1 << i
);
864 lg4ff_set_leds(hid
, entry
->led_state
);
865 } else if (value
!= LED_OFF
&& !state
) {
866 entry
->led_state
|= 1 << i
;
867 lg4ff_set_leds(hid
, entry
->led_state
);
873 static enum led_brightness
lg4ff_led_get_brightness(struct led_classdev
*led_cdev
)
875 struct device
*dev
= led_cdev
->dev
->parent
;
876 struct hid_device
*hid
= container_of(dev
, struct hid_device
, dev
);
877 struct lg_drv_data
*drv_data
= hid_get_drvdata(hid
);
878 struct lg4ff_device_entry
*entry
;
882 hid_err(hid
, "Device data not found.");
886 entry
= (struct lg4ff_device_entry
*)drv_data
->device_props
;
889 hid_err(hid
, "Device properties not found.");
893 for (i
= 0; i
< 5; i
++)
894 if (led_cdev
== entry
->led
[i
]) {
895 value
= (entry
->led_state
>> i
) & 1;
899 return value
? LED_FULL
: LED_OFF
;
903 static u16
lg4ff_identify_multimode_wheel(struct hid_device
*hid
, const u16 reported_product_id
, const u16 bcdDevice
)
905 const struct lg4ff_wheel_ident_checklist
*checklist
;
906 int i
, from_idx
, to_idx
;
908 switch (reported_product_id
) {
909 case USB_DEVICE_ID_LOGITECH_WHEEL
:
910 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL
:
911 checklist
= &lg4ff_main_checklist
;
913 to_idx
= checklist
->count
- 1;
915 case USB_DEVICE_ID_LOGITECH_G25_WHEEL
:
916 checklist
= &lg4ff_main_checklist
;
918 to_idx
= checklist
->count
- 2; /* End identity check at G25 */
920 case USB_DEVICE_ID_LOGITECH_G27_WHEEL
:
921 checklist
= &lg4ff_main_checklist
;
922 from_idx
= 1; /* Start identity check at G27 */
923 to_idx
= checklist
->count
- 3; /* End identity check at G27 */
925 case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL
:
926 checklist
= &lg4ff_main_checklist
;
928 to_idx
= checklist
->count
- 4; /* End identity check at DFGT */
934 for (i
= from_idx
; i
<= to_idx
; i
++) {
935 const u16 mask
= checklist
->models
[i
]->mask
;
936 const u16 result
= checklist
->models
[i
]->result
;
937 const u16 real_product_id
= checklist
->models
[i
]->real_product_id
;
939 if ((bcdDevice
& mask
) == result
) {
940 dbg_hid("Found wheel with real PID %X whose reported PID is %X\n", real_product_id
, reported_product_id
);
941 return real_product_id
;
945 /* No match found. This is either Driving Force or an unknown
946 * wheel model, do not touch it */
947 dbg_hid("Wheel with bcdDevice %X was not recognized as multimode wheel, leaving in its current mode\n", bcdDevice
);
951 static int lg4ff_handle_multimode_wheel(struct hid_device
*hid
, u16
*real_product_id
, const u16 bcdDevice
)
953 const u16 reported_product_id
= hid
->product
;
956 *real_product_id
= lg4ff_identify_multimode_wheel(hid
, reported_product_id
, bcdDevice
);
957 /* Probed wheel is not a multimode wheel */
958 if (!*real_product_id
) {
959 *real_product_id
= reported_product_id
;
960 dbg_hid("Wheel is not a multimode wheel\n");
961 return LG4FF_MMODE_NOT_MULTIMODE
;
964 /* Switch from "Driving Force" mode to native mode automatically.
965 * Otherwise keep the wheel in its current mode */
966 if (reported_product_id
== USB_DEVICE_ID_LOGITECH_WHEEL
&&
967 reported_product_id
!= *real_product_id
&&
968 !lg4ff_no_autoswitch
) {
969 const struct lg4ff_compat_mode_switch
*s
= lg4ff_get_mode_switch_command(*real_product_id
, *real_product_id
);
972 hid_err(hid
, "Invalid product id %X\n", *real_product_id
);
973 return LG4FF_MMODE_NOT_MULTIMODE
;
976 ret
= lg4ff_switch_compatibility_mode(hid
, s
);
978 /* Wheel could not have been switched to native mode,
979 * leave it in "Driving Force" mode and continue */
980 hid_err(hid
, "Unable to switch wheel mode, errno %d\n", ret
);
981 return LG4FF_MMODE_IS_MULTIMODE
;
983 return LG4FF_MMODE_SWITCHED
;
986 return LG4FF_MMODE_IS_MULTIMODE
;
990 int lg4ff_init(struct hid_device
*hid
)
992 struct hid_input
*hidinput
= list_entry(hid
->inputs
.next
, struct hid_input
, list
);
993 struct input_dev
*dev
= hidinput
->input
;
994 const struct usb_device_descriptor
*udesc
= &(hid_to_usb_dev(hid
)->descriptor
);
995 const u16 bcdDevice
= le16_to_cpu(udesc
->bcdDevice
);
996 struct lg4ff_device_entry
*entry
;
997 struct lg_drv_data
*drv_data
;
999 int mmode_ret
, mmode_idx
= -1;
1000 u16 real_product_id
;
1002 /* Check that the report looks ok */
1003 if (!hid_validate_values(hid
, HID_OUTPUT_REPORT
, 0, 0, 7))
1006 /* Check if a multimode wheel has been connected and
1007 * handle it appropriately */
1008 mmode_ret
= lg4ff_handle_multimode_wheel(hid
, &real_product_id
, bcdDevice
);
1010 /* Wheel has been told to switch to native mode. There is no point in going on
1011 * with the initialization as the wheel will do a USB reset when it switches mode
1013 if (mmode_ret
== LG4FF_MMODE_SWITCHED
)
1016 /* Check what wheel has been connected */
1017 for (i
= 0; i
< ARRAY_SIZE(lg4ff_devices
); i
++) {
1018 if (hid
->product
== lg4ff_devices
[i
].product_id
) {
1019 dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices
[i
].product_id
);
1024 if (i
== ARRAY_SIZE(lg4ff_devices
)) {
1025 hid_err(hid
, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to"
1026 "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n");
1030 if (mmode_ret
== LG4FF_MMODE_IS_MULTIMODE
) {
1031 for (mmode_idx
= 0; mmode_idx
< ARRAY_SIZE(lg4ff_multimode_wheels
); mmode_idx
++) {
1032 if (real_product_id
== lg4ff_multimode_wheels
[mmode_idx
].product_id
)
1036 if (mmode_idx
== ARRAY_SIZE(lg4ff_multimode_wheels
)) {
1037 hid_err(hid
, "Device product ID %X is not listed as a multimode wheel", real_product_id
);
1042 /* Set supported force feedback capabilities */
1043 for (j
= 0; lg4ff_devices
[i
].ff_effects
[j
] >= 0; j
++)
1044 set_bit(lg4ff_devices
[i
].ff_effects
[j
], dev
->ffbit
);
1046 error
= input_ff_create_memless(dev
, NULL
, hid_lg4ff_play
);
1051 /* Get private driver data */
1052 drv_data
= hid_get_drvdata(hid
);
1054 hid_err(hid
, "Cannot add device, private driver data not allocated\n");
1058 /* Initialize device properties */
1059 entry
= kzalloc(sizeof(struct lg4ff_device_entry
), GFP_KERNEL
);
1061 hid_err(hid
, "Cannot add device, insufficient memory to allocate device properties.\n");
1064 drv_data
->device_props
= entry
;
1066 entry
->product_id
= lg4ff_devices
[i
].product_id
;
1067 entry
->real_product_id
= real_product_id
;
1068 entry
->min_range
= lg4ff_devices
[i
].min_range
;
1069 entry
->max_range
= lg4ff_devices
[i
].max_range
;
1070 entry
->set_range
= lg4ff_devices
[i
].set_range
;
1071 if (mmode_ret
== LG4FF_MMODE_IS_MULTIMODE
) {
1072 BUG_ON(mmode_idx
== -1);
1073 entry
->alternate_modes
= lg4ff_multimode_wheels
[mmode_idx
].alternate_modes
;
1074 entry
->real_tag
= lg4ff_multimode_wheels
[mmode_idx
].real_tag
;
1075 entry
->real_name
= lg4ff_multimode_wheels
[mmode_idx
].real_name
;
1078 /* Check if autocentering is available and
1079 * set the centering force to zero by default */
1080 if (test_bit(FF_AUTOCENTER
, dev
->ffbit
)) {
1081 /* Formula Force EX expects different autocentering command */
1082 if ((bcdDevice
>> 8) == LG4FF_FFEX_REV_MAJ
&&
1083 (bcdDevice
& 0xff) == LG4FF_FFEX_REV_MIN
)
1084 dev
->ff
->set_autocenter
= hid_lg4ff_set_autocenter_ffex
;
1086 dev
->ff
->set_autocenter
= hid_lg4ff_set_autocenter_default
;
1088 dev
->ff
->set_autocenter(dev
, 0);
1091 /* Create sysfs interface */
1092 error
= device_create_file(&hid
->dev
, &dev_attr_range
);
1095 if (mmode_ret
== LG4FF_MMODE_IS_MULTIMODE
) {
1096 error
= device_create_file(&hid
->dev
, &dev_attr_real_id
);
1099 error
= device_create_file(&hid
->dev
, &dev_attr_alternate_modes
);
1103 dbg_hid("sysfs interface created\n");
1105 /* Set the maximum range to start with */
1106 entry
->range
= entry
->max_range
;
1107 if (entry
->set_range
!= NULL
)
1108 entry
->set_range(hid
, entry
->range
);
1110 #ifdef CONFIG_LEDS_CLASS
1111 /* register led subsystem - G27 only */
1112 entry
->led_state
= 0;
1113 for (j
= 0; j
< 5; j
++)
1114 entry
->led
[j
] = NULL
;
1116 if (lg4ff_devices
[i
].product_id
== USB_DEVICE_ID_LOGITECH_G27_WHEEL
) {
1117 struct led_classdev
*led
;
1121 lg4ff_set_leds(hid
, 0);
1123 name_sz
= strlen(dev_name(&hid
->dev
)) + 8;
1125 for (j
= 0; j
< 5; j
++) {
1126 led
= kzalloc(sizeof(struct led_classdev
)+name_sz
, GFP_KERNEL
);
1128 hid_err(hid
, "can't allocate memory for LED %d\n", j
);
1132 name
= (void *)(&led
[1]);
1133 snprintf(name
, name_sz
, "%s::RPM%d", dev_name(&hid
->dev
), j
+1);
1135 led
->brightness
= 0;
1136 led
->max_brightness
= 1;
1137 led
->brightness_get
= lg4ff_led_get_brightness
;
1138 led
->brightness_set
= lg4ff_led_set_brightness
;
1140 entry
->led
[j
] = led
;
1141 error
= led_classdev_register(&hid
->dev
, led
);
1144 hid_err(hid
, "failed to register LED %d. Aborting.\n", j
);
1146 /* Deregister LEDs (if any) */
1147 for (j
= 0; j
< 5; j
++) {
1148 led
= entry
->led
[j
];
1149 entry
->led
[j
] = NULL
;
1152 led_classdev_unregister(led
);
1155 goto out
; /* Let the driver continue without LEDs */
1161 hid_info(hid
, "Force feedback support for Logitech Gaming Wheels\n");
1165 int lg4ff_deinit(struct hid_device
*hid
)
1167 struct lg4ff_device_entry
*entry
;
1168 struct lg_drv_data
*drv_data
;
1170 drv_data
= hid_get_drvdata(hid
);
1172 hid_err(hid
, "Error while deinitializing device, no private driver data.\n");
1175 entry
= drv_data
->device_props
;
1177 goto out
; /* Nothing more to do */
1179 device_remove_file(&hid
->dev
, &dev_attr_range
);
1181 /* Multimode devices will have at least the "MODE_NATIVE" bit set */
1182 if (entry
->alternate_modes
) {
1183 device_remove_file(&hid
->dev
, &dev_attr_real_id
);
1184 device_remove_file(&hid
->dev
, &dev_attr_alternate_modes
);
1187 #ifdef CONFIG_LEDS_CLASS
1190 struct led_classdev
*led
;
1192 /* Deregister LEDs (if any) */
1193 for (j
= 0; j
< 5; j
++) {
1195 led
= entry
->led
[j
];
1196 entry
->led
[j
] = NULL
;
1199 led_classdev_unregister(led
);
1205 /* Deallocate memory */
1209 dbg_hid("Device successfully unregistered\n");