Exemplo n.º 1
0
static int lusb_open(void **driver,
                     struct bladerf_devinfo *info_in,
                     struct bladerf_devinfo *info_out)
{
    int status;
    struct bladerf_lusb *lusb = NULL;
    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));
        return error_conv(status);
    }

    /* 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

    status = find_and_open_device(context, info_in, &lusb, info_out);
    if (status != 0) {
        libusb_exit(context);

        if (status == BLADERF_ERR_NODEV) {
            log_debug("No devices available on the libusb backend.\n");
        } else {
            log_debug("Failed to open bladeRF on libusb backend: %s\n",
                    bladerf_strerror(status));
        }
    } else {
        assert(lusb != NULL);

        /* Cosmin and Marian from Null Team (null.ro) and YateBTS(.com) found
         * that it is possible to recover from "Issue #95: Not enough bandwidth
         * for altsetting" by performing a USB port reset prior to actually
         * trying to use the device.
         */
#       if ENABLE_USB_DEV_RESET_ON_OPEN
        if (bladerf_usb_reset_device_on_open) {
            status = reset_and_reopen(context, &lusb, info_out);
        }
#       endif

        if (status == 0) {
            *driver = (void *) lusb;
        }
    }

    return status;
}
Exemplo n.º 2
0
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;
}