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