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