1 /* $NetBSD: usbhid.c,v 1.14 2000/07/03 02:51:37 matt Exp $ */
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (augustss@netbsd.org).
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
43 #include <sys/types.h>
50 #include <dev/usb/usbhid.h>
57 struct variable *next;
66 static void usage(void);
67 static void dumpitem(const char *label, struct hid_item *h);
68 static void dumpitems(report_desc_t r);
69 static void prdata(u_char *buf, struct hid_item *h);
70 static void dumpdata(int f, report_desc_t r, int loop);
71 static void writedata(int f, report_desc_t r);
74 parceargs(report_desc_t r, int all, int nnames, char **names)
79 char hname[1000], *tmp1, *tmp2;
80 struct variable *var, **pnext;
81 int i, instance, cp, t;
86 errx(1, "Must not specify -w to read variables");
88 for (d = hid_start_parse(r,
89 1<<hid_input | 1<<hid_output | 1<<hid_feature, -1);
90 hid_get_item(d, &h); ) {
91 if (h.kind == hid_collection) {
92 cp += sprintf(&colls[cp], "%s%s:%s",
94 hid_usage_page(HID_PAGE(h.usage)),
95 hid_usage_in_page(h.usage));
96 } else if (h.kind == hid_endcollection) {
97 tmp1 = strrchr(colls, '.');
106 if ((h.kind != hid_input && h.kind != hid_output &&
107 h.kind != hid_feature) || (h.flags & HIO_CONST))
109 var = malloc(sizeof(*var));
110 memset(var, 0, sizeof(*var));
111 asprintf(&var->name, "%s%s%s:%s",
112 colls, colls[0] != 0 ? "." : "",
113 hid_usage_page(HID_PAGE(h.usage)),
114 hid_usage_in_page(h.usage));
122 for (i = 0; i < nnames; i++) {
123 var = malloc(sizeof(*var));
124 memset(var, 0, sizeof(*var));
125 tmp1 = tmp2 = strdup(names[i]);
127 var->name = strsep(&tmp1, "#");
129 var->instance = atoi(tmp1);
132 errx(1, "Must specify -w to write variables");
133 var->val = atoi(tmp2);
136 errx(1, "Must not specify -w to read variables");
142 for (d = hid_start_parse(r,
143 1<<hid_input | 1<<hid_output | 1<<hid_feature, -1);
144 hid_get_item(d, &h); ) {
145 if (h.kind == hid_collection) {
146 cp += sprintf(&colls[cp], "%s%s:%s",
148 hid_usage_page(HID_PAGE(h.usage)),
149 hid_usage_in_page(h.usage));
150 } else if (h.kind == hid_endcollection) {
151 tmp1 = strrchr(colls, '.');
160 if ((h.kind != hid_input && h.kind != hid_output &&
161 h.kind != hid_feature) || (h.flags & HIO_CONST))
163 snprintf(hname, sizeof(hname), "%s%s%s:%s",
164 colls, colls[0] != 0 ? "." : "",
165 hid_usage_page(HID_PAGE(h.usage)),
166 hid_usage_in_page(h.usage));
167 t = strlen(hname) - strlen(var->name);
169 if (strcmp(hname + t, var->name) != 0)
171 if (hname[t - 1] != '.')
173 } else if (strcmp(hname, var->name) != 0)
175 if (var->instance != instance++)
181 if (var->h.usage == 0)
182 errx(1, "Unknown item '%s'", var->name);
191 "usage: %s -f device "
192 "[-l] [-n] [-r] [-t tablefile] [-v] [-x] [-z] name ...\n",
196 "[-l] [-n] [-r] [-t tablefile] [-v] [-x] [-z] -a\n",
200 "[-t tablefile] [-v] [-z] -w name=value\n",
206 dumpitem(const char *label, struct hid_item *h)
208 if ((h->flags & HIO_CONST) && !verbose)
210 printf("%s rid=%d size=%d count=%d page=%s usage=%s%s%s", label,
211 h->report_ID, h->report_size, h->report_count,
212 hid_usage_page(HID_PAGE(h->usage)),
213 hid_usage_in_page(h->usage),
214 h->flags & HIO_CONST ? " Const" : "",
215 h->flags & HIO_VARIABLE ? "" : " Array");
216 printf(", logical range %d..%d",
217 h->logical_minimum, h->logical_maximum);
218 if (h->physical_minimum != h->physical_maximum)
219 printf(", physical range %d..%d",
220 h->physical_minimum, h->physical_maximum);
222 printf(", unit=0x%02x exp=%d", h->unit, h->unit_exponent);
227 hid_collection_type(int32_t type)
232 case 0: return ("Physical");
233 case 1: return ("Application");
234 case 2: return ("Logical");
235 case 3: return ("Report");
236 case 4: return ("Named_Array");
237 case 5: return ("Usage_Switch");
238 case 6: return ("Usage_Modifier");
240 snprintf(num, sizeof(num), "0x%02x", type);
245 dumpitems(report_desc_t r)
251 for (d = hid_start_parse(r, ~0, -1); hid_get_item(d, &h); ) {
254 printf("Collection type=%s page=%s usage=%s\n",
255 hid_collection_type(h.collection),
256 hid_usage_page(HID_PAGE(h.usage)),
257 hid_usage_in_page(h.usage));
259 case hid_endcollection:
260 printf("End collection\n");
263 dumpitem("Input ", &h);
266 dumpitem("Output ", &h);
269 dumpitem("Feature", &h);
274 size = hid_report_size(r, hid_input, -1);
275 printf("Total input size %d bytes\n", size);
277 size = hid_report_size(r, hid_output, -1);
278 printf("Total output size %d bytes\n", size);
280 size = hid_report_size(r, hid_feature, -1);
281 printf("Total feature size %d bytes\n", size);
285 prdata(u_char *buf, struct hid_item *h)
291 for (i = 0; i < h->report_count; i++) {
292 data = hid_get_data(buf, h);
295 if (h->logical_minimum < 0)
296 printf("%d", (int)data);
300 printf(" [0x%x]", data);
301 h->pos += h->report_size;
307 dumpdata(int f, report_desc_t rd, int loop)
309 struct variable *var;
310 int dlen, havedata, i, match, r, rid, use_rid;
314 kind = zflag ? 3 : 0;
316 use_rid = !!hid_get_report_id(f);
325 for (var = vars; var; var = var->next) {
326 if (rid == var->h.report_ID &&
333 dlen = hid_report_size(rd, kind < 3 ? kind : hid_input, rid);
337 memset(dbuf, 0, dlen);
340 r = hid_get_report(f, kind, dbuf, dlen);
342 warn("hid_get_report(rid %d)", rid);
343 havedata = !r && (rid == 0 || dbuf[0] == rid);
347 r = read(f, dbuf, dlen);
349 err(1, "read error");
353 printf("Got %s report %d (%d bytes):",
354 kind == hid_output ? "output" :
355 kind == hid_feature ? "feature" : "input",
356 use_rid ? dbuf[0] : 0, dlen);
358 for (i = 0; i < dlen; i++)
359 printf(" %02x", dbuf[i]);
364 for (var = vars; var; var = var->next) {
365 if ((kind < 3 ? kind : hid_input) != var->h.kind)
367 if (var->h.report_ID != 0 &&
368 dbuf[0] != var->h.report_ID)
372 printf("%s=", var->name);
374 prdata(dbuf, &var->h);
380 } while (loop || kind < 3);
384 writedata(int f, report_desc_t rd)
386 struct variable *var;
393 for (kind = 0; kind < 3; kind ++) {
394 for (rid = 0; rid < 256; rid ++) {
395 for (var = vars; var; var = var->next) {
396 if (rid == var->h.report_ID && kind == var->h.kind)
401 dlen = hid_report_size(rd, kind, rid);
405 memset(dbuf, 0, dlen);
407 if (!zflag && hid_get_report(f, kind, dbuf, dlen) == 0) {
409 printf("Got %s report %d (%d bytes):",
410 kind == hid_input ? "input" :
411 kind == hid_output ? "output" : "feature",
413 for (i = 0; i < dlen; i++)
414 printf(" %02x", dbuf[i]);
418 warn("hid_get_report(rid %d)", rid);
420 printf("Can't get %s report %d (%d bytes). "
421 "Will be initialized with zeros.\n",
422 kind == hid_input ? "input" :
423 kind == hid_output ? "output" : "feature",
427 for (var = vars; var; var = var->next) {
428 if (rid != var->h.report_ID || kind != var->h.kind)
430 hid_set_data(dbuf, &var->h, var->val);
433 printf("Setting %s report %d (%d bytes):",
434 kind == hid_output ? "output" :
435 kind == hid_feature ? "feature" : "input",
437 for (i = 0; i < dlen; i++)
438 printf(" %02x", dbuf[i]);
441 r = hid_set_report(f, kind, dbuf, dlen);
443 warn("hid_set_report(rid %d)", rid);
450 main(int argc, char **argv)
454 char devnam[100], *dev = NULL;
461 while ((ch = getopt(argc, argv, "af:lnrt:vwxz")) != -1) {
503 if (argc == 0 && !all && !repdump)
508 snprintf(devnam, sizeof(devnam), "/dev/uhid%s", dev);
510 snprintf(devnam, sizeof(devnam), "/dev/%s", dev);
516 f = open(dev, O_RDWR);
520 r = hid_get_report_desc(f);
522 errx(1, "USB_GET_REPORT_DESC");
525 printf("Report descriptor:\n");
528 if (argc != 0 || all) {
529 parceargs(r, all, argc, argv);
533 dumpdata(f, r, loop);
536 hid_dispose_report_desc(r);