blob: 854982be31949fff82abb7d13eca6e26927a8e04 [file] [log] [blame]
Simon Wood32c88cb2010-09-22 13:19:42 +02001/*
Simon Wood64013802012-04-21 05:41:16 -07002 * Force feedback support for Logitech Gaming Wheels
Simon Wood32c88cb2010-09-22 13:19:42 +02003 *
Simon Wood64013802012-04-21 05:41:16 -07004 * Including G27, G25, DFP, DFGT, FFEX, Momo, Momo2 &
5 * Speed Force Wireless (WiiWheel)
Simon Wood32c88cb2010-09-22 13:19:42 +02006 *
7 * Copyright (c) 2010 Simon Wood <simon@mungewell.org>
8 */
9
10/*
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.
15 *
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.
20 *
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
24 */
25
26
27#include <linux/input.h>
28#include <linux/usb.h>
29#include <linux/hid.h>
30
31#include "usbhid/usbhid.h"
32#include "hid-lg.h"
Michal Malýa54dc7792015-02-18 17:59:22 +010033#include "hid-lg4ff.h"
Michal Malý7362cd22011-08-04 16:16:09 +020034#include "hid-ids.h"
Simon Wood32c88cb2010-09-22 13:19:42 +020035
Michal Malý30bb75d2011-08-04 16:20:40 +020036#define to_hid_device(pdev) container_of(pdev, struct hid_device, dev)
37
Michal Malýb96d23e2015-02-18 17:59:21 +010038#define LG4FF_MMODE_IS_MULTIMODE 0
Michal Malýe7c23442015-02-18 17:59:20 +010039#define LG4FF_MMODE_SWITCHED 1
40#define LG4FF_MMODE_NOT_MULTIMODE 2
41
Michal Malýb96d23e2015-02-18 17:59:21 +010042#define LG4FF_MODE_NATIVE_IDX 0
43#define LG4FF_MODE_DFEX_IDX 1
44#define LG4FF_MODE_DFP_IDX 2
45#define LG4FF_MODE_G25_IDX 3
46#define LG4FF_MODE_DFGT_IDX 4
47#define LG4FF_MODE_G27_IDX 5
48#define LG4FF_MODE_MAX_IDX 6
49
50#define LG4FF_MODE_NATIVE BIT(LG4FF_MODE_NATIVE_IDX)
51#define LG4FF_MODE_DFEX BIT(LG4FF_MODE_DFEX_IDX)
52#define LG4FF_MODE_DFP BIT(LG4FF_MODE_DFP_IDX)
53#define LG4FF_MODE_G25 BIT(LG4FF_MODE_G25_IDX)
54#define LG4FF_MODE_DFGT BIT(LG4FF_MODE_DFGT_IDX)
55#define LG4FF_MODE_G27 BIT(LG4FF_MODE_G27_IDX)
56
57#define LG4FF_DFEX_TAG "DF-EX"
58#define LG4FF_DFEX_NAME "Driving Force / Formula EX"
59#define LG4FF_DFP_TAG "DFP"
60#define LG4FF_DFP_NAME "Driving Force Pro"
61#define LG4FF_G25_TAG "G25"
62#define LG4FF_G25_NAME "G25 Racing Wheel"
63#define LG4FF_G27_TAG "G27"
64#define LG4FF_G27_NAME "G27 Racing Wheel"
65#define LG4FF_DFGT_TAG "DFGT"
66#define LG4FF_DFGT_NAME "Driving Force GT"
67
Michal Malýe7c23442015-02-18 17:59:20 +010068#define LG4FF_FFEX_REV_MAJ 0x21
69#define LG4FF_FFEX_REV_MIN 0x00
70
Michal Malý30bb75d2011-08-04 16:20:40 +020071static void hid_lg4ff_set_range_dfp(struct hid_device *hid, u16 range);
72static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range);
Michal Malý30bb75d2011-08-04 16:20:40 +020073
Michal Malý30bb75d2011-08-04 16:20:40 +020074struct lg4ff_device_entry {
Michal Malý2b24a962012-09-23 22:41:08 +020075 __u32 product_id;
Michal Malý30bb75d2011-08-04 16:20:40 +020076 __u16 range;
77 __u16 min_range;
78 __u16 max_range;
Simon Wood22bcefd2012-04-21 05:41:15 -070079#ifdef CONFIG_LEDS_CLASS
80 __u8 led_state;
81 struct led_classdev *led[5];
82#endif
Michal Malýb96d23e2015-02-18 17:59:21 +010083 u32 alternate_modes;
84 const char *real_tag;
85 const char *real_name;
86 u16 real_product_id;
Michal Malý30bb75d2011-08-04 16:20:40 +020087 struct list_head list;
88 void (*set_range)(struct hid_device *hid, u16 range);
89};
90
Michal Malý7362cd22011-08-04 16:16:09 +020091static const signed short lg4ff_wheel_effects[] = {
Simon Wood32c88cb2010-09-22 13:19:42 +020092 FF_CONSTANT,
93 FF_AUTOCENTER,
94 -1
95};
96
Michal Malý7362cd22011-08-04 16:16:09 +020097struct lg4ff_wheel {
98 const __u32 product_id;
99 const signed short *ff_effects;
100 const __u16 min_range;
101 const __u16 max_range;
Michal Malý30bb75d2011-08-04 16:20:40 +0200102 void (*set_range)(struct hid_device *hid, u16 range);
Michal Malý7362cd22011-08-04 16:16:09 +0200103};
104
Michal Malýe7c23442015-02-18 17:59:20 +0100105struct lg4ff_compat_mode_switch {
106 const __u8 cmd_count; /* Number of commands to send */
107 const __u8 cmd[];
108};
109
110struct lg4ff_wheel_ident_info {
111 const u16 mask;
112 const u16 result;
113 const u16 real_product_id;
114};
115
116struct lg4ff_wheel_ident_checklist {
117 const u32 count;
118 const struct lg4ff_wheel_ident_info *models[];
119};
120
Michal Malýb96d23e2015-02-18 17:59:21 +0100121struct lg4ff_multimode_wheel {
122 const u16 product_id;
123 const u32 alternate_modes;
124 const char *real_tag;
125 const char *real_name;
126};
127
128struct lg4ff_alternate_mode {
129 const u16 product_id;
130 const char *tag;
131 const char *name;
132};
133
Michal Malý7362cd22011-08-04 16:16:09 +0200134static const struct lg4ff_wheel lg4ff_devices[] = {
Michal Malý30bb75d2011-08-04 16:20:40 +0200135 {USB_DEVICE_ID_LOGITECH_WHEEL, lg4ff_wheel_effects, 40, 270, NULL},
136 {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL, lg4ff_wheel_effects, 40, 270, NULL},
137 {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_dfp},
138 {USB_DEVICE_ID_LOGITECH_G25_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
139 {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
140 {USB_DEVICE_ID_LOGITECH_G27_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
141 {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL},
142 {USB_DEVICE_ID_LOGITECH_WII_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}
Michal Malý7362cd22011-08-04 16:16:09 +0200143};
144
Michal Malýb96d23e2015-02-18 17:59:21 +0100145static const struct lg4ff_multimode_wheel lg4ff_multimode_wheels[] = {
146 {USB_DEVICE_ID_LOGITECH_DFP_WHEEL,
147 LG4FF_MODE_NATIVE | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
148 LG4FF_DFP_TAG, LG4FF_DFP_NAME},
149 {USB_DEVICE_ID_LOGITECH_G25_WHEEL,
150 LG4FF_MODE_NATIVE | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
151 LG4FF_G25_TAG, LG4FF_G25_NAME},
152 {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL,
153 LG4FF_MODE_NATIVE | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
154 LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
155 {USB_DEVICE_ID_LOGITECH_G27_WHEEL,
156 LG4FF_MODE_NATIVE | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
157 LG4FF_G27_TAG, LG4FF_G27_NAME},
158};
159
160static const struct lg4ff_alternate_mode lg4ff_alternate_modes[] = {
161 [LG4FF_MODE_NATIVE_IDX] = {0, "native", ""},
162 [LG4FF_MODE_DFEX_IDX] = {USB_DEVICE_ID_LOGITECH_WHEEL, LG4FF_DFEX_TAG, LG4FF_DFEX_NAME},
163 [LG4FF_MODE_DFP_IDX] = {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, LG4FF_DFP_TAG, LG4FF_DFP_NAME},
164 [LG4FF_MODE_G25_IDX] = {USB_DEVICE_ID_LOGITECH_G25_WHEEL, LG4FF_G25_TAG, LG4FF_G25_NAME},
165 [LG4FF_MODE_DFGT_IDX] = {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
166 [LG4FF_MODE_G27_IDX] = {USB_DEVICE_ID_LOGITECH_G27_WHEEL, LG4FF_G27_TAG, LG4FF_G27_NAME}
167};
168
Michal Malýe7c23442015-02-18 17:59:20 +0100169/* Multimode wheel identificators */
170static const struct lg4ff_wheel_ident_info lg4ff_dfp_ident_info = {
171 0xf000,
172 0x1000,
173 USB_DEVICE_ID_LOGITECH_DFP_WHEEL
Michal Malý96440c82011-08-04 16:18:11 +0200174};
175
Michal Malýe7c23442015-02-18 17:59:20 +0100176static const struct lg4ff_wheel_ident_info lg4ff_g25_ident_info = {
177 0xff00,
178 0x1200,
179 USB_DEVICE_ID_LOGITECH_G25_WHEEL
Michal Malý96440c82011-08-04 16:18:11 +0200180};
181
Michal Malýe7c23442015-02-18 17:59:20 +0100182static const struct lg4ff_wheel_ident_info lg4ff_g27_ident_info = {
183 0xfff0,
184 0x1230,
185 USB_DEVICE_ID_LOGITECH_G27_WHEEL
186};
187
188static const struct lg4ff_wheel_ident_info lg4ff_dfgt_ident_info = {
189 0xff00,
190 0x1300,
191 USB_DEVICE_ID_LOGITECH_DFGT_WHEEL
192};
193
194/* Multimode wheel identification checklists */
195static const struct lg4ff_wheel_ident_checklist lg4ff_main_checklist = {
196 4,
197 {&lg4ff_dfgt_ident_info,
198 &lg4ff_g27_ident_info,
199 &lg4ff_g25_ident_info,
200 &lg4ff_dfp_ident_info}
201};
202
203/* Compatibility mode switching commands */
Michal Malýf31a2de2015-02-18 17:59:23 +0100204/* EXT_CMD9 - Understood by G27 and DFGT */
205static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfex = {
206 2,
207 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
208 0xf8, 0x09, 0x00, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DF-EX with detach */
209};
210
211static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfp = {
212 2,
213 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
214 0xf8, 0x09, 0x01, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DFP with detach */
215};
216
217static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g25 = {
218 2,
219 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
220 0xf8, 0x09, 0x02, 0x01, 0x00, 0x00, 0x00} /* Switch mode to G25 with detach */
221};
222
223static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfgt = {
224 2,
225 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
226 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DFGT with detach */
227};
228
229static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g27 = {
230 2,
231 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
232 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* Switch mode to G27 with detach */
233};
234
235/* EXT_CMD1 - Understood by DFP, G25, G27 and DFGT */
236static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext01_dfp = {
Michal Malý96440c82011-08-04 16:18:11 +0200237 1,
238 {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
239};
240
Michal Malýf31a2de2015-02-18 17:59:23 +0100241/* EXT_CMD16 - Understood by G25 and G27 */
242static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext16_g25 = {
Michal Malý96440c82011-08-04 16:18:11 +0200243 1,
244 {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
245};
246
Michal Malý2b24a962012-09-23 22:41:08 +0200247/* Recalculates X axis value accordingly to currently selected range */
248static __s32 lg4ff_adjust_dfp_x_axis(__s32 value, __u16 range)
249{
250 __u16 max_range;
251 __s32 new_value;
252
253 if (range == 900)
254 return value;
255 else if (range == 200)
256 return value;
257 else if (range < 200)
258 max_range = 200;
259 else
260 max_range = 900;
261
262 new_value = 8192 + mult_frac(value - 8192, max_range, range);
263 if (new_value < 0)
264 return 0;
265 else if (new_value > 16383)
266 return 16383;
267 else
268 return new_value;
269}
270
271int lg4ff_adjust_input_event(struct hid_device *hid, struct hid_field *field,
272 struct hid_usage *usage, __s32 value, struct lg_drv_data *drv_data)
273{
274 struct lg4ff_device_entry *entry = drv_data->device_props;
275 __s32 new_value = 0;
276
277 if (!entry) {
278 hid_err(hid, "Device properties not found");
279 return 0;
280 }
281
282 switch (entry->product_id) {
283 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
284 switch (usage->code) {
285 case ABS_X:
286 new_value = lg4ff_adjust_dfp_x_axis(value, entry->range);
287 input_event(field->hidinput->input, usage->type, usage->code, new_value);
288 return 1;
289 default:
290 return 0;
291 }
292 default:
293 return 0;
294 }
295}
296
Michal Malý30bb75d2011-08-04 16:20:40 +0200297static int hid_lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
Simon Wood32c88cb2010-09-22 13:19:42 +0200298{
299 struct hid_device *hid = input_get_drvdata(dev);
300 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
301 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malý74479ba2012-09-24 01:09:30 +0200302 __s32 *value = report->field[0]->value;
Simon Wood32c88cb2010-09-22 13:19:42 +0200303 int x;
304
Michal Malýa80fe5d2012-09-24 01:13:17 +0200305#define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0)
Simon Wood32c88cb2010-09-22 13:19:42 +0200306
307 switch (effect->type) {
308 case FF_CONSTANT:
309 x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */
310 CLAMP(x);
Simon Wood56930e72013-11-06 12:30:42 -0700311
312 if (x == 0x80) {
313 /* De-activate force in slot-1*/
314 value[0] = 0x13;
315 value[1] = 0x00;
316 value[2] = 0x00;
317 value[3] = 0x00;
318 value[4] = 0x00;
319 value[5] = 0x00;
320 value[6] = 0x00;
321
322 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
323 return 0;
324 }
325
Michal Malý74479ba2012-09-24 01:09:30 +0200326 value[0] = 0x11; /* Slot 1 */
327 value[1] = 0x08;
328 value[2] = x;
329 value[3] = 0x80;
330 value[4] = 0x00;
331 value[5] = 0x00;
332 value[6] = 0x00;
Simon Wood32c88cb2010-09-22 13:19:42 +0200333
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100334 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Simon Wood32c88cb2010-09-22 13:19:42 +0200335 break;
336 }
337 return 0;
338}
339
Michal Malý6e2de8e2011-08-04 16:22:07 +0200340/* Sends default autocentering command compatible with
341 * all wheels except Formula Force EX */
342static void hid_lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude)
Simon Wood32c88cb2010-09-22 13:19:42 +0200343{
344 struct hid_device *hid = input_get_drvdata(dev);
345 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
346 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malý74479ba2012-09-24 01:09:30 +0200347 __s32 *value = report->field[0]->value;
Simon Woodf8c23152013-11-06 12:30:40 -0700348 __u32 expand_a, expand_b;
Simon Wood18597622013-11-06 12:30:44 -0700349 struct lg4ff_device_entry *entry;
350 struct lg_drv_data *drv_data;
351
352 drv_data = hid_get_drvdata(hid);
353 if (!drv_data) {
354 hid_err(hid, "Private driver data not found!\n");
355 return;
356 }
357
358 entry = drv_data->device_props;
359 if (!entry) {
360 hid_err(hid, "Device properties not found!\n");
361 return;
362 }
Simon Woodf8c23152013-11-06 12:30:40 -0700363
Simon Woodd2c02da2013-11-06 12:30:41 -0700364 /* De-activate Auto-Center */
365 if (magnitude == 0) {
366 value[0] = 0xf5;
367 value[1] = 0x00;
368 value[2] = 0x00;
369 value[3] = 0x00;
370 value[4] = 0x00;
371 value[5] = 0x00;
372 value[6] = 0x00;
373
374 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
375 return;
376 }
377
Simon Woodf8c23152013-11-06 12:30:40 -0700378 if (magnitude <= 0xaaaa) {
379 expand_a = 0x0c * magnitude;
380 expand_b = 0x80 * magnitude;
381 } else {
382 expand_a = (0x0c * 0xaaaa) + 0x06 * (magnitude - 0xaaaa);
383 expand_b = (0x80 * 0xaaaa) + 0xff * (magnitude - 0xaaaa);
384 }
Simon Wood32c88cb2010-09-22 13:19:42 +0200385
Simon Wood18597622013-11-06 12:30:44 -0700386 /* Adjust for non-MOMO wheels */
387 switch (entry->product_id) {
388 case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL:
389 case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2:
390 break;
391 default:
392 expand_a = expand_a >> 1;
393 break;
394 }
395
Michal Malý74479ba2012-09-24 01:09:30 +0200396 value[0] = 0xfe;
397 value[1] = 0x0d;
Simon Woodf8c23152013-11-06 12:30:40 -0700398 value[2] = expand_a / 0xaaaa;
399 value[3] = expand_a / 0xaaaa;
400 value[4] = expand_b / 0xaaaa;
Michal Malý74479ba2012-09-24 01:09:30 +0200401 value[5] = 0x00;
402 value[6] = 0x00;
Simon Wood32c88cb2010-09-22 13:19:42 +0200403
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100404 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Simon Woodd2c02da2013-11-06 12:30:41 -0700405
406 /* Activate Auto-Center */
407 value[0] = 0x14;
408 value[1] = 0x00;
409 value[2] = 0x00;
410 value[3] = 0x00;
411 value[4] = 0x00;
412 value[5] = 0x00;
413 value[6] = 0x00;
414
415 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Simon Wood32c88cb2010-09-22 13:19:42 +0200416}
417
Michal Malý6e2de8e2011-08-04 16:22:07 +0200418/* Sends autocentering command compatible with Formula Force EX */
419static void hid_lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude)
420{
421 struct hid_device *hid = input_get_drvdata(dev);
422 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
423 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malý74479ba2012-09-24 01:09:30 +0200424 __s32 *value = report->field[0]->value;
Michal Malý6e2de8e2011-08-04 16:22:07 +0200425 magnitude = magnitude * 90 / 65535;
Michal Malý6e2de8e2011-08-04 16:22:07 +0200426
Michal Malý74479ba2012-09-24 01:09:30 +0200427 value[0] = 0xfe;
428 value[1] = 0x03;
429 value[2] = magnitude >> 14;
430 value[3] = magnitude >> 14;
431 value[4] = magnitude;
432 value[5] = 0x00;
433 value[6] = 0x00;
Michal Malý6e2de8e2011-08-04 16:22:07 +0200434
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100435 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malý6e2de8e2011-08-04 16:22:07 +0200436}
437
Michal Malý30bb75d2011-08-04 16:20:40 +0200438/* Sends command to set range compatible with G25/G27/Driving Force GT */
439static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range)
440{
441 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
442 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malý74479ba2012-09-24 01:09:30 +0200443 __s32 *value = report->field[0]->value;
444
Michal Malý30bb75d2011-08-04 16:20:40 +0200445 dbg_hid("G25/G27/DFGT: setting range to %u\n", range);
446
Michal Malý74479ba2012-09-24 01:09:30 +0200447 value[0] = 0xf8;
448 value[1] = 0x81;
449 value[2] = range & 0x00ff;
450 value[3] = (range & 0xff00) >> 8;
451 value[4] = 0x00;
452 value[5] = 0x00;
453 value[6] = 0x00;
Michal Malý30bb75d2011-08-04 16:20:40 +0200454
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100455 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malý30bb75d2011-08-04 16:20:40 +0200456}
457
458/* Sends commands to set range compatible with Driving Force Pro wheel */
459static void hid_lg4ff_set_range_dfp(struct hid_device *hid, __u16 range)
460{
461 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
462 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
463 int start_left, start_right, full_range;
Michal Malý74479ba2012-09-24 01:09:30 +0200464 __s32 *value = report->field[0]->value;
465
Michal Malý30bb75d2011-08-04 16:20:40 +0200466 dbg_hid("Driving Force Pro: setting range to %u\n", range);
467
468 /* Prepare "coarse" limit command */
Michal Malý74479ba2012-09-24 01:09:30 +0200469 value[0] = 0xf8;
Michal Malýa80fe5d2012-09-24 01:13:17 +0200470 value[1] = 0x00; /* Set later */
Michal Malý74479ba2012-09-24 01:09:30 +0200471 value[2] = 0x00;
472 value[3] = 0x00;
473 value[4] = 0x00;
474 value[5] = 0x00;
475 value[6] = 0x00;
Michal Malý30bb75d2011-08-04 16:20:40 +0200476
477 if (range > 200) {
478 report->field[0]->value[1] = 0x03;
479 full_range = 900;
480 } else {
481 report->field[0]->value[1] = 0x02;
482 full_range = 200;
483 }
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100484 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malý30bb75d2011-08-04 16:20:40 +0200485
486 /* Prepare "fine" limit command */
Michal Malý74479ba2012-09-24 01:09:30 +0200487 value[0] = 0x81;
488 value[1] = 0x0b;
489 value[2] = 0x00;
490 value[3] = 0x00;
491 value[4] = 0x00;
492 value[5] = 0x00;
493 value[6] = 0x00;
Michal Malý30bb75d2011-08-04 16:20:40 +0200494
495 if (range == 200 || range == 900) { /* Do not apply any fine limit */
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100496 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malý30bb75d2011-08-04 16:20:40 +0200497 return;
498 }
499
500 /* Construct fine limit command */
501 start_left = (((full_range - range + 1) * 2047) / full_range);
502 start_right = 0xfff - start_left;
503
Michal Malý74479ba2012-09-24 01:09:30 +0200504 value[2] = start_left >> 4;
505 value[3] = start_right >> 4;
506 value[4] = 0xff;
507 value[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
508 value[6] = 0xff;
Michal Malý30bb75d2011-08-04 16:20:40 +0200509
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100510 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malý30bb75d2011-08-04 16:20:40 +0200511}
512
Michal Malýf31a2de2015-02-18 17:59:23 +0100513static const struct lg4ff_compat_mode_switch *lg4ff_get_mode_switch_command(const u16 real_product_id, const u16 target_product_id)
514{
515 switch (real_product_id) {
516 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
517 switch (target_product_id) {
518 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
519 return &lg4ff_mode_switch_ext01_dfp;
520 /* DFP can only be switched to its native mode */
521 default:
522 return NULL;
523 }
524 break;
525 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
526 switch (target_product_id) {
527 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
528 return &lg4ff_mode_switch_ext01_dfp;
529 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
530 return &lg4ff_mode_switch_ext16_g25;
531 /* G25 can only be switched to DFP mode or its native mode */
532 default:
533 return NULL;
534 }
535 break;
536 case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
537 switch (target_product_id) {
538 case USB_DEVICE_ID_LOGITECH_WHEEL:
539 return &lg4ff_mode_switch_ext09_dfex;
540 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
541 return &lg4ff_mode_switch_ext09_dfp;
542 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
543 return &lg4ff_mode_switch_ext09_g25;
544 case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
545 return &lg4ff_mode_switch_ext09_g27;
546 /* G27 can only be switched to DF-EX, DFP, G25 or its native mode */
547 default:
548 return NULL;
549 }
550 break;
551 case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
552 switch (target_product_id) {
553 case USB_DEVICE_ID_LOGITECH_WHEEL:
554 return &lg4ff_mode_switch_ext09_dfex;
555 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
556 return &lg4ff_mode_switch_ext09_dfp;
557 case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
558 return &lg4ff_mode_switch_ext09_dfgt;
559 /* DFGT can only be switched to DF-EX, DFP or its native mode */
560 default:
561 return NULL;
562 }
563 break;
564 /* No other wheels have multiple modes */
565 default:
566 return NULL;
567 }
568}
569
Michal Malýe7c23442015-02-18 17:59:20 +0100570static int lg4ff_switch_compatibility_mode(struct hid_device *hid, const struct lg4ff_compat_mode_switch *s)
Michal Malý96440c82011-08-04 16:18:11 +0200571{
Michal Malýe7c23442015-02-18 17:59:20 +0100572 struct usb_device *usbdev = hid_to_usb_dev(hid);
573 struct usbhid_device *usbhid = hid->driver_data;
574 u8 i;
Michal Malý96440c82011-08-04 16:18:11 +0200575
Michal Malýe7c23442015-02-18 17:59:20 +0100576 for (i = 0; i < s->cmd_count; i++) {
577 int xferd, ret;
578 u8 data[7];
Michal Malý96440c82011-08-04 16:18:11 +0200579
Michal Malýe7c23442015-02-18 17:59:20 +0100580 memcpy(data, s->cmd + (7*i), 7);
581 ret = usb_interrupt_msg(usbdev, usbhid->urbout->pipe, data, 7, &xferd, USB_CTRL_SET_TIMEOUT);
582 if (ret)
583 return ret;
Michal Malý96440c82011-08-04 16:18:11 +0200584 }
Michal Malýe7c23442015-02-18 17:59:20 +0100585 return 0;
Michal Malý96440c82011-08-04 16:18:11 +0200586}
Simon Wood32c88cb2010-09-22 13:19:42 +0200587
Michal Malýb96d23e2015-02-18 17:59:21 +0100588static ssize_t lg4ff_alternate_modes_show(struct device *dev, struct device_attribute *attr, char *buf)
589{
590 struct hid_device *hid = to_hid_device(dev);
591 struct lg4ff_device_entry *entry;
592 struct lg_drv_data *drv_data;
593 ssize_t count = 0;
594 int i;
595
596 drv_data = hid_get_drvdata(hid);
597 if (!drv_data) {
598 hid_err(hid, "Private driver data not found!\n");
599 return 0;
600 }
601
602 entry = drv_data->device_props;
603 if (!entry) {
604 hid_err(hid, "Device properties not found!\n");
605 return 0;
606 }
607
608 if (!entry->real_name) {
609 hid_err(hid, "NULL pointer to string\n");
610 return 0;
611 }
612
613 for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
614 if (entry->alternate_modes & BIT(i)) {
615 /* Print tag and full name */
616 count += scnprintf(buf + count, PAGE_SIZE - count, "%s: %s",
617 lg4ff_alternate_modes[i].tag,
618 !lg4ff_alternate_modes[i].product_id ? entry->real_name : lg4ff_alternate_modes[i].name);
619 if (count >= PAGE_SIZE - 1)
620 return count;
621
622 /* Mark the currently active mode with an asterisk */
623 if (lg4ff_alternate_modes[i].product_id == entry->product_id ||
624 (lg4ff_alternate_modes[i].product_id == 0 && entry->product_id == entry->real_product_id))
625 count += scnprintf(buf + count, PAGE_SIZE - count, " *\n");
626 else
627 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
628
629 if (count >= PAGE_SIZE - 1)
630 return count;
631 }
632 }
633
634 return count;
635}
636
637static ssize_t lg4ff_alternate_modes_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
638{
Michal Malýf31a2de2015-02-18 17:59:23 +0100639 struct hid_device *hid = to_hid_device(dev);
640 struct lg4ff_device_entry *entry;
641 struct lg_drv_data *drv_data;
642 const struct lg4ff_compat_mode_switch *s;
643 u16 target_product_id = 0;
644 int i, ret;
645 char *lbuf;
646
647 drv_data = hid_get_drvdata(hid);
648 if (!drv_data) {
649 hid_err(hid, "Private driver data not found!\n");
650 return -EINVAL;
651 }
652
653 entry = drv_data->device_props;
654 if (!entry) {
655 hid_err(hid, "Device properties not found!\n");
656 return -EINVAL;
657 }
658
659 /* Allow \n at the end of the input parameter */
660 lbuf = kasprintf(GFP_KERNEL, "%s", buf);
661 if (!lbuf)
662 return -ENOMEM;
663
664 i = strlen(lbuf);
665 if (lbuf[i-1] == '\n') {
666 if (i == 1) {
667 kfree(lbuf);
668 return -EINVAL;
669 }
670 lbuf[i-1] = '\0';
671 }
672
673 for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
674 const u16 mode_product_id = lg4ff_alternate_modes[i].product_id;
675 const char *tag = lg4ff_alternate_modes[i].tag;
676
677 if (entry->alternate_modes & BIT(i)) {
678 if (!strcmp(tag, lbuf)) {
679 if (!mode_product_id)
680 target_product_id = entry->real_product_id;
681 else
682 target_product_id = mode_product_id;
683 break;
684 }
685 }
686 }
687
688 if (i == LG4FF_MODE_MAX_IDX) {
689 hid_info(hid, "Requested mode \"%s\" is not supported by the device\n", lbuf);
690 kfree(lbuf);
691 return -EINVAL;
692 }
693 kfree(lbuf); /* Not needed anymore */
694
695 if (target_product_id == entry->product_id) /* Nothing to do */
696 return count;
697
698 /* Automatic switching has to be disabled for the switch to DF-EX mode to work correctly */
699 if (target_product_id == USB_DEVICE_ID_LOGITECH_WHEEL && !lg4ff_no_autoswitch) {
700 hid_info(hid, "\"%s\" cannot be switched to \"DF-EX\" mode. Load the \"hid_logitech\" module with \"lg4ff_no_autoswitch=1\" parameter set and try again\n",
701 entry->real_name);
702 return -EINVAL;
703 }
704
705 /* Take care of hardware limitations */
706 if ((entry->real_product_id == USB_DEVICE_ID_LOGITECH_DFP_WHEEL || entry->real_product_id == USB_DEVICE_ID_LOGITECH_G25_WHEEL) &&
707 entry->product_id > target_product_id) {
708 hid_info(hid, "\"%s\" cannot be switched back into \"%s\" mode\n", entry->real_name, lg4ff_alternate_modes[i].name);
709 return -EINVAL;
710 }
711
712 s = lg4ff_get_mode_switch_command(entry->real_product_id, target_product_id);
713 if (!s) {
714 hid_err(hid, "Invalid target product ID %X\n", target_product_id);
715 return -EINVAL;
716 }
717
718 ret = lg4ff_switch_compatibility_mode(hid, s);
719 return (ret == 0 ? count : ret);
Michal Malýb96d23e2015-02-18 17:59:21 +0100720}
721static DEVICE_ATTR(alternate_modes, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_alternate_modes_show, lg4ff_alternate_modes_store);
722
Michal Malý30bb75d2011-08-04 16:20:40 +0200723/* Read current range and display it in terminal */
Vivien Didelot2f1cec32015-01-19 02:03:26 -0500724static ssize_t range_show(struct device *dev, struct device_attribute *attr,
725 char *buf)
Michal Malý30bb75d2011-08-04 16:20:40 +0200726{
Michal Malý30bb75d2011-08-04 16:20:40 +0200727 struct hid_device *hid = to_hid_device(dev);
Michal Malý3b6b17b2012-04-09 09:08:49 +0200728 struct lg4ff_device_entry *entry;
729 struct lg_drv_data *drv_data;
Michal Malý30bb75d2011-08-04 16:20:40 +0200730 size_t count;
731
Michal Malý3b6b17b2012-04-09 09:08:49 +0200732 drv_data = hid_get_drvdata(hid);
733 if (!drv_data) {
734 hid_err(hid, "Private driver data not found!\n");
735 return 0;
Michal Malý30bb75d2011-08-04 16:20:40 +0200736 }
Michal Malý3b6b17b2012-04-09 09:08:49 +0200737
738 entry = drv_data->device_props;
739 if (!entry) {
740 hid_err(hid, "Device properties not found!\n");
Michal Malý30bb75d2011-08-04 16:20:40 +0200741 return 0;
742 }
743
744 count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->range);
745 return count;
746}
747
748/* Set range to user specified value, call appropriate function
749 * according to the type of the wheel */
Vivien Didelot2f1cec32015-01-19 02:03:26 -0500750static ssize_t range_store(struct device *dev, struct device_attribute *attr,
751 const char *buf, size_t count)
Michal Malý30bb75d2011-08-04 16:20:40 +0200752{
Michal Malý30bb75d2011-08-04 16:20:40 +0200753 struct hid_device *hid = to_hid_device(dev);
Michal Malý3b6b17b2012-04-09 09:08:49 +0200754 struct lg4ff_device_entry *entry;
755 struct lg_drv_data *drv_data;
Michal Malý30bb75d2011-08-04 16:20:40 +0200756 __u16 range = simple_strtoul(buf, NULL, 10);
757
Michal Malý3b6b17b2012-04-09 09:08:49 +0200758 drv_data = hid_get_drvdata(hid);
759 if (!drv_data) {
760 hid_err(hid, "Private driver data not found!\n");
Simon Wood29ff6652014-08-14 20:43:01 -0600761 return -EINVAL;
Michal Malý30bb75d2011-08-04 16:20:40 +0200762 }
Michal Malý3b6b17b2012-04-09 09:08:49 +0200763
764 entry = drv_data->device_props;
765 if (!entry) {
766 hid_err(hid, "Device properties not found!\n");
Simon Wood29ff6652014-08-14 20:43:01 -0600767 return -EINVAL;
Michal Malý30bb75d2011-08-04 16:20:40 +0200768 }
769
770 if (range == 0)
771 range = entry->max_range;
772
773 /* Check if the wheel supports range setting
774 * and that the range is within limits for the wheel */
775 if (entry->set_range != NULL && range >= entry->min_range && range <= entry->max_range) {
776 entry->set_range(hid, range);
777 entry->range = range;
778 }
779
780 return count;
781}
Vivien Didelot2f1cec32015-01-19 02:03:26 -0500782static DEVICE_ATTR_RW(range);
Michal Malý30bb75d2011-08-04 16:20:40 +0200783
Michal Malýb96d23e2015-02-18 17:59:21 +0100784static ssize_t lg4ff_real_id_show(struct device *dev, struct device_attribute *attr, char *buf)
785{
786 struct hid_device *hid = to_hid_device(dev);
787 struct lg4ff_device_entry *entry;
788 struct lg_drv_data *drv_data;
789 size_t count;
790
791 drv_data = hid_get_drvdata(hid);
792 if (!drv_data) {
793 hid_err(hid, "Private driver data not found!\n");
794 return 0;
795 }
796
797 entry = drv_data->device_props;
798 if (!entry) {
799 hid_err(hid, "Device properties not found!\n");
800 return 0;
801 }
802
803 if (!entry->real_tag || !entry->real_name) {
804 hid_err(hid, "NULL pointer to string\n");
805 return 0;
806 }
807
808 count = scnprintf(buf, PAGE_SIZE, "%s: %s\n", entry->real_tag, entry->real_name);
809 return count;
810}
811
812static ssize_t lg4ff_real_id_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
813{
814 /* Real ID is a read-only value */
815 return -EPERM;
816}
817static DEVICE_ATTR(real_id, S_IRUGO, lg4ff_real_id_show, lg4ff_real_id_store);
818
Simon Wood22bcefd2012-04-21 05:41:15 -0700819#ifdef CONFIG_LEDS_CLASS
820static void lg4ff_set_leds(struct hid_device *hid, __u8 leds)
821{
822 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
823 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malý74479ba2012-09-24 01:09:30 +0200824 __s32 *value = report->field[0]->value;
Simon Wood22bcefd2012-04-21 05:41:15 -0700825
Michal Malý74479ba2012-09-24 01:09:30 +0200826 value[0] = 0xf8;
827 value[1] = 0x12;
828 value[2] = leds;
829 value[3] = 0x00;
830 value[4] = 0x00;
831 value[5] = 0x00;
832 value[6] = 0x00;
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100833 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Simon Wood22bcefd2012-04-21 05:41:15 -0700834}
835
836static void lg4ff_led_set_brightness(struct led_classdev *led_cdev,
837 enum led_brightness value)
838{
839 struct device *dev = led_cdev->dev->parent;
840 struct hid_device *hid = container_of(dev, struct hid_device, dev);
Axel Lin4629fd12012-09-13 14:09:33 +0800841 struct lg_drv_data *drv_data = hid_get_drvdata(hid);
Simon Wood22bcefd2012-04-21 05:41:15 -0700842 struct lg4ff_device_entry *entry;
843 int i, state = 0;
844
845 if (!drv_data) {
846 hid_err(hid, "Device data not found.");
847 return;
848 }
849
850 entry = (struct lg4ff_device_entry *)drv_data->device_props;
851
852 if (!entry) {
853 hid_err(hid, "Device properties not found.");
854 return;
855 }
856
857 for (i = 0; i < 5; i++) {
858 if (led_cdev != entry->led[i])
859 continue;
860 state = (entry->led_state >> i) & 1;
861 if (value == LED_OFF && state) {
862 entry->led_state &= ~(1 << i);
863 lg4ff_set_leds(hid, entry->led_state);
864 } else if (value != LED_OFF && !state) {
865 entry->led_state |= 1 << i;
866 lg4ff_set_leds(hid, entry->led_state);
867 }
868 break;
869 }
870}
871
872static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev)
873{
874 struct device *dev = led_cdev->dev->parent;
875 struct hid_device *hid = container_of(dev, struct hid_device, dev);
Axel Lin4629fd12012-09-13 14:09:33 +0800876 struct lg_drv_data *drv_data = hid_get_drvdata(hid);
Simon Wood22bcefd2012-04-21 05:41:15 -0700877 struct lg4ff_device_entry *entry;
878 int i, value = 0;
879
880 if (!drv_data) {
881 hid_err(hid, "Device data not found.");
882 return LED_OFF;
883 }
884
885 entry = (struct lg4ff_device_entry *)drv_data->device_props;
886
887 if (!entry) {
888 hid_err(hid, "Device properties not found.");
889 return LED_OFF;
890 }
891
892 for (i = 0; i < 5; i++)
893 if (led_cdev == entry->led[i]) {
894 value = (entry->led_state >> i) & 1;
895 break;
896 }
897
898 return value ? LED_FULL : LED_OFF;
899}
900#endif
901
Michal Malýe7c23442015-02-18 17:59:20 +0100902static u16 lg4ff_identify_multimode_wheel(struct hid_device *hid, const u16 reported_product_id, const u16 bcdDevice)
903{
904 const struct lg4ff_wheel_ident_checklist *checklist;
905 int i, from_idx, to_idx;
906
907 switch (reported_product_id) {
908 case USB_DEVICE_ID_LOGITECH_WHEEL:
909 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
910 checklist = &lg4ff_main_checklist;
911 from_idx = 0;
912 to_idx = checklist->count - 1;
913 break;
914 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
915 checklist = &lg4ff_main_checklist;
916 from_idx = 0;
917 to_idx = checklist->count - 2; /* End identity check at G25 */
918 break;
919 case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
920 checklist = &lg4ff_main_checklist;
921 from_idx = 1; /* Start identity check at G27 */
922 to_idx = checklist->count - 3; /* End identity check at G27 */
923 break;
924 case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
925 checklist = &lg4ff_main_checklist;
926 from_idx = 0;
927 to_idx = checklist->count - 4; /* End identity check at DFGT */
928 break;
929 default:
930 return 0;
931 }
932
933 for (i = from_idx; i <= to_idx; i++) {
934 const u16 mask = checklist->models[i]->mask;
935 const u16 result = checklist->models[i]->result;
936 const u16 real_product_id = checklist->models[i]->real_product_id;
937
938 if ((bcdDevice & mask) == result) {
939 dbg_hid("Found wheel with real PID %X whose reported PID is %X\n", real_product_id, reported_product_id);
940 return real_product_id;
941 }
942 }
943
Michal Malýf31a2de2015-02-18 17:59:23 +0100944 /* No match found. This is either Driving Force or an unknown
945 * wheel model, do not touch it */
Michal Malýe7c23442015-02-18 17:59:20 +0100946 dbg_hid("Wheel with bcdDevice %X was not recognized as multimode wheel, leaving in its current mode\n", bcdDevice);
947 return 0;
948}
949
950static int lg4ff_handle_multimode_wheel(struct hid_device *hid, u16 *real_product_id, const u16 bcdDevice)
951{
952 const u16 reported_product_id = hid->product;
953 int ret;
954
955 *real_product_id = lg4ff_identify_multimode_wheel(hid, reported_product_id, bcdDevice);
956 /* Probed wheel is not a multimode wheel */
957 if (!*real_product_id) {
958 *real_product_id = reported_product_id;
959 dbg_hid("Wheel is not a multimode wheel\n");
960 return LG4FF_MMODE_NOT_MULTIMODE;
961 }
962
963 /* Switch from "Driving Force" mode to native mode automatically.
964 * Otherwise keep the wheel in its current mode */
965 if (reported_product_id == USB_DEVICE_ID_LOGITECH_WHEEL &&
Michal Malýa54dc7792015-02-18 17:59:22 +0100966 reported_product_id != *real_product_id &&
967 !lg4ff_no_autoswitch) {
Michal Malýf31a2de2015-02-18 17:59:23 +0100968 const struct lg4ff_compat_mode_switch *s = lg4ff_get_mode_switch_command(*real_product_id, *real_product_id);
Michal Malýe7c23442015-02-18 17:59:20 +0100969
Michal Malýf31a2de2015-02-18 17:59:23 +0100970 if (!s) {
Michal Malýe7c23442015-02-18 17:59:20 +0100971 hid_err(hid, "Invalid product id %X\n", *real_product_id);
Michal Malýb96d23e2015-02-18 17:59:21 +0100972 return LG4FF_MMODE_NOT_MULTIMODE;
Michal Malýe7c23442015-02-18 17:59:20 +0100973 }
974
975 ret = lg4ff_switch_compatibility_mode(hid, s);
976 if (ret) {
977 /* Wheel could not have been switched to native mode,
978 * leave it in "Driving Force" mode and continue */
979 hid_err(hid, "Unable to switch wheel mode, errno %d\n", ret);
Michal Malýb96d23e2015-02-18 17:59:21 +0100980 return LG4FF_MMODE_IS_MULTIMODE;
Michal Malýe7c23442015-02-18 17:59:20 +0100981 }
982 return LG4FF_MMODE_SWITCHED;
983 }
984
Michal Malýb96d23e2015-02-18 17:59:21 +0100985 return LG4FF_MMODE_IS_MULTIMODE;
Michal Malýe7c23442015-02-18 17:59:20 +0100986}
987
988
Simon Wood32c88cb2010-09-22 13:19:42 +0200989int lg4ff_init(struct hid_device *hid)
990{
991 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
Simon Wood32c88cb2010-09-22 13:19:42 +0200992 struct input_dev *dev = hidinput->input;
Michal Malýe7c23442015-02-18 17:59:20 +0100993 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
994 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
Michal Malý30bb75d2011-08-04 16:20:40 +0200995 struct lg4ff_device_entry *entry;
Michal Malý3b6b17b2012-04-09 09:08:49 +0200996 struct lg_drv_data *drv_data;
Michal Malýb96d23e2015-02-18 17:59:21 +0100997 int error, i, j;
998 int mmode_ret, mmode_idx = -1;
Michal Malýe7c23442015-02-18 17:59:20 +0100999 u16 real_product_id;
Simon Wood32c88cb2010-09-22 13:19:42 +02001000
Simon Wood32c88cb2010-09-22 13:19:42 +02001001 /* Check that the report looks ok */
Kees Cook0fb6bd02013-09-11 21:56:54 +02001002 if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
Simon Wood32c88cb2010-09-22 13:19:42 +02001003 return -1;
Michal Malý30bb75d2011-08-04 16:20:40 +02001004
Michal Malýe7c23442015-02-18 17:59:20 +01001005 /* Check if a multimode wheel has been connected and
1006 * handle it appropriately */
Michal Malýb96d23e2015-02-18 17:59:21 +01001007 mmode_ret = lg4ff_handle_multimode_wheel(hid, &real_product_id, bcdDevice);
Michal Malýe7c23442015-02-18 17:59:20 +01001008
1009 /* Wheel has been told to switch to native mode. There is no point in going on
1010 * with the initialization as the wheel will do a USB reset when it switches mode
1011 */
Michal Malýb96d23e2015-02-18 17:59:21 +01001012 if (mmode_ret == LG4FF_MMODE_SWITCHED)
Michal Malýe7c23442015-02-18 17:59:20 +01001013 return 0;
1014
Michal Malý7362cd22011-08-04 16:16:09 +02001015 /* Check what wheel has been connected */
1016 for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
1017 if (hid->product == lg4ff_devices[i].product_id) {
1018 dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
1019 break;
1020 }
1021 }
Simon Wood32c88cb2010-09-22 13:19:42 +02001022
Michal Malý7362cd22011-08-04 16:16:09 +02001023 if (i == ARRAY_SIZE(lg4ff_devices)) {
1024 hid_err(hid, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to"
1025 "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n");
1026 return -1;
1027 }
1028
Michal Malýb96d23e2015-02-18 17:59:21 +01001029 if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1030 for (mmode_idx = 0; mmode_idx < ARRAY_SIZE(lg4ff_multimode_wheels); mmode_idx++) {
1031 if (real_product_id == lg4ff_multimode_wheels[mmode_idx].product_id)
1032 break;
1033 }
1034
1035 if (mmode_idx == ARRAY_SIZE(lg4ff_multimode_wheels)) {
1036 hid_err(hid, "Device product ID %X is not listed as a multimode wheel", real_product_id);
1037 return -1;
1038 }
1039 }
1040
Michal Malý7362cd22011-08-04 16:16:09 +02001041 /* Set supported force feedback capabilities */
1042 for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
1043 set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
Simon Wood32c88cb2010-09-22 13:19:42 +02001044
1045 error = input_ff_create_memless(dev, NULL, hid_lg4ff_play);
1046
1047 if (error)
1048 return error;
1049
Michal Malý3b6b17b2012-04-09 09:08:49 +02001050 /* Get private driver data */
1051 drv_data = hid_get_drvdata(hid);
1052 if (!drv_data) {
1053 hid_err(hid, "Cannot add device, private driver data not allocated\n");
1054 return -1;
Michal Malý30bb75d2011-08-04 16:20:40 +02001055 }
1056
Michal Malý3b6b17b2012-04-09 09:08:49 +02001057 /* Initialize device properties */
Thomas Meyer8383c6b2011-11-08 20:14:14 +01001058 entry = kzalloc(sizeof(struct lg4ff_device_entry), GFP_KERNEL);
Michal Malý30bb75d2011-08-04 16:20:40 +02001059 if (!entry) {
Michal Malý3b6b17b2012-04-09 09:08:49 +02001060 hid_err(hid, "Cannot add device, insufficient memory to allocate device properties.\n");
Michal Malý30bb75d2011-08-04 16:20:40 +02001061 return -ENOMEM;
1062 }
Michal Malý3b6b17b2012-04-09 09:08:49 +02001063 drv_data->device_props = entry;
1064
Michal Malý2b24a962012-09-23 22:41:08 +02001065 entry->product_id = lg4ff_devices[i].product_id;
Michal Malýb96d23e2015-02-18 17:59:21 +01001066 entry->real_product_id = real_product_id;
Michal Malý30bb75d2011-08-04 16:20:40 +02001067 entry->min_range = lg4ff_devices[i].min_range;
1068 entry->max_range = lg4ff_devices[i].max_range;
1069 entry->set_range = lg4ff_devices[i].set_range;
Michal Malýb96d23e2015-02-18 17:59:21 +01001070 if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1071 BUG_ON(mmode_idx == -1);
1072 entry->alternate_modes = lg4ff_multimode_wheels[mmode_idx].alternate_modes;
1073 entry->real_tag = lg4ff_multimode_wheels[mmode_idx].real_tag;
1074 entry->real_name = lg4ff_multimode_wheels[mmode_idx].real_name;
1075 }
Michal Malý30bb75d2011-08-04 16:20:40 +02001076
Simon Wood114a55c2013-11-06 12:30:43 -07001077 /* Check if autocentering is available and
1078 * set the centering force to zero by default */
1079 if (test_bit(FF_AUTOCENTER, dev->ffbit)) {
Michal Malýe7c23442015-02-18 17:59:20 +01001080 /* Formula Force EX expects different autocentering command */
1081 if ((bcdDevice >> 8) == LG4FF_FFEX_REV_MAJ &&
1082 (bcdDevice & 0xff) == LG4FF_FFEX_REV_MIN)
Simon Wood114a55c2013-11-06 12:30:43 -07001083 dev->ff->set_autocenter = hid_lg4ff_set_autocenter_ffex;
1084 else
1085 dev->ff->set_autocenter = hid_lg4ff_set_autocenter_default;
1086
1087 dev->ff->set_autocenter(dev, 0);
1088 }
1089
Michal Malý30bb75d2011-08-04 16:20:40 +02001090 /* Create sysfs interface */
1091 error = device_create_file(&hid->dev, &dev_attr_range);
1092 if (error)
1093 return error;
Michal Malýb96d23e2015-02-18 17:59:21 +01001094 if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1095 error = device_create_file(&hid->dev, &dev_attr_real_id);
1096 if (error)
1097 return error;
1098 error = device_create_file(&hid->dev, &dev_attr_alternate_modes);
1099 if (error)
1100 return error;
1101 }
Michal Malý30bb75d2011-08-04 16:20:40 +02001102 dbg_hid("sysfs interface created\n");
1103
1104 /* Set the maximum range to start with */
1105 entry->range = entry->max_range;
1106 if (entry->set_range != NULL)
1107 entry->set_range(hid, entry->range);
1108
Simon Wood22bcefd2012-04-21 05:41:15 -07001109#ifdef CONFIG_LEDS_CLASS
1110 /* register led subsystem - G27 only */
1111 entry->led_state = 0;
1112 for (j = 0; j < 5; j++)
1113 entry->led[j] = NULL;
1114
1115 if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL) {
1116 struct led_classdev *led;
1117 size_t name_sz;
1118 char *name;
1119
1120 lg4ff_set_leds(hid, 0);
1121
1122 name_sz = strlen(dev_name(&hid->dev)) + 8;
1123
1124 for (j = 0; j < 5; j++) {
1125 led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
1126 if (!led) {
1127 hid_err(hid, "can't allocate memory for LED %d\n", j);
1128 goto err;
1129 }
1130
1131 name = (void *)(&led[1]);
1132 snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1);
1133 led->name = name;
1134 led->brightness = 0;
1135 led->max_brightness = 1;
1136 led->brightness_get = lg4ff_led_get_brightness;
1137 led->brightness_set = lg4ff_led_set_brightness;
1138
1139 entry->led[j] = led;
1140 error = led_classdev_register(&hid->dev, led);
1141
1142 if (error) {
1143 hid_err(hid, "failed to register LED %d. Aborting.\n", j);
1144err:
1145 /* Deregister LEDs (if any) */
1146 for (j = 0; j < 5; j++) {
1147 led = entry->led[j];
1148 entry->led[j] = NULL;
1149 if (!led)
1150 continue;
1151 led_classdev_unregister(led);
1152 kfree(led);
1153 }
1154 goto out; /* Let the driver continue without LEDs */
1155 }
1156 }
1157 }
Simon Wood22bcefd2012-04-21 05:41:15 -07001158out:
Jiri Kosinac6e6dc82012-04-23 21:08:15 +02001159#endif
Simon Wood64013802012-04-21 05:41:16 -07001160 hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n");
Simon Wood32c88cb2010-09-22 13:19:42 +02001161 return 0;
1162}
1163
Michal Malý30bb75d2011-08-04 16:20:40 +02001164int lg4ff_deinit(struct hid_device *hid)
1165{
Michal Malý30bb75d2011-08-04 16:20:40 +02001166 struct lg4ff_device_entry *entry;
Michal Malý3b6b17b2012-04-09 09:08:49 +02001167 struct lg_drv_data *drv_data;
1168
Michal Malý3b6b17b2012-04-09 09:08:49 +02001169 drv_data = hid_get_drvdata(hid);
1170 if (!drv_data) {
1171 hid_err(hid, "Error while deinitializing device, no private driver data.\n");
Michal Malý30bb75d2011-08-04 16:20:40 +02001172 return -1;
1173 }
Michal Malý3b6b17b2012-04-09 09:08:49 +02001174 entry = drv_data->device_props;
Michal Malýe7c23442015-02-18 17:59:20 +01001175 if (!entry)
1176 goto out; /* Nothing more to do */
1177
1178 device_remove_file(&hid->dev, &dev_attr_range);
Simon Wood22bcefd2012-04-21 05:41:15 -07001179
Michal Malýb96d23e2015-02-18 17:59:21 +01001180 /* Multimode devices will have at least the "MODE_NATIVE" bit set */
1181 if (entry->alternate_modes) {
1182 device_remove_file(&hid->dev, &dev_attr_real_id);
1183 device_remove_file(&hid->dev, &dev_attr_alternate_modes);
1184 }
1185
Simon Wood22bcefd2012-04-21 05:41:15 -07001186#ifdef CONFIG_LEDS_CLASS
1187 {
1188 int j;
1189 struct led_classdev *led;
1190
1191 /* Deregister LEDs (if any) */
1192 for (j = 0; j < 5; j++) {
1193
1194 led = entry->led[j];
1195 entry->led[j] = NULL;
1196 if (!led)
1197 continue;
1198 led_classdev_unregister(led);
1199 kfree(led);
1200 }
1201 }
1202#endif
1203
Michal Malý3b6b17b2012-04-09 09:08:49 +02001204 /* Deallocate memory */
1205 kfree(entry);
Michal Malý30bb75d2011-08-04 16:20:40 +02001206
Michal Malýe7c23442015-02-18 17:59:20 +01001207out:
Michal Malý30bb75d2011-08-04 16:20:40 +02001208 dbg_hid("Device successfully unregistered\n");
1209 return 0;
1210}