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