3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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);
189 be_dev_add_quirk(struct libusb20_backend *pbe,
190 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
193 struct libusb20_quirk q;
196 memset(&q, 0, sizeof(q));
200 q.bcdDeviceLow = lorev;
201 q.bcdDeviceHigh = hirev;
202 strlcpy(q.quirkname, str, sizeof(q.quirkname));
204 error = libusb20_be_add_dev_quirk(pbe, &q);
206 fprintf(stderr, "Adding quirk '%s' failed, continuing.\n", str);
212 get_token(const char *str, uint8_t narg)
216 for (n = 0; n != (sizeof(token) / sizeof(token[0])); n++) {
217 if (strcasecmp(str, token[n].name) == 0) {
218 if (token[n].narg > narg) {
219 /* too few arguments */
222 return (token[n].value);
229 num_id(const char *name, const char *type)
235 val = strtoul(name, &ep, 0);
240 errx(1, "%s: illegal %s name", name, type);
246 get_int(const char *s)
252 val = strtoul(s, &ep, 0);
257 errx(1, "illegal number: %s", s);
263 duplicate_option(const char *ptr)
265 fprintf(stderr, "Syntax error: "
266 "Duplicate option: '%s'\n", ptr);
274 "usbconfig - configure the USB subsystem" "\n"
275 "usage: usbconfig [-u <busnum>] [-a <devaddr>] [-i <ifaceindex>] [-v] [cmds...]" "\n"
276 "usage: usbconfig -d [ugen]<busnum>.<devaddr> [-i <ifaceindex>] [-v] [cmds...]" "\n"
278 " set_config <cfg_index>" "\n"
279 " set_alt <alt_index>" "\n"
280 " set_template <template>" "\n"
282 " add_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
283 " remove_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
284 " add_quirk <quirk>" "\n"
285 " remove_quirk <quirk>" "\n"
286 " detach_kernel_driver" "\n"
287 " dump_quirk_names" "\n"
288 " dump_device_quirks" "\n"
289 " dump_all_desc" "\n"
290 " dump_device_desc" "\n"
291 " dump_curr_config_desc" "\n"
292 " dump_all_config_desc" "\n"
293 " dump_string <index>" "\n"
304 " do_request <bmReqTyp> <bReq> <wVal> <wIdx> <wLen> <data...>" "\n"
310 reset_options(struct options *opt)
314 memset(opt, 0, sizeof(*opt));
319 flush_command(struct libusb20_backend *pbe, struct options *opt)
321 struct libusb20_device *pdev = NULL;
322 uint32_t matches = 0;
325 /* check for invalid option combinations */
326 if ((opt->got_suspend +
329 opt->got_set_config +
331 opt->got_power_save +
333 opt->got_power_off) > 1) {
334 err(1, "can only specify one of 'set_config', "
335 "'set_alt', 'reset', 'suspend', 'resume', "
336 "'power_save', 'power_on' and 'power_off' "
337 "at the same time!");
339 if (opt->got_dump_quirk_names) {
341 dump_be_quirk_names(pbe);
343 if (opt->got_dump_device_quirks) {
345 dump_be_dev_quirks(pbe);
347 if (opt->got_remove_device_quirk) {
349 be_dev_remove_quirk(pbe,
350 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
352 if (opt->got_add_device_quirk) {
354 be_dev_add_quirk(pbe,
355 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
357 if (opt->got_set_template) {
359 if (libusb20_be_set_template(pbe, opt->template)) {
360 fprintf(stderr, "Setting USB template %u failed, "
361 "continuing.\n", opt->template);
364 if (opt->got_get_template) {
366 if (libusb20_be_get_template(pbe, &opt->template))
367 printf("USB template: <unknown>\n");
369 printf("USB template: %u\n", opt->template);
371 if (opt->got_any == 0) {
373 * do not scan through all the devices if there are no valid
378 while ((pdev = libusb20_be_device_foreach(pbe, pdev))) {
381 (libusb20_dev_get_bus_number(pdev) != opt->bus)) {
385 (libusb20_dev_get_address(pdev) != opt->addr)) {
390 if (opt->got_remove_quirk) {
391 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
393 ddesc = libusb20_dev_get_device_desc(pdev);
395 be_dev_remove_quirk(pbe,
396 ddesc->idVendor, ddesc->idProduct,
397 ddesc->bcdDevice, ddesc->bcdDevice,
401 if (opt->got_add_quirk) {
402 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
404 ddesc = libusb20_dev_get_device_desc(pdev);
406 be_dev_add_quirk(pbe,
407 ddesc->idVendor, ddesc->idProduct,
408 ddesc->bcdDevice, ddesc->bcdDevice,
412 if (libusb20_dev_open(pdev, 0)) {
413 err(1, "could not open device");
415 if (opt->got_dump_string) {
416 dump_string_by_index(pdev, opt->string_index);
418 if (opt->got_do_request) {
422 if (libusb20_dev_request_sync(pdev, &opt->setup,
423 opt->buffer, &actlen, 5000 /* 5 seconds */ , 0)) {
424 printf("REQUEST = <ERROR>\n");
425 } else if (!(opt->setup.bmRequestType &
426 LIBUSB20_ENDPOINT_IN)) {
427 printf("REQUEST = <OK>\n");
430 printf("REQUEST = <");
431 for (t = 0; t != actlen; t++) {
433 ((uint8_t *)opt->buffer)[t],
434 (t == (actlen - 1)) ? "" : " ");
437 for (t = 0; t != actlen; t++) {
440 c = ((uint8_t *)opt->buffer)[t];
442 (c != '>') && isprint(c)) {
449 if (opt->got_set_config) {
450 if (libusb20_dev_set_config_index(pdev,
451 opt->config_index)) {
452 err(1, "could not set config index");
455 if (opt->got_set_alt) {
456 if (libusb20_dev_set_alt_index(pdev, opt->iface,
458 err(1, "could not set alternate setting");
461 if (opt->got_reset) {
462 if (libusb20_dev_reset(pdev)) {
463 err(1, "could not reset device");
466 if (opt->got_suspend) {
467 if (libusb20_dev_set_power_mode(pdev,
468 LIBUSB20_POWER_SUSPEND)) {
469 err(1, "could not set suspend");
472 if (opt->got_resume) {
473 if (libusb20_dev_set_power_mode(pdev,
474 LIBUSB20_POWER_RESUME)) {
475 err(1, "could not set resume");
478 if (opt->got_power_off) {
479 if (libusb20_dev_set_power_mode(pdev,
480 LIBUSB20_POWER_OFF)) {
481 err(1, "could not set power OFF");
484 if (opt->got_power_save) {
485 if (libusb20_dev_set_power_mode(pdev,
486 LIBUSB20_POWER_SAVE)) {
487 err(1, "could not set power SAVE");
490 if (opt->got_power_on) {
491 if (libusb20_dev_set_power_mode(pdev,
492 LIBUSB20_POWER_ON)) {
493 err(1, "could not set power ON");
496 if (opt->got_detach_kernel_driver) {
497 if (libusb20_dev_detach_kernel_driver(pdev, opt->iface)) {
498 err(1, "could not detach kernel driver");
502 (opt->got_dump_all_desc ||
503 opt->got_dump_device_desc ||
504 opt->got_dump_curr_config ||
505 opt->got_dump_all_config ||
506 opt->got_dump_info ||
507 opt->got_dump_stats);
509 if (opt->got_list || dump_any) {
510 dump_device_info(pdev,
511 opt->got_show_iface_driver);
513 if (opt->got_dump_device_desc) {
515 dump_device_desc(pdev);
517 if (opt->got_dump_all_config) {
519 dump_config(pdev, 1);
520 } else if (opt->got_dump_curr_config) {
522 dump_config(pdev, 0);
523 } else if (opt->got_dump_all_desc) {
525 dump_device_desc(pdev);
526 dump_config(pdev, 1);
528 if (opt->got_dump_stats) {
530 dump_device_stats(pdev);
535 if (libusb20_dev_close(pdev)) {
536 err(1, "could not close device");
541 printf("No device match or lack of permissions.\n");
550 main(int argc, char **argv)
552 struct libusb20_backend *pbe;
553 struct options *opt = &options;
564 pbe = libusb20_be_alloc_default();
566 err(1, "could not access USB backend\n");
568 while ((ch = getopt(argc, argv, "a:d:hi:u:v")) != -1) {
571 opt->addr = num_id(optarg, "addr");
576 if (strncmp(optarg, "ugen", strlen("ugen")) == 0) {
577 ptr = optarg + strlen("ugen");
578 } else if (strncmp(optarg, "/dev/ugen",
579 strlen("/dev/ugen")) == 0) {
580 ptr = optarg + strlen("/dev/ugen");
584 if ((sscanf(ptr, "%d.%d",
585 &unit, &addr) != 2) ||
586 (unit < 0) || (unit > 65535) ||
587 (addr < 0) || (addr > 65535)) {
589 "parse '%s'", optarg);
602 opt->iface = num_id(optarg, "iface");
606 opt->bus = num_id(optarg, "busnum");
611 opt->got_dump_device_desc = 1;
612 opt->got_dump_curr_config = 1;
613 opt->got_show_iface_driver = 1;
614 opt->got_any += 2; /* only the dump options count */
624 for (n = 0; n != argc; n++) {
626 /* get number of additional options */
630 switch (get_token(argv[n], t)) {
632 if (opt->got_add_quirk) {
633 flush_command(pbe, opt);
635 opt->quirkname = argv[n + 1];
638 opt->got_add_quirk = 1;
643 if (opt->got_remove_quirk) {
644 flush_command(pbe, opt);
646 opt->quirkname = argv[n + 1];
649 opt->got_remove_quirk = 1;
653 case T_ADD_DEVICE_QUIRK:
654 if (opt->got_add_device_quirk) {
655 flush_command(pbe, opt);
657 opt->vid = num_id(argv[n + 1], "Vendor ID");
658 opt->pid = num_id(argv[n + 2], "Product ID");
659 opt->lo_rev = num_id(argv[n + 3], "Low Revision");
660 opt->hi_rev = num_id(argv[n + 4], "High Revision");
661 opt->quirkname = argv[n + 5];
664 opt->got_add_device_quirk = 1;
668 case T_REMOVE_DEVICE_QUIRK:
669 if (opt->got_remove_device_quirk) {
670 flush_command(pbe, opt);
672 opt->vid = num_id(argv[n + 1], "Vendor ID");
673 opt->pid = num_id(argv[n + 2], "Product ID");
674 opt->lo_rev = num_id(argv[n + 3], "Low Revision");
675 opt->hi_rev = num_id(argv[n + 4], "High Revision");
676 opt->quirkname = argv[n + 5];
678 opt->got_remove_device_quirk = 1;
682 case T_DETACH_KERNEL_DRIVER:
683 if (opt->got_detach_kernel_driver)
684 duplicate_option(argv[n]);
685 opt->got_detach_kernel_driver = 1;
689 case T_DUMP_QUIRK_NAMES:
690 if (opt->got_dump_quirk_names)
691 duplicate_option(argv[n]);
692 opt->got_dump_quirk_names = 1;
696 case T_DUMP_DEVICE_QUIRKS:
697 if (opt->got_dump_device_quirks)
698 duplicate_option(argv[n]);
699 opt->got_dump_device_quirks = 1;
703 case T_SHOW_IFACE_DRIVER:
704 opt->got_show_iface_driver = 1;
708 if (opt->got_set_config)
709 duplicate_option(argv[n]);
710 opt->config_index = num_id(argv[n + 1], "cfg_index");
711 opt->got_set_config = 1;
716 if (opt->got_set_alt)
717 duplicate_option(argv[n]);
718 opt->alt_index = num_id(argv[n + 1], "cfg_index");
719 opt->got_set_alt = 1;
724 if (opt->got_set_template)
725 duplicate_option(argv[n]);
726 opt->template = get_int(argv[n + 1]);
727 opt->got_set_template = 1;
732 if (opt->got_get_template)
733 duplicate_option(argv[n]);
734 opt->got_get_template = 1;
737 case T_DUMP_ALL_DESC:
738 if (opt->got_dump_all_desc)
739 duplicate_option(argv[n]);
740 opt->got_dump_all_desc = 1;
743 case T_DUMP_DEVICE_DESC:
744 if (opt->got_dump_device_desc)
745 duplicate_option(argv[n]);
746 opt->got_dump_device_desc = 1;
749 case T_DUMP_CURR_CONFIG_DESC:
750 if (opt->got_dump_curr_config)
751 duplicate_option(argv[n]);
752 opt->got_dump_curr_config = 1;
755 case T_DUMP_ALL_CONFIG_DESC:
756 if (opt->got_dump_all_config)
757 duplicate_option(argv[n]);
758 opt->got_dump_all_config = 1;
762 if (opt->got_dump_info)
763 duplicate_option(argv[n]);
764 opt->got_dump_info = 1;
768 if (opt->got_dump_stats)
769 duplicate_option(argv[n]);
770 opt->got_dump_stats = 1;
774 if (opt->got_dump_string)
775 duplicate_option(argv[n]);
776 opt->string_index = num_id(argv[n + 1], "str_index");
777 opt->got_dump_string = 1;
782 if (opt->got_suspend)
783 duplicate_option(argv[n]);
784 opt->got_suspend = 1;
789 duplicate_option(argv[n]);
794 if (opt->got_power_off)
795 duplicate_option(argv[n]);
796 opt->got_power_off = 1;
800 if (opt->got_power_save)
801 duplicate_option(argv[n]);
802 opt->got_power_save = 1;
806 if (opt->got_power_on)
807 duplicate_option(argv[n]);
808 opt->got_power_on = 1;
813 duplicate_option(argv[n]);
819 duplicate_option(argv[n]);
824 if (opt->got_do_request)
825 duplicate_option(argv[n]);
826 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &opt->setup);
827 opt->setup.bmRequestType = num_id(argv[n + 1], "bmReqTyp");
828 opt->setup.bRequest = num_id(argv[n + 2], "bReq");
829 opt->setup.wValue = num_id(argv[n + 3], "wVal");
830 opt->setup.wIndex = num_id(argv[n + 4], "wIndex");
831 opt->setup.wLength = num_id(argv[n + 5], "wLen");
832 if (opt->setup.wLength != 0) {
833 opt->buffer = malloc(opt->setup.wLength);
840 if (!(opt->setup.bmRequestType &
841 LIBUSB20_ENDPOINT_IN)) {
844 if (t < opt->setup.wLength) {
845 err(1, "request data missing");
847 t = opt->setup.wLength;
849 ((uint8_t *)opt->buffer)[t] =
850 num_id(argv[n + t + 1], "req_data");
852 n += opt->setup.wLength;
854 opt->got_do_request = 1;
861 if ((ptr[0] == 'u') &&
867 if ((sscanf(ptr, "%d.%d",
868 &unit, &addr) != 2) ||
869 (unit < 0) || (unit > 65535) ||
870 (addr < 0) || (addr > 65535)) {
886 /* flush out last command */
887 flush_command(pbe, opt);
889 /* list all the devices */
892 flush_command(pbe, opt);
895 libusb20_be_free(pbe);