1 /* $NetBSD: usbhid.c,v 1.14 2000/07/03 02:51:37 matt Exp $ */
5 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
7 * Copyright (c) 1998 The NetBSD Foundation, Inc.
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Lennart Augustsson (augustss@netbsd.org).
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/types.h>
45 #include <dev/usb/usbhid.h>
47 static struct variable {
52 struct variable *next;
55 static int verbose = 0;
56 static int noname = 0;
57 static int hexdump = 0;
61 static void usage(void);
62 static void dumpitem(const char *label, struct hid_item *h);
63 static void dumpitems(report_desc_t r);
64 static void prdata(u_char *buf, struct hid_item *h);
65 static void dumpdata(int f, report_desc_t r, int loop);
66 static void writedata(int f, report_desc_t r);
69 parceargs(report_desc_t r, int all, int nnames, char **names)
74 char hname[1000], *tmp1, *tmp2;
75 struct variable *var, **pnext;
76 int i, instance, cp, t;
81 errx(1, "Must not specify -w to read variables");
83 for (d = hid_start_parse(r,
84 1<<hid_input | 1<<hid_output | 1<<hid_feature, -1);
85 hid_get_item(d, &h); ) {
86 if (h.kind == hid_collection) {
87 cp += sprintf(&colls[cp], "%s%s:%s",
89 hid_usage_page(HID_PAGE(h.usage)),
90 hid_usage_in_page(h.usage));
91 } else if (h.kind == hid_endcollection) {
92 tmp1 = strrchr(colls, '.');
101 if ((h.kind != hid_input && h.kind != hid_output &&
102 h.kind != hid_feature) || (h.flags & HIO_CONST))
104 var = malloc(sizeof(*var));
105 memset(var, 0, sizeof(*var));
106 asprintf(&var->name, "%s%s%s:%s",
107 colls, colls[0] != 0 ? "." : "",
108 hid_usage_page(HID_PAGE(h.usage)),
109 hid_usage_in_page(h.usage));
117 for (i = 0; i < nnames; i++) {
118 var = malloc(sizeof(*var));
119 memset(var, 0, sizeof(*var));
120 tmp1 = tmp2 = strdup(names[i]);
122 var->name = strsep(&tmp1, "#");
124 var->instance = atoi(tmp1);
127 errx(1, "Must specify -w to write variables");
128 var->val = atoi(tmp2);
131 errx(1, "Must not specify -w to read variables");
137 for (d = hid_start_parse(r,
138 1<<hid_input | 1<<hid_output | 1<<hid_feature, -1);
139 hid_get_item(d, &h); ) {
140 if (h.kind == hid_collection) {
141 cp += sprintf(&colls[cp], "%s%s:%s",
143 hid_usage_page(HID_PAGE(h.usage)),
144 hid_usage_in_page(h.usage));
145 } else if (h.kind == hid_endcollection) {
146 tmp1 = strrchr(colls, '.');
155 if ((h.kind != hid_input && h.kind != hid_output &&
156 h.kind != hid_feature) || (h.flags & HIO_CONST))
158 snprintf(hname, sizeof(hname), "%s%s%s:%s",
159 colls, colls[0] != 0 ? "." : "",
160 hid_usage_page(HID_PAGE(h.usage)),
161 hid_usage_in_page(h.usage));
162 t = strlen(hname) - strlen(var->name);
164 if (strcmp(hname + t, var->name) != 0)
166 if (hname[t - 1] != '.')
168 } else if (strcmp(hname, var->name) != 0)
170 if (var->instance != instance++)
176 if (var->h.usage == 0)
177 errx(1, "Unknown item '%s'", var->name);
186 "usage: %s -f device "
187 "[-l] [-n] [-r] [-t tablefile] [-v] [-x] [-z] name ...\n",
191 "[-l] [-n] [-r] [-t tablefile] [-v] [-x] [-z] -a\n",
195 "[-t tablefile] [-v] [-z] -w name=value\n",
201 dumpitem(const char *label, struct hid_item *h)
203 if ((h->flags & HIO_CONST) && !verbose)
205 printf("%s rid=%d size=%d count=%d page=%s usage=%s%s%s", label,
206 h->report_ID, h->report_size, h->report_count,
207 hid_usage_page(HID_PAGE(h->usage)),
208 hid_usage_in_page(h->usage),
209 h->flags & HIO_CONST ? " Const" : "",
210 h->flags & HIO_VARIABLE ? "" : " Array");
211 printf(", logical range %d..%d",
212 h->logical_minimum, h->logical_maximum);
213 if (h->physical_minimum != h->physical_maximum)
214 printf(", physical range %d..%d",
215 h->physical_minimum, h->physical_maximum);
217 printf(", unit=0x%02x exp=%d", h->unit, h->unit_exponent);
222 hid_collection_type(int32_t type)
227 case 0: return ("Physical");
228 case 1: return ("Application");
229 case 2: return ("Logical");
230 case 3: return ("Report");
231 case 4: return ("Named_Array");
232 case 5: return ("Usage_Switch");
233 case 6: return ("Usage_Modifier");
235 snprintf(num, sizeof(num), "0x%02x", type);
240 dumpitems(report_desc_t r)
246 for (d = hid_start_parse(r, ~0, -1); hid_get_item(d, &h); ) {
249 printf("Collection type=%s page=%s usage=%s\n",
250 hid_collection_type(h.collection),
251 hid_usage_page(HID_PAGE(h.usage)),
252 hid_usage_in_page(h.usage));
254 case hid_endcollection:
255 printf("End collection\n");
258 dumpitem("Input ", &h);
261 dumpitem("Output ", &h);
264 dumpitem("Feature", &h);
269 size = hid_report_size(r, hid_input, -1);
270 printf("Total input size %d bytes\n", size);
272 size = hid_report_size(r, hid_output, -1);
273 printf("Total output size %d bytes\n", size);
275 size = hid_report_size(r, hid_feature, -1);
276 printf("Total feature size %d bytes\n", size);
280 prdata(u_char *buf, struct hid_item *h)
286 for (i = 0; i < h->report_count; i++) {
287 data = hid_get_data(buf, h);
290 if (h->logical_minimum < 0)
291 printf("%d", (int)data);
295 printf(" [0x%x]", data);
296 h->pos += h->report_size;
302 dumpdata(int f, report_desc_t rd, int loop)
304 struct variable *var;
305 int dlen, havedata, i, match, r, rid, use_rid;
309 kind = zflag ? 3 : 0;
311 use_rid = !!hid_get_report_id(f);
320 for (var = vars; var; var = var->next) {
321 if (rid == var->h.report_ID &&
328 dlen = hid_report_size(rd, kind < 3 ? kind : hid_input, rid);
332 memset(dbuf, 0, dlen);
335 r = hid_get_report(f, kind, dbuf, dlen);
337 warn("hid_get_report(rid %d)", rid);
338 havedata = !r && (rid == 0 || dbuf[0] == rid);
342 r = read(f, dbuf, dlen);
344 err(1, "read error");
348 printf("Got %s report %d (%d bytes):",
349 kind == hid_output ? "output" :
350 kind == hid_feature ? "feature" : "input",
351 use_rid ? dbuf[0] : 0, dlen);
353 for (i = 0; i < dlen; i++)
354 printf(" %02x", dbuf[i]);
359 for (var = vars; var; var = var->next) {
360 if ((kind < 3 ? kind : hid_input) != var->h.kind)
362 if (var->h.report_ID != 0 &&
363 dbuf[0] != var->h.report_ID)
367 printf("%s=", var->name);
369 prdata(dbuf, &var->h);
375 } while (loop || kind < 3);
379 writedata(int f, report_desc_t rd)
381 struct variable *var;
388 for (kind = 0; kind < 3; kind ++) {
389 for (rid = 0; rid < 256; rid ++) {
390 for (var = vars; var; var = var->next) {
391 if (rid == var->h.report_ID && kind == var->h.kind)
396 dlen = hid_report_size(rd, kind, rid);
400 memset(dbuf, 0, dlen);
402 if (!zflag && hid_get_report(f, kind, dbuf, dlen) == 0) {
404 printf("Got %s report %d (%d bytes):",
405 kind == hid_input ? "input" :
406 kind == hid_output ? "output" : "feature",
408 for (i = 0; i < dlen; i++)
409 printf(" %02x", dbuf[i]);
413 warn("hid_get_report(rid %d)", rid);
415 printf("Can't get %s report %d (%d bytes). "
416 "Will be initialized with zeros.\n",
417 kind == hid_input ? "input" :
418 kind == hid_output ? "output" : "feature",
422 for (var = vars; var; var = var->next) {
423 if (rid != var->h.report_ID || kind != var->h.kind)
425 hid_set_data(dbuf, &var->h, var->val);
428 printf("Setting %s report %d (%d bytes):",
429 kind == hid_output ? "output" :
430 kind == hid_feature ? "feature" : "input",
432 for (i = 0; i < dlen; i++)
433 printf(" %02x", dbuf[i]);
436 r = hid_set_report(f, kind, dbuf, dlen);
438 warn("hid_set_report(rid %d)", rid);
445 main(int argc, char **argv)
449 char devnam[100], *dev = NULL;
456 while ((ch = getopt(argc, argv, "af:lnrt:vwxz")) != -1) {
498 if (argc == 0 && !all && !repdump)
503 snprintf(devnam, sizeof(devnam), "/dev/uhid%s", dev);
505 snprintf(devnam, sizeof(devnam), "/dev/%s", dev);
511 f = open(dev, O_RDWR);
515 r = hid_get_report_desc(f);
517 errx(1, "USB_GET_REPORT_DESC");
520 printf("Report descriptor:\n");
523 if (argc != 0 || all) {
524 parceargs(r, all, argc, argv);
528 dumpdata(f, r, loop);
531 hid_dispose_report_desc(r);