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