Home | History | Annotate | Line # | Download | only in usbhidctl
usbhid.c revision 1.8
      1  1.8  augustss /*	$NetBSD: usbhid.c,v 1.8 1999/04/21 17:41:08 augustss Exp $	*/
      2  1.1  augustss 
      3  1.1  augustss /*
      4  1.1  augustss  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  1.1  augustss  * All rights reserved.
      6  1.1  augustss  *
      7  1.5  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8  1.5  augustss  * by Lennart Augustsson (augustss (at) netbsd.org).
      9  1.1  augustss  *
     10  1.1  augustss  * Redistribution and use in source and binary forms, with or without
     11  1.1  augustss  * modification, are permitted provided that the following conditions
     12  1.1  augustss  * are met:
     13  1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     14  1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     15  1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  augustss  *    documentation and/or other materials provided with the distribution.
     18  1.1  augustss  * 3. All advertising materials mentioning features or use of this software
     19  1.1  augustss  *    must display the following acknowledgement:
     20  1.1  augustss  *        This product includes software developed by the NetBSD
     21  1.1  augustss  *        Foundation, Inc. and its contributors.
     22  1.1  augustss  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  augustss  *    contributors may be used to endorse or promote products derived
     24  1.1  augustss  *    from this software without specific prior written permission.
     25  1.1  augustss  *
     26  1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  augustss  */
     38  1.1  augustss 
     39  1.1  augustss #include <stdio.h>
     40  1.1  augustss #include <stdlib.h>
     41  1.1  augustss #include <sys/types.h>
     42  1.1  augustss #include <fcntl.h>
     43  1.1  augustss #include <unistd.h>
     44  1.1  augustss #include <err.h>
     45  1.4  augustss #include <ctype.h>
     46  1.1  augustss #include <dev/usb/usb.h>
     47  1.1  augustss #include <dev/usb/usbhid.h>
     48  1.6  augustss #include <errno.h>
     49  1.1  augustss 
     50  1.1  augustss #include "hidsubr.h"
     51  1.1  augustss 
     52  1.1  augustss #define HIDTABLE "/usr/share/misc/usb_hid_usages"
     53  1.1  augustss 
     54  1.1  augustss #define USBDEV "/dev/uhid0"
     55  1.1  augustss 
     56  1.1  augustss int verbose = 0;
     57  1.3  augustss int all = 0;
     58  1.3  augustss int noname = 0;
     59  1.3  augustss 
     60  1.3  augustss char **names;
     61  1.3  augustss int nnames;
     62  1.1  augustss 
     63  1.1  augustss void prbits(int bits, char **strs, int n);
     64  1.1  augustss void usage(void);
     65  1.8  augustss void dumpitem(char *label, struct hid_item *h);
     66  1.1  augustss void dumpitems(u_char *buf, int len);
     67  1.1  augustss void rev(struct hid_item **p);
     68  1.1  augustss u_long getdata(u_char *buf, int hpos, int hsize, int sign);
     69  1.1  augustss void prdata(u_char *buf, struct hid_item *h);
     70  1.1  augustss void dumpdata(int f, u_char *buf, int len, int loop);
     71  1.3  augustss int gotname(char *n);
     72  1.3  augustss 
     73  1.3  augustss int
     74  1.3  augustss gotname(char *n)
     75  1.3  augustss {
     76  1.3  augustss 	int i;
     77  1.3  augustss 
     78  1.3  augustss 	for (i = 0; i < nnames; i++)
     79  1.3  augustss 		if (strcmp(names[i], n) == 0)
     80  1.3  augustss 			return 1;
     81  1.3  augustss 	return 0;
     82  1.3  augustss }
     83  1.1  augustss 
     84  1.1  augustss void
     85  1.1  augustss prbits(int bits, char **strs, int n)
     86  1.1  augustss {
     87  1.1  augustss 	int i;
     88  1.1  augustss 
     89  1.1  augustss 	for(i = 0; i < n; i++, bits >>= 1)
     90  1.1  augustss 		if (strs[i*2])
     91  1.1  augustss 			printf("%s%s", i == 0 ? "" : ", ", strs[i*2 + (bits&1)]);
     92  1.1  augustss }
     93  1.1  augustss 
     94  1.1  augustss void
     95  1.1  augustss usage(void)
     96  1.1  augustss {
     97  1.1  augustss 	extern char *__progname;
     98  1.1  augustss 
     99  1.6  augustss 	fprintf(stderr, "Usage: %s -f device [-l] [-n] [-r] [-t tablefile] [-v] name ...\n", __progname);
    100  1.6  augustss 	fprintf(stderr, "       %s -f device [-l] [-n] [-r] [-t tablefile] [-v] -a\n", __progname);
    101  1.1  augustss 	exit(1);
    102  1.1  augustss }
    103  1.1  augustss 
    104  1.1  augustss void
    105  1.8  augustss dumpitem(char *label, struct hid_item *h)
    106  1.8  augustss {
    107  1.8  augustss 	if ((h->flags & HIO_CONST) && !verbose)
    108  1.8  augustss 		return;
    109  1.8  augustss 	printf("%s size=%d count=%d page=%s usage=%s%s", label,
    110  1.8  augustss 	       h->report_size, h->report_count,
    111  1.8  augustss 	       usage_page(HID_PAGE(h->usage)),
    112  1.8  augustss 	       usage_in_page(h->usage),
    113  1.8  augustss 	       h->flags & HIO_CONST ? " Const" : "");
    114  1.8  augustss 	printf(", logical range %d..%d",
    115  1.8  augustss 	       h->logical_minimum, h->logical_maximum);
    116  1.8  augustss 	if (h->physical_minimum != h->physical_maximum)
    117  1.8  augustss 		printf(", physical range %d..%d",
    118  1.8  augustss 		       h->physical_minimum, h->physical_maximum);
    119  1.8  augustss 	if (h->unit)
    120  1.8  augustss 		printf(", unit=0x%02x exp=%d", h->unit, h->unit_exponent);
    121  1.8  augustss 	printf("\n");
    122  1.8  augustss }
    123  1.8  augustss 
    124  1.8  augustss void
    125  1.1  augustss dumpitems(u_char *buf, int len)
    126  1.1  augustss {
    127  1.1  augustss 	struct hid_data *d;
    128  1.1  augustss 	struct hid_item h;
    129  1.8  augustss 	int report_id, size;
    130  1.1  augustss 
    131  1.1  augustss 	for (d = hid_start_parse(buf, len, ~0); hid_get_item(d, &h); ) {
    132  1.1  augustss 		switch (h.kind) {
    133  1.1  augustss 		case hid_collection:
    134  1.1  augustss 			printf("Collection page=%s usage=%s\n",
    135  1.1  augustss 			       usage_page(HID_PAGE(h.usage)),
    136  1.1  augustss 			       usage_in_page(h.usage));
    137  1.1  augustss 			break;
    138  1.1  augustss 		case hid_endcollection:
    139  1.1  augustss 			printf("End collection\n");
    140  1.1  augustss 			break;
    141  1.1  augustss 		case hid_input:
    142  1.8  augustss 			dumpitem("Input  ", &h);
    143  1.1  augustss 			break;
    144  1.1  augustss 		case hid_output:
    145  1.8  augustss 			dumpitem("Output ", &h);
    146  1.1  augustss 			break;
    147  1.1  augustss 		case hid_feature:
    148  1.8  augustss 			dumpitem("Feature", &h);
    149  1.1  augustss 			break;
    150  1.1  augustss 		}
    151  1.1  augustss 	}
    152  1.1  augustss 	hid_end_parse(d);
    153  1.8  augustss 	size = hid_report_size(buf, len, hid_input, &report_id);
    154  1.8  augustss 	size -= report_id != 0;
    155  1.8  augustss 	printf("Total   input size %s%d bytes\n",
    156  1.8  augustss 	       report_id && size ? "1+" : "", size);
    157  1.8  augustss 
    158  1.8  augustss 	size = hid_report_size(buf, len, hid_output, &report_id);
    159  1.8  augustss 	size -= report_id != 0;
    160  1.8  augustss 	printf("Total  output size %s%d bytes\n",
    161  1.8  augustss 	       report_id && size ? "1+" : "", size);
    162  1.8  augustss 
    163  1.8  augustss 	size = hid_report_size(buf, len, hid_feature, &report_id);
    164  1.8  augustss 	size -= report_id != 0;
    165  1.8  augustss 	printf("Total feature size %s%d bytes\n",
    166  1.8  augustss 	       report_id && size ? "1+" : "", size);
    167  1.1  augustss }
    168  1.1  augustss 
    169  1.1  augustss void
    170  1.1  augustss rev(struct hid_item **p)
    171  1.1  augustss {
    172  1.1  augustss 	struct hid_item *cur, *prev, *next;
    173  1.1  augustss 
    174  1.1  augustss 	prev = 0;
    175  1.1  augustss 	cur = *p;
    176  1.1  augustss 	while(cur != 0) {
    177  1.1  augustss 		next = cur->next;
    178  1.1  augustss 		cur->next = prev;
    179  1.1  augustss 		prev = cur;
    180  1.1  augustss 		cur = next;
    181  1.1  augustss 	}
    182  1.1  augustss 	*p = prev;
    183  1.1  augustss }
    184  1.1  augustss 
    185  1.1  augustss u_long
    186  1.1  augustss getdata(u_char *buf, int hpos, int hsize, int sign)
    187  1.1  augustss {
    188  1.1  augustss 	u_long data;
    189  1.1  augustss 	int i, size, s;
    190  1.1  augustss 
    191  1.1  augustss 	data = 0;
    192  1.1  augustss 	s = hpos/8;
    193  1.2  augustss 	for (i = hpos; i < hpos+hsize; i += 8)
    194  1.2  augustss 		data |= buf[i / 8] << ((i/8-s) * 8);
    195  1.1  augustss 	data >>= (hpos % 8);
    196  1.1  augustss 	data &= (1 << hsize) - 1;
    197  1.1  augustss 	size = 32 - hsize;
    198  1.2  augustss 	if (sign)
    199  1.1  augustss 		/* Need to sign extend */
    200  1.1  augustss 		data = ((long)data << size) >> size;
    201  1.1  augustss 	return data;
    202  1.1  augustss }
    203  1.1  augustss 
    204  1.1  augustss void
    205  1.1  augustss prdata(u_char *buf, struct hid_item *h)
    206  1.1  augustss {
    207  1.1  augustss 	u_long data;
    208  1.1  augustss 	int i, pos;
    209  1.1  augustss 
    210  1.1  augustss 	pos = h->pos;
    211  1.1  augustss 	for (i = 0; i < h->report_count; i++) {
    212  1.1  augustss 		data = getdata(buf, pos, h->report_size,
    213  1.1  augustss 			       h->logical_minimum < 0);
    214  1.1  augustss 		if (h->logical_minimum < 0)
    215  1.1  augustss 			printf("%ld", (long)data);
    216  1.1  augustss 		else
    217  1.8  augustss 			printf("%lu", data);
    218  1.1  augustss 		pos += h->report_size;
    219  1.1  augustss 	}
    220  1.1  augustss }
    221  1.1  augustss 
    222  1.1  augustss void
    223  1.1  augustss dumpdata(int f, u_char *buf, int len, int loop)
    224  1.1  augustss {
    225  1.1  augustss 	struct hid_data *d;
    226  1.1  augustss 	struct hid_item h, *hids, *n;
    227  1.1  augustss 	int r, dlen;
    228  1.1  augustss 	u_char *dbuf;
    229  1.1  augustss 	static int one = 1;
    230  1.1  augustss 	u_int32_t colls[100];
    231  1.1  augustss 	int sp = 0;
    232  1.7  augustss 	int report_id;
    233  1.3  augustss 	char namebuf[10000], *namep;
    234  1.1  augustss 
    235  1.1  augustss 	hids = 0;
    236  1.1  augustss 	for (d = hid_start_parse(buf, len, 1<<hid_input);
    237  1.1  augustss 	     hid_get_item(d, &h); ) {
    238  1.1  augustss 		if (h.kind == hid_collection)
    239  1.1  augustss 			colls[++sp] = h.usage;
    240  1.1  augustss 		else if (h.kind == hid_endcollection)
    241  1.1  augustss 			--sp;
    242  1.1  augustss 		if (h.kind != hid_input || (h.flags & HIO_CONST))
    243  1.1  augustss 			continue;
    244  1.1  augustss 		h.next = hids;
    245  1.1  augustss 		h.collection = colls[sp];
    246  1.1  augustss 		hids = malloc(sizeof *hids);
    247  1.1  augustss 		*hids = h;
    248  1.1  augustss 	}
    249  1.1  augustss 	hid_end_parse(d);
    250  1.1  augustss 	rev(&hids);
    251  1.7  augustss 	dlen = hid_report_size(buf, len, hid_input, &report_id);
    252  1.1  augustss 	dbuf = malloc(dlen);
    253  1.1  augustss 	if (!loop)
    254  1.6  augustss 		if (ioctl(f, USB_SET_IMMED, &one) < 0) {
    255  1.6  augustss 			if (errno == EOPNOTSUPP)
    256  1.6  augustss 				warnx("device does not support immediate mode, only changes reported.");
    257  1.6  augustss 			else
    258  1.6  augustss 				err(1, "USB_SET_IMMED");
    259  1.6  augustss 		}
    260  1.1  augustss 	do {
    261  1.1  augustss 		r = read(f, dbuf, dlen);
    262  1.1  augustss 		if (r != dlen) {
    263  1.1  augustss 			err(1, "bad read %d != %d", r, dlen);
    264  1.1  augustss 		}
    265  1.1  augustss 		for (n = hids; n; n = n->next) {
    266  1.3  augustss 			namep = namebuf;
    267  1.3  augustss 			namep += sprintf(namep, "%s:%s.",
    268  1.3  augustss 					 usage_page(HID_PAGE(n->collection)),
    269  1.3  augustss 					 usage_in_page(n->collection));
    270  1.3  augustss 			namep += sprintf(namep, "%s:%s",
    271  1.3  augustss 					 usage_page(HID_PAGE(n->usage)),
    272  1.3  augustss 					 usage_in_page(n->usage));
    273  1.3  augustss 			if (all || gotname(namebuf)) {
    274  1.3  augustss 				if (!noname)
    275  1.3  augustss 					printf("%s=", namebuf);
    276  1.7  augustss 				prdata(dbuf + (report_id != 0), n);
    277  1.3  augustss 				printf("\n");
    278  1.3  augustss 			}
    279  1.1  augustss 		}
    280  1.1  augustss 		if (loop)
    281  1.1  augustss 			printf("\n");
    282  1.1  augustss 	} while (loop);
    283  1.1  augustss 	free(dbuf);
    284  1.1  augustss }
    285  1.1  augustss 
    286  1.1  augustss int
    287  1.1  augustss main(int argc, char **argv)
    288  1.1  augustss {
    289  1.1  augustss 	int f, r;
    290  1.4  augustss 	char devname[100], *dev = 0;
    291  1.1  augustss 	int ch;
    292  1.1  augustss 	extern char *optarg;
    293  1.1  augustss 	extern int optind;
    294  1.1  augustss 	struct usb_ctl_report_desc rep;
    295  1.1  augustss 	int repdump = 0;
    296  1.1  augustss 	int loop = 0;
    297  1.1  augustss 	char *table = HIDTABLE;
    298  1.1  augustss 
    299  1.3  augustss 	while ((ch = getopt(argc, argv, "af:lnrt:v")) != -1) {
    300  1.1  augustss 		switch(ch) {
    301  1.3  augustss 		case 'a':
    302  1.3  augustss 			all++;
    303  1.3  augustss 			break;
    304  1.1  augustss 		case 'f':
    305  1.1  augustss 			dev = optarg;
    306  1.1  augustss 			break;
    307  1.1  augustss 		case 'l':
    308  1.1  augustss 			loop ^= 1;
    309  1.1  augustss 			break;
    310  1.1  augustss 		case 'n':
    311  1.3  augustss 			noname++;
    312  1.1  augustss 			break;
    313  1.1  augustss 		case 'r':
    314  1.1  augustss 			repdump++;
    315  1.1  augustss 			break;
    316  1.1  augustss 		case 't':
    317  1.1  augustss 			table = optarg;
    318  1.1  augustss 			break;
    319  1.1  augustss 		case 'v':
    320  1.1  augustss 			verbose++;
    321  1.1  augustss 			break;
    322  1.1  augustss 		case '?':
    323  1.1  augustss 		default:
    324  1.1  augustss 			usage();
    325  1.1  augustss 		}
    326  1.1  augustss 	}
    327  1.1  augustss 	argc -= optind;
    328  1.1  augustss 	argv += optind;
    329  1.3  augustss 	if (dev == 0)
    330  1.1  augustss 		usage();
    331  1.3  augustss 	names = argv;
    332  1.3  augustss 	nnames = argc;
    333  1.6  augustss 
    334  1.8  augustss 	if (nnames == 0 && !all && !repdump)
    335  1.6  augustss 		usage();
    336  1.4  augustss 
    337  1.4  augustss 	if (dev[0] != '/') {
    338  1.4  augustss 		if (isdigit(dev[0]))
    339  1.4  augustss 			sprintf(devname, "/dev/uhid%s", dev);
    340  1.4  augustss 		else
    341  1.4  augustss 			sprintf(devname, "/dev/%s", dev);
    342  1.4  augustss 		dev = devname;
    343  1.4  augustss 	}
    344  1.1  augustss 
    345  1.1  augustss 	init_hid(table);
    346  1.1  augustss 
    347  1.1  augustss 	f = open(dev, O_RDWR);
    348  1.1  augustss 	if (f < 0)
    349  1.1  augustss 		err(1, "%s", dev);
    350  1.1  augustss 
    351  1.1  augustss 	rep.size = 0;
    352  1.1  augustss 	r = ioctl(f, USB_GET_REPORT_DESC, &rep);
    353  1.1  augustss 	if (r)
    354  1.1  augustss 		errx(1, "USB_GET_REPORT_DESC");
    355  1.1  augustss 
    356  1.1  augustss 	if (repdump) {
    357  1.1  augustss 		printf("Report descriptor\n");
    358  1.1  augustss 		dumpitems(rep.data, rep.size);
    359  1.1  augustss 	}
    360  1.8  augustss 	if (nnames != 0 || all)
    361  1.8  augustss 		dumpdata(f, rep.data, rep.size, loop);
    362  1.1  augustss 
    363  1.1  augustss 	exit(0);
    364  1.1  augustss }
    365