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