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