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 "buildcfg.h" 36 extern void delete_hal_non_volatile_store (bool forceDelete); 37 extern void verify_hal_non_volatile_store (); 38 extern "C" 39 { 40 #include "userial.h" 41 } 42 43 44 /////////////////////////////////////// 45 // private declaration, definition 46 47 48 static nfc_stack_callback_t* gAndroidHalCallback = NULL; 49 static nfc_stack_data_callback_t* gAndroidHalDataCallback = NULL; 50 static SyncEvent gOpenCompletedEvent; 51 static SyncEvent gPostInitCompletedEvent; 52 static SyncEvent gCloseCompletedEvent; 53 54 UINT32 ScrProtocolTraceFlag = SCR_PROTO_TRACE_ALL; //0x017F00; 55 56 static void BroadcomHalCallback (UINT8 event, tHAL_NFC_STATUS status); 57 static void BroadcomHalDataCallback (UINT16 data_len, UINT8* p_data); 58 59 extern tNFC_HAL_CFG *p_nfc_hal_cfg; 60 extern const UINT8 nfca_version_string []; 61 extern const UINT8 nfa_version_string []; 62 63 /////////////////////////////////////// 64 65 66 int HaiInitializeLibrary (const bcm2079x_dev_t* device) 67 { 68 ALOGD ("%s: enter", __FUNCTION__); 69 ALOGE ("%s: ver=%s nfa=%s", __FUNCTION__, nfca_version_string, nfa_version_string); 70 int retval = EACCES; 71 unsigned long freq = 0; 72 unsigned long num = 0; 73 char temp[120]; 74 UINT8 logLevel = 0; 75 76 logLevel = InitializeGlobalAppLogLevel (); 77 78 verify_hal_non_volatile_store (); 79 if ( GetNumValue ( NAME_PRESERVE_STORAGE, (char*)&num, sizeof ( num ) ) && 80 (num == 1) ) 81 ALOGD ("%s: preserve HAL NV store", __FUNCTION__); 82 else 83 { 84 delete_hal_non_volatile_store (false); 85 } 86 87 // Initialize protocol logging level 88 if ( GetNumValue ( NAME_PROTOCOL_TRACE_LEVEL, &num, sizeof ( num ) ) ) 89 ScrProtocolTraceFlag = num; 90 91 tUSERIAL_OPEN_CFG cfg; 92 struct tUART_CONFIG uart; 93 94 if ( GetStrValue ( NAME_UART_PARITY, temp, sizeof ( temp ) ) ) 95 { 96 if ( strcmp ( temp, "even" ) == 0 ) 97 uart.m_iParity = USERIAL_PARITY_EVEN; 98 else if ( strcmp ( temp, "odd" ) == 0 ) 99 uart.m_iParity = USERIAL_PARITY_ODD; 100 else if ( strcmp ( temp, "none" ) == 0 ) 101 uart.m_iParity = USERIAL_PARITY_NONE; 102 } 103 else 104 uart.m_iParity = USERIAL_PARITY_NONE; 105 106 if ( GetStrValue ( NAME_UART_STOPBITS, temp, sizeof ( temp ) ) ) 107 { 108 if ( strcmp ( temp, "1" ) == 0 ) 109 uart.m_iStopbits = USERIAL_STOPBITS_1; 110 else if ( strcmp ( temp, "2" ) == 0 ) 111 uart.m_iStopbits = USERIAL_STOPBITS_2; 112 else if ( strcmp ( temp, "1.5" ) == 0 ) 113 uart.m_iStopbits = USERIAL_STOPBITS_1_5; 114 } 115 else if ( GetNumValue ( NAME_UART_STOPBITS, &num, sizeof ( num ) ) ) 116 { 117 if ( num == 1 ) 118 uart.m_iStopbits = USERIAL_STOPBITS_1; 119 else if ( num == 2 ) 120 uart.m_iStopbits = USERIAL_STOPBITS_2; 121 } 122 else 123 uart.m_iStopbits = USERIAL_STOPBITS_1; 124 125 if ( GetNumValue ( NAME_UART_DATABITS, &num, sizeof ( num ) ) ) 126 { 127 if ( 5 <= num && num <= 8 ) 128 uart.m_iDatabits = ( 1 << ( num + 1 ) ); 129 } 130 else 131 uart.m_iDatabits = USERIAL_DATABITS_8; 132 133 if ( GetNumValue ( NAME_UART_BAUD, &num, sizeof ( num ) ) ) 134 { 135 if ( num == 300 ) uart.m_iBaudrate = USERIAL_BAUD_300; 136 else if ( num == 600 ) uart.m_iBaudrate = USERIAL_BAUD_600; 137 else if ( num == 1200 ) uart.m_iBaudrate = USERIAL_BAUD_1200; 138 else if ( num == 2400 ) uart.m_iBaudrate = USERIAL_BAUD_2400; 139 else if ( num == 9600 ) uart.m_iBaudrate = USERIAL_BAUD_9600; 140 else if ( num == 19200 ) uart.m_iBaudrate = USERIAL_BAUD_19200; 141 else if ( num == 57600 ) uart.m_iBaudrate = USERIAL_BAUD_57600; 142 else if ( num == 115200 ) uart.m_iBaudrate = USERIAL_BAUD_115200; 143 else if ( num == 230400 ) uart.m_iBaudrate = USERIAL_BAUD_230400; 144 else if ( num == 460800 ) uart.m_iBaudrate = USERIAL_BAUD_460800; 145 else if ( num == 921600 ) uart.m_iBaudrate = USERIAL_BAUD_921600; 146 } 147 else if ( GetStrValue ( NAME_UART_BAUD, temp, sizeof ( temp ) ) ) 148 { 149 if ( strcmp ( temp, "auto" ) == 0 ) 150 uart.m_iBaudrate = USERIAL_BAUD_AUTO; 151 } 152 else 153 uart.m_iBaudrate = USERIAL_BAUD_115200; 154 155 memset (&cfg, 0, sizeof(tUSERIAL_OPEN_CFG)); 156 cfg.fmt = uart.m_iDatabits | uart.m_iParity | uart.m_iStopbits; 157 cfg.baud = uart.m_iBaudrate; 158 159 ALOGD ("%s: uart config=0x%04x, %d\n", __func__, cfg.fmt, cfg.baud); 160 USERIAL_Init(&cfg); 161 162 if ( GetNumValue ( NAME_NFCC_ENABLE_TIMEOUT, &num, sizeof ( num ) ) ) 163 { 164 p_nfc_hal_cfg->nfc_hal_nfcc_enable_timeout = num; 165 } 166 167 if ( GetNumValue ( NAME_NFA_MAX_EE_SUPPORTED, &num, sizeof ( num ) ) && num == 0 ) 168 { 169 // Since NFA_MAX_EE_SUPPORTED is explicetly set to 0, no UICC support is needed. 170 p_nfc_hal_cfg->nfc_hal_hci_uicc_support = 0; 171 } 172 173 HAL_NfcInitialize (); 174 HAL_NfcSetTraceLevel (logLevel); // Initialize HAL's logging level 175 176 retval = 0; 177 ALOGD ("%s: exit %d", __FUNCTION__, retval); 178 return retval; 179 } 180 181 182 int HaiTerminateLibrary () 183 { 184 int retval = EACCES; 185 ALOGD ("%s: enter", __FUNCTION__); 186 187 HAL_NfcTerminate (); 188 gAndroidHalCallback = NULL; 189 gAndroidHalDataCallback = NULL; 190 GKI_shutdown (); 191 retval = 0; 192 ALOGD ("%s: exit %d", __FUNCTION__, retval); 193 return retval; 194 } 195 196 197 int HaiOpen (const bcm2079x_dev_t* device, nfc_stack_callback_t* halCallbackFunc, nfc_stack_data_callback_t* halDataCallbackFunc) 198 { 199 ALOGD ("%s: enter", __FUNCTION__); 200 int retval = EACCES; 201 202 gAndroidHalCallback = halCallbackFunc; 203 gAndroidHalDataCallback = halDataCallbackFunc; 204 205 SyncEventGuard guard (gOpenCompletedEvent); 206 HAL_NfcOpen (BroadcomHalCallback, BroadcomHalDataCallback); 207 gOpenCompletedEvent.wait (); 208 209 retval = 0; 210 ALOGD ("%s: exit %d", __FUNCTION__, retval); 211 return retval; 212 } 213 214 215 void BroadcomHalCallback (UINT8 event, tHAL_NFC_STATUS status) 216 { 217 ALOGD ("%s: enter; event=0x%X", __FUNCTION__, event); 218 switch (event) 219 { 220 case HAL_NFC_OPEN_CPLT_EVT: 221 { 222 ALOGD ("%s: HAL_NFC_OPEN_CPLT_EVT; status=0x%X", __FUNCTION__, status); 223 SyncEventGuard guard (gOpenCompletedEvent); 224 gOpenCompletedEvent.notifyOne (); 225 break; 226 } 227 228 case HAL_NFC_POST_INIT_CPLT_EVT: 229 { 230 ALOGD ("%s: HAL_NFC_POST_INIT_CPLT_EVT", __FUNCTION__); 231 SyncEventGuard guard (gPostInitCompletedEvent); 232 gPostInitCompletedEvent.notifyOne (); 233 break; 234 } 235 236 case HAL_NFC_CLOSE_CPLT_EVT: 237 { 238 ALOGD ("%s: HAL_NFC_CLOSE_CPLT_EVT", __FUNCTION__); 239 SyncEventGuard guard (gCloseCompletedEvent); 240 gCloseCompletedEvent.notifyOne (); 241 break; 242 } 243 244 case HAL_NFC_ERROR_EVT: 245 { 246 ALOGD ("%s: HAL_NFC_ERROR_EVT", __FUNCTION__); 247 { 248 SyncEventGuard guard (gOpenCompletedEvent); 249 gOpenCompletedEvent.notifyOne (); 250 } 251 { 252 SyncEventGuard guard (gPostInitCompletedEvent); 253 gPostInitCompletedEvent.notifyOne (); 254 } 255 { 256 SyncEventGuard guard (gCloseCompletedEvent); 257 gCloseCompletedEvent.notifyOne (); 258 } 259 break; 260 } 261 } 262 gAndroidHalCallback (event, status); 263 ALOGD ("%s: exit; event=0x%X", __FUNCTION__, event); 264 } 265 266 267 void BroadcomHalDataCallback (UINT16 data_len, UINT8* p_data) 268 { 269 ALOGD ("%s: enter; len=%u", __FUNCTION__, data_len); 270 gAndroidHalDataCallback (data_len, p_data); 271 } 272 273 274 int HaiClose (const bcm2079x_dev_t* device) 275 { 276 ALOGD ("%s: enter", __FUNCTION__); 277 int retval = EACCES; 278 279 SyncEventGuard guard (gCloseCompletedEvent); 280 HAL_NfcClose (); 281 gCloseCompletedEvent.wait (); 282 retval = 0; 283 ALOGD ("%s: exit %d", __FUNCTION__, retval); 284 return retval; 285 } 286 287 288 int HaiCoreInitialized (const bcm2079x_dev_t* device, uint8_t* coreInitResponseParams) 289 { 290 ALOGD ("%s: enter", __FUNCTION__); 291 int retval = EACCES; 292 293 SyncEventGuard guard (gPostInitCompletedEvent); 294 HAL_NfcCoreInitialized (coreInitResponseParams); 295 gPostInitCompletedEvent.wait (); 296 retval = 0; 297 ALOGD ("%s: exit %d", __FUNCTION__, retval); 298 return retval; 299 } 300 301 302 int HaiWrite (const bcm2079x_dev_t* dev, uint16_t dataLen, const uint8_t* data) 303 { 304 ALOGD ("%s: enter; len=%u", __FUNCTION__, dataLen); 305 int retval = EACCES; 306 307 HAL_NfcWrite (dataLen, const_cast<UINT8*> (data)); 308 retval = 0; 309 ALOGD ("%s: exit %d", __FUNCTION__, retval); 310 return retval; 311 } 312 313 314 int HaiPreDiscover (const bcm2079x_dev_t* device) 315 { 316 ALOGD ("%s: enter", __FUNCTION__); 317 int retval = EACCES; 318 319 retval = HAL_NfcPreDiscover () ? 1 : 0; 320 ALOGD ("%s: exit %d", __FUNCTION__, retval); 321 return retval; 322 } 323 324 325 int HaiControlGranted (const bcm2079x_dev_t* device) 326 { 327 ALOGD ("%s: enter", __FUNCTION__); 328 int retval = EACCES; 329 330 HAL_NfcControlGranted (); 331 retval = 0; 332 ALOGD ("%s: exit %d", __FUNCTION__, retval); 333 return retval; 334 } 335 336 337 int HaiPowerCycle (const bcm2079x_dev_t* device) 338 { 339 ALOGD ("%s: enter", __FUNCTION__); 340 int retval = EACCES; 341 342 HAL_NfcPowerCycle (); 343 retval = 0; 344 ALOGD ("%s: exit %d", __FUNCTION__, retval); 345 return retval; 346 } 347 348 349 int HaiGetMaxNfcee (const bcm2079x_dev_t* device, uint8_t* maxNfcee) 350 { 351 ALOGD ("%s: enter", __FUNCTION__); 352 int retval = EACCES; 353 354 if ( maxNfcee ) 355 { 356 unsigned long num; 357 358 // At this point we can see if there is a chip-specific value for max ee. 359 if ( GetNumValue ( NAME_NFA_MAX_EE_SUPPORTED, &num, sizeof ( num ) ) ) 360 { 361 *maxNfcee = num; 362 } 363 else 364 *maxNfcee = HAL_NfcGetMaxNfcee (); 365 366 ALOGD("%s: max_ee from HAL to use %d", __FUNCTION__, *maxNfcee); 367 retval = 0; 368 } 369 ALOGD ("%s: exit %d", __FUNCTION__, retval); 370 return retval; 371 } 372