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