1 /* $FreeBSD: head/sys/dev/usb/usb_hid.c 246122 2013-01-30 15:26:04Z hselasky $ */
2 /* $NetBSD: hid.c,v 1.17 2001/11/13 06:24:53 lukem Exp $ */
6 * Copyright (c) 1998 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Lennart Augustsson (lennart@augustsson.net) at
11 * Carlstedt Research & Technology.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/stdint.h>
36 #include <sys/param.h>
37 #include <sys/queue.h>
38 #include <sys/types.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
42 #include <sys/module.h>
44 #include <sys/mutex.h>
45 #include <sys/condvar.h>
46 #include <sys/sysctl.h>
47 #include <sys/unistd.h>
48 #include <sys/callout.h>
49 #include <sys/malloc.h>
52 #include <bus/u4b/usb.h>
53 #include <bus/u4b/usbdi.h>
54 #include <bus/u4b/usbdi_util.h>
55 #include <bus/u4b/usbhid.h>
57 #define USB_DEBUG_VAR usb_debug
59 #include <bus/u4b/usb_core.h>
60 #include <bus/u4b/usb_debug.h>
61 #include <bus/u4b/usb_process.h>
62 #include <bus/u4b/usb_device.h>
63 #include <bus/u4b/usb_request.h>
65 static void hid_clear_local(struct hid_item
*);
66 static uint8_t hid_get_byte(struct hid_data
*s
, const uint16_t wSize
);
81 struct hid_item cur
[MAXPUSH
];
82 struct hid_pos_data last_pos
[MAXID
];
83 int32_t usages_min
[MAXUSAGE
];
84 int32_t usages_max
[MAXUSAGE
];
85 int32_t usage_last
; /* last seen usage */
86 uint32_t loc_size
; /* last seen size */
87 uint32_t loc_count
; /* last seen count */
88 uint8_t kindset
; /* we have 5 kinds so 8 bits are enough */
89 uint8_t pushlevel
; /* current pushlevel */
90 uint8_t ncount
; /* end usage item count */
91 uint8_t icount
; /* current usage item count */
92 uint8_t nusage
; /* end "usages_min/max" index */
93 uint8_t iusage
; /* current "usages_min/max" index */
94 uint8_t ousage
; /* current "usages_min/max" offset */
95 uint8_t susage
; /* usage set flags */
98 /*------------------------------------------------------------------------*
100 *------------------------------------------------------------------------*/
102 hid_clear_local(struct hid_item
*c
)
108 c
->usage_minimum
= 0;
109 c
->usage_maximum
= 0;
110 c
->designator_index
= 0;
111 c
->designator_minimum
= 0;
112 c
->designator_maximum
= 0;
114 c
->string_minimum
= 0;
115 c
->string_maximum
= 0;
116 c
->set_delimiter
= 0;
120 hid_switch_rid(struct hid_data
*s
, struct hid_item
*c
, int32_t next_rID
)
124 /* check for same report ID - optimise */
126 if (c
->report_ID
== next_rID
)
129 /* save current position for current rID */
131 if (c
->report_ID
== 0) {
134 for (i
= 1; i
!= MAXID
; i
++) {
135 if (s
->last_pos
[i
].rid
== c
->report_ID
)
137 if (s
->last_pos
[i
].rid
== 0)
142 s
->last_pos
[i
].rid
= c
->report_ID
;
143 s
->last_pos
[i
].pos
= c
->loc
.pos
;
146 /* store next report ID */
148 c
->report_ID
= next_rID
;
150 /* lookup last position for next rID */
155 for (i
= 1; i
!= MAXID
; i
++) {
156 if (s
->last_pos
[i
].rid
== next_rID
)
158 if (s
->last_pos
[i
].rid
== 0)
163 s
->last_pos
[i
].rid
= next_rID
;
164 c
->loc
.pos
= s
->last_pos
[i
].pos
;
166 DPRINTF("Out of RID entries, position is set to zero!\n");
171 /*------------------------------------------------------------------------*
173 *------------------------------------------------------------------------*/
175 hid_start_parse(const void *d
, usb_size_t len
, int kindset
)
179 if ((kindset
-1) & kindset
) {
180 DPRINTFN(0, "Only one bit can be "
181 "set in the kindset\n");
185 s
= kmalloc(sizeof *s
, M_TEMP
, M_WAITOK
| M_ZERO
);
187 s
->end
= ((const uint8_t *)d
) + len
;
188 s
->kindset
= kindset
;
192 /*------------------------------------------------------------------------*
194 *------------------------------------------------------------------------*/
196 hid_end_parse(struct hid_data
*s
)
204 /*------------------------------------------------------------------------*
205 * get byte from HID descriptor
206 *------------------------------------------------------------------------*/
208 hid_get_byte(struct hid_data
*s
, const uint16_t wSize
)
215 /* check if end is reached */
219 /* read out a byte */
222 /* check if data pointer can be advanced by "wSize" bytes */
223 if ((s
->end
- ptr
) < wSize
)
234 /*------------------------------------------------------------------------*
236 *------------------------------------------------------------------------*/
238 hid_get_item(struct hid_data
*s
, struct hid_item
*h
)
241 unsigned int bTag
, bType
, bSize
;
249 c
= &s
->cur
[s
->pushlevel
];
252 /* check if there is an array of items */
253 if (s
->icount
< s
->ncount
) {
254 /* get current usage */
255 if (s
->iusage
< s
->nusage
) {
256 dval
= s
->usages_min
[s
->iusage
] + s
->ousage
;
258 s
->usage_last
= dval
;
259 if (dval
== s
->usages_max
[s
->iusage
]) {
266 DPRINTFN(1, "Using last usage\n");
267 dval
= s
->usage_last
;
271 * Only copy HID item, increment position and return
272 * if correct kindset!
274 if (s
->kindset
& (1 << c
->kind
)) {
276 DPRINTFN(1, "%u,%u,%u\n", h
->loc
.pos
,
277 h
->loc
.size
, h
->loc
.count
);
278 c
->loc
.pos
+= c
->loc
.size
* c
->loc
.count
;
283 /* reset state variables */
293 while (s
->p
!= s
->end
) {
295 bSize
= hid_get_byte(s
, 1);
298 bSize
= hid_get_byte(s
, 1);
299 bSize
|= hid_get_byte(s
, 1) << 8;
300 bTag
= hid_get_byte(s
, 1);
301 bType
= 0xff; /* XXX what should it be */
305 bType
= (bSize
>> 2) & 3;
316 dval
= (int8_t)hid_get_byte(s
, 1);
320 dval
= hid_get_byte(s
, 1);
321 dval
|= hid_get_byte(s
, 1) << 8;
322 dval
= (int16_t)dval
;
326 dval
= hid_get_byte(s
, 1);
327 dval
|= hid_get_byte(s
, 1) << 8;
328 dval
|= hid_get_byte(s
, 1) << 16;
329 dval
|= hid_get_byte(s
, 1) << 24;
333 dval
= hid_get_byte(s
, bSize
);
334 DPRINTFN(0, "bad length %u (data=0x%02x)\n",
346 c
->loc
.count
= s
->loc_count
;
347 c
->loc
.size
= s
->loc_size
;
349 if (c
->flags
& HIO_VARIABLE
) {
350 /* range check usage count */
351 if (c
->loc
.count
> 255) {
352 DPRINTFN(0, "Number of "
353 "items truncated to 255\n");
356 s
->ncount
= c
->loc
.count
;
359 * The "top" loop will return
369 c
->kind
= hid_output
;
372 case 10: /* Collection */
373 c
->kind
= hid_collection
;
374 c
->collection
= dval
;
376 c
->usage
= s
->usage_last
;
379 case 11: /* Feature */
380 c
->kind
= hid_feature
;
383 case 12: /* End collection */
384 c
->kind
= hid_endcollection
;
385 if (c
->collevel
== 0) {
386 DPRINTFN(0, "invalid end collection\n");
393 DPRINTFN(0, "Main bTag=%d\n", bTag
);
400 c
->_usage_page
= dval
<< 16;
403 c
->logical_minimum
= dval
;
406 c
->logical_maximum
= dval
;
409 c
->physical_minimum
= dval
;
412 c
->physical_maximum
= dval
;
415 c
->unit_exponent
= dval
;
421 /* mask because value is unsigned */
422 s
->loc_size
= dval
& mask
;
425 hid_switch_rid(s
, c
, dval
& mask
);
428 /* mask because value is unsigned */
429 s
->loc_count
= dval
& mask
;
433 if (s
->pushlevel
< MAXPUSH
) {
434 s
->cur
[s
->pushlevel
] = *c
;
435 /* store size and count */
436 c
->loc
.size
= s
->loc_size
;
437 c
->loc
.count
= s
->loc_count
;
438 /* update current item pointer */
439 c
= &s
->cur
[s
->pushlevel
];
441 DPRINTFN(0, "Cannot push "
442 "item @ %d\n", s
->pushlevel
);
447 if (s
->pushlevel
< MAXPUSH
) {
448 /* preserve position */
450 c
= &s
->cur
[s
->pushlevel
];
451 /* restore size and count */
452 s
->loc_size
= c
->loc
.size
;
453 s
->loc_count
= c
->loc
.count
;
454 /* set default item location */
459 DPRINTFN(0, "Cannot pop "
460 "item @ %d\n", s
->pushlevel
);
464 DPRINTFN(0, "Global bTag=%d\n", bTag
);
472 dval
= (dval
& mask
) | c
->_usage_page
;
474 /* set last usage, in case of a collection */
475 s
->usage_last
= dval
;
477 if (s
->nusage
< MAXUSAGE
) {
478 s
->usages_min
[s
->nusage
] = dval
;
479 s
->usages_max
[s
->nusage
] = dval
;
482 DPRINTFN(0, "max usage reached\n");
485 /* clear any pending usage sets */
492 dval
= (dval
& mask
) | c
->_usage_page
;
493 c
->usage_minimum
= dval
;
500 dval
= (dval
& mask
) | c
->_usage_page
;
501 c
->usage_maximum
= dval
;
508 if ((s
->nusage
< MAXUSAGE
) &&
509 (c
->usage_minimum
<= c
->usage_maximum
)) {
510 /* add usage range */
511 s
->usages_min
[s
->nusage
] =
513 s
->usages_max
[s
->nusage
] =
517 DPRINTFN(0, "Usage set dropped\n");
522 c
->designator_index
= dval
;
525 c
->designator_minimum
= dval
;
528 c
->designator_maximum
= dval
;
531 c
->string_index
= dval
;
534 c
->string_minimum
= dval
;
537 c
->string_maximum
= dval
;
540 c
->set_delimiter
= dval
;
543 DPRINTFN(0, "Local bTag=%d\n", bTag
);
548 DPRINTFN(0, "default bType=%d\n", bType
);
555 /*------------------------------------------------------------------------*
557 *------------------------------------------------------------------------*/
559 hid_report_size(const void *buf
, usb_size_t len
, enum hid_kind k
, uint8_t *id
)
572 for (d
= hid_start_parse(buf
, len
, 1 << k
); hid_get_item(d
, &h
);) {
574 /* check for ID-byte presense */
575 if ((h
.report_ID
!= 0) && !any_id
) {
580 /* compute minimum */
581 if (lpos
> h
.loc
.pos
)
583 /* compute end position */
584 temp
= h
.loc
.pos
+ (h
.loc
.size
* h
.loc
.count
);
585 /* compute maximum */
592 /* safety check - can happen in case of currupt descriptors */
598 /* check for ID byte */
604 /* return length in bytes rounded up */
605 return ((temp
+ 7) / 8);
608 /*------------------------------------------------------------------------*
610 *------------------------------------------------------------------------*/
612 hid_locate(const void *desc
, usb_size_t size
, int32_t u
, enum hid_kind k
,
613 uint8_t index
, struct hid_location
*loc
, uint32_t *flags
, uint8_t *id
)
618 for (d
= hid_start_parse(desc
, size
, 1 << k
); hid_get_item(d
, &h
);) {
619 if (h
.kind
== k
&& !(h
.flags
& HIO_CONST
) && h
.usage
== u
) {
642 /*------------------------------------------------------------------------*
644 *------------------------------------------------------------------------*/
646 hid_get_data_sub(const uint8_t *buf
, usb_size_t len
, struct hid_location
*loc
,
649 uint32_t hpos
= loc
->pos
;
650 uint32_t hsize
= loc
->size
;
655 DPRINTFN(11, "hid_get_data: loc %d/%d\n", hpos
, hsize
);
657 /* Range check and limit */
663 /* Get data in a safe way */
671 data
|= buf
[rpos
] << (8 * n
);
674 /* Correctly shift down data */
675 data
= (data
>> (hpos
% 8));
678 /* Mask and sign extend in one */
680 data
= (int32_t)((int32_t)data
<< n
) >> n
;
682 data
= (uint32_t)((uint32_t)data
<< n
) >> n
;
684 DPRINTFN(11, "hid_get_data: loc %d/%d = %lu\n",
685 loc
->pos
, loc
->size
, (long)data
);
690 hid_get_data(const uint8_t *buf
, usb_size_t len
, struct hid_location
*loc
)
692 return (hid_get_data_sub(buf
, len
, loc
, 1));
696 hid_get_data_unsigned(const uint8_t *buf
, usb_size_t len
, struct hid_location
*loc
)
698 return (hid_get_data_sub(buf
, len
, loc
, 0));
701 /*------------------------------------------------------------------------*
703 *------------------------------------------------------------------------*/
705 hid_put_data_unsigned(uint8_t *buf
, usb_size_t len
,
706 struct hid_location
*loc
, unsigned int value
)
708 uint32_t hpos
= loc
->pos
;
709 uint32_t hsize
= loc
->size
;
715 DPRINTFN(11, "hid_put_data: loc %d/%d = %u\n", hpos
, hsize
, value
);
717 /* Range check and limit */
723 /* Put data in a safe way */
726 data
= ((uint64_t)value
) << (hpos
% 8);
727 mask
= ((1ULL << hsize
) - 1ULL) << (hpos
% 8);
732 buf
[rpos
] &= ~(mask
>> (8 * n
));
733 buf
[rpos
] |= (data
>> (8 * n
));
738 /*------------------------------------------------------------------------*
740 *------------------------------------------------------------------------*/
742 hid_is_collection(const void *desc
, usb_size_t size
, int32_t usage
)
748 hd
= hid_start_parse(desc
, size
, hid_input
);
752 while ((err
= hid_get_item(hd
, &hi
))) {
753 if (hi
.kind
== hid_collection
&&
761 /*------------------------------------------------------------------------*
762 * hid_get_descriptor_from_usb
764 * This function will search for a HID descriptor between two USB
765 * interface descriptors.
768 * NULL: No more HID descriptors.
769 * Else: Pointer to HID descriptor.
770 *------------------------------------------------------------------------*/
771 struct usb_hid_descriptor
*
772 hid_get_descriptor_from_usb(struct usb_config_descriptor
*cd
,
773 struct usb_interface_descriptor
*id
)
775 struct usb_descriptor
*desc
= (void *)id
;
780 while ((desc
= usb_desc_foreach(cd
, desc
))) {
781 if ((desc
->bDescriptorType
== UDESC_HID
) &&
782 (desc
->bLength
>= USB_HID_DESCRIPTOR_SIZE(0))) {
785 if (desc
->bDescriptorType
== UDESC_INTERFACE
) {
792 /*------------------------------------------------------------------------*
793 * usbd_req_get_hid_desc
795 * This function will read out an USB report descriptor from the USB
800 * Else: Success. The pointer should eventually be passed to free().
801 *------------------------------------------------------------------------*/
803 usbd_req_get_hid_desc(struct usb_device
*udev
, struct lock
*lock
,
804 void **descp
, uint16_t *sizep
,
805 struct malloc_type
*mem
, uint8_t iface_index
)
807 struct usb_interface
*iface
= usbd_get_iface(udev
, iface_index
);
808 struct usb_hid_descriptor
*hid
;
811 if ((iface
== NULL
) || (iface
->idesc
== NULL
)) {
812 return (USB_ERR_INVAL
);
814 hid
= hid_get_descriptor_from_usb
815 (usbd_get_config_descriptor(udev
), iface
->idesc
);
818 return (USB_ERR_IOERROR
);
820 *sizep
= UGETW(hid
->descrs
[0].wDescriptorLength
);
822 return (USB_ERR_IOERROR
);
825 lockmgr(lock
, LK_RELEASE
);
827 *descp
= kmalloc(*sizep
, mem
, M_ZERO
| M_WAITOK
);
830 lockmgr(lock
, LK_EXCLUSIVE
);
832 if (*descp
== NULL
) {
833 return (USB_ERR_NOMEM
);
835 err
= usbd_req_get_report_descriptor
836 (udev
, lock
, *descp
, *sizep
, iface_index
);
843 return (USB_ERR_NORMAL_COMPLETION
);
846 /*------------------------------------------------------------------------*
849 * This function will decide if a USB descriptor belongs to a USB mouse.
852 * Zero: Not a USB mouse.
853 * Else: Is a USB mouse.
854 *------------------------------------------------------------------------*/
856 hid_is_mouse(const void *d_ptr
, uint16_t d_len
)
863 hd
= hid_start_parse(d_ptr
, d_len
, 1 << hid_input
);
870 while (hid_get_item(hd
, &hi
)) {
875 else if (hi
.collection
== 1 &&
877 HID_USAGE2(HUP_GENERIC_DESKTOP
, HUG_MOUSE
))
880 case hid_endcollection
:
888 HID_USAGE2(HUP_GENERIC_DESKTOP
, HUG_X
) &&
889 (hi
.flags
& (HIO_CONST
|HIO_RELATIVE
)) == HIO_RELATIVE
)
892 HID_USAGE2(HUP_GENERIC_DESKTOP
, HUG_Y
) &&
893 (hi
.flags
& (HIO_CONST
|HIO_RELATIVE
)) == HIO_RELATIVE
)
904 /*------------------------------------------------------------------------*
907 * This function will decide if a USB descriptor belongs to a USB keyboard.
910 * Zero: Not a USB keyboard.
911 * Else: Is a USB keyboard.
912 *------------------------------------------------------------------------*/
914 hid_is_keyboard(const void *d_ptr
, uint16_t d_len
)
916 if (hid_is_collection(d_ptr
, d_len
,
917 HID_USAGE2(HUP_GENERIC_DESKTOP
, HUG_KEYBOARD
)))