xref: /freebsd/usr.bin/usbhidctl/usbhid.c (revision 1946089b)
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 <unistd.h>
46 #include <err.h>
47 #include <ctype.h>
48 #include <errno.h>
49 #include <usbhid.h>
50 #include <dev/usb/usbhid.h>
51 
52 int verbose = 0;
53 int all = 0;
54 int noname = 0;
55 int hexdump = 0;
56 static int reportid;
57 
58 char **names;
59 int nnames;
60 
61 void prbits(int bits, char **strs, int n);
62 void usage(void);
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);
68 int gotname(char *n);
69 
70 int
71 gotname(char *n)
72 {
73 	int i;
74 
75 	for (i = 0; i < nnames; i++)
76 		if (strcmp(names[i], n) == 0)
77 			return 1;
78 	return 0;
79 }
80 
81 void
82 prbits(int bits, char **strs, int n)
83 {
84 	int i;
85 
86 	for(i = 0; i < n; i++, bits >>= 1)
87 		if (strs[i*2])
88 			printf("%s%s", i == 0 ? "" : ", ", strs[i*2 + (bits&1)]);
89 }
90 
91 void
92 usage(void)
93 {
94 
95 	fprintf(stderr,
96                 "usage: %s -f device "
97                 "[-l] [-n] [-r] [-t tablefile] [-v] [-x] name ...\n",
98                 getprogname());
99 	fprintf(stderr,
100                 "       %s -f device "
101                 "[-l] [-n] [-r] [-t tablefile] [-v] [-x] -a\n",
102                 getprogname());
103 	exit(1);
104 }
105 
106 void
107 dumpitem(const char *label, struct hid_item *h)
108 {
109 	if ((h->flags & HIO_CONST) && !verbose)
110 		return;
111 	printf("%s size=%d count=%d page=%s usage=%s%s", label,
112 	       h->report_size, h->report_count,
113 	       hid_usage_page(HID_PAGE(h->usage)),
114 	       hid_usage_in_page(h->usage),
115 	       h->flags & HIO_CONST ? " Const" : "");
116 	printf(", logical range %d..%d",
117 	       h->logical_minimum, h->logical_maximum);
118 	if (h->physical_minimum != h->physical_maximum)
119 		printf(", physical range %d..%d",
120 		       h->physical_minimum, h->physical_maximum);
121 	if (h->unit)
122 		printf(", unit=0x%02x exp=%d", h->unit, h->unit_exponent);
123 	printf("\n");
124 }
125 
126 void
127 dumpitems(report_desc_t r)
128 {
129 	struct hid_data *d;
130 	struct hid_item h;
131 	int size;
132 
133 	for (d = hid_start_parse(r, ~0, reportid); hid_get_item(d, &h); ) {
134 		switch (h.kind) {
135 		case hid_collection:
136 			printf("Collection page=%s usage=%s\n",
137 			       hid_usage_page(HID_PAGE(h.usage)),
138 			       hid_usage_in_page(h.usage));
139 			break;
140 		case hid_endcollection:
141 			printf("End collection\n");
142 			break;
143 		case hid_input:
144 			dumpitem("Input  ", &h);
145 			break;
146 		case hid_output:
147 			dumpitem("Output ", &h);
148 			break;
149 		case hid_feature:
150 			dumpitem("Feature", &h);
151 			break;
152 		}
153 	}
154 	hid_end_parse(d);
155 	size = hid_report_size(r, hid_input, 0);
156 	printf("Total   input size %d bytes\n", size);
157 
158 	size = hid_report_size(r, hid_output, 0);
159 	printf("Total  output size %d bytes\n", size);
160 
161 	size = hid_report_size(r, hid_feature, 0);
162 	printf("Total feature size %d bytes\n", size);
163 }
164 
165 void
166 rev(struct hid_item **p)
167 {
168 	struct hid_item *cur, *prev, *next;
169 
170 	prev = 0;
171 	cur = *p;
172 	while(cur != 0) {
173 		next = cur->next;
174 		cur->next = prev;
175 		prev = cur;
176 		cur = next;
177 	}
178 	*p = prev;
179 }
180 
181 void
182 prdata(u_char *buf, struct hid_item *h)
183 {
184 	u_int data;
185 	int i, pos;
186 
187 	pos = h->pos;
188 	for (i = 0; i < h->report_count; i++) {
189 		data = hid_get_data(buf, h);
190 		if (h->logical_minimum < 0)
191 			printf("%d", (int)data);
192 		else
193 			printf("%u", data);
194                 if (hexdump)
195 			printf(" [0x%x]", data);
196 		pos += h->report_size;
197 	}
198 }
199 
200 void
201 dumpdata(int f, report_desc_t rd, int loop)
202 {
203 	struct hid_data *d;
204 	struct hid_item h, *hids, *n;
205 	int r, dlen;
206 	u_char *dbuf;
207 	u_int32_t colls[100];
208 	int sp = 0;
209 	char namebuf[10000], *namep;
210 
211 	hids = 0;
212 	for (d = hid_start_parse(rd, 1<<hid_input, reportid);
213 	     hid_get_item(d, &h); ) {
214 		if (h.kind == hid_collection)
215 			colls[++sp] = h.usage;
216 		else if (h.kind == hid_endcollection)
217 			--sp;
218 		if (h.kind != hid_input || (h.flags & HIO_CONST))
219 			continue;
220 		h.next = hids;
221 		h.collection = colls[sp];
222 		hids = malloc(sizeof *hids);
223 		*hids = h;
224 	}
225 	hid_end_parse(d);
226 	rev(&hids);
227 	dlen = hid_report_size(rd, hid_input, 0);
228 	dbuf = malloc(dlen);
229 	if (!loop)
230 		if (hid_set_immed(f, 1) < 0) {
231 			if (errno == EOPNOTSUPP)
232 				warnx("device does not support immediate mode, only changes reported.");
233 			else
234 				err(1, "USB_SET_IMMED");
235 		}
236 	do {
237 		r = read(f, dbuf, dlen);
238 		if (r != dlen) {
239 			err(1, "bad read %d != %d", r, dlen);
240 		}
241 		for (n = hids; n; n = n->next) {
242 			namep = namebuf;
243 			namep += sprintf(namep, "%s:%s.",
244 					 hid_usage_page(HID_PAGE(n->collection)),
245 					 hid_usage_in_page(n->collection));
246 			namep += sprintf(namep, "%s:%s",
247 					 hid_usage_page(HID_PAGE(n->usage)),
248 					 hid_usage_in_page(n->usage));
249 			if (all || gotname(namebuf)) {
250 				if (!noname)
251 					printf("%s=", namebuf);
252 				prdata(dbuf + (reportid != 0), n);
253 				printf("\n");
254 			}
255 		}
256 		if (loop)
257 			printf("\n");
258 	} while (loop);
259 	free(dbuf);
260 }
261 
262 int
263 main(int argc, char **argv)
264 {
265 	int f;
266 	report_desc_t r;
267 	char devnam[100], *dev = 0;
268 	int ch;
269 	int repdump = 0;
270 	int loop = 0;
271 	char *table = 0;
272 
273 	while ((ch = getopt(argc, argv, "af:lnrt:vx")) != -1) {
274 		switch(ch) {
275 		case 'a':
276 			all++;
277 			break;
278 		case 'f':
279 			dev = optarg;
280 			break;
281 		case 'l':
282 			loop ^= 1;
283 			break;
284 		case 'n':
285 			noname++;
286 			break;
287 		case 'r':
288 			repdump++;
289 			break;
290 		case 't':
291 			table = optarg;
292 			break;
293 		case 'v':
294 			verbose++;
295 			break;
296 		case 'x':
297 			hexdump = 1;
298 			break;
299 		case '?':
300 		default:
301 			usage();
302 		}
303 	}
304 	argc -= optind;
305 	argv += optind;
306 	if (dev == 0)
307 		usage();
308 	names = argv;
309 	nnames = argc;
310 
311 	if (nnames == 0 && !all && !repdump)
312 		usage();
313 
314 	if (dev[0] != '/') {
315 		if (isdigit(dev[0]))
316 			snprintf(devnam, sizeof(devnam), "/dev/uhid%s", dev);
317 		else
318 			snprintf(devnam, sizeof(devnam), "/dev/%s", dev);
319 		dev = devnam;
320 	}
321 
322 	hid_init(table);
323 
324 	f = open(dev, O_RDWR);
325 	if (f < 0)
326 		err(1, "%s", dev);
327 
328 	r = hid_get_report_desc(f);
329 	if (r == 0)
330 		errx(1, "USB_GET_REPORT_DESC");
331 
332 	if (repdump) {
333 		printf("Report descriptor:\n");
334 		dumpitems(r);
335 	}
336 	if (nnames != 0 || all)
337 		dumpdata(f, r, loop);
338 
339 	hid_dispose_report_desc(r);
340 	exit(0);
341 }
342