blob: 76e1f64e9765e76256cbebd157a4e7b665e90870 [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 Torokhov58b93992010-03-08 22:37:10 -080071static inline int match_scancode(unsigned int code, unsigned int scancode)
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +020072{
73 if (scancode == 0)
74 return 1;
Dmitry Torokhov58b93992010-03-08 22:37:10 -080075
76 return (code & (HID_USAGE_PAGE | HID_USAGE)) == scancode;
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +020077}
78
Dmitry Torokhov58b93992010-03-08 22:37:10 -080079static inline int match_keycode(unsigned int code, unsigned int keycode)
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +020080{
81 if (keycode == 0)
82 return 1;
Dmitry Torokhov58b93992010-03-08 22:37:10 -080083
84 return code == keycode;
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +020085}
86
87static struct hid_usage *hidinput_find_key(struct hid_device *hid,
Dmitry Torokhov58b93992010-03-08 22:37:10 -080088 unsigned int scancode,
89 unsigned int keycode)
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +020090{
91 int i, j, k;
92 struct hid_report *report;
93 struct hid_usage *usage;
94
95 for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
96 list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
97 for (i = 0; i < report->maxfield; i++) {
98 for ( j = 0; j < report->field[i]->maxusage; j++) {
99 usage = report->field[i]->usage + j;
100 if (usage->type == EV_KEY &&
101 match_scancode(usage->hid, scancode) &&
102 match_keycode(usage->code, keycode))
103 return usage;
104 }
105 }
106 }
107 }
108 return NULL;
109}
110
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800111static int hidinput_getkeycode(struct input_dev *dev,
112 unsigned int scancode, unsigned int *keycode)
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200113{
Dmitry Torokhovf202df62007-10-31 12:33:26 +0100114 struct hid_device *hid = input_get_drvdata(dev);
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200115 struct hid_usage *usage;
Jiri Kosina77b29662007-08-09 14:04:56 +0200116
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200117 usage = hidinput_find_key(hid, scancode, 0);
118 if (usage) {
119 *keycode = usage->code;
120 return 0;
121 }
122 return -EINVAL;
123}
124
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800125static int hidinput_setkeycode(struct input_dev *dev,
126 unsigned int scancode, unsigned int keycode)
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200127{
Dmitry Torokhovf202df62007-10-31 12:33:26 +0100128 struct hid_device *hid = input_get_drvdata(dev);
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200129 struct hid_usage *usage;
130 int old_keycode;
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);
Joe Perches587d1452010-10-25 19:44:21 -0700139 dbg_hid("Assigned keycode %d to HID usage code %x\n",
140 keycode, scancode);
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200141 /* Set the keybit for the old keycode if the old keycode is used
142 * by another key */
143 if (hidinput_find_key (hid, 0, old_keycode))
144 set_bit(old_keycode, dev->keybit);
Jiri Kosina77b29662007-08-09 14:04:56 +0200145
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200146 return 0;
147 }
Jiri Kosina77b29662007-08-09 14:04:56 +0200148
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200149 return -EINVAL;
150}
151
152
Nikolai Kondrashov4ea6e4f2010-09-15 14:51:14 +0400153/**
154 * hidinput_calc_abs_res - calculate an absolute axis resolution
155 * @field: the HID report field to calculate resolution for
156 * @code: axis code
157 *
158 * The formula is:
159 * (logical_maximum - logical_minimum)
160 * resolution = ----------------------------------------------------------
161 * (physical_maximum - physical_minimum) * 10 ^ unit_exponent
162 *
163 * as seen in the HID specification v1.11 6.2.2.7 Global Items.
164 *
165 * Only exponent 1 length units are processed. Centimeters are converted to
166 * inches. Degrees are converted to radians.
167 */
168static __s32 hidinput_calc_abs_res(const struct hid_field *field, __u16 code)
169{
170 __s32 unit_exponent = field->unit_exponent;
171 __s32 logical_extents = field->logical_maximum -
172 field->logical_minimum;
173 __s32 physical_extents = field->physical_maximum -
174 field->physical_minimum;
175 __s32 prev;
176
177 /* Check if the extents are sane */
178 if (logical_extents <= 0 || physical_extents <= 0)
179 return 0;
180
181 /*
182 * Verify and convert units.
183 * See HID specification v1.11 6.2.2.7 Global Items for unit decoding
184 */
185 if (code == ABS_X || code == ABS_Y || code == ABS_Z) {
186 if (field->unit == 0x11) { /* If centimeters */
187 /* Convert to inches */
188 prev = logical_extents;
189 logical_extents *= 254;
190 if (logical_extents < prev)
191 return 0;
192 unit_exponent += 2;
193 } else if (field->unit != 0x13) { /* If not inches */
194 return 0;
195 }
196 } else if (code == ABS_RX || code == ABS_RY || code == ABS_RZ) {
197 if (field->unit == 0x14) { /* If degrees */
198 /* Convert to radians */
199 prev = logical_extents;
200 logical_extents *= 573;
201 if (logical_extents < prev)
202 return 0;
203 unit_exponent += 1;
204 } else if (field->unit != 0x12) { /* If not radians */
205 return 0;
206 }
207 } else {
208 return 0;
209 }
210
211 /* Apply negative unit exponent */
212 for (; unit_exponent < 0; unit_exponent++) {
213 prev = logical_extents;
214 logical_extents *= 10;
215 if (logical_extents < prev)
216 return 0;
217 }
218 /* Apply positive unit exponent */
219 for (; unit_exponent > 0; unit_exponent--) {
220 prev = physical_extents;
221 physical_extents *= 10;
222 if (physical_extents < prev)
223 return 0;
224 }
225
226 /* Calculate resolution */
227 return logical_extents / physical_extents;
228}
229
Jiri Kosinadde58452006-12-08 18:40:44 +0100230static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_field *field,
231 struct hid_usage *usage)
232{
233 struct input_dev *input = hidinput->input;
Dmitry Torokhove0712982007-05-09 10:17:31 +0200234 struct hid_device *device = input_get_drvdata(input);
Jiri Slaby3715ade2008-07-31 11:09:37 +0200235 int max = 0, code;
Jiri Kosinadde58452006-12-08 18:40:44 +0100236 unsigned long *bit = NULL;
237
238 field->hidinput = hidinput;
239
Jiri Kosinadde58452006-12-08 18:40:44 +0100240 if (field->flags & HID_MAIN_ITEM_CONSTANT)
241 goto ignore;
242
Jiri Kosina82eb1212007-08-11 23:39:42 +0200243 /* only LED usages are supported in output fields */
244 if (field->report_type == HID_OUTPUT_REPORT &&
245 (usage->hid & HID_USAGE_PAGE) != HID_UP_LED) {
Jiri Kosina82eb1212007-08-11 23:39:42 +0200246 goto ignore;
247 }
248
Jiri Slabyc500c972008-05-16 11:49:16 +0200249 if (device->driver->input_mapping) {
250 int ret = device->driver->input_mapping(device, hidinput, field,
251 usage, &bit, &max);
252 if (ret > 0)
253 goto mapped;
254 if (ret < 0)
255 goto ignore;
256 }
257
Jiri Kosinadde58452006-12-08 18:40:44 +0100258 switch (usage->hid & HID_USAGE_PAGE) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200259 case HID_UP_UNDEFINED:
260 goto ignore;
Jiri Kosinadde58452006-12-08 18:40:44 +0100261
Jiri Slaby880d29f2008-06-18 23:55:41 +0200262 case HID_UP_KEYBOARD:
263 set_bit(EV_REP, input->evbit);
Jiri Kosinadde58452006-12-08 18:40:44 +0100264
Jiri Slaby880d29f2008-06-18 23:55:41 +0200265 if ((usage->hid & HID_USAGE) < 256) {
266 if (!hid_keyboard[usage->hid & HID_USAGE]) goto ignore;
267 map_key_clear(hid_keyboard[usage->hid & HID_USAGE]);
268 } else
269 map_key(KEY_UNKNOWN);
Jiri Kosinadde58452006-12-08 18:40:44 +0100270
Jiri Slaby880d29f2008-06-18 23:55:41 +0200271 break;
Jiri Kosinadde58452006-12-08 18:40:44 +0100272
Jiri Slaby880d29f2008-06-18 23:55:41 +0200273 case HID_UP_BUTTON:
Jiri Kosina7f978b92010-02-17 09:36:35 +0100274 code = ((usage->hid - 1) & HID_USAGE);
Jiri Kosinadde58452006-12-08 18:40:44 +0100275
Jiri Slaby880d29f2008-06-18 23:55:41 +0200276 switch (field->application) {
277 case HID_GD_MOUSE:
278 case HID_GD_POINTER: code += 0x110; break;
Jiri Kosinacf2f765f2010-01-04 12:20:56 +0100279 case HID_GD_JOYSTICK:
Daniel Mackfc99f222010-07-12 19:28:26 +0200280 if (code <= 0xf)
281 code += BTN_JOYSTICK;
282 else
283 code += BTN_TRIGGER_HAPPY;
284 break;
Jiri Slaby880d29f2008-06-18 23:55:41 +0200285 case HID_GD_GAMEPAD: code += 0x130; break;
Jiri Kosinadde58452006-12-08 18:40:44 +0100286 default:
Jiri Slaby880d29f2008-06-18 23:55:41 +0200287 switch (field->physical) {
288 case HID_GD_MOUSE:
289 case HID_GD_POINTER: code += 0x110; break;
290 case HID_GD_JOYSTICK: code += 0x120; break;
291 case HID_GD_GAMEPAD: code += 0x130; break;
292 default: code += 0x100;
Jiri Kosinadde58452006-12-08 18:40:44 +0100293 }
Jiri Slaby880d29f2008-06-18 23:55:41 +0200294 }
295
296 map_key(code);
297 break;
298
299 case HID_UP_SIMULATION:
300 switch (usage->hid & 0xffff) {
301 case 0xba: map_abs(ABS_RUDDER); break;
302 case 0xbb: map_abs(ABS_THROTTLE); break;
303 case 0xc4: map_abs(ABS_GAS); break;
304 case 0xc5: map_abs(ABS_BRAKE); break;
305 case 0xc8: map_abs(ABS_WHEEL); break;
306 default: goto ignore;
307 }
308 break;
309
310 case HID_UP_GENDESK:
311 if ((usage->hid & 0xf0) == 0x80) { /* SystemControl */
312 switch (usage->hid & 0xf) {
313 case 0x1: map_key_clear(KEY_POWER); break;
314 case 0x2: map_key_clear(KEY_SLEEP); break;
315 case 0x3: map_key_clear(KEY_WAKEUP); break;
316 default: goto unknown;
Jiri Kosinadde58452006-12-08 18:40:44 +0100317 }
Jiri Kosinadde58452006-12-08 18:40:44 +0100318 break;
Jiri Slaby880d29f2008-06-18 23:55:41 +0200319 }
320
321 if ((usage->hid & 0xf0) == 0x90) { /* D-pad */
322 switch (usage->hid) {
323 case HID_GD_UP: usage->hat_dir = 1; break;
324 case HID_GD_DOWN: usage->hat_dir = 5; break;
325 case HID_GD_RIGHT: usage->hat_dir = 3; break;
326 case HID_GD_LEFT: usage->hat_dir = 7; break;
327 default: goto unknown;
328 }
329 if (field->dpad) {
330 map_abs(field->dpad);
331 goto ignore;
332 }
333 map_abs(ABS_HAT0X);
334 break;
335 }
336
337 switch (usage->hid) {
338 /* These usage IDs map directly to the usage codes. */
339 case HID_GD_X: case HID_GD_Y: case HID_GD_Z:
340 case HID_GD_RX: case HID_GD_RY: case HID_GD_RZ:
341 case HID_GD_SLIDER: case HID_GD_DIAL: case HID_GD_WHEEL:
342 if (field->flags & HID_MAIN_ITEM_RELATIVE)
343 map_rel(usage->hid & 0xf);
344 else
345 map_abs(usage->hid & 0xf);
346 break;
347
348 case HID_GD_HATSWITCH:
349 usage->hat_min = field->logical_minimum;
350 usage->hat_max = field->logical_maximum;
351 map_abs(ABS_HAT0X);
352 break;
353
354 case HID_GD_START: map_key_clear(BTN_START); break;
355 case HID_GD_SELECT: map_key_clear(BTN_SELECT); break;
356
357 default: goto unknown;
358 }
359
360 break;
361
362 case HID_UP_LED:
363 switch (usage->hid & 0xffff) { /* HID-Value: */
364 case 0x01: map_led (LED_NUML); break; /* "Num Lock" */
365 case 0x02: map_led (LED_CAPSL); break; /* "Caps Lock" */
366 case 0x03: map_led (LED_SCROLLL); break; /* "Scroll Lock" */
367 case 0x04: map_led (LED_COMPOSE); break; /* "Compose" */
368 case 0x05: map_led (LED_KANA); break; /* "Kana" */
369 case 0x27: map_led (LED_SLEEP); break; /* "Stand-By" */
370 case 0x4c: map_led (LED_SUSPEND); break; /* "System Suspend" */
371 case 0x09: map_led (LED_MUTE); break; /* "Mute" */
372 case 0x4b: map_led (LED_MISC); break; /* "Generic Indicator" */
373 case 0x19: map_led (LED_MAIL); break; /* "Message Waiting" */
374 case 0x4d: map_led (LED_CHARGING); break; /* "External Power Connected" */
375
376 default: goto ignore;
377 }
378 break;
379
380 case HID_UP_DIGITIZER:
381 switch (usage->hid & 0xff) {
Forest Bond8c8b01c2010-07-13 23:50:57 +0200382 case 0x00: /* Undefined */
383 goto ignore;
384
Jiri Slaby880d29f2008-06-18 23:55:41 +0200385 case 0x30: /* TipPressure */
386 if (!test_bit(BTN_TOUCH, input->keybit)) {
387 device->quirks |= HID_QUIRK_NOTOUCH;
388 set_bit(EV_KEY, input->evbit);
389 set_bit(BTN_TOUCH, input->keybit);
390 }
391 map_abs_clear(ABS_PRESSURE);
392 break;
393
394 case 0x32: /* InRange */
395 switch (field->physical & 0xff) {
396 case 0x21: map_key(BTN_TOOL_MOUSE); break;
397 case 0x22: map_key(BTN_TOOL_FINGER); break;
398 default: map_key(BTN_TOOL_PEN); break;
399 }
400 break;
401
402 case 0x3c: /* Invert */
403 map_key_clear(BTN_TOOL_RUBBER);
404 break;
405
406 case 0x33: /* Touch */
407 case 0x42: /* TipSwitch */
408 case 0x43: /* TipSwitch2 */
409 device->quirks &= ~HID_QUIRK_NOTOUCH;
410 map_key_clear(BTN_TOUCH);
411 break;
412
413 case 0x44: /* BarrelSwitch */
414 map_key_clear(BTN_STYLUS);
415 break;
416
Nikolai Kondrashov50b636972010-08-06 23:03:07 +0400417 case 0x46: /* TabletPick */
418 map_key_clear(BTN_STYLUS2);
419 break;
420
Jiri Slaby880d29f2008-06-18 23:55:41 +0200421 default: goto unknown;
422 }
423 break;
424
425 case HID_UP_CONSUMER: /* USB HUT v1.1, pages 56-62 */
426 switch (usage->hid & HID_USAGE) {
427 case 0x000: goto ignore;
428 case 0x034: map_key_clear(KEY_SLEEP); break;
429 case 0x036: map_key_clear(BTN_MISC); break;
430
431 case 0x040: map_key_clear(KEY_MENU); break;
432 case 0x045: map_key_clear(KEY_RADIO); break;
433
434 case 0x083: map_key_clear(KEY_LAST); break;
435 case 0x088: map_key_clear(KEY_PC); break;
436 case 0x089: map_key_clear(KEY_TV); break;
437 case 0x08a: map_key_clear(KEY_WWW); break;
438 case 0x08b: map_key_clear(KEY_DVD); break;
439 case 0x08c: map_key_clear(KEY_PHONE); break;
440 case 0x08d: map_key_clear(KEY_PROGRAM); break;
441 case 0x08e: map_key_clear(KEY_VIDEOPHONE); break;
442 case 0x08f: map_key_clear(KEY_GAMES); break;
443 case 0x090: map_key_clear(KEY_MEMO); break;
444 case 0x091: map_key_clear(KEY_CD); break;
445 case 0x092: map_key_clear(KEY_VCR); break;
446 case 0x093: map_key_clear(KEY_TUNER); break;
447 case 0x094: map_key_clear(KEY_EXIT); break;
448 case 0x095: map_key_clear(KEY_HELP); break;
449 case 0x096: map_key_clear(KEY_TAPE); break;
450 case 0x097: map_key_clear(KEY_TV2); break;
451 case 0x098: map_key_clear(KEY_SAT); break;
452 case 0x09a: map_key_clear(KEY_PVR); break;
453
454 case 0x09c: map_key_clear(KEY_CHANNELUP); break;
455 case 0x09d: map_key_clear(KEY_CHANNELDOWN); break;
456 case 0x0a0: map_key_clear(KEY_VCR2); break;
457
458 case 0x0b0: map_key_clear(KEY_PLAY); break;
459 case 0x0b1: map_key_clear(KEY_PAUSE); break;
460 case 0x0b2: map_key_clear(KEY_RECORD); break;
461 case 0x0b3: map_key_clear(KEY_FASTFORWARD); break;
462 case 0x0b4: map_key_clear(KEY_REWIND); break;
463 case 0x0b5: map_key_clear(KEY_NEXTSONG); break;
464 case 0x0b6: map_key_clear(KEY_PREVIOUSSONG); break;
465 case 0x0b7: map_key_clear(KEY_STOPCD); break;
466 case 0x0b8: map_key_clear(KEY_EJECTCD); break;
467 case 0x0bc: map_key_clear(KEY_MEDIA_REPEAT); break;
468
469 case 0x0cd: map_key_clear(KEY_PLAYPAUSE); break;
470 case 0x0e0: map_abs_clear(ABS_VOLUME); break;
471 case 0x0e2: map_key_clear(KEY_MUTE); break;
472 case 0x0e5: map_key_clear(KEY_BASSBOOST); break;
473 case 0x0e9: map_key_clear(KEY_VOLUMEUP); break;
474 case 0x0ea: map_key_clear(KEY_VOLUMEDOWN); break;
475
476 case 0x182: map_key_clear(KEY_BOOKMARKS); break;
477 case 0x183: map_key_clear(KEY_CONFIG); break;
478 case 0x184: map_key_clear(KEY_WORDPROCESSOR); break;
479 case 0x185: map_key_clear(KEY_EDITOR); break;
480 case 0x186: map_key_clear(KEY_SPREADSHEET); break;
481 case 0x187: map_key_clear(KEY_GRAPHICSEDITOR); break;
482 case 0x188: map_key_clear(KEY_PRESENTATION); break;
483 case 0x189: map_key_clear(KEY_DATABASE); break;
484 case 0x18a: map_key_clear(KEY_MAIL); break;
485 case 0x18b: map_key_clear(KEY_NEWS); break;
486 case 0x18c: map_key_clear(KEY_VOICEMAIL); break;
487 case 0x18d: map_key_clear(KEY_ADDRESSBOOK); break;
488 case 0x18e: map_key_clear(KEY_CALENDAR); break;
489 case 0x191: map_key_clear(KEY_FINANCE); break;
490 case 0x192: map_key_clear(KEY_CALC); break;
491 case 0x194: map_key_clear(KEY_FILE); break;
492 case 0x196: map_key_clear(KEY_WWW); break;
Leo P White06905352010-02-08 13:02:05 +0000493 case 0x199: map_key_clear(KEY_CHAT); break;
Jiri Slaby880d29f2008-06-18 23:55:41 +0200494 case 0x19c: map_key_clear(KEY_LOGOFF); break;
495 case 0x19e: map_key_clear(KEY_COFFEE); break;
496 case 0x1a6: map_key_clear(KEY_HELP); break;
497 case 0x1a7: map_key_clear(KEY_DOCUMENTS); break;
498 case 0x1ab: map_key_clear(KEY_SPELLCHECK); break;
499 case 0x1b6: map_key_clear(KEY_MEDIA); break;
500 case 0x1b7: map_key_clear(KEY_SOUND); break;
501 case 0x1bc: map_key_clear(KEY_MESSENGER); break;
502 case 0x1bd: map_key_clear(KEY_INFO); break;
503 case 0x201: map_key_clear(KEY_NEW); break;
504 case 0x202: map_key_clear(KEY_OPEN); break;
505 case 0x203: map_key_clear(KEY_CLOSE); break;
506 case 0x204: map_key_clear(KEY_EXIT); break;
507 case 0x207: map_key_clear(KEY_SAVE); break;
508 case 0x208: map_key_clear(KEY_PRINT); break;
509 case 0x209: map_key_clear(KEY_PROPS); break;
510 case 0x21a: map_key_clear(KEY_UNDO); break;
511 case 0x21b: map_key_clear(KEY_COPY); break;
512 case 0x21c: map_key_clear(KEY_CUT); break;
513 case 0x21d: map_key_clear(KEY_PASTE); break;
514 case 0x21f: map_key_clear(KEY_FIND); break;
515 case 0x221: map_key_clear(KEY_SEARCH); break;
516 case 0x222: map_key_clear(KEY_GOTO); break;
517 case 0x223: map_key_clear(KEY_HOMEPAGE); break;
518 case 0x224: map_key_clear(KEY_BACK); break;
519 case 0x225: map_key_clear(KEY_FORWARD); break;
520 case 0x226: map_key_clear(KEY_STOP); break;
521 case 0x227: map_key_clear(KEY_REFRESH); break;
522 case 0x22a: map_key_clear(KEY_BOOKMARKS); break;
523 case 0x22d: map_key_clear(KEY_ZOOMIN); break;
524 case 0x22e: map_key_clear(KEY_ZOOMOUT); break;
525 case 0x22f: map_key_clear(KEY_ZOOMRESET); break;
526 case 0x233: map_key_clear(KEY_SCROLLUP); break;
527 case 0x234: map_key_clear(KEY_SCROLLDOWN); break;
528 case 0x238: map_rel(REL_HWHEEL); break;
529 case 0x25f: map_key_clear(KEY_CANCEL); break;
530 case 0x279: map_key_clear(KEY_REDO); break;
531
532 case 0x289: map_key_clear(KEY_REPLY); break;
533 case 0x28b: map_key_clear(KEY_FORWARDMAIL); break;
534 case 0x28c: map_key_clear(KEY_SEND); break;
535
536 default: goto ignore;
537 }
538 break;
539
540 case HID_UP_HPVENDOR: /* Reported on a Dutch layout HP5308 */
541 set_bit(EV_REP, input->evbit);
542 switch (usage->hid & HID_USAGE) {
543 case 0x021: map_key_clear(KEY_PRINT); break;
544 case 0x070: map_key_clear(KEY_HP); break;
545 case 0x071: map_key_clear(KEY_CAMERA); break;
546 case 0x072: map_key_clear(KEY_SOUND); break;
547 case 0x073: map_key_clear(KEY_QUESTION); break;
548 case 0x080: map_key_clear(KEY_EMAIL); break;
549 case 0x081: map_key_clear(KEY_CHAT); break;
550 case 0x082: map_key_clear(KEY_SEARCH); break;
551 case 0x083: map_key_clear(KEY_CONNECT); break;
552 case 0x084: map_key_clear(KEY_FINANCE); break;
553 case 0x085: map_key_clear(KEY_SPORT); break;
554 case 0x086: map_key_clear(KEY_SHOP); break;
555 default: goto ignore;
556 }
557 break;
558
559 case HID_UP_MSVENDOR:
560 goto ignore;
561
562 case HID_UP_CUSTOM: /* Reported on Logitech and Apple USB keyboards */
563 set_bit(EV_REP, input->evbit);
564 goto ignore;
565
566 case HID_UP_LOGIVENDOR:
567 goto ignore;
Daniel Mackfc99f222010-07-12 19:28:26 +0200568
Jiri Slaby880d29f2008-06-18 23:55:41 +0200569 case HID_UP_PID:
570 switch (usage->hid & HID_USAGE) {
571 case 0xa4: map_key_clear(BTN_DEAD); break;
572 default: goto ignore;
573 }
574 break;
575
576 default:
577 unknown:
578 if (field->report_size == 1) {
579 if (field->report->type == HID_OUTPUT_REPORT) {
580 map_led(LED_MISC);
581 break;
582 }
583 map_key(BTN_MISC);
584 break;
585 }
586 if (field->flags & HID_MAIN_ITEM_RELATIVE) {
587 map_rel(REL_MISC);
588 break;
589 }
590 map_abs(ABS_MISC);
591 break;
Jiri Kosinadde58452006-12-08 18:40:44 +0100592 }
593
Jiri Kosina10bd0652007-11-22 15:18:18 +0100594mapped:
Jiri Slabyc500c972008-05-16 11:49:16 +0200595 if (device->driver->input_mapped && device->driver->input_mapped(device,
596 hidinput, field, usage, &bit, &max) < 0)
597 goto ignore;
598
Jiri Kosinadde58452006-12-08 18:40:44 +0100599 set_bit(usage->type, input->evbit);
600
601 while (usage->code <= max && test_and_set_bit(usage->code, bit))
602 usage->code = find_next_zero_bit(bit, max + 1, usage->code);
603
604 if (usage->code > max)
605 goto ignore;
606
607
608 if (usage->type == EV_ABS) {
609
610 int a = field->logical_minimum;
611 int b = field->logical_maximum;
612
613 if ((device->quirks & HID_QUIRK_BADPAD) && (usage->code == ABS_X || usage->code == ABS_Y)) {
614 a = field->logical_minimum = 0;
615 b = field->logical_maximum = 255;
616 }
617
618 if (field->application == HID_GD_GAMEPAD || field->application == HID_GD_JOYSTICK)
619 input_set_abs_params(input, usage->code, a, b, (b - a) >> 8, (b - a) >> 4);
620 else input_set_abs_params(input, usage->code, a, b, 0, 0);
621
Nikolai Kondrashov4ea6e4f2010-09-15 14:51:14 +0400622 input_abs_set_res(input, usage->code,
623 hidinput_calc_abs_res(field, usage->code));
624
Henrik Rydberg6967b4d2010-06-23 09:31:37 -0700625 /* use a larger default input buffer for MT devices */
626 if (usage->code == ABS_MT_POSITION_X && input->hint_events_per_packet == 0)
627 input_set_events_per_packet(input, 60);
Jiri Kosinadde58452006-12-08 18:40:44 +0100628 }
629
630 if (usage->type == EV_ABS &&
631 (usage->hat_min < usage->hat_max || usage->hat_dir)) {
632 int i;
633 for (i = usage->code; i < usage->code + 2 && i <= max; i++) {
634 input_set_abs_params(input, i, -1, 1, 0, 0);
635 set_bit(i, input->absbit);
636 }
637 if (usage->hat_dir && !field->dpad)
638 field->dpad = usage->code;
639 }
640
Jiri Kosina2c1d8ae2007-07-04 16:45:59 +0200641 /* for those devices which produce Consumer volume usage as relative,
642 * we emulate pressing volumeup/volumedown appropriate number of times
643 * in hidinput_hid_event()
644 */
645 if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
646 (usage->code == ABS_VOLUME)) {
647 set_bit(KEY_VOLUMEUP, input->keybit);
648 set_bit(KEY_VOLUMEDOWN, input->keybit);
649 }
650
Jiri Kosinac01d50d2007-08-20 12:13:34 +0200651 if (usage->type == EV_KEY) {
652 set_bit(EV_MSC, input->evbit);
653 set_bit(MSC_SCAN, input->mscbit);
654 }
655
Jiri Kosinadde58452006-12-08 18:40:44 +0100656ignore:
Jiri Kosinadde58452006-12-08 18:40:44 +0100657 return;
Jiri Kosinaa635f9d2009-06-12 15:20:55 +0200658
Jiri Kosinadde58452006-12-08 18:40:44 +0100659}
660
661void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
662{
663 struct input_dev *input;
Al Virob4482a42007-10-14 19:35:40 +0100664 unsigned *quirks = &hid->quirks;
Jiri Kosinadde58452006-12-08 18:40:44 +0100665
666 if (!field->hidinput)
667 return;
668
669 input = field->hidinput->input;
670
671 if (!usage->type)
672 return;
673
Jiri Kosinadde58452006-12-08 18:40:44 +0100674 if (usage->hat_min < usage->hat_max || usage->hat_dir) {
675 int hat_dir = usage->hat_dir;
676 if (!hat_dir)
677 hat_dir = (value - usage->hat_min) * 8 / (usage->hat_max - usage->hat_min + 1) + 1;
678 if (hat_dir < 0 || hat_dir > 8) hat_dir = 0;
679 input_event(input, usage->type, usage->code , hid_hat_to_axis[hat_dir].x);
Daniel Mackfc99f222010-07-12 19:28:26 +0200680 input_event(input, usage->type, usage->code + 1, hid_hat_to_axis[hat_dir].y);
681 return;
682 }
Jiri Kosinadde58452006-12-08 18:40:44 +0100683
684 if (usage->hid == (HID_UP_DIGITIZER | 0x003c)) { /* Invert */
685 *quirks = value ? (*quirks | HID_QUIRK_INVERT) : (*quirks & ~HID_QUIRK_INVERT);
686 return;
687 }
688
689 if (usage->hid == (HID_UP_DIGITIZER | 0x0032)) { /* InRange */
690 if (value) {
691 input_event(input, usage->type, (*quirks & HID_QUIRK_INVERT) ? BTN_TOOL_RUBBER : usage->code, 1);
692 return;
693 }
694 input_event(input, usage->type, usage->code, 0);
695 input_event(input, usage->type, BTN_TOOL_RUBBER, 0);
696 return;
697 }
698
699 if (usage->hid == (HID_UP_DIGITIZER | 0x0030) && (*quirks & HID_QUIRK_NOTOUCH)) { /* Pressure */
700 int a = field->logical_minimum;
701 int b = field->logical_maximum;
702 input_event(input, EV_KEY, BTN_TOUCH, value > a + ((b - a) >> 3));
703 }
704
705 if (usage->hid == (HID_UP_PID | 0x83UL)) { /* Simultaneous Effects Max */
Jiri Kosina58037eb2007-05-30 15:07:13 +0200706 dbg_hid("Maximum Effects - %d\n",value);
Jiri Kosinadde58452006-12-08 18:40:44 +0100707 return;
708 }
709
710 if (usage->hid == (HID_UP_PID | 0x7fUL)) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200711 dbg_hid("PID Pool Report\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100712 return;
713 }
714
715 if ((usage->type == EV_KEY) && (usage->code == 0)) /* Key 0 is "unassigned", not KEY_UNKNOWN */
716 return;
717
Jiri Kosina2c1d8ae2007-07-04 16:45:59 +0200718 if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
719 (usage->code == ABS_VOLUME)) {
720 int count = abs(value);
721 int direction = value > 0 ? KEY_VOLUMEUP : KEY_VOLUMEDOWN;
722 int i;
723
724 for (i = 0; i < count; i++) {
725 input_event(input, EV_KEY, direction, 1);
726 input_sync(input);
727 input_event(input, EV_KEY, direction, 0);
728 input_sync(input);
729 }
730 return;
731 }
732
Jiri Kosinac01d50d2007-08-20 12:13:34 +0200733 /* report the usage code as scancode if the key status has changed */
734 if (usage->type == EV_KEY && !!test_bit(usage->code, input->key) != value)
735 input_event(input, EV_MSC, MSC_SCAN, usage->hid);
Jiri Kosina1fe8736d2007-08-09 13:24:11 +0200736
Jiri Kosinadde58452006-12-08 18:40:44 +0100737 input_event(input, usage->type, usage->code, value);
738
739 if ((field->flags & HID_MAIN_ITEM_RELATIVE) && (usage->type == EV_KEY))
740 input_event(input, usage->type, usage->code, 0);
741}
742
743void hidinput_report_event(struct hid_device *hid, struct hid_report *report)
744{
745 struct hid_input *hidinput;
746
Henrik Rydberg24750f32010-08-24 10:54:44 +0200747 if (hid->quirks & HID_QUIRK_NO_INPUT_SYNC)
748 return;
749
Jiri Kosinadde58452006-12-08 18:40:44 +0100750 list_for_each_entry(hidinput, &hid->inputs, list)
751 input_sync(hidinput->input);
752}
Jiri Kosina229695e2006-12-08 18:40:53 +0100753EXPORT_SYMBOL_GPL(hidinput_report_event);
Jiri Kosinadde58452006-12-08 18:40:44 +0100754
Jiri Kosina229695e2006-12-08 18:40:53 +0100755int hidinput_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field)
Jiri Kosinadde58452006-12-08 18:40:44 +0100756{
757 struct hid_report *report;
758 int i, j;
759
760 list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
761 for (i = 0; i < report->maxfield; i++) {
762 *field = report->field[i];
763 for (j = 0; j < (*field)->maxusage; j++)
764 if ((*field)->usage[j].type == type && (*field)->usage[j].code == code)
765 return j;
766 }
767 }
768 return -1;
769}
Jiri Kosina229695e2006-12-08 18:40:53 +0100770EXPORT_SYMBOL_GPL(hidinput_find_field);
Jiri Kosinadde58452006-12-08 18:40:44 +0100771
Jiri Kosina7c379142007-01-24 11:54:19 +0100772static int hidinput_open(struct input_dev *dev)
773{
Dmitry Torokhove0712982007-05-09 10:17:31 +0200774 struct hid_device *hid = input_get_drvdata(dev);
775
Jiri Slabyc500c972008-05-16 11:49:16 +0200776 return hid->ll_driver->open(hid);
Jiri Kosina7c379142007-01-24 11:54:19 +0100777}
778
779static void hidinput_close(struct input_dev *dev)
780{
Dmitry Torokhove0712982007-05-09 10:17:31 +0200781 struct hid_device *hid = input_get_drvdata(dev);
782
Jiri Slabyc500c972008-05-16 11:49:16 +0200783 hid->ll_driver->close(hid);
Jiri Kosina7c379142007-01-24 11:54:19 +0100784}
785
Jiri Kosinadde58452006-12-08 18:40:44 +0100786/*
787 * Register the input device; print a message.
788 * Configure the input layer interface
789 * Read all reports and initialize the absolute field values.
790 */
791
Jiri Slaby93c10132008-06-27 00:04:24 +0200792int hidinput_connect(struct hid_device *hid, unsigned int force)
Jiri Kosinadde58452006-12-08 18:40:44 +0100793{
Jiri Kosinadde58452006-12-08 18:40:44 +0100794 struct hid_report *report;
795 struct hid_input *hidinput = NULL;
796 struct input_dev *input_dev;
797 int i, j, k;
Anssi Hannula5556fea2007-01-11 16:51:17 +0200798 int max_report_type = HID_OUTPUT_REPORT;
Jiri Kosinadde58452006-12-08 18:40:44 +0100799
800 INIT_LIST_HEAD(&hid->inputs);
801
Jiri Slaby93c10132008-06-27 00:04:24 +0200802 if (!force) {
803 for (i = 0; i < hid->maxcollection; i++) {
804 struct hid_collection *col = &hid->collection[i];
805 if (col->type == HID_COLLECTION_APPLICATION ||
806 col->type == HID_COLLECTION_PHYSICAL)
807 if (IS_INPUT_APPLICATION(col->usage))
808 break;
809 }
Jiri Kosinadde58452006-12-08 18:40:44 +0100810
Jiri Slaby93c10132008-06-27 00:04:24 +0200811 if (i == hid->maxcollection)
812 return -1;
813 }
Jiri Kosinadde58452006-12-08 18:40:44 +0100814
Anssi Hannula5556fea2007-01-11 16:51:17 +0200815 if (hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORTS)
816 max_report_type = HID_INPUT_REPORT;
817
818 for (k = HID_INPUT_REPORT; k <= max_report_type; k++)
Jiri Kosinadde58452006-12-08 18:40:44 +0100819 list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
820
821 if (!report->maxfield)
822 continue;
823
824 if (!hidinput) {
825 hidinput = kzalloc(sizeof(*hidinput), GFP_KERNEL);
826 input_dev = input_allocate_device();
827 if (!hidinput || !input_dev) {
828 kfree(hidinput);
829 input_free_device(input_dev);
Jiri Kosina58037eb2007-05-30 15:07:13 +0200830 err_hid("Out of memory during hid input probe");
Dirk Hohndel368d2902007-10-30 13:02:44 +0100831 goto out_unwind;
Jiri Kosinadde58452006-12-08 18:40:44 +0100832 }
833
Dmitry Torokhove0712982007-05-09 10:17:31 +0200834 input_set_drvdata(input_dev, hid);
Jiri Slabyc500c972008-05-16 11:49:16 +0200835 input_dev->event =
836 hid->ll_driver->hidinput_input_event;
Jiri Kosina7c379142007-01-24 11:54:19 +0100837 input_dev->open = hidinput_open;
838 input_dev->close = hidinput_close;
Marvin Raaijmakersfe7ba312007-05-09 10:57:20 +0200839 input_dev->setkeycode = hidinput_setkeycode;
840 input_dev->getkeycode = hidinput_getkeycode;
Jiri Kosinadde58452006-12-08 18:40:44 +0100841
842 input_dev->name = hid->name;
843 input_dev->phys = hid->phys;
844 input_dev->uniq = hid->uniq;
Jiri Kosina229695e2006-12-08 18:40:53 +0100845 input_dev->id.bustype = hid->bus;
846 input_dev->id.vendor = hid->vendor;
847 input_dev->id.product = hid->product;
848 input_dev->id.version = hid->version;
Jiri Slaby85cdaf52008-05-16 11:49:15 +0200849 input_dev->dev.parent = hid->dev.parent;
Jiri Kosinadde58452006-12-08 18:40:44 +0100850 hidinput->input = input_dev;
851 list_add_tail(&hidinput->list, &hid->inputs);
852 }
853
854 for (i = 0; i < report->maxfield; i++)
855 for (j = 0; j < report->field[i]->maxusage; j++)
856 hidinput_configure_usage(hidinput, report->field[i],
857 report->field[i]->usage + j);
858
859 if (hid->quirks & HID_QUIRK_MULTI_INPUT) {
860 /* This will leave hidinput NULL, so that it
861 * allocates another one if we have more inputs on
862 * the same interface. Some devices (e.g. Happ's
863 * UGCI) cram a lot of unrelated inputs into the
864 * same interface. */
865 hidinput->report = report;
Dirk Hohndel368d2902007-10-30 13:02:44 +0100866 if (input_register_device(hidinput->input))
867 goto out_cleanup;
Jiri Kosinadde58452006-12-08 18:40:44 +0100868 hidinput = NULL;
869 }
870 }
871
Dirk Hohndel368d2902007-10-30 13:02:44 +0100872 if (hidinput && input_register_device(hidinput->input))
873 goto out_cleanup;
Jiri Kosinadde58452006-12-08 18:40:44 +0100874
875 return 0;
Dirk Hohndel368d2902007-10-30 13:02:44 +0100876
877out_cleanup:
878 input_free_device(hidinput->input);
879 kfree(hidinput);
880out_unwind:
881 /* unwind the ones we already registered */
882 hidinput_disconnect(hid);
883
884 return -1;
Jiri Kosinadde58452006-12-08 18:40:44 +0100885}
Jiri Kosina229695e2006-12-08 18:40:53 +0100886EXPORT_SYMBOL_GPL(hidinput_connect);
Jiri Kosinadde58452006-12-08 18:40:44 +0100887
888void hidinput_disconnect(struct hid_device *hid)
889{
890 struct hid_input *hidinput, *next;
891
892 list_for_each_entry_safe(hidinput, next, &hid->inputs, list) {
893 list_del(&hidinput->list);
894 input_unregister_device(hidinput->input);
895 kfree(hidinput);
896 }
897}
Jiri Kosina229695e2006-12-08 18:40:53 +0100898EXPORT_SYMBOL_GPL(hidinput_disconnect);
899