blob: a6223bc5c734ef2ed23e45aeb36e1c17c87f2719 [file] [log] [blame]
Jiri Kosinadde58452006-12-08 18:40:44 +01001/*
Jiri Kosinadde58452006-12-08 18:40:44 +01002 * Copyright (c) 2000-2001 Vojtech Pavlik
Jiri Kosinab55fd232007-02-21 19:27:49 +01003 * Copyright (c) 2006-2007 Jiri Kosina
Jiri Kosinadde58452006-12-08 18:40:44 +01004 *
5 * HID to Linux Input mapping
6 */
7
8/*
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Should you need to contact me, the author, you can do so either by
24 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
25 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
26 */
27
28#include <linux/module.h>
29#include <linux/slab.h>
30#include <linux/kernel.h>
Jiri Kosinadde58452006-12-08 18:40:44 +010031
Jiri Kosinadde58452006-12-08 18:40:44 +010032#include <linux/hid.h>
Jiri Kosinac080d892007-01-25 11:43:31 +010033#include <linux/hid-debug.h>
Jiri Kosinadde58452006-12-08 18:40:44 +010034
35#define unk KEY_UNKNOWN
36
37static const unsigned char hid_keyboard[256] = {
38 0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
39 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44, 2, 3,
40 4, 5, 6, 7, 8, 9, 10, 11, 28, 1, 14, 15, 57, 12, 13, 26,
41 27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
42 65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
43 105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
44 72, 73, 82, 83, 86,127,116,117,183,184,185,186,187,188,189,190,
45 191,192,193,194,134,138,130,132,128,129,131,137,133,135,136,113,
46 115,114,unk,unk,unk,121,unk, 89, 93,124, 92, 94, 95,unk,unk,unk,
47 122,123, 90, 91, 85,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
48 unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
Jiri Kosina1fe8736d2007-08-09 13:24:11 +020049 unk,unk,unk,unk,unk,unk,179,180,unk,unk,unk,unk,unk,unk,unk,unk,
Jiri Kosinadde58452006-12-08 18:40:44 +010050 unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
51 unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
52 29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
53 150,158,159,128,136,177,178,176,142,152,173,140,unk,unk,unk,unk
54};
55
56static const struct {
57 __s32 x;
58 __s32 y;
59} hid_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
60
Jiri Slaby022e8c42008-05-16 11:49:18 +020061#define map_abs(c) hid_map_usage(hidinput, usage, &bit, &max, EV_ABS, (c))
62#define map_rel(c) hid_map_usage(hidinput, usage, &bit, &max, EV_REL, (c))
63#define map_key(c) hid_map_usage(hidinput, usage, &bit, &max, EV_KEY, (c))
64#define map_led(c) hid_map_usage(hidinput, usage, &bit, &max, EV_LED, (c))
Jiri Kosinadde58452006-12-08 18:40:44 +010065
Jiri Slaby022e8c42008-05-16 11:49:18 +020066#define map_abs_clear(c) hid_map_usage_clear(hidinput, usage, &bit, \
67 &max, EV_ABS, (c))
68#define map_key_clear(c) hid_map_usage_clear(hidinput, usage, &bit, \
69 &max, EV_KEY, (c))
Jiri Kosinadde58452006-12-08 18:40:44 +010070
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +020071static inline int match_scancode(int code, int scancode)
72{
73 if (scancode == 0)
74 return 1;
75 return ((code & (HID_USAGE_PAGE | HID_USAGE)) == scancode);
76}
77
78static inline int match_keycode(int code, int keycode)
79{
80 if (keycode == 0)
81 return 1;
82 return (code == keycode);
83}
84
85static struct hid_usage *hidinput_find_key(struct hid_device *hid,
86 int scancode, int keycode)
87{
88 int i, j, k;
89 struct hid_report *report;
90 struct hid_usage *usage;
91
92 for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
93 list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
94 for (i = 0; i < report->maxfield; i++) {
95 for ( j = 0; j < report->field[i]->maxusage; j++) {
96 usage = report->field[i]->usage + j;
97 if (usage->type == EV_KEY &&
98 match_scancode(usage->hid, scancode) &&
99 match_keycode(usage->code, keycode))
100 return usage;
101 }
102 }
103 }
104 }
105 return NULL;
106}
107
108static int hidinput_getkeycode(struct input_dev *dev, int scancode,
109 int *keycode)
110{
Dmitry Torokhovf202df62007-10-31 12:33:26 +0100111 struct hid_device *hid = input_get_drvdata(dev);
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200112 struct hid_usage *usage;
Jiri Kosina77b29662007-08-09 14:04:56 +0200113
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200114 usage = hidinput_find_key(hid, scancode, 0);
115 if (usage) {
116 *keycode = usage->code;
117 return 0;
118 }
119 return -EINVAL;
120}
121
122static int hidinput_setkeycode(struct input_dev *dev, int scancode,
123 int keycode)
124{
Dmitry Torokhovf202df62007-10-31 12:33:26 +0100125 struct hid_device *hid = input_get_drvdata(dev);
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200126 struct hid_usage *usage;
127 int old_keycode;
Jiri Kosina77b29662007-08-09 14:04:56 +0200128
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200129 if (keycode < 0 || keycode > KEY_MAX)
130 return -EINVAL;
Jiri Kosina77b29662007-08-09 14:04:56 +0200131
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200132 usage = hidinput_find_key(hid, scancode, 0);
133 if (usage) {
134 old_keycode = usage->code;
135 usage->code = keycode;
Jiri Kosina77b29662007-08-09 14:04:56 +0200136
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200137 clear_bit(old_keycode, dev->keybit);
138 set_bit(usage->code, dev->keybit);
Jiri Kosina58037eb2007-05-30 15:07:13 +0200139 dbg_hid(KERN_DEBUG "Assigned keycode %d to HID usage code %x\n", keycode, scancode);
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200140 /* Set the keybit for the old keycode if the old keycode is used
141 * by another key */
142 if (hidinput_find_key (hid, 0, old_keycode))
143 set_bit(old_keycode, dev->keybit);
Jiri Kosina77b29662007-08-09 14:04:56 +0200144
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200145 return 0;
146 }
Jiri Kosina77b29662007-08-09 14:04:56 +0200147
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200148 return -EINVAL;
149}
150
151
Jiri Kosinadde58452006-12-08 18:40:44 +0100152static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_field *field,
153 struct hid_usage *usage)
154{
155 struct input_dev *input = hidinput->input;
Dmitry Torokhove0712982007-05-09 10:17:31 +0200156 struct hid_device *device = input_get_drvdata(input);
Jiri Kosina10bd0652007-11-22 15:18:18 +0100157 int max = 0, code, ret;
Jiri Kosinadde58452006-12-08 18:40:44 +0100158 unsigned long *bit = NULL;
159
160 field->hidinput = hidinput;
161
Jiri Kosina58037eb2007-05-30 15:07:13 +0200162 dbg_hid("Mapping: ");
Jiri Kosinac080d892007-01-25 11:43:31 +0100163 hid_resolv_usage(usage->hid);
Jiri Kosina58037eb2007-05-30 15:07:13 +0200164 dbg_hid_line(" ---> ");
Jiri Kosinadde58452006-12-08 18:40:44 +0100165
166 if (field->flags & HID_MAIN_ITEM_CONSTANT)
167 goto ignore;
168
Jiri Kosina82eb1212007-08-11 23:39:42 +0200169 /* only LED usages are supported in output fields */
170 if (field->report_type == HID_OUTPUT_REPORT &&
171 (usage->hid & HID_USAGE_PAGE) != HID_UP_LED) {
172 dbg_hid_line(" [non-LED output field] ");
173 goto ignore;
174 }
175
Jiri Kosina87bc2aa2007-11-23 13:16:02 +0100176 /* handle input mappings for quirky devices */
Jiri Slaby022e8c42008-05-16 11:49:18 +0200177 ret = hidinput_mapping_quirks(usage, hidinput, &bit, &max);
Jiri Kosina10bd0652007-11-22 15:18:18 +0100178 if (ret)
179 goto mapped;
180
Jiri Slabyc500c972008-05-16 11:49:16 +0200181 if (device->driver->input_mapping) {
182 int ret = device->driver->input_mapping(device, hidinput, field,
183 usage, &bit, &max);
184 if (ret > 0)
185 goto mapped;
186 if (ret < 0)
187 goto ignore;
188 }
189
Jiri Kosinadde58452006-12-08 18:40:44 +0100190 switch (usage->hid & HID_USAGE_PAGE) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200191 case HID_UP_UNDEFINED:
192 goto ignore;
Jiri Kosinadde58452006-12-08 18:40:44 +0100193
Jiri Slaby880d29f2008-06-18 23:55:41 +0200194 case HID_UP_KEYBOARD:
195 set_bit(EV_REP, input->evbit);
Jiri Kosinadde58452006-12-08 18:40:44 +0100196
Jiri Slaby880d29f2008-06-18 23:55:41 +0200197 if ((usage->hid & HID_USAGE) < 256) {
198 if (!hid_keyboard[usage->hid & HID_USAGE]) goto ignore;
199 map_key_clear(hid_keyboard[usage->hid & HID_USAGE]);
200 } else
201 map_key(KEY_UNKNOWN);
Jiri Kosinadde58452006-12-08 18:40:44 +0100202
Jiri Slaby880d29f2008-06-18 23:55:41 +0200203 break;
Jiri Kosinadde58452006-12-08 18:40:44 +0100204
Jiri Slaby880d29f2008-06-18 23:55:41 +0200205 case HID_UP_BUTTON:
206 code = ((usage->hid - 1) & 0xf);
Jiri Kosinadde58452006-12-08 18:40:44 +0100207
Jiri Slaby880d29f2008-06-18 23:55:41 +0200208 switch (field->application) {
209 case HID_GD_MOUSE:
210 case HID_GD_POINTER: code += 0x110; break;
211 case HID_GD_JOYSTICK: code += 0x120; break;
212 case HID_GD_GAMEPAD: code += 0x130; break;
Jiri Kosinadde58452006-12-08 18:40:44 +0100213 default:
Jiri Slaby880d29f2008-06-18 23:55:41 +0200214 switch (field->physical) {
215 case HID_GD_MOUSE:
216 case HID_GD_POINTER: code += 0x110; break;
217 case HID_GD_JOYSTICK: code += 0x120; break;
218 case HID_GD_GAMEPAD: code += 0x130; break;
219 default: code += 0x100;
Jiri Kosinadde58452006-12-08 18:40:44 +0100220 }
Jiri Slaby880d29f2008-06-18 23:55:41 +0200221 }
222
223 map_key(code);
224 break;
225
226 case HID_UP_SIMULATION:
227 switch (usage->hid & 0xffff) {
228 case 0xba: map_abs(ABS_RUDDER); break;
229 case 0xbb: map_abs(ABS_THROTTLE); break;
230 case 0xc4: map_abs(ABS_GAS); break;
231 case 0xc5: map_abs(ABS_BRAKE); break;
232 case 0xc8: map_abs(ABS_WHEEL); break;
233 default: goto ignore;
234 }
235 break;
236
237 case HID_UP_GENDESK:
238 if ((usage->hid & 0xf0) == 0x80) { /* SystemControl */
239 switch (usage->hid & 0xf) {
240 case 0x1: map_key_clear(KEY_POWER); break;
241 case 0x2: map_key_clear(KEY_SLEEP); break;
242 case 0x3: map_key_clear(KEY_WAKEUP); break;
243 default: goto unknown;
Jiri Kosinadde58452006-12-08 18:40:44 +0100244 }
Jiri Kosinadde58452006-12-08 18:40:44 +0100245 break;
Jiri Slaby880d29f2008-06-18 23:55:41 +0200246 }
247
248 if ((usage->hid & 0xf0) == 0x90) { /* D-pad */
249 switch (usage->hid) {
250 case HID_GD_UP: usage->hat_dir = 1; break;
251 case HID_GD_DOWN: usage->hat_dir = 5; break;
252 case HID_GD_RIGHT: usage->hat_dir = 3; break;
253 case HID_GD_LEFT: usage->hat_dir = 7; break;
254 default: goto unknown;
255 }
256 if (field->dpad) {
257 map_abs(field->dpad);
258 goto ignore;
259 }
260 map_abs(ABS_HAT0X);
261 break;
262 }
263
264 switch (usage->hid) {
265 /* These usage IDs map directly to the usage codes. */
266 case HID_GD_X: case HID_GD_Y: case HID_GD_Z:
267 case HID_GD_RX: case HID_GD_RY: case HID_GD_RZ:
268 case HID_GD_SLIDER: case HID_GD_DIAL: case HID_GD_WHEEL:
269 if (field->flags & HID_MAIN_ITEM_RELATIVE)
270 map_rel(usage->hid & 0xf);
271 else
272 map_abs(usage->hid & 0xf);
273 break;
274
275 case HID_GD_HATSWITCH:
276 usage->hat_min = field->logical_minimum;
277 usage->hat_max = field->logical_maximum;
278 map_abs(ABS_HAT0X);
279 break;
280
281 case HID_GD_START: map_key_clear(BTN_START); break;
282 case HID_GD_SELECT: map_key_clear(BTN_SELECT); break;
283
284 default: goto unknown;
285 }
286
287 break;
288
289 case HID_UP_LED:
290 switch (usage->hid & 0xffff) { /* HID-Value: */
291 case 0x01: map_led (LED_NUML); break; /* "Num Lock" */
292 case 0x02: map_led (LED_CAPSL); break; /* "Caps Lock" */
293 case 0x03: map_led (LED_SCROLLL); break; /* "Scroll Lock" */
294 case 0x04: map_led (LED_COMPOSE); break; /* "Compose" */
295 case 0x05: map_led (LED_KANA); break; /* "Kana" */
296 case 0x27: map_led (LED_SLEEP); break; /* "Stand-By" */
297 case 0x4c: map_led (LED_SUSPEND); break; /* "System Suspend" */
298 case 0x09: map_led (LED_MUTE); break; /* "Mute" */
299 case 0x4b: map_led (LED_MISC); break; /* "Generic Indicator" */
300 case 0x19: map_led (LED_MAIL); break; /* "Message Waiting" */
301 case 0x4d: map_led (LED_CHARGING); break; /* "External Power Connected" */
302
303 default: goto ignore;
304 }
305 break;
306
307 case HID_UP_DIGITIZER:
308 switch (usage->hid & 0xff) {
309 case 0x30: /* TipPressure */
310 if (!test_bit(BTN_TOUCH, input->keybit)) {
311 device->quirks |= HID_QUIRK_NOTOUCH;
312 set_bit(EV_KEY, input->evbit);
313 set_bit(BTN_TOUCH, input->keybit);
314 }
315 map_abs_clear(ABS_PRESSURE);
316 break;
317
318 case 0x32: /* InRange */
319 switch (field->physical & 0xff) {
320 case 0x21: map_key(BTN_TOOL_MOUSE); break;
321 case 0x22: map_key(BTN_TOOL_FINGER); break;
322 default: map_key(BTN_TOOL_PEN); break;
323 }
324 break;
325
326 case 0x3c: /* Invert */
327 map_key_clear(BTN_TOOL_RUBBER);
328 break;
329
330 case 0x33: /* Touch */
331 case 0x42: /* TipSwitch */
332 case 0x43: /* TipSwitch2 */
333 device->quirks &= ~HID_QUIRK_NOTOUCH;
334 map_key_clear(BTN_TOUCH);
335 break;
336
337 case 0x44: /* BarrelSwitch */
338 map_key_clear(BTN_STYLUS);
339 break;
340
341 default: goto unknown;
342 }
343 break;
344
345 case HID_UP_CONSUMER: /* USB HUT v1.1, pages 56-62 */
346 switch (usage->hid & HID_USAGE) {
347 case 0x000: goto ignore;
348 case 0x034: map_key_clear(KEY_SLEEP); break;
349 case 0x036: map_key_clear(BTN_MISC); break;
350
351 case 0x040: map_key_clear(KEY_MENU); break;
352 case 0x045: map_key_clear(KEY_RADIO); break;
353
354 case 0x083: map_key_clear(KEY_LAST); break;
355 case 0x088: map_key_clear(KEY_PC); break;
356 case 0x089: map_key_clear(KEY_TV); break;
357 case 0x08a: map_key_clear(KEY_WWW); break;
358 case 0x08b: map_key_clear(KEY_DVD); break;
359 case 0x08c: map_key_clear(KEY_PHONE); break;
360 case 0x08d: map_key_clear(KEY_PROGRAM); break;
361 case 0x08e: map_key_clear(KEY_VIDEOPHONE); break;
362 case 0x08f: map_key_clear(KEY_GAMES); break;
363 case 0x090: map_key_clear(KEY_MEMO); break;
364 case 0x091: map_key_clear(KEY_CD); break;
365 case 0x092: map_key_clear(KEY_VCR); break;
366 case 0x093: map_key_clear(KEY_TUNER); break;
367 case 0x094: map_key_clear(KEY_EXIT); break;
368 case 0x095: map_key_clear(KEY_HELP); break;
369 case 0x096: map_key_clear(KEY_TAPE); break;
370 case 0x097: map_key_clear(KEY_TV2); break;
371 case 0x098: map_key_clear(KEY_SAT); break;
372 case 0x09a: map_key_clear(KEY_PVR); break;
373
374 case 0x09c: map_key_clear(KEY_CHANNELUP); break;
375 case 0x09d: map_key_clear(KEY_CHANNELDOWN); break;
376 case 0x0a0: map_key_clear(KEY_VCR2); break;
377
378 case 0x0b0: map_key_clear(KEY_PLAY); break;
379 case 0x0b1: map_key_clear(KEY_PAUSE); break;
380 case 0x0b2: map_key_clear(KEY_RECORD); break;
381 case 0x0b3: map_key_clear(KEY_FASTFORWARD); break;
382 case 0x0b4: map_key_clear(KEY_REWIND); break;
383 case 0x0b5: map_key_clear(KEY_NEXTSONG); break;
384 case 0x0b6: map_key_clear(KEY_PREVIOUSSONG); break;
385 case 0x0b7: map_key_clear(KEY_STOPCD); break;
386 case 0x0b8: map_key_clear(KEY_EJECTCD); break;
387 case 0x0bc: map_key_clear(KEY_MEDIA_REPEAT); break;
388
389 case 0x0cd: map_key_clear(KEY_PLAYPAUSE); break;
390 case 0x0e0: map_abs_clear(ABS_VOLUME); break;
391 case 0x0e2: map_key_clear(KEY_MUTE); break;
392 case 0x0e5: map_key_clear(KEY_BASSBOOST); break;
393 case 0x0e9: map_key_clear(KEY_VOLUMEUP); break;
394 case 0x0ea: map_key_clear(KEY_VOLUMEDOWN); break;
395
396 case 0x182: map_key_clear(KEY_BOOKMARKS); break;
397 case 0x183: map_key_clear(KEY_CONFIG); break;
398 case 0x184: map_key_clear(KEY_WORDPROCESSOR); break;
399 case 0x185: map_key_clear(KEY_EDITOR); break;
400 case 0x186: map_key_clear(KEY_SPREADSHEET); break;
401 case 0x187: map_key_clear(KEY_GRAPHICSEDITOR); break;
402 case 0x188: map_key_clear(KEY_PRESENTATION); break;
403 case 0x189: map_key_clear(KEY_DATABASE); break;
404 case 0x18a: map_key_clear(KEY_MAIL); break;
405 case 0x18b: map_key_clear(KEY_NEWS); break;
406 case 0x18c: map_key_clear(KEY_VOICEMAIL); break;
407 case 0x18d: map_key_clear(KEY_ADDRESSBOOK); break;
408 case 0x18e: map_key_clear(KEY_CALENDAR); break;
409 case 0x191: map_key_clear(KEY_FINANCE); break;
410 case 0x192: map_key_clear(KEY_CALC); break;
411 case 0x194: map_key_clear(KEY_FILE); break;
412 case 0x196: map_key_clear(KEY_WWW); break;
413 case 0x19c: map_key_clear(KEY_LOGOFF); break;
414 case 0x19e: map_key_clear(KEY_COFFEE); break;
415 case 0x1a6: map_key_clear(KEY_HELP); break;
416 case 0x1a7: map_key_clear(KEY_DOCUMENTS); break;
417 case 0x1ab: map_key_clear(KEY_SPELLCHECK); break;
418 case 0x1b6: map_key_clear(KEY_MEDIA); break;
419 case 0x1b7: map_key_clear(KEY_SOUND); break;
420 case 0x1bc: map_key_clear(KEY_MESSENGER); break;
421 case 0x1bd: map_key_clear(KEY_INFO); break;
422 case 0x201: map_key_clear(KEY_NEW); break;
423 case 0x202: map_key_clear(KEY_OPEN); break;
424 case 0x203: map_key_clear(KEY_CLOSE); break;
425 case 0x204: map_key_clear(KEY_EXIT); break;
426 case 0x207: map_key_clear(KEY_SAVE); break;
427 case 0x208: map_key_clear(KEY_PRINT); break;
428 case 0x209: map_key_clear(KEY_PROPS); break;
429 case 0x21a: map_key_clear(KEY_UNDO); break;
430 case 0x21b: map_key_clear(KEY_COPY); break;
431 case 0x21c: map_key_clear(KEY_CUT); break;
432 case 0x21d: map_key_clear(KEY_PASTE); break;
433 case 0x21f: map_key_clear(KEY_FIND); break;
434 case 0x221: map_key_clear(KEY_SEARCH); break;
435 case 0x222: map_key_clear(KEY_GOTO); break;
436 case 0x223: map_key_clear(KEY_HOMEPAGE); break;
437 case 0x224: map_key_clear(KEY_BACK); break;
438 case 0x225: map_key_clear(KEY_FORWARD); break;
439 case 0x226: map_key_clear(KEY_STOP); break;
440 case 0x227: map_key_clear(KEY_REFRESH); break;
441 case 0x22a: map_key_clear(KEY_BOOKMARKS); break;
442 case 0x22d: map_key_clear(KEY_ZOOMIN); break;
443 case 0x22e: map_key_clear(KEY_ZOOMOUT); break;
444 case 0x22f: map_key_clear(KEY_ZOOMRESET); break;
445 case 0x233: map_key_clear(KEY_SCROLLUP); break;
446 case 0x234: map_key_clear(KEY_SCROLLDOWN); break;
447 case 0x238: map_rel(REL_HWHEEL); break;
448 case 0x25f: map_key_clear(KEY_CANCEL); break;
449 case 0x279: map_key_clear(KEY_REDO); break;
450
451 case 0x289: map_key_clear(KEY_REPLY); break;
452 case 0x28b: map_key_clear(KEY_FORWARDMAIL); break;
453 case 0x28c: map_key_clear(KEY_SEND); break;
454
455 default: goto ignore;
456 }
457 break;
458
459 case HID_UP_HPVENDOR: /* Reported on a Dutch layout HP5308 */
460 set_bit(EV_REP, input->evbit);
461 switch (usage->hid & HID_USAGE) {
462 case 0x021: map_key_clear(KEY_PRINT); break;
463 case 0x070: map_key_clear(KEY_HP); break;
464 case 0x071: map_key_clear(KEY_CAMERA); break;
465 case 0x072: map_key_clear(KEY_SOUND); break;
466 case 0x073: map_key_clear(KEY_QUESTION); break;
467 case 0x080: map_key_clear(KEY_EMAIL); break;
468 case 0x081: map_key_clear(KEY_CHAT); break;
469 case 0x082: map_key_clear(KEY_SEARCH); break;
470 case 0x083: map_key_clear(KEY_CONNECT); break;
471 case 0x084: map_key_clear(KEY_FINANCE); break;
472 case 0x085: map_key_clear(KEY_SPORT); break;
473 case 0x086: map_key_clear(KEY_SHOP); break;
474 default: goto ignore;
475 }
476 break;
477
478 case HID_UP_MSVENDOR:
479 goto ignore;
480
481 case HID_UP_CUSTOM: /* Reported on Logitech and Apple USB keyboards */
482 set_bit(EV_REP, input->evbit);
483 goto ignore;
484
485 case HID_UP_LOGIVENDOR:
486 goto ignore;
487
488 case HID_UP_PID:
489 switch (usage->hid & HID_USAGE) {
490 case 0xa4: map_key_clear(BTN_DEAD); break;
491 default: goto ignore;
492 }
493 break;
494
495 default:
496 unknown:
497 if (field->report_size == 1) {
498 if (field->report->type == HID_OUTPUT_REPORT) {
499 map_led(LED_MISC);
500 break;
501 }
502 map_key(BTN_MISC);
503 break;
504 }
505 if (field->flags & HID_MAIN_ITEM_RELATIVE) {
506 map_rel(REL_MISC);
507 break;
508 }
509 map_abs(ABS_MISC);
510 break;
Jiri Kosinadde58452006-12-08 18:40:44 +0100511 }
512
Jiri Kosina10bd0652007-11-22 15:18:18 +0100513mapped:
Jiri Slabyc500c972008-05-16 11:49:16 +0200514 if (device->driver->input_mapped && device->driver->input_mapped(device,
515 hidinput, field, usage, &bit, &max) < 0)
516 goto ignore;
517
Pavel Trollerc80e5ff2007-10-29 11:13:46 +0100518 if ((device->quirks & (HID_QUIRK_2WHEEL_MOUSE_HACK_7 | HID_QUIRK_2WHEEL_MOUSE_HACK_5 |
519 HID_QUIRK_2WHEEL_MOUSE_HACK_B8)) && (usage->type == EV_REL) &&
520 (usage->code == REL_WHEEL))
521 set_bit(REL_HWHEEL, bit);
Jiri Kosinadde58452006-12-08 18:40:44 +0100522
523 if (((device->quirks & HID_QUIRK_2WHEEL_MOUSE_HACK_5) && (usage->hid == 0x00090005))
524 || ((device->quirks & HID_QUIRK_2WHEEL_MOUSE_HACK_7) && (usage->hid == 0x00090007)))
525 goto ignore;
526
Jiri Kosinadde58452006-12-08 18:40:44 +0100527 set_bit(usage->type, input->evbit);
528
529 while (usage->code <= max && test_and_set_bit(usage->code, bit))
530 usage->code = find_next_zero_bit(bit, max + 1, usage->code);
531
532 if (usage->code > max)
533 goto ignore;
534
535
536 if (usage->type == EV_ABS) {
537
538 int a = field->logical_minimum;
539 int b = field->logical_maximum;
540
541 if ((device->quirks & HID_QUIRK_BADPAD) && (usage->code == ABS_X || usage->code == ABS_Y)) {
542 a = field->logical_minimum = 0;
543 b = field->logical_maximum = 255;
544 }
545
546 if (field->application == HID_GD_GAMEPAD || field->application == HID_GD_JOYSTICK)
547 input_set_abs_params(input, usage->code, a, b, (b - a) >> 8, (b - a) >> 4);
548 else input_set_abs_params(input, usage->code, a, b, 0, 0);
549
550 }
551
552 if (usage->type == EV_ABS &&
553 (usage->hat_min < usage->hat_max || usage->hat_dir)) {
554 int i;
555 for (i = usage->code; i < usage->code + 2 && i <= max; i++) {
556 input_set_abs_params(input, i, -1, 1, 0, 0);
557 set_bit(i, input->absbit);
558 }
559 if (usage->hat_dir && !field->dpad)
560 field->dpad = usage->code;
561 }
562
Jiri Kosina2c1d8ae2007-07-04 16:45:59 +0200563 /* for those devices which produce Consumer volume usage as relative,
564 * we emulate pressing volumeup/volumedown appropriate number of times
565 * in hidinput_hid_event()
566 */
567 if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
568 (usage->code == ABS_VOLUME)) {
569 set_bit(KEY_VOLUMEUP, input->keybit);
570 set_bit(KEY_VOLUMEDOWN, input->keybit);
571 }
572
Jiri Kosinac01d50d2007-08-20 12:13:34 +0200573 if (usage->type == EV_KEY) {
574 set_bit(EV_MSC, input->evbit);
575 set_bit(MSC_SCAN, input->mscbit);
576 }
577
Jiri Kosinac080d892007-01-25 11:43:31 +0100578 hid_resolv_event(usage->type, usage->code);
Jiri Kosina58037eb2007-05-30 15:07:13 +0200579
580 dbg_hid_line("\n");
581
Jiri Kosinadde58452006-12-08 18:40:44 +0100582 return;
583
584ignore:
Jiri Kosina58037eb2007-05-30 15:07:13 +0200585 dbg_hid_line("IGNORED\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100586 return;
587}
588
589void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
590{
591 struct input_dev *input;
Al Virob4482a42007-10-14 19:35:40 +0100592 unsigned *quirks = &hid->quirks;
Jiri Kosinadde58452006-12-08 18:40:44 +0100593
594 if (!field->hidinput)
595 return;
596
597 input = field->hidinput->input;
598
599 if (!usage->type)
600 return;
601
Jiri Kosina87bc2aa2007-11-23 13:16:02 +0100602 /* handle input events for quirky devices */
Jiri Kosina68a1f2c2008-02-07 16:48:46 +0100603 if (hidinput_event_quirks(hid, field, usage, value))
604 return;
Jiri Kosinadde58452006-12-08 18:40:44 +0100605
606 if (usage->hat_min < usage->hat_max || usage->hat_dir) {
607 int hat_dir = usage->hat_dir;
608 if (!hat_dir)
609 hat_dir = (value - usage->hat_min) * 8 / (usage->hat_max - usage->hat_min + 1) + 1;
610 if (hat_dir < 0 || hat_dir > 8) hat_dir = 0;
611 input_event(input, usage->type, usage->code , hid_hat_to_axis[hat_dir].x);
612 input_event(input, usage->type, usage->code + 1, hid_hat_to_axis[hat_dir].y);
613 return;
614 }
615
616 if (usage->hid == (HID_UP_DIGITIZER | 0x003c)) { /* Invert */
617 *quirks = value ? (*quirks | HID_QUIRK_INVERT) : (*quirks & ~HID_QUIRK_INVERT);
618 return;
619 }
620
621 if (usage->hid == (HID_UP_DIGITIZER | 0x0032)) { /* InRange */
622 if (value) {
623 input_event(input, usage->type, (*quirks & HID_QUIRK_INVERT) ? BTN_TOOL_RUBBER : usage->code, 1);
624 return;
625 }
626 input_event(input, usage->type, usage->code, 0);
627 input_event(input, usage->type, BTN_TOOL_RUBBER, 0);
628 return;
629 }
630
631 if (usage->hid == (HID_UP_DIGITIZER | 0x0030) && (*quirks & HID_QUIRK_NOTOUCH)) { /* Pressure */
632 int a = field->logical_minimum;
633 int b = field->logical_maximum;
634 input_event(input, EV_KEY, BTN_TOUCH, value > a + ((b - a) >> 3));
635 }
636
637 if (usage->hid == (HID_UP_PID | 0x83UL)) { /* Simultaneous Effects Max */
Jiri Kosina58037eb2007-05-30 15:07:13 +0200638 dbg_hid("Maximum Effects - %d\n",value);
Jiri Kosinadde58452006-12-08 18:40:44 +0100639 return;
640 }
641
642 if (usage->hid == (HID_UP_PID | 0x7fUL)) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200643 dbg_hid("PID Pool Report\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100644 return;
645 }
646
647 if ((usage->type == EV_KEY) && (usage->code == 0)) /* Key 0 is "unassigned", not KEY_UNKNOWN */
648 return;
649
Jiri Kosina2c1d8ae2007-07-04 16:45:59 +0200650 if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
651 (usage->code == ABS_VOLUME)) {
652 int count = abs(value);
653 int direction = value > 0 ? KEY_VOLUMEUP : KEY_VOLUMEDOWN;
654 int i;
655
656 for (i = 0; i < count; i++) {
657 input_event(input, EV_KEY, direction, 1);
658 input_sync(input);
659 input_event(input, EV_KEY, direction, 0);
660 input_sync(input);
661 }
662 return;
663 }
664
Jiri Kosinac01d50d2007-08-20 12:13:34 +0200665 /* report the usage code as scancode if the key status has changed */
666 if (usage->type == EV_KEY && !!test_bit(usage->code, input->key) != value)
667 input_event(input, EV_MSC, MSC_SCAN, usage->hid);
Jiri Kosina1fe8736d2007-08-09 13:24:11 +0200668
Jiri Kosinadde58452006-12-08 18:40:44 +0100669 input_event(input, usage->type, usage->code, value);
670
671 if ((field->flags & HID_MAIN_ITEM_RELATIVE) && (usage->type == EV_KEY))
672 input_event(input, usage->type, usage->code, 0);
673}
674
675void hidinput_report_event(struct hid_device *hid, struct hid_report *report)
676{
677 struct hid_input *hidinput;
678
679 list_for_each_entry(hidinput, &hid->inputs, list)
680 input_sync(hidinput->input);
681}
Jiri Kosina229695e2006-12-08 18:40:53 +0100682EXPORT_SYMBOL_GPL(hidinput_report_event);
Jiri Kosinadde58452006-12-08 18:40:44 +0100683
Jiri Kosina229695e2006-12-08 18:40:53 +0100684int hidinput_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field)
Jiri Kosinadde58452006-12-08 18:40:44 +0100685{
686 struct hid_report *report;
687 int i, j;
688
689 list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
690 for (i = 0; i < report->maxfield; i++) {
691 *field = report->field[i];
692 for (j = 0; j < (*field)->maxusage; j++)
693 if ((*field)->usage[j].type == type && (*field)->usage[j].code == code)
694 return j;
695 }
696 }
697 return -1;
698}
Jiri Kosina229695e2006-12-08 18:40:53 +0100699EXPORT_SYMBOL_GPL(hidinput_find_field);
Jiri Kosinadde58452006-12-08 18:40:44 +0100700
Jiri Kosina7c379142007-01-24 11:54:19 +0100701static int hidinput_open(struct input_dev *dev)
702{
Dmitry Torokhove0712982007-05-09 10:17:31 +0200703 struct hid_device *hid = input_get_drvdata(dev);
704
Jiri Slabyc500c972008-05-16 11:49:16 +0200705 return hid->ll_driver->open(hid);
Jiri Kosina7c379142007-01-24 11:54:19 +0100706}
707
708static void hidinput_close(struct input_dev *dev)
709{
Dmitry Torokhove0712982007-05-09 10:17:31 +0200710 struct hid_device *hid = input_get_drvdata(dev);
711
Jiri Slabyc500c972008-05-16 11:49:16 +0200712 hid->ll_driver->close(hid);
Jiri Kosina7c379142007-01-24 11:54:19 +0100713}
714
Jiri Kosinadde58452006-12-08 18:40:44 +0100715/*
716 * Register the input device; print a message.
717 * Configure the input layer interface
718 * Read all reports and initialize the absolute field values.
719 */
720
721int hidinput_connect(struct hid_device *hid)
722{
Jiri Kosinadde58452006-12-08 18:40:44 +0100723 struct hid_report *report;
724 struct hid_input *hidinput = NULL;
725 struct input_dev *input_dev;
726 int i, j, k;
Anssi Hannula5556fea2007-01-11 16:51:17 +0200727 int max_report_type = HID_OUTPUT_REPORT;
Jiri Kosinadde58452006-12-08 18:40:44 +0100728
Stelian Pop0ce91cf2007-09-11 23:34:55 +0200729 if (hid->quirks & HID_QUIRK_IGNORE_HIDINPUT)
730 return -1;
731
Jiri Kosinadde58452006-12-08 18:40:44 +0100732 INIT_LIST_HEAD(&hid->inputs);
733
734 for (i = 0; i < hid->maxcollection; i++)
735 if (hid->collection[i].type == HID_COLLECTION_APPLICATION ||
736 hid->collection[i].type == HID_COLLECTION_PHYSICAL)
737 if (IS_INPUT_APPLICATION(hid->collection[i].usage))
738 break;
739
Jiri Kosinadefd2082007-05-19 16:28:04 +0200740 if (i == hid->maxcollection && (hid->quirks & HID_QUIRK_HIDINPUT) == 0)
Jiri Kosinadde58452006-12-08 18:40:44 +0100741 return -1;
742
Anssi Hannula5556fea2007-01-11 16:51:17 +0200743 if (hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORTS)
744 max_report_type = HID_INPUT_REPORT;
745
746 for (k = HID_INPUT_REPORT; k <= max_report_type; k++)
Jiri Kosinadde58452006-12-08 18:40:44 +0100747 list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
748
749 if (!report->maxfield)
750 continue;
751
752 if (!hidinput) {
753 hidinput = kzalloc(sizeof(*hidinput), GFP_KERNEL);
754 input_dev = input_allocate_device();
755 if (!hidinput || !input_dev) {
756 kfree(hidinput);
757 input_free_device(input_dev);
Jiri Kosina58037eb2007-05-30 15:07:13 +0200758 err_hid("Out of memory during hid input probe");
Dirk Hohndel368d2902007-10-30 13:02:44 +0100759 goto out_unwind;
Jiri Kosinadde58452006-12-08 18:40:44 +0100760 }
761
Dmitry Torokhove0712982007-05-09 10:17:31 +0200762 input_set_drvdata(input_dev, hid);
Jiri Slabyc500c972008-05-16 11:49:16 +0200763 input_dev->event =
764 hid->ll_driver->hidinput_input_event;
Jiri Kosina7c379142007-01-24 11:54:19 +0100765 input_dev->open = hidinput_open;
766 input_dev->close = hidinput_close;
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200767 input_dev->setkeycode = hidinput_setkeycode;
768 input_dev->getkeycode = hidinput_getkeycode;
Jiri Kosinadde58452006-12-08 18:40:44 +0100769
770 input_dev->name = hid->name;
771 input_dev->phys = hid->phys;
772 input_dev->uniq = hid->uniq;
Jiri Kosina229695e2006-12-08 18:40:53 +0100773 input_dev->id.bustype = hid->bus;
774 input_dev->id.vendor = hid->vendor;
775 input_dev->id.product = hid->product;
776 input_dev->id.version = hid->version;
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200777 input_dev->dev.parent = hid->dev.parent;
Jiri Kosinadde58452006-12-08 18:40:44 +0100778 hidinput->input = input_dev;
779 list_add_tail(&hidinput->list, &hid->inputs);
780 }
781
782 for (i = 0; i < report->maxfield; i++)
783 for (j = 0; j < report->field[i]->maxusage; j++)
784 hidinput_configure_usage(hidinput, report->field[i],
785 report->field[i]->usage + j);
786
787 if (hid->quirks & HID_QUIRK_MULTI_INPUT) {
788 /* This will leave hidinput NULL, so that it
789 * allocates another one if we have more inputs on
790 * the same interface. Some devices (e.g. Happ's
791 * UGCI) cram a lot of unrelated inputs into the
792 * same interface. */
793 hidinput->report = report;
Dirk Hohndel368d2902007-10-30 13:02:44 +0100794 if (input_register_device(hidinput->input))
795 goto out_cleanup;
Jiri Kosinadde58452006-12-08 18:40:44 +0100796 hidinput = NULL;
797 }
798 }
799
Dirk Hohndel368d2902007-10-30 13:02:44 +0100800 if (hidinput && input_register_device(hidinput->input))
801 goto out_cleanup;
Jiri Kosinadde58452006-12-08 18:40:44 +0100802
803 return 0;
Dirk Hohndel368d2902007-10-30 13:02:44 +0100804
805out_cleanup:
806 input_free_device(hidinput->input);
807 kfree(hidinput);
808out_unwind:
809 /* unwind the ones we already registered */
810 hidinput_disconnect(hid);
811
812 return -1;
Jiri Kosinadde58452006-12-08 18:40:44 +0100813}
Jiri Kosina229695e2006-12-08 18:40:53 +0100814EXPORT_SYMBOL_GPL(hidinput_connect);
Jiri Kosinadde58452006-12-08 18:40:44 +0100815
816void hidinput_disconnect(struct hid_device *hid)
817{
818 struct hid_input *hidinput, *next;
819
820 list_for_each_entry_safe(hidinput, next, &hid->inputs, list) {
821 list_del(&hidinput->list);
822 input_unregister_device(hidinput->input);
823 kfree(hidinput);
824 }
825}
Jiri Kosina229695e2006-12-08 18:40:53 +0100826EXPORT_SYMBOL_GPL(hidinput_disconnect);
827