xref: /freebsd/sys/dev/hid/hid.h (revision f5dc2263ab1be8a35a7e27e82103f9ccd41ae584)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
5  * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
6  * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef _HID_HID_H_
31 #define	_HID_HID_H_
32 
33 /* Usage pages */
34 #define	HUP_UNDEFINED		0x0000
35 #define	HUP_GENERIC_DESKTOP	0x0001
36 #define	HUP_SIMULATION		0x0002
37 #define	HUP_VR_CONTROLS		0x0003
38 #define	HUP_SPORTS_CONTROLS	0x0004
39 #define	HUP_GAMING_CONTROLS	0x0005
40 #define	HUP_KEYBOARD		0x0007
41 #define	HUP_LEDS		0x0008
42 #define	HUP_BUTTON		0x0009
43 #define	HUP_ORDINALS		0x000a
44 #define	HUP_TELEPHONY		0x000b
45 #define	HUP_CONSUMER		0x000c
46 #define	HUP_DIGITIZERS		0x000d
47 #define	HUP_PHYSICAL_IFACE	0x000e
48 #define	HUP_UNICODE		0x0010
49 #define	HUP_ALPHANUM_DISPLAY	0x0014
50 #define	HUP_MONITOR		0x0080
51 #define	HUP_MONITOR_ENUM_VAL	0x0081
52 #define	HUP_VESA_VC		0x0082
53 #define	HUP_VESA_CMD		0x0083
54 #define	HUP_POWER		0x0084
55 #define	HUP_BATTERY_SYSTEM	0x0085
56 #define	HUP_BARCODE_SCANNER	0x008b
57 #define	HUP_SCALE		0x008c
58 #define	HUP_CAMERA_CONTROL	0x0090
59 #define	HUP_ARCADE		0x0091
60 #define	HUP_RESERVED_FF		0x00ff
61 #define	HUP_FIDO		0xf1d0
62 #define	HUP_VENDOR_00		0xff00
63 #define	HUP_VENDOR_01		0xff01
64 /* XXX compat */
65 #define	HUP_APPLE		HUP_RESERVED_FF
66 #define	HUP_MICROSOFT		HUP_VENDOR_00
67 #define	HUP_HP			HUP_VENDOR_01
68 
69 /* Usages, generic desktop */
70 #define	HUG_POINTER		0x0001
71 #define	HUG_MOUSE		0x0002
72 #define	HUG_JOYSTICK		0x0004
73 #define	HUG_GAME_PAD		0x0005
74 #define	HUG_KEYBOARD		0x0006
75 #define	HUG_KEYPAD		0x0007
76 #define	HUG_MULTIAXIS_CNTROLLER	0x0008
77 #define	HUG_RADIO_CONTROL       0x000c
78 #define	HUG_X			0x0030
79 #define	HUG_Y			0x0031
80 #define	HUG_Z			0x0032
81 #define	HUG_RX			0x0033
82 #define	HUG_RY			0x0034
83 #define	HUG_RZ			0x0035
84 #define	HUG_SLIDER		0x0036
85 #define	HUG_DIAL		0x0037
86 #define	HUG_WHEEL		0x0038
87 #define	HUG_HAT_SWITCH		0x0039
88 #define	HUG_COUNTED_BUFFER	0x003a
89 #define	HUG_BYTE_COUNT		0x003b
90 #define	HUG_MOTION_WAKEUP	0x003c
91 #define	HUG_VX			0x0040
92 #define	HUG_VY			0x0041
93 #define	HUG_VZ			0x0042
94 #define	HUG_VBRX		0x0043
95 #define	HUG_VBRY		0x0044
96 #define	HUG_VBRZ		0x0045
97 #define	HUG_VNO			0x0046
98 #define	HUG_TWHEEL		0x0048	/* M$ Wireless Intellimouse Wheel */
99 #define	HUG_SYSTEM_CONTROL	0x0080
100 #define	HUG_SYSTEM_POWER_DOWN	0x0081
101 #define	HUG_SYSTEM_SLEEP	0x0082
102 #define	HUG_SYSTEM_WAKEUP	0x0083
103 #define	HUG_SYSTEM_CONTEXT_MENU	0x0084
104 #define	HUG_SYSTEM_MAIN_MENU	0x0085
105 #define	HUG_SYSTEM_APP_MENU	0x0086
106 #define	HUG_SYSTEM_MENU_HELP	0x0087
107 #define	HUG_SYSTEM_MENU_EXIT	0x0088
108 #define	HUG_SYSTEM_MENU_SELECT	0x0089
109 #define	HUG_SYSTEM_MENU_RIGHT	0x008a
110 #define	HUG_SYSTEM_MENU_LEFT	0x008b
111 #define	HUG_SYSTEM_MENU_UP	0x008c
112 #define	HUG_SYSTEM_MENU_DOWN	0x008d
113 #define	HUG_SYSTEM_POWER_UP	0x008e
114 #define	HUG_SYSTEM_RESTART	0x008f
115 #define	HUG_D_PAD_UP		0x0090
116 #define	HUG_D_PAD_DOWN		0x0091
117 #define	HUG_D_PAD_RIGHT		0x0092
118 #define	HUG_D_PAD_LEFT		0x0093
119 #define	HUG_APPLE_EJECT		0x00b8
120 #define	HUG_RADIO_BUTTON        0x00c6
121 
122 /* Usages Digitizers */
123 #define	HUD_UNDEFINED		0x0000
124 #define	HUD_DIGITIZER		0x0001
125 #define	HUD_PEN			0x0002
126 #define	HUD_TOUCHSCREEN		0x0004
127 #define	HUD_TOUCHPAD		0x0005
128 #define	HUD_CONFIG		0x000e
129 #define	HUD_FINGER		0x0022
130 #define	HUD_TIP_PRESSURE	0x0030
131 #define	HUD_BARREL_PRESSURE	0x0031
132 #define	HUD_IN_RANGE		0x0032
133 #define	HUD_TOUCH		0x0033
134 #define	HUD_UNTOUCH		0x0034
135 #define	HUD_TAP			0x0035
136 #define	HUD_QUALITY		0x0036
137 #define	HUD_DATA_VALID		0x0037
138 #define	HUD_TRANSDUCER_INDEX	0x0038
139 #define	HUD_TABLET_FKEYS	0x0039
140 #define	HUD_PROGRAM_CHANGE_KEYS	0x003a
141 #define	HUD_BATTERY_STRENGTH	0x003b
142 #define	HUD_INVERT		0x003c
143 #define	HUD_X_TILT		0x003d
144 #define	HUD_Y_TILT		0x003e
145 #define	HUD_AZIMUTH		0x003f
146 #define	HUD_ALTITUDE		0x0040
147 #define	HUD_TWIST		0x0041
148 #define	HUD_TIP_SWITCH		0x0042
149 #define	HUD_SEC_TIP_SWITCH	0x0043
150 #define	HUD_BARREL_SWITCH	0x0044
151 #define	HUD_ERASER		0x0045
152 #define	HUD_TABLET_PICK		0x0046
153 #define	HUD_CONFIDENCE		0x0047
154 #define	HUD_WIDTH		0x0048
155 #define	HUD_HEIGHT		0x0049
156 #define	HUD_CONTACTID		0x0051
157 #define	HUD_INPUT_MODE		0x0052
158 #define	HUD_DEVICE_INDEX	0x0053
159 #define	HUD_CONTACTCOUNT	0x0054
160 #define	HUD_CONTACT_MAX		0x0055
161 #define	HUD_SCAN_TIME		0x0056
162 #define	HUD_SURFACE_SWITCH	0x0057
163 #define	HUD_BUTTONS_SWITCH	0x0058
164 #define	HUD_BUTTON_TYPE		0x0059
165 #define	HUD_SEC_BARREL_SWITCH	0x005a
166 #define	HUD_LATENCY_MODE	0x0060
167 
168 /* Usages, Consumer */
169 #define	HUC_CONTROL		0x0001
170 #define	HUC_HEADPHONE		0x0005
171 #define	HUC_AC_PAN		0x0238
172 
173 /* Usages, FIDO */
174 #define	HUF_U2FHID		0x0001
175 
176 #define	HID_USAGE2(p,u)		(((p) << 16) | (u))
177 #define	HID_GET_USAGE(u)	((u) & 0xffff)
178 #define	HID_GET_USAGE_PAGE(u)	(((u) >> 16) & 0xffff)
179 
180 #define	HID_INPUT_REPORT	0x01
181 #define	HID_OUTPUT_REPORT	0x02
182 #define	HID_FEATURE_REPORT	0x03
183 
184 /* Bits in the input/output/feature items */
185 #define	HIO_CONST	0x001
186 #define	HIO_VARIABLE	0x002
187 #define	HIO_RELATIVE	0x004
188 #define	HIO_WRAP	0x008
189 #define	HIO_NONLINEAR	0x010
190 #define	HIO_NOPREF	0x020
191 #define	HIO_NULLSTATE	0x040
192 #define	HIO_VOLATILE	0x080
193 #define	HIO_BUFBYTES	0x100
194 
195 /* Units of Measure */
196 #define	HUM_CENTIMETER	0x11
197 #define	HUM_RADIAN	0x12
198 #define	HUM_INCH	0x13
199 #define	HUM_INCH_EGALAX	0x33
200 #define	HUM_DEGREE	0x14
201 
202 #if defined(_KERNEL) || defined(_STANDALONE)
203 
204 #define	HID_ITEM_MAXUSAGE	8
205 #define	HID_MAX_AUTO_QUIRK	8	/* maximum number of dynamic quirks */
206 #define	HID_PNP_ID_SIZE		20	/* includes null terminator */
207 
208 /* Declare global HID debug variable. */
209 extern int hid_debug;
210 
211 /* Check if HID debugging is enabled. */
212 #ifdef HID_DEBUG_VAR
213 #ifdef HID_DEBUG
214 #define DPRINTFN(n,fmt,...) do {			\
215 	if ((HID_DEBUG_VAR) >= (n)) {			\
216 		printf("%s: " fmt,			\
217 		    __FUNCTION__ ,##__VA_ARGS__);	\
218 	}						\
219 } while (0)
220 #define DPRINTF(...)	DPRINTFN(1, __VA_ARGS__)
221 #else
222 #define DPRINTF(...)	do { } while (0)
223 #define DPRINTFN(...)	do { } while (0)
224 #endif
225 #endif
226 
227 /* Declare parent SYSCTL HID node. */
228 #ifdef SYSCTL_DECL
229 SYSCTL_DECL(_hw_hid);
230 #endif
231 
232 typedef uint32_t hid_size_t;
233 
234 #define	HID_IN_POLLING_MODE()	(SCHEDULER_STOPPED() || kdb_active)
235 
236 enum hid_kind {
237 	hid_input, hid_output, hid_feature, hid_collection, hid_endcollection
238 };
239 
240 struct hid_location {
241 	uint32_t size;
242 	uint32_t count;
243 	uint32_t pos;
244 };
245 
246 struct hid_item {
247 	/* Global */
248 	uint32_t _usage_page;
249 	int32_t	logical_minimum;
250 	int32_t	logical_maximum;
251 	int32_t	physical_minimum;
252 	int32_t	physical_maximum;
253 	uint32_t unit_exponent;
254 	uint32_t unit;
255 	uint32_t report_ID;
256 	/* Local */
257 	int	nusages;
258 	union {
259 		uint32_t usage;
260 		uint32_t usages[HID_ITEM_MAXUSAGE];
261 	};
262 	uint32_t usage_minimum;
263 	uint32_t usage_maximum;
264 	uint32_t designator_index;
265 	uint32_t designator_minimum;
266 	uint32_t designator_maximum;
267 	uint32_t string_index;
268 	uint32_t string_minimum;
269 	uint32_t string_maximum;
270 	uint32_t set_delimiter;
271 	/* Misc */
272 	uint32_t collection;
273 	int	collevel;
274 	enum hid_kind kind;
275 	uint32_t flags;
276 	/* Location */
277 	struct hid_location loc;
278 };
279 
280 struct hid_absinfo {
281 	int32_t min;
282 	int32_t max;
283 	int32_t res;
284 };
285 
286 struct hid_device_info {
287 	char		name[80];
288 	char		serial[80];
289 	char		idPnP[HID_PNP_ID_SIZE];
290 	uint16_t	idBus;
291 	uint16_t	idVendor;
292 	uint16_t	idProduct;
293 	uint16_t	idVersion;
294 	hid_size_t	rdescsize;	/* Report descriptor size */
295 	uint8_t		autoQuirk[HID_MAX_AUTO_QUIRK];
296 };
297 
298 struct hid_rdesc_info {
299 	void		*data;
300 	hid_size_t	len;
301 	hid_size_t	isize;
302 	hid_size_t	osize;
303 	hid_size_t	fsize;
304 	uint8_t		iid;
305 	uint8_t		oid;
306 	uint8_t		fid;
307 	/* Max sizes for HID requests supported by transport backend */
308 	hid_size_t	rdsize;
309 	hid_size_t	wrsize;
310 	hid_size_t	grsize;
311 	hid_size_t	srsize;
312 };
313 
314 typedef void hid_intr_t(void *context, void *data, hid_size_t len);
315 typedef bool hid_test_quirk_t(const struct hid_device_info *dev_info,
316     uint16_t quirk);
317 
318 extern hid_test_quirk_t *hid_test_quirk_p;
319 
320 /* prototypes from "usb_hid.c" */
321 
322 struct hid_data *hid_start_parse(const void *d, hid_size_t len, int kindset);
323 void	hid_end_parse(struct hid_data *s);
324 int	hid_get_item(struct hid_data *s, struct hid_item *h);
325 int	hid_report_size(const void *buf, hid_size_t len, enum hid_kind k,
326 	    uint8_t id);
327 int	hid_report_size_max(const void *buf, hid_size_t len, enum hid_kind k,
328 	    uint8_t *id);
329 int	hid_locate(const void *desc, hid_size_t size, int32_t usage,
330 	    enum hid_kind kind, uint8_t index, struct hid_location *loc,
331 	    uint32_t *flags, uint8_t *id);
332 int32_t hid_get_data(const uint8_t *buf, hid_size_t len,
333 	    struct hid_location *loc);
334 uint32_t hid_get_udata(const uint8_t *buf, hid_size_t len,
335 	    struct hid_location *loc);
336 void	hid_put_udata(uint8_t *buf, hid_size_t len,
337 	    struct hid_location *loc, unsigned int value);
338 int	hid_is_collection(const void *desc, hid_size_t size, int32_t usage);
339 int32_t	hid_item_resolution(struct hid_item *hi);
340 int	hid_is_mouse(const void *d_ptr, uint16_t d_len);
341 int	hid_is_keyboard(const void *d_ptr, uint16_t d_len);
342 bool	hid_test_quirk(const struct hid_device_info *dev_info, uint16_t quirk);
343 int	hid_add_dynamic_quirk(struct hid_device_info *dev_info,
344 	    uint16_t quirk);
345 void	hid_quirk_unload(void *arg);
346 
347 int	hid_intr_start(device_t);
348 int	hid_intr_stop(device_t);
349 void	hid_intr_poll(device_t);
350 int	hid_get_rdesc(device_t, void *, hid_size_t);
351 int	hid_read(device_t, void *, hid_size_t, hid_size_t *);
352 int	hid_write(device_t, const void *, hid_size_t);
353 int	hid_get_report(device_t, void *, hid_size_t, hid_size_t *, uint8_t,
354 	    uint8_t);
355 int	hid_set_report(device_t, const void *, hid_size_t, uint8_t, uint8_t);
356 int	hid_set_idle(device_t, uint16_t, uint8_t);
357 int	hid_set_protocol(device_t, uint16_t);
358 int	hid_ioctl(device_t, unsigned long, uintptr_t);
359 #endif	/* _KERNEL || _STANDALONE */
360 #endif	/* _HID_HID_H_ */
361