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