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;
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_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,
122 static struct options options;
124 static const struct token token[] = {
128 {"set_config", T_SET_CONFIG, 1},
129 {"set_alt", T_SET_ALT, 1},
130 {"set_template", T_SET_TEMPLATE, 1},
131 {"get_template", T_GET_TEMPLATE, 0},
132 {"add_dev_quirk_vplh", T_ADD_DEVICE_QUIRK, 5},
133 {"remove_dev_quirk_vplh", T_REMOVE_DEVICE_QUIRK, 5},
134 {"dump_quirk_names", T_DUMP_QUIRK_NAMES, 0},
135 {"dump_device_quirks", T_DUMP_DEVICE_QUIRKS, 0},
136 {"dump_device_desc", T_DUMP_DEVICE_DESC, 0},
137 {"dump_curr_config_desc", T_DUMP_CURR_CONFIG_DESC, 0},
138 {"dump_all_config_desc", T_DUMP_ALL_CONFIG_DESC, 0},
139 {"dump_string", T_DUMP_STRING, 1},
140 {"dump_info", T_DUMP_INFO, 0},
141 {"show_ifdrv", T_SHOW_IFACE_DRIVER, 0},
142 {"suspend", T_SUSPEND, 0},
143 {"resume", T_RESUME, 0},
144 {"power_off", T_POWER_OFF, 0},
145 {"power_save", T_POWER_SAVE, 0},
146 {"power_on", T_POWER_ON, 0},
147 {"reset", T_RESET, 0},
149 {"do_request", T_DO_REQUEST, 5},
153 be_dev_remove_quirk(struct libusb20_backend *pbe,
154 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
157 struct libusb20_quirk q;
160 memset(&q, 0, sizeof(q));
164 q.bcdDeviceLow = lorev;
165 q.bcdDeviceHigh = hirev;
166 strlcpy(q.quirkname, str, sizeof(q.quirkname));
168 error = libusb20_be_remove_dev_quirk(pbe, &q);
170 printf("Removing quirk '%s' failed, continuing.\n", str);
176 be_dev_add_quirk(struct libusb20_backend *pbe,
177 uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
180 struct libusb20_quirk q;
183 memset(&q, 0, sizeof(q));
187 q.bcdDeviceLow = lorev;
188 q.bcdDeviceHigh = hirev;
189 strlcpy(q.quirkname, str, sizeof(q.quirkname));
191 error = libusb20_be_add_dev_quirk(pbe, &q);
193 printf("Adding quirk '%s' failed, continuing.\n", str);
199 get_token(const char *str, uint8_t narg)
203 for (n = 0; n != (sizeof(token) / sizeof(token[0])); n++) {
204 if (strcasecmp(str, token[n].name) == 0) {
205 if (token[n].narg > narg) {
206 /* too few arguments */
209 return (token[n].value);
216 num_id(const char *name, const char *type)
222 val = strtoul(name, &ep, 0);
227 errx(1, "%s: illegal %s name", name, type);
233 get_int(const char *s)
239 val = strtoul(s, &ep, 0);
244 errx(1, "illegal number: %s", s);
253 "usbconfig - configure the USB subsystem" "\n"
254 "usage: usbconfig -u <busnum> -a <devaddr> -i <ifaceindex> [cmds...]" "\n"
256 " set_config <cfg_index>" "\n"
257 " set_alt <alt_index>" "\n"
258 " set_template <template>" "\n"
260 " add_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
261 " remove_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
262 " dump_quirk_names" "\n"
263 " dump_device_quirks" "\n"
264 " dump_device_desc" "\n"
265 " dump_curr_config_desc" "\n"
266 " dump_all_config_desc" "\n"
267 " dump_string <index>" "\n"
277 " do_request <bmReqTyp> <bReq> <wVal> <wIdx> <wLen> <data...>" "\n"
283 reset_options(struct options *opt)
287 memset(opt, 0, sizeof(*opt));
292 flush_command(struct libusb20_backend *pbe, struct options *opt)
294 struct libusb20_device *pdev = NULL;
295 uint32_t matches = 0;
298 /* check for invalid option combinations */
299 if ((opt->got_suspend +
302 opt->got_set_config +
304 opt->got_power_save +
306 opt->got_power_off) > 1) {
307 err(1, "can only specify one of 'set_config', "
308 "'set_alt', 'reset', 'suspend', 'resume', "
309 "'power_save', 'power_on' and 'power_off' "
310 "at the same time!");
312 if (opt->got_dump_quirk_names) {
314 dump_be_quirk_names(pbe);
316 if (opt->got_dump_device_quirks) {
318 dump_be_dev_quirks(pbe);
320 if (opt->got_remove_device_quirk) {
322 be_dev_remove_quirk(pbe,
323 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
325 if (opt->got_add_device_quirk) {
327 be_dev_add_quirk(pbe,
328 opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
330 if (opt->got_set_template) {
332 if (libusb20_be_set_template(pbe, opt->template)) {
333 printf("Setting USB template %u failed, "
334 "continuing.\n", opt->template);
337 if (opt->got_get_template) {
339 if (libusb20_be_get_template(pbe, &opt->template))
340 printf("USB template: <unknown>\n");
342 printf("USB template: %u\n", opt->template);
344 if (opt->got_any == 0) {
346 * do not scan through all the devices if there are no valid
351 while ((pdev = libusb20_be_device_foreach(pbe, pdev))) {
354 (libusb20_dev_get_bus_number(pdev) != opt->bus)) {
358 (libusb20_dev_get_address(pdev) != opt->addr)) {
363 if (libusb20_dev_open(pdev, 0)) {
364 err(1, "could not open device");
366 if (opt->got_dump_string) {
371 err(1, "out of memory");
373 if (libusb20_dev_req_string_simple_sync(pdev,
374 opt->string_index, pbuf, 256)) {
375 printf("STRING_0x%02x = <read error>\n",
378 printf("STRING_0x%02x = <%s>\n",
379 opt->string_index, pbuf);
383 if (opt->got_do_request) {
387 if (libusb20_dev_request_sync(pdev, &opt->setup,
388 opt->buffer, &actlen, 5000 /* 5 seconds */ , 0)) {
389 printf("REQUEST = <ERROR>\n");
390 } else if (!(opt->setup.bmRequestType &
391 LIBUSB20_ENDPOINT_IN)) {
392 printf("REQUEST = <OK>\n");
395 printf("REQUEST = <");
396 for (t = 0; t != actlen; t++) {
398 ((uint8_t *)opt->buffer)[t],
399 (t == (actlen - 1)) ? "" : " ");
402 for (t = 0; t != actlen; t++) {
405 c = ((uint8_t *)opt->buffer)[t];
407 (c != '>') && isprint(c)) {
414 if (opt->got_set_config) {
415 if (libusb20_dev_set_config_index(pdev,
416 opt->config_index)) {
417 err(1, "could not set config index");
420 if (opt->got_set_alt) {
421 if (libusb20_dev_set_alt_index(pdev, opt->iface,
423 err(1, "could not set alternate setting");
426 if (opt->got_reset) {
427 if (libusb20_dev_reset(pdev)) {
428 err(1, "could not reset device");
431 if (opt->got_suspend) {
432 if (libusb20_dev_set_power_mode(pdev,
433 LIBUSB20_POWER_SUSPEND)) {
434 err(1, "could not set suspend");
437 if (opt->got_resume) {
438 if (libusb20_dev_set_power_mode(pdev,
439 LIBUSB20_POWER_RESUME)) {
440 err(1, "could not set resume");
443 if (opt->got_power_off) {
444 if (libusb20_dev_set_power_mode(pdev,
445 LIBUSB20_POWER_OFF)) {
446 err(1, "could not set power OFF");
449 if (opt->got_power_save) {
450 if (libusb20_dev_set_power_mode(pdev,
451 LIBUSB20_POWER_SAVE)) {
452 err(1, "could not set power SAVE");
455 if (opt->got_power_on) {
456 if (libusb20_dev_set_power_mode(pdev,
457 LIBUSB20_POWER_ON)) {
458 err(1, "could not set power ON");
462 (opt->got_dump_device_desc ||
463 opt->got_dump_curr_config ||
464 opt->got_dump_all_config ||
467 if (opt->got_list || dump_any) {
468 dump_device_info(pdev,
469 opt->got_show_iface_driver);
471 if (opt->got_dump_device_desc) {
473 dump_device_desc(pdev);
475 if (opt->got_dump_all_config) {
477 dump_config(pdev, 1);
478 } else if (opt->got_dump_curr_config) {
480 dump_config(pdev, 0);
485 if (libusb20_dev_close(pdev)) {
486 err(1, "could not close device");
491 printf("No device match or lack of permissions.\n");
500 main(int argc, char **argv)
502 struct libusb20_backend *pbe;
503 struct options *opt = &options;
510 pbe = libusb20_be_alloc_default();
512 err(1, "could not access USB backend\n");
514 for (n = 1; n != argc; n++) {
516 /* get number of additional options */
520 switch (get_token(argv[n], t)) {
521 case T_ADD_DEVICE_QUIRK:
522 if (opt->got_add_device_quirk) {
523 flush_command(pbe, opt);
525 opt->vid = num_id(argv[n + 1], "Vendor ID");
526 opt->pid = num_id(argv[n + 2], "Product ID");
527 opt->lo_rev = num_id(argv[n + 3], "Low Revision");
528 opt->hi_rev = num_id(argv[n + 4], "High Revision");
529 opt->quirkname = argv[n + 5];
532 opt->got_add_device_quirk = 1;
536 case T_REMOVE_DEVICE_QUIRK:
537 if (opt->got_remove_device_quirk) {
538 flush_command(pbe, opt);
540 opt->vid = num_id(argv[n + 1], "Vendor ID");
541 opt->pid = num_id(argv[n + 2], "Product ID");
542 opt->lo_rev = num_id(argv[n + 3], "Low Revision");
543 opt->hi_rev = num_id(argv[n + 4], "High Revision");
544 opt->quirkname = argv[n + 5];
546 opt->got_remove_device_quirk = 1;
550 case T_DUMP_QUIRK_NAMES:
551 opt->got_dump_quirk_names = 1;
555 case T_DUMP_DEVICE_QUIRKS:
556 opt->got_dump_device_quirks = 1;
560 case T_SHOW_IFACE_DRIVER:
561 opt->got_show_iface_driver = 1;
566 /* allow multiple commands on the same line */
567 flush_command(pbe, opt);
569 opt->bus = num_id(argv[n + 1], "busnum");
574 opt->addr = num_id(argv[n + 1], "addr");
579 opt->iface = num_id(argv[n + 1], "iface");
584 opt->config_index = num_id(argv[n + 1], "cfg_index");
585 opt->got_set_config = 1;
590 opt->alt_index = num_id(argv[n + 1], "cfg_index");
591 opt->got_set_alt = 1;
596 opt->template = get_int(argv[n + 1]);
597 opt->got_set_template = 1;
602 opt->got_get_template = 1;
605 case T_DUMP_DEVICE_DESC:
606 opt->got_dump_device_desc = 1;
609 case T_DUMP_CURR_CONFIG_DESC:
610 opt->got_dump_curr_config = 1;
613 case T_DUMP_ALL_CONFIG_DESC:
614 opt->got_dump_all_config = 1;
618 opt->got_dump_info = 1;
622 if (opt->got_dump_string) {
623 flush_command(pbe, opt);
625 opt->string_index = num_id(argv[n + 1], "str_index");
626 opt->got_dump_string = 1;
631 opt->got_suspend = 1;
639 opt->got_power_off = 1;
643 opt->got_power_save = 1;
647 opt->got_power_on = 1;
659 if (opt->got_do_request) {
660 flush_command(pbe, opt);
662 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &opt->setup);
663 opt->setup.bmRequestType = num_id(argv[n + 1], "bmReqTyp");
664 opt->setup.bRequest = num_id(argv[n + 2], "bReq");
665 opt->setup.wValue = num_id(argv[n + 3], "wVal");
666 opt->setup.wIndex = num_id(argv[n + 4], "wIndex");
667 opt->setup.wLength = num_id(argv[n + 5], "wLen");
668 if (opt->setup.wLength != 0) {
669 opt->buffer = malloc(opt->setup.wLength);
676 if (!(opt->setup.bmRequestType &
677 LIBUSB20_ENDPOINT_IN)) {
680 if (t < opt->setup.wLength) {
681 err(1, "request data missing");
683 t = opt->setup.wLength;
685 ((uint8_t *)opt->buffer)[t] =
686 num_id(argv[n + t + 1], "req_data");
688 n += opt->setup.wLength;
690 opt->got_do_request = 1;
699 /* flush out last command */
700 flush_command(pbe, opt);
702 /* list all the devices */
705 flush_command(pbe, opt);
708 libusb20_be_free(pbe);