@@ -115,6 +115,7 @@ typedef enum {
115115 HACKRF_VENDOR_REQUEST_TEST_RTC_OSC = 58 ,
116116 HACKRF_VENDOR_REQUEST_RADIO_WRITE_REG = 59 ,
117117 HACKRF_VENDOR_REQUEST_RADIO_READ_REG = 60 ,
118+ HACKRF_VENDOR_REQUEST_GET_BUFFER_SIZE = 61 ,
118119} hackrf_vendor_request ;
119120
120121#define USB_CONFIG_STANDARD 0x1
@@ -138,7 +139,6 @@ typedef enum {
138139
139140#define TRANSFER_COUNT 4
140141#define TRANSFER_BUFFER_SIZE 262144
141- #define DEVICE_BUFFER_SIZE 32768
142142#define USB_MAX_SERIAL_LENGTH 32
143143
144144struct hackrf_device {
@@ -163,6 +163,7 @@ struct hackrf_device {
163163 hackrf_flush_cb_fn flush_callback ;
164164 hackrf_tx_block_complete_cb_fn tx_completion_callback ;
165165 void * flush_ctx ;
166+ uint32_t buffer_size ;
166167};
167168
168169typedef struct {
@@ -712,6 +713,7 @@ static int hackrf_open_setup(libusb_device_handle* usb_device, hackrf_device** d
712713{
713714 int result ;
714715 hackrf_device * lib_device ;
716+ uint32_t buffer_size ;
715717 struct libusb_device_descriptor device_descriptor ;
716718 libusb_device * dev = libusb_get_device (usb_device );
717719 result = libusb_get_device_descriptor (dev , & device_descriptor );
@@ -765,6 +767,30 @@ static int hackrf_open_setup(libusb_device_handle* usb_device, hackrf_device** d
765767 lib_device -> flush_ctx = NULL ;
766768 lib_device -> tx_completion_callback = NULL ;
767769
770+ if (lib_device -> usb_api_version >= 0x0112 ) {
771+ // Fetch buffer size from device so we know how many bytes to flush TX with.
772+ result = libusb_control_transfer (
773+ lib_device -> usb_device ,
774+ LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR |
775+ LIBUSB_RECIPIENT_DEVICE ,
776+ HACKRF_VENDOR_REQUEST_GET_BUFFER_SIZE ,
777+ 0 ,
778+ 0 ,
779+ (unsigned char * ) & buffer_size ,
780+ 4 ,
781+ 0 );
782+
783+ if (result < 4 ) {
784+ last_libusb_error = result ;
785+ return HACKRF_ERROR_LIBUSB ;
786+ }
787+
788+ lib_device -> buffer_size = FROM_LE32 (buffer_size );
789+ } else {
790+ // All older firmware uses a fixed 32KB buffer size.
791+ lib_device -> buffer_size = 32768 ;
792+ }
793+
768794 result = pthread_mutex_init (& lib_device -> transfer_lock , NULL );
769795 if (result != 0 ) {
770796 free (lib_device );
@@ -2371,8 +2397,8 @@ ADDAPI int ADDCALL hackrf_enable_tx_flush(
23712397 device -> flush_transfer ,
23722398 device -> usb_device ,
23732399 TX_ENDPOINT_ADDRESS ,
2374- calloc (1 , DEVICE_BUFFER_SIZE ),
2375- DEVICE_BUFFER_SIZE ,
2400+ calloc (1 , device -> buffer_size ),
2401+ device -> buffer_size ,
23762402 hackrf_libusb_flush_callback ,
23772403 device ,
23782404 0 );
0 commit comments