xref: /freebsd/share/examples/libusb20/control.c (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
1 /*-
2  * SPDX-License-Identifier: Beerware
3  *
4  * ----------------------------------------------------------------------------
5  * "THE BEER-WARE LICENSE" (Revision 42) (by Poul-Henning Kamp):
6  * <joerg@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
7  * can do whatever you want with this stuff. If we meet some day, and you think
8  * this stuff is worth it, you can buy me a beer in return.        Joerg Wunsch
9  * ----------------------------------------------------------------------------
10  *
11  * $FreeBSD$
12  */
13 
14 /*
15  * Simple demo program to illustrate the handling of FreeBSD's
16  * libusb20.
17  */
18 
19 /*
20  * Examples:
21  * Just list all VID:PID pairs
22  * ./control
23  *
24  * Standard device request GET_STATUS, report two bytes of status
25  * (bit 0 in the first byte returned is the "self powered" bit)
26  * ./control -v 0x3eb -p 0x2103 in std dev get_status 0 0 2
27  *
28  * Request input reports through the interrupt pipe from a mouse
29  * device (move the mouse around after issuing the command):
30  * ./control -v 0x093a -p 0x2516 -i 0x81
31  *
32  */
33 
34 
35 #include <limits.h>
36 #include <stdbool.h>
37 #include <stdio.h>
38 #include <stdint.h>
39 #include <stdlib.h>
40 #include <sysexits.h>
41 #include <unistd.h>
42 #include <string.h>
43 
44 #include <libusb20.h>
45 #include <libusb20_desc.h>
46 
47 #include <sys/queue.h>
48 
49 #include "util.h"
50 
51 /*
52  * If you want to see the details of the internal datastructures
53  * in the debugger, unifdef the following.
54  */
55 #ifdef DEBUG
56 #  include "/usr/src/lib/libusb/libusb20_int.h"
57 #endif
58 
59 #define BUFLEN 64
60 
61 #define TIMEOUT 5000 		/* 5 s */
62 
63 int intr_ep;		/* endpoints */
64 struct LIBUSB20_CONTROL_SETUP_DECODED setup;
65 
66 uint8_t out_buf[BUFLEN];
67 uint16_t out_len;
68 
69 bool do_request;
70 
71 static void
72 doit(struct libusb20_device *dev)
73 {
74   int rv;
75 
76   if (do_request)
77     printf("doit(): bmRequestType 0x%02x, bRequest 0x%02x, wValue 0x%04x, wIndex 0x%04x, wLength 0x%04x\n",
78 	   setup.bmRequestType,
79 	   setup.bRequest,
80 	   setup.wValue,
81 	   setup.wIndex,
82 	   setup.wLength);
83 
84   /*
85    * Open the device, allocating memory for two possible (bulk or
86    * interrupt) transfers.
87    *
88    * If only control transfers are intended (via
89    * libusb20_dev_request_sync()), transfer_max can be given as 0.
90    */
91   if ((rv = libusb20_dev_open(dev, 1)) != 0)
92     {
93       fprintf(stderr, "libusb20_dev_open: %s\n", libusb20_strerror(rv));
94       return;
95     }
96 
97   /*
98    * If the device has more than one configuration, select the desired
99    * one here.
100    */
101   if ((rv = libusb20_dev_set_config_index(dev, 0)) != 0)
102     {
103       fprintf(stderr, "libusb20_dev_set_config_index: %s\n", libusb20_strerror(rv));
104       return;
105     }
106 
107   uint8_t *data = 0;
108   uint16_t actlen;
109 
110   if ((setup.bmRequestType & 0x80) != 0)
111     {
112       /* this is an IN request, allocate a buffer */
113       data = malloc(setup.wLength);
114       if (data == 0)
115 	{
116 	  fprintf(stderr,
117 		  "Out of memory allocating %u bytes of reply buffer\n",
118 		  setup.wLength);
119 	  return;
120 	}
121     }
122   else
123     data = out_buf;
124 
125   if (do_request)
126     {
127       if ((rv = libusb20_dev_request_sync(dev, &setup, data,
128 					  &actlen,
129 					  TIMEOUT,
130 					  0 /* flags */)) != 0)
131 	{
132 	  fprintf(stderr,
133 		  "libusb20_dev_request_sync: %s\n", libusb20_strerror(rv));
134 	}
135       printf("sent %d bytes\n", actlen);
136       if ((setup.bmRequestType & 0x80) != 0)
137 	{
138 	  print_formatted(data, (uint32_t)setup.wLength);
139 	  free(data);
140 	}
141     }
142 
143   if (intr_ep != 0)
144     {
145       /*
146        * One transfer has been requested in libusb20_dev_open() above;
147        * obtain the corresponding transfer struct pointer.
148        */
149       struct libusb20_transfer *xfr_intr = libusb20_tr_get_pointer(dev, 0);
150 
151       if (xfr_intr == NULL)
152 	{
153 	  fprintf(stderr, "libusb20_tr_get_pointer: %s\n", libusb20_strerror(rv));
154 	  return;
155 	}
156 
157       /*
158        * Open the interrupt transfer.
159        */
160       if ((rv = libusb20_tr_open(xfr_intr, 0, 1, intr_ep)) != 0)
161 	{
162 	  fprintf(stderr, "libusb20_tr_open: %s\n", libusb20_strerror(rv));
163 	  return;
164 	}
165 
166       uint8_t in_buf[BUFLEN];
167       uint32_t rlen;
168 
169       if ((rv = libusb20_tr_bulk_intr_sync(xfr_intr, in_buf, BUFLEN, &rlen, TIMEOUT))
170 	  != 0)
171 	{
172 	  fprintf(stderr, "libusb20_tr_bulk_intr_sync: %s\n", libusb20_strerror(rv));
173 	}
174       printf("received %d bytes\n", rlen);
175       if (rlen > 0)
176 	print_formatted(in_buf, rlen);
177 
178       libusb20_tr_close(xfr_intr);
179     }
180 
181   libusb20_dev_close(dev);
182 }
183 
184 static void
185 usage(void)
186 {
187   fprintf(stderr,
188 	  "Usage ./usb [-i <INTR_EP>] -v <VID> -p <PID> [dir type rcpt req wValue wIndex wLength [<outdata> ...]]\n");
189   exit(EX_USAGE);
190 }
191 
192 static const char *reqnames[] =
193 {
194   "get_status",
195   "clear_feature",
196   "res1",
197   "set_feature",
198   "res2",
199   "set_address",
200   "get_descriptor",
201   "set_descriptor",
202   "get_configuration",
203   "set_configuration",
204   "get_interface",
205   "set_interface",
206   "synch_frame",
207 };
208 
209 static int
210 get_req(const char *reqname)
211 {
212   size_t i;
213   size_t l = strlen(reqname);
214 
215   for (i = 0;
216        i < sizeof reqnames / sizeof reqnames[0];
217        i++)
218     if (strncasecmp(reqname, reqnames[i], l) == 0)
219       return i;
220 
221   return strtoul(reqname, 0, 0);
222 }
223 
224 
225 static int
226 parse_req(int argc, char **argv)
227 {
228   int idx;
229   uint8_t rt = 0;
230 
231   for (idx = 0; argc != 0 && idx <= 6; argc--, idx++)
232     switch (idx)
233       {
234       case 0:
235 	/* dir[ection]: i[n] | o[ut] */
236 	if (*argv[idx] == 'i')
237 	  rt |= 0x80;
238 	else if (*argv[idx] == 'o')
239 	  /* nop */;
240 	else
241 	  {
242 	    fprintf(stderr, "request direction must be \"in\" or \"out\" (got %s)\n",
243 		    argv[idx]);
244 	    return -1;
245 	  }
246 	break;
247 
248       case 1:
249 	/* type: s[tandard] | c[lass] | v[endor] */
250 	if (*argv[idx] == 's')
251 	  /* nop */;
252 	else if (*argv[idx] == 'c')
253 	  rt |= 0x20;
254 	else if (*argv[idx] == 'v')
255 	  rt |= 0x40;
256 	else
257 	  {
258 	    fprintf(stderr,
259 		    "request type must be one of \"standard\", \"class\", or \"vendor\" (got %s)\n",
260 		    argv[idx]);
261 	    return -1;
262 	  }
263 	break;
264 
265       case 2:
266 	/* rcpt: d[evice], i[nterface], e[ndpoint], o[ther] */
267 	if (*argv[idx] == 'd')
268 	  /* nop */;
269 	else if (*argv[idx] == 'i')
270 	  rt |= 1;
271 	else if (*argv[idx] == 'e')
272 	  rt |= 2;
273 	else if (*argv[idx] == 'o')
274 	  rt |= 3;
275 	else
276 	  {
277 	    fprintf(stderr,
278 		    "recipient must be one of \"device\", \"interface\", \"endpoint\", or \"other\" (got %s)\n",
279 		    argv[idx]);
280 	    return -1;
281 	  }
282 	setup.bmRequestType = rt;
283 	break;
284 
285       case 3:
286 	setup.bRequest = get_req(argv[idx]);
287 	break;
288 
289       case 4:
290 	setup.wValue = strtoul(argv[idx], 0, 0);
291 	break;
292 
293       case 5:
294 	setup.wIndex = strtoul(argv[idx], 0, 0);
295 	break;
296 
297       case 6:
298 	setup.wLength = strtoul(argv[idx], 0, 0);
299 	break;
300       }
301 
302   return argc;
303 }
304 
305 
306 int
307 main(int argc, char **argv)
308 {
309   unsigned int vid = UINT_MAX, pid = UINT_MAX; /* impossible VID:PID */
310   int c;
311 
312   /*
313    * Initialize setup struct.  This step is required, and initializes
314    * internal fields in the struct.
315    *
316    * All the "public" fields are named exactly the way as the USB
317    * standard describes them, namely:
318    *
319    *	setup.bmRequestType: bitmask, bit 7 is direction
320    *	                              bits 6/5 is request type
321    *	                                       (standard, class, vendor)
322    *	                              bits 4..0 is recipient
323    *	                                       (device, interface, endpoint,
324    *	                                        other)
325    *	setup.bRequest:      the request itself (see get_req() for standard
326    *	                                         requests, or specific value)
327    *	setup.wValue:        a 16-bit value
328    *	setup.wIndex:        another 16-bit value
329    *	setup.wLength:       length of associated data transfer, direction
330    *	                     depends on bit 7 of bmRequestType
331    */
332   LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &setup);
333 
334   while ((c = getopt(argc, argv, "i:p:v:")) != -1)
335     switch (c)
336       {
337       case 'i':
338 	intr_ep = strtol(optarg, NULL, 0);
339 	break;
340 
341       case 'p':
342 	pid = strtol(optarg, NULL, 0);
343 	break;
344 
345       case 'v':
346 	vid = strtol(optarg, NULL, 0);
347 	break;
348 
349       default:
350 	usage();
351 	break;
352       }
353   argc -= optind;
354   argv += optind;
355 
356   if (vid != UINT_MAX || pid != UINT_MAX)
357     {
358       if (intr_ep != 0 && (intr_ep & 0x80) == 0)
359 	{
360 	  fprintf(stderr, "Interrupt endpoint must be of type IN\n");
361 	  usage();
362 	}
363 
364       if (argc > 0)
365 	{
366 	  do_request = true;
367 
368 	  int rv = parse_req(argc, argv);
369 	  if (rv < 0)
370 	    return EX_USAGE;
371 	  argc = rv;
372 
373 	  if (argc > 0)
374 	    {
375 	      for (out_len = 0; argc > 0 && out_len < BUFLEN; out_len++, argc--)
376 		{
377 		  unsigned n = strtoul(argv[out_len], 0, 0);
378 		  if (n > 255)
379 		    fprintf(stderr,
380 			    "Warning: data #%d 0x%0x > 0xff, truncating\n",
381 			    out_len, n);
382 		  out_buf[out_len] = (uint8_t)n;
383 		}
384 	      out_len++;
385 	      if (argc > 0)
386 		fprintf(stderr,
387 			"Data count exceeds maximum of %d, ignoring %d elements\n",
388 			BUFLEN, optind);
389 	    }
390 	}
391     }
392 
393   struct libusb20_backend *be;
394   struct libusb20_device *dev;
395 
396   if ((be = libusb20_be_alloc_default()) == NULL)
397     {
398       perror("libusb20_be_alloc()");
399       return 1;
400     }
401 
402   dev = NULL;
403   while ((dev = libusb20_be_device_foreach(be, dev)) != NULL)
404     {
405       struct LIBUSB20_DEVICE_DESC_DECODED *ddp =
406       libusb20_dev_get_device_desc(dev);
407 
408       printf("Found device %s (VID:PID = 0x%04x:0x%04x)\n",
409 	     libusb20_dev_get_desc(dev),
410 	     ddp->idVendor, ddp->idProduct);
411 
412       if (ddp->idVendor == vid && ddp->idProduct == pid)
413 	doit(dev);
414     }
415 
416   libusb20_be_free(be);
417   return 0;
418 }
419