Add support for National Instruments USB-845x devices

Change-Id: I9477b6f0193bfdf20bbe63421a7fb97b597ec549
Signed-off-by: Miklós Márton <martonmiklosqdev@gmail.com>
Reviewed-on: https://review.coreboot.org/c/flashrom/+/25683
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Nico Huber <nico.h@gmx.de>
diff --git a/Makefile b/Makefile
index f0a1b61..5cf4481 100644
--- a/Makefile
+++ b/Makefile
@@ -206,6 +206,10 @@
 # Some functions provided by Microsoft do not work as described in C99 specifications. This macro fixes that
 # for MinGW. See http://sourceforge.net/p/mingw-w64/wiki2/printf%20and%20scanf%20family/ */
 FLASHROM_CFLAGS += -D__USE_MINGW_ANSI_STDIO=1
+
+# National Instruments USB-845x is Windows only for now
+CONFIG_NI845X_SPI ?= no
+
 # For now we disable all PCI-based programmers on Windows/MinGW (no libpci).
 ifeq ($(CONFIG_INTERNAL), yes)
 UNSUPPORTED_FEATURES += CONFIG_INTERNAL=yes
@@ -294,6 +298,15 @@
 endif
 endif
 
+ifneq ($(TARGET_OS), MinGW)
+# NI USB-845x only supported on Windows at the moment
+ifeq ($(CONFIG_NI845X_SPI), yes)
+UNSUPPORTED_FEATURES += CONFIG_NI845X_SPI=yes
+else
+override CONFIG_NI845X_SPI = no
+endif
+endif
+
 ifeq ($(TARGET_OS), libpayload)
 ifeq ($(MAKECMDGOALS),)
 .DEFAULT_GOAL := libflashrom.a
@@ -981,6 +994,26 @@
 PROGRAMMER_OBJS += jlink_spi.o
 endif
 
+ifeq ($(CONFIG_NI845X_SPI), yes)
+FEATURE_CFLAGS += -D'CONFIG_NI845X_SPI=1'
+
+ifeq ($(CONFIG_NI845X_LIBRARY_PATH),)
+# if the user did not specified the NI-845x headers/lib path
+# do a guess for both 32 and 64 bit Windows versions
+NI845X_LIBS += -L'${PROGRAMFILES}\National Instruments\NI-845x\MS Visual C'
+NI845X_LIBS += -L'${PROGRAMFILES(x86)}\National Instruments\NI-845x\MS Visual C'
+NI845X_INCLUDES += -I'${PROGRAMFILES}\National Instruments\NI-845x\MS Visual C'
+NI845X_INCLUDES += -I'${PROGRAMFILES(x86)}\National Instruments\NI-845x\MS Visual C'
+else
+NI845X_LIBS += -L'$(CONFIG_NI845X_LIBRARY_PATH)'
+NI845X_INCLUDES += -I'$(CONFIG_NI845X_LIBRARY_PATH)'
+endif
+
+FEATURE_CFLAGS += $(NI845X_INCLUDES)
+LIBS += -lni845x
+PROGRAMMER_OBJS += ni845x_spi.o
+endif
+
 ifneq ($(NEED_SERIAL), )
 LIB_OBJS += serial.o custom_baud.o
 endif
@@ -1078,7 +1111,7 @@
 endif
 
 $(PROGRAM)$(EXEC_SUFFIX): $(OBJS)
-	$(CC) $(LDFLAGS) -o $(PROGRAM)$(EXEC_SUFFIX) $(OBJS) $(LIBS) $(PCILIBS) $(FEATURE_LIBS) $(USBLIBS) $(USB1LIBS) $(JAYLINKLIBS)
+	$(CC) $(LDFLAGS) -o $(PROGRAM)$(EXEC_SUFFIX) $(OBJS) $(LIBS) $(PCILIBS) $(FEATURE_LIBS) $(USBLIBS) $(USB1LIBS) $(JAYLINKLIBS) $(NI845X_LIBS)
 
 libflashrom.a: $(LIBFLASHROM_OBJS)
 	$(AR) rcs $@ $^
@@ -1390,6 +1423,23 @@
 endef
 export CLOCK_GETTIME_TEST
 
+define NI845X_TEST
+#include <ni845x.h>
+
+int main(int argc, char **argv)
+{
+    (void) argc;
+    (void) argv;
+    char I2C_Device[256];
+    NiHandle Dev_Handle;
+    uInt32 NumberFound = 0;
+    ni845xFindDevice(I2C_Device, &Dev_Handle, &NumberFound);
+    ni845xCloseFindDeviceHandle(Dev_Handle);
+    return 0;
+}
+endef
+export NI845X_TEST
+
 features: compiler
 	@echo "FEATURES := yes" > .features.tmp
 ifneq ($(NEED_LIBFTDI), )
@@ -1435,6 +1485,22 @@
 		( echo "no."; echo "LINUX_I2C_SUPPORT := no" >> .features.tmp ) } \
 		2>>$(BUILD_DETAILS_FILE) | tee -a $(BUILD_DETAILS_FILE)
 endif
+ifeq ($(CONFIG_NI845X_SPI), yes)
+	@printf "Checking for NI USB-845x installation... " | tee -a $(BUILD_DETAILS_FILE)
+	@echo "$$NI845X_TEST" > .featuretest.c
+	@printf "\nexec: %s\n" "$(CC) $(CPPFLAGS) $(CFLAGS) $(NI845X_INCLUDES) $(LDFLAGS) .featuretest.c -o .featuretest$(EXEC_SUFFIX) $(NI845X_LIBS) $(LIBS)" >>$(BUILD_DETAILS_FILE)
+
+	@ { { { { { $(CC) $(CPPFLAGS) $(CFLAGS) $(NI845X_INCLUDES) $(LDFLAGS) .featuretest.c -o .featuretest$(EXEC_SUFFIX) $(NI845X_LIBS) $(LIBS) >&2 && \
+		( echo "yes."; echo "NI845X_SUPPORT := yes" >> .features.tmp ) ||	\
+		{   echo -e "\nUnable to find NI-845x headers or libraries."; \
+			echo "Please pass the NI-845x library path to the make with the CONFIG_NI845X_LIBRARY_PATH parameter,"; \
+			echo "or disable the NI-845x support by specifying make CONFIG_NI845X_SPI=no"; \
+			echo "For the NI-845x 17.0 the library path is:"; \
+			echo " On 32 bit systems: C:\Program Files)\National Instruments\NI-845x\MS Visual C"; \
+			echo " On 64 bit systems: C:\Program Files (x86)\National Instruments\NI-845x\MS Visual C"; \
+			exit 1; }; } \
+		2>>$(BUILD_DETAILS_FILE); echo $? >&3 ; } | tee -a $(BUILD_DETAILS_FILE) >&4; } 3>&1;} | { read rc ; exit ${rc}; } } 4>&1
+endif
 	@printf "Checking for utsname support... " | tee -a $(BUILD_DETAILS_FILE)
 	@echo "$$UTSNAME_TEST" > .featuretest.c
 	@printf "\nexec: %s\n" "$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) .featuretest.c -o .featuretest$(EXEC_SUFFIX)" >>$(BUILD_DETAILS_FILE)