HID: move ignore quirks
[linux-2.6.git] / 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_rel(c)      hid_map_usage(hidinput, usage, bit, max, EV_REL, (c))
20 #define map_key(c)      hid_map_usage(hidinput, usage, bit, max, EV_KEY, (c))
21
22 #define map_key_clear(c)        hid_map_usage_clear(hidinput, usage, bit, \
23                 max, EV_KEY, (c))
24
25 static int quirk_belkin_wkbd(struct hid_usage *usage,
26                 struct hid_input *hidinput, unsigned long **bit, int *max)
27 {
28         if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
29                 return 0;
30
31         switch (usage->hid & HID_USAGE) {
32                 case 0x03a: map_key_clear(KEY_SOUND);           break;
33                 case 0x03b: map_key_clear(KEY_CAMERA);          break;
34                 case 0x03c: map_key_clear(KEY_DOCUMENTS);       break;
35                 default:
36                         return 0;
37         }
38         return 1;
39 }
40
41 static int quirk_cherry_cymotion(struct hid_usage *usage,
42                 struct hid_input *hidinput, unsigned long **bit, int *max)
43 {
44         if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
45                 return 0;
46
47         switch (usage->hid & HID_USAGE) {
48                 case 0x301: map_key_clear(KEY_PROG1);           break;
49                 case 0x302: map_key_clear(KEY_PROG2);           break;
50                 case 0x303: map_key_clear(KEY_PROG3);           break;
51                 default:
52                         return 0;
53         }
54         return 1;
55 }
56
57 static int quirk_gyration_remote(struct hid_usage *usage,
58                 struct hid_input *hidinput, 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, hidinput->input->evbit);
64         switch(usage->hid & HID_USAGE) {
65                 /* Reported on Gyration MCE Remote */
66                 case 0x00d: map_key_clear(KEY_HOME);            break;
67                 case 0x024: map_key_clear(KEY_DVD);             break;
68                 case 0x025: map_key_clear(KEY_PVR);             break;
69                 case 0x046: map_key_clear(KEY_MEDIA);           break;
70                 case 0x047: map_key_clear(KEY_MP3);             break;
71                 case 0x049: map_key_clear(KEY_CAMERA);          break;
72                 case 0x04a: map_key_clear(KEY_VIDEO);           break;
73
74                 default:
75                         return 0;
76         }
77         return 1;
78 }
79
80 static int quirk_chicony_tactical_pad(struct hid_usage *usage,
81                 struct hid_input *hidinput, unsigned long **bit, int *max)
82 {
83         if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR)
84                 return 0;
85
86         set_bit(EV_REP, hidinput->input->evbit);
87         switch (usage->hid & HID_USAGE) {
88                 case 0xff01: map_key_clear(BTN_1);              break;
89                 case 0xff02: map_key_clear(BTN_2);              break;
90                 case 0xff03: map_key_clear(BTN_3);              break;
91                 case 0xff04: map_key_clear(BTN_4);              break;
92                 case 0xff05: map_key_clear(BTN_5);              break;
93                 case 0xff06: map_key_clear(BTN_6);              break;
94                 case 0xff07: map_key_clear(BTN_7);              break;
95                 case 0xff08: map_key_clear(BTN_8);              break;
96                 case 0xff09: map_key_clear(BTN_9);              break;
97                 case 0xff0a: map_key_clear(BTN_A);              break;
98                 case 0xff0b: map_key_clear(BTN_B);              break;
99                 default:
100                         return 0;
101         }
102         return 1;
103 }
104
105 static int quirk_microsoft_ergonomy_kb(struct hid_usage *usage,
106                 struct hid_input *hidinput, unsigned long **bit, int *max)
107 {
108         struct input_dev *input = hidinput->input;
109
110         if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR)
111                 return 0;
112
113         switch(usage->hid & HID_USAGE) {
114                 case 0xfd06: map_key_clear(KEY_CHAT);           break;
115                 case 0xfd07: map_key_clear(KEY_PHONE);          break;
116                 case 0xff05:
117                         set_bit(EV_REP, input->evbit);
118                         map_key_clear(KEY_F13);
119                         set_bit(KEY_F14, input->keybit);
120                         set_bit(KEY_F15, input->keybit);
121                         set_bit(KEY_F16, input->keybit);
122                         set_bit(KEY_F17, input->keybit);
123                         set_bit(KEY_F18, input->keybit);
124                 default:
125                         return 0;
126         }
127         return 1;
128 }
129
130 static int quirk_microsoft_presenter_8k(struct hid_usage *usage,
131                 struct hid_input *hidinput, unsigned long **bit, int *max)
132 {
133         if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR)
134                 return 0;
135
136         set_bit(EV_REP, hidinput->input->evbit);
137         switch(usage->hid & HID_USAGE) {
138                 case 0xfd08: map_key_clear(KEY_FORWARD);        break;
139                 case 0xfd09: map_key_clear(KEY_BACK);           break;
140                 case 0xfd0b: map_key_clear(KEY_PLAYPAUSE);      break;
141                 case 0xfd0e: map_key_clear(KEY_CLOSE);          break;
142                 case 0xfd0f: map_key_clear(KEY_PLAY);           break;
143                 default:
144                         return 0;
145         }
146         return 1;
147 }
148
149 static int quirk_petalynx_remote(struct hid_usage *usage,
150                 struct hid_input *hidinput, unsigned long **bit, int *max)
151 {
152         if (((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR) &&
153                         ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER))
154                 return 0;
155
156         if ((usage->hid & HID_USAGE_PAGE) == HID_UP_LOGIVENDOR)
157                 switch(usage->hid & HID_USAGE) {
158                         case 0x05a: map_key_clear(KEY_TEXT);            break;
159                         case 0x05b: map_key_clear(KEY_RED);             break;
160                         case 0x05c: map_key_clear(KEY_GREEN);           break;
161                         case 0x05d: map_key_clear(KEY_YELLOW);          break;
162                         case 0x05e: map_key_clear(KEY_BLUE);            break;
163                         default:
164                                 return 0;
165                 }
166
167         if ((usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER)
168                 switch(usage->hid & HID_USAGE) {
169                         case 0x0f6: map_key_clear(KEY_NEXT);            break;
170                         case 0x0fa: map_key_clear(KEY_BACK);            break;
171                         default:
172                                 return 0;
173                 }
174         return 1;
175 }
176
177 static int quirk_cherry_genius_29e(struct hid_usage *usage,
178                 struct hid_input *hidinput, unsigned long **bit, int *max)
179 {
180         if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
181                 return 0;
182
183         switch (usage->hid & HID_USAGE) {
184                 case 0x156: map_key_clear(KEY_WORDPROCESSOR);   break;
185                 case 0x157: map_key_clear(KEY_SPREADSHEET);     break;
186                 case 0x158: map_key_clear(KEY_PRESENTATION);    break;
187                 case 0x15c: map_key_clear(KEY_STOP);            break;
188
189                 default:
190                         return 0;
191         }
192         return 1;
193 }
194
195 static int quirk_btc_8193(struct hid_usage *usage, struct hid_input *hidinput,
196                               unsigned long **bit, int *max)
197 {
198         if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
199                 return 0;
200
201         switch (usage->hid & HID_USAGE) {
202                 case 0x230: map_key(BTN_MOUSE);                 break;
203                 case 0x231: map_rel(REL_WHEEL);                 break;
204                 /* 
205                  * this keyboard has a scrollwheel implemented in
206                  * totally broken way. We map this usage temporarily
207                  * to HWHEEL and handle it in the event quirk handler
208                  */
209                 case 0x232: map_rel(REL_HWHEEL);                break;
210
211                 default:
212                         return 0;
213         }
214         return 1;
215 }
216
217 static int quirk_sunplus_wdesktop(struct hid_usage *usage,
218                 struct hid_input *hidinput, unsigned long **bit, int *max)
219 {
220         if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
221                 return 0;
222
223         switch (usage->hid & HID_USAGE) {
224                 case 0x2003: map_key_clear(KEY_ZOOMIN);         break;
225                 case 0x2103: map_key_clear(KEY_ZOOMOUT);        break;
226                 default:
227                         return 0;
228         }
229         return 1;
230 }
231
232 #define VENDOR_ID_BELKIN                        0x1020
233 #define DEVICE_ID_BELKIN_WIRELESS_KEYBOARD      0x0006
234
235 #define VENDOR_ID_CHERRY                        0x046a
236 #define DEVICE_ID_CHERRY_CYMOTION               0x0023
237
238 #define VENDOR_ID_CHICONY                       0x04f2
239 #define DEVICE_ID_CHICONY_TACTICAL_PAD          0x0418
240
241 #define VENDOR_ID_EZKEY                         0x0518
242 #define DEVICE_ID_BTC_8193                      0x0002
243
244 #define VENDOR_ID_GYRATION                      0x0c16
245 #define DEVICE_ID_GYRATION_REMOTE               0x0002
246
247 #define VENDOR_ID_MICROSOFT                     0x045e
248 #define DEVICE_ID_MS4K                          0x00db
249 #define DEVICE_ID_MS6K                          0x00f9
250 #define DEVICE_IS_MS_PRESENTER_8K_BT            0x0701
251 #define DEVICE_ID_MS_PRESENTER_8K_USB           0x0713
252
253 #define VENDOR_ID_MONTEREY                      0x0566
254 #define DEVICE_ID_GENIUS_KB29E                  0x3004
255
256 #define VENDOR_ID_PETALYNX                      0x18b1
257 #define DEVICE_ID_PETALYNX_MAXTER_REMOTE        0x0037
258
259 #define VENDOR_ID_SUNPLUS                       0x04fc
260 #define DEVICE_ID_SUNPLUS_WDESKTOP              0x05d8
261
262 static const struct hid_input_blacklist {
263         __u16 idVendor;
264         __u16 idProduct;
265         int (*quirk)(struct hid_usage *, struct hid_input *, unsigned long **,
266                         int *);
267 } hid_input_blacklist[] = {
268         { VENDOR_ID_BELKIN, DEVICE_ID_BELKIN_WIRELESS_KEYBOARD, quirk_belkin_wkbd },
269
270         { VENDOR_ID_CHERRY, DEVICE_ID_CHERRY_CYMOTION, quirk_cherry_cymotion },
271
272         { VENDOR_ID_CHICONY, DEVICE_ID_CHICONY_TACTICAL_PAD, quirk_chicony_tactical_pad },
273
274         { VENDOR_ID_EZKEY, DEVICE_ID_BTC_8193, quirk_btc_8193 },
275
276         { VENDOR_ID_GYRATION, DEVICE_ID_GYRATION_REMOTE, quirk_gyration_remote },
277
278         { VENDOR_ID_MICROSOFT, DEVICE_ID_MS4K, quirk_microsoft_ergonomy_kb },
279         { VENDOR_ID_MICROSOFT, DEVICE_ID_MS6K, quirk_microsoft_ergonomy_kb },
280         { VENDOR_ID_MICROSOFT, DEVICE_IS_MS_PRESENTER_8K_BT, quirk_microsoft_presenter_8k },
281         { VENDOR_ID_MICROSOFT, DEVICE_ID_MS_PRESENTER_8K_USB, quirk_microsoft_presenter_8k },
282
283         { VENDOR_ID_MONTEREY, DEVICE_ID_GENIUS_KB29E, quirk_cherry_genius_29e },
284
285         { VENDOR_ID_PETALYNX, DEVICE_ID_PETALYNX_MAXTER_REMOTE, quirk_petalynx_remote },
286
287         { VENDOR_ID_SUNPLUS, DEVICE_ID_SUNPLUS_WDESKTOP, quirk_sunplus_wdesktop },
288
289         { 0, 0, NULL }
290 };
291
292 int hidinput_mapping_quirks(struct hid_usage *usage, 
293                 struct hid_input *hi, unsigned long **bit, int *max)
294 {
295         struct hid_device *device = input_get_drvdata(hi->input);
296         int i = 0;
297         
298         while (hid_input_blacklist[i].quirk) {
299                 if (hid_input_blacklist[i].idVendor == device->vendor &&
300                                 hid_input_blacklist[i].idProduct == device->product)
301                         return hid_input_blacklist[i].quirk(usage, hi, bit,
302                                         max);
303                 i++;
304         }
305         return 0;
306 }
307
308 int hidinput_event_quirks(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
309 {
310         struct input_dev *input;
311
312         input = field->hidinput->input;
313
314         if (((hid->quirks & HID_QUIRK_2WHEEL_MOUSE_HACK_5) && (usage->hid == 0x00090005))
315                 || ((hid->quirks & HID_QUIRK_2WHEEL_MOUSE_HACK_7) && (usage->hid == 0x00090007))) {
316                 if (value) hid->quirks |=  HID_QUIRK_2WHEEL_MOUSE_HACK_ON;
317                 else       hid->quirks &= ~HID_QUIRK_2WHEEL_MOUSE_HACK_ON;
318                 return 1;
319         }
320
321         if ((hid->quirks & HID_QUIRK_2WHEEL_MOUSE_HACK_B8) &&
322                         (usage->type == EV_REL) &&
323                         (usage->code == REL_WHEEL)) {
324                 hid->delayed_value = value;
325                 return 1;
326         }
327
328         if ((hid->quirks & HID_QUIRK_2WHEEL_MOUSE_HACK_B8) &&
329                         (usage->hid == 0x000100b8)) {
330                 input_event(input, EV_REL, value ? REL_HWHEEL : REL_WHEEL, hid->delayed_value);
331                 return 1;
332         }
333
334         if ((hid->quirks & HID_QUIRK_INVERT_HWHEEL) && (usage->code == REL_HWHEEL)) {
335                 input_event(input, usage->type, usage->code, -value);
336                 return 1;
337         }
338
339         if ((hid->quirks & HID_QUIRK_2WHEEL_MOUSE_HACK_ON) && (usage->code == REL_WHEEL)) {
340                 input_event(input, usage->type, REL_HWHEEL, value);
341                 return 1;
342         }
343
344         if ((hid->quirks & HID_QUIRK_APPLE_HAS_FN) && hidinput_apple_event(hid, input, usage, value))
345                 return 1;
346
347         /* Handling MS keyboards special buttons */
348         if (hid->quirks & HID_QUIRK_MICROSOFT_KEYS && 
349                         usage->hid == (HID_UP_MSVENDOR | 0xff05)) {
350                 int key = 0;
351                 static int last_key = 0;
352                 switch (value) {
353                         case 0x01: key = KEY_F14; break;
354                         case 0x02: key = KEY_F15; break;
355                         case 0x04: key = KEY_F16; break;
356                         case 0x08: key = KEY_F17; break;
357                         case 0x10: key = KEY_F18; break;
358                         default: break;
359                 }
360                 if (key) {
361                         input_event(input, usage->type, key, 1);
362                         last_key = key;
363                 } else {
364                         input_event(input, usage->type, last_key, 0);
365                 }
366         }
367
368         /* handle the temporary quirky mapping to HWHEEL */
369         if (hid->quirks & HID_QUIRK_HWHEEL_WHEEL_INVERT &&
370                         usage->type == EV_REL && usage->code == REL_HWHEEL) {
371                 input_event(input, usage->type, REL_WHEEL, -value);
372                 return 1;
373         }
374
375         /* Gyration MCE remote "Sleep" key */
376         if (hid->vendor == VENDOR_ID_GYRATION &&
377             hid->product == DEVICE_ID_GYRATION_REMOTE &&
378             (usage->hid & HID_USAGE_PAGE) == HID_UP_GENDESK &&
379             (usage->hid & 0xff) == 0x82) {
380                 input_event(input, usage->type, usage->code, 1);
381                 input_sync(input);
382                 input_event(input, usage->type, usage->code, 0);
383                 input_sync(input);
384                 return 1;
385         }
386         return 0;
387 }
388
389