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