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 DFP_REV_MAJ 0x11
38 #define DFP_REV_MIN 0x06
39 #define FFEX_REV_MAJ 0x21
40 #define FFEX_REV_MIN 0x00
41 #define G25_REV_MAJ 0x12
42 #define G25_REV_MIN 0x22
43 #define G27_REV_MAJ 0x12
44 #define G27_REV_MIN 0x38
47 #define DFP_X_MAX 16383
48 #define DFP_PEDAL_MIN 0
49 #define DFP_PEDAL_MAX 255
51 #define to_hid_device(pdev) container_of(pdev, struct hid_device, dev)
53 static void hid_lg4ff_set_range_dfp(struct hid_device
*hid
, u16 range
);
54 static void hid_lg4ff_set_range_g25(struct hid_device
*hid
, u16 range
);
55 static ssize_t
lg4ff_range_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
);
56 static ssize_t
lg4ff_range_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
);
58 static DEVICE_ATTR(range
, S_IRWXU
| S_IRWXG
| S_IRWXO
, lg4ff_range_show
, lg4ff_range_store
);
60 struct lg4ff_device_entry
{
65 #ifdef CONFIG_LEDS_CLASS
67 struct led_classdev
*led
[5];
69 struct list_head list
;
70 void (*set_range
)(struct hid_device
*hid
, u16 range
);
73 static const signed short lg4ff_wheel_effects
[] = {
80 const __u32 product_id
;
81 const signed short *ff_effects
;
82 const __u16 min_range
;
83 const __u16 max_range
;
84 void (*set_range
)(struct hid_device
*hid
, u16 range
);
87 static const struct lg4ff_wheel lg4ff_devices
[] = {
88 {USB_DEVICE_ID_LOGITECH_WHEEL
, lg4ff_wheel_effects
, 40, 270, NULL
},
89 {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL
, lg4ff_wheel_effects
, 40, 270, NULL
},
90 {USB_DEVICE_ID_LOGITECH_DFP_WHEEL
, lg4ff_wheel_effects
, 40, 900, hid_lg4ff_set_range_dfp
},
91 {USB_DEVICE_ID_LOGITECH_G25_WHEEL
, lg4ff_wheel_effects
, 40, 900, hid_lg4ff_set_range_g25
},
92 {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL
, lg4ff_wheel_effects
, 40, 900, hid_lg4ff_set_range_g25
},
93 {USB_DEVICE_ID_LOGITECH_G27_WHEEL
, lg4ff_wheel_effects
, 40, 900, hid_lg4ff_set_range_g25
},
94 {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2
, lg4ff_wheel_effects
, 40, 270, NULL
},
95 {USB_DEVICE_ID_LOGITECH_WII_WHEEL
, lg4ff_wheel_effects
, 40, 270, NULL
}
98 struct lg4ff_native_cmd
{
99 const __u8 cmd_num
; /* Number of commands to send */
103 struct lg4ff_usb_revision
{
106 const struct lg4ff_native_cmd
*command
;
109 static const struct lg4ff_native_cmd native_dfp
= {
111 {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
114 static const struct lg4ff_native_cmd native_dfgt
= {
116 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */
117 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* 2nd command */
120 static const struct lg4ff_native_cmd native_g25
= {
122 {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
125 static const struct lg4ff_native_cmd native_g27
= {
127 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */
128 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* 2nd command */
131 static const struct lg4ff_usb_revision lg4ff_revs
[] = {
132 {DFGT_REV_MAJ
, DFGT_REV_MIN
, &native_dfgt
}, /* Driving Force GT */
133 {DFP_REV_MAJ
, DFP_REV_MIN
, &native_dfp
}, /* Driving Force Pro */
134 {G25_REV_MAJ
, G25_REV_MIN
, &native_g25
}, /* G25 */
135 {G27_REV_MAJ
, G27_REV_MIN
, &native_g27
}, /* G27 */
138 /* Recalculates X axis value accordingly to currently selected range */
139 static __s32
lg4ff_adjust_dfp_x_axis(__s32 value
, __u16 range
)
146 else if (range
== 200)
148 else if (range
< 200)
153 new_value
= 8192 + mult_frac(value
- 8192, max_range
, range
);
156 else if (new_value
> 16383)
162 int lg4ff_adjust_input_event(struct hid_device
*hid
, struct hid_field
*field
,
163 struct hid_usage
*usage
, __s32 value
, struct lg_drv_data
*drv_data
)
165 struct lg4ff_device_entry
*entry
= drv_data
->device_props
;
169 hid_err(hid
, "Device properties not found");
173 switch (entry
->product_id
) {
174 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL
:
175 switch (usage
->code
) {
177 new_value
= lg4ff_adjust_dfp_x_axis(value
, entry
->range
);
178 input_event(field
->hidinput
->input
, usage
->type
, usage
->code
, new_value
);
188 static int hid_lg4ff_play(struct input_dev
*dev
, void *data
, struct ff_effect
*effect
)
190 struct hid_device
*hid
= input_get_drvdata(dev
);
191 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
192 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
193 __s32
*value
= report
->field
[0]->value
;
196 #define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0)
198 switch (effect
->type
) {
200 x
= effect
->u
.ramp
.start_level
+ 0x80; /* 0x80 is no force */
202 value
[0] = 0x11; /* Slot 1 */
210 usbhid_submit_report(hid
, report
, USB_DIR_OUT
);
216 /* Sends default autocentering command compatible with
217 * all wheels except Formula Force EX */
218 static void hid_lg4ff_set_autocenter_default(struct input_dev
*dev
, u16 magnitude
)
220 struct hid_device
*hid
= input_get_drvdata(dev
);
221 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
222 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
223 __s32
*value
= report
->field
[0]->value
;
227 value
[2] = magnitude
>> 13;
228 value
[3] = magnitude
>> 13;
229 value
[4] = magnitude
>> 8;
233 usbhid_submit_report(hid
, report
, USB_DIR_OUT
);
236 /* Sends autocentering command compatible with Formula Force EX */
237 static void hid_lg4ff_set_autocenter_ffex(struct input_dev
*dev
, u16 magnitude
)
239 struct hid_device
*hid
= input_get_drvdata(dev
);
240 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
241 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
242 __s32
*value
= report
->field
[0]->value
;
243 magnitude
= magnitude
* 90 / 65535;
247 value
[2] = magnitude
>> 14;
248 value
[3] = magnitude
>> 14;
249 value
[4] = magnitude
;
253 usbhid_submit_report(hid
, report
, USB_DIR_OUT
);
256 /* Sends command to set range compatible with G25/G27/Driving Force GT */
257 static void hid_lg4ff_set_range_g25(struct hid_device
*hid
, u16 range
)
259 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
260 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
261 __s32
*value
= report
->field
[0]->value
;
263 dbg_hid("G25/G27/DFGT: setting range to %u\n", range
);
267 value
[2] = range
& 0x00ff;
268 value
[3] = (range
& 0xff00) >> 8;
273 usbhid_submit_report(hid
, report
, USB_DIR_OUT
);
276 /* Sends commands to set range compatible with Driving Force Pro wheel */
277 static void hid_lg4ff_set_range_dfp(struct hid_device
*hid
, __u16 range
)
279 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
280 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
281 int start_left
, start_right
, full_range
;
282 __s32
*value
= report
->field
[0]->value
;
284 dbg_hid("Driving Force Pro: setting range to %u\n", range
);
286 /* Prepare "coarse" limit command */
288 value
[1] = 0x00; /* Set later */
296 report
->field
[0]->value
[1] = 0x03;
299 report
->field
[0]->value
[1] = 0x02;
302 usbhid_submit_report(hid
, report
, USB_DIR_OUT
);
304 /* Prepare "fine" limit command */
313 if (range
== 200 || range
== 900) { /* Do not apply any fine limit */
314 usbhid_submit_report(hid
, report
, USB_DIR_OUT
);
318 /* Construct fine limit command */
319 start_left
= (((full_range
- range
+ 1) * 2047) / full_range
);
320 start_right
= 0xfff - start_left
;
322 value
[2] = start_left
>> 4;
323 value
[3] = start_right
>> 4;
325 value
[5] = (start_right
& 0xe) << 4 | (start_left
& 0xe);
328 usbhid_submit_report(hid
, report
, USB_DIR_OUT
);
331 static void hid_lg4ff_switch_native(struct hid_device
*hid
, const struct lg4ff_native_cmd
*cmd
)
333 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
334 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
338 while (j
< 7*cmd
->cmd_num
) {
339 for (i
= 0; i
< 7; i
++)
340 report
->field
[0]->value
[i
] = cmd
->cmd
[j
++];
342 usbhid_submit_report(hid
, report
, USB_DIR_OUT
);
346 /* Read current range and display it in terminal */
347 static ssize_t
lg4ff_range_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
349 struct hid_device
*hid
= to_hid_device(dev
);
350 struct lg4ff_device_entry
*entry
;
351 struct lg_drv_data
*drv_data
;
354 drv_data
= hid_get_drvdata(hid
);
356 hid_err(hid
, "Private driver data not found!\n");
360 entry
= drv_data
->device_props
;
362 hid_err(hid
, "Device properties not found!\n");
366 count
= scnprintf(buf
, PAGE_SIZE
, "%u\n", entry
->range
);
370 /* Set range to user specified value, call appropriate function
371 * according to the type of the wheel */
372 static ssize_t
lg4ff_range_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
374 struct hid_device
*hid
= to_hid_device(dev
);
375 struct lg4ff_device_entry
*entry
;
376 struct lg_drv_data
*drv_data
;
377 __u16 range
= simple_strtoul(buf
, NULL
, 10);
379 drv_data
= hid_get_drvdata(hid
);
381 hid_err(hid
, "Private driver data not found!\n");
385 entry
= drv_data
->device_props
;
387 hid_err(hid
, "Device properties not found!\n");
392 range
= entry
->max_range
;
394 /* Check if the wheel supports range setting
395 * and that the range is within limits for the wheel */
396 if (entry
->set_range
!= NULL
&& range
>= entry
->min_range
&& range
<= entry
->max_range
) {
397 entry
->set_range(hid
, range
);
398 entry
->range
= range
;
404 #ifdef CONFIG_LEDS_CLASS
405 static void lg4ff_set_leds(struct hid_device
*hid
, __u8 leds
)
407 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
408 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
409 __s32
*value
= report
->field
[0]->value
;
418 usbhid_submit_report(hid
, report
, USB_DIR_OUT
);
421 static void lg4ff_led_set_brightness(struct led_classdev
*led_cdev
,
422 enum led_brightness value
)
424 struct device
*dev
= led_cdev
->dev
->parent
;
425 struct hid_device
*hid
= container_of(dev
, struct hid_device
, dev
);
426 struct lg_drv_data
*drv_data
= hid_get_drvdata(hid
);
427 struct lg4ff_device_entry
*entry
;
431 hid_err(hid
, "Device data not found.");
435 entry
= (struct lg4ff_device_entry
*)drv_data
->device_props
;
438 hid_err(hid
, "Device properties not found.");
442 for (i
= 0; i
< 5; i
++) {
443 if (led_cdev
!= entry
->led
[i
])
445 state
= (entry
->led_state
>> i
) & 1;
446 if (value
== LED_OFF
&& state
) {
447 entry
->led_state
&= ~(1 << i
);
448 lg4ff_set_leds(hid
, entry
->led_state
);
449 } else if (value
!= LED_OFF
&& !state
) {
450 entry
->led_state
|= 1 << i
;
451 lg4ff_set_leds(hid
, entry
->led_state
);
457 static enum led_brightness
lg4ff_led_get_brightness(struct led_classdev
*led_cdev
)
459 struct device
*dev
= led_cdev
->dev
->parent
;
460 struct hid_device
*hid
= container_of(dev
, struct hid_device
, dev
);
461 struct lg_drv_data
*drv_data
= hid_get_drvdata(hid
);
462 struct lg4ff_device_entry
*entry
;
466 hid_err(hid
, "Device data not found.");
470 entry
= (struct lg4ff_device_entry
*)drv_data
->device_props
;
473 hid_err(hid
, "Device properties not found.");
477 for (i
= 0; i
< 5; i
++)
478 if (led_cdev
== entry
->led
[i
]) {
479 value
= (entry
->led_state
>> i
) & 1;
483 return value
? LED_FULL
: LED_OFF
;
487 int lg4ff_init(struct hid_device
*hid
)
489 struct hid_input
*hidinput
= list_entry(hid
->inputs
.next
, struct hid_input
, list
);
490 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
491 struct input_dev
*dev
= hidinput
->input
;
492 struct hid_report
*report
;
493 struct hid_field
*field
;
494 struct lg4ff_device_entry
*entry
;
495 struct lg_drv_data
*drv_data
;
496 struct usb_device_descriptor
*udesc
;
498 __u16 bcdDevice
, rev_maj
, rev_min
;
500 /* Find the report to use */
501 if (list_empty(report_list
)) {
502 hid_err(hid
, "No output report found\n");
506 /* Check that the report looks ok */
507 report
= list_entry(report_list
->next
, struct hid_report
, list
);
509 hid_err(hid
, "NULL output report\n");
513 field
= report
->field
[0];
515 hid_err(hid
, "NULL field\n");
519 /* Check what wheel has been connected */
520 for (i
= 0; i
< ARRAY_SIZE(lg4ff_devices
); i
++) {
521 if (hid
->product
== lg4ff_devices
[i
].product_id
) {
522 dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices
[i
].product_id
);
527 if (i
== ARRAY_SIZE(lg4ff_devices
)) {
528 hid_err(hid
, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to"
529 "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n");
533 /* Attempt to switch wheel to native mode when applicable */
534 udesc
= &(hid_to_usb_dev(hid
)->descriptor
);
536 hid_err(hid
, "NULL USB device descriptor\n");
539 bcdDevice
= le16_to_cpu(udesc
->bcdDevice
);
540 rev_maj
= bcdDevice
>> 8;
541 rev_min
= bcdDevice
& 0xff;
543 if (lg4ff_devices
[i
].product_id
== USB_DEVICE_ID_LOGITECH_WHEEL
) {
544 dbg_hid("Generic wheel detected, can it do native?\n");
545 dbg_hid("USB revision: %2x.%02x\n", rev_maj
, rev_min
);
547 for (j
= 0; j
< ARRAY_SIZE(lg4ff_revs
); j
++) {
548 if (lg4ff_revs
[j
].rev_maj
== rev_maj
&& lg4ff_revs
[j
].rev_min
== rev_min
) {
549 hid_lg4ff_switch_native(hid
, lg4ff_revs
[j
].command
);
550 hid_info(hid
, "Switched to native mode\n");
555 /* Set supported force feedback capabilities */
556 for (j
= 0; lg4ff_devices
[i
].ff_effects
[j
] >= 0; j
++)
557 set_bit(lg4ff_devices
[i
].ff_effects
[j
], dev
->ffbit
);
559 error
= input_ff_create_memless(dev
, NULL
, hid_lg4ff_play
);
564 /* Check if autocentering is available and
565 * set the centering force to zero by default */
566 if (test_bit(FF_AUTOCENTER
, dev
->ffbit
)) {
567 if (rev_maj
== FFEX_REV_MAJ
&& rev_min
== FFEX_REV_MIN
) /* Formula Force EX expects different autocentering command */
568 dev
->ff
->set_autocenter
= hid_lg4ff_set_autocenter_ffex
;
570 dev
->ff
->set_autocenter
= hid_lg4ff_set_autocenter_default
;
572 dev
->ff
->set_autocenter(dev
, 0);
575 /* Get private driver data */
576 drv_data
= hid_get_drvdata(hid
);
578 hid_err(hid
, "Cannot add device, private driver data not allocated\n");
582 /* Initialize device properties */
583 entry
= kzalloc(sizeof(struct lg4ff_device_entry
), GFP_KERNEL
);
585 hid_err(hid
, "Cannot add device, insufficient memory to allocate device properties.\n");
588 drv_data
->device_props
= entry
;
590 entry
->product_id
= lg4ff_devices
[i
].product_id
;
591 entry
->min_range
= lg4ff_devices
[i
].min_range
;
592 entry
->max_range
= lg4ff_devices
[i
].max_range
;
593 entry
->set_range
= lg4ff_devices
[i
].set_range
;
595 /* Create sysfs interface */
596 error
= device_create_file(&hid
->dev
, &dev_attr_range
);
599 dbg_hid("sysfs interface created\n");
601 /* Set default axes parameters */
602 switch (lg4ff_devices
[i
].product_id
) {
603 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL
:
604 dbg_hid("Setting axes parameters for Driving Force Pro\n");
605 input_set_abs_params(dev
, ABS_X
, DFP_X_MIN
, DFP_X_MAX
, 0, 0);
606 input_set_abs_params(dev
, ABS_Y
, DFP_PEDAL_MIN
, DFP_PEDAL_MAX
, 0, 0);
607 input_set_abs_params(dev
, ABS_RZ
, DFP_PEDAL_MIN
, DFP_PEDAL_MAX
, 0, 0);
613 /* Set the maximum range to start with */
614 entry
->range
= entry
->max_range
;
615 if (entry
->set_range
!= NULL
)
616 entry
->set_range(hid
, entry
->range
);
618 #ifdef CONFIG_LEDS_CLASS
619 /* register led subsystem - G27 only */
620 entry
->led_state
= 0;
621 for (j
= 0; j
< 5; j
++)
622 entry
->led
[j
] = NULL
;
624 if (lg4ff_devices
[i
].product_id
== USB_DEVICE_ID_LOGITECH_G27_WHEEL
) {
625 struct led_classdev
*led
;
629 lg4ff_set_leds(hid
, 0);
631 name_sz
= strlen(dev_name(&hid
->dev
)) + 8;
633 for (j
= 0; j
< 5; j
++) {
634 led
= kzalloc(sizeof(struct led_classdev
)+name_sz
, GFP_KERNEL
);
636 hid_err(hid
, "can't allocate memory for LED %d\n", j
);
640 name
= (void *)(&led
[1]);
641 snprintf(name
, name_sz
, "%s::RPM%d", dev_name(&hid
->dev
), j
+1);
644 led
->max_brightness
= 1;
645 led
->brightness_get
= lg4ff_led_get_brightness
;
646 led
->brightness_set
= lg4ff_led_set_brightness
;
649 error
= led_classdev_register(&hid
->dev
, led
);
652 hid_err(hid
, "failed to register LED %d. Aborting.\n", j
);
654 /* Deregister LEDs (if any) */
655 for (j
= 0; j
< 5; j
++) {
657 entry
->led
[j
] = NULL
;
660 led_classdev_unregister(led
);
663 goto out
; /* Let the driver continue without LEDs */
669 hid_info(hid
, "Force feedback support for Logitech Gaming Wheels\n");
675 int lg4ff_deinit(struct hid_device
*hid
)
677 struct lg4ff_device_entry
*entry
;
678 struct lg_drv_data
*drv_data
;
680 device_remove_file(&hid
->dev
, &dev_attr_range
);
682 drv_data
= hid_get_drvdata(hid
);
684 hid_err(hid
, "Error while deinitializing device, no private driver data.\n");
687 entry
= drv_data
->device_props
;
689 hid_err(hid
, "Error while deinitializing device, no device properties data.\n");
693 #ifdef CONFIG_LEDS_CLASS
696 struct led_classdev
*led
;
698 /* Deregister LEDs (if any) */
699 for (j
= 0; j
< 5; j
++) {
702 entry
->led
[j
] = NULL
;
705 led_classdev_unregister(led
);
711 /* Deallocate memory */
714 dbg_hid("Device successfully unregistered\n");