blob: f7f4eb6e20e766b18ee3fd940e516456726cb2a1 [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ýd0afd842015-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 {
Michal Malýc918fe72015-04-08 22:56:48 +020092 spinlock_t report_lock; /* Protect output HID report */
Michal Malý72529c62015-04-08 22:56:46 +020093 struct lg4ff_wheel_data wdata;
94};
95
Michal Malý7362cd22011-08-04 16:16:09 +020096static const signed short lg4ff_wheel_effects[] = {
Simon Wood32c88cb2010-09-22 13:19:42 +020097 FF_CONSTANT,
98 FF_AUTOCENTER,
99 -1
100};
101
Michal Malý7362cd22011-08-04 16:16:09 +0200102struct lg4ff_wheel {
Michal Malý2a552c32015-04-08 22:56:39 +0200103 const u32 product_id;
Michal Malý7362cd22011-08-04 16:16:09 +0200104 const signed short *ff_effects;
Michal Malý2a552c32015-04-08 22:56:39 +0200105 const u16 min_range;
106 const u16 max_range;
Michal Malý30bb75d2011-08-04 16:20:40 +0200107 void (*set_range)(struct hid_device *hid, u16 range);
Michal Malý7362cd22011-08-04 16:16:09 +0200108};
109
Michal Malýe7c23442015-02-18 17:59:20 +0100110struct lg4ff_compat_mode_switch {
Michal Malý2a552c32015-04-08 22:56:39 +0200111 const u8 cmd_count; /* Number of commands to send */
112 const u8 cmd[];
Michal Malýe7c23442015-02-18 17:59:20 +0100113};
114
115struct lg4ff_wheel_ident_info {
116 const u16 mask;
117 const u16 result;
118 const u16 real_product_id;
119};
120
121struct lg4ff_wheel_ident_checklist {
122 const u32 count;
123 const struct lg4ff_wheel_ident_info *models[];
124};
125
Michal Malýb96d23e2015-02-18 17:59:21 +0100126struct lg4ff_multimode_wheel {
127 const u16 product_id;
128 const u32 alternate_modes;
129 const char *real_tag;
130 const char *real_name;
131};
132
133struct lg4ff_alternate_mode {
134 const u16 product_id;
135 const char *tag;
136 const char *name;
137};
138
Michal Malý7362cd22011-08-04 16:16:09 +0200139static const struct lg4ff_wheel lg4ff_devices[] = {
Michal Malý30bb75d2011-08-04 16:20:40 +0200140 {USB_DEVICE_ID_LOGITECH_WHEEL, lg4ff_wheel_effects, 40, 270, NULL},
141 {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL, lg4ff_wheel_effects, 40, 270, NULL},
Michal Malýd0afd842015-04-08 22:56:40 +0200142 {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_dfp},
143 {USB_DEVICE_ID_LOGITECH_G25_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
144 {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
145 {USB_DEVICE_ID_LOGITECH_G27_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
Michal Malý30bb75d2011-08-04 16:20:40 +0200146 {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL},
147 {USB_DEVICE_ID_LOGITECH_WII_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}
Michal Malý7362cd22011-08-04 16:16:09 +0200148};
149
Michal Malýb96d23e2015-02-18 17:59:21 +0100150static const struct lg4ff_multimode_wheel lg4ff_multimode_wheels[] = {
151 {USB_DEVICE_ID_LOGITECH_DFP_WHEEL,
152 LG4FF_MODE_NATIVE | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
153 LG4FF_DFP_TAG, LG4FF_DFP_NAME},
154 {USB_DEVICE_ID_LOGITECH_G25_WHEEL,
155 LG4FF_MODE_NATIVE | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
156 LG4FF_G25_TAG, LG4FF_G25_NAME},
157 {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL,
158 LG4FF_MODE_NATIVE | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
159 LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
160 {USB_DEVICE_ID_LOGITECH_G27_WHEEL,
161 LG4FF_MODE_NATIVE | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
162 LG4FF_G27_TAG, LG4FF_G27_NAME},
163};
164
165static const struct lg4ff_alternate_mode lg4ff_alternate_modes[] = {
166 [LG4FF_MODE_NATIVE_IDX] = {0, "native", ""},
167 [LG4FF_MODE_DFEX_IDX] = {USB_DEVICE_ID_LOGITECH_WHEEL, LG4FF_DFEX_TAG, LG4FF_DFEX_NAME},
168 [LG4FF_MODE_DFP_IDX] = {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, LG4FF_DFP_TAG, LG4FF_DFP_NAME},
169 [LG4FF_MODE_G25_IDX] = {USB_DEVICE_ID_LOGITECH_G25_WHEEL, LG4FF_G25_TAG, LG4FF_G25_NAME},
170 [LG4FF_MODE_DFGT_IDX] = {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
171 [LG4FF_MODE_G27_IDX] = {USB_DEVICE_ID_LOGITECH_G27_WHEEL, LG4FF_G27_TAG, LG4FF_G27_NAME}
172};
173
Michal Malýe7c23442015-02-18 17:59:20 +0100174/* Multimode wheel identificators */
175static const struct lg4ff_wheel_ident_info lg4ff_dfp_ident_info = {
176 0xf000,
177 0x1000,
178 USB_DEVICE_ID_LOGITECH_DFP_WHEEL
Michal Malý96440c82011-08-04 16:18:11 +0200179};
180
Michal Malýe7c23442015-02-18 17:59:20 +0100181static const struct lg4ff_wheel_ident_info lg4ff_g25_ident_info = {
182 0xff00,
183 0x1200,
184 USB_DEVICE_ID_LOGITECH_G25_WHEEL
Michal Malý96440c82011-08-04 16:18:11 +0200185};
186
Michal Malýe7c23442015-02-18 17:59:20 +0100187static const struct lg4ff_wheel_ident_info lg4ff_g27_ident_info = {
188 0xfff0,
189 0x1230,
190 USB_DEVICE_ID_LOGITECH_G27_WHEEL
191};
192
193static const struct lg4ff_wheel_ident_info lg4ff_dfgt_ident_info = {
194 0xff00,
195 0x1300,
196 USB_DEVICE_ID_LOGITECH_DFGT_WHEEL
197};
198
199/* Multimode wheel identification checklists */
200static const struct lg4ff_wheel_ident_checklist lg4ff_main_checklist = {
201 4,
202 {&lg4ff_dfgt_ident_info,
203 &lg4ff_g27_ident_info,
204 &lg4ff_g25_ident_info,
205 &lg4ff_dfp_ident_info}
206};
207
208/* Compatibility mode switching commands */
Michal Malýf31a2de2015-02-18 17:59:23 +0100209/* EXT_CMD9 - Understood by G27 and DFGT */
210static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfex = {
211 2,
212 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
213 0xf8, 0x09, 0x00, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DF-EX with detach */
214};
215
216static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfp = {
217 2,
218 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
219 0xf8, 0x09, 0x01, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DFP with detach */
220};
221
222static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g25 = {
223 2,
224 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
225 0xf8, 0x09, 0x02, 0x01, 0x00, 0x00, 0x00} /* Switch mode to G25 with detach */
226};
227
228static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfgt = {
229 2,
230 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
231 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DFGT with detach */
232};
233
234static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g27 = {
235 2,
236 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
237 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* Switch mode to G27 with detach */
238};
239
240/* EXT_CMD1 - Understood by DFP, G25, G27 and DFGT */
241static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext01_dfp = {
Michal Malý96440c82011-08-04 16:18:11 +0200242 1,
243 {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
244};
245
Michal Malýf31a2de2015-02-18 17:59:23 +0100246/* EXT_CMD16 - Understood by G25 and G27 */
247static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext16_g25 = {
Michal Malý96440c82011-08-04 16:18:11 +0200248 1,
249 {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
250};
251
Michal Malý2b24a962012-09-23 22:41:08 +0200252/* Recalculates X axis value accordingly to currently selected range */
Michal Malý2a552c32015-04-08 22:56:39 +0200253static s32 lg4ff_adjust_dfp_x_axis(s32 value, u16 range)
Michal Malý2b24a962012-09-23 22:41:08 +0200254{
Michal Malý2a552c32015-04-08 22:56:39 +0200255 u16 max_range;
256 s32 new_value;
Michal Malý2b24a962012-09-23 22:41:08 +0200257
258 if (range == 900)
259 return value;
260 else if (range == 200)
261 return value;
262 else if (range < 200)
263 max_range = 200;
264 else
265 max_range = 900;
266
267 new_value = 8192 + mult_frac(value - 8192, max_range, range);
268 if (new_value < 0)
269 return 0;
270 else if (new_value > 16383)
271 return 16383;
272 else
273 return new_value;
274}
275
276int lg4ff_adjust_input_event(struct hid_device *hid, struct hid_field *field,
Michal Malý2a552c32015-04-08 22:56:39 +0200277 struct hid_usage *usage, s32 value, struct lg_drv_data *drv_data)
Michal Malý2b24a962012-09-23 22:41:08 +0200278{
279 struct lg4ff_device_entry *entry = drv_data->device_props;
Michal Malý2a552c32015-04-08 22:56:39 +0200280 s32 new_value = 0;
Michal Malý2b24a962012-09-23 22:41:08 +0200281
282 if (!entry) {
283 hid_err(hid, "Device properties not found");
284 return 0;
285 }
286
Michal Malý72529c62015-04-08 22:56:46 +0200287 switch (entry->wdata.product_id) {
Michal Malý2b24a962012-09-23 22:41:08 +0200288 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
289 switch (usage->code) {
290 case ABS_X:
Michal Malý72529c62015-04-08 22:56:46 +0200291 new_value = lg4ff_adjust_dfp_x_axis(value, entry->wdata.range);
Michal Malý2b24a962012-09-23 22:41:08 +0200292 input_event(field->hidinput->input, usage->type, usage->code, new_value);
293 return 1;
294 default:
295 return 0;
296 }
297 default:
298 return 0;
299 }
300}
301
Michal Malýd0afd842015-04-08 22:56:40 +0200302static int lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
Simon Wood32c88cb2010-09-22 13:19:42 +0200303{
304 struct hid_device *hid = input_get_drvdata(dev);
305 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
306 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malýc918fe72015-04-08 22:56:48 +0200307 struct lg4ff_device_entry *entry;
308 struct lg_drv_data *drv_data;
309 unsigned long flags;
Michal Malý2a552c32015-04-08 22:56:39 +0200310 s32 *value = report->field[0]->value;
Simon Wood32c88cb2010-09-22 13:19:42 +0200311 int x;
312
Michal Malýc918fe72015-04-08 22:56:48 +0200313 drv_data = hid_get_drvdata(hid);
314 if (!drv_data) {
315 hid_err(hid, "Private driver data not found!\n");
316 return -EINVAL;
317 }
318
319 entry = drv_data->device_props;
320 if (!entry) {
321 hid_err(hid, "Device properties not found!\n");
322 return -EINVAL;
323 }
324
Michal Malýa80fe5d2012-09-24 01:13:17 +0200325#define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0)
Simon Wood32c88cb2010-09-22 13:19:42 +0200326
327 switch (effect->type) {
328 case FF_CONSTANT:
329 x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */
330 CLAMP(x);
Simon Wood56930e72013-11-06 12:30:42 -0700331
Michal Malýc918fe72015-04-08 22:56:48 +0200332 spin_lock_irqsave(&entry->report_lock, flags);
Simon Wood56930e72013-11-06 12:30:42 -0700333 if (x == 0x80) {
334 /* De-activate force in slot-1*/
335 value[0] = 0x13;
336 value[1] = 0x00;
337 value[2] = 0x00;
338 value[3] = 0x00;
339 value[4] = 0x00;
340 value[5] = 0x00;
341 value[6] = 0x00;
342
343 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malýc918fe72015-04-08 22:56:48 +0200344 spin_unlock_irqrestore(&entry->report_lock, flags);
Simon Wood56930e72013-11-06 12:30:42 -0700345 return 0;
346 }
347
Michal Malý74479ba2012-09-24 01:09:30 +0200348 value[0] = 0x11; /* Slot 1 */
349 value[1] = 0x08;
350 value[2] = x;
351 value[3] = 0x80;
352 value[4] = 0x00;
353 value[5] = 0x00;
354 value[6] = 0x00;
Simon Wood32c88cb2010-09-22 13:19:42 +0200355
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100356 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malýc918fe72015-04-08 22:56:48 +0200357 spin_unlock_irqrestore(&entry->report_lock, flags);
Simon Wood32c88cb2010-09-22 13:19:42 +0200358 break;
359 }
360 return 0;
361}
362
Michal Malý6e2de8e2011-08-04 16:22:07 +0200363/* Sends default autocentering command compatible with
364 * all wheels except Formula Force EX */
Michal Malýd0afd842015-04-08 22:56:40 +0200365static void lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude)
Simon Wood32c88cb2010-09-22 13:19:42 +0200366{
367 struct hid_device *hid = input_get_drvdata(dev);
368 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
369 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malý2a552c32015-04-08 22:56:39 +0200370 s32 *value = report->field[0]->value;
371 u32 expand_a, expand_b;
Simon Wood18597622013-11-06 12:30:44 -0700372 struct lg4ff_device_entry *entry;
373 struct lg_drv_data *drv_data;
Michal Malýc918fe72015-04-08 22:56:48 +0200374 unsigned long flags;
Simon Wood18597622013-11-06 12:30:44 -0700375
376 drv_data = hid_get_drvdata(hid);
377 if (!drv_data) {
378 hid_err(hid, "Private driver data not found!\n");
379 return;
380 }
381
382 entry = drv_data->device_props;
383 if (!entry) {
384 hid_err(hid, "Device properties not found!\n");
385 return;
386 }
Simon Woodf8c23152013-11-06 12:30:40 -0700387
Simon Woodd2c02da2013-11-06 12:30:41 -0700388 /* De-activate Auto-Center */
Michal Malýc918fe72015-04-08 22:56:48 +0200389 spin_lock_irqsave(&entry->report_lock, flags);
Simon Woodd2c02da2013-11-06 12:30:41 -0700390 if (magnitude == 0) {
391 value[0] = 0xf5;
392 value[1] = 0x00;
393 value[2] = 0x00;
394 value[3] = 0x00;
395 value[4] = 0x00;
396 value[5] = 0x00;
397 value[6] = 0x00;
398
399 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malýc918fe72015-04-08 22:56:48 +0200400 spin_unlock_irqrestore(&entry->report_lock, flags);
Simon Woodd2c02da2013-11-06 12:30:41 -0700401 return;
402 }
403
Simon Woodf8c23152013-11-06 12:30:40 -0700404 if (magnitude <= 0xaaaa) {
405 expand_a = 0x0c * magnitude;
406 expand_b = 0x80 * magnitude;
407 } else {
408 expand_a = (0x0c * 0xaaaa) + 0x06 * (magnitude - 0xaaaa);
409 expand_b = (0x80 * 0xaaaa) + 0xff * (magnitude - 0xaaaa);
410 }
Simon Wood32c88cb2010-09-22 13:19:42 +0200411
Simon Wood18597622013-11-06 12:30:44 -0700412 /* Adjust for non-MOMO wheels */
Michal Malý72529c62015-04-08 22:56:46 +0200413 switch (entry->wdata.product_id) {
Simon Wood18597622013-11-06 12:30:44 -0700414 case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL:
415 case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2:
416 break;
417 default:
418 expand_a = expand_a >> 1;
419 break;
420 }
421
Michal Malý74479ba2012-09-24 01:09:30 +0200422 value[0] = 0xfe;
423 value[1] = 0x0d;
Simon Woodf8c23152013-11-06 12:30:40 -0700424 value[2] = expand_a / 0xaaaa;
425 value[3] = expand_a / 0xaaaa;
426 value[4] = expand_b / 0xaaaa;
Michal Malý74479ba2012-09-24 01:09:30 +0200427 value[5] = 0x00;
428 value[6] = 0x00;
Simon Wood32c88cb2010-09-22 13:19:42 +0200429
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100430 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Simon Woodd2c02da2013-11-06 12:30:41 -0700431
432 /* Activate Auto-Center */
433 value[0] = 0x14;
434 value[1] = 0x00;
435 value[2] = 0x00;
436 value[3] = 0x00;
437 value[4] = 0x00;
438 value[5] = 0x00;
439 value[6] = 0x00;
440
441 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malýc918fe72015-04-08 22:56:48 +0200442 spin_unlock_irqrestore(&entry->report_lock, flags);
Simon Wood32c88cb2010-09-22 13:19:42 +0200443}
444
Michal Malý6e2de8e2011-08-04 16:22:07 +0200445/* Sends autocentering command compatible with Formula Force EX */
Michal Malýd0afd842015-04-08 22:56:40 +0200446static void lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude)
Michal Malý6e2de8e2011-08-04 16:22:07 +0200447{
448 struct hid_device *hid = input_get_drvdata(dev);
449 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
450 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malýc918fe72015-04-08 22:56:48 +0200451 struct lg4ff_device_entry *entry;
452 struct lg_drv_data *drv_data;
453 unsigned long flags;
Michal Malý2a552c32015-04-08 22:56:39 +0200454 s32 *value = report->field[0]->value;
Michal Malý6e2de8e2011-08-04 16:22:07 +0200455 magnitude = magnitude * 90 / 65535;
Michal Malý6e2de8e2011-08-04 16:22:07 +0200456
Michal Malýc918fe72015-04-08 22:56:48 +0200457 drv_data = hid_get_drvdata(hid);
458 if (!drv_data) {
459 hid_err(hid, "Private driver data not found!\n");
460 return;
461 }
462
463 entry = drv_data->device_props;
464 if (!entry) {
465 hid_err(hid, "Device properties not found!\n");
466 return;
467 }
468
469 spin_lock_irqsave(&entry->report_lock, flags);
Michal Malý74479ba2012-09-24 01:09:30 +0200470 value[0] = 0xfe;
471 value[1] = 0x03;
472 value[2] = magnitude >> 14;
473 value[3] = magnitude >> 14;
474 value[4] = magnitude;
475 value[5] = 0x00;
476 value[6] = 0x00;
Michal Malý6e2de8e2011-08-04 16:22:07 +0200477
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100478 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malýc918fe72015-04-08 22:56:48 +0200479 spin_unlock_irqrestore(&entry->report_lock, flags);
Michal Malý6e2de8e2011-08-04 16:22:07 +0200480}
481
Michal Malý30bb75d2011-08-04 16:20:40 +0200482/* Sends command to set range compatible with G25/G27/Driving Force GT */
Michal Malýd0afd842015-04-08 22:56:40 +0200483static void lg4ff_set_range_g25(struct hid_device *hid, u16 range)
Michal Malý30bb75d2011-08-04 16:20:40 +0200484{
485 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
486 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malýc918fe72015-04-08 22:56:48 +0200487 struct lg4ff_device_entry *entry;
488 struct lg_drv_data *drv_data;
489 unsigned long flags;
Michal Malý2a552c32015-04-08 22:56:39 +0200490 s32 *value = report->field[0]->value;
Michal Malý74479ba2012-09-24 01:09:30 +0200491
Michal Malýc918fe72015-04-08 22:56:48 +0200492 drv_data = hid_get_drvdata(hid);
493 if (!drv_data) {
494 hid_err(hid, "Private driver data not found!\n");
495 return;
496 }
497
498 entry = drv_data->device_props;
499 if (!entry) {
500 hid_err(hid, "Device properties not found!\n");
501 return;
502 }
503
Michal Malý30bb75d2011-08-04 16:20:40 +0200504 dbg_hid("G25/G27/DFGT: setting range to %u\n", range);
505
Michal Malýc918fe72015-04-08 22:56:48 +0200506 spin_lock_irqsave(&entry->report_lock, flags);
Michal Malý74479ba2012-09-24 01:09:30 +0200507 value[0] = 0xf8;
508 value[1] = 0x81;
509 value[2] = range & 0x00ff;
510 value[3] = (range & 0xff00) >> 8;
511 value[4] = 0x00;
512 value[5] = 0x00;
513 value[6] = 0x00;
Michal Malý30bb75d2011-08-04 16:20:40 +0200514
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100515 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malýc918fe72015-04-08 22:56:48 +0200516 spin_unlock_irqrestore(&entry->report_lock, flags);
Michal Malý30bb75d2011-08-04 16:20:40 +0200517}
518
519/* Sends commands to set range compatible with Driving Force Pro wheel */
Michal Malýd0afd842015-04-08 22:56:40 +0200520static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range)
Michal Malý30bb75d2011-08-04 16:20:40 +0200521{
522 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
523 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
524 int start_left, start_right, full_range;
Michal Malýc918fe72015-04-08 22:56:48 +0200525 struct lg4ff_device_entry *entry;
526 struct lg_drv_data *drv_data;
527 unsigned long flags;
Michal Malý2a552c32015-04-08 22:56:39 +0200528 s32 *value = report->field[0]->value;
Michal Malý74479ba2012-09-24 01:09:30 +0200529
Michal Malýc918fe72015-04-08 22:56:48 +0200530 drv_data = hid_get_drvdata(hid);
531 if (!drv_data) {
532 hid_err(hid, "Private driver data not found!\n");
533 return;
534 }
535
536 entry = drv_data->device_props;
537 if (!entry) {
538 hid_err(hid, "Device properties not found!\n");
539 return;
540 }
541
Michal Malý30bb75d2011-08-04 16:20:40 +0200542 dbg_hid("Driving Force Pro: setting range to %u\n", range);
543
544 /* Prepare "coarse" limit command */
Michal Malýc918fe72015-04-08 22:56:48 +0200545 spin_lock_irqsave(&entry->report_lock, flags);
Michal Malý74479ba2012-09-24 01:09:30 +0200546 value[0] = 0xf8;
Michal Malýa80fe5d2012-09-24 01:13:17 +0200547 value[1] = 0x00; /* Set later */
Michal Malý74479ba2012-09-24 01:09:30 +0200548 value[2] = 0x00;
549 value[3] = 0x00;
550 value[4] = 0x00;
551 value[5] = 0x00;
552 value[6] = 0x00;
Michal Malý30bb75d2011-08-04 16:20:40 +0200553
554 if (range > 200) {
555 report->field[0]->value[1] = 0x03;
556 full_range = 900;
557 } else {
558 report->field[0]->value[1] = 0x02;
559 full_range = 200;
560 }
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100561 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malý30bb75d2011-08-04 16:20:40 +0200562
563 /* Prepare "fine" limit command */
Michal Malý74479ba2012-09-24 01:09:30 +0200564 value[0] = 0x81;
565 value[1] = 0x0b;
566 value[2] = 0x00;
567 value[3] = 0x00;
568 value[4] = 0x00;
569 value[5] = 0x00;
570 value[6] = 0x00;
Michal Malý30bb75d2011-08-04 16:20:40 +0200571
572 if (range == 200 || range == 900) { /* Do not apply any fine limit */
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100573 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malýc918fe72015-04-08 22:56:48 +0200574 spin_unlock_irqrestore(&entry->report_lock, flags);
Michal Malý30bb75d2011-08-04 16:20:40 +0200575 return;
576 }
577
578 /* Construct fine limit command */
579 start_left = (((full_range - range + 1) * 2047) / full_range);
580 start_right = 0xfff - start_left;
581
Michal Malý74479ba2012-09-24 01:09:30 +0200582 value[2] = start_left >> 4;
583 value[3] = start_right >> 4;
584 value[4] = 0xff;
585 value[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
586 value[6] = 0xff;
Michal Malý30bb75d2011-08-04 16:20:40 +0200587
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100588 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malýc918fe72015-04-08 22:56:48 +0200589 spin_unlock_irqrestore(&entry->report_lock, flags);
Michal Malý30bb75d2011-08-04 16:20:40 +0200590}
591
Michal Malýf31a2de2015-02-18 17:59:23 +0100592static const struct lg4ff_compat_mode_switch *lg4ff_get_mode_switch_command(const u16 real_product_id, const u16 target_product_id)
593{
594 switch (real_product_id) {
595 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
596 switch (target_product_id) {
597 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
598 return &lg4ff_mode_switch_ext01_dfp;
599 /* DFP can only be switched to its native mode */
600 default:
601 return NULL;
602 }
603 break;
604 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
605 switch (target_product_id) {
606 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
607 return &lg4ff_mode_switch_ext01_dfp;
608 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
609 return &lg4ff_mode_switch_ext16_g25;
610 /* G25 can only be switched to DFP mode or its native mode */
611 default:
612 return NULL;
613 }
614 break;
615 case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
616 switch (target_product_id) {
617 case USB_DEVICE_ID_LOGITECH_WHEEL:
618 return &lg4ff_mode_switch_ext09_dfex;
619 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
620 return &lg4ff_mode_switch_ext09_dfp;
621 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
622 return &lg4ff_mode_switch_ext09_g25;
623 case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
624 return &lg4ff_mode_switch_ext09_g27;
625 /* G27 can only be switched to DF-EX, DFP, G25 or its native mode */
626 default:
627 return NULL;
628 }
629 break;
630 case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
631 switch (target_product_id) {
632 case USB_DEVICE_ID_LOGITECH_WHEEL:
633 return &lg4ff_mode_switch_ext09_dfex;
634 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
635 return &lg4ff_mode_switch_ext09_dfp;
636 case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
637 return &lg4ff_mode_switch_ext09_dfgt;
638 /* DFGT can only be switched to DF-EX, DFP or its native mode */
639 default:
640 return NULL;
641 }
642 break;
643 /* No other wheels have multiple modes */
644 default:
645 return NULL;
646 }
647}
648
Michal Malýe7c23442015-02-18 17:59:20 +0100649static int lg4ff_switch_compatibility_mode(struct hid_device *hid, const struct lg4ff_compat_mode_switch *s)
Michal Malý96440c82011-08-04 16:18:11 +0200650{
Michal Malýc1740d12015-02-18 22:49:33 +0100651 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
652 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malýc918fe72015-04-08 22:56:48 +0200653 struct lg4ff_device_entry *entry;
654 struct lg_drv_data *drv_data;
655 unsigned long flags;
Michal Malý2a552c32015-04-08 22:56:39 +0200656 s32 *value = report->field[0]->value;
Michal Malýe7c23442015-02-18 17:59:20 +0100657 u8 i;
Michal Malý96440c82011-08-04 16:18:11 +0200658
Michal Malýc918fe72015-04-08 22:56:48 +0200659 drv_data = hid_get_drvdata(hid);
660 if (!drv_data) {
661 hid_err(hid, "Private driver data not found!\n");
662 return -EINVAL;
663 }
664
665 entry = drv_data->device_props;
666 if (!entry) {
667 hid_err(hid, "Device properties not found!\n");
668 return -EINVAL;
669 }
670
671 spin_lock_irqsave(&entry->report_lock, flags);
Michal Malýe7c23442015-02-18 17:59:20 +0100672 for (i = 0; i < s->cmd_count; i++) {
Michal Malýc1740d12015-02-18 22:49:33 +0100673 u8 j;
Michal Malý96440c82011-08-04 16:18:11 +0200674
Michal Malýc1740d12015-02-18 22:49:33 +0100675 for (j = 0; j < 7; j++)
676 value[j] = s->cmd[j + (7*i)];
677
678 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malý96440c82011-08-04 16:18:11 +0200679 }
Michal Malýc918fe72015-04-08 22:56:48 +0200680 spin_unlock_irqrestore(&entry->report_lock, flags);
Michal Malýc1740d12015-02-18 22:49:33 +0100681 hid_hw_wait(hid);
Michal Malýe7c23442015-02-18 17:59:20 +0100682 return 0;
Michal Malý96440c82011-08-04 16:18:11 +0200683}
Simon Wood32c88cb2010-09-22 13:19:42 +0200684
Michal Malýb96d23e2015-02-18 17:59:21 +0100685static ssize_t lg4ff_alternate_modes_show(struct device *dev, struct device_attribute *attr, char *buf)
686{
687 struct hid_device *hid = to_hid_device(dev);
688 struct lg4ff_device_entry *entry;
689 struct lg_drv_data *drv_data;
690 ssize_t count = 0;
691 int i;
692
693 drv_data = hid_get_drvdata(hid);
694 if (!drv_data) {
695 hid_err(hid, "Private driver data not found!\n");
696 return 0;
697 }
698
699 entry = drv_data->device_props;
700 if (!entry) {
701 hid_err(hid, "Device properties not found!\n");
702 return 0;
703 }
704
Michal Malý72529c62015-04-08 22:56:46 +0200705 if (!entry->wdata.real_name) {
Michal Malýb96d23e2015-02-18 17:59:21 +0100706 hid_err(hid, "NULL pointer to string\n");
707 return 0;
708 }
709
710 for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
Michal Malý72529c62015-04-08 22:56:46 +0200711 if (entry->wdata.alternate_modes & BIT(i)) {
Michal Malýb96d23e2015-02-18 17:59:21 +0100712 /* Print tag and full name */
713 count += scnprintf(buf + count, PAGE_SIZE - count, "%s: %s",
714 lg4ff_alternate_modes[i].tag,
Michal Malý72529c62015-04-08 22:56:46 +0200715 !lg4ff_alternate_modes[i].product_id ? entry->wdata.real_name : lg4ff_alternate_modes[i].name);
Michal Malýb96d23e2015-02-18 17:59:21 +0100716 if (count >= PAGE_SIZE - 1)
717 return count;
718
719 /* Mark the currently active mode with an asterisk */
Michal Malý72529c62015-04-08 22:56:46 +0200720 if (lg4ff_alternate_modes[i].product_id == entry->wdata.product_id ||
721 (lg4ff_alternate_modes[i].product_id == 0 && entry->wdata.product_id == entry->wdata.real_product_id))
Michal Malýb96d23e2015-02-18 17:59:21 +0100722 count += scnprintf(buf + count, PAGE_SIZE - count, " *\n");
723 else
724 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
725
726 if (count >= PAGE_SIZE - 1)
727 return count;
728 }
729 }
730
731 return count;
732}
733
734static ssize_t lg4ff_alternate_modes_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
735{
Michal Malýf31a2de2015-02-18 17:59:23 +0100736 struct hid_device *hid = to_hid_device(dev);
737 struct lg4ff_device_entry *entry;
738 struct lg_drv_data *drv_data;
739 const struct lg4ff_compat_mode_switch *s;
740 u16 target_product_id = 0;
741 int i, ret;
742 char *lbuf;
743
744 drv_data = hid_get_drvdata(hid);
745 if (!drv_data) {
746 hid_err(hid, "Private driver data not found!\n");
747 return -EINVAL;
748 }
749
750 entry = drv_data->device_props;
751 if (!entry) {
752 hid_err(hid, "Device properties not found!\n");
753 return -EINVAL;
754 }
755
756 /* Allow \n at the end of the input parameter */
757 lbuf = kasprintf(GFP_KERNEL, "%s", buf);
758 if (!lbuf)
759 return -ENOMEM;
760
761 i = strlen(lbuf);
762 if (lbuf[i-1] == '\n') {
763 if (i == 1) {
764 kfree(lbuf);
765 return -EINVAL;
766 }
767 lbuf[i-1] = '\0';
768 }
769
770 for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
771 const u16 mode_product_id = lg4ff_alternate_modes[i].product_id;
772 const char *tag = lg4ff_alternate_modes[i].tag;
773
Michal Malý72529c62015-04-08 22:56:46 +0200774 if (entry->wdata.alternate_modes & BIT(i)) {
Michal Malýf31a2de2015-02-18 17:59:23 +0100775 if (!strcmp(tag, lbuf)) {
776 if (!mode_product_id)
Michal Malý72529c62015-04-08 22:56:46 +0200777 target_product_id = entry->wdata.real_product_id;
Michal Malýf31a2de2015-02-18 17:59:23 +0100778 else
779 target_product_id = mode_product_id;
780 break;
781 }
782 }
783 }
784
785 if (i == LG4FF_MODE_MAX_IDX) {
786 hid_info(hid, "Requested mode \"%s\" is not supported by the device\n", lbuf);
787 kfree(lbuf);
788 return -EINVAL;
789 }
790 kfree(lbuf); /* Not needed anymore */
791
Michal Malý72529c62015-04-08 22:56:46 +0200792 if (target_product_id == entry->wdata.product_id) /* Nothing to do */
Michal Malýf31a2de2015-02-18 17:59:23 +0100793 return count;
794
795 /* Automatic switching has to be disabled for the switch to DF-EX mode to work correctly */
796 if (target_product_id == USB_DEVICE_ID_LOGITECH_WHEEL && !lg4ff_no_autoswitch) {
797 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 +0200798 entry->wdata.real_name);
Michal Malýf31a2de2015-02-18 17:59:23 +0100799 return -EINVAL;
800 }
801
802 /* Take care of hardware limitations */
Michal Malý72529c62015-04-08 22:56:46 +0200803 if ((entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_DFP_WHEEL || entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_G25_WHEEL) &&
804 entry->wdata.product_id > target_product_id) {
805 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 +0100806 return -EINVAL;
807 }
808
Michal Malý72529c62015-04-08 22:56:46 +0200809 s = lg4ff_get_mode_switch_command(entry->wdata.real_product_id, target_product_id);
Michal Malýf31a2de2015-02-18 17:59:23 +0100810 if (!s) {
811 hid_err(hid, "Invalid target product ID %X\n", target_product_id);
812 return -EINVAL;
813 }
814
815 ret = lg4ff_switch_compatibility_mode(hid, s);
816 return (ret == 0 ? count : ret);
Michal Malýb96d23e2015-02-18 17:59:21 +0100817}
818static DEVICE_ATTR(alternate_modes, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_alternate_modes_show, lg4ff_alternate_modes_store);
819
Michal Malýfbf85e22015-04-08 22:56:41 +0200820/* Export the currently set range of the wheel */
821static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr,
822 char *buf)
Michal Malý30bb75d2011-08-04 16:20:40 +0200823{
Michal Malý30bb75d2011-08-04 16:20:40 +0200824 struct hid_device *hid = to_hid_device(dev);
Michal Malý3b6b17b2012-04-09 09:08:49 +0200825 struct lg4ff_device_entry *entry;
826 struct lg_drv_data *drv_data;
Michal Malý30bb75d2011-08-04 16:20:40 +0200827 size_t count;
828
Michal Malý3b6b17b2012-04-09 09:08:49 +0200829 drv_data = hid_get_drvdata(hid);
830 if (!drv_data) {
831 hid_err(hid, "Private driver data not found!\n");
832 return 0;
Michal Malý30bb75d2011-08-04 16:20:40 +0200833 }
Michal Malý3b6b17b2012-04-09 09:08:49 +0200834
835 entry = drv_data->device_props;
836 if (!entry) {
837 hid_err(hid, "Device properties not found!\n");
Michal Malý30bb75d2011-08-04 16:20:40 +0200838 return 0;
839 }
840
Michal Malý72529c62015-04-08 22:56:46 +0200841 count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->wdata.range);
Michal Malý30bb75d2011-08-04 16:20:40 +0200842 return count;
843}
844
845/* Set range to user specified value, call appropriate function
846 * according to the type of the wheel */
Michal Malýfbf85e22015-04-08 22:56:41 +0200847static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr,
848 const char *buf, size_t count)
Michal Malý30bb75d2011-08-04 16:20:40 +0200849{
Michal Malý30bb75d2011-08-04 16:20:40 +0200850 struct hid_device *hid = to_hid_device(dev);
Michal Malý3b6b17b2012-04-09 09:08:49 +0200851 struct lg4ff_device_entry *entry;
852 struct lg_drv_data *drv_data;
Michal Malý2a552c32015-04-08 22:56:39 +0200853 u16 range = simple_strtoul(buf, NULL, 10);
Michal Malý30bb75d2011-08-04 16:20:40 +0200854
Michal Malý3b6b17b2012-04-09 09:08:49 +0200855 drv_data = hid_get_drvdata(hid);
856 if (!drv_data) {
857 hid_err(hid, "Private driver data not found!\n");
Simon Wood29ff6652014-08-14 20:43:01 -0600858 return -EINVAL;
Michal Malý30bb75d2011-08-04 16:20:40 +0200859 }
Michal Malý3b6b17b2012-04-09 09:08:49 +0200860
861 entry = drv_data->device_props;
862 if (!entry) {
863 hid_err(hid, "Device properties not found!\n");
Simon Wood29ff6652014-08-14 20:43:01 -0600864 return -EINVAL;
Michal Malý30bb75d2011-08-04 16:20:40 +0200865 }
866
867 if (range == 0)
Michal Malý72529c62015-04-08 22:56:46 +0200868 range = entry->wdata.max_range;
Michal Malý30bb75d2011-08-04 16:20:40 +0200869
870 /* Check if the wheel supports range setting
871 * and that the range is within limits for the wheel */
Michal Malý72529c62015-04-08 22:56:46 +0200872 if (entry->wdata.set_range && range >= entry->wdata.min_range && range <= entry->wdata.max_range) {
873 entry->wdata.set_range(hid, range);
874 entry->wdata.range = range;
Michal Malý30bb75d2011-08-04 16:20:40 +0200875 }
876
877 return count;
878}
Michal Malýfbf85e22015-04-08 22:56:41 +0200879static 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 +0200880
Michal Malýb96d23e2015-02-18 17:59:21 +0100881static ssize_t lg4ff_real_id_show(struct device *dev, struct device_attribute *attr, char *buf)
882{
883 struct hid_device *hid = to_hid_device(dev);
884 struct lg4ff_device_entry *entry;
885 struct lg_drv_data *drv_data;
886 size_t count;
887
888 drv_data = hid_get_drvdata(hid);
889 if (!drv_data) {
890 hid_err(hid, "Private driver data not found!\n");
891 return 0;
892 }
893
894 entry = drv_data->device_props;
895 if (!entry) {
896 hid_err(hid, "Device properties not found!\n");
897 return 0;
898 }
899
Michal Malý72529c62015-04-08 22:56:46 +0200900 if (!entry->wdata.real_tag || !entry->wdata.real_name) {
Michal Malýb96d23e2015-02-18 17:59:21 +0100901 hid_err(hid, "NULL pointer to string\n");
902 return 0;
903 }
904
Michal Malý72529c62015-04-08 22:56:46 +0200905 count = scnprintf(buf, PAGE_SIZE, "%s: %s\n", entry->wdata.real_tag, entry->wdata.real_name);
Michal Malýb96d23e2015-02-18 17:59:21 +0100906 return count;
907}
908
909static ssize_t lg4ff_real_id_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
910{
911 /* Real ID is a read-only value */
912 return -EPERM;
913}
914static DEVICE_ATTR(real_id, S_IRUGO, lg4ff_real_id_show, lg4ff_real_id_store);
915
Simon Wood22bcefd2012-04-21 05:41:15 -0700916#ifdef CONFIG_LEDS_CLASS
Michal Malý2a552c32015-04-08 22:56:39 +0200917static void lg4ff_set_leds(struct hid_device *hid, u8 leds)
Simon Wood22bcefd2012-04-21 05:41:15 -0700918{
919 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
920 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malýc918fe72015-04-08 22:56:48 +0200921 struct lg_drv_data *drv_data;
922 struct lg4ff_device_entry *entry;
923 unsigned long flags;
Michal Malý2a552c32015-04-08 22:56:39 +0200924 s32 *value = report->field[0]->value;
Simon Wood22bcefd2012-04-21 05:41:15 -0700925
Michal Malýc918fe72015-04-08 22:56:48 +0200926 drv_data = hid_get_drvdata(hid);
927 if (!drv_data) {
928 hid_err(hid, "Private driver data not found!\n");
929 return;
930 }
931
932 entry = drv_data->device_props;
933 if (!entry) {
934 hid_err(hid, "Device properties not found!\n");
935 return;
936 }
937
938 spin_lock_irqsave(&entry->report_lock, flags);
Michal Malý74479ba2012-09-24 01:09:30 +0200939 value[0] = 0xf8;
940 value[1] = 0x12;
941 value[2] = leds;
942 value[3] = 0x00;
943 value[4] = 0x00;
944 value[5] = 0x00;
945 value[6] = 0x00;
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100946 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malýc918fe72015-04-08 22:56:48 +0200947 spin_unlock_irqrestore(&entry->report_lock, flags);
Simon Wood22bcefd2012-04-21 05:41:15 -0700948}
949
950static void lg4ff_led_set_brightness(struct led_classdev *led_cdev,
951 enum led_brightness value)
952{
953 struct device *dev = led_cdev->dev->parent;
954 struct hid_device *hid = container_of(dev, struct hid_device, dev);
Axel Lin4629fd12012-09-13 14:09:33 +0800955 struct lg_drv_data *drv_data = hid_get_drvdata(hid);
Simon Wood22bcefd2012-04-21 05:41:15 -0700956 struct lg4ff_device_entry *entry;
957 int i, state = 0;
958
959 if (!drv_data) {
960 hid_err(hid, "Device data not found.");
961 return;
962 }
963
Michal Malý371a1d92015-04-08 22:56:43 +0200964 entry = drv_data->device_props;
Simon Wood22bcefd2012-04-21 05:41:15 -0700965
966 if (!entry) {
967 hid_err(hid, "Device properties not found.");
968 return;
969 }
970
971 for (i = 0; i < 5; i++) {
Michal Malý72529c62015-04-08 22:56:46 +0200972 if (led_cdev != entry->wdata.led[i])
Simon Wood22bcefd2012-04-21 05:41:15 -0700973 continue;
Michal Malý72529c62015-04-08 22:56:46 +0200974 state = (entry->wdata.led_state >> i) & 1;
Simon Wood22bcefd2012-04-21 05:41:15 -0700975 if (value == LED_OFF && state) {
Michal Malý72529c62015-04-08 22:56:46 +0200976 entry->wdata.led_state &= ~(1 << i);
977 lg4ff_set_leds(hid, entry->wdata.led_state);
Simon Wood22bcefd2012-04-21 05:41:15 -0700978 } else if (value != LED_OFF && !state) {
Michal Malý72529c62015-04-08 22:56:46 +0200979 entry->wdata.led_state |= 1 << i;
980 lg4ff_set_leds(hid, entry->wdata.led_state);
Simon Wood22bcefd2012-04-21 05:41:15 -0700981 }
982 break;
983 }
984}
985
986static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev)
987{
988 struct device *dev = led_cdev->dev->parent;
989 struct hid_device *hid = container_of(dev, struct hid_device, dev);
Axel Lin4629fd12012-09-13 14:09:33 +0800990 struct lg_drv_data *drv_data = hid_get_drvdata(hid);
Simon Wood22bcefd2012-04-21 05:41:15 -0700991 struct lg4ff_device_entry *entry;
992 int i, value = 0;
993
994 if (!drv_data) {
995 hid_err(hid, "Device data not found.");
996 return LED_OFF;
997 }
998
Michal Malý371a1d92015-04-08 22:56:43 +0200999 entry = drv_data->device_props;
Simon Wood22bcefd2012-04-21 05:41:15 -07001000
1001 if (!entry) {
1002 hid_err(hid, "Device properties not found.");
1003 return LED_OFF;
1004 }
1005
1006 for (i = 0; i < 5; i++)
Michal Malý72529c62015-04-08 22:56:46 +02001007 if (led_cdev == entry->wdata.led[i]) {
1008 value = (entry->wdata.led_state >> i) & 1;
Simon Wood22bcefd2012-04-21 05:41:15 -07001009 break;
1010 }
1011
1012 return value ? LED_FULL : LED_OFF;
1013}
1014#endif
1015
Michal Malýe7c23442015-02-18 17:59:20 +01001016static u16 lg4ff_identify_multimode_wheel(struct hid_device *hid, const u16 reported_product_id, const u16 bcdDevice)
1017{
1018 const struct lg4ff_wheel_ident_checklist *checklist;
1019 int i, from_idx, to_idx;
1020
1021 switch (reported_product_id) {
1022 case USB_DEVICE_ID_LOGITECH_WHEEL:
1023 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
1024 checklist = &lg4ff_main_checklist;
1025 from_idx = 0;
1026 to_idx = checklist->count - 1;
1027 break;
1028 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
1029 checklist = &lg4ff_main_checklist;
1030 from_idx = 0;
1031 to_idx = checklist->count - 2; /* End identity check at G25 */
1032 break;
1033 case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
1034 checklist = &lg4ff_main_checklist;
1035 from_idx = 1; /* Start identity check at G27 */
1036 to_idx = checklist->count - 3; /* End identity check at G27 */
1037 break;
1038 case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
1039 checklist = &lg4ff_main_checklist;
1040 from_idx = 0;
1041 to_idx = checklist->count - 4; /* End identity check at DFGT */
1042 break;
1043 default:
1044 return 0;
1045 }
1046
1047 for (i = from_idx; i <= to_idx; i++) {
1048 const u16 mask = checklist->models[i]->mask;
1049 const u16 result = checklist->models[i]->result;
1050 const u16 real_product_id = checklist->models[i]->real_product_id;
1051
1052 if ((bcdDevice & mask) == result) {
1053 dbg_hid("Found wheel with real PID %X whose reported PID is %X\n", real_product_id, reported_product_id);
1054 return real_product_id;
1055 }
1056 }
1057
Michal Malýf31a2de2015-02-18 17:59:23 +01001058 /* No match found. This is either Driving Force or an unknown
1059 * wheel model, do not touch it */
Michal Malýe7c23442015-02-18 17:59:20 +01001060 dbg_hid("Wheel with bcdDevice %X was not recognized as multimode wheel, leaving in its current mode\n", bcdDevice);
1061 return 0;
1062}
1063
1064static int lg4ff_handle_multimode_wheel(struct hid_device *hid, u16 *real_product_id, const u16 bcdDevice)
1065{
1066 const u16 reported_product_id = hid->product;
1067 int ret;
1068
1069 *real_product_id = lg4ff_identify_multimode_wheel(hid, reported_product_id, bcdDevice);
1070 /* Probed wheel is not a multimode wheel */
1071 if (!*real_product_id) {
1072 *real_product_id = reported_product_id;
1073 dbg_hid("Wheel is not a multimode wheel\n");
1074 return LG4FF_MMODE_NOT_MULTIMODE;
1075 }
1076
1077 /* Switch from "Driving Force" mode to native mode automatically.
1078 * Otherwise keep the wheel in its current mode */
1079 if (reported_product_id == USB_DEVICE_ID_LOGITECH_WHEEL &&
Michal Malýa54dc7792015-02-18 17:59:22 +01001080 reported_product_id != *real_product_id &&
1081 !lg4ff_no_autoswitch) {
Michal Malýf31a2de2015-02-18 17:59:23 +01001082 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 +01001083
Michal Malýf31a2de2015-02-18 17:59:23 +01001084 if (!s) {
Michal Malýe7c23442015-02-18 17:59:20 +01001085 hid_err(hid, "Invalid product id %X\n", *real_product_id);
Michal Malýb96d23e2015-02-18 17:59:21 +01001086 return LG4FF_MMODE_NOT_MULTIMODE;
Michal Malýe7c23442015-02-18 17:59:20 +01001087 }
1088
1089 ret = lg4ff_switch_compatibility_mode(hid, s);
1090 if (ret) {
1091 /* Wheel could not have been switched to native mode,
1092 * leave it in "Driving Force" mode and continue */
1093 hid_err(hid, "Unable to switch wheel mode, errno %d\n", ret);
Michal Malýb96d23e2015-02-18 17:59:21 +01001094 return LG4FF_MMODE_IS_MULTIMODE;
Michal Malýe7c23442015-02-18 17:59:20 +01001095 }
1096 return LG4FF_MMODE_SWITCHED;
1097 }
1098
Michal Malýb96d23e2015-02-18 17:59:21 +01001099 return LG4FF_MMODE_IS_MULTIMODE;
Michal Malýe7c23442015-02-18 17:59:20 +01001100}
1101
1102
Simon Wood32c88cb2010-09-22 13:19:42 +02001103int lg4ff_init(struct hid_device *hid)
1104{
1105 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
Simon Wood32c88cb2010-09-22 13:19:42 +02001106 struct input_dev *dev = hidinput->input;
Michal Malýe7c23442015-02-18 17:59:20 +01001107 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
1108 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
Michal Malý30bb75d2011-08-04 16:20:40 +02001109 struct lg4ff_device_entry *entry;
Michal Malý3b6b17b2012-04-09 09:08:49 +02001110 struct lg_drv_data *drv_data;
Michal Malýb96d23e2015-02-18 17:59:21 +01001111 int error, i, j;
1112 int mmode_ret, mmode_idx = -1;
Michal Malýe7c23442015-02-18 17:59:20 +01001113 u16 real_product_id;
Simon Wood32c88cb2010-09-22 13:19:42 +02001114
Simon Wood32c88cb2010-09-22 13:19:42 +02001115 /* Check that the report looks ok */
Kees Cook0fb6bd02013-09-11 21:56:54 +02001116 if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
Simon Wood32c88cb2010-09-22 13:19:42 +02001117 return -1;
Michal Malý30bb75d2011-08-04 16:20:40 +02001118
Michal Malý72529c62015-04-08 22:56:46 +02001119 drv_data = hid_get_drvdata(hid);
1120 if (!drv_data) {
1121 hid_err(hid, "Cannot add device, private driver data not allocated\n");
1122 return -1;
1123 }
1124 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1125 if (!entry)
1126 return -ENOMEM;
Michal Malýc918fe72015-04-08 22:56:48 +02001127 spin_lock_init(&entry->report_lock);
Michal Malý72529c62015-04-08 22:56:46 +02001128 drv_data->device_props = entry;
1129
Michal Malýe7c23442015-02-18 17:59:20 +01001130 /* Check if a multimode wheel has been connected and
1131 * handle it appropriately */
Michal Malýb96d23e2015-02-18 17:59:21 +01001132 mmode_ret = lg4ff_handle_multimode_wheel(hid, &real_product_id, bcdDevice);
Michal Malýe7c23442015-02-18 17:59:20 +01001133
1134 /* Wheel has been told to switch to native mode. There is no point in going on
1135 * with the initialization as the wheel will do a USB reset when it switches mode
1136 */
Michal Malýb96d23e2015-02-18 17:59:21 +01001137 if (mmode_ret == LG4FF_MMODE_SWITCHED)
Michal Malýe7c23442015-02-18 17:59:20 +01001138 return 0;
Michal Malý72529c62015-04-08 22:56:46 +02001139 else if (mmode_ret < 0) {
1140 hid_err(hid, "Unable to switch device mode during initialization, errno %d\n", mmode_ret);
1141 error = mmode_ret;
1142 goto err_init;
1143 }
Michal Malýe7c23442015-02-18 17:59:20 +01001144
Michal Malý7362cd22011-08-04 16:16:09 +02001145 /* Check what wheel has been connected */
1146 for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
1147 if (hid->product == lg4ff_devices[i].product_id) {
1148 dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
1149 break;
1150 }
1151 }
Simon Wood32c88cb2010-09-22 13:19:42 +02001152
Michal Malý7362cd22011-08-04 16:16:09 +02001153 if (i == ARRAY_SIZE(lg4ff_devices)) {
Michal Malý9c2a6bd2015-04-08 22:56:44 +02001154 hid_err(hid, "This device is flagged to be handled by the lg4ff module but this module does not know how to handle it. "
1155 "Please report this as a bug to LKML, Simon Wood <simon@mungewell.org> or "
1156 "Michal Maly <madcatxster@devoid-pointer.net>\n");
Michal Malý72529c62015-04-08 22:56:46 +02001157 error = -1;
1158 goto err_init;
Michal Malý7362cd22011-08-04 16:16:09 +02001159 }
1160
Michal Malýb96d23e2015-02-18 17:59:21 +01001161 if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1162 for (mmode_idx = 0; mmode_idx < ARRAY_SIZE(lg4ff_multimode_wheels); mmode_idx++) {
1163 if (real_product_id == lg4ff_multimode_wheels[mmode_idx].product_id)
1164 break;
1165 }
1166
1167 if (mmode_idx == ARRAY_SIZE(lg4ff_multimode_wheels)) {
1168 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 +02001169 error = -1;
1170 goto err_init;
Michal Malýb96d23e2015-02-18 17:59:21 +01001171 }
1172 }
1173
Michal Malý7362cd22011-08-04 16:16:09 +02001174 /* Set supported force feedback capabilities */
1175 for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
1176 set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
Simon Wood32c88cb2010-09-22 13:19:42 +02001177
Michal Malýd0afd842015-04-08 22:56:40 +02001178 error = input_ff_create_memless(dev, NULL, lg4ff_play);
Simon Wood32c88cb2010-09-22 13:19:42 +02001179
1180 if (error)
Michal Malý72529c62015-04-08 22:56:46 +02001181 goto err_init;
Simon Wood32c88cb2010-09-22 13:19:42 +02001182
Michal Malý72529c62015-04-08 22:56:46 +02001183 entry->wdata.product_id = lg4ff_devices[i].product_id;
1184 entry->wdata.real_product_id = real_product_id;
1185 entry->wdata.min_range = lg4ff_devices[i].min_range;
1186 entry->wdata.max_range = lg4ff_devices[i].max_range;
1187 entry->wdata.set_range = lg4ff_devices[i].set_range;
Michal Malýb96d23e2015-02-18 17:59:21 +01001188 if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1189 BUG_ON(mmode_idx == -1);
Michal Malý72529c62015-04-08 22:56:46 +02001190 entry->wdata.alternate_modes = lg4ff_multimode_wheels[mmode_idx].alternate_modes;
1191 entry->wdata.real_tag = lg4ff_multimode_wheels[mmode_idx].real_tag;
1192 entry->wdata.real_name = lg4ff_multimode_wheels[mmode_idx].real_name;
Michal Malýb96d23e2015-02-18 17:59:21 +01001193 }
Michal Malý30bb75d2011-08-04 16:20:40 +02001194
Simon Wood114a55c2013-11-06 12:30:43 -07001195 /* Check if autocentering is available and
1196 * set the centering force to zero by default */
1197 if (test_bit(FF_AUTOCENTER, dev->ffbit)) {
Michal Malýe7c23442015-02-18 17:59:20 +01001198 /* Formula Force EX expects different autocentering command */
1199 if ((bcdDevice >> 8) == LG4FF_FFEX_REV_MAJ &&
1200 (bcdDevice & 0xff) == LG4FF_FFEX_REV_MIN)
Michal Malýd0afd842015-04-08 22:56:40 +02001201 dev->ff->set_autocenter = lg4ff_set_autocenter_ffex;
Simon Wood114a55c2013-11-06 12:30:43 -07001202 else
Michal Malýd0afd842015-04-08 22:56:40 +02001203 dev->ff->set_autocenter = lg4ff_set_autocenter_default;
Simon Wood114a55c2013-11-06 12:30:43 -07001204
1205 dev->ff->set_autocenter(dev, 0);
1206 }
1207
Michal Malý30bb75d2011-08-04 16:20:40 +02001208 /* Create sysfs interface */
1209 error = device_create_file(&hid->dev, &dev_attr_range);
1210 if (error)
Michal Malý72529c62015-04-08 22:56:46 +02001211 goto err_init;
Michal Malýb96d23e2015-02-18 17:59:21 +01001212 if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1213 error = device_create_file(&hid->dev, &dev_attr_real_id);
1214 if (error)
Michal Malý72529c62015-04-08 22:56:46 +02001215 goto err_init;
1216
Michal Malýb96d23e2015-02-18 17:59:21 +01001217 error = device_create_file(&hid->dev, &dev_attr_alternate_modes);
1218 if (error)
Michal Malý72529c62015-04-08 22:56:46 +02001219 goto err_init;
Michal Malýb96d23e2015-02-18 17:59:21 +01001220 }
Michal Malý30bb75d2011-08-04 16:20:40 +02001221 dbg_hid("sysfs interface created\n");
1222
1223 /* Set the maximum range to start with */
Michal Malý72529c62015-04-08 22:56:46 +02001224 entry->wdata.range = entry->wdata.max_range;
1225 if (entry->wdata.set_range)
1226 entry->wdata.set_range(hid, entry->wdata.range);
Michal Malý30bb75d2011-08-04 16:20:40 +02001227
Simon Wood22bcefd2012-04-21 05:41:15 -07001228#ifdef CONFIG_LEDS_CLASS
1229 /* register led subsystem - G27 only */
Michal Malý72529c62015-04-08 22:56:46 +02001230 entry->wdata.led_state = 0;
Simon Wood22bcefd2012-04-21 05:41:15 -07001231 for (j = 0; j < 5; j++)
Michal Malý72529c62015-04-08 22:56:46 +02001232 entry->wdata.led[j] = NULL;
Simon Wood22bcefd2012-04-21 05:41:15 -07001233
1234 if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL) {
1235 struct led_classdev *led;
1236 size_t name_sz;
1237 char *name;
1238
1239 lg4ff_set_leds(hid, 0);
1240
1241 name_sz = strlen(dev_name(&hid->dev)) + 8;
1242
1243 for (j = 0; j < 5; j++) {
1244 led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
1245 if (!led) {
1246 hid_err(hid, "can't allocate memory for LED %d\n", j);
Michal Malý72529c62015-04-08 22:56:46 +02001247 goto err_leds;
Simon Wood22bcefd2012-04-21 05:41:15 -07001248 }
1249
1250 name = (void *)(&led[1]);
1251 snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1);
1252 led->name = name;
1253 led->brightness = 0;
1254 led->max_brightness = 1;
1255 led->brightness_get = lg4ff_led_get_brightness;
1256 led->brightness_set = lg4ff_led_set_brightness;
1257
Michal Malý72529c62015-04-08 22:56:46 +02001258 entry->wdata.led[j] = led;
Simon Wood22bcefd2012-04-21 05:41:15 -07001259 error = led_classdev_register(&hid->dev, led);
1260
1261 if (error) {
1262 hid_err(hid, "failed to register LED %d. Aborting.\n", j);
Michal Malý72529c62015-04-08 22:56:46 +02001263err_leds:
Simon Wood22bcefd2012-04-21 05:41:15 -07001264 /* Deregister LEDs (if any) */
1265 for (j = 0; j < 5; j++) {
Michal Malý72529c62015-04-08 22:56:46 +02001266 led = entry->wdata.led[j];
1267 entry->wdata.led[j] = NULL;
Simon Wood22bcefd2012-04-21 05:41:15 -07001268 if (!led)
1269 continue;
1270 led_classdev_unregister(led);
1271 kfree(led);
1272 }
1273 goto out; /* Let the driver continue without LEDs */
1274 }
1275 }
1276 }
Simon Wood22bcefd2012-04-21 05:41:15 -07001277out:
Jiri Kosinac6e6dc82012-04-23 21:08:15 +02001278#endif
Simon Wood64013802012-04-21 05:41:16 -07001279 hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n");
Simon Wood32c88cb2010-09-22 13:19:42 +02001280 return 0;
Michal Malý72529c62015-04-08 22:56:46 +02001281
1282err_init:
1283 drv_data->device_props = NULL;
1284 kfree(entry);
1285 return error;
Simon Wood32c88cb2010-09-22 13:19:42 +02001286}
1287
Michal Malý30bb75d2011-08-04 16:20:40 +02001288int lg4ff_deinit(struct hid_device *hid)
1289{
Michal Malý30bb75d2011-08-04 16:20:40 +02001290 struct lg4ff_device_entry *entry;
Michal Malý3b6b17b2012-04-09 09:08:49 +02001291 struct lg_drv_data *drv_data;
1292
Michal Malý3b6b17b2012-04-09 09:08:49 +02001293 drv_data = hid_get_drvdata(hid);
1294 if (!drv_data) {
1295 hid_err(hid, "Error while deinitializing device, no private driver data.\n");
Michal Malý30bb75d2011-08-04 16:20:40 +02001296 return -1;
1297 }
Michal Malý3b6b17b2012-04-09 09:08:49 +02001298 entry = drv_data->device_props;
Michal Malýe7c23442015-02-18 17:59:20 +01001299 if (!entry)
1300 goto out; /* Nothing more to do */
1301
Michal Malýb96d23e2015-02-18 17:59:21 +01001302 /* Multimode devices will have at least the "MODE_NATIVE" bit set */
Michal Malý72529c62015-04-08 22:56:46 +02001303 if (entry->wdata.alternate_modes) {
Michal Malýb96d23e2015-02-18 17:59:21 +01001304 device_remove_file(&hid->dev, &dev_attr_real_id);
1305 device_remove_file(&hid->dev, &dev_attr_alternate_modes);
1306 }
1307
Michal Malý72529c62015-04-08 22:56:46 +02001308 device_remove_file(&hid->dev, &dev_attr_range);
Simon Wood22bcefd2012-04-21 05:41:15 -07001309#ifdef CONFIG_LEDS_CLASS
1310 {
1311 int j;
1312 struct led_classdev *led;
1313
1314 /* Deregister LEDs (if any) */
1315 for (j = 0; j < 5; j++) {
1316
Michal Malý72529c62015-04-08 22:56:46 +02001317 led = entry->wdata.led[j];
1318 entry->wdata.led[j] = NULL;
Simon Wood22bcefd2012-04-21 05:41:15 -07001319 if (!led)
1320 continue;
1321 led_classdev_unregister(led);
1322 kfree(led);
1323 }
1324 }
1325#endif
Michal Malýb211a632015-04-08 22:56:47 +02001326 hid_hw_stop(hid);
1327 drv_data->device_props = NULL;
Simon Wood22bcefd2012-04-21 05:41:15 -07001328
Michal Malý3b6b17b2012-04-09 09:08:49 +02001329 kfree(entry);
Michal Malýe7c23442015-02-18 17:59:20 +01001330out:
Michal Malý30bb75d2011-08-04 16:20:40 +02001331 dbg_hid("Device successfully unregistered\n");
1332 return 0;
1333}