blob: 8782fe1aaa0796e42d17f1ad39d1c72c652d58c5 [file] [log] [blame]
Simon Wood32c88cb2010-09-22 13:19:42 +02001/*
Simon Wood64013802012-04-21 05:41:16 -07002 * Force feedback support for Logitech Gaming Wheels
Simon Wood32c88cb2010-09-22 13:19:42 +02003 *
Simon Wood64013802012-04-21 05:41:16 -07004 * Including G27, G25, DFP, DFGT, FFEX, Momo, Momo2 &
5 * Speed Force Wireless (WiiWheel)
Simon Wood32c88cb2010-09-22 13:19:42 +02006 *
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"
Michal Malý7362cd22011-08-04 16:16:09 +020033#include "hid-ids.h"
Simon Wood32c88cb2010-09-22 13:19:42 +020034
Michal Malý96440c82011-08-04 16:18:11 +020035#define DFGT_REV_MAJ 0x13
36#define DFGT_REV_MIN 0x22
Simon Woodd9919382013-04-26 19:29:54 -060037#define DFGT2_REV_MIN 0x26
Michal Malý96440c82011-08-04 16:18:11 +020038#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
Michal Malý30bb75d2011-08-04 16:20:40 +020047#define to_hid_device(pdev) container_of(pdev, struct hid_device, dev)
48
49static void hid_lg4ff_set_range_dfp(struct hid_device *hid, u16 range);
50static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range);
51static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf);
52static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count);
53
54static DEVICE_ATTR(range, S_IRWXU | S_IRWXG | S_IRWXO, lg4ff_range_show, lg4ff_range_store);
55
Michal Malý30bb75d2011-08-04 16:20:40 +020056struct lg4ff_device_entry {
Michal Malý2b24a962012-09-23 22:41:08 +020057 __u32 product_id;
Michal Malý30bb75d2011-08-04 16:20:40 +020058 __u16 range;
59 __u16 min_range;
60 __u16 max_range;
Simon Wood22bcefd2012-04-21 05:41:15 -070061#ifdef CONFIG_LEDS_CLASS
62 __u8 led_state;
63 struct led_classdev *led[5];
64#endif
Michal Malý30bb75d2011-08-04 16:20:40 +020065 struct list_head list;
66 void (*set_range)(struct hid_device *hid, u16 range);
67};
68
Michal Malý7362cd22011-08-04 16:16:09 +020069static const signed short lg4ff_wheel_effects[] = {
Simon Wood32c88cb2010-09-22 13:19:42 +020070 FF_CONSTANT,
71 FF_AUTOCENTER,
72 -1
73};
74
Michal Malý7362cd22011-08-04 16:16:09 +020075struct lg4ff_wheel {
76 const __u32 product_id;
77 const signed short *ff_effects;
78 const __u16 min_range;
79 const __u16 max_range;
Michal Malý30bb75d2011-08-04 16:20:40 +020080 void (*set_range)(struct hid_device *hid, u16 range);
Michal Malý7362cd22011-08-04 16:16:09 +020081};
82
83static const struct lg4ff_wheel lg4ff_devices[] = {
Michal Malý30bb75d2011-08-04 16:20:40 +020084 {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}
Michal Malý7362cd22011-08-04 16:16:09 +020092};
93
Michal Malý96440c82011-08-04 16:18:11 +020094struct lg4ff_native_cmd {
95 const __u8 cmd_num; /* Number of commands to send */
96 const __u8 cmd[];
97};
98
99struct lg4ff_usb_revision {
100 const __u16 rev_maj;
101 const __u16 rev_min;
102 const struct lg4ff_native_cmd *command;
103};
104
105static const struct lg4ff_native_cmd native_dfp = {
106 1,
107 {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
108};
109
110static 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
116static const struct lg4ff_native_cmd native_g25 = {
117 1,
118 {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
119};
120
121static 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
127static const struct lg4ff_usb_revision lg4ff_revs[] = {
128 {DFGT_REV_MAJ, DFGT_REV_MIN, &native_dfgt}, /* Driving Force GT */
Simon Woodd9919382013-04-26 19:29:54 -0600129 {DFGT_REV_MAJ, DFGT2_REV_MIN, &native_dfgt}, /* Driving Force GT v2 */
Michal Malý96440c82011-08-04 16:18:11 +0200130 {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
Michal Malý2b24a962012-09-23 22:41:08 +0200135/* Recalculates X axis value accordingly to currently selected range */
136static __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
159int 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
Michal Malý30bb75d2011-08-04 16:20:40 +0200185static int hid_lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
Simon Wood32c88cb2010-09-22 13:19:42 +0200186{
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);
Michal Malý74479ba2012-09-24 01:09:30 +0200190 __s32 *value = report->field[0]->value;
Simon Wood32c88cb2010-09-22 13:19:42 +0200191 int x;
192
Michal Malýa80fe5d2012-09-24 01:13:17 +0200193#define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0)
Simon Wood32c88cb2010-09-22 13:19:42 +0200194
195 switch (effect->type) {
196 case FF_CONSTANT:
197 x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */
198 CLAMP(x);
Michal Malý74479ba2012-09-24 01:09:30 +0200199 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;
Simon Wood32c88cb2010-09-22 13:19:42 +0200206
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100207 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Simon Wood32c88cb2010-09-22 13:19:42 +0200208 break;
209 }
210 return 0;
211}
212
Michal Malý6e2de8e2011-08-04 16:22:07 +0200213/* Sends default autocentering command compatible with
214 * all wheels except Formula Force EX */
215static void hid_lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude)
Simon Wood32c88cb2010-09-22 13:19:42 +0200216{
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);
Michal Malý74479ba2012-09-24 01:09:30 +0200220 __s32 *value = report->field[0]->value;
Simon Wood32c88cb2010-09-22 13:19:42 +0200221
Michal Malý74479ba2012-09-24 01:09:30 +0200222 value[0] = 0xfe;
223 value[1] = 0x0d;
224 value[2] = magnitude >> 13;
225 value[3] = magnitude >> 13;
226 value[4] = magnitude >> 8;
227 value[5] = 0x00;
228 value[6] = 0x00;
Simon Wood32c88cb2010-09-22 13:19:42 +0200229
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100230 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Simon Wood32c88cb2010-09-22 13:19:42 +0200231}
232
Michal Malý6e2de8e2011-08-04 16:22:07 +0200233/* Sends autocentering command compatible with Formula Force EX */
234static void hid_lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude)
235{
236 struct hid_device *hid = input_get_drvdata(dev);
237 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
238 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malý74479ba2012-09-24 01:09:30 +0200239 __s32 *value = report->field[0]->value;
Michal Malý6e2de8e2011-08-04 16:22:07 +0200240 magnitude = magnitude * 90 / 65535;
Michal Malý6e2de8e2011-08-04 16:22:07 +0200241
Michal Malý74479ba2012-09-24 01:09:30 +0200242 value[0] = 0xfe;
243 value[1] = 0x03;
244 value[2] = magnitude >> 14;
245 value[3] = magnitude >> 14;
246 value[4] = magnitude;
247 value[5] = 0x00;
248 value[6] = 0x00;
Michal Malý6e2de8e2011-08-04 16:22:07 +0200249
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100250 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malý6e2de8e2011-08-04 16:22:07 +0200251}
252
Michal Malý30bb75d2011-08-04 16:20:40 +0200253/* Sends command to set range compatible with G25/G27/Driving Force GT */
254static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range)
255{
256 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
257 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malý74479ba2012-09-24 01:09:30 +0200258 __s32 *value = report->field[0]->value;
259
Michal Malý30bb75d2011-08-04 16:20:40 +0200260 dbg_hid("G25/G27/DFGT: setting range to %u\n", range);
261
Michal Malý74479ba2012-09-24 01:09:30 +0200262 value[0] = 0xf8;
263 value[1] = 0x81;
264 value[2] = range & 0x00ff;
265 value[3] = (range & 0xff00) >> 8;
266 value[4] = 0x00;
267 value[5] = 0x00;
268 value[6] = 0x00;
Michal Malý30bb75d2011-08-04 16:20:40 +0200269
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100270 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malý30bb75d2011-08-04 16:20:40 +0200271}
272
273/* Sends commands to set range compatible with Driving Force Pro wheel */
274static void hid_lg4ff_set_range_dfp(struct hid_device *hid, __u16 range)
275{
276 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
277 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
278 int start_left, start_right, full_range;
Michal Malý74479ba2012-09-24 01:09:30 +0200279 __s32 *value = report->field[0]->value;
280
Michal Malý30bb75d2011-08-04 16:20:40 +0200281 dbg_hid("Driving Force Pro: setting range to %u\n", range);
282
283 /* Prepare "coarse" limit command */
Michal Malý74479ba2012-09-24 01:09:30 +0200284 value[0] = 0xf8;
Michal Malýa80fe5d2012-09-24 01:13:17 +0200285 value[1] = 0x00; /* Set later */
Michal Malý74479ba2012-09-24 01:09:30 +0200286 value[2] = 0x00;
287 value[3] = 0x00;
288 value[4] = 0x00;
289 value[5] = 0x00;
290 value[6] = 0x00;
Michal Malý30bb75d2011-08-04 16:20:40 +0200291
292 if (range > 200) {
293 report->field[0]->value[1] = 0x03;
294 full_range = 900;
295 } else {
296 report->field[0]->value[1] = 0x02;
297 full_range = 200;
298 }
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100299 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malý30bb75d2011-08-04 16:20:40 +0200300
301 /* Prepare "fine" limit command */
Michal Malý74479ba2012-09-24 01:09:30 +0200302 value[0] = 0x81;
303 value[1] = 0x0b;
304 value[2] = 0x00;
305 value[3] = 0x00;
306 value[4] = 0x00;
307 value[5] = 0x00;
308 value[6] = 0x00;
Michal Malý30bb75d2011-08-04 16:20:40 +0200309
310 if (range == 200 || range == 900) { /* Do not apply any fine limit */
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100311 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malý30bb75d2011-08-04 16:20:40 +0200312 return;
313 }
314
315 /* Construct fine limit command */
316 start_left = (((full_range - range + 1) * 2047) / full_range);
317 start_right = 0xfff - start_left;
318
Michal Malý74479ba2012-09-24 01:09:30 +0200319 value[2] = start_left >> 4;
320 value[3] = start_right >> 4;
321 value[4] = 0xff;
322 value[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
323 value[6] = 0xff;
Michal Malý30bb75d2011-08-04 16:20:40 +0200324
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100325 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malý30bb75d2011-08-04 16:20:40 +0200326}
327
Michal Malý96440c82011-08-04 16:18:11 +0200328static void hid_lg4ff_switch_native(struct hid_device *hid, const struct lg4ff_native_cmd *cmd)
329{
330 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
331 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
332 __u8 i, j;
333
334 j = 0;
335 while (j < 7*cmd->cmd_num) {
336 for (i = 0; i < 7; i++)
337 report->field[0]->value[i] = cmd->cmd[j++];
338
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100339 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Michal Malý96440c82011-08-04 16:18:11 +0200340 }
341}
Simon Wood32c88cb2010-09-22 13:19:42 +0200342
Michal Malý30bb75d2011-08-04 16:20:40 +0200343/* Read current range and display it in terminal */
344static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
345{
Michal Malý30bb75d2011-08-04 16:20:40 +0200346 struct hid_device *hid = to_hid_device(dev);
Michal Malý3b6b17b2012-04-09 09:08:49 +0200347 struct lg4ff_device_entry *entry;
348 struct lg_drv_data *drv_data;
Michal Malý30bb75d2011-08-04 16:20:40 +0200349 size_t count;
350
Michal Malý3b6b17b2012-04-09 09:08:49 +0200351 drv_data = hid_get_drvdata(hid);
352 if (!drv_data) {
353 hid_err(hid, "Private driver data not found!\n");
354 return 0;
Michal Malý30bb75d2011-08-04 16:20:40 +0200355 }
Michal Malý3b6b17b2012-04-09 09:08:49 +0200356
357 entry = drv_data->device_props;
358 if (!entry) {
359 hid_err(hid, "Device properties not found!\n");
Michal Malý30bb75d2011-08-04 16:20:40 +0200360 return 0;
361 }
362
363 count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->range);
364 return count;
365}
366
367/* Set range to user specified value, call appropriate function
368 * according to the type of the wheel */
369static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
370{
Michal Malý30bb75d2011-08-04 16:20:40 +0200371 struct hid_device *hid = to_hid_device(dev);
Michal Malý3b6b17b2012-04-09 09:08:49 +0200372 struct lg4ff_device_entry *entry;
373 struct lg_drv_data *drv_data;
Michal Malý30bb75d2011-08-04 16:20:40 +0200374 __u16 range = simple_strtoul(buf, NULL, 10);
375
Michal Malý3b6b17b2012-04-09 09:08:49 +0200376 drv_data = hid_get_drvdata(hid);
377 if (!drv_data) {
378 hid_err(hid, "Private driver data not found!\n");
379 return 0;
Michal Malý30bb75d2011-08-04 16:20:40 +0200380 }
Michal Malý3b6b17b2012-04-09 09:08:49 +0200381
382 entry = drv_data->device_props;
383 if (!entry) {
384 hid_err(hid, "Device properties not found!\n");
385 return 0;
Michal Malý30bb75d2011-08-04 16:20:40 +0200386 }
387
388 if (range == 0)
389 range = entry->max_range;
390
391 /* Check if the wheel supports range setting
392 * and that the range is within limits for the wheel */
393 if (entry->set_range != NULL && range >= entry->min_range && range <= entry->max_range) {
394 entry->set_range(hid, range);
395 entry->range = range;
396 }
397
398 return count;
399}
400
Simon Wood22bcefd2012-04-21 05:41:15 -0700401#ifdef CONFIG_LEDS_CLASS
402static void lg4ff_set_leds(struct hid_device *hid, __u8 leds)
403{
404 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
405 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
Michal Malý74479ba2012-09-24 01:09:30 +0200406 __s32 *value = report->field[0]->value;
Simon Wood22bcefd2012-04-21 05:41:15 -0700407
Michal Malý74479ba2012-09-24 01:09:30 +0200408 value[0] = 0xf8;
409 value[1] = 0x12;
410 value[2] = leds;
411 value[3] = 0x00;
412 value[4] = 0x00;
413 value[5] = 0x00;
414 value[6] = 0x00;
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100415 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Simon Wood22bcefd2012-04-21 05:41:15 -0700416}
417
418static void lg4ff_led_set_brightness(struct led_classdev *led_cdev,
419 enum led_brightness value)
420{
421 struct device *dev = led_cdev->dev->parent;
422 struct hid_device *hid = container_of(dev, struct hid_device, dev);
Axel Lin4629fd12012-09-13 14:09:33 +0800423 struct lg_drv_data *drv_data = hid_get_drvdata(hid);
Simon Wood22bcefd2012-04-21 05:41:15 -0700424 struct lg4ff_device_entry *entry;
425 int i, state = 0;
426
427 if (!drv_data) {
428 hid_err(hid, "Device data not found.");
429 return;
430 }
431
432 entry = (struct lg4ff_device_entry *)drv_data->device_props;
433
434 if (!entry) {
435 hid_err(hid, "Device properties not found.");
436 return;
437 }
438
439 for (i = 0; i < 5; i++) {
440 if (led_cdev != entry->led[i])
441 continue;
442 state = (entry->led_state >> i) & 1;
443 if (value == LED_OFF && state) {
444 entry->led_state &= ~(1 << i);
445 lg4ff_set_leds(hid, entry->led_state);
446 } else if (value != LED_OFF && !state) {
447 entry->led_state |= 1 << i;
448 lg4ff_set_leds(hid, entry->led_state);
449 }
450 break;
451 }
452}
453
454static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev)
455{
456 struct device *dev = led_cdev->dev->parent;
457 struct hid_device *hid = container_of(dev, struct hid_device, dev);
Axel Lin4629fd12012-09-13 14:09:33 +0800458 struct lg_drv_data *drv_data = hid_get_drvdata(hid);
Simon Wood22bcefd2012-04-21 05:41:15 -0700459 struct lg4ff_device_entry *entry;
460 int i, value = 0;
461
462 if (!drv_data) {
463 hid_err(hid, "Device data not found.");
464 return LED_OFF;
465 }
466
467 entry = (struct lg4ff_device_entry *)drv_data->device_props;
468
469 if (!entry) {
470 hid_err(hid, "Device properties not found.");
471 return LED_OFF;
472 }
473
474 for (i = 0; i < 5; i++)
475 if (led_cdev == entry->led[i]) {
476 value = (entry->led_state >> i) & 1;
477 break;
478 }
479
480 return value ? LED_FULL : LED_OFF;
481}
482#endif
483
Simon Wood32c88cb2010-09-22 13:19:42 +0200484int lg4ff_init(struct hid_device *hid)
485{
486 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
Simon Wood32c88cb2010-09-22 13:19:42 +0200487 struct input_dev *dev = hidinput->input;
Michal Malý30bb75d2011-08-04 16:20:40 +0200488 struct lg4ff_device_entry *entry;
Michal Malý3b6b17b2012-04-09 09:08:49 +0200489 struct lg_drv_data *drv_data;
Michal Malý30bb75d2011-08-04 16:20:40 +0200490 struct usb_device_descriptor *udesc;
Michal Malý7362cd22011-08-04 16:16:09 +0200491 int error, i, j;
Michal Malý96440c82011-08-04 16:18:11 +0200492 __u16 bcdDevice, rev_maj, rev_min;
Simon Wood32c88cb2010-09-22 13:19:42 +0200493
Simon Wood32c88cb2010-09-22 13:19:42 +0200494 /* Check that the report looks ok */
Kees Cook0fb6bd02013-09-11 21:56:54 +0200495 if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
Simon Wood32c88cb2010-09-22 13:19:42 +0200496 return -1;
Michal Malý30bb75d2011-08-04 16:20:40 +0200497
Michal Malý7362cd22011-08-04 16:16:09 +0200498 /* Check what wheel has been connected */
499 for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
500 if (hid->product == lg4ff_devices[i].product_id) {
501 dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
502 break;
503 }
504 }
Simon Wood32c88cb2010-09-22 13:19:42 +0200505
Michal Malý7362cd22011-08-04 16:16:09 +0200506 if (i == ARRAY_SIZE(lg4ff_devices)) {
507 hid_err(hid, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to"
508 "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n");
509 return -1;
510 }
511
Michal Malý96440c82011-08-04 16:18:11 +0200512 /* Attempt to switch wheel to native mode when applicable */
513 udesc = &(hid_to_usb_dev(hid)->descriptor);
514 if (!udesc) {
515 hid_err(hid, "NULL USB device descriptor\n");
516 return -1;
517 }
518 bcdDevice = le16_to_cpu(udesc->bcdDevice);
519 rev_maj = bcdDevice >> 8;
520 rev_min = bcdDevice & 0xff;
521
522 if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_WHEEL) {
523 dbg_hid("Generic wheel detected, can it do native?\n");
524 dbg_hid("USB revision: %2x.%02x\n", rev_maj, rev_min);
525
526 for (j = 0; j < ARRAY_SIZE(lg4ff_revs); j++) {
527 if (lg4ff_revs[j].rev_maj == rev_maj && lg4ff_revs[j].rev_min == rev_min) {
528 hid_lg4ff_switch_native(hid, lg4ff_revs[j].command);
529 hid_info(hid, "Switched to native mode\n");
530 }
531 }
532 }
533
Michal Malý7362cd22011-08-04 16:16:09 +0200534 /* Set supported force feedback capabilities */
535 for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
536 set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
Simon Wood32c88cb2010-09-22 13:19:42 +0200537
538 error = input_ff_create_memless(dev, NULL, hid_lg4ff_play);
539
540 if (error)
541 return error;
542
Michal Malý6e2de8e2011-08-04 16:22:07 +0200543 /* Check if autocentering is available and
544 * set the centering force to zero by default */
545 if (test_bit(FF_AUTOCENTER, dev->ffbit)) {
Michal Malý74479ba2012-09-24 01:09:30 +0200546 if (rev_maj == FFEX_REV_MAJ && rev_min == FFEX_REV_MIN) /* Formula Force EX expects different autocentering command */
Michal Malý6e2de8e2011-08-04 16:22:07 +0200547 dev->ff->set_autocenter = hid_lg4ff_set_autocenter_ffex;
548 else
549 dev->ff->set_autocenter = hid_lg4ff_set_autocenter_default;
550
551 dev->ff->set_autocenter(dev, 0);
552 }
Simon Wood32c88cb2010-09-22 13:19:42 +0200553
Michal Malý3b6b17b2012-04-09 09:08:49 +0200554 /* Get private driver data */
555 drv_data = hid_get_drvdata(hid);
556 if (!drv_data) {
557 hid_err(hid, "Cannot add device, private driver data not allocated\n");
558 return -1;
Michal Malý30bb75d2011-08-04 16:20:40 +0200559 }
560
Michal Malý3b6b17b2012-04-09 09:08:49 +0200561 /* Initialize device properties */
Thomas Meyer8383c6b2011-11-08 20:14:14 +0100562 entry = kzalloc(sizeof(struct lg4ff_device_entry), GFP_KERNEL);
Michal Malý30bb75d2011-08-04 16:20:40 +0200563 if (!entry) {
Michal Malý3b6b17b2012-04-09 09:08:49 +0200564 hid_err(hid, "Cannot add device, insufficient memory to allocate device properties.\n");
Michal Malý30bb75d2011-08-04 16:20:40 +0200565 return -ENOMEM;
566 }
Michal Malý3b6b17b2012-04-09 09:08:49 +0200567 drv_data->device_props = entry;
568
Michal Malý2b24a962012-09-23 22:41:08 +0200569 entry->product_id = lg4ff_devices[i].product_id;
Michal Malý30bb75d2011-08-04 16:20:40 +0200570 entry->min_range = lg4ff_devices[i].min_range;
571 entry->max_range = lg4ff_devices[i].max_range;
572 entry->set_range = lg4ff_devices[i].set_range;
Michal Malý30bb75d2011-08-04 16:20:40 +0200573
574 /* Create sysfs interface */
575 error = device_create_file(&hid->dev, &dev_attr_range);
576 if (error)
577 return error;
578 dbg_hid("sysfs interface created\n");
579
580 /* Set the maximum range to start with */
581 entry->range = entry->max_range;
582 if (entry->set_range != NULL)
583 entry->set_range(hid, entry->range);
584
Simon Wood22bcefd2012-04-21 05:41:15 -0700585#ifdef CONFIG_LEDS_CLASS
586 /* register led subsystem - G27 only */
587 entry->led_state = 0;
588 for (j = 0; j < 5; j++)
589 entry->led[j] = NULL;
590
591 if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL) {
592 struct led_classdev *led;
593 size_t name_sz;
594 char *name;
595
596 lg4ff_set_leds(hid, 0);
597
598 name_sz = strlen(dev_name(&hid->dev)) + 8;
599
600 for (j = 0; j < 5; j++) {
601 led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
602 if (!led) {
603 hid_err(hid, "can't allocate memory for LED %d\n", j);
604 goto err;
605 }
606
607 name = (void *)(&led[1]);
608 snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1);
609 led->name = name;
610 led->brightness = 0;
611 led->max_brightness = 1;
612 led->brightness_get = lg4ff_led_get_brightness;
613 led->brightness_set = lg4ff_led_set_brightness;
614
615 entry->led[j] = led;
616 error = led_classdev_register(&hid->dev, led);
617
618 if (error) {
619 hid_err(hid, "failed to register LED %d. Aborting.\n", j);
620err:
621 /* Deregister LEDs (if any) */
622 for (j = 0; j < 5; j++) {
623 led = entry->led[j];
624 entry->led[j] = NULL;
625 if (!led)
626 continue;
627 led_classdev_unregister(led);
628 kfree(led);
629 }
630 goto out; /* Let the driver continue without LEDs */
631 }
632 }
633 }
Simon Wood22bcefd2012-04-21 05:41:15 -0700634out:
Jiri Kosinac6e6dc82012-04-23 21:08:15 +0200635#endif
Simon Wood64013802012-04-21 05:41:16 -0700636 hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n");
Simon Wood32c88cb2010-09-22 13:19:42 +0200637 return 0;
638}
639
Michal Malý2b24a962012-09-23 22:41:08 +0200640
641
Michal Malý30bb75d2011-08-04 16:20:40 +0200642int lg4ff_deinit(struct hid_device *hid)
643{
Michal Malý30bb75d2011-08-04 16:20:40 +0200644 struct lg4ff_device_entry *entry;
Michal Malý3b6b17b2012-04-09 09:08:49 +0200645 struct lg_drv_data *drv_data;
646
Michal Malý6a2e1762012-03-31 11:35:05 +0200647 device_remove_file(&hid->dev, &dev_attr_range);
648
Michal Malý3b6b17b2012-04-09 09:08:49 +0200649 drv_data = hid_get_drvdata(hid);
650 if (!drv_data) {
651 hid_err(hid, "Error while deinitializing device, no private driver data.\n");
Michal Malý30bb75d2011-08-04 16:20:40 +0200652 return -1;
653 }
Michal Malý3b6b17b2012-04-09 09:08:49 +0200654 entry = drv_data->device_props;
655 if (!entry) {
656 hid_err(hid, "Error while deinitializing device, no device properties data.\n");
657 return -1;
658 }
Simon Wood22bcefd2012-04-21 05:41:15 -0700659
660#ifdef CONFIG_LEDS_CLASS
661 {
662 int j;
663 struct led_classdev *led;
664
665 /* Deregister LEDs (if any) */
666 for (j = 0; j < 5; j++) {
667
668 led = entry->led[j];
669 entry->led[j] = NULL;
670 if (!led)
671 continue;
672 led_classdev_unregister(led);
673 kfree(led);
674 }
675 }
676#endif
677
Michal Malý3b6b17b2012-04-09 09:08:49 +0200678 /* Deallocate memory */
679 kfree(entry);
Michal Malý30bb75d2011-08-04 16:20:40 +0200680
Michal Malý30bb75d2011-08-04 16:20:40 +0200681 dbg_hid("Device successfully unregistered\n");
682 return 0;
683}