ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / usb / input / hid-input.c
1 /*
2  * $Id: hid-input.c,v 1.2 2002/04/23 00:59:25 rdamazio Exp $
3  *
4  *  Copyright (c) 2000-2001 Vojtech Pavlik
5  *
6  *  USB HID to Linux Input mapping
7  */
8
9 /*
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  * Should you need to contact me, the author, you can do so either by
25  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
26  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
27  */
28
29 #include <linux/module.h>
30 #include <linux/slab.h>
31 #include <linux/kernel.h>
32 #include <linux/input.h>
33 #include <linux/usb.h>
34
35 #include "hid.h"
36
37 #define unk     KEY_UNKNOWN
38
39 static unsigned char hid_keyboard[256] = {
40           0,  0,  0,  0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
41          50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44,  2,  3,
42           4,  5,  6,  7,  8,  9, 10, 11, 28,  1, 14, 15, 57, 12, 13, 26,
43          27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
44          65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
45         105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
46          72, 73, 82, 83, 86,127,116,117,183,184,185,186,187,188,189,190,
47         191,192,193,194,134,138,130,132,128,129,131,137,133,135,136,113,
48         115,114,unk,unk,unk,121,unk, 89, 93,124, 92, 94, 95,unk,unk,unk,
49         122,123, 90, 91, 85,unk,unk,unk,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         unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
53         unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,unk,
54          29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
55         150,158,159,128,136,177,178,176,142,152,173,140,unk,unk,unk,unk
56 };
57
58 static struct {
59         __s32 x;
60         __s32 y;
61 }  hid_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
62
63 static struct input_dev *find_input(struct hid_device *hid, struct hid_field *field)
64 {
65         struct list_head *lh;
66         struct hid_input *hidinput;
67
68         list_for_each (lh, &hid->inputs) {
69                 int i;
70
71                 hidinput = list_entry(lh, struct hid_input, list);
72
73                 if (! hidinput->report)
74                         continue;
75
76                 for (i = 0; i < hidinput->report->maxfield; i++)
77                         if (hidinput->report->field[i] == field)
78                                 return &hidinput->input;
79         }
80
81         /* Assume we only have one input and use it */
82         if (!list_empty(&hid->inputs)) {
83                 hidinput = list_entry(hid->inputs.next, struct hid_input, list);
84                 return &hidinput->input;
85         }
86
87         /* This is really a bug */
88         return NULL;
89 }
90
91 static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_field *field,
92                                      struct hid_usage *usage)
93 {
94         struct input_dev *input = &hidinput->input;
95         struct hid_device *device = hidinput->input.private;
96         int max;
97         int is_abs = 0;
98         unsigned long *bit;
99
100         switch (usage->hid & HID_USAGE_PAGE) {
101
102                 case HID_UP_KEYBOARD:
103
104                         set_bit(EV_REP, input->evbit);
105                         usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
106
107                         if ((usage->hid & HID_USAGE) < 256) {
108                                 if (!(usage->code = hid_keyboard[usage->hid & HID_USAGE]))
109                                         return;
110                                 clear_bit(usage->code, bit);
111                         } else
112                                 usage->code = KEY_UNKNOWN;
113
114                         break;
115
116                 case HID_UP_BUTTON:
117
118                         usage->code = ((usage->hid - 1) & 0xf) + 0x100;
119                         usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
120
121                         switch (field->application) {
122                                 case HID_GD_GAMEPAD:  usage->code += 0x10;
123                                 case HID_GD_JOYSTICK: usage->code += 0x10;
124                                 case HID_GD_MOUSE:    usage->code += 0x10; break;
125                                 default:
126                                         if (field->physical == HID_GD_POINTER)
127                                                 usage->code += 0x10;
128                                         break;
129                         }
130                         break;
131
132                 case HID_UP_GENDESK:
133
134                         if ((usage->hid & 0xf0) == 0x80) {      /* SystemControl */
135                                 switch (usage->hid & 0xf) {
136                                         case 0x1: usage->code = KEY_POWER;  break;
137                                         case 0x2: usage->code = KEY_SLEEP;  break;
138                                         case 0x3: usage->code = KEY_WAKEUP; break;
139                                         default: usage->code = KEY_UNKNOWN; break;
140                                 }
141                                 usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
142                                 break;
143                         }
144
145                         usage->code = usage->hid & 0xf;
146
147                         if (field->report_size == 1) {
148                                 usage->code = BTN_MISC;
149                                 usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
150                                 break;
151                         }
152
153                         if (field->flags & HID_MAIN_ITEM_RELATIVE) {
154                                 usage->type = EV_REL; bit = input->relbit; max = REL_MAX;
155                                 break;
156                         }
157
158                         usage->type = EV_ABS; bit = input->absbit; max = ABS_MAX;
159
160                         if (usage->hid == HID_GD_HATSWITCH) {
161                                 usage->code = ABS_HAT0X;
162                                 usage->hat_min = field->logical_minimum;
163                                 usage->hat_max = field->logical_maximum;
164                         }
165                         break;
166
167                 case HID_UP_LED:
168
169                         usage->code = (usage->hid - 1) & 0xf;
170                         usage->type = EV_LED; bit = input->ledbit; max = LED_MAX;
171                         break;
172
173                 case HID_UP_DIGITIZER:
174
175                         switch (usage->hid & 0xff) {
176
177                                 case 0x30: /* TipPressure */
178
179                                         if (!test_bit(BTN_TOUCH, input->keybit)) {
180                                                 device->quirks |= HID_QUIRK_NOTOUCH;
181                                                 set_bit(EV_KEY, input->evbit);
182                                                 set_bit(BTN_TOUCH, input->keybit);
183                                         }
184                                         usage->type = EV_ABS; bit = input->absbit; max = ABS_MAX;
185                                         usage->code = ABS_PRESSURE;
186                                         clear_bit(usage->code, bit);
187                                         break;
188
189                                 case 0x32: /* InRange */
190
191                                         usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
192                                         switch (field->physical & 0xff) {
193                                                 case 0x21: usage->code = BTN_TOOL_MOUSE; break;
194                                                 case 0x22: usage->code = BTN_TOOL_FINGER; break;
195                                                 default: usage->code = BTN_TOOL_PEN; break;
196                                         }
197                                         break;
198
199                                 case 0x3c: /* Invert */
200
201                                         usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
202                                         usage->code = BTN_TOOL_RUBBER;
203                                         clear_bit(usage->code, bit);
204                                         break;
205
206                                 case 0x33: /* Touch */
207                                 case 0x42: /* TipSwitch */
208                                 case 0x43: /* TipSwitch2 */
209
210                                         device->quirks &= ~HID_QUIRK_NOTOUCH;
211                                         usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
212                                         usage->code = BTN_TOUCH;
213                                         clear_bit(usage->code, bit);
214                                         break;
215
216                                 case 0x44: /* BarrelSwitch */
217
218                                         usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
219                                         usage->code = BTN_STYLUS;
220                                         clear_bit(usage->code, bit);
221                                         break;
222
223                                 default:  goto unknown;
224                         }
225                         break;
226
227                 case HID_UP_CONSUMER:   /* USB HUT v1.1, pages 56-62 */
228
229                         set_bit(EV_REP, input->evbit);
230                         switch (usage->hid & HID_USAGE) {
231                                 case 0x000: usage->code = 0; break;
232                                 case 0x034: usage->code = KEY_SLEEP;            break;
233                                 case 0x036: usage->code = BTN_MISC;             break;
234                                 case 0x08a: usage->code = KEY_WWW;              break;
235                                 case 0x095: usage->code = KEY_HELP;             break;
236
237                                 case 0x0b0: usage->code = KEY_PLAY;             break;
238                                 case 0x0b1: usage->code = KEY_PAUSE;            break;
239                                 case 0x0b2: usage->code = KEY_RECORD;           break;
240                                 case 0x0b3: usage->code = KEY_FASTFORWARD;      break;
241                                 case 0x0b4: usage->code = KEY_REWIND;           break;
242                                 case 0x0b5: usage->code = KEY_NEXTSONG;         break;
243                                 case 0x0b6: usage->code = KEY_PREVIOUSSONG;     break;
244                                 case 0x0b7: usage->code = KEY_STOPCD;           break;
245                                 case 0x0b8: usage->code = KEY_EJECTCD;          break;
246                                 case 0x0cd: usage->code = KEY_PLAYPAUSE;        break;
247                                 case 0x0e0: is_abs = 1;
248                                             usage->code = ABS_VOLUME;
249                                             break;
250                                 case 0x0e2: usage->code = KEY_MUTE;             break;
251                                 case 0x0e5: usage->code = KEY_BASSBOOST;        break;
252                                 case 0x0e9: usage->code = KEY_VOLUMEUP;         break;
253                                 case 0x0ea: usage->code = KEY_VOLUMEDOWN;       break;
254
255                                 case 0x183: usage->code = KEY_CONFIG;           break;
256                                 case 0x18a: usage->code = KEY_MAIL;             break;
257                                 case 0x192: usage->code = KEY_CALC;             break;
258                                 case 0x194: usage->code = KEY_FILE;             break;
259                                 case 0x21a: usage->code = KEY_UNDO;             break;
260                                 case 0x21b: usage->code = KEY_COPY;             break;
261                                 case 0x21c: usage->code = KEY_CUT;              break;
262                                 case 0x21d: usage->code = KEY_PASTE;            break;
263
264                                 case 0x221: usage->code = KEY_FIND;             break;
265                                 case 0x223: usage->code = KEY_HOMEPAGE;         break;
266                                 case 0x224: usage->code = KEY_BACK;             break;
267                                 case 0x225: usage->code = KEY_FORWARD;          break;
268                                 case 0x226: usage->code = KEY_STOP;             break;
269                                 case 0x227: usage->code = KEY_REFRESH;          break;
270                                 case 0x22a: usage->code = KEY_BOOKMARKS;        break;
271
272                                 default:    usage->code = KEY_UNKNOWN;          break;
273                         }
274
275                         if (is_abs) {
276                                 usage->type = EV_ABS; bit = input->absbit; max = ABS_MAX;
277                         } else  {
278                                 usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
279                         }
280                         break;
281
282                 case HID_UP_HPVENDOR:   /* Reported on a Dutch layout HP5308 */
283
284                         set_bit(EV_REP, input->evbit);
285                         switch (usage->hid & HID_USAGE) {
286                                 case 0x021: usage->code = KEY_PRINT;            break;
287                                 case 0x070: usage->code = KEY_HP;               break;
288                                 case 0x071: usage->code = KEY_CAMERA;           break;
289                                 case 0x072: usage->code = KEY_SOUND;            break;
290                                 case 0x073: usage->code = KEY_QUESTION;         break;
291
292                                 case 0x080: usage->code = KEY_EMAIL;            break;
293                                 case 0x081: usage->code = KEY_CHAT;             break;
294                                 case 0x082: usage->code = KEY_SEARCH;           break;
295                                 case 0x083: usage->code = KEY_CONNECT;          break;
296                                 case 0x084: usage->code = KEY_FINANCE;          break;
297                                 case 0x085: usage->code = KEY_SPORT;            break;
298                                 case 0x086: usage->code = KEY_SHOP;             break;
299
300                                 default:    usage->code = KEY_UNKNOWN;          break;
301
302                         }
303
304                         usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
305                         break;
306                         
307                 case HID_UP_PID:
308
309                         usage->type = EV_FF; bit = input->ffbit; max = FF_MAX;
310                         
311                         switch(usage->hid & HID_USAGE) {
312                                 case 0x26: set_bit(FF_CONSTANT, input->ffbit); break;
313                                 case 0x27: set_bit(FF_RAMP,     input->ffbit); break;
314                                 case 0x28: set_bit(FF_CUSTOM,   input->ffbit); break;
315                                 case 0x30: set_bit(FF_SQUARE,   input->ffbit);
316                                            set_bit(FF_PERIODIC, input->ffbit); break;
317                                 case 0x31: set_bit(FF_SINE,     input->ffbit);
318                                            set_bit(FF_PERIODIC, input->ffbit); break;
319                                 case 0x32: set_bit(FF_TRIANGLE, input->ffbit);
320                                            set_bit(FF_PERIODIC, input->ffbit); break;
321                                 case 0x33: set_bit(FF_SAW_UP,   input->ffbit);
322                                            set_bit(FF_PERIODIC, input->ffbit); break;
323                                 case 0x34: set_bit(FF_SAW_DOWN, input->ffbit);
324                                            set_bit(FF_PERIODIC, input->ffbit); break;
325                                 case 0x40: set_bit(FF_SPRING,   input->ffbit); break;
326                                 case 0x41: set_bit(FF_DAMPER,   input->ffbit); break;
327                                 case 0x42: set_bit(FF_INERTIA , input->ffbit); break;
328                                 case 0x43: set_bit(FF_FRICTION, input->ffbit); break;
329                                 case 0x7e: usage->code = FF_GAIN;       break;
330                                 case 0x83:  /* Simultaneous Effects Max */
331                                         input->ff_effects_max = (field->value[0]);
332                                         dbg("Maximum Effects - %d",input->ff_effects_max);
333                                         break;
334                                 case 0x98:  /* Device Control */
335                                         usage->code = FF_AUTOCENTER;    break;
336                                 case 0xa4:  /* Safety Switch */
337                                         usage->code = BTN_DEAD;
338                                         bit = input->keybit;
339                                         usage->type = EV_KEY;
340                                         max = KEY_MAX;
341                                         dbg("Safety Switch Report\n");
342                                         break;
343                                 case 0x9f: /* Device Paused */
344                                 case 0xa0: /* Actuators Enabled */
345                                         dbg("Not telling the input API about ");
346                                         resolv_usage(usage->hid);
347                                         return;
348                         }
349                         break;
350                 default:
351                 unknown:
352                         resolv_usage(usage->hid);
353
354                         if (field->report_size == 1) {
355
356                                 if (field->report->type == HID_OUTPUT_REPORT) {
357                                         usage->code = LED_MISC;
358                                         usage->type = EV_LED; bit = input->ledbit; max = LED_MAX;
359                                         break;
360                                 }
361
362                                 usage->code = BTN_MISC;
363                                 usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX;
364                                 break;
365                         }
366
367                         if (field->flags & HID_MAIN_ITEM_RELATIVE) {
368                                 usage->code = REL_MISC;
369                                 usage->type = EV_REL; bit = input->relbit; max = REL_MAX;
370                                 break;
371                         }
372
373                         usage->code = ABS_MISC;
374                         usage->type = EV_ABS; bit = input->absbit; max = ABS_MAX;
375                         break;
376         }
377
378         set_bit(usage->type, input->evbit);
379         if ((usage->type == EV_REL)
380                         && (device->quirks & (HID_QUIRK_2WHEEL_MOUSE_HACK_BACK
381                                 | HID_QUIRK_2WHEEL_MOUSE_HACK_EXTRA))
382                         && (usage->code == REL_WHEEL)) {
383                 set_bit(REL_HWHEEL, bit);
384         }
385
386         while (usage->code <= max && test_and_set_bit(usage->code, bit)) {
387                 usage->code = find_next_zero_bit(bit, max + 1, usage->code);
388         }
389
390         if (usage->code > max)
391                 return;
392
393         if (usage->type == EV_ABS) {
394                 int a = field->logical_minimum;
395                 int b = field->logical_maximum;
396
397                 if ((device->quirks & HID_QUIRK_BADPAD) && (usage->code == ABS_X || usage->code == ABS_Y)) {
398                         a = field->logical_minimum = 0;
399                         b = field->logical_maximum = 255;
400                 }
401                 
402                 input->absmin[usage->code] = a;
403                 input->absmax[usage->code] = b;
404                 input->absfuzz[usage->code] = 0;
405                 input->absflat[usage->code] = 0;
406
407                 if (field->application == HID_GD_GAMEPAD || field->application == HID_GD_JOYSTICK) {
408                         input->absfuzz[usage->code] = (b - a) >> 8;
409                         input->absflat[usage->code] = (b - a) >> 4;
410                 }
411         }
412
413         if (usage->hat_min != usage->hat_max) {
414                 int i;
415                 for (i = usage->code; i < usage->code + 2 && i <= max; i++) {
416                         input->absmax[i] = 1;
417                         input->absmin[i] = -1;
418                         input->absfuzz[i] = 0;
419                         input->absflat[i] = 0;
420                 }
421                 set_bit(usage->code + 1, input->absbit);
422         }
423 }
424
425 void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value, struct pt_regs *regs)
426 {
427         struct input_dev *input = find_input(hid, field);
428         int *quirks = &hid->quirks;
429
430         if (!input)
431                 return;
432
433         input_regs(input, regs);
434
435         if (((hid->quirks & HID_QUIRK_2WHEEL_MOUSE_HACK_EXTRA) && (usage->code == BTN_EXTRA))
436                 || ((hid->quirks & HID_QUIRK_2WHEEL_MOUSE_HACK_BACK) && (usage->code == BTN_BACK))) {
437                 if (value)
438                         hid->quirks |= HID_QUIRK_2WHEEL_MOUSE_HACK_ON;
439                 else
440                         hid->quirks &= ~HID_QUIRK_2WHEEL_MOUSE_HACK_ON;
441                 return;
442         }
443
444         if ((hid->quirks & HID_QUIRK_2WHEEL_MOUSE_HACK_ON)
445                         && (usage->code == REL_WHEEL)) {
446                 input_event(input, usage->type, REL_HWHEEL, value);
447                 return;
448         }
449
450         if (usage->hat_min != usage->hat_max ) { /* FIXME: hat_max can be 0 and hat_min 1 */
451                 value = (value - usage->hat_min) * 8 / (usage->hat_max - usage->hat_min + 1) + 1;
452                 if (value < 0 || value > 8) value = 0;
453                 input_event(input, usage->type, usage->code    , hid_hat_to_axis[value].x);
454                 input_event(input, usage->type, usage->code + 1, hid_hat_to_axis[value].y);
455                 return;
456         }
457
458         if (usage->hid == (HID_UP_DIGITIZER | 0x003c)) { /* Invert */
459                 *quirks = value ? (*quirks | HID_QUIRK_INVERT) : (*quirks & ~HID_QUIRK_INVERT);
460                 return;
461         }
462
463         if (usage->hid == (HID_UP_DIGITIZER | 0x0032)) { /* InRange */
464                 if (value) {
465                         input_event(input, usage->type, (*quirks & HID_QUIRK_INVERT) ? BTN_TOOL_RUBBER : usage->code, 1);
466                         return;
467                 }
468                 input_event(input, usage->type, usage->code, 0);
469                 input_event(input, usage->type, BTN_TOOL_RUBBER, 0);
470                 return;
471         }
472
473         if (usage->hid == (HID_UP_DIGITIZER | 0x0030) && (*quirks & HID_QUIRK_NOTOUCH)) { /* Pressure */
474                 int a = field->logical_minimum;
475                 int b = field->logical_maximum;
476                 input_event(input, EV_KEY, BTN_TOUCH, value > a + ((b - a) >> 3));
477         }
478
479         if (usage->hid == (HID_UP_PID | 0x83UL)) { /* Simultaneous Effects Max */
480                 input->ff_effects_max = value;
481                 dbg("Maximum Effects - %d",input->ff_effects_max);
482                 return;
483         }
484         if (usage->hid == (HID_UP_PID | 0x7fUL)) {
485                 dbg("PID Pool Report\n");
486                 return;
487         }
488
489         if((usage->type == EV_KEY) && (usage->code == 0)) /* Key 0 is "unassigned", not KEY_UNKNOWN */
490                 return;
491
492         input_event(input, usage->type, usage->code, value);
493
494         if ((field->flags & HID_MAIN_ITEM_RELATIVE) && (usage->type == EV_KEY))
495                 input_event(input, usage->type, usage->code, 0);
496 }
497
498 void hidinput_report_event(struct hid_device *hid, struct hid_report *report)
499 {
500         struct list_head *lh;
501         struct hid_input *hidinput;
502
503         list_for_each (lh, &hid->inputs) {
504                 hidinput = list_entry(lh, struct hid_input, list);
505                 input_sync(&hidinput->input);
506         }
507 }
508
509 static int hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
510 {
511         struct hid_device *hid = dev->private;
512         struct hid_field *field = NULL;
513         int offset;
514
515         if (type == EV_FF)
516                 return hid_ff_event(hid, dev, type, code, value);
517
518         if ((offset = hid_find_field(hid, type, code, &field)) == -1) {
519                 warn("event field not found");
520                 return -1;
521         }
522
523         hid_set_field(field, offset, value);
524         hid_submit_report(hid, field->report, USB_DIR_OUT);
525
526         return 0;
527 }
528
529 static int hidinput_open(struct input_dev *dev)
530 {
531         struct hid_device *hid = dev->private;
532         return hid_open(hid);
533 }
534
535 static void hidinput_close(struct input_dev *dev)
536 {
537         struct hid_device *hid = dev->private;
538         hid_close(hid);
539 }
540
541 /*
542  * Register the input device; print a message.
543  * Configure the input layer interface
544  * Read all reports and initialize the absolute field values.
545  */
546
547 int hidinput_connect(struct hid_device *hid)
548 {
549         struct usb_device *dev = hid->dev;
550         struct hid_report_enum *report_enum;
551         struct hid_report *report;
552         struct list_head *list;
553         struct hid_input *hidinput = NULL;
554         int i, j, k;
555
556         INIT_LIST_HEAD(&hid->inputs);
557
558         for (i = 0; i < hid->maxcollection; i++)
559                 if (hid->collection[i].type == HID_COLLECTION_APPLICATION &&
560                     IS_INPUT_APPLICATION(hid->collection[i].usage))
561                         break;
562
563         if (i == hid->maxcollection)
564                 return -1;
565
566         for (k = HID_INPUT_REPORT; k <= HID_OUTPUT_REPORT; k++) {
567                 report_enum = hid->report_enum + k;
568                 list = report_enum->report_list.next;
569                 while (list != &report_enum->report_list) {
570                         report = (struct hid_report *) list;
571
572                         if (!report->maxfield) {
573                                 list = list->next;
574                                 continue;
575                         }
576
577                         if (!hidinput) {
578                                 hidinput = kmalloc(sizeof(*hidinput), GFP_KERNEL);
579                                 if (!hidinput) {
580                                         err("Out of memory during hid input probe");
581                                         return -1;
582                                 }
583                                 memset(hidinput, 0, sizeof(*hidinput));
584
585                                 list_add_tail(&hidinput->list, &hid->inputs);
586
587                                 hidinput->input.private = hid;
588                                 hidinput->input.event = hidinput_input_event;
589                                 hidinput->input.open = hidinput_open;
590                                 hidinput->input.close = hidinput_close;
591
592                                 hidinput->input.name = hid->name;
593                                 hidinput->input.phys = hid->phys;
594                                 hidinput->input.uniq = hid->uniq;
595                                 hidinput->input.id.bustype = BUS_USB;
596                                 hidinput->input.id.vendor = dev->descriptor.idVendor;
597                                 hidinput->input.id.product = dev->descriptor.idProduct;
598                                 hidinput->input.id.version = dev->descriptor.bcdDevice;
599                                 hidinput->input.dev = &hid->intf->dev;
600                         }
601
602                         for (i = 0; i < report->maxfield; i++)
603                                 for (j = 0; j < report->field[i]->maxusage; j++)
604                                         hidinput_configure_usage(hidinput, report->field[i],
605                                                                  report->field[i]->usage + j);
606
607                         if (hid->quirks & HID_QUIRK_MULTI_INPUT) {
608                                 /* This will leave hidinput NULL, so that it
609                                  * allocates another one if we have more inputs on
610                                  * the same interface. Some devices (e.g. Happ's
611                                  * UGCI) cram a lot of unrelated inputs into the
612                                  * same interface. */
613                                 hidinput->report = report;
614                                 input_register_device(&hidinput->input);
615                                 hidinput = NULL;
616                         }
617
618                         list = list->next;
619                 }
620         }
621
622         /* This only gets called when we are a single-input (most of the
623          * time). IOW, not a HID_QUIRK_MULTI_INPUT. The hid_ff_init() is
624          * only useful in this case, and not for multi-input quirks. */
625         if (hidinput) {
626                 hid_ff_init(hid);
627                 input_register_device(&hidinput->input);
628         }
629
630         return 0;
631 }
632
633 void hidinput_disconnect(struct hid_device *hid)
634 {
635         struct list_head *lh, *next;
636         struct hid_input *hidinput;
637
638         list_for_each_safe (lh, next, &hid->inputs) {
639                 hidinput = list_entry(lh, struct hid_input, list);
640                 input_unregister_device(&hidinput->input);
641                 list_del(&hidinput->list);
642                 kfree(hidinput);
643         }
644 }