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