1 /* $FreeBSD: src/usr.sbin/usbconfig/usbconfig.c,v 1.11 2010/10/24 13:48:11 hselasky Exp $ */
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
37 #include <libusb20_desc.h>
43 const char *quirkname;
50 struct LIBUSB20_CONTROL_SETUP_DECODED setup;
56 uint16_t lo_rev; /* inclusive */
57 uint16_t hi_rev; /* inclusive */
65 uint8_t got_set_config:1;
66 uint8_t got_set_alt:1;
67 uint8_t got_set_template:1;
68 uint8_t got_get_template:1;
69 uint8_t got_suspend:1;
72 uint8_t got_power_off:1;
73 uint8_t got_power_save:1;
74 uint8_t got_power_on:1;
75 uint8_t got_dump_device_quirks:1;
76 uint8_t got_dump_quirk_names:1;
77 uint8_t got_dump_device_desc:1;
78 uint8_t got_dump_curr_config:1;
79 uint8_t got_dump_all_config:1;
80 uint8_t got_dump_info:1;
81 uint8_t got_show_iface_driver:1;
82 uint8_t got_remove_device_quirk:1;
83 uint8_t got_add_device_quirk:1;
84 uint8_t got_remove_quirk:1;
85 uint8_t got_add_quirk:1;
86 uint8_t got_dump_string:1;
87 uint8_t got_do_request:1;
106 T_REMOVE_DEVICE_QUIRK,
111 T_DUMP_DEVICE_QUIRKS,
113 T_DUMP_CURR_CONFIG_DESC,
114 T_DUMP_ALL_CONFIG_DESC,
127 static struct options options;
129 static const struct token token[] = {
134 {"set_config", T_SET_CONFIG, 1},
135 {"set_alt", T_SET_ALT, 1},
136 {"set_template", T_SET_TEMPLATE, 1},
137 {"get_template", T_GET_TEMPLATE, 0},
138 {"add_dev_quirk_vplh", T_ADD_DEVICE_QUIRK, 5},
139 {"remove_dev_quirk_vplh", T_REMOVE_DEVICE_QUIRK, 5},
140 {"add_quirk", T_ADD_QUIRK, 1},
141 {"remove_quirk", T_REMOVE_QUIRK, 1},
142 {"dump_quirk_names", T_DUMP_QUIRK_NAMES, 0},
143 {"dump_device_quirks", T_DUMP_DEVICE_QUIRKS, 0},
144 {"dump_device_desc", T_DUMP_DEVICE_DESC, 0},
145 {"dump_curr_config_desc", T_DUMP_CURR_CONFIG_DESC, 0},
146 {"dump_all_config_desc", T_DUMP_ALL_CONFIG_DESC, 0},
147 {"dump_string", T_DUMP_STRING, 1},
148 {"dump_info", T_DUMP_INFO, 0},
149 {"show_ifdrv", T_SHOW_IFACE_DRIVER, 0},
150 {"suspend", T_SUSPEND, 0},
151 {"resume", T_RESUME, 0},
152 {"power_off", T_POWER_OFF, 0},
153 {"power_save", T_POWER_SAVE, 0},
154 {"power_on", T_POWER_ON, 0},
155 {"reset", T_RESET, 0},
157 {"do_request", T_DO_REQUEST, 5},
161 be_dev_remove_quirk(struct libusb20_backend *pbe,
162 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
165 struct libusb20_quirk q;
168 memset(&q, 0, sizeof(q));
172 q.bcdDeviceLow = lorev;
173 q.bcdDeviceHigh = hirev;
174 strlcpy(q.quirkname, str, sizeof(q.quirkname));
176 error = libusb20_be_remove_dev_quirk(pbe, &q);
178 fprintf(stderr, "Removing quirk '%s' failed, continuing.\n", str);
184 be_dev_add_quirk(struct libusb20_backend *pbe,
185 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
188 struct libusb20_quirk q;
191 memset(&q, 0, sizeof(q));
195 q.bcdDeviceLow = lorev;
196 q.bcdDeviceHigh = hirev;
197 strlcpy(q.quirkname, str, sizeof(q.quirkname));
199 error = libusb20_be_add_dev_quirk(pbe, &q);
201 fprintf(stderr, "Adding quirk '%s' failed, continuing.\n", str);
207 get_token(const char *str, uint8_t narg)
211 for (n = 0; n != (sizeof(token) / sizeof(token[0])); n++) {
212 if (strcasecmp(str, token[n].name) == 0) {
213 if (token[n].narg > narg) {
214 /* too few arguments */
217 return (token[n].value);
224 num_id(const char *name, const char *type)
230 val = strtoul(name, &ep, 0);
235 errx(1, "%s: illegal %s name", name, type);
241 get_int(const char *s)
247 val = strtoul(s, &ep, 0);
252 errx(1, "illegal number: %s", s);
258 duplicate_option(const char *ptr)
260 fprintf(stderr, "Syntax error: "
261 "Duplicate option: '%s'\n", ptr);
269 "usbconfig - configure the USB subsystem" "\n"
270 "usage: usbconfig -u <busnum> -a <devaddr> -i <ifaceindex> [cmds...]" "\n"
271 "usage: usbconfig -d [ugen]<busnum>.<devaddr> -i <ifaceindex> [cmds...]" "\n"
273 " set_config <cfg_index>" "\n"
274 " set_alt <alt_index>" "\n"
275 " set_template <template>" "\n"
277 " add_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
278 " remove_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
279 " add_quirk <quirk>" "\n"
280 " remove_quirk <quirk>" "\n"
281 " dump_quirk_names" "\n"
282 " dump_device_quirks" "\n"
283 " dump_device_desc" "\n"
284 " dump_curr_config_desc" "\n"
285 " dump_all_config_desc" "\n"
286 " dump_string <index>" "\n"
296 " do_request <bmReqTyp> <bReq> <wVal> <wIdx> <wLen> <data...>" "\n"
302 reset_options(struct options *opt)
306 memset(opt, 0, sizeof(*opt));
311 flush_command(struct libusb20_backend *pbe, struct options *opt)
313 struct libusb20_device *pdev = NULL;
314 uint32_t matches = 0;
317 /* check for invalid option combinations */
318 if ((opt->got_suspend +
321 opt->got_set_config +
323 opt->got_power_save +
325 opt->got_power_off) > 1) {
326 err(1, "can only specify one of 'set_config', "
327 "'set_alt', 'reset', 'suspend', 'resume', "
328 "'power_save', 'power_on' and 'power_off' "
329 "at the same time!");
331 if (opt->got_dump_quirk_names) {
333 dump_be_quirk_names(pbe);
335 if (opt->got_dump_device_quirks) {
337 dump_be_dev_quirks(pbe);
339 if (opt->got_remove_device_quirk) {
341 be_dev_remove_quirk(pbe,
342 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
344 if (opt->got_add_device_quirk) {
346 be_dev_add_quirk(pbe,
347 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
349 if (opt->got_set_template) {
351 if (libusb20_be_set_template(pbe, opt->template)) {
352 fprintf(stderr, "Setting USB template %u failed, "
353 "continuing.\n", opt->template);
356 if (opt->got_get_template) {
358 if (libusb20_be_get_template(pbe, &opt->template))
359 printf("USB template: <unknown>\n");
361 printf("USB template: %u\n", opt->template);
363 if (opt->got_any == 0) {
365 * do not scan through all the devices if there are no valid
370 while ((pdev = libusb20_be_device_foreach(pbe, pdev))) {
373 (libusb20_dev_get_bus_number(pdev) != opt->bus)) {
377 (libusb20_dev_get_address(pdev) != opt->addr)) {
382 if (opt->got_remove_quirk) {
383 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
385 ddesc = libusb20_dev_get_device_desc(pdev);
387 be_dev_remove_quirk(pbe,
388 ddesc->idVendor, ddesc->idProduct,
389 ddesc->bcdDevice, ddesc->bcdDevice,
393 if (opt->got_add_quirk) {
394 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
396 ddesc = libusb20_dev_get_device_desc(pdev);
398 be_dev_add_quirk(pbe,
399 ddesc->idVendor, ddesc->idProduct,
400 ddesc->bcdDevice, ddesc->bcdDevice,
404 if (libusb20_dev_open(pdev, 0)) {
405 err(1, "could not open device");
407 if (opt->got_dump_string) {
408 dump_string_by_index(pdev, opt->string_index);
410 if (opt->got_do_request) {
414 if (libusb20_dev_request_sync(pdev, &opt->setup,
415 opt->buffer, &actlen, 5000 /* 5 seconds */ , 0)) {
416 printf("REQUEST = <ERROR>\n");
417 } else if (!(opt->setup.bmRequestType &
418 LIBUSB20_ENDPOINT_IN)) {
419 printf("REQUEST = <OK>\n");
422 printf("REQUEST = <");
423 for (t = 0; t != actlen; t++) {
425 ((uint8_t *)opt->buffer)[t],
426 (t == (actlen - 1)) ? "" : " ");
429 for (t = 0; t != actlen; t++) {
432 c = ((uint8_t *)opt->buffer)[t];
434 (c != '>') && isprint(c)) {
441 if (opt->got_set_config) {
442 if (libusb20_dev_set_config_index(pdev,
443 opt->config_index)) {
444 err(1, "could not set config index");
447 if (opt->got_set_alt) {
448 if (libusb20_dev_set_alt_index(pdev, opt->iface,
450 err(1, "could not set alternate setting");
453 if (opt->got_reset) {
454 if (libusb20_dev_reset(pdev)) {
455 err(1, "could not reset device");
458 if (opt->got_suspend) {
459 if (libusb20_dev_set_power_mode(pdev,
460 LIBUSB20_POWER_SUSPEND)) {
461 err(1, "could not set suspend");
464 if (opt->got_resume) {
465 if (libusb20_dev_set_power_mode(pdev,
466 LIBUSB20_POWER_RESUME)) {
467 err(1, "could not set resume");
470 if (opt->got_power_off) {
471 if (libusb20_dev_set_power_mode(pdev,
472 LIBUSB20_POWER_OFF)) {
473 err(1, "could not set power OFF");
476 if (opt->got_power_save) {
477 if (libusb20_dev_set_power_mode(pdev,
478 LIBUSB20_POWER_SAVE)) {
479 err(1, "could not set power SAVE");
482 if (opt->got_power_on) {
483 if (libusb20_dev_set_power_mode(pdev,
484 LIBUSB20_POWER_ON)) {
485 err(1, "could not set power ON");
489 (opt->got_dump_device_desc ||
490 opt->got_dump_curr_config ||
491 opt->got_dump_all_config ||
494 if (opt->got_list || dump_any) {
495 dump_device_info(pdev,
496 opt->got_show_iface_driver);
498 if (opt->got_dump_device_desc) {
500 dump_device_desc(pdev);
502 if (opt->got_dump_all_config) {
504 dump_config(pdev, 1);
505 } else if (opt->got_dump_curr_config) {
507 dump_config(pdev, 0);
512 if (libusb20_dev_close(pdev)) {
513 err(1, "could not close device");
518 printf("No device match or lack of permissions.\n");
527 main(int argc, char **argv)
529 struct libusb20_backend *pbe;
530 struct options *opt = &options;
540 pbe = libusb20_be_alloc_default();
542 err(1, "could not access USB backend\n");
544 for (n = 1; n != argc; n++) {
546 /* get number of additional options */
550 switch (get_token(argv[n], t)) {
552 if (opt->got_add_quirk) {
553 flush_command(pbe, opt);
555 opt->quirkname = argv[n + 1];
558 opt->got_add_quirk = 1;
563 if (opt->got_remove_quirk) {
564 flush_command(pbe, opt);
566 opt->quirkname = argv[n + 1];
569 opt->got_remove_quirk = 1;
573 case T_ADD_DEVICE_QUIRK:
574 if (opt->got_add_device_quirk) {
575 flush_command(pbe, opt);
577 opt->vid = num_id(argv[n + 1], "Vendor ID");
578 opt->pid = num_id(argv[n + 2], "Product ID");
579 opt->lo_rev = num_id(argv[n + 3], "Low Revision");
580 opt->hi_rev = num_id(argv[n + 4], "High Revision");
581 opt->quirkname = argv[n + 5];
584 opt->got_add_device_quirk = 1;
588 case T_REMOVE_DEVICE_QUIRK:
589 if (opt->got_remove_device_quirk) {
590 flush_command(pbe, opt);
592 opt->vid = num_id(argv[n + 1], "Vendor ID");
593 opt->pid = num_id(argv[n + 2], "Product ID");
594 opt->lo_rev = num_id(argv[n + 3], "Low Revision");
595 opt->hi_rev = num_id(argv[n + 4], "High Revision");
596 opt->quirkname = argv[n + 5];
598 opt->got_remove_device_quirk = 1;
602 case T_DUMP_QUIRK_NAMES:
603 if (opt->got_dump_quirk_names)
604 duplicate_option(argv[n]);
605 opt->got_dump_quirk_names = 1;
609 case T_DUMP_DEVICE_QUIRKS:
610 if (opt->got_dump_device_quirks)
611 duplicate_option(argv[n]);
612 opt->got_dump_device_quirks = 1;
616 case T_SHOW_IFACE_DRIVER:
617 opt->got_show_iface_driver = 1;
622 /* allow multiple commands on the same line */
623 flush_command(pbe, opt);
627 if ((ptr[0] == 'u') &&
633 if ((sscanf(ptr, "%d.%d",
634 &unit, &addr) != 2) ||
635 (unit < 0) || (unit > 65535) ||
636 (addr < 0) || (addr > 65535)) {
638 "parse '%s'", argv[n + 1]);
649 /* allow multiple commands on the same line */
650 flush_command(pbe, opt);
652 opt->bus = num_id(argv[n + 1], "busnum");
657 opt->addr = num_id(argv[n + 1], "addr");
662 opt->iface = num_id(argv[n + 1], "iface");
667 if (opt->got_set_config)
668 duplicate_option(argv[n]);
669 opt->config_index = num_id(argv[n + 1], "cfg_index");
670 opt->got_set_config = 1;
675 if (opt->got_set_alt)
676 duplicate_option(argv[n]);
677 opt->alt_index = num_id(argv[n + 1], "cfg_index");
678 opt->got_set_alt = 1;
683 if (opt->got_set_template)
684 duplicate_option(argv[n]);
685 opt->template = get_int(argv[n + 1]);
686 opt->got_set_template = 1;
691 if (opt->got_get_template)
692 duplicate_option(argv[n]);
693 opt->got_get_template = 1;
696 case T_DUMP_DEVICE_DESC:
697 if (opt->got_dump_device_desc)
698 duplicate_option(argv[n]);
699 opt->got_dump_device_desc = 1;
702 case T_DUMP_CURR_CONFIG_DESC:
703 if (opt->got_dump_curr_config)
704 duplicate_option(argv[n]);
705 opt->got_dump_curr_config = 1;
708 case T_DUMP_ALL_CONFIG_DESC:
709 if (opt->got_dump_all_config)
710 duplicate_option(argv[n]);
711 opt->got_dump_all_config = 1;
715 if (opt->got_dump_info)
716 duplicate_option(argv[n]);
717 opt->got_dump_info = 1;
721 if (opt->got_dump_string)
722 duplicate_option(argv[n]);
723 opt->string_index = num_id(argv[n + 1], "str_index");
724 opt->got_dump_string = 1;
729 if (opt->got_suspend)
730 duplicate_option(argv[n]);
731 opt->got_suspend = 1;
736 duplicate_option(argv[n]);
741 if (opt->got_power_off)
742 duplicate_option(argv[n]);
743 opt->got_power_off = 1;
747 if (opt->got_power_save)
748 duplicate_option(argv[n]);
749 opt->got_power_save = 1;
753 if (opt->got_power_on)
754 duplicate_option(argv[n]);
755 opt->got_power_on = 1;
760 duplicate_option(argv[n]);
766 duplicate_option(argv[n]);
771 if (opt->got_do_request)
772 duplicate_option(argv[n]);
773 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &opt->setup);
774 opt->setup.bmRequestType = num_id(argv[n + 1], "bmReqTyp");
775 opt->setup.bRequest = num_id(argv[n + 2], "bReq");
776 opt->setup.wValue = num_id(argv[n + 3], "wVal");
777 opt->setup.wIndex = num_id(argv[n + 4], "wIndex");
778 opt->setup.wLength = num_id(argv[n + 5], "wLen");
779 if (opt->setup.wLength != 0) {
780 opt->buffer = malloc(opt->setup.wLength);
787 if (!(opt->setup.bmRequestType &
788 LIBUSB20_ENDPOINT_IN)) {
791 if (t < opt->setup.wLength) {
792 err(1, "request data missing");
794 t = opt->setup.wLength;
796 ((uint8_t *)opt->buffer)[t] =
797 num_id(argv[n + t + 1], "req_data");
799 n += opt->setup.wLength;
801 opt->got_do_request = 1;
810 /* flush out last command */
811 flush_command(pbe, opt);
813 /* list all the devices */
816 flush_command(pbe, opt);
819 libusb20_be_free(pbe);