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