blob: 0ddae2a00d59a595b4d7ad436dc8cedcfdde7b71 [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);
487 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
488 struct input_dev *dev = hidinput->input;
489 struct hid_report *report;
490 struct hid_field *field;
Michal Malý30bb75d2011-08-04 16:20:40 +0200491 struct lg4ff_device_entry *entry;
Michal Malý3b6b17b2012-04-09 09:08:49 +0200492 struct lg_drv_data *drv_data;
Michal Malý30bb75d2011-08-04 16:20:40 +0200493 struct usb_device_descriptor *udesc;
Michal Malý7362cd22011-08-04 16:16:09 +0200494 int error, i, j;
Michal Malý96440c82011-08-04 16:18:11 +0200495 __u16 bcdDevice, rev_maj, rev_min;
Simon Wood32c88cb2010-09-22 13:19:42 +0200496
497 /* Find the report to use */
498 if (list_empty(report_list)) {
Joe Perches4291ee32010-12-09 19:29:03 -0800499 hid_err(hid, "No output report found\n");
Simon Wood32c88cb2010-09-22 13:19:42 +0200500 return -1;
501 }
502
503 /* Check that the report looks ok */
504 report = list_entry(report_list->next, struct hid_report, list);
505 if (!report) {
Joe Perches4291ee32010-12-09 19:29:03 -0800506 hid_err(hid, "NULL output report\n");
Simon Wood32c88cb2010-09-22 13:19:42 +0200507 return -1;
508 }
509
510 field = report->field[0];
511 if (!field) {
Joe Perches4291ee32010-12-09 19:29:03 -0800512 hid_err(hid, "NULL field\n");
Simon Wood32c88cb2010-09-22 13:19:42 +0200513 return -1;
514 }
Michal Malý30bb75d2011-08-04 16:20:40 +0200515
Michal Malý7362cd22011-08-04 16:16:09 +0200516 /* Check what wheel has been connected */
517 for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
518 if (hid->product == lg4ff_devices[i].product_id) {
519 dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
520 break;
521 }
522 }
Simon Wood32c88cb2010-09-22 13:19:42 +0200523
Michal Malý7362cd22011-08-04 16:16:09 +0200524 if (i == ARRAY_SIZE(lg4ff_devices)) {
525 hid_err(hid, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to"
526 "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n");
527 return -1;
528 }
529
Michal Malý96440c82011-08-04 16:18:11 +0200530 /* Attempt to switch wheel to native mode when applicable */
531 udesc = &(hid_to_usb_dev(hid)->descriptor);
532 if (!udesc) {
533 hid_err(hid, "NULL USB device descriptor\n");
534 return -1;
535 }
536 bcdDevice = le16_to_cpu(udesc->bcdDevice);
537 rev_maj = bcdDevice >> 8;
538 rev_min = bcdDevice & 0xff;
539
540 if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_WHEEL) {
541 dbg_hid("Generic wheel detected, can it do native?\n");
542 dbg_hid("USB revision: %2x.%02x\n", rev_maj, rev_min);
543
544 for (j = 0; j < ARRAY_SIZE(lg4ff_revs); j++) {
545 if (lg4ff_revs[j].rev_maj == rev_maj && lg4ff_revs[j].rev_min == rev_min) {
546 hid_lg4ff_switch_native(hid, lg4ff_revs[j].command);
547 hid_info(hid, "Switched to native mode\n");
548 }
549 }
550 }
551
Michal Malý7362cd22011-08-04 16:16:09 +0200552 /* Set supported force feedback capabilities */
553 for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
554 set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
Simon Wood32c88cb2010-09-22 13:19:42 +0200555
556 error = input_ff_create_memless(dev, NULL, hid_lg4ff_play);
557
558 if (error)
559 return error;
560
Michal Malý6e2de8e2011-08-04 16:22:07 +0200561 /* Check if autocentering is available and
562 * set the centering force to zero by default */
563 if (test_bit(FF_AUTOCENTER, dev->ffbit)) {
Michal Malý74479ba2012-09-24 01:09:30 +0200564 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 +0200565 dev->ff->set_autocenter = hid_lg4ff_set_autocenter_ffex;
566 else
567 dev->ff->set_autocenter = hid_lg4ff_set_autocenter_default;
568
569 dev->ff->set_autocenter(dev, 0);
570 }
Simon Wood32c88cb2010-09-22 13:19:42 +0200571
Michal Malý3b6b17b2012-04-09 09:08:49 +0200572 /* Get private driver data */
573 drv_data = hid_get_drvdata(hid);
574 if (!drv_data) {
575 hid_err(hid, "Cannot add device, private driver data not allocated\n");
576 return -1;
Michal Malý30bb75d2011-08-04 16:20:40 +0200577 }
578
Michal Malý3b6b17b2012-04-09 09:08:49 +0200579 /* Initialize device properties */
Thomas Meyer8383c6b2011-11-08 20:14:14 +0100580 entry = kzalloc(sizeof(struct lg4ff_device_entry), GFP_KERNEL);
Michal Malý30bb75d2011-08-04 16:20:40 +0200581 if (!entry) {
Michal Malý3b6b17b2012-04-09 09:08:49 +0200582 hid_err(hid, "Cannot add device, insufficient memory to allocate device properties.\n");
Michal Malý30bb75d2011-08-04 16:20:40 +0200583 return -ENOMEM;
584 }
Michal Malý3b6b17b2012-04-09 09:08:49 +0200585 drv_data->device_props = entry;
586
Michal Malý2b24a962012-09-23 22:41:08 +0200587 entry->product_id = lg4ff_devices[i].product_id;
Michal Malý30bb75d2011-08-04 16:20:40 +0200588 entry->min_range = lg4ff_devices[i].min_range;
589 entry->max_range = lg4ff_devices[i].max_range;
590 entry->set_range = lg4ff_devices[i].set_range;
Michal Malý30bb75d2011-08-04 16:20:40 +0200591
592 /* Create sysfs interface */
593 error = device_create_file(&hid->dev, &dev_attr_range);
594 if (error)
595 return error;
596 dbg_hid("sysfs interface created\n");
597
598 /* Set the maximum range to start with */
599 entry->range = entry->max_range;
600 if (entry->set_range != NULL)
601 entry->set_range(hid, entry->range);
602
Simon Wood22bcefd2012-04-21 05:41:15 -0700603#ifdef CONFIG_LEDS_CLASS
604 /* register led subsystem - G27 only */
605 entry->led_state = 0;
606 for (j = 0; j < 5; j++)
607 entry->led[j] = NULL;
608
609 if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL) {
610 struct led_classdev *led;
611 size_t name_sz;
612 char *name;
613
614 lg4ff_set_leds(hid, 0);
615
616 name_sz = strlen(dev_name(&hid->dev)) + 8;
617
618 for (j = 0; j < 5; j++) {
619 led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
620 if (!led) {
621 hid_err(hid, "can't allocate memory for LED %d\n", j);
622 goto err;
623 }
624
625 name = (void *)(&led[1]);
626 snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1);
627 led->name = name;
628 led->brightness = 0;
629 led->max_brightness = 1;
630 led->brightness_get = lg4ff_led_get_brightness;
631 led->brightness_set = lg4ff_led_set_brightness;
632
633 entry->led[j] = led;
634 error = led_classdev_register(&hid->dev, led);
635
636 if (error) {
637 hid_err(hid, "failed to register LED %d. Aborting.\n", j);
638err:
639 /* Deregister LEDs (if any) */
640 for (j = 0; j < 5; j++) {
641 led = entry->led[j];
642 entry->led[j] = NULL;
643 if (!led)
644 continue;
645 led_classdev_unregister(led);
646 kfree(led);
647 }
648 goto out; /* Let the driver continue without LEDs */
649 }
650 }
651 }
Simon Wood22bcefd2012-04-21 05:41:15 -0700652out:
Jiri Kosinac6e6dc82012-04-23 21:08:15 +0200653#endif
Simon Wood64013802012-04-21 05:41:16 -0700654 hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n");
Simon Wood32c88cb2010-09-22 13:19:42 +0200655 return 0;
656}
657
Michal Malý2b24a962012-09-23 22:41:08 +0200658
659
Michal Malý30bb75d2011-08-04 16:20:40 +0200660int lg4ff_deinit(struct hid_device *hid)
661{
Michal Malý30bb75d2011-08-04 16:20:40 +0200662 struct lg4ff_device_entry *entry;
Michal Malý3b6b17b2012-04-09 09:08:49 +0200663 struct lg_drv_data *drv_data;
664
Michal Malý6a2e1762012-03-31 11:35:05 +0200665 device_remove_file(&hid->dev, &dev_attr_range);
666
Michal Malý3b6b17b2012-04-09 09:08:49 +0200667 drv_data = hid_get_drvdata(hid);
668 if (!drv_data) {
669 hid_err(hid, "Error while deinitializing device, no private driver data.\n");
Michal Malý30bb75d2011-08-04 16:20:40 +0200670 return -1;
671 }
Michal Malý3b6b17b2012-04-09 09:08:49 +0200672 entry = drv_data->device_props;
673 if (!entry) {
674 hid_err(hid, "Error while deinitializing device, no device properties data.\n");
675 return -1;
676 }
Simon Wood22bcefd2012-04-21 05:41:15 -0700677
678#ifdef CONFIG_LEDS_CLASS
679 {
680 int j;
681 struct led_classdev *led;
682
683 /* Deregister LEDs (if any) */
684 for (j = 0; j < 5; j++) {
685
686 led = entry->led[j];
687 entry->led[j] = NULL;
688 if (!led)
689 continue;
690 led_classdev_unregister(led);
691 kfree(led);
692 }
693 }
694#endif
695
Michal Malý3b6b17b2012-04-09 09:08:49 +0200696 /* Deallocate memory */
697 kfree(entry);
Michal Malý30bb75d2011-08-04 16:20:40 +0200698
Michal Malý30bb75d2011-08-04 16:20:40 +0200699 dbg_hid("Device successfully unregistered\n");
700 return 0;
701}