xref: /freebsd-src/share/examples/libusb20/control.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1*64de3fddSPedro F. Giffuni /*-
2*64de3fddSPedro F. Giffuni  * SPDX-License-Identifier: Beerware
3*64de3fddSPedro F. Giffuni  *
4*64de3fddSPedro F. Giffuni  * ----------------------------------------------------------------------------
53b9676efSJoerg Wunsch  * "THE BEER-WARE LICENSE" (Revision 42) (by Poul-Henning Kamp):
63b9676efSJoerg Wunsch  * <joerg@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
73b9676efSJoerg Wunsch  * can do whatever you want with this stuff. If we meet some day, and you think
83b9676efSJoerg Wunsch  * this stuff is worth it, you can buy me a beer in return.        Joerg Wunsch
93b9676efSJoerg Wunsch  * ----------------------------------------------------------------------------
103b9676efSJoerg Wunsch  */
113b9676efSJoerg Wunsch 
123b9676efSJoerg Wunsch /*
133b9676efSJoerg Wunsch  * Simple demo program to illustrate the handling of FreeBSD's
143b9676efSJoerg Wunsch  * libusb20.
153b9676efSJoerg Wunsch  */
163b9676efSJoerg Wunsch 
173b9676efSJoerg Wunsch /*
183b9676efSJoerg Wunsch  * Examples:
193b9676efSJoerg Wunsch  * Just list all VID:PID pairs
203b9676efSJoerg Wunsch  * ./control
213b9676efSJoerg Wunsch  *
223b9676efSJoerg Wunsch  * Standard device request GET_STATUS, report two bytes of status
233b9676efSJoerg Wunsch  * (bit 0 in the first byte returned is the "self powered" bit)
243b9676efSJoerg Wunsch  * ./control -v 0x3eb -p 0x2103 in std dev get_status 0 0 2
253b9676efSJoerg Wunsch  *
263b9676efSJoerg Wunsch  * Request input reports through the interrupt pipe from a mouse
273b9676efSJoerg Wunsch  * device (move the mouse around after issuing the command):
283b9676efSJoerg Wunsch  * ./control -v 0x093a -p 0x2516 -i 0x81
293b9676efSJoerg Wunsch  *
303b9676efSJoerg Wunsch  */
313b9676efSJoerg Wunsch 
323b9676efSJoerg Wunsch 
333b9676efSJoerg Wunsch #include <limits.h>
343b9676efSJoerg Wunsch #include <stdbool.h>
353b9676efSJoerg Wunsch #include <stdio.h>
363b9676efSJoerg Wunsch #include <stdint.h>
373b9676efSJoerg Wunsch #include <stdlib.h>
383b9676efSJoerg Wunsch #include <sysexits.h>
393b9676efSJoerg Wunsch #include <unistd.h>
402578c12eSHans Petter Selasky #include <string.h>
413b9676efSJoerg Wunsch 
423b9676efSJoerg Wunsch #include <libusb20.h>
433b9676efSJoerg Wunsch #include <libusb20_desc.h>
443b9676efSJoerg Wunsch 
453b9676efSJoerg Wunsch #include <sys/queue.h>
463b9676efSJoerg Wunsch 
472578c12eSHans Petter Selasky #include "util.h"
482578c12eSHans Petter Selasky 
493b9676efSJoerg Wunsch /*
503b9676efSJoerg Wunsch  * If you want to see the details of the internal datastructures
513b9676efSJoerg Wunsch  * in the debugger, unifdef the following.
523b9676efSJoerg Wunsch  */
533b9676efSJoerg Wunsch #ifdef DEBUG
543b9676efSJoerg Wunsch #  include "/usr/src/lib/libusb/libusb20_int.h"
553b9676efSJoerg Wunsch #endif
563b9676efSJoerg Wunsch 
573b9676efSJoerg Wunsch #define BUFLEN 64
583b9676efSJoerg Wunsch 
593b9676efSJoerg Wunsch #define TIMEOUT 5000 		/* 5 s */
603b9676efSJoerg Wunsch 
613b9676efSJoerg Wunsch int intr_ep;		/* endpoints */
623b9676efSJoerg Wunsch struct LIBUSB20_CONTROL_SETUP_DECODED setup;
633b9676efSJoerg Wunsch 
643b9676efSJoerg Wunsch uint8_t out_buf[BUFLEN];
653b9676efSJoerg Wunsch uint16_t out_len;
663b9676efSJoerg Wunsch 
673b9676efSJoerg Wunsch bool do_request;
683b9676efSJoerg Wunsch 
693b9676efSJoerg Wunsch static void
doit(struct libusb20_device * dev)703b9676efSJoerg Wunsch doit(struct libusb20_device *dev)
713b9676efSJoerg Wunsch {
723b9676efSJoerg Wunsch   int rv;
733b9676efSJoerg Wunsch 
743b9676efSJoerg Wunsch   if (do_request)
753b9676efSJoerg Wunsch     printf("doit(): bmRequestType 0x%02x, bRequest 0x%02x, wValue 0x%04x, wIndex 0x%04x, wLength 0x%04x\n",
763b9676efSJoerg Wunsch 	   setup.bmRequestType,
773b9676efSJoerg Wunsch 	   setup.bRequest,
783b9676efSJoerg Wunsch 	   setup.wValue,
793b9676efSJoerg Wunsch 	   setup.wIndex,
803b9676efSJoerg Wunsch 	   setup.wLength);
813b9676efSJoerg Wunsch 
823b9676efSJoerg Wunsch   /*
833b9676efSJoerg Wunsch    * Open the device, allocating memory for two possible (bulk or
843b9676efSJoerg Wunsch    * interrupt) transfers.
853b9676efSJoerg Wunsch    *
863b9676efSJoerg Wunsch    * If only control transfers are intended (via
873b9676efSJoerg Wunsch    * libusb20_dev_request_sync()), transfer_max can be given as 0.
883b9676efSJoerg Wunsch    */
893b9676efSJoerg Wunsch   if ((rv = libusb20_dev_open(dev, 1)) != 0)
903b9676efSJoerg Wunsch     {
912578c12eSHans Petter Selasky       fprintf(stderr, "libusb20_dev_open: %s\n", libusb20_strerror(rv));
923b9676efSJoerg Wunsch       return;
933b9676efSJoerg Wunsch     }
943b9676efSJoerg Wunsch 
953b9676efSJoerg Wunsch   /*
963b9676efSJoerg Wunsch    * If the device has more than one configuration, select the desired
973b9676efSJoerg Wunsch    * one here.
983b9676efSJoerg Wunsch    */
993b9676efSJoerg Wunsch   if ((rv = libusb20_dev_set_config_index(dev, 0)) != 0)
1003b9676efSJoerg Wunsch     {
1012578c12eSHans Petter Selasky       fprintf(stderr, "libusb20_dev_set_config_index: %s\n", libusb20_strerror(rv));
1023b9676efSJoerg Wunsch       return;
1033b9676efSJoerg Wunsch     }
1043b9676efSJoerg Wunsch 
1053b9676efSJoerg Wunsch   uint8_t *data = 0;
1063b9676efSJoerg Wunsch   uint16_t actlen;
1073b9676efSJoerg Wunsch 
1083b9676efSJoerg Wunsch   if ((setup.bmRequestType & 0x80) != 0)
1093b9676efSJoerg Wunsch     {
1103b9676efSJoerg Wunsch       /* this is an IN request, allocate a buffer */
1113b9676efSJoerg Wunsch       data = malloc(setup.wLength);
1123b9676efSJoerg Wunsch       if (data == 0)
1133b9676efSJoerg Wunsch 	{
1143b9676efSJoerg Wunsch 	  fprintf(stderr,
1153b9676efSJoerg Wunsch 		  "Out of memory allocating %u bytes of reply buffer\n",
1163b9676efSJoerg Wunsch 		  setup.wLength);
1173b9676efSJoerg Wunsch 	  return;
1183b9676efSJoerg Wunsch 	}
1193b9676efSJoerg Wunsch     }
1203b9676efSJoerg Wunsch   else
1213b9676efSJoerg Wunsch     data = out_buf;
1223b9676efSJoerg Wunsch 
1233b9676efSJoerg Wunsch   if (do_request)
1243b9676efSJoerg Wunsch     {
1253b9676efSJoerg Wunsch       if ((rv = libusb20_dev_request_sync(dev, &setup, data,
1263b9676efSJoerg Wunsch 					  &actlen,
1273b9676efSJoerg Wunsch 					  TIMEOUT,
1283b9676efSJoerg Wunsch 					  0 /* flags */)) != 0)
1293b9676efSJoerg Wunsch 	{
1303b9676efSJoerg Wunsch 	  fprintf(stderr,
1312578c12eSHans Petter Selasky 		  "libusb20_dev_request_sync: %s\n", libusb20_strerror(rv));
1323b9676efSJoerg Wunsch 	}
1333b9676efSJoerg Wunsch       printf("sent %d bytes\n", actlen);
1343b9676efSJoerg Wunsch       if ((setup.bmRequestType & 0x80) != 0)
1353b9676efSJoerg Wunsch 	{
1363b9676efSJoerg Wunsch 	  print_formatted(data, (uint32_t)setup.wLength);
1373b9676efSJoerg Wunsch 	  free(data);
1383b9676efSJoerg Wunsch 	}
1393b9676efSJoerg Wunsch     }
1403b9676efSJoerg Wunsch 
1413b9676efSJoerg Wunsch   if (intr_ep != 0)
1423b9676efSJoerg Wunsch     {
1433b9676efSJoerg Wunsch       /*
1443b9676efSJoerg Wunsch        * One transfer has been requested in libusb20_dev_open() above;
1453b9676efSJoerg Wunsch        * obtain the corresponding transfer struct pointer.
1463b9676efSJoerg Wunsch        */
1473b9676efSJoerg Wunsch       struct libusb20_transfer *xfr_intr = libusb20_tr_get_pointer(dev, 0);
1483b9676efSJoerg Wunsch 
1493b9676efSJoerg Wunsch       if (xfr_intr == NULL)
1503b9676efSJoerg Wunsch 	{
1512578c12eSHans Petter Selasky 	  fprintf(stderr, "libusb20_tr_get_pointer: %s\n", libusb20_strerror(rv));
1523b9676efSJoerg Wunsch 	  return;
1533b9676efSJoerg Wunsch 	}
1543b9676efSJoerg Wunsch 
1553b9676efSJoerg Wunsch       /*
1563b9676efSJoerg Wunsch        * Open the interrupt transfer.
1573b9676efSJoerg Wunsch        */
1583b9676efSJoerg Wunsch       if ((rv = libusb20_tr_open(xfr_intr, 0, 1, intr_ep)) != 0)
1593b9676efSJoerg Wunsch 	{
1602578c12eSHans Petter Selasky 	  fprintf(stderr, "libusb20_tr_open: %s\n", libusb20_strerror(rv));
1613b9676efSJoerg Wunsch 	  return;
1623b9676efSJoerg Wunsch 	}
1633b9676efSJoerg Wunsch 
1643b9676efSJoerg Wunsch       uint8_t in_buf[BUFLEN];
1653b9676efSJoerg Wunsch       uint32_t rlen;
1663b9676efSJoerg Wunsch 
1673b9676efSJoerg Wunsch       if ((rv = libusb20_tr_bulk_intr_sync(xfr_intr, in_buf, BUFLEN, &rlen, TIMEOUT))
1683b9676efSJoerg Wunsch 	  != 0)
1693b9676efSJoerg Wunsch 	{
1702578c12eSHans Petter Selasky 	  fprintf(stderr, "libusb20_tr_bulk_intr_sync: %s\n", libusb20_strerror(rv));
1713b9676efSJoerg Wunsch 	}
1723b9676efSJoerg Wunsch       printf("received %d bytes\n", rlen);
1733b9676efSJoerg Wunsch       if (rlen > 0)
1743b9676efSJoerg Wunsch 	print_formatted(in_buf, rlen);
1753b9676efSJoerg Wunsch 
1763b9676efSJoerg Wunsch       libusb20_tr_close(xfr_intr);
1773b9676efSJoerg Wunsch     }
1783b9676efSJoerg Wunsch 
1793b9676efSJoerg Wunsch   libusb20_dev_close(dev);
1803b9676efSJoerg Wunsch }
1813b9676efSJoerg Wunsch 
1823b9676efSJoerg Wunsch static void
usage(void)1833b9676efSJoerg Wunsch usage(void)
1843b9676efSJoerg Wunsch {
1853b9676efSJoerg Wunsch   fprintf(stderr,
1863b9676efSJoerg Wunsch 	  "Usage ./usb [-i <INTR_EP>] -v <VID> -p <PID> [dir type rcpt req wValue wIndex wLength [<outdata> ...]]\n");
1873b9676efSJoerg Wunsch   exit(EX_USAGE);
1883b9676efSJoerg Wunsch }
1893b9676efSJoerg Wunsch 
1903b9676efSJoerg Wunsch static const char *reqnames[] =
1913b9676efSJoerg Wunsch {
1923b9676efSJoerg Wunsch   "get_status",
1933b9676efSJoerg Wunsch   "clear_feature",
1943b9676efSJoerg Wunsch   "res1",
1953b9676efSJoerg Wunsch   "set_feature",
1963b9676efSJoerg Wunsch   "res2",
1973b9676efSJoerg Wunsch   "set_address",
1983b9676efSJoerg Wunsch   "get_descriptor",
1993b9676efSJoerg Wunsch   "set_descriptor",
2003b9676efSJoerg Wunsch   "get_configuration",
2013b9676efSJoerg Wunsch   "set_configuration",
2023b9676efSJoerg Wunsch   "get_interface",
2033b9676efSJoerg Wunsch   "set_interface",
2043b9676efSJoerg Wunsch   "synch_frame",
2053b9676efSJoerg Wunsch };
2063b9676efSJoerg Wunsch 
2073b9676efSJoerg Wunsch static int
get_req(const char * reqname)2083b9676efSJoerg Wunsch get_req(const char *reqname)
2093b9676efSJoerg Wunsch {
2103b9676efSJoerg Wunsch   size_t i;
2113b9676efSJoerg Wunsch   size_t l = strlen(reqname);
2123b9676efSJoerg Wunsch 
2133b9676efSJoerg Wunsch   for (i = 0;
2143b9676efSJoerg Wunsch        i < sizeof reqnames / sizeof reqnames[0];
2153b9676efSJoerg Wunsch        i++)
2163b9676efSJoerg Wunsch     if (strncasecmp(reqname, reqnames[i], l) == 0)
2173b9676efSJoerg Wunsch       return i;
2183b9676efSJoerg Wunsch 
2193b9676efSJoerg Wunsch   return strtoul(reqname, 0, 0);
2203b9676efSJoerg Wunsch }
2213b9676efSJoerg Wunsch 
2223b9676efSJoerg Wunsch 
2233b9676efSJoerg Wunsch static int
parse_req(int argc,char ** argv)2243b9676efSJoerg Wunsch parse_req(int argc, char **argv)
2253b9676efSJoerg Wunsch {
2263b9676efSJoerg Wunsch   int idx;
2273b9676efSJoerg Wunsch   uint8_t rt = 0;
2283b9676efSJoerg Wunsch 
2293b9676efSJoerg Wunsch   for (idx = 0; argc != 0 && idx <= 6; argc--, idx++)
2303b9676efSJoerg Wunsch     switch (idx)
2313b9676efSJoerg Wunsch       {
2323b9676efSJoerg Wunsch       case 0:
2333b9676efSJoerg Wunsch 	/* dir[ection]: i[n] | o[ut] */
2343b9676efSJoerg Wunsch 	if (*argv[idx] == 'i')
2353b9676efSJoerg Wunsch 	  rt |= 0x80;
2363b9676efSJoerg Wunsch 	else if (*argv[idx] == 'o')
2373b9676efSJoerg Wunsch 	  /* nop */;
2383b9676efSJoerg Wunsch 	else
2393b9676efSJoerg Wunsch 	  {
2403b9676efSJoerg Wunsch 	    fprintf(stderr, "request direction must be \"in\" or \"out\" (got %s)\n",
2413b9676efSJoerg Wunsch 		    argv[idx]);
2423b9676efSJoerg Wunsch 	    return -1;
2433b9676efSJoerg Wunsch 	  }
2443b9676efSJoerg Wunsch 	break;
2453b9676efSJoerg Wunsch 
2463b9676efSJoerg Wunsch       case 1:
2473b9676efSJoerg Wunsch 	/* type: s[tandard] | c[lass] | v[endor] */
2483b9676efSJoerg Wunsch 	if (*argv[idx] == 's')
2493b9676efSJoerg Wunsch 	  /* nop */;
2503b9676efSJoerg Wunsch 	else if (*argv[idx] == 'c')
2513b9676efSJoerg Wunsch 	  rt |= 0x20;
2523b9676efSJoerg Wunsch 	else if (*argv[idx] == 'v')
2533b9676efSJoerg Wunsch 	  rt |= 0x40;
2543b9676efSJoerg Wunsch 	else
2553b9676efSJoerg Wunsch 	  {
2563b9676efSJoerg Wunsch 	    fprintf(stderr,
2573b9676efSJoerg Wunsch 		    "request type must be one of \"standard\", \"class\", or \"vendor\" (got %s)\n",
2583b9676efSJoerg Wunsch 		    argv[idx]);
2593b9676efSJoerg Wunsch 	    return -1;
2603b9676efSJoerg Wunsch 	  }
2613b9676efSJoerg Wunsch 	break;
2623b9676efSJoerg Wunsch 
2633b9676efSJoerg Wunsch       case 2:
2643b9676efSJoerg Wunsch 	/* rcpt: d[evice], i[nterface], e[ndpoint], o[ther] */
2653b9676efSJoerg Wunsch 	if (*argv[idx] == 'd')
2663b9676efSJoerg Wunsch 	  /* nop */;
2673b9676efSJoerg Wunsch 	else if (*argv[idx] == 'i')
2683b9676efSJoerg Wunsch 	  rt |= 1;
2693b9676efSJoerg Wunsch 	else if (*argv[idx] == 'e')
2703b9676efSJoerg Wunsch 	  rt |= 2;
2713b9676efSJoerg Wunsch 	else if (*argv[idx] == 'o')
2723b9676efSJoerg Wunsch 	  rt |= 3;
2733b9676efSJoerg Wunsch 	else
2743b9676efSJoerg Wunsch 	  {
2753b9676efSJoerg Wunsch 	    fprintf(stderr,
2763b9676efSJoerg Wunsch 		    "recipient must be one of \"device\", \"interface\", \"endpoint\", or \"other\" (got %s)\n",
2773b9676efSJoerg Wunsch 		    argv[idx]);
2783b9676efSJoerg Wunsch 	    return -1;
2793b9676efSJoerg Wunsch 	  }
2803b9676efSJoerg Wunsch 	setup.bmRequestType = rt;
2813b9676efSJoerg Wunsch 	break;
2823b9676efSJoerg Wunsch 
2833b9676efSJoerg Wunsch       case 3:
2843b9676efSJoerg Wunsch 	setup.bRequest = get_req(argv[idx]);
2853b9676efSJoerg Wunsch 	break;
2863b9676efSJoerg Wunsch 
2873b9676efSJoerg Wunsch       case 4:
2883b9676efSJoerg Wunsch 	setup.wValue = strtoul(argv[idx], 0, 0);
2893b9676efSJoerg Wunsch 	break;
2903b9676efSJoerg Wunsch 
2913b9676efSJoerg Wunsch       case 5:
2923b9676efSJoerg Wunsch 	setup.wIndex = strtoul(argv[idx], 0, 0);
2933b9676efSJoerg Wunsch 	break;
2943b9676efSJoerg Wunsch 
2953b9676efSJoerg Wunsch       case 6:
2963b9676efSJoerg Wunsch 	setup.wLength = strtoul(argv[idx], 0, 0);
2973b9676efSJoerg Wunsch 	break;
2983b9676efSJoerg Wunsch       }
2993b9676efSJoerg Wunsch 
3003b9676efSJoerg Wunsch   return argc;
3013b9676efSJoerg Wunsch }
3023b9676efSJoerg Wunsch 
3033b9676efSJoerg Wunsch 
3043b9676efSJoerg Wunsch int
main(int argc,char ** argv)3053b9676efSJoerg Wunsch main(int argc, char **argv)
3063b9676efSJoerg Wunsch {
3073b9676efSJoerg Wunsch   unsigned int vid = UINT_MAX, pid = UINT_MAX; /* impossible VID:PID */
3083b9676efSJoerg Wunsch   int c;
3093b9676efSJoerg Wunsch 
3103b9676efSJoerg Wunsch   /*
3113b9676efSJoerg Wunsch    * Initialize setup struct.  This step is required, and initializes
3123b9676efSJoerg Wunsch    * internal fields in the struct.
3133b9676efSJoerg Wunsch    *
3143b9676efSJoerg Wunsch    * All the "public" fields are named exactly the way as the USB
3153b9676efSJoerg Wunsch    * standard describes them, namely:
3163b9676efSJoerg Wunsch    *
3173b9676efSJoerg Wunsch    *	setup.bmRequestType: bitmask, bit 7 is direction
3183b9676efSJoerg Wunsch    *	                              bits 6/5 is request type
3193b9676efSJoerg Wunsch    *	                                       (standard, class, vendor)
3203b9676efSJoerg Wunsch    *	                              bits 4..0 is recipient
3213b9676efSJoerg Wunsch    *	                                       (device, interface, endpoint,
3223b9676efSJoerg Wunsch    *	                                        other)
3233b9676efSJoerg Wunsch    *	setup.bRequest:      the request itself (see get_req() for standard
3243b9676efSJoerg Wunsch    *	                                         requests, or specific value)
3253b9676efSJoerg Wunsch    *	setup.wValue:        a 16-bit value
3263b9676efSJoerg Wunsch    *	setup.wIndex:        another 16-bit value
3273b9676efSJoerg Wunsch    *	setup.wLength:       length of associated data transfer, direction
3283b9676efSJoerg Wunsch    *	                     depends on bit 7 of bmRequestType
3293b9676efSJoerg Wunsch    */
3303b9676efSJoerg Wunsch   LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &setup);
3313b9676efSJoerg Wunsch 
3323b9676efSJoerg Wunsch   while ((c = getopt(argc, argv, "i:p:v:")) != -1)
3333b9676efSJoerg Wunsch     switch (c)
3343b9676efSJoerg Wunsch       {
3353b9676efSJoerg Wunsch       case 'i':
3363b9676efSJoerg Wunsch 	intr_ep = strtol(optarg, NULL, 0);
3373b9676efSJoerg Wunsch 	break;
3383b9676efSJoerg Wunsch 
3393b9676efSJoerg Wunsch       case 'p':
3403b9676efSJoerg Wunsch 	pid = strtol(optarg, NULL, 0);
3413b9676efSJoerg Wunsch 	break;
3423b9676efSJoerg Wunsch 
3433b9676efSJoerg Wunsch       case 'v':
3443b9676efSJoerg Wunsch 	vid = strtol(optarg, NULL, 0);
3453b9676efSJoerg Wunsch 	break;
3463b9676efSJoerg Wunsch 
3473b9676efSJoerg Wunsch       default:
3483b9676efSJoerg Wunsch 	usage();
3493b9676efSJoerg Wunsch 	break;
3503b9676efSJoerg Wunsch       }
3513b9676efSJoerg Wunsch   argc -= optind;
3523b9676efSJoerg Wunsch   argv += optind;
3533b9676efSJoerg Wunsch 
3543b9676efSJoerg Wunsch   if (vid != UINT_MAX || pid != UINT_MAX)
3553b9676efSJoerg Wunsch     {
3563b9676efSJoerg Wunsch       if (intr_ep != 0 && (intr_ep & 0x80) == 0)
3573b9676efSJoerg Wunsch 	{
3583b9676efSJoerg Wunsch 	  fprintf(stderr, "Interrupt endpoint must be of type IN\n");
3593b9676efSJoerg Wunsch 	  usage();
3603b9676efSJoerg Wunsch 	}
3613b9676efSJoerg Wunsch 
3623b9676efSJoerg Wunsch       if (argc > 0)
3633b9676efSJoerg Wunsch 	{
3643b9676efSJoerg Wunsch 	  do_request = true;
3653b9676efSJoerg Wunsch 
3663b9676efSJoerg Wunsch 	  int rv = parse_req(argc, argv);
3673b9676efSJoerg Wunsch 	  if (rv < 0)
3683b9676efSJoerg Wunsch 	    return EX_USAGE;
3693b9676efSJoerg Wunsch 	  argc = rv;
3703b9676efSJoerg Wunsch 
3713b9676efSJoerg Wunsch 	  if (argc > 0)
3723b9676efSJoerg Wunsch 	    {
3733b9676efSJoerg Wunsch 	      for (out_len = 0; argc > 0 && out_len < BUFLEN; out_len++, argc--)
3743b9676efSJoerg Wunsch 		{
3753b9676efSJoerg Wunsch 		  unsigned n = strtoul(argv[out_len], 0, 0);
3763b9676efSJoerg Wunsch 		  if (n > 255)
3773b9676efSJoerg Wunsch 		    fprintf(stderr,
3783b9676efSJoerg Wunsch 			    "Warning: data #%d 0x%0x > 0xff, truncating\n",
3793b9676efSJoerg Wunsch 			    out_len, n);
3803b9676efSJoerg Wunsch 		  out_buf[out_len] = (uint8_t)n;
3813b9676efSJoerg Wunsch 		}
3823b9676efSJoerg Wunsch 	      out_len++;
3833b9676efSJoerg Wunsch 	      if (argc > 0)
3843b9676efSJoerg Wunsch 		fprintf(stderr,
3853b9676efSJoerg Wunsch 			"Data count exceeds maximum of %d, ignoring %d elements\n",
3863b9676efSJoerg Wunsch 			BUFLEN, optind);
3873b9676efSJoerg Wunsch 	    }
3883b9676efSJoerg Wunsch 	}
3893b9676efSJoerg Wunsch     }
3903b9676efSJoerg Wunsch 
3913b9676efSJoerg Wunsch   struct libusb20_backend *be;
3923b9676efSJoerg Wunsch   struct libusb20_device *dev;
3933b9676efSJoerg Wunsch 
3943b9676efSJoerg Wunsch   if ((be = libusb20_be_alloc_default()) == NULL)
3953b9676efSJoerg Wunsch     {
3963b9676efSJoerg Wunsch       perror("libusb20_be_alloc()");
3973b9676efSJoerg Wunsch       return 1;
3983b9676efSJoerg Wunsch     }
3993b9676efSJoerg Wunsch 
4003b9676efSJoerg Wunsch   dev = NULL;
4013b9676efSJoerg Wunsch   while ((dev = libusb20_be_device_foreach(be, dev)) != NULL)
4023b9676efSJoerg Wunsch     {
4033b9676efSJoerg Wunsch       struct LIBUSB20_DEVICE_DESC_DECODED *ddp =
4043b9676efSJoerg Wunsch       libusb20_dev_get_device_desc(dev);
4053b9676efSJoerg Wunsch 
4063b9676efSJoerg Wunsch       printf("Found device %s (VID:PID = 0x%04x:0x%04x)\n",
4073b9676efSJoerg Wunsch 	     libusb20_dev_get_desc(dev),
4083b9676efSJoerg Wunsch 	     ddp->idVendor, ddp->idProduct);
4093b9676efSJoerg Wunsch 
4103b9676efSJoerg Wunsch       if (ddp->idVendor == vid && ddp->idProduct == pid)
4113b9676efSJoerg Wunsch 	doit(dev);
4123b9676efSJoerg Wunsch     }
4133b9676efSJoerg Wunsch 
4143b9676efSJoerg Wunsch   libusb20_be_free(be);
4153b9676efSJoerg Wunsch   return 0;
4163b9676efSJoerg Wunsch }
417