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_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;
110 T_REMOVE_DEVICE_QUIRK,
115 T_DUMP_DEVICE_QUIRKS,
118 T_DUMP_CURR_CONFIG_DESC,
119 T_DUMP_ALL_CONFIG_DESC,
132 static struct options options;
134 static const struct token token[] = {
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 {"dump_quirk_names", T_DUMP_QUIRK_NAMES, 0},
148 {"dump_device_quirks", T_DUMP_DEVICE_QUIRKS, 0},
149 {"dump_all_desc", T_DUMP_ALL_DESC, 0},
150 {"dump_device_desc", T_DUMP_DEVICE_DESC, 0},
151 {"dump_curr_config_desc", T_DUMP_CURR_CONFIG_DESC, 0},
152 {"dump_all_config_desc", T_DUMP_ALL_CONFIG_DESC, 0},
153 {"dump_string", T_DUMP_STRING, 1},
154 {"dump_info", T_DUMP_INFO, 0},
155 {"show_ifdrv", T_SHOW_IFACE_DRIVER, 0},
156 {"suspend", T_SUSPEND, 0},
157 {"resume", T_RESUME, 0},
158 {"power_off", T_POWER_OFF, 0},
159 {"power_save", T_POWER_SAVE, 0},
160 {"power_on", T_POWER_ON, 0},
161 {"reset", T_RESET, 0},
163 {"do_request", T_DO_REQUEST, 5},
167 be_dev_remove_quirk(struct libusb20_backend *pbe,
168 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
171 struct libusb20_quirk q;
174 memset(&q, 0, sizeof(q));
178 q.bcdDeviceLow = lorev;
179 q.bcdDeviceHigh = hirev;
180 strlcpy(q.quirkname, str, sizeof(q.quirkname));
182 error = libusb20_be_remove_dev_quirk(pbe, &q);
184 fprintf(stderr, "Removing quirk '%s' failed, continuing.\n", str);
190 be_dev_add_quirk(struct libusb20_backend *pbe,
191 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
194 struct libusb20_quirk q;
197 memset(&q, 0, sizeof(q));
201 q.bcdDeviceLow = lorev;
202 q.bcdDeviceHigh = hirev;
203 strlcpy(q.quirkname, str, sizeof(q.quirkname));
205 error = libusb20_be_add_dev_quirk(pbe, &q);
207 fprintf(stderr, "Adding quirk '%s' failed, continuing.\n", str);
213 get_token(const char *str, uint8_t narg)
217 for (n = 0; n != (sizeof(token) / sizeof(token[0])); n++) {
218 if (strcasecmp(str, token[n].name) == 0) {
219 if (token[n].narg > narg) {
220 /* too few arguments */
223 return (token[n].value);
230 num_id(const char *name, const char *type)
236 val = strtoul(name, &ep, 0);
241 errx(1, "%s: illegal %s name", name, type);
247 get_int(const char *s)
253 val = strtoul(s, &ep, 0);
258 errx(1, "illegal number: %s", s);
264 duplicate_option(const char *ptr)
266 fprintf(stderr, "Syntax error: "
267 "Duplicate option: '%s'\n", ptr);
275 "usbconfig - configure the USB subsystem" "\n"
276 "usage: usbconfig -u <busnum> -a <devaddr> -i <ifaceindex> [cmds...]" "\n"
277 "usage: usbconfig -d [ugen]<busnum>.<devaddr> -i <ifaceindex> [cmds...]" "\n"
279 " set_config <cfg_index>" "\n"
280 " set_alt <alt_index>" "\n"
281 " set_template <template>" "\n"
283 " add_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
284 " remove_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
285 " add_quirk <quirk>" "\n"
286 " remove_quirk <quirk>" "\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"
303 " do_request <bmReqTyp> <bReq> <wVal> <wIdx> <wLen> <data...>" "\n"
309 reset_options(struct options *opt)
313 memset(opt, 0, sizeof(*opt));
318 flush_command(struct libusb20_backend *pbe, struct options *opt)
320 struct libusb20_device *pdev = NULL;
321 uint32_t matches = 0;
324 /* check for invalid option combinations */
325 if ((opt->got_suspend +
328 opt->got_set_config +
330 opt->got_power_save +
332 opt->got_power_off) > 1) {
333 err(1, "can only specify one of 'set_config', "
334 "'set_alt', 'reset', 'suspend', 'resume', "
335 "'power_save', 'power_on' and 'power_off' "
336 "at the same time!");
338 if (opt->got_dump_quirk_names) {
340 dump_be_quirk_names(pbe);
342 if (opt->got_dump_device_quirks) {
344 dump_be_dev_quirks(pbe);
346 if (opt->got_remove_device_quirk) {
348 be_dev_remove_quirk(pbe,
349 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
351 if (opt->got_add_device_quirk) {
353 be_dev_add_quirk(pbe,
354 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
356 if (opt->got_set_template) {
358 if (libusb20_be_set_template(pbe, opt->template)) {
359 fprintf(stderr, "Setting USB template %u failed, "
360 "continuing.\n", opt->template);
363 if (opt->got_get_template) {
365 if (libusb20_be_get_template(pbe, &opt->template))
366 printf("USB template: <unknown>\n");
368 printf("USB template: %u\n", opt->template);
370 if (opt->got_any == 0) {
372 * do not scan through all the devices if there are no valid
377 while ((pdev = libusb20_be_device_foreach(pbe, pdev))) {
380 (libusb20_dev_get_bus_number(pdev) != opt->bus)) {
384 (libusb20_dev_get_address(pdev) != opt->addr)) {
389 if (opt->got_remove_quirk) {
390 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
392 ddesc = libusb20_dev_get_device_desc(pdev);
394 be_dev_remove_quirk(pbe,
395 ddesc->idVendor, ddesc->idProduct,
396 ddesc->bcdDevice, ddesc->bcdDevice,
400 if (opt->got_add_quirk) {
401 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
403 ddesc = libusb20_dev_get_device_desc(pdev);
405 be_dev_add_quirk(pbe,
406 ddesc->idVendor, ddesc->idProduct,
407 ddesc->bcdDevice, ddesc->bcdDevice,
411 if (libusb20_dev_open(pdev, 0)) {
412 err(1, "could not open device");
414 if (opt->got_dump_string) {
415 dump_string_by_index(pdev, opt->string_index);
417 if (opt->got_do_request) {
421 if (libusb20_dev_request_sync(pdev, &opt->setup,
422 opt->buffer, &actlen, 5000 /* 5 seconds */ , 0)) {
423 printf("REQUEST = <ERROR>\n");
424 } else if (!(opt->setup.bmRequestType &
425 LIBUSB20_ENDPOINT_IN)) {
426 printf("REQUEST = <OK>\n");
429 printf("REQUEST = <");
430 for (t = 0; t != actlen; t++) {
432 ((uint8_t *)opt->buffer)[t],
433 (t == (actlen - 1)) ? "" : " ");
436 for (t = 0; t != actlen; t++) {
439 c = ((uint8_t *)opt->buffer)[t];
441 (c != '>') && isprint(c)) {
448 if (opt->got_set_config) {
449 if (libusb20_dev_set_config_index(pdev,
450 opt->config_index)) {
451 err(1, "could not set config index");
454 if (opt->got_set_alt) {
455 if (libusb20_dev_set_alt_index(pdev, opt->iface,
457 err(1, "could not set alternate setting");
460 if (opt->got_reset) {
461 if (libusb20_dev_reset(pdev)) {
462 err(1, "could not reset device");
465 if (opt->got_suspend) {
466 if (libusb20_dev_set_power_mode(pdev,
467 LIBUSB20_POWER_SUSPEND)) {
468 err(1, "could not set suspend");
471 if (opt->got_resume) {
472 if (libusb20_dev_set_power_mode(pdev,
473 LIBUSB20_POWER_RESUME)) {
474 err(1, "could not set resume");
477 if (opt->got_power_off) {
478 if (libusb20_dev_set_power_mode(pdev,
479 LIBUSB20_POWER_OFF)) {
480 err(1, "could not set power OFF");
483 if (opt->got_power_save) {
484 if (libusb20_dev_set_power_mode(pdev,
485 LIBUSB20_POWER_SAVE)) {
486 err(1, "could not set power SAVE");
489 if (opt->got_power_on) {
490 if (libusb20_dev_set_power_mode(pdev,
491 LIBUSB20_POWER_ON)) {
492 err(1, "could not set power ON");
496 (opt->got_dump_all_desc ||
497 opt->got_dump_device_desc ||
498 opt->got_dump_curr_config ||
499 opt->got_dump_all_config ||
502 if (opt->got_list || dump_any) {
503 dump_device_info(pdev,
504 opt->got_show_iface_driver);
506 if (opt->got_dump_device_desc) {
508 dump_device_desc(pdev);
510 if (opt->got_dump_all_config) {
512 dump_config(pdev, 1);
513 } else if (opt->got_dump_curr_config) {
515 dump_config(pdev, 0);
516 } else if (opt->got_dump_all_desc) {
518 dump_device_desc(pdev);
519 dump_config(pdev, 1);
524 if (libusb20_dev_close(pdev)) {
525 err(1, "could not close device");
530 printf("No device match or lack of permissions.\n");
539 main(int argc, char **argv)
541 struct libusb20_backend *pbe;
542 struct options *opt = &options;
552 pbe = libusb20_be_alloc_default();
554 err(1, "could not access USB backend\n");
556 for (n = 1; n != argc; n++) {
558 /* get number of additional options */
562 switch (get_token(argv[n], t)) {
564 if (opt->got_add_quirk) {
565 flush_command(pbe, opt);
567 opt->quirkname = argv[n + 1];
570 opt->got_add_quirk = 1;
575 if (opt->got_remove_quirk) {
576 flush_command(pbe, opt);
578 opt->quirkname = argv[n + 1];
581 opt->got_remove_quirk = 1;
585 case T_ADD_DEVICE_QUIRK:
586 if (opt->got_add_device_quirk) {
587 flush_command(pbe, opt);
589 opt->vid = num_id(argv[n + 1], "Vendor ID");
590 opt->pid = num_id(argv[n + 2], "Product ID");
591 opt->lo_rev = num_id(argv[n + 3], "Low Revision");
592 opt->hi_rev = num_id(argv[n + 4], "High Revision");
593 opt->quirkname = argv[n + 5];
596 opt->got_add_device_quirk = 1;
600 case T_REMOVE_DEVICE_QUIRK:
601 if (opt->got_remove_device_quirk) {
602 flush_command(pbe, opt);
604 opt->vid = num_id(argv[n + 1], "Vendor ID");
605 opt->pid = num_id(argv[n + 2], "Product ID");
606 opt->lo_rev = num_id(argv[n + 3], "Low Revision");
607 opt->hi_rev = num_id(argv[n + 4], "High Revision");
608 opt->quirkname = argv[n + 5];
610 opt->got_remove_device_quirk = 1;
614 case T_DUMP_QUIRK_NAMES:
615 if (opt->got_dump_quirk_names)
616 duplicate_option(argv[n]);
617 opt->got_dump_quirk_names = 1;
621 case T_DUMP_DEVICE_QUIRKS:
622 if (opt->got_dump_device_quirks)
623 duplicate_option(argv[n]);
624 opt->got_dump_device_quirks = 1;
628 case T_SHOW_IFACE_DRIVER:
629 opt->got_show_iface_driver = 1;
634 /* allow multiple commands on the same line */
635 flush_command(pbe, opt);
639 if ((ptr[0] == 'u') &&
645 if ((sscanf(ptr, "%d.%d",
646 &unit, &addr) != 2) ||
647 (unit < 0) || (unit > 65535) ||
648 (addr < 0) || (addr > 65535)) {
650 "parse '%s'", argv[n + 1]);
661 /* allow multiple commands on the same line */
662 flush_command(pbe, opt);
664 opt->bus = num_id(argv[n + 1], "busnum");
669 opt->addr = num_id(argv[n + 1], "addr");
674 opt->iface = num_id(argv[n + 1], "iface");
679 if (opt->got_set_config)
680 duplicate_option(argv[n]);
681 opt->config_index = num_id(argv[n + 1], "cfg_index");
682 opt->got_set_config = 1;
687 if (opt->got_set_alt)
688 duplicate_option(argv[n]);
689 opt->alt_index = num_id(argv[n + 1], "cfg_index");
690 opt->got_set_alt = 1;
695 if (opt->got_set_template)
696 duplicate_option(argv[n]);
697 opt->template = get_int(argv[n + 1]);
698 opt->got_set_template = 1;
703 if (opt->got_get_template)
704 duplicate_option(argv[n]);
705 opt->got_get_template = 1;
708 case T_DUMP_ALL_DESC:
709 if (opt->got_dump_all_desc)
710 duplicate_option(argv[n]);
711 opt->got_dump_all_desc = 1;
714 case T_DUMP_DEVICE_DESC:
715 if (opt->got_dump_device_desc)
716 duplicate_option(argv[n]);
717 opt->got_dump_device_desc = 1;
720 case T_DUMP_CURR_CONFIG_DESC:
721 if (opt->got_dump_curr_config)
722 duplicate_option(argv[n]);
723 opt->got_dump_curr_config = 1;
726 case T_DUMP_ALL_CONFIG_DESC:
727 if (opt->got_dump_all_config)
728 duplicate_option(argv[n]);
729 opt->got_dump_all_config = 1;
733 if (opt->got_dump_info)
734 duplicate_option(argv[n]);
735 opt->got_dump_info = 1;
739 if (opt->got_dump_string)
740 duplicate_option(argv[n]);
741 opt->string_index = num_id(argv[n + 1], "str_index");
742 opt->got_dump_string = 1;
747 if (opt->got_suspend)
748 duplicate_option(argv[n]);
749 opt->got_suspend = 1;
754 duplicate_option(argv[n]);
759 if (opt->got_power_off)
760 duplicate_option(argv[n]);
761 opt->got_power_off = 1;
765 if (opt->got_power_save)
766 duplicate_option(argv[n]);
767 opt->got_power_save = 1;
771 if (opt->got_power_on)
772 duplicate_option(argv[n]);
773 opt->got_power_on = 1;
778 duplicate_option(argv[n]);
784 duplicate_option(argv[n]);
789 if (opt->got_do_request)
790 duplicate_option(argv[n]);
791 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &opt->setup);
792 opt->setup.bmRequestType = num_id(argv[n + 1], "bmReqTyp");
793 opt->setup.bRequest = num_id(argv[n + 2], "bReq");
794 opt->setup.wValue = num_id(argv[n + 3], "wVal");
795 opt->setup.wIndex = num_id(argv[n + 4], "wIndex");
796 opt->setup.wLength = num_id(argv[n + 5], "wLen");
797 if (opt->setup.wLength != 0) {
798 opt->buffer = malloc(opt->setup.wLength);
805 if (!(opt->setup.bmRequestType &
806 LIBUSB20_ENDPOINT_IN)) {
809 if (t < opt->setup.wLength) {
810 err(1, "request data missing");
812 t = opt->setup.wLength;
814 ((uint8_t *)opt->buffer)[t] =
815 num_id(argv[n + t + 1], "req_data");
817 n += opt->setup.wLength;
819 opt->got_do_request = 1;
828 /* flush out last command */
829 flush_command(pbe, opt);
831 /* list all the devices */
834 flush_command(pbe, opt);
837 libusb20_be_free(pbe);