IPVS: fix netns if reading ip_vs_* procfs entries
[linux-2.6/linux-mips.git] / drivers / hid / hid-input.c
blob33dde8724e02faec04fe1388103343a4cf03eda2
1 /*
2 * Copyright (c) 2000-2001 Vojtech Pavlik
3 * Copyright (c) 2006-2010 Jiri Kosina
5 * HID to Linux Input mapping
6 */
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.
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.
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
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
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #include <linux/kernel.h>
32 #include <linux/hid.h>
33 #include <linux/hid-debug.h>
35 #define unk KEY_UNKNOWN
37 static 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,
49 unk,unk,unk,unk,unk,unk,179,180,unk,unk,unk,unk,unk,unk,unk,unk,
50 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
56 static 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}};
61 #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))
66 #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))
71 static bool match_scancode(struct hid_usage *usage,
72 unsigned int cur_idx, unsigned int scancode)
74 return (usage->hid & (HID_USAGE_PAGE | HID_USAGE)) == scancode;
77 static bool match_keycode(struct hid_usage *usage,
78 unsigned int cur_idx, unsigned int keycode)
81 * We should exclude unmapped usages when doing lookup by keycode.
83 return (usage->type == EV_KEY && usage->code == keycode);
86 static bool match_index(struct hid_usage *usage,
87 unsigned int cur_idx, unsigned int idx)
89 return cur_idx == idx;
92 typedef bool (*hid_usage_cmp_t)(struct hid_usage *usage,
93 unsigned int cur_idx, unsigned int val);
95 static struct hid_usage *hidinput_find_key(struct hid_device *hid,
96 hid_usage_cmp_t match,
97 unsigned int value,
98 unsigned int *usage_idx)
100 unsigned int i, j, k, cur_idx = 0;
101 struct hid_report *report;
102 struct hid_usage *usage;
104 for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
105 list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
106 for (i = 0; i < report->maxfield; i++) {
107 for (j = 0; j < report->field[i]->maxusage; j++) {
108 usage = report->field[i]->usage + j;
109 if (usage->type == EV_KEY || usage->type == 0) {
110 if (match(usage, cur_idx, value)) {
111 if (usage_idx)
112 *usage_idx = cur_idx;
113 return usage;
115 cur_idx++;
121 return NULL;
124 static struct hid_usage *hidinput_locate_usage(struct hid_device *hid,
125 const struct input_keymap_entry *ke,
126 unsigned int *index)
128 struct hid_usage *usage;
129 unsigned int scancode;
131 if (ke->flags & INPUT_KEYMAP_BY_INDEX)
132 usage = hidinput_find_key(hid, match_index, ke->index, index);
133 else if (input_scancode_to_scalar(ke, &scancode) == 0)
134 usage = hidinput_find_key(hid, match_scancode, scancode, index);
135 else
136 usage = NULL;
138 return usage;
141 static int hidinput_getkeycode(struct input_dev *dev,
142 struct input_keymap_entry *ke)
144 struct hid_device *hid = input_get_drvdata(dev);
145 struct hid_usage *usage;
146 unsigned int scancode, index;
148 usage = hidinput_locate_usage(hid, ke, &index);
149 if (usage) {
150 ke->keycode = usage->type == EV_KEY ?
151 usage->code : KEY_RESERVED;
152 ke->index = index;
153 scancode = usage->hid & (HID_USAGE_PAGE | HID_USAGE);
154 ke->len = sizeof(scancode);
155 memcpy(ke->scancode, &scancode, sizeof(scancode));
156 return 0;
159 return -EINVAL;
162 static int hidinput_setkeycode(struct input_dev *dev,
163 const struct input_keymap_entry *ke,
164 unsigned int *old_keycode)
166 struct hid_device *hid = input_get_drvdata(dev);
167 struct hid_usage *usage;
169 usage = hidinput_locate_usage(hid, ke, NULL);
170 if (usage) {
171 *old_keycode = usage->type == EV_KEY ?
172 usage->code : KEY_RESERVED;
173 usage->code = ke->keycode;
175 clear_bit(*old_keycode, dev->keybit);
176 set_bit(usage->code, dev->keybit);
177 dbg_hid("Assigned keycode %d to HID usage code %x\n",
178 usage->code, usage->hid);
181 * Set the keybit for the old keycode if the old keycode is used
182 * by another key
184 if (hidinput_find_key(hid, match_keycode, *old_keycode, NULL))
185 set_bit(*old_keycode, dev->keybit);
187 return 0;
190 return -EINVAL;
195 * hidinput_calc_abs_res - calculate an absolute axis resolution
196 * @field: the HID report field to calculate resolution for
197 * @code: axis code
199 * The formula is:
200 * (logical_maximum - logical_minimum)
201 * resolution = ----------------------------------------------------------
202 * (physical_maximum - physical_minimum) * 10 ^ unit_exponent
204 * as seen in the HID specification v1.11 6.2.2.7 Global Items.
206 * Only exponent 1 length units are processed. Centimeters and inches are
207 * converted to millimeters. Degrees are converted to radians.
209 static __s32 hidinput_calc_abs_res(const struct hid_field *field, __u16 code)
211 __s32 unit_exponent = field->unit_exponent;
212 __s32 logical_extents = field->logical_maximum -
213 field->logical_minimum;
214 __s32 physical_extents = field->physical_maximum -
215 field->physical_minimum;
216 __s32 prev;
218 /* Check if the extents are sane */
219 if (logical_extents <= 0 || physical_extents <= 0)
220 return 0;
223 * Verify and convert units.
224 * See HID specification v1.11 6.2.2.7 Global Items for unit decoding
226 if (code == ABS_X || code == ABS_Y || code == ABS_Z) {
227 if (field->unit == 0x11) { /* If centimeters */
228 /* Convert to millimeters */
229 unit_exponent += 1;
230 } else if (field->unit == 0x13) { /* If inches */
231 /* Convert to millimeters */
232 prev = physical_extents;
233 physical_extents *= 254;
234 if (physical_extents < prev)
235 return 0;
236 unit_exponent -= 1;
237 } else {
238 return 0;
240 } else if (code == ABS_RX || code == ABS_RY || code == ABS_RZ) {
241 if (field->unit == 0x14) { /* If degrees */
242 /* Convert to radians */
243 prev = logical_extents;
244 logical_extents *= 573;
245 if (logical_extents < prev)
246 return 0;
247 unit_exponent += 1;
248 } else if (field->unit != 0x12) { /* If not radians */
249 return 0;
251 } else {
252 return 0;
255 /* Apply negative unit exponent */
256 for (; unit_exponent < 0; unit_exponent++) {
257 prev = logical_extents;
258 logical_extents *= 10;
259 if (logical_extents < prev)
260 return 0;
262 /* Apply positive unit exponent */
263 for (; unit_exponent > 0; unit_exponent--) {
264 prev = physical_extents;
265 physical_extents *= 10;
266 if (physical_extents < prev)
267 return 0;
270 /* Calculate resolution */
271 return logical_extents / physical_extents;
274 static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_field *field,
275 struct hid_usage *usage)
277 struct input_dev *input = hidinput->input;
278 struct hid_device *device = input_get_drvdata(input);
279 int max = 0, code;
280 unsigned long *bit = NULL;
282 field->hidinput = hidinput;
284 if (field->flags & HID_MAIN_ITEM_CONSTANT)
285 goto ignore;
287 /* only LED usages are supported in output fields */
288 if (field->report_type == HID_OUTPUT_REPORT &&
289 (usage->hid & HID_USAGE_PAGE) != HID_UP_LED) {
290 goto ignore;
293 if (device->driver->input_mapping) {
294 int ret = device->driver->input_mapping(device, hidinput, field,
295 usage, &bit, &max);
296 if (ret > 0)
297 goto mapped;
298 if (ret < 0)
299 goto ignore;
302 switch (usage->hid & HID_USAGE_PAGE) {
303 case HID_UP_UNDEFINED:
304 goto ignore;
306 case HID_UP_KEYBOARD:
307 set_bit(EV_REP, input->evbit);
309 if ((usage->hid & HID_USAGE) < 256) {
310 if (!hid_keyboard[usage->hid & HID_USAGE]) goto ignore;
311 map_key_clear(hid_keyboard[usage->hid & HID_USAGE]);
312 } else
313 map_key(KEY_UNKNOWN);
315 break;
317 case HID_UP_BUTTON:
318 code = ((usage->hid - 1) & HID_USAGE);
320 switch (field->application) {
321 case HID_GD_MOUSE:
322 case HID_GD_POINTER: code += BTN_MOUSE; break;
323 case HID_GD_JOYSTICK:
324 if (code <= 0xf)
325 code += BTN_JOYSTICK;
326 else
327 code += BTN_TRIGGER_HAPPY;
328 break;
329 case HID_GD_GAMEPAD: code += BTN_GAMEPAD; break;
330 default:
331 switch (field->physical) {
332 case HID_GD_MOUSE:
333 case HID_GD_POINTER: code += BTN_MOUSE; break;
334 case HID_GD_JOYSTICK: code += BTN_JOYSTICK; break;
335 case HID_GD_GAMEPAD: code += BTN_GAMEPAD; break;
336 default: code += BTN_MISC;
340 map_key(code);
341 break;
343 case HID_UP_SIMULATION:
344 switch (usage->hid & 0xffff) {
345 case 0xba: map_abs(ABS_RUDDER); break;
346 case 0xbb: map_abs(ABS_THROTTLE); break;
347 case 0xc4: map_abs(ABS_GAS); break;
348 case 0xc5: map_abs(ABS_BRAKE); break;
349 case 0xc8: map_abs(ABS_WHEEL); break;
350 default: goto ignore;
352 break;
354 case HID_UP_GENDESK:
355 if ((usage->hid & 0xf0) == 0x80) { /* SystemControl */
356 switch (usage->hid & 0xf) {
357 case 0x1: map_key_clear(KEY_POWER); break;
358 case 0x2: map_key_clear(KEY_SLEEP); break;
359 case 0x3: map_key_clear(KEY_WAKEUP); break;
360 default: goto unknown;
362 break;
365 if ((usage->hid & 0xf0) == 0x90) { /* D-pad */
366 switch (usage->hid) {
367 case HID_GD_UP: usage->hat_dir = 1; break;
368 case HID_GD_DOWN: usage->hat_dir = 5; break;
369 case HID_GD_RIGHT: usage->hat_dir = 3; break;
370 case HID_GD_LEFT: usage->hat_dir = 7; break;
371 default: goto unknown;
373 if (field->dpad) {
374 map_abs(field->dpad);
375 goto ignore;
377 map_abs(ABS_HAT0X);
378 break;
381 switch (usage->hid) {
382 /* These usage IDs map directly to the usage codes. */
383 case HID_GD_X: case HID_GD_Y: case HID_GD_Z:
384 case HID_GD_RX: case HID_GD_RY: case HID_GD_RZ:
385 case HID_GD_SLIDER: case HID_GD_DIAL: case HID_GD_WHEEL:
386 if (field->flags & HID_MAIN_ITEM_RELATIVE)
387 map_rel(usage->hid & 0xf);
388 else
389 map_abs(usage->hid & 0xf);
390 break;
392 case HID_GD_HATSWITCH:
393 usage->hat_min = field->logical_minimum;
394 usage->hat_max = field->logical_maximum;
395 map_abs(ABS_HAT0X);
396 break;
398 case HID_GD_START: map_key_clear(BTN_START); break;
399 case HID_GD_SELECT: map_key_clear(BTN_SELECT); break;
401 default: goto unknown;
404 break;
406 case HID_UP_LED:
407 switch (usage->hid & 0xffff) { /* HID-Value: */
408 case 0x01: map_led (LED_NUML); break; /* "Num Lock" */
409 case 0x02: map_led (LED_CAPSL); break; /* "Caps Lock" */
410 case 0x03: map_led (LED_SCROLLL); break; /* "Scroll Lock" */
411 case 0x04: map_led (LED_COMPOSE); break; /* "Compose" */
412 case 0x05: map_led (LED_KANA); break; /* "Kana" */
413 case 0x27: map_led (LED_SLEEP); break; /* "Stand-By" */
414 case 0x4c: map_led (LED_SUSPEND); break; /* "System Suspend" */
415 case 0x09: map_led (LED_MUTE); break; /* "Mute" */
416 case 0x4b: map_led (LED_MISC); break; /* "Generic Indicator" */
417 case 0x19: map_led (LED_MAIL); break; /* "Message Waiting" */
418 case 0x4d: map_led (LED_CHARGING); break; /* "External Power Connected" */
420 default: goto ignore;
422 break;
424 case HID_UP_DIGITIZER:
425 switch (usage->hid & 0xff) {
426 case 0x00: /* Undefined */
427 goto ignore;
429 case 0x30: /* TipPressure */
430 if (!test_bit(BTN_TOUCH, input->keybit)) {
431 device->quirks |= HID_QUIRK_NOTOUCH;
432 set_bit(EV_KEY, input->evbit);
433 set_bit(BTN_TOUCH, input->keybit);
435 map_abs_clear(ABS_PRESSURE);
436 break;
438 case 0x32: /* InRange */
439 switch (field->physical & 0xff) {
440 case 0x21: map_key(BTN_TOOL_MOUSE); break;
441 case 0x22: map_key(BTN_TOOL_FINGER); break;
442 default: map_key(BTN_TOOL_PEN); break;
444 break;
446 case 0x3c: /* Invert */
447 map_key_clear(BTN_TOOL_RUBBER);
448 break;
450 case 0x33: /* Touch */
451 case 0x42: /* TipSwitch */
452 case 0x43: /* TipSwitch2 */
453 device->quirks &= ~HID_QUIRK_NOTOUCH;
454 map_key_clear(BTN_TOUCH);
455 break;
457 case 0x44: /* BarrelSwitch */
458 map_key_clear(BTN_STYLUS);
459 break;
461 case 0x46: /* TabletPick */
462 map_key_clear(BTN_STYLUS2);
463 break;
465 default: goto unknown;
467 break;
469 case HID_UP_CONSUMER: /* USB HUT v1.1, pages 56-62 */
470 switch (usage->hid & HID_USAGE) {
471 case 0x000: goto ignore;
472 case 0x034: map_key_clear(KEY_SLEEP); break;
473 case 0x036: map_key_clear(BTN_MISC); break;
475 case 0x040: map_key_clear(KEY_MENU); break;
476 case 0x045: map_key_clear(KEY_RADIO); break;
478 case 0x083: map_key_clear(KEY_LAST); break;
479 case 0x088: map_key_clear(KEY_PC); break;
480 case 0x089: map_key_clear(KEY_TV); break;
481 case 0x08a: map_key_clear(KEY_WWW); break;
482 case 0x08b: map_key_clear(KEY_DVD); break;
483 case 0x08c: map_key_clear(KEY_PHONE); break;
484 case 0x08d: map_key_clear(KEY_PROGRAM); break;
485 case 0x08e: map_key_clear(KEY_VIDEOPHONE); break;
486 case 0x08f: map_key_clear(KEY_GAMES); break;
487 case 0x090: map_key_clear(KEY_MEMO); break;
488 case 0x091: map_key_clear(KEY_CD); break;
489 case 0x092: map_key_clear(KEY_VCR); break;
490 case 0x093: map_key_clear(KEY_TUNER); break;
491 case 0x094: map_key_clear(KEY_EXIT); break;
492 case 0x095: map_key_clear(KEY_HELP); break;
493 case 0x096: map_key_clear(KEY_TAPE); break;
494 case 0x097: map_key_clear(KEY_TV2); break;
495 case 0x098: map_key_clear(KEY_SAT); break;
496 case 0x09a: map_key_clear(KEY_PVR); break;
498 case 0x09c: map_key_clear(KEY_CHANNELUP); break;
499 case 0x09d: map_key_clear(KEY_CHANNELDOWN); break;
500 case 0x0a0: map_key_clear(KEY_VCR2); break;
502 case 0x0b0: map_key_clear(KEY_PLAY); break;
503 case 0x0b1: map_key_clear(KEY_PAUSE); break;
504 case 0x0b2: map_key_clear(KEY_RECORD); break;
505 case 0x0b3: map_key_clear(KEY_FASTFORWARD); break;
506 case 0x0b4: map_key_clear(KEY_REWIND); break;
507 case 0x0b5: map_key_clear(KEY_NEXTSONG); break;
508 case 0x0b6: map_key_clear(KEY_PREVIOUSSONG); break;
509 case 0x0b7: map_key_clear(KEY_STOPCD); break;
510 case 0x0b8: map_key_clear(KEY_EJECTCD); break;
511 case 0x0bc: map_key_clear(KEY_MEDIA_REPEAT); break;
513 case 0x0cd: map_key_clear(KEY_PLAYPAUSE); break;
514 case 0x0e0: map_abs_clear(ABS_VOLUME); break;
515 case 0x0e2: map_key_clear(KEY_MUTE); break;
516 case 0x0e5: map_key_clear(KEY_BASSBOOST); break;
517 case 0x0e9: map_key_clear(KEY_VOLUMEUP); break;
518 case 0x0ea: map_key_clear(KEY_VOLUMEDOWN); break;
520 case 0x182: map_key_clear(KEY_BOOKMARKS); break;
521 case 0x183: map_key_clear(KEY_CONFIG); break;
522 case 0x184: map_key_clear(KEY_WORDPROCESSOR); break;
523 case 0x185: map_key_clear(KEY_EDITOR); break;
524 case 0x186: map_key_clear(KEY_SPREADSHEET); break;
525 case 0x187: map_key_clear(KEY_GRAPHICSEDITOR); break;
526 case 0x188: map_key_clear(KEY_PRESENTATION); break;
527 case 0x189: map_key_clear(KEY_DATABASE); break;
528 case 0x18a: map_key_clear(KEY_MAIL); break;
529 case 0x18b: map_key_clear(KEY_NEWS); break;
530 case 0x18c: map_key_clear(KEY_VOICEMAIL); break;
531 case 0x18d: map_key_clear(KEY_ADDRESSBOOK); break;
532 case 0x18e: map_key_clear(KEY_CALENDAR); break;
533 case 0x191: map_key_clear(KEY_FINANCE); break;
534 case 0x192: map_key_clear(KEY_CALC); break;
535 case 0x194: map_key_clear(KEY_FILE); break;
536 case 0x196: map_key_clear(KEY_WWW); break;
537 case 0x199: map_key_clear(KEY_CHAT); break;
538 case 0x19c: map_key_clear(KEY_LOGOFF); break;
539 case 0x19e: map_key_clear(KEY_COFFEE); break;
540 case 0x1a6: map_key_clear(KEY_HELP); break;
541 case 0x1a7: map_key_clear(KEY_DOCUMENTS); break;
542 case 0x1ab: map_key_clear(KEY_SPELLCHECK); break;
543 case 0x1b6: map_key_clear(KEY_MEDIA); break;
544 case 0x1b7: map_key_clear(KEY_SOUND); break;
545 case 0x1bc: map_key_clear(KEY_MESSENGER); break;
546 case 0x1bd: map_key_clear(KEY_INFO); break;
547 case 0x201: map_key_clear(KEY_NEW); break;
548 case 0x202: map_key_clear(KEY_OPEN); break;
549 case 0x203: map_key_clear(KEY_CLOSE); break;
550 case 0x204: map_key_clear(KEY_EXIT); break;
551 case 0x207: map_key_clear(KEY_SAVE); break;
552 case 0x208: map_key_clear(KEY_PRINT); break;
553 case 0x209: map_key_clear(KEY_PROPS); break;
554 case 0x21a: map_key_clear(KEY_UNDO); break;
555 case 0x21b: map_key_clear(KEY_COPY); break;
556 case 0x21c: map_key_clear(KEY_CUT); break;
557 case 0x21d: map_key_clear(KEY_PASTE); break;
558 case 0x21f: map_key_clear(KEY_FIND); break;
559 case 0x221: map_key_clear(KEY_SEARCH); break;
560 case 0x222: map_key_clear(KEY_GOTO); break;
561 case 0x223: map_key_clear(KEY_HOMEPAGE); break;
562 case 0x224: map_key_clear(KEY_BACK); break;
563 case 0x225: map_key_clear(KEY_FORWARD); break;
564 case 0x226: map_key_clear(KEY_STOP); break;
565 case 0x227: map_key_clear(KEY_REFRESH); break;
566 case 0x22a: map_key_clear(KEY_BOOKMARKS); break;
567 case 0x22d: map_key_clear(KEY_ZOOMIN); break;
568 case 0x22e: map_key_clear(KEY_ZOOMOUT); break;
569 case 0x22f: map_key_clear(KEY_ZOOMRESET); break;
570 case 0x233: map_key_clear(KEY_SCROLLUP); break;
571 case 0x234: map_key_clear(KEY_SCROLLDOWN); break;
572 case 0x238: map_rel(REL_HWHEEL); break;
573 case 0x25f: map_key_clear(KEY_CANCEL); break;
574 case 0x279: map_key_clear(KEY_REDO); break;
576 case 0x289: map_key_clear(KEY_REPLY); break;
577 case 0x28b: map_key_clear(KEY_FORWARDMAIL); break;
578 case 0x28c: map_key_clear(KEY_SEND); break;
580 default: goto ignore;
582 break;
584 case HID_UP_HPVENDOR: /* Reported on a Dutch layout HP5308 */
585 set_bit(EV_REP, input->evbit);
586 switch (usage->hid & HID_USAGE) {
587 case 0x021: map_key_clear(KEY_PRINT); break;
588 case 0x070: map_key_clear(KEY_HP); break;
589 case 0x071: map_key_clear(KEY_CAMERA); break;
590 case 0x072: map_key_clear(KEY_SOUND); break;
591 case 0x073: map_key_clear(KEY_QUESTION); break;
592 case 0x080: map_key_clear(KEY_EMAIL); break;
593 case 0x081: map_key_clear(KEY_CHAT); break;
594 case 0x082: map_key_clear(KEY_SEARCH); break;
595 case 0x083: map_key_clear(KEY_CONNECT); break;
596 case 0x084: map_key_clear(KEY_FINANCE); break;
597 case 0x085: map_key_clear(KEY_SPORT); break;
598 case 0x086: map_key_clear(KEY_SHOP); break;
599 default: goto ignore;
601 break;
603 case HID_UP_MSVENDOR:
604 goto ignore;
606 case HID_UP_CUSTOM: /* Reported on Logitech and Apple USB keyboards */
607 set_bit(EV_REP, input->evbit);
608 goto ignore;
610 case HID_UP_LOGIVENDOR:
611 goto ignore;
613 case HID_UP_PID:
614 switch (usage->hid & HID_USAGE) {
615 case 0xa4: map_key_clear(BTN_DEAD); break;
616 default: goto ignore;
618 break;
620 default:
621 unknown:
622 if (field->report_size == 1) {
623 if (field->report->type == HID_OUTPUT_REPORT) {
624 map_led(LED_MISC);
625 break;
627 map_key(BTN_MISC);
628 break;
630 if (field->flags & HID_MAIN_ITEM_RELATIVE) {
631 map_rel(REL_MISC);
632 break;
634 map_abs(ABS_MISC);
635 break;
638 mapped:
639 if (device->driver->input_mapped && device->driver->input_mapped(device,
640 hidinput, field, usage, &bit, &max) < 0)
641 goto ignore;
643 set_bit(usage->type, input->evbit);
645 while (usage->code <= max && test_and_set_bit(usage->code, bit))
646 usage->code = find_next_zero_bit(bit, max + 1, usage->code);
648 if (usage->code > max)
649 goto ignore;
652 if (usage->type == EV_ABS) {
654 int a = field->logical_minimum;
655 int b = field->logical_maximum;
657 if ((device->quirks & HID_QUIRK_BADPAD) && (usage->code == ABS_X || usage->code == ABS_Y)) {
658 a = field->logical_minimum = 0;
659 b = field->logical_maximum = 255;
662 if (field->application == HID_GD_GAMEPAD || field->application == HID_GD_JOYSTICK)
663 input_set_abs_params(input, usage->code, a, b, (b - a) >> 8, (b - a) >> 4);
664 else input_set_abs_params(input, usage->code, a, b, 0, 0);
666 input_abs_set_res(input, usage->code,
667 hidinput_calc_abs_res(field, usage->code));
669 /* use a larger default input buffer for MT devices */
670 if (usage->code == ABS_MT_POSITION_X && input->hint_events_per_packet == 0)
671 input_set_events_per_packet(input, 60);
674 if (usage->type == EV_ABS &&
675 (usage->hat_min < usage->hat_max || usage->hat_dir)) {
676 int i;
677 for (i = usage->code; i < usage->code + 2 && i <= max; i++) {
678 input_set_abs_params(input, i, -1, 1, 0, 0);
679 set_bit(i, input->absbit);
681 if (usage->hat_dir && !field->dpad)
682 field->dpad = usage->code;
685 /* for those devices which produce Consumer volume usage as relative,
686 * we emulate pressing volumeup/volumedown appropriate number of times
687 * in hidinput_hid_event()
689 if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
690 (usage->code == ABS_VOLUME)) {
691 set_bit(KEY_VOLUMEUP, input->keybit);
692 set_bit(KEY_VOLUMEDOWN, input->keybit);
695 if (usage->type == EV_KEY) {
696 set_bit(EV_MSC, input->evbit);
697 set_bit(MSC_SCAN, input->mscbit);
700 ignore:
701 return;
705 void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
707 struct input_dev *input;
708 unsigned *quirks = &hid->quirks;
710 if (!field->hidinput)
711 return;
713 input = field->hidinput->input;
715 if (!usage->type)
716 return;
718 if (usage->hat_min < usage->hat_max || usage->hat_dir) {
719 int hat_dir = usage->hat_dir;
720 if (!hat_dir)
721 hat_dir = (value - usage->hat_min) * 8 / (usage->hat_max - usage->hat_min + 1) + 1;
722 if (hat_dir < 0 || hat_dir > 8) hat_dir = 0;
723 input_event(input, usage->type, usage->code , hid_hat_to_axis[hat_dir].x);
724 input_event(input, usage->type, usage->code + 1, hid_hat_to_axis[hat_dir].y);
725 return;
728 if (usage->hid == (HID_UP_DIGITIZER | 0x003c)) { /* Invert */
729 *quirks = value ? (*quirks | HID_QUIRK_INVERT) : (*quirks & ~HID_QUIRK_INVERT);
730 return;
733 if (usage->hid == (HID_UP_DIGITIZER | 0x0032)) { /* InRange */
734 if (value) {
735 input_event(input, usage->type, (*quirks & HID_QUIRK_INVERT) ? BTN_TOOL_RUBBER : usage->code, 1);
736 return;
738 input_event(input, usage->type, usage->code, 0);
739 input_event(input, usage->type, BTN_TOOL_RUBBER, 0);
740 return;
743 if (usage->hid == (HID_UP_DIGITIZER | 0x0030) && (*quirks & HID_QUIRK_NOTOUCH)) { /* Pressure */
744 int a = field->logical_minimum;
745 int b = field->logical_maximum;
746 input_event(input, EV_KEY, BTN_TOUCH, value > a + ((b - a) >> 3));
749 if (usage->hid == (HID_UP_PID | 0x83UL)) { /* Simultaneous Effects Max */
750 dbg_hid("Maximum Effects - %d\n",value);
751 return;
754 if (usage->hid == (HID_UP_PID | 0x7fUL)) {
755 dbg_hid("PID Pool Report\n");
756 return;
759 if ((usage->type == EV_KEY) && (usage->code == 0)) /* Key 0 is "unassigned", not KEY_UNKNOWN */
760 return;
762 if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
763 (usage->code == ABS_VOLUME)) {
764 int count = abs(value);
765 int direction = value > 0 ? KEY_VOLUMEUP : KEY_VOLUMEDOWN;
766 int i;
768 for (i = 0; i < count; i++) {
769 input_event(input, EV_KEY, direction, 1);
770 input_sync(input);
771 input_event(input, EV_KEY, direction, 0);
772 input_sync(input);
774 return;
777 /* report the usage code as scancode if the key status has changed */
778 if (usage->type == EV_KEY && !!test_bit(usage->code, input->key) != value)
779 input_event(input, EV_MSC, MSC_SCAN, usage->hid);
781 input_event(input, usage->type, usage->code, value);
783 if ((field->flags & HID_MAIN_ITEM_RELATIVE) && (usage->type == EV_KEY))
784 input_event(input, usage->type, usage->code, 0);
787 void hidinput_report_event(struct hid_device *hid, struct hid_report *report)
789 struct hid_input *hidinput;
791 if (hid->quirks & HID_QUIRK_NO_INPUT_SYNC)
792 return;
794 list_for_each_entry(hidinput, &hid->inputs, list)
795 input_sync(hidinput->input);
797 EXPORT_SYMBOL_GPL(hidinput_report_event);
799 int hidinput_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field)
801 struct hid_report *report;
802 int i, j;
804 list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
805 for (i = 0; i < report->maxfield; i++) {
806 *field = report->field[i];
807 for (j = 0; j < (*field)->maxusage; j++)
808 if ((*field)->usage[j].type == type && (*field)->usage[j].code == code)
809 return j;
812 return -1;
814 EXPORT_SYMBOL_GPL(hidinput_find_field);
816 static int hidinput_open(struct input_dev *dev)
818 struct hid_device *hid = input_get_drvdata(dev);
820 return hid_hw_open(hid);
823 static void hidinput_close(struct input_dev *dev)
825 struct hid_device *hid = input_get_drvdata(dev);
827 hid_hw_close(hid);
830 static void report_features(struct hid_device *hid)
832 struct hid_driver *drv = hid->driver;
833 struct hid_report_enum *rep_enum;
834 struct hid_report *rep;
835 int i, j;
837 if (!drv->feature_mapping)
838 return;
840 rep_enum = &hid->report_enum[HID_FEATURE_REPORT];
841 list_for_each_entry(rep, &rep_enum->report_list, list)
842 for (i = 0; i < rep->maxfield; i++)
843 for (j = 0; j < rep->field[i]->maxusage; j++)
844 drv->feature_mapping(hid, rep->field[i],
845 rep->field[i]->usage + j);
849 * Register the input device; print a message.
850 * Configure the input layer interface
851 * Read all reports and initialize the absolute field values.
854 int hidinput_connect(struct hid_device *hid, unsigned int force)
856 struct hid_report *report;
857 struct hid_input *hidinput = NULL;
858 struct input_dev *input_dev;
859 int i, j, k;
861 INIT_LIST_HEAD(&hid->inputs);
863 if (!force) {
864 for (i = 0; i < hid->maxcollection; i++) {
865 struct hid_collection *col = &hid->collection[i];
866 if (col->type == HID_COLLECTION_APPLICATION ||
867 col->type == HID_COLLECTION_PHYSICAL)
868 if (IS_INPUT_APPLICATION(col->usage))
869 break;
872 if (i == hid->maxcollection)
873 return -1;
876 report_features(hid);
878 for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
879 if (k == HID_OUTPUT_REPORT &&
880 hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORTS)
881 continue;
883 list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
885 if (!report->maxfield)
886 continue;
888 if (!hidinput) {
889 hidinput = kzalloc(sizeof(*hidinput), GFP_KERNEL);
890 input_dev = input_allocate_device();
891 if (!hidinput || !input_dev) {
892 kfree(hidinput);
893 input_free_device(input_dev);
894 hid_err(hid, "Out of memory during hid input probe\n");
895 goto out_unwind;
898 input_set_drvdata(input_dev, hid);
899 input_dev->event =
900 hid->ll_driver->hidinput_input_event;
901 input_dev->open = hidinput_open;
902 input_dev->close = hidinput_close;
903 input_dev->setkeycode = hidinput_setkeycode;
904 input_dev->getkeycode = hidinput_getkeycode;
906 input_dev->name = hid->name;
907 input_dev->phys = hid->phys;
908 input_dev->uniq = hid->uniq;
909 input_dev->id.bustype = hid->bus;
910 input_dev->id.vendor = hid->vendor;
911 input_dev->id.product = hid->product;
912 input_dev->id.version = hid->version;
913 input_dev->dev.parent = hid->dev.parent;
914 hidinput->input = input_dev;
915 list_add_tail(&hidinput->list, &hid->inputs);
918 for (i = 0; i < report->maxfield; i++)
919 for (j = 0; j < report->field[i]->maxusage; j++)
920 hidinput_configure_usage(hidinput, report->field[i],
921 report->field[i]->usage + j);
923 if (hid->quirks & HID_QUIRK_MULTI_INPUT) {
924 /* This will leave hidinput NULL, so that it
925 * allocates another one if we have more inputs on
926 * the same interface. Some devices (e.g. Happ's
927 * UGCI) cram a lot of unrelated inputs into the
928 * same interface. */
929 hidinput->report = report;
930 if (input_register_device(hidinput->input))
931 goto out_cleanup;
932 hidinput = NULL;
937 if (hidinput && input_register_device(hidinput->input))
938 goto out_cleanup;
940 return 0;
942 out_cleanup:
943 list_del(&hidinput->list);
944 input_free_device(hidinput->input);
945 kfree(hidinput);
946 out_unwind:
947 /* unwind the ones we already registered */
948 hidinput_disconnect(hid);
950 return -1;
952 EXPORT_SYMBOL_GPL(hidinput_connect);
954 void hidinput_disconnect(struct hid_device *hid)
956 struct hid_input *hidinput, *next;
958 list_for_each_entry_safe(hidinput, next, &hid->inputs, list) {
959 list_del(&hidinput->list);
960 input_unregister_device(hidinput->input);
961 kfree(hidinput);
964 EXPORT_SYMBOL_GPL(hidinput_disconnect);