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