From b6099569e31705174920f44083f7f1bc22c445c7 Mon Sep 17 00:00:00 2001 From: Thomas Tsou Date: Fri, 27 Aug 2010 13:27:32 -0700 Subject: usrp1: Cleanup libusb device handling This patch limits all libusb device enumeration operations to FSF (Vendor ID = 0xfffe) devices, which removes a lot of unncessary libusb output when debug mode is enabled. The reference counts held by the libusb device list are also reduced, which prevents holding references to unused devices. --- host/lib/transport/libusb1_base.cpp | 50 +++++++++++++++++++++++----- host/lib/transport/libusb1_base.hpp | 2 ++ host/lib/transport/libusb1_device_handle.cpp | 31 ++++------------- 3 files changed, 50 insertions(+), 33 deletions(-) (limited to 'host') diff --git a/host/lib/transport/libusb1_base.cpp b/host/lib/transport/libusb1_base.cpp index 6965de214..493d4eff3 100644 --- a/host/lib/transport/libusb1_base.cpp +++ b/host/lib/transport/libusb1_base.cpp @@ -16,10 +16,22 @@ // #include "libusb1_base.hpp" +#include #include using namespace uhd::transport; +bool check_fsf_device(libusb_device *dev) +{ + libusb_device_descriptor desc; + + if (libusb_get_device_descriptor(dev, &desc) < 0) { + UHD_ASSERT_THROW("USB: failed to get device descriptor"); + } + + return desc.idVendor == 0xfffe; +} + void libusb::init(libusb_context **ctx, int debug_level) { if (libusb_init(ctx) < 0) @@ -28,27 +40,47 @@ void libusb::init(libusb_context **ctx, int debug_level) libusb_set_debug(*ctx, debug_level); } +std::vector libusb::get_fsf_device_list(libusb_context *ctx) +{ + libusb_device **libusb_dev_list; + std::vector fsf_dev_list; + + ssize_t dev_cnt = libusb_get_device_list(ctx, &libusb_dev_list); + + //find the FSF devices + for (ssize_t i = 0; i < dev_cnt; i++) { + libusb_device *dev = libusb_dev_list[i]; + + if (check_fsf_device(dev)) + fsf_dev_list.push_back(dev); + else + libusb_unref_device(dev); + } + + libusb_free_device_list(libusb_dev_list, 0); + + return fsf_dev_list; +} libusb_device_handle *libusb::open_device(libusb_context *ctx, usb_device_handle::sptr handle) { - libusb_device **dev_list; - libusb_device_handle *dev_handle; + libusb_device_handle *dev_handle = NULL; + std::vector fsf_dev_list = get_fsf_device_list(ctx); - ssize_t dev_cnt = libusb_get_device_list(ctx, &dev_list); - - //find and open the receive device - for (ssize_t i = 0; i < dev_cnt; i++) { - libusb_device *dev = dev_list[i]; + //find and open the USB device + for (size_t i = 0; i < fsf_dev_list.size(); i++) { + libusb_device *dev = fsf_dev_list[i]; if (compare_device(dev, handle)) { libusb_open(dev, &dev_handle); + libusb_unref_device(dev); break; } + + libusb_unref_device(dev); } - libusb_free_device_list(dev_list, 0); - return dev_handle; } diff --git a/host/lib/transport/libusb1_base.hpp b/host/lib/transport/libusb1_base.hpp index ae560cd52..708a42c73 100644 --- a/host/lib/transport/libusb1_base.hpp +++ b/host/lib/transport/libusb1_base.hpp @@ -27,6 +27,8 @@ namespace uhd { namespace transport { namespace libusb { void init(libusb_context **ctx, int debug_level); + std::vector get_fsf_device_list(libusb_context *ctx); + libusb_device_handle *open_device(libusb_context *ctx, usb_device_handle::sptr handle); diff --git a/host/lib/transport/libusb1_device_handle.cpp b/host/lib/transport/libusb1_device_handle.cpp index 5289f668f..4885099eb 100644 --- a/host/lib/transport/libusb1_device_handle.cpp +++ b/host/lib/transport/libusb1_device_handle.cpp @@ -88,38 +88,21 @@ usb_device_handle::sptr make_usb_device_handle(libusb_device *dev) device_addr)); } -bool check_fsf_device(libusb_device *dev) -{ - libusb_device_descriptor desc; - - if (libusb_get_device_descriptor(dev, &desc) < 0) { - UHD_ASSERT_THROW("USB: failed to get device descriptor"); - } - - return desc.idVendor == 0xfffe; -} - std::vector usb_device_handle::get_device_list() { libusb_context *ctx = NULL; - libusb_device **list; - std::vector device_list; + std::vector libusb_device_list; + std::vector device_handle_list; libusb::init(&ctx, libusb_debug_level); - ssize_t cnt = libusb_get_device_list(ctx, &list); - - if (cnt < 0) - throw std::runtime_error("USB: enumeration failed"); + libusb_device_list = libusb::get_fsf_device_list(ctx); - ssize_t i = 0; - for (i = 0; i < cnt; i++) { - libusb_device *dev = list[i]; - if (check_fsf_device(dev)) - device_list.push_back(make_usb_device_handle(dev)); + for (size_t i = 0; i < libusb_device_list.size(); i++) { + libusb_device *dev = libusb_device_list[i]; + device_handle_list.push_back(make_usb_device_handle(dev)); } - libusb_free_device_list(list, 0); libusb_exit(ctx); - return device_list; + return device_handle_list; } -- cgit v1.2.3 From 72fc20bc8ba4cb636a04de78210aac483f4ffaf3 Mon Sep 17 00:00:00 2001 From: Thomas Tsou Date: Fri, 27 Aug 2010 13:46:22 -0700 Subject: usrp1: Disable default debug output for libusb implementations --- host/lib/transport/libusb1_control.cpp | 2 +- host/lib/transport/libusb1_device_handle.cpp | 2 +- host/lib/transport/libusb1_zero_copy.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'host') diff --git a/host/lib/transport/libusb1_control.cpp b/host/lib/transport/libusb1_control.cpp index 8bf271256..4b827c350 100644 --- a/host/lib/transport/libusb1_control.cpp +++ b/host/lib/transport/libusb1_control.cpp @@ -20,7 +20,7 @@ using namespace uhd::transport; -const int libusb_debug_level = 3; +const int libusb_debug_level = 0; const int libusb_timeout = 0; /*********************************************************************** diff --git a/host/lib/transport/libusb1_device_handle.cpp b/host/lib/transport/libusb1_device_handle.cpp index 4885099eb..3476fdc4e 100644 --- a/host/lib/transport/libusb1_device_handle.cpp +++ b/host/lib/transport/libusb1_device_handle.cpp @@ -20,7 +20,7 @@ using namespace uhd::transport; -const int libusb_debug_level = 3; +const int libusb_debug_level = 0; class libusb1_device_handle_impl : public usb_device_handle { public: diff --git a/host/lib/transport/libusb1_zero_copy.cpp b/host/lib/transport/libusb1_zero_copy.cpp index 55aa10cbb..2149625f9 100644 --- a/host/lib/transport/libusb1_zero_copy.cpp +++ b/host/lib/transport/libusb1_zero_copy.cpp @@ -25,7 +25,7 @@ using namespace uhd::transport; -const int libusb_debug_level = 3; +const int libusb_debug_level = 0; const int libusb_timeout = 0; /*********************************************************************** -- cgit v1.2.3 From 7f8c73faa22aaf2a381193d9f460857785e45023 Mon Sep 17 00:00:00 2001 From: Thomas Tsou Date: Fri, 27 Aug 2010 15:59:05 -0700 Subject: usrp1: Handle degenerate managed send buffer cases Handle degenerate usage of send buffer commits. If the buffer is destroyed without ever being submitted, submit a zero byte transfer to return control to the underlying structure. If a committed buffer is re-committed, then report an error message and return 0 bytes back. --- host/lib/transport/libusb1_zero_copy.cpp | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'host') diff --git a/host/lib/transport/libusb1_zero_copy.cpp b/host/lib/transport/libusb1_zero_copy.cpp index 2149625f9..4469991b8 100644 --- a/host/lib/transport/libusb1_zero_copy.cpp +++ b/host/lib/transport/libusb1_zero_copy.cpp @@ -555,7 +555,7 @@ public: libusb_managed_send_buffer_impl(libusb_transfer *lut, usb_endpoint *endpoint, size_t buff_size) - : _buff(lut->buffer, buff_size) + : _buff(lut->buffer, buff_size), _committed(false) { _lut = lut; _endpoint = endpoint; @@ -563,18 +563,32 @@ public: ~libusb_managed_send_buffer_impl() { - /* NOP */ + if (!_committed) { + _lut->length = 0; + _lut->actual_length = 0; + _endpoint->submit(_lut); + } } ssize_t commit(size_t num_bytes) { + if (_committed) { + std::cerr << "UHD: send buffer already committed" << std::endl; + return 0; + } + + UHD_ASSERT_THROW(num_bytes <= boost::asio::buffer_size(_buff)); + _lut->length = num_bytes; _lut->actual_length = 0; - if (_endpoint->submit(_lut)) + if (_endpoint->submit(_lut)) { + _committed = true; return num_bytes; - else + } + else { return 0; + } } private: @@ -586,6 +600,7 @@ private: libusb_transfer *_lut; usb_endpoint *_endpoint; const boost::asio::mutable_buffer _buff; + bool _committed; }; -- cgit v1.2.3 From ec8005e3d5d1a80f89f5cfbaa8e1cdadf2d14db6 Mon Sep 17 00:00:00 2001 From: Thomas Tsou Date: Fri, 27 Aug 2010 16:20:15 -0700 Subject: usrp1: Wait for USB device renumeration only when necessary Waiting for FX2 renumeration is only needed after a firmware load so move it out of the main device discovery loop and into the firmware load itself. --- host/lib/usrp/usrp1/usrp1_ctrl.cpp | 4 ++++ host/lib/usrp/usrp1/usrp1_impl.cpp | 3 --- 2 files changed, 4 insertions(+), 3 deletions(-) (limited to 'host') diff --git a/host/lib/usrp/usrp1/usrp1_ctrl.cpp b/host/lib/usrp/usrp1/usrp1_ctrl.cpp index b7c6bdaf6..98226b738 100644 --- a/host/lib/usrp/usrp1/usrp1_ctrl.cpp +++ b/host/lib/usrp/usrp1/usrp1_ctrl.cpp @@ -19,6 +19,7 @@ #include "usrp_commands.h" #include #include +#include #include #include #include @@ -209,6 +210,9 @@ public: usrp_set_firmware_hash(hash); file.close(); + //wait for things to settle + boost::this_thread::sleep(boost::posix_time::milliseconds(1000)); + return USRP_FIRMWARE_LOAD_SUCCESS; } //type anything else is unhandled diff --git a/host/lib/usrp/usrp1/usrp1_impl.cpp b/host/lib/usrp/usrp1/usrp1_impl.cpp index d37eec566..3c3306525 100644 --- a/host/lib/usrp/usrp1/usrp1_impl.cpp +++ b/host/lib/usrp/usrp1/usrp1_impl.cpp @@ -69,9 +69,6 @@ static device_addrs_t usrp1_find(const device_addr_t &hint) } } - //wait for things to settle - boost::this_thread::sleep(boost::posix_time::milliseconds(1000)); - //get descriptors again with serial number device_list = usb_device_handle::get_device_list(); -- cgit v1.2.3