HID: hid-lg4ff: Introduce a module parameter to disable automatic switch of compatibi...
[linux-block.git] / drivers / hid / hid-lg4ff.c
CommitLineData
32c88cbc 1/*
64013800 2 * Force feedback support for Logitech Gaming Wheels
32c88cbc 3 *
64013800
SW
4 * Including G27, G25, DFP, DFGT, FFEX, Momo, Momo2 &
5 * Speed Force Wireless (WiiWheel)
32c88cbc
SW
6 *
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"
a54dc779 33#include "hid-lg4ff.h"
7362cd22 34#include "hid-ids.h"
32c88cbc 35
30bb75d7
MM
36#define to_hid_device(pdev) container_of(pdev, struct hid_device, dev)
37
b96d23ec 38#define LG4FF_MMODE_IS_MULTIMODE 0
e7c23449
MM
39#define LG4FF_MMODE_SWITCHED 1
40#define LG4FF_MMODE_NOT_MULTIMODE 2
41
b96d23ec
MM
42#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
e7c23449
MM
68#define LG4FF_FFEX_REV_MAJ 0x21
69#define LG4FF_FFEX_REV_MIN 0x00
70
30bb75d7
MM
71static 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);
30bb75d7 73
30bb75d7 74struct lg4ff_device_entry {
2b24a960 75 __u32 product_id;
30bb75d7
MM
76 __u16 range;
77 __u16 min_range;
78 __u16 max_range;
22bcefdc
SW
79#ifdef CONFIG_LEDS_CLASS
80 __u8 led_state;
81 struct led_classdev *led[5];
82#endif
b96d23ec
MM
83 u32 alternate_modes;
84 const char *real_tag;
85 const char *real_name;
86 u16 real_product_id;
30bb75d7
MM
87 struct list_head list;
88 void (*set_range)(struct hid_device *hid, u16 range);
89};
90
7362cd22 91static const signed short lg4ff_wheel_effects[] = {
32c88cbc
SW
92 FF_CONSTANT,
93 FF_AUTOCENTER,
94 -1
95};
96
7362cd22
MM
97struct lg4ff_wheel {
98 const __u32 product_id;
99 const signed short *ff_effects;
100 const __u16 min_range;
101 const __u16 max_range;
30bb75d7 102 void (*set_range)(struct hid_device *hid, u16 range);
7362cd22
MM
103};
104
e7c23449
MM
105struct 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
b96d23ec
MM
121struct 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
7362cd22 134static const struct lg4ff_wheel lg4ff_devices[] = {
30bb75d7
MM
135 {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}
7362cd22
MM
143};
144
b96d23ec
MM
145static 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
e7c23449
MM
169/* 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
174};
175
176static const struct lg4ff_wheel_ident_info lg4ff_g25_ident_info = {
177 0xff00,
178 0x1200,
179 USB_DEVICE_ID_LOGITECH_G25_WHEEL
180};
181
182static const struct lg4ff_wheel_ident_info lg4ff_g27_ident_info = {
183 0xfff0,
184 0x1230,
185 USB_DEVICE_ID_LOGITECH_G27_WHEEL
96440c8a
MM
186};
187
e7c23449
MM
188static const struct lg4ff_wheel_ident_info lg4ff_dfgt_ident_info = {
189 0xff00,
190 0x1300,
191 USB_DEVICE_ID_LOGITECH_DFGT_WHEEL
96440c8a
MM
192};
193
e7c23449
MM
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 = {
96440c8a
MM
205 1,
206 {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
207};
208
e7c23449 209static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_dfgt = {
96440c8a
MM
210 2,
211 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */
212 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* 2nd command */
213};
214
e7c23449 215static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_g25 = {
96440c8a
MM
216 1,
217 {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
218};
219
e7c23449 220static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_g27 = {
96440c8a
MM
221 2,
222 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */
223 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* 2nd command */
224};
225
2b24a960
MM
226/* 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
30bb75d7 276static int hid_lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
32c88cbc
SW
277{
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);
74479ba8 281 __s32 *value = report->field[0]->value;
32c88cbc
SW
282 int x;
283
a80fe5d6 284#define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0)
32c88cbc
SW
285
286 switch (effect->type) {
287 case FF_CONSTANT:
288 x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */
289 CLAMP(x);
56930e7a
SW
290
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
74479ba8
MM
305 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;
32c88cbc 312
d8814272 313 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
32c88cbc
SW
314 break;
315 }
316 return 0;
317}
318
6e2de8e0
MM
319/* 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)
32c88cbc
SW
322{
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);
74479ba8 326 __s32 *value = report->field[0]->value;
f8c23156 327 __u32 expand_a, expand_b;
1859762e
SW
328 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 }
f8c23156 342
d2c02da5
SW
343 /* 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
f8c23156
SW
357 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 }
32c88cbc 364
1859762e
SW
365 /* 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
74479ba8
MM
375 value[0] = 0xfe;
376 value[1] = 0x0d;
f8c23156
SW
377 value[2] = expand_a / 0xaaaa;
378 value[3] = expand_a / 0xaaaa;
379 value[4] = expand_b / 0xaaaa;
74479ba8
MM
380 value[5] = 0x00;
381 value[6] = 0x00;
32c88cbc 382
d8814272 383 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
d2c02da5
SW
384
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);
32c88cbc
SW
395}
396
6e2de8e0
MM
397/* 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);
74479ba8 403 __s32 *value = report->field[0]->value;
6e2de8e0 404 magnitude = magnitude * 90 / 65535;
6e2de8e0 405
74479ba8
MM
406 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;
6e2de8e0 413
d8814272 414 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
6e2de8e0
MM
415}
416
30bb75d7
MM
417/* 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);
74479ba8
MM
422 __s32 *value = report->field[0]->value;
423
30bb75d7
MM
424 dbg_hid("G25/G27/DFGT: setting range to %u\n", range);
425
74479ba8
MM
426 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;
30bb75d7 433
d8814272 434 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
30bb75d7
MM
435}
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;
74479ba8
MM
443 __s32 *value = report->field[0]->value;
444
30bb75d7
MM
445 dbg_hid("Driving Force Pro: setting range to %u\n", range);
446
447 /* Prepare "coarse" limit command */
74479ba8 448 value[0] = 0xf8;
a80fe5d6 449 value[1] = 0x00; /* Set later */
74479ba8
MM
450 value[2] = 0x00;
451 value[3] = 0x00;
452 value[4] = 0x00;
453 value[5] = 0x00;
454 value[6] = 0x00;
30bb75d7
MM
455
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 }
d8814272 463 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
30bb75d7
MM
464
465 /* Prepare "fine" limit command */
74479ba8
MM
466 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;
30bb75d7
MM
473
474 if (range == 200 || range == 900) { /* Do not apply any fine limit */
d8814272 475 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
30bb75d7
MM
476 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
74479ba8
MM
483 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;
30bb75d7 488
d8814272 489 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
30bb75d7
MM
490}
491
e7c23449 492static int lg4ff_switch_compatibility_mode(struct hid_device *hid, const struct lg4ff_compat_mode_switch *s)
96440c8a 493{
e7c23449
MM
494 struct usb_device *usbdev = hid_to_usb_dev(hid);
495 struct usbhid_device *usbhid = hid->driver_data;
496 u8 i;
96440c8a 497
e7c23449
MM
498 for (i = 0; i < s->cmd_count; i++) {
499 int xferd, ret;
500 u8 data[7];
96440c8a 501
e7c23449
MM
502 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;
96440c8a 506 }
e7c23449 507 return 0;
96440c8a 508}
32c88cbc 509
b96d23ec
MM
510static 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
30bb75d7 565/* Read current range and display it in terminal */
2f1cec32
VD
566static ssize_t range_show(struct device *dev, struct device_attribute *attr,
567 char *buf)
30bb75d7 568{
30bb75d7 569 struct hid_device *hid = to_hid_device(dev);
3b6b17b7
MM
570 struct lg4ff_device_entry *entry;
571 struct lg_drv_data *drv_data;
30bb75d7
MM
572 size_t count;
573
3b6b17b7
MM
574 drv_data = hid_get_drvdata(hid);
575 if (!drv_data) {
576 hid_err(hid, "Private driver data not found!\n");
577 return 0;
30bb75d7 578 }
3b6b17b7
MM
579
580 entry = drv_data->device_props;
581 if (!entry) {
582 hid_err(hid, "Device properties not found!\n");
30bb75d7
MM
583 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 */
2f1cec32
VD
592static ssize_t range_store(struct device *dev, struct device_attribute *attr,
593 const char *buf, size_t count)
30bb75d7 594{
30bb75d7 595 struct hid_device *hid = to_hid_device(dev);
3b6b17b7
MM
596 struct lg4ff_device_entry *entry;
597 struct lg_drv_data *drv_data;
30bb75d7
MM
598 __u16 range = simple_strtoul(buf, NULL, 10);
599
3b6b17b7
MM
600 drv_data = hid_get_drvdata(hid);
601 if (!drv_data) {
602 hid_err(hid, "Private driver data not found!\n");
29ff6657 603 return -EINVAL;
30bb75d7 604 }
3b6b17b7
MM
605
606 entry = drv_data->device_props;
607 if (!entry) {
608 hid_err(hid, "Device properties not found!\n");
29ff6657 609 return -EINVAL;
30bb75d7
MM
610 }
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}
2f1cec32 624static DEVICE_ATTR_RW(range);
30bb75d7 625
b96d23ec
MM
626static 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
22bcefdc
SW
661#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);
74479ba8
MM
666 __s32 *value = report->field[0]->value;
667
668 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;
d8814272 675 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
22bcefdc
SW
676}
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);
4629fd16 683 struct lg_drv_data *drv_data = hid_get_drvdata(hid);
22bcefdc
SW
684 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);
4629fd16 718 struct lg_drv_data *drv_data = hid_get_drvdata(hid);
22bcefdc
SW
719 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
e7c23449
MM
744static 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 &&
a54dc779
MM
807 reported_product_id != *real_product_id &&
808 !lg4ff_no_autoswitch) {
e7c23449
MM
809 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);
b96d23ec 826 return LG4FF_MMODE_NOT_MULTIMODE;
e7c23449
MM
827 }
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);
b96d23ec 834 return LG4FF_MMODE_IS_MULTIMODE;
e7c23449
MM
835 }
836 return LG4FF_MMODE_SWITCHED;
837 }
838
b96d23ec 839 return LG4FF_MMODE_IS_MULTIMODE;
e7c23449
MM
840}
841
842
32c88cbc
SW
843int lg4ff_init(struct hid_device *hid)
844{
845 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
32c88cbc 846 struct input_dev *dev = hidinput->input;
e7c23449
MM
847 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
848 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
30bb75d7 849 struct lg4ff_device_entry *entry;
3b6b17b7 850 struct lg_drv_data *drv_data;
b96d23ec
MM
851 int error, i, j;
852 int mmode_ret, mmode_idx = -1;
e7c23449 853 u16 real_product_id;
32c88cbc 854
32c88cbc 855 /* Check that the report looks ok */
0fb6bd06 856 if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
32c88cbc 857 return -1;
30bb75d7 858
e7c23449
MM
859 /* Check if a multimode wheel has been connected and
860 * handle it appropriately */
b96d23ec 861 mmode_ret = lg4ff_handle_multimode_wheel(hid, &real_product_id, bcdDevice);
e7c23449
MM
862
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 */
b96d23ec 866 if (mmode_ret == LG4FF_MMODE_SWITCHED)
e7c23449
MM
867 return 0;
868
7362cd22
MM
869 /* 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 }
876
877 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 }
32c88cbc 882
b96d23ec
MM
883 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
7362cd22
MM
895 /* 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);
32c88cbc
SW
898
899 error = input_ff_create_memless(dev, NULL, hid_lg4ff_play);
900
901 if (error)
902 return error;
903
3b6b17b7
MM
904 /* 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;
30bb75d7
MM
909 }
910
3b6b17b7 911 /* Initialize device properties */
8383c6bf 912 entry = kzalloc(sizeof(struct lg4ff_device_entry), GFP_KERNEL);
30bb75d7 913 if (!entry) {
3b6b17b7 914 hid_err(hid, "Cannot add device, insufficient memory to allocate device properties.\n");
30bb75d7
MM
915 return -ENOMEM;
916 }
3b6b17b7
MM
917 drv_data->device_props = entry;
918
2b24a960 919 entry->product_id = lg4ff_devices[i].product_id;
b96d23ec 920 entry->real_product_id = real_product_id;
30bb75d7
MM
921 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;
b96d23ec
MM
924 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 }
30bb75d7 930
114a55cf
SW
931 /* Check if autocentering is available and
932 * set the centering force to zero by default */
933 if (test_bit(FF_AUTOCENTER, dev->ffbit)) {
e7c23449
MM
934 /* Formula Force EX expects different autocentering command */
935 if ((bcdDevice >> 8) == LG4FF_FFEX_REV_MAJ &&
936 (bcdDevice & 0xff) == LG4FF_FFEX_REV_MIN)
114a55cf
SW
937 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
30bb75d7
MM
944 /* Create sysfs interface */
945 error = device_create_file(&hid->dev, &dev_attr_range);
946 if (error)
947 return error;
b96d23ec
MM
948 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 }
30bb75d7
MM
956 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
22bcefdc
SW
963#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 }
22bcefdc 1012out:
c6e6dc87 1013#endif
64013800 1014 hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n");
32c88cbc
SW
1015 return 0;
1016}
1017
30bb75d7
MM
1018int lg4ff_deinit(struct hid_device *hid)
1019{
30bb75d7 1020 struct lg4ff_device_entry *entry;
3b6b17b7
MM
1021 struct lg_drv_data *drv_data;
1022
3b6b17b7
MM
1023 drv_data = hid_get_drvdata(hid);
1024 if (!drv_data) {
1025 hid_err(hid, "Error while deinitializing device, no private driver data.\n");
1026 return -1;
30bb75d7 1027 }
3b6b17b7 1028 entry = drv_data->device_props;
e7c23449
MM
1029 if (!entry)
1030 goto out; /* Nothing more to do */
1031
1032 device_remove_file(&hid->dev, &dev_attr_range);
22bcefdc 1033
b96d23ec
MM
1034 /* 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
22bcefdc
SW
1040#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
3b6b17b7
MM
1058 /* Deallocate memory */
1059 kfree(entry);
30bb75d7 1060
e7c23449 1061out:
30bb75d7
MM
1062 dbg_hid("Device successfully unregistered\n");
1063 return 0;
1064}