1 /* $NetBSD: usbhid.c,v 1.14 2000/07/03 02:51:37 matt Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 1998 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Lennart Augustsson (augustss@netbsd.org).
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
37 #include <sys/types.h>
44 #include <dev/usb/usbhid.h>
46 static struct variable {
51 struct variable *next;
54 static int verbose = 0;
55 static int noname = 0;
56 static int hexdump = 0;
60 static void usage(void) __dead2;
61 static void dumpitem(const char *label, struct hid_item *h);
62 static void dumpitems(report_desc_t r);
63 static void prdata(u_char *buf, struct hid_item *h);
64 static void dumpdata(int f, report_desc_t r, int loop);
65 static void writedata(int f, report_desc_t r);
68 parceargs(report_desc_t r, int all, int nnames, char **names)
73 char hname[1000], *tmp1, *tmp2;
74 struct variable *var, **pnext;
75 int i, instance, cp, t;
80 errx(1, "Must not specify -w to read variables");
82 for (d = hid_start_parse(r,
83 1<<hid_input | 1<<hid_output | 1<<hid_feature, -1);
84 hid_get_item(d, &h); ) {
85 if (h.kind == hid_collection) {
86 cp += sprintf(&colls[cp], "%s%s:%s",
88 hid_usage_page(HID_PAGE(h.usage)),
89 hid_usage_in_page(h.usage));
90 } else if (h.kind == hid_endcollection) {
91 tmp1 = strrchr(colls, '.');
100 if ((h.kind != hid_input && h.kind != hid_output &&
101 h.kind != hid_feature) || (h.flags & HIO_CONST))
103 var = malloc(sizeof(*var));
104 memset(var, 0, sizeof(*var));
105 asprintf(&var->name, "%s%s%s:%s",
106 colls, colls[0] != 0 ? "." : "",
107 hid_usage_page(HID_PAGE(h.usage)),
108 hid_usage_in_page(h.usage));
116 for (i = 0; i < nnames; i++) {
117 var = malloc(sizeof(*var));
118 memset(var, 0, sizeof(*var));
119 tmp1 = tmp2 = strdup(names[i]);
121 var->name = strsep(&tmp1, "#");
123 var->instance = atoi(tmp1);
126 errx(1, "Must specify -w to write variables");
127 var->val = atoi(tmp2);
130 errx(1, "Must not specify -w to read variables");
136 for (d = hid_start_parse(r,
137 1<<hid_input | 1<<hid_output | 1<<hid_feature, -1);
138 hid_get_item(d, &h); ) {
139 if (h.kind == hid_collection) {
140 cp += sprintf(&colls[cp], "%s%s:%s",
142 hid_usage_page(HID_PAGE(h.usage)),
143 hid_usage_in_page(h.usage));
144 } else if (h.kind == hid_endcollection) {
145 tmp1 = strrchr(colls, '.');
154 if ((h.kind != hid_input && h.kind != hid_output &&
155 h.kind != hid_feature) || (h.flags & HIO_CONST))
157 snprintf(hname, sizeof(hname), "%s%s%s:%s",
158 colls, colls[0] != 0 ? "." : "",
159 hid_usage_page(HID_PAGE(h.usage)),
160 hid_usage_in_page(h.usage));
161 t = strlen(hname) - strlen(var->name);
163 if (strcmp(hname + t, var->name) != 0)
165 if (hname[t - 1] != '.')
167 } else if (strcmp(hname, var->name) != 0)
169 if (var->instance != instance++)
175 if (var->h.usage == 0)
176 errx(1, "Unknown item '%s'", var->name);
185 "usage: %s -f device "
186 "[-l] [-n] [-r] [-t tablefile] [-v] [-x] [-z] name ...\n",
190 "[-l] [-n] [-r] [-t tablefile] [-v] [-x] [-z] -a\n",
194 "[-t tablefile] [-v] [-z] -w name=value\n",
200 dumpitem(const char *label, struct hid_item *h)
202 if ((h->flags & HIO_CONST) && !verbose)
204 printf("%s rid=%d pos=%d size=%d count=%d page=%s usage=%s%s%s", label,
205 h->report_ID, h->pos, h->report_size, h->report_count,
206 hid_usage_page(HID_PAGE(h->usage)),
207 hid_usage_in_page(h->usage),
208 h->flags & HIO_CONST ? " Const" : "",
209 h->flags & HIO_VARIABLE ? "" : " Array");
210 printf(", logical range %d..%d",
211 h->logical_minimum, h->logical_maximum);
212 if (h->physical_minimum != h->physical_maximum)
213 printf(", physical range %d..%d",
214 h->physical_minimum, h->physical_maximum);
216 printf(", unit=0x%02x exp=%d", h->unit, h->unit_exponent);
221 hid_collection_type(int32_t type)
226 case 0: return ("Physical");
227 case 1: return ("Application");
228 case 2: return ("Logical");
229 case 3: return ("Report");
230 case 4: return ("Named_Array");
231 case 5: return ("Usage_Switch");
232 case 6: return ("Usage_Modifier");
234 snprintf(num, sizeof(num), "0x%02x", type);
239 dumpitems(report_desc_t r)
245 for (d = hid_start_parse(r, ~0, -1); hid_get_item(d, &h); ) {
248 printf("Collection type=%s page=%s usage=%s\n",
249 hid_collection_type(h.collection),
250 hid_usage_page(HID_PAGE(h.usage)),
251 hid_usage_in_page(h.usage));
253 case hid_endcollection:
254 printf("End collection\n");
257 dumpitem("Input ", &h);
260 dumpitem("Output ", &h);
263 dumpitem("Feature", &h);
268 size = hid_report_size(r, hid_input, -1);
269 printf("Total input size %d bytes\n", size);
271 size = hid_report_size(r, hid_output, -1);
272 printf("Total output size %d bytes\n", size);
274 size = hid_report_size(r, hid_feature, -1);
275 printf("Total feature size %d bytes\n", size);
279 prdata(u_char *buf, struct hid_item *h)
285 for (i = 0; i < h->report_count; i++) {
286 data = hid_get_data(buf, h);
289 if (h->logical_minimum < 0)
290 printf("%d", (int)data);
294 printf(" [0x%x]", data);
295 h->pos += h->report_size;
301 dumpdata(int f, report_desc_t rd, int loop)
303 struct variable *var;
304 int dlen, havedata, i, match, r, rid, use_rid;
308 kind = zflag ? 3 : 0;
310 use_rid = !!hid_get_report_id(f);
319 for (var = vars; var; var = var->next) {
320 if (rid == var->h.report_ID &&
327 dlen = hid_report_size(rd, kind < 3 ? kind : hid_input, rid);
331 memset(dbuf, 0, dlen);
334 r = hid_get_report(f, kind, dbuf, dlen);
336 warn("hid_get_report(rid %d)", rid);
337 havedata = !r && (rid == 0 || dbuf[0] == rid);
341 r = read(f, dbuf, dlen);
343 err(1, "read error");
347 printf("Got %s report %d (%d bytes):",
348 kind == hid_output ? "output" :
349 kind == hid_feature ? "feature" : "input",
350 use_rid ? dbuf[0] : 0, dlen);
352 for (i = 0; i < dlen; i++)
353 printf(" %02x", dbuf[i]);
358 for (var = vars; var; var = var->next) {
359 if ((kind < 3 ? kind : hid_input) != var->h.kind)
361 if (var->h.report_ID != 0 &&
362 dbuf[0] != var->h.report_ID)
366 printf("%s=", var->name);
368 prdata(dbuf, &var->h);
374 } while (loop || kind < 3);
378 writedata(int f, report_desc_t rd)
380 struct variable *var;
387 for (kind = 0; kind < 3; kind ++) {
388 for (rid = 0; rid < 256; rid ++) {
389 for (var = vars; var; var = var->next) {
390 if (rid == var->h.report_ID && kind == var->h.kind)
395 dlen = hid_report_size(rd, kind, rid);
399 memset(dbuf, 0, dlen);
401 if (!zflag && hid_get_report(f, kind, dbuf, dlen) == 0) {
403 printf("Got %s report %d (%d bytes):",
404 kind == hid_input ? "input" :
405 kind == hid_output ? "output" : "feature",
407 for (i = 0; i < dlen; i++)
408 printf(" %02x", dbuf[i]);
412 warn("hid_get_report(rid %d)", rid);
414 printf("Can't get %s report %d (%d bytes). "
415 "Will be initialized with zeros.\n",
416 kind == hid_input ? "input" :
417 kind == hid_output ? "output" : "feature",
421 for (var = vars; var; var = var->next) {
422 if (rid != var->h.report_ID || kind != var->h.kind)
424 hid_set_data(dbuf, &var->h, var->val);
427 printf("Setting %s report %d (%d bytes):",
428 kind == hid_output ? "output" :
429 kind == hid_feature ? "feature" : "input",
431 for (i = 0; i < dlen; i++)
432 printf(" %02x", dbuf[i]);
435 r = hid_set_report(f, kind, dbuf, dlen);
437 warn("hid_set_report(rid %d)", rid);
444 main(int argc, char **argv)
448 char devnam[100], *dev = NULL;
455 while ((ch = getopt(argc, argv, "af:lnrt:vwxz")) != -1) {
497 if (argc == 0 && !all && !repdump)
502 snprintf(devnam, sizeof(devnam), "/dev/uhid%s", dev);
504 snprintf(devnam, sizeof(devnam), "/dev/%s", dev);
510 f = open(dev, O_RDWR);
514 r = hid_get_report_desc(f);
516 errx(1, "USB_GET_REPORT_DESC");
519 printf("Report descriptor:\n");
522 if (argc != 0 || all) {
523 parceargs(r, all, argc, argv);
527 dumpdata(f, r, loop);
530 hid_dispose_report_desc(r);