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