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