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
46 #define to_hid_device(pdev) container_of(pdev, struct hid_device, dev)
48 static void hid_lg4ff_set_range_dfp(struct hid_device
*hid
, u16 range
);
49 static void hid_lg4ff_set_range_g25(struct hid_device
*hid
, u16 range
);
50 static ssize_t
lg4ff_range_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
);
51 static ssize_t
lg4ff_range_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
);
53 static DEVICE_ATTR(range
, S_IRWXU
| S_IRWXG
| S_IRWXO
, lg4ff_range_show
, lg4ff_range_store
);
55 struct lg4ff_device_entry
{
60 #ifdef CONFIG_LEDS_CLASS
62 struct led_classdev
*led
[5];
64 struct list_head list
;
65 void (*set_range
)(struct hid_device
*hid
, u16 range
);
68 static const signed short lg4ff_wheel_effects
[] = {
75 const __u32 product_id
;
76 const signed short *ff_effects
;
77 const __u16 min_range
;
78 const __u16 max_range
;
79 void (*set_range
)(struct hid_device
*hid
, u16 range
);
82 static const struct lg4ff_wheel lg4ff_devices
[] = {
83 {USB_DEVICE_ID_LOGITECH_WHEEL
, lg4ff_wheel_effects
, 40, 270, NULL
},
84 {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL
, lg4ff_wheel_effects
, 40, 270, NULL
},
85 {USB_DEVICE_ID_LOGITECH_DFP_WHEEL
, lg4ff_wheel_effects
, 40, 900, hid_lg4ff_set_range_dfp
},
86 {USB_DEVICE_ID_LOGITECH_G25_WHEEL
, lg4ff_wheel_effects
, 40, 900, hid_lg4ff_set_range_g25
},
87 {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL
, lg4ff_wheel_effects
, 40, 900, hid_lg4ff_set_range_g25
},
88 {USB_DEVICE_ID_LOGITECH_G27_WHEEL
, lg4ff_wheel_effects
, 40, 900, hid_lg4ff_set_range_g25
},
89 {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2
, lg4ff_wheel_effects
, 40, 270, NULL
},
90 {USB_DEVICE_ID_LOGITECH_WII_WHEEL
, lg4ff_wheel_effects
, 40, 270, NULL
}
93 struct lg4ff_native_cmd
{
94 const __u8 cmd_num
; /* Number of commands to send */
98 struct lg4ff_usb_revision
{
101 const struct lg4ff_native_cmd
*command
;
104 static const struct lg4ff_native_cmd native_dfp
= {
106 {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
109 static const struct lg4ff_native_cmd native_dfgt
= {
111 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */
112 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* 2nd command */
115 static const struct lg4ff_native_cmd native_g25
= {
117 {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
120 static const struct lg4ff_native_cmd native_g27
= {
122 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */
123 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* 2nd command */
126 static const struct lg4ff_usb_revision lg4ff_revs
[] = {
127 {DFGT_REV_MAJ
, DFGT_REV_MIN
, &native_dfgt
}, /* Driving Force GT */
128 {DFP_REV_MAJ
, DFP_REV_MIN
, &native_dfp
}, /* Driving Force Pro */
129 {G25_REV_MAJ
, G25_REV_MIN
, &native_g25
}, /* G25 */
130 {G27_REV_MAJ
, G27_REV_MIN
, &native_g27
}, /* G27 */
133 /* Recalculates X axis value accordingly to currently selected range */
134 static __s32
lg4ff_adjust_dfp_x_axis(__s32 value
, __u16 range
)
141 else if (range
== 200)
143 else if (range
< 200)
148 new_value
= 8192 + mult_frac(value
- 8192, max_range
, range
);
151 else if (new_value
> 16383)
157 int lg4ff_adjust_input_event(struct hid_device
*hid
, struct hid_field
*field
,
158 struct hid_usage
*usage
, __s32 value
, struct lg_drv_data
*drv_data
)
160 struct lg4ff_device_entry
*entry
= drv_data
->device_props
;
164 hid_err(hid
, "Device properties not found");
168 switch (entry
->product_id
) {
169 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL
:
170 switch (usage
->code
) {
172 new_value
= lg4ff_adjust_dfp_x_axis(value
, entry
->range
);
173 input_event(field
->hidinput
->input
, usage
->type
, usage
->code
, new_value
);
183 static int hid_lg4ff_play(struct input_dev
*dev
, void *data
, struct ff_effect
*effect
)
185 struct hid_device
*hid
= input_get_drvdata(dev
);
186 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
187 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
188 __s32
*value
= report
->field
[0]->value
;
191 #define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0)
193 switch (effect
->type
) {
195 x
= effect
->u
.ramp
.start_level
+ 0x80; /* 0x80 is no force */
197 value
[0] = 0x11; /* Slot 1 */
205 usbhid_submit_report(hid
, report
, USB_DIR_OUT
);
211 /* Sends default autocentering command compatible with
212 * all wheels except Formula Force EX */
213 static void hid_lg4ff_set_autocenter_default(struct input_dev
*dev
, u16 magnitude
)
215 struct hid_device
*hid
= input_get_drvdata(dev
);
216 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
217 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
218 __s32
*value
= report
->field
[0]->value
;
222 value
[2] = magnitude
>> 13;
223 value
[3] = magnitude
>> 13;
224 value
[4] = magnitude
>> 8;
228 usbhid_submit_report(hid
, report
, USB_DIR_OUT
);
231 /* Sends autocentering command compatible with Formula Force EX */
232 static void hid_lg4ff_set_autocenter_ffex(struct input_dev
*dev
, u16 magnitude
)
234 struct hid_device
*hid
= input_get_drvdata(dev
);
235 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
236 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
237 __s32
*value
= report
->field
[0]->value
;
238 magnitude
= magnitude
* 90 / 65535;
242 value
[2] = magnitude
>> 14;
243 value
[3] = magnitude
>> 14;
244 value
[4] = magnitude
;
248 usbhid_submit_report(hid
, report
, USB_DIR_OUT
);
251 /* Sends command to set range compatible with G25/G27/Driving Force GT */
252 static void hid_lg4ff_set_range_g25(struct hid_device
*hid
, u16 range
)
254 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
255 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
256 __s32
*value
= report
->field
[0]->value
;
258 dbg_hid("G25/G27/DFGT: setting range to %u\n", range
);
262 value
[2] = range
& 0x00ff;
263 value
[3] = (range
& 0xff00) >> 8;
268 usbhid_submit_report(hid
, report
, USB_DIR_OUT
);
271 /* Sends commands to set range compatible with Driving Force Pro wheel */
272 static void hid_lg4ff_set_range_dfp(struct hid_device
*hid
, __u16 range
)
274 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
275 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
276 int start_left
, start_right
, full_range
;
277 __s32
*value
= report
->field
[0]->value
;
279 dbg_hid("Driving Force Pro: setting range to %u\n", range
);
281 /* Prepare "coarse" limit command */
283 value
[1] = 0x00; /* Set later */
291 report
->field
[0]->value
[1] = 0x03;
294 report
->field
[0]->value
[1] = 0x02;
297 usbhid_submit_report(hid
, report
, USB_DIR_OUT
);
299 /* Prepare "fine" limit command */
308 if (range
== 200 || range
== 900) { /* Do not apply any fine limit */
309 usbhid_submit_report(hid
, report
, USB_DIR_OUT
);
313 /* Construct fine limit command */
314 start_left
= (((full_range
- range
+ 1) * 2047) / full_range
);
315 start_right
= 0xfff - start_left
;
317 value
[2] = start_left
>> 4;
318 value
[3] = start_right
>> 4;
320 value
[5] = (start_right
& 0xe) << 4 | (start_left
& 0xe);
323 usbhid_submit_report(hid
, report
, USB_DIR_OUT
);
326 static void hid_lg4ff_switch_native(struct hid_device
*hid
, const struct lg4ff_native_cmd
*cmd
)
328 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
329 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
333 while (j
< 7*cmd
->cmd_num
) {
334 for (i
= 0; i
< 7; i
++)
335 report
->field
[0]->value
[i
] = cmd
->cmd
[j
++];
337 usbhid_submit_report(hid
, report
, USB_DIR_OUT
);
341 /* Read current range and display it in terminal */
342 static ssize_t
lg4ff_range_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
344 struct hid_device
*hid
= to_hid_device(dev
);
345 struct lg4ff_device_entry
*entry
;
346 struct lg_drv_data
*drv_data
;
349 drv_data
= hid_get_drvdata(hid
);
351 hid_err(hid
, "Private driver data not found!\n");
355 entry
= drv_data
->device_props
;
357 hid_err(hid
, "Device properties not found!\n");
361 count
= scnprintf(buf
, PAGE_SIZE
, "%u\n", entry
->range
);
365 /* Set range to user specified value, call appropriate function
366 * according to the type of the wheel */
367 static ssize_t
lg4ff_range_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
369 struct hid_device
*hid
= to_hid_device(dev
);
370 struct lg4ff_device_entry
*entry
;
371 struct lg_drv_data
*drv_data
;
372 __u16 range
= simple_strtoul(buf
, NULL
, 10);
374 drv_data
= hid_get_drvdata(hid
);
376 hid_err(hid
, "Private driver data not found!\n");
380 entry
= drv_data
->device_props
;
382 hid_err(hid
, "Device properties not found!\n");
387 range
= entry
->max_range
;
389 /* Check if the wheel supports range setting
390 * and that the range is within limits for the wheel */
391 if (entry
->set_range
!= NULL
&& range
>= entry
->min_range
&& range
<= entry
->max_range
) {
392 entry
->set_range(hid
, range
);
393 entry
->range
= range
;
399 #ifdef CONFIG_LEDS_CLASS
400 static void lg4ff_set_leds(struct hid_device
*hid
, __u8 leds
)
402 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
403 struct hid_report
*report
= list_entry(report_list
->next
, struct hid_report
, list
);
404 __s32
*value
= report
->field
[0]->value
;
413 usbhid_submit_report(hid
, report
, USB_DIR_OUT
);
416 static void lg4ff_led_set_brightness(struct led_classdev
*led_cdev
,
417 enum led_brightness value
)
419 struct device
*dev
= led_cdev
->dev
->parent
;
420 struct hid_device
*hid
= container_of(dev
, struct hid_device
, dev
);
421 struct lg_drv_data
*drv_data
= hid_get_drvdata(hid
);
422 struct lg4ff_device_entry
*entry
;
426 hid_err(hid
, "Device data not found.");
430 entry
= (struct lg4ff_device_entry
*)drv_data
->device_props
;
433 hid_err(hid
, "Device properties not found.");
437 for (i
= 0; i
< 5; i
++) {
438 if (led_cdev
!= entry
->led
[i
])
440 state
= (entry
->led_state
>> i
) & 1;
441 if (value
== LED_OFF
&& state
) {
442 entry
->led_state
&= ~(1 << i
);
443 lg4ff_set_leds(hid
, entry
->led_state
);
444 } else if (value
!= LED_OFF
&& !state
) {
445 entry
->led_state
|= 1 << i
;
446 lg4ff_set_leds(hid
, entry
->led_state
);
452 static enum led_brightness
lg4ff_led_get_brightness(struct led_classdev
*led_cdev
)
454 struct device
*dev
= led_cdev
->dev
->parent
;
455 struct hid_device
*hid
= container_of(dev
, struct hid_device
, dev
);
456 struct lg_drv_data
*drv_data
= hid_get_drvdata(hid
);
457 struct lg4ff_device_entry
*entry
;
461 hid_err(hid
, "Device data not found.");
465 entry
= (struct lg4ff_device_entry
*)drv_data
->device_props
;
468 hid_err(hid
, "Device properties not found.");
472 for (i
= 0; i
< 5; i
++)
473 if (led_cdev
== entry
->led
[i
]) {
474 value
= (entry
->led_state
>> i
) & 1;
478 return value
? LED_FULL
: LED_OFF
;
482 int lg4ff_init(struct hid_device
*hid
)
484 struct hid_input
*hidinput
= list_entry(hid
->inputs
.next
, struct hid_input
, list
);
485 struct list_head
*report_list
= &hid
->report_enum
[HID_OUTPUT_REPORT
].report_list
;
486 struct input_dev
*dev
= hidinput
->input
;
487 struct hid_report
*report
;
488 struct hid_field
*field
;
489 struct lg4ff_device_entry
*entry
;
490 struct lg_drv_data
*drv_data
;
491 struct usb_device_descriptor
*udesc
;
493 __u16 bcdDevice
, rev_maj
, rev_min
;
495 /* Find the report to use */
496 if (list_empty(report_list
)) {
497 hid_err(hid
, "No output report found\n");
501 /* Check that the report looks ok */
502 report
= list_entry(report_list
->next
, struct hid_report
, list
);
504 hid_err(hid
, "NULL output report\n");
508 field
= report
->field
[0];
510 hid_err(hid
, "NULL field\n");
514 /* Check what wheel has been connected */
515 for (i
= 0; i
< ARRAY_SIZE(lg4ff_devices
); i
++) {
516 if (hid
->product
== lg4ff_devices
[i
].product_id
) {
517 dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices
[i
].product_id
);
522 if (i
== ARRAY_SIZE(lg4ff_devices
)) {
523 hid_err(hid
, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to"
524 "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n");
528 /* Attempt to switch wheel to native mode when applicable */
529 udesc
= &(hid_to_usb_dev(hid
)->descriptor
);
531 hid_err(hid
, "NULL USB device descriptor\n");
534 bcdDevice
= le16_to_cpu(udesc
->bcdDevice
);
535 rev_maj
= bcdDevice
>> 8;
536 rev_min
= bcdDevice
& 0xff;
538 if (lg4ff_devices
[i
].product_id
== USB_DEVICE_ID_LOGITECH_WHEEL
) {
539 dbg_hid("Generic wheel detected, can it do native?\n");
540 dbg_hid("USB revision: %2x.%02x\n", rev_maj
, rev_min
);
542 for (j
= 0; j
< ARRAY_SIZE(lg4ff_revs
); j
++) {
543 if (lg4ff_revs
[j
].rev_maj
== rev_maj
&& lg4ff_revs
[j
].rev_min
== rev_min
) {
544 hid_lg4ff_switch_native(hid
, lg4ff_revs
[j
].command
);
545 hid_info(hid
, "Switched to native mode\n");
550 /* Set supported force feedback capabilities */
551 for (j
= 0; lg4ff_devices
[i
].ff_effects
[j
] >= 0; j
++)
552 set_bit(lg4ff_devices
[i
].ff_effects
[j
], dev
->ffbit
);
554 error
= input_ff_create_memless(dev
, NULL
, hid_lg4ff_play
);
559 /* Check if autocentering is available and
560 * set the centering force to zero by default */
561 if (test_bit(FF_AUTOCENTER
, dev
->ffbit
)) {
562 if (rev_maj
== FFEX_REV_MAJ
&& rev_min
== FFEX_REV_MIN
) /* Formula Force EX expects different autocentering command */
563 dev
->ff
->set_autocenter
= hid_lg4ff_set_autocenter_ffex
;
565 dev
->ff
->set_autocenter
= hid_lg4ff_set_autocenter_default
;
567 dev
->ff
->set_autocenter(dev
, 0);
570 /* Get private driver data */
571 drv_data
= hid_get_drvdata(hid
);
573 hid_err(hid
, "Cannot add device, private driver data not allocated\n");
577 /* Initialize device properties */
578 entry
= kzalloc(sizeof(struct lg4ff_device_entry
), GFP_KERNEL
);
580 hid_err(hid
, "Cannot add device, insufficient memory to allocate device properties.\n");
583 drv_data
->device_props
= entry
;
585 entry
->product_id
= lg4ff_devices
[i
].product_id
;
586 entry
->min_range
= lg4ff_devices
[i
].min_range
;
587 entry
->max_range
= lg4ff_devices
[i
].max_range
;
588 entry
->set_range
= lg4ff_devices
[i
].set_range
;
590 /* Create sysfs interface */
591 error
= device_create_file(&hid
->dev
, &dev_attr_range
);
594 dbg_hid("sysfs interface created\n");
596 /* Set the maximum range to start with */
597 entry
->range
= entry
->max_range
;
598 if (entry
->set_range
!= NULL
)
599 entry
->set_range(hid
, entry
->range
);
601 #ifdef CONFIG_LEDS_CLASS
602 /* register led subsystem - G27 only */
603 entry
->led_state
= 0;
604 for (j
= 0; j
< 5; j
++)
605 entry
->led
[j
] = NULL
;
607 if (lg4ff_devices
[i
].product_id
== USB_DEVICE_ID_LOGITECH_G27_WHEEL
) {
608 struct led_classdev
*led
;
612 lg4ff_set_leds(hid
, 0);
614 name_sz
= strlen(dev_name(&hid
->dev
)) + 8;
616 for (j
= 0; j
< 5; j
++) {
617 led
= kzalloc(sizeof(struct led_classdev
)+name_sz
, GFP_KERNEL
);
619 hid_err(hid
, "can't allocate memory for LED %d\n", j
);
623 name
= (void *)(&led
[1]);
624 snprintf(name
, name_sz
, "%s::RPM%d", dev_name(&hid
->dev
), j
+1);
627 led
->max_brightness
= 1;
628 led
->brightness_get
= lg4ff_led_get_brightness
;
629 led
->brightness_set
= lg4ff_led_set_brightness
;
632 error
= led_classdev_register(&hid
->dev
, led
);
635 hid_err(hid
, "failed to register LED %d. Aborting.\n", j
);
637 /* Deregister LEDs (if any) */
638 for (j
= 0; j
< 5; j
++) {
640 entry
->led
[j
] = NULL
;
643 led_classdev_unregister(led
);
646 goto out
; /* Let the driver continue without LEDs */
652 hid_info(hid
, "Force feedback support for Logitech Gaming Wheels\n");
658 int lg4ff_deinit(struct hid_device
*hid
)
660 struct lg4ff_device_entry
*entry
;
661 struct lg_drv_data
*drv_data
;
663 device_remove_file(&hid
->dev
, &dev_attr_range
);
665 drv_data
= hid_get_drvdata(hid
);
667 hid_err(hid
, "Error while deinitializing device, no private driver data.\n");
670 entry
= drv_data
->device_props
;
672 hid_err(hid
, "Error while deinitializing device, no device properties data.\n");
676 #ifdef CONFIG_LEDS_CLASS
679 struct led_classdev
*led
;
681 /* Deregister LEDs (if any) */
682 for (j
= 0; j
< 5; j
++) {
685 entry
->led
[j
] = NULL
;
688 led_classdev_unregister(led
);
694 /* Deallocate memory */
697 dbg_hid("Device successfully unregistered\n");