From: ttsou Date: Mon, 14 Sep 2009 18:43:37 +0000 (-0400) Subject: Commonized more usrp_prims code and renamed libusb-0.12 files to libusb0 X-Git-Url: https://git.gag.com/?a=commitdiff_plain;h=68b0364367f8d99a34f0c38e8a0db0a290f297fc;p=debian%2Fgnuradio Commonized more usrp_prims code and renamed libusb-0.12 files to libusb0 --- diff --git a/usrp/host/include/usrp/usrp_prims.h.in b/usrp/host/include/usrp/usrp_prims.h.in index 49a9d3d6..7f5457af 100644 --- a/usrp/host/include/usrp/usrp_prims.h.in +++ b/usrp/host/include/usrp/usrp_prims.h.in @@ -260,20 +260,26 @@ std::string usrp_serial_number(libusb_device_handle *udh); * Internal functions */ -libusb_device_handle *usrp_open_interface(libusb_device *dev, - int interface, - int altinterface); +libusb_device_handle * +usrp_open_interface(libusb_device *dev, int interface, int altinterface); -int write_cmd (libusb_device_handle *udh, int request, int value, - int index, unsigned char *bytes, int len); +int write_cmd (libusb_device_handle *udh, int request, int value, int index, + unsigned char *bytes, int len); -libusb_device_descriptor get_usb_device_descriptor (libusb_device *q); +/* + * Compatibility functions + */ + +libusb_device *_get_usb_device (libusb_device_handle *udh); + +libusb_device_descriptor _get_usb_device_descriptor (libusb_device *q); -libusb_device *get_usb_device (libusb_device_handle *udh); +int _get_usb_string_descriptor (libusb_device_handle *udh, int index, + unsigned char* data, int length); -int usb_control_transfer (struct usb_dev_handle *udh, int request_type, - int request, int value, int index, - unsigned char *data, int length, - unsigned int timeout); +int _usb_control_transfer (struct usb_dev_handle *udh, int request_type, + int request, int value, int index, + unsigned char *data, int length, + unsigned int timeout); #endif /* _USRP_PRIMS_H_ */ diff --git a/usrp/host/lib/Makefile.am b/usrp/host/lib/Makefile.am index 7c5dc25a..7455b153 100644 --- a/usrp/host/lib/Makefile.am +++ b/usrp/host/lib/Makefile.am @@ -75,15 +75,15 @@ darwin_CODE = \ win32_CODE = \ fusb_win32.cc \ fusb_sysconfig_win32.cc \ - usrp_prims_libusb.cc \ - usrp_basic_libusb.cc + usrp_prims_libusb0.cc \ + usrp_basic_libusb0.cc linux_CODE = \ fusb_linux.cc \ fusb_sysconfig_linux.cc \ - usrp_prims_libusb.cc \ - usrp_basic_libusb.cc + usrp_prims_libusb0.cc \ + usrp_basic_libusb0.cc ra_wb_CODE = \ fusb_ra_wb.cc \ diff --git a/usrp/host/lib/fusb.h b/usrp/host/lib/fusb.h index 341e570a..2ff7dc89 100644 --- a/usrp/host/lib/fusb.h +++ b/usrp/host/lib/fusb.h @@ -25,6 +25,10 @@ #ifndef _FUSB_H_ #define _FUSB_H_ +/* + * This is bad, but it works for now. The fusb header files are not installed. + */ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -33,7 +37,7 @@ struct libusb_device_handle; #else struct usb_dev_handle; -typedef struct usb_dev_handle libusb_device_handle; +typedef usb_dev_handle libusb_device_handle; #endif struct libusb_context; diff --git a/usrp/host/lib/fusb_libusb1.cc b/usrp/host/lib/fusb_libusb1.cc index e50e4408..9d79cfe5 100644 --- a/usrp/host/lib/fusb_libusb1.cc +++ b/usrp/host/lib/fusb_libusb1.cc @@ -20,9 +20,9 @@ * Boston, MA 02110-1301, USA. */ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif +//#ifdef HAVE_CONFIG_H +//#include "config.h" +//#endif #include #include diff --git a/usrp/host/lib/fusb_linux.cc b/usrp/host/lib/fusb_linux.cc index 6c484569..0b3aaed7 100644 --- a/usrp/host/lib/fusb_linux.cc +++ b/usrp/host/lib/fusb_linux.cc @@ -20,9 +20,9 @@ * Boston, MA 02110-1301, USA. */ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif +//#ifdef HAVE_CONFIG_H +//#include "config.h" +//#endif #include #include // libusb header diff --git a/usrp/host/lib/usrp_basic_libusb.cc b/usrp/host/lib/usrp_basic_libusb.cc deleted file mode 100644 index bd22d6ad..00000000 --- a/usrp/host/lib/usrp_basic_libusb.cc +++ /dev/null @@ -1,137 +0,0 @@ -/* -*- c++ -*- */ -/* - * Copyright 2003,2004,2008,2009 Free Software Foundation, Inc. - * - * This file is part of GNU Radio - * - * GNU Radio is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3, or (at your option) - * any later version. - * - * GNU Radio is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with GNU Radio; see the file COPYING. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, - * Boston, MA 02110-1301, USA. - */ - -//#ifdef HAVE_CONFIG_H -//#include "config.h" -//#endif - -#include -#include "usrp/usrp_prims.h" -#include "usrp_interfaces.h" -#include "fpga_regs_common.h" -#include "fpga_regs_standard.h" -#include "fusb.h" -#include "db_boards.h" -#include -#include -#include -#include -#include -#include -#include - -using namespace ad9862; - -#define NELEM(x) (sizeof (x) / sizeof (x[0])) - - -static const double POLLING_INTERVAL = 0.1; // seconds - - -////////////////////////////////////////////////////////////////// -// -// usrp_basic -// -//////////////////////////////////////////////////////////////// - - -// Given: -// CLKIN = 64 MHz -// CLKSEL pin = high -// -// CLKOUT1 = CLKIN = 64 MHz -// CLKOUT2 = CLKIN = 64 MHz -// ADC is clocked at 64 MHz -// DAC is clocked at 128 MHz - -static unsigned char common_regs[] = { - REG_GENERAL, 0, - REG_DLL, (DLL_DISABLE_INTERNAL_XTAL_OSC - | DLL_MULT_2X - | DLL_FAST), - REG_CLKOUT, CLKOUT2_EQ_DLL_OVER_2, - REG_AUX_ADC_CLK, AUX_ADC_CLK_CLK_OVER_4 -}; - - -usrp_basic::usrp_basic (int which_board, - struct usb_dev_handle * - open_interface (struct usb_device *dev), - const std::string fpga_filename, - const std::string firmware_filename) - : d_udh (0), - d_usb_data_rate (16000000), // SWAG, see below - d_bytes_per_poll ((int) (POLLING_INTERVAL * d_usb_data_rate)), - d_verbose (false), d_fpga_master_clock_freq(64000000), d_db(2) -{ - /* - * SWAG: Scientific Wild Ass Guess. - * - * d_usb_data_rate is used only to determine how often to poll for over- and under-runs. - * We defualt it to 1/2 of our best case. Classes derived from usrp_basic (e.g., - * usrp_standard_tx and usrp_standard_rx) call set_usb_data_rate() to tell us the - * actual rate. This doesn't change our throughput, that's determined by the signal - * processing code in the FPGA (which we know nothing about), and the system limits - * determined by libusb, fusb_*, and the underlying drivers. - */ - memset (d_fpga_shadows, 0, sizeof (d_fpga_shadows)); - - usrp_one_time_init (); - - if (!usrp_load_standard_bits (which_board, false, fpga_filename, firmware_filename)) - throw std::runtime_error ("usrp_basic/usrp_load_standard_bits"); - - struct usb_device *dev = usrp_find_device (which_board); - if (dev == 0){ - fprintf (stderr, "usrp_basic: can't find usrp[%d]\n", which_board); - throw std::runtime_error ("usrp_basic/usrp_find_device"); - } - - if (!(usrp_usrp_p(dev) && usrp_hw_rev(dev) >= 1)){ - fprintf (stderr, "usrp_basic: sorry, this code only works with USRP revs >= 1\n"); - throw std::runtime_error ("usrp_basic/bad_rev"); - } - - if ((d_udh = open_interface (dev)) == 0) - throw std::runtime_error ("usrp_basic/open_interface"); - - // initialize registers that are common to rx and tx - - if (!usrp_9862_write_many_all (d_udh, common_regs, sizeof (common_regs))){ - fprintf (stderr, "usrp_basic: failed to init common AD9862 regs\n"); - throw std::runtime_error ("usrp_basic/init_9862"); - } - - _write_fpga_reg (FR_MODE, 0); // ensure we're in normal mode - _write_fpga_reg (FR_DEBUG_EN, 0); // disable debug outputs -} - -usrp_basic::~usrp_basic () -{ - // shutdown_daughterboards(); // call from ~usrp_basic_{tx,rx} - - d_db.resize(0); // forget db shared ptrs - - if (d_udh) - usb_close (d_udh); -} - diff --git a/usrp/host/lib/usrp_basic_libusb0.cc b/usrp/host/lib/usrp_basic_libusb0.cc new file mode 100644 index 00000000..bd22d6ad --- /dev/null +++ b/usrp/host/lib/usrp_basic_libusb0.cc @@ -0,0 +1,137 @@ +/* -*- c++ -*- */ +/* + * Copyright 2003,2004,2008,2009 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +//#ifdef HAVE_CONFIG_H +//#include "config.h" +//#endif + +#include +#include "usrp/usrp_prims.h" +#include "usrp_interfaces.h" +#include "fpga_regs_common.h" +#include "fpga_regs_standard.h" +#include "fusb.h" +#include "db_boards.h" +#include +#include +#include +#include +#include +#include +#include + +using namespace ad9862; + +#define NELEM(x) (sizeof (x) / sizeof (x[0])) + + +static const double POLLING_INTERVAL = 0.1; // seconds + + +////////////////////////////////////////////////////////////////// +// +// usrp_basic +// +//////////////////////////////////////////////////////////////// + + +// Given: +// CLKIN = 64 MHz +// CLKSEL pin = high +// +// CLKOUT1 = CLKIN = 64 MHz +// CLKOUT2 = CLKIN = 64 MHz +// ADC is clocked at 64 MHz +// DAC is clocked at 128 MHz + +static unsigned char common_regs[] = { + REG_GENERAL, 0, + REG_DLL, (DLL_DISABLE_INTERNAL_XTAL_OSC + | DLL_MULT_2X + | DLL_FAST), + REG_CLKOUT, CLKOUT2_EQ_DLL_OVER_2, + REG_AUX_ADC_CLK, AUX_ADC_CLK_CLK_OVER_4 +}; + + +usrp_basic::usrp_basic (int which_board, + struct usb_dev_handle * + open_interface (struct usb_device *dev), + const std::string fpga_filename, + const std::string firmware_filename) + : d_udh (0), + d_usb_data_rate (16000000), // SWAG, see below + d_bytes_per_poll ((int) (POLLING_INTERVAL * d_usb_data_rate)), + d_verbose (false), d_fpga_master_clock_freq(64000000), d_db(2) +{ + /* + * SWAG: Scientific Wild Ass Guess. + * + * d_usb_data_rate is used only to determine how often to poll for over- and under-runs. + * We defualt it to 1/2 of our best case. Classes derived from usrp_basic (e.g., + * usrp_standard_tx and usrp_standard_rx) call set_usb_data_rate() to tell us the + * actual rate. This doesn't change our throughput, that's determined by the signal + * processing code in the FPGA (which we know nothing about), and the system limits + * determined by libusb, fusb_*, and the underlying drivers. + */ + memset (d_fpga_shadows, 0, sizeof (d_fpga_shadows)); + + usrp_one_time_init (); + + if (!usrp_load_standard_bits (which_board, false, fpga_filename, firmware_filename)) + throw std::runtime_error ("usrp_basic/usrp_load_standard_bits"); + + struct usb_device *dev = usrp_find_device (which_board); + if (dev == 0){ + fprintf (stderr, "usrp_basic: can't find usrp[%d]\n", which_board); + throw std::runtime_error ("usrp_basic/usrp_find_device"); + } + + if (!(usrp_usrp_p(dev) && usrp_hw_rev(dev) >= 1)){ + fprintf (stderr, "usrp_basic: sorry, this code only works with USRP revs >= 1\n"); + throw std::runtime_error ("usrp_basic/bad_rev"); + } + + if ((d_udh = open_interface (dev)) == 0) + throw std::runtime_error ("usrp_basic/open_interface"); + + // initialize registers that are common to rx and tx + + if (!usrp_9862_write_many_all (d_udh, common_regs, sizeof (common_regs))){ + fprintf (stderr, "usrp_basic: failed to init common AD9862 regs\n"); + throw std::runtime_error ("usrp_basic/init_9862"); + } + + _write_fpga_reg (FR_MODE, 0); // ensure we're in normal mode + _write_fpga_reg (FR_DEBUG_EN, 0); // disable debug outputs +} + +usrp_basic::~usrp_basic () +{ + // shutdown_daughterboards(); // call from ~usrp_basic_{tx,rx} + + d_db.resize(0); // forget db shared ptrs + + if (d_udh) + usb_close (d_udh); +} + diff --git a/usrp/host/lib/usrp_prims_common.cc b/usrp/host/lib/usrp_prims_common.cc index c71ba90b..c9fcf80d 100644 --- a/usrp/host/lib/usrp_prims_common.cc +++ b/usrp/host/lib/usrp_prims_common.cc @@ -67,7 +67,6 @@ static const int hash_slot_addr[2] = { static const char *default_firmware_filename = "std.ihx"; static const char *default_fpga_filename = "std_2rxhb_2tx.rbf"; - static char * find_file (const char *filename, int hw_rev) { @@ -117,7 +116,7 @@ static void power_down_9862s (libusb_device_handle *udh); int usrp_hw_rev (libusb_device *q) { - libusb_device_descriptor desc = get_usb_device_descriptor(q); + libusb_device_descriptor desc = _get_usb_device_descriptor(q); return desc.bcdDevice & 0x00FF; } @@ -127,14 +126,14 @@ usrp_hw_rev (libusb_device *q) static bool _usrp_configured_p (libusb_device *q) { - libusb_device_descriptor desc = get_usb_device_descriptor(q); + libusb_device_descriptor desc = _get_usb_device_descriptor(q); return (desc.bcdDevice & 0xFF00) != 0; } bool usrp_usrp_p (libusb_device *q) { - libusb_device_descriptor desc = get_usb_device_descriptor(q); + libusb_device_descriptor desc = _get_usb_device_descriptor(q); return (desc.idVendor == USB_VID_FSF && desc.idProduct == USB_PID_FSF_USRP); } @@ -142,7 +141,7 @@ usrp_usrp_p (libusb_device *q) bool usrp_fx2_p (libusb_device *q) { - libusb_device_descriptor desc = get_usb_device_descriptor(q); + libusb_device_descriptor desc = _get_usb_device_descriptor(q); return (desc.idVendor == USB_VID_CYPRESS && desc.idProduct == USB_PID_CYPRESS_FX2); } @@ -218,7 +217,7 @@ write_internal_ram (libusb_device_handle *udh, unsigned char *buf, if (n > quanta) n = quanta; - a = usb_control_transfer (udh, 0x40, 0xA0, addr, 0, + a = _usb_control_transfer (udh, 0x40, 0xA0, addr, 0, (unsigned char*)(buf + (addr - start_addr)), n, 1000); if (a < 0){ @@ -424,7 +423,7 @@ usrp_set_hash (libusb_device_handle *udh, int which, which &= 1; // we use the Cypress firmware down load command to jam it in. - int r = usb_control_transfer (udh, 0x40, 0xa0, hash_slot_addr[which], 0, + int r = _usb_control_transfer (udh, 0x40, 0xa0, hash_slot_addr[which], 0, (unsigned char *) hash, USRP_HASH_SIZE, 1000); return r == USRP_HASH_SIZE; } @@ -436,7 +435,7 @@ usrp_get_hash (libusb_device_handle *udh, int which, which &= 1; // we use the Cypress firmware upload command to fetch it. - int r = usb_control_transfer (udh, 0xc0, 0xa0, hash_slot_addr[which], 0, + int r = _usb_control_transfer (udh, 0xc0, 0xa0, hash_slot_addr[which], 0, (unsigned char *) hash, USRP_HASH_SIZE, 1000); return r == USRP_HASH_SIZE; } @@ -489,7 +488,7 @@ usrp1_fpga_read (libusb_device_handle *udh, bool usrp_write_fpga_reg (libusb_device_handle *udh, int reg, int value) { - switch (usrp_hw_rev (get_usb_device (udh))){ + switch (usrp_hw_rev (_get_usb_device (udh))){ case 0: // not supported ;) abort(); @@ -501,7 +500,7 @@ usrp_write_fpga_reg (libusb_device_handle *udh, int reg, int value) bool usrp_read_fpga_reg (libusb_device_handle *udh, int reg, int *value) { - switch (usrp_hw_rev (get_usb_device (udh))){ + switch (usrp_hw_rev (_get_usb_device (udh))){ case 0: // not supported ;) abort(); @@ -935,12 +934,12 @@ static void power_down_9862s (libusb_device_handle *udh) { static const unsigned char regs[] = { - REG_RX_PWR_DN, 0x01, // everything - REG_TX_PWR_DN, 0x0f, // pwr dn digital and analog_both - REG_TX_MODULATOR, 0x00 // coarse & fine modulators disabled + REG_RX_PWR_DN, 0x01, // everything + REG_TX_PWR_DN, 0x0f, // pwr dn digital and analog_both + REG_TX_MODULATOR, 0x00 // coarse & fine modulators disabled }; - switch (usrp_hw_rev (get_usb_device (udh))){ + switch (usrp_hw_rev (_get_usb_device (udh))){ case 0: break; @@ -1214,6 +1213,24 @@ usrp_write_dboard_offsets (libusb_device_handle *udh, int slot_id, 0, buf, sizeof (buf)); } +// ---------------------------------------------------------------- + +std::string +usrp_serial_number(libusb_device_handle *udh) +{ + libusb_device_descriptor desc = + _get_usb_device_descriptor (_get_usb_device (udh)); + + unsigned char iserial = desc.iSerialNumber; + if (iserial == 0) + return ""; + + unsigned char buf[1024]; + if (_get_usb_string_descriptor (udh, iserial, buf, sizeof(buf)) < 0) + return ""; + + return (char*) buf; +} diff --git a/usrp/host/lib/usrp_prims_libusb.cc b/usrp/host/lib/usrp_prims_libusb.cc deleted file mode 100644 index 999dc589..00000000 --- a/usrp/host/lib/usrp_prims_libusb.cc +++ /dev/null @@ -1,216 +0,0 @@ -/* -*- c++ -*- */ -/* - * Copyright 2003,2004,2006,2009 Free Software Foundation, Inc. - * - * This file is part of GNU Radio - * - * GNU Radio is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3, or (at your option) - * any later version. - * - * GNU Radio is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with GNU Radio; see the file COPYING. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, - * Boston, MA 02110-1301, USA. - */ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include "usrp/usrp_prims.h" -#include "usrp_commands.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include - -extern "C" { -#include "md5.h" -}; - -using namespace ad9862; - -struct usb_device_descriptor -get_usb_device_descriptor (struct usb_device *q) -{ - return q->descriptor; -} - -struct usb_device * -get_usb_device (struct usb_dev_handle *udh) -{ - return usb_device (udh); -} - -int -usb_control_transfer (struct usb_dev_handle *udh, int request_type, - int request, int value, int index, - unsigned char *data, int length, unsigned int timeout) -{ - return usb_control_msg (udh, request_type, - request, value, index, - (char*) data, length, (int) timeout); - -} - - -// ---------------------------------------------------------------- - - -void -usrp_one_time_init (libusb_context **ctx) -{ - static bool first = true; - - if (first) { - first = false; - usb_init (); // usb library init - usb_find_busses (); - usb_find_devices (); - } -} - -void -usrp_rescan () -{ - usb_find_busses (); - usb_find_devices (); -} - - -// ---------------------------------------------------------------- - - -struct usb_device * -usrp_find_device (int nth, bool fx2_ok_p, libusb_context *ctx) -{ - struct usb_bus *p; - struct usb_device *q; - int n_found = 0; - - usrp_one_time_init (); - - p = usb_get_busses(); - while (p != NULL){ - q = p->devices; - while (q != NULL){ - if (usrp_usrp_p (q) || (fx2_ok_p && usrp_fx2_p (q))){ - if (n_found == nth) // return this one - return q; - n_found++; // keep looking - } - q = q->next; - } - p = p->next; - } - return 0; // not found -} - -struct usb_dev_handle * -usrp_open_interface (struct usb_device *dev, int interface, int altinterface) -{ - struct usb_dev_handle *udh = usb_open (dev); - if (udh == 0) - return 0; - - if (dev != usb_device (udh)){ - fprintf (stderr, "%s:%d: internal error!\n", __FILE__, __LINE__); - abort (); - } - -#if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) - // There's no get get_configuration function, and with some of the newer kernels - // setting the configuration, even if to the same value, hoses any other processes - // that have it open. Hence we opt to not set it at all (We've only - // got a single configuration anyway). This may hose the win32 stuff... - - // Appears to be required for libusb-win32 and Cygwin -- dew 09/20/06 - if (usb_set_configuration (udh, 1) < 0){ - /* - * Ignore this error. - * - * Seems that something changed in drivers/usb/core/devio.c:proc_setconfig such that - * it returns -EBUSY if _any_ of the interfaces of a device are open. - * We've only got a single configuration, so setting it doesn't even seem - * like it should be required. - */ - } -#endif - - if (usb_claim_interface (udh, interface) < 0){ - fprintf (stderr, "%s:usb_claim_interface: failed interface %d\n", __FUNCTION__,interface); - fprintf (stderr, "%s\n", usb_strerror()); - usb_close (udh); - return 0; - } - - if (usb_set_altinterface (udh, altinterface) < 0){ - fprintf (stderr, "%s:usb_set_alt_interface: failed\n", __FUNCTION__); - fprintf (stderr, "%s\n", usb_strerror()); - usb_release_interface (udh, interface); - usb_close (udh); - return 0; - } - - return udh; -} - -bool -usrp_close_interface (struct usb_dev_handle *udh) -{ - // we're assuming that closing an interface automatically releases it. - return usb_close (udh) == 0; -} - - -// ---------------------------------------------------------------- -// write vendor extension command to USRP - - -int -write_cmd (struct usb_dev_handle *udh, - int request, int value, int index, - unsigned char *bytes, int len) -{ - int requesttype = (request & 0x80) ? VRT_VENDOR_IN : VRT_VENDOR_OUT; - - int r = usb_control_msg (udh, requesttype, request, value, index, - (char *) bytes, len, 1000); - if (r < 0){ - // we get EPIPE if the firmware stalls the endpoint. - if (errno != EPIPE) - fprintf (stderr, "usb_control_msg failed: %s\n", usb_strerror ()); - } - - return r; -} - - -// ---------------------------------------------------------------- - - -std::string -usrp_serial_number(struct usb_dev_handle *udh) -{ - unsigned char iserial = usb_device(udh)->descriptor.iSerialNumber; - if (iserial == 0) - return ""; - - char buf[1024]; - if (usb_get_string_simple(udh, iserial, buf, sizeof(buf)) < 0) - return ""; - - return buf; -} diff --git a/usrp/host/lib/usrp_prims_libusb0.cc b/usrp/host/lib/usrp_prims_libusb0.cc new file mode 100644 index 00000000..7ff83106 --- /dev/null +++ b/usrp/host/lib/usrp_prims_libusb0.cc @@ -0,0 +1,207 @@ +/* -*- c++ -*- */ +/* + * Copyright 2003,2004,2006,2009 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3, or (at your option) + * any later version. + * + * GNU Radio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "usrp/usrp_prims.h" +#include "usrp_commands.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +extern "C" { +#include "md5.h" +}; + +using namespace ad9862; + +/* + * libusb 0.12 / 1.0 compatibility + */ + +struct usb_device * +_get_usb_device (struct usb_dev_handle *udh) +{ + return usb_device (udh); +} + +struct usb_device_descriptor +_get_usb_device_descriptor (struct usb_device *q) +{ + return q->descriptor; +} +int +_get_usb_string_descriptor (struct usb_dev_handle *udh, int index, + unsigned char* data, int length) +{ + return usb_get_string_simple (udh, index, (char*) data, length); +} + +int +_usb_control_transfer (struct usb_dev_handle *udh, int request_type, + int request, int value, int index, + unsigned char *data, int length, unsigned int timeout) +{ + return usb_control_msg (udh, request_type,request, value, index, + (char*) data, length, (int) timeout); +} + + +// ---------------------------------------------------------------- + + +void +usrp_one_time_init (libusb_context **ctx) +{ + static bool first = true; + + if (first) { + first = false; + usb_init (); // usb library init + usb_find_busses (); + usb_find_devices (); + } +} + +void +usrp_rescan () +{ + usb_find_busses (); + usb_find_devices (); +} + + +// ---------------------------------------------------------------- + + +struct usb_device * +usrp_find_device (int nth, bool fx2_ok_p, libusb_context *ctx) +{ + struct usb_bus *p; + struct usb_device *q; + int n_found = 0; + + usrp_one_time_init (); + + p = usb_get_busses(); + while (p != NULL){ + q = p->devices; + while (q != NULL){ + if (usrp_usrp_p (q) || (fx2_ok_p && usrp_fx2_p (q))){ + if (n_found == nth) // return this one + return q; + n_found++; // keep looking + } + q = q->next; + } + p = p->next; + } + return 0; // not found +} + +struct usb_dev_handle * +usrp_open_interface (struct usb_device *dev, int interface, int altinterface) +{ + struct usb_dev_handle *udh = usb_open (dev); + if (udh == 0) + return 0; + + if (dev != usb_device (udh)){ + fprintf (stderr, "%s:%d: internal error!\n", __FILE__, __LINE__); + abort (); + } + +#if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) + // There's no get get_configuration function, and with some of the newer kernels + // setting the configuration, even if to the same value, hoses any other processes + // that have it open. Hence we opt to not set it at all (We've only + // got a single configuration anyway). This may hose the win32 stuff... + + // Appears to be required for libusb-win32 and Cygwin -- dew 09/20/06 + if (usb_set_configuration (udh, 1) < 0){ + /* + * Ignore this error. + * + * Seems that something changed in drivers/usb/core/devio.c:proc_setconfig such that + * it returns -EBUSY if _any_ of the interfaces of a device are open. + * We've only got a single configuration, so setting it doesn't even seem + * like it should be required. + */ + } +#endif + + if (usb_claim_interface (udh, interface) < 0){ + fprintf (stderr, "%s:usb_claim_interface: failed interface %d\n", __FUNCTION__,interface); + fprintf (stderr, "%s\n", usb_strerror()); + usb_close (udh); + return 0; + } + + if (usb_set_altinterface (udh, altinterface) < 0){ + fprintf (stderr, "%s:usb_set_alt_interface: failed\n", __FUNCTION__); + fprintf (stderr, "%s\n", usb_strerror()); + usb_release_interface (udh, interface); + usb_close (udh); + return 0; + } + + return udh; +} + +bool +usrp_close_interface (struct usb_dev_handle *udh) +{ + // we're assuming that closing an interface automatically releases it. + return usb_close (udh) == 0; +} + + +// ---------------------------------------------------------------- +// write vendor extension command to USRP + + +int +write_cmd (struct usb_dev_handle *udh, + int request, int value, int index, + unsigned char *bytes, int len) +{ + int requesttype = (request & 0x80) ? VRT_VENDOR_IN : VRT_VENDOR_OUT; + + int r = usb_control_msg (udh, requesttype, request, value, index, + (char *) bytes, len, 1000); + if (r < 0){ + // we get EPIPE if the firmware stalls the endpoint. + if (errno != EPIPE) + fprintf (stderr, "usb_control_msg failed: %s\n", usb_strerror ()); + } + + return r; +} + diff --git a/usrp/host/lib/usrp_prims_libusb1.cc b/usrp/host/lib/usrp_prims_libusb1.cc index 5cea97d8..4ce81509 100644 --- a/usrp/host/lib/usrp_prims_libusb1.cc +++ b/usrp/host/lib/usrp_prims_libusb1.cc @@ -49,9 +49,18 @@ extern "C" { using namespace ad9862; +/* + * libusb 0.12 / 1.0 compatibility + */ + +struct libusb_device * +_get_usb_device (struct libusb_device_handle *udh) +{ + return libusb_get_device (udh); +} struct libusb_device_descriptor -get_usb_device_descriptor(struct libusb_device *q) +_get_usb_device_descriptor(struct libusb_device *q) { int ret; struct libusb_device_descriptor desc; @@ -62,21 +71,20 @@ get_usb_device_descriptor(struct libusb_device *q) return desc; } -struct libusb_device * -get_usb_device (struct libusb_device_handle *udh) +int +_get_usb_string_descriptor (struct libusb_device_handle *udh, int index, + unsigned char* data, int length) { - return libusb_get_device (udh); + return libusb_get_string_descriptor_ascii (udh, (uint8_t) index, data, length); } int -usb_control_transfer (struct usb_dev_handle *udh, uint8_t request_type, - uint8_t request, uint16_t value, uint16_t index, - unsigned char *data, uint16_t length, - unsigned int timeout) +_usb_control_transfer (struct libusb_dev_handle *udh, int request_type, + int request, int value, int index, + unsigned char *data, int length, unsigned int timeout) { return libusb_control_transfer (udh, request_type, request, value, index, - *data, length, timeout); - + data, length, timeout); } @@ -199,23 +207,3 @@ write_cmd (struct libusb_device_handle *udh, return r; } - -// ---------------------------------------------------------------- - -std::string -usrp_serial_number(struct libusb_device_handle *udh) -{ - struct libusb_device_descriptor desc; - if (libusb_get_device_descriptor(libusb_get_device(udh), &desc) < 0) - fprintf (stderr, "usrp: libusb_get_device_descriptor failed\n"); - - unsigned char iserial = desc.iSerialNumber; - if (iserial == 0) - return ""; - - unsigned char buf[1024]; - if (libusb_get_string_descriptor_ascii(udh, iserial, buf, sizeof(buf)) < 0) - return ""; - - return (char*) buf; -}