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" | |
7362cd22 | 33 | #include "hid-ids.h" |
32c88cbc | 34 | |
96440c8a MM |
35 | #define DFGT_REV_MAJ 0x13 |
36 | #define DFGT_REV_MIN 0x22 | |
d991938a | 37 | #define DFGT2_REV_MIN 0x26 |
96440c8a MM |
38 | #define DFP_REV_MAJ 0x11 |
39 | #define DFP_REV_MIN 0x06 | |
40 | #define FFEX_REV_MAJ 0x21 | |
41 | #define FFEX_REV_MIN 0x00 | |
42 | #define G25_REV_MAJ 0x12 | |
43 | #define G25_REV_MIN 0x22 | |
44 | #define G27_REV_MAJ 0x12 | |
45 | #define G27_REV_MIN 0x38 | |
46 | ||
30bb75d7 MM |
47 | #define to_hid_device(pdev) container_of(pdev, struct hid_device, dev) |
48 | ||
49 | static void hid_lg4ff_set_range_dfp(struct hid_device *hid, u16 range); | |
50 | static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range); | |
51 | static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf); | |
52 | static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count); | |
53 | ||
54 | static DEVICE_ATTR(range, S_IRWXU | S_IRWXG | S_IRWXO, lg4ff_range_show, lg4ff_range_store); | |
55 | ||
30bb75d7 | 56 | struct lg4ff_device_entry { |
2b24a960 | 57 | __u32 product_id; |
30bb75d7 MM |
58 | __u16 range; |
59 | __u16 min_range; | |
60 | __u16 max_range; | |
22bcefdc SW |
61 | #ifdef CONFIG_LEDS_CLASS |
62 | __u8 led_state; | |
63 | struct led_classdev *led[5]; | |
64 | #endif | |
30bb75d7 MM |
65 | struct list_head list; |
66 | void (*set_range)(struct hid_device *hid, u16 range); | |
67 | }; | |
68 | ||
7362cd22 | 69 | static const signed short lg4ff_wheel_effects[] = { |
32c88cbc SW |
70 | FF_CONSTANT, |
71 | FF_AUTOCENTER, | |
72 | -1 | |
73 | }; | |
74 | ||
7362cd22 MM |
75 | struct lg4ff_wheel { |
76 | const __u32 product_id; | |
77 | const signed short *ff_effects; | |
78 | const __u16 min_range; | |
79 | const __u16 max_range; | |
30bb75d7 | 80 | void (*set_range)(struct hid_device *hid, u16 range); |
7362cd22 MM |
81 | }; |
82 | ||
83 | static const struct lg4ff_wheel lg4ff_devices[] = { | |
30bb75d7 MM |
84 | {USB_DEVICE_ID_LOGITECH_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}, |
85 | {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}, | |
86 | {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_dfp}, | |
87 | {USB_DEVICE_ID_LOGITECH_G25_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25}, | |
88 | {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25}, | |
89 | {USB_DEVICE_ID_LOGITECH_G27_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25}, | |
90 | {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL}, | |
91 | {USB_DEVICE_ID_LOGITECH_WII_WHEEL, lg4ff_wheel_effects, 40, 270, NULL} | |
7362cd22 MM |
92 | }; |
93 | ||
96440c8a MM |
94 | struct lg4ff_native_cmd { |
95 | const __u8 cmd_num; /* Number of commands to send */ | |
96 | const __u8 cmd[]; | |
97 | }; | |
98 | ||
99 | struct lg4ff_usb_revision { | |
100 | const __u16 rev_maj; | |
101 | const __u16 rev_min; | |
102 | const struct lg4ff_native_cmd *command; | |
103 | }; | |
104 | ||
105 | static const struct lg4ff_native_cmd native_dfp = { | |
106 | 1, | |
107 | {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00} | |
108 | }; | |
109 | ||
110 | static const struct lg4ff_native_cmd native_dfgt = { | |
111 | 2, | |
112 | {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */ | |
113 | 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* 2nd command */ | |
114 | }; | |
115 | ||
116 | static const struct lg4ff_native_cmd native_g25 = { | |
117 | 1, | |
118 | {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00} | |
119 | }; | |
120 | ||
121 | static const struct lg4ff_native_cmd native_g27 = { | |
122 | 2, | |
123 | {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */ | |
124 | 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* 2nd command */ | |
125 | }; | |
126 | ||
127 | static const struct lg4ff_usb_revision lg4ff_revs[] = { | |
128 | {DFGT_REV_MAJ, DFGT_REV_MIN, &native_dfgt}, /* Driving Force GT */ | |
d991938a | 129 | {DFGT_REV_MAJ, DFGT2_REV_MIN, &native_dfgt}, /* Driving Force GT v2 */ |
96440c8a MM |
130 | {DFP_REV_MAJ, DFP_REV_MIN, &native_dfp}, /* Driving Force Pro */ |
131 | {G25_REV_MAJ, G25_REV_MIN, &native_g25}, /* G25 */ | |
132 | {G27_REV_MAJ, G27_REV_MIN, &native_g27}, /* G27 */ | |
133 | }; | |
134 | ||
2b24a960 MM |
135 | /* Recalculates X axis value accordingly to currently selected range */ |
136 | static __s32 lg4ff_adjust_dfp_x_axis(__s32 value, __u16 range) | |
137 | { | |
138 | __u16 max_range; | |
139 | __s32 new_value; | |
140 | ||
141 | if (range == 900) | |
142 | return value; | |
143 | else if (range == 200) | |
144 | return value; | |
145 | else if (range < 200) | |
146 | max_range = 200; | |
147 | else | |
148 | max_range = 900; | |
149 | ||
150 | new_value = 8192 + mult_frac(value - 8192, max_range, range); | |
151 | if (new_value < 0) | |
152 | return 0; | |
153 | else if (new_value > 16383) | |
154 | return 16383; | |
155 | else | |
156 | return new_value; | |
157 | } | |
158 | ||
159 | int lg4ff_adjust_input_event(struct hid_device *hid, struct hid_field *field, | |
160 | struct hid_usage *usage, __s32 value, struct lg_drv_data *drv_data) | |
161 | { | |
162 | struct lg4ff_device_entry *entry = drv_data->device_props; | |
163 | __s32 new_value = 0; | |
164 | ||
165 | if (!entry) { | |
166 | hid_err(hid, "Device properties not found"); | |
167 | return 0; | |
168 | } | |
169 | ||
170 | switch (entry->product_id) { | |
171 | case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: | |
172 | switch (usage->code) { | |
173 | case ABS_X: | |
174 | new_value = lg4ff_adjust_dfp_x_axis(value, entry->range); | |
175 | input_event(field->hidinput->input, usage->type, usage->code, new_value); | |
176 | return 1; | |
177 | default: | |
178 | return 0; | |
179 | } | |
180 | default: | |
181 | return 0; | |
182 | } | |
183 | } | |
184 | ||
30bb75d7 | 185 | static int hid_lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect) |
32c88cbc SW |
186 | { |
187 | struct hid_device *hid = input_get_drvdata(dev); | |
188 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; | |
189 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); | |
74479ba8 | 190 | __s32 *value = report->field[0]->value; |
32c88cbc SW |
191 | int x; |
192 | ||
a80fe5d6 | 193 | #define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0) |
32c88cbc SW |
194 | |
195 | switch (effect->type) { | |
196 | case FF_CONSTANT: | |
197 | x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */ | |
198 | CLAMP(x); | |
74479ba8 MM |
199 | value[0] = 0x11; /* Slot 1 */ |
200 | value[1] = 0x08; | |
201 | value[2] = x; | |
202 | value[3] = 0x80; | |
203 | value[4] = 0x00; | |
204 | value[5] = 0x00; | |
205 | value[6] = 0x00; | |
32c88cbc | 206 | |
d8814272 | 207 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
32c88cbc SW |
208 | break; |
209 | } | |
210 | return 0; | |
211 | } | |
212 | ||
6e2de8e0 MM |
213 | /* Sends default autocentering command compatible with |
214 | * all wheels except Formula Force EX */ | |
215 | static void hid_lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude) | |
32c88cbc SW |
216 | { |
217 | struct hid_device *hid = input_get_drvdata(dev); | |
218 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; | |
219 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); | |
74479ba8 | 220 | __s32 *value = report->field[0]->value; |
32c88cbc | 221 | |
74479ba8 MM |
222 | value[0] = 0xfe; |
223 | value[1] = 0x0d; | |
224 | value[2] = magnitude >> 13; | |
225 | value[3] = magnitude >> 13; | |
226 | value[4] = magnitude >> 8; | |
227 | value[5] = 0x00; | |
228 | value[6] = 0x00; | |
32c88cbc | 229 | |
d8814272 | 230 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
32c88cbc SW |
231 | } |
232 | ||
6e2de8e0 MM |
233 | /* Sends autocentering command compatible with Formula Force EX */ |
234 | static void hid_lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude) | |
235 | { | |
236 | struct hid_device *hid = input_get_drvdata(dev); | |
237 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; | |
238 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); | |
74479ba8 | 239 | __s32 *value = report->field[0]->value; |
6e2de8e0 | 240 | magnitude = magnitude * 90 / 65535; |
6e2de8e0 | 241 | |
74479ba8 MM |
242 | value[0] = 0xfe; |
243 | value[1] = 0x03; | |
244 | value[2] = magnitude >> 14; | |
245 | value[3] = magnitude >> 14; | |
246 | value[4] = magnitude; | |
247 | value[5] = 0x00; | |
248 | value[6] = 0x00; | |
6e2de8e0 | 249 | |
d8814272 | 250 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
6e2de8e0 MM |
251 | } |
252 | ||
30bb75d7 MM |
253 | /* Sends command to set range compatible with G25/G27/Driving Force GT */ |
254 | static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range) | |
255 | { | |
256 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; | |
257 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); | |
74479ba8 MM |
258 | __s32 *value = report->field[0]->value; |
259 | ||
30bb75d7 MM |
260 | dbg_hid("G25/G27/DFGT: setting range to %u\n", range); |
261 | ||
74479ba8 MM |
262 | value[0] = 0xf8; |
263 | value[1] = 0x81; | |
264 | value[2] = range & 0x00ff; | |
265 | value[3] = (range & 0xff00) >> 8; | |
266 | value[4] = 0x00; | |
267 | value[5] = 0x00; | |
268 | value[6] = 0x00; | |
30bb75d7 | 269 | |
d8814272 | 270 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
30bb75d7 MM |
271 | } |
272 | ||
273 | /* Sends commands to set range compatible with Driving Force Pro wheel */ | |
274 | static void hid_lg4ff_set_range_dfp(struct hid_device *hid, __u16 range) | |
275 | { | |
276 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; | |
277 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); | |
278 | int start_left, start_right, full_range; | |
74479ba8 MM |
279 | __s32 *value = report->field[0]->value; |
280 | ||
30bb75d7 MM |
281 | dbg_hid("Driving Force Pro: setting range to %u\n", range); |
282 | ||
283 | /* Prepare "coarse" limit command */ | |
74479ba8 | 284 | value[0] = 0xf8; |
a80fe5d6 | 285 | value[1] = 0x00; /* Set later */ |
74479ba8 MM |
286 | value[2] = 0x00; |
287 | value[3] = 0x00; | |
288 | value[4] = 0x00; | |
289 | value[5] = 0x00; | |
290 | value[6] = 0x00; | |
30bb75d7 MM |
291 | |
292 | if (range > 200) { | |
293 | report->field[0]->value[1] = 0x03; | |
294 | full_range = 900; | |
295 | } else { | |
296 | report->field[0]->value[1] = 0x02; | |
297 | full_range = 200; | |
298 | } | |
d8814272 | 299 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
30bb75d7 MM |
300 | |
301 | /* Prepare "fine" limit command */ | |
74479ba8 MM |
302 | value[0] = 0x81; |
303 | value[1] = 0x0b; | |
304 | value[2] = 0x00; | |
305 | value[3] = 0x00; | |
306 | value[4] = 0x00; | |
307 | value[5] = 0x00; | |
308 | value[6] = 0x00; | |
30bb75d7 MM |
309 | |
310 | if (range == 200 || range == 900) { /* Do not apply any fine limit */ | |
d8814272 | 311 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
30bb75d7 MM |
312 | return; |
313 | } | |
314 | ||
315 | /* Construct fine limit command */ | |
316 | start_left = (((full_range - range + 1) * 2047) / full_range); | |
317 | start_right = 0xfff - start_left; | |
318 | ||
74479ba8 MM |
319 | value[2] = start_left >> 4; |
320 | value[3] = start_right >> 4; | |
321 | value[4] = 0xff; | |
322 | value[5] = (start_right & 0xe) << 4 | (start_left & 0xe); | |
323 | value[6] = 0xff; | |
30bb75d7 | 324 | |
d8814272 | 325 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
30bb75d7 MM |
326 | } |
327 | ||
96440c8a MM |
328 | static void hid_lg4ff_switch_native(struct hid_device *hid, const struct lg4ff_native_cmd *cmd) |
329 | { | |
330 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; | |
331 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); | |
332 | __u8 i, j; | |
333 | ||
334 | j = 0; | |
335 | while (j < 7*cmd->cmd_num) { | |
336 | for (i = 0; i < 7; i++) | |
337 | report->field[0]->value[i] = cmd->cmd[j++]; | |
338 | ||
d8814272 | 339 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
96440c8a MM |
340 | } |
341 | } | |
32c88cbc | 342 | |
30bb75d7 MM |
343 | /* Read current range and display it in terminal */ |
344 | static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf) | |
345 | { | |
30bb75d7 | 346 | struct hid_device *hid = to_hid_device(dev); |
3b6b17b7 MM |
347 | struct lg4ff_device_entry *entry; |
348 | struct lg_drv_data *drv_data; | |
30bb75d7 MM |
349 | size_t count; |
350 | ||
3b6b17b7 MM |
351 | drv_data = hid_get_drvdata(hid); |
352 | if (!drv_data) { | |
353 | hid_err(hid, "Private driver data not found!\n"); | |
354 | return 0; | |
30bb75d7 | 355 | } |
3b6b17b7 MM |
356 | |
357 | entry = drv_data->device_props; | |
358 | if (!entry) { | |
359 | hid_err(hid, "Device properties not found!\n"); | |
30bb75d7 MM |
360 | return 0; |
361 | } | |
362 | ||
363 | count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->range); | |
364 | return count; | |
365 | } | |
366 | ||
367 | /* Set range to user specified value, call appropriate function | |
368 | * according to the type of the wheel */ | |
369 | static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) | |
370 | { | |
30bb75d7 | 371 | struct hid_device *hid = to_hid_device(dev); |
3b6b17b7 MM |
372 | struct lg4ff_device_entry *entry; |
373 | struct lg_drv_data *drv_data; | |
30bb75d7 MM |
374 | __u16 range = simple_strtoul(buf, NULL, 10); |
375 | ||
3b6b17b7 MM |
376 | drv_data = hid_get_drvdata(hid); |
377 | if (!drv_data) { | |
378 | hid_err(hid, "Private driver data not found!\n"); | |
379 | return 0; | |
30bb75d7 | 380 | } |
3b6b17b7 MM |
381 | |
382 | entry = drv_data->device_props; | |
383 | if (!entry) { | |
384 | hid_err(hid, "Device properties not found!\n"); | |
385 | return 0; | |
30bb75d7 MM |
386 | } |
387 | ||
388 | if (range == 0) | |
389 | range = entry->max_range; | |
390 | ||
391 | /* Check if the wheel supports range setting | |
392 | * and that the range is within limits for the wheel */ | |
393 | if (entry->set_range != NULL && range >= entry->min_range && range <= entry->max_range) { | |
394 | entry->set_range(hid, range); | |
395 | entry->range = range; | |
396 | } | |
397 | ||
398 | return count; | |
399 | } | |
400 | ||
22bcefdc SW |
401 | #ifdef CONFIG_LEDS_CLASS |
402 | static void lg4ff_set_leds(struct hid_device *hid, __u8 leds) | |
403 | { | |
404 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; | |
405 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); | |
74479ba8 MM |
406 | __s32 *value = report->field[0]->value; |
407 | ||
408 | value[0] = 0xf8; | |
409 | value[1] = 0x12; | |
410 | value[2] = leds; | |
411 | value[3] = 0x00; | |
412 | value[4] = 0x00; | |
413 | value[5] = 0x00; | |
414 | value[6] = 0x00; | |
d8814272 | 415 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
22bcefdc SW |
416 | } |
417 | ||
418 | static void lg4ff_led_set_brightness(struct led_classdev *led_cdev, | |
419 | enum led_brightness value) | |
420 | { | |
421 | struct device *dev = led_cdev->dev->parent; | |
422 | struct hid_device *hid = container_of(dev, struct hid_device, dev); | |
4629fd16 | 423 | struct lg_drv_data *drv_data = hid_get_drvdata(hid); |
22bcefdc SW |
424 | struct lg4ff_device_entry *entry; |
425 | int i, state = 0; | |
426 | ||
427 | if (!drv_data) { | |
428 | hid_err(hid, "Device data not found."); | |
429 | return; | |
430 | } | |
431 | ||
432 | entry = (struct lg4ff_device_entry *)drv_data->device_props; | |
433 | ||
434 | if (!entry) { | |
435 | hid_err(hid, "Device properties not found."); | |
436 | return; | |
437 | } | |
438 | ||
439 | for (i = 0; i < 5; i++) { | |
440 | if (led_cdev != entry->led[i]) | |
441 | continue; | |
442 | state = (entry->led_state >> i) & 1; | |
443 | if (value == LED_OFF && state) { | |
444 | entry->led_state &= ~(1 << i); | |
445 | lg4ff_set_leds(hid, entry->led_state); | |
446 | } else if (value != LED_OFF && !state) { | |
447 | entry->led_state |= 1 << i; | |
448 | lg4ff_set_leds(hid, entry->led_state); | |
449 | } | |
450 | break; | |
451 | } | |
452 | } | |
453 | ||
454 | static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev) | |
455 | { | |
456 | struct device *dev = led_cdev->dev->parent; | |
457 | struct hid_device *hid = container_of(dev, struct hid_device, dev); | |
4629fd16 | 458 | struct lg_drv_data *drv_data = hid_get_drvdata(hid); |
22bcefdc SW |
459 | struct lg4ff_device_entry *entry; |
460 | int i, value = 0; | |
461 | ||
462 | if (!drv_data) { | |
463 | hid_err(hid, "Device data not found."); | |
464 | return LED_OFF; | |
465 | } | |
466 | ||
467 | entry = (struct lg4ff_device_entry *)drv_data->device_props; | |
468 | ||
469 | if (!entry) { | |
470 | hid_err(hid, "Device properties not found."); | |
471 | return LED_OFF; | |
472 | } | |
473 | ||
474 | for (i = 0; i < 5; i++) | |
475 | if (led_cdev == entry->led[i]) { | |
476 | value = (entry->led_state >> i) & 1; | |
477 | break; | |
478 | } | |
479 | ||
480 | return value ? LED_FULL : LED_OFF; | |
481 | } | |
482 | #endif | |
483 | ||
32c88cbc SW |
484 | int lg4ff_init(struct hid_device *hid) |
485 | { | |
486 | struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); | |
32c88cbc | 487 | struct input_dev *dev = hidinput->input; |
30bb75d7 | 488 | struct lg4ff_device_entry *entry; |
3b6b17b7 | 489 | struct lg_drv_data *drv_data; |
30bb75d7 | 490 | struct usb_device_descriptor *udesc; |
7362cd22 | 491 | int error, i, j; |
96440c8a | 492 | __u16 bcdDevice, rev_maj, rev_min; |
32c88cbc | 493 | |
32c88cbc | 494 | /* Check that the report looks ok */ |
0fb6bd06 | 495 | if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7)) |
32c88cbc | 496 | return -1; |
30bb75d7 | 497 | |
7362cd22 MM |
498 | /* Check what wheel has been connected */ |
499 | for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) { | |
500 | if (hid->product == lg4ff_devices[i].product_id) { | |
501 | dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id); | |
502 | break; | |
503 | } | |
504 | } | |
505 | ||
506 | if (i == ARRAY_SIZE(lg4ff_devices)) { | |
507 | hid_err(hid, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to" | |
508 | "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n"); | |
509 | return -1; | |
510 | } | |
32c88cbc | 511 | |
96440c8a MM |
512 | /* Attempt to switch wheel to native mode when applicable */ |
513 | udesc = &(hid_to_usb_dev(hid)->descriptor); | |
514 | if (!udesc) { | |
515 | hid_err(hid, "NULL USB device descriptor\n"); | |
516 | return -1; | |
517 | } | |
518 | bcdDevice = le16_to_cpu(udesc->bcdDevice); | |
519 | rev_maj = bcdDevice >> 8; | |
520 | rev_min = bcdDevice & 0xff; | |
521 | ||
522 | if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_WHEEL) { | |
523 | dbg_hid("Generic wheel detected, can it do native?\n"); | |
524 | dbg_hid("USB revision: %2x.%02x\n", rev_maj, rev_min); | |
525 | ||
526 | for (j = 0; j < ARRAY_SIZE(lg4ff_revs); j++) { | |
527 | if (lg4ff_revs[j].rev_maj == rev_maj && lg4ff_revs[j].rev_min == rev_min) { | |
528 | hid_lg4ff_switch_native(hid, lg4ff_revs[j].command); | |
529 | hid_info(hid, "Switched to native mode\n"); | |
530 | } | |
531 | } | |
532 | } | |
533 | ||
7362cd22 MM |
534 | /* Set supported force feedback capabilities */ |
535 | for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++) | |
536 | set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit); | |
32c88cbc SW |
537 | |
538 | error = input_ff_create_memless(dev, NULL, hid_lg4ff_play); | |
539 | ||
540 | if (error) | |
541 | return error; | |
542 | ||
6e2de8e0 MM |
543 | /* Check if autocentering is available and |
544 | * set the centering force to zero by default */ | |
545 | if (test_bit(FF_AUTOCENTER, dev->ffbit)) { | |
74479ba8 | 546 | if (rev_maj == FFEX_REV_MAJ && rev_min == FFEX_REV_MIN) /* Formula Force EX expects different autocentering command */ |
6e2de8e0 MM |
547 | dev->ff->set_autocenter = hid_lg4ff_set_autocenter_ffex; |
548 | else | |
549 | dev->ff->set_autocenter = hid_lg4ff_set_autocenter_default; | |
550 | ||
551 | dev->ff->set_autocenter(dev, 0); | |
552 | } | |
32c88cbc | 553 | |
3b6b17b7 MM |
554 | /* Get private driver data */ |
555 | drv_data = hid_get_drvdata(hid); | |
556 | if (!drv_data) { | |
557 | hid_err(hid, "Cannot add device, private driver data not allocated\n"); | |
558 | return -1; | |
30bb75d7 MM |
559 | } |
560 | ||
3b6b17b7 | 561 | /* Initialize device properties */ |
8383c6bf | 562 | entry = kzalloc(sizeof(struct lg4ff_device_entry), GFP_KERNEL); |
30bb75d7 | 563 | if (!entry) { |
3b6b17b7 | 564 | hid_err(hid, "Cannot add device, insufficient memory to allocate device properties.\n"); |
30bb75d7 MM |
565 | return -ENOMEM; |
566 | } | |
3b6b17b7 MM |
567 | drv_data->device_props = entry; |
568 | ||
2b24a960 | 569 | entry->product_id = lg4ff_devices[i].product_id; |
30bb75d7 MM |
570 | entry->min_range = lg4ff_devices[i].min_range; |
571 | entry->max_range = lg4ff_devices[i].max_range; | |
572 | entry->set_range = lg4ff_devices[i].set_range; | |
30bb75d7 MM |
573 | |
574 | /* Create sysfs interface */ | |
575 | error = device_create_file(&hid->dev, &dev_attr_range); | |
576 | if (error) | |
577 | return error; | |
578 | dbg_hid("sysfs interface created\n"); | |
579 | ||
580 | /* Set the maximum range to start with */ | |
581 | entry->range = entry->max_range; | |
582 | if (entry->set_range != NULL) | |
583 | entry->set_range(hid, entry->range); | |
584 | ||
22bcefdc SW |
585 | #ifdef CONFIG_LEDS_CLASS |
586 | /* register led subsystem - G27 only */ | |
587 | entry->led_state = 0; | |
588 | for (j = 0; j < 5; j++) | |
589 | entry->led[j] = NULL; | |
590 | ||
591 | if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL) { | |
592 | struct led_classdev *led; | |
593 | size_t name_sz; | |
594 | char *name; | |
595 | ||
596 | lg4ff_set_leds(hid, 0); | |
597 | ||
598 | name_sz = strlen(dev_name(&hid->dev)) + 8; | |
599 | ||
600 | for (j = 0; j < 5; j++) { | |
601 | led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL); | |
602 | if (!led) { | |
603 | hid_err(hid, "can't allocate memory for LED %d\n", j); | |
604 | goto err; | |
605 | } | |
606 | ||
607 | name = (void *)(&led[1]); | |
608 | snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1); | |
609 | led->name = name; | |
610 | led->brightness = 0; | |
611 | led->max_brightness = 1; | |
612 | led->brightness_get = lg4ff_led_get_brightness; | |
613 | led->brightness_set = lg4ff_led_set_brightness; | |
614 | ||
615 | entry->led[j] = led; | |
616 | error = led_classdev_register(&hid->dev, led); | |
617 | ||
618 | if (error) { | |
619 | hid_err(hid, "failed to register LED %d. Aborting.\n", j); | |
620 | err: | |
621 | /* Deregister LEDs (if any) */ | |
622 | for (j = 0; j < 5; j++) { | |
623 | led = entry->led[j]; | |
624 | entry->led[j] = NULL; | |
625 | if (!led) | |
626 | continue; | |
627 | led_classdev_unregister(led); | |
628 | kfree(led); | |
629 | } | |
630 | goto out; /* Let the driver continue without LEDs */ | |
631 | } | |
632 | } | |
633 | } | |
22bcefdc | 634 | out: |
c6e6dc87 | 635 | #endif |
64013800 | 636 | hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n"); |
32c88cbc SW |
637 | return 0; |
638 | } | |
639 | ||
2b24a960 MM |
640 | |
641 | ||
30bb75d7 MM |
642 | int lg4ff_deinit(struct hid_device *hid) |
643 | { | |
30bb75d7 | 644 | struct lg4ff_device_entry *entry; |
3b6b17b7 MM |
645 | struct lg_drv_data *drv_data; |
646 | ||
6a2e176b MM |
647 | device_remove_file(&hid->dev, &dev_attr_range); |
648 | ||
3b6b17b7 MM |
649 | drv_data = hid_get_drvdata(hid); |
650 | if (!drv_data) { | |
651 | hid_err(hid, "Error while deinitializing device, no private driver data.\n"); | |
652 | return -1; | |
30bb75d7 | 653 | } |
3b6b17b7 MM |
654 | entry = drv_data->device_props; |
655 | if (!entry) { | |
656 | hid_err(hid, "Error while deinitializing device, no device properties data.\n"); | |
30bb75d7 MM |
657 | return -1; |
658 | } | |
22bcefdc SW |
659 | |
660 | #ifdef CONFIG_LEDS_CLASS | |
661 | { | |
662 | int j; | |
663 | struct led_classdev *led; | |
664 | ||
665 | /* Deregister LEDs (if any) */ | |
666 | for (j = 0; j < 5; j++) { | |
667 | ||
668 | led = entry->led[j]; | |
669 | entry->led[j] = NULL; | |
670 | if (!led) | |
671 | continue; | |
672 | led_classdev_unregister(led); | |
673 | kfree(led); | |
674 | } | |
675 | } | |
676 | #endif | |
677 | ||
3b6b17b7 MM |
678 | /* Deallocate memory */ |
679 | kfree(entry); | |
30bb75d7 | 680 | |
30bb75d7 MM |
681 | dbg_hid("Device successfully unregistered\n"); |
682 | return 0; | |
683 | } |