xref: /freebsd/sys/dev/usb/usb_hid.c (revision 5bb3134a8c21cb87b30e135ef168483f0333dabb)
1 /* $FreeBSD$ */
2 /*	$NetBSD: hid.c,v 1.17 2001/11/13 06:24:53 lukem Exp $	*/
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5  *
6  * Copyright (c) 1998 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Lennart Augustsson (lennart@augustsson.net) at
11  * Carlstedt Research & Technology.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
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.
21  *
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.
33  */
34 
35 #ifdef USB_GLOBAL_INCLUDE_FILE
36 #include USB_GLOBAL_INCLUDE_FILE
37 #else
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>
45 #include <sys/bus.h>
46 #include <sys/module.h>
47 #include <sys/lock.h>
48 #include <sys/mutex.h>
49 #include <sys/condvar.h>
50 #include <sys/sysctl.h>
51 #include <sys/sx.h>
52 #include <sys/unistd.h>
53 #include <sys/callout.h>
54 #include <sys/malloc.h>
55 #include <sys/priv.h>
56 
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>
61 
62 #define	USB_DEBUG_VAR usb_debug
63 
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 */
70 
71 /*------------------------------------------------------------------------*
72  *	hid_get_descriptor_from_usb
73  *
74  * This function will search for a HID descriptor between two USB
75  * interface descriptors.
76  *
77  * Return values:
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)
84 {
85 	struct usb_descriptor *desc = (void *)id;
86 
87 	if (desc == NULL) {
88 		return (NULL);
89 	}
90 	while ((desc = usb_desc_foreach(cd, desc))) {
91 		if ((desc->bDescriptorType == UDESC_HID) &&
92 		    (desc->bLength >= USB_HID_DESCRIPTOR_SIZE(0))) {
93 			return (void *)desc;
94 		}
95 		if (desc->bDescriptorType == UDESC_INTERFACE) {
96 			break;
97 		}
98 	}
99 	return (NULL);
100 }
101 
102 /*------------------------------------------------------------------------*
103  *	usbd_req_get_hid_desc
104  *
105  * This function will read out an USB report descriptor from the USB
106  * device.
107  *
108  * Return values:
109  * NULL: Failure.
110  * Else: Success. The pointer should eventually be passed to free().
111  *------------------------------------------------------------------------*/
112 usb_error_t
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)
116 {
117 	struct usb_interface *iface = usbd_get_iface(udev, iface_index);
118 	struct usb_hid_descriptor *hid;
119 	usb_error_t err;
120 
121 	if ((iface == NULL) || (iface->idesc == NULL)) {
122 		return (USB_ERR_INVAL);
123 	}
124 	hid = hid_get_descriptor_from_usb
125 	    (usbd_get_config_descriptor(udev), iface->idesc);
126 
127 	if (hid == NULL) {
128 		return (USB_ERR_IOERROR);
129 	}
130 	*sizep = UGETW(hid->descrs[0].wDescriptorLength);
131 	if (*sizep == 0) {
132 		return (USB_ERR_IOERROR);
133 	}
134 	if (mtx)
135 		mtx_unlock(mtx);
136 
137 	*descp = malloc(*sizep, mem, M_ZERO | M_WAITOK);
138 
139 	if (mtx)
140 		mtx_lock(mtx);
141 
142 	if (*descp == NULL) {
143 		return (USB_ERR_NOMEM);
144 	}
145 	err = usbd_req_get_report_descriptor
146 	    (udev, mtx, *descp, *sizep, iface_index);
147 
148 	if (err) {
149 		free(*descp, mem);
150 		*descp = NULL;
151 		return (err);
152 	}
153 	return (USB_ERR_NORMAL_COMPLETION);
154 }
155