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.12 2007/12/21 03:40:36 imp Exp $ */
3 /* $DragonFly: src/usr.bin/usbhidctl/usbhid.c,v 1.4 2008/11/24 17:15:17 hasso Exp $ */
6 * Copyright (c) 1998 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Lennart Augustsson (augustss@netbsd.org).
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
44 #include <sys/types.h>
46 #include <sys/ioctl.h>
52 #include <bus/usb/usb.h>
53 #include <bus/usb/usbhid.h>
64 void prbits(int bits, char **strs, int n);
66 void dumpitem(const char *label, struct hid_item *h);
67 void dumpitems(report_desc_t r);
68 void rev(struct hid_item **p);
69 void prdata(u_char *buf, struct hid_item *h);
70 void dumpdata(int f, report_desc_t r, int loop);
78 for (i = 0; i < nnames; i++)
79 if (strcmp(names[i], n) == 0)
85 prbits(int bits, char **strs, int n)
89 for(i = 0; i < n; i++, bits >>= 1)
91 printf("%s%s", i == 0 ? "" : ", ", strs[i*2 + (bits&1)]);
97 extern char *__progname;
100 "usage: %s -f device "
101 "[-l] [-n] [-r] [-t tablefile] [-v] [-x] name ...\n",
105 "[-l] [-n] [-r] [-t tablefile] [-v] [-x] -a\n",
111 dumpitem(const char *label, struct hid_item *h)
113 if ((h->flags & HIO_CONST) && !verbose)
115 printf("%s size=%d count=%d page=%s usage=%s%s", label,
116 h->report_size, h->report_count,
117 hid_usage_page(HID_PAGE(h->usage)),
118 hid_usage_in_page(h->usage),
119 h->flags & HIO_CONST ? " Const" : "");
120 printf(", logical range %d..%d",
121 h->logical_minimum, h->logical_maximum);
122 if (h->physical_minimum != h->physical_maximum)
123 printf(", physical range %d..%d",
124 h->physical_minimum, h->physical_maximum);
126 printf(", unit=0x%02x exp=%d", h->unit, h->unit_exponent);
131 dumpitems(report_desc_t r)
137 for (d = hid_start_parse(r, ~0, reportid); hid_get_item(d, &h); ) {
140 printf("Collection page=%s usage=%s\n",
141 hid_usage_page(HID_PAGE(h.usage)),
142 hid_usage_in_page(h.usage));
144 case hid_endcollection:
145 printf("End collection\n");
148 dumpitem("Input ", &h);
151 dumpitem("Output ", &h);
154 dumpitem("Feature", &h);
159 size = hid_report_size(r, hid_input, 0);
160 printf("Total input size %d bytes\n", size);
162 size = hid_report_size(r, hid_output, 0);
163 printf("Total output size %d bytes\n", size);
165 size = hid_report_size(r, hid_feature, 0);
166 printf("Total feature size %d bytes\n", size);
170 rev(struct hid_item **p)
172 struct hid_item *cur, *prev, *next;
186 prdata(u_char *buf, struct hid_item *h)
192 for (i = 0; i < h->report_count; i++) {
193 data = hid_get_data(buf, h);
194 if (h->logical_minimum < 0)
195 printf("%d", (int)data);
199 printf(" [0x%x]", data);
200 pos += h->report_size;
205 dumpdata(int f, report_desc_t rd, int loop)
208 struct hid_item h, *hids, *n;
212 u_int32_t colls[100];
214 char namebuf[10000], *namep;
217 for (d = hid_start_parse(rd, 1<<hid_input, reportid);
218 hid_get_item(d, &h); ) {
219 if (h.kind == hid_collection)
220 colls[++sp] = h.usage;
221 else if (h.kind == hid_endcollection)
223 if (h.kind != hid_input || (h.flags & HIO_CONST))
226 h.collection = colls[sp];
227 hids = malloc(sizeof *hids);
232 dlen = hid_report_size(rd, hid_input, 0);
235 if (ioctl(f, USB_SET_IMMED, &one) < 0) {
236 if (errno == EOPNOTSUPP)
237 warnx("device does not support immediate mode, only changes reported.");
239 err(1, "USB_SET_IMMED");
242 r = read(f, dbuf, dlen);
244 err(1, "bad read %d != %d", r, dlen);
246 for (n = hids; n; n = n->next) {
248 namep += sprintf(namep, "%s:%s.",
249 hid_usage_page(HID_PAGE(n->collection)),
250 hid_usage_in_page(n->collection));
251 namep += sprintf(namep, "%s:%s",
252 hid_usage_page(HID_PAGE(n->usage)),
253 hid_usage_in_page(n->usage));
254 if (all || gotname(namebuf)) {
256 printf("%s=", namebuf);
257 prdata(dbuf + (reportid != 0), n);
268 main(int argc, char **argv)
272 char devnam[100], *dev = 0;
278 while ((ch = getopt(argc, argv, "af:lnrt:vx")) != -1) {
316 if (nnames == 0 && !all && !repdump)
321 snprintf(devnam, sizeof(devnam), "/dev/uhid%s", dev);
323 snprintf(devnam, sizeof(devnam), "/dev/%s", dev);
329 f = open(dev, O_RDWR);
333 r = hid_get_report_desc(f);
335 errx(1, "USB_GET_REPORT_DESC");
338 printf("Report descriptor:\n");
341 if (nnames != 0 || all)
342 dumpdata(f, r, loop);
344 hid_dispose_report_desc(r);