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