blob: 1232210b1cc586f80703479eec634e595addbd9d [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ýc1740d12015-02-18 22:49:33 +0100572 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
573 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
574 __s32 *value = report->field[0]->value;
Michal Malýe7c23442015-02-18 17:59:20 +0100575 u8 i;
Michal Malý96440c82011-08-04 16:18:11 +0200576
Michal Malýe7c23442015-02-18 17:59:20 +0100577 for (i = 0; i < s->cmd_count; i++) {
Michal Malýc1740d12015-02-18 22:49:33 +0100578 u8 j;
Michal Malý96440c82011-08-04 16:18:11 +0200579
Michal Malýc1740d12015-02-18 22:49:33 +0100580 for (j = 0; j < 7; j++)
581 value[j] = s->cmd[j + (7*i)];
582
583 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malý96440c82011-08-04 16:18:11 +0200584 }
Michal Malýc1740d12015-02-18 22:49:33 +0100585 hid_hw_wait(hid);
Michal Malýe7c23442015-02-18 17:59:20 +0100586 return 0;
Michal Malý96440c82011-08-04 16:18:11 +0200587}
Simon Wood32c88cb2010-09-22 13:19:42 +0200588
Michal Malýb96d23e2015-02-18 17:59:21 +0100589static ssize_t lg4ff_alternate_modes_show(struct device *dev, struct device_attribute *attr, char *buf)
590{
591 struct hid_device *hid = to_hid_device(dev);
592 struct lg4ff_device_entry *entry;
593 struct lg_drv_data *drv_data;
594 ssize_t count = 0;
595 int i;
596
597 drv_data = hid_get_drvdata(hid);
598 if (!drv_data) {
599 hid_err(hid, "Private driver data not found!\n");
600 return 0;
601 }
602
603 entry = drv_data->device_props;
604 if (!entry) {
605 hid_err(hid, "Device properties not found!\n");
606 return 0;
607 }
608
609 if (!entry->real_name) {
610 hid_err(hid, "NULL pointer to string\n");
611 return 0;
612 }
613
614 for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
615 if (entry->alternate_modes & BIT(i)) {
616 /* Print tag and full name */
617 count += scnprintf(buf + count, PAGE_SIZE - count, "%s: %s",
618 lg4ff_alternate_modes[i].tag,
619 !lg4ff_alternate_modes[i].product_id ? entry->real_name : lg4ff_alternate_modes[i].name);
620 if (count >= PAGE_SIZE - 1)
621 return count;
622
623 /* Mark the currently active mode with an asterisk */
624 if (lg4ff_alternate_modes[i].product_id == entry->product_id ||
625 (lg4ff_alternate_modes[i].product_id == 0 && entry->product_id == entry->real_product_id))
626 count += scnprintf(buf + count, PAGE_SIZE - count, " *\n");
627 else
628 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
629
630 if (count >= PAGE_SIZE - 1)
631 return count;
632 }
633 }
634
635 return count;
636}
637
638static ssize_t lg4ff_alternate_modes_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
639{
Michal Malýf31a2de2015-02-18 17:59:23 +0100640 struct hid_device *hid = to_hid_device(dev);
641 struct lg4ff_device_entry *entry;
642 struct lg_drv_data *drv_data;
643 const struct lg4ff_compat_mode_switch *s;
644 u16 target_product_id = 0;
645 int i, ret;
646 char *lbuf;
647
648 drv_data = hid_get_drvdata(hid);
649 if (!drv_data) {
650 hid_err(hid, "Private driver data not found!\n");
651 return -EINVAL;
652 }
653
654 entry = drv_data->device_props;
655 if (!entry) {
656 hid_err(hid, "Device properties not found!\n");
657 return -EINVAL;
658 }
659
660 /* Allow \n at the end of the input parameter */
661 lbuf = kasprintf(GFP_KERNEL, "%s", buf);
662 if (!lbuf)
663 return -ENOMEM;
664
665 i = strlen(lbuf);
666 if (lbuf[i-1] == '\n') {
667 if (i == 1) {
668 kfree(lbuf);
669 return -EINVAL;
670 }
671 lbuf[i-1] = '\0';
672 }
673
674 for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
675 const u16 mode_product_id = lg4ff_alternate_modes[i].product_id;
676 const char *tag = lg4ff_alternate_modes[i].tag;
677
678 if (entry->alternate_modes & BIT(i)) {
679 if (!strcmp(tag, lbuf)) {
680 if (!mode_product_id)
681 target_product_id = entry->real_product_id;
682 else
683 target_product_id = mode_product_id;
684 break;
685 }
686 }
687 }
688
689 if (i == LG4FF_MODE_MAX_IDX) {
690 hid_info(hid, "Requested mode \"%s\" is not supported by the device\n", lbuf);
691 kfree(lbuf);
692 return -EINVAL;
693 }
694 kfree(lbuf); /* Not needed anymore */
695
696 if (target_product_id == entry->product_id) /* Nothing to do */
697 return count;
698
699 /* Automatic switching has to be disabled for the switch to DF-EX mode to work correctly */
700 if (target_product_id == USB_DEVICE_ID_LOGITECH_WHEEL && !lg4ff_no_autoswitch) {
701 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",
702 entry->real_name);
703 return -EINVAL;
704 }
705
706 /* Take care of hardware limitations */
707 if ((entry->real_product_id == USB_DEVICE_ID_LOGITECH_DFP_WHEEL || entry->real_product_id == USB_DEVICE_ID_LOGITECH_G25_WHEEL) &&
708 entry->product_id > target_product_id) {
709 hid_info(hid, "\"%s\" cannot be switched back into \"%s\" mode\n", entry->real_name, lg4ff_alternate_modes[i].name);
710 return -EINVAL;
711 }
712
713 s = lg4ff_get_mode_switch_command(entry->real_product_id, target_product_id);
714 if (!s) {
715 hid_err(hid, "Invalid target product ID %X\n", target_product_id);
716 return -EINVAL;
717 }
718
719 ret = lg4ff_switch_compatibility_mode(hid, s);
720 return (ret == 0 ? count : ret);
Michal Malýb96d23e2015-02-18 17:59:21 +0100721}
722static DEVICE_ATTR(alternate_modes, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_alternate_modes_show, lg4ff_alternate_modes_store);
723
Michal Malý30bb75d2011-08-04 16:20:40 +0200724/* Read current range and display it in terminal */
Vivien Didelot2f1cec32015-01-19 02:03:26 -0500725static ssize_t range_show(struct device *dev, struct device_attribute *attr,
726 char *buf)
Michal Malý30bb75d2011-08-04 16:20:40 +0200727{
Michal Malý30bb75d2011-08-04 16:20:40 +0200728 struct hid_device *hid = to_hid_device(dev);
Michal Malý3b6b17b2012-04-09 09:08:49 +0200729 struct lg4ff_device_entry *entry;
730 struct lg_drv_data *drv_data;
Michal Malý30bb75d2011-08-04 16:20:40 +0200731 size_t count;
732
Michal Malý3b6b17b2012-04-09 09:08:49 +0200733 drv_data = hid_get_drvdata(hid);
734 if (!drv_data) {
735 hid_err(hid, "Private driver data not found!\n");
736 return 0;
Michal Malý30bb75d2011-08-04 16:20:40 +0200737 }
Michal Malý3b6b17b2012-04-09 09:08:49 +0200738
739 entry = drv_data->device_props;
740 if (!entry) {
741 hid_err(hid, "Device properties not found!\n");
Michal Malý30bb75d2011-08-04 16:20:40 +0200742 return 0;
743 }
744
745 count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->range);
746 return count;
747}
748
749/* Set range to user specified value, call appropriate function
750 * according to the type of the wheel */
Vivien Didelot2f1cec32015-01-19 02:03:26 -0500751static ssize_t range_store(struct device *dev, struct device_attribute *attr,
752 const char *buf, size_t count)
Michal Malý30bb75d2011-08-04 16:20:40 +0200753{
Michal Malý30bb75d2011-08-04 16:20:40 +0200754 struct hid_device *hid = to_hid_device(dev);
Michal Malý3b6b17b2012-04-09 09:08:49 +0200755 struct lg4ff_device_entry *entry;
756 struct lg_drv_data *drv_data;
Michal Malý30bb75d2011-08-04 16:20:40 +0200757 __u16 range = simple_strtoul(buf, NULL, 10);
758
Michal Malý3b6b17b2012-04-09 09:08:49 +0200759 drv_data = hid_get_drvdata(hid);
760 if (!drv_data) {
761 hid_err(hid, "Private driver data not found!\n");
Simon Wood29ff6652014-08-14 20:43:01 -0600762 return -EINVAL;
Michal Malý30bb75d2011-08-04 16:20:40 +0200763 }
Michal Malý3b6b17b2012-04-09 09:08:49 +0200764
765 entry = drv_data->device_props;
766 if (!entry) {
767 hid_err(hid, "Device properties not found!\n");
Simon Wood29ff6652014-08-14 20:43:01 -0600768 return -EINVAL;
Michal Malý30bb75d2011-08-04 16:20:40 +0200769 }
770
771 if (range == 0)
772 range = entry->max_range;
773
774 /* Check if the wheel supports range setting
775 * and that the range is within limits for the wheel */
776 if (entry->set_range != NULL && range >= entry->min_range && range <= entry->max_range) {
777 entry->set_range(hid, range);
778 entry->range = range;
779 }
780
781 return count;
782}
Vivien Didelot2f1cec32015-01-19 02:03:26 -0500783static DEVICE_ATTR_RW(range);
Michal Malý30bb75d2011-08-04 16:20:40 +0200784
Michal Malýb96d23e2015-02-18 17:59:21 +0100785static ssize_t lg4ff_real_id_show(struct device *dev, struct device_attribute *attr, char *buf)
786{
787 struct hid_device *hid = to_hid_device(dev);
788 struct lg4ff_device_entry *entry;
789 struct lg_drv_data *drv_data;
790 size_t count;
791
792 drv_data = hid_get_drvdata(hid);
793 if (!drv_data) {
794 hid_err(hid, "Private driver data not found!\n");
795 return 0;
796 }
797
798 entry = drv_data->device_props;
799 if (!entry) {
800 hid_err(hid, "Device properties not found!\n");
801 return 0;
802 }
803
804 if (!entry->real_tag || !entry->real_name) {
805 hid_err(hid, "NULL pointer to string\n");
806 return 0;
807 }
808
809 count = scnprintf(buf, PAGE_SIZE, "%s: %s\n", entry->real_tag, entry->real_name);
810 return count;
811}
812
813static ssize_t lg4ff_real_id_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
814{
815 /* Real ID is a read-only value */
816 return -EPERM;
817}
818static DEVICE_ATTR(real_id, S_IRUGO, lg4ff_real_id_show, lg4ff_real_id_store);
819
Simon Wood22bcefd2012-04-21 05:41:15 -0700820#ifdef CONFIG_LEDS_CLASS
821static void lg4ff_set_leds(struct hid_device *hid, __u8 leds)
822{
823 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
824 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malý74479ba2012-09-24 01:09:30 +0200825 __s32 *value = report->field[0]->value;
Simon Wood22bcefd2012-04-21 05:41:15 -0700826
Michal Malý74479ba2012-09-24 01:09:30 +0200827 value[0] = 0xf8;
828 value[1] = 0x12;
829 value[2] = leds;
830 value[3] = 0x00;
831 value[4] = 0x00;
832 value[5] = 0x00;
833 value[6] = 0x00;
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100834 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Simon Wood22bcefd2012-04-21 05:41:15 -0700835}
836
837static void lg4ff_led_set_brightness(struct led_classdev *led_cdev,
838 enum led_brightness value)
839{
840 struct device *dev = led_cdev->dev->parent;
841 struct hid_device *hid = container_of(dev, struct hid_device, dev);
Axel Lin4629fd12012-09-13 14:09:33 +0800842 struct lg_drv_data *drv_data = hid_get_drvdata(hid);
Simon Wood22bcefd2012-04-21 05:41:15 -0700843 struct lg4ff_device_entry *entry;
844 int i, state = 0;
845
846 if (!drv_data) {
847 hid_err(hid, "Device data not found.");
848 return;
849 }
850
851 entry = (struct lg4ff_device_entry *)drv_data->device_props;
852
853 if (!entry) {
854 hid_err(hid, "Device properties not found.");
855 return;
856 }
857
858 for (i = 0; i < 5; i++) {
859 if (led_cdev != entry->led[i])
860 continue;
861 state = (entry->led_state >> i) & 1;
862 if (value == LED_OFF && state) {
863 entry->led_state &= ~(1 << i);
864 lg4ff_set_leds(hid, entry->led_state);
865 } else if (value != LED_OFF && !state) {
866 entry->led_state |= 1 << i;
867 lg4ff_set_leds(hid, entry->led_state);
868 }
869 break;
870 }
871}
872
873static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev)
874{
875 struct device *dev = led_cdev->dev->parent;
876 struct hid_device *hid = container_of(dev, struct hid_device, dev);
Axel Lin4629fd12012-09-13 14:09:33 +0800877 struct lg_drv_data *drv_data = hid_get_drvdata(hid);
Simon Wood22bcefd2012-04-21 05:41:15 -0700878 struct lg4ff_device_entry *entry;
879 int i, value = 0;
880
881 if (!drv_data) {
882 hid_err(hid, "Device data not found.");
883 return LED_OFF;
884 }
885
886 entry = (struct lg4ff_device_entry *)drv_data->device_props;
887
888 if (!entry) {
889 hid_err(hid, "Device properties not found.");
890 return LED_OFF;
891 }
892
893 for (i = 0; i < 5; i++)
894 if (led_cdev == entry->led[i]) {
895 value = (entry->led_state >> i) & 1;
896 break;
897 }
898
899 return value ? LED_FULL : LED_OFF;
900}
901#endif
902
Michal Malýe7c23442015-02-18 17:59:20 +0100903static u16 lg4ff_identify_multimode_wheel(struct hid_device *hid, const u16 reported_product_id, const u16 bcdDevice)
904{
905 const struct lg4ff_wheel_ident_checklist *checklist;
906 int i, from_idx, to_idx;
907
908 switch (reported_product_id) {
909 case USB_DEVICE_ID_LOGITECH_WHEEL:
910 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
911 checklist = &lg4ff_main_checklist;
912 from_idx = 0;
913 to_idx = checklist->count - 1;
914 break;
915 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
916 checklist = &lg4ff_main_checklist;
917 from_idx = 0;
918 to_idx = checklist->count - 2; /* End identity check at G25 */
919 break;
920 case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
921 checklist = &lg4ff_main_checklist;
922 from_idx = 1; /* Start identity check at G27 */
923 to_idx = checklist->count - 3; /* End identity check at G27 */
924 break;
925 case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
926 checklist = &lg4ff_main_checklist;
927 from_idx = 0;
928 to_idx = checklist->count - 4; /* End identity check at DFGT */
929 break;
930 default:
931 return 0;
932 }
933
934 for (i = from_idx; i <= to_idx; i++) {
935 const u16 mask = checklist->models[i]->mask;
936 const u16 result = checklist->models[i]->result;
937 const u16 real_product_id = checklist->models[i]->real_product_id;
938
939 if ((bcdDevice & mask) == result) {
940 dbg_hid("Found wheel with real PID %X whose reported PID is %X\n", real_product_id, reported_product_id);
941 return real_product_id;
942 }
943 }
944
Michal Malýf31a2de2015-02-18 17:59:23 +0100945 /* No match found. This is either Driving Force or an unknown
946 * wheel model, do not touch it */
Michal Malýe7c23442015-02-18 17:59:20 +0100947 dbg_hid("Wheel with bcdDevice %X was not recognized as multimode wheel, leaving in its current mode\n", bcdDevice);
948 return 0;
949}
950
951static int lg4ff_handle_multimode_wheel(struct hid_device *hid, u16 *real_product_id, const u16 bcdDevice)
952{
953 const u16 reported_product_id = hid->product;
954 int ret;
955
956 *real_product_id = lg4ff_identify_multimode_wheel(hid, reported_product_id, bcdDevice);
957 /* Probed wheel is not a multimode wheel */
958 if (!*real_product_id) {
959 *real_product_id = reported_product_id;
960 dbg_hid("Wheel is not a multimode wheel\n");
961 return LG4FF_MMODE_NOT_MULTIMODE;
962 }
963
964 /* Switch from "Driving Force" mode to native mode automatically.
965 * Otherwise keep the wheel in its current mode */
966 if (reported_product_id == USB_DEVICE_ID_LOGITECH_WHEEL &&
Michal Malýa54dc7792015-02-18 17:59:22 +0100967 reported_product_id != *real_product_id &&
968 !lg4ff_no_autoswitch) {
Michal Malýf31a2de2015-02-18 17:59:23 +0100969 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 +0100970
Michal Malýf31a2de2015-02-18 17:59:23 +0100971 if (!s) {
Michal Malýe7c23442015-02-18 17:59:20 +0100972 hid_err(hid, "Invalid product id %X\n", *real_product_id);
Michal Malýb96d23e2015-02-18 17:59:21 +0100973 return LG4FF_MMODE_NOT_MULTIMODE;
Michal Malýe7c23442015-02-18 17:59:20 +0100974 }
975
976 ret = lg4ff_switch_compatibility_mode(hid, s);
977 if (ret) {
978 /* Wheel could not have been switched to native mode,
979 * leave it in "Driving Force" mode and continue */
980 hid_err(hid, "Unable to switch wheel mode, errno %d\n", ret);
Michal Malýb96d23e2015-02-18 17:59:21 +0100981 return LG4FF_MMODE_IS_MULTIMODE;
Michal Malýe7c23442015-02-18 17:59:20 +0100982 }
983 return LG4FF_MMODE_SWITCHED;
984 }
985
Michal Malýb96d23e2015-02-18 17:59:21 +0100986 return LG4FF_MMODE_IS_MULTIMODE;
Michal Malýe7c23442015-02-18 17:59:20 +0100987}
988
989
Simon Wood32c88cb2010-09-22 13:19:42 +0200990int lg4ff_init(struct hid_device *hid)
991{
992 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
Simon Wood32c88cb2010-09-22 13:19:42 +0200993 struct input_dev *dev = hidinput->input;
Michal Malýe7c23442015-02-18 17:59:20 +0100994 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
995 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
Michal Malý30bb75d2011-08-04 16:20:40 +0200996 struct lg4ff_device_entry *entry;
Michal Malý3b6b17b2012-04-09 09:08:49 +0200997 struct lg_drv_data *drv_data;
Michal Malýb96d23e2015-02-18 17:59:21 +0100998 int error, i, j;
999 int mmode_ret, mmode_idx = -1;
Michal Malýe7c23442015-02-18 17:59:20 +01001000 u16 real_product_id;
Simon Wood32c88cb2010-09-22 13:19:42 +02001001
Simon Wood32c88cb2010-09-22 13:19:42 +02001002 /* Check that the report looks ok */
Kees Cook0fb6bd02013-09-11 21:56:54 +02001003 if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
Simon Wood32c88cb2010-09-22 13:19:42 +02001004 return -1;
Michal Malý30bb75d2011-08-04 16:20:40 +02001005
Michal Malýe7c23442015-02-18 17:59:20 +01001006 /* Check if a multimode wheel has been connected and
1007 * handle it appropriately */
Michal Malýb96d23e2015-02-18 17:59:21 +01001008 mmode_ret = lg4ff_handle_multimode_wheel(hid, &real_product_id, bcdDevice);
Michal Malýe7c23442015-02-18 17:59:20 +01001009
1010 /* Wheel has been told to switch to native mode. There is no point in going on
1011 * with the initialization as the wheel will do a USB reset when it switches mode
1012 */
Michal Malýb96d23e2015-02-18 17:59:21 +01001013 if (mmode_ret == LG4FF_MMODE_SWITCHED)
Michal Malýe7c23442015-02-18 17:59:20 +01001014 return 0;
1015
Michal Malý7362cd22011-08-04 16:16:09 +02001016 /* Check what wheel has been connected */
1017 for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
1018 if (hid->product == lg4ff_devices[i].product_id) {
1019 dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
1020 break;
1021 }
1022 }
Simon Wood32c88cb2010-09-22 13:19:42 +02001023
Michal Malý7362cd22011-08-04 16:16:09 +02001024 if (i == ARRAY_SIZE(lg4ff_devices)) {
1025 hid_err(hid, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to"
1026 "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n");
1027 return -1;
1028 }
1029
Michal Malýb96d23e2015-02-18 17:59:21 +01001030 if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1031 for (mmode_idx = 0; mmode_idx < ARRAY_SIZE(lg4ff_multimode_wheels); mmode_idx++) {
1032 if (real_product_id == lg4ff_multimode_wheels[mmode_idx].product_id)
1033 break;
1034 }
1035
1036 if (mmode_idx == ARRAY_SIZE(lg4ff_multimode_wheels)) {
1037 hid_err(hid, "Device product ID %X is not listed as a multimode wheel", real_product_id);
1038 return -1;
1039 }
1040 }
1041
Michal Malý7362cd22011-08-04 16:16:09 +02001042 /* Set supported force feedback capabilities */
1043 for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
1044 set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
Simon Wood32c88cb2010-09-22 13:19:42 +02001045
1046 error = input_ff_create_memless(dev, NULL, hid_lg4ff_play);
1047
1048 if (error)
1049 return error;
1050
Michal Malý3b6b17b2012-04-09 09:08:49 +02001051 /* Get private driver data */
1052 drv_data = hid_get_drvdata(hid);
1053 if (!drv_data) {
1054 hid_err(hid, "Cannot add device, private driver data not allocated\n");
1055 return -1;
Michal Malý30bb75d2011-08-04 16:20:40 +02001056 }
1057
Michal Malý3b6b17b2012-04-09 09:08:49 +02001058 /* Initialize device properties */
Thomas Meyer8383c6b2011-11-08 20:14:14 +01001059 entry = kzalloc(sizeof(struct lg4ff_device_entry), GFP_KERNEL);
Michal Malý30bb75d2011-08-04 16:20:40 +02001060 if (!entry) {
Michal Malý3b6b17b2012-04-09 09:08:49 +02001061 hid_err(hid, "Cannot add device, insufficient memory to allocate device properties.\n");
Michal Malý30bb75d2011-08-04 16:20:40 +02001062 return -ENOMEM;
1063 }
Michal Malý3b6b17b2012-04-09 09:08:49 +02001064 drv_data->device_props = entry;
1065
Michal Malý2b24a962012-09-23 22:41:08 +02001066 entry->product_id = lg4ff_devices[i].product_id;
Michal Malýb96d23e2015-02-18 17:59:21 +01001067 entry->real_product_id = real_product_id;
Michal Malý30bb75d2011-08-04 16:20:40 +02001068 entry->min_range = lg4ff_devices[i].min_range;
1069 entry->max_range = lg4ff_devices[i].max_range;
1070 entry->set_range = lg4ff_devices[i].set_range;
Michal Malýb96d23e2015-02-18 17:59:21 +01001071 if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1072 BUG_ON(mmode_idx == -1);
1073 entry->alternate_modes = lg4ff_multimode_wheels[mmode_idx].alternate_modes;
1074 entry->real_tag = lg4ff_multimode_wheels[mmode_idx].real_tag;
1075 entry->real_name = lg4ff_multimode_wheels[mmode_idx].real_name;
1076 }
Michal Malý30bb75d2011-08-04 16:20:40 +02001077
Simon Wood114a55c2013-11-06 12:30:43 -07001078 /* Check if autocentering is available and
1079 * set the centering force to zero by default */
1080 if (test_bit(FF_AUTOCENTER, dev->ffbit)) {
Michal Malýe7c23442015-02-18 17:59:20 +01001081 /* Formula Force EX expects different autocentering command */
1082 if ((bcdDevice >> 8) == LG4FF_FFEX_REV_MAJ &&
1083 (bcdDevice & 0xff) == LG4FF_FFEX_REV_MIN)
Simon Wood114a55c2013-11-06 12:30:43 -07001084 dev->ff->set_autocenter = hid_lg4ff_set_autocenter_ffex;
1085 else
1086 dev->ff->set_autocenter = hid_lg4ff_set_autocenter_default;
1087
1088 dev->ff->set_autocenter(dev, 0);
1089 }
1090
Michal Malý30bb75d2011-08-04 16:20:40 +02001091 /* Create sysfs interface */
1092 error = device_create_file(&hid->dev, &dev_attr_range);
1093 if (error)
1094 return error;
Michal Malýb96d23e2015-02-18 17:59:21 +01001095 if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1096 error = device_create_file(&hid->dev, &dev_attr_real_id);
1097 if (error)
1098 return error;
1099 error = device_create_file(&hid->dev, &dev_attr_alternate_modes);
1100 if (error)
1101 return error;
1102 }
Michal Malý30bb75d2011-08-04 16:20:40 +02001103 dbg_hid("sysfs interface created\n");
1104
1105 /* Set the maximum range to start with */
1106 entry->range = entry->max_range;
1107 if (entry->set_range != NULL)
1108 entry->set_range(hid, entry->range);
1109
Simon Wood22bcefd2012-04-21 05:41:15 -07001110#ifdef CONFIG_LEDS_CLASS
1111 /* register led subsystem - G27 only */
1112 entry->led_state = 0;
1113 for (j = 0; j < 5; j++)
1114 entry->led[j] = NULL;
1115
1116 if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL) {
1117 struct led_classdev *led;
1118 size_t name_sz;
1119 char *name;
1120
1121 lg4ff_set_leds(hid, 0);
1122
1123 name_sz = strlen(dev_name(&hid->dev)) + 8;
1124
1125 for (j = 0; j < 5; j++) {
1126 led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
1127 if (!led) {
1128 hid_err(hid, "can't allocate memory for LED %d\n", j);
1129 goto err;
1130 }
1131
1132 name = (void *)(&led[1]);
1133 snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1);
1134 led->name = name;
1135 led->brightness = 0;
1136 led->max_brightness = 1;
1137 led->brightness_get = lg4ff_led_get_brightness;
1138 led->brightness_set = lg4ff_led_set_brightness;
1139
1140 entry->led[j] = led;
1141 error = led_classdev_register(&hid->dev, led);
1142
1143 if (error) {
1144 hid_err(hid, "failed to register LED %d. Aborting.\n", j);
1145err:
1146 /* Deregister LEDs (if any) */
1147 for (j = 0; j < 5; j++) {
1148 led = entry->led[j];
1149 entry->led[j] = NULL;
1150 if (!led)
1151 continue;
1152 led_classdev_unregister(led);
1153 kfree(led);
1154 }
1155 goto out; /* Let the driver continue without LEDs */
1156 }
1157 }
1158 }
Simon Wood22bcefd2012-04-21 05:41:15 -07001159out:
Jiri Kosinac6e6dc82012-04-23 21:08:15 +02001160#endif
Simon Wood64013802012-04-21 05:41:16 -07001161 hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n");
Simon Wood32c88cb2010-09-22 13:19:42 +02001162 return 0;
1163}
1164
Michal Malý30bb75d2011-08-04 16:20:40 +02001165int lg4ff_deinit(struct hid_device *hid)
1166{
Michal Malý30bb75d2011-08-04 16:20:40 +02001167 struct lg4ff_device_entry *entry;
Michal Malý3b6b17b2012-04-09 09:08:49 +02001168 struct lg_drv_data *drv_data;
1169
Michal Malý3b6b17b2012-04-09 09:08:49 +02001170 drv_data = hid_get_drvdata(hid);
1171 if (!drv_data) {
1172 hid_err(hid, "Error while deinitializing device, no private driver data.\n");
Michal Malý30bb75d2011-08-04 16:20:40 +02001173 return -1;
1174 }
Michal Malý3b6b17b2012-04-09 09:08:49 +02001175 entry = drv_data->device_props;
Michal Malýe7c23442015-02-18 17:59:20 +01001176 if (!entry)
1177 goto out; /* Nothing more to do */
1178
1179 device_remove_file(&hid->dev, &dev_attr_range);
Simon Wood22bcefd2012-04-21 05:41:15 -07001180
Michal Malýb96d23e2015-02-18 17:59:21 +01001181 /* Multimode devices will have at least the "MODE_NATIVE" bit set */
1182 if (entry->alternate_modes) {
1183 device_remove_file(&hid->dev, &dev_attr_real_id);
1184 device_remove_file(&hid->dev, &dev_attr_alternate_modes);
1185 }
1186
Simon Wood22bcefd2012-04-21 05:41:15 -07001187#ifdef CONFIG_LEDS_CLASS
1188 {
1189 int j;
1190 struct led_classdev *led;
1191
1192 /* Deregister LEDs (if any) */
1193 for (j = 0; j < 5; j++) {
1194
1195 led = entry->led[j];
1196 entry->led[j] = NULL;
1197 if (!led)
1198 continue;
1199 led_classdev_unregister(led);
1200 kfree(led);
1201 }
1202 }
1203#endif
1204
Michal Malý3b6b17b2012-04-09 09:08:49 +02001205 /* Deallocate memory */
1206 kfree(entry);
Michal Malý30bb75d2011-08-04 16:20:40 +02001207
Michal Malýe7c23442015-02-18 17:59:20 +01001208out:
Michal Malý30bb75d2011-08-04 16:20:40 +02001209 dbg_hid("Device successfully unregistered\n");
1210 return 0;
1211}