1f9f848faSopenharmony_ci/*-
2f9f848faSopenharmony_ci * SPDX-License-Identifier: BSD-2-Clause
3f9f848faSopenharmony_ci *
4f9f848faSopenharmony_ci * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
5f9f848faSopenharmony_ci * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
6f9f848faSopenharmony_ci * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
7f9f848faSopenharmony_ci *
8f9f848faSopenharmony_ci * Redistribution and use in source and binary forms, with or without
9f9f848faSopenharmony_ci * modification, are permitted provided that the following conditions
10f9f848faSopenharmony_ci * are met:
11f9f848faSopenharmony_ci * 1. Redistributions of source code must retain the above copyright
12f9f848faSopenharmony_ci *    notice, this list of conditions and the following disclaimer.
13f9f848faSopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright
14f9f848faSopenharmony_ci *    notice, this list of conditions and the following disclaimer in the
15f9f848faSopenharmony_ci *    documentation and/or other materials provided with the distribution.
16f9f848faSopenharmony_ci *
17f9f848faSopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18f9f848faSopenharmony_ci * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19f9f848faSopenharmony_ci * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20f9f848faSopenharmony_ci * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21f9f848faSopenharmony_ci * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22f9f848faSopenharmony_ci * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23f9f848faSopenharmony_ci * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24f9f848faSopenharmony_ci * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25f9f848faSopenharmony_ci * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26f9f848faSopenharmony_ci * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27f9f848faSopenharmony_ci * SUCH DAMAGE.
28f9f848faSopenharmony_ci */
29f9f848faSopenharmony_ci
30f9f848faSopenharmony_ci#ifndef _USB_HID_H_
31f9f848faSopenharmony_ci#define	_USB_HID_H_
32f9f848faSopenharmony_ci
33f9f848faSopenharmony_ci#define	UR_GET_HID_DESCRIPTOR	0x06
34f9f848faSopenharmony_ci#define	UDESC_HID		0x21
35f9f848faSopenharmony_ci#define	UDESC_REPORT		0x22
36f9f848faSopenharmony_ci#define	UDESC_PHYSICAL		0x23
37f9f848faSopenharmony_ci#define	UR_SET_HID_DESCRIPTOR	0x07
38f9f848faSopenharmony_ci#define	UR_GET_REPORT		0x01
39f9f848faSopenharmony_ci#define	UR_SET_REPORT		0x09
40f9f848faSopenharmony_ci#define	UR_GET_IDLE		0x02
41f9f848faSopenharmony_ci#define	UR_SET_IDLE		0x0a
42f9f848faSopenharmony_ci#define	UR_GET_PROTOCOL		0x03
43f9f848faSopenharmony_ci#define	UR_SET_PROTOCOL		0x0b
44f9f848faSopenharmony_ci
45f9f848faSopenharmony_cistruct usb_hid_descriptor {
46f9f848faSopenharmony_ci	uByte	bLength;
47f9f848faSopenharmony_ci	uByte	bDescriptorType;
48f9f848faSopenharmony_ci	uWord	bcdHID;
49f9f848faSopenharmony_ci	uByte	bCountryCode;
50f9f848faSopenharmony_ci	uByte	bNumDescriptors;
51f9f848faSopenharmony_ci	struct {
52f9f848faSopenharmony_ci		uByte	bDescriptorType;
53f9f848faSopenharmony_ci		uWord	wDescriptorLength;
54f9f848faSopenharmony_ci	}	descrs[1];
55f9f848faSopenharmony_ci} __packed;
56f9f848faSopenharmony_ci
57f9f848faSopenharmony_ci#define	USB_HID_DESCRIPTOR_SIZE(n) (9+((n)*3))
58f9f848faSopenharmony_ci
59f9f848faSopenharmony_ci/* Usage pages */
60f9f848faSopenharmony_ci#define	HUP_UNDEFINED		0x0000
61f9f848faSopenharmony_ci#define	HUP_GENERIC_DESKTOP	0x0001
62f9f848faSopenharmony_ci#define	HUP_SIMULATION		0x0002
63f9f848faSopenharmony_ci#define	HUP_VR_CONTROLS		0x0003
64f9f848faSopenharmony_ci#define	HUP_SPORTS_CONTROLS	0x0004
65f9f848faSopenharmony_ci#define	HUP_GAMING_CONTROLS	0x0005
66f9f848faSopenharmony_ci#define	HUP_KEYBOARD		0x0007
67f9f848faSopenharmony_ci#define	HUP_LEDS		0x0008
68f9f848faSopenharmony_ci#define	HUP_BUTTON		0x0009
69f9f848faSopenharmony_ci#define	HUP_ORDINALS		0x000a
70f9f848faSopenharmony_ci#define	HUP_TELEPHONY		0x000b
71f9f848faSopenharmony_ci#define	HUP_CONSUMER		0x000c
72f9f848faSopenharmony_ci#define	HUP_DIGITIZERS		0x000d
73f9f848faSopenharmony_ci#define	HUP_PHYSICAL_IFACE	0x000e
74f9f848faSopenharmony_ci#define	HUP_UNICODE		0x0010
75f9f848faSopenharmony_ci#define	HUP_ALPHANUM_DISPLAY	0x0014
76f9f848faSopenharmony_ci#define	HUP_MONITOR		0x0080
77f9f848faSopenharmony_ci#define	HUP_MONITOR_ENUM_VAL	0x0081
78f9f848faSopenharmony_ci#define	HUP_VESA_VC		0x0082
79f9f848faSopenharmony_ci#define	HUP_VESA_CMD		0x0083
80f9f848faSopenharmony_ci#define	HUP_POWER		0x0084
81f9f848faSopenharmony_ci#define	HUP_BATTERY_SYSTEM	0x0085
82f9f848faSopenharmony_ci#define	HUP_BARCODE_SCANNER	0x008b
83f9f848faSopenharmony_ci#define	HUP_SCALE		0x008c
84f9f848faSopenharmony_ci#define	HUP_CAMERA_CONTROL	0x0090
85f9f848faSopenharmony_ci#define	HUP_ARCADE		0x0091
86f9f848faSopenharmony_ci#define	HUP_MICROSOFT		0xff00
87f9f848faSopenharmony_ci
88f9f848faSopenharmony_ci/* Usages, generic desktop */
89f9f848faSopenharmony_ci#define	HUG_POINTER		0x0001
90f9f848faSopenharmony_ci#define	HUG_MOUSE		0x0002
91f9f848faSopenharmony_ci#define	HUG_JOYSTICK		0x0004
92f9f848faSopenharmony_ci#define	HUG_GAME_PAD		0x0005
93f9f848faSopenharmony_ci#define	HUG_KEYBOARD		0x0006
94f9f848faSopenharmony_ci#define	HUG_KEYPAD		0x0007
95f9f848faSopenharmony_ci#define	HUG_X			0x0030
96f9f848faSopenharmony_ci#define	HUG_Y			0x0031
97f9f848faSopenharmony_ci#define	HUG_Z			0x0032
98f9f848faSopenharmony_ci#define	HUG_RX			0x0033
99f9f848faSopenharmony_ci#define	HUG_RY			0x0034
100f9f848faSopenharmony_ci#define	HUG_RZ			0x0035
101f9f848faSopenharmony_ci#define	HUG_SLIDER		0x0036
102f9f848faSopenharmony_ci#define	HUG_DIAL		0x0037
103f9f848faSopenharmony_ci#define	HUG_WHEEL		0x0038
104f9f848faSopenharmony_ci#define	HUG_HAT_SWITCH		0x0039
105f9f848faSopenharmony_ci#define	HUG_COUNTED_BUFFER	0x003a
106f9f848faSopenharmony_ci#define	HUG_BYTE_COUNT		0x003b
107f9f848faSopenharmony_ci#define	HUG_MOTION_WAKEUP	0x003c
108f9f848faSopenharmony_ci#define	HUG_VX			0x0040
109f9f848faSopenharmony_ci#define	HUG_VY			0x0041
110f9f848faSopenharmony_ci#define	HUG_VZ			0x0042
111f9f848faSopenharmony_ci#define	HUG_VBRX		0x0043
112f9f848faSopenharmony_ci#define	HUG_VBRY		0x0044
113f9f848faSopenharmony_ci#define	HUG_VBRZ		0x0045
114f9f848faSopenharmony_ci#define	HUG_VNO			0x0046
115f9f848faSopenharmony_ci#define	HUG_TWHEEL		0x0048	/* M$ Wireless Intellimouse Wheel */
116f9f848faSopenharmony_ci#define	HUG_SYSTEM_CONTROL	0x0080
117f9f848faSopenharmony_ci#define	HUG_SYSTEM_POWER_DOWN	0x0081
118f9f848faSopenharmony_ci#define	HUG_SYSTEM_SLEEP	0x0082
119f9f848faSopenharmony_ci#define	HUG_SYSTEM_WAKEUP	0x0083
120f9f848faSopenharmony_ci#define	HUG_SYSTEM_CONTEXT_MENU	0x0084
121f9f848faSopenharmony_ci#define	HUG_SYSTEM_MAIN_MENU	0x0085
122f9f848faSopenharmony_ci#define	HUG_SYSTEM_APP_MENU	0x0086
123f9f848faSopenharmony_ci#define	HUG_SYSTEM_MENU_HELP	0x0087
124f9f848faSopenharmony_ci#define	HUG_SYSTEM_MENU_EXIT	0x0088
125f9f848faSopenharmony_ci#define	HUG_SYSTEM_MENU_SELECT	0x0089
126f9f848faSopenharmony_ci#define	HUG_SYSTEM_MENU_RIGHT	0x008a
127f9f848faSopenharmony_ci#define	HUG_SYSTEM_MENU_LEFT	0x008b
128f9f848faSopenharmony_ci#define	HUG_SYSTEM_MENU_UP	0x008c
129f9f848faSopenharmony_ci#define	HUG_SYSTEM_MENU_DOWN	0x008d
130f9f848faSopenharmony_ci#define	HUG_APPLE_EJECT		0x00b8
131f9f848faSopenharmony_ci
132f9f848faSopenharmony_ci/* Usages Digitizers */
133f9f848faSopenharmony_ci#define	HUD_UNDEFINED		0x0000
134f9f848faSopenharmony_ci#define	HUD_DIGITIZER		0x0001
135f9f848faSopenharmony_ci#define	HUD_PEN			0x0002
136f9f848faSopenharmony_ci#define	HUD_TOUCHSCREEN		0x0004
137f9f848faSopenharmony_ci#define	HUD_TOUCHPAD		0x0005
138f9f848faSopenharmony_ci#define	HUD_CONFIG		0x000e
139f9f848faSopenharmony_ci#define	HUD_FINGER		0x0022
140f9f848faSopenharmony_ci#define	HUD_TIP_PRESSURE	0x0030
141f9f848faSopenharmony_ci#define	HUD_BARREL_PRESSURE	0x0031
142f9f848faSopenharmony_ci#define	HUD_IN_RANGE		0x0032
143f9f848faSopenharmony_ci#define	HUD_TOUCH		0x0033
144f9f848faSopenharmony_ci#define	HUD_UNTOUCH		0x0034
145f9f848faSopenharmony_ci#define	HUD_TAP			0x0035
146f9f848faSopenharmony_ci#define	HUD_QUALITY		0x0036
147f9f848faSopenharmony_ci#define	HUD_DATA_VALID		0x0037
148f9f848faSopenharmony_ci#define	HUD_TRANSDUCER_INDEX	0x0038
149f9f848faSopenharmony_ci#define	HUD_TABLET_FKEYS	0x0039
150f9f848faSopenharmony_ci#define	HUD_PROGRAM_CHANGE_KEYS	0x003a
151f9f848faSopenharmony_ci#define	HUD_BATTERY_STRENGTH	0x003b
152f9f848faSopenharmony_ci#define	HUD_INVERT		0x003c
153f9f848faSopenharmony_ci#define	HUD_X_TILT		0x003d
154f9f848faSopenharmony_ci#define	HUD_Y_TILT		0x003e
155f9f848faSopenharmony_ci#define	HUD_AZIMUTH		0x003f
156f9f848faSopenharmony_ci#define	HUD_ALTITUDE		0x0040
157f9f848faSopenharmony_ci#define	HUD_TWIST		0x0041
158f9f848faSopenharmony_ci#define	HUD_TIP_SWITCH		0x0042
159f9f848faSopenharmony_ci#define	HUD_SEC_TIP_SWITCH	0x0043
160f9f848faSopenharmony_ci#define	HUD_BARREL_SWITCH	0x0044
161f9f848faSopenharmony_ci#define	HUD_ERASER		0x0045
162f9f848faSopenharmony_ci#define	HUD_TABLET_PICK		0x0046
163f9f848faSopenharmony_ci#define	HUD_CONFIDENCE		0x0047
164f9f848faSopenharmony_ci#define	HUD_WIDTH		0x0048
165f9f848faSopenharmony_ci#define	HUD_HEIGHT		0x0049
166f9f848faSopenharmony_ci#define	HUD_CONTACTID		0x0051
167f9f848faSopenharmony_ci#define	HUD_INPUT_MODE		0x0052
168f9f848faSopenharmony_ci#define	HUD_DEVICE_INDEX	0x0053
169f9f848faSopenharmony_ci#define	HUD_CONTACTCOUNT	0x0054
170f9f848faSopenharmony_ci#define	HUD_CONTACT_MAX		0x0055
171f9f848faSopenharmony_ci#define	HUD_SCAN_TIME		0x0056
172f9f848faSopenharmony_ci#define	HUD_BUTTON_TYPE		0x0059
173f9f848faSopenharmony_ci
174f9f848faSopenharmony_ci/* Usages, Consumer */
175f9f848faSopenharmony_ci#define	HUC_AC_PAN		0x0238
176f9f848faSopenharmony_ci
177f9f848faSopenharmony_ci#define	HID_USAGE2(p,u) (((p) << 16) | (u))
178f9f848faSopenharmony_ci
179f9f848faSopenharmony_ci#define	UHID_INPUT_REPORT 0x01
180f9f848faSopenharmony_ci#define	UHID_OUTPUT_REPORT 0x02
181f9f848faSopenharmony_ci#define	UHID_FEATURE_REPORT 0x03
182f9f848faSopenharmony_ci
183f9f848faSopenharmony_ci/* Bits in the input/output/feature items */
184f9f848faSopenharmony_ci#define	HIO_CONST	0x001
185f9f848faSopenharmony_ci#define	HIO_VARIABLE	0x002
186f9f848faSopenharmony_ci#define	HIO_RELATIVE	0x004
187f9f848faSopenharmony_ci#define	HIO_WRAP	0x008
188f9f848faSopenharmony_ci#define	HIO_NONLINEAR	0x010
189f9f848faSopenharmony_ci#define	HIO_NOPREF	0x020
190f9f848faSopenharmony_ci#define	HIO_NULLSTATE	0x040
191f9f848faSopenharmony_ci#define	HIO_VOLATILE	0x080
192f9f848faSopenharmony_ci#define	HIO_BUFBYTES	0x100
193f9f848faSopenharmony_ci
194f9f848faSopenharmony_ci/* Units of Measure */
195f9f848faSopenharmony_ci#define	HUM_CENTIMETER	0x11
196f9f848faSopenharmony_ci#define	HUM_RADIAN	0x12
197f9f848faSopenharmony_ci#define	HUM_INCH	0x13
198f9f848faSopenharmony_ci#define	HUM_DEGREE	0x14
199f9f848faSopenharmony_ci
200f9f848faSopenharmony_ci#ifdef _KERNEL
201f9f848faSopenharmony_cistruct usb_config_descriptor;
202f9f848faSopenharmony_ci
203f9f848faSopenharmony_cienum hid_kind {
204f9f848faSopenharmony_ci	hid_input, hid_output, hid_feature, hid_collection, hid_endcollection
205f9f848faSopenharmony_ci};
206f9f848faSopenharmony_ci
207f9f848faSopenharmony_cistruct hid_location {
208f9f848faSopenharmony_ci	uint32_t size;
209f9f848faSopenharmony_ci	uint32_t count;
210f9f848faSopenharmony_ci	uint32_t pos;
211f9f848faSopenharmony_ci};
212f9f848faSopenharmony_ci
213f9f848faSopenharmony_cistruct hid_item {
214f9f848faSopenharmony_ci	/* Global */
215f9f848faSopenharmony_ci	int32_t	_usage_page;
216f9f848faSopenharmony_ci	int32_t	logical_minimum;
217f9f848faSopenharmony_ci	int32_t	logical_maximum;
218f9f848faSopenharmony_ci	int32_t	physical_minimum;
219f9f848faSopenharmony_ci	int32_t	physical_maximum;
220f9f848faSopenharmony_ci	int32_t	unit_exponent;
221f9f848faSopenharmony_ci	int32_t	unit;
222f9f848faSopenharmony_ci	int32_t	report_ID;
223f9f848faSopenharmony_ci	/* Local */
224f9f848faSopenharmony_ci	int32_t	usage;
225f9f848faSopenharmony_ci	int32_t	usage_minimum;
226f9f848faSopenharmony_ci	int32_t	usage_maximum;
227f9f848faSopenharmony_ci	int32_t	designator_index;
228f9f848faSopenharmony_ci	int32_t	designator_minimum;
229f9f848faSopenharmony_ci	int32_t	designator_maximum;
230f9f848faSopenharmony_ci	int32_t	string_index;
231f9f848faSopenharmony_ci	int32_t	string_minimum;
232f9f848faSopenharmony_ci	int32_t	string_maximum;
233f9f848faSopenharmony_ci	int32_t	set_delimiter;
234f9f848faSopenharmony_ci	/* Misc */
235f9f848faSopenharmony_ci	int32_t	collection;
236f9f848faSopenharmony_ci	int	collevel;
237f9f848faSopenharmony_ci	enum hid_kind kind;
238f9f848faSopenharmony_ci	uint32_t flags;
239f9f848faSopenharmony_ci	/* Location */
240f9f848faSopenharmony_ci	struct hid_location loc;
241f9f848faSopenharmony_ci};
242f9f848faSopenharmony_ci
243f9f848faSopenharmony_ci/* prototypes from "usb_hid.c" */
244f9f848faSopenharmony_ci
245f9f848faSopenharmony_cistruct hid_data *hid_start_parse(const void *d, usb_size_t len, int kindset);
246f9f848faSopenharmony_civoid	hid_end_parse(struct hid_data *s);
247f9f848faSopenharmony_ciint	hid_get_item(struct hid_data *s, struct hid_item *h);
248f9f848faSopenharmony_ciint	hid_report_size(const void *buf, usb_size_t len, enum hid_kind k,
249f9f848faSopenharmony_ci	    uint8_t *id);
250f9f848faSopenharmony_ciint	hid_locate(const void *desc, usb_size_t size, int32_t usage,
251f9f848faSopenharmony_ci	    enum hid_kind kind, uint8_t index, struct hid_location *loc,
252f9f848faSopenharmony_ci	    uint32_t *flags, uint8_t *id);
253f9f848faSopenharmony_ciint32_t hid_get_data(const uint8_t *buf, usb_size_t len,
254f9f848faSopenharmony_ci	    struct hid_location *loc);
255f9f848faSopenharmony_ciuint32_t hid_get_data_unsigned(const uint8_t *buf, usb_size_t len,
256f9f848faSopenharmony_ci	    struct hid_location *loc);
257f9f848faSopenharmony_civoid hid_put_data_unsigned(uint8_t *buf, usb_size_t len,
258f9f848faSopenharmony_ci	    struct hid_location *loc, unsigned int value);
259f9f848faSopenharmony_ciint	hid_is_collection(const void *desc, usb_size_t size, int32_t usage);
260f9f848faSopenharmony_cistruct usb_hid_descriptor *hid_get_descriptor_from_usb(
261f9f848faSopenharmony_ci	    struct usb_config_descriptor *cd,
262f9f848faSopenharmony_ci	    struct usb_interface_descriptor *id);
263f9f848faSopenharmony_ciusb_error_t usbd_req_get_hid_desc(struct usb_device *udev, struct mtx *mtx,
264f9f848faSopenharmony_ci	    void **descp, uint16_t *sizep, struct malloc_type *mem,
265f9f848faSopenharmony_ci	    uint8_t iface_index);
266f9f848faSopenharmony_ciint	hid_is_mouse(const void *d_ptr, uint16_t d_len);
267f9f848faSopenharmony_ciint	hid_is_keyboard(const void *d_ptr, uint16_t d_len);
268f9f848faSopenharmony_ci#endif					/* _KERNEL */
269f9f848faSopenharmony_ci#endif					/* _USB_HID_H_ */
270