diff --git a/host/hackrf-tools/src/hackrf_info.c b/host/hackrf-tools/src/hackrf_info.c index 68c95938c..d138d5358 100644 --- a/host/hackrf-tools/src/hackrf_info.c +++ b/host/hackrf-tools/src/hackrf_info.c @@ -254,6 +254,23 @@ int main(void) hackrf_error_name(result), result); } + + result = hackrf_device_list_bus_sharing(list, i); + if (result < 0) { + fprintf(stderr, + "hackrf_device_list_bus_sharing() failed: %s (%d)\n", + hackrf_error_name(result), + result); + } else if (result == 0) { + printf("This device is on its own USB bus.\n"); + } else if (result == 1) { + printf("There is 1 other device on the same USB bus.\n"); + printf("You may have problems at high sample rates.\n"); + } else { + printf("There are %d other devices on the same USB bus.\n", + result); + printf("You may have problems at high sample rates.\n"); + } } hackrf_device_list_free(list); diff --git a/host/libhackrf/src/hackrf.c b/host/libhackrf/src/hackrf.c index 728f8961b..7d6b34cd6 100644 --- a/host/libhackrf/src/hackrf.c +++ b/host/libhackrf/src/hackrf.c @@ -860,6 +860,30 @@ int ADDCALL hackrf_device_list_open( return hackrf_open_setup(usb_device, device); } +int ADDCALL hackrf_device_list_bus_sharing(hackrf_device_list_t* list, int idx) +{ + libusb_device *usb_dev, *hackrf_dev; + uint8_t hackrf_bus; + int other_device_count = 0; + int i; + if (list == NULL || list->usb_devices == NULL || list->usb_device_index == NULL || + idx < 0 || idx > list->devicecount) { + return HACKRF_ERROR_INVALID_PARAM; + } + hackrf_dev = list->usb_devices[list->usb_device_index[idx]]; + hackrf_bus = libusb_get_bus_number(hackrf_dev); + for (i = 0; i < list->usb_devicecount; i++) { + usb_dev = (libusb_device*) list->usb_devices[i]; + // Don't count the HackRF, devices on other buses, or the root hub. + if (usb_dev != hackrf_dev && + libusb_get_bus_number(usb_dev) == hackrf_bus && + libusb_get_parent(usb_dev) != NULL) { + other_device_count++; + } + } + return other_device_count; +} + int ADDCALL hackrf_set_transceiver_mode( hackrf_device* device, hackrf_transceiver_mode value) diff --git a/host/libhackrf/src/hackrf.h b/host/libhackrf/src/hackrf.h index ecfc9569a..3908a4d1c 100644 --- a/host/libhackrf/src/hackrf.h +++ b/host/libhackrf/src/hackrf.h @@ -1106,6 +1106,18 @@ extern ADDAPI int ADDCALL hackrf_device_list_open( int idx, hackrf_device** device); +/** + * Check if a listed HackRF device is sharing its USB bus with other devices. + * + * @param[in] list device list to query + * @param[in] idx index of the HackRF device in the list + * @return The number of devices sharing the USB bus with the specified HackRF, or a negative error code from @ref hackrf_error + * + */ +extern ADDAPI int ADDCALL hackrf_device_list_bus_sharing( + hackrf_device_list_t* list, + int idx); + /** * Free a previously allocated @ref hackrf_device_list list. * @param[in] list list to free