blob: 1b109a5cf922ddd6ab9ff743cf29193bf5fb6e91 [file] [log] [blame]
Simon Wood32c88cb2010-09-22 13:19:42 +02001/*
Simon Wood64013802012-04-21 05:41:16 -07002 * Force feedback support for Logitech Gaming Wheels
Simon Wood32c88cb2010-09-22 13:19:42 +02003 *
Simon Wood64013802012-04-21 05:41:16 -07004 * Including G27, G25, DFP, DFGT, FFEX, Momo, Momo2 &
5 * Speed Force Wireless (WiiWheel)
Simon Wood32c88cb2010-09-22 13:19:42 +02006 *
7 * Copyright (c) 2010 Simon Wood <simon@mungewell.org>
8 */
9
10/*
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26
27#include <linux/input.h>
28#include <linux/usb.h>
29#include <linux/hid.h>
30
31#include "usbhid/usbhid.h"
32#include "hid-lg.h"
Michal Malýa54dc7792015-02-18 17:59:22 +010033#include "hid-lg4ff.h"
Michal Malý7362cd22011-08-04 16:16:09 +020034#include "hid-ids.h"
Simon Wood32c88cb2010-09-22 13:19:42 +020035
Michal Malýb96d23e2015-02-18 17:59:21 +010036#define LG4FF_MMODE_IS_MULTIMODE 0
Michal Malýe7c23442015-02-18 17:59:20 +010037#define LG4FF_MMODE_SWITCHED 1
38#define LG4FF_MMODE_NOT_MULTIMODE 2
39
Michal Malýb96d23e2015-02-18 17:59:21 +010040#define LG4FF_MODE_NATIVE_IDX 0
41#define LG4FF_MODE_DFEX_IDX 1
42#define LG4FF_MODE_DFP_IDX 2
43#define LG4FF_MODE_G25_IDX 3
44#define LG4FF_MODE_DFGT_IDX 4
45#define LG4FF_MODE_G27_IDX 5
Simon Wood29fae1c2015-11-02 07:56:52 -070046#define LG4FF_MODE_G29_IDX 6
47#define LG4FF_MODE_MAX_IDX 7
Michal Malýb96d23e2015-02-18 17:59:21 +010048
49#define LG4FF_MODE_NATIVE BIT(LG4FF_MODE_NATIVE_IDX)
50#define LG4FF_MODE_DFEX BIT(LG4FF_MODE_DFEX_IDX)
51#define LG4FF_MODE_DFP BIT(LG4FF_MODE_DFP_IDX)
52#define LG4FF_MODE_G25 BIT(LG4FF_MODE_G25_IDX)
53#define LG4FF_MODE_DFGT BIT(LG4FF_MODE_DFGT_IDX)
54#define LG4FF_MODE_G27 BIT(LG4FF_MODE_G27_IDX)
Simon Wood29fae1c2015-11-02 07:56:52 -070055#define LG4FF_MODE_G29 BIT(LG4FF_MODE_G29_IDX)
Michal Malýb96d23e2015-02-18 17:59:21 +010056
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"
Simon Wood29fae1c2015-11-02 07:56:52 -070065#define LG4FF_G29_TAG "G29"
66#define LG4FF_G29_NAME "G29 Racing Wheel"
Michal Malýb96d23e2015-02-18 17:59:21 +010067#define LG4FF_DFGT_TAG "DFGT"
68#define LG4FF_DFGT_NAME "Driving Force GT"
69
Michal Malýe7c23442015-02-18 17:59:20 +010070#define LG4FF_FFEX_REV_MAJ 0x21
71#define LG4FF_FFEX_REV_MIN 0x00
72
Michal Malýd0afd842015-04-08 22:56:40 +020073static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range);
74static void lg4ff_set_range_g25(struct hid_device *hid, u16 range);
Michal Malý30bb75d2011-08-04 16:20:40 +020075
Michal Malý72529c62015-04-08 22:56:46 +020076struct lg4ff_wheel_data {
Michal Malý5d9d60a2015-04-08 22:56:50 +020077 const u32 product_id;
Simon Wood961af462016-09-18 10:55:37 -060078 u16 combine;
Michal Malý2a552c32015-04-08 22:56:39 +020079 u16 range;
Michal Malý5d9d60a2015-04-08 22:56:50 +020080 const u16 min_range;
81 const u16 max_range;
Simon Wood22bcefd2012-04-21 05:41:15 -070082#ifdef CONFIG_LEDS_CLASS
Michal Malý2a552c32015-04-08 22:56:39 +020083 u8 led_state;
Simon Wood22bcefd2012-04-21 05:41:15 -070084 struct led_classdev *led[5];
85#endif
Michal Malý5d9d60a2015-04-08 22:56:50 +020086 const u32 alternate_modes;
87 const char * const real_tag;
88 const char * const real_name;
89 const u16 real_product_id;
Michal Malý72529c62015-04-08 22:56:46 +020090
Michal Malý30bb75d2011-08-04 16:20:40 +020091 void (*set_range)(struct hid_device *hid, u16 range);
92};
93
Michal Malý72529c62015-04-08 22:56:46 +020094struct lg4ff_device_entry {
Michal Malýc918fe72015-04-08 22:56:48 +020095 spinlock_t report_lock; /* Protect output HID report */
Michal Malýc28abd82015-04-08 22:56:49 +020096 struct hid_report *report;
Michal Malý72529c62015-04-08 22:56:46 +020097 struct lg4ff_wheel_data wdata;
98};
99
Michal Malý7362cd22011-08-04 16:16:09 +0200100static const signed short lg4ff_wheel_effects[] = {
Simon Wood32c88cb2010-09-22 13:19:42 +0200101 FF_CONSTANT,
102 FF_AUTOCENTER,
103 -1
104};
105
Michal Malý7362cd22011-08-04 16:16:09 +0200106struct lg4ff_wheel {
Michal Malý2a552c32015-04-08 22:56:39 +0200107 const u32 product_id;
Michal Malý7362cd22011-08-04 16:16:09 +0200108 const signed short *ff_effects;
Michal Malý2a552c32015-04-08 22:56:39 +0200109 const u16 min_range;
110 const u16 max_range;
Michal Malý30bb75d2011-08-04 16:20:40 +0200111 void (*set_range)(struct hid_device *hid, u16 range);
Michal Malý7362cd22011-08-04 16:16:09 +0200112};
113
Michal Malýe7c23442015-02-18 17:59:20 +0100114struct lg4ff_compat_mode_switch {
Michal Malý2a552c32015-04-08 22:56:39 +0200115 const u8 cmd_count; /* Number of commands to send */
116 const u8 cmd[];
Michal Malýe7c23442015-02-18 17:59:20 +0100117};
118
119struct lg4ff_wheel_ident_info {
Simon Woodbbec1bd2015-11-02 07:56:51 -0700120 const u32 modes;
Michal Malýe7c23442015-02-18 17:59:20 +0100121 const u16 mask;
122 const u16 result;
123 const u16 real_product_id;
124};
125
Michal Malýb96d23e2015-02-18 17:59:21 +0100126struct lg4ff_multimode_wheel {
127 const u16 product_id;
128 const u32 alternate_modes;
129 const char *real_tag;
130 const char *real_name;
131};
132
133struct lg4ff_alternate_mode {
134 const u16 product_id;
135 const char *tag;
136 const char *name;
137};
138
Michal Malý7362cd22011-08-04 16:16:09 +0200139static const struct lg4ff_wheel lg4ff_devices[] = {
Simon Wood560bea32016-09-18 10:55:41 -0600140 {USB_DEVICE_ID_LOGITECH_WINGMAN_FFG, lg4ff_wheel_effects, 40, 180, NULL},
Michal Malý30bb75d2011-08-04 16:20:40 +0200141 {USB_DEVICE_ID_LOGITECH_WHEEL, lg4ff_wheel_effects, 40, 270, NULL},
142 {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL, lg4ff_wheel_effects, 40, 270, NULL},
Michal Malýd0afd842015-04-08 22:56:40 +0200143 {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_dfp},
144 {USB_DEVICE_ID_LOGITECH_G25_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
145 {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
146 {USB_DEVICE_ID_LOGITECH_G27_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
Simon Wood29fae1c2015-11-02 07:56:52 -0700147 {USB_DEVICE_ID_LOGITECH_G29_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
Michal Malý30bb75d2011-08-04 16:20:40 +0200148 {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL},
149 {USB_DEVICE_ID_LOGITECH_WII_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}
Michal Malý7362cd22011-08-04 16:16:09 +0200150};
151
Michal Malýb96d23e2015-02-18 17:59:21 +0100152static const struct lg4ff_multimode_wheel lg4ff_multimode_wheels[] = {
153 {USB_DEVICE_ID_LOGITECH_DFP_WHEEL,
154 LG4FF_MODE_NATIVE | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
155 LG4FF_DFP_TAG, LG4FF_DFP_NAME},
156 {USB_DEVICE_ID_LOGITECH_G25_WHEEL,
157 LG4FF_MODE_NATIVE | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
158 LG4FF_G25_TAG, LG4FF_G25_NAME},
159 {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL,
160 LG4FF_MODE_NATIVE | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
161 LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
162 {USB_DEVICE_ID_LOGITECH_G27_WHEEL,
163 LG4FF_MODE_NATIVE | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
164 LG4FF_G27_TAG, LG4FF_G27_NAME},
Simon Wood29fae1c2015-11-02 07:56:52 -0700165 {USB_DEVICE_ID_LOGITECH_G29_WHEEL,
166 LG4FF_MODE_NATIVE | LG4FF_MODE_G29 | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
167 LG4FF_G29_TAG, LG4FF_G29_NAME},
Michal Malýb96d23e2015-02-18 17:59:21 +0100168};
169
170static const struct lg4ff_alternate_mode lg4ff_alternate_modes[] = {
171 [LG4FF_MODE_NATIVE_IDX] = {0, "native", ""},
172 [LG4FF_MODE_DFEX_IDX] = {USB_DEVICE_ID_LOGITECH_WHEEL, LG4FF_DFEX_TAG, LG4FF_DFEX_NAME},
173 [LG4FF_MODE_DFP_IDX] = {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, LG4FF_DFP_TAG, LG4FF_DFP_NAME},
174 [LG4FF_MODE_G25_IDX] = {USB_DEVICE_ID_LOGITECH_G25_WHEEL, LG4FF_G25_TAG, LG4FF_G25_NAME},
175 [LG4FF_MODE_DFGT_IDX] = {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
Simon Wood29fae1c2015-11-02 07:56:52 -0700176 [LG4FF_MODE_G27_IDX] = {USB_DEVICE_ID_LOGITECH_G27_WHEEL, LG4FF_G27_TAG, LG4FF_G27_NAME},
177 [LG4FF_MODE_G29_IDX] = {USB_DEVICE_ID_LOGITECH_G29_WHEEL, LG4FF_G29_TAG, LG4FF_G29_NAME},
Michal Malýb96d23e2015-02-18 17:59:21 +0100178};
179
Michal Malýe7c23442015-02-18 17:59:20 +0100180/* Multimode wheel identificators */
181static const struct lg4ff_wheel_ident_info lg4ff_dfp_ident_info = {
Simon Woodbbec1bd2015-11-02 07:56:51 -0700182 LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
Michal Malýe7c23442015-02-18 17:59:20 +0100183 0xf000,
184 0x1000,
185 USB_DEVICE_ID_LOGITECH_DFP_WHEEL
Michal Malý96440c82011-08-04 16:18:11 +0200186};
187
Michal Malýe7c23442015-02-18 17:59:20 +0100188static const struct lg4ff_wheel_ident_info lg4ff_g25_ident_info = {
Simon Woodbbec1bd2015-11-02 07:56:51 -0700189 LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
Michal Malýe7c23442015-02-18 17:59:20 +0100190 0xff00,
191 0x1200,
192 USB_DEVICE_ID_LOGITECH_G25_WHEEL
Michal Malý96440c82011-08-04 16:18:11 +0200193};
194
Michal Malýe7c23442015-02-18 17:59:20 +0100195static const struct lg4ff_wheel_ident_info lg4ff_g27_ident_info = {
Simon Woodbbec1bd2015-11-02 07:56:51 -0700196 LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
Michal Malýe7c23442015-02-18 17:59:20 +0100197 0xfff0,
198 0x1230,
199 USB_DEVICE_ID_LOGITECH_G27_WHEEL
200};
201
202static const struct lg4ff_wheel_ident_info lg4ff_dfgt_ident_info = {
Simon Woodbbec1bd2015-11-02 07:56:51 -0700203 LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
Michal Malýe7c23442015-02-18 17:59:20 +0100204 0xff00,
205 0x1300,
206 USB_DEVICE_ID_LOGITECH_DFGT_WHEEL
207};
208
Simon Wood29fae1c2015-11-02 07:56:52 -0700209static const struct lg4ff_wheel_ident_info lg4ff_g29_ident_info = {
210 LG4FF_MODE_G29 | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
211 0xfff8,
212 0x1350,
213 USB_DEVICE_ID_LOGITECH_G29_WHEEL
214};
215
216static const struct lg4ff_wheel_ident_info lg4ff_g29_ident_info2 = {
217 LG4FF_MODE_G29 | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
218 0xff00,
219 0x8900,
220 USB_DEVICE_ID_LOGITECH_G29_WHEEL
221};
222
Michal Malýe7c23442015-02-18 17:59:20 +0100223/* Multimode wheel identification checklists */
Simon Woodbbec1bd2015-11-02 07:56:51 -0700224static const struct lg4ff_wheel_ident_info *lg4ff_main_checklist[] = {
Simon Wood29fae1c2015-11-02 07:56:52 -0700225 &lg4ff_g29_ident_info,
226 &lg4ff_g29_ident_info2,
Simon Woodbbec1bd2015-11-02 07:56:51 -0700227 &lg4ff_dfgt_ident_info,
228 &lg4ff_g27_ident_info,
229 &lg4ff_g25_ident_info,
230 &lg4ff_dfp_ident_info
Michal Malýe7c23442015-02-18 17:59:20 +0100231};
232
233/* Compatibility mode switching commands */
Michal Malýf31a2de2015-02-18 17:59:23 +0100234/* EXT_CMD9 - Understood by G27 and DFGT */
235static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfex = {
236 2,
237 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
238 0xf8, 0x09, 0x00, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DF-EX with detach */
239};
240
241static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfp = {
242 2,
243 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
244 0xf8, 0x09, 0x01, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DFP with detach */
245};
246
247static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g25 = {
248 2,
249 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
250 0xf8, 0x09, 0x02, 0x01, 0x00, 0x00, 0x00} /* Switch mode to G25 with detach */
251};
252
253static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfgt = {
254 2,
255 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
256 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DFGT with detach */
257};
258
259static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g27 = {
260 2,
261 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
262 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* Switch mode to G27 with detach */
263};
264
Simon Wood29fae1c2015-11-02 07:56:52 -0700265static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g29 = {
266 2,
267 {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */
268 0xf8, 0x09, 0x05, 0x01, 0x01, 0x00, 0x00} /* Switch mode to G29 with detach */
269};
270
Michal Malýf31a2de2015-02-18 17:59:23 +0100271/* EXT_CMD1 - Understood by DFP, G25, G27 and DFGT */
272static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext01_dfp = {
Michal Malý96440c82011-08-04 16:18:11 +0200273 1,
274 {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
275};
276
Michal Malýf31a2de2015-02-18 17:59:23 +0100277/* EXT_CMD16 - Understood by G25 and G27 */
278static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext16_g25 = {
Michal Malý96440c82011-08-04 16:18:11 +0200279 1,
280 {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
281};
282
Michal Malý2b24a962012-09-23 22:41:08 +0200283/* Recalculates X axis value accordingly to currently selected range */
Michal Malý2a552c32015-04-08 22:56:39 +0200284static s32 lg4ff_adjust_dfp_x_axis(s32 value, u16 range)
Michal Malý2b24a962012-09-23 22:41:08 +0200285{
Michal Malý2a552c32015-04-08 22:56:39 +0200286 u16 max_range;
287 s32 new_value;
Michal Malý2b24a962012-09-23 22:41:08 +0200288
289 if (range == 900)
290 return value;
291 else if (range == 200)
292 return value;
293 else if (range < 200)
294 max_range = 200;
295 else
296 max_range = 900;
297
298 new_value = 8192 + mult_frac(value - 8192, max_range, range);
299 if (new_value < 0)
300 return 0;
301 else if (new_value > 16383)
302 return 16383;
303 else
304 return new_value;
305}
306
307int lg4ff_adjust_input_event(struct hid_device *hid, struct hid_field *field,
Michal Malý2a552c32015-04-08 22:56:39 +0200308 struct hid_usage *usage, s32 value, struct lg_drv_data *drv_data)
Michal Malý2b24a962012-09-23 22:41:08 +0200309{
310 struct lg4ff_device_entry *entry = drv_data->device_props;
Michal Malý2a552c32015-04-08 22:56:39 +0200311 s32 new_value = 0;
Michal Malý2b24a962012-09-23 22:41:08 +0200312
313 if (!entry) {
314 hid_err(hid, "Device properties not found");
315 return 0;
316 }
317
Michal Malý72529c62015-04-08 22:56:46 +0200318 switch (entry->wdata.product_id) {
Michal Malý2b24a962012-09-23 22:41:08 +0200319 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
320 switch (usage->code) {
321 case ABS_X:
Michal Malý72529c62015-04-08 22:56:46 +0200322 new_value = lg4ff_adjust_dfp_x_axis(value, entry->wdata.range);
Michal Malý2b24a962012-09-23 22:41:08 +0200323 input_event(field->hidinput->input, usage->type, usage->code, new_value);
324 return 1;
325 default:
326 return 0;
327 }
328 default:
329 return 0;
330 }
331}
332
Simon Woodc832f862016-09-18 10:55:38 -0600333int lg4ff_raw_event(struct hid_device *hdev, struct hid_report *report,
334 u8 *rd, int size, struct lg_drv_data *drv_data)
335{
Simon Woodb4566342016-09-18 10:55:39 -0600336 int offset;
Simon Woodc832f862016-09-18 10:55:38 -0600337 struct lg4ff_device_entry *entry = drv_data->device_props;
338
339 if (!entry)
340 return 0;
341
342 /* adjust HID report present combined pedals data */
343 if (entry->wdata.combine) {
344 switch (entry->wdata.product_id) {
345 case USB_DEVICE_ID_LOGITECH_WHEEL:
346 rd[5] = rd[3];
347 rd[6] = 0x7F;
348 return 1;
Simon Wood560bea32016-09-18 10:55:41 -0600349 case USB_DEVICE_ID_LOGITECH_WINGMAN_FFG:
Simon Woodc832f862016-09-18 10:55:38 -0600350 case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL:
351 case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2:
352 rd[4] = rd[3];
353 rd[5] = 0x7F;
354 return 1;
355 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
356 rd[5] = rd[4];
357 rd[6] = 0x7F;
358 return 1;
Simon Woodb4566342016-09-18 10:55:39 -0600359 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
360 case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
361 offset = 5;
362 break;
363 case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
364 case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
365 offset = 6;
366 break;
367 case USB_DEVICE_ID_LOGITECH_WII_WHEEL:
368 offset = 3;
369 break;
Simon Woodc832f862016-09-18 10:55:38 -0600370 default:
371 return 0;
372 }
Simon Woodb4566342016-09-18 10:55:39 -0600373
374 /* Compute a combined axis when wheel does not supply it */
375 rd[offset] = (0xFF + rd[offset] - rd[offset+1]) >> 1;
376 rd[offset+1] = 0x7F;
377 return 1;
Simon Woodc832f862016-09-18 10:55:38 -0600378 }
379
380 return 0;
381}
382
Michal Malý5d9d60a2015-04-08 22:56:50 +0200383static void lg4ff_init_wheel_data(struct lg4ff_wheel_data * const wdata, const struct lg4ff_wheel *wheel,
384 const struct lg4ff_multimode_wheel *mmode_wheel,
385 const u16 real_product_id)
386{
387 u32 alternate_modes = 0;
388 const char *real_tag = NULL;
389 const char *real_name = NULL;
390
391 if (mmode_wheel) {
392 alternate_modes = mmode_wheel->alternate_modes;
393 real_tag = mmode_wheel->real_tag;
394 real_name = mmode_wheel->real_name;
395 }
396
397 {
398 struct lg4ff_wheel_data t_wdata = { .product_id = wheel->product_id,
399 .real_product_id = real_product_id,
Simon Wood961af462016-09-18 10:55:37 -0600400 .combine = 0,
Michal Malý5d9d60a2015-04-08 22:56:50 +0200401 .min_range = wheel->min_range,
402 .max_range = wheel->max_range,
403 .set_range = wheel->set_range,
404 .alternate_modes = alternate_modes,
405 .real_tag = real_tag,
406 .real_name = real_name };
407
408 memcpy(wdata, &t_wdata, sizeof(t_wdata));
409 }
410}
411
Michal Malýd0afd842015-04-08 22:56:40 +0200412static int lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
Simon Wood32c88cb2010-09-22 13:19:42 +0200413{
414 struct hid_device *hid = input_get_drvdata(dev);
Michal Malýc918fe72015-04-08 22:56:48 +0200415 struct lg4ff_device_entry *entry;
416 struct lg_drv_data *drv_data;
417 unsigned long flags;
Michal Malýc28abd82015-04-08 22:56:49 +0200418 s32 *value;
Simon Wood32c88cb2010-09-22 13:19:42 +0200419 int x;
420
Michal Malýc918fe72015-04-08 22:56:48 +0200421 drv_data = hid_get_drvdata(hid);
422 if (!drv_data) {
423 hid_err(hid, "Private driver data not found!\n");
424 return -EINVAL;
425 }
426
427 entry = drv_data->device_props;
428 if (!entry) {
429 hid_err(hid, "Device properties not found!\n");
430 return -EINVAL;
431 }
Michal Malýc28abd82015-04-08 22:56:49 +0200432 value = entry->report->field[0]->value;
Michal Malýc918fe72015-04-08 22:56:48 +0200433
Michal Malýa80fe5d2012-09-24 01:13:17 +0200434#define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0)
Simon Wood32c88cb2010-09-22 13:19:42 +0200435
436 switch (effect->type) {
437 case FF_CONSTANT:
438 x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */
439 CLAMP(x);
Simon Wood56930e72013-11-06 12:30:42 -0700440
Michal Malýc918fe72015-04-08 22:56:48 +0200441 spin_lock_irqsave(&entry->report_lock, flags);
Simon Wood56930e72013-11-06 12:30:42 -0700442 if (x == 0x80) {
443 /* De-activate force in slot-1*/
444 value[0] = 0x13;
445 value[1] = 0x00;
446 value[2] = 0x00;
447 value[3] = 0x00;
448 value[4] = 0x00;
449 value[5] = 0x00;
450 value[6] = 0x00;
451
Michal Malýc28abd82015-04-08 22:56:49 +0200452 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
Michal Malýc918fe72015-04-08 22:56:48 +0200453 spin_unlock_irqrestore(&entry->report_lock, flags);
Simon Wood56930e72013-11-06 12:30:42 -0700454 return 0;
455 }
456
Michal Malý74479ba2012-09-24 01:09:30 +0200457 value[0] = 0x11; /* Slot 1 */
458 value[1] = 0x08;
459 value[2] = x;
460 value[3] = 0x80;
461 value[4] = 0x00;
462 value[5] = 0x00;
463 value[6] = 0x00;
Simon Wood32c88cb2010-09-22 13:19:42 +0200464
Michal Malýc28abd82015-04-08 22:56:49 +0200465 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
Michal Malýc918fe72015-04-08 22:56:48 +0200466 spin_unlock_irqrestore(&entry->report_lock, flags);
Simon Wood32c88cb2010-09-22 13:19:42 +0200467 break;
468 }
469 return 0;
470}
471
Michal Malý6e2de8e2011-08-04 16:22:07 +0200472/* Sends default autocentering command compatible with
473 * all wheels except Formula Force EX */
Michal Malýd0afd842015-04-08 22:56:40 +0200474static void lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude)
Simon Wood32c88cb2010-09-22 13:19:42 +0200475{
476 struct hid_device *hid = input_get_drvdata(dev);
477 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
478 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malý2a552c32015-04-08 22:56:39 +0200479 s32 *value = report->field[0]->value;
480 u32 expand_a, expand_b;
Simon Wood18597622013-11-06 12:30:44 -0700481 struct lg4ff_device_entry *entry;
482 struct lg_drv_data *drv_data;
Michal Malýc918fe72015-04-08 22:56:48 +0200483 unsigned long flags;
Simon Wood18597622013-11-06 12:30:44 -0700484
485 drv_data = hid_get_drvdata(hid);
486 if (!drv_data) {
487 hid_err(hid, "Private driver data not found!\n");
488 return;
489 }
490
491 entry = drv_data->device_props;
492 if (!entry) {
493 hid_err(hid, "Device properties not found!\n");
494 return;
495 }
Michal Malýc28abd82015-04-08 22:56:49 +0200496 value = entry->report->field[0]->value;
Simon Woodf8c23152013-11-06 12:30:40 -0700497
Simon Woodd2c02da2013-11-06 12:30:41 -0700498 /* De-activate Auto-Center */
Michal Malýc918fe72015-04-08 22:56:48 +0200499 spin_lock_irqsave(&entry->report_lock, flags);
Simon Woodd2c02da2013-11-06 12:30:41 -0700500 if (magnitude == 0) {
501 value[0] = 0xf5;
502 value[1] = 0x00;
503 value[2] = 0x00;
504 value[3] = 0x00;
505 value[4] = 0x00;
506 value[5] = 0x00;
507 value[6] = 0x00;
508
Michal Malýc28abd82015-04-08 22:56:49 +0200509 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
Michal Malýc918fe72015-04-08 22:56:48 +0200510 spin_unlock_irqrestore(&entry->report_lock, flags);
Simon Woodd2c02da2013-11-06 12:30:41 -0700511 return;
512 }
513
Simon Woodf8c23152013-11-06 12:30:40 -0700514 if (magnitude <= 0xaaaa) {
515 expand_a = 0x0c * magnitude;
516 expand_b = 0x80 * magnitude;
517 } else {
518 expand_a = (0x0c * 0xaaaa) + 0x06 * (magnitude - 0xaaaa);
519 expand_b = (0x80 * 0xaaaa) + 0xff * (magnitude - 0xaaaa);
520 }
Simon Wood32c88cb2010-09-22 13:19:42 +0200521
Simon Wood18597622013-11-06 12:30:44 -0700522 /* Adjust for non-MOMO wheels */
Michal Malý72529c62015-04-08 22:56:46 +0200523 switch (entry->wdata.product_id) {
Simon Wood18597622013-11-06 12:30:44 -0700524 case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL:
525 case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2:
526 break;
527 default:
528 expand_a = expand_a >> 1;
529 break;
530 }
531
Michal Malý74479ba2012-09-24 01:09:30 +0200532 value[0] = 0xfe;
533 value[1] = 0x0d;
Simon Woodf8c23152013-11-06 12:30:40 -0700534 value[2] = expand_a / 0xaaaa;
535 value[3] = expand_a / 0xaaaa;
536 value[4] = expand_b / 0xaaaa;
Michal Malý74479ba2012-09-24 01:09:30 +0200537 value[5] = 0x00;
538 value[6] = 0x00;
Simon Wood32c88cb2010-09-22 13:19:42 +0200539
Michal Malýc28abd82015-04-08 22:56:49 +0200540 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
Simon Woodd2c02da2013-11-06 12:30:41 -0700541
542 /* Activate Auto-Center */
543 value[0] = 0x14;
544 value[1] = 0x00;
545 value[2] = 0x00;
546 value[3] = 0x00;
547 value[4] = 0x00;
548 value[5] = 0x00;
549 value[6] = 0x00;
550
Michal Malýc28abd82015-04-08 22:56:49 +0200551 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
Michal Malýc918fe72015-04-08 22:56:48 +0200552 spin_unlock_irqrestore(&entry->report_lock, flags);
Simon Wood32c88cb2010-09-22 13:19:42 +0200553}
554
Michal Malý6e2de8e2011-08-04 16:22:07 +0200555/* Sends autocentering command compatible with Formula Force EX */
Michal Malýd0afd842015-04-08 22:56:40 +0200556static void lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude)
Michal Malý6e2de8e2011-08-04 16:22:07 +0200557{
558 struct hid_device *hid = input_get_drvdata(dev);
Michal Malýc918fe72015-04-08 22:56:48 +0200559 struct lg4ff_device_entry *entry;
560 struct lg_drv_data *drv_data;
561 unsigned long flags;
Michal Malýc28abd82015-04-08 22:56:49 +0200562 s32 *value;
Michal Malý6e2de8e2011-08-04 16:22:07 +0200563 magnitude = magnitude * 90 / 65535;
Michal Malý6e2de8e2011-08-04 16:22:07 +0200564
Michal Malýc918fe72015-04-08 22:56:48 +0200565 drv_data = hid_get_drvdata(hid);
566 if (!drv_data) {
567 hid_err(hid, "Private driver data not found!\n");
568 return;
569 }
570
571 entry = drv_data->device_props;
572 if (!entry) {
573 hid_err(hid, "Device properties not found!\n");
574 return;
575 }
Michal Malýc28abd82015-04-08 22:56:49 +0200576 value = entry->report->field[0]->value;
Michal Malýc918fe72015-04-08 22:56:48 +0200577
578 spin_lock_irqsave(&entry->report_lock, flags);
Michal Malý74479ba2012-09-24 01:09:30 +0200579 value[0] = 0xfe;
580 value[1] = 0x03;
581 value[2] = magnitude >> 14;
582 value[3] = magnitude >> 14;
583 value[4] = magnitude;
584 value[5] = 0x00;
585 value[6] = 0x00;
Michal Malý6e2de8e2011-08-04 16:22:07 +0200586
Michal Malýc28abd82015-04-08 22:56:49 +0200587 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
Michal Malýc918fe72015-04-08 22:56:48 +0200588 spin_unlock_irqrestore(&entry->report_lock, flags);
Michal Malý6e2de8e2011-08-04 16:22:07 +0200589}
590
Michal Malý30bb75d2011-08-04 16:20:40 +0200591/* Sends command to set range compatible with G25/G27/Driving Force GT */
Michal Malýd0afd842015-04-08 22:56:40 +0200592static void lg4ff_set_range_g25(struct hid_device *hid, u16 range)
Michal Malý30bb75d2011-08-04 16:20:40 +0200593{
Michal Malýc918fe72015-04-08 22:56:48 +0200594 struct lg4ff_device_entry *entry;
595 struct lg_drv_data *drv_data;
596 unsigned long flags;
Michal Malýc28abd82015-04-08 22:56:49 +0200597 s32 *value;
Michal Malý74479ba2012-09-24 01:09:30 +0200598
Michal Malýc918fe72015-04-08 22:56:48 +0200599 drv_data = hid_get_drvdata(hid);
600 if (!drv_data) {
601 hid_err(hid, "Private driver data not found!\n");
602 return;
603 }
604
605 entry = drv_data->device_props;
606 if (!entry) {
607 hid_err(hid, "Device properties not found!\n");
608 return;
609 }
Michal Malýc28abd82015-04-08 22:56:49 +0200610 value = entry->report->field[0]->value;
Michal Malý30bb75d2011-08-04 16:20:40 +0200611 dbg_hid("G25/G27/DFGT: setting range to %u\n", range);
612
Michal Malýc918fe72015-04-08 22:56:48 +0200613 spin_lock_irqsave(&entry->report_lock, flags);
Michal Malý74479ba2012-09-24 01:09:30 +0200614 value[0] = 0xf8;
615 value[1] = 0x81;
616 value[2] = range & 0x00ff;
617 value[3] = (range & 0xff00) >> 8;
618 value[4] = 0x00;
619 value[5] = 0x00;
620 value[6] = 0x00;
Michal Malý30bb75d2011-08-04 16:20:40 +0200621
Michal Malýc28abd82015-04-08 22:56:49 +0200622 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
Michal Malýc918fe72015-04-08 22:56:48 +0200623 spin_unlock_irqrestore(&entry->report_lock, flags);
Michal Malý30bb75d2011-08-04 16:20:40 +0200624}
625
626/* Sends commands to set range compatible with Driving Force Pro wheel */
Michal Malýd0afd842015-04-08 22:56:40 +0200627static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range)
Michal Malý30bb75d2011-08-04 16:20:40 +0200628{
Michal Malýc918fe72015-04-08 22:56:48 +0200629 struct lg4ff_device_entry *entry;
630 struct lg_drv_data *drv_data;
631 unsigned long flags;
Michal Malýc28abd82015-04-08 22:56:49 +0200632 int start_left, start_right, full_range;
633 s32 *value;
Michal Malý74479ba2012-09-24 01:09:30 +0200634
Michal Malýc918fe72015-04-08 22:56:48 +0200635 drv_data = hid_get_drvdata(hid);
636 if (!drv_data) {
637 hid_err(hid, "Private driver data not found!\n");
638 return;
639 }
640
641 entry = drv_data->device_props;
642 if (!entry) {
643 hid_err(hid, "Device properties not found!\n");
644 return;
645 }
Michal Malýc28abd82015-04-08 22:56:49 +0200646 value = entry->report->field[0]->value;
Michal Malý30bb75d2011-08-04 16:20:40 +0200647 dbg_hid("Driving Force Pro: setting range to %u\n", range);
648
649 /* Prepare "coarse" limit command */
Michal Malýc918fe72015-04-08 22:56:48 +0200650 spin_lock_irqsave(&entry->report_lock, flags);
Michal Malý74479ba2012-09-24 01:09:30 +0200651 value[0] = 0xf8;
Michal Malýa80fe5d2012-09-24 01:13:17 +0200652 value[1] = 0x00; /* Set later */
Michal Malý74479ba2012-09-24 01:09:30 +0200653 value[2] = 0x00;
654 value[3] = 0x00;
655 value[4] = 0x00;
656 value[5] = 0x00;
657 value[6] = 0x00;
Michal Malý30bb75d2011-08-04 16:20:40 +0200658
659 if (range > 200) {
Michal Malýc28abd82015-04-08 22:56:49 +0200660 value[1] = 0x03;
Michal Malý30bb75d2011-08-04 16:20:40 +0200661 full_range = 900;
662 } else {
Michal Malýc28abd82015-04-08 22:56:49 +0200663 value[1] = 0x02;
Michal Malý30bb75d2011-08-04 16:20:40 +0200664 full_range = 200;
665 }
Michal Malýc28abd82015-04-08 22:56:49 +0200666 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
Michal Malý30bb75d2011-08-04 16:20:40 +0200667
668 /* Prepare "fine" limit command */
Michal Malý74479ba2012-09-24 01:09:30 +0200669 value[0] = 0x81;
670 value[1] = 0x0b;
671 value[2] = 0x00;
672 value[3] = 0x00;
673 value[4] = 0x00;
674 value[5] = 0x00;
675 value[6] = 0x00;
Michal Malý30bb75d2011-08-04 16:20:40 +0200676
677 if (range == 200 || range == 900) { /* Do not apply any fine limit */
Michal Malýc28abd82015-04-08 22:56:49 +0200678 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
Michal Malýc918fe72015-04-08 22:56:48 +0200679 spin_unlock_irqrestore(&entry->report_lock, flags);
Michal Malý30bb75d2011-08-04 16:20:40 +0200680 return;
681 }
682
683 /* Construct fine limit command */
684 start_left = (((full_range - range + 1) * 2047) / full_range);
685 start_right = 0xfff - start_left;
686
Michal Malý74479ba2012-09-24 01:09:30 +0200687 value[2] = start_left >> 4;
688 value[3] = start_right >> 4;
689 value[4] = 0xff;
690 value[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
691 value[6] = 0xff;
Michal Malý30bb75d2011-08-04 16:20:40 +0200692
Michal Malýc28abd82015-04-08 22:56:49 +0200693 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
Michal Malýc918fe72015-04-08 22:56:48 +0200694 spin_unlock_irqrestore(&entry->report_lock, flags);
Michal Malý30bb75d2011-08-04 16:20:40 +0200695}
696
Michal Malýf31a2de2015-02-18 17:59:23 +0100697static const struct lg4ff_compat_mode_switch *lg4ff_get_mode_switch_command(const u16 real_product_id, const u16 target_product_id)
698{
699 switch (real_product_id) {
700 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
701 switch (target_product_id) {
702 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
703 return &lg4ff_mode_switch_ext01_dfp;
704 /* DFP can only be switched to its native mode */
705 default:
706 return NULL;
707 }
708 break;
709 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
710 switch (target_product_id) {
711 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
712 return &lg4ff_mode_switch_ext01_dfp;
713 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
714 return &lg4ff_mode_switch_ext16_g25;
715 /* G25 can only be switched to DFP mode or its native mode */
716 default:
717 return NULL;
718 }
719 break;
720 case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
721 switch (target_product_id) {
722 case USB_DEVICE_ID_LOGITECH_WHEEL:
723 return &lg4ff_mode_switch_ext09_dfex;
724 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
725 return &lg4ff_mode_switch_ext09_dfp;
726 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
727 return &lg4ff_mode_switch_ext09_g25;
728 case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
729 return &lg4ff_mode_switch_ext09_g27;
730 /* G27 can only be switched to DF-EX, DFP, G25 or its native mode */
731 default:
732 return NULL;
733 }
734 break;
Simon Wood29fae1c2015-11-02 07:56:52 -0700735 case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
736 switch (target_product_id) {
737 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
738 return &lg4ff_mode_switch_ext09_dfp;
739 case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
740 return &lg4ff_mode_switch_ext09_dfgt;
741 case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
742 return &lg4ff_mode_switch_ext09_g25;
743 case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
744 return &lg4ff_mode_switch_ext09_g27;
745 case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
746 return &lg4ff_mode_switch_ext09_g29;
747 /* G29 can only be switched to DF-EX, DFP, DFGT, G25, G27 or its native mode */
748 default:
749 return NULL;
750 }
751 break;
Michal Malýf31a2de2015-02-18 17:59:23 +0100752 case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
753 switch (target_product_id) {
754 case USB_DEVICE_ID_LOGITECH_WHEEL:
755 return &lg4ff_mode_switch_ext09_dfex;
756 case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
757 return &lg4ff_mode_switch_ext09_dfp;
758 case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
759 return &lg4ff_mode_switch_ext09_dfgt;
760 /* DFGT can only be switched to DF-EX, DFP or its native mode */
761 default:
762 return NULL;
763 }
764 break;
765 /* No other wheels have multiple modes */
766 default:
767 return NULL;
768 }
769}
770
Michal Malýe7c23442015-02-18 17:59:20 +0100771static int lg4ff_switch_compatibility_mode(struct hid_device *hid, const struct lg4ff_compat_mode_switch *s)
Michal Malý96440c82011-08-04 16:18:11 +0200772{
Michal Malýc918fe72015-04-08 22:56:48 +0200773 struct lg4ff_device_entry *entry;
774 struct lg_drv_data *drv_data;
775 unsigned long flags;
Michal Malýc28abd82015-04-08 22:56:49 +0200776 s32 *value;
Michal Malýe7c23442015-02-18 17:59:20 +0100777 u8 i;
Michal Malý96440c82011-08-04 16:18:11 +0200778
Michal Malýc918fe72015-04-08 22:56:48 +0200779 drv_data = hid_get_drvdata(hid);
780 if (!drv_data) {
781 hid_err(hid, "Private driver data not found!\n");
782 return -EINVAL;
783 }
784
785 entry = drv_data->device_props;
786 if (!entry) {
787 hid_err(hid, "Device properties not found!\n");
788 return -EINVAL;
789 }
Michal Malýc28abd82015-04-08 22:56:49 +0200790 value = entry->report->field[0]->value;
Michal Malýc918fe72015-04-08 22:56:48 +0200791
792 spin_lock_irqsave(&entry->report_lock, flags);
Michal Malýe7c23442015-02-18 17:59:20 +0100793 for (i = 0; i < s->cmd_count; i++) {
Michal Malýc1740d12015-02-18 22:49:33 +0100794 u8 j;
Michal Malý96440c82011-08-04 16:18:11 +0200795
Michal Malýc1740d12015-02-18 22:49:33 +0100796 for (j = 0; j < 7; j++)
797 value[j] = s->cmd[j + (7*i)];
798
Michal Malýc28abd82015-04-08 22:56:49 +0200799 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
Michal Malý96440c82011-08-04 16:18:11 +0200800 }
Michal Malýc918fe72015-04-08 22:56:48 +0200801 spin_unlock_irqrestore(&entry->report_lock, flags);
Michal Malýc1740d12015-02-18 22:49:33 +0100802 hid_hw_wait(hid);
Michal Malýe7c23442015-02-18 17:59:20 +0100803 return 0;
Michal Malý96440c82011-08-04 16:18:11 +0200804}
Simon Wood32c88cb2010-09-22 13:19:42 +0200805
Michal Malýb96d23e2015-02-18 17:59:21 +0100806static ssize_t lg4ff_alternate_modes_show(struct device *dev, struct device_attribute *attr, char *buf)
807{
808 struct hid_device *hid = to_hid_device(dev);
809 struct lg4ff_device_entry *entry;
810 struct lg_drv_data *drv_data;
811 ssize_t count = 0;
812 int i;
813
814 drv_data = hid_get_drvdata(hid);
815 if (!drv_data) {
816 hid_err(hid, "Private driver data not found!\n");
817 return 0;
818 }
819
820 entry = drv_data->device_props;
821 if (!entry) {
822 hid_err(hid, "Device properties not found!\n");
823 return 0;
824 }
825
Michal Malý72529c62015-04-08 22:56:46 +0200826 if (!entry->wdata.real_name) {
Michal Malýb96d23e2015-02-18 17:59:21 +0100827 hid_err(hid, "NULL pointer to string\n");
828 return 0;
829 }
830
831 for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
Michal Malý72529c62015-04-08 22:56:46 +0200832 if (entry->wdata.alternate_modes & BIT(i)) {
Michal Malýb96d23e2015-02-18 17:59:21 +0100833 /* Print tag and full name */
834 count += scnprintf(buf + count, PAGE_SIZE - count, "%s: %s",
835 lg4ff_alternate_modes[i].tag,
Michal Malý72529c62015-04-08 22:56:46 +0200836 !lg4ff_alternate_modes[i].product_id ? entry->wdata.real_name : lg4ff_alternate_modes[i].name);
Michal Malýb96d23e2015-02-18 17:59:21 +0100837 if (count >= PAGE_SIZE - 1)
838 return count;
839
840 /* Mark the currently active mode with an asterisk */
Michal Malý72529c62015-04-08 22:56:46 +0200841 if (lg4ff_alternate_modes[i].product_id == entry->wdata.product_id ||
842 (lg4ff_alternate_modes[i].product_id == 0 && entry->wdata.product_id == entry->wdata.real_product_id))
Michal Malýb96d23e2015-02-18 17:59:21 +0100843 count += scnprintf(buf + count, PAGE_SIZE - count, " *\n");
844 else
845 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
846
847 if (count >= PAGE_SIZE - 1)
848 return count;
849 }
850 }
851
852 return count;
853}
854
855static ssize_t lg4ff_alternate_modes_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
856{
Michal Malýf31a2de2015-02-18 17:59:23 +0100857 struct hid_device *hid = to_hid_device(dev);
858 struct lg4ff_device_entry *entry;
859 struct lg_drv_data *drv_data;
860 const struct lg4ff_compat_mode_switch *s;
861 u16 target_product_id = 0;
862 int i, ret;
863 char *lbuf;
864
865 drv_data = hid_get_drvdata(hid);
866 if (!drv_data) {
867 hid_err(hid, "Private driver data not found!\n");
868 return -EINVAL;
869 }
870
871 entry = drv_data->device_props;
872 if (!entry) {
873 hid_err(hid, "Device properties not found!\n");
874 return -EINVAL;
875 }
876
877 /* Allow \n at the end of the input parameter */
878 lbuf = kasprintf(GFP_KERNEL, "%s", buf);
879 if (!lbuf)
880 return -ENOMEM;
881
882 i = strlen(lbuf);
883 if (lbuf[i-1] == '\n') {
884 if (i == 1) {
885 kfree(lbuf);
886 return -EINVAL;
887 }
888 lbuf[i-1] = '\0';
889 }
890
891 for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
892 const u16 mode_product_id = lg4ff_alternate_modes[i].product_id;
893 const char *tag = lg4ff_alternate_modes[i].tag;
894
Michal Malý72529c62015-04-08 22:56:46 +0200895 if (entry->wdata.alternate_modes & BIT(i)) {
Michal Malýf31a2de2015-02-18 17:59:23 +0100896 if (!strcmp(tag, lbuf)) {
897 if (!mode_product_id)
Michal Malý72529c62015-04-08 22:56:46 +0200898 target_product_id = entry->wdata.real_product_id;
Michal Malýf31a2de2015-02-18 17:59:23 +0100899 else
900 target_product_id = mode_product_id;
901 break;
902 }
903 }
904 }
905
906 if (i == LG4FF_MODE_MAX_IDX) {
907 hid_info(hid, "Requested mode \"%s\" is not supported by the device\n", lbuf);
908 kfree(lbuf);
909 return -EINVAL;
910 }
911 kfree(lbuf); /* Not needed anymore */
912
Michal Malý72529c62015-04-08 22:56:46 +0200913 if (target_product_id == entry->wdata.product_id) /* Nothing to do */
Michal Malýf31a2de2015-02-18 17:59:23 +0100914 return count;
915
916 /* Automatic switching has to be disabled for the switch to DF-EX mode to work correctly */
917 if (target_product_id == USB_DEVICE_ID_LOGITECH_WHEEL && !lg4ff_no_autoswitch) {
918 hid_info(hid, "\"%s\" cannot be switched to \"DF-EX\" mode. Load the \"hid_logitech\" module with \"lg4ff_no_autoswitch=1\" parameter set and try again\n",
Michal Malý72529c62015-04-08 22:56:46 +0200919 entry->wdata.real_name);
Michal Malýf31a2de2015-02-18 17:59:23 +0100920 return -EINVAL;
921 }
922
923 /* Take care of hardware limitations */
Michal Malý72529c62015-04-08 22:56:46 +0200924 if ((entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_DFP_WHEEL || entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_G25_WHEEL) &&
925 entry->wdata.product_id > target_product_id) {
926 hid_info(hid, "\"%s\" cannot be switched back into \"%s\" mode\n", entry->wdata.real_name, lg4ff_alternate_modes[i].name);
Michal Malýf31a2de2015-02-18 17:59:23 +0100927 return -EINVAL;
928 }
929
Michal Malý72529c62015-04-08 22:56:46 +0200930 s = lg4ff_get_mode_switch_command(entry->wdata.real_product_id, target_product_id);
Michal Malýf31a2de2015-02-18 17:59:23 +0100931 if (!s) {
932 hid_err(hid, "Invalid target product ID %X\n", target_product_id);
933 return -EINVAL;
934 }
935
936 ret = lg4ff_switch_compatibility_mode(hid, s);
937 return (ret == 0 ? count : ret);
Michal Malýb96d23e2015-02-18 17:59:21 +0100938}
939static DEVICE_ATTR(alternate_modes, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_alternate_modes_show, lg4ff_alternate_modes_store);
940
Simon Wood961af462016-09-18 10:55:37 -0600941static ssize_t lg4ff_combine_show(struct device *dev, struct device_attribute *attr,
942 char *buf)
943{
944 struct hid_device *hid = to_hid_device(dev);
945 struct lg4ff_device_entry *entry;
946 struct lg_drv_data *drv_data;
947 size_t count;
948
949 drv_data = hid_get_drvdata(hid);
950 if (!drv_data) {
951 hid_err(hid, "Private driver data not found!\n");
952 return 0;
953 }
954
955 entry = drv_data->device_props;
956 if (!entry) {
957 hid_err(hid, "Device properties not found!\n");
958 return 0;
959 }
960
961 count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->wdata.combine);
962 return count;
963}
964
965static ssize_t lg4ff_combine_store(struct device *dev, struct device_attribute *attr,
966 const char *buf, size_t count)
967{
968 struct hid_device *hid = to_hid_device(dev);
969 struct lg4ff_device_entry *entry;
970 struct lg_drv_data *drv_data;
971 u16 combine = simple_strtoul(buf, NULL, 10);
972
973 drv_data = hid_get_drvdata(hid);
974 if (!drv_data) {
975 hid_err(hid, "Private driver data not found!\n");
976 return -EINVAL;
977 }
978
979 entry = drv_data->device_props;
980 if (!entry) {
981 hid_err(hid, "Device properties not found!\n");
982 return -EINVAL;
983 }
984
985 if (combine > 1)
986 combine = 1;
987
988 entry->wdata.combine = combine;
989 return count;
990}
991static DEVICE_ATTR(combine_pedals, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_combine_show, lg4ff_combine_store);
992
Michal Malýfbf85e22015-04-08 22:56:41 +0200993/* Export the currently set range of the wheel */
994static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr,
995 char *buf)
Michal Malý30bb75d2011-08-04 16:20:40 +0200996{
Michal Malý30bb75d2011-08-04 16:20:40 +0200997 struct hid_device *hid = to_hid_device(dev);
Michal Malý3b6b17b2012-04-09 09:08:49 +0200998 struct lg4ff_device_entry *entry;
999 struct lg_drv_data *drv_data;
Michal Malý30bb75d2011-08-04 16:20:40 +02001000 size_t count;
1001
Michal Malý3b6b17b2012-04-09 09:08:49 +02001002 drv_data = hid_get_drvdata(hid);
1003 if (!drv_data) {
1004 hid_err(hid, "Private driver data not found!\n");
1005 return 0;
Michal Malý30bb75d2011-08-04 16:20:40 +02001006 }
Michal Malý3b6b17b2012-04-09 09:08:49 +02001007
1008 entry = drv_data->device_props;
1009 if (!entry) {
1010 hid_err(hid, "Device properties not found!\n");
Michal Malý30bb75d2011-08-04 16:20:40 +02001011 return 0;
1012 }
1013
Michal Malý72529c62015-04-08 22:56:46 +02001014 count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->wdata.range);
Michal Malý30bb75d2011-08-04 16:20:40 +02001015 return count;
1016}
1017
1018/* Set range to user specified value, call appropriate function
1019 * according to the type of the wheel */
Michal Malýfbf85e22015-04-08 22:56:41 +02001020static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr,
1021 const char *buf, size_t count)
Michal Malý30bb75d2011-08-04 16:20:40 +02001022{
Michal Malý30bb75d2011-08-04 16:20:40 +02001023 struct hid_device *hid = to_hid_device(dev);
Michal Malý3b6b17b2012-04-09 09:08:49 +02001024 struct lg4ff_device_entry *entry;
1025 struct lg_drv_data *drv_data;
Michal Malý2a552c32015-04-08 22:56:39 +02001026 u16 range = simple_strtoul(buf, NULL, 10);
Michal Malý30bb75d2011-08-04 16:20:40 +02001027
Michal Malý3b6b17b2012-04-09 09:08:49 +02001028 drv_data = hid_get_drvdata(hid);
1029 if (!drv_data) {
1030 hid_err(hid, "Private driver data not found!\n");
Simon Wood29ff6652014-08-14 20:43:01 -06001031 return -EINVAL;
Michal Malý30bb75d2011-08-04 16:20:40 +02001032 }
Michal Malý3b6b17b2012-04-09 09:08:49 +02001033
1034 entry = drv_data->device_props;
1035 if (!entry) {
1036 hid_err(hid, "Device properties not found!\n");
Simon Wood29ff6652014-08-14 20:43:01 -06001037 return -EINVAL;
Michal Malý30bb75d2011-08-04 16:20:40 +02001038 }
1039
1040 if (range == 0)
Michal Malý72529c62015-04-08 22:56:46 +02001041 range = entry->wdata.max_range;
Michal Malý30bb75d2011-08-04 16:20:40 +02001042
1043 /* Check if the wheel supports range setting
1044 * and that the range is within limits for the wheel */
Michal Malý72529c62015-04-08 22:56:46 +02001045 if (entry->wdata.set_range && range >= entry->wdata.min_range && range <= entry->wdata.max_range) {
1046 entry->wdata.set_range(hid, range);
1047 entry->wdata.range = range;
Michal Malý30bb75d2011-08-04 16:20:40 +02001048 }
1049
1050 return count;
1051}
Michal Malýfbf85e22015-04-08 22:56:41 +02001052static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_range_show, lg4ff_range_store);
Michal Malý30bb75d2011-08-04 16:20:40 +02001053
Michal Malýb96d23e2015-02-18 17:59:21 +01001054static ssize_t lg4ff_real_id_show(struct device *dev, struct device_attribute *attr, char *buf)
1055{
1056 struct hid_device *hid = to_hid_device(dev);
1057 struct lg4ff_device_entry *entry;
1058 struct lg_drv_data *drv_data;
1059 size_t count;
1060
1061 drv_data = hid_get_drvdata(hid);
1062 if (!drv_data) {
1063 hid_err(hid, "Private driver data not found!\n");
1064 return 0;
1065 }
1066
1067 entry = drv_data->device_props;
1068 if (!entry) {
1069 hid_err(hid, "Device properties not found!\n");
1070 return 0;
1071 }
1072
Michal Malý72529c62015-04-08 22:56:46 +02001073 if (!entry->wdata.real_tag || !entry->wdata.real_name) {
Michal Malýb96d23e2015-02-18 17:59:21 +01001074 hid_err(hid, "NULL pointer to string\n");
1075 return 0;
1076 }
1077
Michal Malý72529c62015-04-08 22:56:46 +02001078 count = scnprintf(buf, PAGE_SIZE, "%s: %s\n", entry->wdata.real_tag, entry->wdata.real_name);
Michal Malýb96d23e2015-02-18 17:59:21 +01001079 return count;
1080}
1081
1082static ssize_t lg4ff_real_id_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1083{
1084 /* Real ID is a read-only value */
1085 return -EPERM;
1086}
1087static DEVICE_ATTR(real_id, S_IRUGO, lg4ff_real_id_show, lg4ff_real_id_store);
1088
Simon Wood22bcefd2012-04-21 05:41:15 -07001089#ifdef CONFIG_LEDS_CLASS
Michal Malý2a552c32015-04-08 22:56:39 +02001090static void lg4ff_set_leds(struct hid_device *hid, u8 leds)
Simon Wood22bcefd2012-04-21 05:41:15 -07001091{
Michal Malýc918fe72015-04-08 22:56:48 +02001092 struct lg_drv_data *drv_data;
1093 struct lg4ff_device_entry *entry;
1094 unsigned long flags;
Michal Malýc28abd82015-04-08 22:56:49 +02001095 s32 *value;
Simon Wood22bcefd2012-04-21 05:41:15 -07001096
Michal Malýc918fe72015-04-08 22:56:48 +02001097 drv_data = hid_get_drvdata(hid);
1098 if (!drv_data) {
1099 hid_err(hid, "Private driver data not found!\n");
1100 return;
1101 }
1102
1103 entry = drv_data->device_props;
1104 if (!entry) {
1105 hid_err(hid, "Device properties not found!\n");
1106 return;
1107 }
Michal Malýc28abd82015-04-08 22:56:49 +02001108 value = entry->report->field[0]->value;
Michal Malýc918fe72015-04-08 22:56:48 +02001109
1110 spin_lock_irqsave(&entry->report_lock, flags);
Michal Malý74479ba2012-09-24 01:09:30 +02001111 value[0] = 0xf8;
1112 value[1] = 0x12;
1113 value[2] = leds;
1114 value[3] = 0x00;
1115 value[4] = 0x00;
1116 value[5] = 0x00;
1117 value[6] = 0x00;
Michal Malýc28abd82015-04-08 22:56:49 +02001118 hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
Michal Malýc918fe72015-04-08 22:56:48 +02001119 spin_unlock_irqrestore(&entry->report_lock, flags);
Simon Wood22bcefd2012-04-21 05:41:15 -07001120}
1121
1122static void lg4ff_led_set_brightness(struct led_classdev *led_cdev,
1123 enum led_brightness value)
1124{
1125 struct device *dev = led_cdev->dev->parent;
Geliang Tangee79a8f2015-12-27 17:25:21 +08001126 struct hid_device *hid = to_hid_device(dev);
Axel Lin4629fd12012-09-13 14:09:33 +08001127 struct lg_drv_data *drv_data = hid_get_drvdata(hid);
Simon Wood22bcefd2012-04-21 05:41:15 -07001128 struct lg4ff_device_entry *entry;
1129 int i, state = 0;
1130
1131 if (!drv_data) {
1132 hid_err(hid, "Device data not found.");
1133 return;
1134 }
1135
Michal Malý371a1d92015-04-08 22:56:43 +02001136 entry = drv_data->device_props;
Simon Wood22bcefd2012-04-21 05:41:15 -07001137
1138 if (!entry) {
1139 hid_err(hid, "Device properties not found.");
1140 return;
1141 }
1142
1143 for (i = 0; i < 5; i++) {
Michal Malý72529c62015-04-08 22:56:46 +02001144 if (led_cdev != entry->wdata.led[i])
Simon Wood22bcefd2012-04-21 05:41:15 -07001145 continue;
Michal Malý72529c62015-04-08 22:56:46 +02001146 state = (entry->wdata.led_state >> i) & 1;
Simon Wood22bcefd2012-04-21 05:41:15 -07001147 if (value == LED_OFF && state) {
Michal Malý72529c62015-04-08 22:56:46 +02001148 entry->wdata.led_state &= ~(1 << i);
1149 lg4ff_set_leds(hid, entry->wdata.led_state);
Simon Wood22bcefd2012-04-21 05:41:15 -07001150 } else if (value != LED_OFF && !state) {
Michal Malý72529c62015-04-08 22:56:46 +02001151 entry->wdata.led_state |= 1 << i;
1152 lg4ff_set_leds(hid, entry->wdata.led_state);
Simon Wood22bcefd2012-04-21 05:41:15 -07001153 }
1154 break;
1155 }
1156}
1157
1158static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev)
1159{
1160 struct device *dev = led_cdev->dev->parent;
Geliang Tangee79a8f2015-12-27 17:25:21 +08001161 struct hid_device *hid = to_hid_device(dev);
Axel Lin4629fd12012-09-13 14:09:33 +08001162 struct lg_drv_data *drv_data = hid_get_drvdata(hid);
Simon Wood22bcefd2012-04-21 05:41:15 -07001163 struct lg4ff_device_entry *entry;
1164 int i, value = 0;
1165
1166 if (!drv_data) {
1167 hid_err(hid, "Device data not found.");
1168 return LED_OFF;
1169 }
1170
Michal Malý371a1d92015-04-08 22:56:43 +02001171 entry = drv_data->device_props;
Simon Wood22bcefd2012-04-21 05:41:15 -07001172
1173 if (!entry) {
1174 hid_err(hid, "Device properties not found.");
1175 return LED_OFF;
1176 }
1177
1178 for (i = 0; i < 5; i++)
Michal Malý72529c62015-04-08 22:56:46 +02001179 if (led_cdev == entry->wdata.led[i]) {
1180 value = (entry->wdata.led_state >> i) & 1;
Simon Wood22bcefd2012-04-21 05:41:15 -07001181 break;
1182 }
1183
1184 return value ? LED_FULL : LED_OFF;
1185}
1186#endif
1187
Michal Malýe7c23442015-02-18 17:59:20 +01001188static u16 lg4ff_identify_multimode_wheel(struct hid_device *hid, const u16 reported_product_id, const u16 bcdDevice)
1189{
Simon Woodbbec1bd2015-11-02 07:56:51 -07001190 u32 current_mode;
1191 int i;
Michal Malýe7c23442015-02-18 17:59:20 +01001192
Simon Woodbbec1bd2015-11-02 07:56:51 -07001193 /* identify current mode from USB PID */
1194 for (i = 1; i < ARRAY_SIZE(lg4ff_alternate_modes); i++) {
1195 dbg_hid("Testing whether PID is %X\n", lg4ff_alternate_modes[i].product_id);
1196 if (reported_product_id == lg4ff_alternate_modes[i].product_id)
1197 break;
Michal Malýe7c23442015-02-18 17:59:20 +01001198 }
1199
Simon Woodbbec1bd2015-11-02 07:56:51 -07001200 if (i == ARRAY_SIZE(lg4ff_alternate_modes))
1201 return 0;
Michal Malýe7c23442015-02-18 17:59:20 +01001202
Simon Woodbbec1bd2015-11-02 07:56:51 -07001203 current_mode = BIT(i);
1204
1205 for (i = 0; i < ARRAY_SIZE(lg4ff_main_checklist); i++) {
1206 const u16 mask = lg4ff_main_checklist[i]->mask;
1207 const u16 result = lg4ff_main_checklist[i]->result;
1208 const u16 real_product_id = lg4ff_main_checklist[i]->real_product_id;
1209
1210 if ((current_mode & lg4ff_main_checklist[i]->modes) && \
1211 (bcdDevice & mask) == result) {
Michal Malýe7c23442015-02-18 17:59:20 +01001212 dbg_hid("Found wheel with real PID %X whose reported PID is %X\n", real_product_id, reported_product_id);
1213 return real_product_id;
1214 }
1215 }
1216
Michal Malýf31a2de2015-02-18 17:59:23 +01001217 /* No match found. This is either Driving Force or an unknown
1218 * wheel model, do not touch it */
Michal Malýe7c23442015-02-18 17:59:20 +01001219 dbg_hid("Wheel with bcdDevice %X was not recognized as multimode wheel, leaving in its current mode\n", bcdDevice);
1220 return 0;
1221}
1222
1223static int lg4ff_handle_multimode_wheel(struct hid_device *hid, u16 *real_product_id, const u16 bcdDevice)
1224{
1225 const u16 reported_product_id = hid->product;
1226 int ret;
1227
1228 *real_product_id = lg4ff_identify_multimode_wheel(hid, reported_product_id, bcdDevice);
1229 /* Probed wheel is not a multimode wheel */
1230 if (!*real_product_id) {
1231 *real_product_id = reported_product_id;
1232 dbg_hid("Wheel is not a multimode wheel\n");
1233 return LG4FF_MMODE_NOT_MULTIMODE;
1234 }
1235
1236 /* Switch from "Driving Force" mode to native mode automatically.
1237 * Otherwise keep the wheel in its current mode */
1238 if (reported_product_id == USB_DEVICE_ID_LOGITECH_WHEEL &&
Michal Malýa54dc7792015-02-18 17:59:22 +01001239 reported_product_id != *real_product_id &&
1240 !lg4ff_no_autoswitch) {
Michal Malýf31a2de2015-02-18 17:59:23 +01001241 const struct lg4ff_compat_mode_switch *s = lg4ff_get_mode_switch_command(*real_product_id, *real_product_id);
Michal Malýe7c23442015-02-18 17:59:20 +01001242
Michal Malýf31a2de2015-02-18 17:59:23 +01001243 if (!s) {
Michal Malýe7c23442015-02-18 17:59:20 +01001244 hid_err(hid, "Invalid product id %X\n", *real_product_id);
Michal Malýb96d23e2015-02-18 17:59:21 +01001245 return LG4FF_MMODE_NOT_MULTIMODE;
Michal Malýe7c23442015-02-18 17:59:20 +01001246 }
1247
1248 ret = lg4ff_switch_compatibility_mode(hid, s);
1249 if (ret) {
1250 /* Wheel could not have been switched to native mode,
1251 * leave it in "Driving Force" mode and continue */
1252 hid_err(hid, "Unable to switch wheel mode, errno %d\n", ret);
Michal Malýb96d23e2015-02-18 17:59:21 +01001253 return LG4FF_MMODE_IS_MULTIMODE;
Michal Malýe7c23442015-02-18 17:59:20 +01001254 }
1255 return LG4FF_MMODE_SWITCHED;
1256 }
1257
Michal Malýb96d23e2015-02-18 17:59:21 +01001258 return LG4FF_MMODE_IS_MULTIMODE;
Michal Malýe7c23442015-02-18 17:59:20 +01001259}
1260
1261
Simon Wood32c88cb2010-09-22 13:19:42 +02001262int lg4ff_init(struct hid_device *hid)
1263{
Alan Stern7b5e3ad2019-10-03 14:53:59 -04001264 struct hid_input *hidinput;
1265 struct input_dev *dev;
Michal Malýc28abd82015-04-08 22:56:49 +02001266 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
1267 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malýe7c23442015-02-18 17:59:20 +01001268 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
1269 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
Michal Malý5d9d60a2015-04-08 22:56:50 +02001270 const struct lg4ff_multimode_wheel *mmode_wheel = NULL;
Michal Malý30bb75d2011-08-04 16:20:40 +02001271 struct lg4ff_device_entry *entry;
Michal Malý3b6b17b2012-04-09 09:08:49 +02001272 struct lg_drv_data *drv_data;
Michal Malýb96d23e2015-02-18 17:59:21 +01001273 int error, i, j;
1274 int mmode_ret, mmode_idx = -1;
Michal Malýe7c23442015-02-18 17:59:20 +01001275 u16 real_product_id;
Simon Wood32c88cb2010-09-22 13:19:42 +02001276
Alan Stern7b5e3ad2019-10-03 14:53:59 -04001277 if (list_empty(&hid->inputs)) {
1278 hid_err(hid, "no inputs found\n");
1279 return -ENODEV;
1280 }
1281 hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1282 dev = hidinput->input;
1283
Simon Wood32c88cb2010-09-22 13:19:42 +02001284 /* Check that the report looks ok */
Kees Cook0fb6bd02013-09-11 21:56:54 +02001285 if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
Simon Wood32c88cb2010-09-22 13:19:42 +02001286 return -1;
Michal Malý30bb75d2011-08-04 16:20:40 +02001287
Michal Malý72529c62015-04-08 22:56:46 +02001288 drv_data = hid_get_drvdata(hid);
1289 if (!drv_data) {
1290 hid_err(hid, "Cannot add device, private driver data not allocated\n");
1291 return -1;
1292 }
1293 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1294 if (!entry)
1295 return -ENOMEM;
Michal Malýc918fe72015-04-08 22:56:48 +02001296 spin_lock_init(&entry->report_lock);
Michal Malýc28abd82015-04-08 22:56:49 +02001297 entry->report = report;
Michal Malý72529c62015-04-08 22:56:46 +02001298 drv_data->device_props = entry;
1299
Michal Malýe7c23442015-02-18 17:59:20 +01001300 /* Check if a multimode wheel has been connected and
1301 * handle it appropriately */
Michal Malýb96d23e2015-02-18 17:59:21 +01001302 mmode_ret = lg4ff_handle_multimode_wheel(hid, &real_product_id, bcdDevice);
Michal Malýe7c23442015-02-18 17:59:20 +01001303
1304 /* Wheel has been told to switch to native mode. There is no point in going on
1305 * with the initialization as the wheel will do a USB reset when it switches mode
1306 */
Michal Malýb96d23e2015-02-18 17:59:21 +01001307 if (mmode_ret == LG4FF_MMODE_SWITCHED)
Michal Malýe7c23442015-02-18 17:59:20 +01001308 return 0;
Michal Malý72529c62015-04-08 22:56:46 +02001309 else if (mmode_ret < 0) {
1310 hid_err(hid, "Unable to switch device mode during initialization, errno %d\n", mmode_ret);
1311 error = mmode_ret;
1312 goto err_init;
1313 }
Michal Malýe7c23442015-02-18 17:59:20 +01001314
Michal Malý7362cd22011-08-04 16:16:09 +02001315 /* Check what wheel has been connected */
1316 for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
1317 if (hid->product == lg4ff_devices[i].product_id) {
1318 dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
1319 break;
1320 }
1321 }
Simon Wood32c88cb2010-09-22 13:19:42 +02001322
Michal Malý7362cd22011-08-04 16:16:09 +02001323 if (i == ARRAY_SIZE(lg4ff_devices)) {
Michal Malý9c2a6bd2015-04-08 22:56:44 +02001324 hid_err(hid, "This device is flagged to be handled by the lg4ff module but this module does not know how to handle it. "
1325 "Please report this as a bug to LKML, Simon Wood <simon@mungewell.org> or "
1326 "Michal Maly <madcatxster@devoid-pointer.net>\n");
Michal Malý72529c62015-04-08 22:56:46 +02001327 error = -1;
1328 goto err_init;
Michal Malý7362cd22011-08-04 16:16:09 +02001329 }
1330
Michal Malýb96d23e2015-02-18 17:59:21 +01001331 if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1332 for (mmode_idx = 0; mmode_idx < ARRAY_SIZE(lg4ff_multimode_wheels); mmode_idx++) {
1333 if (real_product_id == lg4ff_multimode_wheels[mmode_idx].product_id)
1334 break;
1335 }
1336
1337 if (mmode_idx == ARRAY_SIZE(lg4ff_multimode_wheels)) {
1338 hid_err(hid, "Device product ID %X is not listed as a multimode wheel", real_product_id);
Michal Malý72529c62015-04-08 22:56:46 +02001339 error = -1;
1340 goto err_init;
Michal Malýb96d23e2015-02-18 17:59:21 +01001341 }
1342 }
1343
Michal Malý7362cd22011-08-04 16:16:09 +02001344 /* Set supported force feedback capabilities */
1345 for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
1346 set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
Simon Wood32c88cb2010-09-22 13:19:42 +02001347
Michal Malýd0afd842015-04-08 22:56:40 +02001348 error = input_ff_create_memless(dev, NULL, lg4ff_play);
Simon Wood32c88cb2010-09-22 13:19:42 +02001349
1350 if (error)
Michal Malý72529c62015-04-08 22:56:46 +02001351 goto err_init;
Simon Wood32c88cb2010-09-22 13:19:42 +02001352
Michal Malý5d9d60a2015-04-08 22:56:50 +02001353 /* Initialize device properties */
Michal Malýb96d23e2015-02-18 17:59:21 +01001354 if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1355 BUG_ON(mmode_idx == -1);
Michal Malý5d9d60a2015-04-08 22:56:50 +02001356 mmode_wheel = &lg4ff_multimode_wheels[mmode_idx];
Michal Malýb96d23e2015-02-18 17:59:21 +01001357 }
Michal Malý5d9d60a2015-04-08 22:56:50 +02001358 lg4ff_init_wheel_data(&entry->wdata, &lg4ff_devices[i], mmode_wheel, real_product_id);
Michal Malý30bb75d2011-08-04 16:20:40 +02001359
Simon Wood114a55c2013-11-06 12:30:43 -07001360 /* Check if autocentering is available and
1361 * set the centering force to zero by default */
1362 if (test_bit(FF_AUTOCENTER, dev->ffbit)) {
Michal Malýe7c23442015-02-18 17:59:20 +01001363 /* Formula Force EX expects different autocentering command */
1364 if ((bcdDevice >> 8) == LG4FF_FFEX_REV_MAJ &&
1365 (bcdDevice & 0xff) == LG4FF_FFEX_REV_MIN)
Michal Malýd0afd842015-04-08 22:56:40 +02001366 dev->ff->set_autocenter = lg4ff_set_autocenter_ffex;
Simon Wood114a55c2013-11-06 12:30:43 -07001367 else
Michal Malýd0afd842015-04-08 22:56:40 +02001368 dev->ff->set_autocenter = lg4ff_set_autocenter_default;
Simon Wood114a55c2013-11-06 12:30:43 -07001369
1370 dev->ff->set_autocenter(dev, 0);
1371 }
1372
Michal Malý30bb75d2011-08-04 16:20:40 +02001373 /* Create sysfs interface */
Simon Wood961af462016-09-18 10:55:37 -06001374 error = device_create_file(&hid->dev, &dev_attr_combine_pedals);
1375 if (error)
1376 hid_warn(hid, "Unable to create sysfs interface for \"combine\", errno %d\n", error);
Michal Malý30bb75d2011-08-04 16:20:40 +02001377 error = device_create_file(&hid->dev, &dev_attr_range);
1378 if (error)
Michal Malýd61a70ec2015-04-08 22:56:51 +02001379 hid_warn(hid, "Unable to create sysfs interface for \"range\", errno %d\n", error);
Michal Malýb96d23e2015-02-18 17:59:21 +01001380 if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1381 error = device_create_file(&hid->dev, &dev_attr_real_id);
1382 if (error)
Michal Malýd61a70ec2015-04-08 22:56:51 +02001383 hid_warn(hid, "Unable to create sysfs interface for \"real_id\", errno %d\n", error);
Michal Malýb96d23e2015-02-18 17:59:21 +01001384 error = device_create_file(&hid->dev, &dev_attr_alternate_modes);
1385 if (error)
Michal Malýd61a70ec2015-04-08 22:56:51 +02001386 hid_warn(hid, "Unable to create sysfs interface for \"alternate_modes\", errno %d\n", error);
Michal Malýb96d23e2015-02-18 17:59:21 +01001387 }
Michal Malý30bb75d2011-08-04 16:20:40 +02001388 dbg_hid("sysfs interface created\n");
1389
1390 /* Set the maximum range to start with */
Michal Malý72529c62015-04-08 22:56:46 +02001391 entry->wdata.range = entry->wdata.max_range;
1392 if (entry->wdata.set_range)
1393 entry->wdata.set_range(hid, entry->wdata.range);
Michal Malý30bb75d2011-08-04 16:20:40 +02001394
Simon Wood22bcefd2012-04-21 05:41:15 -07001395#ifdef CONFIG_LEDS_CLASS
Simon Wood29fae1c2015-11-02 07:56:52 -07001396 /* register led subsystem - G27/G29 only */
Michal Malý72529c62015-04-08 22:56:46 +02001397 entry->wdata.led_state = 0;
Simon Wood22bcefd2012-04-21 05:41:15 -07001398 for (j = 0; j < 5; j++)
Michal Malý72529c62015-04-08 22:56:46 +02001399 entry->wdata.led[j] = NULL;
Simon Wood22bcefd2012-04-21 05:41:15 -07001400
Simon Wood29fae1c2015-11-02 07:56:52 -07001401 if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL ||
1402 lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G29_WHEEL) {
Simon Wood22bcefd2012-04-21 05:41:15 -07001403 struct led_classdev *led;
1404 size_t name_sz;
1405 char *name;
1406
1407 lg4ff_set_leds(hid, 0);
1408
1409 name_sz = strlen(dev_name(&hid->dev)) + 8;
1410
1411 for (j = 0; j < 5; j++) {
1412 led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
1413 if (!led) {
1414 hid_err(hid, "can't allocate memory for LED %d\n", j);
Michal Malý72529c62015-04-08 22:56:46 +02001415 goto err_leds;
Simon Wood22bcefd2012-04-21 05:41:15 -07001416 }
1417
1418 name = (void *)(&led[1]);
1419 snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1);
1420 led->name = name;
1421 led->brightness = 0;
1422 led->max_brightness = 1;
1423 led->brightness_get = lg4ff_led_get_brightness;
1424 led->brightness_set = lg4ff_led_set_brightness;
1425
Michal Malý72529c62015-04-08 22:56:46 +02001426 entry->wdata.led[j] = led;
Simon Wood22bcefd2012-04-21 05:41:15 -07001427 error = led_classdev_register(&hid->dev, led);
1428
1429 if (error) {
1430 hid_err(hid, "failed to register LED %d. Aborting.\n", j);
Michal Malý72529c62015-04-08 22:56:46 +02001431err_leds:
Simon Wood22bcefd2012-04-21 05:41:15 -07001432 /* Deregister LEDs (if any) */
1433 for (j = 0; j < 5; j++) {
Michal Malý72529c62015-04-08 22:56:46 +02001434 led = entry->wdata.led[j];
1435 entry->wdata.led[j] = NULL;
Simon Wood22bcefd2012-04-21 05:41:15 -07001436 if (!led)
1437 continue;
1438 led_classdev_unregister(led);
1439 kfree(led);
1440 }
1441 goto out; /* Let the driver continue without LEDs */
1442 }
1443 }
1444 }
Simon Wood22bcefd2012-04-21 05:41:15 -07001445out:
Jiri Kosinac6e6dc82012-04-23 21:08:15 +02001446#endif
Simon Wood64013802012-04-21 05:41:16 -07001447 hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n");
Simon Wood32c88cb2010-09-22 13:19:42 +02001448 return 0;
Michal Malý72529c62015-04-08 22:56:46 +02001449
1450err_init:
1451 drv_data->device_props = NULL;
1452 kfree(entry);
1453 return error;
Simon Wood32c88cb2010-09-22 13:19:42 +02001454}
1455
Michal Malý30bb75d2011-08-04 16:20:40 +02001456int lg4ff_deinit(struct hid_device *hid)
1457{
Michal Malý30bb75d2011-08-04 16:20:40 +02001458 struct lg4ff_device_entry *entry;
Michal Malý3b6b17b2012-04-09 09:08:49 +02001459 struct lg_drv_data *drv_data;
1460
Michal Malý3b6b17b2012-04-09 09:08:49 +02001461 drv_data = hid_get_drvdata(hid);
1462 if (!drv_data) {
1463 hid_err(hid, "Error while deinitializing device, no private driver data.\n");
Michal Malý30bb75d2011-08-04 16:20:40 +02001464 return -1;
1465 }
Michal Malý3b6b17b2012-04-09 09:08:49 +02001466 entry = drv_data->device_props;
Michal Malýe7c23442015-02-18 17:59:20 +01001467 if (!entry)
1468 goto out; /* Nothing more to do */
1469
Michal Malýb96d23e2015-02-18 17:59:21 +01001470 /* Multimode devices will have at least the "MODE_NATIVE" bit set */
Michal Malý72529c62015-04-08 22:56:46 +02001471 if (entry->wdata.alternate_modes) {
Michal Malýb96d23e2015-02-18 17:59:21 +01001472 device_remove_file(&hid->dev, &dev_attr_real_id);
1473 device_remove_file(&hid->dev, &dev_attr_alternate_modes);
1474 }
1475
Simon Wood961af462016-09-18 10:55:37 -06001476 device_remove_file(&hid->dev, &dev_attr_combine_pedals);
Michal Malý72529c62015-04-08 22:56:46 +02001477 device_remove_file(&hid->dev, &dev_attr_range);
Simon Wood22bcefd2012-04-21 05:41:15 -07001478#ifdef CONFIG_LEDS_CLASS
1479 {
1480 int j;
1481 struct led_classdev *led;
1482
1483 /* Deregister LEDs (if any) */
1484 for (j = 0; j < 5; j++) {
1485
Michal Malý72529c62015-04-08 22:56:46 +02001486 led = entry->wdata.led[j];
1487 entry->wdata.led[j] = NULL;
Simon Wood22bcefd2012-04-21 05:41:15 -07001488 if (!led)
1489 continue;
1490 led_classdev_unregister(led);
1491 kfree(led);
1492 }
1493 }
1494#endif
Michal Malýb211a632015-04-08 22:56:47 +02001495 drv_data->device_props = NULL;
Simon Wood22bcefd2012-04-21 05:41:15 -07001496
Michal Malý3b6b17b2012-04-09 09:08:49 +02001497 kfree(entry);
Michal Malýe7c23442015-02-18 17:59:20 +01001498out:
Michal Malý30bb75d2011-08-04 16:20:40 +02001499 dbg_hid("Device successfully unregistered\n");
1500 return 0;
1501}