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