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
36
37///////////////////////////////////////
38// private declaration, definition
39
40
41static nfc_stack_callback_t* gAndroidHalCallback = NULL;
42static nfc_stack_data_callback_t* gAndroidHalDataCallback = NULL;
43static SyncEvent gOpenCompletedEvent;
44static SyncEvent gPostInitCompletedEvent;
45static SyncEvent gCloseCompletedEvent;
46
47UINT32 ScrProtocolTraceFlag = SCR_PROTO_TRACE_ALL; //0x017F00;
48
49static void BroadcomHalCallback (UINT8 event, tHAL_NFC_STATUS status);
50static void BroadcomHalDataCallback (UINT16 data_len, UINT8* p_data);
51
52
53///////////////////////////////////////
54
55
56int HaiInitializeLibrary (const bcm2079x_dev_t* device)
57{
58    ALOGD ("%s: enter", __FUNCTION__);
59    int retval = EACCES;
60    unsigned long freq = 0;
61    unsigned long num = 0;
62
63    InitializeGlobalAppLogLevel ();
64
65    //initialize the crystal frequency
66    if (GetNumValue((char*)NAME_XTAL_FREQUENCY, &freq, sizeof(freq)))
67    {
68        ALOGD("%s: setting xtal frequency=%lu", __FUNCTION__, freq);
69        nfc_post_reset_cb.dev_init_config.xtal_freq = (UINT16) freq;
70        nfc_post_reset_cb.dev_init_config.flags |= NFC_HAL_DEV_INIT_FLAGS_SET_XTAL_FREQ;
71    }
72
73    // Initialize protocol logging level
74    if ( GetNumValue ( NAME_PROTOCOL_TRACE_LEVEL, &num, sizeof ( num ) ) )
75        ScrProtocolTraceFlag = num;
76
77    HAL_NfcInitialize ();
78
79    // Initialize appliation logging level
80    if ( GetNumValue ( NAME_APPL_TRACE_LEVEL, &num, sizeof ( num ) ) ) {
81        HAL_NfcSetTraceLevel(num);
82    }
83
84    retval = 0;
85    ALOGD ("%s: exit %d", __FUNCTION__, retval);
86    return retval;
87}
88
89
90int HaiTerminateLibrary ()
91{
92    int retval = EACCES;
93    ALOGD ("%s: enter", __FUNCTION__);
94
95    HAL_NfcTerminate ();
96    gAndroidHalCallback = NULL;
97    gAndroidHalDataCallback = NULL;
98    GKI_shutdown ();
99    retval = 0;
100    ALOGD ("%s: exit %d", __FUNCTION__, retval);
101    return retval;
102}
103
104
105int HaiOpen (const bcm2079x_dev_t* device, nfc_stack_callback_t* halCallbackFunc, nfc_stack_data_callback_t* halDataCallbackFunc)
106{
107    ALOGD ("%s: enter", __FUNCTION__);
108    int retval = EACCES;
109
110    gAndroidHalCallback = halCallbackFunc;
111    gAndroidHalDataCallback = halDataCallbackFunc;
112
113    SyncEventGuard guard (gOpenCompletedEvent);
114    HAL_NfcOpen (BroadcomHalCallback, BroadcomHalDataCallback);
115    gOpenCompletedEvent.wait ();
116
117    retval = 0;
118    ALOGD ("%s: exit %d", __FUNCTION__, retval);
119    return retval;
120}
121
122
123void BroadcomHalCallback (UINT8 event, tHAL_NFC_STATUS status)
124{
125    ALOGD ("%s: enter; event=0x%X", __FUNCTION__, event);
126    switch (event)
127    {
128    case HAL_NFC_OPEN_CPLT_EVT:
129        {
130            ALOGD ("%s: HAL_NFC_OPEN_CPLT_EVT; status=0x%X", __FUNCTION__, status);
131            SyncEventGuard guard (gOpenCompletedEvent);
132            gOpenCompletedEvent.notifyOne ();
133            break;
134        }
135
136    case HAL_NFC_POST_INIT_CPLT_EVT:
137        {
138            ALOGD ("%s: HAL_NFC_POST_INIT_CPLT_EVT", __FUNCTION__);
139            SyncEventGuard guard (gPostInitCompletedEvent);
140            gPostInitCompletedEvent.notifyOne ();
141            break;
142        }
143
144    case HAL_NFC_CLOSE_CPLT_EVT:
145        {
146            ALOGD ("%s: HAL_NFC_CLOSE_CPLT_EVT", __FUNCTION__);
147            SyncEventGuard guard (gCloseCompletedEvent);
148            gCloseCompletedEvent.notifyOne ();
149            break;
150        }
151
152    case HAL_NFC_ERROR_EVT:
153        {
154            ALOGD ("%s: HAL_NFC_ERROR_EVT", __FUNCTION__);
155            {
156                SyncEventGuard guard (gOpenCompletedEvent);
157                gOpenCompletedEvent.notifyOne ();
158            }
159            {
160                SyncEventGuard guard (gPostInitCompletedEvent);
161                gPostInitCompletedEvent.notifyOne ();
162            }
163            {
164                SyncEventGuard guard (gCloseCompletedEvent);
165                gCloseCompletedEvent.notifyOne ();
166            }
167            break;
168        }
169    }
170    gAndroidHalCallback (event, status);
171    ALOGD ("%s: exit; event=0x%X", __FUNCTION__, event);
172}
173
174
175void BroadcomHalDataCallback (UINT16 data_len, UINT8* p_data)
176{
177    ALOGD ("%s: enter; len=%u", __FUNCTION__, data_len);
178    gAndroidHalDataCallback (data_len, p_data);
179}
180
181
182int HaiClose (const bcm2079x_dev_t* device)
183{
184    ALOGD ("%s: enter", __FUNCTION__);
185    int retval = EACCES;
186
187    SyncEventGuard guard (gCloseCompletedEvent);
188    HAL_NfcClose ();
189    gCloseCompletedEvent.wait ();
190    retval = 0;
191    ALOGD ("%s: exit %d", __FUNCTION__, retval);
192    return retval;
193}
194
195
196int HaiCoreInitialized (const bcm2079x_dev_t* device, uint8_t* coreInitResponseParams)
197{
198    ALOGD ("%s: enter", __FUNCTION__);
199    int retval = EACCES;
200
201    SyncEventGuard guard (gPostInitCompletedEvent);
202    HAL_NfcCoreInitialized (coreInitResponseParams);
203    gPostInitCompletedEvent.wait ();
204    retval = 0;
205    ALOGD ("%s: exit %d", __FUNCTION__, retval);
206    return retval;
207}
208
209
210int HaiWrite (const bcm2079x_dev_t* dev, uint16_t dataLen, const uint8_t* data)
211{
212    ALOGD ("%s: enter; len=%u", __FUNCTION__, dataLen);
213    int retval = EACCES;
214
215    HAL_NfcWrite (dataLen, const_cast<UINT8*> (data));
216    retval = 0;
217    ALOGD ("%s: exit %d", __FUNCTION__, retval);
218    return retval;
219}
220
221
222int HaiPreDiscover (const bcm2079x_dev_t* device)
223{
224    ALOGD ("%s: enter", __FUNCTION__);
225    int retval = EACCES;
226
227    HAL_NfcPreDiscover ();
228    retval = 0;
229    ALOGD ("%s: exit %d", __FUNCTION__, retval);
230    return retval;
231}
232
233
234int HaiControlGranted (const bcm2079x_dev_t* device)
235{
236    ALOGD ("%s: enter", __FUNCTION__);
237    int retval = EACCES;
238
239    HAL_NfcControlGranted ();
240    retval = 0;
241    ALOGD ("%s: exit %d", __FUNCTION__, retval);
242    return retval;
243}
244
245
246int HaiPowerCycle (const bcm2079x_dev_t* device)
247{
248    ALOGD ("%s: enter", __FUNCTION__);
249    int retval = EACCES;
250
251    HAL_NfcPowerCycle ();
252    retval = 0;
253    ALOGD ("%s: exit %d", __FUNCTION__, retval);
254    return retval;
255}
256