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;
90 uint8_t got_detach_kernel_driver:1;
109 T_REMOVE_DEVICE_QUIRK,
113 T_DETACH_KERNEL_DRIVER,
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 {"detach_kernel_driver", T_DETACH_KERNEL_DRIVER, 0},
148 {"dump_quirk_names", T_DUMP_QUIRK_NAMES, 0},
149 {"dump_device_quirks", T_DUMP_DEVICE_QUIRKS, 0},
150 {"dump_all_desc", T_DUMP_ALL_DESC, 0},
151 {"dump_device_desc", T_DUMP_DEVICE_DESC, 0},
152 {"dump_curr_config_desc", T_DUMP_CURR_CONFIG_DESC, 0},
153 {"dump_all_config_desc", T_DUMP_ALL_CONFIG_DESC, 0},
154 {"dump_string", T_DUMP_STRING, 1},
155 {"dump_info", T_DUMP_INFO, 0},
156 {"show_ifdrv", T_SHOW_IFACE_DRIVER, 0},
157 {"suspend", T_SUSPEND, 0},
158 {"resume", T_RESUME, 0},
159 {"power_off", T_POWER_OFF, 0},
160 {"power_save", T_POWER_SAVE, 0},
161 {"power_on", T_POWER_ON, 0},
162 {"reset", T_RESET, 0},
164 {"do_request", T_DO_REQUEST, 5},
168 be_dev_remove_quirk(struct libusb20_backend *pbe,
169 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
172 struct libusb20_quirk q;
175 memset(&q, 0, sizeof(q));
179 q.bcdDeviceLow = lorev;
180 q.bcdDeviceHigh = hirev;
181 strlcpy(q.quirkname, str, sizeof(q.quirkname));
183 error = libusb20_be_remove_dev_quirk(pbe, &q);
185 fprintf(stderr, "Removing quirk '%s' failed, continuing.\n", str);
191 be_dev_add_quirk(struct libusb20_backend *pbe,
192 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
195 struct libusb20_quirk q;
198 memset(&q, 0, sizeof(q));
202 q.bcdDeviceLow = lorev;
203 q.bcdDeviceHigh = hirev;
204 strlcpy(q.quirkname, str, sizeof(q.quirkname));
206 error = libusb20_be_add_dev_quirk(pbe, &q);
208 fprintf(stderr, "Adding quirk '%s' failed, continuing.\n", str);
214 get_token(const char *str, uint8_t narg)
218 for (n = 0; n != (sizeof(token) / sizeof(token[0])); n++) {
219 if (strcasecmp(str, token[n].name) == 0) {
220 if (token[n].narg > narg) {
221 /* too few arguments */
224 return (token[n].value);
231 num_id(const char *name, const char *type)
237 val = strtoul(name, &ep, 0);
242 errx(1, "%s: illegal %s name", name, type);
248 get_int(const char *s)
254 val = strtoul(s, &ep, 0);
259 errx(1, "illegal number: %s", s);
265 duplicate_option(const char *ptr)
267 fprintf(stderr, "Syntax error: "
268 "Duplicate option: '%s'\n", ptr);
276 "usbconfig - configure the USB subsystem" "\n"
277 "usage: usbconfig -u <busnum> -a <devaddr> -i <ifaceindex> [cmds...]" "\n"
278 "usage: usbconfig -d [ugen]<busnum>.<devaddr> -i <ifaceindex> [cmds...]" "\n"
280 " set_config <cfg_index>" "\n"
281 " set_alt <alt_index>" "\n"
282 " set_template <template>" "\n"
284 " add_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
285 " remove_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
286 " add_quirk <quirk>" "\n"
287 " remove_quirk <quirk>" "\n"
288 " detach_kernel_driver" "\n"
289 " dump_quirk_names" "\n"
290 " dump_device_quirks" "\n"
291 " dump_all_desc" "\n"
292 " dump_device_desc" "\n"
293 " dump_curr_config_desc" "\n"
294 " dump_all_config_desc" "\n"
295 " dump_string <index>" "\n"
305 " do_request <bmReqTyp> <bReq> <wVal> <wIdx> <wLen> <data...>" "\n"
311 reset_options(struct options *opt)
315 memset(opt, 0, sizeof(*opt));
320 flush_command(struct libusb20_backend *pbe, struct options *opt)
322 struct libusb20_device *pdev = NULL;
323 uint32_t matches = 0;
326 /* check for invalid option combinations */
327 if ((opt->got_suspend +
330 opt->got_set_config +
332 opt->got_power_save +
334 opt->got_power_off) > 1) {
335 err(1, "can only specify one of 'set_config', "
336 "'set_alt', 'reset', 'suspend', 'resume', "
337 "'power_save', 'power_on' and 'power_off' "
338 "at the same time!");
340 if (opt->got_dump_quirk_names) {
342 dump_be_quirk_names(pbe);
344 if (opt->got_dump_device_quirks) {
346 dump_be_dev_quirks(pbe);
348 if (opt->got_remove_device_quirk) {
350 be_dev_remove_quirk(pbe,
351 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
353 if (opt->got_add_device_quirk) {
355 be_dev_add_quirk(pbe,
356 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
358 if (opt->got_set_template) {
360 if (libusb20_be_set_template(pbe, opt->template)) {
361 fprintf(stderr, "Setting USB template %u failed, "
362 "continuing.\n", opt->template);
365 if (opt->got_get_template) {
367 if (libusb20_be_get_template(pbe, &opt->template))
368 printf("USB template: <unknown>\n");
370 printf("USB template: %u\n", opt->template);
372 if (opt->got_any == 0) {
374 * do not scan through all the devices if there are no valid
379 while ((pdev = libusb20_be_device_foreach(pbe, pdev))) {
382 (libusb20_dev_get_bus_number(pdev) != opt->bus)) {
386 (libusb20_dev_get_address(pdev) != opt->addr)) {
391 if (opt->got_remove_quirk) {
392 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
394 ddesc = libusb20_dev_get_device_desc(pdev);
396 be_dev_remove_quirk(pbe,
397 ddesc->idVendor, ddesc->idProduct,
398 ddesc->bcdDevice, ddesc->bcdDevice,
402 if (opt->got_add_quirk) {
403 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
405 ddesc = libusb20_dev_get_device_desc(pdev);
407 be_dev_add_quirk(pbe,
408 ddesc->idVendor, ddesc->idProduct,
409 ddesc->bcdDevice, ddesc->bcdDevice,
413 if (libusb20_dev_open(pdev, 0)) {
414 err(1, "could not open device");
416 if (opt->got_dump_string) {
417 dump_string_by_index(pdev, opt->string_index);
419 if (opt->got_do_request) {
423 if (libusb20_dev_request_sync(pdev, &opt->setup,
424 opt->buffer, &actlen, 5000 /* 5 seconds */ , 0)) {
425 printf("REQUEST = <ERROR>\n");
426 } else if (!(opt->setup.bmRequestType &
427 LIBUSB20_ENDPOINT_IN)) {
428 printf("REQUEST = <OK>\n");
431 printf("REQUEST = <");
432 for (t = 0; t != actlen; t++) {
434 ((uint8_t *)opt->buffer)[t],
435 (t == (actlen - 1)) ? "" : " ");
438 for (t = 0; t != actlen; t++) {
441 c = ((uint8_t *)opt->buffer)[t];
443 (c != '>') && isprint(c)) {
450 if (opt->got_set_config) {
451 if (libusb20_dev_set_config_index(pdev,
452 opt->config_index)) {
453 err(1, "could not set config index");
456 if (opt->got_set_alt) {
457 if (libusb20_dev_set_alt_index(pdev, opt->iface,
459 err(1, "could not set alternate setting");
462 if (opt->got_reset) {
463 if (libusb20_dev_reset(pdev)) {
464 err(1, "could not reset device");
467 if (opt->got_suspend) {
468 if (libusb20_dev_set_power_mode(pdev,
469 LIBUSB20_POWER_SUSPEND)) {
470 err(1, "could not set suspend");
473 if (opt->got_resume) {
474 if (libusb20_dev_set_power_mode(pdev,
475 LIBUSB20_POWER_RESUME)) {
476 err(1, "could not set resume");
479 if (opt->got_power_off) {
480 if (libusb20_dev_set_power_mode(pdev,
481 LIBUSB20_POWER_OFF)) {
482 err(1, "could not set power OFF");
485 if (opt->got_power_save) {
486 if (libusb20_dev_set_power_mode(pdev,
487 LIBUSB20_POWER_SAVE)) {
488 err(1, "could not set power SAVE");
491 if (opt->got_power_on) {
492 if (libusb20_dev_set_power_mode(pdev,
493 LIBUSB20_POWER_ON)) {
494 err(1, "could not set power ON");
497 if (opt->got_detach_kernel_driver) {
498 if (libusb20_dev_detach_kernel_driver(pdev, opt->iface)) {
499 err(1, "could not detach kernel driver");
503 (opt->got_dump_all_desc ||
504 opt->got_dump_device_desc ||
505 opt->got_dump_curr_config ||
506 opt->got_dump_all_config ||
509 if (opt->got_list || dump_any) {
510 dump_device_info(pdev,
511 opt->got_show_iface_driver);
513 if (opt->got_dump_device_desc) {
515 dump_device_desc(pdev);
517 if (opt->got_dump_all_config) {
519 dump_config(pdev, 1);
520 } else if (opt->got_dump_curr_config) {
522 dump_config(pdev, 0);
523 } else if (opt->got_dump_all_desc) {
525 dump_device_desc(pdev);
526 dump_config(pdev, 1);
531 if (libusb20_dev_close(pdev)) {
532 err(1, "could not close device");
537 printf("No device match or lack of permissions.\n");
546 main(int argc, char **argv)
548 struct libusb20_backend *pbe;
549 struct options *opt = &options;
559 pbe = libusb20_be_alloc_default();
561 err(1, "could not access USB backend\n");
563 for (n = 1; n != argc; n++) {
565 /* get number of additional options */
569 switch (get_token(argv[n], t)) {
571 if (opt->got_add_quirk) {
572 flush_command(pbe, opt);
574 opt->quirkname = argv[n + 1];
577 opt->got_add_quirk = 1;
582 if (opt->got_remove_quirk) {
583 flush_command(pbe, opt);
585 opt->quirkname = argv[n + 1];
588 opt->got_remove_quirk = 1;
592 case T_ADD_DEVICE_QUIRK:
593 if (opt->got_add_device_quirk) {
594 flush_command(pbe, opt);
596 opt->vid = num_id(argv[n + 1], "Vendor ID");
597 opt->pid = num_id(argv[n + 2], "Product ID");
598 opt->lo_rev = num_id(argv[n + 3], "Low Revision");
599 opt->hi_rev = num_id(argv[n + 4], "High Revision");
600 opt->quirkname = argv[n + 5];
603 opt->got_add_device_quirk = 1;
607 case T_REMOVE_DEVICE_QUIRK:
608 if (opt->got_remove_device_quirk) {
609 flush_command(pbe, opt);
611 opt->vid = num_id(argv[n + 1], "Vendor ID");
612 opt->pid = num_id(argv[n + 2], "Product ID");
613 opt->lo_rev = num_id(argv[n + 3], "Low Revision");
614 opt->hi_rev = num_id(argv[n + 4], "High Revision");
615 opt->quirkname = argv[n + 5];
617 opt->got_remove_device_quirk = 1;
621 case T_DETACH_KERNEL_DRIVER:
622 if (opt->got_detach_kernel_driver)
623 duplicate_option(argv[n]);
624 opt->got_detach_kernel_driver = 1;
628 case T_DUMP_QUIRK_NAMES:
629 if (opt->got_dump_quirk_names)
630 duplicate_option(argv[n]);
631 opt->got_dump_quirk_names = 1;
635 case T_DUMP_DEVICE_QUIRKS:
636 if (opt->got_dump_device_quirks)
637 duplicate_option(argv[n]);
638 opt->got_dump_device_quirks = 1;
642 case T_SHOW_IFACE_DRIVER:
643 opt->got_show_iface_driver = 1;
648 /* allow multiple commands on the same line */
649 flush_command(pbe, opt);
653 if ((ptr[0] == 'u') &&
659 if ((sscanf(ptr, "%d.%d",
660 &unit, &addr) != 2) ||
661 (unit < 0) || (unit > 65535) ||
662 (addr < 0) || (addr > 65535)) {
664 "parse '%s'", argv[n + 1]);
675 /* allow multiple commands on the same line */
676 flush_command(pbe, opt);
678 opt->bus = num_id(argv[n + 1], "busnum");
683 opt->addr = num_id(argv[n + 1], "addr");
688 opt->iface = num_id(argv[n + 1], "iface");
693 if (opt->got_set_config)
694 duplicate_option(argv[n]);
695 opt->config_index = num_id(argv[n + 1], "cfg_index");
696 opt->got_set_config = 1;
701 if (opt->got_set_alt)
702 duplicate_option(argv[n]);
703 opt->alt_index = num_id(argv[n + 1], "cfg_index");
704 opt->got_set_alt = 1;
709 if (opt->got_set_template)
710 duplicate_option(argv[n]);
711 opt->template = get_int(argv[n + 1]);
712 opt->got_set_template = 1;
717 if (opt->got_get_template)
718 duplicate_option(argv[n]);
719 opt->got_get_template = 1;
722 case T_DUMP_ALL_DESC:
723 if (opt->got_dump_all_desc)
724 duplicate_option(argv[n]);
725 opt->got_dump_all_desc = 1;
728 case T_DUMP_DEVICE_DESC:
729 if (opt->got_dump_device_desc)
730 duplicate_option(argv[n]);
731 opt->got_dump_device_desc = 1;
734 case T_DUMP_CURR_CONFIG_DESC:
735 if (opt->got_dump_curr_config)
736 duplicate_option(argv[n]);
737 opt->got_dump_curr_config = 1;
740 case T_DUMP_ALL_CONFIG_DESC:
741 if (opt->got_dump_all_config)
742 duplicate_option(argv[n]);
743 opt->got_dump_all_config = 1;
747 if (opt->got_dump_info)
748 duplicate_option(argv[n]);
749 opt->got_dump_info = 1;
753 if (opt->got_dump_string)
754 duplicate_option(argv[n]);
755 opt->string_index = num_id(argv[n + 1], "str_index");
756 opt->got_dump_string = 1;
761 if (opt->got_suspend)
762 duplicate_option(argv[n]);
763 opt->got_suspend = 1;
768 duplicate_option(argv[n]);
773 if (opt->got_power_off)
774 duplicate_option(argv[n]);
775 opt->got_power_off = 1;
779 if (opt->got_power_save)
780 duplicate_option(argv[n]);
781 opt->got_power_save = 1;
785 if (opt->got_power_on)
786 duplicate_option(argv[n]);
787 opt->got_power_on = 1;
792 duplicate_option(argv[n]);
798 duplicate_option(argv[n]);
803 if (opt->got_do_request)
804 duplicate_option(argv[n]);
805 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &opt->setup);
806 opt->setup.bmRequestType = num_id(argv[n + 1], "bmReqTyp");
807 opt->setup.bRequest = num_id(argv[n + 2], "bReq");
808 opt->setup.wValue = num_id(argv[n + 3], "wVal");
809 opt->setup.wIndex = num_id(argv[n + 4], "wIndex");
810 opt->setup.wLength = num_id(argv[n + 5], "wLen");
811 if (opt->setup.wLength != 0) {
812 opt->buffer = malloc(opt->setup.wLength);
819 if (!(opt->setup.bmRequestType &
820 LIBUSB20_ENDPOINT_IN)) {
823 if (t < opt->setup.wLength) {
824 err(1, "request data missing");
826 t = opt->setup.wLength;
828 ((uint8_t *)opt->buffer)[t] =
829 num_id(argv[n + t + 1], "req_data");
831 n += opt->setup.wLength;
833 opt->got_do_request = 1;
840 if ((ptr[0] == 'u') &&
846 if ((sscanf(ptr, "%d.%d",
847 &unit, &addr) != 2) ||
848 (unit < 0) || (unit > 65535) ||
849 (addr < 0) || (addr > 65535)) {
865 /* flush out last command */
866 flush_command(pbe, opt);
868 /* list all the devices */
871 flush_command(pbe, opt);
874 libusb20_be_free(pbe);