xref: /freebsd/share/examples/libusb20/bulk.c (revision ab2043b81eaba0d7d7769b4a58b2b6d17bc464a3)
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  * ----------------------------------------------------------------------------
7  *
8  * $FreeBSD$
9  */
10 
11 /*
12  * Simple demo program to illustrate the handling of FreeBSD's
13  * libusb20.
14  *
15  * Issues a bulk output, and then requests a bulk input.
16  */
17 
18 /*
19  * Examples:
20  * Just list all VID:PID pairs
21  * ./bulk
22  *
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
25  *
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
29  * gets stuck.)
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
31  */
32 
33 
34 #include <limits.h>
35 #include <stdio.h>
36 #include <stdint.h>
37 #include <stdlib.h>
38 #include <sysexits.h>
39 #include <unistd.h>
40 
41 #include <libusb20.h>
42 #include <libusb20_desc.h>
43 
44 #include "aux.h"
45 
46 /*
47  * If you want to see the details of the internal datastructures
48  * in the debugger, unifdef the following.
49  */
50 #ifdef DEBUG
51 #  include <sys/queue.h>
52 #  include "/usr/src/lib/libusb/libusb20_int.h"
53 #endif
54 
55 #define BUFLEN 64
56 
57 #define TIMEOUT 5000 		/* 5 s */
58 
59 int in_ep, out_ep;		/* endpoints */
60 uint8_t out_buf[BUFLEN];
61 uint16_t out_len;
62 
63 static void
64 doit(struct libusb20_device *dev)
65 {
66   int rv;
67 
68   /*
69    * Open the device, allocating memory for two possible (bulk or
70    * interrupt) transfers.
71    *
72    * If only control transfers are intended (via
73    * libusb20_dev_request_sync()), transfer_max can be given as 0.
74    */
75   if ((rv = libusb20_dev_open(dev, 2)) != 0)
76     {
77       fprintf(stderr, "libusb20_dev_open: %s\n", usb_error(rv));
78       return;
79     }
80 
81   /*
82    * If the device has more than one configuration, select the desired
83    * one here.
84    */
85   if ((rv = libusb20_dev_set_config_index(dev, 0)) != 0)
86     {
87       fprintf(stderr, "libusb20_dev_set_config_index: %s\n", usb_error(rv));
88       return;
89     }
90 
91   /*
92    * Two transfers have been requested in libusb20_dev_open() above;
93    * obtain the corresponding transfer struct pointers.
94    */
95   struct libusb20_transfer *xfr_out = libusb20_tr_get_pointer(dev, 0);
96   struct libusb20_transfer *xfr_in = libusb20_tr_get_pointer(dev, 1);
97 
98   if (xfr_in == NULL || xfr_out == NULL)
99     {
100       fprintf(stderr, "libusb20_tr_get_pointer: %s\n", usb_error(rv));
101       return;
102     }
103 
104   /*
105    * Open both transfers, the "out" one for the write endpoint, the
106    * "in" one for the read endpoint (ep | 0x80).
107    */
108   if ((rv = libusb20_tr_open(xfr_out, 0, 1, out_ep)) != 0)
109     {
110       fprintf(stderr, "libusb20_tr_open: %s\n", usb_error(rv));
111       return;
112     }
113   if ((rv = libusb20_tr_open(xfr_in, 0, 1, in_ep)) != 0)
114     {
115       fprintf(stderr, "libusb20_tr_open: %s\n", usb_error(rv));
116       return;
117     }
118 
119   uint8_t in_buf[BUFLEN];
120   uint32_t rlen;
121 
122   if (out_len > 0)
123     {
124       if ((rv = libusb20_tr_bulk_intr_sync(xfr_out, out_buf, out_len, &rlen, TIMEOUT))
125 	  != 0)
126 	{
127 	  fprintf(stderr, "libusb20_tr_bulk_intr_sync (OUT): %s\n", usb_error(rv));
128 	}
129       printf("sent %d bytes\n", rlen);
130     }
131 
132   if ((rv = libusb20_tr_bulk_intr_sync(xfr_in, in_buf, BUFLEN, &rlen, TIMEOUT))
133       != 0)
134     {
135       fprintf(stderr, "libusb20_tr_bulk_intr_sync: %s\n", usb_error(rv));
136     }
137       printf("received %d bytes\n", rlen);
138       if (rlen > 0)
139 	print_formatted(in_buf, rlen);
140 
141   libusb20_tr_close(xfr_out);
142   libusb20_tr_close(xfr_in);
143 
144   libusb20_dev_close(dev);
145 }
146 
147 static void
148 usage(void)
149 {
150   fprintf(stderr,
151 	  "Usage ./usb -i <IN_EP> -o <OUT_EP> -v <VID> -p <PID> [<outdata> ...\n]");
152   exit(EX_USAGE);
153 }
154 
155 int
156 main(int argc, char **argv)
157 {
158   unsigned int vid = UINT_MAX, pid = UINT_MAX; /* impossible VID:PID */
159   int c;
160 
161   while ((c = getopt(argc, argv, "i:o:p:v:")) != -1)
162     switch (c)
163       {
164       case 'i':
165 	in_ep = strtol(optarg, NULL, 0);
166 	break;
167 
168       case 'o':
169 	out_ep = strtol(optarg, NULL, 0);
170 	break;
171 
172       case 'p':
173 	pid = strtol(optarg, NULL, 0);
174 	break;
175 
176       case 'v':
177 	vid = strtol(optarg, NULL, 0);
178 	break;
179 
180       default:
181 	usage();
182 	break;
183       }
184   argc -= optind;
185   argv += optind;
186 
187   if (vid != UINT_MAX || pid != UINT_MAX)
188     {
189       if (in_ep == 0 || out_ep == 0)
190 	{
191 	  usage();
192 	}
193       if ((in_ep & 0x80) == 0)
194 	{
195 	  fprintf(stderr, "IN_EP must have bit 7 set\n");
196 	  return (EX_USAGE);
197 	}
198 
199       if (argc > 0)
200 	{
201 	  for (out_len = 0; argc > 0 && out_len < BUFLEN; out_len++, argc--)
202 	    {
203 	      unsigned n = strtoul(argv[out_len], 0, 0);
204 	      if (n > 255)
205 		fprintf(stderr,
206 			"Warning: data #%d 0x%0x > 0xff, truncating\n",
207 			out_len, n);
208 	      out_buf[out_len] = (uint8_t)n;
209 	    }
210 	  out_len++;
211 	  if (argc > 0)
212 	    fprintf(stderr,
213 		    "Data count exceeds maximum of %d, ignoring %d elements\n",
214 		    BUFLEN, optind);
215 	}
216     }
217 
218   struct libusb20_backend *be;
219   struct libusb20_device *dev;
220 
221   if ((be = libusb20_be_alloc_default()) == NULL)
222     {
223       perror("libusb20_be_alloc()");
224       return 1;
225     }
226 
227   dev = NULL;
228   while ((dev = libusb20_be_device_foreach(be, dev)) != NULL)
229     {
230       struct LIBUSB20_DEVICE_DESC_DECODED *ddp =
231       libusb20_dev_get_device_desc(dev);
232 
233       printf("Found device %s (VID:PID = 0x%04x:0x%04x)\n",
234 	     libusb20_dev_get_desc(dev),
235 	     ddp->idVendor, ddp->idProduct);
236 
237       if (ddp->idVendor == vid && ddp->idProduct == pid)
238 	doit(dev);
239     }
240 
241   libusb20_be_free(be);
242   return 0;
243 }
244