HID: lg4ff - Add support for native mode switching
[pandora-kernel.git] / drivers / hid / hid-lg4ff.c
1 /*
2  *  Force feedback support for Logitech Speed Force Wireless
3  *
4  *  http://wiibrew.org/wiki/Logitech_USB_steering_wheel
5  *
6  *  Copyright (c) 2010 Simon Wood <simon@mungewell.org>
7  */
8
9 /*
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24
25
26 #include <linux/input.h>
27 #include <linux/usb.h>
28 #include <linux/hid.h>
29
30 #include "usbhid/usbhid.h"
31 #include "hid-lg.h"
32 #include "hid-ids.h"
33
34 #define DFGT_REV_MAJ 0x13
35 #define DFGT_REV_MIN 0x22
36 #define DFP_REV_MAJ 0x11
37 #define DFP_REV_MIN 0x06
38 #define FFEX_REV_MAJ 0x21
39 #define FFEX_REV_MIN 0x00
40 #define G25_REV_MAJ 0x12
41 #define G25_REV_MIN 0x22
42 #define G27_REV_MAJ 0x12
43 #define G27_REV_MIN 0x38
44
45 static const signed short lg4ff_wheel_effects[] = {
46         FF_CONSTANT,
47         FF_AUTOCENTER,
48         -1
49 };
50
51 struct lg4ff_wheel {
52         const __u32 product_id;
53         const signed short *ff_effects;
54         const __u16 min_range;
55         const __u16 max_range;
56 };
57
58 static const struct lg4ff_wheel lg4ff_devices[] = {
59         {USB_DEVICE_ID_LOGITECH_WHEEL,       lg4ff_wheel_effects, 40, 270},
60         {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL,  lg4ff_wheel_effects, 40, 270},
61         {USB_DEVICE_ID_LOGITECH_DFP_WHEEL,   lg4ff_wheel_effects, 40, 900},
62         {USB_DEVICE_ID_LOGITECH_G25_WHEEL,   lg4ff_wheel_effects, 40, 900},
63         {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL,  lg4ff_wheel_effects, 40, 900},
64         {USB_DEVICE_ID_LOGITECH_G27_WHEEL,   lg4ff_wheel_effects, 40, 900},
65         {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270},
66         {USB_DEVICE_ID_LOGITECH_WII_WHEEL,   lg4ff_wheel_effects, 40, 270}
67 };
68
69 struct lg4ff_native_cmd {
70         const __u8 cmd_num;     /* Number of commands to send */
71         const __u8 cmd[];
72 };
73
74 struct lg4ff_usb_revision {
75         const __u16 rev_maj;
76         const __u16 rev_min;
77         const struct lg4ff_native_cmd *command;
78 };
79
80 static const struct lg4ff_native_cmd native_dfp = {
81         1,
82         {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
83 };
84
85 static const struct lg4ff_native_cmd native_dfgt = {
86         2,
87         {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,      /* 1st command */
88          0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00}      /* 2nd command */
89 };
90
91 static const struct lg4ff_native_cmd native_g25 = {
92         1,
93         {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
94 };
95
96 static const struct lg4ff_native_cmd native_g27 = {
97         2,
98         {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,      /* 1st command */
99          0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00}      /* 2nd command */
100 };
101
102 static const struct lg4ff_usb_revision lg4ff_revs[] = {
103         {DFGT_REV_MAJ, DFGT_REV_MIN, &native_dfgt},     /* Driving Force GT */
104         {DFP_REV_MAJ,  DFP_REV_MIN,  &native_dfp},      /* Driving Force Pro */
105         {G25_REV_MAJ,  G25_REV_MIN,  &native_g25},      /* G25 */
106         {G27_REV_MAJ,  G27_REV_MIN,  &native_g27},      /* G27 */
107 };
108
109 static int hid_lg4ff_play(struct input_dev *dev, void *data,
110                          struct ff_effect *effect)
111 {
112         struct hid_device *hid = input_get_drvdata(dev);
113         struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
114         struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
115         int x;
116
117 #define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff
118
119         switch (effect->type) {
120         case FF_CONSTANT:
121                 x = effect->u.ramp.start_level + 0x80;  /* 0x80 is no force */
122                 CLAMP(x);
123                 report->field[0]->value[0] = 0x11;      /* Slot 1 */
124                 report->field[0]->value[1] = 0x08;
125                 report->field[0]->value[2] = x;
126                 report->field[0]->value[3] = 0x80;
127                 report->field[0]->value[4] = 0x00;
128                 report->field[0]->value[5] = 0x00;
129                 report->field[0]->value[6] = 0x00;
130
131                 usbhid_submit_report(hid, report, USB_DIR_OUT);
132                 break;
133         }
134         return 0;
135 }
136
137 static void hid_lg4ff_set_autocenter(struct input_dev *dev, u16 magnitude)
138 {
139         struct hid_device *hid = input_get_drvdata(dev);
140         struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
141         struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
142
143         report->field[0]->value[0] = 0xfe;
144         report->field[0]->value[1] = 0x0d;
145         report->field[0]->value[2] = magnitude >> 13;
146         report->field[0]->value[3] = magnitude >> 13;
147         report->field[0]->value[4] = magnitude >> 8;
148         report->field[0]->value[5] = 0x00;
149         report->field[0]->value[6] = 0x00;
150
151         usbhid_submit_report(hid, report, USB_DIR_OUT);
152 }
153
154 static void hid_lg4ff_switch_native(struct hid_device *hid, const struct lg4ff_native_cmd *cmd)
155 {
156         struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
157         struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
158         __u8 i, j;
159
160         j = 0;
161         while (j < 7*cmd->cmd_num) {
162                 for (i = 0; i < 7; i++)
163                         report->field[0]->value[i] = cmd->cmd[j++];
164
165                 usbhid_submit_report(hid, report, USB_DIR_OUT);
166         }
167 }
168
169 int lg4ff_init(struct hid_device *hid)
170 {
171         struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
172         struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
173         struct input_dev *dev = hidinput->input;
174         struct hid_report *report;
175         struct hid_field *field;
176         struct usb_device_descriptor *udesc = 0;
177         int error, i, j;
178         __u16 bcdDevice, rev_maj, rev_min;
179
180         /* Find the report to use */
181         if (list_empty(report_list)) {
182                 hid_err(hid, "No output report found\n");
183                 return -1;
184         }
185
186         /* Check that the report looks ok */
187         report = list_entry(report_list->next, struct hid_report, list);
188         if (!report) {
189                 hid_err(hid, "NULL output report\n");
190                 return -1;
191         }
192
193         field = report->field[0];
194         if (!field) {
195                 hid_err(hid, "NULL field\n");
196                 return -1;
197         }
198         
199         /* Check what wheel has been connected */
200         for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
201                 if (hid->product == lg4ff_devices[i].product_id) {
202                         dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
203                         break;
204                 }
205         }
206
207         if (i == ARRAY_SIZE(lg4ff_devices)) {
208                 hid_err(hid, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to"
209                              "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n");
210                 return -1;
211         }
212
213         /* Attempt to switch wheel to native mode when applicable */
214         udesc = &(hid_to_usb_dev(hid)->descriptor);
215         if (!udesc) {
216                 hid_err(hid, "NULL USB device descriptor\n");
217                 return -1;
218         }
219         bcdDevice = le16_to_cpu(udesc->bcdDevice);
220         rev_maj = bcdDevice >> 8;
221         rev_min = bcdDevice & 0xff;
222
223         if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_WHEEL) {
224                 dbg_hid("Generic wheel detected, can it do native?\n");
225                 dbg_hid("USB revision: %2x.%02x\n", rev_maj, rev_min);
226
227                 for (j = 0; j < ARRAY_SIZE(lg4ff_revs); j++) {
228                         if (lg4ff_revs[j].rev_maj == rev_maj && lg4ff_revs[j].rev_min == rev_min) {
229                                 hid_lg4ff_switch_native(hid, lg4ff_revs[j].command);
230                                 hid_info(hid, "Switched to native mode\n");
231                         }
232                 }
233         }
234
235         /* Set supported force feedback capabilities */
236         for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
237                 set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
238
239         error = input_ff_create_memless(dev, NULL, hid_lg4ff_play);
240
241         if (error)
242                 return error;
243
244         if (test_bit(FF_AUTOCENTER, dev->ffbit))
245                 dev->ff->set_autocenter = hid_lg4ff_set_autocenter;
246
247         hid_info(hid, "Force feedback for Logitech Speed Force Wireless by Simon Wood <simon@mungewell.org>\n");
248         return 0;
249 }
250