MFC r1.13 (HEAD):
[dragonfly.git] / usr.bin / usbhidctl / usbhid.c
blobada93e78c66b00a772c00362fd71c217ecc13e16
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 $ */
3 /* $DragonFly: src/usr.bin/usbhidctl/usbhid.c,v 1.3 2003/08/08 04:18:44 dillon Exp $ */
5 /*
6 * Copyright (c) 1998 The NetBSD Foundation, Inc.
7 * All rights reserved.
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
14 * are met:
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.
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <sys/types.h>
45 #include <fcntl.h>
46 #include <sys/ioctl.h>
47 #include <unistd.h>
48 #include <err.h>
49 #include <ctype.h>
50 #include <errno.h>
51 #include <libusbhid.h>
52 #include <bus/usb/usb.h>
53 #include <bus/usb/usbhid.h>
55 int verbose = 0;
56 int all = 0;
57 int noname = 0;
59 char **names;
60 int nnames;
62 void prbits(int bits, char **strs, int n);
63 void usage(void);
64 void dumpitem(const char *label, struct hid_item *h);
65 void dumpitems(report_desc_t r);
66 void rev(struct hid_item **p);
67 void prdata(u_char *buf, struct hid_item *h);
68 void dumpdata(int f, report_desc_t r, int loop);
69 int gotname(char *n);
71 int
72 gotname(char *n)
74 int i;
76 for (i = 0; i < nnames; i++)
77 if (strcmp(names[i], n) == 0)
78 return 1;
79 return 0;
82 void
83 prbits(int bits, char **strs, int n)
85 int i;
87 for(i = 0; i < n; i++, bits >>= 1)
88 if (strs[i*2])
89 printf("%s%s", i == 0 ? "" : ", ", strs[i*2 + (bits&1)]);
92 void
93 usage(void)
95 extern char *__progname;
97 fprintf(stderr, "Usage: %s -f device [-l] [-n] [-r] [-t tablefile] [-v] name ...\n", __progname);
98 fprintf(stderr, " %s -f device [-l] [-n] [-r] [-t tablefile] [-v] -a\n", __progname);
99 exit(1);
102 void
103 dumpitem(const char *label, struct hid_item *h)
105 if ((h->flags & HIO_CONST) && !verbose)
106 return;
107 printf("%s size=%d count=%d page=%s usage=%s%s", label,
108 h->report_size, h->report_count,
109 hid_usage_page(HID_PAGE(h->usage)),
110 hid_usage_in_page(h->usage),
111 h->flags & HIO_CONST ? " Const" : "");
112 printf(", logical range %d..%d",
113 h->logical_minimum, h->logical_maximum);
114 if (h->physical_minimum != h->physical_maximum)
115 printf(", physical range %d..%d",
116 h->physical_minimum, h->physical_maximum);
117 if (h->unit)
118 printf(", unit=0x%02x exp=%d", h->unit, h->unit_exponent);
119 printf("\n");
122 void
123 dumpitems(report_desc_t r)
125 struct hid_data *d;
126 struct hid_item h;
127 int size;
129 for (d = hid_start_parse(r, ~0); hid_get_item(d, &h); ) {
130 switch (h.kind) {
131 case hid_collection:
132 printf("Collection page=%s usage=%s\n",
133 hid_usage_page(HID_PAGE(h.usage)),
134 hid_usage_in_page(h.usage));
135 break;
136 case hid_endcollection:
137 printf("End collection\n");
138 break;
139 case hid_input:
140 dumpitem("Input ", &h);
141 break;
142 case hid_output:
143 dumpitem("Output ", &h);
144 break;
145 case hid_feature:
146 dumpitem("Feature", &h);
147 break;
150 hid_end_parse(d);
151 size = hid_report_size(r, 0, hid_input);
152 printf("Total input size %d bytes\n", size);
154 size = hid_report_size(r, 0, hid_output);
155 printf("Total output size %d bytes\n", size);
157 size = hid_report_size(r, 0, hid_feature);
158 printf("Total feature size %d bytes\n", size);
161 void
162 rev(struct hid_item **p)
164 struct hid_item *cur, *prev, *next;
166 prev = 0;
167 cur = *p;
168 while(cur != 0) {
169 next = cur->next;
170 cur->next = prev;
171 prev = cur;
172 cur = next;
174 *p = prev;
177 void
178 prdata(u_char *buf, struct hid_item *h)
180 u_int data;
181 int i, pos;
183 pos = h->pos;
184 for (i = 0; i < h->report_count; i++) {
185 data = hid_get_data(buf, h);
186 if (h->logical_minimum < 0)
187 printf("%d", (int)data);
188 else
189 printf("%u", data);
190 pos += h->report_size;
194 void
195 dumpdata(int f, report_desc_t rd, int loop)
197 struct hid_data *d;
198 struct hid_item h, *hids, *n;
199 int r, dlen;
200 u_char *dbuf;
201 static int one = 1;
202 u_int32_t colls[100];
203 int sp = 0;
204 int report_id;
205 char namebuf[10000], *namep;
207 hids = 0;
208 for (d = hid_start_parse(rd, 1<<hid_input);
209 hid_get_item(d, &h); ) {
210 if (h.kind == hid_collection)
211 colls[++sp] = h.usage;
212 else if (h.kind == hid_endcollection)
213 --sp;
214 if (h.kind != hid_input || (h.flags & HIO_CONST))
215 continue;
216 h.next = hids;
217 h.collection = colls[sp];
218 hids = malloc(sizeof *hids);
219 *hids = h;
221 hid_end_parse(d);
222 rev(&hids);
223 dlen = hid_report_size(rd, 0, hid_input);
224 dbuf = malloc(dlen);
225 if (!loop)
226 if (ioctl(f, USB_SET_IMMED, &one) < 0) {
227 if (errno == EOPNOTSUPP)
228 warnx("device does not support immediate mode, only changes reported.");
229 else
230 err(1, "USB_SET_IMMED");
232 do {
233 r = read(f, dbuf, dlen);
234 if (r != dlen) {
235 err(1, "bad read %d != %d", r, dlen);
237 for (n = hids; n; n = n->next) {
238 namep = namebuf;
239 namep += sprintf(namep, "%s:%s.",
240 hid_usage_page(HID_PAGE(n->collection)),
241 hid_usage_in_page(n->collection));
242 namep += sprintf(namep, "%s:%s",
243 hid_usage_page(HID_PAGE(n->usage)),
244 hid_usage_in_page(n->usage));
245 if (all || gotname(namebuf)) {
246 if (!noname)
247 printf("%s=", namebuf);
248 prdata(dbuf + (report_id != 0), n);
249 printf("\n");
252 if (loop)
253 printf("\n");
254 } while (loop);
255 free(dbuf);
259 main(int argc, char **argv)
261 int f;
262 report_desc_t r;
263 char devnam[100], *dev = 0;
264 int ch;
265 int repdump = 0;
266 int loop = 0;
267 char *table = 0;
269 while ((ch = getopt(argc, argv, "af:lnrt:v")) != -1) {
270 switch(ch) {
271 case 'a':
272 all++;
273 break;
274 case 'f':
275 dev = optarg;
276 break;
277 case 'l':
278 loop ^= 1;
279 break;
280 case 'n':
281 noname++;
282 break;
283 case 'r':
284 repdump++;
285 break;
286 case 't':
287 table = optarg;
288 break;
289 case 'v':
290 verbose++;
291 break;
292 case '?':
293 default:
294 usage();
297 argc -= optind;
298 argv += optind;
299 if (dev == 0)
300 usage();
301 names = argv;
302 nnames = argc;
304 if (nnames == 0 && !all && !repdump)
305 usage();
307 if (dev[0] != '/') {
308 if (isdigit(dev[0]))
309 snprintf(devnam, sizeof(devnam), "/dev/uhid%s", dev);
310 else
311 snprintf(devnam, sizeof(devnam), "/dev/%s", dev);
312 dev = devnam;
315 hid_init(table);
317 f = open(dev, O_RDWR);
318 if (f < 0)
319 err(1, "%s", dev);
321 r = hid_get_report_desc(f);
322 if (r == 0)
323 errx(1, "USB_GET_REPORT_DESC");
325 if (repdump) {
326 printf("Report descriptor:\n");
327 dumpitems(r);
329 if (nnames != 0 || all)
330 dumpdata(f, r, loop);
332 hid_dispose_report_desc(r);
333 exit(0);