xref: /freebsd/usr.bin/usbhidctl/usbhid.c (revision 9a2a66f1)
109d308a8SNick Hibma /*	$NetBSD: usbhid.c,v 1.14 2000/07/03 02:51:37 matt Exp $	*/
209d308a8SNick Hibma /*	$FreeBSD$ */
309d308a8SNick Hibma 
409d308a8SNick Hibma /*
509d308a8SNick Hibma  * Copyright (c) 1998 The NetBSD Foundation, Inc.
609d308a8SNick Hibma  * All rights reserved.
709d308a8SNick Hibma  *
809d308a8SNick Hibma  * This code is derived from software contributed to The NetBSD Foundation
909d308a8SNick Hibma  * by Lennart Augustsson (augustss@netbsd.org).
1009d308a8SNick Hibma  *
1109d308a8SNick Hibma  * Redistribution and use in source and binary forms, with or without
1209d308a8SNick Hibma  * modification, are permitted provided that the following conditions
1309d308a8SNick Hibma  * are met:
1409d308a8SNick Hibma  * 1. Redistributions of source code must retain the above copyright
1509d308a8SNick Hibma  *    notice, this list of conditions and the following disclaimer.
1609d308a8SNick Hibma  * 2. Redistributions in binary form must reproduce the above copyright
1709d308a8SNick Hibma  *    notice, this list of conditions and the following disclaimer in the
1809d308a8SNick Hibma  *    documentation and/or other materials provided with the distribution.
1909d308a8SNick Hibma  * 3. All advertising materials mentioning features or use of this software
2009d308a8SNick Hibma  *    must display the following acknowledgement:
2109d308a8SNick Hibma  *        This product includes software developed by the NetBSD
2209d308a8SNick Hibma  *        Foundation, Inc. and its contributors.
2309d308a8SNick Hibma  * 4. Neither the name of The NetBSD Foundation nor the names of its
2409d308a8SNick Hibma  *    contributors may be used to endorse or promote products derived
2509d308a8SNick Hibma  *    from this software without specific prior written permission.
2609d308a8SNick Hibma  *
2709d308a8SNick Hibma  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2809d308a8SNick Hibma  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2909d308a8SNick Hibma  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
3009d308a8SNick Hibma  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
3109d308a8SNick Hibma  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3209d308a8SNick Hibma  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3309d308a8SNick Hibma  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3409d308a8SNick Hibma  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3509d308a8SNick Hibma  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3609d308a8SNick Hibma  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3709d308a8SNick Hibma  * POSSIBILITY OF SUCH DAMAGE.
3809d308a8SNick Hibma  */
3909d308a8SNick Hibma 
4009d308a8SNick Hibma #include <stdio.h>
4109d308a8SNick Hibma #include <stdlib.h>
4209d308a8SNick Hibma #include <string.h>
4309d308a8SNick Hibma #include <sys/types.h>
4409d308a8SNick Hibma #include <fcntl.h>
4509d308a8SNick Hibma #include <sys/ioctl.h>
4609d308a8SNick Hibma #include <unistd.h>
4709d308a8SNick Hibma #include <err.h>
4809d308a8SNick Hibma #include <ctype.h>
4909d308a8SNick Hibma #include <errno.h>
50cf0e07e5SMatthew N. Dodd #include <usbhid.h>
5109d308a8SNick Hibma #include <dev/usb/usb.h>
5209d308a8SNick Hibma #include <dev/usb/usbhid.h>
5309d308a8SNick Hibma 
5409d308a8SNick Hibma int verbose = 0;
5509d308a8SNick Hibma int all = 0;
5609d308a8SNick Hibma int noname = 0;
579a2a66f1SGreg Lehey int hexdump = 0;
58cf0e07e5SMatthew N. Dodd static int reportid;
5909d308a8SNick Hibma 
6009d308a8SNick Hibma char **names;
6109d308a8SNick Hibma int nnames;
6209d308a8SNick Hibma 
6309d308a8SNick Hibma void prbits(int bits, char **strs, int n);
6409d308a8SNick Hibma void usage(void);
651a27f4f2SMark Murray void dumpitem(const char *label, struct hid_item *h);
6609d308a8SNick Hibma void dumpitems(report_desc_t r);
6709d308a8SNick Hibma void rev(struct hid_item **p);
6809d308a8SNick Hibma void prdata(u_char *buf, struct hid_item *h);
6909d308a8SNick Hibma void dumpdata(int f, report_desc_t r, int loop);
7009d308a8SNick Hibma int gotname(char *n);
7109d308a8SNick Hibma 
7209d308a8SNick Hibma int
7309d308a8SNick Hibma gotname(char *n)
7409d308a8SNick Hibma {
7509d308a8SNick Hibma 	int i;
7609d308a8SNick Hibma 
7709d308a8SNick Hibma 	for (i = 0; i < nnames; i++)
7809d308a8SNick Hibma 		if (strcmp(names[i], n) == 0)
7909d308a8SNick Hibma 			return 1;
8009d308a8SNick Hibma 	return 0;
8109d308a8SNick Hibma }
8209d308a8SNick Hibma 
8309d308a8SNick Hibma void
8409d308a8SNick Hibma prbits(int bits, char **strs, int n)
8509d308a8SNick Hibma {
8609d308a8SNick Hibma 	int i;
8709d308a8SNick Hibma 
8809d308a8SNick Hibma 	for(i = 0; i < n; i++, bits >>= 1)
8909d308a8SNick Hibma 		if (strs[i*2])
9009d308a8SNick Hibma 			printf("%s%s", i == 0 ? "" : ", ", strs[i*2 + (bits&1)]);
9109d308a8SNick Hibma }
9209d308a8SNick Hibma 
9309d308a8SNick Hibma void
9409d308a8SNick Hibma usage(void)
9509d308a8SNick Hibma {
9609d308a8SNick Hibma 	extern char *__progname;
9709d308a8SNick Hibma 
989a2a66f1SGreg Lehey 	fprintf(stderr,
999a2a66f1SGreg Lehey                 "usage: %s -f device "
1009a2a66f1SGreg Lehey                 "[-l] [-n] [-r] [-t tablefile] [-v] [x] name ...\n",
1019a2a66f1SGreg Lehey                 __progname);
1029a2a66f1SGreg Lehey 	fprintf(stderr,
1039a2a66f1SGreg Lehey                 "       %s -f device "
1049a2a66f1SGreg Lehey                 "[-l] [-n] [-r] [-t tablefile] [-v] [x] -a\n",
1059a2a66f1SGreg Lehey                 __progname);
10609d308a8SNick Hibma 	exit(1);
10709d308a8SNick Hibma }
10809d308a8SNick Hibma 
10909d308a8SNick Hibma void
1101a27f4f2SMark Murray dumpitem(const char *label, struct hid_item *h)
11109d308a8SNick Hibma {
11209d308a8SNick Hibma 	if ((h->flags & HIO_CONST) && !verbose)
11309d308a8SNick Hibma 		return;
11409d308a8SNick Hibma 	printf("%s size=%d count=%d page=%s usage=%s%s", label,
11509d308a8SNick Hibma 	       h->report_size, h->report_count,
11609d308a8SNick Hibma 	       hid_usage_page(HID_PAGE(h->usage)),
11709d308a8SNick Hibma 	       hid_usage_in_page(h->usage),
11809d308a8SNick Hibma 	       h->flags & HIO_CONST ? " Const" : "");
11909d308a8SNick Hibma 	printf(", logical range %d..%d",
12009d308a8SNick Hibma 	       h->logical_minimum, h->logical_maximum);
12109d308a8SNick Hibma 	if (h->physical_minimum != h->physical_maximum)
12209d308a8SNick Hibma 		printf(", physical range %d..%d",
12309d308a8SNick Hibma 		       h->physical_minimum, h->physical_maximum);
12409d308a8SNick Hibma 	if (h->unit)
12509d308a8SNick Hibma 		printf(", unit=0x%02x exp=%d", h->unit, h->unit_exponent);
12609d308a8SNick Hibma 	printf("\n");
12709d308a8SNick Hibma }
12809d308a8SNick Hibma 
12909d308a8SNick Hibma void
13009d308a8SNick Hibma dumpitems(report_desc_t r)
13109d308a8SNick Hibma {
13209d308a8SNick Hibma 	struct hid_data *d;
13309d308a8SNick Hibma 	struct hid_item h;
1341d10fe50SNick Hibma 	int size;
13509d308a8SNick Hibma 
136cf0e07e5SMatthew N. Dodd 	for (d = hid_start_parse(r, ~0, reportid); hid_get_item(d, &h); ) {
13709d308a8SNick Hibma 		switch (h.kind) {
13809d308a8SNick Hibma 		case hid_collection:
13909d308a8SNick Hibma 			printf("Collection page=%s usage=%s\n",
14009d308a8SNick Hibma 			       hid_usage_page(HID_PAGE(h.usage)),
14109d308a8SNick Hibma 			       hid_usage_in_page(h.usage));
14209d308a8SNick Hibma 			break;
14309d308a8SNick Hibma 		case hid_endcollection:
14409d308a8SNick Hibma 			printf("End collection\n");
14509d308a8SNick Hibma 			break;
14609d308a8SNick Hibma 		case hid_input:
14709d308a8SNick Hibma 			dumpitem("Input  ", &h);
14809d308a8SNick Hibma 			break;
14909d308a8SNick Hibma 		case hid_output:
15009d308a8SNick Hibma 			dumpitem("Output ", &h);
15109d308a8SNick Hibma 			break;
15209d308a8SNick Hibma 		case hid_feature:
15309d308a8SNick Hibma 			dumpitem("Feature", &h);
15409d308a8SNick Hibma 			break;
15509d308a8SNick Hibma 		}
15609d308a8SNick Hibma 	}
15709d308a8SNick Hibma 	hid_end_parse(d);
1581d10fe50SNick Hibma 	size = hid_report_size(r, 0, hid_input);
1591a2cdef4SNick Hibma 	printf("Total   input size %d bytes\n", size);
16009d308a8SNick Hibma 
1611d10fe50SNick Hibma 	size = hid_report_size(r, 0, hid_output);
1621d10fe50SNick Hibma 	printf("Total  output size %d bytes\n", size);
16309d308a8SNick Hibma 
1641d10fe50SNick Hibma 	size = hid_report_size(r, 0, hid_feature);
1651d10fe50SNick Hibma 	printf("Total feature size %d bytes\n", size);
16609d308a8SNick Hibma }
16709d308a8SNick Hibma 
16809d308a8SNick Hibma void
16909d308a8SNick Hibma rev(struct hid_item **p)
17009d308a8SNick Hibma {
17109d308a8SNick Hibma 	struct hid_item *cur, *prev, *next;
17209d308a8SNick Hibma 
17309d308a8SNick Hibma 	prev = 0;
17409d308a8SNick Hibma 	cur = *p;
17509d308a8SNick Hibma 	while(cur != 0) {
17609d308a8SNick Hibma 		next = cur->next;
17709d308a8SNick Hibma 		cur->next = prev;
17809d308a8SNick Hibma 		prev = cur;
17909d308a8SNick Hibma 		cur = next;
18009d308a8SNick Hibma 	}
18109d308a8SNick Hibma 	*p = prev;
18209d308a8SNick Hibma }
18309d308a8SNick Hibma 
18409d308a8SNick Hibma void
18509d308a8SNick Hibma prdata(u_char *buf, struct hid_item *h)
18609d308a8SNick Hibma {
18709d308a8SNick Hibma 	u_int data;
18809d308a8SNick Hibma 	int i, pos;
18909d308a8SNick Hibma 
19009d308a8SNick Hibma 	pos = h->pos;
19109d308a8SNick Hibma 	for (i = 0; i < h->report_count; i++) {
19209d308a8SNick Hibma 		data = hid_get_data(buf, h);
19309d308a8SNick Hibma 		if (h->logical_minimum < 0)
19409d308a8SNick Hibma 			printf("%d", (int)data);
19509d308a8SNick Hibma 		else
19609d308a8SNick Hibma 			printf("%u", data);
1979a2a66f1SGreg Lehey                 if (hexdump)
1989a2a66f1SGreg Lehey 			printf(" [0x%x]", data);
19909d308a8SNick Hibma 		pos += h->report_size;
20009d308a8SNick Hibma 	}
20109d308a8SNick Hibma }
20209d308a8SNick Hibma 
20309d308a8SNick Hibma void
20409d308a8SNick Hibma dumpdata(int f, report_desc_t rd, int loop)
20509d308a8SNick Hibma {
20609d308a8SNick Hibma 	struct hid_data *d;
20709d308a8SNick Hibma 	struct hid_item h, *hids, *n;
20809d308a8SNick Hibma 	int r, dlen;
20909d308a8SNick Hibma 	u_char *dbuf;
21009d308a8SNick Hibma 	static int one = 1;
21109d308a8SNick Hibma 	u_int32_t colls[100];
21209d308a8SNick Hibma 	int sp = 0;
21309d308a8SNick Hibma 	char namebuf[10000], *namep;
21409d308a8SNick Hibma 
21509d308a8SNick Hibma 	hids = 0;
216cf0e07e5SMatthew N. Dodd 	for (d = hid_start_parse(rd, 1<<hid_input, reportid);
21709d308a8SNick Hibma 	     hid_get_item(d, &h); ) {
21809d308a8SNick Hibma 		if (h.kind == hid_collection)
21909d308a8SNick Hibma 			colls[++sp] = h.usage;
22009d308a8SNick Hibma 		else if (h.kind == hid_endcollection)
22109d308a8SNick Hibma 			--sp;
22209d308a8SNick Hibma 		if (h.kind != hid_input || (h.flags & HIO_CONST))
22309d308a8SNick Hibma 			continue;
22409d308a8SNick Hibma 		h.next = hids;
22509d308a8SNick Hibma 		h.collection = colls[sp];
22609d308a8SNick Hibma 		hids = malloc(sizeof *hids);
22709d308a8SNick Hibma 		*hids = h;
22809d308a8SNick Hibma 	}
22909d308a8SNick Hibma 	hid_end_parse(d);
23009d308a8SNick Hibma 	rev(&hids);
2311d10fe50SNick Hibma 	dlen = hid_report_size(rd, 0, hid_input);
23209d308a8SNick Hibma 	dbuf = malloc(dlen);
23309d308a8SNick Hibma 	if (!loop)
23409d308a8SNick Hibma 		if (ioctl(f, USB_SET_IMMED, &one) < 0) {
23509d308a8SNick Hibma 			if (errno == EOPNOTSUPP)
23609d308a8SNick Hibma 				warnx("device does not support immediate mode, only changes reported.");
23709d308a8SNick Hibma 			else
23809d308a8SNick Hibma 				err(1, "USB_SET_IMMED");
23909d308a8SNick Hibma 		}
24009d308a8SNick Hibma 	do {
24109d308a8SNick Hibma 		r = read(f, dbuf, dlen);
24209d308a8SNick Hibma 		if (r != dlen) {
24309d308a8SNick Hibma 			err(1, "bad read %d != %d", r, dlen);
24409d308a8SNick Hibma 		}
24509d308a8SNick Hibma 		for (n = hids; n; n = n->next) {
24609d308a8SNick Hibma 			namep = namebuf;
24709d308a8SNick Hibma 			namep += sprintf(namep, "%s:%s.",
24809d308a8SNick Hibma 					 hid_usage_page(HID_PAGE(n->collection)),
24909d308a8SNick Hibma 					 hid_usage_in_page(n->collection));
25009d308a8SNick Hibma 			namep += sprintf(namep, "%s:%s",
25109d308a8SNick Hibma 					 hid_usage_page(HID_PAGE(n->usage)),
25209d308a8SNick Hibma 					 hid_usage_in_page(n->usage));
25309d308a8SNick Hibma 			if (all || gotname(namebuf)) {
25409d308a8SNick Hibma 				if (!noname)
25509d308a8SNick Hibma 					printf("%s=", namebuf);
256caf357a2SMatthew N. Dodd 				prdata(dbuf + (reportid != 0), n);
25709d308a8SNick Hibma 				printf("\n");
25809d308a8SNick Hibma 			}
25909d308a8SNick Hibma 		}
26009d308a8SNick Hibma 		if (loop)
26109d308a8SNick Hibma 			printf("\n");
26209d308a8SNick Hibma 	} while (loop);
26309d308a8SNick Hibma 	free(dbuf);
26409d308a8SNick Hibma }
26509d308a8SNick Hibma 
26609d308a8SNick Hibma int
26709d308a8SNick Hibma main(int argc, char **argv)
26809d308a8SNick Hibma {
26909d308a8SNick Hibma 	int f;
27009d308a8SNick Hibma 	report_desc_t r;
2711a27f4f2SMark Murray 	char devnam[100], *dev = 0;
27209d308a8SNick Hibma 	int ch;
27309d308a8SNick Hibma 	int repdump = 0;
27409d308a8SNick Hibma 	int loop = 0;
27509d308a8SNick Hibma 	char *table = 0;
27609d308a8SNick Hibma 
2779a2a66f1SGreg Lehey 	while ((ch = getopt(argc, argv, "af:lnrt:vx")) != -1) {
27809d308a8SNick Hibma 		switch(ch) {
27909d308a8SNick Hibma 		case 'a':
28009d308a8SNick Hibma 			all++;
28109d308a8SNick Hibma 			break;
28209d308a8SNick Hibma 		case 'f':
28309d308a8SNick Hibma 			dev = optarg;
28409d308a8SNick Hibma 			break;
28509d308a8SNick Hibma 		case 'l':
28609d308a8SNick Hibma 			loop ^= 1;
28709d308a8SNick Hibma 			break;
28809d308a8SNick Hibma 		case 'n':
28909d308a8SNick Hibma 			noname++;
29009d308a8SNick Hibma 			break;
29109d308a8SNick Hibma 		case 'r':
29209d308a8SNick Hibma 			repdump++;
29309d308a8SNick Hibma 			break;
29409d308a8SNick Hibma 		case 't':
29509d308a8SNick Hibma 			table = optarg;
29609d308a8SNick Hibma 			break;
29709d308a8SNick Hibma 		case 'v':
29809d308a8SNick Hibma 			verbose++;
29909d308a8SNick Hibma 			break;
3009a2a66f1SGreg Lehey 		case 'x':
3019a2a66f1SGreg Lehey 			hexdump ^= 1;
3029a2a66f1SGreg Lehey 			break;
30309d308a8SNick Hibma 		case '?':
30409d308a8SNick Hibma 		default:
30509d308a8SNick Hibma 			usage();
30609d308a8SNick Hibma 		}
30709d308a8SNick Hibma 	}
30809d308a8SNick Hibma 	argc -= optind;
30909d308a8SNick Hibma 	argv += optind;
31009d308a8SNick Hibma 	if (dev == 0)
31109d308a8SNick Hibma 		usage();
31209d308a8SNick Hibma 	names = argv;
31309d308a8SNick Hibma 	nnames = argc;
31409d308a8SNick Hibma 
31509d308a8SNick Hibma 	if (nnames == 0 && !all && !repdump)
31609d308a8SNick Hibma 		usage();
31709d308a8SNick Hibma 
31809d308a8SNick Hibma 	if (dev[0] != '/') {
31909d308a8SNick Hibma 		if (isdigit(dev[0]))
3201a27f4f2SMark Murray 			snprintf(devnam, sizeof(devnam), "/dev/uhid%s", dev);
32109d308a8SNick Hibma 		else
3221a27f4f2SMark Murray 			snprintf(devnam, sizeof(devnam), "/dev/%s", dev);
3231a27f4f2SMark Murray 		dev = devnam;
32409d308a8SNick Hibma 	}
32509d308a8SNick Hibma 
32609d308a8SNick Hibma 	hid_init(table);
32709d308a8SNick Hibma 
32809d308a8SNick Hibma 	f = open(dev, O_RDWR);
32909d308a8SNick Hibma 	if (f < 0)
33009d308a8SNick Hibma 		err(1, "%s", dev);
33109d308a8SNick Hibma 
33209d308a8SNick Hibma 	r = hid_get_report_desc(f);
33309d308a8SNick Hibma 	if (r == 0)
33409d308a8SNick Hibma 		errx(1, "USB_GET_REPORT_DESC");
33509d308a8SNick Hibma 
33609d308a8SNick Hibma 	if (repdump) {
33709d308a8SNick Hibma 		printf("Report descriptor:\n");
33809d308a8SNick Hibma 		dumpitems(r);
33909d308a8SNick Hibma 	}
34009d308a8SNick Hibma 	if (nnames != 0 || all)
34109d308a8SNick Hibma 		dumpdata(f, r, loop);
34209d308a8SNick Hibma 
34309d308a8SNick Hibma 	hid_dispose_report_desc(r);
34409d308a8SNick Hibma 	exit(0);
34509d308a8SNick Hibma }
346