2001-12-29 20:37:14 +00:00
|
|
|
/* $NetBSD: hid.c,v 1.17 2001/11/13 06:24:53 lukem Exp $ */
|
1999-10-07 19:26:38 +00:00
|
|
|
/* $FreeBSD$ */
|
1998-11-26 23:13:13 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1998 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
1999-01-07 23:07:57 +00:00
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
2000-05-14 16:43:10 +00:00
|
|
|
* by Lennart Augustsson (lennart@augustsson.net) at
|
1999-01-07 23:07:57 +00:00
|
|
|
* Carlstedt Research & Technology.
|
1998-11-26 23:13:13 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
1999-11-08 21:06:21 +00:00
|
|
|
#if defined(__NetBSD__)
|
1998-11-26 23:13:13 +00:00
|
|
|
#include <sys/kernel.h>
|
1999-01-07 23:07:57 +00:00
|
|
|
#endif
|
1999-11-08 21:06:21 +00:00
|
|
|
#include <sys/malloc.h>
|
2003-07-04 01:50:39 +00:00
|
|
|
|
1998-11-26 23:13:13 +00:00
|
|
|
#include <dev/usb/usb.h>
|
|
|
|
#include <dev/usb/usbhid.h>
|
|
|
|
|
|
|
|
#include <dev/usb/hid.h>
|
|
|
|
|
2002-07-31 14:34:36 +00:00
|
|
|
#ifdef USB_DEBUG
|
1999-04-11 20:50:33 +00:00
|
|
|
#define DPRINTF(x) if (usbdebug) logprintf x
|
|
|
|
#define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
|
1998-11-26 23:13:13 +00:00
|
|
|
extern int usbdebug;
|
|
|
|
#else
|
|
|
|
#define DPRINTF(x)
|
|
|
|
#define DPRINTFN(n,x)
|
|
|
|
#endif
|
|
|
|
|
2000-07-17 18:41:20 +00:00
|
|
|
Static void hid_clear_local(struct hid_item *);
|
1998-11-26 23:13:13 +00:00
|
|
|
|
|
|
|
#define MAXUSAGE 100
|
|
|
|
struct hid_data {
|
|
|
|
u_char *start;
|
|
|
|
u_char *end;
|
|
|
|
u_char *p;
|
|
|
|
struct hid_item cur;
|
|
|
|
int32_t usages[MAXUSAGE];
|
|
|
|
int nu;
|
|
|
|
int minset;
|
|
|
|
int multi;
|
|
|
|
int multimax;
|
|
|
|
int kindset;
|
|
|
|
};
|
|
|
|
|
2000-04-03 20:58:30 +00:00
|
|
|
Static void
|
2001-12-29 20:37:14 +00:00
|
|
|
hid_clear_local(struct hid_item *c)
|
1998-11-26 23:13:13 +00:00
|
|
|
{
|
2001-12-29 20:37:14 +00:00
|
|
|
|
1998-11-26 23:13:13 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct hid_data *
|
2001-12-29 20:37:14 +00:00
|
|
|
hid_start_parse(void *d, int len, int kindset)
|
1998-11-26 23:13:13 +00:00
|
|
|
{
|
|
|
|
struct hid_data *s;
|
|
|
|
|
2003-02-19 05:47:46 +00:00
|
|
|
s = malloc(sizeof *s, M_TEMP, M_WAITOK|M_ZERO);
|
1998-12-15 07:06:51 +00:00
|
|
|
s->start = s->p = d;
|
1999-01-10 18:42:54 +00:00
|
|
|
s->end = (char *)d + len;
|
1998-11-26 23:13:13 +00:00
|
|
|
s->kindset = kindset;
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-12-29 20:37:14 +00:00
|
|
|
hid_end_parse(struct hid_data *s)
|
1998-11-26 23:13:13 +00:00
|
|
|
{
|
2001-12-29 20:37:14 +00:00
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
while (s->cur.next != NULL) {
|
1998-11-26 23:13:13 +00:00
|
|
|
struct hid_item *hi = s->cur.next->next;
|
|
|
|
free(s->cur.next, M_TEMP);
|
|
|
|
s->cur.next = hi;
|
|
|
|
}
|
|
|
|
free(s, M_TEMP);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-12-29 20:37:14 +00:00
|
|
|
hid_get_item(struct hid_data *s, struct hid_item *h)
|
1998-11-26 23:13:13 +00:00
|
|
|
{
|
|
|
|
struct hid_item *c = &s->cur;
|
2000-04-02 23:15:08 +00:00
|
|
|
unsigned int bTag, bType, bSize;
|
2000-05-14 16:59:31 +00:00
|
|
|
u_int32_t oldpos;
|
1998-11-26 23:13:13 +00:00
|
|
|
u_char *data;
|
|
|
|
int32_t dval;
|
|
|
|
u_char *p;
|
|
|
|
struct hid_item *hi;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
top:
|
1999-11-17 22:33:51 +00:00
|
|
|
if (s->multimax != 0) {
|
1998-11-26 23:13:13 +00:00
|
|
|
if (s->multi < s->multimax) {
|
|
|
|
c->usage = s->usages[min(s->multi, s->nu-1)];
|
|
|
|
s->multi++;
|
|
|
|
*h = *c;
|
|
|
|
c->loc.pos += c->loc.size;
|
|
|
|
h->next = 0;
|
|
|
|
return (1);
|
|
|
|
} else {
|
|
|
|
c->loc.count = s->multimax;
|
|
|
|
s->multimax = 0;
|
|
|
|
s->nu = 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;
|
|
|
|
bType = 0xff; /* XXX what should it be */
|
|
|
|
} else {
|
|
|
|
/* short item */
|
|
|
|
bTag = bSize >> 4;
|
|
|
|
bType = (bSize >> 2) & 3;
|
|
|
|
bSize &= 3;
|
|
|
|
if (bSize == 3) bSize = 4;
|
|
|
|
data = p;
|
|
|
|
p += bSize;
|
|
|
|
}
|
|
|
|
s->p = p;
|
|
|
|
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:
|
|
|
|
printf("BAD LENGTH %d\n", bSize);
|
|
|
|
continue;
|
|
|
|
}
|
2003-07-04 01:50:39 +00:00
|
|
|
|
1998-11-26 23:13:13 +00:00
|
|
|
switch (bType) {
|
|
|
|
case 0: /* Main */
|
|
|
|
switch (bTag) {
|
|
|
|
case 8: /* Input */
|
|
|
|
if (!(s->kindset & (1 << hid_input)))
|
|
|
|
continue;
|
|
|
|
c->kind = hid_input;
|
|
|
|
c->flags = dval;
|
|
|
|
ret:
|
|
|
|
if (c->flags & HIO_VARIABLE) {
|
|
|
|
s->multimax = c->loc.count;
|
|
|
|
s->multi = 0;
|
|
|
|
c->loc.count = 1;
|
|
|
|
if (s->minset) {
|
2003-07-04 01:50:39 +00:00
|
|
|
for (i = c->usage_minimum;
|
|
|
|
i <= c->usage_maximum;
|
1998-11-26 23:13:13 +00:00
|
|
|
i++) {
|
|
|
|
s->usages[s->nu] = i;
|
|
|
|
if (s->nu < MAXUSAGE-1)
|
|
|
|
s->nu++;
|
|
|
|
}
|
|
|
|
s->minset = 0;
|
|
|
|
}
|
|
|
|
goto top;
|
|
|
|
} else {
|
|
|
|
*h = *c;
|
|
|
|
h->next = 0;
|
2003-07-04 01:50:39 +00:00
|
|
|
c->loc.pos +=
|
1998-11-26 23:13:13 +00:00
|
|
|
c->loc.size * c->loc.count;
|
|
|
|
hid_clear_local(c);
|
|
|
|
s->minset = 0;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
case 9: /* Output */
|
|
|
|
if (!(s->kindset & (1 << hid_output)))
|
|
|
|
continue;
|
|
|
|
c->kind = hid_output;
|
|
|
|
c->flags = dval;
|
|
|
|
goto ret;
|
|
|
|
case 10: /* Collection */
|
|
|
|
c->kind = hid_collection;
|
|
|
|
c->collection = dval;
|
|
|
|
c->collevel++;
|
|
|
|
*h = *c;
|
|
|
|
hid_clear_local(c);
|
|
|
|
s->nu = 0;
|
|
|
|
return (1);
|
|
|
|
case 11: /* Feature */
|
|
|
|
if (!(s->kindset & (1 << hid_feature)))
|
|
|
|
continue;
|
|
|
|
c->kind = hid_feature;
|
|
|
|
c->flags = dval;
|
|
|
|
goto ret;
|
|
|
|
case 12: /* End collection */
|
|
|
|
c->kind = hid_endcollection;
|
|
|
|
c->collevel--;
|
|
|
|
*h = *c;
|
|
|
|
hid_clear_local(c);
|
|
|
|
s->nu = 0;
|
|
|
|
return (1);
|
|
|
|
default:
|
|
|
|
printf("Main bTag=%d\n", bTag);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
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->loc.size = dval;
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
c->report_ID = dval;
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
c->loc.count = dval;
|
|
|
|
break;
|
|
|
|
case 10: /* Push */
|
2003-02-19 05:47:46 +00:00
|
|
|
hi = malloc(sizeof *hi, M_TEMP, M_WAITOK);
|
1998-11-26 23:13:13 +00:00
|
|
|
*hi = s->cur;
|
|
|
|
c->next = hi;
|
|
|
|
break;
|
|
|
|
case 11: /* Pop */
|
|
|
|
hi = c->next;
|
2000-05-14 16:59:31 +00:00
|
|
|
oldpos = c->loc.pos;
|
1998-11-26 23:13:13 +00:00
|
|
|
s->cur = *hi;
|
2000-05-14 16:59:31 +00:00
|
|
|
c->loc.pos = oldpos;
|
1998-11-26 23:13:13 +00:00
|
|
|
free(hi, M_TEMP);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("Global bTag=%d\n", bTag);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2: /* Local */
|
|
|
|
switch (bTag) {
|
|
|
|
case 0:
|
2003-07-04 01:50:39 +00:00
|
|
|
if (bSize == 1)
|
1998-11-26 23:13:13 +00:00
|
|
|
dval = c->_usage_page | (dval&0xff);
|
2003-07-04 01:50:39 +00:00
|
|
|
else if (bSize == 2)
|
1998-11-26 23:13:13 +00:00
|
|
|
dval = c->_usage_page | (dval&0xffff);
|
|
|
|
c->usage = dval;
|
|
|
|
if (s->nu < MAXUSAGE)
|
|
|
|
s->usages[s->nu++] = dval;
|
|
|
|
/* else XXX */
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
s->minset = 1;
|
2003-07-04 01:50:39 +00:00
|
|
|
if (bSize == 1)
|
1998-11-26 23:13:13 +00:00
|
|
|
dval = c->_usage_page | (dval&0xff);
|
2003-07-04 01:50:39 +00:00
|
|
|
else if (bSize == 2)
|
1998-11-26 23:13:13 +00:00
|
|
|
dval = c->_usage_page | (dval&0xffff);
|
|
|
|
c->usage_minimum = dval;
|
|
|
|
break;
|
|
|
|
case 2:
|
2003-07-04 01:50:39 +00:00
|
|
|
if (bSize == 1)
|
1998-11-26 23:13:13 +00:00
|
|
|
dval = c->_usage_page | (dval&0xff);
|
2003-07-04 01:50:39 +00:00
|
|
|
else if (bSize == 2)
|
1998-11-26 23:13:13 +00: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:
|
|
|
|
printf("Local bTag=%d\n", bTag);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("default bType=%d\n", bType);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-29 20:37:14 +00:00
|
|
|
int
|
|
|
|
hid_report_size(void *buf, int len, enum hid_kind k, u_int8_t *idp)
|
1998-11-26 23:13:13 +00:00
|
|
|
{
|
|
|
|
struct hid_data *d;
|
|
|
|
struct hid_item h;
|
|
|
|
int size, id;
|
|
|
|
|
|
|
|
id = 0;
|
|
|
|
for (d = hid_start_parse(buf, len, 1<<k); hid_get_item(d, &h); )
|
1999-11-17 22:33:51 +00:00
|
|
|
if (h.report_ID != 0)
|
1998-11-26 23:13:13 +00:00
|
|
|
id = h.report_ID;
|
|
|
|
hid_end_parse(d);
|
|
|
|
size = h.loc.pos;
|
1999-11-17 22:33:51 +00:00
|
|
|
if (id != 0) {
|
1998-11-26 23:13:13 +00:00
|
|
|
size += 8;
|
|
|
|
*idp = id; /* XXX wrong */
|
|
|
|
} else
|
|
|
|
*idp = 0;
|
|
|
|
return ((size + 7) / 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-12-29 20:37:14 +00:00
|
|
|
hid_locate(void *desc, int size, u_int32_t u, enum hid_kind k,
|
|
|
|
struct hid_location *loc, u_int32_t *flags)
|
1998-11-26 23:13:13 +00:00
|
|
|
{
|
|
|
|
struct hid_data *d;
|
|
|
|
struct hid_item h;
|
|
|
|
|
|
|
|
for (d = hid_start_parse(desc, size, 1<<k); hid_get_item(d, &h); ) {
|
|
|
|
if (h.kind == k && !(h.flags & HIO_CONST) && h.usage == u) {
|
1999-11-17 22:33:51 +00:00
|
|
|
if (loc != NULL)
|
1998-11-26 23:13:13 +00:00
|
|
|
*loc = h.loc;
|
1999-11-17 22:33:51 +00:00
|
|
|
if (flags != NULL)
|
1998-11-26 23:13:13 +00:00
|
|
|
*flags = h.flags;
|
|
|
|
hid_end_parse(d);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hid_end_parse(d);
|
|
|
|
loc->size = 0;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
u_long
|
2001-12-29 20:37:14 +00:00
|
|
|
hid_get_data(u_char *buf, struct hid_location *loc)
|
1998-11-26 23:13:13 +00:00
|
|
|
{
|
|
|
|
u_int hpos = loc->pos;
|
|
|
|
u_int hsize = loc->size;
|
|
|
|
u_int32_t data;
|
|
|
|
int i, s;
|
|
|
|
|
|
|
|
DPRINTFN(10, ("hid_get_data: loc %d/%d\n", hpos, hsize));
|
|
|
|
|
|
|
|
if (hsize == 0)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
data = 0;
|
2003-07-04 01:50:39 +00:00
|
|
|
s = hpos / 8;
|
1998-11-26 23:13:13 +00:00
|
|
|
for (i = hpos; i < hpos+hsize; i += 8)
|
|
|
|
data |= buf[i / 8] << ((i / 8 - s) * 8);
|
|
|
|
data >>= hpos % 8;
|
|
|
|
data &= (1 << hsize) - 1;
|
|
|
|
hsize = 32 - hsize;
|
|
|
|
/* Sign extend */
|
|
|
|
data = ((int32_t)data << hsize) >> hsize;
|
2003-07-04 01:50:39 +00:00
|
|
|
DPRINTFN(10,("hid_get_data: loc %d/%d = %lu\n",
|
1999-01-07 23:07:57 +00:00
|
|
|
loc->pos, loc->size, (long)data));
|
1998-11-26 23:13:13 +00:00
|
|
|
return (data);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-12-29 20:37:14 +00:00
|
|
|
hid_is_collection(void *desc, int size, u_int32_t usage)
|
1998-11-26 23:13:13 +00:00
|
|
|
{
|
|
|
|
struct hid_data *hd;
|
|
|
|
struct hid_item hi;
|
1999-11-17 22:33:51 +00:00
|
|
|
int err;
|
1998-11-26 23:13:13 +00:00
|
|
|
|
|
|
|
hd = hid_start_parse(desc, size, hid_input);
|
1999-11-17 22:33:51 +00:00
|
|
|
if (hd == NULL)
|
1998-11-26 23:13:13 +00:00
|
|
|
return (0);
|
|
|
|
|
1999-11-17 22:33:51 +00:00
|
|
|
err = hid_get_item(hd, &hi) &&
|
1998-11-26 23:13:13 +00:00
|
|
|
hi.kind == hid_collection &&
|
|
|
|
hi.usage == usage;
|
|
|
|
hid_end_parse(hd);
|
1999-11-17 22:33:51 +00:00
|
|
|
return (err);
|
1998-11-26 23:13:13 +00:00
|
|
|
}
|