xref: /freebsd/lib/libusbhid/parse.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*	$NetBSD: parse.c,v 1.11 2000/09/24 02:19:54 augustss Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 1999, 2001 Lennart Augustsson <augustss@netbsd.org>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 #include <assert.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/time.h>
36 
37 #include <dev/usb/usb.h>
38 #include <dev/usb/usbhid.h>
39 
40 #include "usbhid.h"
41 #include "usbvar.h"
42 
43 #define	MAXUSAGE 100
44 #define	MAXPUSH 4
45 #define	MAXID 64
46 #define	ITEMTYPES 3
47 
48 struct hid_pos_data {
49 	int32_t rid;
50 	uint32_t pos[ITEMTYPES];
51 };
52 
53 struct hid_data {
54 	const uint8_t *start;
55 	const uint8_t *end;
56 	const uint8_t *p;
57 	struct hid_item cur[MAXPUSH];
58 	struct hid_pos_data last_pos[MAXID];
59 	uint32_t pos[ITEMTYPES];
60 	int32_t usages_min[MAXUSAGE];
61 	int32_t usages_max[MAXUSAGE];
62 	int32_t usage_last;	/* last seen usage */
63 	uint32_t loc_size;	/* last seen size */
64 	uint32_t loc_count;	/* last seen count */
65 	uint8_t	kindset;	/* we have 5 kinds so 8 bits are enough */
66 	uint8_t	pushlevel;	/* current pushlevel */
67 	uint8_t	ncount;		/* end usage item count */
68 	uint8_t icount;		/* current usage item count */
69 	uint8_t	nusage;		/* end "usages_min/max" index */
70 	uint8_t	iusage;		/* current "usages_min/max" index */
71 	uint8_t ousage;		/* current "usages_min/max" offset */
72 	uint8_t	susage;		/* usage set flags */
73 	int32_t	reportid;	/* requested report ID */
74 };
75 
76 /*------------------------------------------------------------------------*
77  *	hid_clear_local
78  *------------------------------------------------------------------------*/
79 static void
80 hid_clear_local(hid_item_t *c)
81 {
82 
83 	c->usage = 0;
84 	c->usage_minimum = 0;
85 	c->usage_maximum = 0;
86 	c->designator_index = 0;
87 	c->designator_minimum = 0;
88 	c->designator_maximum = 0;
89 	c->string_index = 0;
90 	c->string_minimum = 0;
91 	c->string_maximum = 0;
92 	c->set_delimiter = 0;
93 }
94 
95 static void
96 hid_switch_rid(struct hid_data *s, struct hid_item *c, int32_t next_rID)
97 {
98 	uint8_t i, j;
99 
100 	/* check for same report ID - optimise */
101 
102 	if (c->report_ID == next_rID)
103 		return;
104 
105 	/* save current position for current rID */
106 
107 	if (c->report_ID == 0) {
108 		i = 0;
109 	} else {
110 		for (i = 1; i != MAXID; i++) {
111 			if (s->last_pos[i].rid == c->report_ID)
112 				break;
113 			if (s->last_pos[i].rid == 0)
114 				break;
115 		}
116 	}
117 	if (i != MAXID) {
118 		s->last_pos[i].rid = c->report_ID;
119 		for (j = 0; j < ITEMTYPES; j++)
120 			s->last_pos[i].pos[j] = s->pos[j];
121 	}
122 
123 	/* store next report ID */
124 
125 	c->report_ID = next_rID;
126 
127 	/* lookup last position for next rID */
128 
129 	if (next_rID == 0) {
130 		i = 0;
131 	} else {
132 		for (i = 1; i != MAXID; i++) {
133 			if (s->last_pos[i].rid == next_rID)
134 				break;
135 			if (s->last_pos[i].rid == 0)
136 				break;
137 		}
138 	}
139 	if (i != MAXID) {
140 		s->last_pos[i].rid = next_rID;
141 		for (j = 0; j < ITEMTYPES; j++)
142 			s->pos[j] = s->last_pos[i].pos[j];
143 	} else {
144 		for (j = 0; j < ITEMTYPES; j++)
145 			s->pos[j] = 0;	/* Out of RID entries. */
146 	}
147 }
148 
149 /*------------------------------------------------------------------------*
150  *	hid_start_parse
151  *------------------------------------------------------------------------*/
152 hid_data_t
153 hid_start_parse(report_desc_t d, int kindset, int id)
154 {
155 	struct hid_data *s;
156 
157 	s = malloc(sizeof *s);
158 	memset(s, 0, sizeof *s);
159 	s->start = s->p = d->data;
160 	s->end = d->data + d->size;
161 	s->kindset = kindset;
162 	s->reportid = id;
163 	return (s);
164 }
165 
166 /*------------------------------------------------------------------------*
167  *	hid_end_parse
168  *------------------------------------------------------------------------*/
169 void
170 hid_end_parse(hid_data_t s)
171 {
172 
173 	if (s == NULL)
174 		return;
175 
176 	free(s);
177 }
178 
179 /*------------------------------------------------------------------------*
180  *	get byte from HID descriptor
181  *------------------------------------------------------------------------*/
182 static uint8_t
183 hid_get_byte(struct hid_data *s, const uint16_t wSize)
184 {
185 	const uint8_t *ptr;
186 	uint8_t retval;
187 
188 	ptr = s->p;
189 
190 	/* check if end is reached */
191 	if (ptr == s->end)
192 		return (0);
193 
194 	/* read out a byte */
195 	retval = *ptr;
196 
197 	/* check if data pointer can be advanced by "wSize" bytes */
198 	if ((s->end - ptr) < wSize)
199 		ptr = s->end;
200 	else
201 		ptr += wSize;
202 
203 	/* update pointer */
204 	s->p = ptr;
205 
206 	return (retval);
207 }
208 
209 /*------------------------------------------------------------------------*
210  *	hid_get_item
211  *------------------------------------------------------------------------*/
212 static int
213 hid_get_item_raw(hid_data_t s, hid_item_t *h)
214 {
215 	hid_item_t *c;
216 	unsigned int bTag, bType, bSize;
217 	int32_t mask;
218 	int32_t dval;
219 
220 	if (s == NULL)
221 		return (0);
222 
223 	c = &s->cur[s->pushlevel];
224 
225  top:
226 	/* check if there is an array of items */
227 	if (s->icount < s->ncount) {
228 		/* get current usage */
229 		if (s->iusage < s->nusage) {
230 			dval = s->usages_min[s->iusage] + s->ousage;
231 			c->usage = dval;
232 			s->usage_last = dval;
233 			if (dval == s->usages_max[s->iusage]) {
234 				s->iusage ++;
235 				s->ousage = 0;
236 			} else {
237 				s->ousage ++;
238 			}
239 		} else {
240 			/* Using last usage */
241 			dval = s->usage_last;
242 		}
243 		s->icount ++;
244 		/*
245 		 * Only copy HID item, increment position and return
246 		 * if correct kindset!
247 		 */
248 		if (s->kindset & (1 << c->kind)) {
249 			*h = *c;
250 			h->pos = s->pos[c->kind];
251 			s->pos[c->kind] += c->report_size * c->report_count;
252 			return (1);
253 		}
254 	}
255 
256 	/* reset state variables */
257 	s->icount = 0;
258 	s->ncount = 0;
259 	s->iusage = 0;
260 	s->nusage = 0;
261 	s->susage = 0;
262 	s->ousage = 0;
263 	hid_clear_local(c);
264 
265 	/* get next item */
266 	while (s->p != s->end) {
267 
268 		bSize = hid_get_byte(s, 1);
269 		if (bSize == 0xfe) {
270 			/* long item */
271 			bSize = hid_get_byte(s, 1);
272 			bSize |= hid_get_byte(s, 1) << 8;
273 			bTag = hid_get_byte(s, 1);
274 			bType = 0xff;	/* XXX what should it be */
275 		} else {
276 			/* short item */
277 			bTag = bSize >> 4;
278 			bType = (bSize >> 2) & 3;
279 			bSize &= 3;
280 			if (bSize == 3)
281 				bSize = 4;
282 		}
283 
284 		switch(bSize) {
285 		case 0:
286 			dval = 0;
287 			mask = 0;
288 			break;
289 		case 1:
290 			dval = (int8_t)hid_get_byte(s, 1);
291 			mask = 0xFF;
292 			break;
293 		case 2:
294 			dval = hid_get_byte(s, 1);
295 			dval |= hid_get_byte(s, 1) << 8;
296 			dval = (int16_t)dval;
297 			mask = 0xFFFF;
298 			break;
299 		case 4:
300 			dval = hid_get_byte(s, 1);
301 			dval |= hid_get_byte(s, 1) << 8;
302 			dval |= hid_get_byte(s, 1) << 16;
303 			dval |= hid_get_byte(s, 1) << 24;
304 			mask = 0xFFFFFFFF;
305 			break;
306 		default:
307 			dval = hid_get_byte(s, bSize);
308 			continue;
309 		}
310 
311 		switch (bType) {
312 		case 0:		/* Main */
313 			switch (bTag) {
314 			case 8:	/* Input */
315 				c->kind = hid_input;
316 				c->flags = dval;
317 		ret:
318 				c->report_count = s->loc_count;
319 				c->report_size = s->loc_size;
320 
321 				if (c->flags & HIO_VARIABLE) {
322 					/* range check usage count */
323 					if (c->report_count > 255) {
324 						s->ncount = 255;
325 					} else
326 						s->ncount = c->report_count;
327 
328 					/*
329 					 * The "top" loop will return
330 					 * one and one item:
331 					 */
332 					c->report_count = 1;
333 					c->usage_minimum = 0;
334 					c->usage_maximum = 0;
335 				} else {
336 					s->ncount = 1;
337 				}
338 				goto top;
339 
340 			case 9:	/* Output */
341 				c->kind = hid_output;
342 				c->flags = dval;
343 				goto ret;
344 			case 10:	/* Collection */
345 				c->kind = hid_collection;
346 				c->collection = dval;
347 				c->collevel++;
348 				c->usage = s->usage_last;
349 				*h = *c;
350 				return (1);
351 			case 11:	/* Feature */
352 				c->kind = hid_feature;
353 				c->flags = dval;
354 				goto ret;
355 			case 12:	/* End collection */
356 				c->kind = hid_endcollection;
357 				if (c->collevel == 0) {
358 					/* Invalid end collection. */
359 					return (0);
360 				}
361 				c->collevel--;
362 				*h = *c;
363 				return (1);
364 			default:
365 				break;
366 			}
367 			break;
368 
369 		case 1:		/* Global */
370 			switch (bTag) {
371 			case 0:
372 				c->_usage_page = dval << 16;
373 				break;
374 			case 1:
375 				c->logical_minimum = dval;
376 				break;
377 			case 2:
378 				c->logical_maximum = dval;
379 				break;
380 			case 3:
381 				c->physical_minimum = dval;
382 				break;
383 			case 4:
384 				c->physical_maximum = dval;
385 				break;
386 			case 5:
387 				c->unit_exponent = dval;
388 				break;
389 			case 6:
390 				c->unit = dval;
391 				break;
392 			case 7:
393 				/* mask because value is unsigned */
394 				s->loc_size = dval & mask;
395 				break;
396 			case 8:
397 				hid_switch_rid(s, c, dval & mask);
398 				break;
399 			case 9:
400 				/* mask because value is unsigned */
401 				s->loc_count = dval & mask;
402 				break;
403 			case 10:	/* Push */
404 				/* stop parsing, if invalid push level */
405 				if ((s->pushlevel + 1) >= MAXPUSH)
406 					return (0);
407 				s->pushlevel ++;
408 				s->cur[s->pushlevel] = *c;
409 				/* store size and count */
410 				c->report_size = s->loc_size;
411 				c->report_count = s->loc_count;
412 				/* update current item pointer */
413 				c = &s->cur[s->pushlevel];
414 				break;
415 			case 11:	/* Pop */
416 				/* stop parsing, if invalid push level */
417 				if (s->pushlevel == 0)
418 					return (0);
419 				s->pushlevel --;
420 				c = &s->cur[s->pushlevel];
421 				/* restore size and count */
422 				s->loc_size = c->report_size;
423 				s->loc_count = c->report_count;
424 				c->report_size = 0;
425 				c->report_count = 0;
426 				break;
427 			default:
428 				break;
429 			}
430 			break;
431 		case 2:		/* Local */
432 			switch (bTag) {
433 			case 0:
434 				if (bSize != 4)
435 					dval = (dval & mask) | c->_usage_page;
436 
437 				/* set last usage, in case of a collection */
438 				s->usage_last = dval;
439 
440 				if (s->nusage < MAXUSAGE) {
441 					s->usages_min[s->nusage] = dval;
442 					s->usages_max[s->nusage] = dval;
443 					s->nusage ++;
444 				}
445 				/* else XXX */
446 
447 				/* clear any pending usage sets */
448 				s->susage = 0;
449 				break;
450 			case 1:
451 				s->susage |= 1;
452 
453 				if (bSize != 4)
454 					dval = (dval & mask) | c->_usage_page;
455 				c->usage_minimum = dval;
456 
457 				goto check_set;
458 			case 2:
459 				s->susage |= 2;
460 
461 				if (bSize != 4)
462 					dval = (dval & mask) | c->_usage_page;
463 				c->usage_maximum = dval;
464 
465 			check_set:
466 				if (s->susage != 3)
467 					break;
468 
469 				/* sanity check */
470 				if ((s->nusage < MAXUSAGE) &&
471 				    (c->usage_minimum <= c->usage_maximum)) {
472 					/* add usage range */
473 					s->usages_min[s->nusage] =
474 					    c->usage_minimum;
475 					s->usages_max[s->nusage] =
476 					    c->usage_maximum;
477 					s->nusage ++;
478 				}
479 				/* else XXX */
480 
481 				s->susage = 0;
482 				break;
483 			case 3:
484 				c->designator_index = dval;
485 				break;
486 			case 4:
487 				c->designator_minimum = dval;
488 				break;
489 			case 5:
490 				c->designator_maximum = dval;
491 				break;
492 			case 7:
493 				c->string_index = dval;
494 				break;
495 			case 8:
496 				c->string_minimum = dval;
497 				break;
498 			case 9:
499 				c->string_maximum = dval;
500 				break;
501 			case 10:
502 				c->set_delimiter = dval;
503 				break;
504 			default:
505 				break;
506 			}
507 			break;
508 		default:
509 			break;
510 		}
511 	}
512 	return (0);
513 }
514 
515 int
516 hid_get_item(hid_data_t s, hid_item_t *h)
517 {
518 	int r;
519 
520 	for (;;) {
521 		r = hid_get_item_raw(s, h);
522 		if (r <= 0 || s->reportid == -1 || h->report_ID == s->reportid)
523 			break;
524 	}
525 	return (r);
526 }
527 
528 int
529 hid_report_size(report_desc_t r, enum hid_kind k, int id)
530 {
531 	struct hid_data *d;
532 	struct hid_item h;
533 	uint32_t temp;
534 	uint32_t hpos;
535 	uint32_t lpos;
536 	int report_id = 0;
537 
538 	hpos = 0;
539 	lpos = 0xFFFFFFFF;
540 
541 	memset(&h, 0, sizeof h);
542 	for (d = hid_start_parse(r, 1 << k, id); hid_get_item(d, &h); ) {
543 		if (h.kind == k) {
544 			/* compute minimum */
545 			if (lpos > h.pos)
546 				lpos = h.pos;
547 			/* compute end position */
548 			temp = h.pos + (h.report_size * h.report_count);
549 			/* compute maximum */
550 			if (hpos < temp)
551 				hpos = temp;
552 			if (h.report_ID != 0)
553 				report_id = 1;
554 		}
555 	}
556 	hid_end_parse(d);
557 
558 	/* safety check - can happen in case of currupt descriptors */
559 	if (lpos > hpos)
560 		temp = 0;
561 	else
562 		temp = hpos - lpos;
563 
564 	/* return length in bytes rounded up */
565 	return ((temp + 7) / 8 + report_id);
566 }
567 
568 int
569 hid_locate(report_desc_t desc, unsigned int u, enum hid_kind k,
570 	   hid_item_t *h, int id)
571 {
572 	struct hid_data *d;
573 
574 	for (d = hid_start_parse(desc, 1 << k, id); hid_get_item(d, h); ) {
575 		if (h->kind == k && !(h->flags & HIO_CONST) && h->usage == u) {
576 			hid_end_parse(d);
577 			return (1);
578 		}
579 	}
580 	hid_end_parse(d);
581 	h->report_size = 0;
582 	return (0);
583 }
584