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