static bool device_is_probe_target(backend_probe_target probe_target, libusb_device *dev) { bool is_probe_target = false; switch (probe_target) { case BACKEND_PROBE_BLADERF: is_probe_target = device_is_bladerf(dev); if (is_probe_target) { log_verbose("Found a bladeRF\n"); } break; case BACKEND_PROBE_FX3_BOOTLOADER: is_probe_target = device_is_fx3_bootloader(dev); if (is_probe_target) { log_verbose("Found an FX3 bootloader.\n"); } break; default: assert(!"Invalid probe target"); } return is_probe_target; }
static int lusb_open_bootloader(void **driver, uint8_t bus, uint8_t addr) { int status; struct libusb_device **dev_list = NULL; ssize_t dev_list_size, i; struct bladerf_lusb *lusb; *driver = NULL; lusb = calloc(1, sizeof(lusb[0])); if (lusb == NULL) { return BLADERF_ERR_MEM; } status = libusb_init(&lusb->context); if (status != 0) { log_debug("Failed to initialize libusb context: %s\n", libusb_error_name(status)); goto error; } dev_list_size = libusb_get_device_list(lusb->context, &dev_list); if (dev_list_size < 0) { log_debug("Failed to get device list: %s\n", libusb_error_name(status)); status = (int) dev_list_size; goto error; } for (i = 0; i < dev_list_size; i++) { if (device_is_fx3_bootloader(dev_list[i]) && bus_matches(bus, dev_list[i]) && addr_matches(addr, dev_list[i])) { status = libusb_open(dev_list[i], &lusb->handle); if (status != 0) { log_debug("Failed to open device: %s\n", libusb_error_name(status)); goto error; } else { status = libusb_claim_interface(lusb->handle, 0); if (status < 0) { log_debug("Failed to claim interface: %s\n", libusb_error_name(status)); goto error; } else { log_verbose("Opened bootloader at %u:%u\n", libusb_get_bus_number(dev_list[i]), libusb_get_device_address(dev_list[i])); *driver = lusb; } break; } } } error: if (dev_list != NULL) { libusb_free_device_list(dev_list, 1); } if (status != 0) { status = error_conv(status); lusb_close_bootloader(lusb); } else if (*driver == NULL) { status = BLADERF_ERR_NODEV; lusb_close_bootloader(lusb); } return status; }
static int lusb_open(void **driver, struct bladerf_devinfo *info_in, struct bladerf_devinfo *info_out) { int status, i, n; int fx3_status; ssize_t count; struct bladerf_lusb *lusb = NULL; libusb_device **list = NULL; struct bladerf_devinfo thisinfo; libusb_context *context; /* Initialize libusb for device tree walking */ status = libusb_init(&context); if (status) { log_error("Could not initialize libusb: %s\n", libusb_error_name(status)); status = error_conv(status); goto error; } /* We can only print this out when log output is enabled, or else we'll * get snagged by -Werror=unused-but-set-variable */ # ifdef LOGGING_ENABLED { char buf[64]; get_libusb_version(buf, sizeof(buf)); log_verbose("Using libusb version: %s\n", buf); } # endif /* Iterate through all the USB devices */ count = libusb_get_device_list(context, &list); for (i = 0, n = 0; i < count; i++) { if (device_is_bladerf(list[i])) { log_verbose("Found a bladeRF (based upon VID/PID)\n"); /* Open the USB device and get some information */ status = get_devinfo(list[i], &thisinfo); if(status < 0) { log_debug("Could not open bladeRF device: %s\n", libusb_error_name(status) ); status = error_conv(status); goto error; } thisinfo.instance = n++; /* Check to see if this matches the info struct */ if (bladerf_devinfo_matches(&thisinfo, info_in)) { lusb = (struct bladerf_lusb *)malloc(sizeof(struct bladerf_lusb)); if (lusb == NULL) { log_debug("Skipping instance %d due to failed allocation\n", thisinfo.instance); lusb = NULL; continue; } lusb->context = context; lusb->dev = list[i]; status = libusb_open(list[i], &lusb->handle); if (status < 0) { status = error_conv(status); goto error; } status = libusb_claim_interface(lusb->handle, 0); if(status < 0) { log_debug("Could not claim interface: %s\n", libusb_error_name(status)); status = error_conv(status); goto error; } memcpy(info_out, &thisinfo, sizeof(struct bladerf_devinfo)); *driver = lusb; break; } else { log_verbose("Devinfo doesn't match - skipping" "(instance=%d, serial=%d, bus/addr=%d\n", bladerf_instance_matches(&thisinfo, info_in), bladerf_serial_matches(&thisinfo, info_in), bladerf_bus_addr_matches(&thisinfo, info_in)); } } if (device_is_fx3_bootloader(list[i])) { fx3_status = get_devinfo(list[i], &thisinfo); if (fx3_status != 0) { log_debug("Could not open FX3 bootloader device: %s\n", libusb_error_name(fx3_status)); continue; } log_info("Found FX3 bootloader device on bus=%d addr=%d. " "This may be a bladeRF.\n", thisinfo.usb_bus, thisinfo.usb_addr); log_info("Use bladeRF-cli command \"recover %d %d " "<FX3 firmware>\" to boot the bladeRF firmware.\n", thisinfo.usb_bus, thisinfo.usb_addr); } } error: if (list) { libusb_free_device_list(list, 1); } if (lusb == NULL) { log_debug("No devices available on the libusb backend.\n"); status = BLADERF_ERR_NODEV; } if (status != 0) { if (lusb != NULL) { if (lusb->handle) { libusb_close(lusb->handle); } free(lusb); } libusb_exit(context); } return status; }
static int lusb_probe(struct bladerf_devinfo_list *info_list) { int status, i, n; ssize_t count; libusb_device **list; struct bladerf_devinfo info; libusb_context *context; /* Initialize libusb for device tree walking */ status = libusb_init(&context); if (status) { log_error("Could not initialize libusb: %s\n", libusb_error_name(status)); goto lusb_probe_done; } count = libusb_get_device_list(context, &list); /* Iterate through all the USB devices */ for (i = 0, n = 0; i < count && status == 0; i++) { if (device_is_bladerf(list[i])) { log_verbose("Found bladeRF (based upon VID/PID)\n"); /* Open the USB device and get some information */ status = get_devinfo(list[i], &info); if (status) { log_error("Could not open bladeRF device: %s\n", libusb_error_name(status) ); } else { info.instance = n++; status = bladerf_devinfo_list_add(info_list, &info); if( status ) { log_error("Could not add device to list: %s\n", bladerf_strerror(status) ); } else { log_verbose("Added instance %d to device list\n", info.instance); } } } if (device_is_fx3_bootloader(list[i])) { status = get_devinfo(list[i], &info); if (status) { log_error("Could not open bladeRF device: %s\n", libusb_error_name(status) ); continue; } log_info("Found FX3 bootloader device on bus=%d addr=%d. This may " "be a bladeRF.\nUse the bladeRF-cli command \"recover" " %d %d <FX3 firmware>\" to boot the bladeRF firmware.\n", info.usb_bus, info.usb_addr, info.usb_bus, info.usb_addr); } } libusb_free_device_list(list, 1); libusb_exit(context); lusb_probe_done: return status; }