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