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
38 #include <sys/types.h>
40 #include <libusb20_desc.h>
46 const char *quirkname;
53 struct LIBUSB20_CONTROL_SETUP_DECODED setup;
59 uint16_t lo_rev; /* inclusive */
60 uint16_t hi_rev; /* inclusive */
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;
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_dump_stats:1;
86 uint8_t got_show_iface_driver:1;
87 uint8_t got_remove_device_quirk:1;
88 uint8_t got_add_device_quirk:1;
89 uint8_t got_remove_quirk:1;
90 uint8_t got_add_quirk:1;
91 uint8_t got_dump_string:1;
92 uint8_t got_do_request:1;
93 uint8_t got_detach_kernel_driver:1;
112 T_REMOVE_DEVICE_QUIRK,
116 T_DETACH_KERNEL_DRIVER,
118 T_DUMP_DEVICE_QUIRKS,
121 T_DUMP_CURR_CONFIG_DESC,
122 T_DUMP_ALL_CONFIG_DESC,
136 static struct options options;
138 static const struct token token[] = {
143 {"set_config", T_SET_CONFIG, 1},
144 {"set_alt", T_SET_ALT, 1},
145 {"set_template", T_SET_TEMPLATE, 1},
146 {"get_template", T_GET_TEMPLATE, 0},
147 {"add_dev_quirk_vplh", T_ADD_DEVICE_QUIRK, 5},
148 {"remove_dev_quirk_vplh", T_REMOVE_DEVICE_QUIRK, 5},
149 {"add_quirk", T_ADD_QUIRK, 1},
150 {"remove_quirk", T_REMOVE_QUIRK, 1},
151 {"detach_kernel_driver", T_DETACH_KERNEL_DRIVER, 0},
152 {"dump_quirk_names", T_DUMP_QUIRK_NAMES, 0},
153 {"dump_device_quirks", T_DUMP_DEVICE_QUIRKS, 0},
154 {"dump_all_desc", T_DUMP_ALL_DESC, 0},
155 {"dump_device_desc", T_DUMP_DEVICE_DESC, 0},
156 {"dump_curr_config_desc", T_DUMP_CURR_CONFIG_DESC, 0},
157 {"dump_all_config_desc", T_DUMP_ALL_CONFIG_DESC, 0},
158 {"dump_string", T_DUMP_STRING, 1},
159 {"dump_info", T_DUMP_INFO, 0},
160 {"dump_stats", T_DUMP_STATS, 0},
161 {"show_ifdrv", T_SHOW_IFACE_DRIVER, 0},
162 {"suspend", T_SUSPEND, 0},
163 {"resume", T_RESUME, 0},
164 {"power_off", T_POWER_OFF, 0},
165 {"power_save", T_POWER_SAVE, 0},
166 {"power_on", T_POWER_ON, 0},
167 {"reset", T_RESET, 0},
169 {"do_request", T_DO_REQUEST, 5},
173 be_dev_remove_quirk(struct libusb20_backend *pbe,
174 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
177 struct libusb20_quirk q;
180 memset(&q, 0, sizeof(q));
184 q.bcdDeviceLow = lorev;
185 q.bcdDeviceHigh = hirev;
186 strlcpy(q.quirkname, str, sizeof(q.quirkname));
188 error = libusb20_be_remove_dev_quirk(pbe, &q);
190 fprintf(stderr, "Removing quirk '%s' failed, continuing.\n", str);
196 be_dev_add_quirk(struct libusb20_backend *pbe,
197 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
200 struct libusb20_quirk q;
203 memset(&q, 0, sizeof(q));
207 q.bcdDeviceLow = lorev;
208 q.bcdDeviceHigh = hirev;
209 strlcpy(q.quirkname, str, sizeof(q.quirkname));
211 error = libusb20_be_add_dev_quirk(pbe, &q);
213 fprintf(stderr, "Adding quirk '%s' failed, continuing.\n", str);
219 get_token(const char *str, uint8_t narg)
223 for (n = 0; n != (sizeof(token) / sizeof(token[0])); n++) {
224 if (strcasecmp(str, token[n].name) == 0) {
225 if (token[n].narg > narg) {
226 /* too few arguments */
229 return (token[n].value);
236 num_id(const char *name, const char *type)
242 val = strtoul(name, &ep, 0);
247 errx(1, "%s: illegal %s name", name, type);
253 get_int(const char *s)
259 val = strtoul(s, &ep, 0);
264 errx(1, "illegal number: %s", s);
270 duplicate_option(const char *ptr)
272 fprintf(stderr, "Syntax error: "
273 "Duplicate option: '%s'\n", ptr);
281 "usbconfig - configure the USB subsystem" "\n"
282 "usage: usbconfig -u <busnum> -a <devaddr> -i <ifaceindex> [cmds...]" "\n"
283 "usage: usbconfig -d [ugen]<busnum>.<devaddr> -i <ifaceindex> [cmds...]" "\n"
285 " set_config <cfg_index>" "\n"
286 " set_alt <alt_index>" "\n"
287 " set_template <template>" "\n"
289 " add_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
290 " remove_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
291 " add_quirk <quirk>" "\n"
292 " remove_quirk <quirk>" "\n"
293 " detach_kernel_driver" "\n"
294 " dump_quirk_names" "\n"
295 " dump_device_quirks" "\n"
296 " dump_all_desc" "\n"
297 " dump_device_desc" "\n"
298 " dump_curr_config_desc" "\n"
299 " dump_all_config_desc" "\n"
300 " dump_string <index>" "\n"
311 " do_request <bmReqTyp> <bReq> <wVal> <wIdx> <wLen> <data...>" "\n"
317 reset_options(struct options *opt)
321 memset(opt, 0, sizeof(*opt));
326 flush_command(struct libusb20_backend *pbe, struct options *opt)
328 struct libusb20_device *pdev = NULL;
329 uint32_t matches = 0;
332 /* check for invalid option combinations */
333 if ((opt->got_suspend +
336 opt->got_set_config +
338 opt->got_power_save +
340 opt->got_power_off) > 1) {
341 err(1, "can only specify one of 'set_config', "
342 "'set_alt', 'reset', 'suspend', 'resume', "
343 "'power_save', 'power_on' and 'power_off' "
344 "at the same time!");
346 if (opt->got_dump_quirk_names) {
348 dump_be_quirk_names(pbe);
350 if (opt->got_dump_device_quirks) {
352 dump_be_dev_quirks(pbe);
354 if (opt->got_remove_device_quirk) {
356 be_dev_remove_quirk(pbe,
357 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
359 if (opt->got_add_device_quirk) {
361 be_dev_add_quirk(pbe,
362 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
364 if (opt->got_set_template) {
366 if (libusb20_be_set_template(pbe, opt->template)) {
367 fprintf(stderr, "Setting USB template %u failed, "
368 "continuing.\n", opt->template);
371 if (opt->got_get_template) {
373 if (libusb20_be_get_template(pbe, &opt->template))
374 printf("USB template: <unknown>\n");
376 printf("USB template: %u\n", opt->template);
378 if (opt->got_any == 0) {
380 * do not scan through all the devices if there are no valid
385 while ((pdev = libusb20_be_device_foreach(pbe, pdev))) {
388 (libusb20_dev_get_bus_number(pdev) != opt->bus)) {
392 (libusb20_dev_get_address(pdev) != opt->addr)) {
397 if (opt->got_remove_quirk) {
398 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
400 ddesc = libusb20_dev_get_device_desc(pdev);
402 be_dev_remove_quirk(pbe,
403 ddesc->idVendor, ddesc->idProduct,
404 ddesc->bcdDevice, ddesc->bcdDevice,
408 if (opt->got_add_quirk) {
409 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
411 ddesc = libusb20_dev_get_device_desc(pdev);
413 be_dev_add_quirk(pbe,
414 ddesc->idVendor, ddesc->idProduct,
415 ddesc->bcdDevice, ddesc->bcdDevice,
419 if (libusb20_dev_open(pdev, 0)) {
420 err(1, "could not open device");
422 if (opt->got_dump_string) {
423 dump_string_by_index(pdev, opt->string_index);
425 if (opt->got_do_request) {
429 if (libusb20_dev_request_sync(pdev, &opt->setup,
430 opt->buffer, &actlen, 5000 /* 5 seconds */ , 0)) {
431 printf("REQUEST = <ERROR>\n");
432 } else if (!(opt->setup.bmRequestType &
433 LIBUSB20_ENDPOINT_IN)) {
434 printf("REQUEST = <OK>\n");
437 printf("REQUEST = <");
438 for (t = 0; t != actlen; t++) {
440 ((uint8_t *)opt->buffer)[t],
441 (t == (actlen - 1)) ? "" : " ");
444 for (t = 0; t != actlen; t++) {
447 c = ((uint8_t *)opt->buffer)[t];
449 (c != '>') && isprint(c)) {
456 if (opt->got_set_config) {
457 if (libusb20_dev_set_config_index(pdev,
458 opt->config_index)) {
459 err(1, "could not set config index");
462 if (opt->got_set_alt) {
463 if (libusb20_dev_set_alt_index(pdev, opt->iface,
465 err(1, "could not set alternate setting");
468 if (opt->got_reset) {
469 if (libusb20_dev_reset(pdev)) {
470 err(1, "could not reset device");
473 if (opt->got_suspend) {
474 if (libusb20_dev_set_power_mode(pdev,
475 LIBUSB20_POWER_SUSPEND)) {
476 err(1, "could not set suspend");
479 if (opt->got_resume) {
480 if (libusb20_dev_set_power_mode(pdev,
481 LIBUSB20_POWER_RESUME)) {
482 err(1, "could not set resume");
485 if (opt->got_power_off) {
486 if (libusb20_dev_set_power_mode(pdev,
487 LIBUSB20_POWER_OFF)) {
488 err(1, "could not set power OFF");
491 if (opt->got_power_save) {
492 if (libusb20_dev_set_power_mode(pdev,
493 LIBUSB20_POWER_SAVE)) {
494 err(1, "could not set power SAVE");
497 if (opt->got_power_on) {
498 if (libusb20_dev_set_power_mode(pdev,
499 LIBUSB20_POWER_ON)) {
500 err(1, "could not set power ON");
503 if (opt->got_detach_kernel_driver) {
504 if (libusb20_dev_detach_kernel_driver(pdev, opt->iface)) {
505 err(1, "could not detach kernel driver");
509 (opt->got_dump_all_desc ||
510 opt->got_dump_device_desc ||
511 opt->got_dump_curr_config ||
512 opt->got_dump_all_config ||
513 opt->got_dump_info ||
514 opt->got_dump_stats);
516 if (opt->got_list || dump_any) {
517 dump_device_info(pdev,
518 opt->got_show_iface_driver);
520 if (opt->got_dump_device_desc) {
522 dump_device_desc(pdev);
524 if (opt->got_dump_all_config) {
526 dump_config(pdev, 1);
527 } else if (opt->got_dump_curr_config) {
529 dump_config(pdev, 0);
530 } else if (opt->got_dump_all_desc) {
532 dump_device_desc(pdev);
533 dump_config(pdev, 1);
535 if (opt->got_dump_stats) {
537 dump_device_stats(pdev);
542 if (libusb20_dev_close(pdev)) {
543 err(1, "could not close device");
548 printf("No device match or lack of permissions.\n");
557 main(int argc, char **argv)
559 struct libusb20_backend *pbe;
560 struct options *opt = &options;
570 pbe = libusb20_be_alloc_default();
572 err(1, "could not access USB backend\n");
574 for (n = 1; n != argc; n++) {
576 /* get number of additional options */
580 switch (get_token(argv[n], t)) {
582 if (opt->got_add_quirk) {
583 flush_command(pbe, opt);
585 opt->quirkname = argv[n + 1];
588 opt->got_add_quirk = 1;
593 if (opt->got_remove_quirk) {
594 flush_command(pbe, opt);
596 opt->quirkname = argv[n + 1];
599 opt->got_remove_quirk = 1;
603 case T_ADD_DEVICE_QUIRK:
604 if (opt->got_add_device_quirk) {
605 flush_command(pbe, opt);
607 opt->vid = num_id(argv[n + 1], "Vendor ID");
608 opt->pid = num_id(argv[n + 2], "Product ID");
609 opt->lo_rev = num_id(argv[n + 3], "Low Revision");
610 opt->hi_rev = num_id(argv[n + 4], "High Revision");
611 opt->quirkname = argv[n + 5];
614 opt->got_add_device_quirk = 1;
618 case T_REMOVE_DEVICE_QUIRK:
619 if (opt->got_remove_device_quirk) {
620 flush_command(pbe, opt);
622 opt->vid = num_id(argv[n + 1], "Vendor ID");
623 opt->pid = num_id(argv[n + 2], "Product ID");
624 opt->lo_rev = num_id(argv[n + 3], "Low Revision");
625 opt->hi_rev = num_id(argv[n + 4], "High Revision");
626 opt->quirkname = argv[n + 5];
628 opt->got_remove_device_quirk = 1;
632 case T_DETACH_KERNEL_DRIVER:
633 if (opt->got_detach_kernel_driver)
634 duplicate_option(argv[n]);
635 opt->got_detach_kernel_driver = 1;
639 case T_DUMP_QUIRK_NAMES:
640 if (opt->got_dump_quirk_names)
641 duplicate_option(argv[n]);
642 opt->got_dump_quirk_names = 1;
646 case T_DUMP_DEVICE_QUIRKS:
647 if (opt->got_dump_device_quirks)
648 duplicate_option(argv[n]);
649 opt->got_dump_device_quirks = 1;
653 case T_SHOW_IFACE_DRIVER:
654 opt->got_show_iface_driver = 1;
659 /* allow multiple commands on the same line */
660 flush_command(pbe, opt);
664 if ((ptr[0] == 'u') &&
670 if ((sscanf(ptr, "%d.%d",
671 &unit, &addr) != 2) ||
672 (unit < 0) || (unit > 65535) ||
673 (addr < 0) || (addr > 65535)) {
675 "parse '%s'", argv[n + 1]);
686 /* allow multiple commands on the same line */
687 flush_command(pbe, opt);
689 opt->bus = num_id(argv[n + 1], "busnum");
694 opt->addr = num_id(argv[n + 1], "addr");
699 opt->iface = num_id(argv[n + 1], "iface");
704 if (opt->got_set_config)
705 duplicate_option(argv[n]);
706 opt->config_index = num_id(argv[n + 1], "cfg_index");
707 opt->got_set_config = 1;
712 if (opt->got_set_alt)
713 duplicate_option(argv[n]);
714 opt->alt_index = num_id(argv[n + 1], "cfg_index");
715 opt->got_set_alt = 1;
720 if (opt->got_set_template)
721 duplicate_option(argv[n]);
722 opt->template = get_int(argv[n + 1]);
723 opt->got_set_template = 1;
728 if (opt->got_get_template)
729 duplicate_option(argv[n]);
730 opt->got_get_template = 1;
733 case T_DUMP_ALL_DESC:
734 if (opt->got_dump_all_desc)
735 duplicate_option(argv[n]);
736 opt->got_dump_all_desc = 1;
739 case T_DUMP_DEVICE_DESC:
740 if (opt->got_dump_device_desc)
741 duplicate_option(argv[n]);
742 opt->got_dump_device_desc = 1;
745 case T_DUMP_CURR_CONFIG_DESC:
746 if (opt->got_dump_curr_config)
747 duplicate_option(argv[n]);
748 opt->got_dump_curr_config = 1;
751 case T_DUMP_ALL_CONFIG_DESC:
752 if (opt->got_dump_all_config)
753 duplicate_option(argv[n]);
754 opt->got_dump_all_config = 1;
758 if (opt->got_dump_info)
759 duplicate_option(argv[n]);
760 opt->got_dump_info = 1;
764 if (opt->got_dump_stats)
765 duplicate_option(argv[n]);
766 opt->got_dump_stats = 1;
770 if (opt->got_dump_string)
771 duplicate_option(argv[n]);
772 opt->string_index = num_id(argv[n + 1], "str_index");
773 opt->got_dump_string = 1;
778 if (opt->got_suspend)
779 duplicate_option(argv[n]);
780 opt->got_suspend = 1;
785 duplicate_option(argv[n]);
790 if (opt->got_power_off)
791 duplicate_option(argv[n]);
792 opt->got_power_off = 1;
796 if (opt->got_power_save)
797 duplicate_option(argv[n]);
798 opt->got_power_save = 1;
802 if (opt->got_power_on)
803 duplicate_option(argv[n]);
804 opt->got_power_on = 1;
809 duplicate_option(argv[n]);
815 duplicate_option(argv[n]);
820 if (opt->got_do_request)
821 duplicate_option(argv[n]);
822 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &opt->setup);
823 opt->setup.bmRequestType = num_id(argv[n + 1], "bmReqTyp");
824 opt->setup.bRequest = num_id(argv[n + 2], "bReq");
825 opt->setup.wValue = num_id(argv[n + 3], "wVal");
826 opt->setup.wIndex = num_id(argv[n + 4], "wIndex");
827 opt->setup.wLength = num_id(argv[n + 5], "wLen");
828 if (opt->setup.wLength != 0) {
829 opt->buffer = malloc(opt->setup.wLength);
836 if (!(opt->setup.bmRequestType &
837 LIBUSB20_ENDPOINT_IN)) {
840 if (t < opt->setup.wLength) {
841 err(1, "request data missing");
843 t = opt->setup.wLength;
845 ((uint8_t *)opt->buffer)[t] =
846 num_id(argv[n + t + 1], "req_data");
848 n += opt->setup.wLength;
850 opt->got_do_request = 1;
857 if ((ptr[0] == 'u') &&
863 if ((sscanf(ptr, "%d.%d",
864 &unit, &addr) != 2) ||
865 (unit < 0) || (unit > 65535) ||
866 (addr < 0) || (addr > 65535)) {
882 /* flush out last command */
883 flush_command(pbe, opt);
885 /* list all the devices */
888 flush_command(pbe, opt);
891 libusb20_be_free(pbe);