From febf68ba0afb5543a672f87fb2642bf1648bb074 Mon Sep 17 00:00:00 2001
From: debrouxl <lionel_debroux@yahoo.fr>
Date: Sun, 13 Feb 2011 18:53:21 +0100
Subject: WIP libticables: add glue code for libusb 1.0 support.

---
 libticables/trunk/src/bsd/detect.c     |    2 +-
 libticables/trunk/src/link_usb.c       |    2 ++
 libticables/trunk/src/linux/detect.c   |    2 +-
 libticables/trunk/src/linux/link_dev.c |    2 +-
 libticables/trunk/src/linux/link_usb.c |    4 ++--
 libticables/trunk/src/probe.c          |    6 +++---
 libticables/trunk/src/ticables.c       |   17 ++++++++++++++---
 7 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/libticables/trunk/src/bsd/detect.c b/libticables/trunk/src/bsd/detect.c
index edd0a9b..35306b0 100644
--- a/libticables/trunk/src/bsd/detect.c
+++ b/libticables/trunk/src/bsd/detect.c
@@ -321,7 +321,7 @@ int bsd_check_parport(const char *devname)
 int bsd_check_libusb(void)
 {
 	ticables_info(_("Check for lib-usb support:"));
-#if defined(HAVE_LIBUSB)
+#if defined(HAVE_LIBUSB) || defined(HAVE_LIBUSB_1_0)
 	ticables_info(_("    usb support: available."));
 #else
 	ticables_info(_("    usb support: not compiled."));
diff --git a/libticables/trunk/src/link_usb.c b/libticables/trunk/src/link_usb.c
index 351eca7..ffb6499 100644
--- a/libticables/trunk/src/link_usb.c
+++ b/libticables/trunk/src/link_usb.c
@@ -33,6 +33,8 @@
 
 #ifdef HAVE_LIBUSB
 # include "linux/link_usb.c"
+#elif defined(HAVE_LIBUSB_1_0)
+# include "linux/link_usb1.c"
 #endif
 #ifdef HAVE_LINUX_TICABLE_H
 # include "linux/link_dev.c"
diff --git a/libticables/trunk/src/linux/detect.c b/libticables/trunk/src/linux/detect.c
index 7422f81..50958c0 100644
--- a/libticables/trunk/src/linux/detect.c
+++ b/libticables/trunk/src/linux/detect.c
@@ -401,7 +401,7 @@ int linux_check_parport(const char *devname)
 int linux_check_libusb(void)
 {
 	ticables_info(_("Check for lib-usb support:"));
-#if defined(HAVE_LIBUSB)
+#if defined(HAVE_LIBUSB) || defined(HAVE_LIBUSB_1_0)
 	ticables_info(_("    usb support: available."));
 #else
 	ticables_info(_("    usb support: not compiled."));
diff --git a/libticables/trunk/src/linux/link_dev.c b/libticables/trunk/src/linux/link_dev.c
index 0539d73..af98fc2 100644
--- a/libticables/trunk/src/linux/link_dev.c
+++ b/libticables/trunk/src/linux/link_dev.c
@@ -406,7 +406,7 @@ TIEXPORT1 int TICALL usb_probe_devices2(int **list)
     return 0;
 }
 
-#ifndef HAVE_LIBUSB
+#if !defined(HAVE_LIBUSB) && !defined(HAVE_LIBUSB_1_0)
 TIEXPORT1 int TICALL usb_probe_devices(int **list)
 {
   return usb_probe_devices2(list);
diff --git a/libticables/trunk/src/linux/link_usb.c b/libticables/trunk/src/linux/link_usb.c
index f292fd8..970d5af 100644
--- a/libticables/trunk/src/linux/link_usb.c
+++ b/libticables/trunk/src/linux/link_usb.c
@@ -31,7 +31,7 @@
  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
 
-/* TI-GRAPH LINK USB and direct USB cable support (lib-usb) */
+/* TI-GRAPH LINK USB and direct USB cable support (libusb 0.1.x) */
 
 /* 
    Some important remarks... (http://lpg.ticalc.org/prj_usb/index.html)
@@ -73,7 +73,7 @@
 	IOCTL_INTERNAL_USB_RESET_PORT followed by an IOCTL_INTERNAL_USB_CYCLE_PORT.
 	NSpire simply needs an RESET_PORT.
 */
-                                     
+
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #endif
diff --git a/libticables/trunk/src/probe.c b/libticables/trunk/src/probe.c
index 7ede314..1a3cbbc 100644
--- a/libticables/trunk/src/probe.c
+++ b/libticables/trunk/src/probe.c
@@ -168,9 +168,9 @@ TIEXPORT1 int TICALL ticables_is_usb_enabled(void)
 {
 #if defined(__WIN32__)
 	return !win32_check_libusb();
-#elif defined(__LINUX__) && defined(HAVE_LIBUSB)
+#elif defined(__LINUX__) && (defined(HAVE_LIBUSB) || defined(HAVE_LIBUSB_1_0))
 	return !linux_check_libusb();
-#elif defined(HAVE_LIBUSB)
+#elif (defined(HAVE_LIBUSB) || defined(HAVE_LIBUSB_1_0))
 	return 1;
 #else
 	return 0;
@@ -194,7 +194,7 @@ extern int usb_probe_devices(int **list);
  **/
 TIEXPORT1 int TICALL ticables_get_usb_devices(int **list, int *len)
 {
-#if defined(__WIN32__) || defined(HAVE_LIBUSB)
+#if defined(__WIN32__) || (defined(HAVE_LIBUSB) || defined(HAVE_LIBUSB_1_0))
 	int i, *p;
 	int ret = 0;
 
diff --git a/libticables/trunk/src/ticables.c b/libticables/trunk/src/ticables.c
index 57de980..f84419d 100644
--- a/libticables/trunk/src/ticables.c
+++ b/libticables/trunk/src/ticables.c
@@ -39,6 +39,8 @@
 # include "./win32/usb.h"
 #elif defined(HAVE_LIBUSB)
 # include <usb.h>
+#elif defined(HAVE_LIBUSB_1_0)
+# include <libusb-1.0/libusb.h>
 #endif
 
 #include "gettext.h"
@@ -64,10 +66,10 @@ static CableFncts const *const cables[] =
 #if !defined(NO_CABLE_PAR) && (defined(HAVE_LINUX_PARPORT_H) || defined(__WIN32__))
 	&cable_par,
 #endif
-#if !defined(NO_CABLE_SLV) && (defined(HAVE_LIBUSB) || defined(__WIN32__))
+#if !defined(NO_CABLE_SLV) && (defined(HAVE_LIBUSB) || defined(HAVE_LIBUSB_1_0) || defined(__WIN32__))
 	&cable_slv,
 #endif
-#if !defined(NO_CABLE_SLV) && (defined(HAVE_LIBUSB) || defined(__WIN32__))
+#if !defined(NO_CABLE_SLV) && (defined(HAVE_LIBUSB) || defined(HAVE_LIBUSB_1_0) || defined(__WIN32__))
 	&cable_raw,
 #endif
 #ifndef NO_CABLE_VTI
@@ -146,6 +148,10 @@ TIEXPORT1 int TICALL ticables_library_init(void)
 #if defined(HAVE_LIBUSB)
 	/* init the libusb */
 	usb_init();
+#elif defined(HAVE_LIBUSB_1_0)
+	/* init the libusb */
+	libusb_init(NULL); // XXX NULL ?
+	// TODO: print the error.
 #endif
 
   	return (++ticables_instance);
@@ -162,7 +168,12 @@ TIEXPORT1 int TICALL ticables_library_init(void)
 TIEXPORT1 int
 TICALL ticables_library_exit(void)
 {
-  	return (--ticables_instance);
+#if defined(HAVE_LIBUSB)
+	// No exit function for libusb 0.1.x.
+#elif defined(HAVE_LIBUSB_1_0)
+	libusb_exit(NULL); // XXX NULL ?
+#endif
+	return (--ticables_instance);
 }
 
 /***********/
-- 
1.7.4.15.g7811d

