Home | History | Annotate | Download | only in dnld
      1 /*
      2  * Copyright (C) 2012-2014 NXP Semiconductors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include <phTmlNfc.h>
     18 #include <phDnldNfc.h>
     19 #include <phNxpNciHal_Dnld.h>
     20 #include <phNxpNciHal_utils.h>
     21 #include <phNxpLog.h>
     22 #include <phNxpConfig.h>
     23 
     24 /* Macro */
     25 #define PHLIBNFC_IOCTL_DNLD_MAX_ATTEMPTS 3
     26 #define PHLIBNFC_IOCTL_DNLD_GETVERLEN          (0x0BU)
     27 #define PHLIBNFC_IOCTL_DNLD_GETVERLEN_MRA2_1   (0x09U)
     28 #define PHLIBNFC_DNLD_MEM_READ (0xECU)
     29 #define PHLIBNFC_DNLD_MEM_WRITE (0xEDU)
     30 #define PHLIBNFC_DNLD_READ_LOG  (0xEEU)
     31 #define NFC_MEM_READ (0xD0U)
     32 #define NFC_MEM_WRITE (0xD1U)
     33 #define NFC_FW_DOWNLOAD (0x09F7U)
     34 
     35 /* External global variable to get FW version */
     36 extern uint16_t wFwVer;
     37 extern uint16_t wMwVer;
     38 
     39 /* RF Configuration structure */
     40 typedef struct phLibNfc_IoctlSetRfConfig
     41 {
     42     uint8_t  bNumOfParams;    /* Number of Rf configurable parameters to be set */
     43     uint8_t  *pInputBuffer;   /* Buffer containing Rf configurable parameters */
     44     uint8_t  bSetSysPmuFlag;  /* Flag to decide wether to set SystemPmu or no from the first byte */
     45 }phLibNfc_IoctlSetRfConfig;
     46 
     47 /* Structure to hold information from EEPROM */
     48 typedef struct phLibNfc_EELogParams
     49 {
     50     uint16_t     wCurrMwVer;        /* Holds current MW version on the chip */
     51     uint16_t     wCurrFwVer;        /* Holds current FW version on the chip */
     52     uint16_t     wNumDnldTrig;      /* Total number of times dnld has been attempted */
     53     uint16_t     wNumDnldSuccess;   /* Total number of times dnld has been successful */
     54     uint16_t     wNumDnldFail;      /* Total number of times dnld has Failed */
     55     uint16_t     wDnldFailCnt;      /* holds the number of times dnld has failed,will be reset on success */
     56     bool_t       bConfig;           /* Flag to be set in dnld mode after successful dnld,to be reset in NCI Mode
     57                                       after setting the NCI configuration */
     58 } phLibNfc_EELogParams_t;
     59 
     60 /* FW download module context structure */
     61 typedef struct
     62 {
     63     bool_t                       bDnldEepromWrite;  /* Flag to indicate eeprom write request*/
     64     bool_t                       bSkipSeq;       /* Flag to indicate FW download sequence to be skipped or not */
     65     bool_t                       bSkipReset;     /* Flag to indicate Reset cmd to be skipped or not in FW download sequence */
     66     bool_t                       bSkipForce;     /* Flag to indicate Force cmd to be skipped or not in FW recovery sequence */
     67     bool_t                       bPrevSessnOpen; /* Flag to indicate previous download session is open or not */
     68     bool_t                       bLibNfcCtxtMem; /* flag to indicate if mem was allocated for gpphLibNfc_Context */
     69     bool_t                       bDnldInitiated; /* Flag to indicate if fw upgrade was initiated */
     70     bool_t                       bSendNciCmd;    /* Flag to indicate if NCI cmd to be sent or not,after PKU */
     71     uint8_t                      bChipVer;       /* holds the hw chip version */
     72     bool_t                       bDnldRecovery;  /* Flag to indicate if dnld recovery sequence needs to be triggered */
     73     bool_t                       bForceDnld;     /* Flag to indicate if forced download option is enabled */
     74     bool_t                       bRetryDnld;     /* Flag to indicate retry download after successful recovery complete */
     75     uint8_t                      bDnldAttempts;  /* Holds the count of no. of dnld attempts made.max 3 */
     76     uint16_t                     IoctlCode;      /* Ioctl code*/
     77     bool_t                       bDnldAttemptFailed;  /* Flag to indicate last download attempt failed */
     78     NFCSTATUS                    bLastStatus;    /* Holds the actual download write attempt status */
     79     phLibNfc_EELogParams_t       tLogParams;     /* holds the params that could be logged to reserved EE address */
     80     uint8_t                      bClkSrcVal;     /* Holds the System clock source read from config file */
     81     uint8_t                      bClkFreqVal;    /* Holds the System clock frequency read from config file */
     82 } phNxpNciHal_fw_Ioctl_Cntx_t;
     83 
     84 
     85 /* Global variables used in this file only*/
     86 static phNxpNciHal_fw_Ioctl_Cntx_t gphNxpNciHal_fw_IoctlCtx;
     87 
     88 /* Local function prototype */
     89 static NFCSTATUS
     90 phNxpNciHal_fw_dnld_reset(void* pContext, NFCSTATUS status, void* pInfo);
     91 
     92 static void
     93 phNxpNciHal_fw_dnld_reset_cb(void* pContext, NFCSTATUS status, void* pInfo);
     94 
     95 static NFCSTATUS
     96 phNxpNciHal_fw_dnld_force(void* pContext, NFCSTATUS status, void* pInfo);
     97 
     98 static void
     99 phNxpNciHal_fw_dnld_force_cb(void* pContext, NFCSTATUS status, void* pInfo);
    100 
    101 static void
    102 phNxpNciHal_fw_dnld_normal_cb(void* pContext, NFCSTATUS status, void* pInfo);
    103 
    104 static NFCSTATUS
    105 phNxpNciHal_fw_dnld_normal(void* pContext, NFCSTATUS status, void* pInfo);
    106 
    107 static void
    108 phNxpNciHal_fw_dnld_get_version_cb(void* pContext, NFCSTATUS status, void* pInfo);
    109 
    110 static NFCSTATUS
    111 phNxpNciHal_fw_dnld_get_version(void* pContext, NFCSTATUS status, void* pInfo);
    112 
    113 static void
    114 phNxpNciHal_fw_dnld_get_sessn_state_cb(void* pContext, NFCSTATUS status, void* pInfo);
    115 
    116 static NFCSTATUS
    117 phNxpNciHal_fw_dnld_get_sessn_state(void* pContext, NFCSTATUS status, void* pInfo);
    118 
    119 static void
    120 phNxpNciHal_fw_dnld_log_read_cb(void* pContext, NFCSTATUS status, void* pInfo);
    121 
    122 static NFCSTATUS
    123 phNxpNciHal_fw_dnld_log_read(void* pContext, NFCSTATUS status, void* pInfo);
    124 
    125 static void
    126 phNxpNciHal_fw_dnld_write_cb(void* pContext, NFCSTATUS status, void* pInfo);
    127 
    128 static NFCSTATUS
    129 phNxpNciHal_fw_dnld_write(void* pContext, NFCSTATUS status, void* pInfo);
    130 
    131 static void
    132 phNxpNciHal_fw_dnld_chk_integrity_cb(void* pContext, NFCSTATUS status, void* pInfo);
    133 
    134 static NFCSTATUS
    135 phNxpNciHal_fw_dnld_chk_integrity(void* pContext, NFCSTATUS status, void* pInfo);
    136 
    137 static void
    138 phNxpNciHal_fw_dnld_log_cb(void* pContext, NFCSTATUS status, void* pInfo);
    139 
    140 static NFCSTATUS
    141 phNxpNciHal_fw_dnld_log(void* pContext, NFCSTATUS status, void* pInfo);
    142 
    143 static void
    144 phNxpNciHal_fw_dnld_send_ncicmd_Cb(void* pContext, NFCSTATUS status, void* pInfo);
    145 
    146 static NFCSTATUS
    147 phNxpNciHal_fw_dnld_send_ncicmd(void* pContext, NFCSTATUS status, void* pInfo);
    148 
    149 static NFCSTATUS
    150 phNxpNciHal_fw_dnld_recover(void* pContext, NFCSTATUS status, void* pInfo);
    151 
    152 static NFCSTATUS
    153 phNxpNciHal_fw_dnld_complete(void* pContext, NFCSTATUS status, void* pInfo);
    154 
    155 /** Internal function to verify Crc Status byte received during CheckIntegrity */
    156 static NFCSTATUS
    157 phLibNfc_VerifyCrcStatus(uint8_t bCrcStatus);
    158 
    159 static void phNxpNciHal_fw_dnld_recover_cb(void* pContext, NFCSTATUS status,
    160         void* pInfo);
    161 
    162 static NFCSTATUS phNxpNciHal_fw_seq_handler(NFCSTATUS (*seq_handler[])(void* pContext, NFCSTATUS status, void* pInfo));
    163 
    164 /* Array of pointers to start fw download seq */
    165 static NFCSTATUS (*phNxpNciHal_dwnld_seqhandler[])(
    166         void* pContext, NFCSTATUS status, void* pInfo) = {
    167 #if(NFC_NXP_CHIP_TYPE == PN547C2)
    168     phNxpNciHal_fw_dnld_normal,
    169     phNxpNciHal_fw_dnld_normal,
    170 #endif
    171     phNxpNciHal_fw_dnld_get_sessn_state,
    172     phNxpNciHal_fw_dnld_get_version,
    173     phNxpNciHal_fw_dnld_log_read,
    174     phNxpNciHal_fw_dnld_write,
    175     phNxpNciHal_fw_dnld_get_sessn_state,
    176     phNxpNciHal_fw_dnld_get_version,
    177     phNxpNciHal_fw_dnld_log,
    178     phNxpNciHal_fw_dnld_chk_integrity,
    179     NULL
    180 };
    181 
    182 /* Download Recovery Sequence */
    183 static NFCSTATUS (*phNxpNciHal_dwnld_rec_seqhandler[])(
    184         void* pContext, NFCSTATUS status, void* pInfo) = {
    185     phNxpNciHal_fw_dnld_reset,
    186     phNxpNciHal_fw_dnld_force,
    187     phNxpNciHal_fw_dnld_recover,
    188     phNxpNciHal_fw_dnld_send_ncicmd,
    189     NULL
    190 };
    191 
    192 /* Download Log Sequence */
    193 static NFCSTATUS (*phNxpNciHal_dwnld_log_seqhandler[])(
    194         void* pContext, NFCSTATUS status, void* pInfo) = {
    195     phNxpNciHal_fw_dnld_log,
    196     NULL
    197 };
    198 
    199 /*******************************************************************************
    200 **
    201 ** Function         phNxpNciHal_fw_dnld_reset_cb
    202 **
    203 ** Description      Download Reset callback
    204 **
    205 ** Returns          None
    206 **
    207 *******************************************************************************/
    208 static void phNxpNciHal_fw_dnld_reset_cb(void* pContext, NFCSTATUS status,
    209         void* pInfo)
    210 {
    211     phNxpNciHal_Sem_t *p_cb_data = (phNxpNciHal_Sem_t*) pContext;
    212     UNUSED(pInfo);
    213     if (NFCSTATUS_SUCCESS == status)
    214     {
    215         NXPLOG_FWDNLD_D("phNxpNciHal_fw_dnld_reset_cb - Request Successful");
    216     }
    217     else
    218     {
    219         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_reset_cb - Request Failed!!");
    220     }
    221     p_cb_data->status = status;
    222 
    223     SEM_POST(p_cb_data);
    224 
    225     return;
    226 }
    227 
    228 /*******************************************************************************
    229 **
    230 ** Function         phNxpNciHal_fw_dnld_reset
    231 **
    232 ** Description      Download Reset
    233 **
    234 ** Returns          NFCSTATUS_SUCCESS if success
    235 **
    236 *******************************************************************************/
    237 static NFCSTATUS phNxpNciHal_fw_dnld_reset(void* pContext, NFCSTATUS status,
    238         void* pInfo)
    239 {
    240     NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
    241     phNxpNciHal_Sem_t cb_data;
    242     UNUSED(pContext);
    243     UNUSED(status);
    244     UNUSED(pInfo);
    245     if((TRUE == (gphNxpNciHal_fw_IoctlCtx.bSkipSeq)) || (TRUE == (gphNxpNciHal_fw_IoctlCtx.bSkipReset)))
    246     {
    247         if(TRUE == (gphNxpNciHal_fw_IoctlCtx.bSkipReset))
    248         {
    249             (gphNxpNciHal_fw_IoctlCtx.bSkipReset) = FALSE;
    250         }
    251         return NFCSTATUS_SUCCESS;
    252     }
    253 
    254     if (phNxpNciHal_init_cb_data(&cb_data, NULL) != NFCSTATUS_SUCCESS)
    255     {
    256         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_reset Create dnld_cb_data  failed");
    257         return NFCSTATUS_FAILED;
    258     }
    259     wStatus = phDnldNfc_Reset((pphDnldNfc_RspCb_t)&phNxpNciHal_fw_dnld_reset_cb, (void*) &cb_data);
    260 
    261     if (wStatus != NFCSTATUS_PENDING)
    262     {
    263         NXPLOG_FWDNLD_E("phDnldNfc_Reset failed");
    264         wStatus = NFCSTATUS_FAILED;
    265         goto clean_and_return;
    266     }
    267 
    268     /* Wait for callback response */
    269     if (SEM_WAIT(cb_data))
    270     {
    271         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_reset semaphore error");
    272         wStatus = NFCSTATUS_FAILED;
    273         goto clean_and_return;
    274     }
    275 
    276     if (cb_data.status != NFCSTATUS_SUCCESS)
    277     {
    278         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_reset cb failed");
    279         wStatus = NFCSTATUS_FAILED;
    280         goto clean_and_return;
    281     }
    282 
    283     wStatus = NFCSTATUS_SUCCESS;
    284 
    285 clean_and_return:
    286     phNxpNciHal_cleanup_cb_data(&cb_data);
    287 
    288 
    289     return wStatus;
    290 }
    291 
    292 /*******************************************************************************
    293 **
    294 ** Function         phNxpNciHal_fw_dnld_normal_cb
    295 **
    296 ** Description      Download Normal callback
    297 **
    298 ** Returns          None
    299 **
    300 *******************************************************************************/
    301 static void phNxpNciHal_fw_dnld_normal_cb(void* pContext, NFCSTATUS status,
    302         void* pInfo)
    303 {
    304     phNxpNciHal_Sem_t *p_cb_data = (phNxpNciHal_Sem_t*) pContext;
    305     UNUSED(pInfo);
    306     if (NFCSTATUS_SUCCESS == status)
    307     {
    308         NXPLOG_FWDNLD_D("phNxpNciHal_fw_dnld_normal_cb - Request Successful");
    309     }
    310     else
    311     {
    312         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_normal_cb - Request Failed!!");
    313         /* In this fail scenario trick the sequence handler to call next recover sequence */
    314         status = NFCSTATUS_SUCCESS;
    315     }
    316     p_cb_data->status = status;
    317 
    318     SEM_POST(p_cb_data);
    319     usleep(1000 * 10);
    320 
    321     return;
    322 }
    323 
    324 /*******************************************************************************
    325 **
    326 ** Function         phNxpNciHal_fw_dnld_force_cb
    327 **
    328 ** Description      Download Force callback
    329 **
    330 ** Returns          None
    331 **
    332 *******************************************************************************/
    333 static void phNxpNciHal_fw_dnld_force_cb(void* pContext, NFCSTATUS status,
    334         void* pInfo)
    335 {
    336     phNxpNciHal_Sem_t *p_cb_data = (phNxpNciHal_Sem_t*) pContext;
    337     UNUSED(pInfo);
    338     if (NFCSTATUS_SUCCESS == status)
    339     {
    340         NXPLOG_FWDNLD_D("phLibNfc_DnldForceCb - Request Successful");
    341         (gphNxpNciHal_fw_IoctlCtx.bDnldRecovery) = FALSE;
    342         (gphNxpNciHal_fw_IoctlCtx.bRetryDnld) = TRUE;
    343         (gphNxpNciHal_fw_IoctlCtx.bSkipReset) = TRUE;
    344     }
    345     else
    346     {
    347         /* In this fail scenario trick the sequence handler to call next recover sequence */
    348         status = NFCSTATUS_SUCCESS;
    349         NXPLOG_FWDNLD_E("phLibNfc_DnldForceCb - Request Failed!!");
    350 
    351     }
    352     p_cb_data->status = status;
    353 
    354     SEM_POST(p_cb_data);
    355     usleep(1000 * 10);
    356 
    357     return;
    358 }
    359 
    360 /*******************************************************************************
    361 **
    362 ** Function         phNxpNciHal_fw_dnld_normal
    363 **
    364 ** Description      Download Normal
    365 **
    366 ** Returns          NFCSTATUS_SUCCESS if success
    367 **
    368 *******************************************************************************/
    369 static NFCSTATUS phNxpNciHal_fw_dnld_normal(void* pContext, NFCSTATUS status,
    370         void* pInfo)
    371 {
    372     NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
    373     uint8_t bClkVal[2];
    374     phDnldNfc_Buff_t tData;
    375     phNxpNciHal_Sem_t cb_data;
    376     UNUSED(pContext);
    377     UNUSED(status);
    378     UNUSED(pInfo);
    379     if(TRUE == (gphNxpNciHal_fw_IoctlCtx.bSkipForce))
    380     {
    381         return NFCSTATUS_SUCCESS;
    382     }
    383     else
    384     {
    385         /*
    386         bClkVal[0] = NXP_SYS_CLK_SRC_SEL;
    387         bClkVal[1] = NXP_SYS_CLK_FREQ_SEL;
    388         */
    389         bClkVal[0] = gphNxpNciHal_fw_IoctlCtx.bClkSrcVal;
    390         bClkVal[1] = gphNxpNciHal_fw_IoctlCtx.bClkFreqVal;
    391 
    392         (tData.pBuff) = bClkVal;
    393         (tData.wLen) = sizeof(bClkVal);
    394 
    395         if(TRUE == (gphNxpNciHal_fw_IoctlCtx.bDnldRecovery))
    396         {
    397             (gphNxpNciHal_fw_IoctlCtx.bDnldAttempts)++;
    398         }
    399 
    400         if (phNxpNciHal_init_cb_data(&cb_data, NULL) != NFCSTATUS_SUCCESS)
    401         {
    402             NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_reset Create dnld_cb_data  failed");
    403             return NFCSTATUS_FAILED;
    404         }
    405         wStatus = phDnldNfc_Force(&tData,(pphDnldNfc_RspCb_t)&phNxpNciHal_fw_dnld_normal_cb, (void*) &cb_data);
    406 
    407         if(NFCSTATUS_PENDING != wStatus)
    408         {
    409             NXPLOG_FWDNLD_E("phDnldNfc_Normal failed");
    410             (gphNxpNciHal_fw_IoctlCtx.bSkipForce) = FALSE;
    411             (gphNxpNciHal_fw_IoctlCtx.bRetryDnld) = FALSE;
    412             goto clean_and_return;
    413         }
    414     }
    415 
    416     /* Wait for callback response */
    417     if (SEM_WAIT(cb_data))
    418     {
    419         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_normal semaphore error");
    420         wStatus = NFCSTATUS_FAILED;
    421         goto clean_and_return;
    422     }
    423 
    424     if (cb_data.status != NFCSTATUS_SUCCESS)
    425     {
    426         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_normal cb failed");
    427         wStatus = NFCSTATUS_FAILED;
    428         goto clean_and_return;
    429     }
    430 
    431     wStatus = NFCSTATUS_SUCCESS;
    432 
    433 clean_and_return:
    434     phNxpNciHal_cleanup_cb_data(&cb_data);
    435 
    436     return wStatus;
    437 }
    438 
    439 /*******************************************************************************
    440 **
    441 ** Function         phNxpNciHal_fw_dnld_force
    442 **
    443 ** Description      Download Force
    444 **
    445 ** Returns          NFCSTATUS_SUCCESS if success
    446 **
    447 *******************************************************************************/
    448 static NFCSTATUS phNxpNciHal_fw_dnld_force(void* pContext, NFCSTATUS status,
    449         void* pInfo)
    450 {
    451     NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
    452     uint8_t bClkVal[2];
    453     phDnldNfc_Buff_t tData;
    454     phNxpNciHal_Sem_t cb_data;
    455     UNUSED(pContext);
    456     UNUSED(status);
    457     UNUSED(pInfo);
    458     if(TRUE == (gphNxpNciHal_fw_IoctlCtx.bSkipForce))
    459     {
    460         return NFCSTATUS_SUCCESS;
    461     }
    462     else
    463     {
    464         /*
    465         bClkVal[0] = NXP_SYS_CLK_SRC_SEL;
    466         bClkVal[1] = NXP_SYS_CLK_FREQ_SEL;
    467         */
    468         bClkVal[0] = gphNxpNciHal_fw_IoctlCtx.bClkSrcVal;
    469         bClkVal[1] = gphNxpNciHal_fw_IoctlCtx.bClkFreqVal;
    470 
    471         (tData.pBuff) = bClkVal;
    472         (tData.wLen) = sizeof(bClkVal);
    473 
    474         if(TRUE == (gphNxpNciHal_fw_IoctlCtx.bDnldRecovery))
    475         {
    476             (gphNxpNciHal_fw_IoctlCtx.bDnldAttempts)++;
    477         }
    478 
    479         if (phNxpNciHal_init_cb_data(&cb_data, NULL) != NFCSTATUS_SUCCESS)
    480         {
    481             NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_reset Create dnld_cb_data  failed");
    482             return NFCSTATUS_FAILED;
    483         }
    484         wStatus = phDnldNfc_Force(&tData,(pphDnldNfc_RspCb_t)&phNxpNciHal_fw_dnld_force_cb, (void*) &cb_data);
    485 
    486         if(NFCSTATUS_PENDING != wStatus)
    487         {
    488             NXPLOG_FWDNLD_E("phDnldNfc_Force failed");
    489             (gphNxpNciHal_fw_IoctlCtx.bSkipForce) = FALSE;
    490             (gphNxpNciHal_fw_IoctlCtx.bRetryDnld) = FALSE;
    491             goto clean_and_return;
    492         }
    493     }
    494 
    495     /* Wait for callback response */
    496     if (SEM_WAIT(cb_data))
    497     {
    498         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_force semaphore error");
    499         wStatus = NFCSTATUS_FAILED;
    500         goto clean_and_return;
    501     }
    502 
    503     if (cb_data.status != NFCSTATUS_SUCCESS)
    504     {
    505         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_force cb failed");
    506         wStatus = NFCSTATUS_FAILED;
    507         goto clean_and_return;
    508     }
    509 
    510     wStatus = NFCSTATUS_SUCCESS;
    511 
    512 clean_and_return:
    513     phNxpNciHal_cleanup_cb_data(&cb_data);
    514 
    515     return wStatus;
    516 }
    517 
    518 /*******************************************************************************
    519 **
    520 ** Function         phNxpNciHal_fw_dnld_get_version_cb
    521 **
    522 ** Description      Download Get version callback
    523 **
    524 ** Returns          None
    525 **
    526 *******************************************************************************/
    527 static void phNxpNciHal_fw_dnld_get_version_cb(void* pContext,
    528         NFCSTATUS status, void* pInfo)
    529 {
    530     phNxpNciHal_Sem_t *p_cb_data = (phNxpNciHal_Sem_t*) pContext;
    531     NFCSTATUS wStatus = status;
    532     pphDnldNfc_Buff_t   pRespBuff;
    533     uint16_t wFwVern = 0;
    534     uint16_t wMwVern = 0;
    535     uint8_t bHwVer = 0;
    536     uint8_t bExpectedLen = 0;
    537     uint8_t bNewVer[2];
    538     uint8_t bCurrVer[2];
    539 
    540     if ((NFCSTATUS_SUCCESS == wStatus) && (NULL != pInfo))
    541     {
    542         NXPLOG_FWDNLD_D ("phNxpNciHal_fw_dnld_get_version_cb - Request Successful");
    543 
    544         pRespBuff = (pphDnldNfc_Buff_t) pInfo;
    545 
    546         if ((0 != pRespBuff->wLen) && (NULL != pRespBuff->pBuff))
    547         {
    548             bHwVer = (pRespBuff->pBuff[0]);
    549             bHwVer &= 0x0F; /* 0x0F is the mask to extract chip version */
    550 
    551             if ((PHDNLDNFC_HWVER_MRA2_1 == bHwVer) || (PHDNLDNFC_HWVER_MRA2_2 == bHwVer) ||
    552                     (PHDNLDNFC_HWVER_PN548AD_MRA1_0 == bHwVer))
    553             {
    554                 bExpectedLen = PHLIBNFC_IOCTL_DNLD_GETVERLEN_MRA2_1;
    555                 (gphNxpNciHal_fw_IoctlCtx.bChipVer) = bHwVer;
    556             }
    557             else if ((bHwVer >= PHDNLDNFC_HWVER_MRA1_0) && (bHwVer
    558                         <= PHDNLDNFC_HWVER_MRA2_0))
    559             {
    560                 bExpectedLen = PHLIBNFC_IOCTL_DNLD_GETVERLEN;
    561                 (gphNxpNciHal_fw_IoctlCtx.bChipVer) = bHwVer;
    562             }
    563             else
    564             {
    565                 wStatus = NFCSTATUS_FAILED;
    566                 NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_get_version_cb - Invalid ChipVersion!!");
    567             }
    568         }
    569         else
    570         {
    571             wStatus = NFCSTATUS_FAILED;
    572             NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_get_version_cb - Version Resp Buff Invalid...\n");
    573         }
    574 
    575         if ((NFCSTATUS_SUCCESS == wStatus) && (bExpectedLen == pRespBuff->wLen)
    576                 && (NULL != pRespBuff->pBuff))
    577         {
    578             NXPLOG_FWDNLD_D("phNxpNciHal_fw_dnld_get_version_cb - Valid Version Resp Buff!!...\n");
    579 
    580             /* Validate version details to confirm if continue with the next sequence of Operations. */
    581             memcpy(bCurrVer, &(pRespBuff->pBuff[bExpectedLen - 2]),
    582                     sizeof(bCurrVer));
    583             wFwVern = wFwVer;
    584             wMwVern = wMwVer;
    585 
    586             memcpy(bNewVer,&wFwVern,sizeof(bNewVer));
    587 
    588             /* check if the ROM code version and FW Major version is valid for the chip*/
    589             /* ES2.2 Rom Version - 0x7 and Valid FW Major Version - 0x1 */
    590             if ((pRespBuff->pBuff[1] == 0x07) &&(bNewVer[1] !=0x01))
    591             {
    592                 NXPLOG_FWDNLD_E("C1 FW on C2 chip is not allowed - FW Major Version!= 1 on ES2.2");
    593                 wStatus = NFCSTATUS_NOT_ALLOWED;
    594             }
    595             /* Major Version number check */
    596             else if((FALSE == (gphNxpNciHal_fw_IoctlCtx.bDnldInitiated)) && (bNewVer[1] < bCurrVer[1]))
    597             {
    598                 NXPLOG_FWDNLD_E("Version Check Failed - MajorVerNum Mismatch\n");
    599                 wStatus = NFCSTATUS_NOT_ALLOWED;
    600             }
    601             /* Minor Version number check - before download.*/
    602             else if((FALSE == (gphNxpNciHal_fw_IoctlCtx.bDnldInitiated)) && ((bNewVer[0] == bCurrVer[0]) &&
    603                         (bNewVer[1] == bCurrVer[1])))
    604             {
    605                 wStatus = NFCSTATUS_SUCCESS;
    606 #if (PH_LIBNFC_ENABLE_FORCE_DOWNLOAD == 0)
    607                 NXPLOG_FWDNLD_D("Version Already UpToDate!!\n");
    608                 (gphNxpNciHal_fw_IoctlCtx.bSkipSeq) = TRUE;
    609 #else
    610                 (gphNxpNciHal_fw_IoctlCtx.bForceDnld) = TRUE;
    611 #endif
    612 
    613             }
    614             /* Minor Version number check - after download
    615              * after download, we should get the same version information.*/
    616             else if ((TRUE == (gphNxpNciHal_fw_IoctlCtx.bDnldInitiated)) && ((bNewVer[0] != bCurrVer[0]) ||
    617                         (bNewVer[1] != bCurrVer[1])))
    618             {
    619                 NXPLOG_FWDNLD_E("Version Not Updated After Download!!\n");
    620                 wStatus = NFCSTATUS_FAILED;
    621             }
    622             else
    623             {
    624                 NXPLOG_FWDNLD_D("Version Check Successful\n");
    625                 /* Store the Mw & Fw Version for updating in EEPROM Log Area after successful download */
    626                 if(TRUE == (gphNxpNciHal_fw_IoctlCtx.bDnldInitiated))
    627                 {
    628                     NXPLOG_FWDNLD_W("Updating Fw & Mw Versions..");
    629                     (gphNxpNciHal_fw_IoctlCtx.tLogParams.wCurrMwVer) = wMwVern;
    630                     (gphNxpNciHal_fw_IoctlCtx.tLogParams.wCurrFwVer) = wFwVern;
    631                 }
    632 
    633             }
    634         }
    635         else
    636         {
    637             NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_get_version_cb - Version Resp Buff Invalid...\n");
    638         }
    639     }
    640     else
    641     {
    642         wStatus = NFCSTATUS_FAILED;
    643         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_get_version_cb - Request Failed!!");
    644     }
    645 
    646     p_cb_data->status = wStatus;
    647     SEM_POST(p_cb_data);
    648     return;
    649 }
    650 
    651 /*******************************************************************************
    652 **
    653 ** Function         phNxpNciHal_fw_dnld_get_version
    654 **
    655 ** Description      Download Get version
    656 **
    657 ** Returns          NFCSTATUS_SUCCESS if success
    658 **
    659 *******************************************************************************/
    660 static NFCSTATUS phNxpNciHal_fw_dnld_get_version(void* pContext,
    661         NFCSTATUS status, void* pInfo)
    662 {
    663     NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
    664     phNxpNciHal_Sem_t cb_data;
    665     static uint8_t bGetVerRes[11];
    666     phDnldNfc_Buff_t tDnldBuff;
    667     UNUSED(pContext);
    668     UNUSED(status);
    669     UNUSED(pInfo);
    670     if((TRUE == (gphNxpNciHal_fw_IoctlCtx.bSkipSeq)) ||
    671             (TRUE == (gphNxpNciHal_fw_IoctlCtx.bPrevSessnOpen)))
    672     {
    673         return NFCSTATUS_SUCCESS;
    674     }
    675 
    676     if (phNxpNciHal_init_cb_data(&cb_data, NULL) != NFCSTATUS_SUCCESS)
    677     {
    678         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_get_version cb_data creation failed");
    679         return NFCSTATUS_FAILED;
    680     }
    681 
    682     tDnldBuff.pBuff = bGetVerRes;
    683     tDnldBuff.wLen = sizeof(bGetVerRes);
    684 
    685     wStatus = phDnldNfc_GetVersion(&tDnldBuff,
    686             (pphDnldNfc_RspCb_t) &phNxpNciHal_fw_dnld_get_version_cb,
    687             (void*) &cb_data);
    688     if (wStatus != NFCSTATUS_PENDING)
    689     {
    690         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_get_version failed");
    691         wStatus = NFCSTATUS_FAILED;
    692         goto clean_and_return;
    693     }
    694     /* Wait for callback response */
    695     if (SEM_WAIT(cb_data))
    696     {
    697         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_get_version semaphore error");
    698         wStatus = NFCSTATUS_FAILED;
    699         goto clean_and_return;
    700     }
    701 
    702     if (cb_data.status != NFCSTATUS_SUCCESS)
    703     {
    704         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_get_version cb failed");
    705         wStatus = NFCSTATUS_FAILED;
    706         goto clean_and_return;
    707     }
    708 
    709     wStatus = NFCSTATUS_SUCCESS;
    710 
    711 clean_and_return:
    712     phNxpNciHal_cleanup_cb_data(&cb_data);
    713 
    714     return wStatus;
    715 }
    716 
    717 /*******************************************************************************
    718 **
    719 ** Function         phNxpNciHal_fw_dnld_get_sessn_state_cb
    720 **
    721 ** Description      Download Get session state callback
    722 **
    723 ** Returns          None
    724 **
    725 *******************************************************************************/
    726 static void phNxpNciHal_fw_dnld_get_sessn_state_cb(void* pContext,
    727         NFCSTATUS status, void* pInfo)
    728 {
    729     phNxpNciHal_Sem_t *p_cb_data = (phNxpNciHal_Sem_t*) pContext;
    730     NFCSTATUS wStatus = status;
    731     pphDnldNfc_Buff_t pRespBuff;
    732     if ((NFCSTATUS_SUCCESS == wStatus) && (NULL != pInfo))
    733     {
    734         NXPLOG_FWDNLD_D("phNxpNciHal_fw_dnld_get_sessn_state_cb - Request Successful");
    735 
    736         pRespBuff = (pphDnldNfc_Buff_t) pInfo;
    737 
    738         if ((3 == (pRespBuff->wLen)) && (NULL != (pRespBuff->pBuff)))
    739         {
    740             NXPLOG_FWDNLD_D("phNxpNciHal_fw_dnld_get_sessn_state_cb - Valid Session State Resp Buff!!...");
    741 
    742             if (phDnldNfc_LCOper == pRespBuff->pBuff[2])
    743             {
    744                 if (PHLIBNFC_FWDNLD_SESSNOPEN == pRespBuff->pBuff[0])
    745                 {
    746                     NXPLOG_FWDNLD_E("Prev Fw Upgrade Session still Open..");
    747                     (gphNxpNciHal_fw_IoctlCtx.bPrevSessnOpen) = TRUE;
    748                     if(TRUE == (gphNxpNciHal_fw_IoctlCtx.bDnldInitiated))
    749                     {
    750                         NXPLOG_FWDNLD_D("Session still Open after Prev Fw Upgrade attempt!!");
    751 
    752                         if((gphNxpNciHal_fw_IoctlCtx.bDnldAttempts) < PHLIBNFC_IOCTL_DNLD_MAX_ATTEMPTS)
    753                         {
    754                             NXPLOG_FWDNLD_W("Setting Dnld Retry ..");
    755                             (gphNxpNciHal_fw_IoctlCtx.bRetryDnld) = TRUE;
    756                         }
    757                         else
    758                         {
    759                             NXPLOG_FWDNLD_E("Max Dnld Retry Counts Exceeded!!");
    760                             (gphNxpNciHal_fw_IoctlCtx.bRetryDnld) = FALSE;
    761                         }
    762                         wStatus = NFCSTATUS_FAILED;
    763                     }
    764                 }
    765                 else
    766                 {
    767                     gphNxpNciHal_fw_IoctlCtx.bPrevSessnOpen = FALSE;
    768                 }
    769             }
    770             else
    771             {
    772                 wStatus = NFCSTATUS_FAILED;
    773                 NXPLOG_FWDNLD_E("NFCC not in Operational State..Fw Upgrade not allowed!!");
    774             }
    775         }
    776         else
    777         {
    778             wStatus = NFCSTATUS_FAILED;
    779             NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_get_sessn_state_cb - Session State Resp Buff Invalid...");
    780         }
    781     }
    782     else
    783     {
    784         wStatus = NFCSTATUS_FAILED;
    785         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_get_sessn_state_cb - Request Failed!!");
    786     }
    787 
    788     p_cb_data->status = wStatus;
    789 
    790     SEM_POST(p_cb_data);
    791 
    792     return;
    793 }
    794 
    795 /*******************************************************************************
    796 **
    797 ** Function         phNxpNciHal_fw_dnld_get_sessn_state
    798 **
    799 ** Description      Download Get session state
    800 **
    801 ** Returns          NFCSTATUS_SUCCESS if success
    802 **
    803 *******************************************************************************/
    804 static NFCSTATUS phNxpNciHal_fw_dnld_get_sessn_state(void* pContext,
    805         NFCSTATUS status, void* pInfo)
    806 {
    807     phDnldNfc_Buff_t tDnldBuff;
    808     static uint8_t bGSnStateRes[3];
    809     NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
    810     phNxpNciHal_Sem_t cb_data;
    811     UNUSED(pContext);
    812     UNUSED(status);
    813     UNUSED(pInfo);
    814     if (TRUE == gphNxpNciHal_fw_IoctlCtx.bSkipSeq)
    815     {
    816         return NFCSTATUS_SUCCESS;
    817     }
    818 
    819     if (phNxpNciHal_init_cb_data(&cb_data, NULL) != NFCSTATUS_SUCCESS)
    820     {
    821         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_get_version cb_data creation failed");
    822         return NFCSTATUS_FAILED;
    823     }
    824 
    825     tDnldBuff.pBuff = bGSnStateRes;
    826     tDnldBuff.wLen = sizeof(bGSnStateRes);
    827 
    828     wStatus = phDnldNfc_GetSessionState(&tDnldBuff,
    829             &phNxpNciHal_fw_dnld_get_sessn_state_cb, (void *) &cb_data);
    830     if (wStatus != NFCSTATUS_PENDING)
    831     {
    832         NXPLOG_FWDNLD_E("phDnldNfc_GetSessionState failed");
    833         wStatus = NFCSTATUS_FAILED;
    834         goto clean_and_return;
    835     }
    836 
    837     /* Wait for callback response */
    838     if (SEM_WAIT(cb_data))
    839     {
    840         NXPLOG_FWDNLD_E("phDnldNfc_GetSessionState semaphore error");
    841         wStatus = NFCSTATUS_FAILED;
    842         goto clean_and_return;
    843     }
    844 
    845     if (cb_data.status != NFCSTATUS_SUCCESS)
    846     {
    847         NXPLOG_FWDNLD_E("phDnldNfc_GetSessionState cb failed");
    848         wStatus = NFCSTATUS_FAILED;
    849         goto clean_and_return;
    850     }
    851 
    852     wStatus = NFCSTATUS_SUCCESS;
    853 
    854 clean_and_return:
    855     phNxpNciHal_cleanup_cb_data(&cb_data);
    856 
    857     return wStatus;
    858 }
    859 
    860 /*******************************************************************************
    861 **
    862 ** Function         phNxpNciHal_fw_dnld_log_read_cb
    863 **
    864 ** Description      Download Logread callback
    865 **
    866 ** Returns          None
    867 **
    868 *******************************************************************************/
    869 static void phNxpNciHal_fw_dnld_log_read_cb(void* pContext, NFCSTATUS status,
    870         void* pInfo)
    871 {
    872     phNxpNciHal_Sem_t *p_cb_data = (phNxpNciHal_Sem_t*) pContext;
    873 
    874     if((NFCSTATUS_SUCCESS == status) && (NULL != pInfo))
    875     {
    876         NXPLOG_FWDNLD_D("phNxpNciHal_fw_dnld_log_read_cb - Request Successful");
    877     }
    878     else
    879     {
    880         status = NFCSTATUS_FAILED;
    881         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_log_read_cb - Request Failed!!");
    882     }
    883 
    884     p_cb_data->status = status;
    885     SEM_POST(p_cb_data);
    886 
    887     return;
    888 }
    889 
    890 /*******************************************************************************
    891 **
    892 ** Function         phNxpNciHal_fw_dnld_log_read
    893 **
    894 ** Description      Download Log Read
    895 **
    896 ** Returns          NFCSTATUS_SUCCESS if success
    897 **
    898 *******************************************************************************/
    899 static NFCSTATUS phNxpNciHal_fw_dnld_log_read(void* pContext, NFCSTATUS status,
    900         void* pInfo)
    901 {
    902     NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
    903     phNxpNciHal_Sem_t cb_data;
    904     phDnldNfc_Buff_t Data;
    905     UNUSED(pContext);
    906     UNUSED(status);
    907     UNUSED(pInfo);
    908     if((((TRUE == (gphNxpNciHal_fw_IoctlCtx.bSkipSeq)) || (TRUE == (gphNxpNciHal_fw_IoctlCtx.bForceDnld))) &&
    909                 (FALSE == (gphNxpNciHal_fw_IoctlCtx.bPrevSessnOpen))) || (((TRUE == (gphNxpNciHal_fw_IoctlCtx.bPrevSessnOpen))) &&
    910                     (TRUE == (gphNxpNciHal_fw_IoctlCtx.bRetryDnld))))
    911 
    912     {
    913         return NFCSTATUS_SUCCESS;
    914     }
    915 
    916     if (phNxpNciHal_init_cb_data(&cb_data, NULL) != NFCSTATUS_SUCCESS)
    917     {
    918         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_log_read cb_data creation failed");
    919         return NFCSTATUS_FAILED;
    920     }
    921 
    922     (Data.pBuff) = (void *)&(gphNxpNciHal_fw_IoctlCtx.tLogParams);
    923     (Data.wLen) = sizeof(phLibNfc_EELogParams_t);
    924 
    925     wStatus = phDnldNfc_ReadLog(&Data,
    926             (pphDnldNfc_RspCb_t) &phNxpNciHal_fw_dnld_log_read_cb,
    927             (void *) &cb_data);
    928     if (wStatus != NFCSTATUS_PENDING)
    929     {
    930         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_log_read failed");
    931         wStatus = NFCSTATUS_FAILED;
    932         goto clean_and_return;
    933     }
    934 
    935     /* Wait for callback response */
    936     if (SEM_WAIT(cb_data))
    937     {
    938         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_log_read semaphore error");
    939         wStatus = NFCSTATUS_FAILED;
    940         goto clean_and_return;
    941     }
    942 
    943     if (cb_data.status != NFCSTATUS_SUCCESS)
    944     {
    945         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_log_read cb failed");
    946         wStatus = NFCSTATUS_FAILED;
    947         goto clean_and_return;
    948     }
    949 
    950     wStatus = NFCSTATUS_SUCCESS;
    951 
    952 clean_and_return:
    953     phNxpNciHal_cleanup_cb_data(&cb_data);
    954 
    955     return wStatus;
    956 }
    957 
    958 /*******************************************************************************
    959 **
    960 ** Function         phNxpNciHal_fw_dnld_write_cb
    961 **
    962 ** Description      Download Write callback
    963 **
    964 ** Returns          None
    965 **
    966 *******************************************************************************/
    967 static void phNxpNciHal_fw_dnld_write_cb(void* pContext, NFCSTATUS status,
    968         void* pInfo)
    969 {
    970     phNxpNciHal_Sem_t *p_cb_data = (phNxpNciHal_Sem_t*) pContext;
    971     UNUSED(pInfo);
    972     if (NFCSTATUS_SUCCESS == status)
    973     {
    974         NXPLOG_FWDNLD_D("phNxpNciHal_fw_dnld_write_cb - Request Successful");
    975         (gphNxpNciHal_fw_IoctlCtx.bDnldEepromWrite) = FALSE;
    976         if(TRUE == (gphNxpNciHal_fw_IoctlCtx.bDnldInitiated))
    977         {
    978             (gphNxpNciHal_fw_IoctlCtx.tLogParams.wNumDnldSuccess) += 1;
    979 
    980             if((gphNxpNciHal_fw_IoctlCtx.tLogParams.wDnldFailCnt) > 0)
    981             {
    982                 NXPLOG_FWDNLD_D("phNxpNciHal_fw_dnld_write_cb - Resetting DnldFailCnt");
    983                 (gphNxpNciHal_fw_IoctlCtx.tLogParams.wDnldFailCnt) = 0;
    984             }
    985 
    986             if(FALSE == (gphNxpNciHal_fw_IoctlCtx.tLogParams.bConfig))
    987             {
    988                 NXPLOG_FWDNLD_D("phNxpNciHal_fw_dnld_write_cb - Setting bConfig for use by NCI mode");
    989                 (gphNxpNciHal_fw_IoctlCtx.tLogParams.bConfig) = TRUE;
    990             }
    991         }
    992 
    993         /* Reset the previously set DnldAttemptFailed flag */
    994         if(TRUE == (gphNxpNciHal_fw_IoctlCtx.bDnldAttemptFailed))
    995         {
    996             (gphNxpNciHal_fw_IoctlCtx.bDnldAttemptFailed) = FALSE;
    997         }
    998     }
    999     else
   1000     {
   1001         if(TRUE == (gphNxpNciHal_fw_IoctlCtx.bDnldInitiated))
   1002         {
   1003             (gphNxpNciHal_fw_IoctlCtx.tLogParams.wNumDnldFail) += 1;
   1004             (gphNxpNciHal_fw_IoctlCtx.tLogParams.wDnldFailCnt) += 1;
   1005             (gphNxpNciHal_fw_IoctlCtx.tLogParams.bConfig) = FALSE;
   1006         }
   1007         if(NFCSTATUS_WRITE_FAILED == status)
   1008         {
   1009             (gphNxpNciHal_fw_IoctlCtx.bSkipSeq) = TRUE;
   1010             (gphNxpNciHal_fw_IoctlCtx.bDnldRecovery) = TRUE;
   1011         }
   1012         //status = NFCSTATUS_FAILED;
   1013 
   1014         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_write_cb - Request Failed!!");
   1015     }
   1016 
   1017     p_cb_data->status = status;
   1018     SEM_POST(p_cb_data);
   1019 
   1020     return;
   1021 }
   1022 
   1023 /*******************************************************************************
   1024 **
   1025 ** Function         phNxpNciHal_fw_dnld_write
   1026 **
   1027 ** Description      Download Write
   1028 **
   1029 ** Returns          NFCSTATUS_SUCCESS if success
   1030 **
   1031 *******************************************************************************/
   1032 static NFCSTATUS phNxpNciHal_fw_dnld_write(void* pContext, NFCSTATUS status,
   1033         void* pInfo)
   1034 {
   1035     NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
   1036     phNxpNciHal_Sem_t cb_data;
   1037     UNUSED(pContext);
   1038     UNUSED(status);
   1039     UNUSED(pInfo);
   1040     if(TRUE == (gphNxpNciHal_fw_IoctlCtx.bRetryDnld))
   1041     {
   1042         (gphNxpNciHal_fw_IoctlCtx.bRetryDnld) = FALSE;
   1043     }
   1044 
   1045     if((TRUE == (gphNxpNciHal_fw_IoctlCtx.bSkipSeq))
   1046             && (FALSE == (gphNxpNciHal_fw_IoctlCtx.bPrevSessnOpen)))
   1047     {
   1048         return NFCSTATUS_SUCCESS;
   1049     }
   1050 
   1051     if (phNxpNciHal_init_cb_data(&cb_data, NULL) != NFCSTATUS_SUCCESS)
   1052     {
   1053         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_write cb_data creation failed");
   1054         return NFCSTATUS_FAILED;
   1055     }
   1056     if(FALSE == (gphNxpNciHal_fw_IoctlCtx.bForceDnld))
   1057     {
   1058         NXPLOG_FWDNLD_D("phNxpNciHal_fw_dnld_write - Incrementing NumDnldTrig..");
   1059         (gphNxpNciHal_fw_IoctlCtx.bDnldInitiated) = TRUE;
   1060         (gphNxpNciHal_fw_IoctlCtx.bDnldAttempts)++;
   1061         (gphNxpNciHal_fw_IoctlCtx.tLogParams.wNumDnldTrig) += 1;
   1062     }
   1063     wStatus = phDnldNfc_Write(FALSE, NULL,
   1064             (pphDnldNfc_RspCb_t) &phNxpNciHal_fw_dnld_write_cb,
   1065             (void *) &cb_data);
   1066     if(FALSE == (gphNxpNciHal_fw_IoctlCtx.bForceDnld))
   1067     {
   1068         if (wStatus != NFCSTATUS_PENDING)
   1069         {
   1070             NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_write failed");
   1071             wStatus = NFCSTATUS_FAILED;
   1072             (gphNxpNciHal_fw_IoctlCtx.tLogParams.wNumDnldFail) += 1;
   1073             (gphNxpNciHal_fw_IoctlCtx.tLogParams.wDnldFailCnt) += 1;
   1074             (gphNxpNciHal_fw_IoctlCtx.tLogParams.bConfig) = FALSE;
   1075             goto clean_and_return;
   1076         }
   1077     }
   1078     /* Wait for callback response */
   1079     if (SEM_WAIT(cb_data))
   1080     {
   1081         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_write semaphore error");
   1082         wStatus = NFCSTATUS_FAILED;
   1083         goto clean_and_return;
   1084     }
   1085 
   1086     if (cb_data.status != NFCSTATUS_SUCCESS)
   1087     {
   1088         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_write cb failed");
   1089         wStatus = cb_data.status;
   1090         goto clean_and_return;
   1091     }
   1092 
   1093     wStatus = NFCSTATUS_SUCCESS;
   1094 
   1095 clean_and_return:
   1096     phNxpNciHal_cleanup_cb_data(&cb_data);
   1097 
   1098     return wStatus;
   1099 }
   1100 
   1101 /*******************************************************************************
   1102 **
   1103 ** Function         phNxpNciHal_fw_dnld_chk_integrity_cb
   1104 **
   1105 ** Description      Download Check Integrity callback
   1106 **
   1107 ** Returns          None
   1108 **
   1109 *******************************************************************************/
   1110 static void phNxpNciHal_fw_dnld_chk_integrity_cb(void* pContext,
   1111         NFCSTATUS status, void* pInfo)
   1112 {
   1113     phNxpNciHal_Sem_t *p_cb_data = (phNxpNciHal_Sem_t*) pContext;
   1114     NFCSTATUS wStatus = status;
   1115     pphDnldNfc_Buff_t pRespBuff;
   1116     //uint8_t bUserDataCrc[4];
   1117 
   1118     if ((NFCSTATUS_SUCCESS == wStatus) && (NULL != pInfo))
   1119     {
   1120         NXPLOG_FWDNLD_D("phNxpNciHal_fw_dnld_chk_integrity_cb - Request Successful");
   1121         pRespBuff = (pphDnldNfc_Buff_t) pInfo;
   1122 
   1123         if ((31 == (pRespBuff->wLen)) && (NULL != (pRespBuff->pBuff)))
   1124         {
   1125             NXPLOG_FWDNLD_D("phNxpNciHal_fw_dnld_chk_integrity_cb - Valid Resp Buff!!...\n");
   1126             wStatus = phLibNfc_VerifyCrcStatus(pRespBuff->pBuff[0]);
   1127             /*
   1128             memcpy(bUserDataCrc, &(pRespBuff->pBuff[27]),
   1129                     sizeof(bUserDataCrc));*/
   1130         }
   1131         else
   1132         {
   1133             NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_chk_integrity_cb - Resp Buff Invalid...\n");
   1134         }
   1135     }
   1136     else
   1137     {
   1138         wStatus = NFCSTATUS_FAILED;
   1139         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_chk_integrity_cb - Request Failed!!");
   1140     }
   1141 
   1142     p_cb_data->status = wStatus;
   1143 
   1144     SEM_POST(p_cb_data);
   1145 
   1146     return;
   1147 }
   1148 
   1149 /*******************************************************************************
   1150 **
   1151 ** Function         phNxpNciHal_fw_dnld_chk_integrity
   1152 **
   1153 ** Description      Download Check Integrity
   1154 **
   1155 ** Returns          NFCSTATUS_SUCCESS if success
   1156 **
   1157 *******************************************************************************/
   1158 static NFCSTATUS phNxpNciHal_fw_dnld_chk_integrity(void* pContext,
   1159         NFCSTATUS status, void* pInfo)
   1160 {
   1161     NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
   1162     phNxpNciHal_Sem_t cb_data;
   1163     phDnldNfc_Buff_t tDnldBuff;
   1164     static uint8_t bChkIntgRes[31];
   1165     UNUSED(pInfo);
   1166     UNUSED(pContext);
   1167     UNUSED(status);
   1168     if(TRUE == gphNxpNciHal_fw_IoctlCtx.bPrevSessnOpen)
   1169     {
   1170         NXPLOG_FWDNLD_D("Previous Upload session is open..Cannot issue ChkIntegrity Cmd!!");
   1171         return NFCSTATUS_SUCCESS;
   1172     }
   1173 
   1174     if(TRUE == (gphNxpNciHal_fw_IoctlCtx.bSkipSeq))
   1175     {
   1176         return NFCSTATUS_SUCCESS;
   1177     }
   1178     else if(TRUE == gphNxpNciHal_fw_IoctlCtx.bPrevSessnOpen)
   1179     {
   1180         NXPLOG_FWDNLD_E("Previous Upload session is open..Cannot issue ChkIntegrity Cmd!!");
   1181         return NFCSTATUS_SUCCESS;
   1182     }
   1183 
   1184     tDnldBuff.pBuff = bChkIntgRes;
   1185     tDnldBuff.wLen = sizeof(bChkIntgRes);
   1186 
   1187     if (phNxpNciHal_init_cb_data(&cb_data, NULL) != NFCSTATUS_SUCCESS)
   1188     {
   1189         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_chk_integrity cb_data creation failed");
   1190         return NFCSTATUS_FAILED;
   1191     }
   1192 
   1193     wStatus = phDnldNfc_CheckIntegrity((gphNxpNciHal_fw_IoctlCtx.bChipVer),
   1194             &tDnldBuff, &phNxpNciHal_fw_dnld_chk_integrity_cb,
   1195             (void *) &cb_data);
   1196     if (wStatus != NFCSTATUS_PENDING)
   1197     {
   1198         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_chk_integrity failed");
   1199         wStatus = NFCSTATUS_FAILED;
   1200         goto clean_and_return;
   1201     }
   1202 
   1203     /* Wait for callback response */
   1204     if (SEM_WAIT(cb_data))
   1205     {
   1206         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_chk_integrity semaphore error");
   1207         wStatus = NFCSTATUS_FAILED;
   1208         goto clean_and_return;
   1209     }
   1210 
   1211     if (cb_data.status != NFCSTATUS_SUCCESS)
   1212     {
   1213         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_chk_integrity cb failed");
   1214         wStatus = NFCSTATUS_FAILED;
   1215         goto clean_and_return;
   1216     }
   1217 
   1218     wStatus = NFCSTATUS_SUCCESS;
   1219 
   1220 clean_and_return:
   1221     phNxpNciHal_cleanup_cb_data(&cb_data);
   1222 
   1223     return wStatus;
   1224 }
   1225 
   1226 /*******************************************************************************
   1227 **
   1228 ** Function         phNxpNciHal_fw_dnld_recover
   1229 **
   1230 ** Description      Download Recover
   1231 **
   1232 ** Returns          NFCSTATUS_SUCCESS if success
   1233 **
   1234 *******************************************************************************/
   1235 static NFCSTATUS  phNxpNciHal_fw_dnld_recover(void* pContext, NFCSTATUS status,
   1236         void* pInfo)
   1237 {
   1238     NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
   1239     phNxpNciHal_Sem_t cb_data;
   1240 
   1241     UNUSED(pInfo);
   1242     UNUSED(status);
   1243     UNUSED(pContext);
   1244     if(TRUE == (gphNxpNciHal_fw_IoctlCtx.bDnldRecovery))
   1245     {
   1246         if (phNxpNciHal_init_cb_data(&cb_data, NULL) != NFCSTATUS_SUCCESS)
   1247         {
   1248             NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_recover cb_data creation failed");
   1249             return NFCSTATUS_FAILED;
   1250         }
   1251         (gphNxpNciHal_fw_IoctlCtx.bDnldAttempts)++;
   1252 
   1253         /* resetting this flag to avoid cyclic issuance of recovery sequence in case of failure */
   1254         (gphNxpNciHal_fw_IoctlCtx.bDnldRecovery) = FALSE;
   1255 
   1256         wStatus = phDnldNfc_Write(TRUE,NULL,(pphDnldNfc_RspCb_t)&phNxpNciHal_fw_dnld_recover_cb, (void*) &cb_data);
   1257 
   1258         if(NFCSTATUS_PENDING != wStatus)
   1259         {
   1260             (gphNxpNciHal_fw_IoctlCtx.bSkipForce) = FALSE;
   1261             (gphNxpNciHal_fw_IoctlCtx.bRetryDnld) = FALSE;
   1262             goto clean_and_return;
   1263         }
   1264         /* Wait for callback response */
   1265         if (SEM_WAIT(cb_data))
   1266         {
   1267             NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_recover semaphore error");
   1268             wStatus = NFCSTATUS_FAILED;
   1269             goto clean_and_return;
   1270         }
   1271 
   1272         if (cb_data.status != NFCSTATUS_SUCCESS)
   1273         {
   1274             NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_recover cb failed");
   1275             wStatus = NFCSTATUS_FAILED;
   1276             goto clean_and_return;
   1277         }
   1278         wStatus = NFCSTATUS_SUCCESS;
   1279 
   1280 clean_and_return:
   1281         phNxpNciHal_cleanup_cb_data(&cb_data);
   1282     }
   1283 
   1284     return wStatus;
   1285 }
   1286 
   1287 /*******************************************************************************
   1288 **
   1289 ** Function         phNxpNciHal_fw_dnld_recover_cb
   1290 **
   1291 ** Description      Download Recover callback
   1292 **
   1293 ** Returns          None
   1294 **
   1295 *******************************************************************************/
   1296 static void phNxpNciHal_fw_dnld_recover_cb(void* pContext, NFCSTATUS status,
   1297         void* pInfo)
   1298 {
   1299     phNxpNciHal_Sem_t *p_cb_data = (phNxpNciHal_Sem_t*) pContext;
   1300     NFCSTATUS wStatus = status;
   1301     UNUSED(pContext);
   1302     UNUSED(pInfo);
   1303 
   1304     if(NFCSTATUS_SUCCESS == wStatus)
   1305     {
   1306         if(FALSE == (gphNxpNciHal_fw_IoctlCtx.bSkipForce))
   1307         {
   1308             NXPLOG_FWDNLD_D("phNxpNciHal_fw_dnld_recoverCb - Request Successful");
   1309             (gphNxpNciHal_fw_IoctlCtx.bRetryDnld) = TRUE;
   1310         }
   1311         else
   1312         {
   1313             NXPLOG_FWDNLD_D("phNxpNciHal_fw_dnld_recoverCb - Production key update Request Successful");
   1314             (gphNxpNciHal_fw_IoctlCtx.bSendNciCmd) = TRUE;
   1315         }
   1316     }
   1317     else
   1318     {
   1319         wStatus = NFCSTATUS_FAILED;
   1320         NXPLOG_FWDNLD_D("phNxpNciHal_fw_dnld_recoverCb - Request Failed!!");
   1321     }
   1322 
   1323     /* resetting this flag to avoid cyclic issuance of recovery sequence in case of failure */
   1324     (gphNxpNciHal_fw_IoctlCtx.bDnldRecovery) = FALSE;
   1325 
   1326     /* reset previously set SkipForce */
   1327     (gphNxpNciHal_fw_IoctlCtx.bSkipForce) = FALSE;
   1328     p_cb_data->status = wStatus;
   1329 
   1330     SEM_POST(p_cb_data);
   1331 
   1332     return;
   1333 }
   1334 
   1335 /*******************************************************************************
   1336 **
   1337 ** Function         phNxpNciHal_fw_dnld_send_ncicmd_cb
   1338 **
   1339 ** Description      Download Send NCI Command callback
   1340 **
   1341 ** Returns          None
   1342 **
   1343 *******************************************************************************/
   1344 static  void phNxpNciHal_fw_dnld_send_ncicmd_cb(void* pContext, NFCSTATUS status,
   1345         void* pInfo)
   1346 {
   1347     phNxpNciHal_Sem_t *p_cb_data = (phNxpNciHal_Sem_t*) pContext;
   1348     NFCSTATUS wStatus = status;
   1349     pphDnldNfc_Buff_t pRespBuff;
   1350     UNUSED(pContext);
   1351 
   1352     if(NFCSTATUS_SUCCESS == wStatus)
   1353     {
   1354         NXPLOG_FWDNLD_D("phNxpNciHal_fw_dnld_send_ncicmdCb - Request Successful");
   1355         pRespBuff = (pphDnldNfc_Buff_t)pInfo;
   1356 
   1357         if((0 != (pRespBuff->wLen)) && (NULL != (pRespBuff->pBuff)))
   1358         {
   1359             if(0 == (pRespBuff->pBuff[3]))
   1360             {
   1361                 NXPLOG_FWDNLD_D("Successful Response received for Nci Reset Cmd");
   1362             }
   1363             else
   1364             {
   1365                 NXPLOG_FWDNLD_E("Nci Reset Request Failed!!");
   1366             }
   1367         }
   1368         else
   1369         {
   1370             NXPLOG_FWDNLD_E("Invalid Response received for Nci Reset Request!!");
   1371         }
   1372         /* Call Tml Ioctl to enable download mode */
   1373         wStatus = phTmlNfc_IoCtl(phTmlNfc_e_EnableDownloadMode);
   1374 
   1375         if(NFCSTATUS_SUCCESS == wStatus)
   1376         {
   1377             NXPLOG_FWDNLD_D("Switched Successfully to dnld mode..");
   1378             (gphNxpNciHal_fw_IoctlCtx.bRetryDnld) = TRUE;
   1379         }
   1380         else
   1381         {
   1382             NXPLOG_FWDNLD_E("Switching back to dnld mode Failed!!");
   1383             (gphNxpNciHal_fw_IoctlCtx.bRetryDnld) = FALSE;
   1384             wStatus = NFCSTATUS_FAILED;
   1385         }
   1386     }
   1387     else
   1388     {
   1389         NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_send_ncicmdCb - Request Failed!!");
   1390     }
   1391 
   1392     (gphNxpNciHal_fw_IoctlCtx.bSendNciCmd) = FALSE;
   1393     p_cb_data->status = wStatus;
   1394 
   1395     SEM_POST(p_cb_data);
   1396 
   1397     return;
   1398 }
   1399 
   1400 /*******************************************************************************
   1401 **
   1402 ** Function         phNxpNciHal_fw_dnld_send_ncicmd
   1403 **
   1404 ** Description      Download Send NCI Command
   1405 **
   1406 ** Returns          NFCSTATUS_SUCCESS if success
   1407 **
   1408 *******************************************************************************/
   1409 static NFCSTATUS phNxpNciHal_fw_dnld_send_ncicmd(void* pContext, NFCSTATUS status,
   1410         void* pInfo)
   1411 {
   1412     NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
   1413     static uint8_t bNciCmd[4] = {0x20,0x00,0x01,0x00};  /* Nci Reset Cmd with KeepConfig option */
   1414     static uint8_t bNciResp[6];
   1415     phDnldNfc_Buff_t tsData;
   1416     phDnldNfc_Buff_t trData;
   1417     phNxpNciHal_Sem_t cb_data;
   1418 
   1419     UNUSED(pInfo);
   1420     UNUSED(status);
   1421     UNUSED(pContext);
   1422     if(FALSE == (gphNxpNciHal_fw_IoctlCtx.bSendNciCmd))
   1423     {
   1424         return NFCSTATUS_SUCCESS;
   1425     }
   1426     else
   1427     {
   1428         /* Call Tml Ioctl to enable/restore normal mode */
   1429         wStatus = phTmlNfc_IoCtl(phTmlNfc_e_EnableNormalMode);
   1430 
   1431         if(NFCSTATUS_SUCCESS != wStatus)
   1432         {
   1433             NXPLOG_FWDNLD_E("Switching to NormalMode Failed!!");
   1434             (gphNxpNciHal_fw_IoctlCtx.bRetryDnld) = FALSE;
   1435             (gphNxpNciHal_fw_IoctlCtx.bSendNciCmd) = FALSE;
   1436         }
   1437         else
   1438         {
   1439             if (phNxpNciHal_init_cb_data(&cb_data, NULL) != NFCSTATUS_SUCCESS)
   1440             {
   1441                 NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_send_ncicmd cb_data creation failed");
   1442                 return NFCSTATUS_FAILED;
   1443             }
   1444             (tsData.pBuff) = bNciCmd;
   1445             (tsData.wLen) = sizeof(bNciCmd);
   1446             (trData.pBuff) = bNciResp;
   1447             (trData.wLen) = sizeof(bNciResp);
   1448 
   1449             wStatus = phDnldNfc_RawReq(&tsData,&trData,
   1450                     (pphDnldNfc_RspCb_t)&phNxpNciHal_fw_dnld_send_ncicmd_cb, (void*) &cb_data);
   1451             if(NFCSTATUS_PENDING != wStatus)
   1452             {
   1453                 goto clean_and_return;
   1454             }
   1455             /* Wait for callback response */
   1456             if (SEM_WAIT(cb_data))
   1457             {
   1458                 NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_send_ncicmd semaphore error");
   1459                 wStatus = NFCSTATUS_FAILED;
   1460                 goto clean_and_return;
   1461             }
   1462 
   1463             if (cb_data.status != NFCSTATUS_SUCCESS)
   1464             {
   1465                 NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_send_ncicmd cb failed");
   1466                 wStatus = NFCSTATUS_FAILED;
   1467                 goto clean_and_return;
   1468             }
   1469             wStatus = NFCSTATUS_SUCCESS;
   1470 
   1471 clean_and_return:
   1472             phNxpNciHal_cleanup_cb_data(&cb_data);
   1473         }
   1474     }
   1475 
   1476     return wStatus;
   1477 }
   1478 
   1479 /*******************************************************************************
   1480 **
   1481 ** Function         phNxpNciHal_fw_dnld_log_cb
   1482 **
   1483 ** Description      Download Log callback
   1484 **
   1485 ** Returns          None
   1486 **
   1487 *******************************************************************************/
   1488 static void phNxpNciHal_fw_dnld_log_cb(void* pContext, NFCSTATUS status,
   1489         void* pInfo)
   1490 {
   1491     phNxpNciHal_Sem_t *p_cb_data = (phNxpNciHal_Sem_t*) pContext;
   1492     NFCSTATUS wStatus = status;
   1493     UNUSED(pContext);
   1494     UNUSED(pInfo);
   1495 
   1496     if(NFCSTATUS_SUCCESS == wStatus)
   1497     {
   1498         NXPLOG_FWDNLD_D("phLibNfc_DnldLogCb - Request Successful");
   1499         (gphNxpNciHal_fw_IoctlCtx.bDnldInitiated) = FALSE;
   1500     }
   1501     else
   1502     {
   1503         wStatus = NFCSTATUS_FAILED;
   1504         NXPLOG_FWDNLD_E("phLibNfc_DnldLogCb - Request Failed!!");
   1505     }
   1506     p_cb_data->status = wStatus;
   1507 
   1508     SEM_POST(p_cb_data);
   1509     return;
   1510 }
   1511 
   1512 /*******************************************************************************
   1513 **
   1514 ** Function         phNxpNciHal_fw_dnld_log
   1515 **
   1516 ** Description      Download Log
   1517 **
   1518 ** Returns          NFCSTATUS_SUCCESS if success
   1519 **
   1520 *******************************************************************************/
   1521 static NFCSTATUS phNxpNciHal_fw_dnld_log(void* pContext, NFCSTATUS status,
   1522         void* pInfo)
   1523 {
   1524     NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
   1525     phNxpNciHal_Sem_t cb_data;
   1526     phDnldNfc_Buff_t tData;
   1527 
   1528     UNUSED(pInfo);
   1529     UNUSED(status);
   1530     UNUSED(pContext);
   1531     if(((TRUE == (gphNxpNciHal_fw_IoctlCtx.bSkipSeq)) ||
   1532                 (TRUE == (gphNxpNciHal_fw_IoctlCtx.bForceDnld))) &&
   1533             (FALSE == (gphNxpNciHal_fw_IoctlCtx.bDnldInitiated)))
   1534     {
   1535         return NFCSTATUS_SUCCESS;
   1536     }
   1537     else
   1538     {
   1539         if (phNxpNciHal_init_cb_data(&cb_data, NULL) != NFCSTATUS_SUCCESS)
   1540         {
   1541             NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_log cb_data creation failed");
   1542             return NFCSTATUS_FAILED;
   1543         }
   1544         (tData.pBuff) = (void *)&(gphNxpNciHal_fw_IoctlCtx.tLogParams);
   1545         (tData.wLen) = sizeof(gphNxpNciHal_fw_IoctlCtx.tLogParams);
   1546 
   1547         wStatus = phDnldNfc_Log(&tData,(pphDnldNfc_RspCb_t)&phNxpNciHal_fw_dnld_log_cb, (void*) &cb_data);
   1548 
   1549         if (wStatus != NFCSTATUS_PENDING)
   1550         {
   1551             NXPLOG_FWDNLD_E("phDnldNfc_Log failed");
   1552             (gphNxpNciHal_fw_IoctlCtx.bDnldInitiated) = FALSE;
   1553             wStatus = NFCSTATUS_FAILED;
   1554             goto clean_and_return;
   1555         }
   1556         /* Wait for callback response */
   1557         if (SEM_WAIT(cb_data))
   1558         {
   1559             NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_log semaphore error");
   1560             wStatus = NFCSTATUS_FAILED;
   1561             goto clean_and_return;
   1562         }
   1563 
   1564         if (cb_data.status != NFCSTATUS_SUCCESS)
   1565         {
   1566             NXPLOG_FWDNLD_E("phNxpNciHal_fw_dnld_log_cb failed");
   1567             wStatus = NFCSTATUS_FAILED;
   1568             goto clean_and_return;
   1569         }
   1570 
   1571         wStatus = NFCSTATUS_SUCCESS;
   1572 
   1573 clean_and_return:
   1574         phNxpNciHal_cleanup_cb_data(&cb_data);
   1575 
   1576         return wStatus;
   1577     }
   1578 
   1579 }
   1580 
   1581 /*******************************************************************************
   1582 **
   1583 ** Function         phNxpNciHal_fw_seq_handler
   1584 **
   1585 ** Description      Sequence Handler
   1586 **
   1587 ** Returns          NFCSTATUS_SUCCESS if sequence completed uninterrupted
   1588 **
   1589 *******************************************************************************/
   1590 static NFCSTATUS phNxpNciHal_fw_seq_handler(NFCSTATUS (*seq_handler[])(void* pContext, NFCSTATUS status, void* pInfo))
   1591 {
   1592     char *pContext = "FW-Download";
   1593     int16_t seq_counter = 0;
   1594     phDnldNfc_Buff_t pInfo;
   1595     NFCSTATUS status = NFCSTATUS_FAILED;
   1596 
   1597     status = phTmlNfc_ReadAbort();
   1598     if(NFCSTATUS_SUCCESS != status)
   1599     {
   1600       NXPLOG_FWDNLD_E("Tml Read Abort failed!!");
   1601       return status;
   1602     }
   1603 
   1604     while(seq_handler[seq_counter] != NULL )
   1605     {
   1606         status = NFCSTATUS_FAILED;
   1607         status = (seq_handler[seq_counter])(pContext, status, &pInfo );
   1608         if(NFCSTATUS_SUCCESS != status)
   1609         {
   1610             NXPLOG_FWDNLD_E(" phNxpNciHal_fw_seq_handler : FAILED");
   1611             break;
   1612         }
   1613         seq_counter++;
   1614     }
   1615     return status;
   1616 }
   1617 
   1618 /*******************************************************************************
   1619 **
   1620 ** Function         phNxpNciHal_fw_dnld_complete
   1621 **
   1622 ** Description      Download Sequence Complete
   1623 **
   1624 ** Returns          NFCSTATUS_SUCCESS if success
   1625 **
   1626 *******************************************************************************/
   1627 static  NFCSTATUS phNxpNciHal_fw_dnld_complete(void* pContext,NFCSTATUS status,
   1628         void* pInfo)
   1629 {
   1630     NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
   1631     NFCSTATUS fStatus = status;
   1632     UNUSED(pInfo);
   1633     UNUSED(pContext);
   1634 
   1635     if(NFCSTATUS_WRITE_FAILED == status)
   1636     {
   1637         if((gphNxpNciHal_fw_IoctlCtx.bDnldAttempts) < PHLIBNFC_IOCTL_DNLD_MAX_ATTEMPTS)
   1638         {
   1639             (gphNxpNciHal_fw_IoctlCtx.bDnldRecovery) = TRUE;
   1640         }
   1641         else
   1642         {
   1643             NXPLOG_FWDNLD_E("Max Dnld Retry Counts Exceeded!!");
   1644             (gphNxpNciHal_fw_IoctlCtx.bDnldRecovery) = FALSE;
   1645             (gphNxpNciHal_fw_IoctlCtx.bRetryDnld) = FALSE;
   1646         }
   1647     }
   1648     else if(NFCSTATUS_REJECTED == status)
   1649     {
   1650         if((gphNxpNciHal_fw_IoctlCtx.bDnldAttempts) < PHLIBNFC_IOCTL_DNLD_MAX_ATTEMPTS)
   1651         {
   1652             (gphNxpNciHal_fw_IoctlCtx.bDnldRecovery) = TRUE;
   1653 
   1654             /* in case of signature error we need to try recover sequence directly bypassing the force cmd */
   1655             (gphNxpNciHal_fw_IoctlCtx.bSkipForce) = TRUE;
   1656         }
   1657         else
   1658         {
   1659             NXPLOG_FWDNLD_E("Max Dnld Retry Counts Exceeded!!");
   1660             (gphNxpNciHal_fw_IoctlCtx.bDnldRecovery) = FALSE;
   1661             (gphNxpNciHal_fw_IoctlCtx.bRetryDnld) = FALSE;
   1662         }
   1663     }
   1664 
   1665     if(TRUE == (gphNxpNciHal_fw_IoctlCtx.bDnldInitiated))
   1666     {
   1667         (gphNxpNciHal_fw_IoctlCtx.bLastStatus) = status;
   1668         (gphNxpNciHal_fw_IoctlCtx.bDnldAttemptFailed) = TRUE;
   1669 
   1670         NXPLOG_FWDNLD_E("Invoking Pending Download Log Sequence..");
   1671         (gphNxpNciHal_fw_IoctlCtx.bDnldInitiated) = FALSE;
   1672         /* Perform the Logging sequence */
   1673         wStatus = phNxpNciHal_fw_seq_handler(phNxpNciHal_dwnld_log_seqhandler);
   1674         status = phNxpNciHal_fw_dnld_complete(pContext, wStatus, &pInfo);
   1675         if (NFCSTATUS_SUCCESS == status)
   1676         {
   1677             NXPLOG_FWDNLD_D(" phNxpNciHal_fw_dnld_complete : SUCCESS");
   1678         }
   1679         else
   1680         {
   1681             NXPLOG_FWDNLD_E(" phNxpNciHal_fw_dnld_complete : FAILED");
   1682         }
   1683 
   1684     }
   1685     else if(TRUE == (gphNxpNciHal_fw_IoctlCtx.bDnldRecovery))
   1686     {
   1687         NXPLOG_FWDNLD_E("Invoking Download Recovery Sequence..");
   1688 
   1689         if(NFCSTATUS_SUCCESS == wStatus)
   1690         {
   1691             /* Perform the download Recovery sequence */
   1692             wStatus = phNxpNciHal_fw_seq_handler(phNxpNciHal_dwnld_rec_seqhandler);
   1693 
   1694             status = phNxpNciHal_fw_dnld_complete(pContext, wStatus, &pInfo);
   1695             if (NFCSTATUS_SUCCESS == status)
   1696             {
   1697                 NXPLOG_FWDNLD_D(" phNxpNciHal_fw_dnld_complete : SUCCESS");
   1698             }
   1699             else
   1700             {
   1701                 NXPLOG_FWDNLD_E(" phNxpNciHal_fw_dnld_complete : FAILED");
   1702             }
   1703         }
   1704     }
   1705     else if(TRUE == (gphNxpNciHal_fw_IoctlCtx.bRetryDnld))
   1706     {
   1707         (gphNxpNciHal_fw_IoctlCtx.bPrevSessnOpen) = FALSE;
   1708         (gphNxpNciHal_fw_IoctlCtx.bDnldInitiated) = FALSE;
   1709         (gphNxpNciHal_fw_IoctlCtx.bForceDnld) = FALSE;
   1710         (gphNxpNciHal_fw_IoctlCtx.bSkipSeq) = FALSE;
   1711         (gphNxpNciHal_fw_IoctlCtx.bSkipForce) = FALSE;
   1712         (gphNxpNciHal_fw_IoctlCtx.bDnldRecovery) = FALSE;
   1713         (gphNxpNciHal_fw_IoctlCtx.bSendNciCmd) = FALSE;
   1714 
   1715         /* Perform the download sequence ... after successful recover attempt */
   1716         wStatus = phNxpNciHal_fw_seq_handler(phNxpNciHal_dwnld_seqhandler);
   1717 
   1718         status = phNxpNciHal_fw_dnld_complete(pContext, wStatus, &pInfo);
   1719         if (NFCSTATUS_SUCCESS == status)
   1720         {
   1721             NXPLOG_FWDNLD_D(" phNxpNciHal_fw_dnld_complete : SUCCESS");
   1722         }
   1723         else
   1724         {
   1725             NXPLOG_FWDNLD_E(" phNxpNciHal_fw_dnld_complete : FAILED");
   1726         }
   1727     }
   1728     else
   1729     {
   1730         if(FALSE == (gphNxpNciHal_fw_IoctlCtx.bSkipSeq))
   1731         {
   1732             if(NFCSTATUS_SUCCESS == status)
   1733             {
   1734                 if(NFC_FW_DOWNLOAD == gphNxpNciHal_fw_IoctlCtx.IoctlCode)
   1735                 {
   1736                     NXPLOG_FWDNLD_E("Fw Download success.. ");
   1737                 }
   1738                 else if(PHLIBNFC_DNLD_MEM_READ == gphNxpNciHal_fw_IoctlCtx.IoctlCode)
   1739                 {
   1740                     NXPLOG_FWDNLD_E("Read Request success.. ");
   1741                 }
   1742                 else if(PHLIBNFC_DNLD_MEM_WRITE == gphNxpNciHal_fw_IoctlCtx.IoctlCode)
   1743                 {
   1744                     NXPLOG_FWDNLD_E("Write Request success.. ");
   1745                 }
   1746                 else if(PHLIBNFC_DNLD_READ_LOG == gphNxpNciHal_fw_IoctlCtx.IoctlCode)
   1747                 {
   1748                     NXPLOG_FWDNLD_E("ReadLog Request success.. ");
   1749                 }
   1750                 else
   1751                 {
   1752                     NXPLOG_FWDNLD_E("Invalid Request!!");
   1753                 }
   1754             }
   1755             else
   1756             {
   1757                 if(NFC_FW_DOWNLOAD == gphNxpNciHal_fw_IoctlCtx.IoctlCode)
   1758                 {
   1759                     NXPLOG_FWDNLD_E("Fw Download Failed!!");
   1760                 }
   1761                 else if(NFC_MEM_READ == gphNxpNciHal_fw_IoctlCtx.IoctlCode)
   1762                 {
   1763                     NXPLOG_FWDNLD_E("Read Request Failed!!");
   1764                 }
   1765                 else if(NFC_MEM_WRITE == gphNxpNciHal_fw_IoctlCtx.IoctlCode)
   1766                 {
   1767                     NXPLOG_FWDNLD_E("Write Request Failed!!");
   1768                 }
   1769                 else if(PHLIBNFC_DNLD_READ_LOG == gphNxpNciHal_fw_IoctlCtx.IoctlCode)
   1770                 {
   1771                     NXPLOG_FWDNLD_E("ReadLog Request Failed!!");
   1772                 }
   1773                 else
   1774                 {
   1775                     NXPLOG_FWDNLD_E("Invalid Request!!");
   1776                 }
   1777             }
   1778         }
   1779 
   1780         if(FALSE == gphNxpNciHal_fw_IoctlCtx.bSendNciCmd)
   1781         {
   1782             /* Call Tml Ioctl to enable/restore normal mode */
   1783             wStatus = phTmlNfc_IoCtl(phTmlNfc_e_EnableNormalMode);
   1784 
   1785             if(NFCSTATUS_SUCCESS != wStatus)
   1786             {
   1787                 NXPLOG_FWDNLD_E("Switching to NormalMode Failed!!");
   1788             }
   1789             else
   1790             {
   1791                 wStatus = fStatus;
   1792             }
   1793         }
   1794 
   1795         (gphNxpNciHal_fw_IoctlCtx.bPrevSessnOpen) = FALSE;
   1796         (gphNxpNciHal_fw_IoctlCtx.bDnldInitiated) = FALSE;
   1797         (gphNxpNciHal_fw_IoctlCtx.bChipVer) = 0;
   1798         (gphNxpNciHal_fw_IoctlCtx.bSkipSeq) = FALSE;
   1799         (gphNxpNciHal_fw_IoctlCtx.bForceDnld) = FALSE;
   1800         (gphNxpNciHal_fw_IoctlCtx.bDnldRecovery) = FALSE;
   1801         (gphNxpNciHal_fw_IoctlCtx.bRetryDnld) = FALSE;
   1802         (gphNxpNciHal_fw_IoctlCtx.bSkipReset) = FALSE;
   1803         (gphNxpNciHal_fw_IoctlCtx.bSkipForce) = FALSE;
   1804         (gphNxpNciHal_fw_IoctlCtx.bSendNciCmd) = FALSE;
   1805         (gphNxpNciHal_fw_IoctlCtx.bDnldAttempts) = 0;
   1806 
   1807         if(FALSE == gphNxpNciHal_fw_IoctlCtx.bDnldAttemptFailed)
   1808         {
   1809         }
   1810         else
   1811         {
   1812             NXPLOG_FWDNLD_E("Returning Download Failed Status to Caller!!");
   1813 
   1814             (gphNxpNciHal_fw_IoctlCtx.bLastStatus) = NFCSTATUS_SUCCESS;
   1815             (gphNxpNciHal_fw_IoctlCtx.bDnldAttemptFailed) = FALSE;
   1816         }
   1817         phDnldNfc_CloseFwLibHandle();
   1818     }
   1819 
   1820     return wStatus;
   1821 }
   1822 
   1823 /*******************************************************************************
   1824 **
   1825 ** Function         phNxpNciHal_fw_download_seq
   1826 **
   1827 ** Description      Download Sequence
   1828 **
   1829 ** Returns          NFCSTATUS_SUCCESS if success
   1830 **
   1831 *******************************************************************************/
   1832 NFCSTATUS phNxpNciHal_fw_download_seq(uint8_t bClkSrcVal, uint8_t bClkFreqVal)
   1833 {
   1834     NFCSTATUS status = NFCSTATUS_FAILED;
   1835     phDnldNfc_Buff_t pInfo;
   1836     char *pContext = "FW-Download";
   1837 
   1838     /* reset the global flags */
   1839     gphNxpNciHal_fw_IoctlCtx.IoctlCode = NFC_FW_DOWNLOAD;
   1840     (gphNxpNciHal_fw_IoctlCtx.bPrevSessnOpen) = FALSE;
   1841     (gphNxpNciHal_fw_IoctlCtx.bDnldInitiated) = FALSE;
   1842     (gphNxpNciHal_fw_IoctlCtx.bChipVer) = 0;
   1843     (gphNxpNciHal_fw_IoctlCtx.bSkipSeq) = FALSE;
   1844     (gphNxpNciHal_fw_IoctlCtx.bForceDnld) = FALSE;
   1845     (gphNxpNciHal_fw_IoctlCtx.bDnldRecovery) = FALSE;
   1846     (gphNxpNciHal_fw_IoctlCtx.bRetryDnld) = FALSE;
   1847     (gphNxpNciHal_fw_IoctlCtx.bSkipReset) = FALSE;
   1848     (gphNxpNciHal_fw_IoctlCtx.bSkipForce) = FALSE;
   1849     (gphNxpNciHal_fw_IoctlCtx.bSendNciCmd) = FALSE;
   1850     (gphNxpNciHal_fw_IoctlCtx.bDnldAttempts) = 0;
   1851     (gphNxpNciHal_fw_IoctlCtx.bClkSrcVal) = bClkSrcVal;
   1852     (gphNxpNciHal_fw_IoctlCtx.bClkFreqVal) = bClkFreqVal;
   1853     /* Get firmware version */
   1854     if (NFCSTATUS_SUCCESS == phDnldNfc_InitImgInfo())
   1855     {
   1856         NXPLOG_FWDNLD_D("phDnldNfc_InitImgInfo:SUCCESS");
   1857         status = phNxpNciHal_fw_seq_handler(phNxpNciHal_dwnld_seqhandler);
   1858     }
   1859     else
   1860     {
   1861         NXPLOG_FWDNLD_E("phDnldNfc_InitImgInfo: FAILED");
   1862     }
   1863 
   1864     /* Chage to normal mode */
   1865     status = phNxpNciHal_fw_dnld_complete(pContext, status, &pInfo);
   1866     /*if (NFCSTATUS_SUCCESS == status)
   1867     {
   1868         NXPLOG_FWDNLD_D(" phNxpNciHal_fw_dnld_complete : SUCCESS");
   1869     }
   1870     else
   1871     {
   1872         NXPLOG_FWDNLD_E(" phNxpNciHal_fw_dnld_complete : FAILED");
   1873     }*/
   1874 
   1875     return status;
   1876 }
   1877 
   1878 static
   1879 NFCSTATUS
   1880 phLibNfc_VerifyCrcStatus(uint8_t bCrcStatus)
   1881 {
   1882     uint8_t bBitPos = 0;
   1883     uint8_t bShiftVal = 1;
   1884     NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
   1885     while(bBitPos < 7)
   1886     {
   1887         if(!(bCrcStatus & bShiftVal))
   1888         {
   1889             switch(bBitPos)
   1890             {
   1891                 case 0:
   1892                 {
   1893                     NXPLOG_FWDNLD_E("User Data Crc is NOT OK!!");
   1894                     wStatus = NFCSTATUS_FAILED;
   1895                     break;
   1896                 }
   1897                 case 1:
   1898                 {
   1899                     NXPLOG_FWDNLD_E("Trim Data Crc is NOT OK!!");
   1900                     wStatus = NFCSTATUS_FAILED;
   1901                     break;
   1902                 }
   1903                 case 2:
   1904                 {
   1905                     NXPLOG_FWDNLD_E("Protected Data Crc is NOT OK!!");
   1906                     wStatus = NFCSTATUS_FAILED;
   1907                     break;
   1908                 }
   1909                 case 3:
   1910                 {
   1911                     NXPLOG_FWDNLD_E("Patch Code Crc is NOT OK!!");
   1912                     wStatus = NFCSTATUS_FAILED;
   1913                     break;
   1914                 }
   1915                 case 4:
   1916                 {
   1917                     NXPLOG_FWDNLD_E("Function Code Crc is NOT OK!!");
   1918                     wStatus = NFCSTATUS_FAILED;
   1919                     break;
   1920                 }
   1921                 case 5:
   1922                 {
   1923                     NXPLOG_FWDNLD_E("Patch Table Crc is NOT OK!!");
   1924                     wStatus = NFCSTATUS_FAILED;
   1925                     break;
   1926                 }
   1927                 case 6:
   1928                 {
   1929                     NXPLOG_FWDNLD_E("Function Table Crc is NOT OK!!");
   1930                     wStatus = NFCSTATUS_FAILED;
   1931                     break;
   1932                 }
   1933                 default:
   1934                 {
   1935                     break;
   1936                 }
   1937             }
   1938         }
   1939 
   1940         bShiftVal <<= 1;
   1941         ++bBitPos;
   1942     }
   1943 
   1944     return wStatus;
   1945 }
   1946