3 * Copyright (c) 2008 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;
49 struct LIBUSB20_CONTROL_SETUP_DECODED setup;
55 uint16_t lo_rev; /* inclusive */
56 uint16_t hi_rev; /* inclusive */
64 uint8_t got_set_config:1;
65 uint8_t got_set_alt:1;
66 uint8_t got_set_owner:1;
67 uint8_t got_set_perm:1;
68 uint8_t got_suspend:1;
71 uint8_t got_power_off:1;
72 uint8_t got_power_save:1;
73 uint8_t got_power_on:1;
74 uint8_t got_dump_device_quirks:1;
75 uint8_t got_dump_quirk_names:1;
76 uint8_t got_dump_device_desc:1;
77 uint8_t got_dump_curr_config:1;
78 uint8_t got_dump_all_config:1;
79 uint8_t got_dump_info:1;
80 uint8_t got_dump_access: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_dump_string:1;
85 uint8_t got_do_request:1;
103 T_REMOVE_DEVICE_QUIRK,
106 T_DUMP_DEVICE_QUIRKS,
108 T_DUMP_CURR_CONFIG_DESC,
109 T_DUMP_ALL_CONFIG_DESC,
123 static struct options options;
125 static const struct token token[] = {
129 {"set_config", T_SET_CONFIG, 1},
130 {"set_alt", T_SET_ALT, 1},
131 {"set_owner", T_SET_OWNER, 1},
132 {"set_perm", T_SET_PERM, 1},
133 {"add_dev_quirk_vplh", T_ADD_DEVICE_QUIRK, 5},
134 {"remove_dev_quirk_vplh", T_REMOVE_DEVICE_QUIRK, 5},
135 {"dump_quirk_names", T_DUMP_QUIRK_NAMES, 0},
136 {"dump_device_quirks", T_DUMP_DEVICE_QUIRKS, 0},
137 {"dump_device_desc", T_DUMP_DEVICE_DESC, 0},
138 {"dump_curr_config_desc", T_DUMP_CURR_CONFIG_DESC, 0},
139 {"dump_all_config_desc", T_DUMP_ALL_CONFIG_DESC, 0},
140 {"dump_string", T_DUMP_STRING, 1},
141 {"dump_access", T_DUMP_ACCESS, 0},
142 {"dump_info", T_DUMP_INFO, 0},
143 {"show_ifdrv", T_SHOW_IFACE_DRIVER, 0},
144 {"suspend", T_SUSPEND, 0},
145 {"resume", T_RESUME, 0},
146 {"power_off", T_POWER_OFF, 0},
147 {"power_save", T_POWER_SAVE, 0},
148 {"power_on", T_POWER_ON, 0},
149 {"reset", T_RESET, 0},
151 {"do_request", T_DO_REQUEST, 5},
155 be_dev_remove_quirk(struct libusb20_backend *pbe,
156 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
159 struct libusb20_quirk q;
162 memset(&q, 0, sizeof(q));
166 q.bcdDeviceLow = lorev;
167 q.bcdDeviceHigh = hirev;
168 strlcpy(q.quirkname, str, sizeof(q.quirkname));
170 error = libusb20_be_remove_dev_quirk(pbe, &q);
172 printf("Removing quirk '%s' failed, continuing.\n", str);
178 be_dev_add_quirk(struct libusb20_backend *pbe,
179 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
182 struct libusb20_quirk q;
185 memset(&q, 0, sizeof(q));
189 q.bcdDeviceLow = lorev;
190 q.bcdDeviceHigh = hirev;
191 strlcpy(q.quirkname, str, sizeof(q.quirkname));
193 error = libusb20_be_add_dev_quirk(pbe, &q);
195 printf("Adding quirk '%s' failed, continuing.\n", str);
201 get_token(const char *str, uint8_t narg)
205 for (n = 0; n != (sizeof(token) / sizeof(token[0])); n++) {
206 if (strcasecmp(str, token[n].name) == 0) {
207 if (token[n].narg > narg) {
208 /* too few arguments */
211 return (token[n].value);
218 num_id(const char *name, const char *type)
224 val = strtoul(name, &ep, 0);
229 errx(1, "%s: illegal %s name", name, type);
243 return ((gr = getgrnam(s)) ? gr->gr_gid : num_id(s, "group"));
255 return ((pw = getpwnam(s)) ? pw->pw_uid : num_id(s, "user"));
259 a_mode(const char *s)
265 val = strtoul(s, &ep, 8);
270 errx(1, "illegal permissions: %s", s);
279 "usbconfig - configure the USB subsystem" "\n"
280 "usage: usbconfig -u <busnum> -a <devaddr> -i <ifaceindex> [cmds...]" "\n"
282 " set_config <cfg_index>" "\n"
283 " set_alt <alt_index>" "\n"
284 " set_owner <user:group>" "\n"
285 " set_perm <mode>" "\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 " dump_quirk_names" "\n"
289 " dump_device_quirks" "\n"
290 " dump_device_desc" "\n"
291 " dump_curr_config_desc" "\n"
292 " dump_all_config_desc" "\n"
293 " dump_string <index>" "\n"
304 " do_request <bmReqTyp> <bReq> <wVal> <wIdx> <wLen> <data...>" "\n"
310 reset_options(struct options *opt)
314 memset(opt, 0, sizeof(*opt));
319 flush_command(struct libusb20_backend *pbe, struct options *opt)
321 struct libusb20_device *pdev = NULL;
322 uint32_t matches = 0;
325 /* check for invalid option combinations */
326 if ((opt->got_suspend +
329 opt->got_set_config +
331 opt->got_power_save +
333 opt->got_power_off) > 1) {
334 err(1, "can only specify one of 'set_config', "
335 "'set_alt', 'reset', 'suspend', 'resume', "
336 "'power_save', 'power_on' and 'power_off' "
337 "at the same time!");
339 if (opt->got_dump_access) {
343 if (opt->got_dump_quirk_names) {
345 dump_be_quirk_names(pbe);
347 if (opt->got_dump_device_quirks) {
349 dump_be_dev_quirks(pbe);
351 if (opt->got_remove_device_quirk) {
353 be_dev_remove_quirk(pbe,
354 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
356 if (opt->got_add_device_quirk) {
358 be_dev_add_quirk(pbe,
359 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
361 if (opt->got_any == 0) {
363 * do not scan through all the devices if there are no valid
368 while ((pdev = libusb20_be_device_foreach(pbe, pdev))) {
371 (libusb20_dev_get_bus_number(pdev) != opt->bus)) {
375 (libusb20_dev_get_address(pdev) != opt->addr)) {
380 /* do owner and permissions first */
382 if (opt->got_bus && opt->got_addr && opt->got_iface) {
383 if (opt->got_set_owner) {
384 if (libusb20_dev_set_iface_owner(pdev,
385 opt->iface, opt->uid, opt->gid)) {
386 err(1, "setting owner and group failed\n");
389 if (opt->got_set_perm) {
390 if (libusb20_dev_set_iface_perm(pdev,
391 opt->iface, opt->mode)) {
392 err(1, "setting mode failed\n");
395 } else if (opt->got_bus && opt->got_addr) {
396 if (opt->got_set_owner) {
397 if (libusb20_dev_set_owner(pdev,
398 opt->uid, opt->gid)) {
399 err(1, "setting owner and group failed\n");
402 if (opt->got_set_perm) {
403 if (libusb20_dev_set_perm(pdev,
405 err(1, "setting mode failed\n");
408 } else if (opt->got_bus) {
409 if (opt->got_set_owner) {
410 if (libusb20_bus_set_owner(pbe, opt->bus,
411 opt->uid, opt->gid)) {
412 err(1, "setting owner and group failed\n");
415 if (opt->got_set_perm) {
416 if (libusb20_bus_set_perm(pbe, opt->bus,
418 err(1, "setting mode failed\n");
422 if (opt->got_set_owner) {
423 if (libusb20_be_set_owner(pbe,
424 opt->uid, opt->gid)) {
425 err(1, "setting owner and group failed\n");
428 if (opt->got_set_perm) {
429 if (libusb20_be_set_perm(pbe,
431 err(1, "setting mode failed\n");
436 if (libusb20_dev_open(pdev, 0)) {
437 err(1, "could not open device");
439 if (opt->got_dump_string) {
444 err(1, "out of memory");
446 if (libusb20_dev_req_string_simple_sync(pdev,
447 opt->string_index, pbuf, 256)) {
448 printf("STRING_0x%02x = <read error>\n",
451 printf("STRING_0x%02x = <%s>\n",
452 opt->string_index, pbuf);
456 if (opt->got_do_request) {
460 if (libusb20_dev_request_sync(pdev, &opt->setup,
461 opt->buffer, &actlen, 5000 /* 5 seconds */ , 0)) {
462 printf("REQUEST = <ERROR>\n");
463 } else if (!(opt->setup.bmRequestType &
464 LIBUSB20_ENDPOINT_IN)) {
465 printf("REQUEST = <OK>\n");
468 printf("REQUEST = <");
469 for (t = 0; t != actlen; t++) {
471 ((uint8_t *)opt->buffer)[t],
472 (t == (actlen - 1)) ? "" : " ");
475 for (t = 0; t != actlen; t++) {
478 c = ((uint8_t *)opt->buffer)[t];
480 (c != '>') && isprint(c)) {
487 if (opt->got_set_config) {
488 if (libusb20_dev_set_config_index(pdev,
489 opt->config_index)) {
490 err(1, "could not set config index");
493 if (opt->got_set_alt) {
494 if (libusb20_dev_set_alt_index(pdev, opt->iface,
496 err(1, "could not set alternate setting");
499 if (opt->got_reset) {
500 if (libusb20_dev_reset(pdev)) {
501 err(1, "could not reset device");
504 if (opt->got_suspend) {
505 if (libusb20_dev_set_power_mode(pdev,
506 LIBUSB20_POWER_SUSPEND)) {
507 err(1, "could not set suspend");
510 if (opt->got_resume) {
511 if (libusb20_dev_set_power_mode(pdev,
512 LIBUSB20_POWER_RESUME)) {
513 err(1, "could not set resume");
516 if (opt->got_power_off) {
517 if (libusb20_dev_set_power_mode(pdev,
518 LIBUSB20_POWER_OFF)) {
519 err(1, "could not set power OFF");
522 if (opt->got_power_save) {
523 if (libusb20_dev_set_power_mode(pdev,
524 LIBUSB20_POWER_SAVE)) {
525 err(1, "could not set power SAVE");
528 if (opt->got_power_on) {
529 if (libusb20_dev_set_power_mode(pdev,
530 LIBUSB20_POWER_ON)) {
531 err(1, "could not set power ON");
535 (opt->got_dump_device_desc ||
536 opt->got_dump_curr_config ||
537 opt->got_dump_all_config ||
538 opt->got_dump_info ||
539 opt->got_dump_access);
541 if (opt->got_list || dump_any) {
542 dump_device_info(pdev,
543 opt->got_show_iface_driver);
545 if (opt->got_dump_access) {
547 dump_device_access(pdev, opt->got_iface ?
550 if (opt->got_dump_device_desc) {
552 dump_device_desc(pdev);
554 if (opt->got_dump_all_config) {
556 dump_config(pdev, 1);
557 } else if (opt->got_dump_curr_config) {
559 dump_config(pdev, 0);
564 if (libusb20_dev_close(pdev)) {
565 err(1, "could not close device");
570 printf("No device match or lack of permissions.\n");
579 main(int argc, char **argv)
581 struct libusb20_backend *pbe;
582 struct options *opt = &options;
590 pbe = libusb20_be_alloc_default();
592 err(1, "could not access USB backend\n");
594 for (n = 1; n != argc; n++) {
596 /* get number of additional options */
600 switch (get_token(argv[n], t)) {
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_DUMP_QUIRK_NAMES:
631 opt->got_dump_quirk_names = 1;
635 case T_DUMP_DEVICE_QUIRKS:
636 opt->got_dump_device_quirks = 1;
640 case T_SHOW_IFACE_DRIVER:
641 opt->got_show_iface_driver = 1;
646 /* allow multiple commands on the same line */
647 flush_command(pbe, opt);
649 opt->bus = num_id(argv[n + 1], "busnum");
654 opt->addr = num_id(argv[n + 1], "addr");
659 opt->iface = num_id(argv[n + 1], "iface");
664 opt->config_index = num_id(argv[n + 1], "cfg_index");
665 opt->got_set_config = 1;
670 opt->alt_index = num_id(argv[n + 1], "cfg_index");
671 opt->got_set_alt = 1;
677 cp = strchr(cp, ':');
679 err(1, "missing colon in '%s'!", argv[n + 1]);
682 opt->gid = a_gid(cp);
683 opt->uid = a_uid(argv[n + 1]);
684 opt->got_set_owner = 1;
689 opt->mode = a_mode(argv[n + 1]);
690 opt->got_set_perm = 1;
694 case T_DUMP_DEVICE_DESC:
695 opt->got_dump_device_desc = 1;
698 case T_DUMP_CURR_CONFIG_DESC:
699 opt->got_dump_curr_config = 1;
702 case T_DUMP_ALL_CONFIG_DESC:
703 opt->got_dump_all_config = 1;
707 opt->got_dump_info = 1;
711 if (opt->got_dump_string) {
712 flush_command(pbe, opt);
714 opt->string_index = num_id(argv[n + 1], "str_index");
715 opt->got_dump_string = 1;
720 opt->got_dump_access = 1;
724 opt->got_suspend = 1;
732 opt->got_power_off = 1;
736 opt->got_power_save = 1;
740 opt->got_power_on = 1;
752 if (opt->got_do_request) {
753 flush_command(pbe, opt);
755 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &opt->setup);
756 opt->setup.bmRequestType = num_id(argv[n + 1], "bmReqTyp");
757 opt->setup.bRequest = num_id(argv[n + 2], "bReq");
758 opt->setup.wValue = num_id(argv[n + 3], "wVal");
759 opt->setup.wIndex = num_id(argv[n + 4], "wIndex");
760 opt->setup.wLength = num_id(argv[n + 5], "wLen");
761 if (opt->setup.wLength != 0) {
762 opt->buffer = malloc(opt->setup.wLength);
769 if (!(opt->setup.bmRequestType &
770 LIBUSB20_ENDPOINT_IN)) {
773 if (t < opt->setup.wLength) {
774 err(1, "request data missing");
776 t = opt->setup.wLength;
778 ((uint8_t *)opt->buffer)[t] =
779 num_id(argv[n + t + 1], "req_data");
781 n += opt->setup.wLength;
783 opt->got_do_request = 1;
792 /* flush out last command */
793 flush_command(pbe, opt);
795 /* list all the devices */
798 flush_command(pbe, opt);
801 libusb20_be_free(pbe);