11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
232c88cbcSSimon Wood /*
364013800SSimon Wood * Force feedback support for Logitech Gaming Wheels
432c88cbcSSimon Wood *
564013800SSimon Wood * Including G27, G25, DFP, DFGT, FFEX, Momo, Momo2 &
664013800SSimon Wood * Speed Force Wireless (WiiWheel)
732c88cbcSSimon Wood *
832c88cbcSSimon Wood * Copyright (c) 2010 Simon Wood <simon@mungewell.org>
932c88cbcSSimon Wood */
1032c88cbcSSimon Wood
1132c88cbcSSimon Wood /*
1232c88cbcSSimon Wood */
1332c88cbcSSimon Wood
1432c88cbcSSimon Wood
1532c88cbcSSimon Wood #include <linux/input.h>
1632c88cbcSSimon Wood #include <linux/usb.h>
1732c88cbcSSimon Wood #include <linux/hid.h>
1832c88cbcSSimon Wood
1932c88cbcSSimon Wood #include "usbhid/usbhid.h"
2032c88cbcSSimon Wood #include "hid-lg.h"
21a54dc779SMichal Malý #include "hid-lg4ff.h"
227362cd22SMichal Malý #include "hid-ids.h"
2332c88cbcSSimon Wood
24b96d23ecSMichal Malý #define LG4FF_MMODE_IS_MULTIMODE 0
25e7c23449SMichal Malý #define LG4FF_MMODE_SWITCHED 1
26e7c23449SMichal Malý #define LG4FF_MMODE_NOT_MULTIMODE 2
27e7c23449SMichal Malý
28b96d23ecSMichal Malý #define LG4FF_MODE_NATIVE_IDX 0
29b96d23ecSMichal Malý #define LG4FF_MODE_DFEX_IDX 1
30b96d23ecSMichal Malý #define LG4FF_MODE_DFP_IDX 2
31b96d23ecSMichal Malý #define LG4FF_MODE_G25_IDX 3
32b96d23ecSMichal Malý #define LG4FF_MODE_DFGT_IDX 4
33b96d23ecSMichal Malý #define LG4FF_MODE_G27_IDX 5
3429fae1c8SSimon Wood #define LG4FF_MODE_G29_IDX 6
3529fae1c8SSimon Wood #define LG4FF_MODE_MAX_IDX 7
36b96d23ecSMichal Malý
37b96d23ecSMichal Malý #define LG4FF_MODE_NATIVE BIT(LG4FF_MODE_NATIVE_IDX)
38b96d23ecSMichal Malý #define LG4FF_MODE_DFEX BIT(LG4FF_MODE_DFEX_IDX)
39b96d23ecSMichal Malý #define LG4FF_MODE_DFP BIT(LG4FF_MODE_DFP_IDX)
40b96d23ecSMichal Malý #define LG4FF_MODE_G25 BIT(LG4FF_MODE_G25_IDX)
41b96d23ecSMichal Malý #define LG4FF_MODE_DFGT BIT(LG4FF_MODE_DFGT_IDX)
42b96d23ecSMichal Malý #define LG4FF_MODE_G27 BIT(LG4FF_MODE_G27_IDX)
4329fae1c8SSimon Wood #define LG4FF_MODE_G29 BIT(LG4FF_MODE_G29_IDX)
44b96d23ecSMichal Malý
45b96d23ecSMichal Malý #define LG4FF_DFEX_TAG "DF-EX"
46b96d23ecSMichal Malý #define LG4FF_DFEX_NAME "Driving Force / Formula EX"
47b96d23ecSMichal Malý #define LG4FF_DFP_TAG "DFP"
48b96d23ecSMichal Malý #define LG4FF_DFP_NAME "Driving Force Pro"
49b96d23ecSMichal Malý #define LG4FF_G25_TAG "G25"
50b96d23ecSMichal Malý #define LG4FF_G25_NAME "G25 Racing Wheel"
51b96d23ecSMichal Malý #define LG4FF_G27_TAG "G27"
52b96d23ecSMichal Malý #define LG4FF_G27_NAME "G27 Racing Wheel"
5329fae1c8SSimon Wood #define LG4FF_G29_TAG "G29"
5429fae1c8SSimon Wood #define LG4FF_G29_NAME "G29 Racing Wheel"
55b96d23ecSMichal Malý #define LG4FF_DFGT_TAG "DFGT"
56b96d23ecSMichal Malý #define LG4FF_DFGT_NAME "Driving Force GT"
57b96d23ecSMichal Malý
58e7c23449SMichal Malý #define LG4FF_FFEX_REV_MAJ 0x21
59e7c23449SMichal Malý #define LG4FF_FFEX_REV_MIN 0x00
60e7c23449SMichal Malý
61d0afd848SMichal Malý static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range);
62d0afd848SMichal Malý static void lg4ff_set_range_g25(struct hid_device *hid, u16 range);
6330bb75d7SMichal Malý
6472529c65SMichal Malý struct lg4ff_wheel_data {
655d9d60adSMichal Malý const u32 product_id;
66961af46fSSimon Wood u16 combine;
672a552c30SMichal Malý u16 range;
685d9d60adSMichal Malý const u16 min_range;
695d9d60adSMichal Malý const u16 max_range;
7022bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS
712a552c30SMichal Malý u8 led_state;
7222bcefdcSSimon Wood struct led_classdev *led[5];
7322bcefdcSSimon Wood #endif
745d9d60adSMichal Malý const u32 alternate_modes;
755d9d60adSMichal Malý const char * const real_tag;
765d9d60adSMichal Malý const char * const real_name;
775d9d60adSMichal Malý const u16 real_product_id;
7872529c65SMichal Malý
7930bb75d7SMichal Malý void (*set_range)(struct hid_device *hid, u16 range);
8030bb75d7SMichal Malý };
8130bb75d7SMichal Malý
8272529c65SMichal Malý struct lg4ff_device_entry {
83c918fe78SMichal Malý spinlock_t report_lock; /* Protect output HID report */
84c28abd8cSMichal Malý struct hid_report *report;
8572529c65SMichal Malý struct lg4ff_wheel_data wdata;
8672529c65SMichal Malý };
8772529c65SMichal Malý
887362cd22SMichal Malý static const signed short lg4ff_wheel_effects[] = {
8932c88cbcSSimon Wood FF_CONSTANT,
9032c88cbcSSimon Wood FF_AUTOCENTER,
9132c88cbcSSimon Wood -1
9232c88cbcSSimon Wood };
9332c88cbcSSimon Wood
94e41b3cdaSJarrad Whitaker static const signed short no_wheel_effects[] = {
95e41b3cdaSJarrad Whitaker -1
96e41b3cdaSJarrad Whitaker };
97e41b3cdaSJarrad Whitaker
987362cd22SMichal Malý struct lg4ff_wheel {
992a552c30SMichal Malý const u32 product_id;
1007362cd22SMichal Malý const signed short *ff_effects;
1012a552c30SMichal Malý const u16 min_range;
1022a552c30SMichal Malý const u16 max_range;
10330bb75d7SMichal Malý void (*set_range)(struct hid_device *hid, u16 range);
1047362cd22SMichal Malý };
1057362cd22SMichal Malý
106e7c23449SMichal Malý struct lg4ff_compat_mode_switch {
1072a552c30SMichal Malý const u8 cmd_count; /* Number of commands to send */
1082a552c30SMichal Malý const u8 cmd[];
109e7c23449SMichal Malý };
110e7c23449SMichal Malý
111e7c23449SMichal Malý struct lg4ff_wheel_ident_info {
112bbec1bd0SSimon Wood const u32 modes;
113e7c23449SMichal Malý const u16 mask;
114e7c23449SMichal Malý const u16 result;
115e7c23449SMichal Malý const u16 real_product_id;
116e7c23449SMichal Malý };
117e7c23449SMichal Malý
118b96d23ecSMichal Malý struct lg4ff_multimode_wheel {
119b96d23ecSMichal Malý const u16 product_id;
120b96d23ecSMichal Malý const u32 alternate_modes;
121b96d23ecSMichal Malý const char *real_tag;
122b96d23ecSMichal Malý const char *real_name;
123b96d23ecSMichal Malý };
124b96d23ecSMichal Malý
125b96d23ecSMichal Malý struct lg4ff_alternate_mode {
126b96d23ecSMichal Malý const u16 product_id;
127b96d23ecSMichal Malý const char *tag;
128b96d23ecSMichal Malý const char *name;
129b96d23ecSMichal Malý };
130b96d23ecSMichal Malý
1317362cd22SMichal Malý static const struct lg4ff_wheel lg4ff_devices[] = {
132e41b3cdaSJarrad Whitaker {USB_DEVICE_ID_LOGITECH_WINGMAN_FG, no_wheel_effects, 40, 180, NULL},
133560bea30SSimon Wood {USB_DEVICE_ID_LOGITECH_WINGMAN_FFG, lg4ff_wheel_effects, 40, 180, NULL},
13430bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_WHEEL, lg4ff_wheel_effects, 40, 270, NULL},
13530bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL, lg4ff_wheel_effects, 40, 270, NULL},
136d0afd848SMichal Malý {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_dfp},
137d0afd848SMichal Malý {USB_DEVICE_ID_LOGITECH_G25_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
138d0afd848SMichal Malý {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
139d0afd848SMichal Malý {USB_DEVICE_ID_LOGITECH_G27_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
14029fae1c8SSimon Wood {USB_DEVICE_ID_LOGITECH_G29_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
14130bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL},
14230bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_WII_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}
1437362cd22SMichal Malý };
1447362cd22SMichal Malý
145b96d23ecSMichal Malý static const struct lg4ff_multimode_wheel lg4ff_multimode_wheels[] = {
146b96d23ecSMichal Malý {USB_DEVICE_ID_LOGITECH_DFP_WHEEL,
147b96d23ecSMichal Malý LG4FF_MODE_NATIVE | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
148b96d23ecSMichal Malý LG4FF_DFP_TAG, LG4FF_DFP_NAME},
149b96d23ecSMichal Malý {USB_DEVICE_ID_LOGITECH_G25_WHEEL,
150b96d23ecSMichal Malý LG4FF_MODE_NATIVE | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
151b96d23ecSMichal Malý LG4FF_G25_TAG, LG4FF_G25_NAME},
152b96d23ecSMichal Malý {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL,
153b96d23ecSMichal Malý LG4FF_MODE_NATIVE | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
154b96d23ecSMichal Malý LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
155b96d23ecSMichal Malý {USB_DEVICE_ID_LOGITECH_G27_WHEEL,
156b96d23ecSMichal Malý LG4FF_MODE_NATIVE | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
157b96d23ecSMichal Malý LG4FF_G27_TAG, LG4FF_G27_NAME},
15829fae1c8SSimon Wood {USB_DEVICE_ID_LOGITECH_G29_WHEEL,
15929fae1c8SSimon Wood LG4FF_MODE_NATIVE | LG4FF_MODE_G29 | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
16029fae1c8SSimon Wood LG4FF_G29_TAG, LG4FF_G29_NAME},
161b96d23ecSMichal Malý };
162b96d23ecSMichal Malý
163b96d23ecSMichal Malý static const struct lg4ff_alternate_mode lg4ff_alternate_modes[] = {
164b96d23ecSMichal Malý [LG4FF_MODE_NATIVE_IDX] = {0, "native", ""},
165b96d23ecSMichal Malý [LG4FF_MODE_DFEX_IDX] = {USB_DEVICE_ID_LOGITECH_WHEEL, LG4FF_DFEX_TAG, LG4FF_DFEX_NAME},
166b96d23ecSMichal Malý [LG4FF_MODE_DFP_IDX] = {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, LG4FF_DFP_TAG, LG4FF_DFP_NAME},
167b96d23ecSMichal Malý [LG4FF_MODE_G25_IDX] = {USB_DEVICE_ID_LOGITECH_G25_WHEEL, LG4FF_G25_TAG, LG4FF_G25_NAME},
168b96d23ecSMichal Malý [LG4FF_MODE_DFGT_IDX] = {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
16929fae1c8SSimon Wood [LG4FF_MODE_G27_IDX] = {USB_DEVICE_ID_LOGITECH_G27_WHEEL, LG4FF_G27_TAG, LG4FF_G27_NAME},
17029fae1c8SSimon Wood [LG4FF_MODE_G29_IDX] = {USB_DEVICE_ID_LOGITECH_G29_WHEEL, LG4FF_G29_TAG, LG4FF_G29_NAME},
171b96d23ecSMichal Malý };
172b96d23ecSMichal Malý
173e7c23449SMichal Malý /* Multimode wheel identificators */
174e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_dfp_ident_info = {
175bbec1bd0SSimon Wood LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
176e7c23449SMichal Malý 0xf000,
177e7c23449SMichal Malý 0x1000,
178e7c23449SMichal Malý USB_DEVICE_ID_LOGITECH_DFP_WHEEL
17996440c8aSMichal Malý };
18096440c8aSMichal Malý
181e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_g25_ident_info = {
182bbec1bd0SSimon Wood LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
183e7c23449SMichal Malý 0xff00,
184e7c23449SMichal Malý 0x1200,
185e7c23449SMichal Malý USB_DEVICE_ID_LOGITECH_G25_WHEEL
18696440c8aSMichal Malý };
18796440c8aSMichal Malý
188e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_g27_ident_info = {
189bbec1bd0SSimon Wood LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
190e7c23449SMichal Malý 0xfff0,
191e7c23449SMichal Malý 0x1230,
192e7c23449SMichal Malý USB_DEVICE_ID_LOGITECH_G27_WHEEL
193e7c23449SMichal Malý };
194e7c23449SMichal Malý
195e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_dfgt_ident_info = {
196bbec1bd0SSimon Wood LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
197e7c23449SMichal Malý 0xff00,
198e7c23449SMichal Malý 0x1300,
199e7c23449SMichal Malý USB_DEVICE_ID_LOGITECH_DFGT_WHEEL
200e7c23449SMichal Malý };
201e7c23449SMichal Malý
20229fae1c8SSimon Wood static const struct lg4ff_wheel_ident_info lg4ff_g29_ident_info = {
20329fae1c8SSimon Wood LG4FF_MODE_G29 | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
20429fae1c8SSimon Wood 0xfff8,
20529fae1c8SSimon Wood 0x1350,
20629fae1c8SSimon Wood USB_DEVICE_ID_LOGITECH_G29_WHEEL
20729fae1c8SSimon Wood };
20829fae1c8SSimon Wood
20929fae1c8SSimon Wood static const struct lg4ff_wheel_ident_info lg4ff_g29_ident_info2 = {
21029fae1c8SSimon Wood LG4FF_MODE_G29 | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
21129fae1c8SSimon Wood 0xff00,
21229fae1c8SSimon Wood 0x8900,
21329fae1c8SSimon Wood USB_DEVICE_ID_LOGITECH_G29_WHEEL
21429fae1c8SSimon Wood };
21529fae1c8SSimon Wood
216e7c23449SMichal Malý /* Multimode wheel identification checklists */
217bbec1bd0SSimon Wood static const struct lg4ff_wheel_ident_info *lg4ff_main_checklist[] = {
21829fae1c8SSimon Wood &lg4ff_g29_ident_info,
21929fae1c8SSimon Wood &lg4ff_g29_ident_info2,
220bbec1bd0SSimon Wood &lg4ff_dfgt_ident_info,
221e7c23449SMichal Malý &lg4ff_g27_ident_info,
222e7c23449SMichal Malý &lg4ff_g25_ident_info,
223bbec1bd0SSimon Wood &lg4ff_dfp_ident_info
224e7c23449SMichal Malý };
225e7c23449SMichal Malý
226e7c23449SMichal Malý /* Compatibility mode switching commands */
227f31a2de3SMichal Malý /* EXT_CMD9 - Understood by G27 and DFGT */
228f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfex = {
229f31a2de3SMichal Malý 2,
230f31a2de3SMichal Malý {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
231f31a2de3SMichal Malý 0xf8, 0x09, 0x00, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DF-EX with detach */
232f31a2de3SMichal Malý };
233f31a2de3SMichal Malý
234f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfp = {
235f31a2de3SMichal Malý 2,
236f31a2de3SMichal Malý {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
237f31a2de3SMichal Malý 0xf8, 0x09, 0x01, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DFP with detach */
238f31a2de3SMichal Malý };
239f31a2de3SMichal Malý
240f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g25 = {
241f31a2de3SMichal Malý 2,
242f31a2de3SMichal Malý {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
243f31a2de3SMichal Malý 0xf8, 0x09, 0x02, 0x01, 0x00, 0x00, 0x00} /* Switch mode to G25 with detach */
244f31a2de3SMichal Malý };
245f31a2de3SMichal Malý
246f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfgt = {
247f31a2de3SMichal Malý 2,
248f31a2de3SMichal Malý {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
249f31a2de3SMichal Malý 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DFGT with detach */
250f31a2de3SMichal Malý };
251f31a2de3SMichal Malý
252f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g27 = {
253f31a2de3SMichal Malý 2,
254f31a2de3SMichal Malý {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
255f31a2de3SMichal Malý 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* Switch mode to G27 with detach */
256f31a2de3SMichal Malý };
257f31a2de3SMichal Malý
25829fae1c8SSimon Wood static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g29 = {
25929fae1c8SSimon Wood 2,
26029fae1c8SSimon Wood {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
26129fae1c8SSimon Wood 0xf8, 0x09, 0x05, 0x01, 0x01, 0x00, 0x00} /* Switch mode to G29 with detach */
26229fae1c8SSimon Wood };
26329fae1c8SSimon Wood
264f31a2de3SMichal Malý /* EXT_CMD1 - Understood by DFP, G25, G27 and DFGT */
265f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext01_dfp = {
26696440c8aSMichal Malý 1,
26796440c8aSMichal Malý {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
26896440c8aSMichal Malý };
26996440c8aSMichal Malý
270f31a2de3SMichal Malý /* EXT_CMD16 - Understood by G25 and G27 */
271f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext16_g25 = {
27296440c8aSMichal Malý 1,
27396440c8aSMichal Malý {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
27496440c8aSMichal Malý };
27596440c8aSMichal Malý
2762b24a960SMichal Malý /* Recalculates X axis value accordingly to currently selected range */
lg4ff_adjust_dfp_x_axis(s32 value,u16 range)2772a552c30SMichal Malý static s32 lg4ff_adjust_dfp_x_axis(s32 value, u16 range)
2782b24a960SMichal Malý {
2792a552c30SMichal Malý u16 max_range;
2802a552c30SMichal Malý s32 new_value;
2812b24a960SMichal Malý
2822b24a960SMichal Malý if (range == 900)
2832b24a960SMichal Malý return value;
2842b24a960SMichal Malý else if (range == 200)
2852b24a960SMichal Malý return value;
2862b24a960SMichal Malý else if (range < 200)
2872b24a960SMichal Malý max_range = 200;
2882b24a960SMichal Malý else
2892b24a960SMichal Malý max_range = 900;
2902b24a960SMichal Malý
2912b24a960SMichal Malý new_value = 8192 + mult_frac(value - 8192, max_range, range);
2922b24a960SMichal Malý if (new_value < 0)
2932b24a960SMichal Malý return 0;
2942b24a960SMichal Malý else if (new_value > 16383)
2952b24a960SMichal Malý return 16383;
2962b24a960SMichal Malý else
2972b24a960SMichal Malý return new_value;
2982b24a960SMichal Malý }
2992b24a960SMichal Malý
lg4ff_adjust_input_event(struct hid_device * hid,struct hid_field * field,struct hid_usage * usage,s32 value,struct lg_drv_data * drv_data)3002b24a960SMichal Malý int lg4ff_adjust_input_event(struct hid_device *hid, struct hid_field *field,
3012a552c30SMichal Malý struct hid_usage *usage, s32 value, struct lg_drv_data *drv_data)
3022b24a960SMichal Malý {
3032b24a960SMichal Malý struct lg4ff_device_entry *entry = drv_data->device_props;
3042a552c30SMichal Malý s32 new_value = 0;
3052b24a960SMichal Malý
3062b24a960SMichal Malý if (!entry) {
3072b24a960SMichal Malý hid_err(hid, "Device properties not found");
3082b24a960SMichal Malý return 0;
3092b24a960SMichal Malý }
3102b24a960SMichal Malý
31172529c65SMichal Malý switch (entry->wdata.product_id) {
3122b24a960SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
3132b24a960SMichal Malý switch (usage->code) {
3142b24a960SMichal Malý case ABS_X:
31572529c65SMichal Malý new_value = lg4ff_adjust_dfp_x_axis(value, entry->wdata.range);
3162b24a960SMichal Malý input_event(field->hidinput->input, usage->type, usage->code, new_value);
3172b24a960SMichal Malý return 1;
3182b24a960SMichal Malý default:
3192b24a960SMichal Malý return 0;
3202b24a960SMichal Malý }
3212b24a960SMichal Malý default:
3222b24a960SMichal Malý return 0;
3232b24a960SMichal Malý }
3242b24a960SMichal Malý }
3252b24a960SMichal Malý
lg4ff_raw_event(struct hid_device * hdev,struct hid_report * report,u8 * rd,int size,struct lg_drv_data * drv_data)326c832f86eSSimon Wood int lg4ff_raw_event(struct hid_device *hdev, struct hid_report *report,
327c832f86eSSimon Wood u8 *rd, int size, struct lg_drv_data *drv_data)
328c832f86eSSimon Wood {
329b456634bSSimon Wood int offset;
330c832f86eSSimon Wood struct lg4ff_device_entry *entry = drv_data->device_props;
331c832f86eSSimon Wood
332c832f86eSSimon Wood if (!entry)
333c832f86eSSimon Wood return 0;
334c832f86eSSimon Wood
335c832f86eSSimon Wood /* adjust HID report present combined pedals data */
336c832f86eSSimon Wood if (entry->wdata.combine) {
337c832f86eSSimon Wood switch (entry->wdata.product_id) {
338c832f86eSSimon Wood case USB_DEVICE_ID_LOGITECH_WHEEL:
339c832f86eSSimon Wood rd[5] = rd[3];
340c832f86eSSimon Wood rd[6] = 0x7F;
341c832f86eSSimon Wood return 1;
342e41b3cdaSJarrad Whitaker case USB_DEVICE_ID_LOGITECH_WINGMAN_FG:
343560bea30SSimon Wood case USB_DEVICE_ID_LOGITECH_WINGMAN_FFG:
344c832f86eSSimon Wood case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL:
345c832f86eSSimon Wood case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2:
346c832f86eSSimon Wood rd[4] = rd[3];
347c832f86eSSimon Wood rd[5] = 0x7F;
348c832f86eSSimon Wood return 1;
349c832f86eSSimon Wood case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
350c832f86eSSimon Wood rd[5] = rd[4];
351c832f86eSSimon Wood rd[6] = 0x7F;
352c832f86eSSimon Wood return 1;
353b456634bSSimon Wood case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
354b456634bSSimon Wood case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
355b456634bSSimon Wood offset = 5;
356b456634bSSimon Wood break;
357b456634bSSimon Wood case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
358b456634bSSimon Wood case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
359b456634bSSimon Wood offset = 6;
360b456634bSSimon Wood break;
361b456634bSSimon Wood case USB_DEVICE_ID_LOGITECH_WII_WHEEL:
362b456634bSSimon Wood offset = 3;
363b456634bSSimon Wood break;
364c832f86eSSimon Wood default:
365c832f86eSSimon Wood return 0;
366c832f86eSSimon Wood }
367b456634bSSimon Wood
368b456634bSSimon Wood /* Compute a combined axis when wheel does not supply it */
369b456634bSSimon Wood rd[offset] = (0xFF + rd[offset] - rd[offset+1]) >> 1;
370b456634bSSimon Wood rd[offset+1] = 0x7F;
371b456634bSSimon Wood return 1;
372c832f86eSSimon Wood }
373c832f86eSSimon Wood
374c832f86eSSimon Wood return 0;
375c832f86eSSimon Wood }
376c832f86eSSimon Wood
lg4ff_init_wheel_data(struct lg4ff_wheel_data * const wdata,const struct lg4ff_wheel * wheel,const struct lg4ff_multimode_wheel * mmode_wheel,const u16 real_product_id)3775d9d60adSMichal Malý static void lg4ff_init_wheel_data(struct lg4ff_wheel_data * const wdata, const struct lg4ff_wheel *wheel,
3785d9d60adSMichal Malý const struct lg4ff_multimode_wheel *mmode_wheel,
3795d9d60adSMichal Malý const u16 real_product_id)
3805d9d60adSMichal Malý {
3815d9d60adSMichal Malý u32 alternate_modes = 0;
3825d9d60adSMichal Malý const char *real_tag = NULL;
3835d9d60adSMichal Malý const char *real_name = NULL;
3845d9d60adSMichal Malý
3855d9d60adSMichal Malý if (mmode_wheel) {
3865d9d60adSMichal Malý alternate_modes = mmode_wheel->alternate_modes;
3875d9d60adSMichal Malý real_tag = mmode_wheel->real_tag;
3885d9d60adSMichal Malý real_name = mmode_wheel->real_name;
3895d9d60adSMichal Malý }
3905d9d60adSMichal Malý
3915d9d60adSMichal Malý {
3925d9d60adSMichal Malý struct lg4ff_wheel_data t_wdata = { .product_id = wheel->product_id,
3935d9d60adSMichal Malý .real_product_id = real_product_id,
394961af46fSSimon Wood .combine = 0,
3955d9d60adSMichal Malý .min_range = wheel->min_range,
3965d9d60adSMichal Malý .max_range = wheel->max_range,
3975d9d60adSMichal Malý .set_range = wheel->set_range,
3985d9d60adSMichal Malý .alternate_modes = alternate_modes,
3995d9d60adSMichal Malý .real_tag = real_tag,
4005d9d60adSMichal Malý .real_name = real_name };
4015d9d60adSMichal Malý
4025d9d60adSMichal Malý memcpy(wdata, &t_wdata, sizeof(t_wdata));
4035d9d60adSMichal Malý }
4045d9d60adSMichal Malý }
4055d9d60adSMichal Malý
lg4ff_play(struct input_dev * dev,void * data,struct ff_effect * effect)406d0afd848SMichal Malý static int lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
40732c88cbcSSimon Wood {
40832c88cbcSSimon Wood struct hid_device *hid = input_get_drvdata(dev);
409c918fe78SMichal Malý struct lg4ff_device_entry *entry;
410c918fe78SMichal Malý struct lg_drv_data *drv_data;
411c918fe78SMichal Malý unsigned long flags;
412c28abd8cSMichal Malý s32 *value;
41332c88cbcSSimon Wood int x;
41432c88cbcSSimon Wood
415c918fe78SMichal Malý drv_data = hid_get_drvdata(hid);
416c918fe78SMichal Malý if (!drv_data) {
417c918fe78SMichal Malý hid_err(hid, "Private driver data not found!\n");
418c918fe78SMichal Malý return -EINVAL;
419c918fe78SMichal Malý }
420c918fe78SMichal Malý
421c918fe78SMichal Malý entry = drv_data->device_props;
422c918fe78SMichal Malý if (!entry) {
423c918fe78SMichal Malý hid_err(hid, "Device properties not found!\n");
424c918fe78SMichal Malý return -EINVAL;
425c918fe78SMichal Malý }
426c28abd8cSMichal Malý value = entry->report->field[0]->value;
427c918fe78SMichal Malý
428a80fe5d6SMichal Malý #define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0)
42932c88cbcSSimon Wood
43032c88cbcSSimon Wood switch (effect->type) {
43132c88cbcSSimon Wood case FF_CONSTANT:
43232c88cbcSSimon Wood x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */
43332c88cbcSSimon Wood CLAMP(x);
43456930e7aSSimon Wood
435c918fe78SMichal Malý spin_lock_irqsave(&entry->report_lock, flags);
43656930e7aSSimon Wood if (x == 0x80) {
43756930e7aSSimon Wood /* De-activate force in slot-1*/
43856930e7aSSimon Wood value[0] = 0x13;
43956930e7aSSimon Wood value[1] = 0x00;
44056930e7aSSimon Wood value[2] = 0x00;
44156930e7aSSimon Wood value[3] = 0x00;
44256930e7aSSimon Wood value[4] = 0x00;
44356930e7aSSimon Wood value[5] = 0x00;
44456930e7aSSimon Wood value[6] = 0x00;
44556930e7aSSimon Wood
446c28abd8cSMichal Malý hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
447c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags);
44856930e7aSSimon Wood return 0;
44956930e7aSSimon Wood }
45056930e7aSSimon Wood
45174479ba8SMichal Malý value[0] = 0x11; /* Slot 1 */
45274479ba8SMichal Malý value[1] = 0x08;
45374479ba8SMichal Malý value[2] = x;
45474479ba8SMichal Malý value[3] = 0x80;
45574479ba8SMichal Malý value[4] = 0x00;
45674479ba8SMichal Malý value[5] = 0x00;
45774479ba8SMichal Malý value[6] = 0x00;
45832c88cbcSSimon Wood
459c28abd8cSMichal Malý hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
460c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags);
46132c88cbcSSimon Wood break;
46232c88cbcSSimon Wood }
46332c88cbcSSimon Wood return 0;
46432c88cbcSSimon Wood }
46532c88cbcSSimon Wood
4666e2de8e0SMichal Malý /* Sends default autocentering command compatible with
4676e2de8e0SMichal Malý * all wheels except Formula Force EX */
lg4ff_set_autocenter_default(struct input_dev * dev,u16 magnitude)468d0afd848SMichal Malý static void lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude)
46932c88cbcSSimon Wood {
47032c88cbcSSimon Wood struct hid_device *hid = input_get_drvdata(dev);
4716cb6d98aSColin Ian King s32 *value;
4722a552c30SMichal Malý u32 expand_a, expand_b;
4731859762eSSimon Wood struct lg4ff_device_entry *entry;
4741859762eSSimon Wood struct lg_drv_data *drv_data;
475c918fe78SMichal Malý unsigned long flags;
4761859762eSSimon Wood
4771859762eSSimon Wood drv_data = hid_get_drvdata(hid);
4781859762eSSimon Wood if (!drv_data) {
4791859762eSSimon Wood hid_err(hid, "Private driver data not found!\n");
4801859762eSSimon Wood return;
4811859762eSSimon Wood }
4821859762eSSimon Wood
4831859762eSSimon Wood entry = drv_data->device_props;
4841859762eSSimon Wood if (!entry) {
4851859762eSSimon Wood hid_err(hid, "Device properties not found!\n");
4861859762eSSimon Wood return;
4871859762eSSimon Wood }
488c28abd8cSMichal Malý value = entry->report->field[0]->value;
489f8c23156SSimon Wood
490d2c02da5SSimon Wood /* De-activate Auto-Center */
491c918fe78SMichal Malý spin_lock_irqsave(&entry->report_lock, flags);
492d2c02da5SSimon Wood if (magnitude == 0) {
493d2c02da5SSimon Wood value[0] = 0xf5;
494d2c02da5SSimon Wood value[1] = 0x00;
495d2c02da5SSimon Wood value[2] = 0x00;
496d2c02da5SSimon Wood value[3] = 0x00;
497d2c02da5SSimon Wood value[4] = 0x00;
498d2c02da5SSimon Wood value[5] = 0x00;
499d2c02da5SSimon Wood value[6] = 0x00;
500d2c02da5SSimon Wood
501c28abd8cSMichal Malý hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
502c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags);
503d2c02da5SSimon Wood return;
504d2c02da5SSimon Wood }
505d2c02da5SSimon Wood
506f8c23156SSimon Wood if (magnitude <= 0xaaaa) {
507f8c23156SSimon Wood expand_a = 0x0c * magnitude;
508f8c23156SSimon Wood expand_b = 0x80 * magnitude;
509f8c23156SSimon Wood } else {
510f8c23156SSimon Wood expand_a = (0x0c * 0xaaaa) + 0x06 * (magnitude - 0xaaaa);
511f8c23156SSimon Wood expand_b = (0x80 * 0xaaaa) + 0xff * (magnitude - 0xaaaa);
512f8c23156SSimon Wood }
51332c88cbcSSimon Wood
5141859762eSSimon Wood /* Adjust for non-MOMO wheels */
51572529c65SMichal Malý switch (entry->wdata.product_id) {
5161859762eSSimon Wood case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL:
5171859762eSSimon Wood case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2:
5181859762eSSimon Wood break;
5191859762eSSimon Wood default:
5201859762eSSimon Wood expand_a = expand_a >> 1;
5211859762eSSimon Wood break;
5221859762eSSimon Wood }
5231859762eSSimon Wood
52474479ba8SMichal Malý value[0] = 0xfe;
52574479ba8SMichal Malý value[1] = 0x0d;
526f8c23156SSimon Wood value[2] = expand_a / 0xaaaa;
527f8c23156SSimon Wood value[3] = expand_a / 0xaaaa;
528f8c23156SSimon Wood value[4] = expand_b / 0xaaaa;
52974479ba8SMichal Malý value[5] = 0x00;
53074479ba8SMichal Malý value[6] = 0x00;
53132c88cbcSSimon Wood
532c28abd8cSMichal Malý hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
533d2c02da5SSimon Wood
534d2c02da5SSimon Wood /* Activate Auto-Center */
535d2c02da5SSimon Wood value[0] = 0x14;
536d2c02da5SSimon Wood value[1] = 0x00;
537d2c02da5SSimon Wood value[2] = 0x00;
538d2c02da5SSimon Wood value[3] = 0x00;
539d2c02da5SSimon Wood value[4] = 0x00;
540d2c02da5SSimon Wood value[5] = 0x00;
541d2c02da5SSimon Wood value[6] = 0x00;
542d2c02da5SSimon Wood
543c28abd8cSMichal Malý hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
544c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags);
54532c88cbcSSimon Wood }
54632c88cbcSSimon Wood
5476e2de8e0SMichal Malý /* Sends autocentering command compatible with Formula Force EX */
lg4ff_set_autocenter_ffex(struct input_dev * dev,u16 magnitude)548d0afd848SMichal Malý static void lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude)
5496e2de8e0SMichal Malý {
5506e2de8e0SMichal Malý struct hid_device *hid = input_get_drvdata(dev);
551c918fe78SMichal Malý struct lg4ff_device_entry *entry;
552c918fe78SMichal Malý struct lg_drv_data *drv_data;
553c918fe78SMichal Malý unsigned long flags;
554c28abd8cSMichal Malý s32 *value;
5556e2de8e0SMichal Malý magnitude = magnitude * 90 / 65535;
5566e2de8e0SMichal Malý
557c918fe78SMichal Malý drv_data = hid_get_drvdata(hid);
558c918fe78SMichal Malý if (!drv_data) {
559c918fe78SMichal Malý hid_err(hid, "Private driver data not found!\n");
560c918fe78SMichal Malý return;
561c918fe78SMichal Malý }
562c918fe78SMichal Malý
563c918fe78SMichal Malý entry = drv_data->device_props;
564c918fe78SMichal Malý if (!entry) {
565c918fe78SMichal Malý hid_err(hid, "Device properties not found!\n");
566c918fe78SMichal Malý return;
567c918fe78SMichal Malý }
568c28abd8cSMichal Malý value = entry->report->field[0]->value;
569c918fe78SMichal Malý
570c918fe78SMichal Malý spin_lock_irqsave(&entry->report_lock, flags);
57174479ba8SMichal Malý value[0] = 0xfe;
57274479ba8SMichal Malý value[1] = 0x03;
57374479ba8SMichal Malý value[2] = magnitude >> 14;
57474479ba8SMichal Malý value[3] = magnitude >> 14;
57574479ba8SMichal Malý value[4] = magnitude;
57674479ba8SMichal Malý value[5] = 0x00;
57774479ba8SMichal Malý value[6] = 0x00;
5786e2de8e0SMichal Malý
579c28abd8cSMichal Malý hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
580c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags);
5816e2de8e0SMichal Malý }
5826e2de8e0SMichal Malý
58330bb75d7SMichal Malý /* Sends command to set range compatible with G25/G27/Driving Force GT */
lg4ff_set_range_g25(struct hid_device * hid,u16 range)584d0afd848SMichal Malý static void lg4ff_set_range_g25(struct hid_device *hid, u16 range)
58530bb75d7SMichal Malý {
586c918fe78SMichal Malý struct lg4ff_device_entry *entry;
587c918fe78SMichal Malý struct lg_drv_data *drv_data;
588c918fe78SMichal Malý unsigned long flags;
589c28abd8cSMichal Malý s32 *value;
59074479ba8SMichal Malý
591c918fe78SMichal Malý drv_data = hid_get_drvdata(hid);
592c918fe78SMichal Malý if (!drv_data) {
593c918fe78SMichal Malý hid_err(hid, "Private driver data not found!\n");
594c918fe78SMichal Malý return;
595c918fe78SMichal Malý }
596c918fe78SMichal Malý
597c918fe78SMichal Malý entry = drv_data->device_props;
598c918fe78SMichal Malý if (!entry) {
599c918fe78SMichal Malý hid_err(hid, "Device properties not found!\n");
600c918fe78SMichal Malý return;
601c918fe78SMichal Malý }
602c28abd8cSMichal Malý value = entry->report->field[0]->value;
60330bb75d7SMichal Malý dbg_hid("G25/G27/DFGT: setting range to %u\n", range);
60430bb75d7SMichal Malý
605c918fe78SMichal Malý spin_lock_irqsave(&entry->report_lock, flags);
60674479ba8SMichal Malý value[0] = 0xf8;
60774479ba8SMichal Malý value[1] = 0x81;
60874479ba8SMichal Malý value[2] = range & 0x00ff;
60974479ba8SMichal Malý value[3] = (range & 0xff00) >> 8;
61074479ba8SMichal Malý value[4] = 0x00;
61174479ba8SMichal Malý value[5] = 0x00;
61274479ba8SMichal Malý value[6] = 0x00;
61330bb75d7SMichal Malý
614c28abd8cSMichal Malý hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
615c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags);
61630bb75d7SMichal Malý }
61730bb75d7SMichal Malý
61830bb75d7SMichal Malý /* Sends commands to set range compatible with Driving Force Pro wheel */
lg4ff_set_range_dfp(struct hid_device * hid,u16 range)619d0afd848SMichal Malý static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range)
62030bb75d7SMichal Malý {
621c918fe78SMichal Malý struct lg4ff_device_entry *entry;
622c918fe78SMichal Malý struct lg_drv_data *drv_data;
623c918fe78SMichal Malý unsigned long flags;
624c28abd8cSMichal Malý int start_left, start_right, full_range;
625c28abd8cSMichal Malý s32 *value;
62674479ba8SMichal Malý
627c918fe78SMichal Malý drv_data = hid_get_drvdata(hid);
628c918fe78SMichal Malý if (!drv_data) {
629c918fe78SMichal Malý hid_err(hid, "Private driver data not found!\n");
630c918fe78SMichal Malý return;
631c918fe78SMichal Malý }
632c918fe78SMichal Malý
633c918fe78SMichal Malý entry = drv_data->device_props;
634c918fe78SMichal Malý if (!entry) {
635c918fe78SMichal Malý hid_err(hid, "Device properties not found!\n");
636c918fe78SMichal Malý return;
637c918fe78SMichal Malý }
638c28abd8cSMichal Malý value = entry->report->field[0]->value;
63930bb75d7SMichal Malý dbg_hid("Driving Force Pro: setting range to %u\n", range);
64030bb75d7SMichal Malý
64130bb75d7SMichal Malý /* Prepare "coarse" limit command */
642c918fe78SMichal Malý spin_lock_irqsave(&entry->report_lock, flags);
64374479ba8SMichal Malý value[0] = 0xf8;
64474479ba8SMichal Malý value[1] = 0x00; /* Set later */
64574479ba8SMichal Malý value[2] = 0x00;
64674479ba8SMichal Malý value[3] = 0x00;
64774479ba8SMichal Malý value[4] = 0x00;
64874479ba8SMichal Malý value[5] = 0x00;
64974479ba8SMichal Malý value[6] = 0x00;
65030bb75d7SMichal Malý
65130bb75d7SMichal Malý if (range > 200) {
652c28abd8cSMichal Malý value[1] = 0x03;
65330bb75d7SMichal Malý full_range = 900;
65430bb75d7SMichal Malý } else {
655c28abd8cSMichal Malý value[1] = 0x02;
65630bb75d7SMichal Malý full_range = 200;
65730bb75d7SMichal Malý }
658c28abd8cSMichal Malý hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
65930bb75d7SMichal Malý
66030bb75d7SMichal Malý /* Prepare "fine" limit command */
66174479ba8SMichal Malý value[0] = 0x81;
66274479ba8SMichal Malý value[1] = 0x0b;
66374479ba8SMichal Malý value[2] = 0x00;
66474479ba8SMichal Malý value[3] = 0x00;
66574479ba8SMichal Malý value[4] = 0x00;
66674479ba8SMichal Malý value[5] = 0x00;
66774479ba8SMichal Malý value[6] = 0x00;
66830bb75d7SMichal Malý
66930bb75d7SMichal Malý if (range == 200 || range == 900) { /* Do not apply any fine limit */
670c28abd8cSMichal Malý hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
671c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags);
67230bb75d7SMichal Malý return;
67330bb75d7SMichal Malý }
67430bb75d7SMichal Malý
67530bb75d7SMichal Malý /* Construct fine limit command */
67630bb75d7SMichal Malý start_left = (((full_range - range + 1) * 2047) / full_range);
67730bb75d7SMichal Malý start_right = 0xfff - start_left;
67830bb75d7SMichal Malý
67974479ba8SMichal Malý value[2] = start_left >> 4;
68074479ba8SMichal Malý value[3] = start_right >> 4;
68174479ba8SMichal Malý value[4] = 0xff;
68274479ba8SMichal Malý value[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
68374479ba8SMichal Malý value[6] = 0xff;
68430bb75d7SMichal Malý
685c28abd8cSMichal Malý hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
686c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags);
68730bb75d7SMichal Malý }
68830bb75d7SMichal Malý
lg4ff_get_mode_switch_command(const u16 real_product_id,const u16 target_product_id)689f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch *lg4ff_get_mode_switch_command(const u16 real_product_id, const u16 target_product_id)
690f31a2de3SMichal Malý {
691f31a2de3SMichal Malý switch (real_product_id) {
692f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
693f31a2de3SMichal Malý switch (target_product_id) {
694f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
695f31a2de3SMichal Malý return &lg4ff_mode_switch_ext01_dfp;
696f31a2de3SMichal Malý /* DFP can only be switched to its native mode */
697f31a2de3SMichal Malý default:
698f31a2de3SMichal Malý return NULL;
699f31a2de3SMichal Malý }
700f31a2de3SMichal Malý break;
701f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
702f31a2de3SMichal Malý switch (target_product_id) {
703f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
704f31a2de3SMichal Malý return &lg4ff_mode_switch_ext01_dfp;
705f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
706f31a2de3SMichal Malý return &lg4ff_mode_switch_ext16_g25;
707f31a2de3SMichal Malý /* G25 can only be switched to DFP mode or its native mode */
708f31a2de3SMichal Malý default:
709f31a2de3SMichal Malý return NULL;
710f31a2de3SMichal Malý }
711f31a2de3SMichal Malý break;
712f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
713f31a2de3SMichal Malý switch (target_product_id) {
714f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_WHEEL:
715f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_dfex;
716f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
717f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_dfp;
718f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
719f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_g25;
720f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
721f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_g27;
722f31a2de3SMichal Malý /* G27 can only be switched to DF-EX, DFP, G25 or its native mode */
723f31a2de3SMichal Malý default:
724f31a2de3SMichal Malý return NULL;
725f31a2de3SMichal Malý }
726f31a2de3SMichal Malý break;
72729fae1c8SSimon Wood case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
72829fae1c8SSimon Wood switch (target_product_id) {
72929fae1c8SSimon Wood case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
73029fae1c8SSimon Wood return &lg4ff_mode_switch_ext09_dfp;
73129fae1c8SSimon Wood case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
73229fae1c8SSimon Wood return &lg4ff_mode_switch_ext09_dfgt;
73329fae1c8SSimon Wood case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
73429fae1c8SSimon Wood return &lg4ff_mode_switch_ext09_g25;
73529fae1c8SSimon Wood case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
73629fae1c8SSimon Wood return &lg4ff_mode_switch_ext09_g27;
73729fae1c8SSimon Wood case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
73829fae1c8SSimon Wood return &lg4ff_mode_switch_ext09_g29;
73929fae1c8SSimon Wood /* G29 can only be switched to DF-EX, DFP, DFGT, G25, G27 or its native mode */
74029fae1c8SSimon Wood default:
74129fae1c8SSimon Wood return NULL;
74229fae1c8SSimon Wood }
74329fae1c8SSimon Wood break;
744f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
745f31a2de3SMichal Malý switch (target_product_id) {
746f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_WHEEL:
747f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_dfex;
748f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
749f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_dfp;
750f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
751f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_dfgt;
752f31a2de3SMichal Malý /* DFGT can only be switched to DF-EX, DFP or its native mode */
753f31a2de3SMichal Malý default:
754f31a2de3SMichal Malý return NULL;
755f31a2de3SMichal Malý }
756f31a2de3SMichal Malý break;
757f31a2de3SMichal Malý /* No other wheels have multiple modes */
758f31a2de3SMichal Malý default:
759f31a2de3SMichal Malý return NULL;
760f31a2de3SMichal Malý }
761f31a2de3SMichal Malý }
762f31a2de3SMichal Malý
lg4ff_switch_compatibility_mode(struct hid_device * hid,const struct lg4ff_compat_mode_switch * s)763e7c23449SMichal Malý static int lg4ff_switch_compatibility_mode(struct hid_device *hid, const struct lg4ff_compat_mode_switch *s)
76496440c8aSMichal Malý {
765c918fe78SMichal Malý struct lg4ff_device_entry *entry;
766c918fe78SMichal Malý struct lg_drv_data *drv_data;
767c918fe78SMichal Malý unsigned long flags;
768c28abd8cSMichal Malý s32 *value;
769e7c23449SMichal Malý u8 i;
77096440c8aSMichal Malý
771c918fe78SMichal Malý drv_data = hid_get_drvdata(hid);
772c918fe78SMichal Malý if (!drv_data) {
773c918fe78SMichal Malý hid_err(hid, "Private driver data not found!\n");
774c918fe78SMichal Malý return -EINVAL;
775c918fe78SMichal Malý }
776c918fe78SMichal Malý
777c918fe78SMichal Malý entry = drv_data->device_props;
778c918fe78SMichal Malý if (!entry) {
779c918fe78SMichal Malý hid_err(hid, "Device properties not found!\n");
780c918fe78SMichal Malý return -EINVAL;
781c918fe78SMichal Malý }
782c28abd8cSMichal Malý value = entry->report->field[0]->value;
783c918fe78SMichal Malý
784c918fe78SMichal Malý spin_lock_irqsave(&entry->report_lock, flags);
785e7c23449SMichal Malý for (i = 0; i < s->cmd_count; i++) {
786c1740d13SMichal Malý u8 j;
78796440c8aSMichal Malý
788c1740d13SMichal Malý for (j = 0; j < 7; j++)
789c1740d13SMichal Malý value[j] = s->cmd[j + (7*i)];
790c1740d13SMichal Malý
791c28abd8cSMichal Malý hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
79296440c8aSMichal Malý }
793c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags);
794c1740d13SMichal Malý hid_hw_wait(hid);
795e7c23449SMichal Malý return 0;
79696440c8aSMichal Malý }
79732c88cbcSSimon Wood
lg4ff_alternate_modes_show(struct device * dev,struct device_attribute * attr,char * buf)798b96d23ecSMichal Malý static ssize_t lg4ff_alternate_modes_show(struct device *dev, struct device_attribute *attr, char *buf)
799b96d23ecSMichal Malý {
800b96d23ecSMichal Malý struct hid_device *hid = to_hid_device(dev);
801b96d23ecSMichal Malý struct lg4ff_device_entry *entry;
802b96d23ecSMichal Malý struct lg_drv_data *drv_data;
803b96d23ecSMichal Malý ssize_t count = 0;
804b96d23ecSMichal Malý int i;
805b96d23ecSMichal Malý
806b96d23ecSMichal Malý drv_data = hid_get_drvdata(hid);
807b96d23ecSMichal Malý if (!drv_data) {
808b96d23ecSMichal Malý hid_err(hid, "Private driver data not found!\n");
809b96d23ecSMichal Malý return 0;
810b96d23ecSMichal Malý }
811b96d23ecSMichal Malý
812b96d23ecSMichal Malý entry = drv_data->device_props;
813b96d23ecSMichal Malý if (!entry) {
814b96d23ecSMichal Malý hid_err(hid, "Device properties not found!\n");
815b96d23ecSMichal Malý return 0;
816b96d23ecSMichal Malý }
817b96d23ecSMichal Malý
81872529c65SMichal Malý if (!entry->wdata.real_name) {
819b96d23ecSMichal Malý hid_err(hid, "NULL pointer to string\n");
820b96d23ecSMichal Malý return 0;
821b96d23ecSMichal Malý }
822b96d23ecSMichal Malý
823b96d23ecSMichal Malý for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
82472529c65SMichal Malý if (entry->wdata.alternate_modes & BIT(i)) {
825b96d23ecSMichal Malý /* Print tag and full name */
826b96d23ecSMichal Malý count += scnprintf(buf + count, PAGE_SIZE - count, "%s: %s",
827b96d23ecSMichal Malý lg4ff_alternate_modes[i].tag,
82872529c65SMichal Malý !lg4ff_alternate_modes[i].product_id ? entry->wdata.real_name : lg4ff_alternate_modes[i].name);
829b96d23ecSMichal Malý if (count >= PAGE_SIZE - 1)
830b96d23ecSMichal Malý return count;
831b96d23ecSMichal Malý
832b96d23ecSMichal Malý /* Mark the currently active mode with an asterisk */
83372529c65SMichal Malý if (lg4ff_alternate_modes[i].product_id == entry->wdata.product_id ||
83472529c65SMichal Malý (lg4ff_alternate_modes[i].product_id == 0 && entry->wdata.product_id == entry->wdata.real_product_id))
835b96d23ecSMichal Malý count += scnprintf(buf + count, PAGE_SIZE - count, " *\n");
836b96d23ecSMichal Malý else
837b96d23ecSMichal Malý count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
838b96d23ecSMichal Malý
839b96d23ecSMichal Malý if (count >= PAGE_SIZE - 1)
840b96d23ecSMichal Malý return count;
841b96d23ecSMichal Malý }
842b96d23ecSMichal Malý }
843b96d23ecSMichal Malý
844b96d23ecSMichal Malý return count;
845b96d23ecSMichal Malý }
846b96d23ecSMichal Malý
lg4ff_alternate_modes_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)847b96d23ecSMichal Malý static ssize_t lg4ff_alternate_modes_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
848b96d23ecSMichal Malý {
849f31a2de3SMichal Malý struct hid_device *hid = to_hid_device(dev);
850f31a2de3SMichal Malý struct lg4ff_device_entry *entry;
851f31a2de3SMichal Malý struct lg_drv_data *drv_data;
852f31a2de3SMichal Malý const struct lg4ff_compat_mode_switch *s;
853f31a2de3SMichal Malý u16 target_product_id = 0;
854f31a2de3SMichal Malý int i, ret;
855f31a2de3SMichal Malý char *lbuf;
856f31a2de3SMichal Malý
857f31a2de3SMichal Malý drv_data = hid_get_drvdata(hid);
858f31a2de3SMichal Malý if (!drv_data) {
859f31a2de3SMichal Malý hid_err(hid, "Private driver data not found!\n");
860f31a2de3SMichal Malý return -EINVAL;
861f31a2de3SMichal Malý }
862f31a2de3SMichal Malý
863f31a2de3SMichal Malý entry = drv_data->device_props;
864f31a2de3SMichal Malý if (!entry) {
865f31a2de3SMichal Malý hid_err(hid, "Device properties not found!\n");
866f31a2de3SMichal Malý return -EINVAL;
867f31a2de3SMichal Malý }
868f31a2de3SMichal Malý
869f31a2de3SMichal Malý /* Allow \n at the end of the input parameter */
870f31a2de3SMichal Malý lbuf = kasprintf(GFP_KERNEL, "%s", buf);
871f31a2de3SMichal Malý if (!lbuf)
872f31a2de3SMichal Malý return -ENOMEM;
873f31a2de3SMichal Malý
874f31a2de3SMichal Malý i = strlen(lbuf);
875*d180b649SAnastasia Belova
876*d180b649SAnastasia Belova if (i == 0) {
877*d180b649SAnastasia Belova kfree(lbuf);
878*d180b649SAnastasia Belova return -EINVAL;
879*d180b649SAnastasia Belova }
880*d180b649SAnastasia Belova
881f31a2de3SMichal Malý if (lbuf[i-1] == '\n') {
882f31a2de3SMichal Malý if (i == 1) {
883f31a2de3SMichal Malý kfree(lbuf);
884f31a2de3SMichal Malý return -EINVAL;
885f31a2de3SMichal Malý }
886f31a2de3SMichal Malý lbuf[i-1] = '\0';
887f31a2de3SMichal Malý }
888f31a2de3SMichal Malý
889f31a2de3SMichal Malý for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
890f31a2de3SMichal Malý const u16 mode_product_id = lg4ff_alternate_modes[i].product_id;
891f31a2de3SMichal Malý const char *tag = lg4ff_alternate_modes[i].tag;
892f31a2de3SMichal Malý
89372529c65SMichal Malý if (entry->wdata.alternate_modes & BIT(i)) {
894f31a2de3SMichal Malý if (!strcmp(tag, lbuf)) {
895f31a2de3SMichal Malý if (!mode_product_id)
89672529c65SMichal Malý target_product_id = entry->wdata.real_product_id;
897f31a2de3SMichal Malý else
898f31a2de3SMichal Malý target_product_id = mode_product_id;
899f31a2de3SMichal Malý break;
900f31a2de3SMichal Malý }
901f31a2de3SMichal Malý }
902f31a2de3SMichal Malý }
903f31a2de3SMichal Malý
904f31a2de3SMichal Malý if (i == LG4FF_MODE_MAX_IDX) {
905f31a2de3SMichal Malý hid_info(hid, "Requested mode \"%s\" is not supported by the device\n", lbuf);
906f31a2de3SMichal Malý kfree(lbuf);
907f31a2de3SMichal Malý return -EINVAL;
908f31a2de3SMichal Malý }
909f31a2de3SMichal Malý kfree(lbuf); /* Not needed anymore */
910f31a2de3SMichal Malý
91172529c65SMichal Malý if (target_product_id == entry->wdata.product_id) /* Nothing to do */
912f31a2de3SMichal Malý return count;
913f31a2de3SMichal Malý
914f31a2de3SMichal Malý /* Automatic switching has to be disabled for the switch to DF-EX mode to work correctly */
915f31a2de3SMichal Malý if (target_product_id == USB_DEVICE_ID_LOGITECH_WHEEL && !lg4ff_no_autoswitch) {
916f31a2de3SMichal Malý 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",
91772529c65SMichal Malý entry->wdata.real_name);
918f31a2de3SMichal Malý return -EINVAL;
919f31a2de3SMichal Malý }
920f31a2de3SMichal Malý
921f31a2de3SMichal Malý /* Take care of hardware limitations */
92272529c65SMichal Malý if ((entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_DFP_WHEEL || entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_G25_WHEEL) &&
92372529c65SMichal Malý entry->wdata.product_id > target_product_id) {
92472529c65SMichal Malý hid_info(hid, "\"%s\" cannot be switched back into \"%s\" mode\n", entry->wdata.real_name, lg4ff_alternate_modes[i].name);
925f31a2de3SMichal Malý return -EINVAL;
926f31a2de3SMichal Malý }
927f31a2de3SMichal Malý
92872529c65SMichal Malý s = lg4ff_get_mode_switch_command(entry->wdata.real_product_id, target_product_id);
929f31a2de3SMichal Malý if (!s) {
930f31a2de3SMichal Malý hid_err(hid, "Invalid target product ID %X\n", target_product_id);
931f31a2de3SMichal Malý return -EINVAL;
932f31a2de3SMichal Malý }
933f31a2de3SMichal Malý
934f31a2de3SMichal Malý ret = lg4ff_switch_compatibility_mode(hid, s);
935f31a2de3SMichal Malý return (ret == 0 ? count : ret);
936b96d23ecSMichal Malý }
937b96d23ecSMichal Malý static DEVICE_ATTR(alternate_modes, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_alternate_modes_show, lg4ff_alternate_modes_store);
938b96d23ecSMichal Malý
lg4ff_combine_show(struct device * dev,struct device_attribute * attr,char * buf)939961af46fSSimon Wood static ssize_t lg4ff_combine_show(struct device *dev, struct device_attribute *attr,
940961af46fSSimon Wood char *buf)
941961af46fSSimon Wood {
942961af46fSSimon Wood struct hid_device *hid = to_hid_device(dev);
943961af46fSSimon Wood struct lg4ff_device_entry *entry;
944961af46fSSimon Wood struct lg_drv_data *drv_data;
945961af46fSSimon Wood size_t count;
946961af46fSSimon Wood
947961af46fSSimon Wood drv_data = hid_get_drvdata(hid);
948961af46fSSimon Wood if (!drv_data) {
949961af46fSSimon Wood hid_err(hid, "Private driver data not found!\n");
950961af46fSSimon Wood return 0;
951961af46fSSimon Wood }
952961af46fSSimon Wood
953961af46fSSimon Wood entry = drv_data->device_props;
954961af46fSSimon Wood if (!entry) {
955961af46fSSimon Wood hid_err(hid, "Device properties not found!\n");
956961af46fSSimon Wood return 0;
957961af46fSSimon Wood }
958961af46fSSimon Wood
959961af46fSSimon Wood count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->wdata.combine);
960961af46fSSimon Wood return count;
961961af46fSSimon Wood }
962961af46fSSimon Wood
lg4ff_combine_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)963961af46fSSimon Wood static ssize_t lg4ff_combine_store(struct device *dev, struct device_attribute *attr,
964961af46fSSimon Wood const char *buf, size_t count)
965961af46fSSimon Wood {
966961af46fSSimon Wood struct hid_device *hid = to_hid_device(dev);
967961af46fSSimon Wood struct lg4ff_device_entry *entry;
968961af46fSSimon Wood struct lg_drv_data *drv_data;
969961af46fSSimon Wood u16 combine = simple_strtoul(buf, NULL, 10);
970961af46fSSimon Wood
971961af46fSSimon Wood drv_data = hid_get_drvdata(hid);
972961af46fSSimon Wood if (!drv_data) {
973961af46fSSimon Wood hid_err(hid, "Private driver data not found!\n");
974961af46fSSimon Wood return -EINVAL;
975961af46fSSimon Wood }
976961af46fSSimon Wood
977961af46fSSimon Wood entry = drv_data->device_props;
978961af46fSSimon Wood if (!entry) {
979961af46fSSimon Wood hid_err(hid, "Device properties not found!\n");
980961af46fSSimon Wood return -EINVAL;
981961af46fSSimon Wood }
982961af46fSSimon Wood
983961af46fSSimon Wood if (combine > 1)
984961af46fSSimon Wood combine = 1;
985961af46fSSimon Wood
986961af46fSSimon Wood entry->wdata.combine = combine;
987961af46fSSimon Wood return count;
988961af46fSSimon Wood }
989961af46fSSimon Wood static DEVICE_ATTR(combine_pedals, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_combine_show, lg4ff_combine_store);
990961af46fSSimon Wood
991fbf85e2aSMichal Malý /* Export the currently set range of the wheel */
lg4ff_range_show(struct device * dev,struct device_attribute * attr,char * buf)992fbf85e2aSMichal Malý static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr,
9932f1cec32SVivien Didelot char *buf)
99430bb75d7SMichal Malý {
99530bb75d7SMichal Malý struct hid_device *hid = to_hid_device(dev);
9963b6b17b7SMichal Malý struct lg4ff_device_entry *entry;
9973b6b17b7SMichal Malý struct lg_drv_data *drv_data;
99830bb75d7SMichal Malý size_t count;
99930bb75d7SMichal Malý
10003b6b17b7SMichal Malý drv_data = hid_get_drvdata(hid);
10013b6b17b7SMichal Malý if (!drv_data) {
10023b6b17b7SMichal Malý hid_err(hid, "Private driver data not found!\n");
10033b6b17b7SMichal Malý return 0;
100430bb75d7SMichal Malý }
10053b6b17b7SMichal Malý
10063b6b17b7SMichal Malý entry = drv_data->device_props;
10073b6b17b7SMichal Malý if (!entry) {
10083b6b17b7SMichal Malý hid_err(hid, "Device properties not found!\n");
100930bb75d7SMichal Malý return 0;
101030bb75d7SMichal Malý }
101130bb75d7SMichal Malý
101272529c65SMichal Malý count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->wdata.range);
101330bb75d7SMichal Malý return count;
101430bb75d7SMichal Malý }
101530bb75d7SMichal Malý
101630bb75d7SMichal Malý /* Set range to user specified value, call appropriate function
101730bb75d7SMichal Malý * according to the type of the wheel */
lg4ff_range_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1018fbf85e2aSMichal Malý static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr,
10192f1cec32SVivien Didelot const char *buf, size_t count)
102030bb75d7SMichal Malý {
102130bb75d7SMichal Malý struct hid_device *hid = to_hid_device(dev);
10223b6b17b7SMichal Malý struct lg4ff_device_entry *entry;
10233b6b17b7SMichal Malý struct lg_drv_data *drv_data;
10242a552c30SMichal Malý u16 range = simple_strtoul(buf, NULL, 10);
102530bb75d7SMichal Malý
10263b6b17b7SMichal Malý drv_data = hid_get_drvdata(hid);
10273b6b17b7SMichal Malý if (!drv_data) {
10283b6b17b7SMichal Malý hid_err(hid, "Private driver data not found!\n");
102929ff6657SSimon Wood return -EINVAL;
103030bb75d7SMichal Malý }
10313b6b17b7SMichal Malý
10323b6b17b7SMichal Malý entry = drv_data->device_props;
10333b6b17b7SMichal Malý if (!entry) {
10343b6b17b7SMichal Malý hid_err(hid, "Device properties not found!\n");
103529ff6657SSimon Wood return -EINVAL;
103630bb75d7SMichal Malý }
103730bb75d7SMichal Malý
103830bb75d7SMichal Malý if (range == 0)
103972529c65SMichal Malý range = entry->wdata.max_range;
104030bb75d7SMichal Malý
104130bb75d7SMichal Malý /* Check if the wheel supports range setting
104230bb75d7SMichal Malý * and that the range is within limits for the wheel */
104372529c65SMichal Malý if (entry->wdata.set_range && range >= entry->wdata.min_range && range <= entry->wdata.max_range) {
104472529c65SMichal Malý entry->wdata.set_range(hid, range);
104572529c65SMichal Malý entry->wdata.range = range;
104630bb75d7SMichal Malý }
104730bb75d7SMichal Malý
104830bb75d7SMichal Malý return count;
104930bb75d7SMichal Malý }
1050fbf85e2aSMichal Malý static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_range_show, lg4ff_range_store);
105130bb75d7SMichal Malý
lg4ff_real_id_show(struct device * dev,struct device_attribute * attr,char * buf)1052b96d23ecSMichal Malý static ssize_t lg4ff_real_id_show(struct device *dev, struct device_attribute *attr, char *buf)
1053b96d23ecSMichal Malý {
1054b96d23ecSMichal Malý struct hid_device *hid = to_hid_device(dev);
1055b96d23ecSMichal Malý struct lg4ff_device_entry *entry;
1056b96d23ecSMichal Malý struct lg_drv_data *drv_data;
1057b96d23ecSMichal Malý size_t count;
1058b96d23ecSMichal Malý
1059b96d23ecSMichal Malý drv_data = hid_get_drvdata(hid);
1060b96d23ecSMichal Malý if (!drv_data) {
1061b96d23ecSMichal Malý hid_err(hid, "Private driver data not found!\n");
1062b96d23ecSMichal Malý return 0;
1063b96d23ecSMichal Malý }
1064b96d23ecSMichal Malý
1065b96d23ecSMichal Malý entry = drv_data->device_props;
1066b96d23ecSMichal Malý if (!entry) {
1067b96d23ecSMichal Malý hid_err(hid, "Device properties not found!\n");
1068b96d23ecSMichal Malý return 0;
1069b96d23ecSMichal Malý }
1070b96d23ecSMichal Malý
107172529c65SMichal Malý if (!entry->wdata.real_tag || !entry->wdata.real_name) {
1072b96d23ecSMichal Malý hid_err(hid, "NULL pointer to string\n");
1073b96d23ecSMichal Malý return 0;
1074b96d23ecSMichal Malý }
1075b96d23ecSMichal Malý
107672529c65SMichal Malý count = scnprintf(buf, PAGE_SIZE, "%s: %s\n", entry->wdata.real_tag, entry->wdata.real_name);
1077b96d23ecSMichal Malý return count;
1078b96d23ecSMichal Malý }
1079b96d23ecSMichal Malý
lg4ff_real_id_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1080b96d23ecSMichal Malý static ssize_t lg4ff_real_id_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1081b96d23ecSMichal Malý {
1082b96d23ecSMichal Malý /* Real ID is a read-only value */
1083b96d23ecSMichal Malý return -EPERM;
1084b96d23ecSMichal Malý }
1085b96d23ecSMichal Malý static DEVICE_ATTR(real_id, S_IRUGO, lg4ff_real_id_show, lg4ff_real_id_store);
1086b96d23ecSMichal Malý
108722bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS
lg4ff_set_leds(struct hid_device * hid,u8 leds)10882a552c30SMichal Malý static void lg4ff_set_leds(struct hid_device *hid, u8 leds)
108922bcefdcSSimon Wood {
1090c918fe78SMichal Malý struct lg_drv_data *drv_data;
1091c918fe78SMichal Malý struct lg4ff_device_entry *entry;
1092c918fe78SMichal Malý unsigned long flags;
1093c28abd8cSMichal Malý s32 *value;
109422bcefdcSSimon Wood
1095c918fe78SMichal Malý drv_data = hid_get_drvdata(hid);
1096c918fe78SMichal Malý if (!drv_data) {
1097c918fe78SMichal Malý hid_err(hid, "Private driver data not found!\n");
1098c918fe78SMichal Malý return;
1099c918fe78SMichal Malý }
1100c918fe78SMichal Malý
1101c918fe78SMichal Malý entry = drv_data->device_props;
1102c918fe78SMichal Malý if (!entry) {
1103c918fe78SMichal Malý hid_err(hid, "Device properties not found!\n");
1104c918fe78SMichal Malý return;
1105c918fe78SMichal Malý }
1106c28abd8cSMichal Malý value = entry->report->field[0]->value;
1107c918fe78SMichal Malý
1108c918fe78SMichal Malý spin_lock_irqsave(&entry->report_lock, flags);
110974479ba8SMichal Malý value[0] = 0xf8;
111074479ba8SMichal Malý value[1] = 0x12;
111174479ba8SMichal Malý value[2] = leds;
111274479ba8SMichal Malý value[3] = 0x00;
111374479ba8SMichal Malý value[4] = 0x00;
111474479ba8SMichal Malý value[5] = 0x00;
111574479ba8SMichal Malý value[6] = 0x00;
1116c28abd8cSMichal Malý hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
1117c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags);
111822bcefdcSSimon Wood }
111922bcefdcSSimon Wood
lg4ff_led_set_brightness(struct led_classdev * led_cdev,enum led_brightness value)112022bcefdcSSimon Wood static void lg4ff_led_set_brightness(struct led_classdev *led_cdev,
112122bcefdcSSimon Wood enum led_brightness value)
112222bcefdcSSimon Wood {
112322bcefdcSSimon Wood struct device *dev = led_cdev->dev->parent;
1124ee79a8f8SGeliang Tang struct hid_device *hid = to_hid_device(dev);
11254629fd16SAxel Lin struct lg_drv_data *drv_data = hid_get_drvdata(hid);
112622bcefdcSSimon Wood struct lg4ff_device_entry *entry;
112722bcefdcSSimon Wood int i, state = 0;
112822bcefdcSSimon Wood
112922bcefdcSSimon Wood if (!drv_data) {
113022bcefdcSSimon Wood hid_err(hid, "Device data not found.");
113122bcefdcSSimon Wood return;
113222bcefdcSSimon Wood }
113322bcefdcSSimon Wood
1134371a1d9eSMichal Malý entry = drv_data->device_props;
113522bcefdcSSimon Wood
113622bcefdcSSimon Wood if (!entry) {
113722bcefdcSSimon Wood hid_err(hid, "Device properties not found.");
113822bcefdcSSimon Wood return;
113922bcefdcSSimon Wood }
114022bcefdcSSimon Wood
114122bcefdcSSimon Wood for (i = 0; i < 5; i++) {
114272529c65SMichal Malý if (led_cdev != entry->wdata.led[i])
114322bcefdcSSimon Wood continue;
114472529c65SMichal Malý state = (entry->wdata.led_state >> i) & 1;
114522bcefdcSSimon Wood if (value == LED_OFF && state) {
114672529c65SMichal Malý entry->wdata.led_state &= ~(1 << i);
114772529c65SMichal Malý lg4ff_set_leds(hid, entry->wdata.led_state);
114822bcefdcSSimon Wood } else if (value != LED_OFF && !state) {
114972529c65SMichal Malý entry->wdata.led_state |= 1 << i;
115072529c65SMichal Malý lg4ff_set_leds(hid, entry->wdata.led_state);
115122bcefdcSSimon Wood }
115222bcefdcSSimon Wood break;
115322bcefdcSSimon Wood }
115422bcefdcSSimon Wood }
115522bcefdcSSimon Wood
lg4ff_led_get_brightness(struct led_classdev * led_cdev)115622bcefdcSSimon Wood static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev)
115722bcefdcSSimon Wood {
115822bcefdcSSimon Wood struct device *dev = led_cdev->dev->parent;
1159ee79a8f8SGeliang Tang struct hid_device *hid = to_hid_device(dev);
11604629fd16SAxel Lin struct lg_drv_data *drv_data = hid_get_drvdata(hid);
116122bcefdcSSimon Wood struct lg4ff_device_entry *entry;
116222bcefdcSSimon Wood int i, value = 0;
116322bcefdcSSimon Wood
116422bcefdcSSimon Wood if (!drv_data) {
116522bcefdcSSimon Wood hid_err(hid, "Device data not found.");
116622bcefdcSSimon Wood return LED_OFF;
116722bcefdcSSimon Wood }
116822bcefdcSSimon Wood
1169371a1d9eSMichal Malý entry = drv_data->device_props;
117022bcefdcSSimon Wood
117122bcefdcSSimon Wood if (!entry) {
117222bcefdcSSimon Wood hid_err(hid, "Device properties not found.");
117322bcefdcSSimon Wood return LED_OFF;
117422bcefdcSSimon Wood }
117522bcefdcSSimon Wood
117622bcefdcSSimon Wood for (i = 0; i < 5; i++)
117772529c65SMichal Malý if (led_cdev == entry->wdata.led[i]) {
117872529c65SMichal Malý value = (entry->wdata.led_state >> i) & 1;
117922bcefdcSSimon Wood break;
118022bcefdcSSimon Wood }
118122bcefdcSSimon Wood
118222bcefdcSSimon Wood return value ? LED_FULL : LED_OFF;
118322bcefdcSSimon Wood }
118422bcefdcSSimon Wood #endif
118522bcefdcSSimon Wood
lg4ff_identify_multimode_wheel(struct hid_device * hid,const u16 reported_product_id,const u16 bcdDevice)1186e7c23449SMichal Malý static u16 lg4ff_identify_multimode_wheel(struct hid_device *hid, const u16 reported_product_id, const u16 bcdDevice)
1187e7c23449SMichal Malý {
1188bbec1bd0SSimon Wood u32 current_mode;
1189bbec1bd0SSimon Wood int i;
1190e7c23449SMichal Malý
1191bbec1bd0SSimon Wood /* identify current mode from USB PID */
1192bbec1bd0SSimon Wood for (i = 1; i < ARRAY_SIZE(lg4ff_alternate_modes); i++) {
1193bbec1bd0SSimon Wood dbg_hid("Testing whether PID is %X\n", lg4ff_alternate_modes[i].product_id);
1194bbec1bd0SSimon Wood if (reported_product_id == lg4ff_alternate_modes[i].product_id)
1195e7c23449SMichal Malý break;
1196e7c23449SMichal Malý }
1197e7c23449SMichal Malý
1198bbec1bd0SSimon Wood if (i == ARRAY_SIZE(lg4ff_alternate_modes))
1199bbec1bd0SSimon Wood return 0;
1200e7c23449SMichal Malý
1201bbec1bd0SSimon Wood current_mode = BIT(i);
1202bbec1bd0SSimon Wood
1203bbec1bd0SSimon Wood for (i = 0; i < ARRAY_SIZE(lg4ff_main_checklist); i++) {
1204bbec1bd0SSimon Wood const u16 mask = lg4ff_main_checklist[i]->mask;
1205bbec1bd0SSimon Wood const u16 result = lg4ff_main_checklist[i]->result;
1206bbec1bd0SSimon Wood const u16 real_product_id = lg4ff_main_checklist[i]->real_product_id;
1207bbec1bd0SSimon Wood
1208bbec1bd0SSimon Wood if ((current_mode & lg4ff_main_checklist[i]->modes) && \
1209bbec1bd0SSimon Wood (bcdDevice & mask) == result) {
1210e7c23449SMichal Malý dbg_hid("Found wheel with real PID %X whose reported PID is %X\n", real_product_id, reported_product_id);
1211e7c23449SMichal Malý return real_product_id;
1212e7c23449SMichal Malý }
1213e7c23449SMichal Malý }
1214e7c23449SMichal Malý
1215f31a2de3SMichal Malý /* No match found. This is either Driving Force or an unknown
1216f31a2de3SMichal Malý * wheel model, do not touch it */
1217e7c23449SMichal Malý dbg_hid("Wheel with bcdDevice %X was not recognized as multimode wheel, leaving in its current mode\n", bcdDevice);
1218e7c23449SMichal Malý return 0;
1219e7c23449SMichal Malý }
1220e7c23449SMichal Malý
lg4ff_handle_multimode_wheel(struct hid_device * hid,u16 * real_product_id,const u16 bcdDevice)1221e7c23449SMichal Malý static int lg4ff_handle_multimode_wheel(struct hid_device *hid, u16 *real_product_id, const u16 bcdDevice)
1222e7c23449SMichal Malý {
1223e7c23449SMichal Malý const u16 reported_product_id = hid->product;
1224e7c23449SMichal Malý int ret;
1225e7c23449SMichal Malý
1226e7c23449SMichal Malý *real_product_id = lg4ff_identify_multimode_wheel(hid, reported_product_id, bcdDevice);
1227e7c23449SMichal Malý /* Probed wheel is not a multimode wheel */
1228e7c23449SMichal Malý if (!*real_product_id) {
1229e7c23449SMichal Malý *real_product_id = reported_product_id;
1230e7c23449SMichal Malý dbg_hid("Wheel is not a multimode wheel\n");
1231e7c23449SMichal Malý return LG4FF_MMODE_NOT_MULTIMODE;
1232e7c23449SMichal Malý }
1233e7c23449SMichal Malý
1234e7c23449SMichal Malý /* Switch from "Driving Force" mode to native mode automatically.
1235e7c23449SMichal Malý * Otherwise keep the wheel in its current mode */
1236e7c23449SMichal Malý if (reported_product_id == USB_DEVICE_ID_LOGITECH_WHEEL &&
1237a54dc779SMichal Malý reported_product_id != *real_product_id &&
1238a54dc779SMichal Malý !lg4ff_no_autoswitch) {
1239f31a2de3SMichal Malý const struct lg4ff_compat_mode_switch *s = lg4ff_get_mode_switch_command(*real_product_id, *real_product_id);
1240e7c23449SMichal Malý
1241f31a2de3SMichal Malý if (!s) {
1242e7c23449SMichal Malý hid_err(hid, "Invalid product id %X\n", *real_product_id);
1243b96d23ecSMichal Malý return LG4FF_MMODE_NOT_MULTIMODE;
1244e7c23449SMichal Malý }
1245e7c23449SMichal Malý
1246e7c23449SMichal Malý ret = lg4ff_switch_compatibility_mode(hid, s);
1247e7c23449SMichal Malý if (ret) {
1248e7c23449SMichal Malý /* Wheel could not have been switched to native mode,
1249e7c23449SMichal Malý * leave it in "Driving Force" mode and continue */
1250e7c23449SMichal Malý hid_err(hid, "Unable to switch wheel mode, errno %d\n", ret);
1251b96d23ecSMichal Malý return LG4FF_MMODE_IS_MULTIMODE;
1252e7c23449SMichal Malý }
1253e7c23449SMichal Malý return LG4FF_MMODE_SWITCHED;
1254e7c23449SMichal Malý }
1255e7c23449SMichal Malý
1256b96d23ecSMichal Malý return LG4FF_MMODE_IS_MULTIMODE;
1257e7c23449SMichal Malý }
1258e7c23449SMichal Malý
1259e7c23449SMichal Malý
lg4ff_init(struct hid_device * hid)126032c88cbcSSimon Wood int lg4ff_init(struct hid_device *hid)
126132c88cbcSSimon Wood {
1262d9d4b1e4SAlan Stern struct hid_input *hidinput;
1263d9d4b1e4SAlan Stern struct input_dev *dev;
1264c28abd8cSMichal Malý struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
1265c28abd8cSMichal Malý struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
1266e7c23449SMichal Malý const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
1267e7c23449SMichal Malý const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
12685d9d60adSMichal Malý const struct lg4ff_multimode_wheel *mmode_wheel = NULL;
126930bb75d7SMichal Malý struct lg4ff_device_entry *entry;
12703b6b17b7SMichal Malý struct lg_drv_data *drv_data;
1271b96d23ecSMichal Malý int error, i, j;
1272b96d23ecSMichal Malý int mmode_ret, mmode_idx = -1;
1273e7c23449SMichal Malý u16 real_product_id;
127432c88cbcSSimon Wood
1275d9d4b1e4SAlan Stern if (list_empty(&hid->inputs)) {
1276d9d4b1e4SAlan Stern hid_err(hid, "no inputs found\n");
1277d9d4b1e4SAlan Stern return -ENODEV;
1278d9d4b1e4SAlan Stern }
1279d9d4b1e4SAlan Stern hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1280d9d4b1e4SAlan Stern dev = hidinput->input;
1281d9d4b1e4SAlan Stern
128232c88cbcSSimon Wood /* Check that the report looks ok */
12830fb6bd06SKees Cook if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
128432c88cbcSSimon Wood return -1;
128532c88cbcSSimon Wood
128672529c65SMichal Malý drv_data = hid_get_drvdata(hid);
128772529c65SMichal Malý if (!drv_data) {
128872529c65SMichal Malý hid_err(hid, "Cannot add device, private driver data not allocated\n");
128972529c65SMichal Malý return -1;
129072529c65SMichal Malý }
129172529c65SMichal Malý entry = kzalloc(sizeof(*entry), GFP_KERNEL);
129272529c65SMichal Malý if (!entry)
129372529c65SMichal Malý return -ENOMEM;
1294c918fe78SMichal Malý spin_lock_init(&entry->report_lock);
1295c28abd8cSMichal Malý entry->report = report;
129672529c65SMichal Malý drv_data->device_props = entry;
129772529c65SMichal Malý
1298e7c23449SMichal Malý /* Check if a multimode wheel has been connected and
1299e7c23449SMichal Malý * handle it appropriately */
1300b96d23ecSMichal Malý mmode_ret = lg4ff_handle_multimode_wheel(hid, &real_product_id, bcdDevice);
1301e7c23449SMichal Malý
1302e7c23449SMichal Malý /* Wheel has been told to switch to native mode. There is no point in going on
1303e7c23449SMichal Malý * with the initialization as the wheel will do a USB reset when it switches mode
1304e7c23449SMichal Malý */
1305b96d23ecSMichal Malý if (mmode_ret == LG4FF_MMODE_SWITCHED)
1306e7c23449SMichal Malý return 0;
130772529c65SMichal Malý else if (mmode_ret < 0) {
130872529c65SMichal Malý hid_err(hid, "Unable to switch device mode during initialization, errno %d\n", mmode_ret);
130972529c65SMichal Malý error = mmode_ret;
131072529c65SMichal Malý goto err_init;
131172529c65SMichal Malý }
1312e7c23449SMichal Malý
13137362cd22SMichal Malý /* Check what wheel has been connected */
13147362cd22SMichal Malý for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
13157362cd22SMichal Malý if (hid->product == lg4ff_devices[i].product_id) {
13167362cd22SMichal Malý dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
13177362cd22SMichal Malý break;
13187362cd22SMichal Malý }
13197362cd22SMichal Malý }
13207362cd22SMichal Malý
13217362cd22SMichal Malý if (i == ARRAY_SIZE(lg4ff_devices)) {
13229c2a6bd1SMichal Malý hid_err(hid, "This device is flagged to be handled by the lg4ff module but this module does not know how to handle it. "
13239c2a6bd1SMichal Malý "Please report this as a bug to LKML, Simon Wood <simon@mungewell.org> or "
13249c2a6bd1SMichal Malý "Michal Maly <madcatxster@devoid-pointer.net>\n");
132572529c65SMichal Malý error = -1;
132672529c65SMichal Malý goto err_init;
13277362cd22SMichal Malý }
13287362cd22SMichal Malý
1329b96d23ecSMichal Malý if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1330b96d23ecSMichal Malý for (mmode_idx = 0; mmode_idx < ARRAY_SIZE(lg4ff_multimode_wheels); mmode_idx++) {
1331b96d23ecSMichal Malý if (real_product_id == lg4ff_multimode_wheels[mmode_idx].product_id)
1332b96d23ecSMichal Malý break;
1333b96d23ecSMichal Malý }
1334b96d23ecSMichal Malý
1335b96d23ecSMichal Malý if (mmode_idx == ARRAY_SIZE(lg4ff_multimode_wheels)) {
1336b96d23ecSMichal Malý hid_err(hid, "Device product ID %X is not listed as a multimode wheel", real_product_id);
133772529c65SMichal Malý error = -1;
133872529c65SMichal Malý goto err_init;
1339b96d23ecSMichal Malý }
1340b96d23ecSMichal Malý }
1341b96d23ecSMichal Malý
13427362cd22SMichal Malý /* Set supported force feedback capabilities */
13437362cd22SMichal Malý for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
13447362cd22SMichal Malý set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
134532c88cbcSSimon Wood
1346d0afd848SMichal Malý error = input_ff_create_memless(dev, NULL, lg4ff_play);
134732c88cbcSSimon Wood
134832c88cbcSSimon Wood if (error)
134972529c65SMichal Malý goto err_init;
135032c88cbcSSimon Wood
13515d9d60adSMichal Malý /* Initialize device properties */
1352b96d23ecSMichal Malý if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1353b96d23ecSMichal Malý BUG_ON(mmode_idx == -1);
13545d9d60adSMichal Malý mmode_wheel = &lg4ff_multimode_wheels[mmode_idx];
1355b96d23ecSMichal Malý }
13565d9d60adSMichal Malý lg4ff_init_wheel_data(&entry->wdata, &lg4ff_devices[i], mmode_wheel, real_product_id);
135730bb75d7SMichal Malý
1358114a55cfSSimon Wood /* Check if autocentering is available and
1359114a55cfSSimon Wood * set the centering force to zero by default */
1360114a55cfSSimon Wood if (test_bit(FF_AUTOCENTER, dev->ffbit)) {
1361e7c23449SMichal Malý /* Formula Force EX expects different autocentering command */
1362e7c23449SMichal Malý if ((bcdDevice >> 8) == LG4FF_FFEX_REV_MAJ &&
1363e7c23449SMichal Malý (bcdDevice & 0xff) == LG4FF_FFEX_REV_MIN)
1364d0afd848SMichal Malý dev->ff->set_autocenter = lg4ff_set_autocenter_ffex;
1365114a55cfSSimon Wood else
1366d0afd848SMichal Malý dev->ff->set_autocenter = lg4ff_set_autocenter_default;
1367114a55cfSSimon Wood
1368114a55cfSSimon Wood dev->ff->set_autocenter(dev, 0);
1369114a55cfSSimon Wood }
1370114a55cfSSimon Wood
137130bb75d7SMichal Malý /* Create sysfs interface */
1372961af46fSSimon Wood error = device_create_file(&hid->dev, &dev_attr_combine_pedals);
1373961af46fSSimon Wood if (error)
1374961af46fSSimon Wood hid_warn(hid, "Unable to create sysfs interface for \"combine\", errno %d\n", error);
137530bb75d7SMichal Malý error = device_create_file(&hid->dev, &dev_attr_range);
137630bb75d7SMichal Malý if (error)
1377d61a70ecSMichal Malý hid_warn(hid, "Unable to create sysfs interface for \"range\", errno %d\n", error);
1378b96d23ecSMichal Malý if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1379b96d23ecSMichal Malý error = device_create_file(&hid->dev, &dev_attr_real_id);
1380b96d23ecSMichal Malý if (error)
1381d61a70ecSMichal Malý hid_warn(hid, "Unable to create sysfs interface for \"real_id\", errno %d\n", error);
1382b96d23ecSMichal Malý error = device_create_file(&hid->dev, &dev_attr_alternate_modes);
1383b96d23ecSMichal Malý if (error)
1384d61a70ecSMichal Malý hid_warn(hid, "Unable to create sysfs interface for \"alternate_modes\", errno %d\n", error);
1385b96d23ecSMichal Malý }
138630bb75d7SMichal Malý dbg_hid("sysfs interface created\n");
138730bb75d7SMichal Malý
138830bb75d7SMichal Malý /* Set the maximum range to start with */
138972529c65SMichal Malý entry->wdata.range = entry->wdata.max_range;
139072529c65SMichal Malý if (entry->wdata.set_range)
139172529c65SMichal Malý entry->wdata.set_range(hid, entry->wdata.range);
139230bb75d7SMichal Malý
139322bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS
139429fae1c8SSimon Wood /* register led subsystem - G27/G29 only */
139572529c65SMichal Malý entry->wdata.led_state = 0;
139622bcefdcSSimon Wood for (j = 0; j < 5; j++)
139772529c65SMichal Malý entry->wdata.led[j] = NULL;
139822bcefdcSSimon Wood
139929fae1c8SSimon Wood if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL ||
140029fae1c8SSimon Wood lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G29_WHEEL) {
140122bcefdcSSimon Wood struct led_classdev *led;
140222bcefdcSSimon Wood size_t name_sz;
140322bcefdcSSimon Wood char *name;
140422bcefdcSSimon Wood
140522bcefdcSSimon Wood lg4ff_set_leds(hid, 0);
140622bcefdcSSimon Wood
140722bcefdcSSimon Wood name_sz = strlen(dev_name(&hid->dev)) + 8;
140822bcefdcSSimon Wood
140922bcefdcSSimon Wood for (j = 0; j < 5; j++) {
141022bcefdcSSimon Wood led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
141122bcefdcSSimon Wood if (!led) {
141222bcefdcSSimon Wood hid_err(hid, "can't allocate memory for LED %d\n", j);
141372529c65SMichal Malý goto err_leds;
141422bcefdcSSimon Wood }
141522bcefdcSSimon Wood
141622bcefdcSSimon Wood name = (void *)(&led[1]);
141722bcefdcSSimon Wood snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1);
141822bcefdcSSimon Wood led->name = name;
141922bcefdcSSimon Wood led->brightness = 0;
142022bcefdcSSimon Wood led->max_brightness = 1;
142122bcefdcSSimon Wood led->brightness_get = lg4ff_led_get_brightness;
142222bcefdcSSimon Wood led->brightness_set = lg4ff_led_set_brightness;
142322bcefdcSSimon Wood
142472529c65SMichal Malý entry->wdata.led[j] = led;
142522bcefdcSSimon Wood error = led_classdev_register(&hid->dev, led);
142622bcefdcSSimon Wood
142722bcefdcSSimon Wood if (error) {
142822bcefdcSSimon Wood hid_err(hid, "failed to register LED %d. Aborting.\n", j);
142972529c65SMichal Malý err_leds:
143022bcefdcSSimon Wood /* Deregister LEDs (if any) */
143122bcefdcSSimon Wood for (j = 0; j < 5; j++) {
143272529c65SMichal Malý led = entry->wdata.led[j];
143372529c65SMichal Malý entry->wdata.led[j] = NULL;
143422bcefdcSSimon Wood if (!led)
143522bcefdcSSimon Wood continue;
143622bcefdcSSimon Wood led_classdev_unregister(led);
143722bcefdcSSimon Wood kfree(led);
143822bcefdcSSimon Wood }
143922bcefdcSSimon Wood goto out; /* Let the driver continue without LEDs */
144022bcefdcSSimon Wood }
144122bcefdcSSimon Wood }
144222bcefdcSSimon Wood }
144322bcefdcSSimon Wood out:
1444c6e6dc87SJiri Kosina #endif
144564013800SSimon Wood hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n");
144632c88cbcSSimon Wood return 0;
144772529c65SMichal Malý
144872529c65SMichal Malý err_init:
144972529c65SMichal Malý drv_data->device_props = NULL;
145072529c65SMichal Malý kfree(entry);
145172529c65SMichal Malý return error;
145232c88cbcSSimon Wood }
145332c88cbcSSimon Wood
lg4ff_deinit(struct hid_device * hid)145430bb75d7SMichal Malý int lg4ff_deinit(struct hid_device *hid)
145530bb75d7SMichal Malý {
145630bb75d7SMichal Malý struct lg4ff_device_entry *entry;
14573b6b17b7SMichal Malý struct lg_drv_data *drv_data;
14586a2e176bSMichal Malý
14593b6b17b7SMichal Malý drv_data = hid_get_drvdata(hid);
14603b6b17b7SMichal Malý if (!drv_data) {
14613b6b17b7SMichal Malý hid_err(hid, "Error while deinitializing device, no private driver data.\n");
146230bb75d7SMichal Malý return -1;
146330bb75d7SMichal Malý }
14643b6b17b7SMichal Malý entry = drv_data->device_props;
1465e7c23449SMichal Malý if (!entry)
1466e7c23449SMichal Malý goto out; /* Nothing more to do */
1467e7c23449SMichal Malý
1468b96d23ecSMichal Malý /* Multimode devices will have at least the "MODE_NATIVE" bit set */
146972529c65SMichal Malý if (entry->wdata.alternate_modes) {
1470b96d23ecSMichal Malý device_remove_file(&hid->dev, &dev_attr_real_id);
1471b96d23ecSMichal Malý device_remove_file(&hid->dev, &dev_attr_alternate_modes);
1472b96d23ecSMichal Malý }
1473b96d23ecSMichal Malý
1474961af46fSSimon Wood device_remove_file(&hid->dev, &dev_attr_combine_pedals);
147572529c65SMichal Malý device_remove_file(&hid->dev, &dev_attr_range);
147622bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS
147722bcefdcSSimon Wood {
147822bcefdcSSimon Wood int j;
147922bcefdcSSimon Wood struct led_classdev *led;
148022bcefdcSSimon Wood
148122bcefdcSSimon Wood /* Deregister LEDs (if any) */
148222bcefdcSSimon Wood for (j = 0; j < 5; j++) {
148322bcefdcSSimon Wood
148472529c65SMichal Malý led = entry->wdata.led[j];
148572529c65SMichal Malý entry->wdata.led[j] = NULL;
148622bcefdcSSimon Wood if (!led)
148722bcefdcSSimon Wood continue;
148822bcefdcSSimon Wood led_classdev_unregister(led);
148922bcefdcSSimon Wood kfree(led);
149022bcefdcSSimon Wood }
149122bcefdcSSimon Wood }
149222bcefdcSSimon Wood #endif
1493b211a638SMichal Malý drv_data->device_props = NULL;
149422bcefdcSSimon Wood
14953b6b17b7SMichal Malý kfree(entry);
1496e7c23449SMichal Malý out:
149730bb75d7SMichal Malý dbg_hid("Device successfully unregistered\n");
149830bb75d7SMichal Malý return 0;
149930bb75d7SMichal Malý }
1500