1999-05-12 04:38:20 +04:00
|
|
|
/* $NetBSD: usbhid.c,v 1.11 1999/05/12 00:38:20 augustss Exp $ */
|
1998-07-13 15:14:03 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1998 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
1998-11-26 01:17:06 +03:00
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Lennart Augustsson (augustss@netbsd.org).
|
1998-07-13 15:14:03 +04: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 <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <err.h>
|
1998-07-23 17:48:59 +04:00
|
|
|
#include <ctype.h>
|
1999-05-12 01:02:24 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <usb.h>
|
1998-07-13 15:14:03 +04:00
|
|
|
#include <dev/usb/usb.h>
|
|
|
|
#include <dev/usb/usbhid.h>
|
|
|
|
|
|
|
|
#define USBDEV "/dev/uhid0"
|
|
|
|
|
|
|
|
int verbose = 0;
|
1998-07-14 00:56:28 +04:00
|
|
|
int all = 0;
|
|
|
|
int noname = 0;
|
|
|
|
|
|
|
|
char **names;
|
|
|
|
int nnames;
|
1998-07-13 15:14:03 +04:00
|
|
|
|
|
|
|
void prbits(int bits, char **strs, int n);
|
|
|
|
void usage(void);
|
1999-04-21 21:41:08 +04:00
|
|
|
void dumpitem(char *label, struct hid_item *h);
|
1999-05-12 01:02:24 +04:00
|
|
|
void dumpitems(report_desc_t r);
|
1998-07-13 15:14:03 +04:00
|
|
|
void rev(struct hid_item **p);
|
|
|
|
void prdata(u_char *buf, struct hid_item *h);
|
1999-05-12 01:02:24 +04:00
|
|
|
void dumpdata(int f, report_desc_t r, int loop);
|
1998-07-14 00:56:28 +04:00
|
|
|
int gotname(char *n);
|
|
|
|
|
|
|
|
int
|
|
|
|
gotname(char *n)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nnames; i++)
|
|
|
|
if (strcmp(names[i], n) == 0)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
1998-07-13 15:14:03 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
prbits(int bits, char **strs, int n)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i = 0; i < n; i++, bits >>= 1)
|
|
|
|
if (strs[i*2])
|
|
|
|
printf("%s%s", i == 0 ? "" : ", ", strs[i*2 + (bits&1)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
extern char *__progname;
|
|
|
|
|
1998-12-03 23:46:10 +03:00
|
|
|
fprintf(stderr, "Usage: %s -f device [-l] [-n] [-r] [-t tablefile] [-v] name ...\n", __progname);
|
|
|
|
fprintf(stderr, " %s -f device [-l] [-n] [-r] [-t tablefile] [-v] -a\n", __progname);
|
1998-07-13 15:14:03 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
1999-04-21 21:41:08 +04:00
|
|
|
void
|
|
|
|
dumpitem(char *label, struct hid_item *h)
|
|
|
|
{
|
|
|
|
if ((h->flags & HIO_CONST) && !verbose)
|
|
|
|
return;
|
|
|
|
printf("%s size=%d count=%d page=%s usage=%s%s", label,
|
|
|
|
h->report_size, h->report_count,
|
1999-05-12 04:38:20 +04:00
|
|
|
hid_usage_page(HID_PAGE(h->usage)),
|
|
|
|
hid_usage_in_page(h->usage),
|
1999-04-21 21:41:08 +04:00
|
|
|
h->flags & HIO_CONST ? " Const" : "");
|
|
|
|
printf(", logical range %d..%d",
|
|
|
|
h->logical_minimum, h->logical_maximum);
|
|
|
|
if (h->physical_minimum != h->physical_maximum)
|
|
|
|
printf(", physical range %d..%d",
|
|
|
|
h->physical_minimum, h->physical_maximum);
|
|
|
|
if (h->unit)
|
|
|
|
printf(", unit=0x%02x exp=%d", h->unit, h->unit_exponent);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
1998-07-13 15:14:03 +04:00
|
|
|
void
|
1999-05-12 01:02:24 +04:00
|
|
|
dumpitems(report_desc_t r)
|
1998-07-13 15:14:03 +04:00
|
|
|
{
|
|
|
|
struct hid_data *d;
|
|
|
|
struct hid_item h;
|
1999-04-21 21:41:08 +04:00
|
|
|
int report_id, size;
|
1998-07-13 15:14:03 +04:00
|
|
|
|
1999-05-12 01:02:24 +04:00
|
|
|
for (d = hid_start_parse(r, ~0); hid_get_item(d, &h); ) {
|
1998-07-13 15:14:03 +04:00
|
|
|
switch (h.kind) {
|
|
|
|
case hid_collection:
|
|
|
|
printf("Collection page=%s usage=%s\n",
|
1999-05-12 04:38:20 +04:00
|
|
|
hid_usage_page(HID_PAGE(h.usage)),
|
|
|
|
hid_usage_in_page(h.usage));
|
1998-07-13 15:14:03 +04:00
|
|
|
break;
|
|
|
|
case hid_endcollection:
|
|
|
|
printf("End collection\n");
|
|
|
|
break;
|
|
|
|
case hid_input:
|
1999-04-21 21:41:08 +04:00
|
|
|
dumpitem("Input ", &h);
|
1998-07-13 15:14:03 +04:00
|
|
|
break;
|
|
|
|
case hid_output:
|
1999-04-21 21:41:08 +04:00
|
|
|
dumpitem("Output ", &h);
|
1998-07-13 15:14:03 +04:00
|
|
|
break;
|
|
|
|
case hid_feature:
|
1999-04-21 21:41:08 +04:00
|
|
|
dumpitem("Feature", &h);
|
1998-07-13 15:14:03 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hid_end_parse(d);
|
1999-05-12 01:02:24 +04:00
|
|
|
size = hid_report_size(r, hid_input, &report_id);
|
1999-04-21 21:41:08 +04:00
|
|
|
size -= report_id != 0;
|
|
|
|
printf("Total input size %s%d bytes\n",
|
|
|
|
report_id && size ? "1+" : "", size);
|
|
|
|
|
1999-05-12 01:02:24 +04:00
|
|
|
size = hid_report_size(r, hid_output, &report_id);
|
1999-04-21 21:41:08 +04:00
|
|
|
size -= report_id != 0;
|
|
|
|
printf("Total output size %s%d bytes\n",
|
|
|
|
report_id && size ? "1+" : "", size);
|
|
|
|
|
1999-05-12 01:02:24 +04:00
|
|
|
size = hid_report_size(r, hid_feature, &report_id);
|
1999-04-21 21:41:08 +04:00
|
|
|
size -= report_id != 0;
|
|
|
|
printf("Total feature size %s%d bytes\n",
|
|
|
|
report_id && size ? "1+" : "", size);
|
1998-07-13 15:14:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rev(struct hid_item **p)
|
|
|
|
{
|
|
|
|
struct hid_item *cur, *prev, *next;
|
|
|
|
|
|
|
|
prev = 0;
|
|
|
|
cur = *p;
|
|
|
|
while(cur != 0) {
|
|
|
|
next = cur->next;
|
|
|
|
cur->next = prev;
|
|
|
|
prev = cur;
|
|
|
|
cur = next;
|
|
|
|
}
|
|
|
|
*p = prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
prdata(u_char *buf, struct hid_item *h)
|
|
|
|
{
|
1999-05-12 04:05:11 +04:00
|
|
|
u_int data;
|
1998-07-13 15:14:03 +04:00
|
|
|
int i, pos;
|
|
|
|
|
|
|
|
pos = h->pos;
|
|
|
|
for (i = 0; i < h->report_count; i++) {
|
1999-05-12 04:38:20 +04:00
|
|
|
data = hid_get_data(buf, h);
|
1998-07-13 15:14:03 +04:00
|
|
|
if (h->logical_minimum < 0)
|
1999-05-12 04:05:11 +04:00
|
|
|
printf("%d", (int)data);
|
1998-07-13 15:14:03 +04:00
|
|
|
else
|
1999-05-12 04:05:11 +04:00
|
|
|
printf("%u", data);
|
1998-07-13 15:14:03 +04:00
|
|
|
pos += h->report_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-05-12 01:02:24 +04:00
|
|
|
dumpdata(int f, report_desc_t rd, int loop)
|
1998-07-13 15:14:03 +04:00
|
|
|
{
|
|
|
|
struct hid_data *d;
|
|
|
|
struct hid_item h, *hids, *n;
|
|
|
|
int r, dlen;
|
|
|
|
u_char *dbuf;
|
|
|
|
static int one = 1;
|
|
|
|
u_int32_t colls[100];
|
|
|
|
int sp = 0;
|
1999-04-21 20:23:14 +04:00
|
|
|
int report_id;
|
1998-07-14 00:56:28 +04:00
|
|
|
char namebuf[10000], *namep;
|
1998-07-13 15:14:03 +04:00
|
|
|
|
|
|
|
hids = 0;
|
1999-05-12 01:02:24 +04:00
|
|
|
for (d = hid_start_parse(rd, 1<<hid_input);
|
1998-07-13 15:14:03 +04:00
|
|
|
hid_get_item(d, &h); ) {
|
|
|
|
if (h.kind == hid_collection)
|
|
|
|
colls[++sp] = h.usage;
|
|
|
|
else if (h.kind == hid_endcollection)
|
|
|
|
--sp;
|
|
|
|
if (h.kind != hid_input || (h.flags & HIO_CONST))
|
|
|
|
continue;
|
|
|
|
h.next = hids;
|
|
|
|
h.collection = colls[sp];
|
|
|
|
hids = malloc(sizeof *hids);
|
|
|
|
*hids = h;
|
|
|
|
}
|
|
|
|
hid_end_parse(d);
|
|
|
|
rev(&hids);
|
1999-05-12 01:02:24 +04:00
|
|
|
dlen = hid_report_size(rd, hid_input, &report_id);
|
1998-07-13 15:14:03 +04:00
|
|
|
dbuf = malloc(dlen);
|
|
|
|
if (!loop)
|
1998-12-03 23:46:10 +03:00
|
|
|
if (ioctl(f, USB_SET_IMMED, &one) < 0) {
|
|
|
|
if (errno == EOPNOTSUPP)
|
|
|
|
warnx("device does not support immediate mode, only changes reported.");
|
|
|
|
else
|
|
|
|
err(1, "USB_SET_IMMED");
|
|
|
|
}
|
1998-07-13 15:14:03 +04:00
|
|
|
do {
|
|
|
|
r = read(f, dbuf, dlen);
|
|
|
|
if (r != dlen) {
|
|
|
|
err(1, "bad read %d != %d", r, dlen);
|
|
|
|
}
|
|
|
|
for (n = hids; n; n = n->next) {
|
1998-07-14 00:56:28 +04:00
|
|
|
namep = namebuf;
|
|
|
|
namep += sprintf(namep, "%s:%s.",
|
1999-05-12 04:38:20 +04:00
|
|
|
hid_usage_page(HID_PAGE(n->collection)),
|
|
|
|
hid_usage_in_page(n->collection));
|
1998-07-14 00:56:28 +04:00
|
|
|
namep += sprintf(namep, "%s:%s",
|
1999-05-12 04:38:20 +04:00
|
|
|
hid_usage_page(HID_PAGE(n->usage)),
|
|
|
|
hid_usage_in_page(n->usage));
|
1998-07-14 00:56:28 +04:00
|
|
|
if (all || gotname(namebuf)) {
|
|
|
|
if (!noname)
|
|
|
|
printf("%s=", namebuf);
|
1999-04-21 20:23:14 +04:00
|
|
|
prdata(dbuf + (report_id != 0), n);
|
1998-07-14 00:56:28 +04:00
|
|
|
printf("\n");
|
|
|
|
}
|
1998-07-13 15:14:03 +04:00
|
|
|
}
|
|
|
|
if (loop)
|
|
|
|
printf("\n");
|
|
|
|
} while (loop);
|
|
|
|
free(dbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
1999-05-12 01:02:24 +04:00
|
|
|
int f;
|
|
|
|
report_desc_t r;
|
1998-07-23 17:48:59 +04:00
|
|
|
char devname[100], *dev = 0;
|
1998-07-13 15:14:03 +04:00
|
|
|
int ch;
|
|
|
|
extern char *optarg;
|
|
|
|
extern int optind;
|
|
|
|
int repdump = 0;
|
|
|
|
int loop = 0;
|
1999-05-12 01:02:24 +04:00
|
|
|
char *table = 0;
|
1998-07-13 15:14:03 +04:00
|
|
|
|
1998-07-14 00:56:28 +04:00
|
|
|
while ((ch = getopt(argc, argv, "af:lnrt:v")) != -1) {
|
1998-07-13 15:14:03 +04:00
|
|
|
switch(ch) {
|
1998-07-14 00:56:28 +04:00
|
|
|
case 'a':
|
|
|
|
all++;
|
|
|
|
break;
|
1998-07-13 15:14:03 +04:00
|
|
|
case 'f':
|
|
|
|
dev = optarg;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
loop ^= 1;
|
|
|
|
break;
|
|
|
|
case 'n':
|
1998-07-14 00:56:28 +04:00
|
|
|
noname++;
|
1998-07-13 15:14:03 +04:00
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
repdump++;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
table = optarg;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbose++;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
1998-07-14 00:56:28 +04:00
|
|
|
if (dev == 0)
|
1998-07-13 15:14:03 +04:00
|
|
|
usage();
|
1998-07-14 00:56:28 +04:00
|
|
|
names = argv;
|
|
|
|
nnames = argc;
|
1998-07-13 15:14:03 +04:00
|
|
|
|
1999-04-21 21:41:08 +04:00
|
|
|
if (nnames == 0 && !all && !repdump)
|
1998-12-03 23:46:10 +03:00
|
|
|
usage();
|
|
|
|
|
1998-07-23 17:48:59 +04:00
|
|
|
if (dev[0] != '/') {
|
|
|
|
if (isdigit(dev[0]))
|
|
|
|
sprintf(devname, "/dev/uhid%s", dev);
|
|
|
|
else
|
|
|
|
sprintf(devname, "/dev/%s", dev);
|
|
|
|
dev = devname;
|
|
|
|
}
|
|
|
|
|
1999-05-12 04:38:20 +04:00
|
|
|
hid_init(table);
|
1998-07-13 15:14:03 +04:00
|
|
|
|
|
|
|
f = open(dev, O_RDWR);
|
|
|
|
if (f < 0)
|
|
|
|
err(1, "%s", dev);
|
|
|
|
|
1999-05-12 04:38:20 +04:00
|
|
|
r = hid_get_report_desc(f);
|
1999-05-12 01:02:24 +04:00
|
|
|
if (r == 0)
|
1998-07-13 15:14:03 +04:00
|
|
|
errx(1, "USB_GET_REPORT_DESC");
|
|
|
|
|
|
|
|
if (repdump) {
|
1999-05-12 01:02:24 +04:00
|
|
|
printf("Report descriptor:\n");
|
|
|
|
dumpitems(r);
|
1998-07-13 15:14:03 +04:00
|
|
|
}
|
1999-04-21 21:41:08 +04:00
|
|
|
if (nnames != 0 || all)
|
1999-05-12 01:02:24 +04:00
|
|
|
dumpdata(f, r, loop);
|
1998-07-13 15:14:03 +04:00
|
|
|
|
1999-05-12 04:38:20 +04:00
|
|
|
hid_dispose_report_desc(r);
|
1998-07-13 15:14:03 +04:00
|
|
|
exit(0);
|
|
|
|
}
|