Follow NetBSD and rename libusb to libusbhid.

MFC after:	7 days
This commit is contained in:
Josef Karthauser 2002-03-27 16:07:20 +00:00
parent 1876df83f4
commit a6402160b1
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=93282
9 changed files with 1 additions and 1176 deletions

View File

@ -28,7 +28,7 @@ SUBDIR= ${_csu} libcom_err libcrypt libkvm msun libmd \
libipx libisc libmenu ${_libmilter} ${_libmp} ${_libncp} \
libnetgraph libopie libpam libpanel libpcap \
${_libresolv} ${_libsm} ${_libsmb} ${_libsmdb} ${_libsmutil} \
libstand ${_libtelnet} libusb ${_libvgl} libwrap libxpg4 liby libz
libstand ${_libtelnet} libusbhid ${_libvgl} libwrap libxpg4 liby libz
.if exists(${.CURDIR}/csu/${MACHINE_ARCH}-${OBJFORMAT})
_csu=csu/${MACHINE_ARCH}-${OBJFORMAT}

View File

@ -1,24 +0,0 @@
# $NetBSD: Makefile,v 1.5 1999/07/23 09:44:38 mrg Exp $
# $FreeBSD$
MAINTAINER= n_hibma@FreeBSD.ORG
LIB= usb
MAN= usb.3
SHLIB_MAJOR= 0
SHLIB_MINOR= 0
MLINKS= usb.3 libusb.3 usb.3 hid_get_report_desc.3 \
usb.3 hid_dispose_report_desc.3 \
usb.3 hid_start_parse.3 usb.3 hid_end_parse.3 \
usb.3 hid_get_item.3 usb.3 hid_report_size.3 usb.3 hid_locate.3 \
usb.3 hid_usage_page.3 usb.3 hid_usage_in_page.3 usb.3 hid_init.3 \
usb.3 hid_get_data.3 usb.3 hid_set_data.3
SRCS= descr.c parse.c usage.c data.c
INCS= libusb.h
.include <bsd.lib.mk>

View File

@ -1,94 +0,0 @@
/* $NetBSD: data.c,v 1.8 2000/04/02 11:10:53 augustss Exp $ */
/*
* Copyright (c) 1999 Lennart Augustsson <augustss@netbsd.org>
* 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.
* 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.
*
* 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.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <assert.h>
#include <stdlib.h>
#include "libusb.h"
int
hid_get_data(const void *p, const hid_item_t *h)
{
const unsigned char *buf;
unsigned int hpos;
unsigned int hsize;
int data;
int i, end, offs;
buf = p;
hpos = h->pos; /* bit position of data */
hsize = h->report_size; /* bit length of data */
if (hsize == 0)
return (0);
offs = hpos / 8;
end = (hpos + hsize) / 8 - offs;
data = 0;
for (i = 0; i <= end; i++)
data |= buf[offs + i] << (i*8);
data >>= hpos % 8;
data &= (1 << hsize) - 1;
if (h->logical_minimum < 0) {
/* Need to sign extend */
hsize = sizeof data * 8 - hsize;
data = (data << hsize) >> hsize;
}
return (data);
}
void
hid_set_data(void *p, const hid_item_t *h, int data)
{
unsigned char *buf;
unsigned int hpos;
unsigned int hsize;
int i, end, offs, mask;
buf = p;
hpos = h->pos; /* bit position of data */
hsize = h->report_size; /* bit length of data */
if (hsize != 32) {
mask = (1 << hsize) - 1;
data &= mask;
} else
mask = ~0;
data <<= (hpos % 8);
mask <<= (hpos % 8);
mask = ~mask;
offs = hpos / 8;
end = (hpos + hsize) / 8 - offs;
for (i = 0; i <= end; i++)
buf[offs + i] = (buf[offs + i] & (mask >> (i*8))) |
((data >> (i*8)) & 0xff);
}

View File

@ -1,81 +0,0 @@
/* $NetBSD: descr.c,v 1.9 2000/09/24 02:13:24 augustss Exp $ */
/*
* Copyright (c) 1999 Lennart Augustsson <augustss@netbsd.org>
* 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.
* 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.
*
* 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.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/types.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#if defined(__FreeBSD__)
#include <sys/ioctl.h>
#endif
#include <dev/usb/usb.h>
#include "libusb.h"
#include "usbvar.h"
report_desc_t
hid_get_report_desc(int fd)
{
struct usb_ctl_report_desc rep;
rep.ucrd_size = 0;
if (ioctl(fd, USB_GET_REPORT_DESC, &rep) < 0)
return (NULL);
return hid_use_report_desc(rep.ucrd_data, (unsigned int)rep.ucrd_size);
}
report_desc_t
hid_use_report_desc(unsigned char *data, unsigned int size)
{
report_desc_t r;
r = malloc(sizeof(*r) + size);
if (r == 0) {
errno = ENOMEM;
return (NULL);
}
r->size = size;
memcpy(r->data, data, size);
return (r);
}
void
hid_dispose_report_desc(report_desc_t r)
{
free(r);
}

View File

@ -1,101 +0,0 @@
/* $NetBSD: usb.h,v 1.8 2000/08/13 22:22:02 augustss Exp $ */
/*
* Copyright (c) 1999 Lennart Augustsson <augustss@netbsd.org>
* 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.
* 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.
*
* 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.
*
* $FreeBSD$
*
*/
#include <sys/cdefs.h>
typedef struct report_desc *report_desc_t;
typedef struct hid_data *hid_data_t;
typedef enum hid_kind {
hid_input, hid_output, hid_feature, hid_collection, hid_endcollection
} hid_kind_t;
typedef struct hid_item {
/* Global */
int _usage_page;
int logical_minimum;
int logical_maximum;
int physical_minimum;
int physical_maximum;
int unit_exponent;
int unit;
int report_size;
int report_ID;
#define NO_REPORT_ID 0
int report_count;
/* Local */
unsigned int usage;
int usage_minimum;
int usage_maximum;
int designator_index;
int designator_minimum;
int designator_maximum;
int string_index;
int string_minimum;
int string_maximum;
int set_delimiter;
/* Misc */
int collection;
int collevel;
enum hid_kind kind;
unsigned int flags;
/* Absolute data position (bits) */
unsigned int pos;
/* */
struct hid_item *next;
} hid_item_t;
#define HID_PAGE(u) (((u) >> 16) & 0xffff)
#define HID_USAGE(u) ((u) & 0xffff)
/* Obtaining a report descriptor, descr.c: */
report_desc_t hid_get_report_desc(int file);
report_desc_t hid_use_report_desc(unsigned char *data, unsigned int size);
void hid_dispose_report_desc(report_desc_t);
/* Parsing of a HID report descriptor, parse.c: */
hid_data_t hid_start_parse(report_desc_t d, int kindset);
void hid_end_parse(hid_data_t s);
int hid_get_item(hid_data_t s, hid_item_t *h);
int hid_report_size(report_desc_t d, unsigned int id, enum hid_kind k);
int hid_locate(report_desc_t d, unsigned int usage, enum hid_kind k, hid_item_t *h);
/* Conversion to/from usage names, usage.c: */
int hid_parse_usage_page(const char *name);
int hid_parse_usage_in_page(const char *name);
const char *hid_usage_page(int i);
const char *hid_usage_in_page(unsigned int u);
void hid_init(const char *file);
/* Extracting/insertion of data, data.c: */
int hid_get_data(const void *p, const hid_item_t *h);
void hid_set_data(void *p, const hid_item_t *h, int data);

View File

@ -1,392 +0,0 @@
/* $NetBSD: parse.c,v 1.11 2000/09/24 02:19:54 augustss Exp $ */
/*
* Copyright (c) 1999 Lennart Augustsson <augustss@netbsd.org>
* 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.
* 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.
*
* 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.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbhid.h>
#include "libusb.h"
#include "usbvar.h"
#define MAXUSAGE 100
struct hid_data {
u_char *start;
u_char *end;
u_char *p;
hid_item_t cur;
unsigned int usages[MAXUSAGE];
int nusage;
int minset;
int multi;
int multimax;
int kindset;
/* 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];
};
static int min(int x, int y) { return x < y ? x : y; }
static void
hid_clear_local(hid_item_t *c)
{
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;
}
hid_data_t
hid_start_parse(report_desc_t d, int kindset)
{
struct hid_data *s;
s = malloc(sizeof *s);
memset(s, 0, sizeof *s);
s->start = s->p = d->data;
s->end = d->data + d->size;
s->kindset = kindset;
return (s);
}
void
hid_end_parse(hid_data_t s)
{
while (s->cur.next) {
hid_item_t *hi = s->cur.next->next;
free(s->cur.next);
s->cur.next = hi;
}
free(s);
}
int
hid_get_item(hid_data_t s, hid_item_t *h)
{
hid_item_t *c;
unsigned int bTag = 0, bType = 0, bSize;
unsigned char *data;
int dval;
unsigned char *p;
hid_item_t *hi;
int i;
hid_kind_t retkind;
c = &s->cur;
top:
if (s->multimax) {
if (s->multi < s->multimax) {
c->usage = s->usages[min(s->multi, s->nusage-1)];
s->multi++;
*h = *c;
/* '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;
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;
/*
* The spec is unclear if the data is signed or unsigned.
*/
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:
return (-1);
}
switch (bType) {
case 0: /* Main */
switch (bTag) {
case 8: /* Input */
retkind = hid_input;
ret:
if (!(s->kindset & (1 << retkind))) {
/* Drop the items of this kind */
s->nusage = 0;
continue;
}
c->kind = retkind;
c->flags = dval;
if (c->flags & HIO_VARIABLE) {
s->multimax = c->report_count;
s->multi = 0;
c->report_count = 1;
if (s->minset) {
for (i = c->usage_minimum;
i <= c->usage_maximum;
i++) {
s->usages[s->nusage] = i;
if (s->nusage < MAXUSAGE-1)
s->nusage++;
}
s->minset = 0;
}
goto top;
} else {
if (s->minset)
c->usage = c->usage_minimum;
*h = *c;
h->next = 0;
h->pos = s->kindpos[c->kind];
s->kindpos[c->kind] += c->report_size * c->report_count;
hid_clear_local(c);
s->minset = 0;
return (1);
}
case 9: /* Output */
retkind = hid_output;
goto ret;
case 10: /* Collection */
c->kind = hid_collection;
c->collection = dval;
c->collevel++;
*h = *c;
hid_clear_local(c);
c->report_ID = NO_REPORT_ID;
s->nusage = 0;
return (1);
case 11: /* Feature */
retkind = hid_feature;
goto ret;
case 12: /* End collection */
c->kind = hid_endcollection;
c->collevel--;
*h = *c;
/*hid_clear_local(c);*/
s->nusage = 0;
return (1);
default:
return (-2);
}
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:
return (-3);
}
break;
case 2: /* Local */
switch (bTag) {
case 0:
if (bSize == 1)
dval = c->_usage_page | (dval&0xff);
else if (bSize == 2)
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;
if (bSize == 1)
dval = c->_usage_page | (dval&0xff);
else if (bSize == 2)
dval = c->_usage_page | (dval&0xffff);
c->usage_minimum = dval;
break;
case 2:
if (bSize == 1)
dval = c->_usage_page | (dval&0xff);
else if (bSize == 2)
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:
return (-4);
}
break;
default:
return (-5);
}
}
}
int
hid_report_size(report_desc_t r, unsigned int id, enum hid_kind k)
{
struct hid_data *d;
hid_item_t h;
unsigned int size = 0;
memset(&h, 0, sizeof h);
d = hid_start_parse(r, 1<<k);
while (hid_get_item(d, &h)) {
if (h.report_ID == id && h.kind == k) {
unsigned int newsize = h.pos + h.report_size;
if (newsize > size)
size = newsize;
}
}
hid_end_parse(d);
if (id != NO_REPORT_ID)
size += 8; /* add 8 bits for the report ID */
return ((size + 7) / 8); /* return size in bytes */
}
int
hid_locate(report_desc_t desc, unsigned int u, enum hid_kind k, hid_item_t *h)
{
hid_data_t d;
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);
}

View File

@ -1,235 +0,0 @@
/* $NetBSD: usage.c,v 1.8 2000/10/10 19:23:58 is Exp $ */
/*
* Copyright (c) 1999 Lennart Augustsson <augustss@netbsd.org>
* 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.
* 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.
*
* 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.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <ctype.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libusb.h"
#define _PATH_HIDTABLE "/usr/share/misc/usb_hid_usages"
struct usage_in_page {
const char *name;
int usage;
};
static struct usage_page {
const char *name;
int usage;
struct usage_in_page *page_contents;
int pagesize, pagesizemax;
} *pages;
static int npages, npagesmax;
#ifdef DEBUG
void
dump_hid_table(void)
{
int i, j;
for (i = 0; i < npages; i++) {
printf("%d\t%s\n", pages[i].usage, pages[i].name);
for (j = 0; j < pages[i].pagesize; j++) {
printf("\t%d\t%s\n", pages[i].page_contents[j].usage,
pages[i].page_contents[j].name);
}
}
}
#endif
void
hid_init(const char *hidname)
{
FILE *f;
char line[100], name[100], *p, *n;
int no;
int lineno;
struct usage_page *curpage = 0;
if (hidname == 0)
hidname = _PATH_HIDTABLE;
f = fopen(hidname, "r");
if (f == NULL)
err(1, "%s", hidname);
for (lineno = 1; ; lineno++) {
if (fgets(line, sizeof line, f) == NULL)
break;
if (line[0] == '#')
continue;
for (p = line; *p && isspace(*p); p++)
;
if (!*p)
continue;
if (sscanf(line, " * %[^\n]", name) == 1)
no = -1;
else if (sscanf(line, " 0x%x %[^\n]", &no, name) != 2 &&
sscanf(line, " %d %[^\n]", &no, name) != 2)
errx(1, "file %s, line %d, syntax error\n",
hidname, lineno);
for (p = name; *p; p++)
if (isspace(*p) || *p == '.')
*p = '_';
n = strdup(name);
if (!n)
err(1, "strdup");
if (isspace(line[0])) {
if (!curpage)
errx(1, "file %s, line %d, syntax error\n",
hidname, lineno);
if (curpage->pagesize >= curpage->pagesizemax) {
curpage->pagesizemax += 10;
curpage->page_contents =
realloc(curpage->page_contents,
curpage->pagesizemax *
sizeof (struct usage_in_page));
if (!curpage->page_contents)
err(1, "realloc");
}
curpage->page_contents[curpage->pagesize].name = n;
curpage->page_contents[curpage->pagesize].usage = no;
curpage->pagesize++;
} else {
if (npages >= npagesmax) {
if (pages == 0) {
npagesmax = 5;
pages = malloc(npagesmax *
sizeof (struct usage_page));
} else {
npagesmax += 5;
pages = realloc(pages,
npagesmax *
sizeof (struct usage_page));
}
if (!pages)
err(1, "alloc");
}
curpage = &pages[npages++];
curpage->name = n;
curpage->usage = no;
curpage->pagesize = 0;
curpage->pagesizemax = 10;
curpage->page_contents =
malloc(curpage->pagesizemax *
sizeof (struct usage_in_page));
if (!curpage->page_contents)
err(1, "malloc");
}
}
fclose(f);
#ifdef DEBUG
dump_hid_table();
#endif
}
const char *
hid_usage_page(int i)
{
static char b[10];
int k;
if (!pages)
errx(1, "no hid table\n");
for (k = 0; k < npages; k++)
if (pages[k].usage == i)
return pages[k].name;
sprintf(b, "0x%04x", i);
return b;
}
const char *
hid_usage_in_page(unsigned int u)
{
int page = HID_PAGE(u);
int i = HID_USAGE(u);
static char b[100];
int j, k, us;
for (k = 0; k < npages; k++)
if (pages[k].usage == page)
break;
if (k >= npages)
goto bad;
for (j = 0; j < pages[k].pagesize; j++) {
us = pages[k].page_contents[j].usage;
if (us == -1) {
sprintf(b, "%s %d",
pages[k].page_contents[j].name, i);
return b;
}
if (us == i)
return pages[k].page_contents[j].name;
}
bad:
sprintf(b, "0x%04x", i);
return b;
}
int
hid_parse_usage_page(const char *name)
{
int k;
if (!pages)
errx(1, "no hid table\n");
for (k = 0; k < npages; k++)
if (strcmp(pages[k].name, name) == 0)
return pages[k].usage;
return -1;
}
/* XXX handle hex */
int
hid_parse_usage_in_page(const char *name)
{
const char *sep = strchr(name, ':');
int k, j;
unsigned int l;
if (sep == NULL)
return -1;
l = sep - name;
for (k = 0; k < npages; k++)
if (strncmp(pages[k].name, name, l) == 0)
goto found;
return -1;
found:
sep++;
for (j = 0; j < pages[k].pagesize; j++)
if (strcmp(pages[k].page_contents[j].name, sep) == 0)
return (pages[k].usage << 16) | pages[k].page_contents[j].usage;
return (-1);
}

View File

@ -1,212 +0,0 @@
.\" $NetBSD: usb.3,v 1.13 2000/09/24 02:17:52 augustss Exp $
.\" $FreeBSD$
.\"
.\" Copyright (c) 1999 Lennart Augustsson <augustss@netbsd.org>
.\" 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.
.\" 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.
.\"
.\" 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.
.\"
.Dd May 11, 1999
.Dt USB 3
.Os
.Sh NAME
.Nm usb ,
.Nm hid_get_report_desc ,
.Nm hid_use_report_desc ,
.Nm hid_dispose_report_desc ,
.Nm hid_start_parse ,
.Nm hid_end_parse ,
.Nm hid_get_item ,
.Nm hid_report_size ,
.Nm hid_locate ,
.Nm hid_usage_page ,
.Nm hid_usage_in_page ,
.Nm hid_init ,
.Nm hid_get_data ,
.Nm hid_set_data
.Nd USB HID access routines
.Sh LIBRARY
.Lb libusb
.Sh SYNOPSIS
.In libusb.h
.Ft report_desc_t
.Fn hid_get_report_desc "int file"
.Ft report_desc_t
.Fn hid_use_report_desc "unsigned char *data" "unsigned int size"
.Ft void
.Fn hid_dispose_report_desc "report_desc_t d"
.Ft hid_data_t
.Fn hid_start_parse "report_desc_t d" "int kindset"
.Ft void
.Fn hid_end_parse "hid_data_t s"
.Ft int
.Fn hid_get_item "hid_data_t s" "hid_item_t *h"
.Ft int
.Fn hid_report_size "report_desc_t d" "unsigned int id" "hid_kind_t k"
.Ft int
.Fn hid_locate "report_desc_t d" "unsigned int usage" "hid_kind_t k" "hid_item_t *h"
.Ft const char *
.Fn hid_usage_page "int i"
.Ft const char *
.Fn hid_usage_in_page "unsigned int u"
.Ft int
.Fn hid_parse_usage_page "const char *name"
.Ft int
.Fn hid_parse_usage_in_page "const char *name"
.Ft void
.Fn hid_init "const char *file"
.Ft int
.Fn hid_get_data "const void *data" "const hid_item_t *h"
.Ft void
.Fn hid_set_data "void *p" "const hid_item_t *h" "int data"
.Sh DESCRIPTION
The
.Nm
library provides routines to extract data from USB Human Interface Devices.
.Ss INTRODUCTION
USB HID devices send and receive data layed out in a device dependent
way. The
.Nm
library contains routines to extract the
.Em report descriptor
which contains the data layout information and then use this information.
.Pp
The routines can be divided into four parts: extraction of the descriptor,
parsing of the descriptor, translating to/from symbolic names, and
data manipulation.
.Ss DESCRIPTOR FUNCTIONS
A report descriptor can be obtained by calling
.Fn hid_get_report_desc
with a file descriptor obtained by opening a
.Xr uhid 4
device. Alternatively a data buffer containing the report descriptor can be
passed into
.Fn hid_use_report_desc .
The data is copied into an internal structure. When the report descriptor
is no longer needed it should be freed by calling
.Fn hid_dispose_report_desc .
The type
.Fa report_desc_t
is opaque and should be used when calling the parsing functions.
If
.Fn hid_dispose_report_desc
fails it will return
.Fa NULL .
.Ss DESCRIPTOR PARSING FUNCTIONS
To parse the report descriptor the
.Fn hid_start_parse
function should be called with a report descriptor and a set that
describes which items that are interesting. The set is obtained
by or-ing together values
.Fa "(1 << k)"
where
.Fa k
is an item of type
.Fa hid_kind_t .
The function returns
.Fa NULL
if the initialization fails, otherwise an opaque value to be used
in subsequent calls.
After parsing the
.Fn hid_end_parse
function should be called to free internal data structures.
.Pp
To iterate through all the items in the report descriptor
.Fn hid_get_item
should be called while it returns a value greater than 0.
When the report descriptor ends it will returns 0; a syntax
error within the report descriptor will cause a return value less
than 0.
The struct pointed to by
.Fa h
will be filled with the relevant data for the item.
The definition of
.Fa hid_item_t
can be found in
.Pa <libusb.h>
and the meaning of the components in the USB HID documentation.
.Pp
Data should be read/written to the device in the size of
the report. The size of a report (of a certain kind) can be
computed by the
.Fn hid_report_size
function.
.Pp
To locate a single item the
.Fn hid_locate
function can be used. It should be given the usage code of
the item and its kind and it will fill the item and return
non-zero if the item was found.
.Pp
.Ss NAME TRANSLATION FUNCTIONS
The function
.Fn hid_usage_page
will return the symbolic name of a usage page, and the function
.Fn hid_usage_in_page
will return the symbolic name of the usage within the page.
Both these functions may return a pointer to static data.
.Pp
The functions
.Fn hid_parse_usage_page
and
.Fn hid_parse_usage_in_page
are the inverses of
.Fn hid_usage_page
and
.Fn hid_usage_in_page .
They take a usage string and return the number of the usage, or -1
if it cannot be found.
.Pp
Before any of these functions can be called the usage table
must be parsed, this is done by calling
.Fn hid_init
with the name of the table. Passing
.Fa NULL
to this function will cause it to use the default table.
.Ss DATA EXTRACTION FUNCTIONS
Given the data obtained from a HID device and an item in the
report descriptor the
.Fn hid_get_data
function extracts the value of the item.
Conversely
.Fn hid_set_data
can be used to put data into a report (which must be zeroed first).
.Sh EXAMPLES
Not yet.
.Sh FILES
.Pa /usr/share/misc/usb_hid_usages
The default HID usage table.
.Sh BUGS
This man page is woefully incomplete.
.Sh SEE ALSO
The
.Tn USB
specifications can be found at
.Dv http://www.usb.org/developers/docs.htm .
.Pp
.Xr uhid 4 ,
.Xr usb 4
.Sh HISTORY
The
.Nm
library first appeared in
.Nx 1.5 .

View File

@ -1,36 +0,0 @@
/* $NetBSD: usbvar.h,v 1.2 1999/05/11 21:15:46 augustss Exp $ */
/*
* Copyright (c) 1999 Lennart Augustsson <augustss@netbsd.org>
* 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.
* 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.
*
* 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.
*
* $FreeBSD$
*
*/
struct report_desc {
unsigned int size;
unsigned char data[1];
};