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