blob: 031b8abd7131d83c5e287bb7e3599d51b857e4e5 [file] [log] [blame]
Simon Wood32c88cb2010-09-22 13:19:42 +02001/*
Simon Wood640138002012-04-21 05:41:16 -07002 * Force feedback support for Logitech Gaming Wheels
Simon Wood32c88cb2010-09-22 13:19:42 +02003 *
Simon Wood640138002012-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ýd0afd84892015-04-08 22:56:40 +020071static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range);
72static void lg4ff_set_range_g25(struct hid_device *hid, u16 range);
Michal Malý30bb75d2011-08-04 16:20:40 +020073
Michal Malý72529c62015-04-08 22:56:46 +020074struct lg4ff_wheel_data {
Michal Malý2a552c32015-04-08 22:56:39 +020075 u32 product_id;
76 u16 range;
77 u16 min_range;
78 u16 max_range;
Simon Wood22bcefd2012-04-21 05:41:15 -070079#ifdef CONFIG_LEDS_CLASS
Michal Malý2a552c32015-04-08 22:56:39 +020080 u8 led_state;
Simon Wood22bcefd2012-04-21 05:41:15 -070081 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ý72529c62015-04-08 22:56:46 +020087
Michal Malý30bb75d2011-08-04 16:20:40 +020088 void (*set_range)(struct hid_device *hid, u16 range);
89};
90
Michal Malý72529c62015-04-08 22:56:46 +020091struct lg4ff_device_entry {
92 struct lg4ff_wheel_data wdata;
93};
94
Michal Malý7362cd22011-08-04 16:16:09 +020095static const signed short lg4ff_wheel_effects[] = {
Simon Wood32c88cb2010-09-22 13:19:42 +020096 FF_CONSTANT,
97 FF_AUTOCENTER,
98 -1
99};
100
Michal Malý7362cd22011-08-04 16:16:09 +0200101struct lg4ff_wheel {
Michal Malý2a552c32015-04-08 22:56:39 +0200102 const u32 product_id;
Michal Malý7362cd22011-08-04 16:16:09 +0200103 const signed short *ff_effects;
Michal Malý2a552c32015-04-08 22:56:39 +0200104 const u16 min_range;
105 const u16 max_range;
Michal Malý30bb75d2011-08-04 16:20:40 +0200106 void (*set_range)(struct hid_device *hid, u16 range);
Michal Malý7362cd22011-08-04 16:16:09 +0200107};
108
Michal Malýe7c23442015-02-18 17:59:20 +0100109struct lg4ff_compat_mode_switch {
Michal Malý2a552c32015-04-08 22:56:39 +0200110 const u8 cmd_count; /* Number of commands to send */
111 const u8 cmd[];
Michal Malýe7c23442015-02-18 17:59:20 +0100112};
113
114struct lg4ff_wheel_ident_info {
115 const u16 mask;
116 const u16 result;
117 const u16 real_product_id;
118};
119
120struct lg4ff_wheel_ident_checklist {
121 const u32 count;
122 const struct lg4ff_wheel_ident_info *models[];
123};
124
Michal Malýb96d23e2015-02-18 17:59:21 +0100125struct lg4ff_multimode_wheel {
126 const u16 product_id;
127 const u32 alternate_modes;
128 const char *real_tag;
129 const char *real_name;
130};
131
132struct lg4ff_alternate_mode {
133 const u16 product_id;
134 const char *tag;
135 const char *name;
136};
137
Michal Malý7362cd22011-08-04 16:16:09 +0200138static const struct lg4ff_wheel lg4ff_devices[] = {
Michal Malý30bb75d2011-08-04 16:20:40 +0200139 {USB_DEVICE_ID_LOGITECH_WHEEL, lg4ff_wheel_effects, 40, 270, NULL},
140 {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL, lg4ff_wheel_effects, 40, 270, NULL},
Michal Malýd0afd84892015-04-08 22:56:40 +0200141 {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_dfp},
142 {USB_DEVICE_ID_LOGITECH_G25_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
143 {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
144 {USB_DEVICE_ID_LOGITECH_G27_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
Michal Malý30bb75d2011-08-04 16:20:40 +0200145 {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL},
146 {USB_DEVICE_ID_LOGITECH_WII_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}
Michal Malý7362cd22011-08-04 16:16:09 +0200147};
148
Michal Malýb96d23e2015-02-18 17:59:21 +0100149static const struct lg4ff_multimode_wheel lg4ff_multimode_wheels[] = {
150 {USB_DEVICE_ID_LOGITECH_DFP_WHEEL,
151 LG4FF_MODE_NATIVE | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
152 LG4FF_DFP_TAG, LG4FF_DFP_NAME},
153 {USB_DEVICE_ID_LOGITECH_G25_WHEEL,
154 LG4FF_MODE_NATIVE | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
155 LG4FF_G25_TAG, LG4FF_G25_NAME},
156 {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL,
157 LG4FF_MODE_NATIVE | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
158 LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
159 {USB_DEVICE_ID_LOGITECH_G27_WHEEL,
160 LG4FF_MODE_NATIVE | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
161 LG4FF_G27_TAG, LG4FF_G27_NAME},
162};
163
164static const struct lg4ff_alternate_mode lg4ff_alternate_modes[] = {
165 [LG4FF_MODE_NATIVE_IDX] = {0, "native", ""},
166 [LG4FF_MODE_DFEX_IDX] = {USB_DEVICE_ID_LOGITECH_WHEEL, LG4FF_DFEX_TAG, LG4FF_DFEX_NAME},
167 [LG4FF_MODE_DFP_IDX] = {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, LG4FF_DFP_TAG, LG4FF_DFP_NAME},
168 [LG4FF_MODE_G25_IDX] = {USB_DEVICE_ID_LOGITECH_G25_WHEEL, LG4FF_G25_TAG, LG4FF_G25_NAME},
169 [LG4FF_MODE_DFGT_IDX] = {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
170 [LG4FF_MODE_G27_IDX] = {USB_DEVICE_ID_LOGITECH_G27_WHEEL, LG4FF_G27_TAG, LG4FF_G27_NAME}
171};
172
Michal Malýe7c23442015-02-18 17:59:20 +0100173/* Multimode wheel identificators */
174static const struct lg4ff_wheel_ident_info lg4ff_dfp_ident_info = {
175 0xf000,
176 0x1000,
177 USB_DEVICE_ID_LOGITECH_DFP_WHEEL
Michal Malý96440c82011-08-04 16:18:11 +0200178};
179
Michal Malýe7c23442015-02-18 17:59:20 +0100180static const struct lg4ff_wheel_ident_info lg4ff_g25_ident_info = {
181 0xff00,
182 0x1200,
183 USB_DEVICE_ID_LOGITECH_G25_WHEEL
Michal Malý96440c82011-08-04 16:18:11 +0200184};
185
Michal Malýe7c23442015-02-18 17:59:20 +0100186static const struct lg4ff_wheel_ident_info lg4ff_g27_ident_info = {
187 0xfff0,
188 0x1230,
189 USB_DEVICE_ID_LOGITECH_G27_WHEEL
190};
191
192static const struct lg4ff_wheel_ident_info lg4ff_dfgt_ident_info = {
193 0xff00,
194 0x1300,
195 USB_DEVICE_ID_LOGITECH_DFGT_WHEEL
196};
197
198/* Multimode wheel identification checklists */
199static const struct lg4ff_wheel_ident_checklist lg4ff_main_checklist = {
200 4,
201 {&lg4ff_dfgt_ident_info,
202 &lg4ff_g27_ident_info,
203 &lg4ff_g25_ident_info,
204 &lg4ff_dfp_ident_info}
205};
206
207/* Compatibility mode switching commands */
Michal Malýf31a2de2015-02-18 17:59:23 +0100208/* EXT_CMD9 - Understood by G27 and DFGT */
209static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfex = {
210 2,
211 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
212 0xf8, 0x09, 0x00, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DF-EX with detach */
213};
214
215static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfp = {
216 2,
217 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
218 0xf8, 0x09, 0x01, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DFP with detach */
219};
220
221static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g25 = {
222 2,
223 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
224 0xf8, 0x09, 0x02, 0x01, 0x00, 0x00, 0x00} /* Switch mode to G25 with detach */
225};
226
227static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfgt = {
228 2,
229 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
230 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DFGT with detach */
231};
232
233static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g27 = {
234 2,
235 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
236 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* Switch mode to G27 with detach */
237};
238
239/* EXT_CMD1 - Understood by DFP, G25, G27 and DFGT */
240static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext01_dfp = {
Michal Malý96440c82011-08-04 16:18:11 +0200241 1,
242 {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
243};
244
Michal Malýf31a2de2015-02-18 17:59:23 +0100245/* EXT_CMD16 - Understood by G25 and G27 */
246static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext16_g25 = {
Michal Malý96440c82011-08-04 16:18:11 +0200247 1,
248 {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
249};
250
Michal Malý2b24a962012-09-23 22:41:08 +0200251/* Recalculates X axis value accordingly to currently selected range */
Michal Malý2a552c32015-04-08 22:56:39 +0200252static s32 lg4ff_adjust_dfp_x_axis(s32 value, u16 range)
Michal Malý2b24a962012-09-23 22:41:08 +0200253{
Michal Malý2a552c32015-04-08 22:56:39 +0200254 u16 max_range;
255 s32 new_value;
Michal Malý2b24a962012-09-23 22:41:08 +0200256
257 if (range == 900)
258 return value;
259 else if (range == 200)
260 return value;
261 else if (range < 200)
262 max_range = 200;
263 else
264 max_range = 900;
265
266 new_value = 8192 + mult_frac(value - 8192, max_range, range);
267 if (new_value < 0)
268 return 0;
269 else if (new_value > 16383)
270 return 16383;
271 else
272 return new_value;
273}
274
275int lg4ff_adjust_input_event(struct hid_device *hid, struct hid_field *field,
Michal Malý2a552c32015-04-08 22:56:39 +0200276 struct hid_usage *usage, s32 value, struct lg_drv_data *drv_data)
Michal Malý2b24a962012-09-23 22:41:08 +0200277{
278 struct lg4ff_device_entry *entry = drv_data->device_props;
Michal Malý2a552c32015-04-08 22:56:39 +0200279 s32 new_value = 0;
Michal Malý2b24a962012-09-23 22:41:08 +0200280
281 if (!entry) {
282 hid_err(hid, "Device properties not found");
283 return 0;
284 }
285
Michal Malý72529c62015-04-08 22:56:46 +0200286 switch (entry->wdata.product_id) {
Michal Malý2b24a962012-09-23 22:41:08 +0200287 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
288 switch (usage->code) {
289 case ABS_X:
Michal Malý72529c62015-04-08 22:56:46 +0200290 new_value = lg4ff_adjust_dfp_x_axis(value, entry->wdata.range);
Michal Malý2b24a962012-09-23 22:41:08 +0200291 input_event(field->hidinput->input, usage->type, usage->code, new_value);
292 return 1;
293 default:
294 return 0;
295 }
296 default:
297 return 0;
298 }
299}
300
Michal Malýd0afd84892015-04-08 22:56:40 +0200301static int lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
Simon Wood32c88cb2010-09-22 13:19:42 +0200302{
303 struct hid_device *hid = input_get_drvdata(dev);
304 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
305 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malý2a552c32015-04-08 22:56:39 +0200306 s32 *value = report->field[0]->value;
Simon Wood32c88cb2010-09-22 13:19:42 +0200307 int x;
308
Michal Malýa80fe5d2012-09-24 01:13:17 +0200309#define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0)
Simon Wood32c88cb2010-09-22 13:19:42 +0200310
311 switch (effect->type) {
312 case FF_CONSTANT:
313 x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */
314 CLAMP(x);
Simon Wood56930e72013-11-06 12:30:42 -0700315
316 if (x == 0x80) {
317 /* De-activate force in slot-1*/
318 value[0] = 0x13;
319 value[1] = 0x00;
320 value[2] = 0x00;
321 value[3] = 0x00;
322 value[4] = 0x00;
323 value[5] = 0x00;
324 value[6] = 0x00;
325
326 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
327 return 0;
328 }
329
Michal Malý74479ba2012-09-24 01:09:30 +0200330 value[0] = 0x11; /* Slot 1 */
331 value[1] = 0x08;
332 value[2] = x;
333 value[3] = 0x80;
334 value[4] = 0x00;
335 value[5] = 0x00;
336 value[6] = 0x00;
Simon Wood32c88cb2010-09-22 13:19:42 +0200337
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100338 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Simon Wood32c88cb2010-09-22 13:19:42 +0200339 break;
340 }
341 return 0;
342}
343
Michal Malý6e2de8e2011-08-04 16:22:07 +0200344/* Sends default autocentering command compatible with
345 * all wheels except Formula Force EX */
Michal Malýd0afd84892015-04-08 22:56:40 +0200346static void lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude)
Simon Wood32c88cb2010-09-22 13:19:42 +0200347{
348 struct hid_device *hid = input_get_drvdata(dev);
349 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
350 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malý2a552c32015-04-08 22:56:39 +0200351 s32 *value = report->field[0]->value;
352 u32 expand_a, expand_b;
Simon Wood18597622013-11-06 12:30:44 -0700353 struct lg4ff_device_entry *entry;
354 struct lg_drv_data *drv_data;
355
356 drv_data = hid_get_drvdata(hid);
357 if (!drv_data) {
358 hid_err(hid, "Private driver data not found!\n");
359 return;
360 }
361
362 entry = drv_data->device_props;
363 if (!entry) {
364 hid_err(hid, "Device properties not found!\n");
365 return;
366 }
Simon Woodf8c23152013-11-06 12:30:40 -0700367
Simon Woodd2c02da2013-11-06 12:30:41 -0700368 /* De-activate Auto-Center */
369 if (magnitude == 0) {
370 value[0] = 0xf5;
371 value[1] = 0x00;
372 value[2] = 0x00;
373 value[3] = 0x00;
374 value[4] = 0x00;
375 value[5] = 0x00;
376 value[6] = 0x00;
377
378 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
379 return;
380 }
381
Simon Woodf8c23152013-11-06 12:30:40 -0700382 if (magnitude <= 0xaaaa) {
383 expand_a = 0x0c * magnitude;
384 expand_b = 0x80 * magnitude;
385 } else {
386 expand_a = (0x0c * 0xaaaa) + 0x06 * (magnitude - 0xaaaa);
387 expand_b = (0x80 * 0xaaaa) + 0xff * (magnitude - 0xaaaa);
388 }
Simon Wood32c88cb2010-09-22 13:19:42 +0200389
Simon Wood18597622013-11-06 12:30:44 -0700390 /* Adjust for non-MOMO wheels */
Michal Malý72529c62015-04-08 22:56:46 +0200391 switch (entry->wdata.product_id) {
Simon Wood18597622013-11-06 12:30:44 -0700392 case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL:
393 case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2:
394 break;
395 default:
396 expand_a = expand_a >> 1;
397 break;
398 }
399
Michal Malý74479ba2012-09-24 01:09:30 +0200400 value[0] = 0xfe;
401 value[1] = 0x0d;
Simon Woodf8c23152013-11-06 12:30:40 -0700402 value[2] = expand_a / 0xaaaa;
403 value[3] = expand_a / 0xaaaa;
404 value[4] = expand_b / 0xaaaa;
Michal Malý74479ba2012-09-24 01:09:30 +0200405 value[5] = 0x00;
406 value[6] = 0x00;
Simon Wood32c88cb2010-09-22 13:19:42 +0200407
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100408 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Simon Woodd2c02da2013-11-06 12:30:41 -0700409
410 /* Activate Auto-Center */
411 value[0] = 0x14;
412 value[1] = 0x00;
413 value[2] = 0x00;
414 value[3] = 0x00;
415 value[4] = 0x00;
416 value[5] = 0x00;
417 value[6] = 0x00;
418
419 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Simon Wood32c88cb2010-09-22 13:19:42 +0200420}
421
Michal Malý6e2de8e2011-08-04 16:22:07 +0200422/* Sends autocentering command compatible with Formula Force EX */
Michal Malýd0afd84892015-04-08 22:56:40 +0200423static void lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude)
Michal Malý6e2de8e2011-08-04 16:22:07 +0200424{
425 struct hid_device *hid = input_get_drvdata(dev);
426 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
427 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malý2a552c32015-04-08 22:56:39 +0200428 s32 *value = report->field[0]->value;
Michal Malý6e2de8e2011-08-04 16:22:07 +0200429 magnitude = magnitude * 90 / 65535;
Michal Malý6e2de8e2011-08-04 16:22:07 +0200430
Michal Malý74479ba2012-09-24 01:09:30 +0200431 value[0] = 0xfe;
432 value[1] = 0x03;
433 value[2] = magnitude >> 14;
434 value[3] = magnitude >> 14;
435 value[4] = magnitude;
436 value[5] = 0x00;
437 value[6] = 0x00;
Michal Malý6e2de8e2011-08-04 16:22:07 +0200438
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100439 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malý6e2de8e2011-08-04 16:22:07 +0200440}
441
Michal Malý30bb75d2011-08-04 16:20:40 +0200442/* Sends command to set range compatible with G25/G27/Driving Force GT */
Michal Malýd0afd84892015-04-08 22:56:40 +0200443static void lg4ff_set_range_g25(struct hid_device *hid, u16 range)
Michal Malý30bb75d2011-08-04 16:20:40 +0200444{
445 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
446 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malý2a552c32015-04-08 22:56:39 +0200447 s32 *value = report->field[0]->value;
Michal Malý74479ba2012-09-24 01:09:30 +0200448
Michal Malý30bb75d2011-08-04 16:20:40 +0200449 dbg_hid("G25/G27/DFGT: setting range to %u\n", range);
450
Michal Malý74479ba2012-09-24 01:09:30 +0200451 value[0] = 0xf8;
452 value[1] = 0x81;
453 value[2] = range & 0x00ff;
454 value[3] = (range & 0xff00) >> 8;
455 value[4] = 0x00;
456 value[5] = 0x00;
457 value[6] = 0x00;
Michal Malý30bb75d2011-08-04 16:20:40 +0200458
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100459 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malý30bb75d2011-08-04 16:20:40 +0200460}
461
462/* Sends commands to set range compatible with Driving Force Pro wheel */
Michal Malýd0afd84892015-04-08 22:56:40 +0200463static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range)
Michal Malý30bb75d2011-08-04 16:20:40 +0200464{
465 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
466 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
467 int start_left, start_right, full_range;
Michal Malý2a552c32015-04-08 22:56:39 +0200468 s32 *value = report->field[0]->value;
Michal Malý74479ba2012-09-24 01:09:30 +0200469
Michal Malý30bb75d2011-08-04 16:20:40 +0200470 dbg_hid("Driving Force Pro: setting range to %u\n", range);
471
472 /* Prepare "coarse" limit command */
Michal Malý74479ba2012-09-24 01:09:30 +0200473 value[0] = 0xf8;
Michal Malýa80fe5d2012-09-24 01:13:17 +0200474 value[1] = 0x00; /* Set later */
Michal Malý74479ba2012-09-24 01:09:30 +0200475 value[2] = 0x00;
476 value[3] = 0x00;
477 value[4] = 0x00;
478 value[5] = 0x00;
479 value[6] = 0x00;
Michal Malý30bb75d2011-08-04 16:20:40 +0200480
481 if (range > 200) {
482 report->field[0]->value[1] = 0x03;
483 full_range = 900;
484 } else {
485 report->field[0]->value[1] = 0x02;
486 full_range = 200;
487 }
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100488 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malý30bb75d2011-08-04 16:20:40 +0200489
490 /* Prepare "fine" limit command */
Michal Malý74479ba2012-09-24 01:09:30 +0200491 value[0] = 0x81;
492 value[1] = 0x0b;
493 value[2] = 0x00;
494 value[3] = 0x00;
495 value[4] = 0x00;
496 value[5] = 0x00;
497 value[6] = 0x00;
Michal Malý30bb75d2011-08-04 16:20:40 +0200498
499 if (range == 200 || range == 900) { /* Do not apply any fine limit */
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100500 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malý30bb75d2011-08-04 16:20:40 +0200501 return;
502 }
503
504 /* Construct fine limit command */
505 start_left = (((full_range - range + 1) * 2047) / full_range);
506 start_right = 0xfff - start_left;
507
Michal Malý74479ba2012-09-24 01:09:30 +0200508 value[2] = start_left >> 4;
509 value[3] = start_right >> 4;
510 value[4] = 0xff;
511 value[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
512 value[6] = 0xff;
Michal Malý30bb75d2011-08-04 16:20:40 +0200513
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100514 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malý30bb75d2011-08-04 16:20:40 +0200515}
516
Michal Malýf31a2de2015-02-18 17:59:23 +0100517static const struct lg4ff_compat_mode_switch *lg4ff_get_mode_switch_command(const u16 real_product_id, const u16 target_product_id)
518{
519 switch (real_product_id) {
520 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
521 switch (target_product_id) {
522 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
523 return &lg4ff_mode_switch_ext01_dfp;
524 /* DFP can only be switched to its native mode */
525 default:
526 return NULL;
527 }
528 break;
529 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
530 switch (target_product_id) {
531 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
532 return &lg4ff_mode_switch_ext01_dfp;
533 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
534 return &lg4ff_mode_switch_ext16_g25;
535 /* G25 can only be switched to DFP mode or its native mode */
536 default:
537 return NULL;
538 }
539 break;
540 case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
541 switch (target_product_id) {
542 case USB_DEVICE_ID_LOGITECH_WHEEL:
543 return &lg4ff_mode_switch_ext09_dfex;
544 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
545 return &lg4ff_mode_switch_ext09_dfp;
546 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
547 return &lg4ff_mode_switch_ext09_g25;
548 case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
549 return &lg4ff_mode_switch_ext09_g27;
550 /* G27 can only be switched to DF-EX, DFP, G25 or its native mode */
551 default:
552 return NULL;
553 }
554 break;
555 case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
556 switch (target_product_id) {
557 case USB_DEVICE_ID_LOGITECH_WHEEL:
558 return &lg4ff_mode_switch_ext09_dfex;
559 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
560 return &lg4ff_mode_switch_ext09_dfp;
561 case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
562 return &lg4ff_mode_switch_ext09_dfgt;
563 /* DFGT can only be switched to DF-EX, DFP or its native mode */
564 default:
565 return NULL;
566 }
567 break;
568 /* No other wheels have multiple modes */
569 default:
570 return NULL;
571 }
572}
573
Michal Malýe7c23442015-02-18 17:59:20 +0100574static int lg4ff_switch_compatibility_mode(struct hid_device *hid, const struct lg4ff_compat_mode_switch *s)
Michal Malý96440c82011-08-04 16:18:11 +0200575{
Michal Malýc1740d12015-02-18 22:49:33 +0100576 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
577 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malý2a552c32015-04-08 22:56:39 +0200578 s32 *value = report->field[0]->value;
Michal Malýe7c23442015-02-18 17:59:20 +0100579 u8 i;
Michal Malý96440c82011-08-04 16:18:11 +0200580
Michal Malýe7c23442015-02-18 17:59:20 +0100581 for (i = 0; i < s->cmd_count; i++) {
Michal Malýc1740d12015-02-18 22:49:33 +0100582 u8 j;
Michal Malý96440c82011-08-04 16:18:11 +0200583
Michal Malýc1740d12015-02-18 22:49:33 +0100584 for (j = 0; j < 7; j++)
585 value[j] = s->cmd[j + (7*i)];
586
587 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malý96440c82011-08-04 16:18:11 +0200588 }
Michal Malýc1740d12015-02-18 22:49:33 +0100589 hid_hw_wait(hid);
Michal Malýe7c23442015-02-18 17:59:20 +0100590 return 0;
Michal Malý96440c82011-08-04 16:18:11 +0200591}
Simon Wood32c88cb2010-09-22 13:19:42 +0200592
Michal Malýb96d23e2015-02-18 17:59:21 +0100593static ssize_t lg4ff_alternate_modes_show(struct device *dev, struct device_attribute *attr, char *buf)
594{
595 struct hid_device *hid = to_hid_device(dev);
596 struct lg4ff_device_entry *entry;
597 struct lg_drv_data *drv_data;
598 ssize_t count = 0;
599 int i;
600
601 drv_data = hid_get_drvdata(hid);
602 if (!drv_data) {
603 hid_err(hid, "Private driver data not found!\n");
604 return 0;
605 }
606
607 entry = drv_data->device_props;
608 if (!entry) {
609 hid_err(hid, "Device properties not found!\n");
610 return 0;
611 }
612
Michal Malý72529c62015-04-08 22:56:46 +0200613 if (!entry->wdata.real_name) {
Michal Malýb96d23e2015-02-18 17:59:21 +0100614 hid_err(hid, "NULL pointer to string\n");
615 return 0;
616 }
617
618 for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
Michal Malý72529c62015-04-08 22:56:46 +0200619 if (entry->wdata.alternate_modes & BIT(i)) {
Michal Malýb96d23e2015-02-18 17:59:21 +0100620 /* Print tag and full name */
621 count += scnprintf(buf + count, PAGE_SIZE - count, "%s: %s",
622 lg4ff_alternate_modes[i].tag,
Michal Malý72529c62015-04-08 22:56:46 +0200623 !lg4ff_alternate_modes[i].product_id ? entry->wdata.real_name : lg4ff_alternate_modes[i].name);
Michal Malýb96d23e2015-02-18 17:59:21 +0100624 if (count >= PAGE_SIZE - 1)
625 return count;
626
627 /* Mark the currently active mode with an asterisk */
Michal Malý72529c62015-04-08 22:56:46 +0200628 if (lg4ff_alternate_modes[i].product_id == entry->wdata.product_id ||
629 (lg4ff_alternate_modes[i].product_id == 0 && entry->wdata.product_id == entry->wdata.real_product_id))
Michal Malýb96d23e2015-02-18 17:59:21 +0100630 count += scnprintf(buf + count, PAGE_SIZE - count, " *\n");
631 else
632 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
633
634 if (count >= PAGE_SIZE - 1)
635 return count;
636 }
637 }
638
639 return count;
640}
641
642static ssize_t lg4ff_alternate_modes_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
643{
Michal Malýf31a2de2015-02-18 17:59:23 +0100644 struct hid_device *hid = to_hid_device(dev);
645 struct lg4ff_device_entry *entry;
646 struct lg_drv_data *drv_data;
647 const struct lg4ff_compat_mode_switch *s;
648 u16 target_product_id = 0;
649 int i, ret;
650 char *lbuf;
651
652 drv_data = hid_get_drvdata(hid);
653 if (!drv_data) {
654 hid_err(hid, "Private driver data not found!\n");
655 return -EINVAL;
656 }
657
658 entry = drv_data->device_props;
659 if (!entry) {
660 hid_err(hid, "Device properties not found!\n");
661 return -EINVAL;
662 }
663
664 /* Allow \n at the end of the input parameter */
665 lbuf = kasprintf(GFP_KERNEL, "%s", buf);
666 if (!lbuf)
667 return -ENOMEM;
668
669 i = strlen(lbuf);
670 if (lbuf[i-1] == '\n') {
671 if (i == 1) {
672 kfree(lbuf);
673 return -EINVAL;
674 }
675 lbuf[i-1] = '\0';
676 }
677
678 for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
679 const u16 mode_product_id = lg4ff_alternate_modes[i].product_id;
680 const char *tag = lg4ff_alternate_modes[i].tag;
681
Michal Malý72529c62015-04-08 22:56:46 +0200682 if (entry->wdata.alternate_modes & BIT(i)) {
Michal Malýf31a2de2015-02-18 17:59:23 +0100683 if (!strcmp(tag, lbuf)) {
684 if (!mode_product_id)
Michal Malý72529c62015-04-08 22:56:46 +0200685 target_product_id = entry->wdata.real_product_id;
Michal Malýf31a2de2015-02-18 17:59:23 +0100686 else
687 target_product_id = mode_product_id;
688 break;
689 }
690 }
691 }
692
693 if (i == LG4FF_MODE_MAX_IDX) {
694 hid_info(hid, "Requested mode \"%s\" is not supported by the device\n", lbuf);
695 kfree(lbuf);
696 return -EINVAL;
697 }
698 kfree(lbuf); /* Not needed anymore */
699
Michal Malý72529c62015-04-08 22:56:46 +0200700 if (target_product_id == entry->wdata.product_id) /* Nothing to do */
Michal Malýf31a2de2015-02-18 17:59:23 +0100701 return count;
702
703 /* Automatic switching has to be disabled for the switch to DF-EX mode to work correctly */
704 if (target_product_id == USB_DEVICE_ID_LOGITECH_WHEEL && !lg4ff_no_autoswitch) {
705 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",
Michal Malý72529c62015-04-08 22:56:46 +0200706 entry->wdata.real_name);
Michal Malýf31a2de2015-02-18 17:59:23 +0100707 return -EINVAL;
708 }
709
710 /* Take care of hardware limitations */
Michal Malý72529c62015-04-08 22:56:46 +0200711 if ((entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_DFP_WHEEL || entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_G25_WHEEL) &&
712 entry->wdata.product_id > target_product_id) {
713 hid_info(hid, "\"%s\" cannot be switched back into \"%s\" mode\n", entry->wdata.real_name, lg4ff_alternate_modes[i].name);
Michal Malýf31a2de2015-02-18 17:59:23 +0100714 return -EINVAL;
715 }
716
Michal Malý72529c62015-04-08 22:56:46 +0200717 s = lg4ff_get_mode_switch_command(entry->wdata.real_product_id, target_product_id);
Michal Malýf31a2de2015-02-18 17:59:23 +0100718 if (!s) {
719 hid_err(hid, "Invalid target product ID %X\n", target_product_id);
720 return -EINVAL;
721 }
722
723 ret = lg4ff_switch_compatibility_mode(hid, s);
724 return (ret == 0 ? count : ret);
Michal Malýb96d23e2015-02-18 17:59:21 +0100725}
726static DEVICE_ATTR(alternate_modes, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_alternate_modes_show, lg4ff_alternate_modes_store);
727
Michal Malýfbf85e22015-04-08 22:56:41 +0200728/* Export the currently set range of the wheel */
729static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr,
730 char *buf)
Michal Malý30bb75d2011-08-04 16:20:40 +0200731{
Michal Malý30bb75d2011-08-04 16:20:40 +0200732 struct hid_device *hid = to_hid_device(dev);
Michal Malý3b6b17b2012-04-09 09:08:49 +0200733 struct lg4ff_device_entry *entry;
734 struct lg_drv_data *drv_data;
Michal Malý30bb75d2011-08-04 16:20:40 +0200735 size_t count;
736
Michal Malý3b6b17b2012-04-09 09:08:49 +0200737 drv_data = hid_get_drvdata(hid);
738 if (!drv_data) {
739 hid_err(hid, "Private driver data not found!\n");
740 return 0;
Michal Malý30bb75d2011-08-04 16:20:40 +0200741 }
Michal Malý3b6b17b2012-04-09 09:08:49 +0200742
743 entry = drv_data->device_props;
744 if (!entry) {
745 hid_err(hid, "Device properties not found!\n");
Michal Malý30bb75d2011-08-04 16:20:40 +0200746 return 0;
747 }
748
Michal Malý72529c62015-04-08 22:56:46 +0200749 count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->wdata.range);
Michal Malý30bb75d2011-08-04 16:20:40 +0200750 return count;
751}
752
753/* Set range to user specified value, call appropriate function
754 * according to the type of the wheel */
Michal Malýfbf85e22015-04-08 22:56:41 +0200755static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr,
756 const char *buf, size_t count)
Michal Malý30bb75d2011-08-04 16:20:40 +0200757{
Michal Malý30bb75d2011-08-04 16:20:40 +0200758 struct hid_device *hid = to_hid_device(dev);
Michal Malý3b6b17b2012-04-09 09:08:49 +0200759 struct lg4ff_device_entry *entry;
760 struct lg_drv_data *drv_data;
Michal Malý2a552c32015-04-08 22:56:39 +0200761 u16 range = simple_strtoul(buf, NULL, 10);
Michal Malý30bb75d2011-08-04 16:20:40 +0200762
Michal Malý3b6b17b2012-04-09 09:08:49 +0200763 drv_data = hid_get_drvdata(hid);
764 if (!drv_data) {
765 hid_err(hid, "Private driver data not found!\n");
Simon Wood29ff6652014-08-14 20:43:01 -0600766 return -EINVAL;
Michal Malý30bb75d2011-08-04 16:20:40 +0200767 }
Michal Malý3b6b17b2012-04-09 09:08:49 +0200768
769 entry = drv_data->device_props;
770 if (!entry) {
771 hid_err(hid, "Device properties not found!\n");
Simon Wood29ff6652014-08-14 20:43:01 -0600772 return -EINVAL;
Michal Malý30bb75d2011-08-04 16:20:40 +0200773 }
774
775 if (range == 0)
Michal Malý72529c62015-04-08 22:56:46 +0200776 range = entry->wdata.max_range;
Michal Malý30bb75d2011-08-04 16:20:40 +0200777
778 /* Check if the wheel supports range setting
779 * and that the range is within limits for the wheel */
Michal Malý72529c62015-04-08 22:56:46 +0200780 if (entry->wdata.set_range && range >= entry->wdata.min_range && range <= entry->wdata.max_range) {
781 entry->wdata.set_range(hid, range);
782 entry->wdata.range = range;
Michal Malý30bb75d2011-08-04 16:20:40 +0200783 }
784
785 return count;
786}
Michal Malýfbf85e22015-04-08 22:56:41 +0200787static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_range_show, lg4ff_range_store);
Michal Malý30bb75d2011-08-04 16:20:40 +0200788
Michal Malýb96d23e2015-02-18 17:59:21 +0100789static ssize_t lg4ff_real_id_show(struct device *dev, struct device_attribute *attr, char *buf)
790{
791 struct hid_device *hid = to_hid_device(dev);
792 struct lg4ff_device_entry *entry;
793 struct lg_drv_data *drv_data;
794 size_t count;
795
796 drv_data = hid_get_drvdata(hid);
797 if (!drv_data) {
798 hid_err(hid, "Private driver data not found!\n");
799 return 0;
800 }
801
802 entry = drv_data->device_props;
803 if (!entry) {
804 hid_err(hid, "Device properties not found!\n");
805 return 0;
806 }
807
Michal Malý72529c62015-04-08 22:56:46 +0200808 if (!entry->wdata.real_tag || !entry->wdata.real_name) {
Michal Malýb96d23e2015-02-18 17:59:21 +0100809 hid_err(hid, "NULL pointer to string\n");
810 return 0;
811 }
812
Michal Malý72529c62015-04-08 22:56:46 +0200813 count = scnprintf(buf, PAGE_SIZE, "%s: %s\n", entry->wdata.real_tag, entry->wdata.real_name);
Michal Malýb96d23e2015-02-18 17:59:21 +0100814 return count;
815}
816
817static ssize_t lg4ff_real_id_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
818{
819 /* Real ID is a read-only value */
820 return -EPERM;
821}
822static DEVICE_ATTR(real_id, S_IRUGO, lg4ff_real_id_show, lg4ff_real_id_store);
823
Simon Wood22bcefd2012-04-21 05:41:15 -0700824#ifdef CONFIG_LEDS_CLASS
Michal Malý2a552c32015-04-08 22:56:39 +0200825static void lg4ff_set_leds(struct hid_device *hid, u8 leds)
Simon Wood22bcefd2012-04-21 05:41:15 -0700826{
827 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
828 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malý2a552c32015-04-08 22:56:39 +0200829 s32 *value = report->field[0]->value;
Simon Wood22bcefd2012-04-21 05:41:15 -0700830
Michal Malý74479ba2012-09-24 01:09:30 +0200831 value[0] = 0xf8;
832 value[1] = 0x12;
833 value[2] = leds;
834 value[3] = 0x00;
835 value[4] = 0x00;
836 value[5] = 0x00;
837 value[6] = 0x00;
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100838 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Simon Wood22bcefd2012-04-21 05:41:15 -0700839}
840
841static void lg4ff_led_set_brightness(struct led_classdev *led_cdev,
842 enum led_brightness value)
843{
844 struct device *dev = led_cdev->dev->parent;
845 struct hid_device *hid = container_of(dev, struct hid_device, dev);
Axel Lin4629fd12012-09-13 14:09:33 +0800846 struct lg_drv_data *drv_data = hid_get_drvdata(hid);
Simon Wood22bcefd2012-04-21 05:41:15 -0700847 struct lg4ff_device_entry *entry;
848 int i, state = 0;
849
850 if (!drv_data) {
851 hid_err(hid, "Device data not found.");
852 return;
853 }
854
Michal Malý371a1d92015-04-08 22:56:43 +0200855 entry = drv_data->device_props;
Simon Wood22bcefd2012-04-21 05:41:15 -0700856
857 if (!entry) {
858 hid_err(hid, "Device properties not found.");
859 return;
860 }
861
862 for (i = 0; i < 5; i++) {
Michal Malý72529c62015-04-08 22:56:46 +0200863 if (led_cdev != entry->wdata.led[i])
Simon Wood22bcefd2012-04-21 05:41:15 -0700864 continue;
Michal Malý72529c62015-04-08 22:56:46 +0200865 state = (entry->wdata.led_state >> i) & 1;
Simon Wood22bcefd2012-04-21 05:41:15 -0700866 if (value == LED_OFF && state) {
Michal Malý72529c62015-04-08 22:56:46 +0200867 entry->wdata.led_state &= ~(1 << i);
868 lg4ff_set_leds(hid, entry->wdata.led_state);
Simon Wood22bcefd2012-04-21 05:41:15 -0700869 } else if (value != LED_OFF && !state) {
Michal Malý72529c62015-04-08 22:56:46 +0200870 entry->wdata.led_state |= 1 << i;
871 lg4ff_set_leds(hid, entry->wdata.led_state);
Simon Wood22bcefd2012-04-21 05:41:15 -0700872 }
873 break;
874 }
875}
876
877static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev)
878{
879 struct device *dev = led_cdev->dev->parent;
880 struct hid_device *hid = container_of(dev, struct hid_device, dev);
Axel Lin4629fd12012-09-13 14:09:33 +0800881 struct lg_drv_data *drv_data = hid_get_drvdata(hid);
Simon Wood22bcefd2012-04-21 05:41:15 -0700882 struct lg4ff_device_entry *entry;
883 int i, value = 0;
884
885 if (!drv_data) {
886 hid_err(hid, "Device data not found.");
887 return LED_OFF;
888 }
889
Michal Malý371a1d92015-04-08 22:56:43 +0200890 entry = drv_data->device_props;
Simon Wood22bcefd2012-04-21 05:41:15 -0700891
892 if (!entry) {
893 hid_err(hid, "Device properties not found.");
894 return LED_OFF;
895 }
896
897 for (i = 0; i < 5; i++)
Michal Malý72529c62015-04-08 22:56:46 +0200898 if (led_cdev == entry->wdata.led[i]) {
899 value = (entry->wdata.led_state >> i) & 1;
Simon Wood22bcefd2012-04-21 05:41:15 -0700900 break;
901 }
902
903 return value ? LED_FULL : LED_OFF;
904}
905#endif
906
Michal Malýe7c23442015-02-18 17:59:20 +0100907static u16 lg4ff_identify_multimode_wheel(struct hid_device *hid, const u16 reported_product_id, const u16 bcdDevice)
908{
909 const struct lg4ff_wheel_ident_checklist *checklist;
910 int i, from_idx, to_idx;
911
912 switch (reported_product_id) {
913 case USB_DEVICE_ID_LOGITECH_WHEEL:
914 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
915 checklist = &lg4ff_main_checklist;
916 from_idx = 0;
917 to_idx = checklist->count - 1;
918 break;
919 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
920 checklist = &lg4ff_main_checklist;
921 from_idx = 0;
922 to_idx = checklist->count - 2; /* End identity check at G25 */
923 break;
924 case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
925 checklist = &lg4ff_main_checklist;
926 from_idx = 1; /* Start identity check at G27 */
927 to_idx = checklist->count - 3; /* End identity check at G27 */
928 break;
929 case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
930 checklist = &lg4ff_main_checklist;
931 from_idx = 0;
932 to_idx = checklist->count - 4; /* End identity check at DFGT */
933 break;
934 default:
935 return 0;
936 }
937
938 for (i = from_idx; i <= to_idx; i++) {
939 const u16 mask = checklist->models[i]->mask;
940 const u16 result = checklist->models[i]->result;
941 const u16 real_product_id = checklist->models[i]->real_product_id;
942
943 if ((bcdDevice & mask) == result) {
944 dbg_hid("Found wheel with real PID %X whose reported PID is %X\n", real_product_id, reported_product_id);
945 return real_product_id;
946 }
947 }
948
Michal Malýf31a2de2015-02-18 17:59:23 +0100949 /* No match found. This is either Driving Force or an unknown
950 * wheel model, do not touch it */
Michal Malýe7c23442015-02-18 17:59:20 +0100951 dbg_hid("Wheel with bcdDevice %X was not recognized as multimode wheel, leaving in its current mode\n", bcdDevice);
952 return 0;
953}
954
955static int lg4ff_handle_multimode_wheel(struct hid_device *hid, u16 *real_product_id, const u16 bcdDevice)
956{
957 const u16 reported_product_id = hid->product;
958 int ret;
959
960 *real_product_id = lg4ff_identify_multimode_wheel(hid, reported_product_id, bcdDevice);
961 /* Probed wheel is not a multimode wheel */
962 if (!*real_product_id) {
963 *real_product_id = reported_product_id;
964 dbg_hid("Wheel is not a multimode wheel\n");
965 return LG4FF_MMODE_NOT_MULTIMODE;
966 }
967
968 /* Switch from "Driving Force" mode to native mode automatically.
969 * Otherwise keep the wheel in its current mode */
970 if (reported_product_id == USB_DEVICE_ID_LOGITECH_WHEEL &&
Michal Malýa54dc7792015-02-18 17:59:22 +0100971 reported_product_id != *real_product_id &&
972 !lg4ff_no_autoswitch) {
Michal Malýf31a2de2015-02-18 17:59:23 +0100973 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 +0100974
Michal Malýf31a2de2015-02-18 17:59:23 +0100975 if (!s) {
Michal Malýe7c23442015-02-18 17:59:20 +0100976 hid_err(hid, "Invalid product id %X\n", *real_product_id);
Michal Malýb96d23e2015-02-18 17:59:21 +0100977 return LG4FF_MMODE_NOT_MULTIMODE;
Michal Malýe7c23442015-02-18 17:59:20 +0100978 }
979
980 ret = lg4ff_switch_compatibility_mode(hid, s);
981 if (ret) {
982 /* Wheel could not have been switched to native mode,
983 * leave it in "Driving Force" mode and continue */
984 hid_err(hid, "Unable to switch wheel mode, errno %d\n", ret);
Michal Malýb96d23e2015-02-18 17:59:21 +0100985 return LG4FF_MMODE_IS_MULTIMODE;
Michal Malýe7c23442015-02-18 17:59:20 +0100986 }
987 return LG4FF_MMODE_SWITCHED;
988 }
989
Michal Malýb96d23e2015-02-18 17:59:21 +0100990 return LG4FF_MMODE_IS_MULTIMODE;
Michal Malýe7c23442015-02-18 17:59:20 +0100991}
992
993
Simon Wood32c88cb2010-09-22 13:19:42 +0200994int lg4ff_init(struct hid_device *hid)
995{
996 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
Simon Wood32c88cb2010-09-22 13:19:42 +0200997 struct input_dev *dev = hidinput->input;
Michal Malýe7c23442015-02-18 17:59:20 +0100998 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
999 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
Michal Malý30bb75d2011-08-04 16:20:40 +02001000 struct lg4ff_device_entry *entry;
Michal Malý3b6b17b2012-04-09 09:08:49 +02001001 struct lg_drv_data *drv_data;
Michal Malýb96d23e2015-02-18 17:59:21 +01001002 int error, i, j;
1003 int mmode_ret, mmode_idx = -1;
Michal Malýe7c23442015-02-18 17:59:20 +01001004 u16 real_product_id;
Simon Wood32c88cb2010-09-22 13:19:42 +02001005
Simon Wood32c88cb2010-09-22 13:19:42 +02001006 /* Check that the report looks ok */
Kees Cook0fb6bd02013-09-11 21:56:54 +02001007 if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
Simon Wood32c88cb2010-09-22 13:19:42 +02001008 return -1;
Michal Malý30bb75d2011-08-04 16:20:40 +02001009
Michal Malý72529c62015-04-08 22:56:46 +02001010 drv_data = hid_get_drvdata(hid);
1011 if (!drv_data) {
1012 hid_err(hid, "Cannot add device, private driver data not allocated\n");
1013 return -1;
1014 }
1015 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1016 if (!entry)
1017 return -ENOMEM;
1018 drv_data->device_props = entry;
1019
Michal Malýe7c23442015-02-18 17:59:20 +01001020 /* Check if a multimode wheel has been connected and
1021 * handle it appropriately */
Michal Malýb96d23e2015-02-18 17:59:21 +01001022 mmode_ret = lg4ff_handle_multimode_wheel(hid, &real_product_id, bcdDevice);
Michal Malýe7c23442015-02-18 17:59:20 +01001023
1024 /* Wheel has been told to switch to native mode. There is no point in going on
1025 * with the initialization as the wheel will do a USB reset when it switches mode
1026 */
Michal Malýb96d23e2015-02-18 17:59:21 +01001027 if (mmode_ret == LG4FF_MMODE_SWITCHED)
Michal Malýe7c23442015-02-18 17:59:20 +01001028 return 0;
Michal Malý72529c62015-04-08 22:56:46 +02001029 else if (mmode_ret < 0) {
1030 hid_err(hid, "Unable to switch device mode during initialization, errno %d\n", mmode_ret);
1031 error = mmode_ret;
1032 goto err_init;
1033 }
Michal Malýe7c23442015-02-18 17:59:20 +01001034
Michal Malý7362cd22011-08-04 16:16:09 +02001035 /* Check what wheel has been connected */
1036 for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
1037 if (hid->product == lg4ff_devices[i].product_id) {
1038 dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
1039 break;
1040 }
1041 }
Simon Wood32c88cb2010-09-22 13:19:42 +02001042
Michal Malý7362cd22011-08-04 16:16:09 +02001043 if (i == ARRAY_SIZE(lg4ff_devices)) {
Michal Malý9c2a6bd2015-04-08 22:56:44 +02001044 hid_err(hid, "This device is flagged to be handled by the lg4ff module but this module does not know how to handle it. "
1045 "Please report this as a bug to LKML, Simon Wood <simon@mungewell.org> or "
1046 "Michal Maly <madcatxster@devoid-pointer.net>\n");
Michal Malý72529c62015-04-08 22:56:46 +02001047 error = -1;
1048 goto err_init;
Michal Malý7362cd22011-08-04 16:16:09 +02001049 }
1050
Michal Malýb96d23e2015-02-18 17:59:21 +01001051 if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1052 for (mmode_idx = 0; mmode_idx < ARRAY_SIZE(lg4ff_multimode_wheels); mmode_idx++) {
1053 if (real_product_id == lg4ff_multimode_wheels[mmode_idx].product_id)
1054 break;
1055 }
1056
1057 if (mmode_idx == ARRAY_SIZE(lg4ff_multimode_wheels)) {
1058 hid_err(hid, "Device product ID %X is not listed as a multimode wheel", real_product_id);
Michal Malý72529c62015-04-08 22:56:46 +02001059 error = -1;
1060 goto err_init;
Michal Malýb96d23e2015-02-18 17:59:21 +01001061 }
1062 }
1063
Michal Malý7362cd22011-08-04 16:16:09 +02001064 /* Set supported force feedback capabilities */
1065 for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
1066 set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
Simon Wood32c88cb2010-09-22 13:19:42 +02001067
Michal Malýd0afd84892015-04-08 22:56:40 +02001068 error = input_ff_create_memless(dev, NULL, lg4ff_play);
Simon Wood32c88cb2010-09-22 13:19:42 +02001069
1070 if (error)
Michal Malý72529c62015-04-08 22:56:46 +02001071 goto err_init;
Simon Wood32c88cb2010-09-22 13:19:42 +02001072
Michal Malý72529c62015-04-08 22:56:46 +02001073 entry->wdata.product_id = lg4ff_devices[i].product_id;
1074 entry->wdata.real_product_id = real_product_id;
1075 entry->wdata.min_range = lg4ff_devices[i].min_range;
1076 entry->wdata.max_range = lg4ff_devices[i].max_range;
1077 entry->wdata.set_range = lg4ff_devices[i].set_range;
Michal Malýb96d23e2015-02-18 17:59:21 +01001078 if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1079 BUG_ON(mmode_idx == -1);
Michal Malý72529c62015-04-08 22:56:46 +02001080 entry->wdata.alternate_modes = lg4ff_multimode_wheels[mmode_idx].alternate_modes;
1081 entry->wdata.real_tag = lg4ff_multimode_wheels[mmode_idx].real_tag;
1082 entry->wdata.real_name = lg4ff_multimode_wheels[mmode_idx].real_name;
Michal Malýb96d23e2015-02-18 17:59:21 +01001083 }
Michal Malý30bb75d2011-08-04 16:20:40 +02001084
Simon Wood114a55c2013-11-06 12:30:43 -07001085 /* Check if autocentering is available and
1086 * set the centering force to zero by default */
1087 if (test_bit(FF_AUTOCENTER, dev->ffbit)) {
Michal Malýe7c23442015-02-18 17:59:20 +01001088 /* Formula Force EX expects different autocentering command */
1089 if ((bcdDevice >> 8) == LG4FF_FFEX_REV_MAJ &&
1090 (bcdDevice & 0xff) == LG4FF_FFEX_REV_MIN)
Michal Malýd0afd84892015-04-08 22:56:40 +02001091 dev->ff->set_autocenter = lg4ff_set_autocenter_ffex;
Simon Wood114a55c2013-11-06 12:30:43 -07001092 else
Michal Malýd0afd84892015-04-08 22:56:40 +02001093 dev->ff->set_autocenter = lg4ff_set_autocenter_default;
Simon Wood114a55c2013-11-06 12:30:43 -07001094
1095 dev->ff->set_autocenter(dev, 0);
1096 }
1097
Michal Malý30bb75d2011-08-04 16:20:40 +02001098 /* Create sysfs interface */
1099 error = device_create_file(&hid->dev, &dev_attr_range);
1100 if (error)
Michal Malý72529c62015-04-08 22:56:46 +02001101 goto err_init;
Michal Malýb96d23e2015-02-18 17:59:21 +01001102 if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1103 error = device_create_file(&hid->dev, &dev_attr_real_id);
1104 if (error)
Michal Malý72529c62015-04-08 22:56:46 +02001105 goto err_init;
1106
Michal Malýb96d23e2015-02-18 17:59:21 +01001107 error = device_create_file(&hid->dev, &dev_attr_alternate_modes);
1108 if (error)
Michal Malý72529c62015-04-08 22:56:46 +02001109 goto err_init;
Michal Malýb96d23e2015-02-18 17:59:21 +01001110 }
Michal Malý30bb75d2011-08-04 16:20:40 +02001111 dbg_hid("sysfs interface created\n");
1112
1113 /* Set the maximum range to start with */
Michal Malý72529c62015-04-08 22:56:46 +02001114 entry->wdata.range = entry->wdata.max_range;
1115 if (entry->wdata.set_range)
1116 entry->wdata.set_range(hid, entry->wdata.range);
Michal Malý30bb75d2011-08-04 16:20:40 +02001117
Simon Wood22bcefd2012-04-21 05:41:15 -07001118#ifdef CONFIG_LEDS_CLASS
1119 /* register led subsystem - G27 only */
Michal Malý72529c62015-04-08 22:56:46 +02001120 entry->wdata.led_state = 0;
Simon Wood22bcefd2012-04-21 05:41:15 -07001121 for (j = 0; j < 5; j++)
Michal Malý72529c62015-04-08 22:56:46 +02001122 entry->wdata.led[j] = NULL;
Simon Wood22bcefd2012-04-21 05:41:15 -07001123
1124 if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL) {
1125 struct led_classdev *led;
1126 size_t name_sz;
1127 char *name;
1128
1129 lg4ff_set_leds(hid, 0);
1130
1131 name_sz = strlen(dev_name(&hid->dev)) + 8;
1132
1133 for (j = 0; j < 5; j++) {
1134 led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
1135 if (!led) {
1136 hid_err(hid, "can't allocate memory for LED %d\n", j);
Michal Malý72529c62015-04-08 22:56:46 +02001137 goto err_leds;
Simon Wood22bcefd2012-04-21 05:41:15 -07001138 }
1139
1140 name = (void *)(&led[1]);
1141 snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1);
1142 led->name = name;
1143 led->brightness = 0;
1144 led->max_brightness = 1;
1145 led->brightness_get = lg4ff_led_get_brightness;
1146 led->brightness_set = lg4ff_led_set_brightness;
1147
Michal Malý72529c62015-04-08 22:56:46 +02001148 entry->wdata.led[j] = led;
Simon Wood22bcefd2012-04-21 05:41:15 -07001149 error = led_classdev_register(&hid->dev, led);
1150
1151 if (error) {
1152 hid_err(hid, "failed to register LED %d. Aborting.\n", j);
Michal Malý72529c62015-04-08 22:56:46 +02001153err_leds:
Simon Wood22bcefd2012-04-21 05:41:15 -07001154 /* Deregister LEDs (if any) */
1155 for (j = 0; j < 5; j++) {
Michal Malý72529c62015-04-08 22:56:46 +02001156 led = entry->wdata.led[j];
1157 entry->wdata.led[j] = NULL;
Simon Wood22bcefd2012-04-21 05:41:15 -07001158 if (!led)
1159 continue;
1160 led_classdev_unregister(led);
1161 kfree(led);
1162 }
1163 goto out; /* Let the driver continue without LEDs */
1164 }
1165 }
1166 }
Simon Wood22bcefd2012-04-21 05:41:15 -07001167out:
Jiri Kosinac6e6dc82012-04-23 21:08:15 +02001168#endif
Simon Wood640138002012-04-21 05:41:16 -07001169 hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n");
Simon Wood32c88cb2010-09-22 13:19:42 +02001170 return 0;
Michal Malý72529c62015-04-08 22:56:46 +02001171
1172err_init:
1173 drv_data->device_props = NULL;
1174 kfree(entry);
1175 return error;
Simon Wood32c88cb2010-09-22 13:19:42 +02001176}
1177
Michal Malý30bb75d2011-08-04 16:20:40 +02001178int lg4ff_deinit(struct hid_device *hid)
1179{
Michal Malý30bb75d2011-08-04 16:20:40 +02001180 struct lg4ff_device_entry *entry;
Michal Malý3b6b17b2012-04-09 09:08:49 +02001181 struct lg_drv_data *drv_data;
1182
Michal Malý3b6b17b2012-04-09 09:08:49 +02001183 drv_data = hid_get_drvdata(hid);
1184 if (!drv_data) {
1185 hid_err(hid, "Error while deinitializing device, no private driver data.\n");
Michal Malý30bb75d2011-08-04 16:20:40 +02001186 return -1;
1187 }
Michal Malý3b6b17b2012-04-09 09:08:49 +02001188 entry = drv_data->device_props;
Michal Malýe7c23442015-02-18 17:59:20 +01001189 if (!entry)
1190 goto out; /* Nothing more to do */
1191
Michal Malýb96d23e2015-02-18 17:59:21 +01001192 /* Multimode devices will have at least the "MODE_NATIVE" bit set */
Michal Malý72529c62015-04-08 22:56:46 +02001193 if (entry->wdata.alternate_modes) {
Michal Malýb96d23e2015-02-18 17:59:21 +01001194 device_remove_file(&hid->dev, &dev_attr_real_id);
1195 device_remove_file(&hid->dev, &dev_attr_alternate_modes);
1196 }
1197
Michal Malý72529c62015-04-08 22:56:46 +02001198 device_remove_file(&hid->dev, &dev_attr_range);
Simon Wood22bcefd2012-04-21 05:41:15 -07001199#ifdef CONFIG_LEDS_CLASS
1200 {
1201 int j;
1202 struct led_classdev *led;
1203
1204 /* Deregister LEDs (if any) */
1205 for (j = 0; j < 5; j++) {
1206
Michal Malý72529c62015-04-08 22:56:46 +02001207 led = entry->wdata.led[j];
1208 entry->wdata.led[j] = NULL;
Simon Wood22bcefd2012-04-21 05:41:15 -07001209 if (!led)
1210 continue;
1211 led_classdev_unregister(led);
1212 kfree(led);
1213 }
1214 }
1215#endif
Michal Malýb211a632015-04-08 22:56:47 +02001216 hid_hw_stop(hid);
1217 drv_data->device_props = NULL;
Simon Wood22bcefd2012-04-21 05:41:15 -07001218
Michal Malý3b6b17b2012-04-09 09:08:49 +02001219 kfree(entry);
Michal Malýe7c23442015-02-18 17:59:20 +01001220out:
Michal Malý30bb75d2011-08-04 16:20:40 +02001221 dbg_hid("Device successfully unregistered\n");
1222 return 0;
1223}