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