Merge branch 'libusb-1.0' of git://github.com/ttsou/gnuradio-ttsou
authorJohnathan Corgan <jcorgan@corganenterprises.com>
Tue, 13 Oct 2009 20:32:39 +0000 (13:32 -0700)
committerJohnathan Corgan <jcorgan@corganenterprises.com>
Tue, 13 Oct 2009 20:32:39 +0000 (13:32 -0700)
This merge fixes libusb build machinery to work for Cygwin, Darwin, Ubuntu,
and F11.

* 'libusb-1.0' of git://github.com/ttsou/gnuradio-ttsou:
  usrp: Applied patch from Michael Dickens for Darwin libusb
  usrp: Applied patch from Don Ward for Cygwin libusb
  fix to restart libusbok checking for non-PKGCONFIG
  usrp: Disabled libusb-1.0 debug output by default
  merged updated libusb checks with older version
  usrp: Enable debug output
  usrp: Reorg of libusb error reporting
  usrp: Added error checking on hash transfers
  augmented search for libusb

config/usrp_libusb.m4 [changed mode: 0644->0755]
gr-usrp/src/Makefile.am
usrp/host/apps/Makefile.am
usrp/host/lib/usrp_prims_common.cc
usrp/host/lib/usrp_prims_libusb0.cc
usrp/host/lib/usrp_prims_libusb1.cc

old mode 100644 (file)
new mode 100755 (executable)
index 0c271e0..428a579
@@ -24,45 +24,100 @@ dnl Boston, MA 02110-1301, USA.
 
 
 AC_DEFUN([USRP_LIBUSB], [
-  libusbok=yes
+  dnl Use PKGCONFIG to check for packages first, then check to
+  dnl make sure the USB_* variables work (whether from PKGCONFIG
+  dnl or overridden by the user)
+
+  libusbok=no
   have_libusb1=no
-  if test [x]$1 = xyes; then
-    PKG_CHECK_MODULES(USB, libusb-1.0, [have_libusb1=yes], [
-      AC_LANG_PUSH(C)
-      AC_CHECK_HEADERS([libusb-1.0/libusb.h], [have_libusb1=yes],
-                       [libusbok=no; AC_MSG_RESULT([USRP requires libusb-1.0. libusb.h not found. See http://www.libusb.org])])
-      AC_SEARCH_LIBS(libusb_bulk_transfer, [usb-1.0], [USB_LIBS="$LIBS"],
-                     [libusbok=no; AC_MSG_RESULT([USRP requires libusb-1.0. libusb_bulk_transfer not found. See http://www.libusb.org])])
-      AC_LANG_POP
+  if test x$1 = xyes; then
+    PKG_CHECK_MODULES(USB, libusb-1.0, [
+      libusbok=yes
+      have_libusb1=yes
+      usb_header='libusb-1.0/libusb.h'
+      usb_lib_func='libusb_bulk_transfer'
+      usb_lib_name='usb-1.0'
     ])
   else
-    PKG_CHECK_MODULES(USB, libusb, [], [
+    dnl not using libusb1 (for now); see if legacy version is found.
+    dnl it might be installed under the name either 'libusb' or
+    dnl 'libusb-legacy', or just available via the
+    dnl user's shell environment
+
+    dnl see if the pkgconfig module 'libusb' is available
+    PKG_CHECK_MODULES(USB, libusb, [libusbok=yes], [libusbok=no])
+    dnl PKG_CHECK_MODULES does not work correctly when embedded
+    if test $libusbok = no; then
+      dnl if not, see if the pkgconfig module 'libusb-legacy' is available
+      PKG_CHECK_MODULES(USB, [libusb-legacy], [libusbok=yes], [libusbok=no])
+    fi
+    dnl set variables for further testing
+    usb_header='usb.h'
+    usb_lib_func='usb_bulk_write'
+    usb_lib_name='usb'
+  fi
+  if test x$1 != xyes || test $have_libusb1 = yes; then
+    dnl Either (1) libusb1 was specified and found; or
+    dnl (2) libusb1 was not specified. Restart checking.
+    libusbok=yes
+
+    dnl Verify that $usb_header is a valid header, and if so,
+    dnl then verify that $usb_lib_func can be found in the
+    dnl library $usb_lib_name.
+
+    dnl If PKGCONFIG found variable USB_INCLUDEDIR, and it is
+    dnl not empty, use it for checking for $usb_header.
+    dnl Otherwise, maybe the user's shell environment is already
+    dnl configured to find this header.
+
+    AC_LANG_PUSH(C)
+    save_CPPFLAGS="$CPPFLAGS"
+    if test x$USB_INCLUDEDIR != x; then
+      USB_INCLUDES="-I$USB_INCLUDEDIR"
+      CPPFLAGS="$CPPFLAGS $USB_INCLUDES"
+      AC_SUBST(USB_INCLUDES)
+    fi
+    AC_CHECK_HEADERS([$usb_header], [], [libusbok=no])
+    CPPFLAGS="$save_CPPFLAGS"
+    AC_LANG_POP(C)
+
+    if test $libusbok = no; then
+      AC_MSG_RESULT([USRP requires libusb header '$usb_header' which was not found or was not usable. See http://www.libusb.org])
+    else
+
+      dnl found the header; now make sure the library is OK
+      dnl On Darwin, need to include the IOKit library.     
+
       AC_LANG_PUSH(C)
-      AC_CHECK_HEADERS([usb.h], [],
-                       [libusbok=no; AC_MSG_RESULT([USRP requires libusb. usb.h not found. See http://www.libusb.org])])
       save_LIBS="$LIBS"
+      LIBS=""
       case "$host_os" in
         darwin*)
-          LIBS="$LIBS -lIOKit"
+          USB_LIBS="$USB_LIBS -lIOKit"
+          LIBS="$USB_LIBS"
+          ;;
+        *) ;;
+      esac
+      AC_CHECK_LIB([$usb_lib_name], [$usb_lib_func], [], [
+        libusbok=no
+        AC_MSG_RESULT([USRP requires library '$usb_lib_name' with function '$usb_lib_func', which was either not found or was not usable. See http://www.libusb.org])
+      ])
+      case "$host_os" in
+        cygwin* | mingw*)
+          USB_LIBS="$LIBS"
           ;;
         *) ;;
       esac
-
-      AC_SEARCH_LIBS(usb_bulk_write, [usb], [USB_LIBS="$LIBS"],
-                     [libusbok=no; AC_MSG_RESULT([USRP requires libusb. usb_bulk_write not found. See http://www.libusb.org])])
       LIBS="$save_LIBS"
-      AC_LANG_POP
-    ])
-  fi
-
-  if test x$libusbok = xyes; then
-    if test x$USB_INCLUDEDIR != x; then
-      USB_INCLUDES="-I$USB_INCLUDEDIR"
-      AC_SUBST(USB_INCLUDES)
+      AC_LANG_POP(C)
     fi
+  fi
+  if test $libusbok = yes; then
     AC_SUBST(USB_LIBS)
     ifelse([$2], , :, [$2])
   else
+    USB_INCLUDES=
+    USB_LIBS=
     ifelse([$3], , :, [$3])
   fi
 ])
index 0835e2b9bb562b6919ac792923ab6a171dcf9ee8..3d1cc17543682c5601eb92dbf0d74b6a1084d3ed 100644 (file)
@@ -37,6 +37,7 @@ AM_CPPFLAGS = \
        $(STD_DEFINES_AND_INCLUDES) \
        $(PYTHON_CPPFLAGS) \
        $(USRP_INCLUDES) \
+       $(USB_INCLUDES) \
        $(WITH_INCLUDES)
 
 lib_LTLIBRARIES = \
index b0c21d55fee6941f69f1776a23e808f511312c21..8aa361b22fcb397cbe7f173e9437f9d89bacde29 100644 (file)
@@ -1,5 +1,5 @@
 #
-# Copyright 2003,2006,2008 Free Software Foundation, Inc.
+# Copyright 2003,2006,2008,2009 Free Software Foundation, Inc.
 # 
 # This file is part of GNU Radio
 # 
@@ -22,7 +22,7 @@
 include $(top_srcdir)/Makefile.common
 
 AM_CPPFLAGS = $(USRP_INCLUDES) $(BOOST_CPPFLAGS) $(CPPUNIT_INCLUDES) \
-          $(WITH_INCLUDES)
+          $(USB_INCLUDES) $(WITH_INCLUDES)
 
 bin_PROGRAMS =                         \
        usrper                          \
index 70e90d7fed44cb93e97c47a83beb5f0a3219de1d..2d8a0fa2ab8cad17c9ea4c4cccd336288bc22ba9 100644 (file)
@@ -215,7 +215,7 @@ write_internal_ram (libusb_device_handle *udh, unsigned char *buf,
                        (unsigned char*)(buf + (addr - start_addr)), n, 1000);
 
     if (a < 0){
-      fprintf(stderr,"write_internal_ram failed: %i\n", a);
+      fprintf(stderr,"write_internal_ram failed\n");
       return false;
     }
   }
@@ -297,7 +297,7 @@ _usrp_load_firmware (libusb_device_handle *udh, const char *filename,
       break;
     }
     else if (type == 0x02){
-      fprintf(stderr, "Extended address: whatever I do with it?\n");
+      fprintf (stderr, "Extended address: whatever I do with it?\n");
       fprintf (stderr, "%s: invalid line: \"%s\"\n", filename, s);
       goto fail;
     }
@@ -419,6 +419,10 @@ usrp_set_hash (libusb_device_handle *udh, int which,
   // 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,
                                 (unsigned char *) hash, USRP_HASH_SIZE, 1000);
+
+  if (r < 0)
+     fprintf (stderr, "usrp: failed to set hash\n");
+
   return r == USRP_HASH_SIZE;
 }
 
@@ -431,6 +435,10 @@ usrp_get_hash (libusb_device_handle *udh, int which,
   // we use the Cypress firmware upload command to fetch it.
   int r = _usb_control_transfer (udh, 0xc0, 0xa0, hash_slot_addr[which], 0,
                                 (unsigned char *) hash, USRP_HASH_SIZE, 1000);
+
+  if (r < 0)
+     fprintf (stderr, "usrp: failed to get hash\n");
+
   return r == USRP_HASH_SIZE;
 }
 
index 7a82eabfaf634180397215cee664d7d07509bfb1..35e397adbb81ee054075e7a966da590539c793f6 100644 (file)
@@ -57,11 +57,18 @@ _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 ret;
+  ret =  usb_get_string_simple (udh, index, (char*) data, length);
+
+  if (ret < 0) {
+    fprintf (stderr, "usrp: usb_get_string_descriptor failed: %s\n",
+             usb_strerror());
+  }
 }
 
 int
@@ -69,8 +76,12 @@ _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);
+  int ret;
+
+  ret = usb_control_msg (udh, request_type,request, value, index,
+                         (char*) data, length, (int) timeout);
+  if (ret < 0) 
+    fprintf (stderr, "usrp: usb_claim_interface failed: %s\n", usb_strerror());
 }
 
 
@@ -198,8 +209,10 @@ write_cmd (struct usb_dev_handle *udh,
                           (char *) bytes, len, 1000);
   if (r < 0){
     // we get EPIPE if the firmware stalls the endpoint.
-    if (errno != EPIPE)
+    if (errno != EPIPE) {
       fprintf (stderr, "usb_control_msg failed: %s\n", usb_strerror ());
+      fprintf (stderr, "write_cmd failed\n");
+    }
   }
 
   return r;
index cf1f8fe0771bf8ed7ca3137c8f4038df52cbced4..49d1a7282e80495fd5d222cf2bcc630d112358a1 100644 (file)
@@ -42,10 +42,49 @@ extern "C" {
 
 using namespace ad9862;
 
+static const int LIBUSB1_DEBUG = 0;
+
 /*
  * libusb 0.12 / 1.0 compatibility
  */
 
+static const char *
+_get_usb_error_str (int usb_err)
+{
+  switch (usb_err) {
+  case LIBUSB_SUCCESS:
+    return "Success (no error)";
+  case LIBUSB_ERROR_IO:
+    return "Input/output error";
+  case LIBUSB_ERROR_INVALID_PARAM:
+    return "Invalid parameter";
+  case LIBUSB_ERROR_ACCESS:
+    return "Access denied (insufficient permissions";
+  case LIBUSB_ERROR_NO_DEVICE:
+    return "No such device (it may have been disconnected)";
+  case LIBUSB_ERROR_NOT_FOUND:
+    return "Entity not found";
+  case LIBUSB_ERROR_BUSY:
+    return "Resource busy";
+  case LIBUSB_ERROR_TIMEOUT:
+    return "Operation timed out";
+  case LIBUSB_ERROR_OVERFLOW:
+    return "Overflow";
+  case LIBUSB_ERROR_PIPE:
+    return "Pipe error";
+   case LIBUSB_ERROR_INTERRUPTED:
+    return "System call interrupted (perhaps due to signal)";
+  case LIBUSB_ERROR_NO_MEM:
+    return "Insufficient memory";
+  case LIBUSB_ERROR_NOT_SUPPORTED:
+    return "Operation not supported or unimplemented on this platform";
+  case LIBUSB_ERROR_OTHER:
+    return "Unknown error";
+  }
+
+  return "Unknown error";
+}
+
 struct libusb_device *
 _get_usb_device (struct libusb_device_handle *udh)
 {
@@ -58,17 +97,27 @@ _get_usb_device_descriptor(struct libusb_device *q)
   int ret;
   struct libusb_device_descriptor desc;
 
-  if ((ret = libusb_get_device_descriptor(q, &desc)) < 0)
-    fprintf (stderr, "usrp: libusb_get_device_descriptor failed %d\n", ret);
+  ret = libusb_get_device_descriptor(q, &desc);
 
+  if (ret < 0) {
+    fprintf (stderr, "usrp: libusb_get_device_descriptor failed: %s\n",
+             _get_usb_error_str(ret));
+  }
   return desc;
 }
 
 int
 _get_usb_string_descriptor (struct libusb_device_handle *udh, int index,
-                           unsigned char* data, int length)
+                            unsigned char* data, int length)
 {
-  return libusb_get_string_descriptor_ascii (udh, (uint8_t) index, data, length);
+  int ret;
+  ret = libusb_get_string_descriptor_ascii (udh, (uint8_t) index, data, length);
+
+  if (ret < 0) {
+    fprintf (stderr, "usrp: libusb_get_string_descriptor_ascii failed: %s\n",
+             _get_usb_error_str(ret));
+  }
+  return ret;
 }
 
 int
@@ -76,11 +125,16 @@ _usb_control_transfer (struct libusb_device_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);
+  int ret;
+  ret = libusb_control_transfer (udh, request_type, request, value, index,
+                                 data, length, timeout);
+  if (ret < 0) {
+    fprintf (stderr, "usrp: libusb_control_transfer failed: %s\n",
+             _get_usb_error_str(ret));
+  }
+  return ret;
 }
 
-
 // ----------------------------------------------------------------
 
 
@@ -90,7 +144,14 @@ usrp_one_time_init (libusb_context **ctx)
   int ret;
 
   if ((ret = libusb_init (ctx)) < 0)
-    fprintf (stderr, "usrp: libusb_init failed %i\n", ret);
+    fprintf (stderr, "usrp: libusb_init failed: %s\n", _get_usb_error_str(ret));
+
+  // Enable debug verbosity if requested. This will only work if the debug
+  // option is compiled into libusb and may produce a generous amount of output
+  // on stdout. If debug output is not compiled into libusb, this call does
+  // nothing. 
+  if (LIBUSB1_DEBUG)
+    libusb_set_debug(*ctx, 3);
 }
 
 void
@@ -117,7 +178,8 @@ usrp_find_device (int nth, bool fx2_ok_p, libusb_context *ctx)
   size_t i = 0;
 
   if (cnt < 0)
-    fprintf(stderr, "usrp: libusb_get_device_list failed %d\n", cnt);
+    fprintf(stderr, "usrp: libusb_get_device_list failed: %s\n",
+            _get_usb_error_str(cnt));
 
   for (i = 0; i < cnt; i++) {
     q = list[i];
@@ -149,16 +211,17 @@ usrp_open_interface (libusb_device *dev, int interface, int altinterface)
   }
 
   if ((ret = libusb_claim_interface (udh, interface)) < 0) {
-    fprintf (stderr, "%s:usb_claim_interface: failed interface %d\n", __FUNCTION__,interface);
-    fprintf (stderr, "%d\n", ret);
+    fprintf (stderr, "%s:usb_claim_interface: failed interface %d\n",
+             __FUNCTION__, interface);
+    fprintf (stderr, "%s\n", _get_usb_error_str(ret));
     libusb_close (udh);
     return 0;
   }
 
   if ((ret = libusb_set_interface_alt_setting (udh, interface,
-                                                   altinterface)) < 0) {
+                                               altinterface)) < 0) {
     fprintf (stderr, "%s:usb_set_alt_interface: failed\n", __FUNCTION__);
-    fprintf (stderr, "%d\n", ret);
+    fprintf (stderr, "%s\n", _get_usb_error_str(ret));
     libusb_release_interface (udh, interface);
     libusb_close (udh);
     return 0;
@@ -185,18 +248,20 @@ write_cmd (struct libusb_device_handle *udh,
            int request, int value, int index,
            unsigned char *bytes, int len)
 {
-  int   requesttype = (request & 0x80) ? VRT_VENDOR_IN : VRT_VENDOR_OUT;
+  int requesttype = (request & 0x80) ? VRT_VENDOR_IN : VRT_VENDOR_OUT;
 
-  int r = libusb_control_transfer(udh, requesttype, request, value, index,
-                                  (unsigned char *) bytes, len, 1000);
+  int ret = libusb_control_transfer(udh, requesttype, request, value, index,
+                                    bytes, len, 1000);
 
-  if (r < 0){
+  if (ret < 0) {
     // we get EPIPE if the firmware stalls the endpoint.
-    if (r != LIBUSB_ERROR_PIPE) {
-      fprintf (stderr, "libusb_control_transfer failed: %i\n", r);
+    if (ret != LIBUSB_ERROR_PIPE) {
+      fprintf (stderr, "usrp: libusb_control_transfer failed: %s\n",
+               _get_usb_error_str(ret));
+      fprintf (stderr, "usrp: write_cmd failed\n");
     }
   }
 
-  return r;
+  return ret;
 }