Home | History | Annotate | Download | only in adaptation
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 1999-2012 Broadcom Corporation
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at:
      8  *
      9  *  http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  ******************************************************************************/
     18 #include "OverrideLog.h"
     19 #include "config.h"
     20 #include "nfc_hal_int.h"
     21 #include "userial.h"
     22 extern "C"
     23 {
     24     #include "nfc_hal_post_reset.h"
     25 }
     26 #include <string>
     27 #include <cutils/properties.h>
     28 #include "spdhelper.h"
     29 #include "StartupConfig.h"
     30 
     31 #define LOG_TAG "NfcNciHal"
     32 
     33 #define FW_PRE_PATCH                        "FW_PRE_PATCH"
     34 #define FW_PATCH                            "FW_PATCH"
     35 #define MAX_RF_DATA_CREDITS                 "MAX_RF_DATA_CREDITS"
     36 
     37 #define MAX_BUFFER      (512)
     38 static char sPrePatchFn[MAX_BUFFER+1];
     39 static char sPatchFn[MAX_BUFFER+1];
     40 static void * sPrmBuf = NULL;
     41 static void * sI2cFixPrmBuf = NULL;
     42 
     43 #define CONFIG_MAX_LEN 256
     44 static UINT8 sConfig [CONFIG_MAX_LEN];
     45 static StartupConfig sStartupConfig;
     46 static StartupConfig sLptdConfig;
     47 static StartupConfig sPreDiscoveryConfig;
     48 static StartupConfig sXtalCustomParam;
     49 extern UINT8 *p_nfc_hal_dm_start_up_cfg; //defined in the HAL
     50 static UINT8 nfa_dm_start_up_vsc_cfg[CONFIG_MAX_LEN];
     51 extern UINT8 *p_nfc_hal_dm_start_up_vsc_cfg; //defined in the HAL
     52 extern UINT8 *p_nfc_hal_dm_lptd_cfg; //defined in the HAL
     53 static UINT8 sDontSendLptd[] = { 0 };
     54 extern UINT8 *p_nfc_hal_pre_discover_cfg; //defined in the HAL
     55 extern UINT8 *p_nfc_hal_dm_xtal_params_cfg; //defined in HAL
     56 
     57 extern tSNOOZE_MODE_CONFIG gSnoozeModeCfg;
     58 extern tNFC_HAL_CFG *p_nfc_hal_cfg;
     59 static void mayDisableSecureElement (StartupConfig& config);
     60 
     61 /* Default patchfile (in NCD format) */
     62 #ifndef NFA_APP_DEFAULT_PATCHFILE_NAME
     63 #define NFA_APP_DEFAULT_PATCHFILE_NAME      "\0"
     64 #endif
     65 
     66 /* Default patchfile (in NCD format) */
     67 #ifndef NFA_APP_DEFAULT_I2C_PATCHFILE_NAME
     68 #define NFA_APP_DEFAULT_I2C_PATCHFILE_NAME  "\0"
     69 #endif
     70 
     71 tNFC_POST_RESET_CB nfc_post_reset_cb =
     72 {
     73     /* Default Patch & Pre-Patch */
     74     NFA_APP_DEFAULT_PATCHFILE_NAME,
     75     NULL,
     76     NFA_APP_DEFAULT_I2C_PATCHFILE_NAME,
     77     NULL,
     78 
     79     /* Default UART baud rate */
     80     NFC_HAL_DEFAULT_BAUD,
     81 
     82     /* Default tNFC_HAL_DEV_INIT_CFG (flags, num_xtal_cfg, {brcm_hw_id, xtal-freq, xtal-index} ) */
     83     {
     84         2, /* number of valid entries */
     85         {
     86             {0x43341000, 37400, NFC_HAL_XTAL_INDEX_37400},      // All revisions of 43341 use 37,400
     87             {0x20795000, 26000, NFC_HAL_XTAL_INDEX_26000},
     88             {0, 0, 0},
     89             {0, 0, 0},
     90             {0, 0, 0},
     91         }
     92     },
     93 
     94     /* Default low power mode settings */
     95     NFC_HAL_LP_SNOOZE_MODE_NONE,    /* Snooze Mode          */
     96     NFC_HAL_LP_IDLE_THRESHOLD_HOST, /* Idle Threshold Host  */
     97     NFC_HAL_LP_IDLE_THRESHOLD_HC,   /* Idle Threshold HC    */
     98     NFC_HAL_LP_ACTIVE_LOW,          /* NFC_WAKE Active Mode */
     99     NFC_HAL_LP_ACTIVE_HIGH,         /* DH_WAKE Active Mode  */
    100 
    101     NFA_APP_MAX_NUM_REINIT,         /* max retry to get NVM type */
    102     0,                              /* current retry count */
    103     TRUE,                           /* debug mode for downloading patchram */
    104     FALSE                           /* skip downloading patchram after reinit because of patch download failure */
    105 };
    106 
    107 
    108 /*******************************************************************************
    109 **
    110 ** Function         getFileLength
    111 **
    112 ** Description      return the size of a file
    113 **
    114 ** Returns          file size in number of bytes
    115 **
    116 *******************************************************************************/
    117 static long getFileLength(FILE* fp)
    118 {
    119     long sz;
    120     fseek(fp, 0L, SEEK_END);
    121     sz = ftell(fp);
    122     fseek(fp, 0L, SEEK_SET);
    123 
    124     return (sz > 0) ? sz : 0;
    125 }
    126 
    127 /*******************************************************************************
    128 **
    129 ** Function         isFileExist
    130 **
    131 ** Description      Check if file name exists (android does not support fexists)
    132 **
    133 ** Returns          TRUE if file exists
    134 **
    135 *******************************************************************************/
    136 static BOOLEAN isFileExist(const char *pFilename)
    137 {
    138     FILE *pf;
    139 
    140     if ((pf = fopen(pFilename, "r")) != NULL)
    141     {
    142         fclose(pf);
    143         return TRUE;
    144     }
    145     return FALSE;
    146 }
    147 
    148 /*******************************************************************************
    149 **
    150 ** Function         findPatchramFile
    151 **
    152 ** Description      Find the patchram file name specified in the .conf
    153 **
    154 ** Returns          pointer to the file name
    155 **
    156 *******************************************************************************/
    157 static const char* findPatchramFile(const char * pConfigName, char * pBuffer, int bufferLen)
    158 {
    159     ALOGD("%s: config=%s", __FUNCTION__, pConfigName);
    160 
    161     if (pConfigName == NULL)
    162     {
    163         ALOGD("%s No patchfile defined\n", __FUNCTION__);
    164         return NULL;
    165     }
    166 
    167     if (GetStrValue(pConfigName, &pBuffer[0], bufferLen))
    168     {
    169         ALOGD("%s found patchfile %s\n", __FUNCTION__, pBuffer);
    170         return (pBuffer[0] == '\0') ? NULL : pBuffer;
    171     }
    172 
    173     ALOGD("%s Cannot find patchfile '%s'\n", __FUNCTION__, pConfigName);
    174     return NULL;
    175 }
    176 
    177 /*******************************************************************************
    178 **
    179 ** Function:    continueAfterSetSnoozeMode
    180 **
    181 ** Description: Called after Snooze Mode is enabled.
    182 **
    183 ** Returns:     none
    184 **
    185 *******************************************************************************/
    186 static void continueAfterSetSnoozeMode(tHAL_NFC_STATUS status)
    187 {
    188     ALOGD("%s: status=%u", __FUNCTION__, status);
    189     //let stack download firmware during next initialization
    190     nfc_post_reset_cb.spd_skip_on_power_cycle = FALSE;
    191     if (status == NCI_STATUS_OK)
    192         HAL_NfcPreInitDone (HAL_NFC_STATUS_OK);
    193     else
    194         HAL_NfcPreInitDone (HAL_NFC_STATUS_FAILED);
    195 }
    196 
    197 /*******************************************************************************
    198 **
    199 ** Function:    postDownloadPatchram
    200 **
    201 ** Description: Called after patch download
    202 **
    203 ** Returns:     none
    204 **
    205 *******************************************************************************/
    206 static void postDownloadPatchram(tHAL_NFC_STATUS status)
    207 {
    208     ALOGD("%s: status=%i", __FUNCTION__, status);
    209     GetStrValue (NAME_SNOOZE_MODE_CFG, (char*)&gSnoozeModeCfg, sizeof(gSnoozeModeCfg));
    210     if (status != HAL_NFC_STATUS_OK)
    211     {
    212         ALOGE("%s: Patch download failed", __FUNCTION__);
    213         if (status == HAL_NFC_STATUS_REFUSED)
    214         {
    215             SpdHelper::setPatchAsBad();
    216         }
    217         else
    218             SpdHelper::incErrorCount();
    219 
    220         /* If in SPD Debug mode, fail immediately and obviously */
    221         if (SpdHelper::isSpdDebug())
    222             HAL_NfcPreInitDone (HAL_NFC_STATUS_FAILED);
    223         else
    224         {
    225             /* otherwise, power cycle the chip and let the stack startup normally */
    226             ALOGD("%s: re-init; don't download firmware", __FUNCTION__);
    227             //stop stack from downloading firmware during next initialization
    228             nfc_post_reset_cb.spd_skip_on_power_cycle = TRUE;
    229             USERIAL_PowerupDevice(0);
    230             HAL_NfcReInit ();
    231         }
    232     }
    233     /* Set snooze mode here */
    234     else if (gSnoozeModeCfg.snooze_mode != NFC_HAL_LP_SNOOZE_MODE_NONE)
    235     {
    236         status = HAL_NfcSetSnoozeMode(gSnoozeModeCfg.snooze_mode,
    237                                        gSnoozeModeCfg.idle_threshold_dh,
    238                                        gSnoozeModeCfg.idle_threshold_nfcc,
    239                                        gSnoozeModeCfg.nfc_wake_active_mode,
    240                                        gSnoozeModeCfg.dh_wake_active_mode,
    241                                        continueAfterSetSnoozeMode);
    242         if (status != NCI_STATUS_OK)
    243         {
    244             ALOGE("%s: Setting snooze mode failed, status=%i", __FUNCTION__, status);
    245             HAL_NfcPreInitDone(HAL_NFC_STATUS_FAILED);
    246         }
    247     }
    248     else
    249     {
    250         ALOGD("%s: Not using Snooze Mode", __FUNCTION__);
    251         HAL_NfcPreInitDone(HAL_NFC_STATUS_OK);
    252     }
    253 }
    254 
    255 
    256 /*******************************************************************************
    257 **
    258 ** Function:    prmCallback
    259 **
    260 ** Description: Patchram callback (for static patchram mode)
    261 **
    262 ** Returns:     none
    263 **
    264 *******************************************************************************/
    265 void prmCallback(UINT8 event)
    266 {
    267     ALOGD("%s: event=0x%x", __FUNCTION__, event);
    268     switch (event)
    269     {
    270     case NFC_HAL_PRM_CONTINUE_EVT:
    271         /* This event does not occur if static patchram buf is used */
    272         break;
    273 
    274     case NFC_HAL_PRM_COMPLETE_EVT:
    275         postDownloadPatchram(HAL_NFC_STATUS_OK);
    276         break;
    277 
    278     case NFC_HAL_PRM_ABORT_EVT:
    279         postDownloadPatchram(HAL_NFC_STATUS_FAILED);
    280         break;
    281 
    282     case NFC_HAL_PRM_ABORT_INVALID_PATCH_EVT:
    283         ALOGD("%s: invalid patch...skipping patch download", __FUNCTION__);
    284         postDownloadPatchram(HAL_NFC_STATUS_REFUSED);
    285         break;
    286 
    287     case NFC_HAL_PRM_ABORT_BAD_SIGNATURE_EVT:
    288         ALOGD("%s: patch authentication failed", __FUNCTION__);
    289         postDownloadPatchram(HAL_NFC_STATUS_REFUSED);
    290         break;
    291 
    292     case NFC_HAL_PRM_ABORT_NO_NVM_EVT:
    293         ALOGD("%s: No NVM detected", __FUNCTION__);
    294         HAL_NfcPreInitDone(HAL_NFC_STATUS_FAILED);
    295         break;
    296 
    297     default:
    298         ALOGD("%s: not handled event=0x%x", __FUNCTION__, event);
    299         break;
    300     }
    301 }
    302 
    303 
    304 /*******************************************************************************
    305 **
    306 ** Function         getNfaValues
    307 **
    308 ** Description      Get configuration values needed by NFA layer
    309 **
    310 ** Returns:         None
    311 **
    312 *******************************************************************************/
    313 static void getNfaValues (UINT32 chipid)
    314 {
    315     unsigned long num = 0;
    316     int actualLen = 0;
    317 
    318     sStartupConfig.initialize ();
    319     sLptdConfig.initialize ();
    320     sPreDiscoveryConfig.initialize();
    321 
    322     actualLen = GetStrValue (NAME_NFA_DM_START_UP_CFG, (char*)sConfig, sizeof(sConfig));
    323     if (actualLen)
    324         sStartupConfig.append (sConfig, actualLen);
    325 
    326     // Set antenna tuning configuration if configured.
    327     actualLen = GetStrValue(NAME_PREINIT_DSP_CFG, (char*)sConfig, sizeof(sConfig));
    328     if (actualLen)
    329         sStartupConfig.append (sConfig, actualLen);
    330 
    331     if ( GetStrValue ( NAME_NFA_DM_START_UP_VSC_CFG, (char*)nfa_dm_start_up_vsc_cfg, sizeof (nfa_dm_start_up_vsc_cfg) ) )
    332     {
    333         p_nfc_hal_dm_start_up_vsc_cfg = &nfa_dm_start_up_vsc_cfg[0];
    334         ALOGD ( "START_UP_VSC_CFG[0] = %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
    335                                                                             nfa_dm_start_up_vsc_cfg[0],
    336                                                                             nfa_dm_start_up_vsc_cfg[1],
    337                                                                             nfa_dm_start_up_vsc_cfg[2],
    338                                                                             nfa_dm_start_up_vsc_cfg[3],
    339                                                                             nfa_dm_start_up_vsc_cfg[4],
    340                                                                             nfa_dm_start_up_vsc_cfg[5],
    341                                                                             nfa_dm_start_up_vsc_cfg[6],
    342                                                                             nfa_dm_start_up_vsc_cfg[7] );
    343     }
    344 
    345     actualLen = GetStrValue(NAME_LPTD_CFG, (char*)sConfig, sizeof(sConfig));
    346     if (actualLen)
    347     {
    348         sLptdConfig.append (sConfig, actualLen);
    349         p_nfc_hal_dm_lptd_cfg = const_cast<UINT8*> (sLptdConfig.getInternalBuffer ());
    350     }
    351     else
    352     {
    353         // Default to not sending any LPTD setting.
    354         p_nfc_hal_dm_lptd_cfg = sDontSendLptd;
    355     }
    356 
    357     mayDisableSecureElement (sStartupConfig);
    358     p_nfc_hal_dm_start_up_cfg = const_cast<UINT8*> (sStartupConfig.getInternalBuffer ());
    359 
    360     actualLen = GetStrValue(NAME_NFA_DM_PRE_DISCOVERY_CFG, (char*)sConfig, sizeof(sConfig));
    361     if (actualLen)
    362     {
    363         sPreDiscoveryConfig.append (sConfig, actualLen);
    364         mayDisableSecureElement (sPreDiscoveryConfig);
    365         p_nfc_hal_pre_discover_cfg = const_cast<UINT8*> (sPreDiscoveryConfig.getInternalBuffer ());
    366     }
    367 
    368     //configure how many secure elements are available for each type of chip
    369     if (p_nfc_hal_cfg->nfc_hal_hci_uicc_support > 0)
    370     {
    371         if ((chipid & BRCM_NFC_GEN_MASK) == BRCM_NFC_20791_GEN)
    372         {
    373             nfc_hal_cb.max_ee = BRCM_NFC_20791_GEN_MAX_EE;
    374             p_nfc_hal_cfg->nfc_hal_hci_uicc_support = HAL_NFC_HCI_UICC0_HOST | HAL_NFC_HCI_UICC1_HOST;
    375         }
    376         else if ((chipid & BRCM_NFC_GEN_MASK) == BRCM_NFC_43341_GEN)
    377         {
    378             nfc_hal_cb.max_ee = BRCM_NFC_43341_GEN_MAX_EE;
    379             p_nfc_hal_cfg->nfc_hal_hci_uicc_support = HAL_NFC_HCI_UICC0_HOST | HAL_NFC_HCI_UICC1_HOST;
    380         }
    381         else if ((chipid & BRCM_NFC_GEN_MASK) == BRCM_NFC_20795_GEN)
    382         {
    383             nfc_hal_cb.max_ee = BRCM_NFC_20795_GEN_MAX_EE;
    384             p_nfc_hal_cfg->nfc_hal_hci_uicc_support = HAL_NFC_HCI_UICC0_HOST | HAL_NFC_HCI_UICC1_HOST | HAL_NFC_HCI_UICC2_HOST;
    385         }
    386 
    387         //let .conf variable determine how many EE's to discover
    388         if (GetNumValue(NAME_NFA_MAX_EE_SUPPORTED, &num, sizeof(num)))
    389             nfc_hal_cb.max_ee = num;
    390     }
    391 }
    392 
    393 /*******************************************************************************
    394 **
    395 ** Function         StartPatchDownload
    396 **
    397 ** Description      Reads configuration settings, and begins the download
    398 **                  process if patch files are configured.
    399 **
    400 ** Returns:         None
    401 **
    402 *******************************************************************************/
    403 static void StartPatchDownload(UINT32 chipid)
    404 {
    405     ALOGD ("%s: chipid=%lx",__FUNCTION__, chipid);
    406 
    407     char chipID[30];
    408     sprintf(chipID, "%lx", chipid);
    409     ALOGD ("%s: chidId=%s", __FUNCTION__, chipID);
    410 
    411     readOptionalConfig(chipID);     // Read optional chip specific settings
    412     readOptionalConfig("fime");     // Read optional FIME specific settings
    413     getNfaValues(chipid);                 // Get NFA configuration values into variables
    414 
    415     findPatchramFile(FW_PATCH, sPatchFn, sizeof(sPatchFn));
    416     findPatchramFile(FW_PRE_PATCH, sPrePatchFn, sizeof(sPatchFn));
    417 
    418     {
    419         FILE *fd;
    420         /* If an I2C fix patch file was specified, then tell the stack about it */
    421         if (sPrePatchFn[0] != '\0')
    422         {
    423             if ((fd = fopen(sPrePatchFn, "rb")) != NULL)
    424             {
    425                 UINT32 lenPrmBuffer = getFileLength(fd);
    426 
    427                 if ((sI2cFixPrmBuf = malloc(lenPrmBuffer)) != NULL)
    428                 {
    429                     size_t actualLen = fread(sI2cFixPrmBuf, 1, lenPrmBuffer, fd);
    430                     if (actualLen == lenPrmBuffer)
    431                     {
    432                         ALOGD("%s Setting I2C fix to %s (size: %lu)", __FUNCTION__, sPrePatchFn, lenPrmBuffer);
    433                         HAL_NfcPrmSetI2cPatch((UINT8*)sI2cFixPrmBuf, (UINT16)lenPrmBuffer, 0);
    434                     }
    435                     else
    436                         ALOGE("%s fail reading i2c fix; actual len=%u; expected len=%lu", __FUNCTION__, actualLen, lenPrmBuffer);
    437                 }
    438                 else
    439                 {
    440                     ALOGE("%s Unable to get buffer to i2c fix (%lu bytes)", __FUNCTION__, lenPrmBuffer);
    441                 }
    442 
    443                 fclose(fd);
    444             }
    445             else
    446             {
    447                 ALOGE("%s Unable to open i2c fix patchfile %s", __FUNCTION__, sPrePatchFn);
    448             }
    449         }
    450     }
    451 
    452     {
    453         FILE *fd;
    454 
    455         /* If a patch file was specified, then download it now */
    456         if (sPatchFn[0] != '\0')
    457         {
    458             UINT32 bDownloadStarted = false;
    459 
    460             /* open patchfile, read it into a buffer */
    461             if ((fd = fopen(sPatchFn, "rb")) != NULL)
    462             {
    463                 UINT32 lenPrmBuffer = getFileLength(fd);
    464                 ALOGD("%s Downloading patchfile %s (size: %lu) format=%u", __FUNCTION__, sPatchFn, lenPrmBuffer, NFC_HAL_PRM_FORMAT_NCD);
    465                 if ((sPrmBuf = malloc(lenPrmBuffer)) != NULL)
    466                 {
    467                     size_t actualLen = fread(sPrmBuf, 1, lenPrmBuffer, fd);
    468                     if (actualLen == lenPrmBuffer)
    469                     {
    470                         if (!SpdHelper::isPatchBad((UINT8*)sPrmBuf, lenPrmBuffer))
    471                         {
    472                             /* Download patch using static memeory mode */
    473                             HAL_NfcPrmDownloadStart(NFC_HAL_PRM_FORMAT_NCD, 0, (UINT8*)sPrmBuf, lenPrmBuffer, 0, prmCallback);
    474                             bDownloadStarted = true;
    475                         }
    476                     }
    477                     else
    478                         ALOGE("%s fail reading patchram", __FUNCTION__);
    479                 }
    480                 else
    481                     ALOGE("%s Unable to buffer to hold patchram (%lu bytes)", __FUNCTION__, lenPrmBuffer);
    482 
    483                 fclose(fd);
    484             }
    485             else
    486                 ALOGE("%s Unable to open patchfile %s", __FUNCTION__, sPatchFn);
    487 
    488             /* If the download never got started */
    489             if (!bDownloadStarted)
    490             {
    491                 /* If debug mode, fail in an obvious way, otherwise try to start stack */
    492                 postDownloadPatchram(SpdHelper::isSpdDebug() ? HAL_NFC_STATUS_FAILED :
    493                         HAL_NFC_STATUS_OK);
    494             }
    495         }
    496         else
    497         {
    498             ALOGE("%s: No patchfile specified or disabled. Proceeding to post-download procedure...", __FUNCTION__);
    499             postDownloadPatchram(HAL_NFC_STATUS_OK);
    500         }
    501     }
    502 
    503     ALOGD ("%s: exit", __FUNCTION__);
    504 }
    505 
    506 /*******************************************************************************
    507 **
    508 ** Function:    nfc_hal_post_reset_init
    509 **
    510 ** Description: Called by the NFC HAL after controller has been reset.
    511 **              Begin to download firmware patch files.
    512 **
    513 ** Returns:     none
    514 **
    515 *******************************************************************************/
    516 void nfc_hal_post_reset_init (UINT32 brcm_hw_id, UINT8 nvm_type)
    517 {
    518     ALOGD("%s: brcm_hw_id=0x%lx, nvm_type=%d", __FUNCTION__, brcm_hw_id, nvm_type);
    519     tHAL_NFC_STATUS stat = HAL_NFC_STATUS_FAILED;
    520     UINT8 max_credits = 1, allow_no_nvm=0;
    521 
    522     p_nfc_hal_cfg->nfc_hal_prm_nvm_required = TRUE; //don't download firmware if controller cannot detect EERPOM
    523 
    524     if (nvm_type == NCI_SPD_NVM_TYPE_NONE)
    525     {
    526         GetNumValue(NAME_ALLOW_NO_NVM, &allow_no_nvm, sizeof(allow_no_nvm));
    527         if (allow_no_nvm == 0)
    528         {
    529         ALOGD("%s: No NVM detected, FAIL the init stage to force a retry", __FUNCTION__);
    530         USERIAL_PowerupDevice (0);
    531         stat = HAL_NfcReInit ();
    532             return;
    533         }
    534 
    535         p_nfc_hal_cfg->nfc_hal_prm_nvm_required = FALSE; //allow download firmware if controller cannot detect EERPOM
    536     }
    537 
    538         /* Start downloading the patch files */
    539         StartPatchDownload(brcm_hw_id);
    540 
    541         if (GetNumValue(MAX_RF_DATA_CREDITS, &max_credits, sizeof(max_credits)) && (max_credits > 0))
    542         {
    543             ALOGD("%s : max_credits=%d", __FUNCTION__, max_credits);
    544             HAL_NfcSetMaxRfDataCredits(max_credits);
    545         }
    546     }
    547 
    548 
    549 /*******************************************************************************
    550 **
    551 ** Function:        mayDisableSecureElement
    552 **
    553 ** Description:     Optionally adjust a TLV to disable secure element.  This feature
    554 **                  is enabled by setting the system property
    555 **                  nfc.disable_secure_element to a bit mask represented by a hex
    556 **                  octet: C0 = do not detect any secure element.
    557 **                         40 = do not detect secure element in slot 0.
    558 **                         80 = do not detect secure element in slot 1.
    559 **
    560 **                  config: a sequence of TLV's.
    561 **
    562 *******************************************************************************/
    563 void mayDisableSecureElement (StartupConfig& config)
    564 {
    565     unsigned int bitmask = 0;
    566     char valueStr [PROPERTY_VALUE_MAX] = {0};
    567     int len = property_get ("nfc.disable_secure_element", valueStr, "");
    568     if (len > 0)
    569     {
    570         sscanf (valueStr, "%x", &bitmask); //read system property as a hex octet
    571         ALOGD ("%s: disable 0x%02X", __FUNCTION__, (UINT8) bitmask);
    572         config.disableSecureElement ((UINT8) (bitmask & 0xC0));
    573     }
    574 }
    575 
    576 
    577 /*******************************************************************************
    578 **
    579 ** Function:    configureCrystalFrequency
    580 **
    581 ** Description: Configure controller's crystal frequency by reading values from
    582 **              .conf file.  If .conf file does not define any value, then use
    583 **              default values defined in struct nfc_post_reset_cb.
    584 **
    585 ** Returns:     none
    586 **
    587 *******************************************************************************/
    588 void configureCrystalFrequency ()
    589 {
    590     unsigned long num = 0;
    591     UINT32 hwId = 0;
    592     UINT16 xtalFreq = 0;
    593     UINT8 xtalIndex = 0;
    594     int actualLen = 0;
    595 
    596     if (GetNumValue (NAME_XTAL_HARDWARE_ID, &num, sizeof(num)))
    597         hwId = num;
    598 
    599     if (GetNumValue (NAME_XTAL_FREQUENCY, &num, sizeof(num)))
    600         xtalFreq = (UINT16) num;
    601 
    602     if (GetNumValue (NAME_XTAL_FREQ_INDEX, &num, sizeof(num)))
    603         xtalIndex = (UINT8) num;
    604 
    605     actualLen = GetStrValue (NAME_XTAL_PARAMS_CFG, (char*)sConfig, sizeof(sConfig));
    606     if (actualLen && (xtalIndex == NFC_HAL_XTAL_INDEX_SPECIAL)) //whether to use custom crystal frequency
    607     {
    608         sXtalCustomParam.append (sConfig, actualLen);
    609         p_nfc_hal_dm_xtal_params_cfg = const_cast<UINT8*> (sXtalCustomParam.getInternalBuffer ());
    610     }
    611 
    612     if ((hwId == 0) && (xtalFreq == 0) && (xtalIndex == 0))
    613         return;
    614 
    615     ALOGD ("%s: hwId=0x%lX; freq=%u; index=%u", __FUNCTION__, hwId, xtalFreq, xtalIndex);
    616     nfc_post_reset_cb.dev_init_config.xtal_cfg[0].brcm_hw_id = (hwId & BRCM_NFC_GEN_MASK);
    617     nfc_post_reset_cb.dev_init_config.xtal_cfg[0].xtal_freq  = xtalFreq;
    618     nfc_post_reset_cb.dev_init_config.xtal_cfg[0].xtal_index = xtalIndex;
    619     nfc_post_reset_cb.dev_init_config.num_xtal_cfg = 1;
    620 }
    621