Skip to content

Commit 5daed4d

Browse files
committed
Read USB API version in hackrf_open() and cache it.
1 parent 04b8c7e commit 5daed4d

1 file changed

Lines changed: 12 additions & 15 deletions

File tree

host/libhackrf/src/hackrf.c

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ typedef enum {
139139

140140
struct hackrf_device {
141141
libusb_device_handle* usb_device;
142+
uint16_t usb_api_version;
142143
struct libusb_transfer** transfers;
143144
hackrf_sample_block_cb_fn callback;
144145
volatile bool
@@ -183,10 +184,8 @@ static const max2837_ft_t max2837_ft[] = {
183184
{28000000},
184185
{0}};
185186

186-
#define USB_API_REQUIRED(device, version) \
187-
uint16_t usb_version = 0; \
188-
hackrf_usb_api_version_read(device, &usb_version); \
189-
if (usb_version < version) \
187+
#define USB_API_REQUIRED(device, version) \
188+
if (device->usb_api_version < version) \
190189
return HACKRF_ERROR_USB_API_VERSION;
191190

192191
static const uint16_t hackrf_usb_vid = 0x1d50;
@@ -709,6 +708,13 @@ static int hackrf_open_setup(libusb_device_handle* usb_device, hackrf_device** d
709708
{
710709
int result;
711710
hackrf_device* lib_device;
711+
struct libusb_device_descriptor device_descriptor;
712+
libusb_device* dev = libusb_get_device(usb_device);
713+
result = libusb_get_device_descriptor(dev, &device_descriptor);
714+
if (result < 0) {
715+
last_libusb_error = result;
716+
return HACKRF_ERROR_LIBUSB;
717+
}
712718

713719
//int speed = libusb_get_device_speed(usb_device);
714720
// TODO: Error or warning if not high speed USB?
@@ -742,6 +748,7 @@ static int hackrf_open_setup(libusb_device_handle* usb_device, hackrf_device** d
742748
#endif
743749

744750
lib_device->usb_device = usb_device;
751+
lib_device->usb_api_version = device_descriptor.bcdDevice;
745752
lib_device->transfers = NULL;
746753
lib_device->callback = NULL;
747754
lib_device->transfer_thread_started = false;
@@ -1672,17 +1679,7 @@ extern ADDAPI int ADDCALL hackrf_usb_api_version_read(
16721679
hackrf_device* device,
16731680
uint16_t* version)
16741681
{
1675-
int result;
1676-
libusb_device* dev;
1677-
struct libusb_device_descriptor desc;
1678-
dev = libusb_get_device(device->usb_device);
1679-
result = libusb_get_device_descriptor(dev, &desc);
1680-
if (result < 0) {
1681-
last_libusb_error = result;
1682-
return HACKRF_ERROR_LIBUSB;
1683-
}
1684-
1685-
*version = desc.bcdDevice;
1682+
*version = device->usb_api_version;
16861683
return HACKRF_SUCCESS;
16871684
}
16881685

0 commit comments

Comments
 (0)