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