Commit | Line | Data |
---|---|---|
32c88cbc | 1 | /* |
64013800 | 2 | * Force feedback support for Logitech Gaming Wheels |
32c88cbc | 3 | * |
64013800 SW |
4 | * Including G27, G25, DFP, DFGT, FFEX, Momo, Momo2 & |
5 | * Speed Force Wireless (WiiWheel) | |
32c88cbc SW |
6 | * |
7 | * Copyright (c) 2010 Simon Wood <simon@mungewell.org> | |
8 | */ | |
9 | ||
10 | /* | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 of the License, or | |
14 | * (at your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, write to the Free Software | |
23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
24 | */ | |
25 | ||
26 | ||
27 | #include <linux/input.h> | |
28 | #include <linux/usb.h> | |
29 | #include <linux/hid.h> | |
30 | ||
31 | #include "usbhid/usbhid.h" | |
32 | #include "hid-lg.h" | |
a54dc779 | 33 | #include "hid-lg4ff.h" |
7362cd22 | 34 | #include "hid-ids.h" |
32c88cbc | 35 | |
30bb75d7 MM |
36 | #define to_hid_device(pdev) container_of(pdev, struct hid_device, dev) |
37 | ||
b96d23ec | 38 | #define LG4FF_MMODE_IS_MULTIMODE 0 |
e7c23449 MM |
39 | #define LG4FF_MMODE_SWITCHED 1 |
40 | #define LG4FF_MMODE_NOT_MULTIMODE 2 | |
41 | ||
b96d23ec MM |
42 | #define LG4FF_MODE_NATIVE_IDX 0 |
43 | #define LG4FF_MODE_DFEX_IDX 1 | |
44 | #define LG4FF_MODE_DFP_IDX 2 | |
45 | #define LG4FF_MODE_G25_IDX 3 | |
46 | #define LG4FF_MODE_DFGT_IDX 4 | |
47 | #define LG4FF_MODE_G27_IDX 5 | |
48 | #define LG4FF_MODE_MAX_IDX 6 | |
49 | ||
50 | #define LG4FF_MODE_NATIVE BIT(LG4FF_MODE_NATIVE_IDX) | |
51 | #define LG4FF_MODE_DFEX BIT(LG4FF_MODE_DFEX_IDX) | |
52 | #define LG4FF_MODE_DFP BIT(LG4FF_MODE_DFP_IDX) | |
53 | #define LG4FF_MODE_G25 BIT(LG4FF_MODE_G25_IDX) | |
54 | #define LG4FF_MODE_DFGT BIT(LG4FF_MODE_DFGT_IDX) | |
55 | #define LG4FF_MODE_G27 BIT(LG4FF_MODE_G27_IDX) | |
56 | ||
57 | #define LG4FF_DFEX_TAG "DF-EX" | |
58 | #define LG4FF_DFEX_NAME "Driving Force / Formula EX" | |
59 | #define LG4FF_DFP_TAG "DFP" | |
60 | #define LG4FF_DFP_NAME "Driving Force Pro" | |
61 | #define LG4FF_G25_TAG "G25" | |
62 | #define LG4FF_G25_NAME "G25 Racing Wheel" | |
63 | #define LG4FF_G27_TAG "G27" | |
64 | #define LG4FF_G27_NAME "G27 Racing Wheel" | |
65 | #define LG4FF_DFGT_TAG "DFGT" | |
66 | #define LG4FF_DFGT_NAME "Driving Force GT" | |
67 | ||
e7c23449 MM |
68 | #define LG4FF_FFEX_REV_MAJ 0x21 |
69 | #define LG4FF_FFEX_REV_MIN 0x00 | |
70 | ||
d0afd848 MM |
71 | static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range); |
72 | static void lg4ff_set_range_g25(struct hid_device *hid, u16 range); | |
30bb75d7 | 73 | |
72529c65 | 74 | struct lg4ff_wheel_data { |
2a552c30 MM |
75 | u32 product_id; |
76 | u16 range; | |
77 | u16 min_range; | |
78 | u16 max_range; | |
22bcefdc | 79 | #ifdef CONFIG_LEDS_CLASS |
2a552c30 | 80 | u8 led_state; |
22bcefdc SW |
81 | struct led_classdev *led[5]; |
82 | #endif | |
b96d23ec MM |
83 | u32 alternate_modes; |
84 | const char *real_tag; | |
85 | const char *real_name; | |
86 | u16 real_product_id; | |
72529c65 | 87 | |
30bb75d7 MM |
88 | void (*set_range)(struct hid_device *hid, u16 range); |
89 | }; | |
90 | ||
72529c65 MM |
91 | struct lg4ff_device_entry { |
92 | struct lg4ff_wheel_data wdata; | |
93 | }; | |
94 | ||
7362cd22 | 95 | static const signed short lg4ff_wheel_effects[] = { |
32c88cbc SW |
96 | FF_CONSTANT, |
97 | FF_AUTOCENTER, | |
98 | -1 | |
99 | }; | |
100 | ||
7362cd22 | 101 | struct lg4ff_wheel { |
2a552c30 | 102 | const u32 product_id; |
7362cd22 | 103 | const signed short *ff_effects; |
2a552c30 MM |
104 | const u16 min_range; |
105 | const u16 max_range; | |
30bb75d7 | 106 | void (*set_range)(struct hid_device *hid, u16 range); |
7362cd22 MM |
107 | }; |
108 | ||
e7c23449 | 109 | struct lg4ff_compat_mode_switch { |
2a552c30 MM |
110 | const u8 cmd_count; /* Number of commands to send */ |
111 | const u8 cmd[]; | |
e7c23449 MM |
112 | }; |
113 | ||
114 | struct lg4ff_wheel_ident_info { | |
115 | const u16 mask; | |
116 | const u16 result; | |
117 | const u16 real_product_id; | |
118 | }; | |
119 | ||
120 | struct lg4ff_wheel_ident_checklist { | |
121 | const u32 count; | |
122 | const struct lg4ff_wheel_ident_info *models[]; | |
123 | }; | |
124 | ||
b96d23ec MM |
125 | struct lg4ff_multimode_wheel { |
126 | const u16 product_id; | |
127 | const u32 alternate_modes; | |
128 | const char *real_tag; | |
129 | const char *real_name; | |
130 | }; | |
131 | ||
132 | struct lg4ff_alternate_mode { | |
133 | const u16 product_id; | |
134 | const char *tag; | |
135 | const char *name; | |
136 | }; | |
137 | ||
7362cd22 | 138 | static const struct lg4ff_wheel lg4ff_devices[] = { |
30bb75d7 MM |
139 | {USB_DEVICE_ID_LOGITECH_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}, |
140 | {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}, | |
d0afd848 MM |
141 | {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_dfp}, |
142 | {USB_DEVICE_ID_LOGITECH_G25_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25}, | |
143 | {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25}, | |
144 | {USB_DEVICE_ID_LOGITECH_G27_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25}, | |
30bb75d7 MM |
145 | {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL}, |
146 | {USB_DEVICE_ID_LOGITECH_WII_WHEEL, lg4ff_wheel_effects, 40, 270, NULL} | |
7362cd22 MM |
147 | }; |
148 | ||
b96d23ec MM |
149 | static const struct lg4ff_multimode_wheel lg4ff_multimode_wheels[] = { |
150 | {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, | |
151 | LG4FF_MODE_NATIVE | LG4FF_MODE_DFP | LG4FF_MODE_DFEX, | |
152 | LG4FF_DFP_TAG, LG4FF_DFP_NAME}, | |
153 | {USB_DEVICE_ID_LOGITECH_G25_WHEEL, | |
154 | LG4FF_MODE_NATIVE | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX, | |
155 | LG4FF_G25_TAG, LG4FF_G25_NAME}, | |
156 | {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, | |
157 | LG4FF_MODE_NATIVE | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX, | |
158 | LG4FF_DFGT_TAG, LG4FF_DFGT_NAME}, | |
159 | {USB_DEVICE_ID_LOGITECH_G27_WHEEL, | |
160 | LG4FF_MODE_NATIVE | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX, | |
161 | LG4FF_G27_TAG, LG4FF_G27_NAME}, | |
162 | }; | |
163 | ||
164 | static const struct lg4ff_alternate_mode lg4ff_alternate_modes[] = { | |
165 | [LG4FF_MODE_NATIVE_IDX] = {0, "native", ""}, | |
166 | [LG4FF_MODE_DFEX_IDX] = {USB_DEVICE_ID_LOGITECH_WHEEL, LG4FF_DFEX_TAG, LG4FF_DFEX_NAME}, | |
167 | [LG4FF_MODE_DFP_IDX] = {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, LG4FF_DFP_TAG, LG4FF_DFP_NAME}, | |
168 | [LG4FF_MODE_G25_IDX] = {USB_DEVICE_ID_LOGITECH_G25_WHEEL, LG4FF_G25_TAG, LG4FF_G25_NAME}, | |
169 | [LG4FF_MODE_DFGT_IDX] = {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, LG4FF_DFGT_TAG, LG4FF_DFGT_NAME}, | |
170 | [LG4FF_MODE_G27_IDX] = {USB_DEVICE_ID_LOGITECH_G27_WHEEL, LG4FF_G27_TAG, LG4FF_G27_NAME} | |
171 | }; | |
172 | ||
e7c23449 MM |
173 | /* Multimode wheel identificators */ |
174 | static const struct lg4ff_wheel_ident_info lg4ff_dfp_ident_info = { | |
175 | 0xf000, | |
176 | 0x1000, | |
177 | USB_DEVICE_ID_LOGITECH_DFP_WHEEL | |
178 | }; | |
179 | ||
180 | static const struct lg4ff_wheel_ident_info lg4ff_g25_ident_info = { | |
181 | 0xff00, | |
182 | 0x1200, | |
183 | USB_DEVICE_ID_LOGITECH_G25_WHEEL | |
184 | }; | |
185 | ||
186 | static const struct lg4ff_wheel_ident_info lg4ff_g27_ident_info = { | |
187 | 0xfff0, | |
188 | 0x1230, | |
189 | USB_DEVICE_ID_LOGITECH_G27_WHEEL | |
96440c8a MM |
190 | }; |
191 | ||
e7c23449 MM |
192 | static const struct lg4ff_wheel_ident_info lg4ff_dfgt_ident_info = { |
193 | 0xff00, | |
194 | 0x1300, | |
195 | USB_DEVICE_ID_LOGITECH_DFGT_WHEEL | |
96440c8a MM |
196 | }; |
197 | ||
e7c23449 MM |
198 | /* Multimode wheel identification checklists */ |
199 | static const struct lg4ff_wheel_ident_checklist lg4ff_main_checklist = { | |
200 | 4, | |
201 | {&lg4ff_dfgt_ident_info, | |
202 | &lg4ff_g27_ident_info, | |
203 | &lg4ff_g25_ident_info, | |
204 | &lg4ff_dfp_ident_info} | |
205 | }; | |
206 | ||
207 | /* Compatibility mode switching commands */ | |
f31a2de3 MM |
208 | /* EXT_CMD9 - Understood by G27 and DFGT */ |
209 | static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfex = { | |
210 | 2, | |
211 | {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */ | |
212 | 0xf8, 0x09, 0x00, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DF-EX with detach */ | |
96440c8a MM |
213 | }; |
214 | ||
f31a2de3 | 215 | static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfp = { |
96440c8a | 216 | 2, |
f31a2de3 MM |
217 | {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */ |
218 | 0xf8, 0x09, 0x01, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DFP with detach */ | |
96440c8a MM |
219 | }; |
220 | ||
f31a2de3 MM |
221 | static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g25 = { |
222 | 2, | |
223 | {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */ | |
224 | 0xf8, 0x09, 0x02, 0x01, 0x00, 0x00, 0x00} /* Switch mode to G25 with detach */ | |
96440c8a MM |
225 | }; |
226 | ||
f31a2de3 | 227 | static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfgt = { |
96440c8a | 228 | 2, |
f31a2de3 MM |
229 | {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */ |
230 | 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DFGT with detach */ | |
231 | }; | |
232 | ||
233 | static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g27 = { | |
234 | 2, | |
235 | {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */ | |
236 | 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* Switch mode to G27 with detach */ | |
237 | }; | |
238 | ||
239 | /* EXT_CMD1 - Understood by DFP, G25, G27 and DFGT */ | |
240 | static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext01_dfp = { | |
241 | 1, | |
242 | {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00} | |
243 | }; | |
244 | ||
245 | /* EXT_CMD16 - Understood by G25 and G27 */ | |
246 | static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext16_g25 = { | |
247 | 1, | |
248 | {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00} | |
96440c8a MM |
249 | }; |
250 | ||
2b24a960 | 251 | /* Recalculates X axis value accordingly to currently selected range */ |
2a552c30 | 252 | static s32 lg4ff_adjust_dfp_x_axis(s32 value, u16 range) |
2b24a960 | 253 | { |
2a552c30 MM |
254 | u16 max_range; |
255 | s32 new_value; | |
2b24a960 MM |
256 | |
257 | if (range == 900) | |
258 | return value; | |
259 | else if (range == 200) | |
260 | return value; | |
261 | else if (range < 200) | |
262 | max_range = 200; | |
263 | else | |
264 | max_range = 900; | |
265 | ||
266 | new_value = 8192 + mult_frac(value - 8192, max_range, range); | |
267 | if (new_value < 0) | |
268 | return 0; | |
269 | else if (new_value > 16383) | |
270 | return 16383; | |
271 | else | |
272 | return new_value; | |
273 | } | |
274 | ||
275 | int lg4ff_adjust_input_event(struct hid_device *hid, struct hid_field *field, | |
2a552c30 | 276 | struct hid_usage *usage, s32 value, struct lg_drv_data *drv_data) |
2b24a960 MM |
277 | { |
278 | struct lg4ff_device_entry *entry = drv_data->device_props; | |
2a552c30 | 279 | s32 new_value = 0; |
2b24a960 MM |
280 | |
281 | if (!entry) { | |
282 | hid_err(hid, "Device properties not found"); | |
283 | return 0; | |
284 | } | |
285 | ||
72529c65 | 286 | switch (entry->wdata.product_id) { |
2b24a960 MM |
287 | case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: |
288 | switch (usage->code) { | |
289 | case ABS_X: | |
72529c65 | 290 | new_value = lg4ff_adjust_dfp_x_axis(value, entry->wdata.range); |
2b24a960 MM |
291 | input_event(field->hidinput->input, usage->type, usage->code, new_value); |
292 | return 1; | |
293 | default: | |
294 | return 0; | |
295 | } | |
296 | default: | |
297 | return 0; | |
298 | } | |
299 | } | |
300 | ||
d0afd848 | 301 | static int lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect) |
32c88cbc SW |
302 | { |
303 | struct hid_device *hid = input_get_drvdata(dev); | |
304 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; | |
305 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); | |
2a552c30 | 306 | s32 *value = report->field[0]->value; |
32c88cbc SW |
307 | int x; |
308 | ||
a80fe5d6 | 309 | #define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0) |
32c88cbc SW |
310 | |
311 | switch (effect->type) { | |
312 | case FF_CONSTANT: | |
313 | x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */ | |
314 | CLAMP(x); | |
56930e7a SW |
315 | |
316 | if (x == 0x80) { | |
317 | /* De-activate force in slot-1*/ | |
318 | value[0] = 0x13; | |
319 | value[1] = 0x00; | |
320 | value[2] = 0x00; | |
321 | value[3] = 0x00; | |
322 | value[4] = 0x00; | |
323 | value[5] = 0x00; | |
324 | value[6] = 0x00; | |
325 | ||
326 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); | |
327 | return 0; | |
328 | } | |
329 | ||
74479ba8 MM |
330 | value[0] = 0x11; /* Slot 1 */ |
331 | value[1] = 0x08; | |
332 | value[2] = x; | |
333 | value[3] = 0x80; | |
334 | value[4] = 0x00; | |
335 | value[5] = 0x00; | |
336 | value[6] = 0x00; | |
32c88cbc | 337 | |
d8814272 | 338 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
32c88cbc SW |
339 | break; |
340 | } | |
341 | return 0; | |
342 | } | |
343 | ||
6e2de8e0 MM |
344 | /* Sends default autocentering command compatible with |
345 | * all wheels except Formula Force EX */ | |
d0afd848 | 346 | static void lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude) |
32c88cbc SW |
347 | { |
348 | struct hid_device *hid = input_get_drvdata(dev); | |
349 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; | |
350 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); | |
2a552c30 MM |
351 | s32 *value = report->field[0]->value; |
352 | u32 expand_a, expand_b; | |
1859762e SW |
353 | struct lg4ff_device_entry *entry; |
354 | struct lg_drv_data *drv_data; | |
355 | ||
356 | drv_data = hid_get_drvdata(hid); | |
357 | if (!drv_data) { | |
358 | hid_err(hid, "Private driver data not found!\n"); | |
359 | return; | |
360 | } | |
361 | ||
362 | entry = drv_data->device_props; | |
363 | if (!entry) { | |
364 | hid_err(hid, "Device properties not found!\n"); | |
365 | return; | |
366 | } | |
f8c23156 | 367 | |
d2c02da5 SW |
368 | /* De-activate Auto-Center */ |
369 | if (magnitude == 0) { | |
370 | value[0] = 0xf5; | |
371 | value[1] = 0x00; | |
372 | value[2] = 0x00; | |
373 | value[3] = 0x00; | |
374 | value[4] = 0x00; | |
375 | value[5] = 0x00; | |
376 | value[6] = 0x00; | |
377 | ||
378 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); | |
379 | return; | |
380 | } | |
381 | ||
f8c23156 SW |
382 | if (magnitude <= 0xaaaa) { |
383 | expand_a = 0x0c * magnitude; | |
384 | expand_b = 0x80 * magnitude; | |
385 | } else { | |
386 | expand_a = (0x0c * 0xaaaa) + 0x06 * (magnitude - 0xaaaa); | |
387 | expand_b = (0x80 * 0xaaaa) + 0xff * (magnitude - 0xaaaa); | |
388 | } | |
32c88cbc | 389 | |
1859762e | 390 | /* Adjust for non-MOMO wheels */ |
72529c65 | 391 | switch (entry->wdata.product_id) { |
1859762e SW |
392 | case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL: |
393 | case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2: | |
394 | break; | |
395 | default: | |
396 | expand_a = expand_a >> 1; | |
397 | break; | |
398 | } | |
399 | ||
74479ba8 MM |
400 | value[0] = 0xfe; |
401 | value[1] = 0x0d; | |
f8c23156 SW |
402 | value[2] = expand_a / 0xaaaa; |
403 | value[3] = expand_a / 0xaaaa; | |
404 | value[4] = expand_b / 0xaaaa; | |
74479ba8 MM |
405 | value[5] = 0x00; |
406 | value[6] = 0x00; | |
32c88cbc | 407 | |
d8814272 | 408 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
d2c02da5 SW |
409 | |
410 | /* Activate Auto-Center */ | |
411 | value[0] = 0x14; | |
412 | value[1] = 0x00; | |
413 | value[2] = 0x00; | |
414 | value[3] = 0x00; | |
415 | value[4] = 0x00; | |
416 | value[5] = 0x00; | |
417 | value[6] = 0x00; | |
418 | ||
419 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); | |
32c88cbc SW |
420 | } |
421 | ||
6e2de8e0 | 422 | /* Sends autocentering command compatible with Formula Force EX */ |
d0afd848 | 423 | static void lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude) |
6e2de8e0 MM |
424 | { |
425 | struct hid_device *hid = input_get_drvdata(dev); | |
426 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; | |
427 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); | |
2a552c30 | 428 | s32 *value = report->field[0]->value; |
6e2de8e0 | 429 | magnitude = magnitude * 90 / 65535; |
6e2de8e0 | 430 | |
74479ba8 MM |
431 | value[0] = 0xfe; |
432 | value[1] = 0x03; | |
433 | value[2] = magnitude >> 14; | |
434 | value[3] = magnitude >> 14; | |
435 | value[4] = magnitude; | |
436 | value[5] = 0x00; | |
437 | value[6] = 0x00; | |
6e2de8e0 | 438 | |
d8814272 | 439 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
6e2de8e0 MM |
440 | } |
441 | ||
30bb75d7 | 442 | /* Sends command to set range compatible with G25/G27/Driving Force GT */ |
d0afd848 | 443 | static void lg4ff_set_range_g25(struct hid_device *hid, u16 range) |
30bb75d7 MM |
444 | { |
445 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; | |
446 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); | |
2a552c30 | 447 | s32 *value = report->field[0]->value; |
74479ba8 | 448 | |
30bb75d7 MM |
449 | dbg_hid("G25/G27/DFGT: setting range to %u\n", range); |
450 | ||
74479ba8 MM |
451 | value[0] = 0xf8; |
452 | value[1] = 0x81; | |
453 | value[2] = range & 0x00ff; | |
454 | value[3] = (range & 0xff00) >> 8; | |
455 | value[4] = 0x00; | |
456 | value[5] = 0x00; | |
457 | value[6] = 0x00; | |
30bb75d7 | 458 | |
d8814272 | 459 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
30bb75d7 MM |
460 | } |
461 | ||
462 | /* Sends commands to set range compatible with Driving Force Pro wheel */ | |
d0afd848 | 463 | static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range) |
30bb75d7 MM |
464 | { |
465 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; | |
466 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); | |
467 | int start_left, start_right, full_range; | |
2a552c30 | 468 | s32 *value = report->field[0]->value; |
74479ba8 | 469 | |
30bb75d7 MM |
470 | dbg_hid("Driving Force Pro: setting range to %u\n", range); |
471 | ||
472 | /* Prepare "coarse" limit command */ | |
74479ba8 | 473 | value[0] = 0xf8; |
a80fe5d6 | 474 | value[1] = 0x00; /* Set later */ |
74479ba8 MM |
475 | value[2] = 0x00; |
476 | value[3] = 0x00; | |
477 | value[4] = 0x00; | |
478 | value[5] = 0x00; | |
479 | value[6] = 0x00; | |
30bb75d7 MM |
480 | |
481 | if (range > 200) { | |
482 | report->field[0]->value[1] = 0x03; | |
483 | full_range = 900; | |
484 | } else { | |
485 | report->field[0]->value[1] = 0x02; | |
486 | full_range = 200; | |
487 | } | |
d8814272 | 488 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
30bb75d7 MM |
489 | |
490 | /* Prepare "fine" limit command */ | |
74479ba8 MM |
491 | value[0] = 0x81; |
492 | value[1] = 0x0b; | |
493 | value[2] = 0x00; | |
494 | value[3] = 0x00; | |
495 | value[4] = 0x00; | |
496 | value[5] = 0x00; | |
497 | value[6] = 0x00; | |
30bb75d7 MM |
498 | |
499 | if (range == 200 || range == 900) { /* Do not apply any fine limit */ | |
d8814272 | 500 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
30bb75d7 MM |
501 | return; |
502 | } | |
503 | ||
504 | /* Construct fine limit command */ | |
505 | start_left = (((full_range - range + 1) * 2047) / full_range); | |
506 | start_right = 0xfff - start_left; | |
507 | ||
74479ba8 MM |
508 | value[2] = start_left >> 4; |
509 | value[3] = start_right >> 4; | |
510 | value[4] = 0xff; | |
511 | value[5] = (start_right & 0xe) << 4 | (start_left & 0xe); | |
512 | value[6] = 0xff; | |
30bb75d7 | 513 | |
d8814272 | 514 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
30bb75d7 MM |
515 | } |
516 | ||
f31a2de3 MM |
517 | static const struct lg4ff_compat_mode_switch *lg4ff_get_mode_switch_command(const u16 real_product_id, const u16 target_product_id) |
518 | { | |
519 | switch (real_product_id) { | |
520 | case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: | |
521 | switch (target_product_id) { | |
522 | case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: | |
523 | return &lg4ff_mode_switch_ext01_dfp; | |
524 | /* DFP can only be switched to its native mode */ | |
525 | default: | |
526 | return NULL; | |
527 | } | |
528 | break; | |
529 | case USB_DEVICE_ID_LOGITECH_G25_WHEEL: | |
530 | switch (target_product_id) { | |
531 | case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: | |
532 | return &lg4ff_mode_switch_ext01_dfp; | |
533 | case USB_DEVICE_ID_LOGITECH_G25_WHEEL: | |
534 | return &lg4ff_mode_switch_ext16_g25; | |
535 | /* G25 can only be switched to DFP mode or its native mode */ | |
536 | default: | |
537 | return NULL; | |
538 | } | |
539 | break; | |
540 | case USB_DEVICE_ID_LOGITECH_G27_WHEEL: | |
541 | switch (target_product_id) { | |
542 | case USB_DEVICE_ID_LOGITECH_WHEEL: | |
543 | return &lg4ff_mode_switch_ext09_dfex; | |
544 | case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: | |
545 | return &lg4ff_mode_switch_ext09_dfp; | |
546 | case USB_DEVICE_ID_LOGITECH_G25_WHEEL: | |
547 | return &lg4ff_mode_switch_ext09_g25; | |
548 | case USB_DEVICE_ID_LOGITECH_G27_WHEEL: | |
549 | return &lg4ff_mode_switch_ext09_g27; | |
550 | /* G27 can only be switched to DF-EX, DFP, G25 or its native mode */ | |
551 | default: | |
552 | return NULL; | |
553 | } | |
554 | break; | |
555 | case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL: | |
556 | switch (target_product_id) { | |
557 | case USB_DEVICE_ID_LOGITECH_WHEEL: | |
558 | return &lg4ff_mode_switch_ext09_dfex; | |
559 | case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: | |
560 | return &lg4ff_mode_switch_ext09_dfp; | |
561 | case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL: | |
562 | return &lg4ff_mode_switch_ext09_dfgt; | |
563 | /* DFGT can only be switched to DF-EX, DFP or its native mode */ | |
564 | default: | |
565 | return NULL; | |
566 | } | |
567 | break; | |
568 | /* No other wheels have multiple modes */ | |
569 | default: | |
570 | return NULL; | |
571 | } | |
572 | } | |
573 | ||
e7c23449 | 574 | static int lg4ff_switch_compatibility_mode(struct hid_device *hid, const struct lg4ff_compat_mode_switch *s) |
96440c8a | 575 | { |
c1740d13 MM |
576 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; |
577 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); | |
2a552c30 | 578 | s32 *value = report->field[0]->value; |
e7c23449 | 579 | u8 i; |
96440c8a | 580 | |
e7c23449 | 581 | for (i = 0; i < s->cmd_count; i++) { |
c1740d13 | 582 | u8 j; |
96440c8a | 583 | |
c1740d13 MM |
584 | for (j = 0; j < 7; j++) |
585 | value[j] = s->cmd[j + (7*i)]; | |
586 | ||
587 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); | |
96440c8a | 588 | } |
c1740d13 | 589 | hid_hw_wait(hid); |
e7c23449 | 590 | return 0; |
96440c8a | 591 | } |
32c88cbc | 592 | |
b96d23ec MM |
593 | static ssize_t lg4ff_alternate_modes_show(struct device *dev, struct device_attribute *attr, char *buf) |
594 | { | |
595 | struct hid_device *hid = to_hid_device(dev); | |
596 | struct lg4ff_device_entry *entry; | |
597 | struct lg_drv_data *drv_data; | |
598 | ssize_t count = 0; | |
599 | int i; | |
600 | ||
601 | drv_data = hid_get_drvdata(hid); | |
602 | if (!drv_data) { | |
603 | hid_err(hid, "Private driver data not found!\n"); | |
604 | return 0; | |
605 | } | |
606 | ||
607 | entry = drv_data->device_props; | |
608 | if (!entry) { | |
609 | hid_err(hid, "Device properties not found!\n"); | |
610 | return 0; | |
611 | } | |
612 | ||
72529c65 | 613 | if (!entry->wdata.real_name) { |
b96d23ec MM |
614 | hid_err(hid, "NULL pointer to string\n"); |
615 | return 0; | |
616 | } | |
617 | ||
618 | for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) { | |
72529c65 | 619 | if (entry->wdata.alternate_modes & BIT(i)) { |
b96d23ec MM |
620 | /* Print tag and full name */ |
621 | count += scnprintf(buf + count, PAGE_SIZE - count, "%s: %s", | |
622 | lg4ff_alternate_modes[i].tag, | |
72529c65 | 623 | !lg4ff_alternate_modes[i].product_id ? entry->wdata.real_name : lg4ff_alternate_modes[i].name); |
b96d23ec MM |
624 | if (count >= PAGE_SIZE - 1) |
625 | return count; | |
626 | ||
627 | /* Mark the currently active mode with an asterisk */ | |
72529c65 MM |
628 | if (lg4ff_alternate_modes[i].product_id == entry->wdata.product_id || |
629 | (lg4ff_alternate_modes[i].product_id == 0 && entry->wdata.product_id == entry->wdata.real_product_id)) | |
b96d23ec MM |
630 | count += scnprintf(buf + count, PAGE_SIZE - count, " *\n"); |
631 | else | |
632 | count += scnprintf(buf + count, PAGE_SIZE - count, "\n"); | |
633 | ||
634 | if (count >= PAGE_SIZE - 1) | |
635 | return count; | |
636 | } | |
637 | } | |
638 | ||
639 | return count; | |
640 | } | |
641 | ||
642 | static ssize_t lg4ff_alternate_modes_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) | |
643 | { | |
f31a2de3 MM |
644 | struct hid_device *hid = to_hid_device(dev); |
645 | struct lg4ff_device_entry *entry; | |
646 | struct lg_drv_data *drv_data; | |
647 | const struct lg4ff_compat_mode_switch *s; | |
648 | u16 target_product_id = 0; | |
649 | int i, ret; | |
650 | char *lbuf; | |
651 | ||
652 | drv_data = hid_get_drvdata(hid); | |
653 | if (!drv_data) { | |
654 | hid_err(hid, "Private driver data not found!\n"); | |
655 | return -EINVAL; | |
656 | } | |
657 | ||
658 | entry = drv_data->device_props; | |
659 | if (!entry) { | |
660 | hid_err(hid, "Device properties not found!\n"); | |
661 | return -EINVAL; | |
662 | } | |
663 | ||
664 | /* Allow \n at the end of the input parameter */ | |
665 | lbuf = kasprintf(GFP_KERNEL, "%s", buf); | |
666 | if (!lbuf) | |
667 | return -ENOMEM; | |
668 | ||
669 | i = strlen(lbuf); | |
670 | if (lbuf[i-1] == '\n') { | |
671 | if (i == 1) { | |
672 | kfree(lbuf); | |
673 | return -EINVAL; | |
674 | } | |
675 | lbuf[i-1] = '\0'; | |
676 | } | |
677 | ||
678 | for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) { | |
679 | const u16 mode_product_id = lg4ff_alternate_modes[i].product_id; | |
680 | const char *tag = lg4ff_alternate_modes[i].tag; | |
681 | ||
72529c65 | 682 | if (entry->wdata.alternate_modes & BIT(i)) { |
f31a2de3 MM |
683 | if (!strcmp(tag, lbuf)) { |
684 | if (!mode_product_id) | |
72529c65 | 685 | target_product_id = entry->wdata.real_product_id; |
f31a2de3 MM |
686 | else |
687 | target_product_id = mode_product_id; | |
688 | break; | |
689 | } | |
690 | } | |
691 | } | |
692 | ||
693 | if (i == LG4FF_MODE_MAX_IDX) { | |
694 | hid_info(hid, "Requested mode \"%s\" is not supported by the device\n", lbuf); | |
695 | kfree(lbuf); | |
696 | return -EINVAL; | |
697 | } | |
698 | kfree(lbuf); /* Not needed anymore */ | |
699 | ||
72529c65 | 700 | if (target_product_id == entry->wdata.product_id) /* Nothing to do */ |
f31a2de3 MM |
701 | return count; |
702 | ||
703 | /* Automatic switching has to be disabled for the switch to DF-EX mode to work correctly */ | |
704 | if (target_product_id == USB_DEVICE_ID_LOGITECH_WHEEL && !lg4ff_no_autoswitch) { | |
705 | 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", | |
72529c65 | 706 | entry->wdata.real_name); |
f31a2de3 MM |
707 | return -EINVAL; |
708 | } | |
709 | ||
710 | /* Take care of hardware limitations */ | |
72529c65 MM |
711 | if ((entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_DFP_WHEEL || entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_G25_WHEEL) && |
712 | entry->wdata.product_id > target_product_id) { | |
713 | hid_info(hid, "\"%s\" cannot be switched back into \"%s\" mode\n", entry->wdata.real_name, lg4ff_alternate_modes[i].name); | |
f31a2de3 MM |
714 | return -EINVAL; |
715 | } | |
716 | ||
72529c65 | 717 | s = lg4ff_get_mode_switch_command(entry->wdata.real_product_id, target_product_id); |
f31a2de3 MM |
718 | if (!s) { |
719 | hid_err(hid, "Invalid target product ID %X\n", target_product_id); | |
720 | return -EINVAL; | |
721 | } | |
722 | ||
723 | ret = lg4ff_switch_compatibility_mode(hid, s); | |
724 | return (ret == 0 ? count : ret); | |
b96d23ec MM |
725 | } |
726 | static DEVICE_ATTR(alternate_modes, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_alternate_modes_show, lg4ff_alternate_modes_store); | |
727 | ||
fbf85e2a MM |
728 | /* Export the currently set range of the wheel */ |
729 | static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, | |
730 | char *buf) | |
30bb75d7 | 731 | { |
30bb75d7 | 732 | struct hid_device *hid = to_hid_device(dev); |
3b6b17b7 MM |
733 | struct lg4ff_device_entry *entry; |
734 | struct lg_drv_data *drv_data; | |
30bb75d7 MM |
735 | size_t count; |
736 | ||
3b6b17b7 MM |
737 | drv_data = hid_get_drvdata(hid); |
738 | if (!drv_data) { | |
739 | hid_err(hid, "Private driver data not found!\n"); | |
740 | return 0; | |
30bb75d7 | 741 | } |
3b6b17b7 MM |
742 | |
743 | entry = drv_data->device_props; | |
744 | if (!entry) { | |
745 | hid_err(hid, "Device properties not found!\n"); | |
30bb75d7 MM |
746 | return 0; |
747 | } | |
748 | ||
72529c65 | 749 | count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->wdata.range); |
30bb75d7 MM |
750 | return count; |
751 | } | |
752 | ||
753 | /* Set range to user specified value, call appropriate function | |
754 | * according to the type of the wheel */ | |
fbf85e2a MM |
755 | static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, |
756 | const char *buf, size_t count) | |
30bb75d7 | 757 | { |
30bb75d7 | 758 | struct hid_device *hid = to_hid_device(dev); |
3b6b17b7 MM |
759 | struct lg4ff_device_entry *entry; |
760 | struct lg_drv_data *drv_data; | |
2a552c30 | 761 | u16 range = simple_strtoul(buf, NULL, 10); |
30bb75d7 | 762 | |
3b6b17b7 MM |
763 | drv_data = hid_get_drvdata(hid); |
764 | if (!drv_data) { | |
765 | hid_err(hid, "Private driver data not found!\n"); | |
29ff6657 | 766 | return -EINVAL; |
30bb75d7 | 767 | } |
3b6b17b7 MM |
768 | |
769 | entry = drv_data->device_props; | |
770 | if (!entry) { | |
771 | hid_err(hid, "Device properties not found!\n"); | |
29ff6657 | 772 | return -EINVAL; |
30bb75d7 MM |
773 | } |
774 | ||
775 | if (range == 0) | |
72529c65 | 776 | range = entry->wdata.max_range; |
30bb75d7 MM |
777 | |
778 | /* Check if the wheel supports range setting | |
779 | * and that the range is within limits for the wheel */ | |
72529c65 MM |
780 | if (entry->wdata.set_range && range >= entry->wdata.min_range && range <= entry->wdata.max_range) { |
781 | entry->wdata.set_range(hid, range); | |
782 | entry->wdata.range = range; | |
30bb75d7 MM |
783 | } |
784 | ||
785 | return count; | |
786 | } | |
fbf85e2a | 787 | static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_range_show, lg4ff_range_store); |
30bb75d7 | 788 | |
b96d23ec MM |
789 | static ssize_t lg4ff_real_id_show(struct device *dev, struct device_attribute *attr, char *buf) |
790 | { | |
791 | struct hid_device *hid = to_hid_device(dev); | |
792 | struct lg4ff_device_entry *entry; | |
793 | struct lg_drv_data *drv_data; | |
794 | size_t count; | |
795 | ||
796 | drv_data = hid_get_drvdata(hid); | |
797 | if (!drv_data) { | |
798 | hid_err(hid, "Private driver data not found!\n"); | |
799 | return 0; | |
800 | } | |
801 | ||
802 | entry = drv_data->device_props; | |
803 | if (!entry) { | |
804 | hid_err(hid, "Device properties not found!\n"); | |
805 | return 0; | |
806 | } | |
807 | ||
72529c65 | 808 | if (!entry->wdata.real_tag || !entry->wdata.real_name) { |
b96d23ec MM |
809 | hid_err(hid, "NULL pointer to string\n"); |
810 | return 0; | |
811 | } | |
812 | ||
72529c65 | 813 | count = scnprintf(buf, PAGE_SIZE, "%s: %s\n", entry->wdata.real_tag, entry->wdata.real_name); |
b96d23ec MM |
814 | return count; |
815 | } | |
816 | ||
817 | static ssize_t lg4ff_real_id_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) | |
818 | { | |
819 | /* Real ID is a read-only value */ | |
820 | return -EPERM; | |
821 | } | |
822 | static DEVICE_ATTR(real_id, S_IRUGO, lg4ff_real_id_show, lg4ff_real_id_store); | |
823 | ||
22bcefdc | 824 | #ifdef CONFIG_LEDS_CLASS |
2a552c30 | 825 | static void lg4ff_set_leds(struct hid_device *hid, u8 leds) |
22bcefdc SW |
826 | { |
827 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; | |
828 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); | |
2a552c30 | 829 | s32 *value = report->field[0]->value; |
74479ba8 MM |
830 | |
831 | value[0] = 0xf8; | |
832 | value[1] = 0x12; | |
833 | value[2] = leds; | |
834 | value[3] = 0x00; | |
835 | value[4] = 0x00; | |
836 | value[5] = 0x00; | |
837 | value[6] = 0x00; | |
d8814272 | 838 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
22bcefdc SW |
839 | } |
840 | ||
841 | static void lg4ff_led_set_brightness(struct led_classdev *led_cdev, | |
842 | enum led_brightness value) | |
843 | { | |
844 | struct device *dev = led_cdev->dev->parent; | |
845 | struct hid_device *hid = container_of(dev, struct hid_device, dev); | |
4629fd16 | 846 | struct lg_drv_data *drv_data = hid_get_drvdata(hid); |
22bcefdc SW |
847 | struct lg4ff_device_entry *entry; |
848 | int i, state = 0; | |
849 | ||
850 | if (!drv_data) { | |
851 | hid_err(hid, "Device data not found."); | |
852 | return; | |
853 | } | |
854 | ||
371a1d9e | 855 | entry = drv_data->device_props; |
22bcefdc SW |
856 | |
857 | if (!entry) { | |
858 | hid_err(hid, "Device properties not found."); | |
859 | return; | |
860 | } | |
861 | ||
862 | for (i = 0; i < 5; i++) { | |
72529c65 | 863 | if (led_cdev != entry->wdata.led[i]) |
22bcefdc | 864 | continue; |
72529c65 | 865 | state = (entry->wdata.led_state >> i) & 1; |
22bcefdc | 866 | if (value == LED_OFF && state) { |
72529c65 MM |
867 | entry->wdata.led_state &= ~(1 << i); |
868 | lg4ff_set_leds(hid, entry->wdata.led_state); | |
22bcefdc | 869 | } else if (value != LED_OFF && !state) { |
72529c65 MM |
870 | entry->wdata.led_state |= 1 << i; |
871 | lg4ff_set_leds(hid, entry->wdata.led_state); | |
22bcefdc SW |
872 | } |
873 | break; | |
874 | } | |
875 | } | |
876 | ||
877 | static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev) | |
878 | { | |
879 | struct device *dev = led_cdev->dev->parent; | |
880 | struct hid_device *hid = container_of(dev, struct hid_device, dev); | |
4629fd16 | 881 | struct lg_drv_data *drv_data = hid_get_drvdata(hid); |
22bcefdc SW |
882 | struct lg4ff_device_entry *entry; |
883 | int i, value = 0; | |
884 | ||
885 | if (!drv_data) { | |
886 | hid_err(hid, "Device data not found."); | |
887 | return LED_OFF; | |
888 | } | |
889 | ||
371a1d9e | 890 | entry = drv_data->device_props; |
22bcefdc SW |
891 | |
892 | if (!entry) { | |
893 | hid_err(hid, "Device properties not found."); | |
894 | return LED_OFF; | |
895 | } | |
896 | ||
897 | for (i = 0; i < 5; i++) | |
72529c65 MM |
898 | if (led_cdev == entry->wdata.led[i]) { |
899 | value = (entry->wdata.led_state >> i) & 1; | |
22bcefdc SW |
900 | break; |
901 | } | |
902 | ||
903 | return value ? LED_FULL : LED_OFF; | |
904 | } | |
905 | #endif | |
906 | ||
e7c23449 MM |
907 | static u16 lg4ff_identify_multimode_wheel(struct hid_device *hid, const u16 reported_product_id, const u16 bcdDevice) |
908 | { | |
909 | const struct lg4ff_wheel_ident_checklist *checklist; | |
910 | int i, from_idx, to_idx; | |
911 | ||
912 | switch (reported_product_id) { | |
913 | case USB_DEVICE_ID_LOGITECH_WHEEL: | |
914 | case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: | |
915 | checklist = &lg4ff_main_checklist; | |
916 | from_idx = 0; | |
917 | to_idx = checklist->count - 1; | |
918 | break; | |
919 | case USB_DEVICE_ID_LOGITECH_G25_WHEEL: | |
920 | checklist = &lg4ff_main_checklist; | |
921 | from_idx = 0; | |
922 | to_idx = checklist->count - 2; /* End identity check at G25 */ | |
923 | break; | |
924 | case USB_DEVICE_ID_LOGITECH_G27_WHEEL: | |
925 | checklist = &lg4ff_main_checklist; | |
926 | from_idx = 1; /* Start identity check at G27 */ | |
927 | to_idx = checklist->count - 3; /* End identity check at G27 */ | |
928 | break; | |
929 | case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL: | |
930 | checklist = &lg4ff_main_checklist; | |
931 | from_idx = 0; | |
932 | to_idx = checklist->count - 4; /* End identity check at DFGT */ | |
933 | break; | |
934 | default: | |
935 | return 0; | |
936 | } | |
937 | ||
938 | for (i = from_idx; i <= to_idx; i++) { | |
939 | const u16 mask = checklist->models[i]->mask; | |
940 | const u16 result = checklist->models[i]->result; | |
941 | const u16 real_product_id = checklist->models[i]->real_product_id; | |
942 | ||
943 | if ((bcdDevice & mask) == result) { | |
944 | dbg_hid("Found wheel with real PID %X whose reported PID is %X\n", real_product_id, reported_product_id); | |
945 | return real_product_id; | |
946 | } | |
947 | } | |
948 | ||
f31a2de3 MM |
949 | /* No match found. This is either Driving Force or an unknown |
950 | * wheel model, do not touch it */ | |
e7c23449 MM |
951 | dbg_hid("Wheel with bcdDevice %X was not recognized as multimode wheel, leaving in its current mode\n", bcdDevice); |
952 | return 0; | |
953 | } | |
954 | ||
955 | static int lg4ff_handle_multimode_wheel(struct hid_device *hid, u16 *real_product_id, const u16 bcdDevice) | |
956 | { | |
957 | const u16 reported_product_id = hid->product; | |
958 | int ret; | |
959 | ||
960 | *real_product_id = lg4ff_identify_multimode_wheel(hid, reported_product_id, bcdDevice); | |
961 | /* Probed wheel is not a multimode wheel */ | |
962 | if (!*real_product_id) { | |
963 | *real_product_id = reported_product_id; | |
964 | dbg_hid("Wheel is not a multimode wheel\n"); | |
965 | return LG4FF_MMODE_NOT_MULTIMODE; | |
966 | } | |
967 | ||
968 | /* Switch from "Driving Force" mode to native mode automatically. | |
969 | * Otherwise keep the wheel in its current mode */ | |
970 | if (reported_product_id == USB_DEVICE_ID_LOGITECH_WHEEL && | |
a54dc779 MM |
971 | reported_product_id != *real_product_id && |
972 | !lg4ff_no_autoswitch) { | |
f31a2de3 | 973 | const struct lg4ff_compat_mode_switch *s = lg4ff_get_mode_switch_command(*real_product_id, *real_product_id); |
e7c23449 | 974 | |
f31a2de3 | 975 | if (!s) { |
e7c23449 | 976 | hid_err(hid, "Invalid product id %X\n", *real_product_id); |
b96d23ec | 977 | return LG4FF_MMODE_NOT_MULTIMODE; |
e7c23449 MM |
978 | } |
979 | ||
980 | ret = lg4ff_switch_compatibility_mode(hid, s); | |
981 | if (ret) { | |
982 | /* Wheel could not have been switched to native mode, | |
983 | * leave it in "Driving Force" mode and continue */ | |
984 | hid_err(hid, "Unable to switch wheel mode, errno %d\n", ret); | |
b96d23ec | 985 | return LG4FF_MMODE_IS_MULTIMODE; |
e7c23449 MM |
986 | } |
987 | return LG4FF_MMODE_SWITCHED; | |
988 | } | |
989 | ||
b96d23ec | 990 | return LG4FF_MMODE_IS_MULTIMODE; |
e7c23449 MM |
991 | } |
992 | ||
993 | ||
32c88cbc SW |
994 | int lg4ff_init(struct hid_device *hid) |
995 | { | |
996 | struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); | |
32c88cbc | 997 | struct input_dev *dev = hidinput->input; |
e7c23449 MM |
998 | const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor); |
999 | const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice); | |
30bb75d7 | 1000 | struct lg4ff_device_entry *entry; |
3b6b17b7 | 1001 | struct lg_drv_data *drv_data; |
b96d23ec MM |
1002 | int error, i, j; |
1003 | int mmode_ret, mmode_idx = -1; | |
e7c23449 | 1004 | u16 real_product_id; |
32c88cbc | 1005 | |
32c88cbc | 1006 | /* Check that the report looks ok */ |
0fb6bd06 | 1007 | if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7)) |
32c88cbc | 1008 | return -1; |
30bb75d7 | 1009 | |
72529c65 MM |
1010 | drv_data = hid_get_drvdata(hid); |
1011 | if (!drv_data) { | |
1012 | hid_err(hid, "Cannot add device, private driver data not allocated\n"); | |
1013 | return -1; | |
1014 | } | |
1015 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); | |
1016 | if (!entry) | |
1017 | return -ENOMEM; | |
1018 | drv_data->device_props = entry; | |
1019 | ||
e7c23449 MM |
1020 | /* Check if a multimode wheel has been connected and |
1021 | * handle it appropriately */ | |
b96d23ec | 1022 | mmode_ret = lg4ff_handle_multimode_wheel(hid, &real_product_id, bcdDevice); |
e7c23449 MM |
1023 | |
1024 | /* Wheel has been told to switch to native mode. There is no point in going on | |
1025 | * with the initialization as the wheel will do a USB reset when it switches mode | |
1026 | */ | |
b96d23ec | 1027 | if (mmode_ret == LG4FF_MMODE_SWITCHED) |
e7c23449 | 1028 | return 0; |
72529c65 MM |
1029 | else if (mmode_ret < 0) { |
1030 | hid_err(hid, "Unable to switch device mode during initialization, errno %d\n", mmode_ret); | |
1031 | error = mmode_ret; | |
1032 | goto err_init; | |
1033 | } | |
e7c23449 | 1034 | |
7362cd22 MM |
1035 | /* Check what wheel has been connected */ |
1036 | for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) { | |
1037 | if (hid->product == lg4ff_devices[i].product_id) { | |
1038 | dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id); | |
1039 | break; | |
1040 | } | |
1041 | } | |
1042 | ||
1043 | if (i == ARRAY_SIZE(lg4ff_devices)) { | |
9c2a6bd1 MM |
1044 | hid_err(hid, "This device is flagged to be handled by the lg4ff module but this module does not know how to handle it. " |
1045 | "Please report this as a bug to LKML, Simon Wood <simon@mungewell.org> or " | |
1046 | "Michal Maly <madcatxster@devoid-pointer.net>\n"); | |
72529c65 MM |
1047 | error = -1; |
1048 | goto err_init; | |
7362cd22 | 1049 | } |
32c88cbc | 1050 | |
b96d23ec MM |
1051 | if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) { |
1052 | for (mmode_idx = 0; mmode_idx < ARRAY_SIZE(lg4ff_multimode_wheels); mmode_idx++) { | |
1053 | if (real_product_id == lg4ff_multimode_wheels[mmode_idx].product_id) | |
1054 | break; | |
1055 | } | |
1056 | ||
1057 | if (mmode_idx == ARRAY_SIZE(lg4ff_multimode_wheels)) { | |
1058 | hid_err(hid, "Device product ID %X is not listed as a multimode wheel", real_product_id); | |
72529c65 MM |
1059 | error = -1; |
1060 | goto err_init; | |
b96d23ec MM |
1061 | } |
1062 | } | |
1063 | ||
7362cd22 MM |
1064 | /* Set supported force feedback capabilities */ |
1065 | for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++) | |
1066 | set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit); | |
32c88cbc | 1067 | |
d0afd848 | 1068 | error = input_ff_create_memless(dev, NULL, lg4ff_play); |
32c88cbc SW |
1069 | |
1070 | if (error) | |
72529c65 | 1071 | goto err_init; |
30bb75d7 | 1072 | |
72529c65 MM |
1073 | entry->wdata.product_id = lg4ff_devices[i].product_id; |
1074 | entry->wdata.real_product_id = real_product_id; | |
1075 | entry->wdata.min_range = lg4ff_devices[i].min_range; | |
1076 | entry->wdata.max_range = lg4ff_devices[i].max_range; | |
1077 | entry->wdata.set_range = lg4ff_devices[i].set_range; | |
b96d23ec MM |
1078 | if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) { |
1079 | BUG_ON(mmode_idx == -1); | |
72529c65 MM |
1080 | entry->wdata.alternate_modes = lg4ff_multimode_wheels[mmode_idx].alternate_modes; |
1081 | entry->wdata.real_tag = lg4ff_multimode_wheels[mmode_idx].real_tag; | |
1082 | entry->wdata.real_name = lg4ff_multimode_wheels[mmode_idx].real_name; | |
b96d23ec | 1083 | } |
30bb75d7 | 1084 | |
114a55cf SW |
1085 | /* Check if autocentering is available and |
1086 | * set the centering force to zero by default */ | |
1087 | if (test_bit(FF_AUTOCENTER, dev->ffbit)) { | |
e7c23449 MM |
1088 | /* Formula Force EX expects different autocentering command */ |
1089 | if ((bcdDevice >> 8) == LG4FF_FFEX_REV_MAJ && | |
1090 | (bcdDevice & 0xff) == LG4FF_FFEX_REV_MIN) | |
d0afd848 | 1091 | dev->ff->set_autocenter = lg4ff_set_autocenter_ffex; |
114a55cf | 1092 | else |
d0afd848 | 1093 | dev->ff->set_autocenter = lg4ff_set_autocenter_default; |
114a55cf SW |
1094 | |
1095 | dev->ff->set_autocenter(dev, 0); | |
1096 | } | |
1097 | ||
30bb75d7 MM |
1098 | /* Create sysfs interface */ |
1099 | error = device_create_file(&hid->dev, &dev_attr_range); | |
1100 | if (error) | |
72529c65 | 1101 | goto err_init; |
b96d23ec MM |
1102 | if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) { |
1103 | error = device_create_file(&hid->dev, &dev_attr_real_id); | |
1104 | if (error) | |
72529c65 MM |
1105 | goto err_init; |
1106 | ||
b96d23ec MM |
1107 | error = device_create_file(&hid->dev, &dev_attr_alternate_modes); |
1108 | if (error) | |
72529c65 | 1109 | goto err_init; |
b96d23ec | 1110 | } |
30bb75d7 MM |
1111 | dbg_hid("sysfs interface created\n"); |
1112 | ||
1113 | /* Set the maximum range to start with */ | |
72529c65 MM |
1114 | entry->wdata.range = entry->wdata.max_range; |
1115 | if (entry->wdata.set_range) | |
1116 | entry->wdata.set_range(hid, entry->wdata.range); | |
30bb75d7 | 1117 | |
22bcefdc SW |
1118 | #ifdef CONFIG_LEDS_CLASS |
1119 | /* register led subsystem - G27 only */ | |
72529c65 | 1120 | entry->wdata.led_state = 0; |
22bcefdc | 1121 | for (j = 0; j < 5; j++) |
72529c65 | 1122 | entry->wdata.led[j] = NULL; |
22bcefdc SW |
1123 | |
1124 | if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL) { | |
1125 | struct led_classdev *led; | |
1126 | size_t name_sz; | |
1127 | char *name; | |
1128 | ||
1129 | lg4ff_set_leds(hid, 0); | |
1130 | ||
1131 | name_sz = strlen(dev_name(&hid->dev)) + 8; | |
1132 | ||
1133 | for (j = 0; j < 5; j++) { | |
1134 | led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL); | |
1135 | if (!led) { | |
1136 | hid_err(hid, "can't allocate memory for LED %d\n", j); | |
72529c65 | 1137 | goto err_leds; |
22bcefdc SW |
1138 | } |
1139 | ||
1140 | name = (void *)(&led[1]); | |
1141 | snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1); | |
1142 | led->name = name; | |
1143 | led->brightness = 0; | |
1144 | led->max_brightness = 1; | |
1145 | led->brightness_get = lg4ff_led_get_brightness; | |
1146 | led->brightness_set = lg4ff_led_set_brightness; | |
1147 | ||
72529c65 | 1148 | entry->wdata.led[j] = led; |
22bcefdc SW |
1149 | error = led_classdev_register(&hid->dev, led); |
1150 | ||
1151 | if (error) { | |
1152 | hid_err(hid, "failed to register LED %d. Aborting.\n", j); | |
72529c65 | 1153 | err_leds: |
22bcefdc SW |
1154 | /* Deregister LEDs (if any) */ |
1155 | for (j = 0; j < 5; j++) { | |
72529c65 MM |
1156 | led = entry->wdata.led[j]; |
1157 | entry->wdata.led[j] = NULL; | |
22bcefdc SW |
1158 | if (!led) |
1159 | continue; | |
1160 | led_classdev_unregister(led); | |
1161 | kfree(led); | |
1162 | } | |
1163 | goto out; /* Let the driver continue without LEDs */ | |
1164 | } | |
1165 | } | |
1166 | } | |
22bcefdc | 1167 | out: |
c6e6dc87 | 1168 | #endif |
64013800 | 1169 | hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n"); |
32c88cbc | 1170 | return 0; |
72529c65 MM |
1171 | |
1172 | err_init: | |
1173 | drv_data->device_props = NULL; | |
1174 | kfree(entry); | |
1175 | return error; | |
32c88cbc SW |
1176 | } |
1177 | ||
30bb75d7 MM |
1178 | int lg4ff_deinit(struct hid_device *hid) |
1179 | { | |
30bb75d7 | 1180 | struct lg4ff_device_entry *entry; |
3b6b17b7 MM |
1181 | struct lg_drv_data *drv_data; |
1182 | ||
3b6b17b7 MM |
1183 | drv_data = hid_get_drvdata(hid); |
1184 | if (!drv_data) { | |
1185 | hid_err(hid, "Error while deinitializing device, no private driver data.\n"); | |
1186 | return -1; | |
30bb75d7 | 1187 | } |
3b6b17b7 | 1188 | entry = drv_data->device_props; |
e7c23449 MM |
1189 | if (!entry) |
1190 | goto out; /* Nothing more to do */ | |
1191 | ||
b96d23ec | 1192 | /* Multimode devices will have at least the "MODE_NATIVE" bit set */ |
72529c65 | 1193 | if (entry->wdata.alternate_modes) { |
b96d23ec MM |
1194 | device_remove_file(&hid->dev, &dev_attr_real_id); |
1195 | device_remove_file(&hid->dev, &dev_attr_alternate_modes); | |
1196 | } | |
1197 | ||
72529c65 | 1198 | device_remove_file(&hid->dev, &dev_attr_range); |
22bcefdc SW |
1199 | #ifdef CONFIG_LEDS_CLASS |
1200 | { | |
1201 | int j; | |
1202 | struct led_classdev *led; | |
1203 | ||
1204 | /* Deregister LEDs (if any) */ | |
1205 | for (j = 0; j < 5; j++) { | |
1206 | ||
72529c65 MM |
1207 | led = entry->wdata.led[j]; |
1208 | entry->wdata.led[j] = NULL; | |
22bcefdc SW |
1209 | if (!led) |
1210 | continue; | |
1211 | led_classdev_unregister(led); | |
1212 | kfree(led); | |
1213 | } | |
1214 | } | |
1215 | #endif | |
b211a638 MM |
1216 | hid_hw_stop(hid); |
1217 | drv_data->device_props = NULL; | |
22bcefdc | 1218 | |
3b6b17b7 | 1219 | kfree(entry); |
e7c23449 | 1220 | out: |
30bb75d7 MM |
1221 | dbg_hid("Device successfully unregistered\n"); |
1222 | return 0; | |
1223 | } |