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 } else { 326 s->ncount = 1; 327 } 328 goto top; 329 330 case 9: /* Output */ 331 c->kind = hid_output; 332 c->flags = dval; 333 goto ret; 334 case 10: /* Collection */ 335 c->kind = hid_collection; 336 c->collection = dval; 337 c->collevel++; 338 c->usage = s->usage_last; 339 *h = *c; 340 return (1); 341 case 11: /* Feature */ 342 c->kind = hid_feature; 343 c->flags = dval; 344 goto ret; 345 case 12: /* End collection */ 346 c->kind = hid_endcollection; 347 if (c->collevel == 0) { 348 /* Invalid end collection. */ 349 return (0); 350 } 351 c->collevel--; 352 *h = *c; 353 return (1); 354 default: 355 break; 356 } 357 break; 358 359 case 1: /* Global */ 360 switch (bTag) { 361 case 0: 362 c->_usage_page = dval << 16; 363 break; 364 case 1: 365 c->logical_minimum = dval; 366 break; 367 case 2: 368 c->logical_maximum = dval; 369 break; 370 case 3: 371 c->physical_minimum = dval; 372 break; 373 case 4: 374 c->physical_maximum = dval; 375 break; 376 case 5: 377 c->unit_exponent = dval; 378 break; 379 case 6: 380 c->unit = dval; 381 break; 382 case 7: 383 /* mask because value is unsigned */ 384 s->loc_size = dval & mask; 385 break; 386 case 8: 387 hid_switch_rid(s, c, dval); 388 break; 389 case 9: 390 /* mask because value is unsigned */ 391 s->loc_count = dval & mask; 392 break; 393 case 10: /* Push */ 394 s->pushlevel ++; 395 if (s->pushlevel < MAXPUSH) { 396 s->cur[s->pushlevel] = *c; 397 /* store size and count */ 398 c->report_size = s->loc_size; 399 c->report_count = s->loc_count; 400 /* update current item pointer */ 401 c = &s->cur[s->pushlevel]; 402 } 403 break; 404 case 11: /* Pop */ 405 s->pushlevel --; 406 if (s->pushlevel < MAXPUSH) { 407 /* preserve position */ 408 oldpos = c->pos; 409 c = &s->cur[s->pushlevel]; 410 /* restore size and count */ 411 s->loc_size = c->report_size; 412 s->loc_count = c->report_count; 413 /* set default item location */ 414 c->pos = oldpos; 415 c->report_size = 0; 416 c->report_count = 0; 417 } 418 break; 419 default: 420 break; 421 } 422 break; 423 case 2: /* Local */ 424 switch (bTag) { 425 case 0: 426 if (bSize != 4) 427 dval = (dval & mask) | c->_usage_page; 428 429 /* set last usage, in case of a collection */ 430 s->usage_last = dval; 431 432 if (s->nusage < MAXUSAGE) { 433 s->usages_min[s->nusage] = dval; 434 s->usages_max[s->nusage] = dval; 435 s->nusage ++; 436 } 437 /* else XXX */ 438 439 /* clear any pending usage sets */ 440 s->susage = 0; 441 break; 442 case 1: 443 s->susage |= 1; 444 445 if (bSize != 4) 446 dval = (dval & mask) | c->_usage_page; 447 c->usage_minimum = dval; 448 449 goto check_set; 450 case 2: 451 s->susage |= 2; 452 453 if (bSize != 4) 454 dval = (dval & mask) | c->_usage_page; 455 c->usage_maximum = dval; 456 457 check_set: 458 if (s->susage != 3) 459 break; 460 461 /* sanity check */ 462 if ((s->nusage < MAXUSAGE) && 463 (c->usage_minimum <= c->usage_maximum)) { 464 /* add usage range */ 465 s->usages_min[s->nusage] = 466 c->usage_minimum; 467 s->usages_max[s->nusage] = 468 c->usage_maximum; 469 s->nusage ++; 470 } 471 /* else XXX */ 472 473 s->susage = 0; 474 break; 475 case 3: 476 c->designator_index = dval; 477 break; 478 case 4: 479 c->designator_minimum = dval; 480 break; 481 case 5: 482 c->designator_maximum = dval; 483 break; 484 case 7: 485 c->string_index = dval; 486 break; 487 case 8: 488 c->string_minimum = dval; 489 break; 490 case 9: 491 c->string_maximum = dval; 492 break; 493 case 10: 494 c->set_delimiter = dval; 495 break; 496 default: 497 break; 498 } 499 break; 500 default: 501 break; 502 } 503 } 504 return (0); 505 } 506 507 int 508 hid_report_size(report_desc_t r, enum hid_kind k, int id) 509 { 510 struct hid_data *d; 511 struct hid_item h; 512 uint32_t temp; 513 uint32_t hpos; 514 uint32_t lpos; 515 516 hpos = 0; 517 lpos = 0xFFFFFFFF; 518 519 memset(&h, 0, sizeof h); 520 for (d = hid_start_parse(r, 1 << k, id); hid_get_item(d, &h); ) { 521 if (h.report_ID == id && h.kind == k) { 522 /* compute minimum */ 523 if (lpos > h.pos) 524 lpos = h.pos; 525 /* compute end position */ 526 temp = h.pos + (h.report_size * h.report_count); 527 /* compute maximum */ 528 if (hpos < temp) 529 hpos = temp; 530 } 531 } 532 hid_end_parse(d); 533 534 /* safety check - can happen in case of currupt descriptors */ 535 if (lpos > hpos) 536 temp = 0; 537 else 538 temp = hpos - lpos; 539 540 if (id) 541 temp += 8; 542 543 /* return length in bytes rounded up */ 544 return ((temp + 7) / 8); 545 } 546 547 int 548 hid_locate(report_desc_t desc, unsigned int u, enum hid_kind k, 549 hid_item_t *h, int id) 550 { 551 struct hid_data *d; 552 553 for (d = hid_start_parse(desc, 1 << k, id); hid_get_item(d, h); ) { 554 if (h->kind == k && !(h->flags & HIO_CONST) && h->usage == u) { 555 hid_end_parse(d); 556 return (1); 557 } 558 } 559 hid_end_parse(d); 560 h->report_size = 0; 561 return (0); 562 } 563