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