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 DFGT_REV_MAJ 0x13
36 #define DFGT_REV_MIN 0x22
37 #define DFGT2_REV_MIN 0x26
38 #define DFP_REV_MAJ 0x11
39 #define DFP_REV_MIN 0x06
40 #define FFEX_REV_MAJ 0x21
41 #define FFEX_REV_MIN 0x00
42 #define G25_REV_MAJ 0x12
43 #define G25_REV_MIN 0x22
44 #define G27_REV_MAJ 0x12
45 #define G27_REV_MIN 0x38
47 #define to_hid_device(pdev) container_of(pdev, struct hid_device, dev)
49 static void hid_lg4ff_set_range_dfp(struct hid_device
*hid
, u16 range
);
50 static void hid_lg4ff_set_range_g25(struct hid_device
*hid
, u16 range
);
51 static ssize_t
lg4ff_range_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
);
52 static ssize_t
lg4ff_range_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
);
54 static DEVICE_ATTR(range
, S_IRWXU
| S_IRWXG
| S_IRWXO
, lg4ff_range_show
, lg4ff_range_store
);
56 struct lg4ff_device_entry
{
61 #ifdef CONFIG_LEDS_CLASS
63 struct led_classdev
*led
[5];
65 struct list_head list
;
66 void (*set_range
)(struct hid_device
*hid
, u16 range
);
69 static const signed short lg4ff_wheel_effects
[] = {
76 const __u32 product_id
;
77 const signed short *ff_effects
;
78 const __u16 min_range
;
79 const __u16 max_range
;
80 void (*set_range
)(struct hid_device
*hid
, u16 range
);
83 static const struct lg4ff_wheel lg4ff_devices
[] = {
84 {USB_DEVICE_ID_LOGITECH_WHEEL
, lg4ff_wheel_effects
, 40, 270, NULL
},
85 {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL
, lg4ff_wheel_effects
, 40, 270, NULL
},
86 {USB_DEVICE_ID_LOGITECH_DFP_WHEEL
, lg4ff_wheel_effects
, 40, 900, hid_lg4ff_set_range_dfp
},
87 {USB_DEVICE_ID_LOGITECH_G25_WHEEL
, lg4ff_wheel_effects
, 40, 900, hid_lg4ff_set_range_g25
},
88 {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL
, lg4ff_wheel_effects
, 40, 900, hid_lg4ff_set_range_g25
},
89 {USB_DEVICE_ID_LOGITECH_G27_WHEEL
, lg4ff_wheel_effects
, 40, 900, hid_lg4ff_set_range_g25
},
90 {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2
, lg4ff_wheel_effects
, 40, 270, NULL
},
91 {USB_DEVICE_ID_LOGITECH_WII_WHEEL
, lg4ff_wheel_effects
, 40, 270, NULL
}
94 struct lg4ff_native_cmd
{
95 const __u8 cmd_num
; /* Number of commands to send */
99 struct lg4ff_usb_revision
{
102 const struct lg4ff_native_cmd
*command
;
105 static const struct lg4ff_native_cmd native_dfp
= {
107 {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
110 static const struct lg4ff_native_cmd native_dfgt
= {
112 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */
113 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* 2nd command */
116 static const struct lg4ff_native_cmd native_g25
= {
118 {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
121 static const struct lg4ff_native_cmd native_g27
= {
123 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */
124 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* 2nd command */
127 static const struct lg4ff_usb_revision lg4ff_revs
[] = {
128 {DFGT_REV_MAJ
, DFGT_REV_MIN
, &native_dfgt
}, /* Driving Force GT */
129 {DFGT_REV_MAJ
, DFGT2_REV_MIN
, &native_dfgt
}, /* Driving Force GT v2 */
130 {DFP_REV_MAJ
, DFP_REV_MIN
, &native_dfp
}, /* Driving Force Pro */
131 {G25_REV_MAJ
, G25_REV_MIN
, &native_g25
}, /* G25 */
132 {G27_REV_MAJ
, G27_REV_MIN
, &native_g27
}, /* G27 */
135 /* Recalculates X axis value accordingly to currently selected range */
136 static __s32
lg4ff_adjust_dfp_x_axis(__s32 value
, __u16 range
)
143 else if (range
== 200)
145 else if (range
< 200)
150 new_value
= 8192 + mult_frac(value
- 8192, max_range
, range
);
153 else if (new_value
> 16383)
159 int lg4ff_adjust_input_event(struct hid_device
*hid
, struct hid_field
*field
,
160 struct hid_usage
*usage
, __s32 value
, struct lg_drv_data
*drv_data
)
162 struct lg4ff_device_entry
*entry
= drv_data
->device_props
;
166 hid_err(hid
, "Device properties not found");
170 switch (entry
->product_id
) {
171 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL
:
172 switch (usage
->code
) {
174 new_value
= lg4ff_adjust_dfp_x_axis(value
, entry
->range
);
175 input_event(field
->hidinput
->input
, usage
->type
, usage
->code
, new_value
);
185 static int hid_lg4ff_play(struct input_dev
*dev
, void *data
, struct ff_effect
*effect
)
187 struct hid_device
*hid
= input_get_drvdata(dev
);
188 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
189 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
190 __s32
*value
= report
->field
[0]->value
;
193 #define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0)
195 switch (effect
->type
) {
197 x
= effect
->u
.ramp
.start_level
+ 0x80; /* 0x80 is no force */
199 value
[0] = 0x11; /* Slot 1 */
207 hid_hw_request(hid
, report
, HID_REQ_SET_REPORT
);
213 /* Sends default autocentering command compatible with
214 * all wheels except Formula Force EX */
215 static void hid_lg4ff_set_autocenter_default(struct input_dev
*dev
, u16 magnitude
)
217 struct hid_device
*hid
= input_get_drvdata(dev
);
218 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
219 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
220 __s32
*value
= report
->field
[0]->value
;
224 value
[2] = magnitude
>> 13;
225 value
[3] = magnitude
>> 13;
226 value
[4] = magnitude
>> 8;
230 hid_hw_request(hid
, report
, HID_REQ_SET_REPORT
);
233 /* Sends autocentering command compatible with Formula Force EX */
234 static void hid_lg4ff_set_autocenter_ffex(struct input_dev
*dev
, u16 magnitude
)
236 struct hid_device
*hid
= input_get_drvdata(dev
);
237 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
238 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
239 __s32
*value
= report
->field
[0]->value
;
240 magnitude
= magnitude
* 90 / 65535;
244 value
[2] = magnitude
>> 14;
245 value
[3] = magnitude
>> 14;
246 value
[4] = magnitude
;
250 hid_hw_request(hid
, report
, HID_REQ_SET_REPORT
);
253 /* Sends command to set range compatible with G25/G27/Driving Force GT */
254 static void hid_lg4ff_set_range_g25(struct hid_device
*hid
, u16 range
)
256 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
257 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
258 __s32
*value
= report
->field
[0]->value
;
260 dbg_hid("G25/G27/DFGT: setting range to %u\n", range
);
264 value
[2] = range
& 0x00ff;
265 value
[3] = (range
& 0xff00) >> 8;
270 hid_hw_request(hid
, report
, HID_REQ_SET_REPORT
);
273 /* Sends commands to set range compatible with Driving Force Pro wheel */
274 static void hid_lg4ff_set_range_dfp(struct hid_device
*hid
, __u16 range
)
276 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
277 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
278 int start_left
, start_right
, full_range
;
279 __s32
*value
= report
->field
[0]->value
;
281 dbg_hid("Driving Force Pro: setting range to %u\n", range
);
283 /* Prepare "coarse" limit command */
285 value
[1] = 0x00; /* Set later */
293 report
->field
[0]->value
[1] = 0x03;
296 report
->field
[0]->value
[1] = 0x02;
299 hid_hw_request(hid
, report
, HID_REQ_SET_REPORT
);
301 /* Prepare "fine" limit command */
310 if (range
== 200 || range
== 900) { /* Do not apply any fine limit */
311 hid_hw_request(hid
, report
, HID_REQ_SET_REPORT
);
315 /* Construct fine limit command */
316 start_left
= (((full_range
- range
+ 1) * 2047) / full_range
);
317 start_right
= 0xfff - start_left
;
319 value
[2] = start_left
>> 4;
320 value
[3] = start_right
>> 4;
322 value
[5] = (start_right
& 0xe) << 4 | (start_left
& 0xe);
325 hid_hw_request(hid
, report
, HID_REQ_SET_REPORT
);
328 static void hid_lg4ff_switch_native(struct hid_device
*hid
, const struct lg4ff_native_cmd
*cmd
)
330 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
331 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
335 while (j
< 7*cmd
->cmd_num
) {
336 for (i
= 0; i
< 7; i
++)
337 report
->field
[0]->value
[i
] = cmd
->cmd
[j
++];
339 hid_hw_request(hid
, report
, HID_REQ_SET_REPORT
);
343 /* Read current range and display it in terminal */
344 static ssize_t
lg4ff_range_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
346 struct hid_device
*hid
= to_hid_device(dev
);
347 struct lg4ff_device_entry
*entry
;
348 struct lg_drv_data
*drv_data
;
351 drv_data
= hid_get_drvdata(hid
);
353 hid_err(hid
, "Private driver data not found!\n");
357 entry
= drv_data
->device_props
;
359 hid_err(hid
, "Device properties not found!\n");
363 count
= scnprintf(buf
, PAGE_SIZE
, "%u\n", entry
->range
);
367 /* Set range to user specified value, call appropriate function
368 * according to the type of the wheel */
369 static ssize_t
lg4ff_range_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
371 struct hid_device
*hid
= to_hid_device(dev
);
372 struct lg4ff_device_entry
*entry
;
373 struct lg_drv_data
*drv_data
;
374 __u16 range
= simple_strtoul(buf
, NULL
, 10);
376 drv_data
= hid_get_drvdata(hid
);
378 hid_err(hid
, "Private driver data not found!\n");
382 entry
= drv_data
->device_props
;
384 hid_err(hid
, "Device properties not found!\n");
389 range
= entry
->max_range
;
391 /* Check if the wheel supports range setting
392 * and that the range is within limits for the wheel */
393 if (entry
->set_range
!= NULL
&& range
>= entry
->min_range
&& range
<= entry
->max_range
) {
394 entry
->set_range(hid
, range
);
395 entry
->range
= range
;
401 #ifdef CONFIG_LEDS_CLASS
402 static void lg4ff_set_leds(struct hid_device
*hid
, __u8 leds
)
404 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
405 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
406 __s32
*value
= report
->field
[0]->value
;
415 hid_hw_request(hid
, report
, HID_REQ_SET_REPORT
);
418 static void lg4ff_led_set_brightness(struct led_classdev
*led_cdev
,
419 enum led_brightness value
)
421 struct device
*dev
= led_cdev
->dev
->parent
;
422 struct hid_device
*hid
= container_of(dev
, struct hid_device
, dev
);
423 struct lg_drv_data
*drv_data
= hid_get_drvdata(hid
);
424 struct lg4ff_device_entry
*entry
;
428 hid_err(hid
, "Device data not found.");
432 entry
= (struct lg4ff_device_entry
*)drv_data
->device_props
;
435 hid_err(hid
, "Device properties not found.");
439 for (i
= 0; i
< 5; i
++) {
440 if (led_cdev
!= entry
->led
[i
])
442 state
= (entry
->led_state
>> i
) & 1;
443 if (value
== LED_OFF
&& state
) {
444 entry
->led_state
&= ~(1 << i
);
445 lg4ff_set_leds(hid
, entry
->led_state
);
446 } else if (value
!= LED_OFF
&& !state
) {
447 entry
->led_state
|= 1 << i
;
448 lg4ff_set_leds(hid
, entry
->led_state
);
454 static enum led_brightness
lg4ff_led_get_brightness(struct led_classdev
*led_cdev
)
456 struct device
*dev
= led_cdev
->dev
->parent
;
457 struct hid_device
*hid
= container_of(dev
, struct hid_device
, dev
);
458 struct lg_drv_data
*drv_data
= hid_get_drvdata(hid
);
459 struct lg4ff_device_entry
*entry
;
463 hid_err(hid
, "Device data not found.");
467 entry
= (struct lg4ff_device_entry
*)drv_data
->device_props
;
470 hid_err(hid
, "Device properties not found.");
474 for (i
= 0; i
< 5; i
++)
475 if (led_cdev
== entry
->led
[i
]) {
476 value
= (entry
->led_state
>> i
) & 1;
480 return value
? LED_FULL
: LED_OFF
;
484 int lg4ff_init(struct hid_device
*hid
)
486 struct hid_input
*hidinput
= list_entry(hid
->inputs
.next
, struct hid_input
, list
);
487 struct input_dev
*dev
= hidinput
->input
;
488 struct lg4ff_device_entry
*entry
;
489 struct lg_drv_data
*drv_data
;
490 struct usb_device_descriptor
*udesc
;
492 __u16 bcdDevice
, rev_maj
, rev_min
;
494 /* Check that the report looks ok */
495 if (!hid_validate_values(hid
, HID_OUTPUT_REPORT
, 0, 0, 7))
498 /* Check what wheel has been connected */
499 for (i
= 0; i
< ARRAY_SIZE(lg4ff_devices
); i
++) {
500 if (hid
->product
== lg4ff_devices
[i
].product_id
) {
501 dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices
[i
].product_id
);
506 if (i
== ARRAY_SIZE(lg4ff_devices
)) {
507 hid_err(hid
, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to"
508 "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n");
512 /* Attempt to switch wheel to native mode when applicable */
513 udesc
= &(hid_to_usb_dev(hid
)->descriptor
);
515 hid_err(hid
, "NULL USB device descriptor\n");
518 bcdDevice
= le16_to_cpu(udesc
->bcdDevice
);
519 rev_maj
= bcdDevice
>> 8;
520 rev_min
= bcdDevice
& 0xff;
522 if (lg4ff_devices
[i
].product_id
== USB_DEVICE_ID_LOGITECH_WHEEL
) {
523 dbg_hid("Generic wheel detected, can it do native?\n");
524 dbg_hid("USB revision: %2x.%02x\n", rev_maj
, rev_min
);
526 for (j
= 0; j
< ARRAY_SIZE(lg4ff_revs
); j
++) {
527 if (lg4ff_revs
[j
].rev_maj
== rev_maj
&& lg4ff_revs
[j
].rev_min
== rev_min
) {
528 hid_lg4ff_switch_native(hid
, lg4ff_revs
[j
].command
);
529 hid_info(hid
, "Switched to native mode\n");
534 /* Set supported force feedback capabilities */
535 for (j
= 0; lg4ff_devices
[i
].ff_effects
[j
] >= 0; j
++)
536 set_bit(lg4ff_devices
[i
].ff_effects
[j
], dev
->ffbit
);
538 error
= input_ff_create_memless(dev
, NULL
, hid_lg4ff_play
);
543 /* Check if autocentering is available and
544 * set the centering force to zero by default */
545 if (test_bit(FF_AUTOCENTER
, dev
->ffbit
)) {
546 if (rev_maj
== FFEX_REV_MAJ
&& rev_min
== FFEX_REV_MIN
) /* Formula Force EX expects different autocentering command */
547 dev
->ff
->set_autocenter
= hid_lg4ff_set_autocenter_ffex
;
549 dev
->ff
->set_autocenter
= hid_lg4ff_set_autocenter_default
;
551 dev
->ff
->set_autocenter(dev
, 0);
554 /* Get private driver data */
555 drv_data
= hid_get_drvdata(hid
);
557 hid_err(hid
, "Cannot add device, private driver data not allocated\n");
561 /* Initialize device properties */
562 entry
= kzalloc(sizeof(struct lg4ff_device_entry
), GFP_KERNEL
);
564 hid_err(hid
, "Cannot add device, insufficient memory to allocate device properties.\n");
567 drv_data
->device_props
= entry
;
569 entry
->product_id
= lg4ff_devices
[i
].product_id
;
570 entry
->min_range
= lg4ff_devices
[i
].min_range
;
571 entry
->max_range
= lg4ff_devices
[i
].max_range
;
572 entry
->set_range
= lg4ff_devices
[i
].set_range
;
574 /* Create sysfs interface */
575 error
= device_create_file(&hid
->dev
, &dev_attr_range
);
578 dbg_hid("sysfs interface created\n");
580 /* Set the maximum range to start with */
581 entry
->range
= entry
->max_range
;
582 if (entry
->set_range
!= NULL
)
583 entry
->set_range(hid
, entry
->range
);
585 #ifdef CONFIG_LEDS_CLASS
586 /* register led subsystem - G27 only */
587 entry
->led_state
= 0;
588 for (j
= 0; j
< 5; j
++)
589 entry
->led
[j
] = NULL
;
591 if (lg4ff_devices
[i
].product_id
== USB_DEVICE_ID_LOGITECH_G27_WHEEL
) {
592 struct led_classdev
*led
;
596 lg4ff_set_leds(hid
, 0);
598 name_sz
= strlen(dev_name(&hid
->dev
)) + 8;
600 for (j
= 0; j
< 5; j
++) {
601 led
= kzalloc(sizeof(struct led_classdev
)+name_sz
, GFP_KERNEL
);
603 hid_err(hid
, "can't allocate memory for LED %d\n", j
);
607 name
= (void *)(&led
[1]);
608 snprintf(name
, name_sz
, "%s::RPM%d", dev_name(&hid
->dev
), j
+1);
611 led
->max_brightness
= 1;
612 led
->brightness_get
= lg4ff_led_get_brightness
;
613 led
->brightness_set
= lg4ff_led_set_brightness
;
616 error
= led_classdev_register(&hid
->dev
, led
);
619 hid_err(hid
, "failed to register LED %d. Aborting.\n", j
);
621 /* Deregister LEDs (if any) */
622 for (j
= 0; j
< 5; j
++) {
624 entry
->led
[j
] = NULL
;
627 led_classdev_unregister(led
);
630 goto out
; /* Let the driver continue without LEDs */
636 hid_info(hid
, "Force feedback support for Logitech Gaming Wheels\n");
642 int lg4ff_deinit(struct hid_device
*hid
)
644 struct lg4ff_device_entry
*entry
;
645 struct lg_drv_data
*drv_data
;
647 device_remove_file(&hid
->dev
, &dev_attr_range
);
649 drv_data
= hid_get_drvdata(hid
);
651 hid_err(hid
, "Error while deinitializing device, no private driver data.\n");
654 entry
= drv_data
->device_props
;
656 hid_err(hid
, "Error while deinitializing device, no device properties data.\n");
660 #ifdef CONFIG_LEDS_CLASS
663 struct led_classdev
*led
;
665 /* Deregister LEDs (if any) */
666 for (j
= 0; j
< 5; j
++) {
669 entry
->led
[j
] = NULL
;
672 led_classdev_unregister(led
);
678 /* Deallocate memory */
681 dbg_hid("Device successfully unregistered\n");