1 /* ----------------------------------------------------------------------------
2 * "THE BEER-WARE LICENSE" (Revision 42) (by Poul-Henning Kamp):
3 * <joerg@FreeBSD.ORG> wrote this file. As long as you retain this notice you
4 * can do whatever you want with this stuff. If we meet some day, and you think
5 * this stuff is worth it, you can buy me a beer in return. Joerg Wunsch
6 * ----------------------------------------------------------------------------
8 * $FreeBSD: head/share/examples/libusb20/bulk.c 257779 2013-11-07 07:22:51Z hselasky $
12 * Simple demo program to illustrate the handling of FreeBSD's
15 * Issues a bulk output, and then requests a bulk input.
20 * Just list all VID:PID pairs
23 * Say "hello" to an Atmel JTAGICEmkII.
24 * ./bulk -o 2 -i 0x82 -v 0x03eb -p 0x2103 0x1b 0 0 1 0 0 0 0x0e 1 0xf3 0x97
26 * Return the INQUIRY data of an USB mass storage device.
27 * (It's best to have the umass(4) driver unloaded while doing such
28 * experiments, and perform a "usbconfig reset" for the device if it
30 * ./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
42 #include <libusb20_desc.h>
47 * If you want to see the details of the internal datastructures
48 * in the debugger, unifdef the following.
51 # include <sys/queue.h>
52 # include "/usr/src/lib/libusb/libusb20_int.h"
57 #define TIMEOUT 5000 /* 5 s */
59 int in_ep, out_ep; /* endpoints */
60 uint8_t out_buf[BUFLEN];
64 doit(struct libusb20_device *dev)
69 * Open the device, allocating memory for two possible (bulk or
70 * interrupt) transfers.
72 * If only control transfers are intended (via
73 * libusb20_dev_request_sync()), transfer_max can be given as 0.
75 if ((rv = libusb20_dev_open(dev, 2)) != 0)
77 fprintf(stderr, "libusb20_dev_open: %s\n", libusb20_strerror(rv));
82 * If the device has more than one configuration, select the desired
85 if ((rv = libusb20_dev_set_config_index(dev, 0)) != 0)
87 fprintf(stderr, "libusb20_dev_set_config_index: %s\n", libusb20_strerror(rv));
92 * Two transfers have been requested in libusb20_dev_open() above;
93 * obtain the corresponding transfer struct pointers.
95 struct libusb20_transfer *xfr_out = libusb20_tr_get_pointer(dev, 0);
96 struct libusb20_transfer *xfr_in = libusb20_tr_get_pointer(dev, 1);
98 if (xfr_in == NULL || xfr_out == NULL)
100 fprintf(stderr, "libusb20_tr_get_pointer: %s\n", libusb20_strerror(rv));
105 * Open both transfers, the "out" one for the write endpoint, the
106 * "in" one for the read endpoint (ep | 0x80).
108 if ((rv = libusb20_tr_open(xfr_out, 0, 1, out_ep)) != 0)
110 fprintf(stderr, "libusb20_tr_open: %s\n", libusb20_strerror(rv));
113 if ((rv = libusb20_tr_open(xfr_in, 0, 1, in_ep)) != 0)
115 fprintf(stderr, "libusb20_tr_open: %s\n", libusb20_strerror(rv));
119 uint8_t in_buf[BUFLEN];
124 if ((rv = libusb20_tr_bulk_intr_sync(xfr_out, out_buf, out_len, &rlen, TIMEOUT))
127 fprintf(stderr, "libusb20_tr_bulk_intr_sync (OUT): %s\n", libusb20_strerror(rv));
129 printf("sent %d bytes\n", rlen);
132 if ((rv = libusb20_tr_bulk_intr_sync(xfr_in, in_buf, BUFLEN, &rlen, TIMEOUT))
135 fprintf(stderr, "libusb20_tr_bulk_intr_sync: %s\n", libusb20_strerror(rv));
137 printf("received %d bytes\n", rlen);
139 print_formatted(in_buf, rlen);
141 libusb20_tr_close(xfr_out);
142 libusb20_tr_close(xfr_in);
144 libusb20_dev_close(dev);
151 "Usage ./usb -i <IN_EP> -o <OUT_EP> -v <VID> -p <PID> [<outdata> ...\n]");
156 main(int argc, char **argv)
158 unsigned int vid = UINT_MAX, pid = UINT_MAX; /* impossible VID:PID */
161 while ((c = getopt(argc, argv, "i:o:p:v:")) != -1)
165 in_ep = strtol(optarg, NULL, 0);
169 out_ep = strtol(optarg, NULL, 0);
173 pid = strtol(optarg, NULL, 0);
177 vid = strtol(optarg, NULL, 0);
187 if (vid != UINT_MAX || pid != UINT_MAX)
189 if (in_ep == 0 || out_ep == 0)
193 if ((in_ep & 0x80) == 0)
195 fprintf(stderr, "IN_EP must have bit 7 set\n");
201 for (out_len = 0; argc > 0 && out_len < BUFLEN; out_len++, argc--)
203 unsigned n = strtoul(argv[out_len], 0, 0);
206 "Warning: data #%d 0x%0x > 0xff, truncating\n",
208 out_buf[out_len] = (uint8_t)n;
213 "Data count exceeds maximum of %d, ignoring %d elements\n",
218 struct libusb20_backend *be;
219 struct libusb20_device *dev;
221 if ((be = libusb20_be_alloc_default()) == NULL)
223 perror("libusb20_be_alloc()");
228 while ((dev = libusb20_be_device_foreach(be, dev)) != NULL)
230 struct LIBUSB20_DEVICE_DESC_DECODED *ddp =
231 libusb20_dev_get_device_desc(dev);
233 printf("Found device %s (VID:PID = 0x%04x:0x%04x)\n",
234 libusb20_dev_get_desc(dev),
235 ddp->idVendor, ddp->idProduct);
237 if (ddp->idVendor == vid && ddp->idProduct == pid)
241 libusb20_be_free(be);