4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000 Vojtech Pavlik
7 * USB HID support for the Linux input drivers
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 * Should you need to contact me, the author, you can do so either by
28 * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
29 * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
32 #include <linux/module.h>
33 #include <linux/malloc.h>
34 #include <linux/input.h>
35 #include <linux/init.h>
36 #include <linux/kernel.h>
37 #include <linux/sched.h>
38 #include <linux/list.h>
40 #include <linux/smp_lock.h>
41 #include <linux/spinlock.h>
50 #include "hid-debug.h"
52 #define hid_dump_input(a,b) do { } while (0)
53 #define hid_dump_device(c) do { } while (0)
56 #define unk KEY_UNKNOWN
58 static unsigned char hid_keyboard
[256] = {
59 0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
60 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44, 2, 3,
61 4, 5, 6, 7, 8, 9, 10, 11, 28, 1, 14, 15, 57, 12, 13, 26,
62 27, 43, 84, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
63 65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
64 105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
65 72, 73, 82, 83, 86,127,116,117, 85, 89, 90, 91, 92, 93, 94, 95,
66 120,121,122,123,unk
,138,unk
,unk
,128,129,131,137,133,135,136,113,
67 115,114,unk
,unk
,unk
,unk
,unk
,124,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,
68 unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,
69 unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,
70 unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,
71 134,130,132,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,
72 unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,unk
,
73 29, 42, 56,125, 97, 54,100,126
79 } hid_hat_to_axis
[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
82 * Register a new report for a device.
85 static struct hid_report
*hid_register_report(struct hid_device
*device
, unsigned type
, unsigned id
)
87 struct hid_report_enum
*report_enum
= device
->report_enum
+ type
;
88 struct hid_report
*report
;
90 if (report_enum
->report_id_hash
[id
])
91 return report_enum
->report_id_hash
[id
];
93 if (!(report
= kmalloc(sizeof(struct hid_report
), GFP_KERNEL
)))
95 memset(report
, 0, sizeof(struct hid_report
));
97 if (id
!= 0) report_enum
->numbered
= 1;
102 report
->device
= device
;
103 report_enum
->report_id_hash
[id
] = report
;
105 list_add_tail(&report
->list
, &report_enum
->report_list
);
111 * Register a new field for this report.
114 static struct hid_field
*hid_register_field(struct hid_report
*report
, unsigned usages
, unsigned values
)
116 if (report
->maxfield
< HID_MAX_FIELDS
) {
117 struct hid_field
*field
;
119 if (!(field
= kmalloc(sizeof(struct hid_field
) + usages
* sizeof(struct hid_usage
)
120 + values
* sizeof(unsigned), GFP_KERNEL
)))
122 memset(field
, 0, sizeof(struct hid_field
) + usages
* sizeof(struct hid_usage
)
123 + values
* sizeof(unsigned));
125 report
->field
[report
->maxfield
++] = field
;
126 field
->usage
= (struct hid_usage
*)(field
+ 1);
127 field
->value
= (unsigned *)(field
->usage
+ usages
);
128 field
->report
= report
;
133 dbg("too many fields in report");
138 * Open a collection. The type/usage is pushed on the stack.
141 static int open_collection(struct hid_parser
*parser
, unsigned type
)
145 usage
= parser
->local
.usage
[0];
147 if (type
== HID_COLLECTION_APPLICATION
)
148 parser
->device
->application
= usage
;
150 if (parser
->collection_stack_ptr
< HID_COLLECTION_STACK_SIZE
) { /* PUSH on stack */
151 struct hid_collection
*collection
= parser
->collection_stack
+ parser
->collection_stack_ptr
++;
152 collection
->type
= type
;
153 collection
->usage
= usage
;
157 dbg("collection stack overflow");
162 * Close a collection.
165 static int close_collection(struct hid_parser
*parser
)
167 if (parser
->collection_stack_ptr
> 0) { /* POP from stack */
168 parser
->collection_stack_ptr
--;
171 dbg("collection stack underflow");
176 * Climb up the stack, search for the specified collection type
177 * and return the usage.
180 static unsigned hid_lookup_collection(struct hid_parser
*parser
, unsigned type
)
183 for (n
= parser
->collection_stack_ptr
- 1; n
>= 0; n
--)
184 if (parser
->collection_stack
[n
].type
== type
)
185 return parser
->collection_stack
[n
].usage
;
186 return 0; /* we know nothing about this usage type */
190 * Add a usage to the temporary parser table.
193 static int hid_add_usage(struct hid_parser
*parser
, unsigned usage
)
195 if (parser
->local
.usage_index
>= MAX_USAGES
) {
196 dbg("usage index exceeded");
199 parser
->local
.usage
[parser
->local
.usage_index
++] = usage
;
204 * Register a new field for this report.
207 static int hid_add_field(struct hid_parser
*parser
, unsigned report_type
, unsigned flags
)
209 struct hid_report
*report
;
210 struct hid_field
*field
;
215 if (!(report
= hid_register_report(parser
->device
, report_type
, parser
->global
.report_id
))) {
216 dbg("hid_register_report failed");
220 if (HID_MAIN_ITEM_VARIABLE
& ~flags
) { /* ARRAY */
221 if (parser
->global
.logical_maximum
<= parser
->global
.logical_minimum
) {
222 dbg("logical range invalid %d %d", parser
->global
.logical_minimum
, parser
->global
.logical_maximum
);
225 usages
= parser
->local
.usage_index
;
226 /* Hint: we can assume usages < MAX_USAGE here */
227 } else { /* VARIABLE */
228 usages
= parser
->global
.report_count
;
230 offset
= report
->size
;
231 report
->size
+= parser
->global
.report_size
*
232 parser
->global
.report_count
;
234 return 0; /* ignore padding fields */
235 if ((field
= hid_register_field(report
, usages
,
236 parser
->global
.report_count
)) == NULL
)
238 field
->physical
= hid_lookup_collection(parser
, HID_COLLECTION_PHYSICAL
);
239 field
->logical
= hid_lookup_collection(parser
, HID_COLLECTION_LOGICAL
);
240 for (i
= 0; i
< usages
; i
++) field
->usage
[i
].hid
= parser
->local
.usage
[i
];
241 field
->maxusage
= usages
;
242 field
->flags
= flags
;
243 field
->report_offset
= offset
;
244 field
->report_type
= report_type
;
245 field
->report_size
= parser
->global
.report_size
;
246 field
->report_count
= parser
->global
.report_count
;
247 field
->logical_minimum
= parser
->global
.logical_minimum
;
248 field
->logical_maximum
= parser
->global
.logical_maximum
;
249 field
->physical_minimum
= parser
->global
.physical_minimum
;
250 field
->physical_maximum
= parser
->global
.physical_maximum
;
251 field
->unit_exponent
= parser
->global
.unit_exponent
;
252 field
->unit
= parser
->global
.unit
;
257 * Read data value from item.
260 static __inline__ __u32
item_udata(struct hid_item
*item
)
262 switch (item
->size
) {
263 case 1: return item
->data
.u8
;
264 case 2: return item
->data
.u16
;
265 case 4: return item
->data
.u32
;
270 static __inline__ __s32
item_sdata(struct hid_item
*item
)
272 switch (item
->size
) {
273 case 1: return item
->data
.s8
;
274 case 2: return item
->data
.s16
;
275 case 4: return item
->data
.s32
;
281 * Process a global item.
284 static int hid_parser_global(struct hid_parser
*parser
, struct hid_item
*item
)
288 case HID_GLOBAL_ITEM_TAG_PUSH
:
290 if (parser
->global_stack_ptr
< HID_GLOBAL_STACK_SIZE
) {
291 memcpy(parser
->global_stack
+ parser
->global_stack_ptr
++,
292 &parser
->global
, sizeof(struct hid_global
));
295 dbg("global enviroment stack overflow");
298 case HID_GLOBAL_ITEM_TAG_POP
:
300 if (parser
->global_stack_ptr
> 0) {
301 memcpy(&parser
->global
, parser
->global_stack
+ --parser
->global_stack_ptr
,
302 sizeof(struct hid_global
));
305 dbg("global enviroment stack underflow");
308 case HID_GLOBAL_ITEM_TAG_USAGE_PAGE
:
309 parser
->global
.usage_page
= item_udata(item
);
312 case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM
:
313 parser
->global
.logical_minimum
= item_sdata(item
);
316 case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM
:
317 parser
->global
.logical_maximum
= item_sdata(item
);
320 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM
:
321 parser
->global
.physical_minimum
= item_sdata(item
);
324 case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM
:
325 parser
->global
.physical_maximum
= item_sdata(item
);
328 case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT
:
329 parser
->global
.unit_exponent
= item_udata(item
);
332 case HID_GLOBAL_ITEM_TAG_UNIT
:
333 parser
->global
.unit
= item_udata(item
);
336 case HID_GLOBAL_ITEM_TAG_REPORT_SIZE
:
337 if ((parser
->global
.report_size
= item_udata(item
)) > 32) {
338 dbg("invalid report_size %d", parser
->global
.report_size
);
343 case HID_GLOBAL_ITEM_TAG_REPORT_COUNT
:
344 if ((parser
->global
.report_count
= item_udata(item
)) > MAX_USAGES
) {
345 dbg("invalid report_count %d", parser
->global
.report_count
);
350 case HID_GLOBAL_ITEM_TAG_REPORT_ID
:
351 if ((parser
->global
.report_id
= item_udata(item
)) == 0) {
352 dbg("report_id 0 is invalid");
358 dbg("unknown global tag 0x%x", item
->tag
);
364 * Process a local item.
367 static int hid_parser_local(struct hid_parser
*parser
, struct hid_item
*item
)
371 if (item
->size
== 0) {
372 dbg("item data expected for local item");
376 data
= item_udata(item
);
380 case HID_LOCAL_ITEM_TAG_DELIMITER
:
384 * We treat items before the first delimiter
385 * as global to all usage sets (branch 0).
386 * In the moment we process only these global
387 * items and the first delimiter set.
389 if (parser
->local
.delimiter_depth
!= 0) {
390 dbg("nested delimiters");
393 parser
->local
.delimiter_depth
++;
394 parser
->local
.delimiter_branch
++;
396 if (parser
->local
.delimiter_depth
< 1) {
397 dbg("bogus close delimiter");
400 parser
->local
.delimiter_depth
--;
404 case HID_LOCAL_ITEM_TAG_USAGE
:
406 if (parser
->local
.delimiter_branch
< 2) {
408 data
= (parser
->global
.usage_page
<< 16) + data
;
409 return hid_add_usage(parser
, data
);
411 dbg("alternative usage ignored");
414 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM
:
416 if (parser
->local
.delimiter_branch
< 2) {
418 data
= (parser
->global
.usage_page
<< 16) + data
;
419 parser
->local
.usage_minimum
= data
;
422 dbg("alternative usage ignored");
425 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM
:
427 if (parser
->local
.delimiter_branch
< 2) {
430 data
= (parser
->global
.usage_page
<< 16) + data
;
431 for (n
= parser
->local
.usage_minimum
; n
<= data
; n
++)
432 if (hid_add_usage(parser
, n
)) {
433 dbg("hid_add_usage failed\n");
438 dbg("alternative usage ignored");
443 dbg("unknown local item tag 0x%x", item
->tag
);
449 * Process a main item.
452 static int hid_parser_main(struct hid_parser
*parser
, struct hid_item
*item
)
457 data
= item_udata(item
);
460 case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION
:
461 ret
= open_collection(parser
, data
& 3);
463 case HID_MAIN_ITEM_TAG_END_COLLECTION
:
464 ret
= close_collection(parser
);
466 case HID_MAIN_ITEM_TAG_INPUT
:
467 ret
= hid_add_field(parser
, HID_INPUT_REPORT
, data
);
469 case HID_MAIN_ITEM_TAG_OUTPUT
:
470 ret
= hid_add_field(parser
, HID_OUTPUT_REPORT
, data
);
472 case HID_MAIN_ITEM_TAG_FEATURE
:
473 ret
= hid_add_field(parser
, HID_FEATURE_REPORT
, data
);
476 dbg("unknown main item tag 0x%x", item
->tag
);
480 memset(&parser
->local
, 0, sizeof(parser
->local
)); /* Reset the local parser environment */
486 * Process a reserved item.
489 static int hid_parser_reserved(struct hid_parser
*parser
, struct hid_item
*item
)
491 dbg("reserved item type, tag 0x%x", item
->tag
);
496 * Free a report and all registered fields. The field->usage and
497 * field->value table's are allocated behind the field, so we need
498 * only to free(field) itself.
501 static void hid_free_report(struct hid_report
*report
)
505 for (n
= 0; n
< report
->maxfield
; n
++)
506 kfree(report
->field
[n
]);
511 * Free a device structure, all reports, and all fields.
514 static void hid_free_device(struct hid_device
*device
)
518 for (i
= 0; i
< HID_REPORT_TYPES
; i
++) {
519 struct hid_report_enum
*report_enum
= device
->report_enum
+ i
;
521 for (j
= 0; j
< 256; j
++) {
522 struct hid_report
*report
= report_enum
->report_id_hash
[j
];
523 if (report
) hid_free_report(report
);
527 if (device
->rdesc
) kfree(device
->rdesc
);
531 * Fetch a report description item from the data stream. We support long
532 * items, though they are not used yet.
535 static __u8
*fetch_item(__u8
*start
, __u8
*end
, struct hid_item
*item
)
537 if ((end
- start
) > 0) {
540 item
->type
= (b
>> 2) & 3;
541 item
->tag
= (b
>> 4) & 15;
543 if (item
->tag
== HID_ITEM_TAG_LONG
) {
545 item
->format
= HID_ITEM_FORMAT_LONG
;
547 if ((end
- start
) >= 2) {
549 item
->size
= *start
++;
550 item
->tag
= *start
++;
552 if ((end
- start
) >= item
->size
) {
553 item
->data
.longdata
= start
;
560 item
->format
= HID_ITEM_FORMAT_SHORT
;
562 switch (item
->size
) {
568 if ((end
- start
) >= 1) {
569 item
->data
.u8
= *start
++;
575 if ((end
- start
) >= 2) {
576 item
->data
.u16
= le16_to_cpu( *((__u16
*)start
)++);
582 if ((end
- start
) >= 4) {
583 item
->data
.u32
= le32_to_cpu( *((__u32
*)start
)++);
593 * Parse a report description into a hid_device structure. Reports are
594 * enumerated, fields are attached to these reports.
597 static struct hid_device
*hid_parse_report(__u8
*start
, unsigned size
)
599 struct hid_device
*device
;
600 struct hid_parser
*parser
;
601 struct hid_item item
;
604 static int (*dispatch_type
[])(struct hid_parser
*parser
,
605 struct hid_item
*item
) = {
612 if (!(device
= kmalloc(sizeof(struct hid_device
), GFP_KERNEL
)))
614 memset(device
, 0, sizeof(struct hid_device
));
616 for (i
= 0; i
< HID_REPORT_TYPES
; i
++)
617 INIT_LIST_HEAD(&device
->report_enum
[i
].report_list
);
619 if (!(device
->rdesc
= (__u8
*)kmalloc(size
, GFP_KERNEL
))) {
623 memcpy(device
->rdesc
, start
, size
);
625 if (!(parser
= kmalloc(sizeof(struct hid_parser
), GFP_KERNEL
))) {
626 kfree(device
->rdesc
);
630 memset(parser
, 0, sizeof(struct hid_parser
));
631 parser
->device
= device
;
634 while ((start
= fetch_item(start
, end
, &item
)) != 0) {
635 if (item
.format
!= HID_ITEM_FORMAT_SHORT
) {
636 dbg("unexpected long global item");
637 hid_free_device(device
);
641 if (dispatch_type
[item
.type
](parser
, &item
)) {
642 dbg("item %u %u %u %u parsing failed\n",
643 item
.format
, (unsigned)item
.size
, (unsigned)item
.type
, (unsigned)item
.tag
);
644 hid_free_device(device
);
650 if (parser
->collection_stack_ptr
) {
651 dbg("unbalanced collection at end of report description");
652 hid_free_device(device
);
656 if (parser
->local
.delimiter_depth
) {
657 dbg("unbalanced delimiter at end of report description");
658 hid_free_device(device
);
667 dbg("item fetching failed at offset %d\n", (int)(end
- start
));
668 hid_free_device(device
);
674 * Convert a signed n-bit integer to signed 32-bit integer. Common
675 * cases are done through the compiler, the screwed things has to be
679 static __inline__ __s32
snto32(__u32 value
, unsigned n
)
682 case 8: return ((__s8
)value
);
683 case 16: return ((__s16
)value
);
684 case 32: return ((__s32
)value
);
686 return value
& (1 << (n
- 1)) ? value
| (-1 << n
) : value
;
690 * Convert a signed 32-bit integer to a signed n-bit integer.
693 static __inline__ __u32
s32ton(__s32 value
, unsigned n
)
695 __s32 a
= value
>> (n
- 1);
696 if (a
&& a
!= -1) return value
> 0 ? 1 << (n
- 1) : (1 << n
) - 1;
697 return value
& ((1 << n
) - 1);
701 * Extract/implement a data field from/to a report. We use 64-bit unsigned,
702 * 32-bit aligned, so that we can possibly have alignment problems on some
706 static __inline__ __u32
extract(__u8
*report
, unsigned offset
, unsigned n
)
708 report
+= (offset
>> 5) << 2; offset
&= 31;
709 return (le64_to_cpu(*(__u64
*)report
) >> offset
) & ((1 << n
) - 1);
712 static __inline__
void implement(__u8
*report
, unsigned offset
, unsigned n
, __u32 value
)
714 report
+= (offset
>> 5) << 2; offset
&= 31;
715 *(__u64
*)report
&= cpu_to_le64(~((((__u64
) 1 << n
) - 1) << offset
));
716 *(__u64
*)report
|= cpu_to_le64((__u64
)value
<< offset
);
719 static void hid_configure_usage(struct hid_device
*device
, struct hid_field
*field
, struct hid_usage
*usage
)
721 struct input_dev
*input
= &device
->input
;
725 switch (usage
->hid
& HID_USAGE_PAGE
) {
727 case HID_UP_KEYBOARD
:
729 if ((usage
->hid
& HID_USAGE
) < 256) {
730 if (!(usage
->code
= hid_keyboard
[usage
->hid
& HID_USAGE
]))
733 usage
->code
= KEY_UNKNOWN
;
735 set_bit(EV_REP
, input
->evbit
);
736 usage
->type
= EV_KEY
; bit
= input
->keybit
; max
= KEY_MAX
;
741 usage
->code
= ((usage
->hid
- 1) & 0xf) + 0x100;
742 usage
->type
= EV_KEY
; bit
= input
->keybit
; max
= KEY_MAX
;
744 switch (device
->application
) {
745 case HID_GD_GAMEPAD
: usage
->code
+= 0x10;
746 case HID_GD_JOYSTICK
: usage
->code
+= 0x10;
747 case HID_GD_MOUSE
: usage
->code
+= 0x10; break;
749 if (field
->physical
== HID_GD_POINTER
)
757 if ((usage
->hid
& 0xf0) == 0x80) { /* SystemControl */
758 switch (usage
->hid
& 0xf) {
759 case 0x1: usage
->code
= KEY_POWER
; break;
760 case 0x2: usage
->code
= KEY_SLEEP
; break;
761 case 0x3: usage
->code
= KEY_WAKEUP
; break;
762 default: usage
->code
= KEY_UNKNOWN
; break;
764 usage
->type
= EV_KEY
; bit
= input
->keybit
; max
= KEY_MAX
;
768 usage
->code
= usage
->hid
& 0xf;
770 if (field
->report_size
== 1) {
771 usage
->code
= BTN_MISC
;
772 usage
->type
= EV_KEY
; bit
= input
->keybit
; max
= KEY_MAX
;
776 if (field
->flags
& HID_MAIN_ITEM_RELATIVE
) {
777 usage
->type
= EV_REL
; bit
= input
->relbit
; max
= REL_MAX
;
781 usage
->type
= EV_ABS
; bit
= input
->absbit
; max
= ABS_MAX
;
783 if (usage
->hid
== HID_GD_HATSWITCH
) {
784 usage
->code
= ABS_HAT0X
;
785 usage
->hat
= 1 + (field
->logical_maximum
== 4);
791 usage
->code
= (usage
->hid
- 1) & 0xf;
792 usage
->type
= EV_LED
; bit
= input
->ledbit
; max
= LED_MAX
;
795 case HID_UP_DIGITIZER
:
797 switch (usage
->hid
& 0xff) {
799 case 0x30: /* TipPressure */
801 usage
->type
= EV_ABS
; bit
= input
->absbit
; max
= ABS_MAX
;
802 usage
->code
= ABS_PRESSURE
;
803 clear_bit(usage
->code
, bit
);
806 case 0x32: /* InRange */
808 usage
->type
= EV_KEY
; bit
= input
->keybit
; max
= KEY_MAX
;
809 switch (field
->physical
& 0xff) {
810 case 0x21: usage
->code
= BTN_TOOL_MOUSE
; break;
811 case 0x22: usage
->code
= BTN_TOOL_FINGER
; break;
812 default: usage
->code
= BTN_TOOL_PEN
; break;
816 case 0x33: /* Touch */
817 case 0x42: /* TipSwitch */
818 case 0x43: /* TipSwitch2 */
820 usage
->type
= EV_KEY
; bit
= input
->keybit
; max
= KEY_MAX
;
821 usage
->code
= BTN_TOUCH
;
822 clear_bit(usage
->code
, bit
);
825 case 0x44: /* BarrelSwitch */
827 usage
->type
= EV_KEY
; bit
= input
->keybit
; max
= KEY_MAX
;
828 usage
->code
= BTN_STYLUS
;
829 clear_bit(usage
->code
, bit
);
832 default: goto unknown
;
838 switch (usage
->hid
& HID_USAGE
) {
840 case 0x0034: usage
->code
= KEY_PHONE
; break;
841 case 0x0036: usage
->code
= KEY_NOTEPAD
; break;
842 case 0x008a: usage
->code
= KEY_MAIL
; break;
843 case 0x0095: usage
->code
= KEY_CALENDAR
; break;
844 case 0x00b7: usage
->code
= KEY_PRINT
; break;
845 case 0x00b8: usage
->code
= KEY_HELP
; break;
846 case 0x00cd: usage
->code
= KEY_SOUND
; break;
847 case 0x00e2: usage
->code
= KEY_PROG1
; break;
848 case 0x00e9: usage
->code
= KEY_PROG2
; break;
849 case 0x00ea: usage
->code
= KEY_PROG3
; break;
850 case 0x018a: usage
->code
= KEY_WWW
; break;
851 case 0x0223: usage
->code
= KEY_FULLSCREEN
; break;
852 default: usage
->code
= KEY_UNKNOWN
; break;
856 usage
->type
= EV_KEY
; bit
= input
->keybit
; max
= KEY_MAX
;
862 if (field
->report_size
== 1) {
863 usage
->code
= BTN_MISC
;
864 usage
->type
= EV_KEY
; bit
= input
->keybit
; max
= KEY_MAX
;
868 if (field
->flags
& HID_MAIN_ITEM_RELATIVE
) {
869 usage
->code
= REL_MISC
;
870 usage
->type
= EV_REL
; bit
= input
->relbit
; max
= REL_MAX
;
874 usage
->code
= ABS_MISC
;
875 usage
->type
= EV_ABS
; bit
= input
->absbit
; max
= ABS_MAX
;
879 set_bit(usage
->type
, input
->evbit
);
881 while (usage
->code
<= max
&& test_and_set_bit(usage
->code
, bit
)) {
882 usage
->code
= find_next_zero_bit(bit
, max
+ 1, usage
->code
);
885 if (usage
->type
== EV_ABS
) {
886 int a
= field
->logical_minimum
;
887 int b
= field
->logical_maximum
;
889 input
->absmin
[usage
->code
] = a
;
890 input
->absmax
[usage
->code
] = b
;
891 input
->absfuzz
[usage
->code
] = (b
- a
) >> 8;
892 input
->absflat
[usage
->code
] = (b
- a
) >> 4;
897 for (i
= usage
->code
; i
< usage
->code
+ 2; i
++) {
898 input
->absmax
[i
] = 1;
899 input
->absmin
[i
] = -1;
900 input
->absfuzz
[i
] = 0;
901 input
->absflat
[i
] = 0;
903 set_bit(usage
->code
+ 1, input
->absbit
);
907 static void hid_process_event(struct input_dev
*input
, int flags
, struct hid_usage
*usage
, __s32 value
)
909 hid_dump_input(usage
, value
);
912 if (usage
->hat
== 2) value
= value
* 2 - 1;
913 input_event(input
, usage
->type
, usage
->code
, hid_hat_to_axis
[value
].x
);
914 input_event(input
, usage
->type
, usage
->code
+ 1, hid_hat_to_axis
[value
].y
);
918 input_event(input
, usage
->type
, usage
->code
, value
);
920 if ((flags
& HID_MAIN_ITEM_RELATIVE
) && (usage
->type
== EV_KEY
))
921 input_event(input
, usage
->type
, usage
->code
, 0);
925 * Search an array for a value.
928 static __inline__
int search(__s32
*array
, __s32 value
, unsigned n
)
930 while (n
--) if (*array
++ == value
) return 0;
935 * Analyse a received field, and fetch the data from it. The field
936 * content is stored for next report processing (we do differential
937 * reporting to the layer).
940 static void hid_input_field(struct hid_device
*dev
, struct hid_field
*field
, __u8
*data
)
943 unsigned count
= field
->report_count
;
944 unsigned offset
= field
->report_offset
;
945 unsigned size
= field
->report_size
;
946 __s32 min
= field
->logical_minimum
;
947 __s32 max
= field
->logical_maximum
;
948 __s32 value
[count
]; /* WARNING: gcc specific */
950 for (n
= 0; n
< count
; n
++)
951 value
[n
] = min
< 0 ? snto32(extract(data
, offset
+ n
* size
, size
), size
) :
952 extract(data
, offset
+ n
* size
, size
);
954 for (n
= 0; n
< count
; n
++) {
956 if (HID_MAIN_ITEM_VARIABLE
& field
->flags
) {
958 if (field
->flags
& HID_MAIN_ITEM_RELATIVE
) {
959 if (!value
[n
]) continue;
961 if (value
[n
] == field
->value
[n
]) continue;
963 hid_process_event(&dev
->input
, field
->flags
, &field
->usage
[n
], value
[n
]);
967 if (field
->value
[n
] >= min
&& field
->value
[n
] <= max
/* non-NULL value */
968 && field
->usage
[field
->value
[n
] - min
].hid
/* nonzero usage */
969 && search(value
, field
->value
[n
], count
))
970 hid_process_event(&dev
->input
, field
->flags
, &field
->usage
[field
->value
[n
] - min
], 0);
972 if (value
[n
] >= min
&& value
[n
] <= max
/* non-NULL value */
973 && field
->usage
[value
[n
] - min
].hid
/* nonzero usage */
974 && search(field
->value
, value
[n
], count
))
975 hid_process_event(&dev
->input
, field
->flags
, &field
->usage
[value
[n
] - min
], 1);
979 memcpy(field
->value
, value
, count
* sizeof(__s32
));
983 * Interrupt input handler - analyse a received report.
986 static void hid_irq(struct urb
*urb
)
988 struct hid_device
*device
= urb
->context
;
989 struct hid_report_enum
*report_enum
= device
->report_enum
+ HID_INPUT_REPORT
;
990 struct hid_report
*report
;
991 __u8
*data
= urb
->transfer_buffer
;
992 int len
= urb
->actual_length
;
996 dbg("nonzero status in irq %d", urb
->status
);
1001 dbg("empty report");
1006 printk(KERN_DEBUG __FILE__
": report (size %u) (%snumbered) = ", len
, report_enum
->numbered
? "" : "un");
1007 for (n
= 0; n
< len
; n
++)
1008 printk(" %02x", data
[n
]);
1012 n
= 0; /* Normally report number is 0 */
1014 if (report_enum
->numbered
) { /* Device uses numbered reports, data[0] is report number */
1019 if (!(report
= report_enum
->report_id_hash
[n
])) {
1020 dbg("undefined report_id %d received", n
);
1022 printk(KERN_DEBUG __FILE__
": report (size %u) = ", len
);
1023 for (n
= 0; n
< len
; n
++)
1024 printk(" %02x", data
[n
]);
1031 if (len
< ((report
->size
- 1) >> 3) + 1) {
1032 dbg("report %d is too short, (%d < %d)", report
->id
, len
, ((report
->size
- 1) >> 3) + 1);
1036 for (n
= 0; n
< report
->maxfield
; n
++)
1037 hid_input_field(device
, report
->field
[n
], data
);
1043 * hid_read_report() s intended to read the hid devices values even
1044 * before the input device is registered, so that the userland interface
1045 * modules start with real values. This is especially important for joydev.c
1046 * automagic calibration. Doesn't work yet, though. Don't know why, the control
1047 * request just times out on most devices I have and returns nonsense on others.
1050 static void hid_read_report(struct hid_device
*hid
, struct hid_report
*report
)
1053 int rlen
= ((report
->size
- 1) >> 3) + 1;
1058 memset(&urb
, 0, sizeof(struct urb
));
1059 memset(rdata
, 0, rlen
);
1061 urb
.transfer_buffer
= rdata
;
1062 urb
.actual_length
= rlen
;
1065 dbg("getting report type %d id %d len %d", report
->type
+ 1, report
->id
, rlen
);
1067 if ((read
= usb_get_report(hid
->dev
, hid
->ifnum
, report
->type
+ 1, report
->id
, rdata
, rlen
)) != rlen
) {
1068 dbg("reading report failed rlen %d read %d", rlen
, read
);
1070 printk(KERN_DEBUG __FILE__
": report = ");
1071 for (j
= 0; j
< rlen
; j
++) printk(" %02x", rdata
[j
]);
1082 * Output the field into the report.
1085 static void hid_output_field(struct hid_field
*field
, __u8
*data
)
1087 unsigned count
= field
->report_count
;
1088 unsigned offset
= field
->report_offset
;
1089 unsigned size
= field
->report_size
;
1092 for (n
= 0; n
< count
; n
++) {
1093 if (field
->logical_minimum
< 0) /* signed values */
1094 implement(data
, offset
+ n
* size
, size
, s32ton(field
->value
[n
], size
));
1095 else /* unsigned values */
1096 implement(data
, offset
+ n
* size
, size
, field
->value
[n
]);
1104 void hid_output_report(struct hid_report
*report
, __u8
*data
)
1107 for (n
= 0; n
< report
->maxfield
; n
++)
1108 hid_output_field(report
->field
[n
], data
);
1112 * Set a field value. The report this field belongs to has to be
1113 * created and transfered to the device, to set this value in the
1117 int hid_set_field(struct hid_field
*field
, unsigned offset
, __s32 value
)
1119 unsigned size
= field
->report_size
;
1121 hid_dump_input(field
->usage
+ offset
, value
);
1123 if (offset
>= field
->report_count
) {
1124 dbg("offset exceeds report_count");
1127 if (field
->logical_minimum
< 0) {
1128 if (value
!= snto32(s32ton(value
, size
), size
)) {
1129 dbg("value %d is out of range", value
);
1133 if ( (value
> field
->logical_maximum
)
1134 || (value
< field
->logical_minimum
)) {
1135 dbg("value %d is invalid", value
);
1138 field
->value
[offset
] = value
;
1142 static int hid_find_field(struct hid_device
*hid
, unsigned int type
, unsigned int code
, struct hid_field
**field
)
1144 struct hid_report_enum
*report_enum
= hid
->report_enum
+ HID_OUTPUT_REPORT
;
1145 struct list_head
*list
= report_enum
->report_list
.next
;
1148 while (list
!= &report_enum
->report_list
) {
1149 struct hid_report
*report
= (struct hid_report
*) list
;
1151 for (i
= 0; i
< report
->maxfield
; i
++) {
1152 *field
= report
->field
[i
];
1153 for (j
= 0; j
< (*field
)->maxusage
; j
++)
1154 if ((*field
)->usage
[j
].type
== type
&& (*field
)->usage
[j
].code
== code
)
1161 static void hid_ctrl(struct urb
*urb
)
1164 warn("ctrl urb status %d received", urb
->status
);
1167 static int hid_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
1169 struct hid_device
*hid
= dev
->private;
1170 struct hid_field
*field
= NULL
;
1173 if ((offset
= hid_find_field(hid
, type
, code
, &field
)) == -1) {
1174 warn("event field not found");
1178 hid_set_field(field
, offset
, value
);
1180 if (hid
->urbout
.status
== -EINPROGRESS
) {
1181 warn("had to kill output urb");
1182 usb_unlink_urb(&hid
->urbout
);
1185 hid_output_report(field
->report
, hid
->bufout
);
1187 hid
->dr
.value
= 0x200 | field
->report
->id
;
1188 hid
->dr
.length
= ((field
->report
->size
- 1) >> 3) + 1;
1189 hid
->urbout
.transfer_buffer_length
= hid
->dr
.length
;
1191 if (usb_submit_urb(&hid
->urbout
)) {
1192 err("usb_submit_urb(out) failed");
1200 * Configure the input layer interface
1201 * Read all reports and initalize the absoulte field values.
1204 static void hid_init_input(struct hid_device
*hid
)
1206 struct hid_report_enum
*report_enum
;
1207 struct list_head
*list
;
1210 hid
->input
.private = hid
;
1211 hid
->input
.event
= hid_event
;
1213 for (k
= HID_INPUT_REPORT
; k
<= HID_OUTPUT_REPORT
; k
++) {
1215 report_enum
= hid
->report_enum
+ k
;
1216 list
= report_enum
->report_list
.next
;
1218 while (list
!= &report_enum
->report_list
) {
1220 struct hid_report
*report
= (struct hid_report
*) list
;
1224 for (i
= 0; i
< report
->maxfield
; i
++)
1225 for (j
= 0; j
< report
->field
[i
]->maxusage
; j
++)
1226 hid_configure_usage(hid
, report
->field
[i
], report
->field
[i
]->usage
+ j
);
1228 if (k
== HID_INPUT_REPORT
) {
1229 usb_set_idle(hid
->dev
, hid
->ifnum
, report
->id
, 0);
1230 hid_read_report(hid
, report
);
1236 #define USB_VENDOR_ID_WACOM 0x056a
1237 #define USB_DEVICE_ID_WACOM_GRAPHIRE 0x0010
1238 #define USB_DEVICE_ID_WACOM_INTUOS 0x0021
1240 struct hid_blacklist
{
1243 } hid_blacklist
[] = {
1244 { USB_VENDOR_ID_WACOM
, USB_DEVICE_ID_WACOM_INTUOS
},
1245 { USB_VENDOR_ID_WACOM
, USB_DEVICE_ID_WACOM_GRAPHIRE
},
1249 static struct hid_device
*usb_hid_configure(struct usb_device
*dev
, int ifnum
)
1251 struct usb_interface_descriptor
*interface
= dev
->actconfig
->interface
[ifnum
].altsetting
+ 0;
1252 struct hid_descriptor
*hdesc
;
1253 struct hid_device
*hid
;
1257 for (n
= 0; hid_blacklist
[n
].idVendor
; n
++)
1258 if ((hid_blacklist
[n
].idVendor
== dev
->descriptor
.idVendor
) &&
1259 (hid_blacklist
[n
].idProduct
== dev
->descriptor
.idProduct
)) return NULL
;
1261 if (interface
->bInterfaceClass
!= USB_INTERFACE_CLASS_HID
)
1264 if (usb_get_extra_descriptor(interface
, USB_DT_HID
, &hdesc
)
1265 && usb_get_extra_descriptor(&interface
->endpoint
[0], USB_DT_HID
, &hdesc
)) {
1266 dbg("class descriptor not present\n");
1270 for (n
= 0; n
< hdesc
->bNumDescriptors
; n
++)
1271 if (hdesc
->desc
[n
].bDescriptorType
== USB_DT_REPORT
)
1272 rsize
= le16_to_cpu(hdesc
->desc
[n
].wDescriptorLength
);
1274 if (!rsize
|| rsize
> 1024) {
1275 dbg("weird size of report descriptor (%u)", rsize
);
1282 if ((n
= usb_get_class_descriptor(dev
, interface
->bInterfaceNumber
, USB_DT_REPORT
, 0, rdesc
, rsize
)) < 0) {
1283 dbg("reading report descriptor failed");
1288 printk(KERN_DEBUG __FILE__
": report (size %u, read %d) = ", rsize
, n
);
1289 for (n
= 0; n
< rsize
; n
++)
1290 printk(" %02x", (unsigned) rdesc
[n
]);
1294 if (!(hid
= hid_parse_report(rdesc
, rsize
))) {
1295 dbg("parsing report descriptor failed");
1300 for (n
= 0; n
< interface
->bNumEndpoints
; n
++) {
1302 struct usb_endpoint_descriptor
*endpoint
= &interface
->endpoint
[n
];
1305 if ((endpoint
->bmAttributes
& 3) != 3) /* Not an interrupt endpoint */
1308 if (!(endpoint
->bEndpointAddress
& 0x80)) /* Not an input endpoint */
1311 pipe
= usb_rcvintpipe(dev
, endpoint
->bEndpointAddress
);
1312 maxp
= usb_maxpacket(dev
, pipe
, usb_pipeout(pipe
));
1314 FILL_INT_URB(&hid
->urb
, dev
, pipe
, hid
->buffer
, maxp
> 32 ? 32 : maxp
, hid_irq
, hid
, endpoint
->bInterval
);
1316 if (usb_submit_urb(&hid
->urb
)) {
1317 dbg("submitting interrupt URB failed");
1324 if (n
== interface
->bNumEndpoints
) {
1325 dbg("couldn't find an input interrupt endpoint");
1326 hid_free_device(hid
);
1330 hid
->version
= hdesc
->bcdHID
;
1331 hid
->country
= hdesc
->bCountryCode
;
1333 hid
->ifnum
= interface
->bInterfaceNumber
;
1335 hid
->dr
.requesttype
= USB_TYPE_CLASS
| USB_RECIP_INTERFACE
;
1336 hid
->dr
.request
= USB_REQ_SET_REPORT
;
1337 hid
->dr
.value
= 0x200;
1338 hid
->dr
.index
= hid
->ifnum
;
1341 FILL_CONTROL_URB(&hid
->urbout
, dev
, usb_sndctrlpipe(dev
, 0),
1342 (void*) &hid
->dr
, hid
->bufout
, 1, hid_ctrl
, hid
);
1344 if (interface
->bInterfaceSubClass
== 1)
1345 usb_set_protocol(dev
, hid
->ifnum
, 1);
1350 static void* hid_probe(struct usb_device
*dev
, unsigned int ifnum
)
1352 char *hid_name
[] = {"Device", "Pointer", "Mouse", "Device", "Joystick",
1353 "Gamepad", "Keyboard", "Keypad", "Multi-Axis Controller"};
1354 struct hid_device
*hid
;
1356 dbg("HID probe called for ifnum %d", ifnum
);
1358 if (!(hid
= usb_hid_configure(dev
, ifnum
)))
1361 hid_dump_device(hid
);
1363 hid_init_input(hid
);
1364 input_register_device(&hid
->input
);
1366 printk(KERN_INFO
"input%d: USB HID v%x.%02x %s\n",
1367 hid
->input
.number
, hid
->version
>> 8, hid
->version
& 0xff,
1368 (hid
->application
& 0xffff) <= 8 ? hid_name
[hid
->application
& 0xffff] : "device");
1373 static void hid_disconnect(struct usb_device
*dev
, void *ptr
)
1375 struct hid_device
*hid
= ptr
;
1377 dbg("cleanup called");
1378 usb_unlink_urb(&hid
->urb
);
1379 input_unregister_device(&hid
->input
);
1380 hid_free_device(hid
);
1383 static struct usb_driver hid_driver
= {
1386 disconnect
: hid_disconnect
1390 void cleanup_module(void)
1392 usb_deregister(&hid_driver
);
1395 int init_module(void)
1400 usb_register(&hid_driver
);
1404 __initcall(hid_init
);