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>
53 void prbits(int bits, char **strs, int n);
55 void dumpitem(const char *label, struct hid_item *h);
56 void dumpitems(report_desc_t r);
57 void rev(struct hid_item **p);
58 void prdata(u_char *buf, struct hid_item *h);
59 void dumpdata(int f, report_desc_t r, int loop);
67 for (i = 0; i < nnames; i++)
68 if (strcmp(names[i], n) == 0)
74 prbits(int bits, char **strs, int n)
78 for(i = 0; i < n; i++, bits >>= 1)
80 printf("%s%s", i == 0 ? "" : ", ", strs[i*2 + (bits&1)]);
88 "usage: %s -f device "
89 "[-l] [-n] [-r] [-t tablefile] [-v] [-x] name ...\n",
93 "[-l] [-n] [-r] [-t tablefile] [-v] [-x] -a\n",
99 dumpitem(const char *label, struct hid_item *h)
101 if ((h->flags & HIO_CONST) && !verbose)
103 printf("%s rid=%d size=%d count=%d page=%s usage=%s%s%s", label,
104 h->report_ID, h->report_size, h->report_count,
105 hid_usage_page(HID_PAGE(h->usage)),
106 hid_usage_in_page(h->usage),
107 h->flags & HIO_CONST ? " Const" : "",
108 h->flags & HIO_VARIABLE ? "" : " Array");
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 hid_collection_type(int32_t type)
125 case 0: return ("Physical");
126 case 1: return ("Application");
127 case 2: return ("Logical");
128 case 3: return ("Report");
129 case 4: return ("Named_Array");
130 case 5: return ("Usage_Switch");
131 case 6: return ("Usage_Modifier");
133 snprintf(num, sizeof(num), "0x%02x", type);
138 dumpitems(report_desc_t r)
144 for (d = hid_start_parse(r, ~0, -1); hid_get_item(d, &h); ) {
147 printf("Collection type=%s page=%s usage=%s\n",
148 hid_collection_type(h.collection),
149 hid_usage_page(HID_PAGE(h.usage)),
150 hid_usage_in_page(h.usage));
152 case hid_endcollection:
153 printf("End collection\n");
156 dumpitem("Input ", &h);
159 dumpitem("Output ", &h);
162 dumpitem("Feature", &h);
167 size = hid_report_size(r, hid_input, -1);
168 printf("Total input size %d bytes\n", size);
170 size = hid_report_size(r, hid_output, -1);
171 printf("Total output size %d bytes\n", size);
173 size = hid_report_size(r, hid_feature, -1);
174 printf("Total feature size %d bytes\n", size);
178 rev(struct hid_item **p)
180 struct hid_item *cur, *prev, *next;
194 prdata(u_char *buf, struct hid_item *h)
200 for (i = 0; i < h->report_count; i++) {
201 data = hid_get_data(buf, h);
204 if (h->logical_minimum < 0)
205 printf("%d", (int)data);
209 printf(" [0x%x]", data);
210 h->pos += h->report_size;
216 dumpdata(int f, report_desc_t rd, int loop)
219 struct hid_item h, *hids, *n;
222 u_int32_t colls[100];
224 char namebuf[10000], *namep;
227 for (d = hid_start_parse(rd, 1<<hid_input, -1);
228 hid_get_item(d, &h); ) {
229 if (h.kind == hid_collection)
230 colls[++sp] = h.usage;
231 else if (h.kind == hid_endcollection)
233 if (h.kind != hid_input || (h.flags & HIO_CONST))
236 h.collection = colls[sp];
237 hids = malloc(sizeof *hids);
242 dlen = hid_report_size(rd, hid_input, -1);
245 if (hid_set_immed(f, 1) < 0) {
246 if (errno == EOPNOTSUPP)
247 warnx("device does not support immediate mode, only changes reported.");
249 err(1, "USB_SET_IMMED");
252 r = read(f, dbuf, dlen);
254 err(1, "read error");
256 for (n = hids; n; n = n->next) {
257 if (n->report_ID != 0 && dbuf[0] != n->report_ID)
260 namep += sprintf(namep, "%s:%s.",
261 hid_usage_page(HID_PAGE(n->collection)),
262 hid_usage_in_page(n->collection));
263 namep += sprintf(namep, "%s:%s",
264 hid_usage_page(HID_PAGE(n->usage)),
265 hid_usage_in_page(n->usage));
266 if (all || gotname(namebuf)) {
268 printf("%s=", namebuf);
280 main(int argc, char **argv)
284 char devnam[100], *dev = 0;
290 while ((ch = getopt(argc, argv, "af:lnrt:vx")) != -1) {
328 if (nnames == 0 && !all && !repdump)
333 snprintf(devnam, sizeof(devnam), "/dev/uhid%s", dev);
335 snprintf(devnam, sizeof(devnam), "/dev/%s", dev);
341 f = open(dev, O_RDWR);
345 r = hid_get_report_desc(f);
347 errx(1, "USB_GET_REPORT_DESC");
350 printf("Report descriptor:\n");
353 if (nnames != 0 || all)
354 dumpdata(f, r, loop);
356 hid_dispose_report_desc(r);