blob: dd307724965f6d9c7b2ebf1eed0f5d808ba29c06 [file] [log] [blame]
Simon Wood32c88cb2010-09-22 13:19:42 +02001/*
Simon Wood64013802012-04-21 05:41:16 -07002 * Force feedback support for Logitech Gaming Wheels
Simon Wood32c88cb2010-09-22 13:19:42 +02003 *
Simon Wood64013802012-04-21 05:41:16 -07004 * Including G27, G25, DFP, DFGT, FFEX, Momo, Momo2 &
5 * Speed Force Wireless (WiiWheel)
Simon Wood32c88cb2010-09-22 13:19:42 +02006 *
7 * Copyright (c) 2010 Simon Wood <simon@mungewell.org>
8 */
9
10/*
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26
27#include <linux/input.h>
28#include <linux/usb.h>
29#include <linux/hid.h>
30
31#include "usbhid/usbhid.h"
32#include "hid-lg.h"
Michal Malýa54dc7792015-02-18 17:59:22 +010033#include "hid-lg4ff.h"
Michal Malý7362cd22011-08-04 16:16:09 +020034#include "hid-ids.h"
Simon Wood32c88cb2010-09-22 13:19:42 +020035
Michal Malý30bb75d2011-08-04 16:20:40 +020036#define to_hid_device(pdev) container_of(pdev, struct hid_device, dev)
37
Michal Malýb96d23e2015-02-18 17:59:21 +010038#define LG4FF_MMODE_IS_MULTIMODE 0
Michal Malýe7c23442015-02-18 17:59:20 +010039#define LG4FF_MMODE_SWITCHED 1
40#define LG4FF_MMODE_NOT_MULTIMODE 2
41
Michal Malýb96d23e2015-02-18 17:59:21 +010042#define LG4FF_MODE_NATIVE_IDX 0
43#define LG4FF_MODE_DFEX_IDX 1
44#define LG4FF_MODE_DFP_IDX 2
45#define LG4FF_MODE_G25_IDX 3
46#define LG4FF_MODE_DFGT_IDX 4
47#define LG4FF_MODE_G27_IDX 5
48#define LG4FF_MODE_MAX_IDX 6
49
50#define LG4FF_MODE_NATIVE BIT(LG4FF_MODE_NATIVE_IDX)
51#define LG4FF_MODE_DFEX BIT(LG4FF_MODE_DFEX_IDX)
52#define LG4FF_MODE_DFP BIT(LG4FF_MODE_DFP_IDX)
53#define LG4FF_MODE_G25 BIT(LG4FF_MODE_G25_IDX)
54#define LG4FF_MODE_DFGT BIT(LG4FF_MODE_DFGT_IDX)
55#define LG4FF_MODE_G27 BIT(LG4FF_MODE_G27_IDX)
56
57#define LG4FF_DFEX_TAG "DF-EX"
58#define LG4FF_DFEX_NAME "Driving Force / Formula EX"
59#define LG4FF_DFP_TAG "DFP"
60#define LG4FF_DFP_NAME "Driving Force Pro"
61#define LG4FF_G25_TAG "G25"
62#define LG4FF_G25_NAME "G25 Racing Wheel"
63#define LG4FF_G27_TAG "G27"
64#define LG4FF_G27_NAME "G27 Racing Wheel"
65#define LG4FF_DFGT_TAG "DFGT"
66#define LG4FF_DFGT_NAME "Driving Force GT"
67
Michal Malýe7c23442015-02-18 17:59:20 +010068#define LG4FF_FFEX_REV_MAJ 0x21
69#define LG4FF_FFEX_REV_MIN 0x00
70
Michal Malý30bb75d2011-08-04 16:20:40 +020071static void hid_lg4ff_set_range_dfp(struct hid_device *hid, u16 range);
72static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range);
Michal Malý30bb75d2011-08-04 16:20:40 +020073
Michal Malý30bb75d2011-08-04 16:20:40 +020074struct lg4ff_device_entry {
Michal Malý2b24a962012-09-23 22:41:08 +020075 __u32 product_id;
Michal Malý30bb75d2011-08-04 16:20:40 +020076 __u16 range;
77 __u16 min_range;
78 __u16 max_range;
Simon Wood22bcefd2012-04-21 05:41:15 -070079#ifdef CONFIG_LEDS_CLASS
80 __u8 led_state;
81 struct led_classdev *led[5];
82#endif
Michal Malýb96d23e2015-02-18 17:59:21 +010083 u32 alternate_modes;
84 const char *real_tag;
85 const char *real_name;
86 u16 real_product_id;
Michal Malý30bb75d2011-08-04 16:20:40 +020087 struct list_head list;
88 void (*set_range)(struct hid_device *hid, u16 range);
89};
90
Michal Malý7362cd22011-08-04 16:16:09 +020091static const signed short lg4ff_wheel_effects[] = {
Simon Wood32c88cb2010-09-22 13:19:42 +020092 FF_CONSTANT,
93 FF_AUTOCENTER,
94 -1
95};
96
Michal Malý7362cd22011-08-04 16:16:09 +020097struct lg4ff_wheel {
98 const __u32 product_id;
99 const signed short *ff_effects;
100 const __u16 min_range;
101 const __u16 max_range;
Michal Malý30bb75d2011-08-04 16:20:40 +0200102 void (*set_range)(struct hid_device *hid, u16 range);
Michal Malý7362cd22011-08-04 16:16:09 +0200103};
104
Michal Malýe7c23442015-02-18 17:59:20 +0100105struct lg4ff_compat_mode_switch {
106 const __u8 cmd_count; /* Number of commands to send */
107 const __u8 cmd[];
108};
109
110struct lg4ff_wheel_ident_info {
111 const u16 mask;
112 const u16 result;
113 const u16 real_product_id;
114};
115
116struct lg4ff_wheel_ident_checklist {
117 const u32 count;
118 const struct lg4ff_wheel_ident_info *models[];
119};
120
Michal Malýb96d23e2015-02-18 17:59:21 +0100121struct lg4ff_multimode_wheel {
122 const u16 product_id;
123 const u32 alternate_modes;
124 const char *real_tag;
125 const char *real_name;
126};
127
128struct lg4ff_alternate_mode {
129 const u16 product_id;
130 const char *tag;
131 const char *name;
132};
133
Michal Malý7362cd22011-08-04 16:16:09 +0200134static const struct lg4ff_wheel lg4ff_devices[] = {
Michal Malý30bb75d2011-08-04 16:20:40 +0200135 {USB_DEVICE_ID_LOGITECH_WHEEL, lg4ff_wheel_effects, 40, 270, NULL},
136 {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL, lg4ff_wheel_effects, 40, 270, NULL},
137 {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_dfp},
138 {USB_DEVICE_ID_LOGITECH_G25_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
139 {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
140 {USB_DEVICE_ID_LOGITECH_G27_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
141 {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL},
142 {USB_DEVICE_ID_LOGITECH_WII_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}
Michal Malý7362cd22011-08-04 16:16:09 +0200143};
144
Michal Malýb96d23e2015-02-18 17:59:21 +0100145static const struct lg4ff_multimode_wheel lg4ff_multimode_wheels[] = {
146 {USB_DEVICE_ID_LOGITECH_DFP_WHEEL,
147 LG4FF_MODE_NATIVE | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
148 LG4FF_DFP_TAG, LG4FF_DFP_NAME},
149 {USB_DEVICE_ID_LOGITECH_G25_WHEEL,
150 LG4FF_MODE_NATIVE | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
151 LG4FF_G25_TAG, LG4FF_G25_NAME},
152 {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL,
153 LG4FF_MODE_NATIVE | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
154 LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
155 {USB_DEVICE_ID_LOGITECH_G27_WHEEL,
156 LG4FF_MODE_NATIVE | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
157 LG4FF_G27_TAG, LG4FF_G27_NAME},
158};
159
160static const struct lg4ff_alternate_mode lg4ff_alternate_modes[] = {
161 [LG4FF_MODE_NATIVE_IDX] = {0, "native", ""},
162 [LG4FF_MODE_DFEX_IDX] = {USB_DEVICE_ID_LOGITECH_WHEEL, LG4FF_DFEX_TAG, LG4FF_DFEX_NAME},
163 [LG4FF_MODE_DFP_IDX] = {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, LG4FF_DFP_TAG, LG4FF_DFP_NAME},
164 [LG4FF_MODE_G25_IDX] = {USB_DEVICE_ID_LOGITECH_G25_WHEEL, LG4FF_G25_TAG, LG4FF_G25_NAME},
165 [LG4FF_MODE_DFGT_IDX] = {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
166 [LG4FF_MODE_G27_IDX] = {USB_DEVICE_ID_LOGITECH_G27_WHEEL, LG4FF_G27_TAG, LG4FF_G27_NAME}
167};
168
Michal Malýe7c23442015-02-18 17:59:20 +0100169/* Multimode wheel identificators */
170static const struct lg4ff_wheel_ident_info lg4ff_dfp_ident_info = {
171 0xf000,
172 0x1000,
173 USB_DEVICE_ID_LOGITECH_DFP_WHEEL
Michal Malý96440c82011-08-04 16:18:11 +0200174};
175
Michal Malýe7c23442015-02-18 17:59:20 +0100176static const struct lg4ff_wheel_ident_info lg4ff_g25_ident_info = {
177 0xff00,
178 0x1200,
179 USB_DEVICE_ID_LOGITECH_G25_WHEEL
Michal Malý96440c82011-08-04 16:18:11 +0200180};
181
Michal Malýe7c23442015-02-18 17:59:20 +0100182static const struct lg4ff_wheel_ident_info lg4ff_g27_ident_info = {
183 0xfff0,
184 0x1230,
185 USB_DEVICE_ID_LOGITECH_G27_WHEEL
186};
187
188static const struct lg4ff_wheel_ident_info lg4ff_dfgt_ident_info = {
189 0xff00,
190 0x1300,
191 USB_DEVICE_ID_LOGITECH_DFGT_WHEEL
192};
193
194/* Multimode wheel identification checklists */
195static const struct lg4ff_wheel_ident_checklist lg4ff_main_checklist = {
196 4,
197 {&lg4ff_dfgt_ident_info,
198 &lg4ff_g27_ident_info,
199 &lg4ff_g25_ident_info,
200 &lg4ff_dfp_ident_info}
201};
202
203/* Compatibility mode switching commands */
204static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_dfp = {
Michal Malý96440c82011-08-04 16:18:11 +0200205 1,
206 {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
207};
208
Michal Malýe7c23442015-02-18 17:59:20 +0100209static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_dfgt = {
Michal Malý96440c82011-08-04 16:18:11 +0200210 2,
211 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */
212 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* 2nd command */
213};
214
Michal Malýe7c23442015-02-18 17:59:20 +0100215static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_g25 = {
Michal Malý96440c82011-08-04 16:18:11 +0200216 1,
217 {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
218};
219
Michal Malýe7c23442015-02-18 17:59:20 +0100220static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_g27 = {
Michal Malý96440c82011-08-04 16:18:11 +0200221 2,
222 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */
223 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* 2nd command */
224};
225
Michal Malý2b24a962012-09-23 22:41:08 +0200226/* Recalculates X axis value accordingly to currently selected range */
227static __s32 lg4ff_adjust_dfp_x_axis(__s32 value, __u16 range)
228{
229 __u16 max_range;
230 __s32 new_value;
231
232 if (range == 900)
233 return value;
234 else if (range == 200)
235 return value;
236 else if (range < 200)
237 max_range = 200;
238 else
239 max_range = 900;
240
241 new_value = 8192 + mult_frac(value - 8192, max_range, range);
242 if (new_value < 0)
243 return 0;
244 else if (new_value > 16383)
245 return 16383;
246 else
247 return new_value;
248}
249
250int lg4ff_adjust_input_event(struct hid_device *hid, struct hid_field *field,
251 struct hid_usage *usage, __s32 value, struct lg_drv_data *drv_data)
252{
253 struct lg4ff_device_entry *entry = drv_data->device_props;
254 __s32 new_value = 0;
255
256 if (!entry) {
257 hid_err(hid, "Device properties not found");
258 return 0;
259 }
260
261 switch (entry->product_id) {
262 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
263 switch (usage->code) {
264 case ABS_X:
265 new_value = lg4ff_adjust_dfp_x_axis(value, entry->range);
266 input_event(field->hidinput->input, usage->type, usage->code, new_value);
267 return 1;
268 default:
269 return 0;
270 }
271 default:
272 return 0;
273 }
274}
275
Michal Malý30bb75d2011-08-04 16:20:40 +0200276static int hid_lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
Simon Wood32c88cb2010-09-22 13:19:42 +0200277{
278 struct hid_device *hid = input_get_drvdata(dev);
279 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
280 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malý74479ba2012-09-24 01:09:30 +0200281 __s32 *value = report->field[0]->value;
Simon Wood32c88cb2010-09-22 13:19:42 +0200282 int x;
283
Michal Malýa80fe5d2012-09-24 01:13:17 +0200284#define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0)
Simon Wood32c88cb2010-09-22 13:19:42 +0200285
286 switch (effect->type) {
287 case FF_CONSTANT:
288 x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */
289 CLAMP(x);
Simon Wood56930e72013-11-06 12:30:42 -0700290
291 if (x == 0x80) {
292 /* De-activate force in slot-1*/
293 value[0] = 0x13;
294 value[1] = 0x00;
295 value[2] = 0x00;
296 value[3] = 0x00;
297 value[4] = 0x00;
298 value[5] = 0x00;
299 value[6] = 0x00;
300
301 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
302 return 0;
303 }
304
Michal Malý74479ba2012-09-24 01:09:30 +0200305 value[0] = 0x11; /* Slot 1 */
306 value[1] = 0x08;
307 value[2] = x;
308 value[3] = 0x80;
309 value[4] = 0x00;
310 value[5] = 0x00;
311 value[6] = 0x00;
Simon Wood32c88cb2010-09-22 13:19:42 +0200312
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100313 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Simon Wood32c88cb2010-09-22 13:19:42 +0200314 break;
315 }
316 return 0;
317}
318
Michal Malý6e2de8e2011-08-04 16:22:07 +0200319/* Sends default autocentering command compatible with
320 * all wheels except Formula Force EX */
321static void hid_lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude)
Simon Wood32c88cb2010-09-22 13:19:42 +0200322{
323 struct hid_device *hid = input_get_drvdata(dev);
324 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
325 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malý74479ba2012-09-24 01:09:30 +0200326 __s32 *value = report->field[0]->value;
Simon Woodf8c23152013-11-06 12:30:40 -0700327 __u32 expand_a, expand_b;
Simon Wood18597622013-11-06 12:30:44 -0700328 struct lg4ff_device_entry *entry;
329 struct lg_drv_data *drv_data;
330
331 drv_data = hid_get_drvdata(hid);
332 if (!drv_data) {
333 hid_err(hid, "Private driver data not found!\n");
334 return;
335 }
336
337 entry = drv_data->device_props;
338 if (!entry) {
339 hid_err(hid, "Device properties not found!\n");
340 return;
341 }
Simon Woodf8c23152013-11-06 12:30:40 -0700342
Simon Woodd2c02da2013-11-06 12:30:41 -0700343 /* De-activate Auto-Center */
344 if (magnitude == 0) {
345 value[0] = 0xf5;
346 value[1] = 0x00;
347 value[2] = 0x00;
348 value[3] = 0x00;
349 value[4] = 0x00;
350 value[5] = 0x00;
351 value[6] = 0x00;
352
353 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
354 return;
355 }
356
Simon Woodf8c23152013-11-06 12:30:40 -0700357 if (magnitude <= 0xaaaa) {
358 expand_a = 0x0c * magnitude;
359 expand_b = 0x80 * magnitude;
360 } else {
361 expand_a = (0x0c * 0xaaaa) + 0x06 * (magnitude - 0xaaaa);
362 expand_b = (0x80 * 0xaaaa) + 0xff * (magnitude - 0xaaaa);
363 }
Simon Wood32c88cb2010-09-22 13:19:42 +0200364
Simon Wood18597622013-11-06 12:30:44 -0700365 /* Adjust for non-MOMO wheels */
366 switch (entry->product_id) {
367 case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL:
368 case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2:
369 break;
370 default:
371 expand_a = expand_a >> 1;
372 break;
373 }
374
Michal Malý74479ba2012-09-24 01:09:30 +0200375 value[0] = 0xfe;
376 value[1] = 0x0d;
Simon Woodf8c23152013-11-06 12:30:40 -0700377 value[2] = expand_a / 0xaaaa;
378 value[3] = expand_a / 0xaaaa;
379 value[4] = expand_b / 0xaaaa;
Michal Malý74479ba2012-09-24 01:09:30 +0200380 value[5] = 0x00;
381 value[6] = 0x00;
Simon Wood32c88cb2010-09-22 13:19:42 +0200382
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100383 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Simon Woodd2c02da2013-11-06 12:30:41 -0700384
385 /* Activate Auto-Center */
386 value[0] = 0x14;
387 value[1] = 0x00;
388 value[2] = 0x00;
389 value[3] = 0x00;
390 value[4] = 0x00;
391 value[5] = 0x00;
392 value[6] = 0x00;
393
394 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Simon Wood32c88cb2010-09-22 13:19:42 +0200395}
396
Michal Malý6e2de8e2011-08-04 16:22:07 +0200397/* Sends autocentering command compatible with Formula Force EX */
398static void hid_lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude)
399{
400 struct hid_device *hid = input_get_drvdata(dev);
401 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
402 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malý74479ba2012-09-24 01:09:30 +0200403 __s32 *value = report->field[0]->value;
Michal Malý6e2de8e2011-08-04 16:22:07 +0200404 magnitude = magnitude * 90 / 65535;
Michal Malý6e2de8e2011-08-04 16:22:07 +0200405
Michal Malý74479ba2012-09-24 01:09:30 +0200406 value[0] = 0xfe;
407 value[1] = 0x03;
408 value[2] = magnitude >> 14;
409 value[3] = magnitude >> 14;
410 value[4] = magnitude;
411 value[5] = 0x00;
412 value[6] = 0x00;
Michal Malý6e2de8e2011-08-04 16:22:07 +0200413
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100414 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malý6e2de8e2011-08-04 16:22:07 +0200415}
416
Michal Malý30bb75d2011-08-04 16:20:40 +0200417/* Sends command to set range compatible with G25/G27/Driving Force GT */
418static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range)
419{
420 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
421 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malý74479ba2012-09-24 01:09:30 +0200422 __s32 *value = report->field[0]->value;
423
Michal Malý30bb75d2011-08-04 16:20:40 +0200424 dbg_hid("G25/G27/DFGT: setting range to %u\n", range);
425
Michal Malý74479ba2012-09-24 01:09:30 +0200426 value[0] = 0xf8;
427 value[1] = 0x81;
428 value[2] = range & 0x00ff;
429 value[3] = (range & 0xff00) >> 8;
430 value[4] = 0x00;
431 value[5] = 0x00;
432 value[6] = 0x00;
Michal Malý30bb75d2011-08-04 16:20:40 +0200433
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100434 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malý30bb75d2011-08-04 16:20:40 +0200435}
436
437/* Sends commands to set range compatible with Driving Force Pro wheel */
438static void hid_lg4ff_set_range_dfp(struct hid_device *hid, __u16 range)
439{
440 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
441 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
442 int start_left, start_right, full_range;
Michal Malý74479ba2012-09-24 01:09:30 +0200443 __s32 *value = report->field[0]->value;
444
Michal Malý30bb75d2011-08-04 16:20:40 +0200445 dbg_hid("Driving Force Pro: setting range to %u\n", range);
446
447 /* Prepare "coarse" limit command */
Michal Malý74479ba2012-09-24 01:09:30 +0200448 value[0] = 0xf8;
Michal Malýa80fe5d2012-09-24 01:13:17 +0200449 value[1] = 0x00; /* Set later */
Michal Malý74479ba2012-09-24 01:09:30 +0200450 value[2] = 0x00;
451 value[3] = 0x00;
452 value[4] = 0x00;
453 value[5] = 0x00;
454 value[6] = 0x00;
Michal Malý30bb75d2011-08-04 16:20:40 +0200455
456 if (range > 200) {
457 report->field[0]->value[1] = 0x03;
458 full_range = 900;
459 } else {
460 report->field[0]->value[1] = 0x02;
461 full_range = 200;
462 }
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100463 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malý30bb75d2011-08-04 16:20:40 +0200464
465 /* Prepare "fine" limit command */
Michal Malý74479ba2012-09-24 01:09:30 +0200466 value[0] = 0x81;
467 value[1] = 0x0b;
468 value[2] = 0x00;
469 value[3] = 0x00;
470 value[4] = 0x00;
471 value[5] = 0x00;
472 value[6] = 0x00;
Michal Malý30bb75d2011-08-04 16:20:40 +0200473
474 if (range == 200 || range == 900) { /* Do not apply any fine limit */
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100475 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malý30bb75d2011-08-04 16:20:40 +0200476 return;
477 }
478
479 /* Construct fine limit command */
480 start_left = (((full_range - range + 1) * 2047) / full_range);
481 start_right = 0xfff - start_left;
482
Michal Malý74479ba2012-09-24 01:09:30 +0200483 value[2] = start_left >> 4;
484 value[3] = start_right >> 4;
485 value[4] = 0xff;
486 value[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
487 value[6] = 0xff;
Michal Malý30bb75d2011-08-04 16:20:40 +0200488
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100489 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malý30bb75d2011-08-04 16:20:40 +0200490}
491
Michal Malýe7c23442015-02-18 17:59:20 +0100492static int lg4ff_switch_compatibility_mode(struct hid_device *hid, const struct lg4ff_compat_mode_switch *s)
Michal Malý96440c82011-08-04 16:18:11 +0200493{
Michal Malýe7c23442015-02-18 17:59:20 +0100494 struct usb_device *usbdev = hid_to_usb_dev(hid);
495 struct usbhid_device *usbhid = hid->driver_data;
496 u8 i;
Michal Malý96440c82011-08-04 16:18:11 +0200497
Michal Malýe7c23442015-02-18 17:59:20 +0100498 for (i = 0; i < s->cmd_count; i++) {
499 int xferd, ret;
500 u8 data[7];
Michal Malý96440c82011-08-04 16:18:11 +0200501
Michal Malýe7c23442015-02-18 17:59:20 +0100502 memcpy(data, s->cmd + (7*i), 7);
503 ret = usb_interrupt_msg(usbdev, usbhid->urbout->pipe, data, 7, &xferd, USB_CTRL_SET_TIMEOUT);
504 if (ret)
505 return ret;
Michal Malý96440c82011-08-04 16:18:11 +0200506 }
Michal Malýe7c23442015-02-18 17:59:20 +0100507 return 0;
Michal Malý96440c82011-08-04 16:18:11 +0200508}
Simon Wood32c88cb2010-09-22 13:19:42 +0200509
Michal Malýb96d23e2015-02-18 17:59:21 +0100510static ssize_t lg4ff_alternate_modes_show(struct device *dev, struct device_attribute *attr, char *buf)
511{
512 struct hid_device *hid = to_hid_device(dev);
513 struct lg4ff_device_entry *entry;
514 struct lg_drv_data *drv_data;
515 ssize_t count = 0;
516 int i;
517
518 drv_data = hid_get_drvdata(hid);
519 if (!drv_data) {
520 hid_err(hid, "Private driver data not found!\n");
521 return 0;
522 }
523
524 entry = drv_data->device_props;
525 if (!entry) {
526 hid_err(hid, "Device properties not found!\n");
527 return 0;
528 }
529
530 if (!entry->real_name) {
531 hid_err(hid, "NULL pointer to string\n");
532 return 0;
533 }
534
535 for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
536 if (entry->alternate_modes & BIT(i)) {
537 /* Print tag and full name */
538 count += scnprintf(buf + count, PAGE_SIZE - count, "%s: %s",
539 lg4ff_alternate_modes[i].tag,
540 !lg4ff_alternate_modes[i].product_id ? entry->real_name : lg4ff_alternate_modes[i].name);
541 if (count >= PAGE_SIZE - 1)
542 return count;
543
544 /* Mark the currently active mode with an asterisk */
545 if (lg4ff_alternate_modes[i].product_id == entry->product_id ||
546 (lg4ff_alternate_modes[i].product_id == 0 && entry->product_id == entry->real_product_id))
547 count += scnprintf(buf + count, PAGE_SIZE - count, " *\n");
548 else
549 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
550
551 if (count >= PAGE_SIZE - 1)
552 return count;
553 }
554 }
555
556 return count;
557}
558
559static ssize_t lg4ff_alternate_modes_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
560{
561 return -ENOSYS;
562}
563static DEVICE_ATTR(alternate_modes, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_alternate_modes_show, lg4ff_alternate_modes_store);
564
Michal Malý30bb75d2011-08-04 16:20:40 +0200565/* Read current range and display it in terminal */
Vivien Didelot2f1cec32015-01-19 02:03:26 -0500566static ssize_t range_show(struct device *dev, struct device_attribute *attr,
567 char *buf)
Michal Malý30bb75d2011-08-04 16:20:40 +0200568{
Michal Malý30bb75d2011-08-04 16:20:40 +0200569 struct hid_device *hid = to_hid_device(dev);
Michal Malý3b6b17b2012-04-09 09:08:49 +0200570 struct lg4ff_device_entry *entry;
571 struct lg_drv_data *drv_data;
Michal Malý30bb75d2011-08-04 16:20:40 +0200572 size_t count;
573
Michal Malý3b6b17b2012-04-09 09:08:49 +0200574 drv_data = hid_get_drvdata(hid);
575 if (!drv_data) {
576 hid_err(hid, "Private driver data not found!\n");
577 return 0;
Michal Malý30bb75d2011-08-04 16:20:40 +0200578 }
Michal Malý3b6b17b2012-04-09 09:08:49 +0200579
580 entry = drv_data->device_props;
581 if (!entry) {
582 hid_err(hid, "Device properties not found!\n");
Michal Malý30bb75d2011-08-04 16:20:40 +0200583 return 0;
584 }
585
586 count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->range);
587 return count;
588}
589
590/* Set range to user specified value, call appropriate function
591 * according to the type of the wheel */
Vivien Didelot2f1cec32015-01-19 02:03:26 -0500592static ssize_t range_store(struct device *dev, struct device_attribute *attr,
593 const char *buf, size_t count)
Michal Malý30bb75d2011-08-04 16:20:40 +0200594{
Michal Malý30bb75d2011-08-04 16:20:40 +0200595 struct hid_device *hid = to_hid_device(dev);
Michal Malý3b6b17b2012-04-09 09:08:49 +0200596 struct lg4ff_device_entry *entry;
597 struct lg_drv_data *drv_data;
Michal Malý30bb75d2011-08-04 16:20:40 +0200598 __u16 range = simple_strtoul(buf, NULL, 10);
599
Michal Malý3b6b17b2012-04-09 09:08:49 +0200600 drv_data = hid_get_drvdata(hid);
601 if (!drv_data) {
602 hid_err(hid, "Private driver data not found!\n");
Simon Wood29ff6652014-08-14 20:43:01 -0600603 return -EINVAL;
Michal Malý30bb75d2011-08-04 16:20:40 +0200604 }
Michal Malý3b6b17b2012-04-09 09:08:49 +0200605
606 entry = drv_data->device_props;
607 if (!entry) {
608 hid_err(hid, "Device properties not found!\n");
Simon Wood29ff6652014-08-14 20:43:01 -0600609 return -EINVAL;
Michal Malý30bb75d2011-08-04 16:20:40 +0200610 }
611
612 if (range == 0)
613 range = entry->max_range;
614
615 /* Check if the wheel supports range setting
616 * and that the range is within limits for the wheel */
617 if (entry->set_range != NULL && range >= entry->min_range && range <= entry->max_range) {
618 entry->set_range(hid, range);
619 entry->range = range;
620 }
621
622 return count;
623}
Vivien Didelot2f1cec32015-01-19 02:03:26 -0500624static DEVICE_ATTR_RW(range);
Michal Malý30bb75d2011-08-04 16:20:40 +0200625
Michal Malýb96d23e2015-02-18 17:59:21 +0100626static ssize_t lg4ff_real_id_show(struct device *dev, struct device_attribute *attr, char *buf)
627{
628 struct hid_device *hid = to_hid_device(dev);
629 struct lg4ff_device_entry *entry;
630 struct lg_drv_data *drv_data;
631 size_t count;
632
633 drv_data = hid_get_drvdata(hid);
634 if (!drv_data) {
635 hid_err(hid, "Private driver data not found!\n");
636 return 0;
637 }
638
639 entry = drv_data->device_props;
640 if (!entry) {
641 hid_err(hid, "Device properties not found!\n");
642 return 0;
643 }
644
645 if (!entry->real_tag || !entry->real_name) {
646 hid_err(hid, "NULL pointer to string\n");
647 return 0;
648 }
649
650 count = scnprintf(buf, PAGE_SIZE, "%s: %s\n", entry->real_tag, entry->real_name);
651 return count;
652}
653
654static ssize_t lg4ff_real_id_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
655{
656 /* Real ID is a read-only value */
657 return -EPERM;
658}
659static DEVICE_ATTR(real_id, S_IRUGO, lg4ff_real_id_show, lg4ff_real_id_store);
660
Simon Wood22bcefd2012-04-21 05:41:15 -0700661#ifdef CONFIG_LEDS_CLASS
662static void lg4ff_set_leds(struct hid_device *hid, __u8 leds)
663{
664 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
665 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malý74479ba2012-09-24 01:09:30 +0200666 __s32 *value = report->field[0]->value;
Simon Wood22bcefd2012-04-21 05:41:15 -0700667
Michal Malý74479ba2012-09-24 01:09:30 +0200668 value[0] = 0xf8;
669 value[1] = 0x12;
670 value[2] = leds;
671 value[3] = 0x00;
672 value[4] = 0x00;
673 value[5] = 0x00;
674 value[6] = 0x00;
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100675 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Simon Wood22bcefd2012-04-21 05:41:15 -0700676}
677
678static void lg4ff_led_set_brightness(struct led_classdev *led_cdev,
679 enum led_brightness value)
680{
681 struct device *dev = led_cdev->dev->parent;
682 struct hid_device *hid = container_of(dev, struct hid_device, dev);
Axel Lin4629fd12012-09-13 14:09:33 +0800683 struct lg_drv_data *drv_data = hid_get_drvdata(hid);
Simon Wood22bcefd2012-04-21 05:41:15 -0700684 struct lg4ff_device_entry *entry;
685 int i, state = 0;
686
687 if (!drv_data) {
688 hid_err(hid, "Device data not found.");
689 return;
690 }
691
692 entry = (struct lg4ff_device_entry *)drv_data->device_props;
693
694 if (!entry) {
695 hid_err(hid, "Device properties not found.");
696 return;
697 }
698
699 for (i = 0; i < 5; i++) {
700 if (led_cdev != entry->led[i])
701 continue;
702 state = (entry->led_state >> i) & 1;
703 if (value == LED_OFF && state) {
704 entry->led_state &= ~(1 << i);
705 lg4ff_set_leds(hid, entry->led_state);
706 } else if (value != LED_OFF && !state) {
707 entry->led_state |= 1 << i;
708 lg4ff_set_leds(hid, entry->led_state);
709 }
710 break;
711 }
712}
713
714static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev)
715{
716 struct device *dev = led_cdev->dev->parent;
717 struct hid_device *hid = container_of(dev, struct hid_device, dev);
Axel Lin4629fd12012-09-13 14:09:33 +0800718 struct lg_drv_data *drv_data = hid_get_drvdata(hid);
Simon Wood22bcefd2012-04-21 05:41:15 -0700719 struct lg4ff_device_entry *entry;
720 int i, value = 0;
721
722 if (!drv_data) {
723 hid_err(hid, "Device data not found.");
724 return LED_OFF;
725 }
726
727 entry = (struct lg4ff_device_entry *)drv_data->device_props;
728
729 if (!entry) {
730 hid_err(hid, "Device properties not found.");
731 return LED_OFF;
732 }
733
734 for (i = 0; i < 5; i++)
735 if (led_cdev == entry->led[i]) {
736 value = (entry->led_state >> i) & 1;
737 break;
738 }
739
740 return value ? LED_FULL : LED_OFF;
741}
742#endif
743
Michal Malýe7c23442015-02-18 17:59:20 +0100744static u16 lg4ff_identify_multimode_wheel(struct hid_device *hid, const u16 reported_product_id, const u16 bcdDevice)
745{
746 const struct lg4ff_wheel_ident_checklist *checklist;
747 int i, from_idx, to_idx;
748
749 switch (reported_product_id) {
750 case USB_DEVICE_ID_LOGITECH_WHEEL:
751 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
752 checklist = &lg4ff_main_checklist;
753 from_idx = 0;
754 to_idx = checklist->count - 1;
755 break;
756 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
757 checklist = &lg4ff_main_checklist;
758 from_idx = 0;
759 to_idx = checklist->count - 2; /* End identity check at G25 */
760 break;
761 case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
762 checklist = &lg4ff_main_checklist;
763 from_idx = 1; /* Start identity check at G27 */
764 to_idx = checklist->count - 3; /* End identity check at G27 */
765 break;
766 case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
767 checklist = &lg4ff_main_checklist;
768 from_idx = 0;
769 to_idx = checklist->count - 4; /* End identity check at DFGT */
770 break;
771 default:
772 return 0;
773 }
774
775 for (i = from_idx; i <= to_idx; i++) {
776 const u16 mask = checklist->models[i]->mask;
777 const u16 result = checklist->models[i]->result;
778 const u16 real_product_id = checklist->models[i]->real_product_id;
779
780 if ((bcdDevice & mask) == result) {
781 dbg_hid("Found wheel with real PID %X whose reported PID is %X\n", real_product_id, reported_product_id);
782 return real_product_id;
783 }
784 }
785
786 /* No match found. This is an unknown wheel model, do not touch it */
787 dbg_hid("Wheel with bcdDevice %X was not recognized as multimode wheel, leaving in its current mode\n", bcdDevice);
788 return 0;
789}
790
791static int lg4ff_handle_multimode_wheel(struct hid_device *hid, u16 *real_product_id, const u16 bcdDevice)
792{
793 const u16 reported_product_id = hid->product;
794 int ret;
795
796 *real_product_id = lg4ff_identify_multimode_wheel(hid, reported_product_id, bcdDevice);
797 /* Probed wheel is not a multimode wheel */
798 if (!*real_product_id) {
799 *real_product_id = reported_product_id;
800 dbg_hid("Wheel is not a multimode wheel\n");
801 return LG4FF_MMODE_NOT_MULTIMODE;
802 }
803
804 /* Switch from "Driving Force" mode to native mode automatically.
805 * Otherwise keep the wheel in its current mode */
806 if (reported_product_id == USB_DEVICE_ID_LOGITECH_WHEEL &&
Michal Malýa54dc7792015-02-18 17:59:22 +0100807 reported_product_id != *real_product_id &&
808 !lg4ff_no_autoswitch) {
Michal Malýe7c23442015-02-18 17:59:20 +0100809 const struct lg4ff_compat_mode_switch *s;
810
811 switch (*real_product_id) {
812 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
813 s = &lg4ff_mode_switch_dfp;
814 break;
815 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
816 s = &lg4ff_mode_switch_g25;
817 break;
818 case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
819 s = &lg4ff_mode_switch_g27;
820 break;
821 case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
822 s = &lg4ff_mode_switch_dfgt;
823 break;
824 default:
825 hid_err(hid, "Invalid product id %X\n", *real_product_id);
Michal Malýb96d23e2015-02-18 17:59:21 +0100826 return LG4FF_MMODE_NOT_MULTIMODE;
Michal Malýe7c23442015-02-18 17:59:20 +0100827 }
828
829 ret = lg4ff_switch_compatibility_mode(hid, s);
830 if (ret) {
831 /* Wheel could not have been switched to native mode,
832 * leave it in "Driving Force" mode and continue */
833 hid_err(hid, "Unable to switch wheel mode, errno %d\n", ret);
Michal Malýb96d23e2015-02-18 17:59:21 +0100834 return LG4FF_MMODE_IS_MULTIMODE;
Michal Malýe7c23442015-02-18 17:59:20 +0100835 }
836 return LG4FF_MMODE_SWITCHED;
837 }
838
Michal Malýb96d23e2015-02-18 17:59:21 +0100839 return LG4FF_MMODE_IS_MULTIMODE;
Michal Malýe7c23442015-02-18 17:59:20 +0100840}
841
842
Simon Wood32c88cb2010-09-22 13:19:42 +0200843int lg4ff_init(struct hid_device *hid)
844{
845 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
Simon Wood32c88cb2010-09-22 13:19:42 +0200846 struct input_dev *dev = hidinput->input;
Michal Malýe7c23442015-02-18 17:59:20 +0100847 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
848 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
Michal Malý30bb75d2011-08-04 16:20:40 +0200849 struct lg4ff_device_entry *entry;
Michal Malý3b6b17b2012-04-09 09:08:49 +0200850 struct lg_drv_data *drv_data;
Michal Malýb96d23e2015-02-18 17:59:21 +0100851 int error, i, j;
852 int mmode_ret, mmode_idx = -1;
Michal Malýe7c23442015-02-18 17:59:20 +0100853 u16 real_product_id;
Simon Wood32c88cb2010-09-22 13:19:42 +0200854
Simon Wood32c88cb2010-09-22 13:19:42 +0200855 /* Check that the report looks ok */
Kees Cook0fb6bd02013-09-11 21:56:54 +0200856 if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
Simon Wood32c88cb2010-09-22 13:19:42 +0200857 return -1;
Michal Malý30bb75d2011-08-04 16:20:40 +0200858
Michal Malýe7c23442015-02-18 17:59:20 +0100859 /* Check if a multimode wheel has been connected and
860 * handle it appropriately */
Michal Malýb96d23e2015-02-18 17:59:21 +0100861 mmode_ret = lg4ff_handle_multimode_wheel(hid, &real_product_id, bcdDevice);
Michal Malýe7c23442015-02-18 17:59:20 +0100862
863 /* Wheel has been told to switch to native mode. There is no point in going on
864 * with the initialization as the wheel will do a USB reset when it switches mode
865 */
Michal Malýb96d23e2015-02-18 17:59:21 +0100866 if (mmode_ret == LG4FF_MMODE_SWITCHED)
Michal Malýe7c23442015-02-18 17:59:20 +0100867 return 0;
868
Michal Malý7362cd22011-08-04 16:16:09 +0200869 /* Check what wheel has been connected */
870 for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
871 if (hid->product == lg4ff_devices[i].product_id) {
872 dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
873 break;
874 }
875 }
Simon Wood32c88cb2010-09-22 13:19:42 +0200876
Michal Malý7362cd22011-08-04 16:16:09 +0200877 if (i == ARRAY_SIZE(lg4ff_devices)) {
878 hid_err(hid, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to"
879 "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n");
880 return -1;
881 }
882
Michal Malýb96d23e2015-02-18 17:59:21 +0100883 if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
884 for (mmode_idx = 0; mmode_idx < ARRAY_SIZE(lg4ff_multimode_wheels); mmode_idx++) {
885 if (real_product_id == lg4ff_multimode_wheels[mmode_idx].product_id)
886 break;
887 }
888
889 if (mmode_idx == ARRAY_SIZE(lg4ff_multimode_wheels)) {
890 hid_err(hid, "Device product ID %X is not listed as a multimode wheel", real_product_id);
891 return -1;
892 }
893 }
894
Michal Malý7362cd22011-08-04 16:16:09 +0200895 /* Set supported force feedback capabilities */
896 for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
897 set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
Simon Wood32c88cb2010-09-22 13:19:42 +0200898
899 error = input_ff_create_memless(dev, NULL, hid_lg4ff_play);
900
901 if (error)
902 return error;
903
Michal Malý3b6b17b2012-04-09 09:08:49 +0200904 /* Get private driver data */
905 drv_data = hid_get_drvdata(hid);
906 if (!drv_data) {
907 hid_err(hid, "Cannot add device, private driver data not allocated\n");
908 return -1;
Michal Malý30bb75d2011-08-04 16:20:40 +0200909 }
910
Michal Malý3b6b17b2012-04-09 09:08:49 +0200911 /* Initialize device properties */
Thomas Meyer8383c6b2011-11-08 20:14:14 +0100912 entry = kzalloc(sizeof(struct lg4ff_device_entry), GFP_KERNEL);
Michal Malý30bb75d2011-08-04 16:20:40 +0200913 if (!entry) {
Michal Malý3b6b17b2012-04-09 09:08:49 +0200914 hid_err(hid, "Cannot add device, insufficient memory to allocate device properties.\n");
Michal Malý30bb75d2011-08-04 16:20:40 +0200915 return -ENOMEM;
916 }
Michal Malý3b6b17b2012-04-09 09:08:49 +0200917 drv_data->device_props = entry;
918
Michal Malý2b24a962012-09-23 22:41:08 +0200919 entry->product_id = lg4ff_devices[i].product_id;
Michal Malýb96d23e2015-02-18 17:59:21 +0100920 entry->real_product_id = real_product_id;
Michal Malý30bb75d2011-08-04 16:20:40 +0200921 entry->min_range = lg4ff_devices[i].min_range;
922 entry->max_range = lg4ff_devices[i].max_range;
923 entry->set_range = lg4ff_devices[i].set_range;
Michal Malýb96d23e2015-02-18 17:59:21 +0100924 if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
925 BUG_ON(mmode_idx == -1);
926 entry->alternate_modes = lg4ff_multimode_wheels[mmode_idx].alternate_modes;
927 entry->real_tag = lg4ff_multimode_wheels[mmode_idx].real_tag;
928 entry->real_name = lg4ff_multimode_wheels[mmode_idx].real_name;
929 }
Michal Malý30bb75d2011-08-04 16:20:40 +0200930
Simon Wood114a55c2013-11-06 12:30:43 -0700931 /* Check if autocentering is available and
932 * set the centering force to zero by default */
933 if (test_bit(FF_AUTOCENTER, dev->ffbit)) {
Michal Malýe7c23442015-02-18 17:59:20 +0100934 /* Formula Force EX expects different autocentering command */
935 if ((bcdDevice >> 8) == LG4FF_FFEX_REV_MAJ &&
936 (bcdDevice & 0xff) == LG4FF_FFEX_REV_MIN)
Simon Wood114a55c2013-11-06 12:30:43 -0700937 dev->ff->set_autocenter = hid_lg4ff_set_autocenter_ffex;
938 else
939 dev->ff->set_autocenter = hid_lg4ff_set_autocenter_default;
940
941 dev->ff->set_autocenter(dev, 0);
942 }
943
Michal Malý30bb75d2011-08-04 16:20:40 +0200944 /* Create sysfs interface */
945 error = device_create_file(&hid->dev, &dev_attr_range);
946 if (error)
947 return error;
Michal Malýb96d23e2015-02-18 17:59:21 +0100948 if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
949 error = device_create_file(&hid->dev, &dev_attr_real_id);
950 if (error)
951 return error;
952 error = device_create_file(&hid->dev, &dev_attr_alternate_modes);
953 if (error)
954 return error;
955 }
Michal Malý30bb75d2011-08-04 16:20:40 +0200956 dbg_hid("sysfs interface created\n");
957
958 /* Set the maximum range to start with */
959 entry->range = entry->max_range;
960 if (entry->set_range != NULL)
961 entry->set_range(hid, entry->range);
962
Simon Wood22bcefd2012-04-21 05:41:15 -0700963#ifdef CONFIG_LEDS_CLASS
964 /* register led subsystem - G27 only */
965 entry->led_state = 0;
966 for (j = 0; j < 5; j++)
967 entry->led[j] = NULL;
968
969 if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL) {
970 struct led_classdev *led;
971 size_t name_sz;
972 char *name;
973
974 lg4ff_set_leds(hid, 0);
975
976 name_sz = strlen(dev_name(&hid->dev)) + 8;
977
978 for (j = 0; j < 5; j++) {
979 led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
980 if (!led) {
981 hid_err(hid, "can't allocate memory for LED %d\n", j);
982 goto err;
983 }
984
985 name = (void *)(&led[1]);
986 snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1);
987 led->name = name;
988 led->brightness = 0;
989 led->max_brightness = 1;
990 led->brightness_get = lg4ff_led_get_brightness;
991 led->brightness_set = lg4ff_led_set_brightness;
992
993 entry->led[j] = led;
994 error = led_classdev_register(&hid->dev, led);
995
996 if (error) {
997 hid_err(hid, "failed to register LED %d. Aborting.\n", j);
998err:
999 /* Deregister LEDs (if any) */
1000 for (j = 0; j < 5; j++) {
1001 led = entry->led[j];
1002 entry->led[j] = NULL;
1003 if (!led)
1004 continue;
1005 led_classdev_unregister(led);
1006 kfree(led);
1007 }
1008 goto out; /* Let the driver continue without LEDs */
1009 }
1010 }
1011 }
Simon Wood22bcefd2012-04-21 05:41:15 -07001012out:
Jiri Kosinac6e6dc82012-04-23 21:08:15 +02001013#endif
Simon Wood64013802012-04-21 05:41:16 -07001014 hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n");
Simon Wood32c88cb2010-09-22 13:19:42 +02001015 return 0;
1016}
1017
Michal Malý30bb75d2011-08-04 16:20:40 +02001018int lg4ff_deinit(struct hid_device *hid)
1019{
Michal Malý30bb75d2011-08-04 16:20:40 +02001020 struct lg4ff_device_entry *entry;
Michal Malý3b6b17b2012-04-09 09:08:49 +02001021 struct lg_drv_data *drv_data;
1022
Michal Malý3b6b17b2012-04-09 09:08:49 +02001023 drv_data = hid_get_drvdata(hid);
1024 if (!drv_data) {
1025 hid_err(hid, "Error while deinitializing device, no private driver data.\n");
Michal Malý30bb75d2011-08-04 16:20:40 +02001026 return -1;
1027 }
Michal Malý3b6b17b2012-04-09 09:08:49 +02001028 entry = drv_data->device_props;
Michal Malýe7c23442015-02-18 17:59:20 +01001029 if (!entry)
1030 goto out; /* Nothing more to do */
1031
1032 device_remove_file(&hid->dev, &dev_attr_range);
Simon Wood22bcefd2012-04-21 05:41:15 -07001033
Michal Malýb96d23e2015-02-18 17:59:21 +01001034 /* Multimode devices will have at least the "MODE_NATIVE" bit set */
1035 if (entry->alternate_modes) {
1036 device_remove_file(&hid->dev, &dev_attr_real_id);
1037 device_remove_file(&hid->dev, &dev_attr_alternate_modes);
1038 }
1039
Simon Wood22bcefd2012-04-21 05:41:15 -07001040#ifdef CONFIG_LEDS_CLASS
1041 {
1042 int j;
1043 struct led_classdev *led;
1044
1045 /* Deregister LEDs (if any) */
1046 for (j = 0; j < 5; j++) {
1047
1048 led = entry->led[j];
1049 entry->led[j] = NULL;
1050 if (!led)
1051 continue;
1052 led_classdev_unregister(led);
1053 kfree(led);
1054 }
1055 }
1056#endif
1057
Michal Malý3b6b17b2012-04-09 09:08:49 +02001058 /* Deallocate memory */
1059 kfree(entry);
Michal Malý30bb75d2011-08-04 16:20:40 +02001060
Michal Malýe7c23442015-02-18 17:59:20 +01001061out:
Michal Malý30bb75d2011-08-04 16:20:40 +02001062 dbg_hid("Device successfully unregistered\n");
1063 return 0;
1064}