2 /* $NetBSD: hid.c,v 1.17 2001/11/13 06:24:53 lukem Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
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 #ifdef USB_GLOBAL_INCLUDE_FILE
36 #include USB_GLOBAL_INCLUDE_FILE
38 #include <sys/stdint.h>
39 #include <sys/stddef.h>
40 #include <sys/param.h>
41 #include <sys/queue.h>
42 #include <sys/types.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/module.h>
48 #include <sys/mutex.h>
49 #include <sys/condvar.h>
50 #include <sys/sysctl.h>
52 #include <sys/unistd.h>
53 #include <sys/callout.h>
54 #include <sys/malloc.h>
57 #include <dev/usb/usb.h>
58 #include <dev/usb/usbdi.h>
59 #include <dev/usb/usbdi_util.h>
60 #include <dev/usb/usbhid.h>
62 #define USB_DEBUG_VAR usb_debug
64 #include <dev/usb/usb_core.h>
65 #include <dev/usb/usb_debug.h>
66 #include <dev/usb/usb_process.h>
67 #include <dev/usb/usb_device.h>
68 #include <dev/usb/usb_request.h>
69 #endif /* USB_GLOBAL_INCLUDE_FILE */
71 /*------------------------------------------------------------------------*
72 * hid_get_descriptor_from_usb
74 * This function will search for a HID descriptor between two USB
75 * interface descriptors.
78 * NULL: No more HID descriptors.
79 * Else: Pointer to HID descriptor.
80 *------------------------------------------------------------------------*/
81 struct usb_hid_descriptor *
82 hid_get_descriptor_from_usb(struct usb_config_descriptor *cd,
83 struct usb_interface_descriptor *id)
85 struct usb_descriptor *desc = (void *)id;
90 while ((desc = usb_desc_foreach(cd, desc))) {
91 if ((desc->bDescriptorType == UDESC_HID) &&
92 (desc->bLength >= USB_HID_DESCRIPTOR_SIZE(0))) {
95 if (desc->bDescriptorType == UDESC_INTERFACE) {
102 /*------------------------------------------------------------------------*
103 * usbd_req_get_hid_desc
105 * This function will read out an USB report descriptor from the USB
110 * Else: Success. The pointer should eventually be passed to free().
111 *------------------------------------------------------------------------*/
113 usbd_req_get_hid_desc(struct usb_device *udev, struct mtx *mtx,
114 void **descp, uint16_t *sizep,
115 struct malloc_type *mem, uint8_t iface_index)
117 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
118 struct usb_hid_descriptor *hid;
121 if ((iface == NULL) || (iface->idesc == NULL)) {
122 return (USB_ERR_INVAL);
124 hid = hid_get_descriptor_from_usb
125 (usbd_get_config_descriptor(udev), iface->idesc);
128 return (USB_ERR_IOERROR);
130 *sizep = UGETW(hid->descrs[0].wDescriptorLength);
132 return (USB_ERR_IOERROR);
137 *descp = malloc(*sizep, mem, M_ZERO | M_WAITOK);
142 if (*descp == NULL) {
143 return (USB_ERR_NOMEM);
145 err = usbd_req_get_report_descriptor
146 (udev, mtx, *descp, *sizep, iface_index);
153 return (USB_ERR_NORMAL_COMPLETION);