From 77f8aadb253df8bbef6a2cae95554115f457eeac Mon Sep 17 00:00:00 2001 From: Jon Szymaniak Date: Sun, 11 Aug 2013 15:52:08 -0400 Subject: [PATCH] libbladeRF: [WIP] Cleanup of backend open and probe --- linux/lib/src/backend.c | 5 ++--- linux/lib/src/backend/libusb.c | 5 ++++- linux/lib/src/backend/linux.c | 6 +++++- linux/lib/src/bladerf.c | 32 ++++++++++++++++---------------- 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/linux/lib/src/backend.c b/linux/lib/src/backend.c index fda67391..3a50f3e0 100644 --- a/linux/lib/src/backend.c +++ b/linux/lib/src/backend.c @@ -31,11 +31,10 @@ static const struct bladerf_fn * backend_fns[] = { int open_with_any_backend(struct bladerf **device, struct bladerf_devinfo *info) { - int status = 0; + int status = BLADERF_ERR_NODEV; const struct bladerf_fn **fn_tbl; - struct bladerf *ret = NULL; - for (fn_tbl = &backend_fns[0]; *fn_tbl != NULL && ret == NULL; fn_tbl++) { + for (fn_tbl = &backend_fns[0]; *fn_tbl != NULL && status != 0; fn_tbl++) { assert((*fn_tbl)->open); status = (*fn_tbl)->open(device, info); } diff --git a/linux/lib/src/backend/libusb.c b/linux/lib/src/backend/libusb.c index 078d02a5..cf539997 100644 --- a/linux/lib/src/backend/libusb.c +++ b/linux/lib/src/backend/libusb.c @@ -597,7 +597,10 @@ static int lusb_get_device_speed(struct bladerf *dev, int *device_speed) static int access_peripheral(struct bladerf_lusb *lusb, int per, int dir, struct uart_cmd *cmd) { - uint8_t buf[16]; + uint8_t buf[16] = { 0 }; /* Zeroing out to avoid some valgrind noise + * on the reserved items that aren't currently + * used (i.e., bytes 4-15 */ + int status, libusb_status, transferred; /* Populate the buffer for transfer */ diff --git a/linux/lib/src/backend/linux.c b/linux/lib/src/backend/linux.c index afae59cf..205902f0 100644 --- a/linux/lib/src/backend/linux.c +++ b/linux/lib/src/backend/linux.c @@ -510,7 +510,11 @@ static int linux_open( struct bladerf **device, struct bladerf_devinfo *info) } else { status = linux_probe(&list); if (status < 0) { - dbg_printf("Probe failed: %s\n", bladerf_strerror(status)); + if (status == BLADERF_ERR_NODEV) { + dbg_printf("No devices available on the Linux driver backend.\n"); + } else { + dbg_printf("Probe failed: %s\n", bladerf_strerror(status)); + } } else { for (i = 0; i < list.num_elt && !ret; i++) { if (bladerf_devinfo_matches(&list.elt[i], info)) { diff --git a/linux/lib/src/bladerf.c b/linux/lib/src/bladerf.c index 2b280a35..a645a148 100644 --- a/linux/lib/src/bladerf.c +++ b/linux/lib/src/bladerf.c @@ -55,31 +55,31 @@ static void init_stats(struct bladerf_stats *stats) int bladerf_open_with_devinfo(struct bladerf **device, struct bladerf_devinfo *devinfo) { + struct bladerf *opened_device; int status; - struct bladerf *ret; *device = NULL; + status = backend_open(device, devinfo); - status = backend_open(&ret, devinfo); if (!status) { + /* Catch bugs from backends returning status = 0, but a NULL device */ + assert(*device); + opened_device = *device; + /* We got a device */ - if (ret) { - bladerf_set_error(&ret->error, ETYPE_LIBBLADERF, 0); - init_stats(&ret->stats); + bladerf_set_error(&opened_device->error, ETYPE_LIBBLADERF, 0); + init_stats(&opened_device->stats); - ret->last_tx_sample_rate = 0; - ret->last_rx_sample_rate = 0; + opened_device->last_tx_sample_rate = 0; + opened_device->last_rx_sample_rate = 0; - status = ret->fn->get_device_speed(ret, &ret->speed); - if (status < 0) { - ret->fn->close(ret); - ret = NULL; - } else { - *device = ret; - } - } else { - status = BLADERF_ERR_NODEV; + status = opened_device->fn->get_device_speed(opened_device, + &opened_device->speed); + + if (status < 0) { + opened_device->fn->close((*device)); + *device = NULL; } }