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