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; |
236b365b SW |
221 | __u32 expand_a, expand_b; |
222 | ||
b806304b SW |
223 | /* De-activate Auto-Center */ |
224 | if (magnitude == 0) { | |
225 | value[0] = 0xf5; | |
226 | value[1] = 0x00; | |
227 | value[2] = 0x00; | |
228 | value[3] = 0x00; | |
229 | value[4] = 0x00; | |
230 | value[5] = 0x00; | |
231 | value[6] = 0x00; | |
232 | ||
233 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); | |
234 | return; | |
235 | } | |
236 | ||
236b365b SW |
237 | if (magnitude <= 0xaaaa) { |
238 | expand_a = 0x0c * magnitude; | |
239 | expand_b = 0x80 * magnitude; | |
240 | } else { | |
241 | expand_a = (0x0c * 0xaaaa) + 0x06 * (magnitude - 0xaaaa); | |
242 | expand_b = (0x80 * 0xaaaa) + 0xff * (magnitude - 0xaaaa); | |
243 | } | |
32c88cbc | 244 | |
74479ba8 MM |
245 | value[0] = 0xfe; |
246 | value[1] = 0x0d; | |
236b365b SW |
247 | value[2] = expand_a / 0xaaaa; |
248 | value[3] = expand_a / 0xaaaa; | |
249 | value[4] = expand_b / 0xaaaa; | |
74479ba8 MM |
250 | value[5] = 0x00; |
251 | value[6] = 0x00; | |
32c88cbc | 252 | |
d8814272 | 253 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
b806304b SW |
254 | |
255 | /* Activate Auto-Center */ | |
256 | value[0] = 0x14; | |
257 | value[1] = 0x00; | |
258 | value[2] = 0x00; | |
259 | value[3] = 0x00; | |
260 | value[4] = 0x00; | |
261 | value[5] = 0x00; | |
262 | value[6] = 0x00; | |
263 | ||
264 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); | |
32c88cbc SW |
265 | } |
266 | ||
6e2de8e0 MM |
267 | /* Sends autocentering command compatible with Formula Force EX */ |
268 | static void hid_lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude) | |
269 | { | |
270 | struct hid_device *hid = input_get_drvdata(dev); | |
271 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; | |
272 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); | |
74479ba8 | 273 | __s32 *value = report->field[0]->value; |
6e2de8e0 | 274 | magnitude = magnitude * 90 / 65535; |
6e2de8e0 | 275 | |
74479ba8 MM |
276 | value[0] = 0xfe; |
277 | value[1] = 0x03; | |
278 | value[2] = magnitude >> 14; | |
279 | value[3] = magnitude >> 14; | |
280 | value[4] = magnitude; | |
281 | value[5] = 0x00; | |
282 | value[6] = 0x00; | |
6e2de8e0 | 283 | |
d8814272 | 284 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
6e2de8e0 MM |
285 | } |
286 | ||
30bb75d7 MM |
287 | /* Sends command to set range compatible with G25/G27/Driving Force GT */ |
288 | static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range) | |
289 | { | |
290 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; | |
291 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); | |
74479ba8 MM |
292 | __s32 *value = report->field[0]->value; |
293 | ||
30bb75d7 MM |
294 | dbg_hid("G25/G27/DFGT: setting range to %u\n", range); |
295 | ||
74479ba8 MM |
296 | value[0] = 0xf8; |
297 | value[1] = 0x81; | |
298 | value[2] = range & 0x00ff; | |
299 | value[3] = (range & 0xff00) >> 8; | |
300 | value[4] = 0x00; | |
301 | value[5] = 0x00; | |
302 | value[6] = 0x00; | |
30bb75d7 | 303 | |
d8814272 | 304 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
30bb75d7 MM |
305 | } |
306 | ||
307 | /* Sends commands to set range compatible with Driving Force Pro wheel */ | |
308 | static void hid_lg4ff_set_range_dfp(struct hid_device *hid, __u16 range) | |
309 | { | |
310 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; | |
311 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); | |
312 | int start_left, start_right, full_range; | |
74479ba8 MM |
313 | __s32 *value = report->field[0]->value; |
314 | ||
30bb75d7 MM |
315 | dbg_hid("Driving Force Pro: setting range to %u\n", range); |
316 | ||
317 | /* Prepare "coarse" limit command */ | |
74479ba8 | 318 | value[0] = 0xf8; |
a80fe5d6 | 319 | value[1] = 0x00; /* Set later */ |
74479ba8 MM |
320 | value[2] = 0x00; |
321 | value[3] = 0x00; | |
322 | value[4] = 0x00; | |
323 | value[5] = 0x00; | |
324 | value[6] = 0x00; | |
30bb75d7 MM |
325 | |
326 | if (range > 200) { | |
327 | report->field[0]->value[1] = 0x03; | |
328 | full_range = 900; | |
329 | } else { | |
330 | report->field[0]->value[1] = 0x02; | |
331 | full_range = 200; | |
332 | } | |
d8814272 | 333 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
30bb75d7 MM |
334 | |
335 | /* Prepare "fine" limit command */ | |
74479ba8 MM |
336 | value[0] = 0x81; |
337 | value[1] = 0x0b; | |
338 | value[2] = 0x00; | |
339 | value[3] = 0x00; | |
340 | value[4] = 0x00; | |
341 | value[5] = 0x00; | |
342 | value[6] = 0x00; | |
30bb75d7 MM |
343 | |
344 | if (range == 200 || range == 900) { /* Do not apply any fine limit */ | |
d8814272 | 345 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
30bb75d7 MM |
346 | return; |
347 | } | |
348 | ||
349 | /* Construct fine limit command */ | |
350 | start_left = (((full_range - range + 1) * 2047) / full_range); | |
351 | start_right = 0xfff - start_left; | |
352 | ||
74479ba8 MM |
353 | value[2] = start_left >> 4; |
354 | value[3] = start_right >> 4; | |
355 | value[4] = 0xff; | |
356 | value[5] = (start_right & 0xe) << 4 | (start_left & 0xe); | |
357 | value[6] = 0xff; | |
30bb75d7 | 358 | |
d8814272 | 359 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
30bb75d7 MM |
360 | } |
361 | ||
96440c8a MM |
362 | static void hid_lg4ff_switch_native(struct hid_device *hid, const struct lg4ff_native_cmd *cmd) |
363 | { | |
364 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; | |
365 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); | |
366 | __u8 i, j; | |
367 | ||
368 | j = 0; | |
369 | while (j < 7*cmd->cmd_num) { | |
370 | for (i = 0; i < 7; i++) | |
371 | report->field[0]->value[i] = cmd->cmd[j++]; | |
372 | ||
d8814272 | 373 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
96440c8a MM |
374 | } |
375 | } | |
32c88cbc | 376 | |
30bb75d7 MM |
377 | /* Read current range and display it in terminal */ |
378 | static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf) | |
379 | { | |
30bb75d7 | 380 | struct hid_device *hid = to_hid_device(dev); |
3b6b17b7 MM |
381 | struct lg4ff_device_entry *entry; |
382 | struct lg_drv_data *drv_data; | |
30bb75d7 MM |
383 | size_t count; |
384 | ||
3b6b17b7 MM |
385 | drv_data = hid_get_drvdata(hid); |
386 | if (!drv_data) { | |
387 | hid_err(hid, "Private driver data not found!\n"); | |
388 | return 0; | |
30bb75d7 | 389 | } |
3b6b17b7 MM |
390 | |
391 | entry = drv_data->device_props; | |
392 | if (!entry) { | |
393 | hid_err(hid, "Device properties not found!\n"); | |
30bb75d7 MM |
394 | return 0; |
395 | } | |
396 | ||
397 | count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->range); | |
398 | return count; | |
399 | } | |
400 | ||
401 | /* Set range to user specified value, call appropriate function | |
402 | * according to the type of the wheel */ | |
403 | static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) | |
404 | { | |
30bb75d7 | 405 | struct hid_device *hid = to_hid_device(dev); |
3b6b17b7 MM |
406 | struct lg4ff_device_entry *entry; |
407 | struct lg_drv_data *drv_data; | |
30bb75d7 MM |
408 | __u16 range = simple_strtoul(buf, NULL, 10); |
409 | ||
3b6b17b7 MM |
410 | drv_data = hid_get_drvdata(hid); |
411 | if (!drv_data) { | |
412 | hid_err(hid, "Private driver data not found!\n"); | |
413 | return 0; | |
30bb75d7 | 414 | } |
3b6b17b7 MM |
415 | |
416 | entry = drv_data->device_props; | |
417 | if (!entry) { | |
418 | hid_err(hid, "Device properties not found!\n"); | |
419 | return 0; | |
30bb75d7 MM |
420 | } |
421 | ||
422 | if (range == 0) | |
423 | range = entry->max_range; | |
424 | ||
425 | /* Check if the wheel supports range setting | |
426 | * and that the range is within limits for the wheel */ | |
427 | if (entry->set_range != NULL && range >= entry->min_range && range <= entry->max_range) { | |
428 | entry->set_range(hid, range); | |
429 | entry->range = range; | |
430 | } | |
431 | ||
432 | return count; | |
433 | } | |
434 | ||
22bcefdc SW |
435 | #ifdef CONFIG_LEDS_CLASS |
436 | static void lg4ff_set_leds(struct hid_device *hid, __u8 leds) | |
437 | { | |
438 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; | |
439 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); | |
74479ba8 MM |
440 | __s32 *value = report->field[0]->value; |
441 | ||
442 | value[0] = 0xf8; | |
443 | value[1] = 0x12; | |
444 | value[2] = leds; | |
445 | value[3] = 0x00; | |
446 | value[4] = 0x00; | |
447 | value[5] = 0x00; | |
448 | value[6] = 0x00; | |
d8814272 | 449 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
22bcefdc SW |
450 | } |
451 | ||
452 | static void lg4ff_led_set_brightness(struct led_classdev *led_cdev, | |
453 | enum led_brightness value) | |
454 | { | |
455 | struct device *dev = led_cdev->dev->parent; | |
456 | struct hid_device *hid = container_of(dev, struct hid_device, dev); | |
4629fd16 | 457 | struct lg_drv_data *drv_data = hid_get_drvdata(hid); |
22bcefdc SW |
458 | struct lg4ff_device_entry *entry; |
459 | int i, state = 0; | |
460 | ||
461 | if (!drv_data) { | |
462 | hid_err(hid, "Device data not found."); | |
463 | return; | |
464 | } | |
465 | ||
466 | entry = (struct lg4ff_device_entry *)drv_data->device_props; | |
467 | ||
468 | if (!entry) { | |
469 | hid_err(hid, "Device properties not found."); | |
470 | return; | |
471 | } | |
472 | ||
473 | for (i = 0; i < 5; i++) { | |
474 | if (led_cdev != entry->led[i]) | |
475 | continue; | |
476 | state = (entry->led_state >> i) & 1; | |
477 | if (value == LED_OFF && state) { | |
478 | entry->led_state &= ~(1 << i); | |
479 | lg4ff_set_leds(hid, entry->led_state); | |
480 | } else if (value != LED_OFF && !state) { | |
481 | entry->led_state |= 1 << i; | |
482 | lg4ff_set_leds(hid, entry->led_state); | |
483 | } | |
484 | break; | |
485 | } | |
486 | } | |
487 | ||
488 | static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev) | |
489 | { | |
490 | struct device *dev = led_cdev->dev->parent; | |
491 | struct hid_device *hid = container_of(dev, struct hid_device, dev); | |
4629fd16 | 492 | struct lg_drv_data *drv_data = hid_get_drvdata(hid); |
22bcefdc SW |
493 | struct lg4ff_device_entry *entry; |
494 | int i, value = 0; | |
495 | ||
496 | if (!drv_data) { | |
497 | hid_err(hid, "Device data not found."); | |
498 | return LED_OFF; | |
499 | } | |
500 | ||
501 | entry = (struct lg4ff_device_entry *)drv_data->device_props; | |
502 | ||
503 | if (!entry) { | |
504 | hid_err(hid, "Device properties not found."); | |
505 | return LED_OFF; | |
506 | } | |
507 | ||
508 | for (i = 0; i < 5; i++) | |
509 | if (led_cdev == entry->led[i]) { | |
510 | value = (entry->led_state >> i) & 1; | |
511 | break; | |
512 | } | |
513 | ||
514 | return value ? LED_FULL : LED_OFF; | |
515 | } | |
516 | #endif | |
517 | ||
32c88cbc SW |
518 | int lg4ff_init(struct hid_device *hid) |
519 | { | |
520 | struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); | |
32c88cbc | 521 | struct input_dev *dev = hidinput->input; |
30bb75d7 | 522 | struct lg4ff_device_entry *entry; |
3b6b17b7 | 523 | struct lg_drv_data *drv_data; |
30bb75d7 | 524 | struct usb_device_descriptor *udesc; |
7362cd22 | 525 | int error, i, j; |
96440c8a | 526 | __u16 bcdDevice, rev_maj, rev_min; |
32c88cbc | 527 | |
32c88cbc | 528 | /* Check that the report looks ok */ |
2ec96f1b | 529 | if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7)) |
32c88cbc | 530 | return -1; |
30bb75d7 | 531 | |
7362cd22 MM |
532 | /* Check what wheel has been connected */ |
533 | for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) { | |
534 | if (hid->product == lg4ff_devices[i].product_id) { | |
535 | dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id); | |
536 | break; | |
537 | } | |
538 | } | |
539 | ||
540 | if (i == ARRAY_SIZE(lg4ff_devices)) { | |
541 | hid_err(hid, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to" | |
542 | "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n"); | |
543 | return -1; | |
544 | } | |
32c88cbc | 545 | |
96440c8a MM |
546 | /* Attempt to switch wheel to native mode when applicable */ |
547 | udesc = &(hid_to_usb_dev(hid)->descriptor); | |
548 | if (!udesc) { | |
549 | hid_err(hid, "NULL USB device descriptor\n"); | |
550 | return -1; | |
551 | } | |
552 | bcdDevice = le16_to_cpu(udesc->bcdDevice); | |
553 | rev_maj = bcdDevice >> 8; | |
554 | rev_min = bcdDevice & 0xff; | |
555 | ||
556 | if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_WHEEL) { | |
557 | dbg_hid("Generic wheel detected, can it do native?\n"); | |
558 | dbg_hid("USB revision: %2x.%02x\n", rev_maj, rev_min); | |
559 | ||
560 | for (j = 0; j < ARRAY_SIZE(lg4ff_revs); j++) { | |
561 | if (lg4ff_revs[j].rev_maj == rev_maj && lg4ff_revs[j].rev_min == rev_min) { | |
562 | hid_lg4ff_switch_native(hid, lg4ff_revs[j].command); | |
563 | hid_info(hid, "Switched to native mode\n"); | |
564 | } | |
565 | } | |
566 | } | |
567 | ||
7362cd22 MM |
568 | /* Set supported force feedback capabilities */ |
569 | for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++) | |
570 | set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit); | |
32c88cbc SW |
571 | |
572 | error = input_ff_create_memless(dev, NULL, hid_lg4ff_play); | |
573 | ||
574 | if (error) | |
575 | return error; | |
576 | ||
3b6b17b7 MM |
577 | /* Get private driver data */ |
578 | drv_data = hid_get_drvdata(hid); | |
579 | if (!drv_data) { | |
580 | hid_err(hid, "Cannot add device, private driver data not allocated\n"); | |
581 | return -1; | |
30bb75d7 MM |
582 | } |
583 | ||
3b6b17b7 | 584 | /* Initialize device properties */ |
8383c6bf | 585 | entry = kzalloc(sizeof(struct lg4ff_device_entry), GFP_KERNEL); |
30bb75d7 | 586 | if (!entry) { |
3b6b17b7 | 587 | hid_err(hid, "Cannot add device, insufficient memory to allocate device properties.\n"); |
30bb75d7 MM |
588 | return -ENOMEM; |
589 | } | |
3b6b17b7 MM |
590 | drv_data->device_props = entry; |
591 | ||
2b24a960 | 592 | entry->product_id = lg4ff_devices[i].product_id; |
30bb75d7 MM |
593 | entry->min_range = lg4ff_devices[i].min_range; |
594 | entry->max_range = lg4ff_devices[i].max_range; | |
595 | entry->set_range = lg4ff_devices[i].set_range; | |
30bb75d7 | 596 | |
43f0ba99 SW |
597 | /* Check if autocentering is available and |
598 | * set the centering force to zero by default */ | |
599 | if (test_bit(FF_AUTOCENTER, dev->ffbit)) { | |
600 | if (rev_maj == FFEX_REV_MAJ && rev_min == FFEX_REV_MIN) /* Formula Force EX expects different autocentering command */ | |
601 | dev->ff->set_autocenter = hid_lg4ff_set_autocenter_ffex; | |
602 | else | |
603 | dev->ff->set_autocenter = hid_lg4ff_set_autocenter_default; | |
604 | ||
605 | dev->ff->set_autocenter(dev, 0); | |
606 | } | |
607 | ||
30bb75d7 MM |
608 | /* Create sysfs interface */ |
609 | error = device_create_file(&hid->dev, &dev_attr_range); | |
610 | if (error) | |
611 | return error; | |
612 | dbg_hid("sysfs interface created\n"); | |
613 | ||
614 | /* Set the maximum range to start with */ | |
615 | entry->range = entry->max_range; | |
616 | if (entry->set_range != NULL) | |
617 | entry->set_range(hid, entry->range); | |
618 | ||
22bcefdc SW |
619 | #ifdef CONFIG_LEDS_CLASS |
620 | /* register led subsystem - G27 only */ | |
621 | entry->led_state = 0; | |
622 | for (j = 0; j < 5; j++) | |
623 | entry->led[j] = NULL; | |
624 | ||
625 | if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL) { | |
626 | struct led_classdev *led; | |
627 | size_t name_sz; | |
628 | char *name; | |
629 | ||
630 | lg4ff_set_leds(hid, 0); | |
631 | ||
632 | name_sz = strlen(dev_name(&hid->dev)) + 8; | |
633 | ||
634 | for (j = 0; j < 5; j++) { | |
635 | led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL); | |
636 | if (!led) { | |
637 | hid_err(hid, "can't allocate memory for LED %d\n", j); | |
638 | goto err; | |
639 | } | |
640 | ||
641 | name = (void *)(&led[1]); | |
642 | snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1); | |
643 | led->name = name; | |
644 | led->brightness = 0; | |
645 | led->max_brightness = 1; | |
646 | led->brightness_get = lg4ff_led_get_brightness; | |
647 | led->brightness_set = lg4ff_led_set_brightness; | |
648 | ||
649 | entry->led[j] = led; | |
650 | error = led_classdev_register(&hid->dev, led); | |
651 | ||
652 | if (error) { | |
653 | hid_err(hid, "failed to register LED %d. Aborting.\n", j); | |
654 | err: | |
655 | /* Deregister LEDs (if any) */ | |
656 | for (j = 0; j < 5; j++) { | |
657 | led = entry->led[j]; | |
658 | entry->led[j] = NULL; | |
659 | if (!led) | |
660 | continue; | |
661 | led_classdev_unregister(led); | |
662 | kfree(led); | |
663 | } | |
664 | goto out; /* Let the driver continue without LEDs */ | |
665 | } | |
666 | } | |
667 | } | |
22bcefdc | 668 | out: |
c6e6dc87 | 669 | #endif |
64013800 | 670 | hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n"); |
32c88cbc SW |
671 | return 0; |
672 | } | |
673 | ||
2b24a960 MM |
674 | |
675 | ||
30bb75d7 MM |
676 | int lg4ff_deinit(struct hid_device *hid) |
677 | { | |
30bb75d7 | 678 | struct lg4ff_device_entry *entry; |
3b6b17b7 MM |
679 | struct lg_drv_data *drv_data; |
680 | ||
6a2e176b MM |
681 | device_remove_file(&hid->dev, &dev_attr_range); |
682 | ||
3b6b17b7 MM |
683 | drv_data = hid_get_drvdata(hid); |
684 | if (!drv_data) { | |
685 | hid_err(hid, "Error while deinitializing device, no private driver data.\n"); | |
686 | return -1; | |
30bb75d7 | 687 | } |
3b6b17b7 MM |
688 | entry = drv_data->device_props; |
689 | if (!entry) { | |
690 | hid_err(hid, "Error while deinitializing device, no device properties data.\n"); | |
30bb75d7 MM |
691 | return -1; |
692 | } | |
22bcefdc SW |
693 | |
694 | #ifdef CONFIG_LEDS_CLASS | |
695 | { | |
696 | int j; | |
697 | struct led_classdev *led; | |
698 | ||
699 | /* Deregister LEDs (if any) */ | |
700 | for (j = 0; j < 5; j++) { | |
701 | ||
702 | led = entry->led[j]; | |
703 | entry->led[j] = NULL; | |
704 | if (!led) | |
705 | continue; | |
706 | led_classdev_unregister(led); | |
707 | kfree(led); | |
708 | } | |
709 | } | |
710 | #endif | |
711 | ||
3b6b17b7 MM |
712 | /* Deallocate memory */ |
713 | kfree(entry); | |
30bb75d7 | 714 | |
30bb75d7 MM |
715 | dbg_hid("Device successfully unregistered\n"); |
716 | return 0; | |
717 | } |