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