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