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;
92 uint8_t got_detach_kernel_driver:1;
111 T_REMOVE_DEVICE_QUIRK,
115 T_DETACH_KERNEL_DRIVER,
117 T_DUMP_DEVICE_QUIRKS,
120 T_DUMP_CURR_CONFIG_DESC,
121 T_DUMP_ALL_CONFIG_DESC,
134 static struct options options;
136 static const struct token token[] = {
141 {"set_config", T_SET_CONFIG, 1},
142 {"set_alt", T_SET_ALT, 1},
143 {"set_template", T_SET_TEMPLATE, 1},
144 {"get_template", T_GET_TEMPLATE, 0},
145 {"add_dev_quirk_vplh", T_ADD_DEVICE_QUIRK, 5},
146 {"remove_dev_quirk_vplh", T_REMOVE_DEVICE_QUIRK, 5},
147 {"add_quirk", T_ADD_QUIRK, 1},
148 {"remove_quirk", T_REMOVE_QUIRK, 1},
149 {"detach_kernel_driver", T_DETACH_KERNEL_DRIVER, 0},
150 {"dump_quirk_names", T_DUMP_QUIRK_NAMES, 0},
151 {"dump_device_quirks", T_DUMP_DEVICE_QUIRKS, 0},
152 {"dump_all_desc", T_DUMP_ALL_DESC, 0},
153 {"dump_device_desc", T_DUMP_DEVICE_DESC, 0},
154 {"dump_curr_config_desc", T_DUMP_CURR_CONFIG_DESC, 0},
155 {"dump_all_config_desc", T_DUMP_ALL_CONFIG_DESC, 0},
156 {"dump_string", T_DUMP_STRING, 1},
157 {"dump_info", T_DUMP_INFO, 0},
158 {"show_ifdrv", T_SHOW_IFACE_DRIVER, 0},
159 {"suspend", T_SUSPEND, 0},
160 {"resume", T_RESUME, 0},
161 {"power_off", T_POWER_OFF, 0},
162 {"power_save", T_POWER_SAVE, 0},
163 {"power_on", T_POWER_ON, 0},
164 {"reset", T_RESET, 0},
166 {"do_request", T_DO_REQUEST, 5},
170 be_dev_remove_quirk(struct libusb20_backend *pbe,
171 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
174 struct libusb20_quirk q;
177 memset(&q, 0, sizeof(q));
181 q.bcdDeviceLow = lorev;
182 q.bcdDeviceHigh = hirev;
183 strlcpy(q.quirkname, str, sizeof(q.quirkname));
185 error = libusb20_be_remove_dev_quirk(pbe, &q);
187 fprintf(stderr, "Removing quirk '%s' failed, continuing.\n", str);
193 be_dev_add_quirk(struct libusb20_backend *pbe,
194 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
197 struct libusb20_quirk q;
200 memset(&q, 0, sizeof(q));
204 q.bcdDeviceLow = lorev;
205 q.bcdDeviceHigh = hirev;
206 strlcpy(q.quirkname, str, sizeof(q.quirkname));
208 error = libusb20_be_add_dev_quirk(pbe, &q);
210 fprintf(stderr, "Adding quirk '%s' failed, continuing.\n", str);
216 get_token(const char *str, uint8_t narg)
220 for (n = 0; n != (sizeof(token) / sizeof(token[0])); n++) {
221 if (strcasecmp(str, token[n].name) == 0) {
222 if (token[n].narg > narg) {
223 /* too few arguments */
226 return (token[n].value);
233 num_id(const char *name, const char *type)
239 val = strtoul(name, &ep, 0);
244 errx(1, "%s: illegal %s name", name, type);
250 get_int(const char *s)
256 val = strtoul(s, &ep, 0);
261 errx(1, "illegal number: %s", s);
267 duplicate_option(const char *ptr)
269 fprintf(stderr, "Syntax error: "
270 "Duplicate option: '%s'\n", ptr);
278 "usbconfig - configure the USB subsystem" "\n"
279 "usage: usbconfig -u <busnum> -a <devaddr> -i <ifaceindex> [cmds...]" "\n"
280 "usage: usbconfig -d [ugen]<busnum>.<devaddr> -i <ifaceindex> [cmds...]" "\n"
282 " set_config <cfg_index>" "\n"
283 " set_alt <alt_index>" "\n"
284 " set_template <template>" "\n"
286 " add_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
287 " remove_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
288 " add_quirk <quirk>" "\n"
289 " remove_quirk <quirk>" "\n"
290 " detach_kernel_driver" "\n"
291 " dump_quirk_names" "\n"
292 " dump_device_quirks" "\n"
293 " dump_all_desc" "\n"
294 " dump_device_desc" "\n"
295 " dump_curr_config_desc" "\n"
296 " dump_all_config_desc" "\n"
297 " dump_string <index>" "\n"
307 " do_request <bmReqTyp> <bReq> <wVal> <wIdx> <wLen> <data...>" "\n"
313 reset_options(struct options *opt)
317 memset(opt, 0, sizeof(*opt));
322 flush_command(struct libusb20_backend *pbe, struct options *opt)
324 struct libusb20_device *pdev = NULL;
325 uint32_t matches = 0;
328 /* check for invalid option combinations */
329 if ((opt->got_suspend +
332 opt->got_set_config +
334 opt->got_power_save +
336 opt->got_power_off) > 1) {
337 err(1, "can only specify one of 'set_config', "
338 "'set_alt', 'reset', 'suspend', 'resume', "
339 "'power_save', 'power_on' and 'power_off' "
340 "at the same time!");
342 if (opt->got_dump_quirk_names) {
344 dump_be_quirk_names(pbe);
346 if (opt->got_dump_device_quirks) {
348 dump_be_dev_quirks(pbe);
350 if (opt->got_remove_device_quirk) {
352 be_dev_remove_quirk(pbe,
353 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
355 if (opt->got_add_device_quirk) {
357 be_dev_add_quirk(pbe,
358 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
360 if (opt->got_set_template) {
362 if (libusb20_be_set_template(pbe, opt->template)) {
363 fprintf(stderr, "Setting USB template %u failed, "
364 "continuing.\n", opt->template);
367 if (opt->got_get_template) {
369 if (libusb20_be_get_template(pbe, &opt->template))
370 printf("USB template: <unknown>\n");
372 printf("USB template: %u\n", opt->template);
374 if (opt->got_any == 0) {
376 * do not scan through all the devices if there are no valid
381 while ((pdev = libusb20_be_device_foreach(pbe, pdev))) {
384 (libusb20_dev_get_bus_number(pdev) != opt->bus)) {
388 (libusb20_dev_get_address(pdev) != opt->addr)) {
393 if (opt->got_remove_quirk) {
394 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
396 ddesc = libusb20_dev_get_device_desc(pdev);
398 be_dev_remove_quirk(pbe,
399 ddesc->idVendor, ddesc->idProduct,
400 ddesc->bcdDevice, ddesc->bcdDevice,
404 if (opt->got_add_quirk) {
405 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
407 ddesc = libusb20_dev_get_device_desc(pdev);
409 be_dev_add_quirk(pbe,
410 ddesc->idVendor, ddesc->idProduct,
411 ddesc->bcdDevice, ddesc->bcdDevice,
415 if (libusb20_dev_open(pdev, 0)) {
416 err(1, "could not open device");
418 if (opt->got_dump_string) {
419 dump_string_by_index(pdev, opt->string_index);
421 if (opt->got_do_request) {
425 if (libusb20_dev_request_sync(pdev, &opt->setup,
426 opt->buffer, &actlen, 5000 /* 5 seconds */ , 0)) {
427 printf("REQUEST = <ERROR>\n");
428 } else if (!(opt->setup.bmRequestType &
429 LIBUSB20_ENDPOINT_IN)) {
430 printf("REQUEST = <OK>\n");
433 printf("REQUEST = <");
434 for (t = 0; t != actlen; t++) {
436 ((uint8_t *)opt->buffer)[t],
437 (t == (actlen - 1)) ? "" : " ");
440 for (t = 0; t != actlen; t++) {
443 c = ((uint8_t *)opt->buffer)[t];
445 (c != '>') && isprint(c)) {
452 if (opt->got_set_config) {
453 if (libusb20_dev_set_config_index(pdev,
454 opt->config_index)) {
455 err(1, "could not set config index");
458 if (opt->got_set_alt) {
459 if (libusb20_dev_set_alt_index(pdev, opt->iface,
461 err(1, "could not set alternate setting");
464 if (opt->got_reset) {
465 if (libusb20_dev_reset(pdev)) {
466 err(1, "could not reset device");
469 if (opt->got_suspend) {
470 if (libusb20_dev_set_power_mode(pdev,
471 LIBUSB20_POWER_SUSPEND)) {
472 err(1, "could not set suspend");
475 if (opt->got_resume) {
476 if (libusb20_dev_set_power_mode(pdev,
477 LIBUSB20_POWER_RESUME)) {
478 err(1, "could not set resume");
481 if (opt->got_power_off) {
482 if (libusb20_dev_set_power_mode(pdev,
483 LIBUSB20_POWER_OFF)) {
484 err(1, "could not set power OFF");
487 if (opt->got_power_save) {
488 if (libusb20_dev_set_power_mode(pdev,
489 LIBUSB20_POWER_SAVE)) {
490 err(1, "could not set power SAVE");
493 if (opt->got_power_on) {
494 if (libusb20_dev_set_power_mode(pdev,
495 LIBUSB20_POWER_ON)) {
496 err(1, "could not set power ON");
499 if (opt->got_detach_kernel_driver) {
500 if (libusb20_dev_detach_kernel_driver(pdev, opt->iface)) {
501 err(1, "could not detach kernel driver");
505 (opt->got_dump_all_desc ||
506 opt->got_dump_device_desc ||
507 opt->got_dump_curr_config ||
508 opt->got_dump_all_config ||
511 if (opt->got_list || dump_any) {
512 dump_device_info(pdev,
513 opt->got_show_iface_driver);
515 if (opt->got_dump_device_desc) {
517 dump_device_desc(pdev);
519 if (opt->got_dump_all_config) {
521 dump_config(pdev, 1);
522 } else if (opt->got_dump_curr_config) {
524 dump_config(pdev, 0);
525 } else if (opt->got_dump_all_desc) {
527 dump_device_desc(pdev);
528 dump_config(pdev, 1);
533 if (libusb20_dev_close(pdev)) {
534 err(1, "could not close device");
539 printf("No device match or lack of permissions.\n");
548 main(int argc, char **argv)
550 struct libusb20_backend *pbe;
551 struct options *opt = &options;
561 pbe = libusb20_be_alloc_default();
563 err(1, "could not access USB backend\n");
565 for (n = 1; n != argc; n++) {
567 /* get number of additional options */
571 switch (get_token(argv[n], t)) {
573 if (opt->got_add_quirk) {
574 flush_command(pbe, opt);
576 opt->quirkname = argv[n + 1];
579 opt->got_add_quirk = 1;
584 if (opt->got_remove_quirk) {
585 flush_command(pbe, opt);
587 opt->quirkname = argv[n + 1];
590 opt->got_remove_quirk = 1;
594 case T_ADD_DEVICE_QUIRK:
595 if (opt->got_add_device_quirk) {
596 flush_command(pbe, opt);
598 opt->vid = num_id(argv[n + 1], "Vendor ID");
599 opt->pid = num_id(argv[n + 2], "Product ID");
600 opt->lo_rev = num_id(argv[n + 3], "Low Revision");
601 opt->hi_rev = num_id(argv[n + 4], "High Revision");
602 opt->quirkname = argv[n + 5];
605 opt->got_add_device_quirk = 1;
609 case T_REMOVE_DEVICE_QUIRK:
610 if (opt->got_remove_device_quirk) {
611 flush_command(pbe, opt);
613 opt->vid = num_id(argv[n + 1], "Vendor ID");
614 opt->pid = num_id(argv[n + 2], "Product ID");
615 opt->lo_rev = num_id(argv[n + 3], "Low Revision");
616 opt->hi_rev = num_id(argv[n + 4], "High Revision");
617 opt->quirkname = argv[n + 5];
619 opt->got_remove_device_quirk = 1;
623 case T_DETACH_KERNEL_DRIVER:
624 if (opt->got_detach_kernel_driver)
625 duplicate_option(argv[n]);
626 opt->got_detach_kernel_driver = 1;
630 case T_DUMP_QUIRK_NAMES:
631 if (opt->got_dump_quirk_names)
632 duplicate_option(argv[n]);
633 opt->got_dump_quirk_names = 1;
637 case T_DUMP_DEVICE_QUIRKS:
638 if (opt->got_dump_device_quirks)
639 duplicate_option(argv[n]);
640 opt->got_dump_device_quirks = 1;
644 case T_SHOW_IFACE_DRIVER:
645 opt->got_show_iface_driver = 1;
650 /* allow multiple commands on the same line */
651 flush_command(pbe, opt);
655 if ((ptr[0] == 'u') &&
661 if ((sscanf(ptr, "%d.%d",
662 &unit, &addr) != 2) ||
663 (unit < 0) || (unit > 65535) ||
664 (addr < 0) || (addr > 65535)) {
666 "parse '%s'", argv[n + 1]);
677 /* allow multiple commands on the same line */
678 flush_command(pbe, opt);
680 opt->bus = num_id(argv[n + 1], "busnum");
685 opt->addr = num_id(argv[n + 1], "addr");
690 opt->iface = num_id(argv[n + 1], "iface");
695 if (opt->got_set_config)
696 duplicate_option(argv[n]);
697 opt->config_index = num_id(argv[n + 1], "cfg_index");
698 opt->got_set_config = 1;
703 if (opt->got_set_alt)
704 duplicate_option(argv[n]);
705 opt->alt_index = num_id(argv[n + 1], "cfg_index");
706 opt->got_set_alt = 1;
711 if (opt->got_set_template)
712 duplicate_option(argv[n]);
713 opt->template = get_int(argv[n + 1]);
714 opt->got_set_template = 1;
719 if (opt->got_get_template)
720 duplicate_option(argv[n]);
721 opt->got_get_template = 1;
724 case T_DUMP_ALL_DESC:
725 if (opt->got_dump_all_desc)
726 duplicate_option(argv[n]);
727 opt->got_dump_all_desc = 1;
730 case T_DUMP_DEVICE_DESC:
731 if (opt->got_dump_device_desc)
732 duplicate_option(argv[n]);
733 opt->got_dump_device_desc = 1;
736 case T_DUMP_CURR_CONFIG_DESC:
737 if (opt->got_dump_curr_config)
738 duplicate_option(argv[n]);
739 opt->got_dump_curr_config = 1;
742 case T_DUMP_ALL_CONFIG_DESC:
743 if (opt->got_dump_all_config)
744 duplicate_option(argv[n]);
745 opt->got_dump_all_config = 1;
749 if (opt->got_dump_info)
750 duplicate_option(argv[n]);
751 opt->got_dump_info = 1;
755 if (opt->got_dump_string)
756 duplicate_option(argv[n]);
757 opt->string_index = num_id(argv[n + 1], "str_index");
758 opt->got_dump_string = 1;
763 if (opt->got_suspend)
764 duplicate_option(argv[n]);
765 opt->got_suspend = 1;
770 duplicate_option(argv[n]);
775 if (opt->got_power_off)
776 duplicate_option(argv[n]);
777 opt->got_power_off = 1;
781 if (opt->got_power_save)
782 duplicate_option(argv[n]);
783 opt->got_power_save = 1;
787 if (opt->got_power_on)
788 duplicate_option(argv[n]);
789 opt->got_power_on = 1;
794 duplicate_option(argv[n]);
800 duplicate_option(argv[n]);
805 if (opt->got_do_request)
806 duplicate_option(argv[n]);
807 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &opt->setup);
808 opt->setup.bmRequestType = num_id(argv[n + 1], "bmReqTyp");
809 opt->setup.bRequest = num_id(argv[n + 2], "bReq");
810 opt->setup.wValue = num_id(argv[n + 3], "wVal");
811 opt->setup.wIndex = num_id(argv[n + 4], "wIndex");
812 opt->setup.wLength = num_id(argv[n + 5], "wLen");
813 if (opt->setup.wLength != 0) {
814 opt->buffer = malloc(opt->setup.wLength);
821 if (!(opt->setup.bmRequestType &
822 LIBUSB20_ENDPOINT_IN)) {
825 if (t < opt->setup.wLength) {
826 err(1, "request data missing");
828 t = opt->setup.wLength;
830 ((uint8_t *)opt->buffer)[t] =
831 num_id(argv[n + t + 1], "req_data");
833 n += opt->setup.wLength;
835 opt->got_do_request = 1;
842 if ((ptr[0] == 'u') &&
848 if ((sscanf(ptr, "%d.%d",
849 &unit, &addr) != 2) ||
850 (unit < 0) || (unit > 65535) ||
851 (addr < 0) || (addr > 65535)) {
867 /* flush out last command */
868 flush_command(pbe, opt);
870 /* list all the devices */
873 flush_command(pbe, opt);
876 libusb20_be_free(pbe);