tcsh: fix warning to keep compiling with WARNS=2
[dragonfly.git] / usr.bin / usbhidctl / usbhid.c
blob56a596893cf5847512f909509d43d422188c6982
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 $ */
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 <usbhid.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;
58 int hexdump = 0;
59 static int reportid;
61 char **names;
62 int nnames;
64 void prbits(int bits, char **strs, int n);
65 void usage(void);
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);
71 int gotname(char *n);
73 int
74 gotname(char *n)
76 int i;
78 for (i = 0; i < nnames; i++)
79 if (strcmp(names[i], n) == 0)
80 return 1;
81 return 0;
84 void
85 prbits(int bits, char **strs, int n)
87 int i;
89 for(i = 0; i < n; i++, bits >>= 1)
90 if (strs[i*2])
91 printf("%s%s", i == 0 ? "" : ", ", strs[i*2 + (bits&1)]);
94 void
95 usage(void)
97 extern char *__progname;
99 fprintf(stderr,
100 "usage: %s -f device "
101 "[-l] [-n] [-r] [-t tablefile] [-v] [-x] name ...\n",
102 __progname);
103 fprintf(stderr,
104 " %s -f device "
105 "[-l] [-n] [-r] [-t tablefile] [-v] [-x] -a\n",
106 __progname);
107 exit(1);
110 void
111 dumpitem(const char *label, struct hid_item *h)
113 if ((h->flags & HIO_CONST) && !verbose)
114 return;
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);
125 if (h->unit)
126 printf(", unit=0x%02x exp=%d", h->unit, h->unit_exponent);
127 printf("\n");
130 void
131 dumpitems(report_desc_t r)
133 struct hid_data *d;
134 struct hid_item h;
135 int size;
137 for (d = hid_start_parse(r, ~0, reportid); hid_get_item(d, &h); ) {
138 switch (h.kind) {
139 case hid_collection:
140 printf("Collection page=%s usage=%s\n",
141 hid_usage_page(HID_PAGE(h.usage)),
142 hid_usage_in_page(h.usage));
143 break;
144 case hid_endcollection:
145 printf("End collection\n");
146 break;
147 case hid_input:
148 dumpitem("Input ", &h);
149 break;
150 case hid_output:
151 dumpitem("Output ", &h);
152 break;
153 case hid_feature:
154 dumpitem("Feature", &h);
155 break;
158 hid_end_parse(d);
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);
169 void
170 rev(struct hid_item **p)
172 struct hid_item *cur, *prev, *next;
174 prev = 0;
175 cur = *p;
176 while(cur != 0) {
177 next = cur->next;
178 cur->next = prev;
179 prev = cur;
180 cur = next;
182 *p = prev;
185 void
186 prdata(u_char *buf, struct hid_item *h)
188 u_int data;
189 int i, pos;
191 pos = h->pos;
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);
196 else
197 printf("%u", data);
198 if (hexdump)
199 printf(" [0x%x]", data);
200 pos += h->report_size;
204 void
205 dumpdata(int f, report_desc_t rd, int loop)
207 struct hid_data *d;
208 struct hid_item h, *hids, *n;
209 int r, dlen;
210 u_char *dbuf;
211 static int one = 1;
212 u_int32_t colls[100];
213 int sp = 0;
214 char namebuf[10000], *namep;
216 hids = 0;
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)
222 --sp;
223 if (h.kind != hid_input || (h.flags & HIO_CONST))
224 continue;
225 h.next = hids;
226 h.collection = colls[sp];
227 hids = malloc(sizeof *hids);
228 *hids = h;
230 hid_end_parse(d);
231 rev(&hids);
232 dlen = hid_report_size(rd, hid_input, 0);
233 dbuf = malloc(dlen);
234 if (!loop)
235 if (ioctl(f, USB_SET_IMMED, &one) < 0) {
236 if (errno == EOPNOTSUPP)
237 warnx("device does not support immediate mode, only changes reported.");
238 else
239 err(1, "USB_SET_IMMED");
241 do {
242 r = read(f, dbuf, dlen);
243 if (r != dlen) {
244 err(1, "bad read %d != %d", r, dlen);
246 for (n = hids; n; n = n->next) {
247 namep = namebuf;
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)) {
255 if (!noname)
256 printf("%s=", namebuf);
257 prdata(dbuf + (reportid != 0), n);
258 printf("\n");
261 if (loop)
262 printf("\n");
263 } while (loop);
264 free(dbuf);
268 main(int argc, char **argv)
270 int f;
271 report_desc_t r;
272 char devnam[100], *dev = 0;
273 int ch;
274 int repdump = 0;
275 int loop = 0;
276 char *table = 0;
278 while ((ch = getopt(argc, argv, "af:lnrt:vx")) != -1) {
279 switch(ch) {
280 case 'a':
281 all++;
282 break;
283 case 'f':
284 dev = optarg;
285 break;
286 case 'l':
287 loop ^= 1;
288 break;
289 case 'n':
290 noname++;
291 break;
292 case 'r':
293 repdump++;
294 break;
295 case 't':
296 table = optarg;
297 break;
298 case 'v':
299 verbose++;
300 break;
301 case 'x':
302 hexdump = 1;
303 break;
304 case '?':
305 default:
306 usage();
309 argc -= optind;
310 argv += optind;
311 if (dev == 0)
312 usage();
313 names = argv;
314 nnames = argc;
316 if (nnames == 0 && !all && !repdump)
317 usage();
319 if (dev[0] != '/') {
320 if (isdigit(dev[0]))
321 snprintf(devnam, sizeof(devnam), "/dev/uhid%s", dev);
322 else
323 snprintf(devnam, sizeof(devnam), "/dev/%s", dev);
324 dev = devnam;
327 hid_init(table);
329 f = open(dev, O_RDWR);
330 if (f < 0)
331 err(1, "%s", dev);
333 r = hid_get_report_desc(f);
334 if (r == 0)
335 errx(1, "USB_GET_REPORT_DESC");
337 if (repdump) {
338 printf("Report descriptor:\n");
339 dumpitems(r);
341 if (nnames != 0 || all)
342 dumpdata(f, r, loop);
344 hid_dispose_report_desc(r);
345 exit(0);