xref: /freebsd/usr.bin/usbhidctl/usbhid.c (revision d056fa046c6a91b90cd98165face0e42a33a5173)
1 /*	$NetBSD: usbhid.c,v 1.14 2000/07/03 02:51:37 matt Exp $	*/
2 /*	$FreeBSD$ */
3 
4 /*
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (augustss@netbsd.org).
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
26  *
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.
38  */
39 
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <sys/types.h>
44 #include <fcntl.h>
45 #include <sys/ioctl.h>
46 #include <unistd.h>
47 #include <err.h>
48 #include <ctype.h>
49 #include <errno.h>
50 #include <usbhid.h>
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usbhid.h>
53 
54 int verbose = 0;
55 int all = 0;
56 int noname = 0;
57 static int reportid;
58 
59 char **names;
60 int nnames;
61 
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);
70 
71 int
72 gotname(char *n)
73 {
74 	int i;
75 
76 	for (i = 0; i < nnames; i++)
77 		if (strcmp(names[i], n) == 0)
78 			return 1;
79 	return 0;
80 }
81 
82 void
83 prbits(int bits, char **strs, int n)
84 {
85 	int i;
86 
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)]);
90 }
91 
92 void
93 usage(void)
94 {
95 	extern char *__progname;
96 
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);
100 }
101 
102 void
103 dumpitem(const char *label, struct hid_item *h)
104 {
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");
120 }
121 
122 void
123 dumpitems(report_desc_t r)
124 {
125 	struct hid_data *d;
126 	struct hid_item h;
127 	int size;
128 
129 	for (d = hid_start_parse(r, ~0, reportid); 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;
148 		}
149 	}
150 	hid_end_parse(d);
151 	size = hid_report_size(r, 0, hid_input);
152 	printf("Total   input size %d bytes\n", size);
153 
154 	size = hid_report_size(r, 0, hid_output);
155 	printf("Total  output size %d bytes\n", size);
156 
157 	size = hid_report_size(r, 0, hid_feature);
158 	printf("Total feature size %d bytes\n", size);
159 }
160 
161 void
162 rev(struct hid_item **p)
163 {
164 	struct hid_item *cur, *prev, *next;
165 
166 	prev = 0;
167 	cur = *p;
168 	while(cur != 0) {
169 		next = cur->next;
170 		cur->next = prev;
171 		prev = cur;
172 		cur = next;
173 	}
174 	*p = prev;
175 }
176 
177 void
178 prdata(u_char *buf, struct hid_item *h)
179 {
180 	u_int data;
181 	int i, pos;
182 
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;
191 	}
192 }
193 
194 void
195 dumpdata(int f, report_desc_t rd, int loop)
196 {
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 	char namebuf[10000], *namep;
205 
206 	hids = 0;
207 	for (d = hid_start_parse(rd, 1<<hid_input, reportid);
208 	     hid_get_item(d, &h); ) {
209 		if (h.kind == hid_collection)
210 			colls[++sp] = h.usage;
211 		else if (h.kind == hid_endcollection)
212 			--sp;
213 		if (h.kind != hid_input || (h.flags & HIO_CONST))
214 			continue;
215 		h.next = hids;
216 		h.collection = colls[sp];
217 		hids = malloc(sizeof *hids);
218 		*hids = h;
219 	}
220 	hid_end_parse(d);
221 	rev(&hids);
222 	dlen = hid_report_size(rd, 0, hid_input);
223 	dbuf = malloc(dlen);
224 	if (!loop)
225 		if (ioctl(f, USB_SET_IMMED, &one) < 0) {
226 			if (errno == EOPNOTSUPP)
227 				warnx("device does not support immediate mode, only changes reported.");
228 			else
229 				err(1, "USB_SET_IMMED");
230 		}
231 	do {
232 		r = read(f, dbuf, dlen);
233 		if (r != dlen) {
234 			err(1, "bad read %d != %d", r, dlen);
235 		}
236 		for (n = hids; n; n = n->next) {
237 			namep = namebuf;
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)) {
245 				if (!noname)
246 					printf("%s=", namebuf);
247 				prdata(dbuf + (reportid != 0), n);
248 				printf("\n");
249 			}
250 		}
251 		if (loop)
252 			printf("\n");
253 	} while (loop);
254 	free(dbuf);
255 }
256 
257 int
258 main(int argc, char **argv)
259 {
260 	int f;
261 	report_desc_t r;
262 	char devnam[100], *dev = 0;
263 	int ch;
264 	int repdump = 0;
265 	int loop = 0;
266 	char *table = 0;
267 
268 	while ((ch = getopt(argc, argv, "af:lnrt:v")) != -1) {
269 		switch(ch) {
270 		case 'a':
271 			all++;
272 			break;
273 		case 'f':
274 			dev = optarg;
275 			break;
276 		case 'l':
277 			loop ^= 1;
278 			break;
279 		case 'n':
280 			noname++;
281 			break;
282 		case 'r':
283 			repdump++;
284 			break;
285 		case 't':
286 			table = optarg;
287 			break;
288 		case 'v':
289 			verbose++;
290 			break;
291 		case '?':
292 		default:
293 			usage();
294 		}
295 	}
296 	argc -= optind;
297 	argv += optind;
298 	if (dev == 0)
299 		usage();
300 	names = argv;
301 	nnames = argc;
302 
303 	if (nnames == 0 && !all && !repdump)
304 		usage();
305 
306 	if (dev[0] != '/') {
307 		if (isdigit(dev[0]))
308 			snprintf(devnam, sizeof(devnam), "/dev/uhid%s", dev);
309 		else
310 			snprintf(devnam, sizeof(devnam), "/dev/%s", dev);
311 		dev = devnam;
312 	}
313 
314 	hid_init(table);
315 
316 	f = open(dev, O_RDWR);
317 	if (f < 0)
318 		err(1, "%s", dev);
319 
320 	r = hid_get_report_desc(f);
321 	if (r == 0)
322 		errx(1, "USB_GET_REPORT_DESC");
323 
324 	if (repdump) {
325 		printf("Report descriptor:\n");
326 		dumpitems(r);
327 	}
328 	if (nnames != 0 || all)
329 		dumpdata(f, r, loop);
330 
331 	hid_dispose_report_desc(r);
332 	exit(0);
333 }
334