3 * SPDX-License-Identifier: BSD-2-Clause
5 * Copyright (c) 2008-2009 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
40 #include <sys/types.h>
42 #include <libusb20_desc.h>
48 const char *quirkname;
55 struct LIBUSB20_CONTROL_SETUP_DECODED setup;
61 uint16_t lo_rev; /* inclusive */
62 uint16_t hi_rev; /* inclusive */
69 uint8_t got_set_config:1;
70 uint8_t got_set_alt:1;
71 uint8_t got_set_template:1;
72 uint8_t got_get_template:1;
73 uint8_t got_suspend:1;
76 uint8_t got_power_off:1;
77 uint8_t got_power_save:1;
78 uint8_t got_power_on:1;
79 uint8_t got_dump_device_quirks:1;
80 uint8_t got_dump_quirk_names:1;
81 uint8_t got_dump_all_desc:1;
82 uint8_t got_dump_device_desc:1;
83 uint8_t got_dump_curr_config:1;
84 uint8_t got_dump_all_config:1;
85 uint8_t got_dump_info:1;
86 uint8_t got_dump_stats:1;
87 uint8_t got_show_iface_driver:1;
88 uint8_t got_remove_device_quirk:1;
89 uint8_t got_add_device_quirk:1;
90 uint8_t got_remove_quirk:1;
91 uint8_t got_add_quirk:1;
92 uint8_t got_dump_string:1;
93 uint8_t got_do_request:1;
94 uint8_t got_detach_kernel_driver:1;
109 T_REMOVE_DEVICE_QUIRK,
113 T_DETACH_KERNEL_DRIVER,
115 T_DUMP_DEVICE_QUIRKS,
118 T_DUMP_CURR_CONFIG_DESC,
119 T_DUMP_ALL_CONFIG_DESC,
133 static struct options options;
135 static const struct token token[] = {
136 {"set_config", T_SET_CONFIG, 1},
137 {"set_alt", T_SET_ALT, 1},
138 {"set_template", T_SET_TEMPLATE, 1},
139 {"get_template", T_GET_TEMPLATE, 0},
140 {"add_dev_quirk_vplh", T_ADD_DEVICE_QUIRK, 5},
141 {"remove_dev_quirk_vplh", T_REMOVE_DEVICE_QUIRK, 5},
142 {"add_quirk", T_ADD_QUIRK, 1},
143 {"remove_quirk", T_REMOVE_QUIRK, 1},
144 {"detach_kernel_driver", T_DETACH_KERNEL_DRIVER, 0},
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 {"dump_stats", T_DUMP_STATS, 0},
154 {"show_ifdrv", T_SHOW_IFACE_DRIVER, 0},
155 {"suspend", T_SUSPEND, 0},
156 {"resume", T_RESUME, 0},
157 {"power_off", T_POWER_OFF, 0},
158 {"power_save", T_POWER_SAVE, 0},
159 {"power_on", T_POWER_ON, 0},
160 {"reset", T_RESET, 0},
162 {"do_request", T_DO_REQUEST, 5},
166 be_dev_remove_quirk(struct libusb20_backend *pbe,
167 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
170 struct libusb20_quirk q;
173 memset(&q, 0, sizeof(q));
177 q.bcdDeviceLow = lorev;
178 q.bcdDeviceHigh = hirev;
179 strlcpy(q.quirkname, str, sizeof(q.quirkname));
181 error = libusb20_be_remove_dev_quirk(pbe, &q);
183 fprintf(stderr, "Removing quirk '%s' failed, continuing.\n", str);
188 be_dev_add_quirk(struct libusb20_backend *pbe,
189 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
192 struct libusb20_quirk q;
195 memset(&q, 0, sizeof(q));
199 q.bcdDeviceLow = lorev;
200 q.bcdDeviceHigh = hirev;
201 strlcpy(q.quirkname, str, sizeof(q.quirkname));
203 error = libusb20_be_add_dev_quirk(pbe, &q);
205 fprintf(stderr, "Adding quirk '%s' failed, continuing.\n", str);
210 get_token(const char *str, uint8_t narg)
214 for (n = 0; n != (sizeof(token) / sizeof(token[0])); n++) {
215 if (strcasecmp(str, token[n].name) == 0) {
216 if (token[n].narg > narg) {
217 /* too few arguments */
220 return (token[n].value);
227 num_id(const char *name, const char *type)
233 val = strtoul(name, &ep, 0);
238 errx(1, "%s: illegal %s name", name, type);
244 get_int(const char *s)
250 val = strtoul(s, &ep, 0);
255 errx(1, "illegal number: %s", s);
261 duplicate_option(const char *ptr)
263 fprintf(stderr, "Syntax error: "
264 "Duplicate option: '%s'\n", ptr);
272 "usbconfig - configure the USB subsystem" "\n"
273 "usage: usbconfig [-u <busnum>] [-a <devaddr>] [-i <ifaceindex>] [-v] [cmds...]" "\n"
274 "usage: usbconfig -d [ugen]<busnum>.<devaddr> [-i <ifaceindex>] [-v] [cmds...]" "\n"
276 " set_config <cfg_index>" "\n"
277 " set_alt <alt_index>" "\n"
278 " set_template <template>" "\n"
280 " add_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
281 " remove_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
282 " add_quirk <quirk>" "\n"
283 " remove_quirk <quirk>" "\n"
284 " detach_kernel_driver" "\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"
302 " do_request <bmReqTyp> <bReq> <wVal> <wIdx> <wLen> <data...>" "\n"
308 reset_options(struct options *opt)
312 memset(opt, 0, sizeof(*opt));
316 flush_command(struct libusb20_backend *pbe, struct options *opt)
318 struct libusb20_device *pdev = NULL;
319 uint32_t matches = 0;
322 /* check for invalid option combinations */
323 if ((opt->got_suspend +
326 opt->got_set_config +
328 opt->got_power_save +
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!");
336 if (opt->got_dump_quirk_names) {
338 dump_be_quirk_names(pbe);
340 if (opt->got_dump_device_quirks) {
342 dump_be_dev_quirks(pbe);
344 if (opt->got_remove_device_quirk) {
346 be_dev_remove_quirk(pbe,
347 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
349 if (opt->got_add_device_quirk) {
351 be_dev_add_quirk(pbe,
352 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
354 if (opt->got_set_template) {
356 if (libusb20_be_set_template(pbe, opt->template)) {
357 fprintf(stderr, "Setting USB template %u failed, "
358 "continuing.\n", opt->template);
361 if (opt->got_get_template) {
363 if (libusb20_be_get_template(pbe, &opt->template))
364 printf("USB template: <unknown>\n");
366 printf("USB template: %u\n", opt->template);
368 if (opt->got_any == 0) {
370 * do not scan through all the devices if there are no valid
375 while ((pdev = libusb20_be_device_foreach(pbe, pdev))) {
378 (libusb20_dev_get_bus_number(pdev) != opt->bus)) {
382 (libusb20_dev_get_address(pdev) != opt->addr)) {
387 if (opt->got_remove_quirk) {
388 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
390 ddesc = libusb20_dev_get_device_desc(pdev);
392 be_dev_remove_quirk(pbe,
393 ddesc->idVendor, ddesc->idProduct,
394 ddesc->bcdDevice, ddesc->bcdDevice,
398 if (opt->got_add_quirk) {
399 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
401 ddesc = libusb20_dev_get_device_desc(pdev);
403 be_dev_add_quirk(pbe,
404 ddesc->idVendor, ddesc->idProduct,
405 ddesc->bcdDevice, ddesc->bcdDevice,
409 if (libusb20_dev_open(pdev, 0)) {
410 err(1, "could not open device");
412 if (opt->got_dump_string) {
413 dump_string_by_index(pdev, opt->string_index);
415 if (opt->got_do_request) {
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");
427 printf("REQUEST = <");
428 for (t = 0; t != actlen; t++) {
430 ((uint8_t *)opt->buffer)[t],
431 (t == (actlen - 1)) ? "" : " ");
434 for (t = 0; t != actlen; t++) {
437 c = ((uint8_t *)opt->buffer)[t];
439 (c != '>') && isprint(c)) {
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");
452 if (opt->got_set_alt) {
453 if (libusb20_dev_set_alt_index(pdev, opt->iface,
455 err(1, "could not set alternate setting");
458 if (opt->got_reset) {
459 if (libusb20_dev_reset(pdev)) {
460 err(1, "could not reset device");
463 if (opt->got_suspend) {
464 if (libusb20_dev_set_power_mode(pdev,
465 LIBUSB20_POWER_SUSPEND)) {
466 err(1, "could not set suspend");
469 if (opt->got_resume) {
470 if (libusb20_dev_set_power_mode(pdev,
471 LIBUSB20_POWER_RESUME)) {
472 err(1, "could not set resume");
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");
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");
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");
493 if (opt->got_detach_kernel_driver) {
494 if (libusb20_dev_detach_kernel_driver(pdev, opt->iface)) {
495 err(1, "could not detach kernel driver");
499 (opt->got_dump_all_desc ||
500 opt->got_dump_device_desc ||
501 opt->got_dump_curr_config ||
502 opt->got_dump_all_config ||
503 opt->got_dump_info ||
504 opt->got_dump_stats);
506 if (opt->got_list || dump_any) {
507 dump_device_info(pdev,
508 opt->got_show_iface_driver);
510 if (opt->got_dump_device_desc) {
512 dump_device_desc(pdev);
514 if (opt->got_dump_all_config) {
516 dump_config(pdev, 1);
517 } else if (opt->got_dump_curr_config) {
519 dump_config(pdev, 0);
520 } else if (opt->got_dump_all_desc) {
522 dump_device_desc(pdev);
523 dump_config(pdev, 1);
525 if (opt->got_dump_stats) {
527 dump_device_stats(pdev);
532 if (libusb20_dev_close(pdev)) {
533 err(1, "could not close device");
538 printf("No device match or lack of permissions.\n");
545 main(int argc, char **argv)
547 struct libusb20_backend *pbe;
548 struct options *opt = &options;
559 pbe = libusb20_be_alloc_default();
561 err(1, "could not access USB backend\n");
563 while ((ch = getopt(argc, argv, "a:d:hi:u:v")) != -1) {
566 opt->addr = num_id(optarg, "addr");
571 if (strncmp(optarg, "ugen", strlen("ugen")) == 0) {
572 ptr = optarg + strlen("ugen");
573 } else if (strncmp(optarg, "/dev/ugen",
574 strlen("/dev/ugen")) == 0) {
575 ptr = optarg + strlen("/dev/ugen");
579 if ((sscanf(ptr, "%d.%d",
580 &unit, &addr) != 2) ||
581 (unit < 0) || (unit > 65535) ||
582 (addr < 0) || (addr > 65535)) {
584 "parse '%s'", optarg);
597 opt->iface = num_id(optarg, "iface");
601 opt->bus = num_id(optarg, "busnum");
606 opt->got_dump_device_desc = 1;
607 opt->got_dump_curr_config = 1;
608 opt->got_show_iface_driver = 1;
609 opt->got_any += 2; /* only the dump options count */
619 for (n = 0; n != argc; n++) {
621 /* get number of additional options */
625 switch (get_token(argv[n], t)) {
627 if (opt->got_add_quirk) {
628 flush_command(pbe, opt);
630 opt->quirkname = argv[n + 1];
633 opt->got_add_quirk = 1;
638 if (opt->got_remove_quirk) {
639 flush_command(pbe, opt);
641 opt->quirkname = argv[n + 1];
644 opt->got_remove_quirk = 1;
648 case T_ADD_DEVICE_QUIRK:
649 if (opt->got_add_device_quirk) {
650 flush_command(pbe, opt);
652 opt->vid = num_id(argv[n + 1], "Vendor ID");
653 opt->pid = num_id(argv[n + 2], "Product ID");
654 opt->lo_rev = num_id(argv[n + 3], "Low Revision");
655 opt->hi_rev = num_id(argv[n + 4], "High Revision");
656 opt->quirkname = argv[n + 5];
659 opt->got_add_device_quirk = 1;
663 case T_REMOVE_DEVICE_QUIRK:
664 if (opt->got_remove_device_quirk) {
665 flush_command(pbe, opt);
667 opt->vid = num_id(argv[n + 1], "Vendor ID");
668 opt->pid = num_id(argv[n + 2], "Product ID");
669 opt->lo_rev = num_id(argv[n + 3], "Low Revision");
670 opt->hi_rev = num_id(argv[n + 4], "High Revision");
671 opt->quirkname = argv[n + 5];
673 opt->got_remove_device_quirk = 1;
677 case T_DETACH_KERNEL_DRIVER:
678 if (opt->got_detach_kernel_driver)
679 duplicate_option(argv[n]);
680 opt->got_detach_kernel_driver = 1;
684 case T_DUMP_QUIRK_NAMES:
685 if (opt->got_dump_quirk_names)
686 duplicate_option(argv[n]);
687 opt->got_dump_quirk_names = 1;
691 case T_DUMP_DEVICE_QUIRKS:
692 if (opt->got_dump_device_quirks)
693 duplicate_option(argv[n]);
694 opt->got_dump_device_quirks = 1;
698 case T_SHOW_IFACE_DRIVER:
699 opt->got_show_iface_driver = 1;
703 if (opt->got_set_config)
704 duplicate_option(argv[n]);
705 opt->config_index = num_id(argv[n + 1], "cfg_index");
706 opt->got_set_config = 1;
711 if (opt->got_set_alt)
712 duplicate_option(argv[n]);
713 opt->alt_index = num_id(argv[n + 1], "cfg_index");
714 opt->got_set_alt = 1;
719 if (opt->got_set_template)
720 duplicate_option(argv[n]);
721 opt->template = get_int(argv[n + 1]);
722 opt->got_set_template = 1;
727 if (opt->got_get_template)
728 duplicate_option(argv[n]);
729 opt->got_get_template = 1;
732 case T_DUMP_ALL_DESC:
733 if (opt->got_dump_all_desc)
734 duplicate_option(argv[n]);
735 opt->got_dump_all_desc = 1;
738 case T_DUMP_DEVICE_DESC:
739 if (opt->got_dump_device_desc)
740 duplicate_option(argv[n]);
741 opt->got_dump_device_desc = 1;
744 case T_DUMP_CURR_CONFIG_DESC:
745 if (opt->got_dump_curr_config)
746 duplicate_option(argv[n]);
747 opt->got_dump_curr_config = 1;
750 case T_DUMP_ALL_CONFIG_DESC:
751 if (opt->got_dump_all_config)
752 duplicate_option(argv[n]);
753 opt->got_dump_all_config = 1;
757 if (opt->got_dump_info)
758 duplicate_option(argv[n]);
759 opt->got_dump_info = 1;
763 if (opt->got_dump_stats)
764 duplicate_option(argv[n]);
765 opt->got_dump_stats = 1;
769 if (opt->got_dump_string)
770 duplicate_option(argv[n]);
771 opt->string_index = num_id(argv[n + 1], "str_index");
772 opt->got_dump_string = 1;
777 if (opt->got_suspend)
778 duplicate_option(argv[n]);
779 opt->got_suspend = 1;
784 duplicate_option(argv[n]);
789 if (opt->got_power_off)
790 duplicate_option(argv[n]);
791 opt->got_power_off = 1;
795 if (opt->got_power_save)
796 duplicate_option(argv[n]);
797 opt->got_power_save = 1;
801 if (opt->got_power_on)
802 duplicate_option(argv[n]);
803 opt->got_power_on = 1;
808 duplicate_option(argv[n]);
814 duplicate_option(argv[n]);
819 if (opt->got_do_request)
820 duplicate_option(argv[n]);
821 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &opt->setup);
822 opt->setup.bmRequestType = num_id(argv[n + 1], "bmReqTyp");
823 opt->setup.bRequest = num_id(argv[n + 2], "bReq");
824 opt->setup.wValue = num_id(argv[n + 3], "wVal");
825 opt->setup.wIndex = num_id(argv[n + 4], "wIndex");
826 opt->setup.wLength = num_id(argv[n + 5], "wLen");
827 if (opt->setup.wLength != 0) {
828 opt->buffer = malloc(opt->setup.wLength);
835 if (!(opt->setup.bmRequestType &
836 LIBUSB20_ENDPOINT_IN)) {
839 if (t < opt->setup.wLength) {
840 err(1, "request data missing");
842 t = opt->setup.wLength;
844 ((uint8_t *)opt->buffer)[t] =
845 num_id(argv[n + t + 1], "req_data");
847 n += opt->setup.wLength;
849 opt->got_do_request = 1;
856 if ((ptr[0] == 'u') &&
862 if ((sscanf(ptr, "%d.%d",
863 &unit, &addr) != 2) ||
864 (unit < 0) || (unit > 65535) ||
865 (addr < 0) || (addr > 65535)) {
881 /* flush out last command */
882 flush_command(pbe, opt);
884 /* list all the devices */
887 flush_command(pbe, opt);
890 libusb20_be_free(pbe);