blob: b12c07e64fbd90c02398cfb66a63e5124794455e [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 Kosina7d39e842010-02-02 20:46:34 +01003 * Copyright (c) 2006-2010 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
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -070071static bool match_scancode(struct hid_usage *usage,
72 unsigned int cur_idx, unsigned int scancode)
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +020073{
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -070074 return (usage->hid & (HID_USAGE_PAGE | HID_USAGE)) == scancode;
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +020075}
76
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -070077static bool match_keycode(struct hid_usage *usage,
78 unsigned int cur_idx, unsigned int keycode)
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +020079{
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -070080 return usage->code == keycode;
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +020081}
82
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -070083static bool match_index(struct hid_usage *usage,
84 unsigned int cur_idx, unsigned int idx)
85{
86 return cur_idx == idx;
87}
88
89typedef bool (*hid_usage_cmp_t)(struct hid_usage *usage,
90 unsigned int cur_idx, unsigned int val);
91
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +020092static struct hid_usage *hidinput_find_key(struct hid_device *hid,
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -070093 hid_usage_cmp_t match,
94 unsigned int value,
95 unsigned int *usage_idx)
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +020096{
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -070097 unsigned int i, j, k, cur_idx = 0;
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +020098 struct hid_report *report;
99 struct hid_usage *usage;
100
101 for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
102 list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
103 for (i = 0; i < report->maxfield; i++) {
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700104 for (j = 0; j < report->field[i]->maxusage; j++) {
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200105 usage = report->field[i]->usage + j;
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700106 if (usage->type == EV_KEY) {
107 if (match(usage, cur_idx, value)) {
108 if (usage_idx)
109 *usage_idx = cur_idx;
110 return usage;
111 }
112 cur_idx++;
113 }
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200114 }
115 }
116 }
117 }
118 return NULL;
119}
120
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700121static struct hid_usage *hidinput_locate_usage(struct hid_device *hid,
122 const struct input_keymap_entry *ke,
123 unsigned int *index)
124{
125 struct hid_usage *usage;
126 unsigned int scancode;
127
128 if (ke->flags & INPUT_KEYMAP_BY_INDEX)
129 usage = hidinput_find_key(hid, match_index, ke->index, index);
130 else if (input_scancode_to_scalar(ke, &scancode) == 0)
131 usage = hidinput_find_key(hid, match_scancode, scancode, index);
132 else
133 usage = NULL;
134
135 return usage;
136}
137
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800138static int hidinput_getkeycode(struct input_dev *dev,
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700139 struct input_keymap_entry *ke)
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200140{
Dmitry Torokhovf202df62007-10-31 12:33:26 +0100141 struct hid_device *hid = input_get_drvdata(dev);
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200142 struct hid_usage *usage;
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700143 unsigned int scancode, index;
Jiri Kosina77b29662007-08-09 14:04:56 +0200144
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700145 usage = hidinput_locate_usage(hid, ke, &index);
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200146 if (usage) {
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700147 ke->keycode = usage->code;
148 ke->index = index;
149 scancode = usage->hid & (HID_USAGE_PAGE | HID_USAGE);
150 ke->len = sizeof(scancode);
151 memcpy(ke->scancode, &scancode, sizeof(scancode));
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200152 return 0;
153 }
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700154
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200155 return -EINVAL;
156}
157
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800158static int hidinput_setkeycode(struct input_dev *dev,
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700159 const struct input_keymap_entry *ke,
160 unsigned int *old_keycode)
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200161{
Dmitry Torokhovf202df62007-10-31 12:33:26 +0100162 struct hid_device *hid = input_get_drvdata(dev);
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200163 struct hid_usage *usage;
Jiri Kosina77b29662007-08-09 14:04:56 +0200164
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700165 usage = hidinput_locate_usage(hid, ke, NULL);
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200166 if (usage) {
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700167 *old_keycode = usage->code;
168 usage->code = ke->keycode;
Jiri Kosina77b29662007-08-09 14:04:56 +0200169
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700170 clear_bit(*old_keycode, dev->keybit);
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200171 set_bit(usage->code, dev->keybit);
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700172 dbg_hid(KERN_DEBUG "Assigned keycode %d to HID usage code %x\n",
173 usage->code, usage->hid);
174
175 /*
176 * Set the keybit for the old keycode if the old keycode is used
177 * by another key
178 */
179 if (hidinput_find_key(hid, match_keycode, *old_keycode, NULL))
180 set_bit(*old_keycode, dev->keybit);
Jiri Kosina77b29662007-08-09 14:04:56 +0200181
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200182 return 0;
183 }
Jiri Kosina77b29662007-08-09 14:04:56 +0200184
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200185 return -EINVAL;
186}
187
188
Jiri Kosinadde58452006-12-08 18:40:44 +0100189static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_field *field,
190 struct hid_usage *usage)
191{
192 struct input_dev *input = hidinput->input;
Dmitry Torokhove0712982007-05-09 10:17:31 +0200193 struct hid_device *device = input_get_drvdata(input);
Jiri Slaby3715ade2008-07-31 11:09:37 +0200194 int max = 0, code;
Jiri Kosinadde58452006-12-08 18:40:44 +0100195 unsigned long *bit = NULL;
196
197 field->hidinput = hidinput;
198
Jiri Kosinadde58452006-12-08 18:40:44 +0100199 if (field->flags & HID_MAIN_ITEM_CONSTANT)
200 goto ignore;
201
Jiri Kosina82eb1212007-08-11 23:39:42 +0200202 /* only LED usages are supported in output fields */
203 if (field->report_type == HID_OUTPUT_REPORT &&
204 (usage->hid & HID_USAGE_PAGE) != HID_UP_LED) {
Jiri Kosina82eb1212007-08-11 23:39:42 +0200205 goto ignore;
206 }
207
Jiri Slabyc500c972008-05-16 11:49:16 +0200208 if (device->driver->input_mapping) {
209 int ret = device->driver->input_mapping(device, hidinput, field,
210 usage, &bit, &max);
211 if (ret > 0)
212 goto mapped;
213 if (ret < 0)
214 goto ignore;
215 }
216
Jiri Kosinadde58452006-12-08 18:40:44 +0100217 switch (usage->hid & HID_USAGE_PAGE) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200218 case HID_UP_UNDEFINED:
219 goto ignore;
Jiri Kosinadde58452006-12-08 18:40:44 +0100220
Jiri Slaby880d29f2008-06-18 23:55:41 +0200221 case HID_UP_KEYBOARD:
222 set_bit(EV_REP, input->evbit);
Jiri Kosinadde58452006-12-08 18:40:44 +0100223
Jiri Slaby880d29f2008-06-18 23:55:41 +0200224 if ((usage->hid & HID_USAGE) < 256) {
225 if (!hid_keyboard[usage->hid & HID_USAGE]) goto ignore;
226 map_key_clear(hid_keyboard[usage->hid & HID_USAGE]);
227 } else
228 map_key(KEY_UNKNOWN);
Jiri Kosinadde58452006-12-08 18:40:44 +0100229
Jiri Slaby880d29f2008-06-18 23:55:41 +0200230 break;
Jiri Kosinadde58452006-12-08 18:40:44 +0100231
Jiri Slaby880d29f2008-06-18 23:55:41 +0200232 case HID_UP_BUTTON:
Jiri Kosina7f978b92010-02-17 09:36:35 +0100233 code = ((usage->hid - 1) & HID_USAGE);
Jiri Kosinadde58452006-12-08 18:40:44 +0100234
Jiri Slaby880d29f2008-06-18 23:55:41 +0200235 switch (field->application) {
236 case HID_GD_MOUSE:
237 case HID_GD_POINTER: code += 0x110; break;
Jiri Kosinacf2f765f2010-01-04 12:20:56 +0100238 case HID_GD_JOYSTICK:
Daniel Mackfc99f222010-07-12 19:28:26 +0200239 if (code <= 0xf)
240 code += BTN_JOYSTICK;
241 else
242 code += BTN_TRIGGER_HAPPY;
243 break;
Jiri Slaby880d29f2008-06-18 23:55:41 +0200244 case HID_GD_GAMEPAD: code += 0x130; break;
Jiri Kosinadde58452006-12-08 18:40:44 +0100245 default:
Jiri Slaby880d29f2008-06-18 23:55:41 +0200246 switch (field->physical) {
247 case HID_GD_MOUSE:
248 case HID_GD_POINTER: code += 0x110; break;
249 case HID_GD_JOYSTICK: code += 0x120; break;
250 case HID_GD_GAMEPAD: code += 0x130; break;
251 default: code += 0x100;
Jiri Kosinadde58452006-12-08 18:40:44 +0100252 }
Jiri Slaby880d29f2008-06-18 23:55:41 +0200253 }
254
255 map_key(code);
256 break;
257
258 case HID_UP_SIMULATION:
259 switch (usage->hid & 0xffff) {
260 case 0xba: map_abs(ABS_RUDDER); break;
261 case 0xbb: map_abs(ABS_THROTTLE); break;
262 case 0xc4: map_abs(ABS_GAS); break;
263 case 0xc5: map_abs(ABS_BRAKE); break;
264 case 0xc8: map_abs(ABS_WHEEL); break;
265 default: goto ignore;
266 }
267 break;
268
269 case HID_UP_GENDESK:
270 if ((usage->hid & 0xf0) == 0x80) { /* SystemControl */
271 switch (usage->hid & 0xf) {
272 case 0x1: map_key_clear(KEY_POWER); break;
273 case 0x2: map_key_clear(KEY_SLEEP); break;
274 case 0x3: map_key_clear(KEY_WAKEUP); break;
275 default: goto unknown;
Jiri Kosinadde58452006-12-08 18:40:44 +0100276 }
Jiri Kosinadde58452006-12-08 18:40:44 +0100277 break;
Jiri Slaby880d29f2008-06-18 23:55:41 +0200278 }
279
280 if ((usage->hid & 0xf0) == 0x90) { /* D-pad */
281 switch (usage->hid) {
282 case HID_GD_UP: usage->hat_dir = 1; break;
283 case HID_GD_DOWN: usage->hat_dir = 5; break;
284 case HID_GD_RIGHT: usage->hat_dir = 3; break;
285 case HID_GD_LEFT: usage->hat_dir = 7; break;
286 default: goto unknown;
287 }
288 if (field->dpad) {
289 map_abs(field->dpad);
290 goto ignore;
291 }
292 map_abs(ABS_HAT0X);
293 break;
294 }
295
296 switch (usage->hid) {
297 /* These usage IDs map directly to the usage codes. */
298 case HID_GD_X: case HID_GD_Y: case HID_GD_Z:
299 case HID_GD_RX: case HID_GD_RY: case HID_GD_RZ:
300 case HID_GD_SLIDER: case HID_GD_DIAL: case HID_GD_WHEEL:
301 if (field->flags & HID_MAIN_ITEM_RELATIVE)
302 map_rel(usage->hid & 0xf);
303 else
304 map_abs(usage->hid & 0xf);
305 break;
306
307 case HID_GD_HATSWITCH:
308 usage->hat_min = field->logical_minimum;
309 usage->hat_max = field->logical_maximum;
310 map_abs(ABS_HAT0X);
311 break;
312
313 case HID_GD_START: map_key_clear(BTN_START); break;
314 case HID_GD_SELECT: map_key_clear(BTN_SELECT); break;
315
316 default: goto unknown;
317 }
318
319 break;
320
321 case HID_UP_LED:
322 switch (usage->hid & 0xffff) { /* HID-Value: */
323 case 0x01: map_led (LED_NUML); break; /* "Num Lock" */
324 case 0x02: map_led (LED_CAPSL); break; /* "Caps Lock" */
325 case 0x03: map_led (LED_SCROLLL); break; /* "Scroll Lock" */
326 case 0x04: map_led (LED_COMPOSE); break; /* "Compose" */
327 case 0x05: map_led (LED_KANA); break; /* "Kana" */
328 case 0x27: map_led (LED_SLEEP); break; /* "Stand-By" */
329 case 0x4c: map_led (LED_SUSPEND); break; /* "System Suspend" */
330 case 0x09: map_led (LED_MUTE); break; /* "Mute" */
331 case 0x4b: map_led (LED_MISC); break; /* "Generic Indicator" */
332 case 0x19: map_led (LED_MAIL); break; /* "Message Waiting" */
333 case 0x4d: map_led (LED_CHARGING); break; /* "External Power Connected" */
334
335 default: goto ignore;
336 }
337 break;
338
339 case HID_UP_DIGITIZER:
340 switch (usage->hid & 0xff) {
Forest Bond8c8b01c2010-07-13 23:50:57 +0200341 case 0x00: /* Undefined */
342 goto ignore;
343
Jiri Slaby880d29f2008-06-18 23:55:41 +0200344 case 0x30: /* TipPressure */
345 if (!test_bit(BTN_TOUCH, input->keybit)) {
346 device->quirks |= HID_QUIRK_NOTOUCH;
347 set_bit(EV_KEY, input->evbit);
348 set_bit(BTN_TOUCH, input->keybit);
349 }
350 map_abs_clear(ABS_PRESSURE);
351 break;
352
353 case 0x32: /* InRange */
354 switch (field->physical & 0xff) {
355 case 0x21: map_key(BTN_TOOL_MOUSE); break;
356 case 0x22: map_key(BTN_TOOL_FINGER); break;
357 default: map_key(BTN_TOOL_PEN); break;
358 }
359 break;
360
361 case 0x3c: /* Invert */
362 map_key_clear(BTN_TOOL_RUBBER);
363 break;
364
365 case 0x33: /* Touch */
366 case 0x42: /* TipSwitch */
367 case 0x43: /* TipSwitch2 */
368 device->quirks &= ~HID_QUIRK_NOTOUCH;
369 map_key_clear(BTN_TOUCH);
370 break;
371
372 case 0x44: /* BarrelSwitch */
373 map_key_clear(BTN_STYLUS);
374 break;
375
376 default: goto unknown;
377 }
378 break;
379
380 case HID_UP_CONSUMER: /* USB HUT v1.1, pages 56-62 */
381 switch (usage->hid & HID_USAGE) {
382 case 0x000: goto ignore;
383 case 0x034: map_key_clear(KEY_SLEEP); break;
384 case 0x036: map_key_clear(BTN_MISC); break;
385
386 case 0x040: map_key_clear(KEY_MENU); break;
387 case 0x045: map_key_clear(KEY_RADIO); break;
388
389 case 0x083: map_key_clear(KEY_LAST); break;
390 case 0x088: map_key_clear(KEY_PC); break;
391 case 0x089: map_key_clear(KEY_TV); break;
392 case 0x08a: map_key_clear(KEY_WWW); break;
393 case 0x08b: map_key_clear(KEY_DVD); break;
394 case 0x08c: map_key_clear(KEY_PHONE); break;
395 case 0x08d: map_key_clear(KEY_PROGRAM); break;
396 case 0x08e: map_key_clear(KEY_VIDEOPHONE); break;
397 case 0x08f: map_key_clear(KEY_GAMES); break;
398 case 0x090: map_key_clear(KEY_MEMO); break;
399 case 0x091: map_key_clear(KEY_CD); break;
400 case 0x092: map_key_clear(KEY_VCR); break;
401 case 0x093: map_key_clear(KEY_TUNER); break;
402 case 0x094: map_key_clear(KEY_EXIT); break;
403 case 0x095: map_key_clear(KEY_HELP); break;
404 case 0x096: map_key_clear(KEY_TAPE); break;
405 case 0x097: map_key_clear(KEY_TV2); break;
406 case 0x098: map_key_clear(KEY_SAT); break;
407 case 0x09a: map_key_clear(KEY_PVR); break;
408
409 case 0x09c: map_key_clear(KEY_CHANNELUP); break;
410 case 0x09d: map_key_clear(KEY_CHANNELDOWN); break;
411 case 0x0a0: map_key_clear(KEY_VCR2); break;
412
413 case 0x0b0: map_key_clear(KEY_PLAY); break;
414 case 0x0b1: map_key_clear(KEY_PAUSE); break;
415 case 0x0b2: map_key_clear(KEY_RECORD); break;
416 case 0x0b3: map_key_clear(KEY_FASTFORWARD); break;
417 case 0x0b4: map_key_clear(KEY_REWIND); break;
418 case 0x0b5: map_key_clear(KEY_NEXTSONG); break;
419 case 0x0b6: map_key_clear(KEY_PREVIOUSSONG); break;
420 case 0x0b7: map_key_clear(KEY_STOPCD); break;
421 case 0x0b8: map_key_clear(KEY_EJECTCD); break;
422 case 0x0bc: map_key_clear(KEY_MEDIA_REPEAT); break;
423
424 case 0x0cd: map_key_clear(KEY_PLAYPAUSE); break;
425 case 0x0e0: map_abs_clear(ABS_VOLUME); break;
426 case 0x0e2: map_key_clear(KEY_MUTE); break;
427 case 0x0e5: map_key_clear(KEY_BASSBOOST); break;
428 case 0x0e9: map_key_clear(KEY_VOLUMEUP); break;
429 case 0x0ea: map_key_clear(KEY_VOLUMEDOWN); break;
430
431 case 0x182: map_key_clear(KEY_BOOKMARKS); break;
432 case 0x183: map_key_clear(KEY_CONFIG); break;
433 case 0x184: map_key_clear(KEY_WORDPROCESSOR); break;
434 case 0x185: map_key_clear(KEY_EDITOR); break;
435 case 0x186: map_key_clear(KEY_SPREADSHEET); break;
436 case 0x187: map_key_clear(KEY_GRAPHICSEDITOR); break;
437 case 0x188: map_key_clear(KEY_PRESENTATION); break;
438 case 0x189: map_key_clear(KEY_DATABASE); break;
439 case 0x18a: map_key_clear(KEY_MAIL); break;
440 case 0x18b: map_key_clear(KEY_NEWS); break;
441 case 0x18c: map_key_clear(KEY_VOICEMAIL); break;
442 case 0x18d: map_key_clear(KEY_ADDRESSBOOK); break;
443 case 0x18e: map_key_clear(KEY_CALENDAR); break;
444 case 0x191: map_key_clear(KEY_FINANCE); break;
445 case 0x192: map_key_clear(KEY_CALC); break;
446 case 0x194: map_key_clear(KEY_FILE); break;
447 case 0x196: map_key_clear(KEY_WWW); break;
Leo P White06905352010-02-08 13:02:05 +0000448 case 0x199: map_key_clear(KEY_CHAT); break;
Jiri Slaby880d29f2008-06-18 23:55:41 +0200449 case 0x19c: map_key_clear(KEY_LOGOFF); break;
450 case 0x19e: map_key_clear(KEY_COFFEE); break;
451 case 0x1a6: map_key_clear(KEY_HELP); break;
452 case 0x1a7: map_key_clear(KEY_DOCUMENTS); break;
453 case 0x1ab: map_key_clear(KEY_SPELLCHECK); break;
454 case 0x1b6: map_key_clear(KEY_MEDIA); break;
455 case 0x1b7: map_key_clear(KEY_SOUND); break;
456 case 0x1bc: map_key_clear(KEY_MESSENGER); break;
457 case 0x1bd: map_key_clear(KEY_INFO); break;
458 case 0x201: map_key_clear(KEY_NEW); break;
459 case 0x202: map_key_clear(KEY_OPEN); break;
460 case 0x203: map_key_clear(KEY_CLOSE); break;
461 case 0x204: map_key_clear(KEY_EXIT); break;
462 case 0x207: map_key_clear(KEY_SAVE); break;
463 case 0x208: map_key_clear(KEY_PRINT); break;
464 case 0x209: map_key_clear(KEY_PROPS); break;
465 case 0x21a: map_key_clear(KEY_UNDO); break;
466 case 0x21b: map_key_clear(KEY_COPY); break;
467 case 0x21c: map_key_clear(KEY_CUT); break;
468 case 0x21d: map_key_clear(KEY_PASTE); break;
469 case 0x21f: map_key_clear(KEY_FIND); break;
470 case 0x221: map_key_clear(KEY_SEARCH); break;
471 case 0x222: map_key_clear(KEY_GOTO); break;
472 case 0x223: map_key_clear(KEY_HOMEPAGE); break;
473 case 0x224: map_key_clear(KEY_BACK); break;
474 case 0x225: map_key_clear(KEY_FORWARD); break;
475 case 0x226: map_key_clear(KEY_STOP); break;
476 case 0x227: map_key_clear(KEY_REFRESH); break;
477 case 0x22a: map_key_clear(KEY_BOOKMARKS); break;
478 case 0x22d: map_key_clear(KEY_ZOOMIN); break;
479 case 0x22e: map_key_clear(KEY_ZOOMOUT); break;
480 case 0x22f: map_key_clear(KEY_ZOOMRESET); break;
481 case 0x233: map_key_clear(KEY_SCROLLUP); break;
482 case 0x234: map_key_clear(KEY_SCROLLDOWN); break;
483 case 0x238: map_rel(REL_HWHEEL); break;
484 case 0x25f: map_key_clear(KEY_CANCEL); break;
485 case 0x279: map_key_clear(KEY_REDO); break;
486
487 case 0x289: map_key_clear(KEY_REPLY); break;
488 case 0x28b: map_key_clear(KEY_FORWARDMAIL); break;
489 case 0x28c: map_key_clear(KEY_SEND); break;
490
491 default: goto ignore;
492 }
493 break;
494
495 case HID_UP_HPVENDOR: /* Reported on a Dutch layout HP5308 */
496 set_bit(EV_REP, input->evbit);
497 switch (usage->hid & HID_USAGE) {
498 case 0x021: map_key_clear(KEY_PRINT); break;
499 case 0x070: map_key_clear(KEY_HP); break;
500 case 0x071: map_key_clear(KEY_CAMERA); break;
501 case 0x072: map_key_clear(KEY_SOUND); break;
502 case 0x073: map_key_clear(KEY_QUESTION); break;
503 case 0x080: map_key_clear(KEY_EMAIL); break;
504 case 0x081: map_key_clear(KEY_CHAT); break;
505 case 0x082: map_key_clear(KEY_SEARCH); break;
506 case 0x083: map_key_clear(KEY_CONNECT); break;
507 case 0x084: map_key_clear(KEY_FINANCE); break;
508 case 0x085: map_key_clear(KEY_SPORT); break;
509 case 0x086: map_key_clear(KEY_SHOP); break;
510 default: goto ignore;
511 }
512 break;
513
514 case HID_UP_MSVENDOR:
515 goto ignore;
516
517 case HID_UP_CUSTOM: /* Reported on Logitech and Apple USB keyboards */
518 set_bit(EV_REP, input->evbit);
519 goto ignore;
520
521 case HID_UP_LOGIVENDOR:
522 goto ignore;
Daniel Mackfc99f222010-07-12 19:28:26 +0200523
Jiri Slaby880d29f2008-06-18 23:55:41 +0200524 case HID_UP_PID:
525 switch (usage->hid & HID_USAGE) {
526 case 0xa4: map_key_clear(BTN_DEAD); break;
527 default: goto ignore;
528 }
529 break;
530
531 default:
532 unknown:
533 if (field->report_size == 1) {
534 if (field->report->type == HID_OUTPUT_REPORT) {
535 map_led(LED_MISC);
536 break;
537 }
538 map_key(BTN_MISC);
539 break;
540 }
541 if (field->flags & HID_MAIN_ITEM_RELATIVE) {
542 map_rel(REL_MISC);
543 break;
544 }
545 map_abs(ABS_MISC);
546 break;
Jiri Kosinadde58452006-12-08 18:40:44 +0100547 }
548
Jiri Kosina10bd0652007-11-22 15:18:18 +0100549mapped:
Jiri Slabyc500c972008-05-16 11:49:16 +0200550 if (device->driver->input_mapped && device->driver->input_mapped(device,
551 hidinput, field, usage, &bit, &max) < 0)
552 goto ignore;
553
Jiri Kosinadde58452006-12-08 18:40:44 +0100554 set_bit(usage->type, input->evbit);
555
556 while (usage->code <= max && test_and_set_bit(usage->code, bit))
557 usage->code = find_next_zero_bit(bit, max + 1, usage->code);
558
559 if (usage->code > max)
560 goto ignore;
561
562
563 if (usage->type == EV_ABS) {
564
565 int a = field->logical_minimum;
566 int b = field->logical_maximum;
567
568 if ((device->quirks & HID_QUIRK_BADPAD) && (usage->code == ABS_X || usage->code == ABS_Y)) {
569 a = field->logical_minimum = 0;
570 b = field->logical_maximum = 255;
571 }
572
573 if (field->application == HID_GD_GAMEPAD || field->application == HID_GD_JOYSTICK)
574 input_set_abs_params(input, usage->code, a, b, (b - a) >> 8, (b - a) >> 4);
575 else input_set_abs_params(input, usage->code, a, b, 0, 0);
576
Henrik Rydberg6967b4d2010-06-23 09:31:37 -0700577 /* use a larger default input buffer for MT devices */
578 if (usage->code == ABS_MT_POSITION_X && input->hint_events_per_packet == 0)
579 input_set_events_per_packet(input, 60);
Jiri Kosinadde58452006-12-08 18:40:44 +0100580 }
581
582 if (usage->type == EV_ABS &&
583 (usage->hat_min < usage->hat_max || usage->hat_dir)) {
584 int i;
585 for (i = usage->code; i < usage->code + 2 && i <= max; i++) {
586 input_set_abs_params(input, i, -1, 1, 0, 0);
587 set_bit(i, input->absbit);
588 }
589 if (usage->hat_dir && !field->dpad)
590 field->dpad = usage->code;
591 }
592
Jiri Kosina2c1d8ae2007-07-04 16:45:59 +0200593 /* for those devices which produce Consumer volume usage as relative,
594 * we emulate pressing volumeup/volumedown appropriate number of times
595 * in hidinput_hid_event()
596 */
597 if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
598 (usage->code == ABS_VOLUME)) {
599 set_bit(KEY_VOLUMEUP, input->keybit);
600 set_bit(KEY_VOLUMEDOWN, input->keybit);
601 }
602
Jiri Kosinac01d50d2007-08-20 12:13:34 +0200603 if (usage->type == EV_KEY) {
604 set_bit(EV_MSC, input->evbit);
605 set_bit(MSC_SCAN, input->mscbit);
606 }
607
Jiri Kosinadde58452006-12-08 18:40:44 +0100608ignore:
Jiri Kosinadde58452006-12-08 18:40:44 +0100609 return;
Jiri Kosinaa635f9d2009-06-12 15:20:55 +0200610
Jiri Kosinadde58452006-12-08 18:40:44 +0100611}
612
613void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
614{
615 struct input_dev *input;
Al Virob4482a42007-10-14 19:35:40 +0100616 unsigned *quirks = &hid->quirks;
Jiri Kosinadde58452006-12-08 18:40:44 +0100617
618 if (!field->hidinput)
619 return;
620
621 input = field->hidinput->input;
622
623 if (!usage->type)
624 return;
625
Jiri Kosinadde58452006-12-08 18:40:44 +0100626 if (usage->hat_min < usage->hat_max || usage->hat_dir) {
627 int hat_dir = usage->hat_dir;
628 if (!hat_dir)
629 hat_dir = (value - usage->hat_min) * 8 / (usage->hat_max - usage->hat_min + 1) + 1;
630 if (hat_dir < 0 || hat_dir > 8) hat_dir = 0;
631 input_event(input, usage->type, usage->code , hid_hat_to_axis[hat_dir].x);
Daniel Mackfc99f222010-07-12 19:28:26 +0200632 input_event(input, usage->type, usage->code + 1, hid_hat_to_axis[hat_dir].y);
633 return;
634 }
Jiri Kosinadde58452006-12-08 18:40:44 +0100635
636 if (usage->hid == (HID_UP_DIGITIZER | 0x003c)) { /* Invert */
637 *quirks = value ? (*quirks | HID_QUIRK_INVERT) : (*quirks & ~HID_QUIRK_INVERT);
638 return;
639 }
640
641 if (usage->hid == (HID_UP_DIGITIZER | 0x0032)) { /* InRange */
642 if (value) {
643 input_event(input, usage->type, (*quirks & HID_QUIRK_INVERT) ? BTN_TOOL_RUBBER : usage->code, 1);
644 return;
645 }
646 input_event(input, usage->type, usage->code, 0);
647 input_event(input, usage->type, BTN_TOOL_RUBBER, 0);
648 return;
649 }
650
651 if (usage->hid == (HID_UP_DIGITIZER | 0x0030) && (*quirks & HID_QUIRK_NOTOUCH)) { /* Pressure */
652 int a = field->logical_minimum;
653 int b = field->logical_maximum;
654 input_event(input, EV_KEY, BTN_TOUCH, value > a + ((b - a) >> 3));
655 }
656
657 if (usage->hid == (HID_UP_PID | 0x83UL)) { /* Simultaneous Effects Max */
Jiri Kosina58037eb2007-05-30 15:07:13 +0200658 dbg_hid("Maximum Effects - %d\n",value);
Jiri Kosinadde58452006-12-08 18:40:44 +0100659 return;
660 }
661
662 if (usage->hid == (HID_UP_PID | 0x7fUL)) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200663 dbg_hid("PID Pool Report\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100664 return;
665 }
666
667 if ((usage->type == EV_KEY) && (usage->code == 0)) /* Key 0 is "unassigned", not KEY_UNKNOWN */
668 return;
669
Jiri Kosina2c1d8ae2007-07-04 16:45:59 +0200670 if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
671 (usage->code == ABS_VOLUME)) {
672 int count = abs(value);
673 int direction = value > 0 ? KEY_VOLUMEUP : KEY_VOLUMEDOWN;
674 int i;
675
676 for (i = 0; i < count; i++) {
677 input_event(input, EV_KEY, direction, 1);
678 input_sync(input);
679 input_event(input, EV_KEY, direction, 0);
680 input_sync(input);
681 }
682 return;
683 }
684
Jiri Kosinac01d50d2007-08-20 12:13:34 +0200685 /* report the usage code as scancode if the key status has changed */
686 if (usage->type == EV_KEY && !!test_bit(usage->code, input->key) != value)
687 input_event(input, EV_MSC, MSC_SCAN, usage->hid);
Jiri Kosina1fe8736d2007-08-09 13:24:11 +0200688
Jiri Kosinadde58452006-12-08 18:40:44 +0100689 input_event(input, usage->type, usage->code, value);
690
691 if ((field->flags & HID_MAIN_ITEM_RELATIVE) && (usage->type == EV_KEY))
692 input_event(input, usage->type, usage->code, 0);
693}
694
695void hidinput_report_event(struct hid_device *hid, struct hid_report *report)
696{
697 struct hid_input *hidinput;
698
699 list_for_each_entry(hidinput, &hid->inputs, list)
700 input_sync(hidinput->input);
701}
Jiri Kosina229695e2006-12-08 18:40:53 +0100702EXPORT_SYMBOL_GPL(hidinput_report_event);
Jiri Kosinadde58452006-12-08 18:40:44 +0100703
Jiri Kosina229695e2006-12-08 18:40:53 +0100704int hidinput_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field)
Jiri Kosinadde58452006-12-08 18:40:44 +0100705{
706 struct hid_report *report;
707 int i, j;
708
709 list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
710 for (i = 0; i < report->maxfield; i++) {
711 *field = report->field[i];
712 for (j = 0; j < (*field)->maxusage; j++)
713 if ((*field)->usage[j].type == type && (*field)->usage[j].code == code)
714 return j;
715 }
716 }
717 return -1;
718}
Jiri Kosina229695e2006-12-08 18:40:53 +0100719EXPORT_SYMBOL_GPL(hidinput_find_field);
Jiri Kosinadde58452006-12-08 18:40:44 +0100720
Jiri Kosina7c379142007-01-24 11:54:19 +0100721static int hidinput_open(struct input_dev *dev)
722{
Dmitry Torokhove0712982007-05-09 10:17:31 +0200723 struct hid_device *hid = input_get_drvdata(dev);
724
Jiri Slabyc500c972008-05-16 11:49:16 +0200725 return hid->ll_driver->open(hid);
Jiri Kosina7c379142007-01-24 11:54:19 +0100726}
727
728static void hidinput_close(struct input_dev *dev)
729{
Dmitry Torokhove0712982007-05-09 10:17:31 +0200730 struct hid_device *hid = input_get_drvdata(dev);
731
Jiri Slabyc500c972008-05-16 11:49:16 +0200732 hid->ll_driver->close(hid);
Jiri Kosina7c379142007-01-24 11:54:19 +0100733}
734
Jiri Kosinadde58452006-12-08 18:40:44 +0100735/*
736 * Register the input device; print a message.
737 * Configure the input layer interface
738 * Read all reports and initialize the absolute field values.
739 */
740
Jiri Slaby93c10132008-06-27 00:04:24 +0200741int hidinput_connect(struct hid_device *hid, unsigned int force)
Jiri Kosinadde58452006-12-08 18:40:44 +0100742{
Jiri Kosinadde58452006-12-08 18:40:44 +0100743 struct hid_report *report;
744 struct hid_input *hidinput = NULL;
745 struct input_dev *input_dev;
746 int i, j, k;
Anssi Hannula5556fea2007-01-11 16:51:17 +0200747 int max_report_type = HID_OUTPUT_REPORT;
Jiri Kosinadde58452006-12-08 18:40:44 +0100748
749 INIT_LIST_HEAD(&hid->inputs);
750
Jiri Slaby93c10132008-06-27 00:04:24 +0200751 if (!force) {
752 for (i = 0; i < hid->maxcollection; i++) {
753 struct hid_collection *col = &hid->collection[i];
754 if (col->type == HID_COLLECTION_APPLICATION ||
755 col->type == HID_COLLECTION_PHYSICAL)
756 if (IS_INPUT_APPLICATION(col->usage))
757 break;
758 }
Jiri Kosinadde58452006-12-08 18:40:44 +0100759
Jiri Slaby93c10132008-06-27 00:04:24 +0200760 if (i == hid->maxcollection)
761 return -1;
762 }
Jiri Kosinadde58452006-12-08 18:40:44 +0100763
Anssi Hannula5556fea2007-01-11 16:51:17 +0200764 if (hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORTS)
765 max_report_type = HID_INPUT_REPORT;
766
767 for (k = HID_INPUT_REPORT; k <= max_report_type; k++)
Jiri Kosinadde58452006-12-08 18:40:44 +0100768 list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
769
770 if (!report->maxfield)
771 continue;
772
773 if (!hidinput) {
774 hidinput = kzalloc(sizeof(*hidinput), GFP_KERNEL);
775 input_dev = input_allocate_device();
776 if (!hidinput || !input_dev) {
777 kfree(hidinput);
778 input_free_device(input_dev);
Jiri Kosina58037eb2007-05-30 15:07:13 +0200779 err_hid("Out of memory during hid input probe");
Dirk Hohndel368d2902007-10-30 13:02:44 +0100780 goto out_unwind;
Jiri Kosinadde58452006-12-08 18:40:44 +0100781 }
782
Dmitry Torokhove0712982007-05-09 10:17:31 +0200783 input_set_drvdata(input_dev, hid);
Jiri Slabyc500c972008-05-16 11:49:16 +0200784 input_dev->event =
785 hid->ll_driver->hidinput_input_event;
Jiri Kosina7c379142007-01-24 11:54:19 +0100786 input_dev->open = hidinput_open;
787 input_dev->close = hidinput_close;
Dmitry Torokhova0bf0ea2010-09-09 21:57:17 -0700788 input_dev->setkeycode_new = hidinput_setkeycode;
789 input_dev->getkeycode_new = hidinput_getkeycode;
Jiri Kosinadde58452006-12-08 18:40:44 +0100790
791 input_dev->name = hid->name;
792 input_dev->phys = hid->phys;
793 input_dev->uniq = hid->uniq;
Jiri Kosina229695e2006-12-08 18:40:53 +0100794 input_dev->id.bustype = hid->bus;
795 input_dev->id.vendor = hid->vendor;
796 input_dev->id.product = hid->product;
797 input_dev->id.version = hid->version;
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200798 input_dev->dev.parent = hid->dev.parent;
Jiri Kosinadde58452006-12-08 18:40:44 +0100799 hidinput->input = input_dev;
800 list_add_tail(&hidinput->list, &hid->inputs);
801 }
802
803 for (i = 0; i < report->maxfield; i++)
804 for (j = 0; j < report->field[i]->maxusage; j++)
805 hidinput_configure_usage(hidinput, report->field[i],
806 report->field[i]->usage + j);
807
808 if (hid->quirks & HID_QUIRK_MULTI_INPUT) {
809 /* This will leave hidinput NULL, so that it
810 * allocates another one if we have more inputs on
811 * the same interface. Some devices (e.g. Happ's
812 * UGCI) cram a lot of unrelated inputs into the
813 * same interface. */
814 hidinput->report = report;
Dirk Hohndel368d2902007-10-30 13:02:44 +0100815 if (input_register_device(hidinput->input))
816 goto out_cleanup;
Jiri Kosinadde58452006-12-08 18:40:44 +0100817 hidinput = NULL;
818 }
819 }
820
Dirk Hohndel368d2902007-10-30 13:02:44 +0100821 if (hidinput && input_register_device(hidinput->input))
822 goto out_cleanup;
Jiri Kosinadde58452006-12-08 18:40:44 +0100823
824 return 0;
Dirk Hohndel368d2902007-10-30 13:02:44 +0100825
826out_cleanup:
827 input_free_device(hidinput->input);
828 kfree(hidinput);
829out_unwind:
830 /* unwind the ones we already registered */
831 hidinput_disconnect(hid);
832
833 return -1;
Jiri Kosinadde58452006-12-08 18:40:44 +0100834}
Jiri Kosina229695e2006-12-08 18:40:53 +0100835EXPORT_SYMBOL_GPL(hidinput_connect);
Jiri Kosinadde58452006-12-08 18:40:44 +0100836
837void hidinput_disconnect(struct hid_device *hid)
838{
839 struct hid_input *hidinput, *next;
840
841 list_for_each_entry_safe(hidinput, next, &hid->inputs, list) {
842 list_del(&hidinput->list);
843 input_unregister_device(hidinput->input);
844 kfree(hidinput);
845 }
846}
Jiri Kosina229695e2006-12-08 18:40:53 +0100847EXPORT_SYMBOL_GPL(hidinput_disconnect);
848