usbconfig.c revision 189110
1205821Sedwin/* $FreeBSD: head/usr.sbin/usbconfig/usbconfig.c 189110 2009-02-27 17:27:16Z thompsa $ */
2205821Sedwin/*-
3205821Sedwin * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4205821Sedwin *
5205821Sedwin * Redistribution and use in source and binary forms, with or without
6205821Sedwin * modification, are permitted provided that the following conditions
7205821Sedwin * are met:
8205821Sedwin * 1. Redistributions of source code must retain the above copyright
9205821Sedwin *    notice, this list of conditions and the following disclaimer.
10205821Sedwin * 2. Redistributions in binary form must reproduce the above copyright
11205821Sedwin *    notice, this list of conditions and the following disclaimer in the
12205821Sedwin *    documentation and/or other materials provided with the distribution.
13205821Sedwin *
14205821Sedwin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15205821Sedwin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16205821Sedwin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17205821Sedwin * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18205821Sedwin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19205821Sedwin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20205821Sedwin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21205821Sedwin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22205821Sedwin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23205821Sedwin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24205821Sedwin * SUCH DAMAGE.
25205821Sedwin */
26205821Sedwin
27205821Sedwin#include <stdio.h>
28205821Sedwin#include <stdlib.h>
29205821Sedwin#include <stdint.h>
30205821Sedwin#include <err.h>
31205821Sedwin#include <string.h>
32205821Sedwin#include <pwd.h>
33205821Sedwin#include <grp.h>
34205821Sedwin#include <errno.h>
35205821Sedwin#include <ctype.h>
36205821Sedwin
37205821Sedwin#include <libusb20_desc.h>
38205821Sedwin#include <libusb20.h>
39205821Sedwin
40205821Sedwin#include "dump.h"
41205821Sedwin
42205821Sedwinstruct options {
43205821Sedwin	const char *quirkname;
44205821Sedwin	void   *buffer;
45205821Sedwin	int template;
46205821Sedwin	gid_t	gid;
47205821Sedwin	uid_t	uid;
48205821Sedwin	mode_t	mode;
49205821Sedwin	uint32_t got_any;
50205821Sedwin	struct LIBUSB20_CONTROL_SETUP_DECODED setup;
51205821Sedwin	uint16_t bus;
52205821Sedwin	uint16_t addr;
53205821Sedwin	uint16_t iface;
54205821Sedwin	uint16_t vid;
55205821Sedwin	uint16_t pid;
56205821Sedwin	uint16_t lo_rev;		/* inclusive */
57205821Sedwin	uint16_t hi_rev;		/* inclusive */
58205821Sedwin	uint8_t	string_index;
59205821Sedwin	uint8_t	config_index;
60205821Sedwin	uint8_t	alt_index;
61205821Sedwin	uint8_t	got_list:1;
62205821Sedwin	uint8_t	got_bus:1;
63205821Sedwin	uint8_t	got_addr:1;
64205821Sedwin	uint8_t	got_iface:1;
65205821Sedwin	uint8_t	got_set_config:1;
66205821Sedwin	uint8_t	got_set_alt:1;
67205821Sedwin	uint8_t	got_set_template:1;
68205821Sedwin	uint8_t	got_get_template:1;
69205821Sedwin	uint8_t	got_suspend:1;
70205821Sedwin	uint8_t	got_resume:1;
71205821Sedwin	uint8_t	got_reset:1;
72205821Sedwin	uint8_t	got_power_off:1;
73205821Sedwin	uint8_t	got_power_save:1;
74205821Sedwin	uint8_t	got_power_on:1;
75205821Sedwin	uint8_t	got_dump_device_quirks:1;
76205821Sedwin	uint8_t	got_dump_quirk_names:1;
77205821Sedwin	uint8_t	got_dump_device_desc:1;
78205821Sedwin	uint8_t	got_dump_curr_config:1;
79205821Sedwin	uint8_t	got_dump_all_config:1;
80205821Sedwin	uint8_t	got_dump_info:1;
81205821Sedwin	uint8_t	got_show_iface_driver:1;
82205821Sedwin	uint8_t	got_remove_device_quirk:1;
83205821Sedwin	uint8_t	got_add_device_quirk:1;
84205821Sedwin	uint8_t	got_dump_string:1;
85205821Sedwin	uint8_t	got_do_request:1;
86205821Sedwin};
87205821Sedwin
88205821Sedwinstruct token {
89205821Sedwin	const char *name;
90205821Sedwin	uint8_t	value;
91205821Sedwin	uint8_t	narg;
92205821Sedwin};
93205821Sedwin
94205821Sedwinenum {
95205821Sedwin	T_UNIT,
96205821Sedwin	T_ADDR,
97205821Sedwin	T_IFACE,
98205821Sedwin	T_SET_CONFIG,
99205821Sedwin	T_SET_ALT,
100205821Sedwin	T_SET_TEMPLATE,
101205821Sedwin	T_GET_TEMPLATE,
102205821Sedwin	T_ADD_DEVICE_QUIRK,
103205821Sedwin	T_REMOVE_DEVICE_QUIRK,
104205821Sedwin	T_SHOW_IFACE_DRIVER,
105205821Sedwin	T_DUMP_QUIRK_NAMES,
106205821Sedwin	T_DUMP_DEVICE_QUIRKS,
107205821Sedwin	T_DUMP_DEVICE_DESC,
108205821Sedwin	T_DUMP_CURR_CONFIG_DESC,
109205821Sedwin	T_DUMP_ALL_CONFIG_DESC,
110205821Sedwin	T_DUMP_STRING,
111205821Sedwin	T_DUMP_INFO,
112205821Sedwin	T_SUSPEND,
113205821Sedwin	T_RESUME,
114205821Sedwin	T_POWER_OFF,
115205821Sedwin	T_POWER_SAVE,
116205821Sedwin	T_POWER_ON,
117205821Sedwin	T_RESET,
118205821Sedwin	T_LIST,
119205821Sedwin	T_DO_REQUEST,
120205821Sedwin};
121205821Sedwin
122205821Sedwinstatic struct options options;
123205821Sedwin
124205821Sedwinstatic const struct token token[] = {
125205821Sedwin	{"-u", T_UNIT, 1},
126	{"-a", T_ADDR, 1},
127	{"-i", T_IFACE, 1},
128	{"set_config", T_SET_CONFIG, 1},
129	{"set_alt", T_SET_ALT, 1},
130	{"set_template", T_SET_TEMPLATE, 1},
131	{"get_template", T_GET_TEMPLATE, 0},
132	{"add_dev_quirk_vplh", T_ADD_DEVICE_QUIRK, 5},
133	{"remove_dev_quirk_vplh", T_REMOVE_DEVICE_QUIRK, 5},
134	{"dump_quirk_names", T_DUMP_QUIRK_NAMES, 0},
135	{"dump_device_quirks", T_DUMP_DEVICE_QUIRKS, 0},
136	{"dump_device_desc", T_DUMP_DEVICE_DESC, 0},
137	{"dump_curr_config_desc", T_DUMP_CURR_CONFIG_DESC, 0},
138	{"dump_all_config_desc", T_DUMP_ALL_CONFIG_DESC, 0},
139	{"dump_string", T_DUMP_STRING, 1},
140	{"dump_info", T_DUMP_INFO, 0},
141	{"show_ifdrv", T_SHOW_IFACE_DRIVER, 0},
142	{"suspend", T_SUSPEND, 0},
143	{"resume", T_RESUME, 0},
144	{"power_off", T_POWER_OFF, 0},
145	{"power_save", T_POWER_SAVE, 0},
146	{"power_on", T_POWER_ON, 0},
147	{"reset", T_RESET, 0},
148	{"list", T_LIST, 0},
149	{"do_request", T_DO_REQUEST, 5},
150};
151
152static void
153be_dev_remove_quirk(struct libusb20_backend *pbe,
154    uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
155    const char *str)
156{
157	struct libusb20_quirk q;
158	int error;
159
160	memset(&q, 0, sizeof(q));
161
162	q.vid = vid;
163	q.pid = pid;
164	q.bcdDeviceLow = lorev;
165	q.bcdDeviceHigh = hirev;
166	strlcpy(q.quirkname, str, sizeof(q.quirkname));
167
168	error = libusb20_be_remove_dev_quirk(pbe, &q);
169	if (error) {
170		printf("Removing quirk '%s' failed, continuing.\n", str);
171	}
172	return;
173}
174
175static void
176be_dev_add_quirk(struct libusb20_backend *pbe,
177    uint16_t vid, uint16_t pid, uint16_t lorev, uint16_t hirev,
178    const char *str)
179{
180	struct libusb20_quirk q;
181	int error;
182
183	memset(&q, 0, sizeof(q));
184
185	q.vid = vid;
186	q.pid = pid;
187	q.bcdDeviceLow = lorev;
188	q.bcdDeviceHigh = hirev;
189	strlcpy(q.quirkname, str, sizeof(q.quirkname));
190
191	error = libusb20_be_add_dev_quirk(pbe, &q);
192	if (error) {
193		printf("Adding quirk '%s' failed, continuing.\n", str);
194	}
195	return;
196}
197
198static uint8_t
199get_token(const char *str, uint8_t narg)
200{
201	uint8_t n;
202
203	for (n = 0; n != (sizeof(token) / sizeof(token[0])); n++) {
204		if (strcasecmp(str, token[n].name) == 0) {
205			if (token[n].narg > narg) {
206				/* too few arguments */
207				break;
208			}
209			return (token[n].value);
210		}
211	}
212	return (0 - 1);
213}
214
215static uid_t
216num_id(const char *name, const char *type)
217{
218	uid_t val;
219	char *ep;
220
221	errno = 0;
222	val = strtoul(name, &ep, 0);
223	if (errno) {
224		err(1, "%s", name);
225	}
226	if (*ep != '\0') {
227		errx(1, "%s: illegal %s name", name, type);
228	}
229	return (val);
230}
231
232static int
233get_int(const char *s)
234{
235	int val;
236	char *ep;
237
238	errno = 0;
239	val = strtoul(s, &ep, 0);
240	if (errno) {
241		err(1, "%s", s);
242	}
243	if (*ep != '\0') {
244		errx(1, "illegal number: %s", s);
245	}
246	return val;
247}
248
249static void
250usage(void)
251{
252	printf(""
253	    "usbconfig - configure the USB subsystem" "\n"
254	    "usage: usbconfig -u <busnum> -a <devaddr> -i <ifaceindex> [cmds...]" "\n"
255	    "commands:" "\n"
256	    "  set_config <cfg_index>" "\n"
257	    "  set_alt <alt_index>" "\n"
258	    "  set_template <template>" "\n"
259	    "  get_template" "\n"
260	    "  add_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
261	    "  remove_dev_quirk_vplh <vid> <pid> <lo_rev> <hi_rev> <quirk>" "\n"
262	    "  dump_quirk_names" "\n"
263	    "  dump_device_quirks" "\n"
264	    "  dump_device_desc" "\n"
265	    "  dump_curr_config_desc" "\n"
266	    "  dump_all_config_desc" "\n"
267	    "  dump_string <index>" "\n"
268	    "  dump_info" "\n"
269	    "  show_ifdrv" "\n"
270	    "  suspend" "\n"
271	    "  resume" "\n"
272	    "  power_off" "\n"
273	    "  power_save" "\n"
274	    "  power_on" "\n"
275	    "  reset" "\n"
276	    "  list" "\n"
277	    "  do_request <bmReqTyp> <bReq> <wVal> <wIdx> <wLen> <data...>" "\n"
278	);
279	exit(1);
280}
281
282static void
283reset_options(struct options *opt)
284{
285	if (opt->buffer)
286		free(opt->buffer);
287	memset(opt, 0, sizeof(*opt));
288	return;
289}
290
291static void
292flush_command(struct libusb20_backend *pbe, struct options *opt)
293{
294	struct libusb20_device *pdev = NULL;
295	uint32_t matches = 0;
296	uint8_t dump_any;
297
298	/* check for invalid option combinations */
299	if ((opt->got_suspend +
300	    opt->got_resume +
301	    opt->got_reset +
302	    opt->got_set_config +
303	    opt->got_set_alt +
304	    opt->got_power_save +
305	    opt->got_power_on +
306	    opt->got_power_off) > 1) {
307		err(1, "can only specify one of 'set_config', "
308		    "'set_alt', 'reset', 'suspend', 'resume', "
309		    "'power_save', 'power_on' and 'power_off' "
310		    "at the same time!");
311	}
312	if (opt->got_dump_quirk_names) {
313		opt->got_any--;
314		dump_be_quirk_names(pbe);
315	}
316	if (opt->got_dump_device_quirks) {
317		opt->got_any--;
318		dump_be_dev_quirks(pbe);
319	}
320	if (opt->got_remove_device_quirk) {
321		opt->got_any--;
322		be_dev_remove_quirk(pbe,
323		    opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
324	}
325	if (opt->got_add_device_quirk) {
326		opt->got_any--;
327		be_dev_add_quirk(pbe,
328		    opt->vid, opt->pid, opt->lo_rev, opt->hi_rev, opt->quirkname);
329	}
330	if (opt->got_set_template) {
331		opt->got_any--;
332		if (libusb20_be_set_template(pbe, opt->template)) {
333			printf("Setting USB template %u failed, "
334			    "continuing.\n", opt->template);
335		}
336	}
337	if (opt->got_get_template) {
338		opt->got_any--;
339		if (libusb20_be_get_template(pbe, &opt->template))
340			printf("USB template: <unknown>\n");
341		else
342			printf("USB template: %u\n", opt->template);
343	}
344	if (opt->got_any == 0) {
345		/*
346		 * do not scan through all the devices if there are no valid
347		 * options
348		 */
349		goto done;
350	}
351	while ((pdev = libusb20_be_device_foreach(pbe, pdev))) {
352
353		if (opt->got_bus &&
354		    (libusb20_dev_get_bus_number(pdev) != opt->bus)) {
355			continue;
356		}
357		if (opt->got_addr &&
358		    (libusb20_dev_get_address(pdev) != opt->addr)) {
359			continue;
360		}
361		matches++;
362
363		if (libusb20_dev_open(pdev, 0)) {
364			err(1, "could not open device");
365		}
366		if (opt->got_dump_string) {
367			char *pbuf;
368
369			pbuf = malloc(256);
370			if (pbuf == NULL) {
371				err(1, "out of memory");
372			}
373			if (libusb20_dev_req_string_simple_sync(pdev,
374			    opt->string_index, pbuf, 256)) {
375				printf("STRING_0x%02x = <read error>\n",
376				    opt->string_index);
377			} else {
378				printf("STRING_0x%02x = <%s>\n",
379				    opt->string_index, pbuf);
380			}
381			free(pbuf);
382		}
383		if (opt->got_do_request) {
384			uint16_t actlen;
385			uint16_t t;
386
387			if (libusb20_dev_request_sync(pdev, &opt->setup,
388			    opt->buffer, &actlen, 5000 /* 5 seconds */ , 0)) {
389				printf("REQUEST = <ERROR>\n");
390			} else if (!(opt->setup.bmRequestType &
391			    LIBUSB20_ENDPOINT_IN)) {
392				printf("REQUEST = <OK>\n");
393			} else {
394				t = actlen;
395				printf("REQUEST = <");
396				for (t = 0; t != actlen; t++) {
397					printf("0x%02x%s",
398					    ((uint8_t *)opt->buffer)[t],
399					    (t == (actlen - 1)) ? "" : " ");
400				}
401				printf("><");
402				for (t = 0; t != actlen; t++) {
403					char c;
404
405					c = ((uint8_t *)opt->buffer)[t];
406					if ((c != '<') &&
407					    (c != '>') && isprint(c)) {
408						putchar(c);
409					}
410				}
411				printf(">\n");
412			}
413		}
414		if (opt->got_set_config) {
415			if (libusb20_dev_set_config_index(pdev,
416			    opt->config_index)) {
417				err(1, "could not set config index");
418			}
419		}
420		if (opt->got_set_alt) {
421			if (libusb20_dev_set_alt_index(pdev, opt->iface,
422			    opt->alt_index)) {
423				err(1, "could not set alternate setting");
424			}
425		}
426		if (opt->got_reset) {
427			if (libusb20_dev_reset(pdev)) {
428				err(1, "could not reset device");
429			}
430		}
431		if (opt->got_suspend) {
432			if (libusb20_dev_set_power_mode(pdev,
433			    LIBUSB20_POWER_SUSPEND)) {
434				err(1, "could not set suspend");
435			}
436		}
437		if (opt->got_resume) {
438			if (libusb20_dev_set_power_mode(pdev,
439			    LIBUSB20_POWER_RESUME)) {
440				err(1, "could not set resume");
441			}
442		}
443		if (opt->got_power_off) {
444			if (libusb20_dev_set_power_mode(pdev,
445			    LIBUSB20_POWER_OFF)) {
446				err(1, "could not set power OFF");
447			}
448		}
449		if (opt->got_power_save) {
450			if (libusb20_dev_set_power_mode(pdev,
451			    LIBUSB20_POWER_SAVE)) {
452				err(1, "could not set power SAVE");
453			}
454		}
455		if (opt->got_power_on) {
456			if (libusb20_dev_set_power_mode(pdev,
457			    LIBUSB20_POWER_ON)) {
458				err(1, "could not set power ON");
459			}
460		}
461		dump_any =
462		    (opt->got_dump_device_desc ||
463		    opt->got_dump_curr_config ||
464		    opt->got_dump_all_config ||
465		    opt->got_dump_info);
466
467		if (opt->got_list || dump_any) {
468			dump_device_info(pdev,
469			    opt->got_show_iface_driver);
470		}
471		if (opt->got_dump_device_desc) {
472			printf("\n");
473			dump_device_desc(pdev);
474		}
475		if (opt->got_dump_all_config) {
476			printf("\n");
477			dump_config(pdev, 1);
478		} else if (opt->got_dump_curr_config) {
479			printf("\n");
480			dump_config(pdev, 0);
481		}
482		if (dump_any) {
483			printf("\n");
484		}
485		if (libusb20_dev_close(pdev)) {
486			err(1, "could not close device");
487		}
488	}
489
490	if (matches == 0) {
491		printf("No device match or lack of permissions.\n");
492	}
493done:
494	reset_options(opt);
495
496	return;
497}
498
499int
500main(int argc, char **argv)
501{
502	struct libusb20_backend *pbe;
503	struct options *opt = &options;
504	char *cp;
505	int n;
506	int t;
507
508	if (argc < 1) {
509		usage();
510	}
511	pbe = libusb20_be_alloc_default();
512	if (pbe == NULL)
513		err(1, "could not access USB backend\n");
514
515	for (n = 1; n != argc; n++) {
516
517		/* get number of additional options */
518		t = (argc - n - 1);
519		if (t > 255)
520			t = 255;
521		switch (get_token(argv[n], t)) {
522		case T_ADD_DEVICE_QUIRK:
523			if (opt->got_add_device_quirk) {
524				flush_command(pbe, opt);
525			}
526			opt->vid = num_id(argv[n + 1], "Vendor ID");
527			opt->pid = num_id(argv[n + 2], "Product ID");
528			opt->lo_rev = num_id(argv[n + 3], "Low Revision");
529			opt->hi_rev = num_id(argv[n + 4], "High Revision");
530			opt->quirkname = argv[n + 5];
531			n += 5;
532
533			opt->got_add_device_quirk = 1;
534			opt->got_any++;
535			break;
536
537		case T_REMOVE_DEVICE_QUIRK:
538			if (opt->got_remove_device_quirk) {
539				flush_command(pbe, opt);
540			}
541			opt->vid = num_id(argv[n + 1], "Vendor ID");
542			opt->pid = num_id(argv[n + 2], "Product ID");
543			opt->lo_rev = num_id(argv[n + 3], "Low Revision");
544			opt->hi_rev = num_id(argv[n + 4], "High Revision");
545			opt->quirkname = argv[n + 5];
546			n += 5;
547			opt->got_remove_device_quirk = 1;
548			opt->got_any++;
549			break;
550
551		case T_DUMP_QUIRK_NAMES:
552			opt->got_dump_quirk_names = 1;
553			opt->got_any++;
554			break;
555
556		case T_DUMP_DEVICE_QUIRKS:
557			opt->got_dump_device_quirks = 1;
558			opt->got_any++;
559			break;
560
561		case T_SHOW_IFACE_DRIVER:
562			opt->got_show_iface_driver = 1;
563			break;
564
565		case T_UNIT:
566			if (opt->got_any) {
567				/* allow multiple commands on the same line */
568				flush_command(pbe, opt);
569			}
570			opt->bus = num_id(argv[n + 1], "busnum");
571			opt->got_bus = 1;
572			n++;
573			break;
574		case T_ADDR:
575			opt->addr = num_id(argv[n + 1], "addr");
576			opt->got_addr = 1;
577			n++;
578			break;
579		case T_IFACE:
580			opt->iface = num_id(argv[n + 1], "iface");
581			opt->got_iface = 1;
582			n++;
583			break;
584		case T_SET_CONFIG:
585			opt->config_index = num_id(argv[n + 1], "cfg_index");
586			opt->got_set_config = 1;
587			opt->got_any++;
588			n++;
589			break;
590		case T_SET_ALT:
591			opt->alt_index = num_id(argv[n + 1], "cfg_index");
592			opt->got_set_alt = 1;
593			opt->got_any++;
594			n++;
595			break;
596		case T_SET_TEMPLATE:
597			opt->template = get_int(argv[n + 1]);
598			opt->got_set_template = 1;
599			opt->got_any++;
600			n++;
601			break;
602		case T_GET_TEMPLATE:
603			opt->got_get_template = 1;
604			opt->got_any++;
605			break;
606		case T_DUMP_DEVICE_DESC:
607			opt->got_dump_device_desc = 1;
608			opt->got_any++;
609			break;
610		case T_DUMP_CURR_CONFIG_DESC:
611			opt->got_dump_curr_config = 1;
612			opt->got_any++;
613			break;
614		case T_DUMP_ALL_CONFIG_DESC:
615			opt->got_dump_all_config = 1;
616			opt->got_any++;
617			break;
618		case T_DUMP_INFO:
619			opt->got_dump_info = 1;
620			opt->got_any++;
621			break;
622		case T_DUMP_STRING:
623			if (opt->got_dump_string) {
624				flush_command(pbe, opt);
625			}
626			opt->string_index = num_id(argv[n + 1], "str_index");
627			opt->got_dump_string = 1;
628			opt->got_any++;
629			n++;
630			break;
631		case T_SUSPEND:
632			opt->got_suspend = 1;
633			opt->got_any++;
634			break;
635		case T_RESUME:
636			opt->got_resume = 1;
637			opt->got_any++;
638			break;
639		case T_POWER_OFF:
640			opt->got_power_off = 1;
641			opt->got_any++;
642			break;
643		case T_POWER_SAVE:
644			opt->got_power_save = 1;
645			opt->got_any++;
646			break;
647		case T_POWER_ON:
648			opt->got_power_on = 1;
649			opt->got_any++;
650			break;
651		case T_RESET:
652			opt->got_reset = 1;
653			opt->got_any++;
654			break;
655		case T_LIST:
656			opt->got_list = 1;
657			opt->got_any++;
658			break;
659		case T_DO_REQUEST:
660			if (opt->got_do_request) {
661				flush_command(pbe, opt);
662			}
663			LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &opt->setup);
664			opt->setup.bmRequestType = num_id(argv[n + 1], "bmReqTyp");
665			opt->setup.bRequest = num_id(argv[n + 2], "bReq");
666			opt->setup.wValue = num_id(argv[n + 3], "wVal");
667			opt->setup.wIndex = num_id(argv[n + 4], "wIndex");
668			opt->setup.wLength = num_id(argv[n + 5], "wLen");
669			if (opt->setup.wLength != 0) {
670				opt->buffer = malloc(opt->setup.wLength);
671			} else {
672				opt->buffer = NULL;
673			}
674
675			n += 5;
676
677			if (!(opt->setup.bmRequestType &
678			    LIBUSB20_ENDPOINT_IN)) {
679				/* copy in data */
680				t = (argc - n - 1);
681				if (t < opt->setup.wLength) {
682					err(1, "request data missing");
683				}
684				t = opt->setup.wLength;
685				while (t--) {
686					((uint8_t *)opt->buffer)[t] =
687					    num_id(argv[n + t + 1], "req_data");
688				}
689				n += opt->setup.wLength;
690			}
691			opt->got_do_request = 1;
692			opt->got_any++;
693			break;
694		default:
695			usage();
696			break;
697		}
698	}
699	if (opt->got_any) {
700		/* flush out last command */
701		flush_command(pbe, opt);
702	} else {
703		/* list all the devices */
704		opt->got_list = 1;
705		opt->got_any++;
706		flush_command(pbe, opt);
707	}
708	/* release data */
709	libusb20_be_free(pbe);
710
711	return (0);
712}
713