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