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