From dc1fed40bfd9e12bcdb42e963b9d66f8ea052c5f Mon Sep 17 00:00:00 2001
From: Yegor Yefremov <yegorslists@googlemail.com>
Date: Sun, 2 Jan 2022 20:56:13 +0100
Subject: [PATCH 1/2] avrftdi: don't use the deprecated ftdi_usb_purge_buffers
 routine

Since commit ed46f09c1ccd1351e003a200ba50e3e4778ac478 (Implement
tc[io]flush methods & deprecate broken purge_buffers methods.)
ftdi_usb_purge_buffers() routine is deprecated. Use HAVE_FTDI_TCIOFLUSH
macro to invoke the newly introduced ftdi_tcioflush() routine.
---
 src/CMakeLists.txt    | 3 +++
 src/avrftdi.c         | 4 ++++
 src/cmake_config.h.in | 3 +++
 src/configure.ac      | 2 ++
 4 files changed, 12 insertions(+)

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 89e92c0f..3e5ed177 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -36,6 +36,7 @@ option(DEBUG_CMAKE "Enable debugging output for this CMake project" OFF)
 
 include(CheckIncludeFile)
 include(CheckFunctionExists)
+include(CheckSymbolExists)
 include(GNUInstallDirs)
 
 set(CONFIG_DIR "${CMAKE_INSTALL_FULL_SYSCONFDIR}")
@@ -177,6 +178,8 @@ find_library(HAVE_LIBFTDI1 NAMES ${PREFERRED_LIBFTDI1})
 if(HAVE_LIBFTDI1)
     set(LIB_LIBFTDI1 ${HAVE_LIBFTDI1})
     set(HAVE_LIBFTDI_TYPE_232H 1)
+    set(CMAKE_REQUIRED_LIBRARIES ${LIB_LIBFTDI1} ${LIB_LIBUSB} ${LIB_LIBUSB_1_0})
+    check_symbol_exists(ftdi_tcioflush "libftdi1/ftdi.h" HAVE_FTDI_TCIOFLUSH)
 endif()
 
 # -------------------------------------
diff --git a/src/avrftdi.c b/src/avrftdi.c
index 654614f6..e7437ddc 100644
--- a/src/avrftdi.c
+++ b/src/avrftdi.c
@@ -720,7 +720,11 @@ static int avrftdi_open(PROGRAMMER * pgm, char *port)
 	/* set SPI mode */
 	E(ftdi_set_bitmode(pdata->ftdic, 0, BITMODE_RESET) < 0, pdata->ftdic);
 	E(ftdi_set_bitmode(pdata->ftdic, pdata->pin_direction & 0xff, BITMODE_MPSSE) < 0, pdata->ftdic);
+#ifdef HAVE_FTDI_TCIOFLUSH
+	E(ftdi_tcioflush(pdata->ftdic), pdata->ftdic);
+#else
 	E(ftdi_usb_purge_buffers(pdata->ftdic), pdata->ftdic);
+#endif
 
 	write_flush(pdata);
 
diff --git a/src/cmake_config.h.in b/src/cmake_config.h.in
index 9643546e..e94dbf30 100644
--- a/src/cmake_config.h.in
+++ b/src/cmake_config.h.in
@@ -50,6 +50,9 @@
 /* Define if lex/flex has yylex_destroy */
 #cmakedefine HAVE_YYLEX_DESTROY 1
 
+/* Define if ftdi1 has 'ftdi_tcioflush' function. */
+#cmakedefine HAVE_FTDI_TCIOFLUSH 1
+
 /* ----- Libraries and Headers ----- */
 
 /* Define to 1 if the system has the type `uint_t'. */
diff --git a/src/configure.ac b/src/configure.ac
index df74ea14..32e54f9c 100644
--- a/src/configure.ac
+++ b/src/configure.ac
@@ -195,6 +195,8 @@ if test x$have_libftdi1 = xyes; then
    LIBFTDI1="-lftdi1"
    AC_DEFINE([HAVE_LIBFTDI1])
    AC_SUBST(LIBFTDI1, $LIBFTDI1)
+   LIBS="${LIBFTDI1}"
+   AC_CHECK_FUNCS(ftdi_tcioflush)
 else
    if test x$have_libftdi = xyes; then
       LIBFTDI="-lftdi -lusb"

From 1529277477a4b46049b32955bb94a3a440998fb4 Mon Sep 17 00:00:00 2001
From: Joerg Wunsch <j@uriah.heep.sax.de>
Date: Wed, 5 Jan 2022 17:26:39 +0100
Subject: [PATCH 2/2] When finding LIBFTDI, don't clobber LIBS but extend it

---
 src/configure.ac | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/configure.ac b/src/configure.ac
index 32e54f9c..21442bb9 100644
--- a/src/configure.ac
+++ b/src/configure.ac
@@ -195,7 +195,7 @@ if test x$have_libftdi1 = xyes; then
    LIBFTDI1="-lftdi1"
    AC_DEFINE([HAVE_LIBFTDI1])
    AC_SUBST(LIBFTDI1, $LIBFTDI1)
-   LIBS="${LIBFTDI1}"
+   LIBS="${LIBS} ${LIBFTDI1}"
    AC_CHECK_FUNCS(ftdi_tcioflush)
 else
    if test x$have_libftdi = xyes; then