usbconfig.c revision 356404
1/* $FreeBSD: stable/10/usr.sbin/usbconfig/usbconfig.c 356404 2020-01-06 09:37:13Z hselasky $ */ 2/*- 3 * Copyright (c) 2008-2009 Hans Petter Selasky. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <stdio.h> 28#include <stdlib.h> 29#include <stdint.h> 30#include <err.h> 31#include <string.h> 32#include <pwd.h> 33#include <grp.h> 34#include <errno.h> 35#include <ctype.h> 36#include <sys/types.h> 37 38#include <libusb20_desc.h> 39#include <libusb20.h> 40 41#include "dump.h" 42 43struct options { 44 const char *quirkname; 45 void *buffer; 46 int template; 47 gid_t gid; 48 uid_t uid; 49 mode_t mode; 50 uint32_t got_any; 51 struct LIBUSB20_CONTROL_SETUP_DECODED setup; 52 uint16_t bus; 53 uint16_t addr; 54 uint16_t iface; 55 uint16_t vid; 56 uint16_t pid; 57 uint16_t lo_rev; /* inclusive */ 58 uint16_t hi_rev; /* inclusive */ 59 uint8_t string_index; 60 uint8_t config_index; 61 uint8_t alt_index; 62 uint8_t got_list:1; 63 uint8_t got_bus:1; 64 uint8_t got_addr:1; 65 uint8_t got_iface:1; 66 uint8_t got_set_config:1; 67 uint8_t got_set_alt:1; 68 uint8_t got_set_template:1; 69 uint8_t got_get_template:1; 70 uint8_t got_suspend:1; 71 uint8_t got_resume:1; 72 uint8_t got_reset:1; 73 uint8_t got_power_off:1; 74 uint8_t got_power_save:1; 75 uint8_t got_power_on:1; 76 uint8_t got_dump_device_quirks:1; 77 uint8_t got_dump_quirk_names:1; 78 uint8_t got_dump_all_desc:1; 79 uint8_t got_dump_device_desc:1; 80 uint8_t got_dump_curr_config:1; 81 uint8_t got_dump_all_config:1; 82 uint8_t got_dump_info:1; 83 uint8_t got_show_iface_driver:1; 84 uint8_t got_remove_device_quirk:1; 85 uint8_t got_add_device_quirk:1; 86 uint8_t got_remove_quirk:1; 87 uint8_t got_add_quirk:1; 88 uint8_t got_dump_string:1; 89 uint8_t got_do_request:1; 90}; 91 92struct token { 93 const char *name; 94 uint8_t value; 95 uint8_t narg; 96}; 97 98enum { 99 T_UNIT, 100 T_ADDR, 101 T_UGEN, 102 T_IFACE, 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_DUMP_QUIRK_NAMES, 113 T_DUMP_DEVICE_QUIRKS, 114 T_DUMP_ALL_DESC, 115 T_DUMP_DEVICE_DESC, 116 T_DUMP_CURR_CONFIG_DESC, 117 T_DUMP_ALL_CONFIG_DESC, 118 T_DUMP_STRING, 119 T_DUMP_INFO, 120 T_SUSPEND, 121 T_RESUME, 122 T_POWER_OFF, 123 T_POWER_SAVE, 124 T_POWER_ON, 125 T_RESET, 126 T_LIST, 127 T_DO_REQUEST, 128}; 129 130static struct options options; 131 132static const struct token token[] = { 133 {"-u", T_UNIT, 1}, 134 {"-a", T_ADDR, 1}, 135 {"-d", T_UGEN, 1}, 136 {"-i", T_IFACE, 1}, 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 {"dump_quirk_names", T_DUMP_QUIRK_NAMES, 0}, 146 {"dump_device_quirks", T_DUMP_DEVICE_QUIRKS, 0}, 147 {"dump_all_desc", T_DUMP_ALL_DESC, 0}, 148 {"dump_device_desc", T_DUMP_DEVICE_DESC, 0}, 149 {"dump_curr_config_desc", T_DUMP_CURR_CONFIG_DESC, 0}, 150 {"dump_all_config_desc", T_DUMP_ALL_CONFIG_DESC, 0}, 151 {"dump_string", T_DUMP_STRING, 1}, 152 {"dump_info", T_DUMP_INFO, 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 164static void 165be_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 187static void 188be_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 210static uint8_t 211get_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 227static uid_t 228num_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 244static int 245get_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 261static void 262duplicate_option(const char *ptr) 263{ 264 fprintf(stderr, "Syntax error: " 265 "Duplicate option: '%s'\n", ptr); 266 exit(1); 267} 268 269static void 270usage(void) 271{ 272 fprintf(stderr, "" 273 "usbconfig - configure the USB subsystem" "\n" 274 "usage: usbconfig -u <busnum> -a <devaddr> -i <ifaceindex> [cmds...]" "\n" 275 "usage: usbconfig -d [ugen]<busnum>.<devaddr> -i <ifaceindex> [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 " dump_quirk_names" "\n" 286 " dump_device_quirks" "\n" 287 " dump_all_desc" "\n" 288 " dump_device_desc" "\n" 289 " dump_curr_config_desc" "\n" 290 " dump_all_config_desc" "\n" 291 " dump_string <index>" "\n" 292 " dump_info" "\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(1); 304} 305 306static void 307reset_options(struct options *opt) 308{ 309 if (opt->buffer) 310 free(opt->buffer); 311 memset(opt, 0, sizeof(*opt)); 312 return; 313} 314 315static void 316flush_command(struct libusb20_backend *pbe, struct options *opt) 317{ 318 struct libusb20_device *pdev = NULL; 319 uint32_t matches = 0; 320 uint8_t dump_any; 321 322 /* check for invalid option combinations */ 323 if ((opt->got_suspend + 324 opt->got_resume + 325 opt->got_reset + 326 opt->got_set_config + 327 opt->got_set_alt + 328 opt->got_power_save + 329 opt->got_power_on + 330 opt->got_power_off) > 1) { 331 err(1, "can only specify one of 'set_config', " 332 "'set_alt', 'reset', 'suspend', 'resume', " 333 "'power_save', 'power_on' and 'power_off' " 334 "at the same time!"); 335 } 336 if (opt->got_dump_quirk_names) { 337 opt->got_any--; 338 dump_be_quirk_names(pbe); 339 } 340 if (opt->got_dump_device_quirks) { 341 opt->got_any--; 342 dump_be_dev_quirks(pbe); 343 } 344 if (opt->got_remove_device_quirk) { 345 opt->got_any--; 346 be_dev_remove_quirk(pbe, 347 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname); 348 } 349 if (opt->got_add_device_quirk) { 350 opt->got_any--; 351 be_dev_add_quirk(pbe, 352 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname); 353 } 354 if (opt->got_set_template) { 355 opt->got_any--; 356 if (libusb20_be_set_template(pbe, opt->template)) { 357 fprintf(stderr, "Setting USB template %u failed, " 358 "continuing.\n", opt->template); 359 } 360 } 361 if (opt->got_get_template) { 362 opt->got_any--; 363 if (libusb20_be_get_template(pbe, &opt->template)) 364 printf("USB template: <unknown>\n"); 365 else 366 printf("USB template: %u\n", opt->template); 367 } 368 if (opt->got_any == 0) { 369 /* 370 * do not scan through all the devices if there are no valid 371 * options 372 */ 373 goto done; 374 } 375 while ((pdev = libusb20_be_device_foreach(pbe, pdev))) { 376 377 if (opt->got_bus && 378 (libusb20_dev_get_bus_number(pdev) != opt->bus)) { 379 continue; 380 } 381 if (opt->got_addr && 382 (libusb20_dev_get_address(pdev) != opt->addr)) { 383 continue; 384 } 385 matches++; 386 387 if (opt->got_remove_quirk) { 388 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc; 389 390 ddesc = libusb20_dev_get_device_desc(pdev); 391 392 be_dev_remove_quirk(pbe, 393 ddesc->idVendor, ddesc->idProduct, 394 ddesc->bcdDevice, ddesc->bcdDevice, 395 opt->quirkname); 396 } 397 398 if (opt->got_add_quirk) { 399 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc; 400 401 ddesc = libusb20_dev_get_device_desc(pdev); 402 403 be_dev_add_quirk(pbe, 404 ddesc->idVendor, ddesc->idProduct, 405 ddesc->bcdDevice, ddesc->bcdDevice, 406 opt->quirkname); 407 } 408 409 if (libusb20_dev_open(pdev, 0)) { 410 err(1, "could not open device"); 411 } 412 if (opt->got_dump_string) { 413 dump_string_by_index(pdev, opt->string_index); 414 } 415 if (opt->got_do_request) { 416 uint16_t actlen; 417 uint16_t t; 418 419 if (libusb20_dev_request_sync(pdev, &opt->setup, 420 opt->buffer, &actlen, 5000 /* 5 seconds */ , 0)) { 421 printf("REQUEST = <ERROR>\n"); 422 } else if (!(opt->setup.bmRequestType & 423 LIBUSB20_ENDPOINT_IN)) { 424 printf("REQUEST = <OK>\n"); 425 } else { 426 t = actlen; 427 printf("REQUEST = <"); 428 for (t = 0; t != actlen; t++) { 429 printf("0x%02x%s", 430 ((uint8_t *)opt->buffer)[t], 431 (t == (actlen - 1)) ? "" : " "); 432 } 433 printf("><"); 434 for (t = 0; t != actlen; t++) { 435 char c; 436 437 c = ((uint8_t *)opt->buffer)[t]; 438 if ((c != '<') && 439 (c != '>') && isprint(c)) { 440 putchar(c); 441 } 442 } 443 printf(">\n"); 444 } 445 } 446 if (opt->got_set_config) { 447 if (libusb20_dev_set_config_index(pdev, 448 opt->config_index)) { 449 err(1, "could not set config index"); 450 } 451 } 452 if (opt->got_set_alt) { 453 if (libusb20_dev_set_alt_index(pdev, opt->iface, 454 opt->alt_index)) { 455 err(1, "could not set alternate setting"); 456 } 457 } 458 if (opt->got_reset) { 459 if (libusb20_dev_reset(pdev)) { 460 err(1, "could not reset device"); 461 } 462 } 463 if (opt->got_suspend) { 464 if (libusb20_dev_set_power_mode(pdev, 465 LIBUSB20_POWER_SUSPEND)) { 466 err(1, "could not set suspend"); 467 } 468 } 469 if (opt->got_resume) { 470 if (libusb20_dev_set_power_mode(pdev, 471 LIBUSB20_POWER_RESUME)) { 472 err(1, "could not set resume"); 473 } 474 } 475 if (opt->got_power_off) { 476 if (libusb20_dev_set_power_mode(pdev, 477 LIBUSB20_POWER_OFF)) { 478 err(1, "could not set power OFF"); 479 } 480 } 481 if (opt->got_power_save) { 482 if (libusb20_dev_set_power_mode(pdev, 483 LIBUSB20_POWER_SAVE)) { 484 err(1, "could not set power SAVE"); 485 } 486 } 487 if (opt->got_power_on) { 488 if (libusb20_dev_set_power_mode(pdev, 489 LIBUSB20_POWER_ON)) { 490 err(1, "could not set power ON"); 491 } 492 } 493 dump_any = 494 (opt->got_dump_all_desc || 495 opt->got_dump_device_desc || 496 opt->got_dump_curr_config || 497 opt->got_dump_all_config || 498 opt->got_dump_info); 499 500 if (opt->got_list || dump_any) { 501 dump_device_info(pdev, 502 opt->got_show_iface_driver); 503 } 504 if (opt->got_dump_device_desc) { 505 printf("\n"); 506 dump_device_desc(pdev); 507 } 508 if (opt->got_dump_all_config) { 509 printf("\n"); 510 dump_config(pdev, 1); 511 } else if (opt->got_dump_curr_config) { 512 printf("\n"); 513 dump_config(pdev, 0); 514 } else if (opt->got_dump_all_desc) { 515 printf("\n"); 516 dump_device_desc(pdev); 517 dump_config(pdev, 1); 518 } 519 if (dump_any) { 520 printf("\n"); 521 } 522 if (libusb20_dev_close(pdev)) { 523 err(1, "could not close device"); 524 } 525 } 526 527 if (matches == 0) { 528 printf("No device match or lack of permissions.\n"); 529 } 530done: 531 reset_options(opt); 532 533 return; 534} 535 536int 537main(int argc, char **argv) 538{ 539 struct libusb20_backend *pbe; 540 struct options *opt = &options; 541 const char *ptr; 542 int unit; 543 int addr; 544 int n; 545 int t; 546 547 if (argc < 1) { 548 usage(); 549 } 550 pbe = libusb20_be_alloc_default(); 551 if (pbe == NULL) 552 err(1, "could not access USB backend\n"); 553 554 for (n = 1; n != argc; n++) { 555 556 /* get number of additional options */ 557 t = (argc - n - 1); 558 if (t > 255) 559 t = 255; 560 switch (get_token(argv[n], t)) { 561 case T_ADD_QUIRK: 562 if (opt->got_add_quirk) { 563 flush_command(pbe, opt); 564 } 565 opt->quirkname = argv[n + 1]; 566 n++; 567 568 opt->got_add_quirk = 1; 569 opt->got_any++; 570 break; 571 572 case T_REMOVE_QUIRK: 573 if (opt->got_remove_quirk) { 574 flush_command(pbe, opt); 575 } 576 opt->quirkname = argv[n + 1]; 577 n++; 578 579 opt->got_remove_quirk = 1; 580 opt->got_any++; 581 break; 582 583 case T_ADD_DEVICE_QUIRK: 584 if (opt->got_add_device_quirk) { 585 flush_command(pbe, opt); 586 } 587 opt->vid = num_id(argv[n + 1], "Vendor ID"); 588 opt->pid = num_id(argv[n + 2], "Product ID"); 589 opt->lo_rev = num_id(argv[n + 3], "Low Revision"); 590 opt->hi_rev = num_id(argv[n + 4], "High Revision"); 591 opt->quirkname = argv[n + 5]; 592 n += 5; 593 594 opt->got_add_device_quirk = 1; 595 opt->got_any++; 596 break; 597 598 case T_REMOVE_DEVICE_QUIRK: 599 if (opt->got_remove_device_quirk) { 600 flush_command(pbe, opt); 601 } 602 opt->vid = num_id(argv[n + 1], "Vendor ID"); 603 opt->pid = num_id(argv[n + 2], "Product ID"); 604 opt->lo_rev = num_id(argv[n + 3], "Low Revision"); 605 opt->hi_rev = num_id(argv[n + 4], "High Revision"); 606 opt->quirkname = argv[n + 5]; 607 n += 5; 608 opt->got_remove_device_quirk = 1; 609 opt->got_any++; 610 break; 611 612 case T_DUMP_QUIRK_NAMES: 613 if (opt->got_dump_quirk_names) 614 duplicate_option(argv[n]); 615 opt->got_dump_quirk_names = 1; 616 opt->got_any++; 617 break; 618 619 case T_DUMP_DEVICE_QUIRKS: 620 if (opt->got_dump_device_quirks) 621 duplicate_option(argv[n]); 622 opt->got_dump_device_quirks = 1; 623 opt->got_any++; 624 break; 625 626 case T_SHOW_IFACE_DRIVER: 627 opt->got_show_iface_driver = 1; 628 break; 629 630 case T_UGEN: 631 if (opt->got_any) { 632 /* allow multiple commands on the same line */ 633 flush_command(pbe, opt); 634 } 635 ptr = argv[n + 1]; 636 637 if ((ptr[0] == 'u') && 638 (ptr[1] == 'g') && 639 (ptr[2] == 'e') && 640 (ptr[3] == 'n')) 641 ptr += 4; 642 643 if ((sscanf(ptr, "%d.%d", 644 &unit, &addr) != 2) || 645 (unit < 0) || (unit > 65535) || 646 (addr < 0) || (addr > 65535)) { 647 errx(1, "cannot " 648 "parse '%s'", argv[n + 1]); 649 } 650 opt->bus = unit; 651 opt->addr = addr; 652 opt->got_bus = 1; 653 opt->got_addr = 1; 654 n++; 655 break; 656 657 case T_UNIT: 658 if (opt->got_any) { 659 /* allow multiple commands on the same line */ 660 flush_command(pbe, opt); 661 } 662 opt->bus = num_id(argv[n + 1], "busnum"); 663 opt->got_bus = 1; 664 n++; 665 break; 666 case T_ADDR: 667 opt->addr = num_id(argv[n + 1], "addr"); 668 opt->got_addr = 1; 669 n++; 670 break; 671 case T_IFACE: 672 opt->iface = num_id(argv[n + 1], "iface"); 673 opt->got_iface = 1; 674 n++; 675 break; 676 case T_SET_CONFIG: 677 if (opt->got_set_config) 678 duplicate_option(argv[n]); 679 opt->config_index = num_id(argv[n + 1], "cfg_index"); 680 opt->got_set_config = 1; 681 opt->got_any++; 682 n++; 683 break; 684 case T_SET_ALT: 685 if (opt->got_set_alt) 686 duplicate_option(argv[n]); 687 opt->alt_index = num_id(argv[n + 1], "cfg_index"); 688 opt->got_set_alt = 1; 689 opt->got_any++; 690 n++; 691 break; 692 case T_SET_TEMPLATE: 693 if (opt->got_set_template) 694 duplicate_option(argv[n]); 695 opt->template = get_int(argv[n + 1]); 696 opt->got_set_template = 1; 697 opt->got_any++; 698 n++; 699 break; 700 case T_GET_TEMPLATE: 701 if (opt->got_get_template) 702 duplicate_option(argv[n]); 703 opt->got_get_template = 1; 704 opt->got_any++; 705 break; 706 case T_DUMP_ALL_DESC: 707 if (opt->got_dump_all_desc) 708 duplicate_option(argv[n]); 709 opt->got_dump_all_desc = 1; 710 opt->got_any++; 711 break; 712 case T_DUMP_DEVICE_DESC: 713 if (opt->got_dump_device_desc) 714 duplicate_option(argv[n]); 715 opt->got_dump_device_desc = 1; 716 opt->got_any++; 717 break; 718 case T_DUMP_CURR_CONFIG_DESC: 719 if (opt->got_dump_curr_config) 720 duplicate_option(argv[n]); 721 opt->got_dump_curr_config = 1; 722 opt->got_any++; 723 break; 724 case T_DUMP_ALL_CONFIG_DESC: 725 if (opt->got_dump_all_config) 726 duplicate_option(argv[n]); 727 opt->got_dump_all_config = 1; 728 opt->got_any++; 729 break; 730 case T_DUMP_INFO: 731 if (opt->got_dump_info) 732 duplicate_option(argv[n]); 733 opt->got_dump_info = 1; 734 opt->got_any++; 735 break; 736 case T_DUMP_STRING: 737 if (opt->got_dump_string) 738 duplicate_option(argv[n]); 739 opt->string_index = num_id(argv[n + 1], "str_index"); 740 opt->got_dump_string = 1; 741 opt->got_any++; 742 n++; 743 break; 744 case T_SUSPEND: 745 if (opt->got_suspend) 746 duplicate_option(argv[n]); 747 opt->got_suspend = 1; 748 opt->got_any++; 749 break; 750 case T_RESUME: 751 if (opt->got_resume) 752 duplicate_option(argv[n]); 753 opt->got_resume = 1; 754 opt->got_any++; 755 break; 756 case T_POWER_OFF: 757 if (opt->got_power_off) 758 duplicate_option(argv[n]); 759 opt->got_power_off = 1; 760 opt->got_any++; 761 break; 762 case T_POWER_SAVE: 763 if (opt->got_power_save) 764 duplicate_option(argv[n]); 765 opt->got_power_save = 1; 766 opt->got_any++; 767 break; 768 case T_POWER_ON: 769 if (opt->got_power_on) 770 duplicate_option(argv[n]); 771 opt->got_power_on = 1; 772 opt->got_any++; 773 break; 774 case T_RESET: 775 if (opt->got_reset) 776 duplicate_option(argv[n]); 777 opt->got_reset = 1; 778 opt->got_any++; 779 break; 780 case T_LIST: 781 if (opt->got_list) 782 duplicate_option(argv[n]); 783 opt->got_list = 1; 784 opt->got_any++; 785 break; 786 case T_DO_REQUEST: 787 if (opt->got_do_request) 788 duplicate_option(argv[n]); 789 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &opt->setup); 790 opt->setup.bmRequestType = num_id(argv[n + 1], "bmReqTyp"); 791 opt->setup.bRequest = num_id(argv[n + 2], "bReq"); 792 opt->setup.wValue = num_id(argv[n + 3], "wVal"); 793 opt->setup.wIndex = num_id(argv[n + 4], "wIndex"); 794 opt->setup.wLength = num_id(argv[n + 5], "wLen"); 795 if (opt->setup.wLength != 0) { 796 opt->buffer = malloc(opt->setup.wLength); 797 } else { 798 opt->buffer = NULL; 799 } 800 801 n += 5; 802 803 if (!(opt->setup.bmRequestType & 804 LIBUSB20_ENDPOINT_IN)) { 805 /* copy in data */ 806 t = (argc - n - 1); 807 if (t < opt->setup.wLength) { 808 err(1, "request data missing"); 809 } 810 t = opt->setup.wLength; 811 while (t--) { 812 ((uint8_t *)opt->buffer)[t] = 813 num_id(argv[n + t + 1], "req_data"); 814 } 815 n += opt->setup.wLength; 816 } 817 opt->got_do_request = 1; 818 opt->got_any++; 819 break; 820 default: 821 usage(); 822 break; 823 } 824 } 825 if (opt->got_any) { 826 /* flush out last command */ 827 flush_command(pbe, opt); 828 } else { 829 /* list all the devices */ 830 opt->got_list = 1; 831 opt->got_any++; 832 flush_command(pbe, opt); 833 } 834 /* release data */ 835 libusb20_be_free(pbe); 836 837 return (0); 838} 839