@@ -111,6 +111,7 @@ typedef enum {
111111 HACKRF_VENDOR_REQUEST_READ_SELFTEST = 56 ,
112112 HACKRF_VENDOR_REQUEST_READ_ADC = 57 ,
113113 HACKRF_VENDOR_REQUEST_TEST_RTC_OSC = 58 ,
114+ HACKRF_VENDOR_REQUEST_GET_BUFFER_SIZE = 59 ,
114115} hackrf_vendor_request ;
115116
116117#define USB_CONFIG_STANDARD 0x1
@@ -134,7 +135,6 @@ typedef enum {
134135
135136#define TRANSFER_COUNT 4
136137#define TRANSFER_BUFFER_SIZE 262144
137- #define DEVICE_BUFFER_SIZE 32768
138138#define USB_MAX_SERIAL_LENGTH 32
139139
140140struct hackrf_device {
@@ -159,6 +159,7 @@ struct hackrf_device {
159159 hackrf_flush_cb_fn flush_callback ;
160160 hackrf_tx_block_complete_cb_fn tx_completion_callback ;
161161 void * flush_ctx ;
162+ uint32_t buffer_size ;
162163};
163164
164165typedef struct {
@@ -708,6 +709,7 @@ static int hackrf_open_setup(libusb_device_handle* usb_device, hackrf_device** d
708709{
709710 int result ;
710711 hackrf_device * lib_device ;
712+ uint32_t buffer_size ;
711713 struct libusb_device_descriptor device_descriptor ;
712714 libusb_device * dev = libusb_get_device (usb_device );
713715 result = libusb_get_device_descriptor (dev , & device_descriptor );
@@ -761,6 +763,30 @@ static int hackrf_open_setup(libusb_device_handle* usb_device, hackrf_device** d
761763 lib_device -> flush_ctx = NULL ;
762764 lib_device -> tx_completion_callback = NULL ;
763765
766+ if (lib_device -> usb_api_version >= 0x0110 ) {
767+ // Fetch buffer size from device so we know how many bytes to flush TX with.
768+ result = libusb_control_transfer (
769+ lib_device -> usb_device ,
770+ LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR |
771+ LIBUSB_RECIPIENT_DEVICE ,
772+ HACKRF_VENDOR_REQUEST_GET_BUFFER_SIZE ,
773+ 0 ,
774+ 0 ,
775+ (unsigned char * ) & buffer_size ,
776+ 4 ,
777+ 0 );
778+
779+ if (result < 4 ) {
780+ last_libusb_error = result ;
781+ return HACKRF_ERROR_LIBUSB ;
782+ }
783+
784+ lib_device -> buffer_size = FROM_LE32 (buffer_size );
785+ } else {
786+ // All older firmware uses a fixed 32KB buffer size.
787+ lib_device -> buffer_size = 32768 ;
788+ }
789+
764790 result = pthread_mutex_init (& lib_device -> transfer_lock , NULL );
765791 if (result != 0 ) {
766792 free (lib_device );
@@ -2343,8 +2369,8 @@ ADDAPI int ADDCALL hackrf_enable_tx_flush(
23432369 device -> flush_transfer ,
23442370 device -> usb_device ,
23452371 TX_ENDPOINT_ADDRESS ,
2346- calloc (1 , DEVICE_BUFFER_SIZE ),
2347- DEVICE_BUFFER_SIZE ,
2372+ calloc (1 , device -> buffer_size ),
2373+ device -> buffer_size ,
23482374 hackrf_libusb_flush_callback ,
23492375 device ,
23502376 0 );
0 commit comments