3 * Copyright (c) 2008-2009 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/types.h>
38 #include <libusb20_desc.h>
44 const char *quirkname;
51 struct LIBUSB20_CONTROL_SETUP_DECODED setup;
57 uint16_t lo_rev; /* inclusive */
58 uint16_t hi_rev; /* inclusive */
66 uint8_t got_set_config:1;
67 uint8_t got_set_alt:1;
68 uint8_t got_set_template:1;
69 uint8_t got_get_template:1;
70 uint8_t got_suspend:1;
73 uint8_t got_power_off:1;
74 uint8_t got_power_save:1;
75 uint8_t got_power_on:1;
76 uint8_t got_dump_device_quirks:1;
77 uint8_t got_dump_quirk_names:1;
78 uint8_t got_dump_all_desc:1;
79 uint8_t got_dump_device_desc:1;
80 uint8_t got_dump_curr_config:1;
81 uint8_t got_dump_all_config:1;
82 uint8_t got_dump_info:1;
83 uint8_t got_dump_stats: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;
91 uint8_t got_detach_kernel_driver:1;
110 T_REMOVE_DEVICE_QUIRK,
114 T_DETACH_KERNEL_DRIVER,
116 T_DUMP_DEVICE_QUIRKS,
119 T_DUMP_CURR_CONFIG_DESC,
120 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 {"dump_stats", T_DUMP_STATS, 0},
159 {"show_ifdrv", T_SHOW_IFACE_DRIVER, 0},
160 {"suspend", T_SUSPEND, 0},
161 {"resume", T_RESUME, 0},
162 {"power_off", T_POWER_OFF, 0},
163 {"power_save", T_POWER_SAVE, 0},
164 {"power_on", T_POWER_ON, 0},
165 {"reset", T_RESET, 0},
167 {"do_request", T_DO_REQUEST, 5},
171 be_dev_remove_quirk(struct libusb20_backend *pbe,
172 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
175 struct libusb20_quirk q;
178 memset(&q, 0, sizeof(q));
182 q.bcdDeviceLow = lorev;
183 q.bcdDeviceHigh = hirev;
184 strlcpy(q.quirkname, str, sizeof(q.quirkname));
186 error = libusb20_be_remove_dev_quirk(pbe, &q);
188 fprintf(stderr, "Removing quirk '%s' failed, continuing.\n", str);
194 be_dev_add_quirk(struct libusb20_backend *pbe,
195 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
198 struct libusb20_quirk q;
201 memset(&q, 0, sizeof(q));
205 q.bcdDeviceLow = lorev;
206 q.bcdDeviceHigh = hirev;
207 strlcpy(q.quirkname, str, sizeof(q.quirkname));
209 error = libusb20_be_add_dev_quirk(pbe, &q);
211 fprintf(stderr, "Adding quirk '%s' failed, continuing.\n", str);
217 get_token(const char *str, uint8_t narg)
221 for (n = 0; n != (sizeof(token) / sizeof(token[0])); n++) {
222 if (strcasecmp(str, token[n].name) == 0) {
223 if (token[n].narg > narg) {
224 /* too few arguments */
227 return (token[n].value);
234 num_id(const char *name, const char *type)
240 val = strtoul(name, &ep, 0);
245 errx(1, "%s: illegal %s name", name, type);
251 get_int(const char *s)
257 val = strtoul(s, &ep, 0);
262 errx(1, "illegal number: %s", s);
268 duplicate_option(const char *ptr)
270 fprintf(stderr, "Syntax error: "
271 "Duplicate option: '%s'\n", ptr);
279 "usbconfig - configure the USB subsystem" "\n"
280 "usage: usbconfig -u <busnum> -a <devaddr> -i <ifaceindex> [cmds...]" "\n"
281 "usage: usbconfig -d [ugen]<busnum>.<devaddr> -i <ifaceindex> [cmds...]" "\n"
283 " set_config <cfg_index>" "\n"
284 " set_alt <alt_index>" "\n"
285 " set_template <template>" "\n"
287 " add_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
288 " remove_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
289 " add_quirk <quirk>" "\n"
290 " remove_quirk <quirk>" "\n"
291 " detach_kernel_driver" "\n"
292 " dump_quirk_names" "\n"
293 " dump_device_quirks" "\n"
294 " dump_all_desc" "\n"
295 " dump_device_desc" "\n"
296 " dump_curr_config_desc" "\n"
297 " dump_all_config_desc" "\n"
298 " dump_string <index>" "\n"
309 " do_request <bmReqTyp> <bReq> <wVal> <wIdx> <wLen> <data...>" "\n"
315 reset_options(struct options *opt)
319 memset(opt, 0, sizeof(*opt));
324 flush_command(struct libusb20_backend *pbe, struct options *opt)
326 struct libusb20_device *pdev = NULL;
327 uint32_t matches = 0;
330 /* check for invalid option combinations */
331 if ((opt->got_suspend +
334 opt->got_set_config +
336 opt->got_power_save +
338 opt->got_power_off) > 1) {
339 err(1, "can only specify one of 'set_config', "
340 "'set_alt', 'reset', 'suspend', 'resume', "
341 "'power_save', 'power_on' and 'power_off' "
342 "at the same time!");
344 if (opt->got_dump_quirk_names) {
346 dump_be_quirk_names(pbe);
348 if (opt->got_dump_device_quirks) {
350 dump_be_dev_quirks(pbe);
352 if (opt->got_remove_device_quirk) {
354 be_dev_remove_quirk(pbe,
355 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
357 if (opt->got_add_device_quirk) {
359 be_dev_add_quirk(pbe,
360 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
362 if (opt->got_set_template) {
364 if (libusb20_be_set_template(pbe, opt->template)) {
365 fprintf(stderr, "Setting USB template %u failed, "
366 "continuing.\n", opt->template);
369 if (opt->got_get_template) {
371 if (libusb20_be_get_template(pbe, &opt->template))
372 printf("USB template: <unknown>\n");
374 printf("USB template: %u\n", opt->template);
376 if (opt->got_any == 0) {
378 * do not scan through all the devices if there are no valid
383 while ((pdev = libusb20_be_device_foreach(pbe, pdev))) {
386 (libusb20_dev_get_bus_number(pdev) != opt->bus)) {
390 (libusb20_dev_get_address(pdev) != opt->addr)) {
395 if (opt->got_remove_quirk) {
396 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
398 ddesc = libusb20_dev_get_device_desc(pdev);
400 be_dev_remove_quirk(pbe,
401 ddesc->idVendor, ddesc->idProduct,
402 ddesc->bcdDevice, ddesc->bcdDevice,
406 if (opt->got_add_quirk) {
407 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
409 ddesc = libusb20_dev_get_device_desc(pdev);
411 be_dev_add_quirk(pbe,
412 ddesc->idVendor, ddesc->idProduct,
413 ddesc->bcdDevice, ddesc->bcdDevice,
417 if (libusb20_dev_open(pdev, 0)) {
418 err(1, "could not open device");
420 if (opt->got_dump_string) {
421 dump_string_by_index(pdev, opt->string_index);
423 if (opt->got_do_request) {
427 if (libusb20_dev_request_sync(pdev, &opt->setup,
428 opt->buffer, &actlen, 5000 /* 5 seconds */ , 0)) {
429 printf("REQUEST = <ERROR>\n");
430 } else if (!(opt->setup.bmRequestType &
431 LIBUSB20_ENDPOINT_IN)) {
432 printf("REQUEST = <OK>\n");
435 printf("REQUEST = <");
436 for (t = 0; t != actlen; t++) {
438 ((uint8_t *)opt->buffer)[t],
439 (t == (actlen - 1)) ? "" : " ");
442 for (t = 0; t != actlen; t++) {
445 c = ((uint8_t *)opt->buffer)[t];
447 (c != '>') && isprint(c)) {
454 if (opt->got_set_config) {
455 if (libusb20_dev_set_config_index(pdev,
456 opt->config_index)) {
457 err(1, "could not set config index");
460 if (opt->got_set_alt) {
461 if (libusb20_dev_set_alt_index(pdev, opt->iface,
463 err(1, "could not set alternate setting");
466 if (opt->got_reset) {
467 if (libusb20_dev_reset(pdev)) {
468 err(1, "could not reset device");
471 if (opt->got_suspend) {
472 if (libusb20_dev_set_power_mode(pdev,
473 LIBUSB20_POWER_SUSPEND)) {
474 err(1, "could not set suspend");
477 if (opt->got_resume) {
478 if (libusb20_dev_set_power_mode(pdev,
479 LIBUSB20_POWER_RESUME)) {
480 err(1, "could not set resume");
483 if (opt->got_power_off) {
484 if (libusb20_dev_set_power_mode(pdev,
485 LIBUSB20_POWER_OFF)) {
486 err(1, "could not set power OFF");
489 if (opt->got_power_save) {
490 if (libusb20_dev_set_power_mode(pdev,
491 LIBUSB20_POWER_SAVE)) {
492 err(1, "could not set power SAVE");
495 if (opt->got_power_on) {
496 if (libusb20_dev_set_power_mode(pdev,
497 LIBUSB20_POWER_ON)) {
498 err(1, "could not set power ON");
501 if (opt->got_detach_kernel_driver) {
502 if (libusb20_dev_detach_kernel_driver(pdev, opt->iface)) {
503 err(1, "could not detach kernel driver");
507 (opt->got_dump_all_desc ||
508 opt->got_dump_device_desc ||
509 opt->got_dump_curr_config ||
510 opt->got_dump_all_config ||
511 opt->got_dump_info ||
512 opt->got_dump_stats);
514 if (opt->got_list || dump_any) {
515 dump_device_info(pdev,
516 opt->got_show_iface_driver);
518 if (opt->got_dump_device_desc) {
520 dump_device_desc(pdev);
522 if (opt->got_dump_all_config) {
524 dump_config(pdev, 1);
525 } else if (opt->got_dump_curr_config) {
527 dump_config(pdev, 0);
528 } else if (opt->got_dump_all_desc) {
530 dump_device_desc(pdev);
531 dump_config(pdev, 1);
533 if (opt->got_dump_stats) {
535 dump_device_stats(pdev);
540 if (libusb20_dev_close(pdev)) {
541 err(1, "could not close device");
546 printf("No device match or lack of permissions.\n");
555 main(int argc, char **argv)
557 struct libusb20_backend *pbe;
558 struct options *opt = &options;
568 pbe = libusb20_be_alloc_default();
570 err(1, "could not access USB backend\n");
572 for (n = 1; n != argc; n++) {
574 /* get number of additional options */
578 switch (get_token(argv[n], t)) {
580 if (opt->got_add_quirk) {
581 flush_command(pbe, opt);
583 opt->quirkname = argv[n + 1];
586 opt->got_add_quirk = 1;
591 if (opt->got_remove_quirk) {
592 flush_command(pbe, opt);
594 opt->quirkname = argv[n + 1];
597 opt->got_remove_quirk = 1;
601 case T_ADD_DEVICE_QUIRK:
602 if (opt->got_add_device_quirk) {
603 flush_command(pbe, opt);
605 opt->vid = num_id(argv[n + 1], "Vendor ID");
606 opt->pid = num_id(argv[n + 2], "Product ID");
607 opt->lo_rev = num_id(argv[n + 3], "Low Revision");
608 opt->hi_rev = num_id(argv[n + 4], "High Revision");
609 opt->quirkname = argv[n + 5];
612 opt->got_add_device_quirk = 1;
616 case T_REMOVE_DEVICE_QUIRK:
617 if (opt->got_remove_device_quirk) {
618 flush_command(pbe, opt);
620 opt->vid = num_id(argv[n + 1], "Vendor ID");
621 opt->pid = num_id(argv[n + 2], "Product ID");
622 opt->lo_rev = num_id(argv[n + 3], "Low Revision");
623 opt->hi_rev = num_id(argv[n + 4], "High Revision");
624 opt->quirkname = argv[n + 5];
626 opt->got_remove_device_quirk = 1;
630 case T_DETACH_KERNEL_DRIVER:
631 if (opt->got_detach_kernel_driver)
632 duplicate_option(argv[n]);
633 opt->got_detach_kernel_driver = 1;
637 case T_DUMP_QUIRK_NAMES:
638 if (opt->got_dump_quirk_names)
639 duplicate_option(argv[n]);
640 opt->got_dump_quirk_names = 1;
644 case T_DUMP_DEVICE_QUIRKS:
645 if (opt->got_dump_device_quirks)
646 duplicate_option(argv[n]);
647 opt->got_dump_device_quirks = 1;
651 case T_SHOW_IFACE_DRIVER:
652 opt->got_show_iface_driver = 1;
657 /* allow multiple commands on the same line */
658 flush_command(pbe, opt);
662 if ((ptr[0] == 'u') &&
668 if ((sscanf(ptr, "%d.%d",
669 &unit, &addr) != 2) ||
670 (unit < 0) || (unit > 65535) ||
671 (addr < 0) || (addr > 65535)) {
673 "parse '%s'", argv[n + 1]);
684 /* allow multiple commands on the same line */
685 flush_command(pbe, opt);
687 opt->bus = num_id(argv[n + 1], "busnum");
692 opt->addr = num_id(argv[n + 1], "addr");
697 opt->iface = num_id(argv[n + 1], "iface");
702 if (opt->got_set_config)
703 duplicate_option(argv[n]);
704 opt->config_index = num_id(argv[n + 1], "cfg_index");
705 opt->got_set_config = 1;
710 if (opt->got_set_alt)
711 duplicate_option(argv[n]);
712 opt->alt_index = num_id(argv[n + 1], "cfg_index");
713 opt->got_set_alt = 1;
718 if (opt->got_set_template)
719 duplicate_option(argv[n]);
720 opt->template = get_int(argv[n + 1]);
721 opt->got_set_template = 1;
726 if (opt->got_get_template)
727 duplicate_option(argv[n]);
728 opt->got_get_template = 1;
731 case T_DUMP_ALL_DESC:
732 if (opt->got_dump_all_desc)
733 duplicate_option(argv[n]);
734 opt->got_dump_all_desc = 1;
737 case T_DUMP_DEVICE_DESC:
738 if (opt->got_dump_device_desc)
739 duplicate_option(argv[n]);
740 opt->got_dump_device_desc = 1;
743 case T_DUMP_CURR_CONFIG_DESC:
744 if (opt->got_dump_curr_config)
745 duplicate_option(argv[n]);
746 opt->got_dump_curr_config = 1;
749 case T_DUMP_ALL_CONFIG_DESC:
750 if (opt->got_dump_all_config)
751 duplicate_option(argv[n]);
752 opt->got_dump_all_config = 1;
756 if (opt->got_dump_info)
757 duplicate_option(argv[n]);
758 opt->got_dump_info = 1;
762 if (opt->got_dump_stats)
763 duplicate_option(argv[n]);
764 opt->got_dump_stats = 1;
768 if (opt->got_dump_string)
769 duplicate_option(argv[n]);
770 opt->string_index = num_id(argv[n + 1], "str_index");
771 opt->got_dump_string = 1;
776 if (opt->got_suspend)
777 duplicate_option(argv[n]);
778 opt->got_suspend = 1;
783 duplicate_option(argv[n]);
788 if (opt->got_power_off)
789 duplicate_option(argv[n]);
790 opt->got_power_off = 1;
794 if (opt->got_power_save)
795 duplicate_option(argv[n]);
796 opt->got_power_save = 1;
800 if (opt->got_power_on)
801 duplicate_option(argv[n]);
802 opt->got_power_on = 1;
807 duplicate_option(argv[n]);
813 duplicate_option(argv[n]);
818 if (opt->got_do_request)
819 duplicate_option(argv[n]);
820 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &opt->setup);
821 opt->setup.bmRequestType = num_id(argv[n + 1], "bmReqTyp");
822 opt->setup.bRequest = num_id(argv[n + 2], "bReq");
823 opt->setup.wValue = num_id(argv[n + 3], "wVal");
824 opt->setup.wIndex = num_id(argv[n + 4], "wIndex");
825 opt->setup.wLength = num_id(argv[n + 5], "wLen");
826 if (opt->setup.wLength != 0) {
827 opt->buffer = malloc(opt->setup.wLength);
834 if (!(opt->setup.bmRequestType &
835 LIBUSB20_ENDPOINT_IN)) {
838 if (t < opt->setup.wLength) {
839 err(1, "request data missing");
841 t = opt->setup.wLength;
843 ((uint8_t *)opt->buffer)[t] =
844 num_id(argv[n + t + 1], "req_data");
846 n += opt->setup.wLength;
848 opt->got_do_request = 1;
855 if ((ptr[0] == 'u') &&
861 if ((sscanf(ptr, "%d.%d",
862 &unit, &addr) != 2) ||
863 (unit < 0) || (unit > 65535) ||
864 (addr < 0) || (addr > 65535)) {
880 /* flush out last command */
881 flush_command(pbe, opt);
883 /* list all the devices */
886 flush_command(pbe, opt);
889 libusb20_be_free(pbe);