Check for failures from libusb_init() in the libusb-1.0 glue code.

BUG=chromium-os:35038
Review URL: https://codereview.chromium.org/11038043

git-svn-id: http://src.chromium.org/svn/trunk/deps/third_party/libmtp@160216 4ff67af0-8c30-449e-8e8b-ad334ec8d88c
diff --git a/patches/12_check_for_libusb_init_failures.patch b/patches/12_check_for_libusb_init_failures.patch
new file mode 100644
index 0000000..3a0a9ba
--- /dev/null
+++ b/patches/12_check_for_libusb_init_failures.patch
@@ -0,0 +1,76 @@
+Index: src/libusb1-glue.c
+===================================================================
+--- src/libusb1-glue.c	(revision 160213)
++++ src/libusb1-glue.c	(working copy)
+@@ -84,7 +84,7 @@
+ static const int mtp_device_table_size = sizeof(mtp_device_table) / sizeof(LIBMTP_device_entry_t);
+ 
+ // Local functions
+-static void init_usb();
++static LIBMTP_error_number_t init_usb();
+ static void close_usb(PTP_USB* ptp_usb);
+ static int find_interface_and_endpoints(libusb_device *dev,
+ 					uint8_t *interface,
+@@ -125,16 +125,20 @@
+ }
+ 
+ 
+-static void init_usb()
++static LIBMTP_error_number_t init_usb()
+ {
+   /*
+    * Some additional libusb debugging please.
+    * We use the same level debug between MTP and USB.
+    */
+-  libusb_init(NULL);
++  if (libusb_init(NULL) < 0) {
++    LIBMTP_ERROR("Libusb1 init failed\n");
++    return LIBMTP_ERROR_USB_LAYER;
++  }
+ 
+   if ((LIBMTP_debug & LIBMTP_DEBUG_USB) != 0)
+     libusb_set_debug(NULL,9);
++  return LIBMTP_ERROR_NONE;
+ }
+ 
+ /**
+@@ -478,8 +482,11 @@
+   ssize_t nrofdevs;
+   libusb_device **devs = NULL;
+   int ret, i;
++  LIBMTP_error_number_t init_usb_ret;
+ 
+-  init_usb();
++  init_usb_ret = init_usb();
++  if (init_usb_ret != LIBMTP_ERROR_NONE)
++    return init_usb_ret;
+ 
+   nrofdevs = libusb_get_device_list (NULL, &devs);
+   for (i = 0; i < nrofdevs ; i++) {
+@@ -551,8 +558,11 @@
+   ssize_t nrofdevs;
+   libusb_device **devs = NULL;
+   int i;
++  LIBMTP_error_number_t init_usb_ret;
+ 
+-  init_usb();
++  init_usb_ret = init_usb();
++  if (init_usb_ret != LIBMTP_ERROR_NONE)
++    return 0;
+ 
+   nrofdevs = libusb_get_device_list (NULL, &devs);
+   for (i = 0; i < nrofdevs ; i++ ) {
+@@ -1904,9 +1914,12 @@
+   ssize_t nrofdevs;
+   libusb_device **devs = NULL;
+   struct libusb_device_descriptor desc;
++  LIBMTP_error_number_t init_usb_ret;
+ 
+   /* See if we can find this raw device again... */
+-  init_usb();
++  init_usb_ret = init_usb();
++  if (init_usb_ret != LIBMTP_ERROR_NONE)
++    return init_usb_ret;
+ 
+   nrofdevs = libusb_get_device_list (NULL, &devs);
+   for (i = 0; i < nrofdevs ; i++) {
diff --git a/src/libusb1-glue.c b/src/libusb1-glue.c
index 73bdd99..1e70d5f 100644
--- a/src/libusb1-glue.c
+++ b/src/libusb1-glue.c
@@ -84,7 +84,7 @@
 static const int mtp_device_table_size = sizeof(mtp_device_table) / sizeof(LIBMTP_device_entry_t);
 
 // Local functions
-static void init_usb();
+static LIBMTP_error_number_t init_usb();
 static void close_usb(PTP_USB* ptp_usb);
 static int find_interface_and_endpoints(libusb_device *dev,
 					uint8_t *interface,
@@ -125,16 +125,20 @@
 }
 
 
-static void init_usb()
+static LIBMTP_error_number_t init_usb()
 {
   /*
    * Some additional libusb debugging please.
    * We use the same level debug between MTP and USB.
    */
-  libusb_init(NULL);
+  if (libusb_init(NULL) < 0) {
+    LIBMTP_ERROR("Libusb1 init failed\n");
+    return LIBMTP_ERROR_USB_LAYER;
+  }
 
   if ((LIBMTP_debug & LIBMTP_DEBUG_USB) != 0)
     libusb_set_debug(NULL,9);
+  return LIBMTP_ERROR_NONE;
 }
 
 /**
@@ -478,8 +482,11 @@
   ssize_t nrofdevs;
   libusb_device **devs = NULL;
   int ret, i;
+  LIBMTP_error_number_t init_usb_ret;
 
-  init_usb();
+  init_usb_ret = init_usb();
+  if (init_usb_ret != LIBMTP_ERROR_NONE)
+    return init_usb_ret;
 
   nrofdevs = libusb_get_device_list (NULL, &devs);
   for (i = 0; i < nrofdevs ; i++) {
@@ -551,8 +558,11 @@
   ssize_t nrofdevs;
   libusb_device **devs = NULL;
   int i;
+  LIBMTP_error_number_t init_usb_ret;
 
-  init_usb();
+  init_usb_ret = init_usb();
+  if (init_usb_ret != LIBMTP_ERROR_NONE)
+    return 0;
 
   nrofdevs = libusb_get_device_list (NULL, &devs);
   for (i = 0; i < nrofdevs ; i++ ) {
@@ -1904,9 +1914,12 @@
   ssize_t nrofdevs;
   libusb_device **devs = NULL;
   struct libusb_device_descriptor desc;
+  LIBMTP_error_number_t init_usb_ret;
 
   /* See if we can find this raw device again... */
-  init_usb();
+  init_usb_ret = init_usb();
+  if (init_usb_ret != LIBMTP_ERROR_NONE)
+    return init_usb_ret;
 
   nrofdevs = libusb_get_device_list (NULL, &devs);
   for (i = 0; i < nrofdevs ; i++) {