blob: 8eb938d7aa02c223f3a05c454ec1fbfac5e6510e [file] [log] [blame]
Simon Wood32c88cb2010-09-22 13:19:42 +02001/*
2 * Force feedback support for Logitech Speed Force Wireless
3 *
4 * http://wiibrew.org/wiki/Logitech_USB_steering_wheel
5 *
6 * Copyright (c) 2010 Simon Wood <simon@mungewell.org>
7 */
8
9/*
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.
14 *
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.
19 *
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
23 */
24
25
26#include <linux/input.h>
27#include <linux/usb.h>
28#include <linux/hid.h>
29
30#include "usbhid/usbhid.h"
31#include "hid-lg.h"
Michal Malý7362cd22011-08-04 16:16:09 +020032#include "hid-ids.h"
Simon Wood32c88cb2010-09-22 13:19:42 +020033
Michal Malý96440c82011-08-04 16:18:11 +020034#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
44
Michal Malý7362cd22011-08-04 16:16:09 +020045static const signed short lg4ff_wheel_effects[] = {
Simon Wood32c88cb2010-09-22 13:19:42 +020046 FF_CONSTANT,
47 FF_AUTOCENTER,
48 -1
49};
50
Michal Malý7362cd22011-08-04 16:16:09 +020051struct lg4ff_wheel {
52 const __u32 product_id;
53 const signed short *ff_effects;
54 const __u16 min_range;
55 const __u16 max_range;
56};
57
58static 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}
67};
68
Michal Malý96440c82011-08-04 16:18:11 +020069struct lg4ff_native_cmd {
70 const __u8 cmd_num; /* Number of commands to send */
71 const __u8 cmd[];
72};
73
74struct lg4ff_usb_revision {
75 const __u16 rev_maj;
76 const __u16 rev_min;
77 const struct lg4ff_native_cmd *command;
78};
79
80static const struct lg4ff_native_cmd native_dfp = {
81 1,
82 {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
83};
84
85static const struct lg4ff_native_cmd native_dfgt = {
86 2,
87 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */
88 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* 2nd command */
89};
90
91static const struct lg4ff_native_cmd native_g25 = {
92 1,
93 {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
94};
95
96static const struct lg4ff_native_cmd native_g27 = {
97 2,
98 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */
99 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* 2nd command */
100};
101
102static 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 */
107};
108
Simon Wood32c88cb2010-09-22 13:19:42 +0200109static int hid_lg4ff_play(struct input_dev *dev, void *data,
110 struct ff_effect *effect)
111{
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);
115 int x;
116
117#define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff
118
119 switch (effect->type) {
120 case FF_CONSTANT:
121 x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */
122 CLAMP(x);
123 report->field[0]->value[0] = 0x11; /* Slot 1 */
Michal Malý7362cd22011-08-04 16:16:09 +0200124 report->field[0]->value[1] = 0x08;
Simon Wood32c88cb2010-09-22 13:19:42 +0200125 report->field[0]->value[2] = x;
Michal Malý7362cd22011-08-04 16:16:09 +0200126 report->field[0]->value[3] = 0x80;
Simon Wood32c88cb2010-09-22 13:19:42 +0200127 report->field[0]->value[4] = 0x00;
Michal Malý7362cd22011-08-04 16:16:09 +0200128 report->field[0]->value[5] = 0x00;
Simon Wood32c88cb2010-09-22 13:19:42 +0200129 report->field[0]->value[6] = 0x00;
Simon Wood32c88cb2010-09-22 13:19:42 +0200130
131 usbhid_submit_report(hid, report, USB_DIR_OUT);
132 break;
133 }
134 return 0;
135}
136
137static void hid_lg4ff_set_autocenter(struct input_dev *dev, u16 magnitude)
138{
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);
Simon Wood32c88cb2010-09-22 13:19:42 +0200142
Michal Malý7362cd22011-08-04 16:16:09 +0200143 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;
Simon Wood32c88cb2010-09-22 13:19:42 +0200150
151 usbhid_submit_report(hid, report, USB_DIR_OUT);
152}
153
Michal Malý96440c82011-08-04 16:18:11 +0200154static void hid_lg4ff_switch_native(struct hid_device *hid, const struct lg4ff_native_cmd *cmd)
155{
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);
158 __u8 i, j;
159
160 j = 0;
161 while (j < 7*cmd->cmd_num) {
162 for (i = 0; i < 7; i++)
163 report->field[0]->value[i] = cmd->cmd[j++];
164
165 usbhid_submit_report(hid, report, USB_DIR_OUT);
166 }
167}
Simon Wood32c88cb2010-09-22 13:19:42 +0200168
169int lg4ff_init(struct hid_device *hid)
170{
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;
Michal Malý96440c82011-08-04 16:18:11 +0200176 struct usb_device_descriptor *udesc = 0;
Michal Malý7362cd22011-08-04 16:16:09 +0200177 int error, i, j;
Michal Malý96440c82011-08-04 16:18:11 +0200178 __u16 bcdDevice, rev_maj, rev_min;
Simon Wood32c88cb2010-09-22 13:19:42 +0200179
180 /* Find the report to use */
181 if (list_empty(report_list)) {
Joe Perches4291ee32010-12-09 19:29:03 -0800182 hid_err(hid, "No output report found\n");
Simon Wood32c88cb2010-09-22 13:19:42 +0200183 return -1;
184 }
185
186 /* Check that the report looks ok */
187 report = list_entry(report_list->next, struct hid_report, list);
188 if (!report) {
Joe Perches4291ee32010-12-09 19:29:03 -0800189 hid_err(hid, "NULL output report\n");
Simon Wood32c88cb2010-09-22 13:19:42 +0200190 return -1;
191 }
192
193 field = report->field[0];
194 if (!field) {
Joe Perches4291ee32010-12-09 19:29:03 -0800195 hid_err(hid, "NULL field\n");
Simon Wood32c88cb2010-09-22 13:19:42 +0200196 return -1;
197 }
Michal Malý7362cd22011-08-04 16:16:09 +0200198
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);
203 break;
204 }
205 }
Simon Wood32c88cb2010-09-22 13:19:42 +0200206
Michal Malý7362cd22011-08-04 16:16:09 +0200207 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");
210 return -1;
211 }
212
Michal Malý96440c82011-08-04 16:18:11 +0200213 /* Attempt to switch wheel to native mode when applicable */
214 udesc = &(hid_to_usb_dev(hid)->descriptor);
215 if (!udesc) {
216 hid_err(hid, "NULL USB device descriptor\n");
217 return -1;
218 }
219 bcdDevice = le16_to_cpu(udesc->bcdDevice);
220 rev_maj = bcdDevice >> 8;
221 rev_min = bcdDevice & 0xff;
222
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);
226
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");
231 }
232 }
233 }
234
Michal Malý7362cd22011-08-04 16:16:09 +0200235 /* 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);
Simon Wood32c88cb2010-09-22 13:19:42 +0200238
239 error = input_ff_create_memless(dev, NULL, hid_lg4ff_play);
240
241 if (error)
242 return error;
243
244 if (test_bit(FF_AUTOCENTER, dev->ffbit))
245 dev->ff->set_autocenter = hid_lg4ff_set_autocenter;
246
Joe Perches4291ee32010-12-09 19:29:03 -0800247 hid_info(hid, "Force feedback for Logitech Speed Force Wireless by Simon Wood <simon@mungewell.org>\n");
Simon Wood32c88cb2010-09-22 13:19:42 +0200248 return 0;
249}
250