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