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