libbladeRF: Cleaned up a few items and issues in OTP/cal readback.

This addresses some invalid accesses reported by valgrind. However, a few issues remain:
  - Accessing calibration data on a 2.0 port causes an invalid access
  - The libusb backend's implemation of open() will need to be
  refactored in order to support matching on serial numbers.
This commit is contained in:
Jon Szymaniak 2013-08-27 00:07:13 -04:00
commit b82952687e
6 changed files with 255 additions and 125 deletions

View file

@ -55,13 +55,15 @@ typedef enum {
BLADERF_BACKEND_LIBUSB /**< libusb */
} bladerf_backend;
/* Length of device serial number string, including NUL-terminator */
#define BLADERF_SERIAL_LENGTH 33
/**
* Information about a bladeRF attached to the system
*/
struct bladerf_devinfo {
bladerf_backend backend; /**< Backend to use when connecting to device */
char serial[33]; /**< Device's serial number */
char serial[BLADERF_SERIAL_LENGTH]; /**< Device serial number string */
uint8_t usb_bus; /**< Bus # device is attached to */
uint8_t usb_addr; /**< Device address on bus */
unsigned int instance; /**< Device instance or ID */
@ -104,6 +106,15 @@ typedef enum {
*/
} bladerf_format;
/**
* FPGA device variant (size)
*/
typedef enum {
BLADERF_FPGA_UNKNOWN = 0, /**< Unable to determine FPGA variant */
BLADERF_FPGA_40KLE = 40, /**< 40 kLE FPGA */
BLADERF_FPGA_115KLE = 115 /**< 115 kLE FPGA */
} bladerf_fpga_size;
/**
* Sample metadata
*/
@ -664,7 +675,7 @@ int bladerf_get_vctcxo_trim(struct bladerf *dev, uint16_t *trim);
*
* @return 0 on success, value from \ref RETCODES list on failure
*/
int bladerf_get_fpga_size(struct bladerf *dev, int *size);
int bladerf_get_fpga_size(struct bladerf *dev, bladerf_fpga_size *size);
/**
* Query firmware version

View file

@ -193,11 +193,16 @@ static int lusb_get_devinfo(libusb_device *dev, struct bladerf_devinfo *info)
dbg_printf( "Couldn't populate devinfo - %s\n", libusb_error_name(status) );
status = error_libusb2bladerf(status);
} else {
/* Populate */
/* Populate device info */
info->backend = BLADERF_BACKEND_LIBUSB;
info->usb_bus = libusb_get_bus_number(dev);
info->usb_addr = libusb_get_device_address(dev);
/* FIXME We need to get the serial number here before we've opened
* the device. Clearing it out for now to avoid uninitialized
* memory issues. */
memset(info->serial, 0, BLADERF_SERIAL_LENGTH);
libusb_close( handle );
}
@ -229,7 +234,12 @@ static int lusb_open(struct bladerf **device, struct bladerf_devinfo *info)
/* Iterate through all the USB devices */
for( i = 0, n = 0; i < count; i++ ) {
if( lusb_device_is_bladerf(list[i]) ) {
/* Open the USB device and get some information */
/* Open the USB device and get some information
*
* FIXME in order for the bladerf_devinfo_matches() to work, this
* routine has to be able to get the serial #.
*/
status = lusb_get_devinfo( list[i], &thisinfo );
if(status < 0) {
dbg_printf( "Could not open bladeRF device: %s\n", libusb_error_name(status) );
@ -278,12 +288,6 @@ static int lusb_open(struct bladerf **device, struct bladerf_devinfo *info)
dev->legacy = LEGACY_ALT_SETTING;
}
if (dev->legacy) {
printf("********************************************************************************\n");
printf("* ENTERING LEGACY MODE, PLEASE UPGRADE TO THE LATEST FIRMWARE BY RUNNING:\n");
printf("* wget http://nuand.com/fx3/latest.img ; bladeRF-cli -b -f latest.img\n");
printf("********************************************************************************\n");
}
/* Claim interfaces */
for( inf = 0; inf < ((dev->legacy & LEGACY_ALT_SETTING) ? 3 : 1) ; inf++ ) {

View file

@ -15,9 +15,6 @@
#include "version.h" /* Generated at build time */
#include "conversions.h"
static int get_otp_field(struct bladerf *dev, char *field, char *val, int maxlen);
static int get_cal_field(struct bladerf *dev, char *field, char *val, int maxlen);
/*------------------------------------------------------------------------------
* Device discovery & initialization/deinitialization
*----------------------------------------------------------------------------*/
@ -106,25 +103,36 @@ int bladerf_open(struct bladerf **device, const char *dev_id)
if (!status) {
status = bladerf_open_with_devinfo(device, &devinfo);
dev = *device;
}
if (!status && !(*device)->legacy) {
dev = *device;
if (!status) {
if (!dev->legacy) {
char tmp[40];
bool ok;
get_otp_field(*device, "S", dev->serial, 32);
get_cal_field(*device, "DAC", tmp, 6);
dev->dac_trim = str2uint(tmp, 0, 0xffff, &ok);
if (!ok)
dev->dac_trim = 0x8000;
get_cal_field(*device, "B", tmp, 6);
dev->variant = str2uint(tmp, 0, 115, &ok);
if (!ok)
dev->variant = 0;
/* FIXME Will this be neccessary since we'll hae to have gotten
* the serial # while probing?
*/
status = bladerf_get_serial_nocache(dev);
dbg_printf("%s: fw=v%d.%d serial=%s dac=0x%.4x var=%d\n", __FUNCTION__, dev->fw_major,
dev->fw_minor, dev->serial, dev->dac_trim, dev->variant);
if (!status) {
status = bladerf_get_vctcxo_trim_nocache(dev);
}
if (!status) {
status = bladerf_get_fpga_size_nocache(dev);
}
if (!status) {
dbg_printf("%s: fw=v%d.%d serial=%s trim=0x%.4x fpga_size=%d\n",
__FUNCTION__, dev->fw_major, dev->fw_minor,
dev->serial, dev->dac_trim, dev->fpga_size);
}
} else {
printf("********************************************************************************\n");
printf("* ENTERING LEGACY MODE, PLEASE UPGRADE TO THE LATEST FIRMWARE BY RUNNING:\n");
printf("* wget http://nuand.com/fx3/latest.img ; bladeRF-cli -b -f latest.img\n");
printf("********************************************************************************\n");
}
}
return status;
@ -533,93 +541,6 @@ int bladerf_stream(struct bladerf *dev, bladerf_module module,
* Device Info
*----------------------------------------------------------------------------*/
/******
* CRC16 implementation from http://softwaremonkey.org/Code/CRC16
*/
typedef unsigned char byte; /* 8 bit unsigned */
typedef unsigned short int word; /* 16 bit unsigned */
word crc16mp(word crcval, void *data_p, word count) {
/* CRC-16 Routine for processing multiple part data blocks.
* Pass 0 into 'crcval' for first call for any given block; for
* subsequent calls pass the CRC returned by the previous call. */
word xx;
byte *ptr=data_p;
while (count-- > 0) {
crcval=(word)(crcval^(word)(((word)*ptr++)<<8));
for (xx=0;xx<8;xx++) {
if(crcval&0x8000) { crcval=(word)((word)(crcval<<1)^0x1021); }
else { crcval=(word)(crcval<<1); }
}
}
return(crcval);
}
static int extract_field(char *ptr, int len, char *field, char *val, int maxlen) {
int c, wlen;
unsigned char *ub, *end;
unsigned short a1, a2;
int flen;
flen = strlen(field);
ub = (unsigned char *)ptr;
end = ub + len;
while (ub < end) {
c = *ub;
if (c == 0xff) // flash and OTP are 0xff if they've never been written to
break;
a1 = *(unsigned short *)(&ub[c+1]); // read checksum
a2 = crc16mp(0, ub, c+1); // calculated checksum
if (a1 == a2) {
if (!strncmp((char *)ub + 1, field, flen)) {
#define MIN(x,y) (((x)<(y))?(x):(y))
wlen = MIN(c - flen, maxlen);
strncpy(val, (char *)ub + 1 + flen, wlen);
val[wlen] = 0;
return 0;
}
} else {
dbg_printf( "%s: Field checksum mistmatch\n", __FUNCTION__);
return BLADERF_ERR_INVAL;
}
ub += c + 3; //skip past `c' bytes, 2 byte CRC field, and 1 byte len field
}
return BLADERF_ERR_INVAL;
}
int get_otp_field(struct bladerf *dev, char *field, char *val, int maxlen) {
int status;
char *otp;
otp = malloc(256);
if (!otp) {
return BLADERF_ERR_MEM;
}
status = dev->fn->get_otp(dev, otp);
if (status < 0)
return status;
return extract_field(otp, 256, field, val, maxlen);
}
int get_cal_field(struct bladerf *dev, char *field, char *val, int maxlen) {
int status;
char *cal;
cal = malloc(256);
if (!cal) {
return BLADERF_ERR_MEM;
}
status = dev->fn->get_cal(dev, cal);
if (status < 0)
return status;
return extract_field(cal, 256, field, val, maxlen);
}
int bladerf_get_serial(struct bladerf *dev, char *serial)
{
strcpy(serial, dev->serial);
@ -632,9 +553,9 @@ int bladerf_get_vctcxo_trim(struct bladerf *dev, uint16_t *trim)
return 0;
}
int bladerf_get_fpga_size(struct bladerf *dev, int *size)
int bladerf_get_fpga_size(struct bladerf *dev, bladerf_fpga_size *size)
{
*size = dev->variant;
*size = dev->fpga_size;
return 0;
}

View file

@ -6,6 +6,8 @@
#include "bladerf_priv.h"
#include "debug.h"
#define OTP_BUFFER_SIZE 256
void bladerf_set_error(struct bladerf_error *error,
bladerf_error type, int val)
{
@ -167,3 +169,135 @@ int bladerf_devinfo_list_add(struct bladerf_devinfo_list *list,
return status;
}
/******
* CRC16 implementation from http://softwaremonkey.org/Code/CRC16
*/
typedef unsigned char byte; /* 8 bit unsigned */
typedef unsigned short int word; /* 16 bit unsigned */
static word crc16mp(word crcval, void *data_p, word count) {
/* CRC-16 Routine for processing multiple part data blocks.
* Pass 0 into 'crcval' for first call for any given block; for
* subsequent calls pass the CRC returned by the previous call. */
word xx;
byte *ptr=data_p;
while (count-- > 0) {
crcval=(word)(crcval^(word)(((word)*ptr++)<<8));
for (xx=0;xx<8;xx++) {
if(crcval&0x8000) { crcval=(word)((word)(crcval<<1)^0x1021); }
else { crcval=(word)(crcval<<1); }
}
}
return(crcval);
}
static int extract_field(char *ptr, int len, char *field,
char *val, size_t maxlen) {
int c, wlen;
unsigned char *ub, *end;
unsigned short a1, a2;
int flen;
flen = strlen(field);
ub = (unsigned char *)ptr;
end = ub + len;
while (ub < end) {
c = *ub;
if (c == 0xff) // flash and OTP are 0xff if they've never been written to
break;
a1 = *(unsigned short *)(&ub[c+1]); // read checksum
a2 = crc16mp(0, ub, c+1); // calculated checksum
if (a1 == a2) {
if (!strncmp((char *)ub + 1, field, flen)) {
wlen = min_sz(c - flen, maxlen);
strncpy(val, (char *)ub + 1 + flen, wlen);
val[wlen] = 0;
return 0;
}
} else {
dbg_printf( "%s: Field checksum mistmatch\n", __FUNCTION__);
return BLADERF_ERR_INVAL;
}
ub += c + 3; //skip past `c' bytes, 2 byte CRC field, and 1 byte len field
}
return BLADERF_ERR_INVAL;
}
int bladerf_get_otp_field(struct bladerf *dev, char *field,
char *data, size_t data_size)
{
int status;
char otp[OTP_BUFFER_SIZE];
status = dev->fn->get_otp(dev, otp);
if (status < 0)
return status;
else
return extract_field(otp, OTP_BUFFER_SIZE, field, data, data_size);
}
int bladerf_get_cal_field(struct bladerf *dev, char *field,
char *data, size_t data_size)
{
int status;
char cal[OTP_BUFFER_SIZE];
status = dev->fn->get_cal(dev, cal);
if (status < 0)
return status;
else
return extract_field(cal, OTP_BUFFER_SIZE, field, data, data_size);
}
int bladerf_get_serial_nocache(struct bladerf *dev)
{
return bladerf_get_otp_field(dev, "S", dev->serial,
BLADERF_SERIAL_LENGTH - 1);
}
int bladerf_get_vctcxo_trim_nocache(struct bladerf *dev)
{
int status;
bool ok;
int16_t trim;
char tmp[7] = { 0 };
status = bladerf_get_cal_field(dev, "DAC", tmp, sizeof(tmp) - 1);
if (!status) {
trim = str2uint(tmp, 0, 0xffff, &ok);
if (ok) {
dev->dac_trim = trim;
} else {
dbg_printf("DAC trim unprogrammed. Defaulting to 0x8000\n");
dev->dac_trim = 0x8000;
}
}
return status;
}
int bladerf_get_fpga_size_nocache(struct bladerf *device)
{
int status;
char tmp[7] = { 0 };
status = bladerf_get_cal_field(device, "B", tmp, sizeof(tmp) - 1);
if (!strcmp("40", tmp)) {
device->fpga_size = BLADERF_FPGA_40KLE;
} else if(!strcmp("115", tmp)) {
device->fpga_size = BLADERF_FPGA_40KLE;
} else {
device->fpga_size = BLADERF_FPGA_UNKNOWN;
}
return status;
}

View file

@ -7,6 +7,8 @@
#include <limits.h>
#include <libbladeRF.h>
#include "host_config.h"
#include "minmax.h"
#include "conversions.h"
/* Reserved values for bladerf_devinfo fields to indicate "undefined" */
#define DEVINFO_SERIAL_ANY "ANY"
@ -136,9 +138,9 @@ struct bladerf_fn {
#define FW_LEGACY_ALT_SETTING_MINOR 1
#define LEGACY_ALT_SETTING 1
struct bladerf {
char serial[33]; /* The device's serial number */
char serial[BLADERF_SERIAL_LENGTH]; /* The device's serial number */
uint16_t dac_trim;
int variant;
bladerf_fpga_size fpga_size;
unsigned int fw_major, fw_minor;
int legacy;
@ -254,9 +256,67 @@ bladerf_get_devinfo_list(struct bladerf_devinfo *devinfo);
/**
* Add and item to our internal devinfo list
*
* 0 on success, BLADERF_ERR_* on failuer
* @param list List to append to
* @param info Info to copy into the list
*
* 0 on success, BLADERF_ERR_* on failure
*/
int bladerf_devinfo_list_add(struct bladerf_devinfo_list *list,
struct bladerf_devinfo *info);
/**
* Read data from one-time-programmabe (OTP) section of flash
*
* @param[in] dev Device handle
* @param[in] field OTP field
* @param[out] data Populated with retrieved data
* @param[in] data_size Size of the data to read
*
* 0 on success, BLADERF_ERR_* on failure
*/
int bladerf_get_otp_field(struct bladerf *device, char *field,
char *data, size_t data_size);
/**
* Read data from calibration ("cal") section of flash
*
* @param[in] dev Device handle
* @param[in] field Cal field
* @param[out] data Populated with retrieved data
* @param[in] data_size Size of the data to read
*
* 0 on success, BLADERF_ERR_* on failure
*/
int bladerf_get_otp_field(struct bladerf *device, char *field,
char *data, size_t data_size);
/**
* Retrieve the device serial from flash
*
* @param[inout] dev Device handle. On success, serial field is updated
*
* 0 on success, BLADERF_ERR_* on failure
*/
int bladerf_get_serial_nocache(struct bladerf *device);
/**
* Retrieve VCTCXO calibration value from flash
*
* @param[inout] dev Device handle. On success, trim field is updated
*
* 0 on success, BLADERF_ERR_* on failure
*/
int bladerf_get_vctcxo_trim_nocache(struct bladerf *device);
/**
* Retrieve FPGA size variant from flash
*
* @param[inout] dev Device handle.
* On success, fpga_size field is updated
*
* 0 on success, BLADERF_ERR_* on failure
*/
int bladerf_get_fpga_size_nocache(struct bladerf *device);
#endif

View file

@ -6,8 +6,8 @@ int cmd_version(struct cli_state *state, int argc, char **argv)
int status;
unsigned int fw_major, fw_minor;
unsigned int fpga_major, fpga_minor;
char serial[40];
int variant;
char serial[BLADERF_SERIAL_LENGTH] = { 0 };
bladerf_fpga_size fpga_size;
uint16_t dac_trim;
bool fpga_loaded = false;
@ -33,7 +33,7 @@ int cmd_version(struct cli_state *state, int argc, char **argv)
return status;
}
status = bladerf_get_fpga_size(state->dev, &variant);
status = bladerf_get_fpga_size(state->dev, &fpga_size);
if (status < 0) {
return status;
}
@ -44,9 +44,9 @@ int cmd_version(struct cli_state *state, int argc, char **argv)
}
printf("\nbladeRF serial: %s\n", serial);
printf("\nSerial #: %s\n", serial);
printf("VCTCXO DAC calibration: 0x%.4x\n", dac_trim);
printf("bladeRF FPGA size: %d KLE\n", variant);
printf("FPGA size: %d KLE\n", fpga_size);
printf("Firmware version: %u.%u\n", fw_major, fw_minor);
if (fpga_loaded) {
printf("FPGA version: %u.%u\n", fpga_major, fpga_minor);