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