1 /* $NetBSD: usbhid.c,v 1.14 2000/07/03 02:51:37 matt Exp $ */
2 /* $FreeBSD: src/usr.bin/usbhidctl/usbhid.c,v 1.6.2.1 2002/04/03 16:48:25 joe 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.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
43 #include <sys/types.h>
45 #include <sys/ioctl.h>
50 #include <libusbhid.h>
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usbhid.h>
61 void prbits(int bits, char **strs, int n);
63 void dumpitem(const char *label, struct hid_item *h);
64 void dumpitems(report_desc_t r);
65 void rev(struct hid_item **p);
66 void prdata(u_char *buf, struct hid_item *h);
67 void dumpdata(int f, report_desc_t r, int loop);
75 for (i = 0; i < nnames; i++)
76 if (strcmp(names[i], n) == 0)
82 prbits(int bits, char **strs, int n)
86 for(i = 0; i < n; i++, bits >>= 1)
88 printf("%s%s", i == 0 ? "" : ", ", strs[i*2 + (bits&1)]);
94 extern char *__progname;
96 fprintf(stderr, "Usage: %s -f device [-l] [-n] [-r] [-t tablefile] [-v] name ...\n", __progname);
97 fprintf(stderr, " %s -f device [-l] [-n] [-r] [-t tablefile] [-v] -a\n", __progname);
102 dumpitem(const char *label, struct hid_item *h)
104 if ((h->flags & HIO_CONST) && !verbose)
106 printf("%s size=%d count=%d page=%s usage=%s%s", label,
107 h->report_size, h->report_count,
108 hid_usage_page(HID_PAGE(h->usage)),
109 hid_usage_in_page(h->usage),
110 h->flags & HIO_CONST ? " Const" : "");
111 printf(", logical range %d..%d",
112 h->logical_minimum, h->logical_maximum);
113 if (h->physical_minimum != h->physical_maximum)
114 printf(", physical range %d..%d",
115 h->physical_minimum, h->physical_maximum);
117 printf(", unit=0x%02x exp=%d", h->unit, h->unit_exponent);
122 dumpitems(report_desc_t r)
128 for (d = hid_start_parse(r, ~0); hid_get_item(d, &h); ) {
131 printf("Collection page=%s usage=%s\n",
132 hid_usage_page(HID_PAGE(h.usage)),
133 hid_usage_in_page(h.usage));
135 case hid_endcollection:
136 printf("End collection\n");
139 dumpitem("Input ", &h);
142 dumpitem("Output ", &h);
145 dumpitem("Feature", &h);
150 size = hid_report_size(r, 0, hid_input);
151 printf("Total input size %d bytes\n", size);
153 size = hid_report_size(r, 0, hid_output);
154 printf("Total output size %d bytes\n", size);
156 size = hid_report_size(r, 0, hid_feature);
157 printf("Total feature size %d bytes\n", size);
161 rev(struct hid_item **p)
163 struct hid_item *cur, *prev, *next;
177 prdata(u_char *buf, struct hid_item *h)
183 for (i = 0; i < h->report_count; i++) {
184 data = hid_get_data(buf, h);
185 if (h->logical_minimum < 0)
186 printf("%d", (int)data);
189 pos += h->report_size;
194 dumpdata(int f, report_desc_t rd, int loop)
197 struct hid_item h, *hids, *n;
201 u_int32_t colls[100];
204 char namebuf[10000], *namep;
207 for (d = hid_start_parse(rd, 1<<hid_input);
208 hid_get_item(d, &h); ) {
209 if (h.kind == hid_collection)
210 colls[++sp] = h.usage;
211 else if (h.kind == hid_endcollection)
213 if (h.kind != hid_input || (h.flags & HIO_CONST))
216 h.collection = colls[sp];
217 hids = malloc(sizeof *hids);
222 dlen = hid_report_size(rd, 0, hid_input);
225 if (ioctl(f, USB_SET_IMMED, &one) < 0) {
226 if (errno == EOPNOTSUPP)
227 warnx("device does not support immediate mode, only changes reported.");
229 err(1, "USB_SET_IMMED");
232 r = read(f, dbuf, dlen);
234 err(1, "bad read %d != %d", r, dlen);
236 for (n = hids; n; n = n->next) {
238 namep += sprintf(namep, "%s:%s.",
239 hid_usage_page(HID_PAGE(n->collection)),
240 hid_usage_in_page(n->collection));
241 namep += sprintf(namep, "%s:%s",
242 hid_usage_page(HID_PAGE(n->usage)),
243 hid_usage_in_page(n->usage));
244 if (all || gotname(namebuf)) {
246 printf("%s=", namebuf);
247 prdata(dbuf + (report_id != 0), n);
258 main(int argc, char **argv)
262 char devnam[100], *dev = 0;
268 while ((ch = getopt(argc, argv, "af:lnrt:v")) != -1) {
303 if (nnames == 0 && !all && !repdump)
308 snprintf(devnam, sizeof(devnam), "/dev/uhid%s", dev);
310 snprintf(devnam, sizeof(devnam), "/dev/%s", dev);
316 f = open(dev, O_RDWR);
320 r = hid_get_report_desc(f);
322 errx(1, "USB_GET_REPORT_DESC");
325 printf("Report descriptor:\n");
328 if (nnames != 0 || all)
329 dumpdata(f, r, loop);
331 hid_dispose_report_desc(r);