1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2008-2009 Hans Petter Selasky. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <stdbool.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <err.h>
33 #include <sysexits.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <pwd.h>
37 #include <grp.h>
38 #include <errno.h>
39 #include <ctype.h>
40 #include <capsicum_helpers.h>
41 #include <sys/param.h>
42
43 #include <libusb20_desc.h>
44 #include <libusb20.h>
45
46 #include "dump.h"
47
48 struct options {
49 const char *quirkname;
50 void *buffer;
51 int template;
52 gid_t gid;
53 uid_t uid;
54 mode_t mode;
55 uint32_t got_any;
56 struct LIBUSB20_CONTROL_SETUP_DECODED setup;
57 uint16_t bus;
58 uint16_t addr;
59 uint16_t iface;
60 uint16_t vid;
61 uint16_t pid;
62 uint16_t lo_rev; /* inclusive */
63 uint16_t hi_rev; /* inclusive */
64 uint8_t string_index;
65 uint8_t config_index;
66 uint8_t alt_index;
67 uint8_t got_list:1;
68 uint8_t got_bus:1;
69 uint8_t got_addr:1;
70 uint8_t got_set_config:1;
71 uint8_t got_set_alt:1;
72 uint8_t got_set_template:1;
73 uint8_t got_get_template:1;
74 uint8_t got_suspend:1;
75 uint8_t got_resume:1;
76 uint8_t got_reset:1;
77 uint8_t got_power_off:1;
78 uint8_t got_power_save:1;
79 uint8_t got_power_on:1;
80 uint8_t got_dump_device_quirks:1;
81 uint8_t got_dump_quirk_names:1;
82 uint8_t got_dump_all_desc:1;
83 uint8_t got_dump_device_desc:1;
84 uint8_t got_dump_curr_config:1;
85 uint8_t got_dump_all_config:1;
86 uint8_t got_dump_info:1;
87 uint8_t got_dump_stats:1;
88 uint8_t got_show_iface_driver:1;
89 uint8_t got_remove_device_quirk:1;
90 uint8_t got_add_device_quirk:1;
91 uint8_t got_remove_quirk:1;
92 uint8_t got_add_quirk:1;
93 uint8_t got_dump_string:1;
94 uint8_t got_do_request:1;
95 uint8_t got_detach_kernel_driver:1;
96 uint8_t opt_dump_in_list_mode:1;
97 uint8_t got_attach_kernel_driver:1;
98 };
99
100 struct token {
101 const char *name;
102 uint8_t value;
103 uint8_t narg;
104 };
105
106 enum {
107 T_SET_CONFIG,
108 T_SET_ALT,
109 T_SET_TEMPLATE,
110 T_GET_TEMPLATE,
111 T_ADD_DEVICE_QUIRK,
112 T_REMOVE_DEVICE_QUIRK,
113 T_ADD_QUIRK,
114 T_REMOVE_QUIRK,
115 T_SHOW_IFACE_DRIVER,
116 T_DETACH_KERNEL_DRIVER,
117 T_DUMP_QUIRK_NAMES,
118 T_DUMP_DEVICE_QUIRKS,
119 T_DUMP_ALL_DESC,
120 T_DUMP_DEVICE_DESC,
121 T_DUMP_CURR_CONFIG_DESC,
122 T_DUMP_ALL_CONFIG_DESC,
123 T_DUMP_STRING,
124 T_DUMP_INFO,
125 T_DUMP_STATS,
126 T_SUSPEND,
127 T_RESUME,
128 T_POWER_OFF,
129 T_POWER_SAVE,
130 T_POWER_ON,
131 T_RESET,
132 T_LIST,
133 T_DO_REQUEST,
134 T_ATTACH_KERNEL_DRIVER,
135 };
136
137 static struct options options;
138
139 static const struct token token[] = {
140 {"set_config", T_SET_CONFIG, 1},
141 {"set_alt", T_SET_ALT, 1},
142 {"set_template", T_SET_TEMPLATE, 1},
143 {"get_template", T_GET_TEMPLATE, 0},
144 {"add_dev_quirk_vplh", T_ADD_DEVICE_QUIRK, 5},
145 {"remove_dev_quirk_vplh", T_REMOVE_DEVICE_QUIRK, 5},
146 {"add_quirk", T_ADD_QUIRK, 1},
147 {"remove_quirk", T_REMOVE_QUIRK, 1},
148 {"detach_kernel_driver", T_DETACH_KERNEL_DRIVER, 0},
149 {"attach_kernel_driver", T_ATTACH_KERNEL_DRIVER, 0},
150 {"dump_quirk_names", T_DUMP_QUIRK_NAMES, 0},
151 {"dump_device_quirks", T_DUMP_DEVICE_QUIRKS, 0},
152 {"dump_all_desc", T_DUMP_ALL_DESC, 0},
153 {"dump_device_desc", T_DUMP_DEVICE_DESC, 0},
154 {"dump_curr_config_desc", T_DUMP_CURR_CONFIG_DESC, 0},
155 {"dump_all_config_desc", T_DUMP_ALL_CONFIG_DESC, 0},
156 {"dump_string", T_DUMP_STRING, 1},
157 {"dump_info", T_DUMP_INFO, 0},
158 {"dump_stats", T_DUMP_STATS, 0},
159 {"show_ifdrv", T_SHOW_IFACE_DRIVER, 0},
160 {"suspend", T_SUSPEND, 0},
161 {"resume", T_RESUME, 0},
162 {"power_off", T_POWER_OFF, 0},
163 {"power_save", T_POWER_SAVE, 0},
164 {"power_on", T_POWER_ON, 0},
165 {"reset", T_RESET, 0},
166 {"list", T_LIST, 0},
167 {"do_request", T_DO_REQUEST, 5},
168 };
169
170 static void
be_dev_remove_quirk(struct libusb20_backend * pbe,uint16_t vid,uint16_t pid,uint16_t lorev,uint16_t hirev,const char * str)171 be_dev_remove_quirk(struct libusb20_backend *pbe,
172 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
173 const char *str)
174 {
175 struct libusb20_quirk q;
176 int error;
177
178 memset(&q, 0, sizeof(q));
179
180 q.vid = vid;
181 q.pid = pid;
182 q.bcdDeviceLow = lorev;
183 q.bcdDeviceHigh = hirev;
184 strlcpy(q.quirkname, str, sizeof(q.quirkname));
185
186 error = libusb20_be_remove_dev_quirk(pbe, &q);
187 if (error) {
188 fprintf(stderr, "Removing quirk '%s' failed, continuing.\n", str);
189 }
190 }
191
192 static void
be_dev_add_quirk(struct libusb20_backend * pbe,uint16_t vid,uint16_t pid,uint16_t lorev,uint16_t hirev,const char * str)193 be_dev_add_quirk(struct libusb20_backend *pbe,
194 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
195 const char *str)
196 {
197 struct libusb20_quirk q;
198 int error;
199
200 memset(&q, 0, sizeof(q));
201
202 q.vid = vid;
203 q.pid = pid;
204 q.bcdDeviceLow = lorev;
205 q.bcdDeviceHigh = hirev;
206 strlcpy(q.quirkname, str, sizeof(q.quirkname));
207
208 error = libusb20_be_add_dev_quirk(pbe, &q);
209 if (error) {
210 fprintf(stderr, "Adding quirk '%s' failed, continuing.\n", str);
211 }
212 }
213
214 static uint8_t
get_token(const char * str,uint8_t narg)215 get_token(const char *str, uint8_t narg)
216 {
217 uint8_t n;
218
219 for (n = 0; n != nitems(token); n++) {
220 if (strcasecmp(str, token[n].name) == 0) {
221 if (token[n].narg > narg) {
222 /* too few arguments */
223 break;
224 }
225 return (token[n].value);
226 }
227 }
228 return (0 - 1);
229 }
230
231 static uid_t
num_id(const char * name,const char * type)232 num_id(const char *name, const char *type)
233 {
234 uid_t val;
235 char *ep;
236
237 errno = 0;
238 val = strtoul(name, &ep, 0);
239 if (errno) {
240 err(1, "%s", name);
241 }
242 if (*ep != '\0') {
243 errx(1, "%s: illegal %s name", name, type);
244 }
245 return (val);
246 }
247
248 static int
get_int(const char * s)249 get_int(const char *s)
250 {
251 int val;
252 char *ep;
253
254 errno = 0;
255 val = strtoul(s, &ep, 0);
256 if (errno) {
257 err(1, "%s", s);
258 }
259 if (*ep != '\0') {
260 errx(1, "illegal number: %s", s);
261 }
262 return val;
263 }
264
265 static void
duplicate_option(const char * ptr)266 duplicate_option(const char *ptr)
267 {
268 fprintf(stderr, "Syntax error: "
269 "Duplicate option: '%s'\n", ptr);
270 exit(1);
271 }
272
273 static void
usage(int exitcode)274 usage(int exitcode)
275 {
276 fprintf(stderr, ""
277 "usbconfig - configure the USB subsystem" "\n"
278 "usage: usbconfig [-u <busnum>] [-a <devaddr>] [-i <ifaceindex>] [-v] [cmds...]" "\n"
279 "usage: usbconfig -d [ugen]<busnum>.<devaddr> [-i <ifaceindex>] [-v] [cmds...]" "\n"
280 "commands:" "\n"
281 " set_config <cfg_index>" "\n"
282 " set_alt <alt_index>" "\n"
283 " set_template <template>" "\n"
284 " get_template" "\n"
285 " add_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
286 " remove_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
287 " add_quirk <quirk>" "\n"
288 " remove_quirk <quirk>" "\n"
289 " detach_kernel_driver" "\n"
290 " attach_kernel_driver" "\n"
291 " dump_quirk_names" "\n"
292 " dump_device_quirks" "\n"
293 " dump_all_desc" "\n"
294 " dump_device_desc" "\n"
295 " dump_curr_config_desc" "\n"
296 " dump_all_config_desc" "\n"
297 " dump_string <index>" "\n"
298 " dump_info" "\n"
299 " dump_stats" "\n"
300 " show_ifdrv" "\n"
301 " suspend" "\n"
302 " resume" "\n"
303 " power_off" "\n"
304 " power_save" "\n"
305 " power_on" "\n"
306 " reset" "\n"
307 " list" "\n"
308 " do_request <bmReqTyp> <bReq> <wVal> <wIdx> <wLen> <data...>" "\n"
309 );
310 exit(exitcode);
311 }
312
313 static void
reset_options(struct options * opt)314 reset_options(struct options *opt)
315 {
316 if (opt->buffer)
317 free(opt->buffer);
318 memset(opt, 0, sizeof(*opt));
319 }
320
321 static void
flush_command(struct libusb20_backend * pbe,struct options * opt)322 flush_command(struct libusb20_backend *pbe, struct options *opt)
323 {
324 struct libusb20_device *pdev = NULL;
325 uint32_t matches = 0;
326 uint8_t dump_any;
327
328 /* check for invalid option combinations */
329 if ((opt->got_suspend +
330 opt->got_resume +
331 opt->got_reset +
332 opt->got_set_config +
333 opt->got_set_alt +
334 opt->got_power_save +
335 opt->got_power_on +
336 opt->got_power_off) > 1) {
337 err(1, "can only specify one of 'set_config', "
338 "'set_alt', 'reset', 'suspend', 'resume', "
339 "'power_save', 'power_on' and 'power_off' "
340 "at the same time!");
341 }
342 if (opt->got_dump_quirk_names) {
343 opt->got_any--;
344 dump_be_quirk_names(pbe);
345 }
346 if (opt->got_dump_device_quirks) {
347 opt->got_any--;
348 dump_be_dev_quirks(pbe);
349 }
350 if (opt->got_remove_device_quirk) {
351 opt->got_any--;
352 be_dev_remove_quirk(pbe,
353 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
354 }
355 if (opt->got_add_device_quirk) {
356 opt->got_any--;
357 be_dev_add_quirk(pbe,
358 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
359 }
360 if (opt->got_set_template) {
361 opt->got_any--;
362 if (libusb20_be_set_template(pbe, opt->template)) {
363 fprintf(stderr, "Setting USB template %u failed, "
364 "continuing.\n", opt->template);
365 }
366 }
367 if (opt->got_get_template) {
368 opt->got_any--;
369 if (libusb20_be_get_template(pbe, &opt->template))
370 printf("USB template: <unknown>\n");
371 else
372 printf("USB template: %u\n", opt->template);
373 }
374 if (opt->got_any == 0) {
375 /*
376 * do not scan through all the devices if there are no valid
377 * options
378 */
379 goto done;
380 }
381 while ((pdev = libusb20_be_device_foreach(pbe, pdev))) {
382
383 if (opt->got_bus &&
384 (libusb20_dev_get_bus_number(pdev) != opt->bus)) {
385 continue;
386 }
387 if (opt->got_addr &&
388 (libusb20_dev_get_address(pdev) != opt->addr)) {
389 continue;
390 }
391 matches++;
392
393 if (opt->got_remove_quirk) {
394 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
395
396 ddesc = libusb20_dev_get_device_desc(pdev);
397
398 be_dev_remove_quirk(pbe,
399 ddesc->idVendor, ddesc->idProduct,
400 ddesc->bcdDevice, ddesc->bcdDevice,
401 opt->quirkname);
402 }
403
404 if (opt->got_add_quirk) {
405 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
406
407 ddesc = libusb20_dev_get_device_desc(pdev);
408
409 be_dev_add_quirk(pbe,
410 ddesc->idVendor, ddesc->idProduct,
411 ddesc->bcdDevice, ddesc->bcdDevice,
412 opt->quirkname);
413 }
414
415 if (libusb20_dev_open(pdev, 0)) {
416 err(1, "could not open device");
417 }
418 if (opt->got_dump_string) {
419 dump_string_by_index(pdev, opt->string_index);
420 }
421 if (opt->got_do_request) {
422 uint16_t actlen;
423 uint16_t t;
424
425 if (libusb20_dev_request_sync(pdev, &opt->setup,
426 opt->buffer, &actlen, 5000 /* 5 seconds */ , 0)) {
427 printf("REQUEST = <ERROR>\n");
428 } else if (!(opt->setup.bmRequestType &
429 LIBUSB20_ENDPOINT_IN)) {
430 printf("REQUEST = <OK>\n");
431 } else {
432 t = actlen;
433 printf("REQUEST = <");
434 for (t = 0; t != actlen; t++) {
435 printf("0x%02x%s",
436 ((uint8_t *)opt->buffer)[t],
437 (t == (actlen - 1)) ? "" : " ");
438 }
439 printf("><");
440 for (t = 0; t != actlen; t++) {
441 char c;
442
443 c = ((uint8_t *)opt->buffer)[t];
444 if ((c != '<') &&
445 (c != '>') && isprint(c)) {
446 putchar(c);
447 }
448 }
449 printf(">\n");
450 }
451 }
452 if (opt->got_set_config) {
453 if (libusb20_dev_set_config_index(pdev,
454 opt->config_index)) {
455 err(1, "could not set config index");
456 }
457 }
458 if (opt->got_set_alt) {
459 if (libusb20_dev_set_alt_index(pdev, opt->iface,
460 opt->alt_index)) {
461 err(1, "could not set alternate setting");
462 }
463 }
464 if (opt->got_reset) {
465 if (libusb20_dev_reset(pdev)) {
466 err(1, "could not reset device");
467 }
468 }
469 if (opt->got_suspend) {
470 if (libusb20_dev_set_power_mode(pdev,
471 LIBUSB20_POWER_SUSPEND)) {
472 err(1, "could not set suspend");
473 }
474 }
475 if (opt->got_resume) {
476 if (libusb20_dev_set_power_mode(pdev,
477 LIBUSB20_POWER_RESUME)) {
478 err(1, "could not set resume");
479 }
480 }
481 if (opt->got_power_off) {
482 if (libusb20_dev_set_power_mode(pdev,
483 LIBUSB20_POWER_OFF)) {
484 err(1, "could not set power OFF");
485 }
486 }
487 if (opt->got_power_save) {
488 if (libusb20_dev_set_power_mode(pdev,
489 LIBUSB20_POWER_SAVE)) {
490 err(1, "could not set power SAVE");
491 }
492 }
493 if (opt->got_power_on) {
494 if (libusb20_dev_set_power_mode(pdev,
495 LIBUSB20_POWER_ON)) {
496 err(1, "could not set power ON");
497 }
498 }
499 if (opt->got_detach_kernel_driver) {
500 if (libusb20_dev_detach_kernel_driver(pdev, opt->iface)) {
501 err(1, "could not detach kernel driver");
502 }
503 }
504 if (opt->got_attach_kernel_driver) {
505 if (libusb20_dev_attach_kernel_driver(pdev, opt->iface)) {
506 err(1, "could not attach kernel driver");
507 }
508 }
509 dump_any =
510 (opt->got_dump_all_desc ||
511 opt->got_dump_device_desc ||
512 opt->got_dump_curr_config ||
513 opt->got_dump_all_config ||
514 opt->got_dump_info ||
515 opt->got_dump_stats);
516
517 if (opt->got_list || dump_any) {
518 dump_device_info(pdev,
519 opt->got_show_iface_driver,
520 opt->opt_dump_in_list_mode && opt->got_dump_device_desc);
521 }
522 if (opt->got_dump_device_desc) {
523 if (!opt->opt_dump_in_list_mode)
524 printf("\n");
525 dump_device_desc(pdev, opt->opt_dump_in_list_mode);
526 }
527 if (opt->got_dump_all_config) {
528 printf("\n");
529 dump_config(pdev, 1);
530 } else if (opt->got_dump_curr_config) {
531 printf("\n");
532 dump_config(pdev, 0);
533 } else if (opt->got_dump_all_desc) {
534 printf("\n");
535 dump_device_desc(pdev, false);
536 dump_config(pdev, 1);
537 }
538 if (opt->got_dump_stats) {
539 printf("\n");
540 dump_device_stats(pdev);
541 }
542 if (dump_any && !opt->opt_dump_in_list_mode) {
543 printf("\n");
544 }
545 if (libusb20_dev_close(pdev)) {
546 err(1, "could not close device");
547 }
548 }
549
550 if (matches == 0) {
551 printf("No device match or lack of permissions.\n");
552 }
553 done:
554 reset_options(opt);
555 }
556
557 int
main(int argc,char ** argv)558 main(int argc, char **argv)
559 {
560 struct libusb20_backend *pbe;
561 struct options *opt = &options;
562 const char *ptr;
563 int unit;
564 int addr;
565 int n;
566 int t;
567 int ch;
568
569 if (argc < 1) {
570 usage(EX_USAGE);
571 }
572 pbe = libusb20_be_alloc_default(NULL);
573 if (pbe == NULL)
574 err(1, "could not access USB backend\n");
575
576 /* The USB vendor database is looked up by pathname. */
577 dump_vendors_init();
578
579 /*
580 * The backend has cached the descriptors needed to reach the
581 * USB devices, so the rest can run in capability mode.
582 */
583 if (caph_enter() < 0)
584 err(1, "caph_enter() failed");
585
586 while ((ch = getopt(argc, argv, "a:d:hi:lu:v")) != -1) {
587 switch (ch) {
588 case 'a':
589 opt->addr = num_id(optarg, "addr");
590 opt->got_addr = 1;
591 break;
592
593 case 'd':
594 if (strncmp(optarg, "ugen", strlen("ugen")) == 0) {
595 ptr = optarg + strlen("ugen");
596 } else if (strncmp(optarg, "/dev/ugen",
597 strlen("/dev/ugen")) == 0) {
598 ptr = optarg + strlen("/dev/ugen");
599 } else {
600 ptr = optarg;
601 }
602 if ((sscanf(ptr, "%d.%d",
603 &unit, &addr) != 2) ||
604 (unit < 0) || (unit > 65535) ||
605 (addr < 0) || (addr > 65535)) {
606 errx(1, "cannot "
607 "parse '%s'", optarg);
608 }
609 opt->bus = unit;
610 opt->addr = addr;
611 opt->got_bus = 1;
612 opt->got_addr = 1;
613 break;
614
615 case 'h':
616 usage(EX_OK);
617 break;
618
619 case 'i':
620 opt->iface = num_id(optarg, "iface");
621 break;
622
623 case 'l':
624 opt->opt_dump_in_list_mode = 1;
625 break;
626
627 case 'u':
628 opt->bus = num_id(optarg, "busnum");
629 opt->got_bus = 1;
630 break;
631
632 case 'v':
633 opt->got_dump_device_desc = 1;
634 opt->got_dump_curr_config = 1;
635 opt->got_show_iface_driver = 1;
636 opt->got_any += 2; /* only the dump options count */
637 break;
638
639 default:
640 usage(EX_USAGE);
641 }
642 }
643 argc -= optind;
644 argv += optind;
645
646 for (n = 0; n != argc; n++) {
647
648 /* get number of additional options */
649 t = (argc - n - 1);
650 if (t > 255)
651 t = 255;
652 switch (get_token(argv[n], t)) {
653 case T_ADD_QUIRK:
654 if (opt->got_add_quirk) {
655 flush_command(pbe, opt);
656 }
657 opt->quirkname = argv[n + 1];
658 n++;
659
660 opt->got_add_quirk = 1;
661 opt->got_any++;
662 break;
663
664 case T_REMOVE_QUIRK:
665 if (opt->got_remove_quirk) {
666 flush_command(pbe, opt);
667 }
668 opt->quirkname = argv[n + 1];
669 n++;
670
671 opt->got_remove_quirk = 1;
672 opt->got_any++;
673 break;
674
675 case T_ADD_DEVICE_QUIRK:
676 if (opt->got_add_device_quirk) {
677 flush_command(pbe, opt);
678 }
679 opt->vid = num_id(argv[n + 1], "Vendor ID");
680 opt->pid = num_id(argv[n + 2], "Product ID");
681 opt->lo_rev = num_id(argv[n + 3], "Low Revision");
682 opt->hi_rev = num_id(argv[n + 4], "High Revision");
683 opt->quirkname = argv[n + 5];
684 n += 5;
685
686 opt->got_add_device_quirk = 1;
687 opt->got_any++;
688 break;
689
690 case T_REMOVE_DEVICE_QUIRK:
691 if (opt->got_remove_device_quirk) {
692 flush_command(pbe, opt);
693 }
694 opt->vid = num_id(argv[n + 1], "Vendor ID");
695 opt->pid = num_id(argv[n + 2], "Product ID");
696 opt->lo_rev = num_id(argv[n + 3], "Low Revision");
697 opt->hi_rev = num_id(argv[n + 4], "High Revision");
698 opt->quirkname = argv[n + 5];
699 n += 5;
700 opt->got_remove_device_quirk = 1;
701 opt->got_any++;
702 break;
703
704 case T_DETACH_KERNEL_DRIVER:
705 if (opt->got_detach_kernel_driver)
706 duplicate_option(argv[n]);
707 opt->got_detach_kernel_driver = 1;
708 opt->got_any++;
709 break;
710
711 case T_ATTACH_KERNEL_DRIVER:
712 if (opt->got_attach_kernel_driver)
713 duplicate_option(argv[n]);
714 opt->got_attach_kernel_driver = 1;
715 opt->got_any++;
716 break;
717
718 case T_DUMP_QUIRK_NAMES:
719 if (opt->got_dump_quirk_names)
720 duplicate_option(argv[n]);
721 opt->got_dump_quirk_names = 1;
722 opt->got_any++;
723 break;
724
725 case T_DUMP_DEVICE_QUIRKS:
726 if (opt->got_dump_device_quirks)
727 duplicate_option(argv[n]);
728 opt->got_dump_device_quirks = 1;
729 opt->got_any++;
730 break;
731
732 case T_SHOW_IFACE_DRIVER:
733 opt->got_show_iface_driver = 1;
734 break;
735
736 case T_SET_CONFIG:
737 if (opt->got_set_config)
738 duplicate_option(argv[n]);
739 opt->config_index = num_id(argv[n + 1], "cfg_index");
740 opt->got_set_config = 1;
741 opt->got_any++;
742 n++;
743 break;
744 case T_SET_ALT:
745 if (opt->got_set_alt)
746 duplicate_option(argv[n]);
747 opt->alt_index = num_id(argv[n + 1], "cfg_index");
748 opt->got_set_alt = 1;
749 opt->got_any++;
750 n++;
751 break;
752 case T_SET_TEMPLATE:
753 if (opt->got_set_template)
754 duplicate_option(argv[n]);
755 opt->template = get_int(argv[n + 1]);
756 opt->got_set_template = 1;
757 opt->got_any++;
758 n++;
759 break;
760 case T_GET_TEMPLATE:
761 if (opt->got_get_template)
762 duplicate_option(argv[n]);
763 opt->got_get_template = 1;
764 opt->got_any++;
765 break;
766 case T_DUMP_ALL_DESC:
767 if (opt->got_dump_all_desc)
768 duplicate_option(argv[n]);
769 opt->got_dump_all_desc = 1;
770 opt->got_any++;
771 break;
772 case T_DUMP_DEVICE_DESC:
773 if (opt->got_dump_device_desc)
774 duplicate_option(argv[n]);
775 opt->got_dump_device_desc = 1;
776 opt->got_any++;
777 break;
778 case T_DUMP_CURR_CONFIG_DESC:
779 if (opt->got_dump_curr_config)
780 duplicate_option(argv[n]);
781 opt->got_dump_curr_config = 1;
782 opt->got_any++;
783 break;
784 case T_DUMP_ALL_CONFIG_DESC:
785 if (opt->got_dump_all_config)
786 duplicate_option(argv[n]);
787 opt->got_dump_all_config = 1;
788 opt->got_any++;
789 break;
790 case T_DUMP_INFO:
791 if (opt->got_dump_info)
792 duplicate_option(argv[n]);
793 opt->got_dump_info = 1;
794 opt->got_any++;
795 break;
796 case T_DUMP_STATS:
797 if (opt->got_dump_stats)
798 duplicate_option(argv[n]);
799 opt->got_dump_stats = 1;
800 opt->got_any++;
801 break;
802 case T_DUMP_STRING:
803 if (opt->got_dump_string)
804 duplicate_option(argv[n]);
805 opt->string_index = num_id(argv[n + 1], "str_index");
806 opt->got_dump_string = 1;
807 opt->got_any++;
808 n++;
809 break;
810 case T_SUSPEND:
811 if (opt->got_suspend)
812 duplicate_option(argv[n]);
813 opt->got_suspend = 1;
814 opt->got_any++;
815 break;
816 case T_RESUME:
817 if (opt->got_resume)
818 duplicate_option(argv[n]);
819 opt->got_resume = 1;
820 opt->got_any++;
821 break;
822 case T_POWER_OFF:
823 if (opt->got_power_off)
824 duplicate_option(argv[n]);
825 opt->got_power_off = 1;
826 opt->got_any++;
827 break;
828 case T_POWER_SAVE:
829 if (opt->got_power_save)
830 duplicate_option(argv[n]);
831 opt->got_power_save = 1;
832 opt->got_any++;
833 break;
834 case T_POWER_ON:
835 if (opt->got_power_on)
836 duplicate_option(argv[n]);
837 opt->got_power_on = 1;
838 opt->got_any++;
839 break;
840 case T_RESET:
841 if (opt->got_reset)
842 duplicate_option(argv[n]);
843 opt->got_reset = 1;
844 opt->got_any++;
845 break;
846 case T_LIST:
847 if (opt->got_list)
848 duplicate_option(argv[n]);
849 opt->got_list = 1;
850 opt->got_any++;
851 break;
852 case T_DO_REQUEST:
853 if (opt->got_do_request)
854 duplicate_option(argv[n]);
855 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &opt->setup);
856 opt->setup.bmRequestType = num_id(argv[n + 1], "bmReqTyp");
857 opt->setup.bRequest = num_id(argv[n + 2], "bReq");
858 opt->setup.wValue = num_id(argv[n + 3], "wVal");
859 opt->setup.wIndex = num_id(argv[n + 4], "wIndex");
860 opt->setup.wLength = num_id(argv[n + 5], "wLen");
861 if (opt->setup.wLength != 0) {
862 opt->buffer = malloc(opt->setup.wLength);
863 } else {
864 opt->buffer = NULL;
865 }
866
867 n += 5;
868
869 if (!(opt->setup.bmRequestType &
870 LIBUSB20_ENDPOINT_IN)) {
871 /* copy in data */
872 t = (argc - n - 1);
873 if (t < opt->setup.wLength) {
874 err(1, "request data missing");
875 }
876 t = opt->setup.wLength;
877 while (t--) {
878 ((uint8_t *)opt->buffer)[t] =
879 num_id(argv[n + t + 1], "req_data");
880 }
881 n += opt->setup.wLength;
882 }
883 opt->got_do_request = 1;
884 opt->got_any++;
885 break;
886 default:
887 if (n == 1) {
888 ptr = argv[n];
889
890 if ((ptr[0] == 'u') &&
891 (ptr[1] == 'g') &&
892 (ptr[2] == 'e') &&
893 (ptr[3] == 'n'))
894 ptr += 4;
895
896 if ((sscanf(ptr, "%d.%d",
897 &unit, &addr) != 2) ||
898 (unit < 0) || (unit > 65535) ||
899 (addr < 0) || (addr > 65535)) {
900 usage(EX_USAGE);
901 break;
902 }
903
904 opt->bus = unit;
905 opt->addr = addr;
906 opt->got_bus = 1;
907 opt->got_addr = 1;
908 break;
909 }
910 usage(EX_USAGE);
911 break;
912 }
913 }
914 if (opt->got_any) {
915 /* flush out last command */
916 flush_command(pbe, opt);
917 } else {
918 /* list all the devices */
919 opt->got_list = 1;
920 opt->got_any++;
921 flush_command(pbe, opt);
922 }
923 /* release data */
924 libusb20_be_free(pbe);
925
926 return (0);
927 }
928