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