--- jdk9/common/autoconf/lib-bundled.m4	2017-04-06 06:48:39.000000000 +0200
+++ jdk9/common/autoconf/lib-bundled.m4	2017-04-10 08:32:29.353288558 +0200
@@ -37,6 +37,7 @@
   LIB_SETUP_LIBPNG
   LIB_SETUP_ZLIB
   LIB_SETUP_LCMS
+  LIB_SETUP_PCSCLITE
 ])
 
 ################################################################################
@@ -232,3 +233,41 @@
   AC_SUBST(LCMS_CFLAGS)
   AC_SUBST(LCMS_LIBS)
 ])
+
+################################################################################
+# Setup pcsclite
+################################################################################
+AC_DEFUN_ONCE([LIB_SETUP_PCSCLITE],
+[
+  AC_ARG_WITH(pcsclite, [AS_HELP_STRING([--with-pcsclite],
+     [use pcsclite from build system or OpenJDK source (system, bundled) @<:@bundled@:>@])])
+
+  AC_MSG_CHECKING([for which pcsclite to use])
+
+  # default is bundled
+  DEFAULT_PCSCLITE=bundled
+  # if user didn't specify, use DEFAULT_PCSCLITE
+  if test "x${with_pcsclite}" = "x"; then
+      with_libpng=${DEFAULT_PCSCLITE}
+  fi
+
+  if test "x${with_pcsclite}" = "xbundled"; then
+      USE_EXTERNAL_PCSCLITE=false
+      AC_MSG_RESULT([bundled])
+  elif test "x${with_pcsclite}" = "xsystem"; then
+      PKG_CHECK_MODULES(PCSCLITE, libpcsclite,
+                   [ PCSCLITE_FOUND=yes ],
+                   [ PCSCLITE_FOUND=no ])
+      if test "x${PCSCLITE_FOUND}" = "xyes"; then
+          USE_EXTERNAL_PCSCLITE=true
+          AC_MSG_RESULT([system])
+      else
+          AC_MSG_RESULT([system not found])
+          AC_MSG_ERROR([--with-pcsclite=system specified, but no pcsclite found!])
+      fi
+  else
+      AC_MSG_ERROR([Invalid value of --with-pcsclite: ${with_pcsclite}, use 'system' or 'bundled'])
+  fi
+
+  AC_SUBST(USE_EXTERNAL_PCSCLITE)
+])
--- jdk9/common/autoconf/spec.gmk.in	2017-04-06 06:48:39.000000000 +0200
+++ jdk9/common/autoconf/spec.gmk.in	2017-04-10 08:32:29.357288558 +0200
@@ -699,6 +699,7 @@
 ENABLE_INTREE_EC:=@ENABLE_INTREE_EC@
 USE_EXTERNAL_LIBJPEG:=@USE_EXTERNAL_LIBJPEG@
 USE_EXTERNAL_LIBGIF:=@USE_EXTERNAL_LIBGIF@
+USE_EXTERNAL_LIBPCSCLITE:=@USE_EXTERNAL_LIBPCSCLITE@
 USE_EXTERNAL_LIBZ:=@USE_EXTERNAL_LIBZ@
 LIBZIP_CAN_USE_MMAP:=@LIBZIP_CAN_USE_MMAP@
 MSVCR_DLL:=@MSVCR_DLL@
--- jdk9/jdk/make/lib/Lib-java.smartcardio.gmk	2017-04-06 22:21:05.000000000 +0200
+++ jdk9/jdk/make/lib/Lib-java.smartcardio.gmk	2017-04-10 08:32:29.357288558 +0200
@@ -30,7 +30,7 @@
 LIBJ2PCSC_SRC := $(JDK_TOPDIR)/src/java.smartcardio/share/native/libj2pcsc \
     $(JDK_TOPDIR)/src/java.smartcardio/$(OPENJDK_TARGET_OS_TYPE)/native/libj2pcsc
 LIBJ2PCSC_CPPFLAGS := $(addprefix -I,$(LIBJ2PCSC_SRC)) \
-    -I$(JDK_TOPDIR)/src/java.smartcardio/$(OPENJDK_TARGET_OS_TYPE)/native/libj2pcsc/MUSCLE \
+    -I/usr/include/PCSC -DUSE_SYSTEM_LIBPCSCLITE \
     -I$(SUPPORT_OUTPUTDIR)/headers/java.smartcardio
 
 $(eval $(call SetupNativeCompilation,BUILD_LIBJ2PCSC, \
@@ -43,7 +43,7 @@
     MAPFILE := $(JDK_TOPDIR)/make/mapfiles/libj2pcsc/mapfile-vers, \
     LDFLAGS := $(LDFLAGS_JDKLIB) \
         $(call SET_SHARED_LIBRARY_ORIGIN), \
-    LIBS_unix := $(LIBDL), \
+    LIBS_unix := -lpcsclite $(LIBDL), \
     LIBS_solaris := -lc, \
     LIBS_windows := winscard.lib, \
     VERSIONINFO_RESOURCE := $(GLOBAL_VERSION_INFO_RESOURCE), \
--- jdk9/jdk/src/java.smartcardio/unix/native/libj2pcsc/pcsc_md.c	2017-04-06 22:21:05.000000000 +0200
+++ jdk9/jdk/src/java.smartcardio/unix/native/libj2pcsc/pcsc_md.c	2017-04-10 08:32:29.357288558 +0200
@@ -36,6 +36,7 @@
 
 #include "pcsc_md.h"
 
+#ifndef USE_SYSTEM_LIBPCSCLITE
 void *hModule;
 FPTR_SCardEstablishContext scardEstablishContext;
 FPTR_SCardConnect scardConnect;
@@ -47,6 +48,7 @@
 FPTR_SCardBeginTransaction scardBeginTransaction;
 FPTR_SCardEndTransaction scardEndTransaction;
 FPTR_SCardControl scardControl;
+#endif
 
 /*
  * Throws a Java Exception by name
@@ -75,6 +77,7 @@
     throwByName(env, "java/io/IOException", msg);
 }
 
+#ifndef USE_SYSTEM_LIBPCSCLITE
 void *findFunction(JNIEnv *env, void *hModule, char *functionName) {
     void *fAddress = dlsym(hModule, functionName);
     if (fAddress == NULL) {
@@ -85,9 +88,11 @@
     }
     return fAddress;
 }
+#endif
 
 JNIEXPORT void JNICALL Java_sun_security_smartcardio_PlatformPCSC_initialize
         (JNIEnv *env, jclass thisClass, jstring jLibName) {
+#ifndef USE_SYSTEM_LIBPCSCLITE
     const char *libName = (*env)->GetStringUTFChars(env, jLibName, NULL);
     if (libName == NULL) {
         throwNullPointerException(env, "PCSC library name is null");
@@ -141,4 +146,5 @@
 #else
     scardControl          = (FPTR_SCardControl)         findFunction(env, hModule, "SCardControl132");
 #endif // __APPLE__
+#endif
 }
--- jdk9/jdk/src/java.smartcardio/unix/native/libj2pcsc/pcsc_md.h	2017-04-06 22:21:05.000000000 +0200
+++ jdk9/jdk/src/java.smartcardio/unix/native/libj2pcsc/pcsc_md.h	2017-04-10 08:32:29.357288558 +0200
@@ -23,6 +23,8 @@
  * questions.
  */
 
+#ifndef USE_SYSTEM_LIBPCSCLITE
+
 typedef LONG (*FPTR_SCardEstablishContext)(ULONG dwScope,
                 const void *pvReserved1,
                 const void *pvReserved2,
@@ -110,3 +112,41 @@
 extern FPTR_SCardBeginTransaction scardBeginTransaction;
 extern FPTR_SCardEndTransaction scardEndTransaction;
 extern FPTR_SCardControl scardControl;
+
+#else
+
+#define CALL_SCardEstablishContext(dwScope, pvReserved1, pvReserved2, phContext) \
+    (SCardEstablishContext(dwScope, pvReserved1, pvReserved2, phContext))
+
+#define CALL_SCardConnect(hContext, szReader, dwSharedMode, dwPreferredProtocols, phCard, pdwActiveProtocols) \
+    (SCardConnect(hContext, szReader, dwSharedMode, dwPreferredProtocols, phCard, pdwActiveProtocols))
+
+#define CALL_SCardDisconnect(hCard, dwDisposition) \
+    (SCardDisconnect(hCard, dwDisposition))
+
+#define CALL_SCardStatus(hCard, mszReaderNames, pcchReaderLen, pdwState, pdwProtocol, pbAtr, pcbAtrLen) \
+    (SCardStatus(hCard, mszReaderNames, pcchReaderLen, pdwState, pdwProtocol, pbAtr, pcbAtrLen))
+
+#define CALL_SCardGetStatusChange(hContext, dwTimeout, rgReaderStates, cReaders) \
+    (SCardGetStatusChange(hContext, dwTimeout, rgReaderStates, cReaders))
+
+#define CALL_SCardTransmit(hCard, pioSendPci, pbSendBuffer, cbSendLength, \
+                            pioRecvPci, pbRecvBuffer, pcbRecvLength) \
+    (SCardTransmit(hCard, pioSendPci, pbSendBuffer, cbSendLength, \
+                            pioRecvPci, pbRecvBuffer, pcbRecvLength))
+
+#define CALL_SCardListReaders(hContext, mszGroups, mszReaders, pcchReaders) \
+    (SCardListReaders(hContext, mszGroups, mszReaders, pcchReaders))
+
+#define CALL_SCardBeginTransaction(hCard) \
+    (SCardBeginTransaction(hCard))
+
+#define CALL_SCardEndTransaction(hCard, dwDisposition) \
+    (SCardEndTransaction(hCard, dwDisposition))
+
+#define CALL_SCardControl(hCard, dwControlCode, pbSendBuffer, cbSendLength, \
+            pbRecvBuffer, pcbRecvLength, lpBytesReturned) \
+    (SCardControl(hCard, dwControlCode, pbSendBuffer, cbSendLength, \
+            pbRecvBuffer, pcbRecvLength, lpBytesReturned))
+
+#endif
