Annotation of src/usr.bin/usbhidctl/usbhid.c, Revision 1.19
1.19 ! cheloha 1: /* $OpenBSD: usbhid.c,v 1.18 2021/12/15 11:21:35 mestre Exp $ */
1.4 nate 2: /* $NetBSD: usbhid.c,v 1.22 2002/02/20 20:30:42 christos Exp $ */
1.1 pvalchev 3:
4: /*
1.4 nate 5: * Copyright (c) 2001 The NetBSD Foundation, Inc.
1.1 pvalchev 6: * All rights reserved.
7: *
8: * This code is derived from software contributed to The NetBSD Foundation
9: * by David Sainty <David.Sainty@dtsp.co.nz>
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: *
20: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30: * POSSIBILITY OF SUCH DAMAGE.
31: */
32:
33: #include <sys/types.h>
34:
35: #include <dev/usb/usb.h>
36: #include <dev/usb/usbhid.h>
37:
38: #include <ctype.h>
39: #include <err.h>
40: #include <errno.h>
41: #include <fcntl.h>
42: #include <limits.h>
43: #include <stdio.h>
44: #include <stdlib.h>
45: #include <string.h>
46: #include <unistd.h>
1.2 pvalchev 47: #include <usbhid.h>
1.1 pvalchev 48:
1.4 nate 49: /*
50: * Zero if not in a verbose mode. Greater levels of verbosity
51: * are indicated by values larger than one.
52: */
53: unsigned int verbose;
54:
1.1 pvalchev 55: /* Parser tokens */
56: #define DELIM_USAGE '.'
57: #define DELIM_PAGE ':'
58: #define DELIM_SET '='
59:
1.4 nate 60: static int reportid;
1.1 pvalchev 61:
62: struct Susbvar {
63: /* Variable name, not NUL terminated */
64: char const *variable;
65: size_t varlen;
66:
67: char const *value; /* Value to set variable to */
68:
69: #define MATCH_ALL (1 << 0)
70: #define MATCH_COLLECTIONS (1 << 1)
71: #define MATCH_NODATA (1 << 2)
72: #define MATCH_CONSTANTS (1 << 3)
73: #define MATCH_WASMATCHED (1 << 4)
74: #define MATCH_SHOWPAGENAME (1 << 5)
75: #define MATCH_SHOWNUMERIC (1 << 6)
76: #define MATCH_WRITABLE (1 << 7)
1.4 nate 77: #define MATCH_SHOWVALUES (1 << 8)
1.1 pvalchev 78: unsigned int mflags;
79:
80: /* Workspace for hidmatch() */
81: ssize_t matchindex;
82:
83: int (*opfunc)(struct hid_item *item, struct Susbvar *var,
84: u_int32_t const *collist, size_t collen, u_char *buf);
85: };
86:
87: struct Sreport {
88: struct usb_ctl_report *buffer;
89:
90: enum {srs_uninit, srs_clean, srs_dirty} status;
91: int report_id;
92: size_t size;
93: };
94:
95: static struct {
96: int uhid_report;
97: hid_kind_t hid_kind;
98: char const *name;
99: } const reptoparam[] = {
100: #define REPORT_INPUT 0
101: { UHID_INPUT_REPORT, hid_input, "input" },
102: #define REPORT_OUTPUT 1
103: { UHID_OUTPUT_REPORT, hid_output, "output" },
104: #define REPORT_FEATURE 2
105: { UHID_FEATURE_REPORT, hid_feature, "feature" }
106: #define REPORT_MAXVAL 2
107: };
108:
1.4 nate 109: /*
110: * Extract 16-bit unsigned usage ID from a numeric string. Returns -1
111: * if string failed to parse correctly.
112: */
113: static int
114: strtousage(const char *nptr, size_t nlen)
115: {
116: char *endptr;
117: long result;
118: char numstr[16];
119:
120: if (nlen >= sizeof(numstr) || !isdigit((unsigned char)*nptr))
121: return -1;
122:
123: /*
124: * We use strtol() here, but unfortunately strtol() requires a
125: * NUL terminated string - which we don't have - at least not
126: * officially.
127: */
128: memcpy(numstr, nptr, nlen);
129: numstr[nlen] = '\0';
130:
131: result = strtol(numstr, &endptr, 0);
132:
133: if (result < 0 || result > 0xffff || endptr != &numstr[nlen])
134: return -1;
135:
136: return result;
137: }
138:
139: struct usagedata {
140: char const *page_name;
141: char const *usage_name;
142: size_t page_len;
143: size_t usage_len;
144: int isfinal;
145: u_int32_t usage_id;
146: };
147:
148: /*
149: * Test a rule against the current usage data. Returns -1 on no
150: * match, 0 on partial match and 1 on complete match.
151: */
152: static int
153: hidtestrule(struct Susbvar *var, struct usagedata *cache)
154: {
155: char const *varname;
156: ssize_t matchindex, pagesplit;
157: size_t strind, varlen;
158: int numusage;
159: u_int32_t usage_id;
160:
161: matchindex = var->matchindex;
162: varname = var->variable;
163: varlen = var->varlen;
164:
165: usage_id = cache->usage_id;
166:
167: /*
168: * Parse the current variable name, locating the end of the
169: * current 'usage', and possibly where the usage page name
170: * ends.
171: */
172: pagesplit = -1;
173: for (strind = matchindex; strind < varlen; strind++) {
174: if (varname[strind] == DELIM_USAGE)
175: break;
176: if (varname[strind] == DELIM_PAGE)
177: pagesplit = strind;
178: }
179:
180: if (cache->isfinal && strind != varlen)
181: /*
182: * Variable name is too long (hit delimiter instead of
183: * end-of-variable).
184: */
185: return -1;
186:
187: if (pagesplit >= 0) {
188: /*
189: * Page name was specified, determine whether it was
190: * symbolic or numeric.
191: */
192: char const *strstart;
193: int numpage;
194:
195: strstart = &varname[matchindex];
196:
197: numpage = strtousage(strstart, pagesplit - matchindex);
198:
199: if (numpage >= 0) {
200: /* Valid numeric */
201:
202: if (numpage != HID_PAGE(usage_id))
203: /* Numeric didn't match page ID */
204: return -1;
205: } else {
206: /* Not a valid numeric */
207:
208: /*
209: * Load and cache the page name if and only if
210: * it hasn't already been loaded (it's a
211: * fairly expensive operation).
212: */
213: if (cache->page_name == NULL) {
214: cache->page_name = hid_usage_page(HID_PAGE(usage_id));
215: cache->page_len = strlen(cache->page_name);
216: }
217:
218: /*
219: * Compare specified page name to actual page
220: * name.
221: */
222: if (cache->page_len !=
223: (size_t)(pagesplit - matchindex) ||
224: memcmp(cache->page_name,
225: &varname[matchindex],
226: cache->page_len) != 0)
227: /* Mismatch, page name wrong */
228: return -1;
229: }
230:
231: /* Page matches, discard page name */
232: matchindex = pagesplit + 1;
233: }
234:
235: numusage = strtousage(&varname[matchindex], strind - matchindex);
236:
237: if (numusage >= 0) {
238: /* Valid numeric */
239:
240: if (numusage != HID_USAGE(usage_id))
241: /* Numeric didn't match usage ID */
242: return -1;
243: } else {
244: /* Not a valid numeric */
245:
246: /* Load and cache the usage name */
247: if (cache->usage_name == NULL) {
248: cache->usage_name = hid_usage_in_page(usage_id);
249: cache->usage_len = strlen(cache->usage_name);
250: }
251:
252: /*
253: * Compare specified usage name to actual usage name
254: */
255: if (cache->usage_len != (size_t)(strind - matchindex) ||
256: memcmp(cache->usage_name, &varname[matchindex],
257: cache->usage_len) != 0)
258: /* Mismatch, usage name wrong */
259: return -1;
260: }
261:
262: if (cache->isfinal)
263: /* Match */
264: return 1;
265:
266: /*
267: * Partial match: Move index past this usage string +
268: * delimiter
269: */
270: var->matchindex = strind + 1;
271:
272: return 0;
273: }
274:
275: /*
276: * hidmatch() determines whether the item specified in 'item', and
1.7 jsg 277: * nested within a hierarchy of collections specified in 'collist'
1.4 nate 278: * matches any of the rules in the list 'varlist'. Returns the
279: * matching rule on success, or NULL on no match.
280: */
1.1 pvalchev 281: static struct Susbvar*
282: hidmatch(u_int32_t const *collist, size_t collen, struct hid_item *item,
283: struct Susbvar *varlist, size_t vlsize)
284: {
1.4 nate 285: size_t colind, vlactive, vlind;
1.1 pvalchev 286: int iscollection;
287:
288: /*
289: * Keep track of how many variables are still "active". When
290: * the active count reaches zero, don't bother to continue
291: * looking for matches.
292: */
293: vlactive = vlsize;
294:
295: iscollection = item->kind == hid_collection ||
296: item->kind == hid_endcollection;
297:
298: for (vlind = 0; vlind < vlsize; vlind++) {
299: struct Susbvar *var;
300:
301: var = &varlist[vlind];
302:
303: var->matchindex = 0;
304:
305: if (!(var->mflags & MATCH_COLLECTIONS) && iscollection) {
306: /* Don't match collections for this variable */
307: var->matchindex = -1;
308: vlactive--;
309: } else if (!iscollection && !(var->mflags & MATCH_CONSTANTS) &&
310: (item->flags & HIO_CONST)) {
311: /*
312: * Don't match constants for this variable,
313: * but ignore the constant bit on collections.
314: */
315: var->matchindex = -1;
316: vlactive--;
317: } else if ((var->mflags & MATCH_WRITABLE) &&
318: ((item->kind != hid_output &&
319: item->kind != hid_feature) ||
320: (item->flags & HIO_CONST))) {
321: /*
322: * If we are only matching writable items, if
323: * this is not an output or feature kind, or
324: * it is a constant, reject it.
325: */
326: var->matchindex = -1;
327: vlactive--;
328: } else if (var->mflags & MATCH_ALL) {
329: /* Match immediately */
330: return &varlist[vlind];
331: }
332: }
333:
1.4 nate 334: /*
335: * Loop through each usage in the collection list, including
336: * the 'item' itself on the final iteration. For each usage,
337: * test which variables named in the rule list are still
338: * applicable - if any.
339: */
1.1 pvalchev 340: for (colind = 0; vlactive > 0 && colind <= collen; colind++) {
1.4 nate 341: struct usagedata cache;
1.1 pvalchev 342:
1.4 nate 343: cache.isfinal = (colind == collen);
344: if (cache.isfinal)
345: cache.usage_id = item->usage;
1.1 pvalchev 346: else
1.4 nate 347: cache.usage_id = collist[colind];
1.1 pvalchev 348:
1.4 nate 349: cache.usage_name = NULL;
350: cache.page_name = NULL;
1.1 pvalchev 351:
1.4 nate 352: /*
353: * Loop through each rule, testing whether the rule is
354: * still applicable or not. For each rule,
355: * 'matchindex' retains the current match state as an
356: * index into the variable name string, or -1 if this
357: * rule has been proven not to match.
358: */
1.1 pvalchev 359: for (vlind = 0; vlind < vlsize; vlind++) {
360: struct Susbvar *var;
1.4 nate 361: int matchres;
1.1 pvalchev 362:
363: var = &varlist[vlind];
364:
1.4 nate 365: if (var->matchindex < 0)
1.1 pvalchev 366: /* Mismatch at a previous level */
367: continue;
368:
1.4 nate 369: matchres = hidtestrule(var, &cache);
1.1 pvalchev 370:
1.4 nate 371: if (matchres < 0) {
372: /* Bad match */
1.1 pvalchev 373: var->matchindex = -1;
374: vlactive--;
375: continue;
1.4 nate 376: } else if (matchres > 0) {
377: /* Complete match */
378: return var;
1.1 pvalchev 379: }
380: }
381: }
382:
383: return NULL;
384: }
385:
386: static void
387: allocreport(struct Sreport *report, report_desc_t rd, int repindex)
388: {
389: int reptsize;
390:
1.4 nate 391: reptsize = hid_report_size(rd, reptoparam[repindex].hid_kind, reportid);
1.1 pvalchev 392: if (reptsize < 0)
393: errx(1, "Negative report size");
394: report->size = reptsize;
395:
396: if (report->size > 0) {
1.16 anton 397: report->buffer = malloc(sizeof(*report->buffer));
1.1 pvalchev 398: if (report->buffer == NULL)
399: err(1, NULL);
400: } else
401: report->buffer = NULL;
402:
403: report->status = srs_clean;
404: }
405:
406: static void
407: freereport(struct Sreport *report)
408: {
1.12 mmcc 409: free(report->buffer);
1.1 pvalchev 410: report->status = srs_uninit;
411: }
412:
413: static void
414: getreport(struct Sreport *report, int hidfd, report_desc_t rd, int repindex)
415: {
416: if (report->status == srs_uninit) {
417: allocreport(report, rd, repindex);
418: if (report->size == 0)
419: return;
420:
1.3 nate 421: report->buffer->ucr_report = reptoparam[repindex].uhid_report;
1.15 deraadt 422: if (ioctl(hidfd, USB_GET_REPORT, report->buffer) == -1)
1.4 nate 423: err(1, "USB_GET_REPORT (probably not supported by "
424: "device)");
1.1 pvalchev 425: }
426: }
427:
428: static void
429: setreport(struct Sreport *report, int hidfd, int repindex)
430: {
431: if (report->status == srs_dirty) {
1.3 nate 432: report->buffer->ucr_report = reptoparam[repindex].uhid_report;
1.1 pvalchev 433:
1.15 deraadt 434: if (ioctl(hidfd, USB_SET_REPORT, report->buffer) == -1)
1.1 pvalchev 435: err(1, "USB_SET_REPORT(%s)",
436: reptoparam[repindex].name);
437:
438: report->status = srs_clean;
439: }
440: }
441:
442: /* ARGSUSED1 */
443: static int
444: varop_value(struct hid_item *item, struct Susbvar *var,
445: u_int32_t const *collist, size_t collen, u_char *buf)
446: {
447: printf("%d\n", hid_get_data(buf, item));
448: return 0;
449: }
450:
451: /* ARGSUSED1 */
452: static int
453: varop_display(struct hid_item *item, struct Susbvar *var,
454: u_int32_t const *collist, size_t collen, u_char *buf)
455: {
456: size_t colitem;
1.4 nate 457: int val, i;
1.1 pvalchev 458:
1.4 nate 459: for (i = 0; i < item->report_count; i++) {
460: for (colitem = 0; colitem < collen; colitem++) {
461: if (var->mflags & MATCH_SHOWPAGENAME)
462: printf("%s:",
463: hid_usage_page(HID_PAGE(collist[colitem])));
464: printf("%s.", hid_usage_in_page(collist[colitem]));
465: }
1.1 pvalchev 466: if (var->mflags & MATCH_SHOWPAGENAME)
1.4 nate 467: printf("%s:", hid_usage_page(HID_PAGE(item->usage)));
468: val = hid_get_data(buf, item);
469: item->pos += item->report_size;
470: if (item->usage_minimum != 0 || item->usage_maximum != 0) {
471: val += item->usage_minimum;
472: printf("%s=1", hid_usage_in_page(val));
473: } else {
474: printf("%s=%d%s", hid_usage_in_page(item->usage),
475: val, item->flags & HIO_CONST ? " (const)" : "");
476: }
477: if (item->report_count > 1)
478: printf(" [%d]", i);
479: printf("\n");
1.1 pvalchev 480: }
481: return 0;
482: }
483:
484: /* ARGSUSED1 */
485: static int
486: varop_modify(struct hid_item *item, struct Susbvar *var,
487: u_int32_t const *collist, size_t collen, u_char *buf)
488: {
489: u_int dataval;
490:
491: dataval = (u_int)strtol(var->value, NULL, 10);
492:
493: hid_set_data(buf, item, dataval);
494:
1.4 nate 495: if (var->mflags & MATCH_SHOWVALUES)
496: /* Display set value */
1.1 pvalchev 497: varop_display(item, var, collist, collen, buf);
498:
499: return 1;
500: }
501:
502: static void
503: reportitem(char const *label, struct hid_item const *item, unsigned int mflags)
504: {
1.4 nate 505: int isconst = item->flags & HIO_CONST,
506: isvar = item->flags & HIO_VARIABLE;
507: printf("%s size=%d count=%d%s%s page=%s", label,
1.1 pvalchev 508: item->report_size, item->report_count,
1.4 nate 509: isconst ? " Const" : "",
510: !isvar && !isconst ? " Array" : "",
511: hid_usage_page(HID_PAGE(item->usage)));
512: if (item->usage_minimum != 0 || item->usage_maximum != 0) {
513: printf(" usage=%s..%s", hid_usage_in_page(item->usage_minimum),
514: hid_usage_in_page(item->usage_maximum));
515: if (mflags & MATCH_SHOWNUMERIC)
516: printf(" (%u:0x%x..%u:0x%x)",
517: HID_PAGE(item->usage_minimum),
518: HID_USAGE(item->usage_minimum),
519: HID_PAGE(item->usage_maximum),
520: HID_USAGE(item->usage_maximum));
521: } else {
522: printf(" usage=%s", hid_usage_in_page(item->usage));
523: if (mflags & MATCH_SHOWNUMERIC)
524: printf(" (%u:0x%x)",
525: HID_PAGE(item->usage), HID_USAGE(item->usage));
526: }
1.1 pvalchev 527: printf(", logical range %d..%d",
528: item->logical_minimum, item->logical_maximum);
529: if (item->physical_minimum != item->physical_maximum)
530: printf(", physical range %d..%d",
531: item->physical_minimum, item->physical_maximum);
532: if (item->unit)
533: printf(", unit=0x%02x exp=%d", item->unit,
534: item->unit_exponent);
535: printf("\n");
536: }
537:
538: /* ARGSUSED1 */
539: static int
540: varop_report(struct hid_item *item, struct Susbvar *var,
541: u_int32_t const *collist, size_t collen, u_char *buf)
542: {
543: switch (item->kind) {
544: case hid_collection:
1.4 nate 545: printf("Collection page=%s usage=%s",
1.1 pvalchev 546: hid_usage_page(HID_PAGE(item->usage)),
547: hid_usage_in_page(item->usage));
1.4 nate 548: if (var->mflags & MATCH_SHOWNUMERIC)
549: printf(" (%u:0x%x)\n",
550: HID_PAGE(item->usage), HID_USAGE(item->usage));
551: else
552: printf("\n");
1.1 pvalchev 553: break;
554: case hid_endcollection:
555: printf("End collection\n");
556: break;
557: case hid_input:
558: reportitem("Input ", item, var->mflags);
559: break;
560: case hid_output:
561: reportitem("Output ", item, var->mflags);
562: break;
563: case hid_feature:
564: reportitem("Feature", item, var->mflags);
565: break;
566: }
567:
568: return 0;
569: }
570:
571: static void
572: devloop(int hidfd, report_desc_t rd, struct Susbvar *varlist, size_t vlsize)
573: {
1.4 nate 574: u_char *dbuf;
1.1 pvalchev 575: struct hid_data *hdata;
1.4 nate 576: size_t collind, dlen;
1.1 pvalchev 577: struct hid_item hitem;
578: u_int32_t colls[128];
579: struct Sreport inreport;
580:
581: allocreport(&inreport, rd, REPORT_INPUT);
582:
583: if (inreport.size <= 0)
584: errx(1, "Input report descriptor invalid length");
585:
586: dlen = inreport.size;
1.3 nate 587: dbuf = inreport.buffer->ucr_data;
1.1 pvalchev 588:
589: for (;;) {
590: ssize_t readlen;
591:
592: readlen = read(hidfd, dbuf, dlen);
1.15 deraadt 593: if (readlen == -1)
1.4 nate 594: err(1, "Device read error");
595: if (dlen != (size_t)readlen)
596: errx(1, "Unexpected response length: %lu != %lu",
597: (unsigned long)readlen, (unsigned long)dlen);
1.1 pvalchev 598:
599: collind = 0;
1.4 nate 600: hdata = hid_start_parse(rd, 1 << hid_input, reportid);
1.1 pvalchev 601: if (hdata == NULL)
602: errx(1, "Failed to start parser");
603:
604: while (hid_get_item(hdata, &hitem)) {
605: struct Susbvar *matchvar;
606:
607: switch (hitem.kind) {
608: case hid_collection:
609: if (collind >= (sizeof(colls) / sizeof(*colls)))
610: errx(1, "Excessive nested collections");
611: colls[collind++] = hitem.usage;
612: break;
613: case hid_endcollection:
614: if (collind == 0)
615: errx(1, "Excessive collection ends");
616: collind--;
617: break;
618: case hid_input:
619: break;
620: case hid_output:
621: case hid_feature:
622: errx(1, "Unexpected non-input item returned");
623: }
624:
1.4 nate 625: if (reportid != -1 && hitem.report_ID != reportid)
626: continue;
627:
1.1 pvalchev 628: matchvar = hidmatch(colls, collind, &hitem,
629: varlist, vlsize);
630:
631: if (matchvar != NULL)
632: matchvar->opfunc(&hitem, matchvar,
633: colls, collind,
1.3 nate 634: inreport.buffer->ucr_data);
1.1 pvalchev 635: }
636: hid_end_parse(hdata);
637: printf("\n");
638: }
639: /* NOTREACHED */
640: }
641:
642: static void
643: devshow(int hidfd, report_desc_t rd, struct Susbvar *varlist, size_t vlsize,
644: int kindset)
645: {
646: struct hid_data *hdata;
1.4 nate 647: size_t collind, repind, vlind;
1.1 pvalchev 648: struct hid_item hitem;
649: u_int32_t colls[128];
650: struct Sreport reports[REPORT_MAXVAL + 1];
651:
652:
653: for (repind = 0; repind < (sizeof(reports) / sizeof(*reports));
654: repind++) {
655: reports[repind].status = srs_uninit;
656: reports[repind].buffer = NULL;
657: }
658:
659: collind = 0;
1.4 nate 660: hdata = hid_start_parse(rd, kindset, reportid);
1.1 pvalchev 661: if (hdata == NULL)
662: errx(1, "Failed to start parser");
663:
664: while (hid_get_item(hdata, &hitem)) {
665: struct Susbvar *matchvar;
666: int repindex;
667:
1.4 nate 668: if (verbose > 3)
669: printf("item: kind=%d repid=%d usage=0x%x\n",
670: hitem.kind, hitem.report_ID, hitem.usage);
1.1 pvalchev 671: repindex = -1;
672: switch (hitem.kind) {
673: case hid_collection:
674: if (collind >= (sizeof(colls) / sizeof(*colls)))
675: errx(1, "Excessive nested collections");
676: colls[collind++] = hitem.usage;
677: break;
678: case hid_endcollection:
679: if (collind == 0)
680: errx(1, "Excessive collection ends");
681: collind--;
682: break;
683: case hid_input:
684: repindex = REPORT_INPUT;
685: break;
686: case hid_output:
687: repindex = REPORT_OUTPUT;
688: break;
689: case hid_feature:
690: repindex = REPORT_FEATURE;
691: break;
692: }
693:
1.4 nate 694: if (reportid != -1 && hitem.report_ID != reportid)
695: continue;
696:
1.1 pvalchev 697: matchvar = hidmatch(colls, collind, &hitem, varlist, vlsize);
698:
699: if (matchvar != NULL) {
700: u_char *bufdata;
701: struct Sreport *repptr;
702:
703: matchvar->mflags |= MATCH_WASMATCHED;
704:
705: if (repindex >= 0)
706: repptr = &reports[repindex];
707: else
708: repptr = NULL;
709:
710: if (repptr != NULL &&
711: !(matchvar->mflags & MATCH_NODATA))
712: getreport(repptr, hidfd, rd, repindex);
713:
714: bufdata = (repptr == NULL || repptr->buffer == NULL) ?
1.3 nate 715: NULL : repptr->buffer->ucr_data;
1.1 pvalchev 716:
717: if (matchvar->opfunc(&hitem, matchvar, colls, collind,
718: bufdata))
719: repptr->status = srs_dirty;
720: }
721: }
722: hid_end_parse(hdata);
723:
724: for (repind = 0; repind < (sizeof(reports) / sizeof(*reports));
725: repind++) {
726: setreport(&reports[repind], hidfd, repind);
727: freereport(&reports[repind]);
728: }
729:
730: /* Warn about any items that we couldn't find a match for */
731: for (vlind = 0; vlind < vlsize; vlind++) {
732: struct Susbvar *var;
733:
734: var = &varlist[vlind];
735:
736: if (var->variable != NULL &&
737: !(var->mflags & MATCH_WASMATCHED))
738: warnx("Failed to match: %.*s", (int)var->varlen,
739: var->variable);
740: }
741: }
742:
743: static void
744: usage(void)
745: {
746: extern char *__progname;
747:
1.9 jakemsr 748: fprintf(stderr, "usage: %s -f device [-t table] [-alv]\n",
1.1 pvalchev 749: __progname);
1.5 jmc 750: fprintf(stderr, " %s -f device [-t table] [-v] -r\n",
1.1 pvalchev 751: __progname);
752: fprintf(stderr,
1.5 jmc 753: " %s -f device [-t table] [-lnv] name ...\n",
1.1 pvalchev 754: __progname);
755: fprintf(stderr,
1.5 jmc 756: " %s -f device [-t table] -w name=value ...\n",
1.1 pvalchev 757: __progname);
1.17 jcs 758: fprintf(stderr, " %s -f device -R\n", __progname);
1.1 pvalchev 759: exit(1);
760: }
761:
762: int
763: main(int argc, char **argv)
764: {
1.4 nate 765: char const *dev;
766: char const *table;
767: size_t varnum;
1.17 jcs 768: uint32_t repsize;
769: int aflag, lflag, nflag, rflag, Rflag, wflag;
770: int ch, hidfd, x;
771: uint8_t *repdata;
1.1 pvalchev 772: report_desc_t repdesc;
773: char devnamebuf[PATH_MAX];
774: struct Susbvar variables[128];
775:
1.17 jcs 776: wflag = aflag = nflag = verbose = rflag = Rflag = lflag = 0;
1.1 pvalchev 777: dev = NULL;
778: table = NULL;
1.19 ! cheloha 779: while ((ch = getopt(argc, argv, "af:lnRrt:vw")) != -1) {
1.1 pvalchev 780: switch (ch) {
781: case 'a':
782: aflag = 1;
783: break;
784: case 'f':
785: dev = optarg;
786: break;
787: case 'l':
788: lflag = 1;
789: break;
790: case 'n':
791: nflag = 1;
792: break;
793: case 'r':
794: rflag = 1;
795: break;
1.17 jcs 796: case 'R':
797: Rflag = 1;
798: break;
1.1 pvalchev 799: case 't':
800: table = optarg;
801: break;
802: case 'v':
803: verbose++;
804: break;
805: case 'w':
806: wflag = 1;
807: break;
808: default:
809: usage();
810: /* NOTREACHED */
811: }
812: }
813: argc -= optind;
814: argv += optind;
1.17 jcs 815: if (dev == NULL || (lflag && (wflag || rflag || Rflag)) ||
816: (rflag && Rflag)) {
1.1 pvalchev 817: /*
818: * No device specified, or attempting to loop and set
819: * or dump report at the same time
820: */
821: usage();
822: /* NOTREACHED */
823: }
1.9 jakemsr 824:
825: if (argc == 0 && rflag == 0)
826: aflag = 1;
1.1 pvalchev 827:
828: for (varnum = 0; varnum < (size_t)argc; varnum++) {
829: char const *name, *valuesep;
830: struct Susbvar *svar;
831:
832: svar = &variables[varnum];
833: name = argv[varnum];
834: valuesep = strchr(name, DELIM_SET);
835:
836: svar->variable = name;
837: svar->mflags = 0;
838:
839: if (valuesep == NULL) {
840: /* Read variable */
841: if (wflag)
842: errx(1, "Must not specify -w to read variables");
843: svar->value = NULL;
844: svar->varlen = strlen(name);
845:
846: if (nflag) {
847: /* Display value of variable only */
848: svar->opfunc = varop_value;
849: } else {
850: /* Display name and value of variable */
851: svar->opfunc = varop_display;
852:
853: if (verbose >= 1)
854: /* Show page names in verbose modes */
855: svar->mflags |= MATCH_SHOWPAGENAME;
856: }
857: } else {
858: /* Write variable */
859: if (!wflag)
860: errx(2, "Must specify -w to set variables");
861: svar->mflags |= MATCH_WRITABLE;
1.4 nate 862: if (verbose >= 1)
863: /*
864: * Allow displaying of set value in
865: * verbose mode. This isn't
866: * particularly useful though, so
867: * don't bother documenting it.
868: */
869: svar->mflags |= MATCH_SHOWVALUES;
1.1 pvalchev 870: svar->varlen = valuesep - name;
871: svar->value = valuesep + 1;
872: svar->opfunc = varop_modify;
873: }
874: }
875:
876: if (aflag || rflag) {
877: struct Susbvar *svar;
878:
879: svar = &variables[varnum++];
880:
881: svar->variable = NULL;
882: svar->mflags = MATCH_ALL;
883:
884: if (rflag) {
885: /*
886: * Dump report descriptor. Do dump collection
887: * items also, and hint that it won't be
888: * necessary to get the item status.
889: */
890: svar->opfunc = varop_report;
891: svar->mflags |= MATCH_COLLECTIONS | MATCH_NODATA;
892:
893: switch (verbose) {
894: default:
895: /* Level 2: Show item numerics and constants */
896: svar->mflags |= MATCH_SHOWNUMERIC;
897: /* FALLTHROUGH */
898: case 1:
899: /* Level 1: Just show constants */
900: svar->mflags |= MATCH_CONSTANTS;
901: /* FALLTHROUGH */
902: case 0:
903: break;
904: }
905: } else {
906: /* Display name and value of variable */
907: svar->opfunc = varop_display;
908:
909: switch (verbose) {
910: default:
911: /* Level 2: Show constants and page names */
912: svar->mflags |= MATCH_CONSTANTS;
913: /* FALLTHROUGH */
914: case 1:
915: /* Level 1: Just show page names */
916: svar->mflags |= MATCH_SHOWPAGENAME;
917: /* FALLTHROUGH */
918: case 0:
919: break;
920: }
921: }
922: }
923:
924: if (varnum == 0) {
925: /* Nothing to do... Display usage information. */
926: usage();
927: /* NOTREACHED */
928: }
929:
1.6 deraadt 930: if (hid_start(table) == -1)
931: errx(1, "hid_init");
1.1 pvalchev 932:
933: if (dev[0] != '/') {
934: snprintf(devnamebuf, sizeof(devnamebuf), "/dev/%s%s",
1.11 deraadt 935: isdigit((unsigned char)dev[0]) ? "uhid" : "", dev);
1.1 pvalchev 936: dev = devnamebuf;
937: }
938:
1.10 miod 939: hidfd = open(dev, wflag ? O_RDWR : O_RDONLY);
1.15 deraadt 940: if (hidfd == -1)
1.1 pvalchev 941: err(1, "%s", dev);
1.18 mestre 942:
943: if (unveil("/", "") == -1)
944: err(1, "unveil /");
945: if (unveil(NULL, NULL) == -1)
946: err(1, "unveil");
1.1 pvalchev 947:
1.15 deraadt 948: if (ioctl(hidfd, USB_GET_REPORT_ID, &reportid) == -1)
1.4 nate 949: reportid = -1;
950: if (verbose > 1)
951: printf("report ID=%d\n", reportid);
1.1 pvalchev 952: repdesc = hid_get_report_desc(hidfd);
953: if (repdesc == 0)
954: errx(1, "USB_GET_REPORT_DESC");
955:
1.17 jcs 956: if (Rflag) {
957: hid_get_report_desc_data(repdesc, &repdata, &repsize);
958:
959: for (x = 0; x < repsize; x++)
960: printf("%s0x%02x", x > 0 ? " " : "", repdata[x]);
961: printf("\n");
962: }
963:
1.1 pvalchev 964: if (lflag) {
965: devloop(hidfd, repdesc, variables, varnum);
966: /* NOTREACHED */
967: }
968:
969: if (rflag)
970: /* Report mode header */
971: printf("Report descriptor:\n");
972:
1.17 jcs 973: if (!Rflag)
974: devshow(hidfd, repdesc, variables, varnum,
975: 1 << hid_input |
976: 1 << hid_output |
977: 1 << hid_feature);
1.1 pvalchev 978:
979: if (rflag) {
980: /* Report mode trailer */
981: size_t repindex;
982: for (repindex = 0;
983: repindex < (sizeof(reptoparam) / sizeof(*reptoparam));
984: repindex++) {
1.4 nate 985: int size;
1.1 pvalchev 986: size = hid_report_size(repdesc,
987: reptoparam[repindex].hid_kind,
1.4 nate 988: reportid);
989: printf("Total %7s size %d bytes\n",
990: reptoparam[repindex].name, size);
1.1 pvalchev 991: }
992: }
993:
994: hid_dispose_report_desc(repdesc);
995: exit(0);
996: /* NOTREACHED */
997: }