1 /* $NetBSD: hid.c,v 1.17 2001/11/13 06:24:53 lukem Exp $ */
3 * SPDX-License-Identifier: BSD-2-Clause
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart@augustsson.net) at
10 * Carlstedt Research & Technology.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 #ifdef USB_GLOBAL_INCLUDE_FILE
35 #include USB_GLOBAL_INCLUDE_FILE
37 #include <sys/stdint.h>
38 #include <sys/stddef.h>
39 #include <sys/param.h>
40 #include <sys/queue.h>
41 #include <sys/types.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
45 #include <sys/module.h>
47 #include <sys/mutex.h>
48 #include <sys/condvar.h>
49 #include <sys/sysctl.h>
51 #include <sys/unistd.h>
52 #include <sys/callout.h>
53 #include <sys/malloc.h>
56 #include <dev/usb/usb.h>
57 #include <dev/usb/usbdi.h>
58 #include <dev/usb/usbdi_util.h>
59 #include <dev/usb/usbhid.h>
61 #define USB_DEBUG_VAR usb_debug
63 #include <dev/usb/usb_core.h>
64 #include <dev/usb/usb_debug.h>
65 #include <dev/usb/usb_process.h>
66 #include <dev/usb/usb_device.h>
67 #include <dev/usb/usb_request.h>
68 #endif /* USB_GLOBAL_INCLUDE_FILE */
70 /*------------------------------------------------------------------------*
71 * hid_get_descriptor_from_usb
73 * This function will search for a HID descriptor between two USB
74 * interface descriptors.
77 * NULL: No more HID descriptors.
78 * Else: Pointer to HID descriptor.
79 *------------------------------------------------------------------------*/
80 struct usb_hid_descriptor *
81 hid_get_descriptor_from_usb(struct usb_config_descriptor *cd,
82 struct usb_interface_descriptor *id)
84 struct usb_descriptor *desc = (void *)id;
89 while ((desc = usb_desc_foreach(cd, desc))) {
90 if ((desc->bDescriptorType == UDESC_HID) &&
91 (desc->bLength >= USB_HID_DESCRIPTOR_SIZE(0))) {
94 if (desc->bDescriptorType == UDESC_INTERFACE) {
101 /*------------------------------------------------------------------------*
102 * usbd_req_get_hid_desc
104 * This function will read out an USB report descriptor from the USB
109 * Else: Success. The pointer should eventually be passed to free().
110 *------------------------------------------------------------------------*/
112 usbd_req_get_hid_desc(struct usb_device *udev, struct mtx *mtx,
113 void **descp, uint16_t *sizep,
114 struct malloc_type *mem, uint8_t iface_index)
116 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
117 struct usb_hid_descriptor *hid;
120 if ((iface == NULL) || (iface->idesc == NULL)) {
121 return (USB_ERR_INVAL);
123 hid = hid_get_descriptor_from_usb
124 (usbd_get_config_descriptor(udev), iface->idesc);
127 return (USB_ERR_IOERROR);
129 *sizep = UGETW(hid->descrs[0].wDescriptorLength);
131 return (USB_ERR_IOERROR);
136 *descp = malloc(*sizep, mem, M_ZERO | M_WAITOK);
141 if (*descp == NULL) {
142 return (USB_ERR_NOMEM);
144 err = usbd_req_get_report_descriptor
145 (udev, mtx, *descp, *sizep, iface_index);
152 return (USB_ERR_NORMAL_COMPLETION);