1/* $FreeBSD: stable/10/usr.sbin/usbconfig/usbconfig.c 356408 2020-01-06 09:40:59Z 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_dump_stats:1; 84 uint8_t got_show_iface_driver:1; 85 uint8_t got_remove_device_quirk:1; 86 uint8_t got_add_device_quirk:1; 87 uint8_t got_remove_quirk:1; 88 uint8_t got_add_quirk:1; 89 uint8_t got_dump_string:1; 90 uint8_t got_do_request:1; 91 uint8_t got_detach_kernel_driver:1; 92}; 93 94struct token { 95 const char *name; 96 uint8_t value; 97 uint8_t narg; 98}; 99 100enum { 101 T_UNIT, 102 T_ADDR, 103 T_UGEN, 104 T_IFACE, 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 134static struct options options; 135 136static const struct token token[] = { 137 {"-u", T_UNIT, 1}, 138 {"-a", T_ADDR, 1}, 139 {"-d", T_UGEN, 1}, 140 {"-i", T_IFACE, 1}, 141 {"set_config", T_SET_CONFIG, 1}, 142 {"set_alt", T_SET_ALT, 1}, 143 {"set_template", T_SET_TEMPLATE, 1}, 144 {"get_template", T_GET_TEMPLATE, 0}, 145 {"add_dev_quirk_vplh", T_ADD_DEVICE_QUIRK, 5}, 146 {"remove_dev_quirk_vplh", T_REMOVE_DEVICE_QUIRK, 5}, 147 {"add_quirk", T_ADD_QUIRK, 1}, 148 {"remove_quirk", T_REMOVE_QUIRK, 1}, 149 {"detach_kernel_driver", T_DETACH_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 170static void 171be_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 return; 191} 192 193static void 194be_dev_add_quirk(struct libusb20_backend *pbe, 195 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev, 196 const char *str) 197{ 198 struct libusb20_quirk q; 199 int error; 200 201 memset(&q, 0, sizeof(q)); 202 203 q.vid = vid; 204 q.pid = pid; 205 q.bcdDeviceLow = lorev; 206 q.bcdDeviceHigh = hirev; 207 strlcpy(q.quirkname, str, sizeof(q.quirkname)); 208 209 error = libusb20_be_add_dev_quirk(pbe, &q); 210 if (error) { 211 fprintf(stderr, "Adding quirk '%s' failed, continuing.\n", str); 212 } 213 return; 214} 215 216static uint8_t 217get_token(const char *str, uint8_t narg) 218{ 219 uint8_t n; 220 221 for (n = 0; n != (sizeof(token) / sizeof(token[0])); n++) { 222 if (strcasecmp(str, token[n].name) == 0) { 223 if (token[n].narg > narg) { 224 /* too few arguments */ 225 break; 226 } 227 return (token[n].value); 228 } 229 } 230 return (0 - 1); 231} 232 233static uid_t 234num_id(const char *name, const char *type) 235{ 236 uid_t val; 237 char *ep; 238 239 errno = 0; 240 val = strtoul(name, &ep, 0); 241 if (errno) { 242 err(1, "%s", name); 243 } 244 if (*ep != '\0') { 245 errx(1, "%s: illegal %s name", name, type); 246 } 247 return (val); 248} 249 250static int 251get_int(const char *s) 252{ 253 int val; 254 char *ep; 255 256 errno = 0; 257 val = strtoul(s, &ep, 0); 258 if (errno) { 259 err(1, "%s", s); 260 } 261 if (*ep != '\0') { 262 errx(1, "illegal number: %s", s); 263 } 264 return val; 265} 266 267static void 268duplicate_option(const char *ptr) 269{ 270 fprintf(stderr, "Syntax error: " 271 "Duplicate option: '%s'\n", ptr); 272 exit(1); 273} 274 275static void 276usage(void) 277{ 278 fprintf(stderr, "" 279 "usbconfig - configure the USB subsystem" "\n" 280 "usage: usbconfig -u <busnum> -a <devaddr> -i <ifaceindex> [cmds...]" "\n" 281 "usage: usbconfig -d [ugen]<busnum>.<devaddr> -i <ifaceindex> [cmds...]" "\n" 282 "commands:" "\n" 283 " set_config <cfg_index>" "\n" 284 " set_alt <alt_index>" "\n" 285 " set_template <template>" "\n" 286 " get_template" "\n" 287 " add_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n" 288 " remove_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n" 289 " add_quirk <quirk>" "\n" 290 " remove_quirk <quirk>" "\n" 291 " detach_kernel_driver" "\n" 292 " dump_quirk_names" "\n" 293 " dump_device_quirks" "\n" 294 " dump_all_desc" "\n" 295 " dump_device_desc" "\n" 296 " dump_curr_config_desc" "\n" 297 " dump_all_config_desc" "\n" 298 " dump_string <index>" "\n" 299 " dump_info" "\n" 300 " dump_stats" "\n" 301 " show_ifdrv" "\n" 302 " suspend" "\n" 303 " resume" "\n" 304 " power_off" "\n" 305 " power_save" "\n" 306 " power_on" "\n" 307 " reset" "\n" 308 " list" "\n" 309 " do_request <bmReqTyp> <bReq> <wVal> <wIdx> <wLen> <data...>" "\n" 310 ); 311 exit(1); 312} 313 314static void 315reset_options(struct options *opt) 316{ 317 if (opt->buffer) 318 free(opt->buffer); 319 memset(opt, 0, sizeof(*opt)); 320 return; 321} 322 323static void 324flush_command(struct libusb20_backend *pbe, struct options *opt) 325{ 326 struct libusb20_device *pdev = NULL; 327 uint32_t matches = 0; 328 uint8_t dump_any; 329 330 /* check for invalid option combinations */ 331 if ((opt->got_suspend + 332 opt->got_resume + 333 opt->got_reset + 334 opt->got_set_config + 335 opt->got_set_alt + 336 opt->got_power_save + 337 opt->got_power_on + 338 opt->got_power_off) > 1) { 339 err(1, "can only specify one of 'set_config', " 340 "'set_alt', 'reset', 'suspend', 'resume', " 341 "'power_save', 'power_on' and 'power_off' " 342 "at the same time!"); 343 } 344 if (opt->got_dump_quirk_names) { 345 opt->got_any--; 346 dump_be_quirk_names(pbe); 347 } 348 if (opt->got_dump_device_quirks) { 349 opt->got_any--; 350 dump_be_dev_quirks(pbe); 351 } 352 if (opt->got_remove_device_quirk) { 353 opt->got_any--; 354 be_dev_remove_quirk(pbe, 355 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname); 356 } 357 if (opt->got_add_device_quirk) { 358 opt->got_any--; 359 be_dev_add_quirk(pbe, 360 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname); 361 } 362 if (opt->got_set_template) { 363 opt->got_any--; 364 if (libusb20_be_set_template(pbe, opt->template)) { 365 fprintf(stderr, "Setting USB template %u failed, " 366 "continuing.\n", opt->template); 367 } 368 } 369 if (opt->got_get_template) { 370 opt->got_any--; 371 if (libusb20_be_get_template(pbe, &opt->template)) 372 printf("USB template: <unknown>\n"); 373 else 374 printf("USB template: %u\n", opt->template); 375 } 376 if (opt->got_any == 0) { 377 /* 378 * do not scan through all the devices if there are no valid 379 * options 380 */ 381 goto done; 382 } 383 while ((pdev = libusb20_be_device_foreach(pbe, pdev))) { 384 385 if (opt->got_bus && 386 (libusb20_dev_get_bus_number(pdev) != opt->bus)) { 387 continue; 388 } 389 if (opt->got_addr && 390 (libusb20_dev_get_address(pdev) != opt->addr)) { 391 continue; 392 } 393 matches++; 394 395 if (opt->got_remove_quirk) { 396 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc; 397 398 ddesc = libusb20_dev_get_device_desc(pdev); 399 400 be_dev_remove_quirk(pbe, 401 ddesc->idVendor, ddesc->idProduct, 402 ddesc->bcdDevice, ddesc->bcdDevice, 403 opt->quirkname); 404 } 405 406 if (opt->got_add_quirk) { 407 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc; 408 409 ddesc = libusb20_dev_get_device_desc(pdev); 410 411 be_dev_add_quirk(pbe, 412 ddesc->idVendor, ddesc->idProduct, 413 ddesc->bcdDevice, ddesc->bcdDevice, 414 opt->quirkname); 415 } 416 417 if (libusb20_dev_open(pdev, 0)) { 418 err(1, "could not open device"); 419 } 420 if (opt->got_dump_string) { 421 dump_string_by_index(pdev, opt->string_index); 422 } 423 if (opt->got_do_request) { 424 uint16_t actlen; 425 uint16_t t; 426 427 if (libusb20_dev_request_sync(pdev, &opt->setup, 428 opt->buffer, &actlen, 5000 /* 5 seconds */ , 0)) { 429 printf("REQUEST = <ERROR>\n"); 430 } else if (!(opt->setup.bmRequestType & 431 LIBUSB20_ENDPOINT_IN)) { 432 printf("REQUEST = <OK>\n"); 433 } else { 434 t = actlen; 435 printf("REQUEST = <"); 436 for (t = 0; t != actlen; t++) { 437 printf("0x%02x%s", 438 ((uint8_t *)opt->buffer)[t], 439 (t == (actlen - 1)) ? "" : " "); 440 } 441 printf("><"); 442 for (t = 0; t != actlen; t++) { 443 char c; 444 445 c = ((uint8_t *)opt->buffer)[t]; 446 if ((c != '<') && 447 (c != '>') && isprint(c)) { 448 putchar(c); 449 } 450 } 451 printf(">\n"); 452 } 453 } 454 if (opt->got_set_config) { 455 if (libusb20_dev_set_config_index(pdev, 456 opt->config_index)) { 457 err(1, "could not set config index"); 458 } 459 } 460 if (opt->got_set_alt) { 461 if (libusb20_dev_set_alt_index(pdev, opt->iface, 462 opt->alt_index)) { 463 err(1, "could not set alternate setting"); 464 } 465 } 466 if (opt->got_reset) { 467 if (libusb20_dev_reset(pdev)) { 468 err(1, "could not reset device"); 469 } 470 } 471 if (opt->got_suspend) { 472 if (libusb20_dev_set_power_mode(pdev, 473 LIBUSB20_POWER_SUSPEND)) { 474 err(1, "could not set suspend"); 475 } 476 } 477 if (opt->got_resume) { 478 if (libusb20_dev_set_power_mode(pdev, 479 LIBUSB20_POWER_RESUME)) { 480 err(1, "could not set resume"); 481 } 482 } 483 if (opt->got_power_off) { 484 if (libusb20_dev_set_power_mode(pdev, 485 LIBUSB20_POWER_OFF)) { 486 err(1, "could not set power OFF"); 487 } 488 } 489 if (opt->got_power_save) { 490 if (libusb20_dev_set_power_mode(pdev, 491 LIBUSB20_POWER_SAVE)) { 492 err(1, "could not set power SAVE"); 493 } 494 } 495 if (opt->got_power_on) { 496 if (libusb20_dev_set_power_mode(pdev, 497 LIBUSB20_POWER_ON)) { 498 err(1, "could not set power ON"); 499 } 500 } 501 if (opt->got_detach_kernel_driver) { 502 if (libusb20_dev_detach_kernel_driver(pdev, opt->iface)) { 503 err(1, "could not detach kernel driver"); 504 } 505 } 506 dump_any = 507 (opt->got_dump_all_desc || 508 opt->got_dump_device_desc || 509 opt->got_dump_curr_config || 510 opt->got_dump_all_config || 511 opt->got_dump_info || 512 opt->got_dump_stats); 513 514 if (opt->got_list || dump_any) { 515 dump_device_info(pdev, 516 opt->got_show_iface_driver); 517 } 518 if (opt->got_dump_device_desc) { 519 printf("\n"); 520 dump_device_desc(pdev); 521 } 522 if (opt->got_dump_all_config) { 523 printf("\n"); 524 dump_config(pdev, 1); 525 } else if (opt->got_dump_curr_config) { 526 printf("\n"); 527 dump_config(pdev, 0); 528 } else if (opt->got_dump_all_desc) { 529 printf("\n"); 530 dump_device_desc(pdev); 531 dump_config(pdev, 1); 532 } 533 if (opt->got_dump_stats) { 534 printf("\n"); 535 dump_device_stats(pdev); 536 } 537 if (dump_any) { 538 printf("\n"); 539 } 540 if (libusb20_dev_close(pdev)) { 541 err(1, "could not close device"); 542 } 543 } 544 545 if (matches == 0) { 546 printf("No device match or lack of permissions.\n"); 547 } 548done: 549 reset_options(opt); 550 551 return; 552} 553 554int 555main(int argc, char **argv) 556{ 557 struct libusb20_backend *pbe; 558 struct options *opt = &options; 559 const char *ptr; 560 int unit; 561 int addr; 562 int n; 563 int t; 564 565 if (argc < 1) { 566 usage(); 567 } 568 pbe = libusb20_be_alloc_default(); 569 if (pbe == NULL) 570 err(1, "could not access USB backend\n"); 571 572 for (n = 1; n != argc; n++) { 573 574 /* get number of additional options */ 575 t = (argc - n - 1); 576 if (t > 255) 577 t = 255; 578 switch (get_token(argv[n], t)) { 579 case T_ADD_QUIRK: 580 if (opt->got_add_quirk) { 581 flush_command(pbe, opt); 582 } 583 opt->quirkname = argv[n + 1]; 584 n++; 585 586 opt->got_add_quirk = 1; 587 opt->got_any++; 588 break; 589 590 case T_REMOVE_QUIRK: 591 if (opt->got_remove_quirk) { 592 flush_command(pbe, opt); 593 } 594 opt->quirkname = argv[n + 1]; 595 n++; 596 597 opt->got_remove_quirk = 1; 598 opt->got_any++; 599 break; 600 601 case T_ADD_DEVICE_QUIRK: 602 if (opt->got_add_device_quirk) { 603 flush_command(pbe, opt); 604 } 605 opt->vid = num_id(argv[n + 1], "Vendor ID"); 606 opt->pid = num_id(argv[n + 2], "Product ID"); 607 opt->lo_rev = num_id(argv[n + 3], "Low Revision"); 608 opt->hi_rev = num_id(argv[n + 4], "High Revision"); 609 opt->quirkname = argv[n + 5]; 610 n += 5; 611 612 opt->got_add_device_quirk = 1; 613 opt->got_any++; 614 break; 615 616 case T_REMOVE_DEVICE_QUIRK: 617 if (opt->got_remove_device_quirk) { 618 flush_command(pbe, opt); 619 } 620 opt->vid = num_id(argv[n + 1], "Vendor ID"); 621 opt->pid = num_id(argv[n + 2], "Product ID"); 622 opt->lo_rev = num_id(argv[n + 3], "Low Revision"); 623 opt->hi_rev = num_id(argv[n + 4], "High Revision"); 624 opt->quirkname = argv[n + 5]; 625 n += 5; 626 opt->got_remove_device_quirk = 1; 627 opt->got_any++; 628 break; 629 630 case T_DETACH_KERNEL_DRIVER: 631 if (opt->got_detach_kernel_driver) 632 duplicate_option(argv[n]); 633 opt->got_detach_kernel_driver = 1; 634 opt->got_any++; 635 break; 636 637 case T_DUMP_QUIRK_NAMES: 638 if (opt->got_dump_quirk_names) 639 duplicate_option(argv[n]); 640 opt->got_dump_quirk_names = 1; 641 opt->got_any++; 642 break; 643 644 case T_DUMP_DEVICE_QUIRKS: 645 if (opt->got_dump_device_quirks) 646 duplicate_option(argv[n]); 647 opt->got_dump_device_quirks = 1; 648 opt->got_any++; 649 break; 650 651 case T_SHOW_IFACE_DRIVER: 652 opt->got_show_iface_driver = 1; 653 break; 654 655 case T_UGEN: 656 if (opt->got_any) { 657 /* allow multiple commands on the same line */ 658 flush_command(pbe, opt); 659 } 660 ptr = argv[n + 1]; 661 662 if ((ptr[0] == 'u') && 663 (ptr[1] == 'g') && 664 (ptr[2] == 'e') && 665 (ptr[3] == 'n')) 666 ptr += 4; 667 668 if ((sscanf(ptr, "%d.%d", 669 &unit, &addr) != 2) || 670 (unit < 0) || (unit > 65535) || 671 (addr < 0) || (addr > 65535)) { 672 errx(1, "cannot " 673 "parse '%s'", argv[n + 1]); 674 } 675 opt->bus = unit; 676 opt->addr = addr; 677 opt->got_bus = 1; 678 opt->got_addr = 1; 679 n++; 680 break; 681 682 case T_UNIT: 683 if (opt->got_any) { 684 /* allow multiple commands on the same line */ 685 flush_command(pbe, opt); 686 } 687 opt->bus = num_id(argv[n + 1], "busnum"); 688 opt->got_bus = 1; 689 n++; 690 break; 691 case T_ADDR: 692 opt->addr = num_id(argv[n + 1], "addr"); 693 opt->got_addr = 1; 694 n++; 695 break; 696 case T_IFACE: 697 opt->iface = num_id(argv[n + 1], "iface"); 698 opt->got_iface = 1; 699 n++; 700 break; 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(); 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(); 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