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