2 * SPDX-License-Identifier: Beerware
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 * ----------------------------------------------------------------------------
15 * Simple demo program to illustrate the handling of FreeBSD's
21 * Just list all VID:PID pairs
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
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
45 #include <libusb20_desc.h>
47 #include <sys/queue.h>
52 * If you want to see the details of the internal datastructures
53 * in the debugger, unifdef the following.
56 # include "/usr/src/lib/libusb/libusb20_int.h"
61 #define TIMEOUT 5000 /* 5 s */
63 int intr_ep; /* endpoints */
64 struct LIBUSB20_CONTROL_SETUP_DECODED setup;
66 uint8_t out_buf[BUFLEN];
72 doit(struct libusb20_device *dev)
77 printf("doit(): bmRequestType 0x%02x, bRequest 0x%02x, wValue 0x%04x, wIndex 0x%04x, wLength 0x%04x\n",
85 * Open the device, allocating memory for two possible (bulk or
86 * interrupt) transfers.
88 * If only control transfers are intended (via
89 * libusb20_dev_request_sync()), transfer_max can be given as 0.
91 if ((rv = libusb20_dev_open(dev, 1)) != 0)
93 fprintf(stderr, "libusb20_dev_open: %s\n", libusb20_strerror(rv));
98 * If the device has more than one configuration, select the desired
101 if ((rv = libusb20_dev_set_config_index(dev, 0)) != 0)
103 fprintf(stderr, "libusb20_dev_set_config_index: %s\n", libusb20_strerror(rv));
110 if ((setup.bmRequestType & 0x80) != 0)
112 /* this is an IN request, allocate a buffer */
113 data = malloc(setup.wLength);
117 "Out of memory allocating %u bytes of reply buffer\n",
127 if ((rv = libusb20_dev_request_sync(dev, &setup, data,
130 0 /* flags */)) != 0)
133 "libusb20_dev_request_sync: %s\n", libusb20_strerror(rv));
135 printf("sent %d bytes\n", actlen);
136 if ((setup.bmRequestType & 0x80) != 0)
138 print_formatted(data, (uint32_t)setup.wLength);
146 * One transfer has been requested in libusb20_dev_open() above;
147 * obtain the corresponding transfer struct pointer.
149 struct libusb20_transfer *xfr_intr = libusb20_tr_get_pointer(dev, 0);
151 if (xfr_intr == NULL)
153 fprintf(stderr, "libusb20_tr_get_pointer: %s\n", libusb20_strerror(rv));
158 * Open the interrupt transfer.
160 if ((rv = libusb20_tr_open(xfr_intr, 0, 1, intr_ep)) != 0)
162 fprintf(stderr, "libusb20_tr_open: %s\n", libusb20_strerror(rv));
166 uint8_t in_buf[BUFLEN];
169 if ((rv = libusb20_tr_bulk_intr_sync(xfr_intr, in_buf, BUFLEN, &rlen, TIMEOUT))
172 fprintf(stderr, "libusb20_tr_bulk_intr_sync: %s\n", libusb20_strerror(rv));
174 printf("received %d bytes\n", rlen);
176 print_formatted(in_buf, rlen);
178 libusb20_tr_close(xfr_intr);
181 libusb20_dev_close(dev);
188 "Usage ./usb [-i <INTR_EP>] -v <VID> -p <PID> [dir type rcpt req wValue wIndex wLength [<outdata> ...]]\n");
192 static const char *reqnames[] =
210 get_req(const char *reqname)
213 size_t l = strlen(reqname);
216 i < sizeof reqnames / sizeof reqnames[0];
218 if (strncasecmp(reqname, reqnames[i], l) == 0)
221 return strtoul(reqname, 0, 0);
226 parse_req(int argc, char **argv)
231 for (idx = 0; argc != 0 && idx <= 6; argc--, idx++)
235 /* dir[ection]: i[n] | o[ut] */
236 if (*argv[idx] == 'i')
238 else if (*argv[idx] == 'o')
242 fprintf(stderr, "request direction must be \"in\" or \"out\" (got %s)\n",
249 /* type: s[tandard] | c[lass] | v[endor] */
250 if (*argv[idx] == 's')
252 else if (*argv[idx] == 'c')
254 else if (*argv[idx] == 'v')
259 "request type must be one of \"standard\", \"class\", or \"vendor\" (got %s)\n",
266 /* rcpt: d[evice], i[nterface], e[ndpoint], o[ther] */
267 if (*argv[idx] == 'd')
269 else if (*argv[idx] == 'i')
271 else if (*argv[idx] == 'e')
273 else if (*argv[idx] == 'o')
278 "recipient must be one of \"device\", \"interface\", \"endpoint\", or \"other\" (got %s)\n",
282 setup.bmRequestType = rt;
286 setup.bRequest = get_req(argv[idx]);
290 setup.wValue = strtoul(argv[idx], 0, 0);
294 setup.wIndex = strtoul(argv[idx], 0, 0);
298 setup.wLength = strtoul(argv[idx], 0, 0);
307 main(int argc, char **argv)
309 unsigned int vid = UINT_MAX, pid = UINT_MAX; /* impossible VID:PID */
313 * Initialize setup struct. This step is required, and initializes
314 * internal fields in the struct.
316 * All the "public" fields are named exactly the way as the USB
317 * standard describes them, namely:
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,
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
332 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &setup);
334 while ((c = getopt(argc, argv, "i:p:v:")) != -1)
338 intr_ep = strtol(optarg, NULL, 0);
342 pid = strtol(optarg, NULL, 0);
346 vid = strtol(optarg, NULL, 0);
356 if (vid != UINT_MAX || pid != UINT_MAX)
358 if (intr_ep != 0 && (intr_ep & 0x80) == 0)
360 fprintf(stderr, "Interrupt endpoint must be of type IN\n");
368 int rv = parse_req(argc, argv);
375 for (out_len = 0; argc > 0 && out_len < BUFLEN; out_len++, argc--)
377 unsigned n = strtoul(argv[out_len], 0, 0);
380 "Warning: data #%d 0x%0x > 0xff, truncating\n",
382 out_buf[out_len] = (uint8_t)n;
387 "Data count exceeds maximum of %d, ignoring %d elements\n",
393 struct libusb20_backend *be;
394 struct libusb20_device *dev;
396 if ((be = libusb20_be_alloc_default()) == NULL)
398 perror("libusb20_be_alloc()");
403 while ((dev = libusb20_be_device_foreach(be, dev)) != NULL)
405 struct LIBUSB20_DEVICE_DESC_DECODED *ddp =
406 libusb20_dev_get_device_desc(dev);
408 printf("Found device %s (VID:PID = 0x%04x:0x%04x)\n",
409 libusb20_dev_get_desc(dev),
410 ddp->idVendor, ddp->idProduct);
412 if (ddp->idVendor == vid && ddp->idProduct == pid)
416 libusb20_be_free(be);