2000-09-24 06:19:54 +04:00
|
|
|
/* $NetBSD: parse.c,v 1.11 2000/09/24 02:19:54 augustss Exp $ */
|
1998-07-13 15:14:03 +04:00
|
|
|
|
|
|
|
/*
|
1999-05-12 01:15:46 +04:00
|
|
|
* Copyright (c) 1999 Lennart Augustsson <augustss@netbsd.org>
|
1998-07-13 15:14:03 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
1999-05-12 01:15:46 +04:00
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
1998-07-13 15:14:03 +04:00
|
|
|
*
|
1999-05-12 01:15:46 +04:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
1998-07-13 15:14:03 +04:00
|
|
|
*/
|
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
#include <assert.h>
|
1998-07-13 15:14:03 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1999-10-13 21:48:04 +04:00
|
|
|
#include <sys/time.h>
|
1999-05-12 01:02:24 +04:00
|
|
|
|
1998-07-13 15:14:03 +04:00
|
|
|
#include <dev/usb/usb.h>
|
|
|
|
#include <dev/usb/usbhid.h>
|
|
|
|
|
1999-05-12 01:02:24 +04:00
|
|
|
#include "usb.h"
|
|
|
|
#include "usbvar.h"
|
1998-07-13 15:14:03 +04:00
|
|
|
|
|
|
|
#define MAXUSAGE 100
|
|
|
|
struct hid_data {
|
|
|
|
u_char *start;
|
|
|
|
u_char *end;
|
|
|
|
u_char *p;
|
1999-05-12 01:02:24 +04:00
|
|
|
hid_item_t cur;
|
|
|
|
unsigned int usages[MAXUSAGE];
|
1998-07-13 15:14:03 +04:00
|
|
|
int nusage;
|
|
|
|
int minset;
|
|
|
|
int multi;
|
|
|
|
int multimax;
|
|
|
|
int kindset;
|
2000-09-24 06:19:54 +04:00
|
|
|
|
|
|
|
/* Absolute data position (bits) for input/output/feature.
|
|
|
|
Assumes that hid_input, hid_output and hid_feature have
|
|
|
|
values 0, 1 and 2. */
|
|
|
|
unsigned int kindpos[3];
|
1998-07-13 15:14:03 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static int min(int x, int y) { return x < y ? x : y; }
|
|
|
|
|
|
|
|
static void
|
1999-05-12 01:02:24 +04:00
|
|
|
hid_clear_local(hid_item_t *c)
|
1998-07-13 15:14:03 +04:00
|
|
|
{
|
1999-09-16 15:44:54 +04:00
|
|
|
|
|
|
|
_DIAGASSERT(c != NULL);
|
|
|
|
|
1998-07-13 15:14:03 +04:00
|
|
|
c->usage = 0;
|
|
|
|
c->usage_minimum = 0;
|
|
|
|
c->usage_maximum = 0;
|
|
|
|
c->designator_index = 0;
|
|
|
|
c->designator_minimum = 0;
|
|
|
|
c->designator_maximum = 0;
|
|
|
|
c->string_index = 0;
|
|
|
|
c->string_minimum = 0;
|
|
|
|
c->string_maximum = 0;
|
|
|
|
c->set_delimiter = 0;
|
|
|
|
}
|
|
|
|
|
1999-05-12 01:02:24 +04:00
|
|
|
hid_data_t
|
|
|
|
hid_start_parse(report_desc_t d, int kindset)
|
1998-07-13 15:14:03 +04:00
|
|
|
{
|
|
|
|
struct hid_data *s;
|
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(d != NULL);
|
|
|
|
|
1998-07-13 15:14:03 +04:00
|
|
|
s = malloc(sizeof *s);
|
|
|
|
memset(s, 0, sizeof *s);
|
1999-05-12 01:02:24 +04:00
|
|
|
s->start = s->p = d->data;
|
|
|
|
s->end = d->data + d->size;
|
1998-07-13 15:14:03 +04:00
|
|
|
s->kindset = kindset;
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-05-12 01:02:24 +04:00
|
|
|
hid_end_parse(hid_data_t s)
|
1998-07-13 15:14:03 +04:00
|
|
|
{
|
1999-09-16 15:44:54 +04:00
|
|
|
|
|
|
|
_DIAGASSERT(s != NULL);
|
|
|
|
|
1998-07-13 15:14:03 +04:00
|
|
|
while (s->cur.next) {
|
1999-05-12 01:02:24 +04:00
|
|
|
hid_item_t *hi = s->cur.next->next;
|
1998-07-13 15:14:03 +04:00
|
|
|
free(s->cur.next);
|
|
|
|
s->cur.next = hi;
|
|
|
|
}
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1999-05-12 01:02:24 +04:00
|
|
|
hid_get_item(hid_data_t s, hid_item_t *h)
|
1998-07-13 15:14:03 +04:00
|
|
|
{
|
1999-09-16 15:44:54 +04:00
|
|
|
hid_item_t *c;
|
2000-09-24 06:19:54 +04:00
|
|
|
unsigned int bTag = 0, bType = 0, bSize;
|
1999-05-12 01:02:24 +04:00
|
|
|
unsigned char *data;
|
|
|
|
int dval;
|
|
|
|
unsigned char *p;
|
|
|
|
hid_item_t *hi;
|
1998-07-13 15:14:03 +04:00
|
|
|
int i;
|
2000-09-24 06:19:54 +04:00
|
|
|
hid_kind_t retkind;
|
1998-07-13 15:14:03 +04:00
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(s != NULL);
|
|
|
|
_DIAGASSERT(h != NULL);
|
|
|
|
|
|
|
|
c = &s->cur;
|
|
|
|
|
1998-07-13 15:14:03 +04:00
|
|
|
top:
|
|
|
|
if (s->multimax) {
|
|
|
|
if (s->multi < s->multimax) {
|
|
|
|
c->usage = s->usages[min(s->multi, s->nusage-1)];
|
|
|
|
s->multi++;
|
|
|
|
*h = *c;
|
2000-09-24 06:19:54 +04:00
|
|
|
|
|
|
|
/* 'multimax' is only non-zero if the current
|
|
|
|
item kind is input/output/feature */
|
|
|
|
h->pos = s->kindpos[c->kind];
|
|
|
|
s->kindpos[c->kind] += c->report_size;
|
1998-07-13 15:14:03 +04:00
|
|
|
h->next = 0;
|
|
|
|
return (1);
|
|
|
|
} else {
|
|
|
|
c->report_count = s->multimax;
|
|
|
|
s->multimax = 0;
|
|
|
|
s->nusage = 0;
|
|
|
|
hid_clear_local(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
p = s->p;
|
|
|
|
if (p >= s->end)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
bSize = *p++;
|
|
|
|
if (bSize == 0xfe) {
|
|
|
|
/* long item */
|
|
|
|
bSize = *p++;
|
|
|
|
bSize |= *p++ << 8;
|
|
|
|
bTag = *p++;
|
|
|
|
data = p;
|
|
|
|
p += bSize;
|
|
|
|
} else {
|
|
|
|
/* short item */
|
|
|
|
bTag = bSize >> 4;
|
|
|
|
bType = (bSize >> 2) & 3;
|
|
|
|
bSize &= 3;
|
|
|
|
if (bSize == 3) bSize = 4;
|
|
|
|
data = p;
|
|
|
|
p += bSize;
|
|
|
|
}
|
|
|
|
s->p = p;
|
1999-05-12 01:02:24 +04:00
|
|
|
/*
|
|
|
|
* The spec is unclear if the data is signed or unsigned.
|
|
|
|
*/
|
1998-07-13 15:14:03 +04:00
|
|
|
switch(bSize) {
|
|
|
|
case 0:
|
|
|
|
dval = 0;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
dval = (int8_t)*data++;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
dval = *data++;
|
|
|
|
dval |= *data++ << 8;
|
|
|
|
dval = (int16_t)dval;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
dval = *data++;
|
|
|
|
dval |= *data++ << 8;
|
|
|
|
dval |= *data++ << 16;
|
|
|
|
dval |= *data++ << 24;
|
|
|
|
break;
|
|
|
|
default:
|
1999-05-12 01:02:24 +04:00
|
|
|
return (-1);
|
1998-07-13 15:14:03 +04:00
|
|
|
}
|
1999-07-02 19:46:05 +04:00
|
|
|
|
1998-07-13 15:14:03 +04:00
|
|
|
switch (bType) {
|
|
|
|
case 0: /* Main */
|
|
|
|
switch (bTag) {
|
|
|
|
case 8: /* Input */
|
2000-09-24 06:19:54 +04:00
|
|
|
retkind = hid_input;
|
|
|
|
ret:
|
|
|
|
if (!(s->kindset & (1 << retkind))) {
|
|
|
|
/* Drop the items of this kind */
|
|
|
|
s->nusage = 0;
|
1998-07-13 15:14:03 +04:00
|
|
|
continue;
|
2000-09-24 06:19:54 +04:00
|
|
|
}
|
|
|
|
c->kind = retkind;
|
1998-07-13 15:14:03 +04:00
|
|
|
c->flags = dval;
|
|
|
|
if (c->flags & HIO_VARIABLE) {
|
|
|
|
s->multimax = c->report_count;
|
|
|
|
s->multi = 0;
|
|
|
|
c->report_count = 1;
|
|
|
|
if (s->minset) {
|
1999-07-02 19:46:05 +04:00
|
|
|
for (i = c->usage_minimum;
|
|
|
|
i <= c->usage_maximum;
|
1998-07-13 15:14:03 +04:00
|
|
|
i++) {
|
|
|
|
s->usages[s->nusage] = i;
|
|
|
|
if (s->nusage < MAXUSAGE-1)
|
|
|
|
s->nusage++;
|
|
|
|
}
|
|
|
|
s->minset = 0;
|
|
|
|
}
|
|
|
|
goto top;
|
|
|
|
} else {
|
1999-07-02 19:46:05 +04:00
|
|
|
if (s->minset)
|
1998-07-13 15:14:03 +04:00
|
|
|
c->usage = c->usage_minimum;
|
|
|
|
*h = *c;
|
|
|
|
h->next = 0;
|
2000-09-24 06:19:54 +04:00
|
|
|
h->pos = s->kindpos[c->kind];
|
|
|
|
s->kindpos[c->kind] += c->report_size * c->report_count;
|
1998-07-13 15:14:03 +04:00
|
|
|
hid_clear_local(c);
|
|
|
|
s->minset = 0;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
case 9: /* Output */
|
2000-09-24 06:19:54 +04:00
|
|
|
retkind = hid_output;
|
1998-07-13 15:14:03 +04:00
|
|
|
goto ret;
|
|
|
|
case 10: /* Collection */
|
|
|
|
c->kind = hid_collection;
|
|
|
|
c->collection = dval;
|
|
|
|
c->collevel++;
|
|
|
|
*h = *c;
|
|
|
|
hid_clear_local(c);
|
2000-02-22 15:39:22 +03:00
|
|
|
c->report_ID = NO_REPORT_ID;
|
1998-07-13 15:14:03 +04:00
|
|
|
s->nusage = 0;
|
|
|
|
return (1);
|
|
|
|
case 11: /* Feature */
|
2000-09-24 06:19:54 +04:00
|
|
|
retkind = hid_feature;
|
1998-07-13 15:14:03 +04:00
|
|
|
goto ret;
|
|
|
|
case 12: /* End collection */
|
|
|
|
c->kind = hid_endcollection;
|
|
|
|
c->collevel--;
|
|
|
|
*h = *c;
|
2000-02-22 15:39:22 +03:00
|
|
|
/*hid_clear_local(c);*/
|
1998-07-13 15:14:03 +04:00
|
|
|
s->nusage = 0;
|
|
|
|
return (1);
|
|
|
|
default:
|
1999-05-12 01:02:24 +04:00
|
|
|
return (-2);
|
1998-07-13 15:14:03 +04:00
|
|
|
}
|
1999-05-12 01:02:24 +04:00
|
|
|
|
1998-07-13 15:14:03 +04:00
|
|
|
case 1: /* Global */
|
|
|
|
switch (bTag) {
|
|
|
|
case 0:
|
|
|
|
c->_usage_page = dval << 16;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
c->logical_minimum = dval;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
c->logical_maximum = dval;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
c->physical_maximum = dval;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
c->physical_maximum = dval;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
c->unit_exponent = dval;
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
c->unit = dval;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
c->report_size = dval;
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
c->report_ID = dval;
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
c->report_count = dval;
|
|
|
|
break;
|
|
|
|
case 10: /* Push */
|
|
|
|
hi = malloc(sizeof *hi);
|
|
|
|
*hi = s->cur;
|
|
|
|
c->next = hi;
|
|
|
|
break;
|
|
|
|
case 11: /* Pop */
|
|
|
|
hi = c->next;
|
|
|
|
s->cur = *hi;
|
|
|
|
free(hi);
|
|
|
|
break;
|
|
|
|
default:
|
1999-05-12 01:02:24 +04:00
|
|
|
return (-3);
|
1998-07-13 15:14:03 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2: /* Local */
|
|
|
|
switch (bTag) {
|
|
|
|
case 0:
|
1999-07-02 19:46:05 +04:00
|
|
|
if (bSize == 1)
|
1998-07-13 15:14:03 +04:00
|
|
|
dval = c->_usage_page | (dval&0xff);
|
1999-07-02 19:46:05 +04:00
|
|
|
else if (bSize == 2)
|
1998-07-13 15:14:03 +04:00
|
|
|
dval = c->_usage_page | (dval&0xffff);
|
|
|
|
c->usage = dval;
|
|
|
|
if (s->nusage < MAXUSAGE)
|
|
|
|
s->usages[s->nusage++] = dval;
|
|
|
|
/* else XXX */
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
s->minset = 1;
|
1999-07-02 19:46:05 +04:00
|
|
|
if (bSize == 1)
|
1998-07-13 15:14:03 +04:00
|
|
|
dval = c->_usage_page | (dval&0xff);
|
1999-07-02 19:46:05 +04:00
|
|
|
else if (bSize == 2)
|
1998-07-13 15:14:03 +04:00
|
|
|
dval = c->_usage_page | (dval&0xffff);
|
|
|
|
c->usage_minimum = dval;
|
|
|
|
break;
|
|
|
|
case 2:
|
1999-07-02 19:46:05 +04:00
|
|
|
if (bSize == 1)
|
1998-07-13 15:14:03 +04:00
|
|
|
dval = c->_usage_page | (dval&0xff);
|
1999-07-02 19:46:05 +04:00
|
|
|
else if (bSize == 2)
|
1998-07-13 15:14:03 +04:00
|
|
|
dval = c->_usage_page | (dval&0xffff);
|
|
|
|
c->usage_maximum = dval;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
c->designator_index = dval;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
c->designator_minimum = dval;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
c->designator_maximum = dval;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
c->string_index = dval;
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
c->string_minimum = dval;
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
c->string_maximum = dval;
|
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
c->set_delimiter = dval;
|
|
|
|
break;
|
|
|
|
default:
|
1999-05-12 01:02:24 +04:00
|
|
|
return (-4);
|
1998-07-13 15:14:03 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
1999-05-12 01:02:24 +04:00
|
|
|
return (-5);
|
1998-07-13 15:14:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-07-02 19:46:05 +04:00
|
|
|
int
|
1999-05-12 01:02:24 +04:00
|
|
|
hid_report_size(report_desc_t r, enum hid_kind k, int *idp)
|
1998-07-13 15:14:03 +04:00
|
|
|
{
|
|
|
|
struct hid_data *d;
|
1999-05-12 01:02:24 +04:00
|
|
|
hid_item_t h;
|
1998-07-13 15:14:03 +04:00
|
|
|
int size, id;
|
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(r != NULL);
|
|
|
|
/* idp may be NULL */
|
|
|
|
|
1998-07-13 15:14:03 +04:00
|
|
|
id = 0;
|
1999-04-21 20:23:14 +04:00
|
|
|
if (idp)
|
|
|
|
*idp = 0;
|
1998-07-13 15:14:03 +04:00
|
|
|
memset(&h, 0, sizeof h);
|
1999-05-12 01:02:24 +04:00
|
|
|
for (d = hid_start_parse(r, 1<<k); hid_get_item(d, &h); ) {
|
2000-02-22 15:39:22 +03:00
|
|
|
if (h.report_ID != NO_REPORT_ID) {
|
1999-04-21 20:23:14 +04:00
|
|
|
if (idp)
|
|
|
|
*idp = h.report_ID;
|
1998-07-13 15:14:03 +04:00
|
|
|
id = 8;
|
1999-04-21 20:23:14 +04:00
|
|
|
}
|
|
|
|
}
|
2000-09-24 06:19:54 +04:00
|
|
|
|
|
|
|
size = d->kindpos[k] + id;
|
1998-07-13 15:14:03 +04:00
|
|
|
hid_end_parse(d);
|
|
|
|
return ((size + 7) / 8);
|
|
|
|
}
|
1999-05-12 04:04:49 +04:00
|
|
|
|
|
|
|
int
|
2000-08-20 19:57:02 +04:00
|
|
|
hid_locate(report_desc_t desc, unsigned int u, enum hid_kind k, hid_item_t *h)
|
1999-05-12 04:04:49 +04:00
|
|
|
{
|
|
|
|
hid_data_t d;
|
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(desc != NULL);
|
|
|
|
_DIAGASSERT(h != NULL);
|
|
|
|
|
1999-05-12 04:04:49 +04:00
|
|
|
for (d = hid_start_parse(desc, 1<<k); hid_get_item(d, h); ) {
|
|
|
|
if (h->kind == k && !(h->flags & HIO_CONST) && h->usage == u) {
|
|
|
|
hid_end_parse(d);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hid_end_parse(d);
|
|
|
|
h->report_size = 0;
|
|
|
|
return (0);
|
|
|
|
}
|