1 /****************************************************************************** 2 * 3 * Copyright (C) 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 19 /****************************************************************************** 20 * 21 * HAL Adaptation Interface (HAI). This interface regulates the interaction 22 * between standard Android HAL and Broadcom-specific HAL. It adapts 23 * Broadcom-specific features to the Android framework. 24 * 25 ******************************************************************************/ 26 #define LOG_TAG "NfcNciHal" 27 #include "OverrideLog.h" 28 #include "HalAdaptation.h" 29 #include "SyncEvent.h" 30 #include "config.h" 31 #include "nfc_hal_int.h" 32 #include "nfc_hal_post_reset.h" 33 #include <errno.h> 34 #include <pthread.h> 35 #include <cutils/properties.h> 36 #include "buildcfg.h" 37 #include "android_logmsg.h" 38 extern void delete_hal_non_volatile_store (bool forceDelete); 39 extern void verify_hal_non_volatile_store (); 40 extern void resetConfig (); 41 extern "C" 42 { 43 #include "userial.h" 44 } 45 46 extern void configureCrystalFrequency (); 47 48 /////////////////////////////////////// 49 // private declaration, definition 50 51 52 static nfc_stack_callback_t* gAndroidHalCallback = NULL; 53 static nfc_stack_data_callback_t* gAndroidHalDataCallback = NULL; 54 static SyncEvent gOpenCompletedEvent; 55 static SyncEvent gPostInitCompletedEvent; 56 static SyncEvent gCloseCompletedEvent; 57 58 UINT32 ScrProtocolTraceFlag = SCR_PROTO_TRACE_ALL; //0x017F00; 59 60 static void BroadcomHalCallback (UINT8 event, tHAL_NFC_STATUS status); 61 static void BroadcomHalDataCallback (UINT16 data_len, UINT8* p_data); 62 63 static bool isColdBoot = true; 64 65 extern tNFC_HAL_CFG *p_nfc_hal_cfg; 66 extern const UINT8 nfca_version_string []; 67 extern const UINT8 nfa_version_string []; 68 69 tNFC_HAL_DM_PRE_SET_MEM nfc_hal_pre_set_mem_20795a1 [] = 70 { 71 {0x0016403c, 0x00000008}, 72 {0x0016403c, 0x00000000}, 73 {0x0014008c, 0x00000001}, 74 {0, 0} 75 }; 76 77 extern tNFC_HAL_DM_PRE_SET_MEM *p_nfc_hal_dm_pre_set_mem; 78 79 /////////////////////////////////////// 80 81 82 int HaiInitializeLibrary (const bcm2079x_dev_t* device) 83 { 84 ALOGD ("%s: enter", __FUNCTION__); 85 ALOGE ("%s: ver=%s nfa=%s", __FUNCTION__, nfca_version_string, nfa_version_string); 86 int retval = EACCES; 87 unsigned long freq = 0; 88 unsigned long num = 0; 89 char temp[120]; 90 int8_t prop_value; 91 UINT8 logLevel = 0; 92 93 logLevel = InitializeGlobalAppLogLevel (); 94 95 if ( GetNumValue ( NAME_GLOBAL_RESET, &num, sizeof ( num ) ) ) 96 { 97 if (num == 1) 98 { 99 // Send commands to disable boc 100 p_nfc_hal_dm_pre_set_mem = nfc_hal_pre_set_mem_20795a1; 101 } 102 } 103 104 configureCrystalFrequency (); 105 verify_hal_non_volatile_store (); 106 if ( GetNumValue ( NAME_PRESERVE_STORAGE, (char*)&num, sizeof ( num ) ) && 107 (num == 1) ) 108 ALOGD ("%s: preserve HAL NV store", __FUNCTION__); 109 else 110 { 111 delete_hal_non_volatile_store (false); 112 } 113 114 if ( GetNumValue ( NAME_USE_RAW_NCI_TRACE, &num, sizeof ( num ) ) ) 115 { 116 if (num == 1) 117 { 118 // display protocol traces in raw format 119 ProtoDispAdapterUseRawOutput (TRUE); 120 } 121 } 122 123 // Initialize protocol logging level 124 InitializeProtocolLogLevel (); 125 126 tUSERIAL_OPEN_CFG cfg; 127 struct tUART_CONFIG uart; 128 129 if ( GetStrValue ( NAME_UART_PARITY, temp, sizeof ( temp ) ) ) 130 { 131 if ( strcmp ( temp, "even" ) == 0 ) 132 uart.m_iParity = USERIAL_PARITY_EVEN; 133 else if ( strcmp ( temp, "odd" ) == 0 ) 134 uart.m_iParity = USERIAL_PARITY_ODD; 135 else if ( strcmp ( temp, "none" ) == 0 ) 136 uart.m_iParity = USERIAL_PARITY_NONE; 137 } 138 else 139 uart.m_iParity = USERIAL_PARITY_NONE; 140 141 if ( GetStrValue ( NAME_UART_STOPBITS, temp, sizeof ( temp ) ) ) 142 { 143 if ( strcmp ( temp, "1" ) == 0 ) 144 uart.m_iStopbits = USERIAL_STOPBITS_1; 145 else if ( strcmp ( temp, "2" ) == 0 ) 146 uart.m_iStopbits = USERIAL_STOPBITS_2; 147 else if ( strcmp ( temp, "1.5" ) == 0 ) 148 uart.m_iStopbits = USERIAL_STOPBITS_1_5; 149 } 150 else if ( GetNumValue ( NAME_UART_STOPBITS, &num, sizeof ( num ) ) ) 151 { 152 if ( num == 1 ) 153 uart.m_iStopbits = USERIAL_STOPBITS_1; 154 else if ( num == 2 ) 155 uart.m_iStopbits = USERIAL_STOPBITS_2; 156 } 157 else 158 uart.m_iStopbits = USERIAL_STOPBITS_1; 159 160 if ( GetNumValue ( NAME_UART_DATABITS, &num, sizeof ( num ) ) ) 161 { 162 if ( 5 <= num && num <= 8 ) 163 uart.m_iDatabits = ( 1 << ( num + 1 ) ); 164 } 165 else 166 uart.m_iDatabits = USERIAL_DATABITS_8; 167 168 if ( GetNumValue ( NAME_UART_BAUD, &num, sizeof ( num ) ) ) 169 { 170 if ( num == 300 ) uart.m_iBaudrate = USERIAL_BAUD_300; 171 else if ( num == 600 ) uart.m_iBaudrate = USERIAL_BAUD_600; 172 else if ( num == 1200 ) uart.m_iBaudrate = USERIAL_BAUD_1200; 173 else if ( num == 2400 ) uart.m_iBaudrate = USERIAL_BAUD_2400; 174 else if ( num == 9600 ) uart.m_iBaudrate = USERIAL_BAUD_9600; 175 else if ( num == 19200 ) uart.m_iBaudrate = USERIAL_BAUD_19200; 176 else if ( num == 57600 ) uart.m_iBaudrate = USERIAL_BAUD_57600; 177 else if ( num == 115200 ) uart.m_iBaudrate = USERIAL_BAUD_115200; 178 else if ( num == 230400 ) uart.m_iBaudrate = USERIAL_BAUD_230400; 179 else if ( num == 460800 ) uart.m_iBaudrate = USERIAL_BAUD_460800; 180 else if ( num == 921600 ) uart.m_iBaudrate = USERIAL_BAUD_921600; 181 } 182 else if ( GetStrValue ( NAME_UART_BAUD, temp, sizeof ( temp ) ) ) 183 { 184 if ( strcmp ( temp, "auto" ) == 0 ) 185 uart.m_iBaudrate = USERIAL_BAUD_AUTO; 186 } 187 else 188 uart.m_iBaudrate = USERIAL_BAUD_115200; 189 190 memset (&cfg, 0, sizeof(tUSERIAL_OPEN_CFG)); 191 cfg.fmt = uart.m_iDatabits | uart.m_iParity | uart.m_iStopbits; 192 cfg.baud = uart.m_iBaudrate; 193 194 ALOGD ("%s: uart config=0x%04x, %d\n", __func__, cfg.fmt, cfg.baud); 195 USERIAL_Init(&cfg); 196 197 if ( GetNumValue ( NAME_NFCC_ENABLE_TIMEOUT, &num, sizeof ( num ) ) ) 198 { 199 p_nfc_hal_cfg->nfc_hal_nfcc_enable_timeout = num; 200 } 201 202 if ( GetNumValue ( NAME_NFA_MAX_EE_SUPPORTED, &num, sizeof ( num ) ) && num == 0 ) 203 { 204 // Since NFA_MAX_EE_SUPPORTED is explicetly set to 0, no UICC support is needed. 205 p_nfc_hal_cfg->nfc_hal_hci_uicc_support = 0; 206 } 207 208 prop_value = property_get_bool("nfc.bcm2079x.isColdboot", 0); 209 if (prop_value) { 210 isColdBoot = true; 211 property_set("nfc.bcm2079x.isColdboot", "0"); 212 } 213 // Set 'first boot' flag based on static variable that will get set to false 214 // after the stack has first initialized the EE. 215 p_nfc_hal_cfg->nfc_hal_first_boot = isColdBoot ? TRUE : FALSE; 216 217 HAL_NfcInitialize (); 218 HAL_NfcSetTraceLevel (logLevel); // Initialize HAL's logging level 219 220 retval = 0; 221 ALOGD ("%s: exit %d", __FUNCTION__, retval); 222 return retval; 223 } 224 225 226 int HaiTerminateLibrary () 227 { 228 int retval = EACCES; 229 ALOGD ("%s: enter", __FUNCTION__); 230 231 HAL_NfcTerminate (); 232 gAndroidHalCallback = NULL; 233 gAndroidHalDataCallback = NULL; 234 GKI_shutdown (); 235 resetConfig (); 236 retval = 0; 237 ALOGD ("%s: exit %d", __FUNCTION__, retval); 238 return retval; 239 } 240 241 242 int HaiOpen (const bcm2079x_dev_t* device, nfc_stack_callback_t* halCallbackFunc, nfc_stack_data_callback_t* halDataCallbackFunc) 243 { 244 ALOGD ("%s: enter", __FUNCTION__); 245 int retval = EACCES; 246 247 gAndroidHalCallback = halCallbackFunc; 248 gAndroidHalDataCallback = halDataCallbackFunc; 249 250 SyncEventGuard guard (gOpenCompletedEvent); 251 HAL_NfcOpen (BroadcomHalCallback, BroadcomHalDataCallback); 252 gOpenCompletedEvent.wait (); 253 254 retval = 0; 255 ALOGD ("%s: exit %d", __FUNCTION__, retval); 256 return retval; 257 } 258 259 260 void BroadcomHalCallback (UINT8 event, tHAL_NFC_STATUS status) 261 { 262 ALOGD ("%s: enter; event=0x%X", __FUNCTION__, event); 263 switch (event) 264 { 265 case HAL_NFC_OPEN_CPLT_EVT: 266 { 267 ALOGD ("%s: HAL_NFC_OPEN_CPLT_EVT; status=0x%X", __FUNCTION__, status); 268 SyncEventGuard guard (gOpenCompletedEvent); 269 gOpenCompletedEvent.notifyOne (); 270 break; 271 } 272 273 case HAL_NFC_POST_INIT_CPLT_EVT: 274 { 275 ALOGD ("%s: HAL_NFC_POST_INIT_CPLT_EVT", __FUNCTION__); 276 SyncEventGuard guard (gPostInitCompletedEvent); 277 gPostInitCompletedEvent.notifyOne (); 278 break; 279 } 280 281 case HAL_NFC_CLOSE_CPLT_EVT: 282 { 283 ALOGD ("%s: HAL_NFC_CLOSE_CPLT_EVT", __FUNCTION__); 284 SyncEventGuard guard (gCloseCompletedEvent); 285 gCloseCompletedEvent.notifyOne (); 286 break; 287 } 288 289 case HAL_NFC_ERROR_EVT: 290 { 291 ALOGD ("%s: HAL_NFC_ERROR_EVT", __FUNCTION__); 292 { 293 SyncEventGuard guard (gOpenCompletedEvent); 294 gOpenCompletedEvent.notifyOne (); 295 } 296 { 297 SyncEventGuard guard (gPostInitCompletedEvent); 298 gPostInitCompletedEvent.notifyOne (); 299 } 300 { 301 SyncEventGuard guard (gCloseCompletedEvent); 302 gCloseCompletedEvent.notifyOne (); 303 } 304 break; 305 } 306 } 307 gAndroidHalCallback (event, status); 308 ALOGD ("%s: exit; event=0x%X", __FUNCTION__, event); 309 } 310 311 312 void BroadcomHalDataCallback (UINT16 data_len, UINT8* p_data) 313 { 314 ALOGD ("%s: enter; len=%u", __FUNCTION__, data_len); 315 gAndroidHalDataCallback (data_len, p_data); 316 } 317 318 319 int HaiClose (const bcm2079x_dev_t* device) 320 { 321 ALOGD ("%s: enter", __FUNCTION__); 322 int retval = EACCES; 323 324 SyncEventGuard guard (gCloseCompletedEvent); 325 HAL_NfcClose (); 326 gCloseCompletedEvent.wait (); 327 retval = 0; 328 ALOGD ("%s: exit %d", __FUNCTION__, retval); 329 return retval; 330 } 331 332 333 int HaiCoreInitialized (const bcm2079x_dev_t* device, uint8_t* coreInitResponseParams) 334 { 335 ALOGD ("%s: enter", __FUNCTION__); 336 int retval = EACCES; 337 338 SyncEventGuard guard (gPostInitCompletedEvent); 339 HAL_NfcCoreInitialized (coreInitResponseParams); 340 gPostInitCompletedEvent.wait (); 341 retval = 0; 342 ALOGD ("%s: exit %d", __FUNCTION__, retval); 343 return retval; 344 } 345 346 347 int HaiWrite (const bcm2079x_dev_t* dev, uint16_t dataLen, const uint8_t* data) 348 { 349 ALOGD ("%s: enter; len=%u", __FUNCTION__, dataLen); 350 int retval = EACCES; 351 352 HAL_NfcWrite (dataLen, const_cast<UINT8*> (data)); 353 retval = 0; 354 ALOGD ("%s: exit %d", __FUNCTION__, retval); 355 return retval; 356 } 357 358 359 int HaiPreDiscover (const bcm2079x_dev_t* device) 360 { 361 ALOGD ("%s: enter", __FUNCTION__); 362 int retval = EACCES; 363 364 // This function is a clear indication that the stack is initializing 365 // EE. So we can reset the cold-boot flag here. 366 isColdBoot = false; 367 retval = HAL_NfcPreDiscover () ? 1 : 0; 368 ALOGD ("%s: exit %d", __FUNCTION__, retval); 369 return retval; 370 } 371 372 373 int HaiControlGranted (const bcm2079x_dev_t* device) 374 { 375 ALOGD ("%s: enter", __FUNCTION__); 376 int retval = EACCES; 377 378 HAL_NfcControlGranted (); 379 retval = 0; 380 ALOGD ("%s: exit %d", __FUNCTION__, retval); 381 return retval; 382 } 383 384 385 int HaiPowerCycle (const bcm2079x_dev_t* device) 386 { 387 ALOGD ("%s: enter", __FUNCTION__); 388 int retval = EACCES; 389 390 HAL_NfcPowerCycle (); 391 retval = 0; 392 ALOGD ("%s: exit %d", __FUNCTION__, retval); 393 return retval; 394 } 395 396 397 int HaiGetMaxNfcee (const bcm2079x_dev_t* device, uint8_t* maxNfcee) 398 { 399 ALOGD ("%s: enter", __FUNCTION__); 400 int retval = EACCES; 401 402 // This function is a clear indication that the stack is initializing 403 // EE. So we can reset the cold-boot flag here. 404 isColdBoot = false; 405 406 if ( maxNfcee ) 407 { 408 *maxNfcee = HAL_NfcGetMaxNfcee (); 409 ALOGD("%s: max_ee from HAL to use %d", __FUNCTION__, *maxNfcee); 410 retval = 0; 411 } 412 ALOGD ("%s: exit %d", __FUNCTION__, retval); 413 return retval; 414 } 415 416