xref: /freebsd/usr.sbin/usbconfig/usbconfig.c (revision 5bf5ca772c6de2d53344a78cf461447cc322ccea)
1 /* $FreeBSD$ */
2 /*-
3  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4  *
5  * Copyright (c) 2008-2009 Hans Petter Selasky. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <err.h>
33 #include <string.h>
34 #include <pwd.h>
35 #include <grp.h>
36 #include <errno.h>
37 #include <ctype.h>
38 #include <sys/types.h>
39 
40 #include <libusb20_desc.h>
41 #include <libusb20.h>
42 
43 #include "dump.h"
44 
45 struct options {
46 	const char *quirkname;
47 	void   *buffer;
48 	int template;
49 	gid_t	gid;
50 	uid_t	uid;
51 	mode_t	mode;
52 	uint32_t got_any;
53 	struct LIBUSB20_CONTROL_SETUP_DECODED setup;
54 	uint16_t bus;
55 	uint16_t addr;
56 	uint16_t iface;
57 	uint16_t vid;
58 	uint16_t pid;
59 	uint16_t lo_rev;		/* inclusive */
60 	uint16_t hi_rev;		/* inclusive */
61 	uint8_t	string_index;
62 	uint8_t	config_index;
63 	uint8_t	alt_index;
64 	uint8_t	got_list:1;
65 	uint8_t	got_bus:1;
66 	uint8_t	got_addr:1;
67 	uint8_t	got_iface:1;
68 	uint8_t	got_set_config:1;
69 	uint8_t	got_set_alt:1;
70 	uint8_t	got_set_template:1;
71 	uint8_t	got_get_template:1;
72 	uint8_t	got_suspend:1;
73 	uint8_t	got_resume:1;
74 	uint8_t	got_reset:1;
75 	uint8_t	got_power_off:1;
76 	uint8_t	got_power_save:1;
77 	uint8_t	got_power_on:1;
78 	uint8_t	got_dump_device_quirks:1;
79 	uint8_t	got_dump_quirk_names:1;
80 	uint8_t	got_dump_device_desc:1;
81 	uint8_t	got_dump_curr_config:1;
82 	uint8_t	got_dump_all_config:1;
83 	uint8_t	got_dump_info:1;
84 	uint8_t	got_show_iface_driver:1;
85 	uint8_t	got_remove_device_quirk:1;
86 	uint8_t	got_add_device_quirk:1;
87 	uint8_t	got_remove_quirk:1;
88 	uint8_t	got_add_quirk:1;
89 	uint8_t	got_dump_string:1;
90 	uint8_t	got_do_request:1;
91 };
92 
93 struct token {
94 	const char *name;
95 	uint8_t	value;
96 	uint8_t	narg;
97 };
98 
99 enum {
100 	T_UNIT,
101 	T_ADDR,
102 	T_UGEN,
103 	T_IFACE,
104 	T_SET_CONFIG,
105 	T_SET_ALT,
106 	T_SET_TEMPLATE,
107 	T_GET_TEMPLATE,
108 	T_ADD_DEVICE_QUIRK,
109 	T_REMOVE_DEVICE_QUIRK,
110 	T_ADD_QUIRK,
111 	T_REMOVE_QUIRK,
112 	T_SHOW_IFACE_DRIVER,
113 	T_DUMP_QUIRK_NAMES,
114 	T_DUMP_DEVICE_QUIRKS,
115 	T_DUMP_DEVICE_DESC,
116 	T_DUMP_CURR_CONFIG_DESC,
117 	T_DUMP_ALL_CONFIG_DESC,
118 	T_DUMP_STRING,
119 	T_DUMP_INFO,
120 	T_SUSPEND,
121 	T_RESUME,
122 	T_POWER_OFF,
123 	T_POWER_SAVE,
124 	T_POWER_ON,
125 	T_RESET,
126 	T_LIST,
127 	T_DO_REQUEST,
128 };
129 
130 static struct options options;
131 
132 static const struct token token[] = {
133 	{"-u", T_UNIT, 1},
134 	{"-a", T_ADDR, 1},
135 	{"-d", T_UGEN, 1},
136 	{"-i", T_IFACE, 1},
137 	{"set_config", T_SET_CONFIG, 1},
138 	{"set_alt", T_SET_ALT, 1},
139 	{"set_template", T_SET_TEMPLATE, 1},
140 	{"get_template", T_GET_TEMPLATE, 0},
141 	{"add_dev_quirk_vplh", T_ADD_DEVICE_QUIRK, 5},
142 	{"remove_dev_quirk_vplh", T_REMOVE_DEVICE_QUIRK, 5},
143 	{"add_quirk", T_ADD_QUIRK, 1},
144 	{"remove_quirk", T_REMOVE_QUIRK, 1},
145 	{"dump_quirk_names", T_DUMP_QUIRK_NAMES, 0},
146 	{"dump_device_quirks", T_DUMP_DEVICE_QUIRKS, 0},
147 	{"dump_device_desc", T_DUMP_DEVICE_DESC, 0},
148 	{"dump_curr_config_desc", T_DUMP_CURR_CONFIG_DESC, 0},
149 	{"dump_all_config_desc", T_DUMP_ALL_CONFIG_DESC, 0},
150 	{"dump_string", T_DUMP_STRING, 1},
151 	{"dump_info", T_DUMP_INFO, 0},
152 	{"show_ifdrv", T_SHOW_IFACE_DRIVER, 0},
153 	{"suspend", T_SUSPEND, 0},
154 	{"resume", T_RESUME, 0},
155 	{"power_off", T_POWER_OFF, 0},
156 	{"power_save", T_POWER_SAVE, 0},
157 	{"power_on", T_POWER_ON, 0},
158 	{"reset", T_RESET, 0},
159 	{"list", T_LIST, 0},
160 	{"do_request", T_DO_REQUEST, 5},
161 };
162 
163 static void
164 be_dev_remove_quirk(struct libusb20_backend *pbe,
165     uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
166     const char *str)
167 {
168 	struct libusb20_quirk q;
169 	int error;
170 
171 	memset(&q, 0, sizeof(q));
172 
173 	q.vid = vid;
174 	q.pid = pid;
175 	q.bcdDeviceLow = lorev;
176 	q.bcdDeviceHigh = hirev;
177 	strlcpy(q.quirkname, str, sizeof(q.quirkname));
178 
179 	error = libusb20_be_remove_dev_quirk(pbe, &q);
180 	if (error) {
181 		fprintf(stderr, "Removing quirk '%s' failed, continuing.\n", str);
182 	}
183 	return;
184 }
185 
186 static void
187 be_dev_add_quirk(struct libusb20_backend *pbe,
188     uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
189     const char *str)
190 {
191 	struct libusb20_quirk q;
192 	int error;
193 
194 	memset(&q, 0, sizeof(q));
195 
196 	q.vid = vid;
197 	q.pid = pid;
198 	q.bcdDeviceLow = lorev;
199 	q.bcdDeviceHigh = hirev;
200 	strlcpy(q.quirkname, str, sizeof(q.quirkname));
201 
202 	error = libusb20_be_add_dev_quirk(pbe, &q);
203 	if (error) {
204 		fprintf(stderr, "Adding quirk '%s' failed, continuing.\n", str);
205 	}
206 	return;
207 }
208 
209 static uint8_t
210 get_token(const char *str, uint8_t narg)
211 {
212 	uint8_t n;
213 
214 	for (n = 0; n != (sizeof(token) / sizeof(token[0])); n++) {
215 		if (strcasecmp(str, token[n].name) == 0) {
216 			if (token[n].narg > narg) {
217 				/* too few arguments */
218 				break;
219 			}
220 			return (token[n].value);
221 		}
222 	}
223 	return (0 - 1);
224 }
225 
226 static uid_t
227 num_id(const char *name, const char *type)
228 {
229 	uid_t val;
230 	char *ep;
231 
232 	errno = 0;
233 	val = strtoul(name, &ep, 0);
234 	if (errno) {
235 		err(1, "%s", name);
236 	}
237 	if (*ep != '\0') {
238 		errx(1, "%s: illegal %s name", name, type);
239 	}
240 	return (val);
241 }
242 
243 static int
244 get_int(const char *s)
245 {
246 	int val;
247 	char *ep;
248 
249 	errno = 0;
250 	val = strtoul(s, &ep, 0);
251 	if (errno) {
252 		err(1, "%s", s);
253 	}
254 	if (*ep != '\0') {
255 		errx(1, "illegal number: %s", s);
256 	}
257 	return val;
258 }
259 
260 static void
261 duplicate_option(const char *ptr)
262 {
263 	fprintf(stderr, "Syntax error: "
264 	    "Duplicate option: '%s'\n", ptr);
265 	exit(1);
266 }
267 
268 static void
269 usage(void)
270 {
271 	fprintf(stderr, ""
272 	    "usbconfig - configure the USB subsystem" "\n"
273 	    "usage: usbconfig -u <busnum> -a <devaddr> -i <ifaceindex> [cmds...]" "\n"
274 	    "usage: usbconfig -d [ugen]<busnum>.<devaddr> -i <ifaceindex> [cmds...]" "\n"
275 	    "commands:" "\n"
276 	    "  set_config <cfg_index>" "\n"
277 	    "  set_alt <alt_index>" "\n"
278 	    "  set_template <template>" "\n"
279 	    "  get_template" "\n"
280 	    "  add_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
281 	    "  remove_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
282 	    "  add_quirk <quirk>" "\n"
283 	    "  remove_quirk <quirk>" "\n"
284 	    "  dump_quirk_names" "\n"
285 	    "  dump_device_quirks" "\n"
286 	    "  dump_device_desc" "\n"
287 	    "  dump_curr_config_desc" "\n"
288 	    "  dump_all_config_desc" "\n"
289 	    "  dump_string <index>" "\n"
290 	    "  dump_info" "\n"
291 	    "  show_ifdrv" "\n"
292 	    "  suspend" "\n"
293 	    "  resume" "\n"
294 	    "  power_off" "\n"
295 	    "  power_save" "\n"
296 	    "  power_on" "\n"
297 	    "  reset" "\n"
298 	    "  list" "\n"
299 	    "  do_request <bmReqTyp> <bReq> <wVal> <wIdx> <wLen> <data...>" "\n"
300 	);
301 	exit(1);
302 }
303 
304 static void
305 reset_options(struct options *opt)
306 {
307 	if (opt->buffer)
308 		free(opt->buffer);
309 	memset(opt, 0, sizeof(*opt));
310 	return;
311 }
312 
313 static void
314 flush_command(struct libusb20_backend *pbe, struct options *opt)
315 {
316 	struct libusb20_device *pdev = NULL;
317 	uint32_t matches = 0;
318 	uint8_t dump_any;
319 
320 	/* check for invalid option combinations */
321 	if ((opt->got_suspend +
322 	    opt->got_resume +
323 	    opt->got_reset +
324 	    opt->got_set_config +
325 	    opt->got_set_alt +
326 	    opt->got_power_save +
327 	    opt->got_power_on +
328 	    opt->got_power_off) > 1) {
329 		err(1, "can only specify one of 'set_config', "
330 		    "'set_alt', 'reset', 'suspend', 'resume', "
331 		    "'power_save', 'power_on' and 'power_off' "
332 		    "at the same time!");
333 	}
334 	if (opt->got_dump_quirk_names) {
335 		opt->got_any--;
336 		dump_be_quirk_names(pbe);
337 	}
338 	if (opt->got_dump_device_quirks) {
339 		opt->got_any--;
340 		dump_be_dev_quirks(pbe);
341 	}
342 	if (opt->got_remove_device_quirk) {
343 		opt->got_any--;
344 		be_dev_remove_quirk(pbe,
345 		    opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
346 	}
347 	if (opt->got_add_device_quirk) {
348 		opt->got_any--;
349 		be_dev_add_quirk(pbe,
350 		    opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
351 	}
352 	if (opt->got_set_template) {
353 		opt->got_any--;
354 		if (libusb20_be_set_template(pbe, opt->template)) {
355 			fprintf(stderr, "Setting USB template %u failed, "
356 			    "continuing.\n", opt->template);
357 		}
358 	}
359 	if (opt->got_get_template) {
360 		opt->got_any--;
361 		if (libusb20_be_get_template(pbe, &opt->template))
362 			printf("USB template: <unknown>\n");
363 		else
364 			printf("USB template: %u\n", opt->template);
365 	}
366 	if (opt->got_any == 0) {
367 		/*
368 		 * do not scan through all the devices if there are no valid
369 		 * options
370 		 */
371 		goto done;
372 	}
373 	while ((pdev = libusb20_be_device_foreach(pbe, pdev))) {
374 
375 		if (opt->got_bus &&
376 		    (libusb20_dev_get_bus_number(pdev) != opt->bus)) {
377 			continue;
378 		}
379 		if (opt->got_addr &&
380 		    (libusb20_dev_get_address(pdev) != opt->addr)) {
381 			continue;
382 		}
383 		matches++;
384 
385 		if (opt->got_remove_quirk) {
386 			struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
387 
388 			ddesc = libusb20_dev_get_device_desc(pdev);
389 
390 			be_dev_remove_quirk(pbe,
391 			    ddesc->idVendor, ddesc->idProduct,
392 			    ddesc->bcdDevice, ddesc->bcdDevice,
393 			    opt->quirkname);
394 		}
395 
396 		if (opt->got_add_quirk) {
397 			struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
398 
399 			ddesc = libusb20_dev_get_device_desc(pdev);
400 
401 			be_dev_add_quirk(pbe,
402 			    ddesc->idVendor, ddesc->idProduct,
403 			    ddesc->bcdDevice, ddesc->bcdDevice,
404 			    opt->quirkname);
405 		}
406 
407 		if (libusb20_dev_open(pdev, 0)) {
408 			err(1, "could not open device");
409 		}
410 		if (opt->got_dump_string) {
411 			dump_string_by_index(pdev, opt->string_index);
412 		}
413 		if (opt->got_do_request) {
414 			uint16_t actlen;
415 			uint16_t t;
416 
417 			if (libusb20_dev_request_sync(pdev, &opt->setup,
418 			    opt->buffer, &actlen, 5000 /* 5 seconds */ , 0)) {
419 				printf("REQUEST = <ERROR>\n");
420 			} else if (!(opt->setup.bmRequestType &
421 			    LIBUSB20_ENDPOINT_IN)) {
422 				printf("REQUEST = <OK>\n");
423 			} else {
424 				t = actlen;
425 				printf("REQUEST = <");
426 				for (t = 0; t != actlen; t++) {
427 					printf("0x%02x%s",
428 					    ((uint8_t *)opt->buffer)[t],
429 					    (t == (actlen - 1)) ? "" : " ");
430 				}
431 				printf("><");
432 				for (t = 0; t != actlen; t++) {
433 					char c;
434 
435 					c = ((uint8_t *)opt->buffer)[t];
436 					if ((c != '<') &&
437 					    (c != '>') && isprint(c)) {
438 						putchar(c);
439 					}
440 				}
441 				printf(">\n");
442 			}
443 		}
444 		if (opt->got_set_config) {
445 			if (libusb20_dev_set_config_index(pdev,
446 			    opt->config_index)) {
447 				err(1, "could not set config index");
448 			}
449 		}
450 		if (opt->got_set_alt) {
451 			if (libusb20_dev_set_alt_index(pdev, opt->iface,
452 			    opt->alt_index)) {
453 				err(1, "could not set alternate setting");
454 			}
455 		}
456 		if (opt->got_reset) {
457 			if (libusb20_dev_reset(pdev)) {
458 				err(1, "could not reset device");
459 			}
460 		}
461 		if (opt->got_suspend) {
462 			if (libusb20_dev_set_power_mode(pdev,
463 			    LIBUSB20_POWER_SUSPEND)) {
464 				err(1, "could not set suspend");
465 			}
466 		}
467 		if (opt->got_resume) {
468 			if (libusb20_dev_set_power_mode(pdev,
469 			    LIBUSB20_POWER_RESUME)) {
470 				err(1, "could not set resume");
471 			}
472 		}
473 		if (opt->got_power_off) {
474 			if (libusb20_dev_set_power_mode(pdev,
475 			    LIBUSB20_POWER_OFF)) {
476 				err(1, "could not set power OFF");
477 			}
478 		}
479 		if (opt->got_power_save) {
480 			if (libusb20_dev_set_power_mode(pdev,
481 			    LIBUSB20_POWER_SAVE)) {
482 				err(1, "could not set power SAVE");
483 			}
484 		}
485 		if (opt->got_power_on) {
486 			if (libusb20_dev_set_power_mode(pdev,
487 			    LIBUSB20_POWER_ON)) {
488 				err(1, "could not set power ON");
489 			}
490 		}
491 		dump_any =
492 		    (opt->got_dump_device_desc ||
493 		    opt->got_dump_curr_config ||
494 		    opt->got_dump_all_config ||
495 		    opt->got_dump_info);
496 
497 		if (opt->got_list || dump_any) {
498 			dump_device_info(pdev,
499 			    opt->got_show_iface_driver);
500 		}
501 		if (opt->got_dump_device_desc) {
502 			printf("\n");
503 			dump_device_desc(pdev);
504 		}
505 		if (opt->got_dump_all_config) {
506 			printf("\n");
507 			dump_config(pdev, 1);
508 		} else if (opt->got_dump_curr_config) {
509 			printf("\n");
510 			dump_config(pdev, 0);
511 		}
512 		if (dump_any) {
513 			printf("\n");
514 		}
515 		if (libusb20_dev_close(pdev)) {
516 			err(1, "could not close device");
517 		}
518 	}
519 
520 	if (matches == 0) {
521 		printf("No device match or lack of permissions.\n");
522 	}
523 done:
524 	reset_options(opt);
525 
526 	return;
527 }
528 
529 int
530 main(int argc, char **argv)
531 {
532 	struct libusb20_backend *pbe;
533 	struct options *opt = &options;
534 	const char *ptr;
535 	int unit;
536 	int addr;
537 	int n;
538 	int t;
539 
540 	if (argc < 1) {
541 		usage();
542 	}
543 	pbe = libusb20_be_alloc_default();
544 	if (pbe == NULL)
545 		err(1, "could not access USB backend\n");
546 
547 	for (n = 1; n != argc; n++) {
548 
549 		/* get number of additional options */
550 		t = (argc - n - 1);
551 		if (t > 255)
552 			t = 255;
553 		switch (get_token(argv[n], t)) {
554 		case T_ADD_QUIRK:
555 			if (opt->got_add_quirk) {
556 				flush_command(pbe, opt);
557 			}
558 			opt->quirkname = argv[n + 1];
559 			n++;
560 
561 			opt->got_add_quirk = 1;
562 			opt->got_any++;
563 			break;
564 
565 		case T_REMOVE_QUIRK:
566 			if (opt->got_remove_quirk) {
567 				flush_command(pbe, opt);
568 			}
569 			opt->quirkname = argv[n + 1];
570 			n++;
571 
572 			opt->got_remove_quirk = 1;
573 			opt->got_any++;
574 			break;
575 
576 		case T_ADD_DEVICE_QUIRK:
577 			if (opt->got_add_device_quirk) {
578 				flush_command(pbe, opt);
579 			}
580 			opt->vid = num_id(argv[n + 1], "Vendor ID");
581 			opt->pid = num_id(argv[n + 2], "Product ID");
582 			opt->lo_rev = num_id(argv[n + 3], "Low Revision");
583 			opt->hi_rev = num_id(argv[n + 4], "High Revision");
584 			opt->quirkname = argv[n + 5];
585 			n += 5;
586 
587 			opt->got_add_device_quirk = 1;
588 			opt->got_any++;
589 			break;
590 
591 		case T_REMOVE_DEVICE_QUIRK:
592 			if (opt->got_remove_device_quirk) {
593 				flush_command(pbe, opt);
594 			}
595 			opt->vid = num_id(argv[n + 1], "Vendor ID");
596 			opt->pid = num_id(argv[n + 2], "Product ID");
597 			opt->lo_rev = num_id(argv[n + 3], "Low Revision");
598 			opt->hi_rev = num_id(argv[n + 4], "High Revision");
599 			opt->quirkname = argv[n + 5];
600 			n += 5;
601 			opt->got_remove_device_quirk = 1;
602 			opt->got_any++;
603 			break;
604 
605 		case T_DUMP_QUIRK_NAMES:
606 			if (opt->got_dump_quirk_names)
607 				duplicate_option(argv[n]);
608 			opt->got_dump_quirk_names = 1;
609 			opt->got_any++;
610 			break;
611 
612 		case T_DUMP_DEVICE_QUIRKS:
613 			if (opt->got_dump_device_quirks)
614 				duplicate_option(argv[n]);
615 			opt->got_dump_device_quirks = 1;
616 			opt->got_any++;
617 			break;
618 
619 		case T_SHOW_IFACE_DRIVER:
620 			opt->got_show_iface_driver = 1;
621 			break;
622 
623 		case T_UGEN:
624 			if (opt->got_any) {
625 				/* allow multiple commands on the same line */
626 				flush_command(pbe, opt);
627 			}
628 			ptr = argv[n + 1];
629 
630 			if ((ptr[0] == 'u') &&
631 			    (ptr[1] == 'g') &&
632 			    (ptr[2] == 'e') &&
633 			    (ptr[3] == 'n'))
634 				ptr += 4;
635 
636 			if ((sscanf(ptr, "%d.%d",
637 			    &unit, &addr) != 2) ||
638 			    (unit < 0) || (unit > 65535) ||
639 			    (addr < 0) || (addr > 65535)) {
640 				errx(1, "cannot "
641 				    "parse '%s'", argv[n + 1]);
642 			}
643 			opt->bus = unit;
644 			opt->addr = addr;
645 			opt->got_bus = 1;
646 			opt->got_addr = 1;
647 			n++;
648 			break;
649 
650 		case T_UNIT:
651 			if (opt->got_any) {
652 				/* allow multiple commands on the same line */
653 				flush_command(pbe, opt);
654 			}
655 			opt->bus = num_id(argv[n + 1], "busnum");
656 			opt->got_bus = 1;
657 			n++;
658 			break;
659 		case T_ADDR:
660 			opt->addr = num_id(argv[n + 1], "addr");
661 			opt->got_addr = 1;
662 			n++;
663 			break;
664 		case T_IFACE:
665 			opt->iface = num_id(argv[n + 1], "iface");
666 			opt->got_iface = 1;
667 			n++;
668 			break;
669 		case T_SET_CONFIG:
670 			if (opt->got_set_config)
671 				duplicate_option(argv[n]);
672 			opt->config_index = num_id(argv[n + 1], "cfg_index");
673 			opt->got_set_config = 1;
674 			opt->got_any++;
675 			n++;
676 			break;
677 		case T_SET_ALT:
678 			if (opt->got_set_alt)
679 				duplicate_option(argv[n]);
680 			opt->alt_index = num_id(argv[n + 1], "cfg_index");
681 			opt->got_set_alt = 1;
682 			opt->got_any++;
683 			n++;
684 			break;
685 		case T_SET_TEMPLATE:
686 			if (opt->got_set_template)
687 				duplicate_option(argv[n]);
688 			opt->template = get_int(argv[n + 1]);
689 			opt->got_set_template = 1;
690 			opt->got_any++;
691 			n++;
692 			break;
693 		case T_GET_TEMPLATE:
694 			if (opt->got_get_template)
695 				duplicate_option(argv[n]);
696 			opt->got_get_template = 1;
697 			opt->got_any++;
698 			break;
699 		case T_DUMP_DEVICE_DESC:
700 			if (opt->got_dump_device_desc)
701 				duplicate_option(argv[n]);
702 			opt->got_dump_device_desc = 1;
703 			opt->got_any++;
704 			break;
705 		case T_DUMP_CURR_CONFIG_DESC:
706 			if (opt->got_dump_curr_config)
707 				duplicate_option(argv[n]);
708 			opt->got_dump_curr_config = 1;
709 			opt->got_any++;
710 			break;
711 		case T_DUMP_ALL_CONFIG_DESC:
712 			if (opt->got_dump_all_config)
713 				duplicate_option(argv[n]);
714 			opt->got_dump_all_config = 1;
715 			opt->got_any++;
716 			break;
717 		case T_DUMP_INFO:
718 			if (opt->got_dump_info)
719 				duplicate_option(argv[n]);
720 			opt->got_dump_info = 1;
721 			opt->got_any++;
722 			break;
723 		case T_DUMP_STRING:
724 			if (opt->got_dump_string)
725 				duplicate_option(argv[n]);
726 			opt->string_index = num_id(argv[n + 1], "str_index");
727 			opt->got_dump_string = 1;
728 			opt->got_any++;
729 			n++;
730 			break;
731 		case T_SUSPEND:
732 			if (opt->got_suspend)
733 				duplicate_option(argv[n]);
734 			opt->got_suspend = 1;
735 			opt->got_any++;
736 			break;
737 		case T_RESUME:
738 			if (opt->got_resume)
739 				duplicate_option(argv[n]);
740 			opt->got_resume = 1;
741 			opt->got_any++;
742 			break;
743 		case T_POWER_OFF:
744 			if (opt->got_power_off)
745 				duplicate_option(argv[n]);
746 			opt->got_power_off = 1;
747 			opt->got_any++;
748 			break;
749 		case T_POWER_SAVE:
750 			if (opt->got_power_save)
751 				duplicate_option(argv[n]);
752 			opt->got_power_save = 1;
753 			opt->got_any++;
754 			break;
755 		case T_POWER_ON:
756 			if (opt->got_power_on)
757 				duplicate_option(argv[n]);
758 			opt->got_power_on = 1;
759 			opt->got_any++;
760 			break;
761 		case T_RESET:
762 			if (opt->got_reset)
763 				duplicate_option(argv[n]);
764 			opt->got_reset = 1;
765 			opt->got_any++;
766 			break;
767 		case T_LIST:
768 			if (opt->got_list)
769 				duplicate_option(argv[n]);
770 			opt->got_list = 1;
771 			opt->got_any++;
772 			break;
773 		case T_DO_REQUEST:
774 			if (opt->got_do_request)
775 				duplicate_option(argv[n]);
776 			LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &opt->setup);
777 			opt->setup.bmRequestType = num_id(argv[n + 1], "bmReqTyp");
778 			opt->setup.bRequest = num_id(argv[n + 2], "bReq");
779 			opt->setup.wValue = num_id(argv[n + 3], "wVal");
780 			opt->setup.wIndex = num_id(argv[n + 4], "wIndex");
781 			opt->setup.wLength = num_id(argv[n + 5], "wLen");
782 			if (opt->setup.wLength != 0) {
783 				opt->buffer = malloc(opt->setup.wLength);
784 			} else {
785 				opt->buffer = NULL;
786 			}
787 
788 			n += 5;
789 
790 			if (!(opt->setup.bmRequestType &
791 			    LIBUSB20_ENDPOINT_IN)) {
792 				/* copy in data */
793 				t = (argc - n - 1);
794 				if (t < opt->setup.wLength) {
795 					err(1, "request data missing");
796 				}
797 				t = opt->setup.wLength;
798 				while (t--) {
799 					((uint8_t *)opt->buffer)[t] =
800 					    num_id(argv[n + t + 1], "req_data");
801 				}
802 				n += opt->setup.wLength;
803 			}
804 			opt->got_do_request = 1;
805 			opt->got_any++;
806 			break;
807 		default:
808 			usage();
809 			break;
810 		}
811 	}
812 	if (opt->got_any) {
813 		/* flush out last command */
814 		flush_command(pbe, opt);
815 	} else {
816 		/* list all the devices */
817 		opt->got_list = 1;
818 		opt->got_any++;
819 		flush_command(pbe, opt);
820 	}
821 	/* release data */
822 	libusb20_be_free(pbe);
823 
824 	return (0);
825 }
826