xref: /freebsd/usr.sbin/usbconfig/usbconfig.c (revision 9124ddeb4a551977cf6b2218291e7c666ce25f47)
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2008-2009 Hans Petter Selasky. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <err.h>
31 #include <string.h>
32 #include <pwd.h>
33 #include <grp.h>
34 #include <errno.h>
35 #include <ctype.h>
36 
37 #include <libusb20_desc.h>
38 #include <libusb20.h>
39 
40 #include "dump.h"
41 
42 struct options {
43 	const char *quirkname;
44 	void   *buffer;
45 	int template;
46 	gid_t	gid;
47 	uid_t	uid;
48 	mode_t	mode;
49 	uint32_t got_any;
50 	struct LIBUSB20_CONTROL_SETUP_DECODED setup;
51 	uint16_t bus;
52 	uint16_t addr;
53 	uint16_t iface;
54 	uint16_t vid;
55 	uint16_t pid;
56 	uint16_t lo_rev;		/* inclusive */
57 	uint16_t hi_rev;		/* inclusive */
58 	uint8_t	string_index;
59 	uint8_t	config_index;
60 	uint8_t	alt_index;
61 	uint8_t	got_list:1;
62 	uint8_t	got_bus:1;
63 	uint8_t	got_addr:1;
64 	uint8_t	got_iface:1;
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;
70 	uint8_t	got_resume:1;
71 	uint8_t	got_reset: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;
88 };
89 
90 struct token {
91 	const char *name;
92 	uint8_t	value;
93 	uint8_t	narg;
94 };
95 
96 enum {
97 	T_UNIT,
98 	T_ADDR,
99 	T_UGEN,
100 	T_IFACE,
101 	T_SET_CONFIG,
102 	T_SET_ALT,
103 	T_SET_TEMPLATE,
104 	T_GET_TEMPLATE,
105 	T_ADD_DEVICE_QUIRK,
106 	T_REMOVE_DEVICE_QUIRK,
107 	T_ADD_QUIRK,
108 	T_REMOVE_QUIRK,
109 	T_SHOW_IFACE_DRIVER,
110 	T_DUMP_QUIRK_NAMES,
111 	T_DUMP_DEVICE_QUIRKS,
112 	T_DUMP_DEVICE_DESC,
113 	T_DUMP_CURR_CONFIG_DESC,
114 	T_DUMP_ALL_CONFIG_DESC,
115 	T_DUMP_STRING,
116 	T_DUMP_INFO,
117 	T_SUSPEND,
118 	T_RESUME,
119 	T_POWER_OFF,
120 	T_POWER_SAVE,
121 	T_POWER_ON,
122 	T_RESET,
123 	T_LIST,
124 	T_DO_REQUEST,
125 };
126 
127 static struct options options;
128 
129 static const struct token token[] = {
130 	{"-u", T_UNIT, 1},
131 	{"-a", T_ADDR, 1},
132 	{"-d", T_UGEN, 1},
133 	{"-i", T_IFACE, 1},
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},
156 	{"list", T_LIST, 0},
157 	{"do_request", T_DO_REQUEST, 5},
158 };
159 
160 static void
161 be_dev_remove_quirk(struct libusb20_backend *pbe,
162     uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
163     const char *str)
164 {
165 	struct libusb20_quirk q;
166 	int error;
167 
168 	memset(&q, 0, sizeof(q));
169 
170 	q.vid = vid;
171 	q.pid = pid;
172 	q.bcdDeviceLow = lorev;
173 	q.bcdDeviceHigh = hirev;
174 	strlcpy(q.quirkname, str, sizeof(q.quirkname));
175 
176 	error = libusb20_be_remove_dev_quirk(pbe, &q);
177 	if (error) {
178 		fprintf(stderr, "Removing quirk '%s' failed, continuing.\n", str);
179 	}
180 	return;
181 }
182 
183 static void
184 be_dev_add_quirk(struct libusb20_backend *pbe,
185     uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
186     const char *str)
187 {
188 	struct libusb20_quirk q;
189 	int error;
190 
191 	memset(&q, 0, sizeof(q));
192 
193 	q.vid = vid;
194 	q.pid = pid;
195 	q.bcdDeviceLow = lorev;
196 	q.bcdDeviceHigh = hirev;
197 	strlcpy(q.quirkname, str, sizeof(q.quirkname));
198 
199 	error = libusb20_be_add_dev_quirk(pbe, &q);
200 	if (error) {
201 		fprintf(stderr, "Adding quirk '%s' failed, continuing.\n", str);
202 	}
203 	return;
204 }
205 
206 static uint8_t
207 get_token(const char *str, uint8_t narg)
208 {
209 	uint8_t n;
210 
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 */
215 				break;
216 			}
217 			return (token[n].value);
218 		}
219 	}
220 	return (0 - 1);
221 }
222 
223 static uid_t
224 num_id(const char *name, const char *type)
225 {
226 	uid_t val;
227 	char *ep;
228 
229 	errno = 0;
230 	val = strtoul(name, &ep, 0);
231 	if (errno) {
232 		err(1, "%s", name);
233 	}
234 	if (*ep != '\0') {
235 		errx(1, "%s: illegal %s name", name, type);
236 	}
237 	return (val);
238 }
239 
240 static int
241 get_int(const char *s)
242 {
243 	int val;
244 	char *ep;
245 
246 	errno = 0;
247 	val = strtoul(s, &ep, 0);
248 	if (errno) {
249 		err(1, "%s", s);
250 	}
251 	if (*ep != '\0') {
252 		errx(1, "illegal number: %s", s);
253 	}
254 	return val;
255 }
256 
257 static void
258 duplicate_option(const char *ptr)
259 {
260 	fprintf(stderr, "Syntax error: "
261 	    "Duplicate option: '%s'\n", ptr);
262 	exit(1);
263 }
264 
265 static void
266 usage(void)
267 {
268 	fprintf(stderr, ""
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"
272 	    "commands:" "\n"
273 	    "  set_config <cfg_index>" "\n"
274 	    "  set_alt <alt_index>" "\n"
275 	    "  set_template <template>" "\n"
276 	    "  get_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"
287 	    "  dump_info" "\n"
288 	    "  show_ifdrv" "\n"
289 	    "  suspend" "\n"
290 	    "  resume" "\n"
291 	    "  power_off" "\n"
292 	    "  power_save" "\n"
293 	    "  power_on" "\n"
294 	    "  reset" "\n"
295 	    "  list" "\n"
296 	    "  do_request <bmReqTyp> <bReq> <wVal> <wIdx> <wLen> <data...>" "\n"
297 	);
298 	exit(1);
299 }
300 
301 static void
302 reset_options(struct options *opt)
303 {
304 	if (opt->buffer)
305 		free(opt->buffer);
306 	memset(opt, 0, sizeof(*opt));
307 	return;
308 }
309 
310 static void
311 flush_command(struct libusb20_backend *pbe, struct options *opt)
312 {
313 	struct libusb20_device *pdev = NULL;
314 	uint32_t matches = 0;
315 	uint8_t dump_any;
316 
317 	/* check for invalid option combinations */
318 	if ((opt->got_suspend +
319 	    opt->got_resume +
320 	    opt->got_reset +
321 	    opt->got_set_config +
322 	    opt->got_set_alt +
323 	    opt->got_power_save +
324 	    opt->got_power_on +
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!");
330 	}
331 	if (opt->got_dump_quirk_names) {
332 		opt->got_any--;
333 		dump_be_quirk_names(pbe);
334 	}
335 	if (opt->got_dump_device_quirks) {
336 		opt->got_any--;
337 		dump_be_dev_quirks(pbe);
338 	}
339 	if (opt->got_remove_device_quirk) {
340 		opt->got_any--;
341 		be_dev_remove_quirk(pbe,
342 		    opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
343 	}
344 	if (opt->got_add_device_quirk) {
345 		opt->got_any--;
346 		be_dev_add_quirk(pbe,
347 		    opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
348 	}
349 	if (opt->got_set_template) {
350 		opt->got_any--;
351 		if (libusb20_be_set_template(pbe, opt->template)) {
352 			fprintf(stderr, "Setting USB template %u failed, "
353 			    "continuing.\n", opt->template);
354 		}
355 	}
356 	if (opt->got_get_template) {
357 		opt->got_any--;
358 		if (libusb20_be_get_template(pbe, &opt->template))
359 			printf("USB template: <unknown>\n");
360 		else
361 			printf("USB template: %u\n", opt->template);
362 	}
363 	if (opt->got_any == 0) {
364 		/*
365 		 * do not scan through all the devices if there are no valid
366 		 * options
367 		 */
368 		goto done;
369 	}
370 	while ((pdev = libusb20_be_device_foreach(pbe, pdev))) {
371 
372 		if (opt->got_bus &&
373 		    (libusb20_dev_get_bus_number(pdev) != opt->bus)) {
374 			continue;
375 		}
376 		if (opt->got_addr &&
377 		    (libusb20_dev_get_address(pdev) != opt->addr)) {
378 			continue;
379 		}
380 		matches++;
381 
382 		if (opt->got_remove_quirk) {
383 			struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
384 
385 			ddesc = libusb20_dev_get_device_desc(pdev);
386 
387 			be_dev_remove_quirk(pbe,
388 			    ddesc->idVendor, ddesc->idProduct,
389 			    ddesc->bcdDevice, ddesc->bcdDevice,
390 			    opt->quirkname);
391 		}
392 
393 		if (opt->got_add_quirk) {
394 			struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
395 
396 			ddesc = libusb20_dev_get_device_desc(pdev);
397 
398 			be_dev_add_quirk(pbe,
399 			    ddesc->idVendor, ddesc->idProduct,
400 			    ddesc->bcdDevice, ddesc->bcdDevice,
401 			    opt->quirkname);
402 		}
403 
404 		if (libusb20_dev_open(pdev, 0)) {
405 			err(1, "could not open device");
406 		}
407 		if (opt->got_dump_string) {
408 			dump_string_by_index(pdev, opt->string_index);
409 		}
410 		if (opt->got_do_request) {
411 			uint16_t actlen;
412 			uint16_t t;
413 
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");
420 			} else {
421 				t = actlen;
422 				printf("REQUEST = <");
423 				for (t = 0; t != actlen; t++) {
424 					printf("0x%02x%s",
425 					    ((uint8_t *)opt->buffer)[t],
426 					    (t == (actlen - 1)) ? "" : " ");
427 				}
428 				printf("><");
429 				for (t = 0; t != actlen; t++) {
430 					char c;
431 
432 					c = ((uint8_t *)opt->buffer)[t];
433 					if ((c != '<') &&
434 					    (c != '>') && isprint(c)) {
435 						putchar(c);
436 					}
437 				}
438 				printf(">\n");
439 			}
440 		}
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");
445 			}
446 		}
447 		if (opt->got_set_alt) {
448 			if (libusb20_dev_set_alt_index(pdev, opt->iface,
449 			    opt->alt_index)) {
450 				err(1, "could not set alternate setting");
451 			}
452 		}
453 		if (opt->got_reset) {
454 			if (libusb20_dev_reset(pdev)) {
455 				err(1, "could not reset device");
456 			}
457 		}
458 		if (opt->got_suspend) {
459 			if (libusb20_dev_set_power_mode(pdev,
460 			    LIBUSB20_POWER_SUSPEND)) {
461 				err(1, "could not set suspend");
462 			}
463 		}
464 		if (opt->got_resume) {
465 			if (libusb20_dev_set_power_mode(pdev,
466 			    LIBUSB20_POWER_RESUME)) {
467 				err(1, "could not set resume");
468 			}
469 		}
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");
474 			}
475 		}
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");
480 			}
481 		}
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");
486 			}
487 		}
488 		dump_any =
489 		    (opt->got_dump_device_desc ||
490 		    opt->got_dump_curr_config ||
491 		    opt->got_dump_all_config ||
492 		    opt->got_dump_info);
493 
494 		if (opt->got_list || dump_any) {
495 			dump_device_info(pdev,
496 			    opt->got_show_iface_driver);
497 		}
498 		if (opt->got_dump_device_desc) {
499 			printf("\n");
500 			dump_device_desc(pdev);
501 		}
502 		if (opt->got_dump_all_config) {
503 			printf("\n");
504 			dump_config(pdev, 1);
505 		} else if (opt->got_dump_curr_config) {
506 			printf("\n");
507 			dump_config(pdev, 0);
508 		}
509 		if (dump_any) {
510 			printf("\n");
511 		}
512 		if (libusb20_dev_close(pdev)) {
513 			err(1, "could not close device");
514 		}
515 	}
516 
517 	if (matches == 0) {
518 		printf("No device match or lack of permissions.\n");
519 	}
520 done:
521 	reset_options(opt);
522 
523 	return;
524 }
525 
526 int
527 main(int argc, char **argv)
528 {
529 	struct libusb20_backend *pbe;
530 	struct options *opt = &options;
531 	const char *ptr;
532 	int unit;
533 	int addr;
534 	int n;
535 	int t;
536 
537 	if (argc < 1) {
538 		usage();
539 	}
540 	pbe = libusb20_be_alloc_default();
541 	if (pbe == NULL)
542 		err(1, "could not access USB backend\n");
543 
544 	for (n = 1; n != argc; n++) {
545 
546 		/* get number of additional options */
547 		t = (argc - n - 1);
548 		if (t > 255)
549 			t = 255;
550 		switch (get_token(argv[n], t)) {
551 		case T_ADD_QUIRK:
552 			if (opt->got_add_quirk) {
553 				flush_command(pbe, opt);
554 			}
555 			opt->quirkname = argv[n + 1];
556 			n++;
557 
558 			opt->got_add_quirk = 1;
559 			opt->got_any++;
560 			break;
561 
562 		case T_REMOVE_QUIRK:
563 			if (opt->got_remove_quirk) {
564 				flush_command(pbe, opt);
565 			}
566 			opt->quirkname = argv[n + 1];
567 			n++;
568 
569 			opt->got_remove_quirk = 1;
570 			opt->got_any++;
571 			break;
572 
573 		case T_ADD_DEVICE_QUIRK:
574 			if (opt->got_add_device_quirk) {
575 				flush_command(pbe, opt);
576 			}
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];
582 			n += 5;
583 
584 			opt->got_add_device_quirk = 1;
585 			opt->got_any++;
586 			break;
587 
588 		case T_REMOVE_DEVICE_QUIRK:
589 			if (opt->got_remove_device_quirk) {
590 				flush_command(pbe, opt);
591 			}
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];
597 			n += 5;
598 			opt->got_remove_device_quirk = 1;
599 			opt->got_any++;
600 			break;
601 
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;
606 			opt->got_any++;
607 			break;
608 
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;
613 			opt->got_any++;
614 			break;
615 
616 		case T_SHOW_IFACE_DRIVER:
617 			opt->got_show_iface_driver = 1;
618 			break;
619 
620 		case T_UGEN:
621 			if (opt->got_any) {
622 				/* allow multiple commands on the same line */
623 				flush_command(pbe, opt);
624 			}
625 			ptr = argv[n + 1];
626 
627 			if ((ptr[0] == 'u') &&
628 			    (ptr[1] == 'g') &&
629 			    (ptr[2] == 'e') &&
630 			    (ptr[3] == 'n'))
631 				ptr += 4;
632 
633 			if ((sscanf(ptr, "%d.%d",
634 			    &unit, &addr) != 2) ||
635 			    (unit < 0) || (unit > 65535) ||
636 			    (addr < 0) || (addr > 65535)) {
637 				errx(1, "cannot "
638 				    "parse '%s'", argv[n + 1]);
639 			}
640 			opt->bus = unit;
641 			opt->addr = addr;
642 			opt->got_bus = 1;
643 			opt->got_addr = 1;
644 			n++;
645 			break;
646 
647 		case T_UNIT:
648 			if (opt->got_any) {
649 				/* allow multiple commands on the same line */
650 				flush_command(pbe, opt);
651 			}
652 			opt->bus = num_id(argv[n + 1], "busnum");
653 			opt->got_bus = 1;
654 			n++;
655 			break;
656 		case T_ADDR:
657 			opt->addr = num_id(argv[n + 1], "addr");
658 			opt->got_addr = 1;
659 			n++;
660 			break;
661 		case T_IFACE:
662 			opt->iface = num_id(argv[n + 1], "iface");
663 			opt->got_iface = 1;
664 			n++;
665 			break;
666 		case T_SET_CONFIG:
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;
671 			opt->got_any++;
672 			n++;
673 			break;
674 		case T_SET_ALT:
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;
679 			opt->got_any++;
680 			n++;
681 			break;
682 		case T_SET_TEMPLATE:
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;
687 			opt->got_any++;
688 			n++;
689 			break;
690 		case T_GET_TEMPLATE:
691 			if (opt->got_get_template)
692 				duplicate_option(argv[n]);
693 			opt->got_get_template = 1;
694 			opt->got_any++;
695 			break;
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;
700 			opt->got_any++;
701 			break;
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;
706 			opt->got_any++;
707 			break;
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;
712 			opt->got_any++;
713 			break;
714 		case T_DUMP_INFO:
715 			if (opt->got_dump_info)
716 				duplicate_option(argv[n]);
717 			opt->got_dump_info = 1;
718 			opt->got_any++;
719 			break;
720 		case T_DUMP_STRING:
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;
725 			opt->got_any++;
726 			n++;
727 			break;
728 		case T_SUSPEND:
729 			if (opt->got_suspend)
730 				duplicate_option(argv[n]);
731 			opt->got_suspend = 1;
732 			opt->got_any++;
733 			break;
734 		case T_RESUME:
735 			if (opt->got_resume)
736 				duplicate_option(argv[n]);
737 			opt->got_resume = 1;
738 			opt->got_any++;
739 			break;
740 		case T_POWER_OFF:
741 			if (opt->got_power_off)
742 				duplicate_option(argv[n]);
743 			opt->got_power_off = 1;
744 			opt->got_any++;
745 			break;
746 		case T_POWER_SAVE:
747 			if (opt->got_power_save)
748 				duplicate_option(argv[n]);
749 			opt->got_power_save = 1;
750 			opt->got_any++;
751 			break;
752 		case T_POWER_ON:
753 			if (opt->got_power_on)
754 				duplicate_option(argv[n]);
755 			opt->got_power_on = 1;
756 			opt->got_any++;
757 			break;
758 		case T_RESET:
759 			if (opt->got_reset)
760 				duplicate_option(argv[n]);
761 			opt->got_reset = 1;
762 			opt->got_any++;
763 			break;
764 		case T_LIST:
765 			if (opt->got_list)
766 				duplicate_option(argv[n]);
767 			opt->got_list = 1;
768 			opt->got_any++;
769 			break;
770 		case T_DO_REQUEST:
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);
781 			} else {
782 				opt->buffer = NULL;
783 			}
784 
785 			n += 5;
786 
787 			if (!(opt->setup.bmRequestType &
788 			    LIBUSB20_ENDPOINT_IN)) {
789 				/* copy in data */
790 				t = (argc - n - 1);
791 				if (t < opt->setup.wLength) {
792 					err(1, "request data missing");
793 				}
794 				t = opt->setup.wLength;
795 				while (t--) {
796 					((uint8_t *)opt->buffer)[t] =
797 					    num_id(argv[n + t + 1], "req_data");
798 				}
799 				n += opt->setup.wLength;
800 			}
801 			opt->got_do_request = 1;
802 			opt->got_any++;
803 			break;
804 		default:
805 			usage();
806 			break;
807 		}
808 	}
809 	if (opt->got_any) {
810 		/* flush out last command */
811 		flush_command(pbe, opt);
812 	} else {
813 		/* list all the devices */
814 		opt->got_list = 1;
815 		opt->got_any++;
816 		flush_command(pbe, opt);
817 	}
818 	/* release data */
819 	libusb20_be_free(pbe);
820 
821 	return (0);
822 }
823