int FTDIDevice_Open(FTDIDevice *dev) { int err; memset(dev, 0, sizeof *dev); if ((err = libusb_init(&dev->libusb))) { return err; } libusb_set_option(dev->libusb, LIBUSB_OPTION_LOG_LEVEL, 2); dev->handle = libusb_open_device_with_vid_pid(dev->libusb, OV_VENDOR, OV_PRODUCT); if (!dev->handle) { dev->handle = libusb_open_device_with_vid_pid(dev->libusb, FTDI_VENDOR, FTDI_PRODUCT_FT2232H); } if (!dev->handle) { return LIBUSB_ERROR_NO_DEVICE; } return DeviceInit(dev); }
//============================================================================== void USB_Device::set_debug_mode(DebugLevel level) { #if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01000106) libusb_set_option(context_.get(), LIBUSB_OPTION_LOG_LEVEL, level); #else libusb_set_debug(context_.get(), level); #endif }
/* Configure CH341A, find the device and set the default interface. */ int32_t ch341Configure(uint16_t vid, uint16_t pid) { struct libusb_device *dev; int32_t ret; struct sigaction sa; uint8_t desc[0x12]; if (devHandle != NULL) { fprintf(stderr, "Call ch341Release before re-configure\n"); return -1; } ret = libusb_init(NULL); if(ret < 0) { fprintf(stderr, "Couldn't initialise libusb\n"); return -1; } libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_INFO); if(!(devHandle = libusb_open_device_with_vid_pid(NULL, vid, pid))) { fprintf(stderr, "Couldn't open device [%04x:%04x].\n", vid, pid); return -1; } if(!(dev = libusb_get_device(devHandle))) { fprintf(stderr, "Couldn't get bus number and address.\n"); goto close_handle; } if(libusb_kernel_driver_active(devHandle, 0)) { ret = libusb_detach_kernel_driver(devHandle, 0); if(ret) { fprintf(stderr, "Failed to detach kernel driver: '%s'\n", strerror(-ret)); goto close_handle; } } ret = libusb_claim_interface(devHandle, 0); if(ret) { fprintf(stderr, "Failed to claim interface 0: '%s'\n", strerror(-ret)); goto close_handle; } ret = libusb_get_descriptor(devHandle, LIBUSB_DT_DEVICE, 0x00, desc, 0x12); if(ret < 0) { fprintf(stderr, "Failed to get device descriptor: '%s'\n", strerror(-ret)); goto release_interface; } printf("Device reported its revision [%d.%02d]\n", desc[12], desc[13]); sa.sa_handler = &sig_int; sa.sa_flags = SA_RESTART; sigfillset(&sa.sa_mask); if (sigaction(SIGINT, &sa, &saold) == -1) { perror("Error: cannot handle SIGINT"); // Should not happen } return 0; release_interface: libusb_release_interface(devHandle, 0); close_handle: libusb_close(devHandle); devHandle = NULL; return -1; }