From 22477da39e4fff73702afde820b13d4d42ff662c Mon Sep 17 00:00:00 2001
From: Rene Liebscher <r.liebscher@gmx.de>
Date: Thu, 16 May 2013 17:11:35 +0000
Subject: [PATCH] * configure.ac: reactivate check for TYPE_232H, which does
 not exist in libftdi < 0.20 * avrftdi*.*: changed include check for
 libftdi/libusb, deactivate 232H if not available * ft245r.c: changed include
 check for libftdi/libusb

git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@1180 81a1dc3b-b13d-400b-aceb-764788c761c2
---
 ChangeLog         |   6 +++
 avrftdi.c         |  66 ++++++++++--------------------
 avrftdi_private.h |  35 ++++++++--------
 avrftdi_tpi.c     |  18 +-------
 configure.ac      |   4 ++
 ft245r.c          | 102 +++++++++++++++++++++++++---------------------
 6 files changed, 106 insertions(+), 125 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index eccdc044..b1415584 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2013-05-16  Rene Liebscher <R.Liebscher@gmx.de>
+
+	* configure.ac: reactivate check for TYPE_232H, which does not exist in libftdi < 0.20
+	* avrftdi*.*: changed include check for libftdi/libusb, deactivate 232H if not available
+	* ft245r.c: changed include check for libftdi/libusb
+
 2013-05-08  Joerg Wunsch <j.gnu@uriah.heep.sax.de>
 
 	* main.c (main): Add option -l logfile.
diff --git a/avrftdi.c b/avrftdi.c
index 27639292..8bc519e8 100644
--- a/avrftdi.c
+++ b/avrftdi.c
@@ -42,15 +42,24 @@
 #include "avrftdi_tpi.h"
 #include "avrftdi_private.h"
 
-#ifdef HAVE_LIBUSB_1_0
-#if defined(HAVE_LIBFTDI1) || defined(HAVE_LIBFTDI)
+#ifdef DO_NOT_BUILD_AVRFTDI
 
-#include <libusb-1.0/libusb.h>
-#ifdef HAVE_LIBFTDI1
-#include <libftdi1/ftdi.h>
-#elif HAVE_LIBFTDI
-#include <ftdi.h>
-#endif
+static int avrftdi_noftdi_open (struct programmer_t *pgm, char * name)
+{
+	fprintf(stderr,
+		"%s: Error: no libftdi or libusb support. Install libftdi1/libusb-1.0 or libftdi/libusb and run configure/make again.\n",
+		progname);
+
+	return -1;
+}
+
+void avrftdi_initpgm(PROGRAMMER * pgm)
+{
+	strcpy(pgm->type, "avrftdi");
+	pgm->open = avrftdi_noftdi_open;
+}
+
+#else
 
 enum { FTDI_SCK = 0, FTDI_MOSI, FTDI_MISO, FTDI_RESET };
 
@@ -647,10 +656,14 @@ static int avrftdi_open(PROGRAMMER * pgm, char *port)
 			pdata->pin_limit = 16;
 			pdata->rx_buffer_size = 4096;
 			break;
+#ifdef HAVE_LIBFTDI_TYPE_232H
 		case TYPE_232H:
 			pdata->pin_limit = 16;
 			pdata->rx_buffer_size = 1024;
 			break;
+#else
+#warning No support for 232H, use a newer libftdi, version >= 0.20
+#endif
 		case TYPE_4232H:
 			pdata->pin_limit = 8;
 			pdata->rx_buffer_size = 2048;
@@ -1168,43 +1181,8 @@ void avrftdi_initpgm(PROGRAMMER * pgm)
 	pgm->vfy_led = set_led_vfy;
 }
 
-#else /*HAVE_LIBFTDI1*/
+#endif /* DO_NOT_BUILD_AVRFTDI */
 
-static int avrftdi_noftdi_open (struct programmer_t *pgm, char * name)
-{
-	fprintf(stderr,
-		"%s: Error: no libftdi1 support. Install libftdi1 and run configure/make again.\n",
-		progname);
-
-	exit(1);
-}
-
-void avrftdi_initpgm(PROGRAMMER * pgm)
-{
-	strcpy(pgm->type, "avrftdi");
-	pgm->open = avrftdi_noftdi_open;
-}
-
-#endif  /* HAVE_LIBFTDI1 */
-
-#else /*HAVE_LIBUSB_1_0*/
-
-static int avrftdi_nousb_open (struct programmer_t *pgm, char * name)
-{
-	fprintf(stderr,
-		"%s: Error: no USB support. Install libusb-1.0 and run configure/make again.\n",
-		progname);
-
-	exit(1);
-}
-
-void avrftdi_initpgm(PROGRAMMER * pgm)
-{
-	strcpy(pgm->type, "avrftdi");
-	pgm->open = avrftdi_nousb_open;
-}
-
-#endif /*HAVE_LIBUSB_1_0*/
 
 const char avrftdi_desc[] = "Interface to the MPSSE Engine of FTDI Chips using libftdi.";
 
diff --git a/avrftdi_private.h b/avrftdi_private.h
index 141f7852..495c6795 100644
--- a/avrftdi_private.h
+++ b/avrftdi_private.h
@@ -3,16 +3,25 @@
 
 #include <stdint.h>
 
-#ifdef HAVE_LIBUSB_1_0
-#if defined(HAVE_LIBFTDI1) || defined(HAVE_LIBFTDI)
-
-#include <libusb-1.0/libusb.h>
-#ifdef HAVE_LIBFTDI1
-#include <libftdi1/ftdi.h>
-#elif HAVE_LIBFTDI
+#if defined(HAVE_LIBFTDI1) && defined(HAVE_LIBUSB_1_0)
+# if defined(HAVE_LIBUSB_1_0_LIBUSB_H)
+#  include <libusb-1.0/libusb.h>
+# else
+#  include <libusb.h>
+# endif
+# include <libftdi1/ftdi.h>
+# undef HAVE_LIBFTDI_TYPE_232H
+# define HAVE_LIBFTDI_TYPE_232H 1
+#elif defined(HAVE_LIBFTDI) && defined(HAVE_USB_H)
+/* ftdi.h includes usb.h */
 #include <ftdi.h>
+#else 
+#warning No libftdi or libusb support. Install libftdi1/libusb-1.0 or libftdi/libusb and run configure/make again.
+#define DO_NOT_BUILD_AVRFTDI
 #endif
 
+#ifndef DO_NOT_BUILD_AVRFTDI
+
 #include "pgm.h"
 #include "pindefs.h"
 
@@ -75,15 +84,5 @@ typedef struct avrftdi_s {
 
 void avrftdi_log(int level, const char * func, int line, const char * fmt, ...);
 
-#else /* HAVE_LIBFTDI1 */
-
-#warning "libftdi1 required for programmer avrftdi."
-
-#endif  /* HAVE_LIBFTDI1 */
-
-#else /* HAVE_LIBUSB_1_0 */
-
-#warning "libusb-1.0 required for programmer avrftdi."
-
-#endif /* HAVE_LIBUSB_1_0 */
+#endif /* DO_NOT_BUILD_AVRFDTI */
 
diff --git a/avrftdi_tpi.c b/avrftdi_tpi.c
index 9c5cde3b..5d83f069 100644
--- a/avrftdi_tpi.c
+++ b/avrftdi_tpi.c
@@ -15,15 +15,7 @@
 #include "avrftdi_tpi.h"
 #include "avrftdi_private.h"
 
-#ifdef HAVE_LIBUSB_1_0
-#if defined(HAVE_LIBFTDI1) || defined(HAVE_LIBFTDI)
-
-#include <libusb-1.0/libusb.h>
-#ifdef HAVE_LIBFTDI1
-#include <libftdi1/ftdi.h>
-#elif HAVE_LIBFTDI
-#include <ftdi.h>
-#endif
+#ifndef DO_NOT_BUILD_AVRFTDI
 
 static void avrftdi_tpi_disable(PROGRAMMER *);
 static int avrftdi_tpi_program_enable(PROGRAMMER * pgm, AVRPART * p);
@@ -256,11 +248,5 @@ avrftdi_tpi_disable(PROGRAMMER * pgm)
 	log_info("Leaving Programming mode.\n");
 }
 
-#else /* HAVE_LIBFTDI1 */
-
-#endif  /* HAVE_LIBFTDI1 */
-
-#else /* HAVE_LIBUSB_1_0 */
-
-#endif /* HAVE_LIBUSB_1_0 */
+#endif /* DO_NOT_BUILD_AVRFTDI */
 
diff --git a/configure.ac b/configure.ac
index 7c96181c..4bd32b1a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -161,6 +161,10 @@ else
       LIBFTDI="-lftdi -lusb"
       AC_DEFINE([HAVE_LIBFTDI])
       AC_SUBST(LIBFTDI, $LIBFTDI)
+      AC_CHECK_DECL(TYPE_232H,[have_libftdi_FT232H=yes], [], [[#include <ftdi.h>]])
+      if test x$have_libftdi_FT232H = xyes; then
+         AC_DEFINE([HAVE_LIBFTDI_TYPE_232H])
+      fi
    fi
 fi
 AC_CHECK_HEADERS([pthread.h])
diff --git a/ft245r.c b/ft245r.c
index d67c79e5..5c208540 100644
--- a/ft245r.c
+++ b/ft245r.c
@@ -69,7 +69,60 @@
 #include "bitbang.h"
 #include "ft245r.h"
 
-#ifdef HAVE_PTHREAD_H
+#if defined(_WIN32)
+#include <windows.h>
+#endif
+
+#if defined(HAVE_LIBFTDI1) && defined(HAVE_LIBUSB_1_0)
+# if defined(HAVE_LIBUSB_1_0_LIBUSB_H)
+#  include <libusb-1.0/libusb.h>
+# else
+#  include <libusb.h>
+# endif
+# include <libftdi1/ftdi.h>
+#elif defined(HAVE_LIBFTDI) && defined(HAVE_USB_H)
+/* ftdi.h includes usb.h */
+#include <ftdi.h>
+#else 
+#warning No libftdi or libusb support. Install libftdi1/libusb-1.0 or libftdi/libusb and run configure/make again.
+#define DO_NOT_BUILD_FT245R
+#endif
+
+#ifndef HAVE_PTHREAD_H
+
+static int ft245r_nopthread_open (struct programmer_t *pgm, char * name) {
+    fprintf(stderr,
+            "%s: error: no pthread support. Please compile again with pthread installed."
+#if defined(_WIN32)
+            " See http://sourceware.org/pthreads-win32/."
+#endif
+            "\n",
+            progname);
+
+    return -1;
+}
+
+void ft245r_initpgm(PROGRAMMER * pgm) {
+    strcpy(pgm->type, "ftdi_syncbb");
+    pgm->open = ft245r_nopthread_open;
+}
+
+#elif defined(DO_NOT_BUILD_FT245R)
+
+static int ft245r_noftdi_open (struct programmer_t *pgm, char * name) {
+    fprintf(stderr,
+            "%s: error: no libftdi or libusb support. Install libftdi1/libusb-1.0 or libftdi/libusb and run configure/make again.\n",
+            progname);
+
+    return -1;
+}
+
+void ft245r_initpgm(PROGRAMMER * pgm) {
+    strcpy(pgm->type, "ftdi_syncbb");
+    pgm->open = ft245r_noftdi_open;
+}
+
+#else
 
 #include <pthread.h>
 
@@ -86,19 +139,6 @@ typedef dispatch_semaphore_t	sem_t;
 #include <semaphore.h>
 #endif
 
-#if defined(HAVE_LIBFTDI1) || defined(HAVE_LIBFTDI)
-
-#if defined(_WIN32)
-#include <windows.h>
-#endif
-
-#ifdef HAVE_LIBFTDI1
-#include <libftdi1/ftdi.h>
-#elif HAVE_LIBFTDI
-#include <ftdi.h>
-#endif
-
-
 #define FT245R_CYCLES	2
 #define FT245R_FRAGMENT_SIZE  512
 #define REQ_OUTSTANDINGS	10
@@ -589,6 +629,7 @@ cleanup:
 cleanup_no_usb:
     ftdi_deinit (handle);
     free(handle);
+    handle = NULL;
     return -1;
 }
 
@@ -899,39 +940,6 @@ void ft245r_initpgm(PROGRAMMER * pgm) {
     handle = NULL;
 }
 
-#else
-static int ft245r_noftdi_open (struct programmer_t *pgm, char * name) {
-    fprintf(stderr,
-            "%s: error: no ftdi support. Please compile again with libftdi installed.\n",
-            progname);
-
-    exit(1);
-}
-
-void ft245r_initpgm(PROGRAMMER * pgm) {
-    strcpy(pgm->type, "ftdi_syncbb");
-    pgm->open = ft245r_noftdi_open;
-}
-#endif
-#else
-
-static int ft245r_nopthread_open (struct programmer_t *pgm, char * name) {
-    fprintf(stderr,
-            "%s: error: no pthread support. Please compile again with pthread installed."
-#if defined(_WIN32)
-            " See http://sourceware.org/pthreads-win32/."
-#endif
-            "\n",
-            progname);
-
-    exit(1);
-}
-
-void ft245r_initpgm(PROGRAMMER * pgm) {
-    strcpy(pgm->type, "ftdi_syncbb");
-    pgm->open = ft245r_nopthread_open;
-}
-
 #endif
 
 const char ft245r_desc[] = "FT245R/FT232R Synchronous BitBangMode Programmer";