Snap for 4778776 from 7ef0003dbb509e2b5c9405d250688a7b3b1310c3 to pi-release

Change-Id: Ie83c9bb3b0b4b18dacf2cc31466f14030e1b626a
diff --git a/libese-spi/p73/lib/phNxpEse_Api.cpp b/libese-spi/p73/lib/phNxpEse_Api.cpp
old mode 100755
new mode 100644
index 6366350..70070fd
--- a/libese-spi/p73/lib/phNxpEse_Api.cpp
+++ b/libese-spi/p73/lib/phNxpEse_Api.cpp
@@ -27,8 +27,6 @@
 #include <phNxpEse_Internal.h>
 
 #define RECIEVE_PACKET_SOF 0xA5
-#define CHAINED_PACKET_WITHSEQN 0x60
-#define CHAINED_PACKET_WITHOUTSEQN 0x20
 #define PH_PAL_ESE_PRINT_PACKET_TX(data, len) \
   ({ phPalEse_print_packet("SEND", data, len); })
 #define PH_PAL_ESE_PRINT_PACKET_RX(data, len) \
@@ -47,7 +45,6 @@
 static unsigned char* phNxpEse_GgetTimerTlvBuffer(unsigned char* timer_buffer,
                                                   unsigned int value);
 #endif
-static int poll_sof_chained_delay = 0;
 /*********************** Global Variables *************************************/
 
 /* ESE Context structure */
@@ -964,16 +961,9 @@
       headerIndex = 0;
       break;
     }
-    /*If it is Chained packet wait for 100 usec*/
-    if (poll_sof_chained_delay == 1) {
-      ALOGD_IF(ese_debug_enabled, "%s Chained Pkt, delay read %dus",
-               __FUNCTION__, WAKE_UP_DELAY * CHAINED_PKT_SCALER);
-      phPalEse_sleep(WAKE_UP_DELAY * CHAINED_PKT_SCALER);
-    } else {
-      ALOGD_IF(ese_debug_enabled, "%s Normal Pkt, delay read %dus",
-               __FUNCTION__, WAKE_UP_DELAY * NAD_POLLING_SCALER);
-      phPalEse_sleep(WAKE_UP_DELAY * NAD_POLLING_SCALER);
-    }
+    ALOGD_IF(ese_debug_enabled, "%s Normal Pkt, delay read %dus", __FUNCTION__,
+             READ_WAKE_UP_DELAY * NAD_POLLING_SCALER);
+    phPalEse_sleep(READ_WAKE_UP_DELAY * NAD_POLLING_SCALER);
   } while (sof_counter < ESE_NAD_POLLING_MAX);
   if (pBuffer[0] == RECIEVE_PACKET_SOF) {
     ALOGD_IF(ese_debug_enabled, "%s SOF FOUND", __FUNCTION__);
@@ -983,16 +973,6 @@
     if (ret < 0) {
       ALOGE("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
     }
-    if ((pBuffer[1] == CHAINED_PACKET_WITHOUTSEQN) ||
-        (pBuffer[1] == CHAINED_PACKET_WITHSEQN)) {
-      poll_sof_chained_delay = 1;
-      ALOGD_IF(ese_debug_enabled, "poll_sof_chained_delay value is %d ",
-               poll_sof_chained_delay);
-    } else {
-      poll_sof_chained_delay = 0;
-      ALOGD_IF(ese_debug_enabled, "poll_sof_chained_delay value is %d ",
-               poll_sof_chained_delay);
-    }
     total_count = 3;
     nNbBytesToRead = pBuffer[2];
     /* Read the Complete data + one byte CRC*/
diff --git a/libese-spi/p73/pal/spi/phNxpEsePal_spi.cpp b/libese-spi/p73/pal/spi/phNxpEsePal_spi.cpp
index 7957199..21057b6 100755
--- a/libese-spi/p73/pal/spi/phNxpEsePal_spi.cpp
+++ b/libese-spi/p73/pal/spi/phNxpEsePal_spi.cpp
@@ -43,6 +43,7 @@
 
 #define MAX_RETRY_CNT 10
 #define HAL_NFC_SPI_DWP_SYNC 21
+#define RF_ON 1
 
 extern int omapi_status;
 extern bool ese_debug_enabled;
@@ -50,7 +51,8 @@
 static int rf_status;
 unsigned long int configNum1, configNum2;
 // Default max retry count for SPI CLT write blocked in secs
-static unsigned long int gsMaxSpiWriteRetryCnt = 10;
+static const uint8_t DEFAULT_MAX_SPI_WRITE_RETRY_COUNT_RF_ON = 10;
+static const uint8_t MAX_SPI_WRITE_RETRY_COUNT_HW_ERR = 3;
 /*******************************************************************************
 **
 ** Function         phPalEse_spi_close
@@ -236,17 +238,11 @@
   } else {
     /* Do Nothing */
   }
-  ALOGE("NXP_SPI_WRITE_TIMEOUT value is... : %ld secs", configNum2);
-  if (configNum2 > 0) {
-    gsMaxSpiWriteRetryCnt = configNum2;
-    ALOGE(" spi_write_timeout Wait time ... : %ld", gsMaxSpiWriteRetryCnt);
-  } else {
-    /* Do Nothing */
-  }
 
+  unsigned int maxRetryCount = 0, retryDelay = 0;
   while (numWrote < nNbBytesToWrite) {
     // usleep(5000);
-    if (rf_status == 0) {
+    if (rf_status != RF_ON) {
       ret = write((intptr_t)pDevHandle, pBuffer + numWrote,
                   nNbBytesToWrite - numWrote);
     } else {
@@ -259,19 +255,23 @@
       return -1;
     } else {
       ALOGE("_spi_write() errno : %x", errno);
-      ALOGD_IF(ese_debug_enabled, "rf_status value is %d", rf_status);
-      if ((errno == EINTR || errno == EAGAIN || rf_status == 1) &&
-          (retryCount < gsMaxSpiWriteRetryCnt)) {
-        /*Configure retry count or timeout here,now its configured for 2*10
-         * secs*/
-        if (retryCount > gsMaxSpiWriteRetryCnt) {
-          ret = -1;
-          break;
-        }
 
+      if (rf_status == RF_ON) {
+        maxRetryCount = (configNum2 > 0)
+                            ? configNum2
+                            : DEFAULT_MAX_SPI_WRITE_RETRY_COUNT_RF_ON;
+        retryDelay = 1000 * WRITE_WAKE_UP_DELAY;
+        ALOGD_IF(ese_debug_enabled, "spi_Write failed as RF is ON.");
+      } else {
+        maxRetryCount = MAX_SPI_WRITE_RETRY_COUNT_HW_ERR;
+        retryDelay = WRITE_WAKE_UP_DELAY;
+        ALOGD_IF(ese_debug_enabled, "spi_write failed");
+      }
+
+      if (retryCount < maxRetryCount) {
         retryCount++;
-        /* 5ms delay to give ESE wake up delay */
-        phPalEse_sleep(1000 * WAKE_UP_DELAY);
+        /*wait for eSE wake up*/
+        phPalEse_sleep(retryDelay);
         ALOGE("_spi_write() failed. Going to retry, counter:%ld !", retryCount);
         continue;
       }
diff --git a/libese-spi/p73/pal/spi/phNxpEsePal_spi.h b/libese-spi/p73/pal/spi/phNxpEsePal_spi.h
index 95ce060..1033694 100755
--- a/libese-spi/p73/pal/spi/phNxpEsePal_spi.h
+++ b/libese-spi/p73/pal/spi/phNxpEsePal_spi.h
@@ -42,15 +42,17 @@
 /*!
  * \brief ESE wakeup delay in case of write error retry
  */
-#define WAKE_UP_DELAY 1000
+#define WRITE_WAKE_UP_DELAY 1000
+
+/*!
+ * \brief ESE SOF polling delay
+ */
+#define READ_WAKE_UP_DELAY 100
+
 /*!
  * \brief ESE wakeup delay in case of write error retry
  */
 #define NAD_POLLING_SCALER 10
-/*!
- * \brief ESE wakeup delay in case of write error retry
- */
-#define CHAINED_PKT_SCALER 1
 
 /* Function declarations */
 /**