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
18 * Issues a bulk output, and then requests a bulk input.
23 * Just list all VID:PID pairs
26 * Say "hello" to an Atmel JTAGICEmkII.
27 * ./bulk -o 2 -i 0x82 -v 0x03eb -p 0x2103 0x1b 0 0 1 0 0 0 0x0e 1 0xf3 0x97
29 * Return the INQUIRY data of an USB mass storage device.
30 * (It's best to have the umass(4) driver unloaded while doing such
31 * experiments, and perform a "usbconfig reset" for the device if it
33 * ./bulk -v 0x5e3 -p 0x723 -i 0x81 -o 2 0x55 0x53 0x42 0x43 1 2 3 4 31 12 0x80 0x24 0 0 0 0x12 0 0 0 36 0 0 0 0 0 0 0 0 0 0
45 #include <libusb20_desc.h>
50 * If you want to see the details of the internal datastructures
51 * in the debugger, unifdef the following.
54 # include <sys/queue.h>
55 # include "/usr/src/lib/libusb/libusb20_int.h"
60 #define TIMEOUT 5000 /* 5 s */
62 int in_ep, out_ep; /* endpoints */
63 uint8_t out_buf[BUFLEN];
67 doit(struct libusb20_device *dev)
72 * Open the device, allocating memory for two possible (bulk or
73 * interrupt) transfers.
75 * If only control transfers are intended (via
76 * libusb20_dev_request_sync()), transfer_max can be given as 0.
78 if ((rv = libusb20_dev_open(dev, 2)) != 0)
80 fprintf(stderr, "libusb20_dev_open: %s\n", libusb20_strerror(rv));
85 * If the device has more than one configuration, select the desired
88 if ((rv = libusb20_dev_set_config_index(dev, 0)) != 0)
90 fprintf(stderr, "libusb20_dev_set_config_index: %s\n", libusb20_strerror(rv));
95 * Two transfers have been requested in libusb20_dev_open() above;
96 * obtain the corresponding transfer struct pointers.
98 struct libusb20_transfer *xfr_out = libusb20_tr_get_pointer(dev, 0);
99 struct libusb20_transfer *xfr_in = libusb20_tr_get_pointer(dev, 1);
101 if (xfr_in == NULL || xfr_out == NULL)
103 fprintf(stderr, "libusb20_tr_get_pointer: %s\n", libusb20_strerror(rv));
108 * Open both transfers, the "out" one for the write endpoint, the
109 * "in" one for the read endpoint (ep | 0x80).
111 if ((rv = libusb20_tr_open(xfr_out, 0, 1, out_ep)) != 0)
113 fprintf(stderr, "libusb20_tr_open: %s\n", libusb20_strerror(rv));
116 if ((rv = libusb20_tr_open(xfr_in, 0, 1, in_ep)) != 0)
118 fprintf(stderr, "libusb20_tr_open: %s\n", libusb20_strerror(rv));
122 uint8_t in_buf[BUFLEN];
127 if ((rv = libusb20_tr_bulk_intr_sync(xfr_out, out_buf, out_len, &rlen, TIMEOUT))
130 fprintf(stderr, "libusb20_tr_bulk_intr_sync (OUT): %s\n", libusb20_strerror(rv));
132 printf("sent %d bytes\n", rlen);
135 if ((rv = libusb20_tr_bulk_intr_sync(xfr_in, in_buf, BUFLEN, &rlen, TIMEOUT))
138 fprintf(stderr, "libusb20_tr_bulk_intr_sync: %s\n", libusb20_strerror(rv));
140 printf("received %d bytes\n", rlen);
142 print_formatted(in_buf, rlen);
144 libusb20_tr_close(xfr_out);
145 libusb20_tr_close(xfr_in);
147 libusb20_dev_close(dev);
154 "Usage ./usb -i <IN_EP> -o <OUT_EP> -v <VID> -p <PID> [<outdata> ...\n]");
159 main(int argc, char **argv)
161 unsigned int vid = UINT_MAX, pid = UINT_MAX; /* impossible VID:PID */
164 while ((c = getopt(argc, argv, "i:o:p:v:")) != -1)
168 in_ep = strtol(optarg, NULL, 0);
172 out_ep = strtol(optarg, NULL, 0);
176 pid = strtol(optarg, NULL, 0);
180 vid = strtol(optarg, NULL, 0);
190 if (vid != UINT_MAX || pid != UINT_MAX)
192 if (in_ep == 0 || out_ep == 0)
196 if ((in_ep & 0x80) == 0)
198 fprintf(stderr, "IN_EP must have bit 7 set\n");
204 for (out_len = 0; argc > 0 && out_len < BUFLEN; out_len++, argc--)
206 unsigned n = strtoul(argv[out_len], 0, 0);
209 "Warning: data #%d 0x%0x > 0xff, truncating\n",
211 out_buf[out_len] = (uint8_t)n;
216 "Data count exceeds maximum of %d, ignoring %d elements\n",
221 struct libusb20_backend *be;
222 struct libusb20_device *dev;
224 if ((be = libusb20_be_alloc_default()) == NULL)
226 perror("libusb20_be_alloc()");
231 while ((dev = libusb20_be_device_foreach(be, dev)) != NULL)
233 struct LIBUSB20_DEVICE_DESC_DECODED *ddp =
234 libusb20_dev_get_device_desc(dev);
236 printf("Found device %s (VID:PID = 0x%04x:0x%04x)\n",
237 libusb20_dev_get_desc(dev),
238 ddp->idVendor, ddp->idProduct);
240 if (ddp->idVendor == vid && ddp->idProduct == pid)
244 libusb20_be_free(be);