2 * Force feedback support for Logitech Speed Force Wireless
4 * http://wiibrew.org/wiki/Logitech_USB_steering_wheel
6 * Copyright (c) 2010 Simon Wood <simon@mungewell.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <linux/input.h>
27 #include <linux/usb.h>
28 #include <linux/hid.h>
30 #include "usbhid/usbhid.h"
34 #define DFGT_REV_MAJ 0x13
35 #define DFGT_REV_MIN 0x22
36 #define DFP_REV_MAJ 0x11
37 #define DFP_REV_MIN 0x06
38 #define FFEX_REV_MAJ 0x21
39 #define FFEX_REV_MIN 0x00
40 #define G25_REV_MAJ 0x12
41 #define G25_REV_MIN 0x22
42 #define G27_REV_MAJ 0x12
43 #define G27_REV_MIN 0x38
45 static const signed short lg4ff_wheel_effects[] = {
52 const __u32 product_id;
53 const signed short *ff_effects;
54 const __u16 min_range;
55 const __u16 max_range;
58 static const struct lg4ff_wheel lg4ff_devices[] = {
59 {USB_DEVICE_ID_LOGITECH_WHEEL, lg4ff_wheel_effects, 40, 270},
60 {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL, lg4ff_wheel_effects, 40, 270},
61 {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, lg4ff_wheel_effects, 40, 900},
62 {USB_DEVICE_ID_LOGITECH_G25_WHEEL, lg4ff_wheel_effects, 40, 900},
63 {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, lg4ff_wheel_effects, 40, 900},
64 {USB_DEVICE_ID_LOGITECH_G27_WHEEL, lg4ff_wheel_effects, 40, 900},
65 {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270},
66 {USB_DEVICE_ID_LOGITECH_WII_WHEEL, lg4ff_wheel_effects, 40, 270}
69 struct lg4ff_native_cmd {
70 const __u8 cmd_num; /* Number of commands to send */
74 struct lg4ff_usb_revision {
77 const struct lg4ff_native_cmd *command;
80 static const struct lg4ff_native_cmd native_dfp = {
82 {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
85 static const struct lg4ff_native_cmd native_dfgt = {
87 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */
88 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* 2nd command */
91 static const struct lg4ff_native_cmd native_g25 = {
93 {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
96 static const struct lg4ff_native_cmd native_g27 = {
98 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */
99 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* 2nd command */
102 static const struct lg4ff_usb_revision lg4ff_revs[] = {
103 {DFGT_REV_MAJ, DFGT_REV_MIN, &native_dfgt}, /* Driving Force GT */
104 {DFP_REV_MAJ, DFP_REV_MIN, &native_dfp}, /* Driving Force Pro */
105 {G25_REV_MAJ, G25_REV_MIN, &native_g25}, /* G25 */
106 {G27_REV_MAJ, G27_REV_MIN, &native_g27}, /* G27 */
109 static int hid_lg4ff_play(struct input_dev *dev, void *data,
110 struct ff_effect *effect)
112 struct hid_device *hid = input_get_drvdata(dev);
113 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
114 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
117 #define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff
119 switch (effect->type) {
121 x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */
123 report->field[0]->value[0] = 0x11; /* Slot 1 */
124 report->field[0]->value[1] = 0x08;
125 report->field[0]->value[2] = x;
126 report->field[0]->value[3] = 0x80;
127 report->field[0]->value[4] = 0x00;
128 report->field[0]->value[5] = 0x00;
129 report->field[0]->value[6] = 0x00;
131 usbhid_submit_report(hid, report, USB_DIR_OUT);
137 static void hid_lg4ff_set_autocenter(struct input_dev *dev, u16 magnitude)
139 struct hid_device *hid = input_get_drvdata(dev);
140 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
141 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
143 report->field[0]->value[0] = 0xfe;
144 report->field[0]->value[1] = 0x0d;
145 report->field[0]->value[2] = magnitude >> 13;
146 report->field[0]->value[3] = magnitude >> 13;
147 report->field[0]->value[4] = magnitude >> 8;
148 report->field[0]->value[5] = 0x00;
149 report->field[0]->value[6] = 0x00;
151 usbhid_submit_report(hid, report, USB_DIR_OUT);
154 static void hid_lg4ff_switch_native(struct hid_device *hid, const struct lg4ff_native_cmd *cmd)
156 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
157 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
161 while (j < 7*cmd->cmd_num) {
162 for (i = 0; i < 7; i++)
163 report->field[0]->value[i] = cmd->cmd[j++];
165 usbhid_submit_report(hid, report, USB_DIR_OUT);
169 int lg4ff_init(struct hid_device *hid)
171 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
172 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
173 struct input_dev *dev = hidinput->input;
174 struct hid_report *report;
175 struct hid_field *field;
176 struct usb_device_descriptor *udesc = 0;
178 __u16 bcdDevice, rev_maj, rev_min;
180 /* Find the report to use */
181 if (list_empty(report_list)) {
182 hid_err(hid, "No output report found\n");
186 /* Check that the report looks ok */
187 report = list_entry(report_list->next, struct hid_report, list);
189 hid_err(hid, "NULL output report\n");
193 field = report->field[0];
195 hid_err(hid, "NULL field\n");
199 /* Check what wheel has been connected */
200 for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
201 if (hid->product == lg4ff_devices[i].product_id) {
202 dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
207 if (i == ARRAY_SIZE(lg4ff_devices)) {
208 hid_err(hid, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to"
209 "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n");
213 /* Attempt to switch wheel to native mode when applicable */
214 udesc = &(hid_to_usb_dev(hid)->descriptor);
216 hid_err(hid, "NULL USB device descriptor\n");
219 bcdDevice = le16_to_cpu(udesc->bcdDevice);
220 rev_maj = bcdDevice >> 8;
221 rev_min = bcdDevice & 0xff;
223 if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_WHEEL) {
224 dbg_hid("Generic wheel detected, can it do native?\n");
225 dbg_hid("USB revision: %2x.%02x\n", rev_maj, rev_min);
227 for (j = 0; j < ARRAY_SIZE(lg4ff_revs); j++) {
228 if (lg4ff_revs[j].rev_maj == rev_maj && lg4ff_revs[j].rev_min == rev_min) {
229 hid_lg4ff_switch_native(hid, lg4ff_revs[j].command);
230 hid_info(hid, "Switched to native mode\n");
235 /* Set supported force feedback capabilities */
236 for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
237 set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
239 error = input_ff_create_memless(dev, NULL, hid_lg4ff_play);
244 if (test_bit(FF_AUTOCENTER, dev->ffbit))
245 dev->ff->set_autocenter = hid_lg4ff_set_autocenter;
247 hid_info(hid, "Force feedback for Logitech Speed Force Wireless by Simon Wood <simon@mungewell.org>\n");