FreeBSD kernel usb device Code
usb_hid.c
Go to the documentation of this file.
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 *------------------------------------------------------------------------*/
81struct usb_hid_descriptor *
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) &&
93 return (void *)desc;
94 }
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 *------------------------------------------------------------------------*/
113usbd_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 }
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 }
146 (udev, mtx, *descp, *sizep, iface_index);
147
148 if (err) {
149 free(*descp, mem);
150 *descp = NULL;
151 return (err);
152 }
154}
uByte bDescriptorType
Definition: usb.h:527
uWord wDescriptorLength
Definition: usbhid.h:60
struct usb_hid_descriptor::@92 descrs[1]
struct usb_interface_descriptor * idesc
Definition: usbdi.h:175
#define UDESC_INTERFACE
Definition: usb.h:199
struct usb_config_descriptor * usbd_get_config_descriptor(struct usb_device *udev)
Definition: usb_device.c:2616
struct usb_interface * usbd_get_iface(struct usb_device *udev, uint8_t iface_index)
Definition: usb_device.c:2370
struct usb_endpoint_descriptor desc
Definition: usb_device.h:0
#define UGETW(w)
Definition: usb_endian.h:53
usb_error_t usbd_req_get_hid_desc(struct usb_device *udev, struct mtx *mtx, void **descp, uint16_t *sizep, struct malloc_type *mem, uint8_t iface_index)
Definition: usb_hid.c:113
struct usb_hid_descriptor * hid_get_descriptor_from_usb(struct usb_config_descriptor *cd, struct usb_interface_descriptor *id)
Definition: usb_hid.c:82
struct usb_descriptor * usb_desc_foreach(struct usb_config_descriptor *cd, struct usb_descriptor *_desc)
Definition: usb_parse.c:74
usb_error_t usbd_req_get_report_descriptor(struct usb_device *udev, struct mtx *mtx, void *d, uint16_t size, uint8_t iface_index)
Definition: usb_request.c:1890
usb_error_t
Definition: usbdi.h:45
@ USB_ERR_NORMAL_COMPLETION
Definition: usbdi.h:46
@ USB_ERR_IOERROR
Definition: usbdi.h:64
@ USB_ERR_NOMEM
Definition: usbdi.h:50
@ USB_ERR_INVAL
Definition: usbdi.h:49
#define UDESC_HID
Definition: usbhid.h:41
#define USB_HID_DESCRIPTOR_SIZE(n)
Definition: usbhid.h:64