hid-input/battery: remove apparently redundant kmalloc
[linux-2.6.git] / drivers / hid / hid-input.c
1 /*
2  *  Copyright (c) 2000-2001 Vojtech Pavlik
3  *  Copyright (c) 2006-2010 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,111,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,111,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
71 static bool match_scancode(struct hid_usage *usage,
72                            unsigned int cur_idx, unsigned int scancode)
73 {
74         return (usage->hid & (HID_USAGE_PAGE | HID_USAGE)) == scancode;
75 }
76
77 static bool match_keycode(struct hid_usage *usage,
78                           unsigned int cur_idx, unsigned int keycode)
79 {
80         /*
81          * We should exclude unmapped usages when doing lookup by keycode.
82          */
83         return (usage->type == EV_KEY && usage->code == keycode);
84 }
85
86 static bool match_index(struct hid_usage *usage,
87                         unsigned int cur_idx, unsigned int idx)
88 {
89         return cur_idx == idx;
90 }
91
92 typedef bool (*hid_usage_cmp_t)(struct hid_usage *usage,
93                                 unsigned int cur_idx, unsigned int val);
94
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)
99 {
100         unsigned int i, j, k, cur_idx = 0;
101         struct hid_report *report;
102         struct hid_usage *usage;
103
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;
114                                                 }
115                                                 cur_idx++;
116                                         }
117                                 }
118                         }
119                 }
120         }
121         return NULL;
122 }
123
124 static struct hid_usage *hidinput_locate_usage(struct hid_device *hid,
125                                         const struct input_keymap_entry *ke,
126                                         unsigned int *index)
127 {
128         struct hid_usage *usage;
129         unsigned int scancode;
130
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;
137
138         return usage;
139 }
140
141 static int hidinput_getkeycode(struct input_dev *dev,
142                                struct input_keymap_entry *ke)
143 {
144         struct hid_device *hid = input_get_drvdata(dev);
145         struct hid_usage *usage;
146         unsigned int scancode, index;
147
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;
157         }
158
159         return -EINVAL;
160 }
161
162 static int hidinput_setkeycode(struct input_dev *dev,
163                                const struct input_keymap_entry *ke,
164                                unsigned int *old_keycode)
165 {
166         struct hid_device *hid = input_get_drvdata(dev);
167         struct hid_usage *usage;
168
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;
174
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);
179
180                 /*
181                  * Set the keybit for the old keycode if the old keycode is used
182                  * by another key
183                  */
184                 if (hidinput_find_key(hid, match_keycode, *old_keycode, NULL))
185                         set_bit(*old_keycode, dev->keybit);
186
187                 return 0;
188         }
189
190         return -EINVAL;
191 }
192
193
194 /**
195  * hidinput_calc_abs_res - calculate an absolute axis resolution
196  * @field: the HID report field to calculate resolution for
197  * @code: axis code
198  *
199  * The formula is:
200  *                         (logical_maximum - logical_minimum)
201  * resolution = ----------------------------------------------------------
202  *              (physical_maximum - physical_minimum) * 10 ^ unit_exponent
203  *
204  * as seen in the HID specification v1.11 6.2.2.7 Global Items.
205  *
206  * Only exponent 1 length units are processed. Centimeters and inches are
207  * converted to millimeters. Degrees are converted to radians.
208  */
209 static __s32 hidinput_calc_abs_res(const struct hid_field *field, __u16 code)
210 {
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;
217
218         /* Check if the extents are sane */
219         if (logical_extents <= 0 || physical_extents <= 0)
220                 return 0;
221
222         /*
223          * Verify and convert units.
224          * See HID specification v1.11 6.2.2.7 Global Items for unit decoding
225          */
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;
239                 }
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;
250                 }
251         } else {
252                 return 0;
253         }
254
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;
261         }
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;
268         }
269
270         /* Calculate resolution */
271         return logical_extents / physical_extents;
272 }
273
274 #ifdef CONFIG_HID_BATTERY_STRENGTH
275 static enum power_supply_property hidinput_battery_props[] = {
276         POWER_SUPPLY_PROP_PRESENT,
277         POWER_SUPPLY_PROP_ONLINE,
278         POWER_SUPPLY_PROP_CAPACITY,
279         POWER_SUPPLY_PROP_MODEL_NAME,
280         POWER_SUPPLY_PROP_STATUS
281 };
282
283 static int hidinput_get_battery_property(struct power_supply *psy,
284                                          enum power_supply_property prop,
285                                          union power_supply_propval *val)
286 {
287         struct hid_device *dev = container_of(psy, struct hid_device, battery);
288         int ret = 0;
289         int ret_rep;
290         __u8 buf[2] = {};
291         unsigned char report_number = dev->battery_report_id;
292
293         switch (prop) {
294         case POWER_SUPPLY_PROP_PRESENT:
295         case POWER_SUPPLY_PROP_ONLINE:
296                 val->intval = 1;
297                 break;
298
299         case POWER_SUPPLY_PROP_CAPACITY:
300                 ret_rep = dev->hid_get_raw_report(dev, report_number,
301                                                   buf, sizeof(buf), HID_FEATURE_REPORT);
302                 if (ret_rep != 2) {
303                         ret = -EINVAL;
304                         break;
305                 }
306
307                 /* store the returned value */
308                 /* I'm not calculating this using the logical_minimum and maximum */
309                 /* because my device returns 0-100 even though the min and max are 0-255 */
310                 val->intval = buf[1];
311                 break;
312
313         case POWER_SUPPLY_PROP_MODEL_NAME:
314                 val->strval = dev->name;
315                 break;
316
317         case POWER_SUPPLY_PROP_STATUS:
318                 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
319                 break;
320
321         default:
322                 ret = -EINVAL;
323                 break;
324         }
325
326         return ret;
327 }
328
329 static void hidinput_setup_battery(struct hid_device *dev, unsigned id, s32 min, s32 max)
330 {
331         struct power_supply *battery = &dev->battery;
332         int ret;
333
334         if (battery->name != NULL)
335                 return;         /* already initialized? */
336
337         battery->name = kasprintf(GFP_KERNEL, "hid-%s-battery", dev->uniq);
338         if (battery->name == NULL)
339                 return;
340
341         battery->type = POWER_SUPPLY_TYPE_USB;
342         battery->properties = hidinput_battery_props;
343         battery->num_properties = ARRAY_SIZE(hidinput_battery_props);
344         battery->use_for_apm = 0;
345         battery->get_property = hidinput_get_battery_property;
346
347         dev->battery_min = min;
348         dev->battery_max = max;
349         dev->battery_report_id = id;
350
351         ret = power_supply_register(&dev->dev, battery);
352         if (ret != 0) {
353                 hid_warn(dev, "can't register power supply: %d\n", ret);
354                 kfree(battery->name);
355                 battery->name = NULL;
356         }
357 }
358
359 static void hidinput_cleanup_battery(struct hid_device *dev)
360 {
361         if (!dev->battery.name)
362                 return;
363
364         power_supply_unregister(&dev->battery);
365         kfree(dev->battery.name);
366         dev->battery.name = NULL;
367 }
368 #else  /* !CONFIG_HID_BATTERY_STRENGTH */
369 static void hidinput_setup_battery(struct hid_device *dev, unsigned id, s32 min, s32 max)
370 {
371 }
372
373 static void hidinput_cleanup_battery(struct hid_device *dev)
374 {
375 }
376 #endif  /* CONFIG_HID_BATTERY_STRENGTH */
377
378 static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_field *field,
379                                      struct hid_usage *usage)
380 {
381         struct input_dev *input = hidinput->input;
382         struct hid_device *device = input_get_drvdata(input);
383         int max = 0, code;
384         unsigned long *bit = NULL;
385
386         field->hidinput = hidinput;
387
388         if (field->flags & HID_MAIN_ITEM_CONSTANT)
389                 goto ignore;
390
391         /* only LED usages are supported in output fields */
392         if (field->report_type == HID_OUTPUT_REPORT &&
393                         (usage->hid & HID_USAGE_PAGE) != HID_UP_LED) {
394                 goto ignore;
395         }
396
397         if (device->driver->input_mapping) {
398                 int ret = device->driver->input_mapping(device, hidinput, field,
399                                 usage, &bit, &max);
400                 if (ret > 0)
401                         goto mapped;
402                 if (ret < 0)
403                         goto ignore;
404         }
405
406         switch (usage->hid & HID_USAGE_PAGE) {
407         case HID_UP_UNDEFINED:
408                 goto ignore;
409
410         case HID_UP_KEYBOARD:
411                 set_bit(EV_REP, input->evbit);
412
413                 if ((usage->hid & HID_USAGE) < 256) {
414                         if (!hid_keyboard[usage->hid & HID_USAGE]) goto ignore;
415                         map_key_clear(hid_keyboard[usage->hid & HID_USAGE]);
416                 } else
417                         map_key(KEY_UNKNOWN);
418
419                 break;
420
421         case HID_UP_BUTTON:
422                 code = ((usage->hid - 1) & HID_USAGE);
423
424                 switch (field->application) {
425                 case HID_GD_MOUSE:
426                 case HID_GD_POINTER:  code += BTN_MOUSE; break;
427                 case HID_GD_JOYSTICK:
428                                 if (code <= 0xf)
429                                         code += BTN_JOYSTICK;
430                                 else
431                                         code += BTN_TRIGGER_HAPPY;
432                                 break;
433                 case HID_GD_GAMEPAD:  code += BTN_GAMEPAD; break;
434                 default:
435                         switch (field->physical) {
436                         case HID_GD_MOUSE:
437                         case HID_GD_POINTER:  code += BTN_MOUSE; break;
438                         case HID_GD_JOYSTICK: code += BTN_JOYSTICK; break;
439                         case HID_GD_GAMEPAD:  code += BTN_GAMEPAD; break;
440                         default:              code += BTN_MISC;
441                         }
442                 }
443
444                 map_key(code);
445                 break;
446
447         case HID_UP_SIMULATION:
448                 switch (usage->hid & 0xffff) {
449                 case 0xba: map_abs(ABS_RUDDER);   break;
450                 case 0xbb: map_abs(ABS_THROTTLE); break;
451                 case 0xc4: map_abs(ABS_GAS);      break;
452                 case 0xc5: map_abs(ABS_BRAKE);    break;
453                 case 0xc8: map_abs(ABS_WHEEL);    break;
454                 default:   goto ignore;
455                 }
456                 break;
457
458         case HID_UP_GENDESK:
459                 if ((usage->hid & 0xf0) == 0x80) {      /* SystemControl */
460                         switch (usage->hid & 0xf) {
461                         case 0x1: map_key_clear(KEY_POWER);  break;
462                         case 0x2: map_key_clear(KEY_SLEEP);  break;
463                         case 0x3: map_key_clear(KEY_WAKEUP); break;
464                         case 0x4: map_key_clear(KEY_CONTEXT_MENU); break;
465                         case 0x5: map_key_clear(KEY_MENU); break;
466                         case 0x6: map_key_clear(KEY_PROG1); break;
467                         case 0x7: map_key_clear(KEY_HELP); break;
468                         case 0x8: map_key_clear(KEY_EXIT); break;
469                         case 0x9: map_key_clear(KEY_SELECT); break;
470                         case 0xa: map_key_clear(KEY_RIGHT); break;
471                         case 0xb: map_key_clear(KEY_LEFT); break;
472                         case 0xc: map_key_clear(KEY_UP); break;
473                         case 0xd: map_key_clear(KEY_DOWN); break;
474                         case 0xe: map_key_clear(KEY_POWER2); break;
475                         case 0xf: map_key_clear(KEY_RESTART); break;
476                         default: goto unknown;
477                         }
478                         break;
479                 }
480
481                 if ((usage->hid & 0xf0) == 0x90) {      /* D-pad */
482                         switch (usage->hid) {
483                         case HID_GD_UP:    usage->hat_dir = 1; break;
484                         case HID_GD_DOWN:  usage->hat_dir = 5; break;
485                         case HID_GD_RIGHT: usage->hat_dir = 3; break;
486                         case HID_GD_LEFT:  usage->hat_dir = 7; break;
487                         default: goto unknown;
488                         }
489                         if (field->dpad) {
490                                 map_abs(field->dpad);
491                                 goto ignore;
492                         }
493                         map_abs(ABS_HAT0X);
494                         break;
495                 }
496
497                 switch (usage->hid) {
498                 /* These usage IDs map directly to the usage codes. */
499                 case HID_GD_X: case HID_GD_Y: case HID_GD_Z:
500                 case HID_GD_RX: case HID_GD_RY: case HID_GD_RZ:
501                 case HID_GD_SLIDER: case HID_GD_DIAL: case HID_GD_WHEEL:
502                         if (field->flags & HID_MAIN_ITEM_RELATIVE)
503                                 map_rel(usage->hid & 0xf);
504                         else
505                                 map_abs(usage->hid & 0xf);
506                         break;
507
508                 case HID_GD_HATSWITCH:
509                         usage->hat_min = field->logical_minimum;
510                         usage->hat_max = field->logical_maximum;
511                         map_abs(ABS_HAT0X);
512                         break;
513
514                 case HID_GD_START:      map_key_clear(BTN_START);       break;
515                 case HID_GD_SELECT:     map_key_clear(BTN_SELECT);      break;
516
517                 default: goto unknown;
518                 }
519
520                 break;
521
522         case HID_UP_LED:
523                 switch (usage->hid & 0xffff) {                /* HID-Value:                   */
524                 case 0x01:  map_led (LED_NUML);     break;    /*   "Num Lock"                 */
525                 case 0x02:  map_led (LED_CAPSL);    break;    /*   "Caps Lock"                */
526                 case 0x03:  map_led (LED_SCROLLL);  break;    /*   "Scroll Lock"              */
527                 case 0x04:  map_led (LED_COMPOSE);  break;    /*   "Compose"                  */
528                 case 0x05:  map_led (LED_KANA);     break;    /*   "Kana"                     */
529                 case 0x27:  map_led (LED_SLEEP);    break;    /*   "Stand-By"                 */
530                 case 0x4c:  map_led (LED_SUSPEND);  break;    /*   "System Suspend"           */
531                 case 0x09:  map_led (LED_MUTE);     break;    /*   "Mute"                     */
532                 case 0x4b:  map_led (LED_MISC);     break;    /*   "Generic Indicator"        */
533                 case 0x19:  map_led (LED_MAIL);     break;    /*   "Message Waiting"          */
534                 case 0x4d:  map_led (LED_CHARGING); break;    /*   "External Power Connected" */
535
536                 default: goto ignore;
537                 }
538                 break;
539
540         case HID_UP_DIGITIZER:
541                 switch (usage->hid & 0xff) {
542                 case 0x00: /* Undefined */
543                         goto ignore;
544
545                 case 0x30: /* TipPressure */
546                         if (!test_bit(BTN_TOUCH, input->keybit)) {
547                                 device->quirks |= HID_QUIRK_NOTOUCH;
548                                 set_bit(EV_KEY, input->evbit);
549                                 set_bit(BTN_TOUCH, input->keybit);
550                         }
551                         map_abs_clear(ABS_PRESSURE);
552                         break;
553
554                 case 0x32: /* InRange */
555                         switch (field->physical & 0xff) {
556                         case 0x21: map_key(BTN_TOOL_MOUSE); break;
557                         case 0x22: map_key(BTN_TOOL_FINGER); break;
558                         default: map_key(BTN_TOOL_PEN); break;
559                         }
560                         break;
561
562                 case 0x3c: /* Invert */
563                         map_key_clear(BTN_TOOL_RUBBER);
564                         break;
565
566                 case 0x33: /* Touch */
567                 case 0x42: /* TipSwitch */
568                 case 0x43: /* TipSwitch2 */
569                         device->quirks &= ~HID_QUIRK_NOTOUCH;
570                         map_key_clear(BTN_TOUCH);
571                         break;
572
573                 case 0x44: /* BarrelSwitch */
574                         map_key_clear(BTN_STYLUS);
575                         break;
576
577                 case 0x46: /* TabletPick */
578                         map_key_clear(BTN_STYLUS2);
579                         break;
580
581                 case 0x51: /* ContactID */
582                         device->quirks |= HID_QUIRK_MULTITOUCH;
583                         goto unknown;
584
585                 default:  goto unknown;
586                 }
587                 break;
588
589         case HID_UP_CONSUMER:   /* USB HUT v1.12, pages 75-84 */
590                 switch (usage->hid & HID_USAGE) {
591                 case 0x000: goto ignore;
592                 case 0x030: map_key_clear(KEY_POWER);           break;
593                 case 0x031: map_key_clear(KEY_RESTART);         break;
594                 case 0x032: map_key_clear(KEY_SLEEP);           break;
595                 case 0x034: map_key_clear(KEY_SLEEP);           break;
596                 case 0x035: map_key_clear(KEY_KBDILLUMTOGGLE);  break;
597                 case 0x036: map_key_clear(BTN_MISC);            break;
598
599                 case 0x040: map_key_clear(KEY_MENU);            break; /* Menu */
600                 case 0x041: map_key_clear(KEY_SELECT);          break; /* Menu Pick */
601                 case 0x042: map_key_clear(KEY_UP);              break; /* Menu Up */
602                 case 0x043: map_key_clear(KEY_DOWN);            break; /* Menu Down */
603                 case 0x044: map_key_clear(KEY_LEFT);            break; /* Menu Left */
604                 case 0x045: map_key_clear(KEY_RIGHT);           break; /* Menu Right */
605                 case 0x046: map_key_clear(KEY_ESC);             break; /* Menu Escape */
606                 case 0x047: map_key_clear(KEY_KPPLUS);          break; /* Menu Value Increase */
607                 case 0x048: map_key_clear(KEY_KPMINUS);         break; /* Menu Value Decrease */
608
609                 case 0x060: map_key_clear(KEY_INFO);            break; /* Data On Screen */
610                 case 0x061: map_key_clear(KEY_SUBTITLE);        break; /* Closed Caption */
611                 case 0x063: map_key_clear(KEY_VCR);             break; /* VCR/TV */
612                 case 0x065: map_key_clear(KEY_CAMERA);          break; /* Snapshot */
613                 case 0x069: map_key_clear(KEY_RED);             break;
614                 case 0x06a: map_key_clear(KEY_GREEN);           break;
615                 case 0x06b: map_key_clear(KEY_BLUE);            break;
616                 case 0x06c: map_key_clear(KEY_YELLOW);          break;
617                 case 0x06d: map_key_clear(KEY_ZOOM);            break;
618
619                 case 0x082: map_key_clear(KEY_VIDEO_NEXT);      break;
620                 case 0x083: map_key_clear(KEY_LAST);            break;
621                 case 0x084: map_key_clear(KEY_ENTER);           break;
622                 case 0x088: map_key_clear(KEY_PC);              break;
623                 case 0x089: map_key_clear(KEY_TV);              break;
624                 case 0x08a: map_key_clear(KEY_WWW);             break;
625                 case 0x08b: map_key_clear(KEY_DVD);             break;
626                 case 0x08c: map_key_clear(KEY_PHONE);           break;
627                 case 0x08d: map_key_clear(KEY_PROGRAM);         break;
628                 case 0x08e: map_key_clear(KEY_VIDEOPHONE);      break;
629                 case 0x08f: map_key_clear(KEY_GAMES);           break;
630                 case 0x090: map_key_clear(KEY_MEMO);            break;
631                 case 0x091: map_key_clear(KEY_CD);              break;
632                 case 0x092: map_key_clear(KEY_VCR);             break;
633                 case 0x093: map_key_clear(KEY_TUNER);           break;
634                 case 0x094: map_key_clear(KEY_EXIT);            break;
635                 case 0x095: map_key_clear(KEY_HELP);            break;
636                 case 0x096: map_key_clear(KEY_TAPE);            break;
637                 case 0x097: map_key_clear(KEY_TV2);             break;
638                 case 0x098: map_key_clear(KEY_SAT);             break;
639                 case 0x09a: map_key_clear(KEY_PVR);             break;
640
641                 case 0x09c: map_key_clear(KEY_CHANNELUP);       break;
642                 case 0x09d: map_key_clear(KEY_CHANNELDOWN);     break;
643                 case 0x0a0: map_key_clear(KEY_VCR2);            break;
644
645                 case 0x0b0: map_key_clear(KEY_PLAY);            break;
646                 case 0x0b1: map_key_clear(KEY_PAUSE);           break;
647                 case 0x0b2: map_key_clear(KEY_RECORD);          break;
648                 case 0x0b3: map_key_clear(KEY_FASTFORWARD);     break;
649                 case 0x0b4: map_key_clear(KEY_REWIND);          break;
650                 case 0x0b5: map_key_clear(KEY_NEXTSONG);        break;
651                 case 0x0b6: map_key_clear(KEY_PREVIOUSSONG);    break;
652                 case 0x0b7: map_key_clear(KEY_STOPCD);          break;
653                 case 0x0b8: map_key_clear(KEY_EJECTCD);         break;
654                 case 0x0bc: map_key_clear(KEY_MEDIA_REPEAT);    break;
655                 case 0x0b9: map_key_clear(KEY_SHUFFLE);         break;
656                 case 0x0bf: map_key_clear(KEY_SLOW);            break;
657
658                 case 0x0cd: map_key_clear(KEY_PLAYPAUSE);       break;
659                 case 0x0e0: map_abs_clear(ABS_VOLUME);          break;
660                 case 0x0e2: map_key_clear(KEY_MUTE);            break;
661                 case 0x0e5: map_key_clear(KEY_BASSBOOST);       break;
662                 case 0x0e9: map_key_clear(KEY_VOLUMEUP);        break;
663                 case 0x0ea: map_key_clear(KEY_VOLUMEDOWN);      break;
664                 case 0x0f5: map_key_clear(KEY_SLOW);            break;
665
666                 case 0x182: map_key_clear(KEY_BOOKMARKS);       break;
667                 case 0x183: map_key_clear(KEY_CONFIG);          break;
668                 case 0x184: map_key_clear(KEY_WORDPROCESSOR);   break;
669                 case 0x185: map_key_clear(KEY_EDITOR);          break;
670                 case 0x186: map_key_clear(KEY_SPREADSHEET);     break;
671                 case 0x187: map_key_clear(KEY_GRAPHICSEDITOR);  break;
672                 case 0x188: map_key_clear(KEY_PRESENTATION);    break;
673                 case 0x189: map_key_clear(KEY_DATABASE);        break;
674                 case 0x18a: map_key_clear(KEY_MAIL);            break;
675                 case 0x18b: map_key_clear(KEY_NEWS);            break;
676                 case 0x18c: map_key_clear(KEY_VOICEMAIL);       break;
677                 case 0x18d: map_key_clear(KEY_ADDRESSBOOK);     break;
678                 case 0x18e: map_key_clear(KEY_CALENDAR);        break;
679                 case 0x191: map_key_clear(KEY_FINANCE);         break;
680                 case 0x192: map_key_clear(KEY_CALC);            break;
681                 case 0x193: map_key_clear(KEY_PLAYER);          break;
682                 case 0x194: map_key_clear(KEY_FILE);            break;
683                 case 0x196: map_key_clear(KEY_WWW);             break;
684                 case 0x199: map_key_clear(KEY_CHAT);            break;
685                 case 0x19c: map_key_clear(KEY_LOGOFF);          break;
686                 case 0x19e: map_key_clear(KEY_COFFEE);          break;
687                 case 0x1a6: map_key_clear(KEY_HELP);            break;
688                 case 0x1a7: map_key_clear(KEY_DOCUMENTS);       break;
689                 case 0x1ab: map_key_clear(KEY_SPELLCHECK);      break;
690                 case 0x1ae: map_key_clear(KEY_KEYBOARD);        break;
691                 case 0x1b6: map_key_clear(KEY_IMAGES);          break;
692                 case 0x1b7: map_key_clear(KEY_AUDIO);           break;
693                 case 0x1b8: map_key_clear(KEY_VIDEO);           break;
694                 case 0x1bc: map_key_clear(KEY_MESSENGER);       break;
695                 case 0x1bd: map_key_clear(KEY_INFO);            break;
696                 case 0x201: map_key_clear(KEY_NEW);             break;
697                 case 0x202: map_key_clear(KEY_OPEN);            break;
698                 case 0x203: map_key_clear(KEY_CLOSE);           break;
699                 case 0x204: map_key_clear(KEY_EXIT);            break;
700                 case 0x207: map_key_clear(KEY_SAVE);            break;
701                 case 0x208: map_key_clear(KEY_PRINT);           break;
702                 case 0x209: map_key_clear(KEY_PROPS);           break;
703                 case 0x21a: map_key_clear(KEY_UNDO);            break;
704                 case 0x21b: map_key_clear(KEY_COPY);            break;
705                 case 0x21c: map_key_clear(KEY_CUT);             break;
706                 case 0x21d: map_key_clear(KEY_PASTE);           break;
707                 case 0x21f: map_key_clear(KEY_FIND);            break;
708                 case 0x221: map_key_clear(KEY_SEARCH);          break;
709                 case 0x222: map_key_clear(KEY_GOTO);            break;
710                 case 0x223: map_key_clear(KEY_HOMEPAGE);        break;
711                 case 0x224: map_key_clear(KEY_BACK);            break;
712                 case 0x225: map_key_clear(KEY_FORWARD);         break;
713                 case 0x226: map_key_clear(KEY_STOP);            break;
714                 case 0x227: map_key_clear(KEY_REFRESH);         break;
715                 case 0x22a: map_key_clear(KEY_BOOKMARKS);       break;
716                 case 0x22d: map_key_clear(KEY_ZOOMIN);          break;
717                 case 0x22e: map_key_clear(KEY_ZOOMOUT);         break;
718                 case 0x22f: map_key_clear(KEY_ZOOMRESET);       break;
719                 case 0x233: map_key_clear(KEY_SCROLLUP);        break;
720                 case 0x234: map_key_clear(KEY_SCROLLDOWN);      break;
721                 case 0x238: map_rel(REL_HWHEEL);                break;
722                 case 0x23d: map_key_clear(KEY_EDIT);            break;
723                 case 0x25f: map_key_clear(KEY_CANCEL);          break;
724                 case 0x269: map_key_clear(KEY_INSERT);          break;
725                 case 0x26a: map_key_clear(KEY_DELETE);          break;
726                 case 0x279: map_key_clear(KEY_REDO);            break;
727
728                 case 0x289: map_key_clear(KEY_REPLY);           break;
729                 case 0x28b: map_key_clear(KEY_FORWARDMAIL);     break;
730                 case 0x28c: map_key_clear(KEY_SEND);            break;
731
732                 default:    goto ignore;
733                 }
734                 break;
735
736         case HID_UP_GENDEVCTRLS:
737                 if ((usage->hid & HID_USAGE) == 0x20) { /* Battery Strength */
738                         hidinput_setup_battery(device,
739                                                field->report->id,
740                                                field->logical_minimum,
741                                                field->logical_maximum);
742                         goto ignore;
743                 } else
744                         goto unknown;
745                 break;
746
747         case HID_UP_HPVENDOR:   /* Reported on a Dutch layout HP5308 */
748                 set_bit(EV_REP, input->evbit);
749                 switch (usage->hid & HID_USAGE) {
750                 case 0x021: map_key_clear(KEY_PRINT);           break;
751                 case 0x070: map_key_clear(KEY_HP);              break;
752                 case 0x071: map_key_clear(KEY_CAMERA);          break;
753                 case 0x072: map_key_clear(KEY_SOUND);           break;
754                 case 0x073: map_key_clear(KEY_QUESTION);        break;
755                 case 0x080: map_key_clear(KEY_EMAIL);           break;
756                 case 0x081: map_key_clear(KEY_CHAT);            break;
757                 case 0x082: map_key_clear(KEY_SEARCH);          break;
758                 case 0x083: map_key_clear(KEY_CONNECT);         break;
759                 case 0x084: map_key_clear(KEY_FINANCE);         break;
760                 case 0x085: map_key_clear(KEY_SPORT);           break;
761                 case 0x086: map_key_clear(KEY_SHOP);            break;
762                 default:    goto ignore;
763                 }
764                 break;
765
766         case HID_UP_MSVENDOR:
767                 goto ignore;
768
769         case HID_UP_CUSTOM: /* Reported on Logitech and Apple USB keyboards */
770                 set_bit(EV_REP, input->evbit);
771                 goto ignore;
772
773         case HID_UP_LOGIVENDOR:
774                 goto ignore;
775
776         case HID_UP_PID:
777                 switch (usage->hid & HID_USAGE) {
778                 case 0xa4: map_key_clear(BTN_DEAD);     break;
779                 default: goto ignore;
780                 }
781                 break;
782
783         default:
784         unknown:
785                 if (field->report_size == 1) {
786                         if (field->report->type == HID_OUTPUT_REPORT) {
787                                 map_led(LED_MISC);
788                                 break;
789                         }
790                         map_key(BTN_MISC);
791                         break;
792                 }
793                 if (field->flags & HID_MAIN_ITEM_RELATIVE) {
794                         map_rel(REL_MISC);
795                         break;
796                 }
797                 map_abs(ABS_MISC);
798                 break;
799         }
800
801 mapped:
802         if (device->driver->input_mapped && device->driver->input_mapped(device,
803                                 hidinput, field, usage, &bit, &max) < 0)
804                 goto ignore;
805
806         set_bit(usage->type, input->evbit);
807
808         while (usage->code <= max && test_and_set_bit(usage->code, bit))
809                 usage->code = find_next_zero_bit(bit, max + 1, usage->code);
810
811         if (usage->code > max)
812                 goto ignore;
813
814
815         if (usage->type == EV_ABS) {
816
817                 int a = field->logical_minimum;
818                 int b = field->logical_maximum;
819
820                 if ((device->quirks & HID_QUIRK_BADPAD) && (usage->code == ABS_X || usage->code == ABS_Y)) {
821                         a = field->logical_minimum = 0;
822                         b = field->logical_maximum = 255;
823                 }
824
825                 if (field->application == HID_GD_GAMEPAD || field->application == HID_GD_JOYSTICK)
826                         input_set_abs_params(input, usage->code, a, b, (b - a) >> 8, (b - a) >> 4);
827                 else    input_set_abs_params(input, usage->code, a, b, 0, 0);
828
829                 input_abs_set_res(input, usage->code,
830                                   hidinput_calc_abs_res(field, usage->code));
831
832                 /* use a larger default input buffer for MT devices */
833                 if (usage->code == ABS_MT_POSITION_X && input->hint_events_per_packet == 0)
834                         input_set_events_per_packet(input, 60);
835         }
836
837         if (usage->type == EV_ABS &&
838             (usage->hat_min < usage->hat_max || usage->hat_dir)) {
839                 int i;
840                 for (i = usage->code; i < usage->code + 2 && i <= max; i++) {
841                         input_set_abs_params(input, i, -1, 1, 0, 0);
842                         set_bit(i, input->absbit);
843                 }
844                 if (usage->hat_dir && !field->dpad)
845                         field->dpad = usage->code;
846         }
847
848         /* for those devices which produce Consumer volume usage as relative,
849          * we emulate pressing volumeup/volumedown appropriate number of times
850          * in hidinput_hid_event()
851          */
852         if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
853                         (usage->code == ABS_VOLUME)) {
854                 set_bit(KEY_VOLUMEUP, input->keybit);
855                 set_bit(KEY_VOLUMEDOWN, input->keybit);
856         }
857
858         if (usage->type == EV_KEY) {
859                 set_bit(EV_MSC, input->evbit);
860                 set_bit(MSC_SCAN, input->mscbit);
861         }
862
863 ignore:
864         return;
865
866 }
867
868 void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value)
869 {
870         struct input_dev *input;
871         unsigned *quirks = &hid->quirks;
872
873         if (!field->hidinput)
874                 return;
875
876         input = field->hidinput->input;
877
878 #ifdef CONFIG_HID_BATTERY_STRENGTH
879         if (usage->hid == HID_DC_BATTERYSTRENGTH) {
880                 hid->battery_val = value;
881                 hid_dbg(hid, "battery value is %d (range %d-%d)\n",
882                         value, hid->battery_min, hid->battery_max);
883                 return;
884         }
885 #endif
886         if (!usage->type)
887                 return;
888
889         if (usage->hat_min < usage->hat_max || usage->hat_dir) {
890                 int hat_dir = usage->hat_dir;
891                 if (!hat_dir)
892                         hat_dir = (value - usage->hat_min) * 8 / (usage->hat_max - usage->hat_min + 1) + 1;
893                 if (hat_dir < 0 || hat_dir > 8) hat_dir = 0;
894                 input_event(input, usage->type, usage->code    , hid_hat_to_axis[hat_dir].x);
895                 input_event(input, usage->type, usage->code + 1, hid_hat_to_axis[hat_dir].y);
896                 return;
897         }
898
899         if (usage->hid == (HID_UP_DIGITIZER | 0x003c)) { /* Invert */
900                 *quirks = value ? (*quirks | HID_QUIRK_INVERT) : (*quirks & ~HID_QUIRK_INVERT);
901                 return;
902         }
903
904         if (usage->hid == (HID_UP_DIGITIZER | 0x0032)) { /* InRange */
905                 if (value) {
906                         input_event(input, usage->type, (*quirks & HID_QUIRK_INVERT) ? BTN_TOOL_RUBBER : usage->code, 1);
907                         return;
908                 }
909                 input_event(input, usage->type, usage->code, 0);
910                 input_event(input, usage->type, BTN_TOOL_RUBBER, 0);
911                 return;
912         }
913
914         if (usage->hid == (HID_UP_DIGITIZER | 0x0030) && (*quirks & HID_QUIRK_NOTOUCH)) { /* Pressure */
915                 int a = field->logical_minimum;
916                 int b = field->logical_maximum;
917                 input_event(input, EV_KEY, BTN_TOUCH, value > a + ((b - a) >> 3));
918         }
919
920         if (usage->hid == (HID_UP_PID | 0x83UL)) { /* Simultaneous Effects Max */
921                 dbg_hid("Maximum Effects - %d\n",value);
922                 return;
923         }
924
925         if (usage->hid == (HID_UP_PID | 0x7fUL)) {
926                 dbg_hid("PID Pool Report\n");
927                 return;
928         }
929
930         if ((usage->type == EV_KEY) && (usage->code == 0)) /* Key 0 is "unassigned", not KEY_UNKNOWN */
931                 return;
932
933         if ((usage->type == EV_ABS) && (field->flags & HID_MAIN_ITEM_RELATIVE) &&
934                         (usage->code == ABS_VOLUME)) {
935                 int count = abs(value);
936                 int direction = value > 0 ? KEY_VOLUMEUP : KEY_VOLUMEDOWN;
937                 int i;
938
939                 for (i = 0; i < count; i++) {
940                         input_event(input, EV_KEY, direction, 1);
941                         input_sync(input);
942                         input_event(input, EV_KEY, direction, 0);
943                         input_sync(input);
944                 }
945                 return;
946         }
947
948         /* Ignore out-of-range values as per HID specification, section 5.10 */
949         if (value < field->logical_minimum || value > field->logical_maximum) {
950                 dbg_hid("Ignoring out-of-range value %x\n", value);
951                 return;
952         }
953
954         /* report the usage code as scancode if the key status has changed */
955         if (usage->type == EV_KEY && !!test_bit(usage->code, input->key) != value)
956                 input_event(input, EV_MSC, MSC_SCAN, usage->hid);
957
958         input_event(input, usage->type, usage->code, value);
959
960         if ((field->flags & HID_MAIN_ITEM_RELATIVE) && (usage->type == EV_KEY))
961                 input_event(input, usage->type, usage->code, 0);
962 }
963
964 void hidinput_report_event(struct hid_device *hid, struct hid_report *report)
965 {
966         struct hid_input *hidinput;
967
968         if (hid->quirks & HID_QUIRK_NO_INPUT_SYNC)
969                 return;
970
971         list_for_each_entry(hidinput, &hid->inputs, list)
972                 input_sync(hidinput->input);
973 }
974 EXPORT_SYMBOL_GPL(hidinput_report_event);
975
976 int hidinput_find_field(struct hid_device *hid, unsigned int type, unsigned int code, struct hid_field **field)
977 {
978         struct hid_report *report;
979         int i, j;
980
981         list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
982                 for (i = 0; i < report->maxfield; i++) {
983                         *field = report->field[i];
984                         for (j = 0; j < (*field)->maxusage; j++)
985                                 if ((*field)->usage[j].type == type && (*field)->usage[j].code == code)
986                                         return j;
987                 }
988         }
989         return -1;
990 }
991 EXPORT_SYMBOL_GPL(hidinput_find_field);
992
993 static int hidinput_open(struct input_dev *dev)
994 {
995         struct hid_device *hid = input_get_drvdata(dev);
996
997         return hid_hw_open(hid);
998 }
999
1000 static void hidinput_close(struct input_dev *dev)
1001 {
1002         struct hid_device *hid = input_get_drvdata(dev);
1003
1004         hid_hw_close(hid);
1005 }
1006
1007 static void report_features(struct hid_device *hid)
1008 {
1009         struct hid_driver *drv = hid->driver;
1010         struct hid_report_enum *rep_enum;
1011         struct hid_report *rep;
1012         int i, j;
1013
1014         rep_enum = &hid->report_enum[HID_FEATURE_REPORT];
1015         list_for_each_entry(rep, &rep_enum->report_list, list)
1016                 for (i = 0; i < rep->maxfield; i++)
1017                         for (j = 0; j < rep->field[i]->maxusage; j++) {
1018                                 /* Verify if Battery Strength feature is available */
1019                                 if (((rep->field[i]->usage + j)->hid & HID_USAGE_PAGE) == HID_UP_GENDEVCTRLS &&
1020                                         ((rep->field[i]->usage + j)->hid & HID_USAGE) == 0x20) {
1021                                         hidinput_setup_battery(hid,
1022                                                                rep->id,
1023                                                                rep->field[i]->logical_minimum,
1024                                                                rep->field[i]->logical_maximum);
1025                                 }
1026
1027                                 if (drv->feature_mapping)
1028                                         drv->feature_mapping(hid, rep->field[i],
1029                                                              rep->field[i]->usage + j);
1030                         }
1031 }
1032
1033 /*
1034  * Register the input device; print a message.
1035  * Configure the input layer interface
1036  * Read all reports and initialize the absolute field values.
1037  */
1038
1039 int hidinput_connect(struct hid_device *hid, unsigned int force)
1040 {
1041         struct hid_report *report;
1042         struct hid_input *hidinput = NULL;
1043         struct input_dev *input_dev;
1044         int i, j, k;
1045
1046         INIT_LIST_HEAD(&hid->inputs);
1047
1048         if (!force) {
1049                 for (i = 0; i < hid->maxcollection; i++) {
1050                         struct hid_collection *col = &hid->collection[i];
1051                         if (col->type == HID_COLLECTION_APPLICATION ||
1052                                         col->type == HID_COLLECTION_PHYSICAL)
1053                                 if (IS_INPUT_APPLICATION(col->usage))
1054                                         break;
1055                 }
1056
1057                 if (i == hid->maxcollection)
1058                         return -1;
1059         }
1060
1061         report_features(hid);
1062
1063         for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
1064                 if (k == HID_OUTPUT_REPORT &&
1065                         hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORTS)
1066                         continue;
1067
1068                 list_for_each_entry(report, &hid->report_enum[k].report_list, list) {
1069
1070                         if (!report->maxfield)
1071                                 continue;
1072
1073                         if (!hidinput) {
1074                                 hidinput = kzalloc(sizeof(*hidinput), GFP_KERNEL);
1075                                 input_dev = input_allocate_device();
1076                                 if (!hidinput || !input_dev) {
1077                                         kfree(hidinput);
1078                                         input_free_device(input_dev);
1079                                         hid_err(hid, "Out of memory during hid input probe\n");
1080                                         goto out_unwind;
1081                                 }
1082
1083                                 input_set_drvdata(input_dev, hid);
1084                                 input_dev->event =
1085                                         hid->ll_driver->hidinput_input_event;
1086                                 input_dev->open = hidinput_open;
1087                                 input_dev->close = hidinput_close;
1088                                 input_dev->setkeycode = hidinput_setkeycode;
1089                                 input_dev->getkeycode = hidinput_getkeycode;
1090
1091                                 input_dev->name = hid->name;
1092                                 input_dev->phys = hid->phys;
1093                                 input_dev->uniq = hid->uniq;
1094                                 input_dev->id.bustype = hid->bus;
1095                                 input_dev->id.vendor  = hid->vendor;
1096                                 input_dev->id.product = hid->product;
1097                                 input_dev->id.version = hid->version;
1098                                 input_dev->dev.parent = hid->dev.parent;
1099                                 hidinput->input = input_dev;
1100                                 list_add_tail(&hidinput->list, &hid->inputs);
1101                         }
1102
1103                         for (i = 0; i < report->maxfield; i++)
1104                                 for (j = 0; j < report->field[i]->maxusage; j++)
1105                                         hidinput_configure_usage(hidinput, report->field[i],
1106                                                                  report->field[i]->usage + j);
1107
1108                         if (hid->quirks & HID_QUIRK_MULTI_INPUT) {
1109                                 /* This will leave hidinput NULL, so that it
1110                                  * allocates another one if we have more inputs on
1111                                  * the same interface. Some devices (e.g. Happ's
1112                                  * UGCI) cram a lot of unrelated inputs into the
1113                                  * same interface. */
1114                                 hidinput->report = report;
1115                                 if (input_register_device(hidinput->input))
1116                                         goto out_cleanup;
1117                                 hidinput = NULL;
1118                         }
1119                 }
1120         }
1121
1122         if (hid->quirks & HID_QUIRK_MULTITOUCH) {
1123                 /* generic hid does not know how to handle multitouch devices */
1124                 if (hidinput)
1125                         goto out_cleanup;
1126                 goto out_unwind;
1127         }
1128
1129         if (hidinput && input_register_device(hidinput->input))
1130                 goto out_cleanup;
1131
1132         return 0;
1133
1134 out_cleanup:
1135         list_del(&hidinput->list);
1136         input_free_device(hidinput->input);
1137         kfree(hidinput);
1138 out_unwind:
1139         /* unwind the ones we already registered */
1140         hidinput_disconnect(hid);
1141
1142         return -1;
1143 }
1144 EXPORT_SYMBOL_GPL(hidinput_connect);
1145
1146 void hidinput_disconnect(struct hid_device *hid)
1147 {
1148         struct hid_input *hidinput, *next;
1149
1150         hidinput_cleanup_battery(hid);
1151
1152         list_for_each_entry_safe(hidinput, next, &hid->inputs, list) {
1153                 list_del(&hidinput->list);
1154                 input_unregister_device(hidinput->input);
1155                 kfree(hidinput);
1156         }
1157 }
1158 EXPORT_SYMBOL_GPL(hidinput_disconnect);
1159