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_device_desc:1;
81 uint8_t got_dump_curr_config:1;
82 uint8_t got_dump_all_config:1;
83 uint8_t got_dump_info:1;
84 uint8_t got_show_iface_driver:1;
85 uint8_t got_remove_device_quirk:1;
86 uint8_t got_add_device_quirk:1;
87 uint8_t got_remove_quirk:1;
88 uint8_t got_add_quirk:1;
89 uint8_t got_dump_string:1;
90 uint8_t got_do_request:1;
109 T_REMOVE_DEVICE_QUIRK,
114 T_DUMP_DEVICE_QUIRKS,
116 T_DUMP_CURR_CONFIG_DESC,
117 T_DUMP_ALL_CONFIG_DESC,
130 static struct options options;
132 static const struct token token[] = {
137 {"set_config", T_SET_CONFIG, 1},
138 {"set_alt", T_SET_ALT, 1},
139 {"set_template", T_SET_TEMPLATE, 1},
140 {"get_template", T_GET_TEMPLATE, 0},
141 {"add_dev_quirk_vplh", T_ADD_DEVICE_QUIRK, 5},
142 {"remove_dev_quirk_vplh", T_REMOVE_DEVICE_QUIRK, 5},
143 {"add_quirk", T_ADD_QUIRK, 1},
144 {"remove_quirk", T_REMOVE_QUIRK, 1},
145 {"dump_quirk_names", T_DUMP_QUIRK_NAMES, 0},
146 {"dump_device_quirks", T_DUMP_DEVICE_QUIRKS, 0},
147 {"dump_device_desc", T_DUMP_DEVICE_DESC, 0},
148 {"dump_curr_config_desc", T_DUMP_CURR_CONFIG_DESC, 0},
149 {"dump_all_config_desc", T_DUMP_ALL_CONFIG_DESC, 0},
150 {"dump_string", T_DUMP_STRING, 1},
151 {"dump_info", T_DUMP_INFO, 0},
152 {"show_ifdrv", T_SHOW_IFACE_DRIVER, 0},
153 {"suspend", T_SUSPEND, 0},
154 {"resume", T_RESUME, 0},
155 {"power_off", T_POWER_OFF, 0},
156 {"power_save", T_POWER_SAVE, 0},
157 {"power_on", T_POWER_ON, 0},
158 {"reset", T_RESET, 0},
160 {"do_request", T_DO_REQUEST, 5},
164 be_dev_remove_quirk(struct libusb20_backend *pbe,
165 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
168 struct libusb20_quirk q;
171 memset(&q, 0, sizeof(q));
175 q.bcdDeviceLow = lorev;
176 q.bcdDeviceHigh = hirev;
177 strlcpy(q.quirkname, str, sizeof(q.quirkname));
179 error = libusb20_be_remove_dev_quirk(pbe, &q);
181 fprintf(stderr, "Removing quirk '%s' failed, continuing.\n", str);
187 be_dev_add_quirk(struct libusb20_backend *pbe,
188 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
191 struct libusb20_quirk q;
194 memset(&q, 0, sizeof(q));
198 q.bcdDeviceLow = lorev;
199 q.bcdDeviceHigh = hirev;
200 strlcpy(q.quirkname, str, sizeof(q.quirkname));
202 error = libusb20_be_add_dev_quirk(pbe, &q);
204 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> [cmds...]" "\n"
274 "usage: usbconfig -d [ugen]<busnum>.<devaddr> -i <ifaceindex> [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 " dump_quirk_names" "\n"
285 " dump_device_quirks" "\n"
286 " dump_device_desc" "\n"
287 " dump_curr_config_desc" "\n"
288 " dump_all_config_desc" "\n"
289 " dump_string <index>" "\n"
299 " do_request <bmReqTyp> <bReq> <wVal> <wIdx> <wLen> <data...>" "\n"
305 reset_options(struct options *opt)
309 memset(opt, 0, sizeof(*opt));
314 flush_command(struct libusb20_backend *pbe, struct options *opt)
316 struct libusb20_device *pdev = NULL;
317 uint32_t matches = 0;
320 /* check for invalid option combinations */
321 if ((opt->got_suspend +
324 opt->got_set_config +
326 opt->got_power_save +
328 opt->got_power_off) > 1) {
329 err(1, "can only specify one of 'set_config', "
330 "'set_alt', 'reset', 'suspend', 'resume', "
331 "'power_save', 'power_on' and 'power_off' "
332 "at the same time!");
334 if (opt->got_dump_quirk_names) {
336 dump_be_quirk_names(pbe);
338 if (opt->got_dump_device_quirks) {
340 dump_be_dev_quirks(pbe);
342 if (opt->got_remove_device_quirk) {
344 be_dev_remove_quirk(pbe,
345 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
347 if (opt->got_add_device_quirk) {
349 be_dev_add_quirk(pbe,
350 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
352 if (opt->got_set_template) {
354 if (libusb20_be_set_template(pbe, opt->template)) {
355 fprintf(stderr, "Setting USB template %u failed, "
356 "continuing.\n", opt->template);
359 if (opt->got_get_template) {
361 if (libusb20_be_get_template(pbe, &opt->template))
362 printf("USB template: <unknown>\n");
364 printf("USB template: %u\n", opt->template);
366 if (opt->got_any == 0) {
368 * do not scan through all the devices if there are no valid
373 while ((pdev = libusb20_be_device_foreach(pbe, pdev))) {
376 (libusb20_dev_get_bus_number(pdev) != opt->bus)) {
380 (libusb20_dev_get_address(pdev) != opt->addr)) {
385 if (opt->got_remove_quirk) {
386 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
388 ddesc = libusb20_dev_get_device_desc(pdev);
390 be_dev_remove_quirk(pbe,
391 ddesc->idVendor, ddesc->idProduct,
392 ddesc->bcdDevice, ddesc->bcdDevice,
396 if (opt->got_add_quirk) {
397 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
399 ddesc = libusb20_dev_get_device_desc(pdev);
401 be_dev_add_quirk(pbe,
402 ddesc->idVendor, ddesc->idProduct,
403 ddesc->bcdDevice, ddesc->bcdDevice,
407 if (libusb20_dev_open(pdev, 0)) {
408 err(1, "could not open device");
410 if (opt->got_dump_string) {
411 dump_string_by_index(pdev, opt->string_index);
413 if (opt->got_do_request) {
417 if (libusb20_dev_request_sync(pdev, &opt->setup,
418 opt->buffer, &actlen, 5000 /* 5 seconds */ , 0)) {
419 printf("REQUEST = <ERROR>\n");
420 } else if (!(opt->setup.bmRequestType &
421 LIBUSB20_ENDPOINT_IN)) {
422 printf("REQUEST = <OK>\n");
425 printf("REQUEST = <");
426 for (t = 0; t != actlen; t++) {
428 ((uint8_t *)opt->buffer)[t],
429 (t == (actlen - 1)) ? "" : " ");
432 for (t = 0; t != actlen; t++) {
435 c = ((uint8_t *)opt->buffer)[t];
437 (c != '>') && isprint(c)) {
444 if (opt->got_set_config) {
445 if (libusb20_dev_set_config_index(pdev,
446 opt->config_index)) {
447 err(1, "could not set config index");
450 if (opt->got_set_alt) {
451 if (libusb20_dev_set_alt_index(pdev, opt->iface,
453 err(1, "could not set alternate setting");
456 if (opt->got_reset) {
457 if (libusb20_dev_reset(pdev)) {
458 err(1, "could not reset device");
461 if (opt->got_suspend) {
462 if (libusb20_dev_set_power_mode(pdev,
463 LIBUSB20_POWER_SUSPEND)) {
464 err(1, "could not set suspend");
467 if (opt->got_resume) {
468 if (libusb20_dev_set_power_mode(pdev,
469 LIBUSB20_POWER_RESUME)) {
470 err(1, "could not set resume");
473 if (opt->got_power_off) {
474 if (libusb20_dev_set_power_mode(pdev,
475 LIBUSB20_POWER_OFF)) {
476 err(1, "could not set power OFF");
479 if (opt->got_power_save) {
480 if (libusb20_dev_set_power_mode(pdev,
481 LIBUSB20_POWER_SAVE)) {
482 err(1, "could not set power SAVE");
485 if (opt->got_power_on) {
486 if (libusb20_dev_set_power_mode(pdev,
487 LIBUSB20_POWER_ON)) {
488 err(1, "could not set power ON");
492 (opt->got_dump_device_desc ||
493 opt->got_dump_curr_config ||
494 opt->got_dump_all_config ||
497 if (opt->got_list || dump_any) {
498 dump_device_info(pdev,
499 opt->got_show_iface_driver);
501 if (opt->got_dump_device_desc) {
503 dump_device_desc(pdev);
505 if (opt->got_dump_all_config) {
507 dump_config(pdev, 1);
508 } else if (opt->got_dump_curr_config) {
510 dump_config(pdev, 0);
515 if (libusb20_dev_close(pdev)) {
516 err(1, "could not close device");
521 printf("No device match or lack of permissions.\n");
530 main(int argc, char **argv)
532 struct libusb20_backend *pbe;
533 struct options *opt = &options;
543 pbe = libusb20_be_alloc_default();
545 err(1, "could not access USB backend\n");
547 for (n = 1; n != argc; n++) {
549 /* get number of additional options */
553 switch (get_token(argv[n], t)) {
555 if (opt->got_add_quirk) {
556 flush_command(pbe, opt);
558 opt->quirkname = argv[n + 1];
561 opt->got_add_quirk = 1;
566 if (opt->got_remove_quirk) {
567 flush_command(pbe, opt);
569 opt->quirkname = argv[n + 1];
572 opt->got_remove_quirk = 1;
576 case T_ADD_DEVICE_QUIRK:
577 if (opt->got_add_device_quirk) {
578 flush_command(pbe, opt);
580 opt->vid = num_id(argv[n + 1], "Vendor ID");
581 opt->pid = num_id(argv[n + 2], "Product ID");
582 opt->lo_rev = num_id(argv[n + 3], "Low Revision");
583 opt->hi_rev = num_id(argv[n + 4], "High Revision");
584 opt->quirkname = argv[n + 5];
587 opt->got_add_device_quirk = 1;
591 case T_REMOVE_DEVICE_QUIRK:
592 if (opt->got_remove_device_quirk) {
593 flush_command(pbe, opt);
595 opt->vid = num_id(argv[n + 1], "Vendor ID");
596 opt->pid = num_id(argv[n + 2], "Product ID");
597 opt->lo_rev = num_id(argv[n + 3], "Low Revision");
598 opt->hi_rev = num_id(argv[n + 4], "High Revision");
599 opt->quirkname = argv[n + 5];
601 opt->got_remove_device_quirk = 1;
605 case T_DUMP_QUIRK_NAMES:
606 if (opt->got_dump_quirk_names)
607 duplicate_option(argv[n]);
608 opt->got_dump_quirk_names = 1;
612 case T_DUMP_DEVICE_QUIRKS:
613 if (opt->got_dump_device_quirks)
614 duplicate_option(argv[n]);
615 opt->got_dump_device_quirks = 1;
619 case T_SHOW_IFACE_DRIVER:
620 opt->got_show_iface_driver = 1;
625 /* allow multiple commands on the same line */
626 flush_command(pbe, opt);
630 if ((ptr[0] == 'u') &&
636 if ((sscanf(ptr, "%d.%d",
637 &unit, &addr) != 2) ||
638 (unit < 0) || (unit > 65535) ||
639 (addr < 0) || (addr > 65535)) {
641 "parse '%s'", argv[n + 1]);
652 /* allow multiple commands on the same line */
653 flush_command(pbe, opt);
655 opt->bus = num_id(argv[n + 1], "busnum");
660 opt->addr = num_id(argv[n + 1], "addr");
665 opt->iface = num_id(argv[n + 1], "iface");
670 if (opt->got_set_config)
671 duplicate_option(argv[n]);
672 opt->config_index = num_id(argv[n + 1], "cfg_index");
673 opt->got_set_config = 1;
678 if (opt->got_set_alt)
679 duplicate_option(argv[n]);
680 opt->alt_index = num_id(argv[n + 1], "cfg_index");
681 opt->got_set_alt = 1;
686 if (opt->got_set_template)
687 duplicate_option(argv[n]);
688 opt->template = get_int(argv[n + 1]);
689 opt->got_set_template = 1;
694 if (opt->got_get_template)
695 duplicate_option(argv[n]);
696 opt->got_get_template = 1;
699 case T_DUMP_DEVICE_DESC:
700 if (opt->got_dump_device_desc)
701 duplicate_option(argv[n]);
702 opt->got_dump_device_desc = 1;
705 case T_DUMP_CURR_CONFIG_DESC:
706 if (opt->got_dump_curr_config)
707 duplicate_option(argv[n]);
708 opt->got_dump_curr_config = 1;
711 case T_DUMP_ALL_CONFIG_DESC:
712 if (opt->got_dump_all_config)
713 duplicate_option(argv[n]);
714 opt->got_dump_all_config = 1;
718 if (opt->got_dump_info)
719 duplicate_option(argv[n]);
720 opt->got_dump_info = 1;
724 if (opt->got_dump_string)
725 duplicate_option(argv[n]);
726 opt->string_index = num_id(argv[n + 1], "str_index");
727 opt->got_dump_string = 1;
732 if (opt->got_suspend)
733 duplicate_option(argv[n]);
734 opt->got_suspend = 1;
739 duplicate_option(argv[n]);
744 if (opt->got_power_off)
745 duplicate_option(argv[n]);
746 opt->got_power_off = 1;
750 if (opt->got_power_save)
751 duplicate_option(argv[n]);
752 opt->got_power_save = 1;
756 if (opt->got_power_on)
757 duplicate_option(argv[n]);
758 opt->got_power_on = 1;
763 duplicate_option(argv[n]);
769 duplicate_option(argv[n]);
774 if (opt->got_do_request)
775 duplicate_option(argv[n]);
776 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &opt->setup);
777 opt->setup.bmRequestType = num_id(argv[n + 1], "bmReqTyp");
778 opt->setup.bRequest = num_id(argv[n + 2], "bReq");
779 opt->setup.wValue = num_id(argv[n + 3], "wVal");
780 opt->setup.wIndex = num_id(argv[n + 4], "wIndex");
781 opt->setup.wLength = num_id(argv[n + 5], "wLen");
782 if (opt->setup.wLength != 0) {
783 opt->buffer = malloc(opt->setup.wLength);
790 if (!(opt->setup.bmRequestType &
791 LIBUSB20_ENDPOINT_IN)) {
794 if (t < opt->setup.wLength) {
795 err(1, "request data missing");
797 t = opt->setup.wLength;
799 ((uint8_t *)opt->buffer)[t] =
800 num_id(argv[n + t + 1], "req_data");
802 n += opt->setup.wLength;
804 opt->got_do_request = 1;
813 /* flush out last command */
814 flush_command(pbe, opt);
816 /* list all the devices */
819 flush_command(pbe, opt);
822 libusb20_be_free(pbe);