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