Merge branch 'master' of /pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / drivers / hid / hid-input-quirks.c
1 /*
2  *  HID-input usage mapping quirks
3  *
4  *  This is used to handle HID-input mappings for devices violating
5  *  HUT 1.12 specification.
6  *
7  * Copyright (c) 2007-2008 Jiri Kosina
8  */
9
10 /*
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU General Public License as published by the Free
13  * Software Foundation; either version 2 of the License
14  */
15
16 #include <linux/input.h>
17 #include <linux/hid.h>
18
19 #define map_abs(c)      do { usage->code = c; usage->type = EV_ABS; *bit = input->absbit; *max = ABS_MAX; } while (0)
20 #define map_rel(c)      do { usage->code = c; usage->type = EV_REL; *bit = input->relbit; *max = REL_MAX; } while (0)
21 #define map_key(c)      do { usage->code = c; usage->type = EV_KEY; *bit = input->keybit; *max = KEY_MAX; } while (0)
22 #define map_led(c)      do { usage->code = c; usage->type = EV_LED; *bit = input->ledbit; *max = LED_MAX; } while (0)
23
24 #define map_abs_clear(c)        do { map_abs(c); clear_bit(c, *bit); } while (0)
25 #define map_key_clear(c)        do { map_key(c); clear_bit(c, *bit); } while (0)
26
27 static int quirk_belkin_wkbd(struct hid_usage *usage, struct input_dev *input,
28                               unsigned long **bit, int *max)
29 {
30         if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
31                 return 0;
32
33         switch (usage->hid & HID_USAGE) {
34                 case 0x03a: map_key_clear(KEY_SOUND);           break;
35                 case 0x03b: map_key_clear(KEY_CAMERA);          break;
36                 case 0x03c: map_key_clear(KEY_DOCUMENTS);       break;
37                 default:
38                         return 0;
39         }
40         return 1;
41 }
42
43 static int quirk_cherry_cymotion(struct hid_usage *usage, struct input_dev *input,
44                               unsigned long **bit, int *max)
45 {
46         if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
47                 return 0;
48
49         switch (usage->hid & HID_USAGE) {
50                 case 0x301: map_key_clear(KEY_PROG1);           break;
51                 case 0x302: map_key_clear(KEY_PROG2);           break;
52                 case 0x303: map_key_clear(KEY_PROG3);           break;
53                 default:
54                         return 0;
55         }
56         return 1;
57 }
58
59 static int quirk_logitech_ultrax_remote(struct hid_usage *usage, struct input_dev *input,
60                               unsigned long **bit, int *max)
61 {
62         if ((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR)
63                 return 0;
64
65         set_bit(EV_REP, input->evbit);
66         switch(usage->hid & HID_USAGE) {
67                 /* Reported on Logitech Ultra X Media Remote */
68                 case 0x004: map_key_clear(KEY_AGAIN);           break;
69                 case 0x00d: map_key_clear(KEY_HOME);            break;
70                 case 0x024: map_key_clear(KEY_SHUFFLE);         break;
71                 case 0x025: map_key_clear(KEY_TV);              break;
72                 case 0x026: map_key_clear(KEY_MENU);            break;
73                 case 0x031: map_key_clear(KEY_AUDIO);           break;
74                 case 0x032: map_key_clear(KEY_TEXT);            break;
75                 case 0x033: map_key_clear(KEY_LAST);            break;
76                 case 0x047: map_key_clear(KEY_MP3);             break;
77                 case 0x048: map_key_clear(KEY_DVD);             break;
78                 case 0x049: map_key_clear(KEY_MEDIA);           break;
79                 case 0x04a: map_key_clear(KEY_VIDEO);           break;
80                 case 0x04b: map_key_clear(KEY_ANGLE);           break;
81                 case 0x04c: map_key_clear(KEY_LANGUAGE);        break;
82                 case 0x04d: map_key_clear(KEY_SUBTITLE);        break;
83                 case 0x051: map_key_clear(KEY_RED);             break;
84                 case 0x052: map_key_clear(KEY_CLOSE);           break;
85
86                 default:
87                         return 0;
88         }
89         return 1;
90 }
91
92 static int quirk_chicony_tactical_pad(struct hid_usage *usage, struct input_dev *input,
93                               unsigned long **bit, int *max)
94 {
95         if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR)
96                 return 0;
97
98         set_bit(EV_REP, input->evbit);
99         switch (usage->hid & HID_USAGE) {
100                 case 0xff01: map_key_clear(BTN_1);              break;
101                 case 0xff02: map_key_clear(BTN_2);              break;
102                 case 0xff03: map_key_clear(BTN_3);              break;
103                 case 0xff04: map_key_clear(BTN_4);              break;
104                 case 0xff05: map_key_clear(BTN_5);              break;
105                 case 0xff06: map_key_clear(BTN_6);              break;
106                 case 0xff07: map_key_clear(BTN_7);              break;
107                 case 0xff08: map_key_clear(BTN_8);              break;
108                 case 0xff09: map_key_clear(BTN_9);              break;
109                 case 0xff0a: map_key_clear(BTN_A);              break;
110                 case 0xff0b: map_key_clear(BTN_B);              break;
111                 default:
112                         return 0;
113         }
114         return 1;
115 }
116
117 static int quirk_microsoft_ergonomy_kb(struct hid_usage *usage, struct input_dev *input,
118                               unsigned long **bit, int *max)
119 {
120         if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR)
121                 return 0;
122
123         switch(usage->hid & HID_USAGE) {
124                 case 0xfd06: map_key_clear(KEY_CHAT);           break;
125                 case 0xfd07: map_key_clear(KEY_PHONE);          break;
126                 case 0xff05:
127                         set_bit(EV_REP, input->evbit);
128                         map_key_clear(KEY_F13);
129                         set_bit(KEY_F14, input->keybit);
130                         set_bit(KEY_F15, input->keybit);
131                         set_bit(KEY_F16, input->keybit);
132                         set_bit(KEY_F17, input->keybit);
133                         set_bit(KEY_F18, input->keybit);
134                 default:
135                         return 0;
136         }
137         return 1;
138 }
139
140 static int quirk_microsoft_presenter_8k(struct hid_usage *usage, struct input_dev *input,
141                               unsigned long **bit, int *max)
142 {
143         if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR)
144                 return 0;
145
146         set_bit(EV_REP, input->evbit);
147         switch(usage->hid & HID_USAGE) {
148                 case 0xfd08: map_key_clear(KEY_FORWARD);        break;
149                 case 0xfd09: map_key_clear(KEY_BACK);           break;
150                 case 0xfd0b: map_key_clear(KEY_PLAYPAUSE);      break;
151                 case 0xfd0e: map_key_clear(KEY_CLOSE);          break;
152                 case 0xfd0f: map_key_clear(KEY_PLAY);           break;
153                 default:
154                         return 0;
155         }
156         return 1;
157 }
158
159 static int quirk_petalynx_remote(struct hid_usage *usage, struct input_dev *input,
160                               unsigned long **bit, int *max)
161 {
162         if (((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR) &&
163                         ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER))
164                 return 0;
165
166         if ((usage->hid & HID_USAGE_PAGE) == HID_UP_LOGIVENDOR)
167                 switch(usage->hid & HID_USAGE) {
168                         case 0x05a: map_key_clear(KEY_TEXT);            break;
169                         case 0x05b: map_key_clear(KEY_RED);             break;
170                         case 0x05c: map_key_clear(KEY_GREEN);           break;
171                         case 0x05d: map_key_clear(KEY_YELLOW);          break;
172                         case 0x05e: map_key_clear(KEY_BLUE);            break;
173                         default:
174                                 return 0;
175                 }
176
177         if ((usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER)
178                 switch(usage->hid & HID_USAGE) {
179                         case 0x0f6: map_key_clear(KEY_NEXT);            break;
180                         case 0x0fa: map_key_clear(KEY_BACK);            break;
181                         default:
182                                 return 0;
183                 }
184         return 1;
185 }
186
187 static int quirk_logitech_wireless(struct hid_usage *usage, struct input_dev *input,
188                               unsigned long **bit, int *max)
189 {
190         if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
191                 return 0;
192
193         switch (usage->hid & HID_USAGE) {
194                 case 0x1001: map_key_clear(KEY_MESSENGER);      break;
195                 case 0x1003: map_key_clear(KEY_SOUND);          break;
196                 case 0x1004: map_key_clear(KEY_VIDEO);          break;
197                 case 0x1005: map_key_clear(KEY_AUDIO);          break;
198                 case 0x100a: map_key_clear(KEY_DOCUMENTS);      break;
199                 case 0x1011: map_key_clear(KEY_PREVIOUSSONG);   break;
200                 case 0x1012: map_key_clear(KEY_NEXTSONG);       break;
201                 case 0x1013: map_key_clear(KEY_CAMERA);         break;
202                 case 0x1014: map_key_clear(KEY_MESSENGER);      break;
203                 case 0x1015: map_key_clear(KEY_RECORD);         break;
204                 case 0x1016: map_key_clear(KEY_PLAYER);         break;
205                 case 0x1017: map_key_clear(KEY_EJECTCD);        break;
206                 case 0x1018: map_key_clear(KEY_MEDIA);          break;
207                 case 0x1019: map_key_clear(KEY_PROG1);          break;
208                 case 0x101a: map_key_clear(KEY_PROG2);          break;
209                 case 0x101b: map_key_clear(KEY_PROG3);          break;
210                 case 0x101f: map_key_clear(KEY_ZOOMIN);         break;
211                 case 0x1020: map_key_clear(KEY_ZOOMOUT);        break;
212                 case 0x1021: map_key_clear(KEY_ZOOMRESET);      break;
213                 case 0x1023: map_key_clear(KEY_CLOSE);          break;
214                 case 0x1027: map_key_clear(KEY_MENU);           break;
215                 /* this one is marked as 'Rotate' */
216                 case 0x1028: map_key_clear(KEY_ANGLE);          break;
217                 case 0x1029: map_key_clear(KEY_SHUFFLE);        break;
218                 case 0x102a: map_key_clear(KEY_BACK);           break;
219                 case 0x102b: map_key_clear(KEY_CYCLEWINDOWS);   break;
220                 case 0x1041: map_key_clear(KEY_BATTERY);        break;
221                 case 0x1042: map_key_clear(KEY_WORDPROCESSOR);  break;
222                 case 0x1043: map_key_clear(KEY_SPREADSHEET);    break;
223                 case 0x1044: map_key_clear(KEY_PRESENTATION);   break;
224                 case 0x1045: map_key_clear(KEY_UNDO);           break;
225                 case 0x1046: map_key_clear(KEY_REDO);           break;
226                 case 0x1047: map_key_clear(KEY_PRINT);          break;
227                 case 0x1048: map_key_clear(KEY_SAVE);           break;
228                 case 0x1049: map_key_clear(KEY_PROG1);          break;
229                 case 0x104a: map_key_clear(KEY_PROG2);          break;
230                 case 0x104b: map_key_clear(KEY_PROG3);          break;
231                 case 0x104c: map_key_clear(KEY_PROG4);          break;
232
233                 default:
234                         return 0;
235         }
236         return 1;
237 }
238
239 static int quirk_cherry_genius_29e(struct hid_usage *usage, struct input_dev *input,
240                               unsigned long **bit, int *max)
241 {
242         if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
243                 return 0;
244
245         switch (usage->hid & HID_USAGE) {
246                 case 0x156: map_key_clear(KEY_WORDPROCESSOR);   break;
247                 case 0x157: map_key_clear(KEY_SPREADSHEET);     break;
248                 case 0x158: map_key_clear(KEY_PRESENTATION);    break;
249                 case 0x15c: map_key_clear(KEY_STOP);            break;
250
251                 default:
252                         return 0;
253         }
254         return 1;
255 }
256
257 static int quirk_btc_8193(struct hid_usage *usage, struct input_dev *input,
258                               unsigned long **bit, int *max)
259 {
260         if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
261                 return 0;
262
263         switch (usage->hid & HID_USAGE) {
264                 case 0x230: map_key(BTN_MOUSE);                 break;
265                 case 0x231: map_rel(REL_WHEEL);                 break;
266                 /* 
267                  * this keyboard has a scrollwheel implemented in
268                  * totally broken way. We map this usage temporarily
269                  * to HWHEEL and handle it in the event quirk handler
270                  */
271                 case 0x232: map_rel(REL_HWHEEL);                break;
272
273                 default:
274                         return 0;
275         }
276         return 1;
277 }
278
279 static int quirk_sunplus_wdesktop(struct hid_usage *usage, struct input_dev *input,
280                               unsigned long **bit, int *max)
281 {
282         if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
283                 return 0;
284
285         switch (usage->hid & HID_USAGE) {
286                 case 0x2003: map_key_clear(KEY_ZOOMIN);         break;
287                 case 0x2103: map_key_clear(KEY_ZOOMOUT);        break;
288                 default:
289                         return 0;
290         }
291         return 1;
292 }
293
294 #define VENDOR_ID_BELKIN                        0x1020
295 #define DEVICE_ID_BELKIN_WIRELESS_KEYBOARD      0x0006
296
297 #define VENDOR_ID_CHERRY                        0x046a
298 #define DEVICE_ID_CHERRY_CYMOTION               0x0023
299
300 #define VENDOR_ID_CHICONY                       0x04f2
301 #define DEVICE_ID_CHICONY_TACTICAL_PAD          0x0418
302
303 #define VENDOR_ID_EZKEY                         0x0518
304 #define DEVICE_ID_BTC_8193                      0x0002
305
306 #define VENDOR_ID_LOGITECH                      0x046d
307 #define DEVICE_ID_LOGITECH_RECEIVER             0xc101
308 #define DEVICE_ID_S510_RECEIVER                 0xc50c
309 #define DEVICE_ID_S510_RECEIVER_2               0xc517
310 #define DEVICE_ID_MX3000_RECEIVER               0xc513
311
312 #define VENDOR_ID_MICROSOFT                     0x045e
313 #define DEVICE_ID_MS4K                          0x00db
314 #define DEVICE_ID_MS6K                          0x00f9
315 #define DEVICE_IS_MS_PRESENTER_8K_BT            0x0701
316 #define DEVICE_ID_MS_PRESENTER_8K_USB           0x0713
317
318 #define VENDOR_ID_MONTEREY                      0x0566
319 #define DEVICE_ID_GENIUS_KB29E                  0x3004
320
321 #define VENDOR_ID_PETALYNX                      0x18b1
322 #define DEVICE_ID_PETALYNX_MAXTER_REMOTE        0x0037
323
324 #define VENDOR_ID_SUNPLUS                       0x04fc
325 #define DEVICE_ID_SUNPLUS_WDESKTOP              0x05d8
326
327 static const struct hid_input_blacklist {
328         __u16 idVendor;
329         __u16 idProduct;
330         int (*quirk)(struct hid_usage *, struct input_dev *, unsigned long **, int *);
331 } hid_input_blacklist[] = {
332         { VENDOR_ID_BELKIN, DEVICE_ID_BELKIN_WIRELESS_KEYBOARD, quirk_belkin_wkbd },
333
334         { VENDOR_ID_CHERRY, DEVICE_ID_CHERRY_CYMOTION, quirk_cherry_cymotion },
335
336         { VENDOR_ID_CHICONY, DEVICE_ID_CHICONY_TACTICAL_PAD, quirk_chicony_tactical_pad },
337
338         { VENDOR_ID_EZKEY, DEVICE_ID_BTC_8193, quirk_btc_8193 },
339
340         { VENDOR_ID_LOGITECH, DEVICE_ID_LOGITECH_RECEIVER, quirk_logitech_ultrax_remote },
341         { VENDOR_ID_LOGITECH, DEVICE_ID_S510_RECEIVER, quirk_logitech_wireless },
342         { VENDOR_ID_LOGITECH, DEVICE_ID_S510_RECEIVER_2, quirk_logitech_wireless },
343         { VENDOR_ID_LOGITECH, DEVICE_ID_MX3000_RECEIVER, quirk_logitech_wireless },
344
345         { VENDOR_ID_MICROSOFT, DEVICE_ID_MS4K, quirk_microsoft_ergonomy_kb },
346         { VENDOR_ID_MICROSOFT, DEVICE_ID_MS6K, quirk_microsoft_ergonomy_kb },
347         { VENDOR_ID_MICROSOFT, DEVICE_IS_MS_PRESENTER_8K_BT, quirk_microsoft_presenter_8k },
348         { VENDOR_ID_MICROSOFT, DEVICE_ID_MS_PRESENTER_8K_USB, quirk_microsoft_presenter_8k },
349
350         { VENDOR_ID_MONTEREY, DEVICE_ID_GENIUS_KB29E, quirk_cherry_genius_29e },
351
352         { VENDOR_ID_PETALYNX, DEVICE_ID_PETALYNX_MAXTER_REMOTE, quirk_petalynx_remote },
353
354         { VENDOR_ID_SUNPLUS, DEVICE_ID_SUNPLUS_WDESKTOP, quirk_sunplus_wdesktop },
355
356         { 0, 0, NULL }
357 };
358
359 int hidinput_mapping_quirks(struct hid_usage *usage, 
360                                    struct input_dev *input, 
361                                    unsigned long **bit, int *max)
362 {
363         struct hid_device *device = input_get_drvdata(input);
364         int i = 0;
365         
366         while (hid_input_blacklist[i].quirk) {
367                 if (hid_input_blacklist[i].idVendor == device->vendor &&
368                                 hid_input_blacklist[i].idProduct == device->product)
369                         return hid_input_blacklist[i].quirk(usage, input, bit, max);
370                 i++;
371         }
372         return 0;
373 }
374
375 int hidinput_event_quirks(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
376 {
377         struct input_dev *input;
378
379         input = field->hidinput->input;
380
381         if (((hid->quirks & HID_QUIRK_2WHEEL_MOUSE_HACK_5) && (usage->hid == 0x00090005))
382                 || ((hid->quirks & HID_QUIRK_2WHEEL_MOUSE_HACK_7) && (usage->hid == 0x00090007))) {
383                 if (value) hid->quirks |=  HID_QUIRK_2WHEEL_MOUSE_HACK_ON;
384                 else       hid->quirks &= ~HID_QUIRK_2WHEEL_MOUSE_HACK_ON;
385                 return 1;
386         }
387
388         if ((hid->quirks & HID_QUIRK_2WHEEL_MOUSE_HACK_B8) &&
389                         (usage->type == EV_REL) &&
390                         (usage->code == REL_WHEEL)) {
391                 hid->delayed_value = value;
392                 return 1;
393         }
394
395         if ((hid->quirks & HID_QUIRK_2WHEEL_MOUSE_HACK_B8) &&
396                         (usage->hid == 0x000100b8)) {
397                 input_event(input, EV_REL, value ? REL_HWHEEL : REL_WHEEL, hid->delayed_value);
398                 return 1;
399         }
400
401         if ((hid->quirks & HID_QUIRK_INVERT_HWHEEL) && (usage->code == REL_HWHEEL)) {
402                 input_event(input, usage->type, usage->code, -value);
403                 return 1;
404         }
405
406         if ((hid->quirks & HID_QUIRK_2WHEEL_MOUSE_HACK_ON) && (usage->code == REL_WHEEL)) {
407                 input_event(input, usage->type, REL_HWHEEL, value);
408                 return 1;
409         }
410
411         if ((hid->quirks & HID_QUIRK_APPLE_HAS_FN) && hidinput_apple_event(hid, input, usage, value))
412                 return 1;
413
414         /* Handling MS keyboards special buttons */
415         if (hid->quirks & HID_QUIRK_MICROSOFT_KEYS && 
416                         usage->hid == (HID_UP_MSVENDOR | 0xff05)) {
417                 int key = 0;
418                 static int last_key = 0;
419                 switch (value) {
420                         case 0x01: key = KEY_F14; break;
421                         case 0x02: key = KEY_F15; break;
422                         case 0x04: key = KEY_F16; break;
423                         case 0x08: key = KEY_F17; break;
424                         case 0x10: key = KEY_F18; break;
425                         default: break;
426                 }
427                 if (key) {
428                         input_event(input, usage->type, key, 1);
429                         last_key = key;
430                 } else {
431                         input_event(input, usage->type, last_key, 0);
432                 }
433         }
434
435         /* handle the temporary quirky mapping to HWHEEL */
436         if (hid->quirks & HID_QUIRK_HWHEEL_WHEEL_INVERT &&
437                         usage->type == EV_REL && usage->code == REL_HWHEEL) {
438                 input_event(input, usage->type, REL_WHEEL, -value);
439                 return 1;
440         }
441         return 0;
442 }
443
444