HID: add driver for the Logitech Flight System G940
[linux-2.6.git] / drivers / hid / hid-lg.c
1 /*
2  *  HID driver for some logitech "special" devices
3  *
4  *  Copyright (c) 1999 Andreas Gal
5  *  Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6  *  Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7  *  Copyright (c) 2006-2007 Jiri Kosina
8  *  Copyright (c) 2007 Paul Walmsley
9  *  Copyright (c) 2008 Jiri Slaby
10  */
11
12 /*
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU General Public License as published by the Free
15  * Software Foundation; either version 2 of the License, or (at your option)
16  * any later version.
17  */
18
19 #include <linux/device.h>
20 #include <linux/hid.h>
21 #include <linux/module.h>
22
23 #include "hid-ids.h"
24 #include "hid-lg.h"
25
26 #define LG_RDESC                0x001
27 #define LG_BAD_RELATIVE_KEYS    0x002
28 #define LG_DUPLICATE_USAGES     0x004
29 #define LG_EXPANDED_KEYMAP      0x010
30 #define LG_IGNORE_DOUBLED_WHEEL 0x020
31 #define LG_WIRELESS             0x040
32 #define LG_INVERT_HWHEEL        0x080
33 #define LG_NOGET                0x100
34 #define LG_FF                   0x200
35 #define LG_FF2                  0x400
36 #define LG_RDESC_REL_ABS        0x800
37 #define LG_FF3                  0x1000
38
39 /*
40  * Certain Logitech keyboards send in report #3 keys which are far
41  * above the logical maximum described in descriptor. This extends
42  * the original value of 0x28c of logical maximum to 0x104d
43  */
44 static void lg_report_fixup(struct hid_device *hdev, __u8 *rdesc,
45                 unsigned int rsize)
46 {
47         unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
48
49         if ((quirks & LG_RDESC) && rsize >= 90 && rdesc[83] == 0x26 &&
50                         rdesc[84] == 0x8c && rdesc[85] == 0x02) {
51                 dev_info(&hdev->dev, "fixing up Logitech keyboard report "
52                                 "descriptor\n");
53                 rdesc[84] = rdesc[89] = 0x4d;
54                 rdesc[85] = rdesc[90] = 0x10;
55         }
56         if ((quirks & LG_RDESC_REL_ABS) && rsize >= 50 &&
57                         rdesc[32] == 0x81 && rdesc[33] == 0x06 &&
58                         rdesc[49] == 0x81 && rdesc[50] == 0x06) {
59                 dev_info(&hdev->dev, "fixing up rel/abs in Logitech "
60                                 "report descriptor\n");
61                 rdesc[33] = rdesc[50] = 0x02;
62         }
63 }
64
65 #define lg_map_key_clear(c)     hid_map_usage_clear(hi, usage, bit, max, \
66                 EV_KEY, (c))
67
68 static int lg_ultrax_remote_mapping(struct hid_input *hi,
69                 struct hid_usage *usage, unsigned long **bit, int *max)
70 {
71         if ((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR)
72                 return 0;
73
74         set_bit(EV_REP, hi->input->evbit);
75         switch (usage->hid & HID_USAGE) {
76         /* Reported on Logitech Ultra X Media Remote */
77         case 0x004: lg_map_key_clear(KEY_AGAIN);        break;
78         case 0x00d: lg_map_key_clear(KEY_HOME);         break;
79         case 0x024: lg_map_key_clear(KEY_SHUFFLE);      break;
80         case 0x025: lg_map_key_clear(KEY_TV);           break;
81         case 0x026: lg_map_key_clear(KEY_MENU);         break;
82         case 0x031: lg_map_key_clear(KEY_AUDIO);        break;
83         case 0x032: lg_map_key_clear(KEY_TEXT);         break;
84         case 0x033: lg_map_key_clear(KEY_LAST);         break;
85         case 0x047: lg_map_key_clear(KEY_MP3);          break;
86         case 0x048: lg_map_key_clear(KEY_DVD);          break;
87         case 0x049: lg_map_key_clear(KEY_MEDIA);        break;
88         case 0x04a: lg_map_key_clear(KEY_VIDEO);        break;
89         case 0x04b: lg_map_key_clear(KEY_ANGLE);        break;
90         case 0x04c: lg_map_key_clear(KEY_LANGUAGE);     break;
91         case 0x04d: lg_map_key_clear(KEY_SUBTITLE);     break;
92         case 0x051: lg_map_key_clear(KEY_RED);          break;
93         case 0x052: lg_map_key_clear(KEY_CLOSE);        break;
94
95         default:
96                 return 0;
97         }
98         return 1;
99 }
100
101 static int lg_dinovo_mapping(struct hid_input *hi, struct hid_usage *usage,
102                 unsigned long **bit, int *max)
103 {
104         if ((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR)
105                 return 0;
106
107         switch (usage->hid & HID_USAGE) {
108
109         case 0x00d: lg_map_key_clear(KEY_MEDIA);        break;
110         default:
111                 return 0;
112
113         }
114         return 1;
115 }
116
117 static int lg_wireless_mapping(struct hid_input *hi, struct hid_usage *usage,
118                 unsigned long **bit, int *max)
119 {
120         if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
121                 return 0;
122
123         switch (usage->hid & HID_USAGE) {
124         case 0x1001: lg_map_key_clear(KEY_MESSENGER);           break;
125         case 0x1003: lg_map_key_clear(KEY_SOUND);               break;
126         case 0x1004: lg_map_key_clear(KEY_VIDEO);               break;
127         case 0x1005: lg_map_key_clear(KEY_AUDIO);               break;
128         case 0x100a: lg_map_key_clear(KEY_DOCUMENTS);           break;
129         case 0x1011: lg_map_key_clear(KEY_PREVIOUSSONG);        break;
130         case 0x1012: lg_map_key_clear(KEY_NEXTSONG);            break;
131         case 0x1013: lg_map_key_clear(KEY_CAMERA);              break;
132         case 0x1014: lg_map_key_clear(KEY_MESSENGER);           break;
133         case 0x1015: lg_map_key_clear(KEY_RECORD);              break;
134         case 0x1016: lg_map_key_clear(KEY_PLAYER);              break;
135         case 0x1017: lg_map_key_clear(KEY_EJECTCD);             break;
136         case 0x1018: lg_map_key_clear(KEY_MEDIA);               break;
137         case 0x1019: lg_map_key_clear(KEY_PROG1);               break;
138         case 0x101a: lg_map_key_clear(KEY_PROG2);               break;
139         case 0x101b: lg_map_key_clear(KEY_PROG3);               break;
140         case 0x101f: lg_map_key_clear(KEY_ZOOMIN);              break;
141         case 0x1020: lg_map_key_clear(KEY_ZOOMOUT);             break;
142         case 0x1021: lg_map_key_clear(KEY_ZOOMRESET);           break;
143         case 0x1023: lg_map_key_clear(KEY_CLOSE);               break;
144         case 0x1027: lg_map_key_clear(KEY_MENU);                break;
145         /* this one is marked as 'Rotate' */
146         case 0x1028: lg_map_key_clear(KEY_ANGLE);               break;
147         case 0x1029: lg_map_key_clear(KEY_SHUFFLE);             break;
148         case 0x102a: lg_map_key_clear(KEY_BACK);                break;
149         case 0x102b: lg_map_key_clear(KEY_CYCLEWINDOWS);        break;
150         case 0x1041: lg_map_key_clear(KEY_BATTERY);             break;
151         case 0x1042: lg_map_key_clear(KEY_WORDPROCESSOR);       break;
152         case 0x1043: lg_map_key_clear(KEY_SPREADSHEET);         break;
153         case 0x1044: lg_map_key_clear(KEY_PRESENTATION);        break;
154         case 0x1045: lg_map_key_clear(KEY_UNDO);                break;
155         case 0x1046: lg_map_key_clear(KEY_REDO);                break;
156         case 0x1047: lg_map_key_clear(KEY_PRINT);               break;
157         case 0x1048: lg_map_key_clear(KEY_SAVE);                break;
158         case 0x1049: lg_map_key_clear(KEY_PROG1);               break;
159         case 0x104a: lg_map_key_clear(KEY_PROG2);               break;
160         case 0x104b: lg_map_key_clear(KEY_PROG3);               break;
161         case 0x104c: lg_map_key_clear(KEY_PROG4);               break;
162
163         default:
164                 return 0;
165         }
166         return 1;
167 }
168
169 static int lg_input_mapping(struct hid_device *hdev, struct hid_input *hi,
170                 struct hid_field *field, struct hid_usage *usage,
171                 unsigned long **bit, int *max)
172 {
173         /* extended mapping for certain Logitech hardware (Logitech cordless
174            desktop LX500) */
175         static const u8 e_keymap[] = {
176                   0,216,  0,213,175,156,  0,  0,  0,  0,
177                 144,  0,  0,  0,  0,  0,  0,  0,  0,212,
178                 174,167,152,161,112,  0,  0,  0,154,  0,
179                   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
180                   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
181                   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
182                   0,  0,  0,  0,  0,183,184,185,186,187,
183                 188,189,190,191,192,193,194,  0,  0,  0
184         };
185         unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
186         unsigned int hid = usage->hid;
187
188         if (hdev->product == USB_DEVICE_ID_LOGITECH_RECEIVER &&
189                         lg_ultrax_remote_mapping(hi, usage, bit, max))
190                 return 1;
191
192         if (hdev->product == USB_DEVICE_ID_DINOVO_MINI &&
193                         lg_dinovo_mapping(hi, usage, bit, max))
194                 return 1;
195
196         if ((quirks & LG_WIRELESS) && lg_wireless_mapping(hi, usage, bit, max))
197                 return 1;
198
199         if ((hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
200                 return 0;
201
202         hid &= HID_USAGE;
203
204         /* Special handling for Logitech Cordless Desktop */
205         if (field->application == HID_GD_MOUSE) {
206                 if ((quirks & LG_IGNORE_DOUBLED_WHEEL) &&
207                                 (hid == 7 || hid == 8))
208                         return -1;
209         } else {
210                 if ((quirks & LG_EXPANDED_KEYMAP) &&
211                                 hid < ARRAY_SIZE(e_keymap) &&
212                                 e_keymap[hid] != 0) {
213                         hid_map_usage(hi, usage, bit, max, EV_KEY,
214                                         e_keymap[hid]);
215                         return 1;
216                 }
217         }
218
219         return 0;
220 }
221
222 static int lg_input_mapped(struct hid_device *hdev, struct hid_input *hi,
223                 struct hid_field *field, struct hid_usage *usage,
224                 unsigned long **bit, int *max)
225 {
226         unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
227
228         if ((quirks & LG_BAD_RELATIVE_KEYS) && usage->type == EV_KEY &&
229                         (field->flags & HID_MAIN_ITEM_RELATIVE))
230                 field->flags &= ~HID_MAIN_ITEM_RELATIVE;
231
232         if ((quirks & LG_DUPLICATE_USAGES) && (usage->type == EV_KEY ||
233                          usage->type == EV_REL || usage->type == EV_ABS))
234                 clear_bit(usage->code, *bit);
235
236         return 0;
237 }
238
239 static int lg_event(struct hid_device *hdev, struct hid_field *field,
240                 struct hid_usage *usage, __s32 value)
241 {
242         unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
243
244         if ((quirks & LG_INVERT_HWHEEL) && usage->code == REL_HWHEEL) {
245                 input_event(field->hidinput->input, usage->type, usage->code,
246                                 -value);
247                 return 1;
248         }
249
250         return 0;
251 }
252
253 static int lg_probe(struct hid_device *hdev, const struct hid_device_id *id)
254 {
255         unsigned long quirks = id->driver_data;
256         unsigned int connect_mask = HID_CONNECT_DEFAULT;
257         int ret;
258
259         hid_set_drvdata(hdev, (void *)quirks);
260
261         if (quirks & LG_NOGET)
262                 hdev->quirks |= HID_QUIRK_NOGET;
263
264         ret = hid_parse(hdev);
265         if (ret) {
266                 dev_err(&hdev->dev, "parse failed\n");
267                 goto err_free;
268         }
269
270         if (quirks & (LG_FF | LG_FF2 | LG_FF3))
271                 connect_mask &= ~HID_CONNECT_FF;
272
273         ret = hid_hw_start(hdev, connect_mask);
274         if (ret) {
275                 dev_err(&hdev->dev, "hw start failed\n");
276                 goto err_free;
277         }
278
279         if (quirks & LG_FF)
280                 lgff_init(hdev);
281         if (quirks & LG_FF2)
282                 lg2ff_init(hdev);
283         if (quirks & LG_FF3)
284                 lg3ff_init(hdev);
285
286         return 0;
287 err_free:
288         return ret;
289 }
290
291 static const struct hid_device_id lg_devices[] = {
292         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_MX3000_RECEIVER),
293                 .driver_data = LG_RDESC | LG_WIRELESS },
294         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER),
295                 .driver_data = LG_RDESC | LG_WIRELESS },
296         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_S510_RECEIVER_2),
297                 .driver_data = LG_RDESC | LG_WIRELESS },
298
299         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RECEIVER),
300                 .driver_data = LG_BAD_RELATIVE_KEYS },
301
302         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_DESKTOP),
303                 .driver_data = LG_DUPLICATE_USAGES },
304         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_EDGE),
305                 .driver_data = LG_DUPLICATE_USAGES },
306         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_DINOVO_MINI),
307                 .driver_data = LG_DUPLICATE_USAGES },
308
309         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_ELITE_KBD),
310                 .driver_data = LG_IGNORE_DOUBLED_WHEEL | LG_EXPANDED_KEYMAP },
311         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_CORDLESS_DESKTOP_LX500),
312                 .driver_data = LG_IGNORE_DOUBLED_WHEEL | LG_EXPANDED_KEYMAP },
313
314         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_EXTREME_3D),
315                 .driver_data = LG_NOGET },
316         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WHEEL),
317                 .driver_data = LG_NOGET | LG_FF },
318
319         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD),
320                 .driver_data = LG_FF },
321         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2_2),
322                 .driver_data = LG_FF },
323         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WINGMAN_F3D),
324                 .driver_data = LG_FF },
325         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_FORCE3D_PRO),
326                 .driver_data = LG_FF },
327         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL),
328                 .driver_data = LG_FF },
329         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2),
330                 .driver_data = LG_FF },
331         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G25_WHEEL),
332                 .driver_data = LG_FF },
333         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_WINGMAN_FFG ),
334                 .driver_data = LG_FF },
335         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2),
336                 .driver_data = LG_FF2 },
337         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_FLIGHT_SYSTEM_G940),
338                 .driver_data = LG_FF3 },
339         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_SPACENAVIGATOR),
340                 .driver_data = LG_RDESC_REL_ABS },
341         { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_SPACETRAVELLER),
342                 .driver_data = LG_RDESC_REL_ABS },
343         { }
344 };
345
346 MODULE_DEVICE_TABLE(hid, lg_devices);
347
348 static struct hid_driver lg_driver = {
349         .name = "logitech",
350         .id_table = lg_devices,
351         .report_fixup = lg_report_fixup,
352         .input_mapping = lg_input_mapping,
353         .input_mapped = lg_input_mapped,
354         .event = lg_event,
355         .probe = lg_probe,
356 };
357
358 static int __init lg_init(void)
359 {
360         return hid_register_driver(&lg_driver);
361 }
362
363 static void __exit lg_exit(void)
364 {
365         hid_unregister_driver(&lg_driver);
366 }
367
368 module_init(lg_init);
369 module_exit(lg_exit);
370 MODULE_LICENSE("GPL");