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_show_iface_driver:1;
84 uint8_t got_remove_device_quirk:1;
85 uint8_t got_add_device_quirk:1;
86 uint8_t got_remove_quirk:1;
87 uint8_t got_add_quirk:1;
88 uint8_t got_dump_string:1;
89 uint8_t got_do_request:1;
108 T_REMOVE_DEVICE_QUIRK,
113 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_all_desc", T_DUMP_ALL_DESC, 0},
148 {"dump_device_desc", T_DUMP_DEVICE_DESC, 0},
149 {"dump_curr_config_desc", T_DUMP_CURR_CONFIG_DESC, 0},
150 {"dump_all_config_desc", T_DUMP_ALL_CONFIG_DESC, 0},
151 {"dump_string", T_DUMP_STRING, 1},
152 {"dump_info", T_DUMP_INFO, 0},
153 {"show_ifdrv", T_SHOW_IFACE_DRIVER, 0},
154 {"suspend", T_SUSPEND, 0},
155 {"resume", T_RESUME, 0},
156 {"power_off", T_POWER_OFF, 0},
157 {"power_save", T_POWER_SAVE, 0},
158 {"power_on", T_POWER_ON, 0},
159 {"reset", T_RESET, 0},
161 {"do_request", T_DO_REQUEST, 5},
165 be_dev_remove_quirk(struct libusb20_backend *pbe,
166 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
169 struct libusb20_quirk q;
172 memset(&q, 0, sizeof(q));
176 q.bcdDeviceLow = lorev;
177 q.bcdDeviceHigh = hirev;
178 strlcpy(q.quirkname, str, sizeof(q.quirkname));
180 error = libusb20_be_remove_dev_quirk(pbe, &q);
182 fprintf(stderr, "Removing quirk '%s' failed, continuing.\n", str);
188 be_dev_add_quirk(struct libusb20_backend *pbe,
189 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
192 struct libusb20_quirk q;
195 memset(&q, 0, sizeof(q));
199 q.bcdDeviceLow = lorev;
200 q.bcdDeviceHigh = hirev;
201 strlcpy(q.quirkname, str, sizeof(q.quirkname));
203 error = libusb20_be_add_dev_quirk(pbe, &q);
205 fprintf(stderr, "Adding quirk '%s' failed, continuing.\n", str);
211 get_token(const char *str, uint8_t narg)
215 for (n = 0; n != (sizeof(token) / sizeof(token[0])); n++) {
216 if (strcasecmp(str, token[n].name) == 0) {
217 if (token[n].narg > narg) {
218 /* too few arguments */
221 return (token[n].value);
228 num_id(const char *name, const char *type)
234 val = strtoul(name, &ep, 0);
239 errx(1, "%s: illegal %s name", name, type);
245 get_int(const char *s)
251 val = strtoul(s, &ep, 0);
256 errx(1, "illegal number: %s", s);
262 duplicate_option(const char *ptr)
264 fprintf(stderr, "Syntax error: "
265 "Duplicate option: '%s'\n", ptr);
273 "usbconfig - configure the USB subsystem" "\n"
274 "usage: usbconfig -u <busnum> -a <devaddr> -i <ifaceindex> [cmds...]" "\n"
275 "usage: usbconfig -d [ugen]<busnum>.<devaddr> -i <ifaceindex> [cmds...]" "\n"
277 " set_config <cfg_index>" "\n"
278 " set_alt <alt_index>" "\n"
279 " set_template <template>" "\n"
281 " add_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
282 " remove_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
283 " add_quirk <quirk>" "\n"
284 " remove_quirk <quirk>" "\n"
285 " dump_quirk_names" "\n"
286 " dump_device_quirks" "\n"
287 " dump_all_desc" "\n"
288 " dump_device_desc" "\n"
289 " dump_curr_config_desc" "\n"
290 " dump_all_config_desc" "\n"
291 " dump_string <index>" "\n"
301 " do_request <bmReqTyp> <bReq> <wVal> <wIdx> <wLen> <data...>" "\n"
307 reset_options(struct options *opt)
311 memset(opt, 0, sizeof(*opt));
316 flush_command(struct libusb20_backend *pbe, struct options *opt)
318 struct libusb20_device *pdev = NULL;
319 uint32_t matches = 0;
322 /* check for invalid option combinations */
323 if ((opt->got_suspend +
326 opt->got_set_config +
328 opt->got_power_save +
330 opt->got_power_off) > 1) {
331 err(1, "can only specify one of 'set_config', "
332 "'set_alt', 'reset', 'suspend', 'resume', "
333 "'power_save', 'power_on' and 'power_off' "
334 "at the same time!");
336 if (opt->got_dump_quirk_names) {
338 dump_be_quirk_names(pbe);
340 if (opt->got_dump_device_quirks) {
342 dump_be_dev_quirks(pbe);
344 if (opt->got_remove_device_quirk) {
346 be_dev_remove_quirk(pbe,
347 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
349 if (opt->got_add_device_quirk) {
351 be_dev_add_quirk(pbe,
352 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
354 if (opt->got_set_template) {
356 if (libusb20_be_set_template(pbe, opt->template)) {
357 fprintf(stderr, "Setting USB template %u failed, "
358 "continuing.\n", opt->template);
361 if (opt->got_get_template) {
363 if (libusb20_be_get_template(pbe, &opt->template))
364 printf("USB template: <unknown>\n");
366 printf("USB template: %u\n", opt->template);
368 if (opt->got_any == 0) {
370 * do not scan through all the devices if there are no valid
375 while ((pdev = libusb20_be_device_foreach(pbe, pdev))) {
378 (libusb20_dev_get_bus_number(pdev) != opt->bus)) {
382 (libusb20_dev_get_address(pdev) != opt->addr)) {
387 if (opt->got_remove_quirk) {
388 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
390 ddesc = libusb20_dev_get_device_desc(pdev);
392 be_dev_remove_quirk(pbe,
393 ddesc->idVendor, ddesc->idProduct,
394 ddesc->bcdDevice, ddesc->bcdDevice,
398 if (opt->got_add_quirk) {
399 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
401 ddesc = libusb20_dev_get_device_desc(pdev);
403 be_dev_add_quirk(pbe,
404 ddesc->idVendor, ddesc->idProduct,
405 ddesc->bcdDevice, ddesc->bcdDevice,
409 if (libusb20_dev_open(pdev, 0)) {
410 err(1, "could not open device");
412 if (opt->got_dump_string) {
413 dump_string_by_index(pdev, opt->string_index);
415 if (opt->got_do_request) {
419 if (libusb20_dev_request_sync(pdev, &opt->setup,
420 opt->buffer, &actlen, 5000 /* 5 seconds */ , 0)) {
421 printf("REQUEST = <ERROR>\n");
422 } else if (!(opt->setup.bmRequestType &
423 LIBUSB20_ENDPOINT_IN)) {
424 printf("REQUEST = <OK>\n");
427 printf("REQUEST = <");
428 for (t = 0; t != actlen; t++) {
430 ((uint8_t *)opt->buffer)[t],
431 (t == (actlen - 1)) ? "" : " ");
434 for (t = 0; t != actlen; t++) {
437 c = ((uint8_t *)opt->buffer)[t];
439 (c != '>') && isprint(c)) {
446 if (opt->got_set_config) {
447 if (libusb20_dev_set_config_index(pdev,
448 opt->config_index)) {
449 err(1, "could not set config index");
452 if (opt->got_set_alt) {
453 if (libusb20_dev_set_alt_index(pdev, opt->iface,
455 err(1, "could not set alternate setting");
458 if (opt->got_reset) {
459 if (libusb20_dev_reset(pdev)) {
460 err(1, "could not reset device");
463 if (opt->got_suspend) {
464 if (libusb20_dev_set_power_mode(pdev,
465 LIBUSB20_POWER_SUSPEND)) {
466 err(1, "could not set suspend");
469 if (opt->got_resume) {
470 if (libusb20_dev_set_power_mode(pdev,
471 LIBUSB20_POWER_RESUME)) {
472 err(1, "could not set resume");
475 if (opt->got_power_off) {
476 if (libusb20_dev_set_power_mode(pdev,
477 LIBUSB20_POWER_OFF)) {
478 err(1, "could not set power OFF");
481 if (opt->got_power_save) {
482 if (libusb20_dev_set_power_mode(pdev,
483 LIBUSB20_POWER_SAVE)) {
484 err(1, "could not set power SAVE");
487 if (opt->got_power_on) {
488 if (libusb20_dev_set_power_mode(pdev,
489 LIBUSB20_POWER_ON)) {
490 err(1, "could not set power ON");
494 (opt->got_dump_all_desc ||
495 opt->got_dump_device_desc ||
496 opt->got_dump_curr_config ||
497 opt->got_dump_all_config ||
500 if (opt->got_list || dump_any) {
501 dump_device_info(pdev,
502 opt->got_show_iface_driver);
504 if (opt->got_dump_device_desc) {
506 dump_device_desc(pdev);
508 if (opt->got_dump_all_config) {
510 dump_config(pdev, 1);
511 } else if (opt->got_dump_curr_config) {
513 dump_config(pdev, 0);
514 } else if (opt->got_dump_all_desc) {
516 dump_device_desc(pdev);
517 dump_config(pdev, 1);
522 if (libusb20_dev_close(pdev)) {
523 err(1, "could not close device");
528 printf("No device match or lack of permissions.\n");
537 main(int argc, char **argv)
539 struct libusb20_backend *pbe;
540 struct options *opt = &options;
550 pbe = libusb20_be_alloc_default();
552 err(1, "could not access USB backend\n");
554 for (n = 1; n != argc; n++) {
556 /* get number of additional options */
560 switch (get_token(argv[n], t)) {
562 if (opt->got_add_quirk) {
563 flush_command(pbe, opt);
565 opt->quirkname = argv[n + 1];
568 opt->got_add_quirk = 1;
573 if (opt->got_remove_quirk) {
574 flush_command(pbe, opt);
576 opt->quirkname = argv[n + 1];
579 opt->got_remove_quirk = 1;
583 case T_ADD_DEVICE_QUIRK:
584 if (opt->got_add_device_quirk) {
585 flush_command(pbe, opt);
587 opt->vid = num_id(argv[n + 1], "Vendor ID");
588 opt->pid = num_id(argv[n + 2], "Product ID");
589 opt->lo_rev = num_id(argv[n + 3], "Low Revision");
590 opt->hi_rev = num_id(argv[n + 4], "High Revision");
591 opt->quirkname = argv[n + 5];
594 opt->got_add_device_quirk = 1;
598 case T_REMOVE_DEVICE_QUIRK:
599 if (opt->got_remove_device_quirk) {
600 flush_command(pbe, opt);
602 opt->vid = num_id(argv[n + 1], "Vendor ID");
603 opt->pid = num_id(argv[n + 2], "Product ID");
604 opt->lo_rev = num_id(argv[n + 3], "Low Revision");
605 opt->hi_rev = num_id(argv[n + 4], "High Revision");
606 opt->quirkname = argv[n + 5];
608 opt->got_remove_device_quirk = 1;
612 case T_DUMP_QUIRK_NAMES:
613 if (opt->got_dump_quirk_names)
614 duplicate_option(argv[n]);
615 opt->got_dump_quirk_names = 1;
619 case T_DUMP_DEVICE_QUIRKS:
620 if (opt->got_dump_device_quirks)
621 duplicate_option(argv[n]);
622 opt->got_dump_device_quirks = 1;
626 case T_SHOW_IFACE_DRIVER:
627 opt->got_show_iface_driver = 1;
632 /* allow multiple commands on the same line */
633 flush_command(pbe, opt);
637 if ((ptr[0] == 'u') &&
643 if ((sscanf(ptr, "%d.%d",
644 &unit, &addr) != 2) ||
645 (unit < 0) || (unit > 65535) ||
646 (addr < 0) || (addr > 65535)) {
648 "parse '%s'", argv[n + 1]);
659 /* allow multiple commands on the same line */
660 flush_command(pbe, opt);
662 opt->bus = num_id(argv[n + 1], "busnum");
667 opt->addr = num_id(argv[n + 1], "addr");
672 opt->iface = num_id(argv[n + 1], "iface");
677 if (opt->got_set_config)
678 duplicate_option(argv[n]);
679 opt->config_index = num_id(argv[n + 1], "cfg_index");
680 opt->got_set_config = 1;
685 if (opt->got_set_alt)
686 duplicate_option(argv[n]);
687 opt->alt_index = num_id(argv[n + 1], "cfg_index");
688 opt->got_set_alt = 1;
693 if (opt->got_set_template)
694 duplicate_option(argv[n]);
695 opt->template = get_int(argv[n + 1]);
696 opt->got_set_template = 1;
701 if (opt->got_get_template)
702 duplicate_option(argv[n]);
703 opt->got_get_template = 1;
706 case T_DUMP_ALL_DESC:
707 if (opt->got_dump_all_desc)
708 duplicate_option(argv[n]);
709 opt->got_dump_all_desc = 1;
712 case T_DUMP_DEVICE_DESC:
713 if (opt->got_dump_device_desc)
714 duplicate_option(argv[n]);
715 opt->got_dump_device_desc = 1;
718 case T_DUMP_CURR_CONFIG_DESC:
719 if (opt->got_dump_curr_config)
720 duplicate_option(argv[n]);
721 opt->got_dump_curr_config = 1;
724 case T_DUMP_ALL_CONFIG_DESC:
725 if (opt->got_dump_all_config)
726 duplicate_option(argv[n]);
727 opt->got_dump_all_config = 1;
731 if (opt->got_dump_info)
732 duplicate_option(argv[n]);
733 opt->got_dump_info = 1;
737 if (opt->got_dump_string)
738 duplicate_option(argv[n]);
739 opt->string_index = num_id(argv[n + 1], "str_index");
740 opt->got_dump_string = 1;
745 if (opt->got_suspend)
746 duplicate_option(argv[n]);
747 opt->got_suspend = 1;
752 duplicate_option(argv[n]);
757 if (opt->got_power_off)
758 duplicate_option(argv[n]);
759 opt->got_power_off = 1;
763 if (opt->got_power_save)
764 duplicate_option(argv[n]);
765 opt->got_power_save = 1;
769 if (opt->got_power_on)
770 duplicate_option(argv[n]);
771 opt->got_power_on = 1;
776 duplicate_option(argv[n]);
782 duplicate_option(argv[n]);
787 if (opt->got_do_request)
788 duplicate_option(argv[n]);
789 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &opt->setup);
790 opt->setup.bmRequestType = num_id(argv[n + 1], "bmReqTyp");
791 opt->setup.bRequest = num_id(argv[n + 2], "bReq");
792 opt->setup.wValue = num_id(argv[n + 3], "wVal");
793 opt->setup.wIndex = num_id(argv[n + 4], "wIndex");
794 opt->setup.wLength = num_id(argv[n + 5], "wLen");
795 if (opt->setup.wLength != 0) {
796 opt->buffer = malloc(opt->setup.wLength);
803 if (!(opt->setup.bmRequestType &
804 LIBUSB20_ENDPOINT_IN)) {
807 if (t < opt->setup.wLength) {
808 err(1, "request data missing");
810 t = opt->setup.wLength;
812 ((uint8_t *)opt->buffer)[t] =
813 num_id(argv[n + t + 1], "req_data");
815 n += opt->setup.wLength;
817 opt->got_do_request = 1;
824 if ((ptr[0] == 'u') &&
830 if ((sscanf(ptr, "%d.%d",
831 &unit, &addr) != 2) ||
832 (unit < 0) || (unit > 65535) ||
833 (addr < 0) || (addr > 65535)) {
849 /* flush out last command */
850 flush_command(pbe, opt);
852 /* list all the devices */
855 flush_command(pbe, opt);
858 libusb20_be_free(pbe);