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