Revision fe7df530
| b/host/lib/transport/CMakeLists.txt | ||
|---|---|---|
| 29 | 29 |
LIBUHD_APPEND_SOURCES( |
| 30 | 30 |
${CMAKE_SOURCE_DIR}/lib/transport/libusb1_control.cpp
|
| 31 | 31 |
${CMAKE_SOURCE_DIR}/lib/transport/libusb1_zero_copy.cpp
|
| 32 |
${CMAKE_SOURCE_DIR}/lib/transport/libusb1_base.cpp
|
|
| 33 |
${CMAKE_SOURCE_DIR}/lib/transport/libusb1_device_handle.cpp
|
|
| 32 | 34 |
) |
| 33 | 35 |
SET(HAVE_USB_SUPPORT TRUE) |
| 34 | 36 |
ENDIF(LIBUSB_FOUND) |
| b/host/lib/transport/libusb1_base.cpp | ||
|---|---|---|
| 1 |
// |
|
| 2 |
// Copyright 2010 Ettus Research LLC |
|
| 3 |
// |
|
| 4 |
// This program is free software: you can redistribute it and/or modify |
|
| 5 |
// it under the terms of the GNU General Public License as published by |
|
| 6 |
// the Free Software Foundation, either version 3 of the License, or |
|
| 7 |
// (at your option) any later version. |
|
| 8 |
// |
|
| 9 |
// This program is distributed in the hope that it will be useful, |
|
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 12 |
// GNU General Public License for more details. |
|
| 13 |
// |
|
| 14 |
// You should have received a copy of the GNU General Public License |
|
| 15 |
// along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
| 16 |
// |
|
| 17 |
|
|
| 18 |
#include "libusb1_base.hpp" |
|
| 19 |
#include <iostream> |
|
| 20 |
|
|
| 21 |
using namespace uhd::transport; |
|
| 22 |
|
|
| 23 |
void libusb::init(libusb_context **ctx, int debug_level) |
|
| 24 |
{
|
|
| 25 |
if (libusb_init(ctx) < 0) |
|
| 26 |
std::cerr << "error: libusb_init" << std::endl; |
|
| 27 |
|
|
| 28 |
libusb_set_debug(*ctx, debug_level); |
|
| 29 |
} |
|
| 30 |
|
|
| 31 |
|
|
| 32 |
libusb_device_handle *libusb::open_device(libusb_context *ctx, |
|
| 33 |
usb_device_handle::sptr handle) |
|
| 34 |
{
|
|
| 35 |
libusb_device **dev_list; |
|
| 36 |
libusb_device_handle *dev_handle; |
|
| 37 |
|
|
| 38 |
ssize_t dev_cnt = libusb_get_device_list(ctx, &dev_list); |
|
| 39 |
|
|
| 40 |
//find and open the receive device |
|
| 41 |
for (ssize_t i = 0; i < dev_cnt; i++) {
|
|
| 42 |
libusb_device *dev = dev_list[i]; |
|
| 43 |
|
|
| 44 |
if (compare_device(dev, handle)) {
|
|
| 45 |
libusb_open(dev, &dev_handle); |
|
| 46 |
break; |
|
| 47 |
} |
|
| 48 |
} |
|
| 49 |
|
|
| 50 |
libusb_free_device_list(dev_list, 0); |
|
| 51 |
|
|
| 52 |
return dev_handle; |
|
| 53 |
} |
|
| 54 |
|
|
| 55 |
|
|
| 56 |
bool libusb::compare_device(libusb_device *dev, |
|
| 57 |
usb_device_handle::sptr handle) |
|
| 58 |
{
|
|
| 59 |
std::string serial = handle->get_serial(); |
|
| 60 |
boost::uint16_t vendor_id = handle->get_vendor_id(); |
|
| 61 |
boost::uint16_t product_id = handle->get_product_id(); |
|
| 62 |
boost::uint8_t device_addr = handle->get_device_addr(); |
|
| 63 |
|
|
| 64 |
libusb_device_descriptor libusb_desc; |
|
| 65 |
if (libusb_get_device_descriptor(dev, &libusb_desc) < 0) |
|
| 66 |
return false; |
|
| 67 |
if (serial != get_serial(dev)) |
|
| 68 |
return false; |
|
| 69 |
if (vendor_id != libusb_desc.idVendor) |
|
| 70 |
return false; |
|
| 71 |
if (product_id != libusb_desc.idProduct) |
|
| 72 |
return false; |
|
| 73 |
if (device_addr != libusb_get_device_address(dev)) |
|
| 74 |
return false; |
|
| 75 |
|
|
| 76 |
return true; |
|
| 77 |
} |
|
| 78 |
|
|
| 79 |
|
|
| 80 |
bool libusb::open_interface(libusb_device_handle *dev_handle, |
|
| 81 |
int interface) |
|
| 82 |
{
|
|
| 83 |
int ret = libusb_claim_interface(dev_handle, interface); |
|
| 84 |
if (ret < 0) {
|
|
| 85 |
std::cerr << "error: libusb_claim_interface() " << ret << std::endl; |
|
| 86 |
return false; |
|
| 87 |
} |
|
| 88 |
else {
|
|
| 89 |
return true; |
|
| 90 |
} |
|
| 91 |
} |
|
| 92 |
|
|
| 93 |
|
|
| 94 |
std::string libusb::get_serial(libusb_device *dev) |
|
| 95 |
{
|
|
| 96 |
unsigned char buff[32]; |
|
| 97 |
|
|
| 98 |
libusb_device_descriptor desc; |
|
| 99 |
if (libusb_get_device_descriptor(dev, &desc) < 0) |
|
| 100 |
return ""; |
|
| 101 |
|
|
| 102 |
if (desc.iSerialNumber == 0) |
|
| 103 |
return ""; |
|
| 104 |
|
|
| 105 |
//open the device because we have to |
|
| 106 |
libusb_device_handle *dev_handle; |
|
| 107 |
if (libusb_open(dev, &dev_handle) < 0) |
|
| 108 |
return ""; |
|
| 109 |
|
|
| 110 |
if (libusb_get_string_descriptor_ascii(dev_handle, desc.iSerialNumber, |
|
| 111 |
buff, sizeof(buff)) < 0) {
|
|
| 112 |
return ""; |
|
| 113 |
} |
|
| 114 |
|
|
| 115 |
libusb_close(dev_handle); |
|
| 116 |
|
|
| 117 |
return (char*) buff; |
|
| 118 |
} |
|
| b/host/lib/transport/libusb1_base.hpp | ||
|---|---|---|
| 1 |
// |
|
| 2 |
// Copyright 2010 Ettus Research LLC |
|
| 3 |
// |
|
| 4 |
// This program is free software: you can redistribute it and/or modify |
|
| 5 |
// it under the terms of the GNU General Public License as published by |
|
| 6 |
// the Free Software Foundation, either version 3 of the License, or |
|
| 7 |
// (at your option) any later version. |
|
| 8 |
// |
|
| 9 |
// This program is distributed in the hope that it will be useful, |
|
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 12 |
// GNU General Public License for more details. |
|
| 13 |
// |
|
| 14 |
// You should have received a copy of the GNU General Public License |
|
| 15 |
// along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
| 16 |
// |
|
| 17 |
|
|
| 18 |
#ifndef INCLUDED_TRANSPORT_LIBUSB_HPP |
|
| 19 |
#define INCLUDED_TRANSPORT_LIBUSB_HPP |
|
| 20 |
|
|
| 21 |
#include <uhd/config.hpp> |
|
| 22 |
#include <uhd/transport/usb_device_handle.hpp> |
|
| 23 |
#include <libusb-1.0/libusb.h> |
|
| 24 |
|
|
| 25 |
namespace uhd { namespace transport {
|
|
| 26 |
|
|
| 27 |
namespace libusb {
|
|
| 28 |
void init(libusb_context **ctx, int debug_level); |
|
| 29 |
|
|
| 30 |
libusb_device_handle *open_device(libusb_context *ctx, |
|
| 31 |
usb_device_handle::sptr handle); |
|
| 32 |
|
|
| 33 |
bool compare_device(libusb_device *dev, usb_device_handle::sptr handle); |
|
| 34 |
|
|
| 35 |
bool open_interface(libusb_device_handle *dev_handle, int interface); |
|
| 36 |
|
|
| 37 |
std::string get_serial(libusb_device *dev); |
|
| 38 |
} |
|
| 39 |
|
|
| 40 |
}} //namespace |
|
| 41 |
|
|
| 42 |
#endif /* INCLUDED_TRANSPORT_LIBUSB_HPP */ |
|
| b/host/lib/transport/libusb1_control.cpp | ||
|---|---|---|
| 15 | 15 |
// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | 16 |
// |
| 17 | 17 |
|
| 18 |
#include <uhd/types/usb_descriptor.hpp>
|
|
| 18 |
#include "libusb1_base.hpp"
|
|
| 19 | 19 |
#include <uhd/transport/usb_control.hpp> |
| 20 |
#include <libusb-1.0/libusb.h> |
|
| 21 |
#include <boost/asio.hpp> |
|
| 22 |
#include <stdexcept> |
|
| 23 |
#include <iostream> |
|
| 24 | 20 |
|
| 25 | 21 |
using namespace uhd::transport; |
| 26 | 22 |
|
| 27 |
static int libusb_debug_level = 0;
|
|
| 28 |
static int libusb_timeout = 0;
|
|
| 23 |
const int libusb_debug_level = 3;
|
|
| 24 |
const int libusb_timeout = 0;
|
|
| 29 | 25 |
|
| 30 | 26 |
/*********************************************************************** |
| 31 | 27 |
* libusb-1.0 implementation of USB control transport |
| 32 | 28 |
**********************************************************************/ |
| 33 | 29 |
class libusb_control_impl : public usb_control {
|
| 34 | 30 |
public: |
| 35 |
libusb_control_impl(uhd::usb_descriptor_t descriptor);
|
|
| 31 |
libusb_control_impl(usb_device_handle::sptr handle);
|
|
| 36 | 32 |
~libusb_control_impl(); |
| 37 | 33 |
|
| 38 | 34 |
size_t submit(boost::uint8_t request_type, |
| ... | ... | |
| 42 | 38 |
unsigned char *buff, |
| 43 | 39 |
boost::uint16_t length); |
| 44 | 40 |
|
| 45 |
static uhd::usb_descriptor_t create_descriptor(libusb_device *dev); |
|
| 46 |
static std::string get_serial(libusb_device *dev); |
|
| 47 |
|
|
| 48 | 41 |
private: |
| 49 |
uhd::usb_descriptor_t _descriptor; |
|
| 50 |
|
|
| 51 | 42 |
libusb_context *_ctx; |
| 52 | 43 |
libusb_device_handle *_dev_handle; |
| 53 |
|
|
| 54 |
bool open_device(); |
|
| 55 |
bool open_interface(); |
|
| 56 |
bool compare_device(libusb_device *dev, uhd::usb_descriptor_t descriptor); |
|
| 57 | 44 |
}; |
| 58 | 45 |
|
| 59 | 46 |
|
| 60 |
libusb_control_impl::libusb_control_impl(uhd::usb_descriptor_t descriptor) |
|
| 61 |
: _descriptor(descriptor), _ctx(NULL), _dev_handle(NULL) |
|
| 47 |
libusb_control_impl::libusb_control_impl(usb_device_handle::sptr handle) |
|
| 62 | 48 |
{
|
| 63 |
if (libusb_init(&_ctx) < 0) |
|
| 64 |
throw std::runtime_error("USB: failed to initialize libusb");
|
|
| 49 |
libusb::init(&_ctx, libusb_debug_level); |
|
| 65 | 50 |
|
| 66 |
libusb_set_debug(_ctx, libusb_debug_level);
|
|
| 51 |
_dev_handle = libusb::open_device(_ctx, handle);
|
|
| 67 | 52 |
|
| 68 |
if (!open_device()) |
|
| 69 |
throw std::runtime_error("USB: failed to open device");
|
|
| 70 |
|
|
| 71 |
if (!open_interface()) |
|
| 72 |
throw std::runtime_error("USB: failed to open device interface");
|
|
| 53 |
libusb::open_interface(_dev_handle, 0); |
|
| 73 | 54 |
} |
| 74 | 55 |
|
| 75 | 56 |
|
| ... | ... | |
| 80 | 61 |
} |
| 81 | 62 |
|
| 82 | 63 |
|
| 83 |
uhd::usb_descriptor_t libusb_control_impl::create_descriptor(libusb_device *dev) |
|
| 84 |
{
|
|
| 85 |
libusb_device_descriptor desc; |
|
| 86 |
|
|
| 87 |
if (libusb_get_device_descriptor(dev, &desc) < 0) |
|
| 88 |
throw std::runtime_error("USB: failed to get device descriptor");
|
|
| 89 |
|
|
| 90 |
uhd::usb_descriptor_t descriptor; |
|
| 91 |
|
|
| 92 |
descriptor.serial = get_serial(dev); |
|
| 93 |
descriptor.product_id = desc.idProduct; |
|
| 94 |
descriptor.vendor_id = desc.idVendor; |
|
| 95 |
descriptor.device_addr = libusb_get_device_address(dev); |
|
| 96 |
|
|
| 97 |
return descriptor; |
|
| 98 |
} |
|
| 99 |
|
|
| 100 |
|
|
| 101 |
std::string libusb_control_impl::get_serial(libusb_device *dev) |
|
| 102 |
{
|
|
| 103 |
unsigned char buff[32]; |
|
| 104 |
|
|
| 105 |
libusb_device_descriptor desc; |
|
| 106 |
if (libusb_get_device_descriptor(dev, &desc) < 0) |
|
| 107 |
return ""; |
|
| 108 |
|
|
| 109 |
if (desc.iSerialNumber == 0) |
|
| 110 |
return ""; |
|
| 111 |
|
|
| 112 |
//open the device because we have to |
|
| 113 |
libusb_device_handle *dev_handle; |
|
| 114 |
if (libusb_open(dev, &dev_handle) < 0) |
|
| 115 |
return ""; |
|
| 116 |
|
|
| 117 |
if (libusb_get_string_descriptor_ascii(dev_handle, desc.iSerialNumber, |
|
| 118 |
buff, sizeof(buff)) < 0) {
|
|
| 119 |
return ""; |
|
| 120 |
} |
|
| 121 |
|
|
| 122 |
libusb_close(dev_handle); |
|
| 123 |
|
|
| 124 |
return (char*) buff; |
|
| 125 |
} |
|
| 126 |
|
|
| 127 |
|
|
| 128 |
bool libusb_control_impl::open_device() |
|
| 129 |
{
|
|
| 130 |
libusb_device **list; |
|
| 131 |
libusb_device *dev; |
|
| 132 |
|
|
| 133 |
ssize_t cnt = libusb_get_device_list(_ctx, &list); |
|
| 134 |
|
|
| 135 |
if (cnt < 0) |
|
| 136 |
return cnt; |
|
| 137 |
|
|
| 138 |
ssize_t i = 0; |
|
| 139 |
for (i = 0; i < cnt; i++) {
|
|
| 140 |
dev = list[i]; |
|
| 141 |
if (compare_device(dev, _descriptor)) |
|
| 142 |
goto found; |
|
| 143 |
} |
|
| 144 |
return false; |
|
| 145 |
|
|
| 146 |
found: |
|
| 147 |
int ret; |
|
| 148 |
if ((ret = libusb_open(dev, &_dev_handle)) < 0) |
|
| 149 |
return false; |
|
| 150 |
else |
|
| 151 |
return true; |
|
| 152 |
} |
|
| 153 |
|
|
| 154 |
|
|
| 155 |
bool libusb_control_impl::compare_device(libusb_device *dev, |
|
| 156 |
uhd::usb_descriptor_t descriptor) |
|
| 157 |
{
|
|
| 158 |
std::string serial = descriptor.serial; |
|
| 159 |
boost::uint16_t vendor_id = descriptor.vendor_id; |
|
| 160 |
boost::uint16_t product_id = descriptor.product_id; |
|
| 161 |
boost::uint8_t device_addr = descriptor.device_addr; |
|
| 162 |
|
|
| 163 |
libusb_device_descriptor libusb_desc; |
|
| 164 |
if (libusb_get_device_descriptor(dev, &libusb_desc) < 0) |
|
| 165 |
return false; |
|
| 166 |
if (serial != get_serial(dev)) |
|
| 167 |
return false; |
|
| 168 |
if (vendor_id != libusb_desc.idVendor) |
|
| 169 |
return false; |
|
| 170 |
if (product_id != libusb_desc.idProduct) |
|
| 171 |
return false; |
|
| 172 |
if (device_addr != libusb_get_device_address(dev)) |
|
| 173 |
return false; |
|
| 174 |
|
|
| 175 |
return true; |
|
| 176 |
} |
|
| 177 |
|
|
| 178 |
|
|
| 179 |
bool libusb_control_impl::open_interface() |
|
| 180 |
{
|
|
| 181 |
if (libusb_claim_interface(_dev_handle, 0) < 0) |
|
| 182 |
return false; |
|
| 183 |
else |
|
| 184 |
return true; |
|
| 185 |
} |
|
| 186 |
|
|
| 187 |
|
|
| 188 | 64 |
size_t libusb_control_impl::submit(boost::uint8_t request_type, |
| 189 | 65 |
boost::uint8_t request, |
| 190 | 66 |
boost::uint16_t value, |
| ... | ... | |
| 206 | 82 |
/*********************************************************************** |
| 207 | 83 |
* USB control public make functions |
| 208 | 84 |
**********************************************************************/ |
| 209 |
usb_control::sptr usb_control::make(uhd::usb_descriptor_t descriptor)
|
|
| 85 |
usb_control::sptr usb_control::make(usb_device_handle::sptr handle)
|
|
| 210 | 86 |
{
|
| 211 |
return sptr(new libusb_control_impl(descriptor));
|
|
| 87 |
return sptr(new libusb_control_impl(handle));
|
|
| 212 | 88 |
} |
| 213 |
|
|
| 214 |
uhd::usb_descriptors_t usb_control::get_device_list() |
|
| 215 |
{
|
|
| 216 |
libusb_device **list; |
|
| 217 |
uhd::usb_descriptors_t descriptors; |
|
| 218 |
|
|
| 219 |
if (libusb_init(NULL) < 0) |
|
| 220 |
throw std::runtime_error("USB: failed to initialize libusb");
|
|
| 221 |
|
|
| 222 |
ssize_t cnt = libusb_get_device_list(NULL, &list); |
|
| 223 |
|
|
| 224 |
if (cnt < 0) |
|
| 225 |
throw std::runtime_error("USB: failed to get device list");
|
|
| 226 |
|
|
| 227 |
ssize_t i = 0; |
|
| 228 |
for (i = 0; i < cnt; i++) {
|
|
| 229 |
libusb_device *dev = list[i]; |
|
| 230 |
descriptors.push_back(libusb_control_impl::create_descriptor(dev)); |
|
| 231 |
} |
|
| 232 |
|
|
| 233 |
libusb_free_device_list(list, 0); |
|
| 234 |
libusb_exit(NULL); |
|
| 235 |
return descriptors; |
|
| 236 |
} |
|
| 237 |
|
|
| 238 |
|
|
| b/host/lib/transport/libusb1_device_handle.cpp | ||
|---|---|---|
| 1 |
// |
|
| 2 |
// Copyright 2010 Ettus Research LLC |
|
| 3 |
// |
|
| 4 |
// This program is free software: you can redistribute it and/or modify |
|
| 5 |
// it under the terms of the GNU General Public License as published by |
|
| 6 |
// the Free Software Foundation, either version 3 of the License, or |
|
| 7 |
// (at your option) any later version. |
|
| 8 |
// |
|
| 9 |
// This program is distributed in the hope that it will be useful, |
|
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 12 |
// GNU General Public License for more details. |
|
| 13 |
// |
|
| 14 |
// You should have received a copy of the GNU General Public License |
|
| 15 |
// along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
| 16 |
// |
|
| 17 |
|
|
| 18 |
#include "libusb1_base.hpp" |
|
| 19 |
#include <uhd/utils/assert.hpp> |
|
| 20 |
|
|
| 21 |
using namespace uhd::transport; |
|
| 22 |
|
|
| 23 |
const int libusb_debug_level = 3; |
|
| 24 |
|
|
| 25 |
class libusb1_device_handle_impl : public usb_device_handle {
|
|
| 26 |
public: |
|
| 27 |
libusb1_device_handle_impl(std::string serial, |
|
| 28 |
boost::uint32_t product_id, |
|
| 29 |
boost::uint32_t vendor_id, |
|
| 30 |
boost::uint32_t device_addr) |
|
| 31 |
: _serial(serial), _product_id(product_id), |
|
| 32 |
_vendor_id(vendor_id), _device_addr(device_addr) |
|
| 33 |
{
|
|
| 34 |
/* NOP */ |
|
| 35 |
} |
|
| 36 |
|
|
| 37 |
~libusb1_device_handle_impl() |
|
| 38 |
{
|
|
| 39 |
/* NOP */ |
|
| 40 |
} |
|
| 41 |
|
|
| 42 |
std::string get_serial() const |
|
| 43 |
{
|
|
| 44 |
return _serial; |
|
| 45 |
} |
|
| 46 |
|
|
| 47 |
boost::uint16_t get_vendor_id() const |
|
| 48 |
{
|
|
| 49 |
return _vendor_id; |
|
| 50 |
} |
|
| 51 |
|
|
| 52 |
|
|
| 53 |
boost::uint16_t get_product_id() const |
|
| 54 |
{
|
|
| 55 |
return _product_id; |
|
| 56 |
} |
|
| 57 |
|
|
| 58 |
boost::uint16_t get_device_addr() const |
|
| 59 |
{
|
|
| 60 |
return _device_addr; |
|
| 61 |
} |
|
| 62 |
|
|
| 63 |
private: |
|
| 64 |
std::string _serial; |
|
| 65 |
boost::uint32_t _product_id; |
|
| 66 |
boost::uint32_t _vendor_id; |
|
| 67 |
boost::uint32_t _device_addr; |
|
| 68 |
}; |
|
| 69 |
|
|
| 70 |
|
|
| 71 |
usb_device_handle::sptr make_usb_device_handle(libusb_device *dev) |
|
| 72 |
{
|
|
| 73 |
libusb_device_descriptor desc; |
|
| 74 |
|
|
| 75 |
if (libusb_get_device_descriptor(dev, &desc) < 0) {
|
|
| 76 |
UHD_ASSERT_THROW("USB: failed to get device descriptor");
|
|
| 77 |
} |
|
| 78 |
|
|
| 79 |
std::string serial = libusb::get_serial(dev); |
|
| 80 |
boost::uint32_t product_id = desc.idProduct; |
|
| 81 |
boost::uint32_t vendor_id = desc.idVendor; |
|
| 82 |
boost::uint32_t device_addr = libusb_get_device_address(dev); |
|
| 83 |
|
|
| 84 |
return usb_device_handle::sptr(new libusb1_device_handle_impl( |
|
| 85 |
serial, |
|
| 86 |
product_id, |
|
| 87 |
vendor_id, |
|
| 88 |
device_addr)); |
|
| 89 |
} |
|
| 90 |
|
|
| 91 |
|
|
| 92 |
std::vector<usb_device_handle::sptr> usb_device_handle::get_device_list() |
|
| 93 |
{
|
|
| 94 |
libusb_context *ctx = NULL; |
|
| 95 |
libusb_device **list; |
|
| 96 |
std::vector<usb_device_handle::sptr> device_list; |
|
| 97 |
|
|
| 98 |
libusb::init(&ctx, libusb_debug_level); |
|
| 99 |
|
|
| 100 |
ssize_t cnt = libusb_get_device_list(ctx, &list); |
|
| 101 |
|
|
| 102 |
if (cnt < 0) |
|
| 103 |
throw std::runtime_error("USB: enumeration failed");
|
|
| 104 |
|
|
| 105 |
ssize_t i = 0; |
|
| 106 |
for (i = 0; i < cnt; i++) {
|
|
| 107 |
libusb_device *dev = list[i]; |
|
| 108 |
device_list.push_back(make_usb_device_handle(dev)); |
|
| 109 |
} |
|
| 110 |
|
|
| 111 |
libusb_free_device_list(list, 0); |
|
| 112 |
libusb_exit(ctx); |
|
| 113 |
return device_list; |
|
| 114 |
} |
|
| b/host/lib/transport/libusb1_zero_copy.cpp | ||
|---|---|---|
| 15 | 15 |
// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | 16 |
// |
| 17 | 17 |
|
| 18 |
|
|
| 18 |
#include "libusb1_base.hpp" |
|
| 19 | 19 |
#include <uhd/transport/usb_zero_copy.hpp> |
| 20 | 20 |
#include <uhd/utils/assert.hpp> |
| 21 |
#include <libusb-1.0/libusb.h> |
|
| 22 | 21 |
#include <boost/asio.hpp> |
| 23 | 22 |
#include <boost/format.hpp> |
| 24 | 23 |
#include <iostream> |
| ... | ... | |
| 26 | 25 |
|
| 27 | 26 |
using namespace uhd::transport; |
| 28 | 27 |
|
| 29 |
static int libusb_debug_level = 3; |
|
| 28 |
const int libusb_debug_level = 3; |
|
| 29 |
const int libusb_timeout = 0; |
|
| 30 | 30 |
|
| 31 | 31 |
/*********************************************************************** |
| 32 | 32 |
* Helper functions |
| ... | ... | |
| 347 | 347 |
std::cerr << "USB: transfer timed out" << std::endl; |
| 348 | 348 |
break; |
| 349 | 349 |
case LIBUSB_TRANSFER_STALL: |
| 350 |
std::cerr << "USB: halt condition detected (endpoint stalled)" << std::endl;
|
|
| 350 |
std::cerr << "USB: halt condition detected (stalled)" << std::endl; |
|
| 351 | 351 |
break; |
| 352 | 352 |
case LIBUSB_TRANSFER_ERROR: |
| 353 | 353 |
std::cerr << "USB: transfer failed" << std::endl; |
| ... | ... | |
| 606 | 606 |
libusb_device_handle *_rx_dev_handle; |
| 607 | 607 |
libusb_device_handle *_tx_dev_handle; |
| 608 | 608 |
|
| 609 |
int _rx_endpoint; |
|
| 610 |
int _tx_endpoint; |
|
| 611 |
|
|
| 612 | 609 |
size_t _recv_buff_size; |
| 613 | 610 |
size_t _send_buff_size; |
| 614 | 611 |
size_t _num_frames; |
| 615 | 612 |
|
| 616 |
bool open_device(uhd::usb_descriptor_t descriptor); |
|
| 617 |
bool open_interface(libusb_device_handle *dev_handle, int interface); |
|
| 618 |
bool compare_device(libusb_device *dev, uhd::usb_descriptor_t descriptor); |
|
| 619 |
|
|
| 620 |
std::string get_serial(libusb_device *dev); |
|
| 621 |
|
|
| 622 | 613 |
public: |
| 623 | 614 |
typedef boost::shared_ptr<libusb_zero_copy_impl> sptr; |
| 624 | 615 |
|
| 625 | 616 |
/* |
| 626 | 617 |
* Structors |
| 627 | 618 |
*/ |
| 628 |
libusb_zero_copy_impl(uhd::usb_descriptor_t descriptor,
|
|
| 619 |
libusb_zero_copy_impl(usb_device_handle::sptr handle,
|
|
| 629 | 620 |
unsigned int rx_endpoint, |
| 630 | 621 |
unsigned int tx_endpoint, |
| 631 | 622 |
size_t recv_buff_size, |
| ... | ... | |
| 641 | 632 |
}; |
| 642 | 633 |
|
| 643 | 634 |
|
| 644 |
libusb_zero_copy_impl::libusb_zero_copy_impl(uhd::usb_descriptor_t descriptor,
|
|
| 635 |
libusb_zero_copy_impl::libusb_zero_copy_impl(usb_device_handle::sptr handle,
|
|
| 645 | 636 |
unsigned int rx_endpoint, |
| 646 | 637 |
unsigned int tx_endpoint, |
| 647 | 638 |
size_t buff_size, |
| ... | ... | |
| 650 | 641 |
_recv_buff_size(block_size), _send_buff_size(block_size), |
| 651 | 642 |
_num_frames(buff_size / block_size) |
| 652 | 643 |
{
|
| 653 |
if (libusb_init(&_rx_ctx) < 0)
|
|
| 654 |
std::cerr << "error: libusb_init" << std::endl;
|
|
| 644 |
libusb::init(&_rx_ctx, libusb_debug_level);
|
|
| 645 |
libusb::init(&_tx_ctx, libusb_debug_level);
|
|
| 655 | 646 |
|
| 656 |
if (libusb_init(&_tx_ctx) < 0) |
|
| 657 |
std::cerr << "error: libusb_init" << std::endl; |
|
| 647 |
UHD_ASSERT_THROW((_rx_ctx != NULL) && (_tx_ctx != NULL)); |
|
| 658 | 648 |
|
| 659 |
libusb_set_debug(_rx_ctx, libusb_debug_level);
|
|
| 660 |
libusb_set_debug(_tx_ctx, libusb_debug_level);
|
|
| 649 |
_rx_dev_handle = libusb::open_device(_rx_ctx, handle);
|
|
| 650 |
_tx_dev_handle = libusb::open_device(_tx_ctx, handle);
|
|
| 661 | 651 |
|
| 662 |
open_device(descriptor); |
|
| 663 |
|
|
| 664 |
open_interface(_rx_dev_handle, 2); |
|
| 665 |
open_interface(_tx_dev_handle, 1); |
|
| 652 |
libusb::open_interface(_rx_dev_handle, 2); |
|
| 653 |
libusb::open_interface(_tx_dev_handle, 1); |
|
| 666 | 654 |
|
| 667 | 655 |
_rx_ep = new usb_endpoint(_rx_dev_handle, |
| 668 | 656 |
_rx_ctx, |
| ... | ... | |
| 693 | 681 |
} |
| 694 | 682 |
|
| 695 | 683 |
|
| 696 |
bool libusb_zero_copy_impl::open_device(uhd::usb_descriptor_t descriptor) |
|
| 697 |
{
|
|
| 698 |
libusb_device **rx_list; |
|
| 699 |
libusb_device **tx_list; |
|
| 700 |
|
|
| 701 |
bool rx_found = false; |
|
| 702 |
bool tx_found = false; |
|
| 703 |
|
|
| 704 |
ssize_t rx_cnt = libusb_get_device_list(_rx_ctx, &rx_list); |
|
| 705 |
ssize_t tx_cnt = libusb_get_device_list(_tx_ctx, &tx_list); |
|
| 706 |
|
|
| 707 |
if ((rx_cnt < 0) | (tx_cnt < 0) | (rx_cnt != tx_cnt)) |
|
| 708 |
return false; |
|
| 709 |
|
|
| 710 |
//find and open the receive device |
|
| 711 |
for (ssize_t i = 0; i < rx_cnt; i++) {
|
|
| 712 |
libusb_device *dev = rx_list[i]; |
|
| 713 |
|
|
| 714 |
if (compare_device(dev, descriptor)) {
|
|
| 715 |
libusb_open(dev, &_rx_dev_handle); |
|
| 716 |
rx_found = true; |
|
| 717 |
break; |
|
| 718 |
} |
|
| 719 |
} |
|
| 720 |
|
|
| 721 |
//find and open the transmit device |
|
| 722 |
for (ssize_t i = 0; i < tx_cnt; i++) {
|
|
| 723 |
libusb_device *dev = tx_list[i]; |
|
| 724 |
|
|
| 725 |
if (compare_device(dev, descriptor)) {
|
|
| 726 |
libusb_open(dev, &_tx_dev_handle); |
|
| 727 |
tx_found = true; |
|
| 728 |
} |
|
| 729 |
} |
|
| 730 |
|
|
| 731 |
libusb_free_device_list(rx_list, 0); |
|
| 732 |
libusb_free_device_list(tx_list, 0); |
|
| 733 |
|
|
| 734 |
if (tx_found && rx_found) |
|
| 735 |
return true; |
|
| 736 |
else |
|
| 737 |
return false; |
|
| 738 |
} |
|
| 739 |
|
|
| 740 |
bool libusb_zero_copy_impl::compare_device(libusb_device *dev, |
|
| 741 |
uhd::usb_descriptor_t descriptor) |
|
| 742 |
{
|
|
| 743 |
std::string serial = descriptor.serial; |
|
| 744 |
boost::uint16_t vendor_id = descriptor.vendor_id; |
|
| 745 |
boost::uint16_t product_id = descriptor.product_id; |
|
| 746 |
boost::uint8_t device_addr = descriptor.device_addr; |
|
| 747 |
|
|
| 748 |
libusb_device_descriptor desc; |
|
| 749 |
libusb_get_device_descriptor(dev, &desc); |
|
| 750 |
|
|
| 751 |
if (serial.compare(get_serial(dev))) |
|
| 752 |
return false; |
|
| 753 |
if (vendor_id != desc.idVendor) |
|
| 754 |
return false; |
|
| 755 |
if (product_id != desc.idProduct) |
|
| 756 |
return false; |
|
| 757 |
if (device_addr != libusb_get_device_address(dev)) |
|
| 758 |
return false; |
|
| 759 |
|
|
| 760 |
return true; |
|
| 761 |
} |
|
| 762 |
|
|
| 763 |
bool libusb_zero_copy_impl::open_interface(libusb_device_handle *dev_handle, |
|
| 764 |
int interface) |
|
| 765 |
{
|
|
| 766 |
int ret = libusb_claim_interface(dev_handle, interface); |
|
| 767 |
if (ret < 0) {
|
|
| 768 |
std::cerr << "error: libusb_claim_interface() " << ret << std::endl; |
|
| 769 |
return false; |
|
| 770 |
} |
|
| 771 |
else {
|
|
| 772 |
return true; |
|
| 773 |
} |
|
| 774 |
} |
|
| 775 |
|
|
| 776 |
std::string libusb_zero_copy_impl::get_serial(libusb_device *dev) |
|
| 777 |
{
|
|
| 778 |
unsigned char buff[32]; |
|
| 779 |
|
|
| 780 |
libusb_device_descriptor desc; |
|
| 781 |
if (libusb_get_device_descriptor(dev, &desc) < 0) {
|
|
| 782 |
std::cerr << "error: libusb_get_device_descriptor()" << std::endl; |
|
| 783 |
return ""; |
|
| 784 |
} |
|
| 785 |
|
|
| 786 |
if (desc.iSerialNumber == 0) |
|
| 787 |
return ""; |
|
| 788 |
|
|
| 789 |
//open the device because we have to |
|
| 790 |
libusb_device_handle *dev_handle; |
|
| 791 |
if (libusb_open(dev, &dev_handle) < 0) {
|
|
| 792 |
return ""; |
|
| 793 |
} |
|
| 794 |
|
|
| 795 |
if (libusb_get_string_descriptor_ascii(dev_handle, desc.iSerialNumber, |
|
| 796 |
buff, sizeof(buff)) < 0) {
|
|
| 797 |
std::cerr << "error: libusb_get_string_descriptor_ascii()" << std::endl; |
|
| 798 |
return ""; |
|
| 799 |
} |
|
| 800 |
|
|
| 801 |
libusb_close(dev_handle); |
|
| 802 |
|
|
| 803 |
return (char*) buff; |
|
| 804 |
} |
|
| 805 |
|
|
| 806 |
|
|
| 807 | 684 |
managed_recv_buffer::sptr libusb_zero_copy_impl::get_recv_buff() |
| 808 | 685 |
{
|
| 809 | 686 |
libusb_transfer *lut = _rx_ep->get_completed_transfer(); |
| ... | ... | |
| 836 | 713 |
/*********************************************************************** |
| 837 | 714 |
* USB zero_copy make functions |
| 838 | 715 |
**********************************************************************/ |
| 839 |
usb_zero_copy::sptr usb_zero_copy::make(uhd::usb_descriptor_t descriptor,
|
|
| 716 |
usb_zero_copy::sptr usb_zero_copy::make(usb_device_handle::sptr handle,
|
|
| 840 | 717 |
unsigned int rx_endpoint, |
| 841 | 718 |
unsigned int tx_endpoint, |
| 842 | 719 |
size_t buff_size, |
| 843 | 720 |
size_t block_size) |
| 844 | 721 |
|
| 845 | 722 |
{
|
| 846 |
return sptr(new libusb_zero_copy_impl(descriptor,
|
|
| 723 |
return sptr(new libusb_zero_copy_impl(handle,
|
|
| 847 | 724 |
rx_endpoint, |
| 848 | 725 |
tx_endpoint, |
| 849 | 726 |
buff_size, |
| b/host/lib/usrp/usrp1/usrp1_impl.cpp | ||
|---|---|---|
| 48 | 48 |
if (hint.has_key("type") and hint["type"] != "usrp1") return usrp1_addrs;
|
| 49 | 49 |
|
| 50 | 50 |
//see what we got on the USB bus |
| 51 |
usb_descriptors_t usb_descriptors;
|
|
| 52 |
usb_descriptors = usb_control::get_device_list();
|
|
| 51 |
std::vector<usb_device_handle::sptr> device_list =
|
|
| 52 |
usb_device_handle::get_device_list();
|
|
| 53 | 53 |
|
| 54 | 54 |
//find the usrps and load firmware |
| 55 |
BOOST_FOREACH(usb_descriptor_t desc, usb_descriptors) {
|
|
| 56 |
if (desc.vendor_id == 0xfffe && desc.product_id == 0x0002) {
|
|
| 57 |
usb_control::sptr ctrl_transport = usb_control::make(desc); |
|
| 55 |
BOOST_FOREACH(usb_device_handle::sptr handle, device_list) {
|
|
| 56 |
if (handle->get_vendor_id() == 0xfffe && |
|
| 57 |
handle->get_product_id() == 0x0002) {
|
|
| 58 |
|
|
| 59 |
usb_control::sptr ctrl_transport = usb_control::make(handle); |
|
| 58 | 60 |
usrp_ctrl::sptr usrp_ctrl = usrp_ctrl::make(ctrl_transport); |
| 59 | 61 |
usrp_ctrl->usrp_load_firmware(filename); |
| 60 | 62 |
} |
| ... | ... | |
| 64 | 66 |
sleep(1); |
| 65 | 67 |
|
| 66 | 68 |
//get descriptors again with serial number |
| 67 |
usb_descriptors = usb_control::get_device_list(); |
|
| 69 |
device_list = usb_device_handle::get_device_list(); |
|
| 70 |
|
|
| 71 |
BOOST_FOREACH(usb_device_handle::sptr handle, device_list) {
|
|
| 72 |
if (handle->get_vendor_id() == 0xfffe && |
|
| 73 |
handle->get_product_id() == 0x0002) {
|
|
| 68 | 74 |
|
| 69 |
BOOST_FOREACH(usb_descriptor_t desc, usb_descriptors) {
|
|
| 70 |
if (desc.vendor_id == 0xfffe && desc.product_id == 0x0002) {
|
|
| 71 | 75 |
device_addr_t new_addr; |
| 72 | 76 |
new_addr["type"] = "usrp1"; |
| 73 |
new_addr["serial"] = desc.serial;
|
|
| 77 |
new_addr["serial"] = handle->get_serial();
|
|
| 74 | 78 |
usrp1_addrs.push_back(new_addr); |
| 75 | 79 |
} |
| 76 | 80 |
} |
| ... | ... | |
| 93 | 97 |
std::cout << "Make usrp1 with " << filename << std::endl; |
| 94 | 98 |
|
| 95 | 99 |
//try to match the given device address with something on the USB bus |
| 96 |
usb_descriptors_t usb_descriptors;
|
|
| 97 |
usb_descriptors = usb_control::get_device_list();
|
|
| 100 |
std::vector<usb_device_handle::sptr> device_list =
|
|
| 101 |
usb_device_handle::get_device_list();
|
|
| 98 | 102 |
|
| 99 | 103 |
//create data and control transports |
| 100 | 104 |
usb_zero_copy::sptr data_transport; |
| 101 | 105 |
usrp_ctrl::sptr usrp_ctrl; |
| 102 | 106 |
|
| 103 |
BOOST_FOREACH(usb_descriptor_t desc, usb_descriptors) {
|
|
| 104 |
if (desc.serial == device_addr["serial"] |
|
| 105 |
&& desc.vendor_id == 0xfffe && desc.product_id == 0x0002) {
|
|
| 107 |
BOOST_FOREACH(usb_device_handle::sptr handle, device_list) {
|
|
| 108 |
if (handle->get_vendor_id() == 0xfffe && |
|
| 109 |
handle->get_product_id() == 0x0002 && |
|
| 110 |
handle->get_serial() == device_addr["serial"]) {
|
|
| 106 | 111 |
|
| 107 |
usb_control::sptr ctrl_transport = usb_control::make(desc);
|
|
| 112 |
usb_control::sptr ctrl_transport = usb_control::make(handle);
|
|
| 108 | 113 |
usrp_ctrl = usrp_ctrl::make(ctrl_transport); |
| 109 | 114 |
usrp_ctrl->usrp_load_fpga(filename); |
| 110 | 115 |
|
| 111 |
data_transport = usb_zero_copy::make(desc, // identifier
|
|
| 116 |
data_transport = usb_zero_copy::make(handle, // identifier
|
|
| 112 | 117 |
6, // IN endpoint |
| 113 | 118 |
2, // OUT endpoint |
| 114 | 119 |
2 * (1 << 20), // buffer size |
Also available in: Unified diff