1 /******************************************************************************
2 *
3 * Copyright (C) 2012-2014 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 *
22 * NFC Hardware Abstraction Layer API: Implementation for Broadcom NFC
23 * controllers
24 *
25 ******************************************************************************/
26 #include <string.h>
27 #include "gki.h"
28 #include "nfc_hal_target.h"
29 #include "nfc_hal_api.h"
30 #include "nfc_hal_int.h"
31
32 /*******************************************************************************
33 ** NFC_HAL_TASK declarations
34 *******************************************************************************/
35 #define NFC_HAL_TASK_STR ((INT8 *) "NFC_HAL_TASK")
36 #define NFC_HAL_TASK_STACK_SIZE 0x400
37 UINT32 nfc_hal_task_stack[(NFC_HAL_TASK_STACK_SIZE+3)/4];
38
39 /*******************************************************************************
40 **
41 ** Function HAL_NfcInitialize
42 **
43 ** Description Called when HAL library is loaded.
44 **
45 ** Initialize GKI and start the HCIT task
46 **
47 ** Returns void
48 **
49 *******************************************************************************/
HAL_NfcInitialize(void)50 void HAL_NfcInitialize (void)
51 {
52 /* Initialize HAL control block */
53 nfc_hal_main_init ();
54
55 HAL_TRACE_API1 ("HAL_NfcInitialize (): NFC_HAL_TASK id=%i", NFC_HAL_TASK);
56
57
58 #ifndef NFC_HAL_SHARED_GKI
59 /* Initialize GKI (not needed if using shared NFC/HAL GKI resources) */
60 GKI_init ();
61 GKI_enable ();
62 #endif
63
64 /* Create the NCI transport task */
65 GKI_create_task ((TASKPTR)nfc_hal_main_task,
66 NFC_HAL_TASK,
67 NFC_HAL_TASK_STR,
68 (UINT16 *) ((UINT8 *)nfc_hal_task_stack + NFC_HAL_TASK_STACK_SIZE),
69 sizeof(nfc_hal_task_stack), NULL, NULL);
70
71 #ifndef NFC_HAL_SHARED_GKI
72 /* Start GKI scheduler (not needed if using shared NFC/HAL GKI resources) */
73 GKI_run (0);
74 #endif
75 }
76
77 /*******************************************************************************
78 **
79 ** Function HAL_NfcTerminate
80 **
81 ** Description Called to terminate NFC HAL
82 **
83 ** Returns void
84 **
85 *******************************************************************************/
HAL_NfcTerminate(void)86 void HAL_NfcTerminate(void)
87 {
88 HAL_TRACE_API0 ("HAL_NfcTerminate ()");
89 }
90
91
92 /*******************************************************************************
93 **
94 ** Function HAL_NfcOpen
95 **
96 ** Description Open transport and intialize the NFCC, and
97 ** Register callback for HAL event notifications,
98 **
99 ** HAL_OPEN_CPLT_EVT will notify when operation is complete.
100 **
101 ** Returns void
102 **
103 *******************************************************************************/
HAL_NfcOpen(tHAL_NFC_CBACK * p_hal_cback,tHAL_NFC_DATA_CBACK * p_data_cback)104 void HAL_NfcOpen (tHAL_NFC_CBACK *p_hal_cback, tHAL_NFC_DATA_CBACK *p_data_cback)
105 {
106 HAL_TRACE_API0 ("HAL_NfcOpen ()");
107
108 /* Only handle if HAL is not opened (stack cback is NULL) */
109 if (p_hal_cback)
110 {
111 nfc_hal_dm_init ();
112 nfc_hal_cb.p_stack_cback = p_hal_cback;
113 nfc_hal_cb.p_data_cback = p_data_cback;
114
115 /* Send startup event to NFC_HAL_TASK */
116 GKI_send_event (NFC_HAL_TASK, NFC_HAL_TASK_EVT_INITIALIZE);
117 }
118 }
119
120 /*******************************************************************************
121 **
122 ** Function HAL_NfcClose
123 **
124 ** Description Prepare for shutdown. A HAL_CLOSE_DONE_EVENT will be
125 ** reported when complete.
126 **
127 ** Returns void
128 **
129 *******************************************************************************/
HAL_NfcClose(void)130 void HAL_NfcClose (void)
131 {
132 HAL_TRACE_API0 ("HAL_NfcClose ()");
133
134 /* Only handle if HAL is opened (stack cback is not-NULL) */
135 if (nfc_hal_cb.p_stack_cback)
136 {
137 /* Send shutdown event to NFC_HAL_TASK */
138 GKI_send_event (NFC_HAL_TASK, NFC_HAL_TASK_EVT_TERMINATE);
139 }
140 }
141
142 /*******************************************************************************
143 **
144 ** Function HAL_NfcCoreInitialized
145 **
146 ** Description Called after the CORE_INIT_RSP is received from the NFCC.
147 ** At this time, the HAL can do any chip-specific configuration,
148 ** and when finished signal the libnfc-nci with event
149 ** HAL_POST_INIT_DONE.
150 **
151 ** Returns void
152 **
153 *******************************************************************************/
HAL_NfcCoreInitialized(UINT8 * p_core_init_rsp_params)154 void HAL_NfcCoreInitialized (UINT8 *p_core_init_rsp_params)
155 {
156 NFC_HDR *p_msg;
157 UINT16 size;
158
159 HAL_TRACE_API0 ("HAL_NfcCoreInitialized ()");
160
161 /* NCI payload len + NCI header size */
162 size = p_core_init_rsp_params[2] + NCI_MSG_HDR_SIZE;
163
164 /* Send message to NFC_HAL_TASK */
165 if ((p_msg = (NFC_HDR *)GKI_getbuf ((UINT16)(size + NFC_HDR_SIZE))) != NULL)
166 {
167 p_msg->event = NFC_HAL_EVT_POST_CORE_RESET;
168 p_msg->offset = 0;
169 p_msg->len = size;
170 p_msg->layer_specific = 0;
171 memcpy ((UINT8 *)(p_msg + 1) + p_msg->offset, p_core_init_rsp_params, size);
172
173 GKI_send_msg (NFC_HAL_TASK, NFC_HAL_TASK_MBOX, p_msg);
174 }
175 }
176
177 /*******************************************************************************
178 **
179 ** Function HAL_NfcWrite
180 **
181 ** Description Send an NCI control message or data packet to the
182 ** transport. If an NCI command message exceeds the transport
183 ** size, HAL is responsible for fragmenting it, Data packets
184 ** must be of the correct size.
185 **
186 ** Returns void
187 **
188 *******************************************************************************/
HAL_NfcWrite(UINT16 data_len,UINT8 * p_data)189 void HAL_NfcWrite (UINT16 data_len, UINT8 *p_data)
190 {
191 NFC_HDR *p_msg;
192 UINT8 mt;
193
194 HAL_TRACE_API0 ("HAL_NfcWrite ()");
195
196 if (data_len > (NCI_MAX_CTRL_SIZE + NCI_MSG_HDR_SIZE))
197 {
198 HAL_TRACE_ERROR1 ("HAL_NfcWrite (): too many bytes (%d)", data_len);
199 return;
200 }
201
202 /* Send message to NFC_HAL_TASK */
203 if ((p_msg = (NFC_HDR *)GKI_getpoolbuf (NFC_HAL_NCI_POOL_ID)) != NULL)
204 {
205 p_msg->event = NFC_HAL_EVT_TO_NFC_NCI;
206 p_msg->offset = NFC_HAL_NCI_MSG_OFFSET_SIZE;
207 p_msg->len = data_len;
208 memcpy ((UINT8 *)(p_msg+1) + p_msg->offset, p_data, data_len);
209
210 /* Check if message is a command or data */
211 mt = (*(p_data) & NCI_MT_MASK) >> NCI_MT_SHIFT;
212 p_msg->layer_specific = (mt == NCI_MT_CMD) ? NFC_HAL_WAIT_RSP_CMD : 0;
213
214
215 GKI_send_msg (NFC_HAL_TASK, NFC_HAL_TASK_MBOX, p_msg);
216 }
217 }
218
219 /*******************************************************************************
220 **
221 ** Function HAL_NfcPreDiscover
222 **
223 ** Description Perform any vendor-specific pre-discovery actions (if needed)
224 ** If any actions were performed TRUE will be returned, and
225 ** HAL_PRE_DISCOVER_DONE_EVENT will notify when actions are
226 ** completed.
227 **
228 ** Returns TRUE if vendor-specific pre-discovery actions initialized
229 ** FALSE if no vendor-specific pre-discovery actions are needed.
230 **
231 *******************************************************************************/
HAL_NfcPreDiscover(void)232 BOOLEAN HAL_NfcPreDiscover (void)
233 {
234 BOOLEAN status = FALSE;
235
236 NFC_HDR *p_msg;
237
238 HAL_TRACE_API0 ("HAL_NfcPreDiscover ()");
239 if (nfc_hal_cb.pre_discover_done == FALSE)
240 {
241 nfc_hal_cb.pre_discover_done = TRUE;
242 if (p_nfc_hal_pre_discover_cfg && *p_nfc_hal_pre_discover_cfg)
243 {
244 status = TRUE;
245 /* Send message to NFC_HAL_TASK */
246 if ((p_msg = (NFC_HDR *)GKI_getpoolbuf (NFC_HAL_NCI_POOL_ID)) != NULL)
247 {
248 p_msg->event = NFC_HAL_EVT_PRE_DISCOVER;
249 GKI_send_msg (NFC_HAL_TASK, NFC_HAL_TASK_MBOX, p_msg);
250 }
251 }
252 }
253
254 HAL_TRACE_API1 ("HAL_NfcPreDiscover status:%d", status);
255 return status;
256 }
257
258 /*******************************************************************************
259 **
260 ** Function HAL_NfcControlGranted
261 **
262 ** Description Grant control to HAL control for sending NCI commands.
263 **
264 ** Call in response to HAL_REQUEST_CONTROL_EVENT.
265 **
266 ** Must only be called when there are no NCI commands pending.
267 **
268 ** HAL_RELEASE_CONTROL_EVENT will notify when HAL no longer
269 ** needs control of NCI.
270 **
271 **
272 ** Returns void
273 **
274 *******************************************************************************/
HAL_NfcControlGranted(void)275 void HAL_NfcControlGranted (void)
276 {
277 NFC_HDR *p_msg;
278 HAL_TRACE_API0 ("HAL_NfcControlGranted ()");
279
280 /* Send message to NFC_HAL_TASK */
281 if ((p_msg = (NFC_HDR *)GKI_getpoolbuf (NFC_HAL_NCI_POOL_ID)) != NULL)
282 {
283 p_msg->event = NFC_HAL_EVT_CONTROL_GRANTED;
284 GKI_send_msg (NFC_HAL_TASK, NFC_HAL_TASK_MBOX, p_msg);
285 }
286 }
287
288 /*******************************************************************************
289 **
290 ** Function HAL_NfcPowerCycle
291 **
292 ** Description Restart NFCC by power cyle
293 **
294 ** HAL_OPEN_CPLT_EVT will notify when operation is complete.
295 **
296 ** Returns void
297 **
298 *******************************************************************************/
HAL_NfcPowerCycle(void)299 void HAL_NfcPowerCycle (void)
300 {
301 HAL_TRACE_API0 ("HAL_NfcPowerCycle ()");
302
303 /* Only handle if HAL is opened (stack cback is not-NULL) */
304 if (nfc_hal_cb.p_stack_cback)
305 {
306 /* Send power cycle event to NFC_HAL_TASK */
307 GKI_send_event (NFC_HAL_TASK, NFC_HAL_TASK_EVT_POWER_CYCLE);
308 }
309 }
310
311 /*******************************************************************************
312 **
313 ** Function HAL_NfcGetMaxNfcee
314 **
315 ** Description Retrieve the maximum number of NFCEEs supported by NFCC
316 **
317 ** Returns the maximum number of NFCEEs supported by NFCC
318 **
319 *******************************************************************************/
HAL_NfcGetMaxNfcee(void)320 UINT8 HAL_NfcGetMaxNfcee (void)
321 {
322 HAL_TRACE_API1 ("HAL_NfcGetMaxNfcee: %d",nfc_hal_cb.max_ee);
323 return nfc_hal_cb.max_ee;
324 }
325
326
327