1 /* $NetBSD: usbhid.c,v 1.14 2000/07/03 02:51:37 matt Exp $ */
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (augustss@netbsd.org).
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
36 #include <sys/types.h>
43 #include <dev/usb/usbhid.h>
54 void prbits(int bits, char **strs, int n);
56 void dumpitem(const char *label, struct hid_item *h);
57 void dumpitems(report_desc_t r);
58 void rev(struct hid_item **p);
59 void prdata(u_char *buf, struct hid_item *h);
60 void dumpdata(int f, report_desc_t r, int loop);
68 for (i = 0; i < nnames; i++)
69 if (strcmp(names[i], n) == 0)
75 prbits(int bits, char **strs, int n)
79 for(i = 0; i < n; i++, bits >>= 1)
81 printf("%s%s", i == 0 ? "" : ", ", strs[i*2 + (bits&1)]);
89 "usage: %s -f device "
90 "[-l] [-n] [-r] [-t tablefile] [-v] [-x] name ...\n",
94 "[-l] [-n] [-r] [-t tablefile] [-v] [-x] -a\n",
100 dumpitem(const char *label, struct hid_item *h)
102 if ((h->flags & HIO_CONST) && !verbose)
104 printf("%s size=%d count=%d page=%s usage=%s%s", label,
105 h->report_size, h->report_count,
106 hid_usage_page(HID_PAGE(h->usage)),
107 hid_usage_in_page(h->usage),
108 h->flags & HIO_CONST ? " Const" : "");
109 printf(", logical range %d..%d",
110 h->logical_minimum, h->logical_maximum);
111 if (h->physical_minimum != h->physical_maximum)
112 printf(", physical range %d..%d",
113 h->physical_minimum, h->physical_maximum);
115 printf(", unit=0x%02x exp=%d", h->unit, h->unit_exponent);
120 dumpitems(report_desc_t r)
126 for (d = hid_start_parse(r, ~0, reportid); hid_get_item(d, &h); ) {
129 printf("Collection page=%s usage=%s\n",
130 hid_usage_page(HID_PAGE(h.usage)),
131 hid_usage_in_page(h.usage));
133 case hid_endcollection:
134 printf("End collection\n");
137 dumpitem("Input ", &h);
140 dumpitem("Output ", &h);
143 dumpitem("Feature", &h);
148 size = hid_report_size(r, hid_input, 0);
149 printf("Total input size %d bytes\n", size);
151 size = hid_report_size(r, hid_output, 0);
152 printf("Total output size %d bytes\n", size);
154 size = hid_report_size(r, hid_feature, 0);
155 printf("Total feature size %d bytes\n", size);
159 rev(struct hid_item **p)
161 struct hid_item *cur, *prev, *next;
175 prdata(u_char *buf, struct hid_item *h)
181 for (i = 0; i < h->report_count; i++) {
182 data = hid_get_data(buf, h);
183 if (h->logical_minimum < 0)
184 printf("%d", (int)data);
188 printf(" [0x%x]", data);
189 pos += h->report_size;
194 dumpdata(int f, report_desc_t rd, int loop)
197 struct hid_item h, *hids, *n;
200 u_int32_t colls[100];
202 char namebuf[10000], *namep;
205 for (d = hid_start_parse(rd, 1<<hid_input, reportid);
206 hid_get_item(d, &h); ) {
207 if (h.kind == hid_collection)
208 colls[++sp] = h.usage;
209 else if (h.kind == hid_endcollection)
211 if (h.kind != hid_input || (h.flags & HIO_CONST))
214 h.collection = colls[sp];
215 hids = malloc(sizeof *hids);
220 dlen = hid_report_size(rd, hid_input, 0);
223 if (hid_set_immed(f, 1) < 0) {
224 if (errno == EOPNOTSUPP)
225 warnx("device does not support immediate mode, only changes reported.");
227 err(1, "USB_SET_IMMED");
230 r = read(f, dbuf, dlen);
232 err(1, "bad read %d != %d", r, dlen);
234 for (n = hids; n; n = n->next) {
236 namep += sprintf(namep, "%s:%s.",
237 hid_usage_page(HID_PAGE(n->collection)),
238 hid_usage_in_page(n->collection));
239 namep += sprintf(namep, "%s:%s",
240 hid_usage_page(HID_PAGE(n->usage)),
241 hid_usage_in_page(n->usage));
242 if (all || gotname(namebuf)) {
244 printf("%s=", namebuf);
245 prdata(dbuf + (reportid != 0), n);
256 main(int argc, char **argv)
260 char devnam[100], *dev = 0;
266 while ((ch = getopt(argc, argv, "af:lnrt:vx")) != -1) {
304 if (nnames == 0 && !all && !repdump)
309 snprintf(devnam, sizeof(devnam), "/dev/uhid%s", dev);
311 snprintf(devnam, sizeof(devnam), "/dev/%s", dev);
317 f = open(dev, O_RDWR);
321 r = hid_get_report_desc(f);
323 errx(1, "USB_GET_REPORT_DESC");
326 printf("Report descriptor:\n");
329 if (nnames != 0 || all)
330 dumpdata(f, r, loop);
332 hid_dispose_report_desc(r);