1 /*
2 * Copyright (c) 2000-2001 Vojtech Pavlik
3 * Copyright (c) 2006-2007 Jiri Kosina
4 *
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>
31
32 #include <linux/hid.h>
33 #include <linux/hid-debug.h>
34
35 #define unk KEY_UNKNOWN
36
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
54 };
55
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}};
60
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))
65
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))
70
match_scancode(int code,int scancode)71 static inline int match_scancode(int code, int scancode)
72 {
73 if (scancode == 0)
74 return 1;
75 return ((code & (HID_USAGE_PAGE | HID_USAGE)) == scancode);
76 }
77
match_keycode(int code,int keycode)78 static inline int match_keycode(int code, int keycode)
79 {
80 if (keycode == 0)
81 return 1;
82 return (code == keycode);
83 }
84
hidinput_find_key(struct hid_device * hid,int scancode,int keycode)85 static struct hid_usage *hidinput_find_key(struct hid_device *hid,
86 int scancode, int keycode)
87 {
88 int i, j, k;
89 struct hid_report *report;
90 struct hid_usage *usage;
91
92 for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
93 list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
94 for (i = 0; i < report->maxfield; i++) {
95 for ( j = 0; j < report->field[i]->maxusage; j++) {
96 usage = report->field[i]->usage + j;
97 if (usage->type == EV_KEY &&
98 match_scancode(usage->hid, scancode) &&
99 match_keycode(usage->code, keycode))
100 return usage;
101 }
102 }
103 }
104 }
105 return NULL;
106 }
107
hidinput_getkeycode(struct input_dev * dev,int scancode,int * keycode)108 static int hidinput_getkeycode(struct input_dev *dev, int scancode,
109 int *keycode)
110 {
111 struct hid_device *hid = input_get_drvdata(dev);
112 struct hid_usage *usage;
113
114 usage = hidinput_find_key(hid, scancode, 0);
115 if (usage) {
116 *keycode = usage->code;
117 return 0;
118 }
119 return -EINVAL;
120 }
121
hidinput_setkeycode(struct input_dev * dev,int scancode,int keycode)122 static int hidinput_setkeycode(struct input_dev *dev, int scancode,
123 int keycode)
124 {
125 struct hid_device *hid = input_get_drvdata(dev);
126 struct hid_usage *usage;
127 int old_keycode;
128
129 if (keycode < 0 || keycode > KEY_MAX)
130 return -EINVAL;
131
132 usage = hidinput_find_key(hid, scancode, 0);
133 if (usage) {
134 old_keycode = usage->code;
135 usage->code = keycode;
136
137 clear_bit(old_keycode, dev->keybit);
138 set_bit(usage->code, dev->keybit);
139 dbg_hid(KERN_DEBUG "Assigned keycode %d to HID usage code %x\n", keycode, scancode);
140 /* Set the keybit for the old keycode if the old keycode is used
141 * by another key */
142 if (hidinput_find_key (hid, 0, old_keycode))
143 set_bit(old_keycode, dev->keybit);
144
145 return 0;
146 }
147
148 return -EINVAL;
149 }
150
151
hidinput_configure_usage(struct hid_input * hidinput,struct hid_field * field,struct hid_usage * usage)152 static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_field *field,
153 struct hid_usage *usage)
154 {
155 struct input_dev *input = hidinput->input;
156 struct hid_device *device = input_get_drvdata(input);
157 int max = 0, code;
158 unsigned long *bit = NULL;
159
160 field->hidinput = hidinput;
161
162 dbg_hid("Mapping: ");
163 hid_resolv_usage(usage->hid);
164 dbg_hid_line(" ---> ");
165
166 if (field->flags & HID_MAIN_ITEM_CONSTANT)
167 goto ignore;
168
169 /* only LED usages are supported in output fields */
170 if (field->report_type == HID_OUTPUT_REPORT &&
171 (usage->hid & HID_USAGE_PAGE) != HID_UP_LED) {
172 dbg_hid_line(" [non-LED output field] ");
173 goto ignore;
174 }
175
176 if (device->driver->input_mapping) {
177 int ret = device->driver->input_mapping(device, hidinput, field,
178 usage, &bit, &max);
179 if (ret > 0)
180 goto mapped;
181 if (ret < 0)
182 goto ignore;
183 }
184
185 switch (usage->hid & HID_USAGE_PAGE) {
186 case HID_UP_UNDEFINED:
187 goto ignore;
188
189 case HID_UP_KEYBOARD:
190 set_bit(EV_REP, input->evbit);
191
192 if ((usage->hid & HID_USAGE) < 256) {
193 if (!hid_keyboard[usage->hid & HID_USAGE]) goto ignore;
194 map_key_clear(hid_keyboard[usage->hid & HID_USAGE]);
195 } else
196 map_key(KEY_UNKNOWN);
197
198 break;
199
200 case HID_UP_BUTTON:
201 code = ((usage->hid - 1) & 0xf);
202
203 switch (field->application) {
204 case HID_GD_MOUSE:
205 case HID_GD_POINTER: code += 0x110; break;
206 case HID_GD_JOYSTICK: code += 0x120; break;
207 case HID_GD_GAMEPAD: code += 0x130; break;
208 default:
209 switch (field->physical) {
210 case HID_GD_MOUSE:
211 case HID_GD_POINTER: code += 0x110; break;
212 case HID_GD_JOYSTICK: code += 0x120; break;
213 case HID_GD_GAMEPAD: code += 0x130; break;
214 default: code += 0x100;
215 }
216 }
217
218 map_key(code);
219 break;
220
221 case HID_UP_SIMULATION:
222 switch (usage->hid & 0xffff) {
223 case 0xba: map_abs(ABS_RUDDER); break;
224 case 0xbb: map_abs(ABS_THROTTLE); break;
225 case 0xc4: map_abs(ABS_GAS); break;
226 case 0xc5: map_abs(ABS_BRAKE); break;
227 case 0xc8: map_abs(ABS_WHEEL); break;
228 default: goto ignore;
229 }
230 break;
231
232 case HID_UP_GENDESK:
233 if ((usage->hid & 0xf0) == 0x80) { /* SystemControl */
234 switch (usage->hid & 0xf) {
235 case 0x1: map_key_clear(KEY_POWER); break;
236 case 0x2: map_key_clear(KEY_SLEEP); break;
237 case 0x3: map_key_clear(KEY_WAKEUP); break;
238 default: goto unknown;
239 }
240 break;
241 }
242
243 if ((usage->hid & 0xf0) == 0x90) { /* D-pad */
244 switch (usage->hid) {
245 case HID_GD_UP: usage->hat_dir = 1; break;
246 case HID_GD_DOWN: usage->hat_dir = 5; break;
247 case HID_GD_RIGHT: usage->hat_dir = 3; break;
248 case HID_GD_LEFT: usage->hat_dir = 7; break;
249 default: goto unknown;
250 }
251 if (field->dpad) {
252 map_abs(field->dpad);
253 goto ignore;
254 }
255 map_abs(ABS_HAT0X);
256 break;
257 }
258
259 switch (usage->hid) {
260 /* These usage IDs map directly to the usage codes. */
261 case HID_GD_X: case HID_GD_Y: case HID_GD_Z:
262 case HID_GD_RX: case HID_GD_RY: case HID_GD_RZ:
263 case HID_GD_SLIDER: case HID_GD_DIAL: case HID_GD_WHEEL:
264 if (field->flags & HID_MAIN_ITEM_RELATIVE)
265 map_rel(usage->hid & 0xf);
266 else
267 map_abs(usage->hid & 0xf);
268 break;
269
270 case HID_GD_HATSWITCH:
271 usage->hat_min = field->logical_minimum;
272 usage->hat_max = field->logical_maximum;
273 map_abs(ABS_HAT0X);
274 break;
275
276 case HID_GD_START: map_key_clear(BTN_START); break;
277 case HID_GD_SELECT: map_key_clear(BTN_SELECT); break;
278
279 default: goto unknown;
280 }
281
282 break;
283
284 case HID_UP_LED:
285 switch (usage->hid & 0xffff) { /* HID-Value: */
286 case 0x01: map_led (LED_NUML); break; /* "Num Lock" */
287 case 0x02: map_led (LED_CAPSL); break; /* "Caps Lock" */
288 case 0x03: map_led (LED_SCROLLL); break; /* "Scroll Lock" */
289 case 0x04: map_led (LED_COMPOSE); break; /* "Compose" */
290 case 0x05: map_led (LED_KANA); break; /* "Kana" */
291 case 0x27: map_led (LED_SLEEP); break; /* "Stand-By" */
292 case 0x4c: map_led (LED_SUSPEND); break; /* "System Suspend" */
293 case 0x09: map_led (LED_MUTE); break; /* "Mute" */
294 case 0x4b: map_led (LED_MISC); break; /* "Generic Indicator" */
295 case 0x19: map_led (LED_MAIL); break; /* "Message Waiting" */
296 case 0x4d: map_led (LED_CHARGING); break; /* "External Power Connected" */
297
298 default: goto ignore;
299 }
300 break;
301
302 case HID_UP_DIGITIZER:
303 switch (usage->hid & 0xff) {
304 case 0x30: /* TipPressure */
305 if (!test_bit(BTN_TOUCH, input->keybit)) {
306 device->quirks |= HID_QUIRK_NOTOUCH;
307 set_bit(EV_KEY, input->evbit);
308 set_bit(BTN_TOUCH, input->keybit);
309 }
310 map_abs_clear(ABS_PRESSURE);
311 break;
312
313 case 0x32: /* InRange */
314 switch (field->physical & 0xff) {
315 case 0x21: map_key(BTN_TOOL_MOUSE); break;
316 case 0x22: map_key(BTN_TOOL_FINGER); break;
317 default: map_key(BTN_TOOL_PEN); break;
318 }
319 break;
320
321 case 0x3c: /* Invert */
322 map_key_clear(BTN_TOOL_RUBBER);
323 break;
324
325 case 0x33: /* Touch */
326 case 0x42: /* TipSwitch */
327 case 0x43: /* TipSwitch2 */
328 device->quirks &= ~HID_QUIRK_NOTOUCH;
329 map_key_clear(BTN_TOUCH);
330 break;
331
332 case 0x44: /* BarrelSwitch */
333 map_key_clear(BTN_STYLUS);
334 break;
335
336 default: goto unknown;
337 }
338 break;
339
340 case HID_UP_CONSUMER: /* USB HUT v1.1, pages 56-62 */
341 switch (usage->hid & HID_USAGE) {
342 case 0x000: goto ignore;
343 case 0x034: map_key_clear(KEY_SLEEP); break;
344 case 0x036: map_key_clear(BTN_MISC); break;
345
346 case 0x040: map_key_clear(KEY_MENU); break;
347 case 0x045: map_key_clear(KEY_RADIO); break;
348
349 case 0x083: map_key_clear(KEY_LAST); break;
350 case 0x088: map_key_clear(KEY_PC); break;
351 case 0x089: map_key_clear(KEY_TV); break;
352 case 0x08a: map_key_clear(KEY_WWW); break;
353 case 0x08b: map_key_clear(KEY_DVD); break;
354 case 0x08c: map_key_clear(KEY_PHONE); break;
355 case 0x08d: map_key_clear(KEY_PROGRAM); break;
356 case 0x08e: map_key_clear(KEY_VIDEOPHONE); break;
357 case 0x08f: map_key_clear(KEY_GAMES); break;
358 case 0x090: map_key_clear(KEY_MEMO); break;
359 case 0x091: map_key_clear(KEY_CD); break;
360 case 0x092: map_key_clear(KEY_VCR); break;
361 case 0x093: map_key_clear(KEY_TUNER); break;
362 case 0x094: map_key_clear(KEY_EXIT); break;
363 case 0x095: map_key_clear(KEY_HELP); break;
364 case 0x096: map_key_clear(KEY_TAPE); break;
365 case 0x097: map_key_clear(KEY_TV2); break;
366 case 0x098: map_key_clear(KEY_SAT); break;
367 case 0x09a: map_key_clear(KEY_PVR); break;
368
369 case 0x09c: map_key_clear(KEY_CHANNELUP); break;
370 case 0x09d: map_key_clear(KEY_CHANNELDOWN); break;
371 case 0x0a0: map_key_clear(KEY_VCR2); break;
372
373 case 0x0b0: map_key_clear(KEY_PLAY); break;
374 case 0x0b1: map_key_clear(KEY_PAUSE); break;
375 case 0x0b2: map_key_clear(KEY_RECORD); break;
376 case 0x0b3: map_key_clear(KEY_FASTFORWARD); break;
377 case 0x0b4: map_key_clear(KEY_REWIND); break;
378 case 0x0b5: map_key_clear(KEY_NEXTSONG); break;
379 case 0x0b6: map_key_clear(KEY_PREVIOUSSONG); break;
380 case 0x0b7: map_key_clear(KEY_STOPCD); break;
381 case 0x0b8: map_key_clear(KEY_EJECTCD); break;
382 case 0x0bc: map_key_clear(KEY_MEDIA_REPEAT); break;
383
384 case 0x0cd: map_key_clear(KEY_PLAYPAUSE); break;
385 case 0x0e0: map_abs_clear(ABS_VOLUME); break;
386 case 0x0e2: map_key_clear(KEY_MUTE); break;
387 case 0x0e5: map_key_clear(KEY_BASSBOOST); break;
388 case 0x0e9: map_key_clear(KEY_VOLUMEUP); break;
389 case 0x0ea: map_key_clear(KEY_VOLUMEDOWN); break;
390
391 case 0x182: map_key_clear(KEY_BOOKMARKS); break;
392 case 0x183: map_key_clear(KEY_CONFIG); break;
393 case 0x184: map_key_clear(KEY_WORDPROCESSOR); break;
394 case 0x185: map_key_clear(KEY_EDITOR); break;
395 case 0x186: map_key_clear(KEY_SPREADSHEET); break;
396 case 0x187: map_key_clear(KEY_GRAPHICSEDITOR); break;
397 case 0x188: map_key_clear(KEY_PRESENTATION); break;
398 case 0x189: map_key_clear(KEY_DATABASE); break;
399 case 0x18a: map_key_clear(KEY_MAIL); break;
400 case 0x18b: map_key_clear(KEY_NEWS); break;
401 case 0x18c: map_key_clear(KEY_VOICEMAIL); break;
402 case 0x18d: map_key_clear(KEY_ADDRESSBOOK); break;
403 case 0x18e: map_key_clear(KEY_CALENDAR); break;
404 case 0x191: map_key_clear(KEY_FINANCE); break;
405 case 0x192: map_key_clear(KEY_CALC); break;
406 case 0x194: map_key_clear(KEY_FILE); break;
407 case 0x196: map_key_clear(KEY_WWW); break;
408 case 0x19c: map_key_clear(KEY_LOGOFF); break;
409 case 0x19e: map_key_clear(KEY_COFFEE); break;
410 case 0x1a6: map_key_clear(KEY_HELP); break;
411 case 0x1a7: map_key_clear(KEY_DOCUMENTS); break;
412 case 0x1ab: map_key_clear(KEY_SPELLCHECK); break;
413 case 0x1b6: map_key_clear(KEY_MEDIA); break;
414 case 0x1b7: map_key_clear(KEY_SOUND); break;
415 case 0x1bc: map_key_clear(KEY_MESSENGER); break;
416 case 0x1bd: map_key_clear(KEY_INFO); break;
417 case 0x201: map_key_clear(KEY_NEW); break;
418 case 0x202: map_key_clear(KEY_OPEN); break;
419 case 0x203: map_key_clear(KEY_CLOSE); break;
420 case 0x204: map_key_clear(KEY_EXIT); break;
421 case 0x207: map_key_clear(KEY_SAVE); break;
422 case 0x208: map_key_clear(KEY_PRINT); break;
423 case 0x209: map_key_clear(KEY_PROPS); break;
424 case 0x21a: map_key_clear(KEY_UNDO); break;
425 case 0x21b: map_key_clear(KEY_COPY); break;
426 case 0x21c: map_key_clear(KEY_CUT); break;
427 case 0x21d: map_key_clear(KEY_PASTE); break;
428 case 0x21f: map_key_clear(KEY_FIND); break;
429 case 0x221: map_key_clear(KEY_SEARCH); break;
430 case 0x222: map_key_clear(KEY_GOTO); break;
431 case 0x223: map_key_clear(KEY_HOMEPAGE); break;
432 case 0x224: map_key_clear(KEY_BACK); break;
433 case 0x225: map_key_clear(KEY_FORWARD); break;
434 case 0x226: map_key_clear(KEY_STOP); break;
435 case 0x227: map_key_clear(KEY_REFRESH); break;
436 case 0x22a: map_key_clear(KEY_BOOKMARKS); break;
437 case 0x22d: map_key_clear(KEY_ZOOMIN); break;
438 case 0x22e: map_key_clear(KEY_ZOOMOUT); break;
439 case 0x22f: map_key_clear(KEY_ZOOMRESET); break;
440 case 0x233: map_key_clear(KEY_SCROLLUP); break;
441 case 0x234: map_key_clear(KEY_SCROLLDOWN); break;
442 case 0x238: map_rel(REL_HWHEEL); break;
443 case 0x25f: map_key_clear(KEY_CANCEL); break;
444 case 0x279: map_key_clear(KEY_REDO); break;
445
446 case 0x289: map_key_clear(KEY_REPLY); break;
447 case 0x28b: map_key_clear(KEY_FORWARDMAIL); break;
448 case 0x28c: map_key_clear(KEY_SEND); break;
449
450 default: goto ignore;
451 }
452 break;
453
454 case HID_UP_HPVENDOR: /* Reported on a Dutch layout HP5308 */
455 set_bit(EV_REP, input->evbit);
456 switch (usage->hid & HID_USAGE) {
457 case 0x021: map_key_clear(KEY_PRINT); break;
458 case 0x070: map_key_clear(KEY_HP); break;
459 case 0x071: map_key_clear(KEY_CAMERA); break;
460 case 0x072: map_key_clear(KEY_SOUND); break;
461 case 0x073: map_key_clear(KEY_QUESTION); break;
462 case 0x080: map_key_clear(KEY_EMAIL); break;
463 case 0x081: map_key_clear(KEY_CHAT); break;
464 case 0x082: map_key_clear(KEY_SEARCH); break;
465 case 0x083: map_key_clear(KEY_CONNECT); break;
466 case 0x084: map_key_clear(KEY_FINANCE); break;
467 case 0x085: map_key_clear(KEY_SPORT); break;
468 case 0x086: map_key_clear(KEY_SHOP); break;
469 default: goto ignore;
470 }
471 break;
472
473 case HID_UP_MSVENDOR:
474 goto ignore;
475
476 case HID_UP_CUSTOM: /* Reported on Logitech and Apple USB keyboards */
477 set_bit(EV_REP, input->evbit);
478 goto ignore;
479
480 case HID_UP_LOGIVENDOR:
481 goto ignore;
482
483 case HID_UP_PID:
484 switch (usage->hid & HID_USAGE) {
485 case 0xa4: map_key_clear(BTN_DEAD); break;
486 default: goto ignore;
487 }
488 break;
489
490 default:
491 unknown:
492 if (field->report_size == 1) {
493 if (field->report->type == HID_OUTPUT_REPORT) {
494 map_led(LED_MISC);
495 break;
496 }
497 map_key(BTN_MISC);
498 break;
499 }
500 if (field->flags & HID_MAIN_ITEM_RELATIVE) {
501 map_rel(REL_MISC);
502 break;
503 }
504 map_abs(ABS_MISC);
505 break;
506 }
507
508 mapped:
509 if (device->driver->input_mapped && device->driver->input_mapped(device,
510 hidinput, field, usage, &bit, &max) < 0)
511 goto ignore;
512
513 set_bit(usage->type, input->evbit);
514
515 while (usage->code <= max && test_and_set_bit(usage->code, bit))
516 usage->code = find_next_zero_bit(bit, max + 1, usage->code);
517
518 if (usage->code > max)
519 goto ignore;
520
521
522 if (usage->type == EV_ABS) {
523
524 int a = field->logical_minimum;
525 int b = field->logical_maximum;
526
527 if ((device->quirks & HID_QUIRK_BADPAD) && (usage->code == ABS_X || usage->code == ABS_Y)) {
528 a = field->logical_minimum = 0;
529 b = field->logical_maximum = 255;
530 }
531
532 if (field->application == HID_GD_GAMEPAD || field->application == HID_GD_JOYSTICK)
533 input_set_abs_params(input, usage->code, a, b, (b - a) >> 8, (b - a) >> 4);
534 else input_set_abs_params(input, usage->code, a, b, 0, 0);
535
536 }
537
538 if (usage->type == EV_ABS &&
539 (usage->hat_min < usage->hat_max || usage->hat_dir)) {
540 int i;
541 for (i = usage->code; i < usage->code + 2 && i <= max; i++) {
542 input_set_abs_params(input, i, -1, 1, 0, 0);
543 set_bit(i, input->absbit);
544 }
545 if (usage->hat_dir && !field->dpad)
546 field->dpad = usage->code;
547 }
548
549 /* for those devices which produce Consumer volume usage as relative,
550 * we emulate pressing volumeup/volumedown appropriate number of times
551 * in hidinput_hid_event()
552 */
553 if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
554 (usage->code == ABS_VOLUME)) {
555 set_bit(KEY_VOLUMEUP, input->keybit);
556 set_bit(KEY_VOLUMEDOWN, input->keybit);
557 }
558
559 if (usage->type == EV_KEY) {
560 set_bit(EV_MSC, input->evbit);
561 set_bit(MSC_SCAN, input->mscbit);
562 }
563
564 hid_resolv_event(usage->type, usage->code);
565
566 dbg_hid_line("\n");
567
568 return;
569
570 ignore:
571 dbg_hid_line("IGNORED\n");
572 return;
573 }
574
hidinput_hid_event(struct hid_device * hid,struct hid_field * field,struct hid_usage * usage,__s32 value)575 void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
576 {
577 struct input_dev *input;
578 unsigned *quirks = &hid->quirks;
579
580 if (!field->hidinput)
581 return;
582
583 input = field->hidinput->input;
584
585 if (!usage->type)
586 return;
587
588 if (usage->hat_min < usage->hat_max || usage->hat_dir) {
589 int hat_dir = usage->hat_dir;
590 if (!hat_dir)
591 hat_dir = (value - usage->hat_min) * 8 / (usage->hat_max - usage->hat_min + 1) + 1;
592 if (hat_dir < 0 || hat_dir > 8) hat_dir = 0;
593 input_event(input, usage->type, usage->code , hid_hat_to_axis[hat_dir].x);
594 input_event(input, usage->type, usage->code + 1, hid_hat_to_axis[hat_dir].y);
595 return;
596 }
597
598 if (usage->hid == (HID_UP_DIGITIZER | 0x003c)) { /* Invert */
599 *quirks = value ? (*quirks | HID_QUIRK_INVERT) : (*quirks & ~HID_QUIRK_INVERT);
600 return;
601 }
602
603 if (usage->hid == (HID_UP_DIGITIZER | 0x0032)) { /* InRange */
604 if (value) {
605 input_event(input, usage->type, (*quirks & HID_QUIRK_INVERT) ? BTN_TOOL_RUBBER : usage->code, 1);
606 return;
607 }
608 input_event(input, usage->type, usage->code, 0);
609 input_event(input, usage->type, BTN_TOOL_RUBBER, 0);
610 return;
611 }
612
613 if (usage->hid == (HID_UP_DIGITIZER | 0x0030) && (*quirks & HID_QUIRK_NOTOUCH)) { /* Pressure */
614 int a = field->logical_minimum;
615 int b = field->logical_maximum;
616 input_event(input, EV_KEY, BTN_TOUCH, value > a + ((b - a) >> 3));
617 }
618
619 if (usage->hid == (HID_UP_PID | 0x83UL)) { /* Simultaneous Effects Max */
620 dbg_hid("Maximum Effects - %d\n",value);
621 return;
622 }
623
624 if (usage->hid == (HID_UP_PID | 0x7fUL)) {
625 dbg_hid("PID Pool Report\n");
626 return;
627 }
628
629 if ((usage->type == EV_KEY) && (usage->code == 0)) /* Key 0 is "unassigned", not KEY_UNKNOWN */
630 return;
631
632 if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
633 (usage->code == ABS_VOLUME)) {
634 int count = abs(value);
635 int direction = value > 0 ? KEY_VOLUMEUP : KEY_VOLUMEDOWN;
636 int i;
637
638 for (i = 0; i < count; i++) {
639 input_event(input, EV_KEY, direction, 1);
640 input_sync(input);
641 input_event(input, EV_KEY, direction, 0);
642 input_sync(input);
643 }
644 return;
645 }
646
647 /* report the usage code as scancode if the key status has changed */
648 if (usage->type == EV_KEY && !!test_bit(usage->code, input->key) != value)
649 input_event(input, EV_MSC, MSC_SCAN, usage->hid);
650
651 input_event(input, usage->type, usage->code, value);
652
653 if ((field->flags & HID_MAIN_ITEM_RELATIVE) && (usage->type == EV_KEY))
654 input_event(input, usage->type, usage->code, 0);
655 }
656
hidinput_report_event(struct hid_device * hid,struct hid_report * report)657 void hidinput_report_event(struct hid_device *hid, struct hid_report *report)
658 {
659 struct hid_input *hidinput;
660
661 list_for_each_entry(hidinput, &hid->inputs, list)
662 input_sync(hidinput->input);
663 }
664 EXPORT_SYMBOL_GPL(hidinput_report_event);
665
hidinput_find_field(struct hid_device * hid,unsigned int type,unsigned int code,struct hid_field ** field)666 int hidinput_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field)
667 {
668 struct hid_report *report;
669 int i, j;
670
671 list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
672 for (i = 0; i < report->maxfield; i++) {
673 *field = report->field[i];
674 for (j = 0; j < (*field)->maxusage; j++)
675 if ((*field)->usage[j].type == type && (*field)->usage[j].code == code)
676 return j;
677 }
678 }
679 return -1;
680 }
681 EXPORT_SYMBOL_GPL(hidinput_find_field);
682
hidinput_open(struct input_dev * dev)683 static int hidinput_open(struct input_dev *dev)
684 {
685 struct hid_device *hid = input_get_drvdata(dev);
686
687 return hid->ll_driver->open(hid);
688 }
689
hidinput_close(struct input_dev * dev)690 static void hidinput_close(struct input_dev *dev)
691 {
692 struct hid_device *hid = input_get_drvdata(dev);
693
694 hid->ll_driver->close(hid);
695 }
696
697 /*
698 * Register the input device; print a message.
699 * Configure the input layer interface
700 * Read all reports and initialize the absolute field values.
701 */
702
hidinput_connect(struct hid_device * hid,unsigned int force)703 int hidinput_connect(struct hid_device *hid, unsigned int force)
704 {
705 struct hid_report *report;
706 struct hid_input *hidinput = NULL;
707 struct input_dev *input_dev;
708 int i, j, k;
709 int max_report_type = HID_OUTPUT_REPORT;
710
711 INIT_LIST_HEAD(&hid->inputs);
712
713 if (!force) {
714 for (i = 0; i < hid->maxcollection; i++) {
715 struct hid_collection *col = &hid->collection[i];
716 if (col->type == HID_COLLECTION_APPLICATION ||
717 col->type == HID_COLLECTION_PHYSICAL)
718 if (IS_INPUT_APPLICATION(col->usage))
719 break;
720 }
721
722 if (i == hid->maxcollection)
723 return -1;
724 }
725
726 if (hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORTS)
727 max_report_type = HID_INPUT_REPORT;
728
729 for (k = HID_INPUT_REPORT; k <= max_report_type; k++)
730 list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
731
732 if (!report->maxfield)
733 continue;
734
735 if (!hidinput) {
736 hidinput = kzalloc(sizeof(*hidinput), GFP_KERNEL);
737 input_dev = input_allocate_device();
738 if (!hidinput || !input_dev) {
739 kfree(hidinput);
740 input_free_device(input_dev);
741 err_hid("Out of memory during hid input probe");
742 goto out_unwind;
743 }
744
745 input_set_drvdata(input_dev, hid);
746 input_dev->event =
747 hid->ll_driver->hidinput_input_event;
748 input_dev->open = hidinput_open;
749 input_dev->close = hidinput_close;
750 input_dev->setkeycode = hidinput_setkeycode;
751 input_dev->getkeycode = hidinput_getkeycode;
752
753 input_dev->name = hid->name;
754 input_dev->phys = hid->phys;
755 input_dev->uniq = hid->uniq;
756 input_dev->id.bustype = hid->bus;
757 input_dev->id.vendor = hid->vendor;
758 input_dev->id.product = hid->product;
759 input_dev->id.version = hid->version;
760 input_dev->dev.parent = hid->dev.parent;
761 hidinput->input = input_dev;
762 list_add_tail(&hidinput->list, &hid->inputs);
763 }
764
765 for (i = 0; i < report->maxfield; i++)
766 for (j = 0; j < report->field[i]->maxusage; j++)
767 hidinput_configure_usage(hidinput, report->field[i],
768 report->field[i]->usage + j);
769
770 if (hid->quirks & HID_QUIRK_MULTI_INPUT) {
771 /* This will leave hidinput NULL, so that it
772 * allocates another one if we have more inputs on
773 * the same interface. Some devices (e.g. Happ's
774 * UGCI) cram a lot of unrelated inputs into the
775 * same interface. */
776 hidinput->report = report;
777 if (input_register_device(hidinput->input))
778 goto out_cleanup;
779 hidinput = NULL;
780 }
781 }
782
783 if (hidinput && input_register_device(hidinput->input))
784 goto out_cleanup;
785
786 return 0;
787
788 out_cleanup:
789 input_free_device(hidinput->input);
790 kfree(hidinput);
791 out_unwind:
792 /* unwind the ones we already registered */
793 hidinput_disconnect(hid);
794
795 return -1;
796 }
797 EXPORT_SYMBOL_GPL(hidinput_connect);
798
hidinput_disconnect(struct hid_device * hid)799 void hidinput_disconnect(struct hid_device *hid)
800 {
801 struct hid_input *hidinput, *next;
802
803 list_for_each_entry_safe(hidinput, next, &hid->inputs, list) {
804 list_del(&hidinput->list);
805 input_unregister_device(hidinput->input);
806 kfree(hidinput);
807 }
808 }
809 EXPORT_SYMBOL_GPL(hidinput_disconnect);
810
811