1 /******************************************************************************
2  *
3  *  Copyright (C) 1999-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 #ifndef GKI_H
19 #define GKI_H
20 
21 #ifdef BUILDCFG
22 #include "buildcfg.h"
23 #endif
24 
25 /* Include platform-specific over-rides */
26 #if (defined(NFC_STANDALONE) && (NFC_STANDALONE == TRUE))
27     #include "gki_target.h"
28 #else
29     /* For non-nfc_standalone, include Bluetooth definitions */
30     #include "bt_target.h"
31 #endif
32 
33 
34 
35 #include "bt_types.h"
36 
37 /* Uncomment this line for verbose GKI debugging and buffer tracking */
38 /*#define GKI_BUFFER_DEBUG   TRUE*/
39 
40 
41 /* Error codes */
42 #define GKI_SUCCESS         0x00
43 #define GKI_FAILURE         0x01
44 #define GKI_INVALID_TASK    0xF0
45 #define GKI_INVALID_POOL    0xFF
46 
47 
48 /************************************************************************
49 ** Mailbox definitions. Each task has 4 mailboxes that are used to
50 ** send buffers to the task.
51 */
52 #define TASK_MBOX_0    0
53 #define TASK_MBOX_1    1
54 #define TASK_MBOX_2    2
55 #define TASK_MBOX_3    3
56 
57 #define NUM_TASK_MBOX  4
58 
59 /************************************************************************
60 ** Event definitions.
61 **
62 ** There are 4 reserved events used to signal messages rcvd in task mailboxes.
63 ** There are 4 reserved events used to signal timeout events.
64 ** There are 8 general purpose events available for applications.
65 */
66 #define MAX_EVENTS              16
67 
68 #define TASK_MBOX_0_EVT_MASK   0x0001
69 #define TASK_MBOX_1_EVT_MASK   0x0002
70 #define TASK_MBOX_2_EVT_MASK   0x0004
71 #define TASK_MBOX_3_EVT_MASK   0x0008
72 
73 
74 #define TIMER_0             0
75 #define TIMER_1             1
76 #define TIMER_2             2
77 #define TIMER_3             3
78 
79 #define TIMER_0_EVT_MASK    0x0010
80 #define TIMER_1_EVT_MASK    0x0020
81 #define TIMER_2_EVT_MASK    0x0040
82 #define TIMER_3_EVT_MASK    0x0080
83 
84 #define APPL_EVT_0          8
85 #define APPL_EVT_1          9
86 #define APPL_EVT_2          10
87 #define APPL_EVT_3          11
88 #define APPL_EVT_4          12
89 #define APPL_EVT_5          13
90 #define APPL_EVT_6          14
91 #define APPL_EVT_7          15
92 
93 #define EVENT_MASK(evt)       ((UINT16)(0x0001 << (evt)))
94 
95 /************************************************************************
96 **  Max Time Queue
97 **/
98 #ifndef GKI_MAX_TIMER_QUEUES
99 #define GKI_MAX_TIMER_QUEUES    3
100 #endif
101 
102 /************************************************************************
103 **  Utility macros for timer conversion
104 **/
105 #ifdef TICKS_PER_SEC
106 #define GKI_MS_TO_TICKS(x)   ((x) / (1000/TICKS_PER_SEC))
107 #define GKI_SECS_TO_TICKS(x) ((x) * (TICKS_PER_SEC))
108 #define GKI_TICKS_TO_MS(x)   ((x) * (1000/TICKS_PER_SEC))
109 #define GKI_TICKS_TO_SECS(x) ((x) * (1/TICKS_PER_SEC))
110 #endif
111 
112 
113 /************************************************************************
114 **  Macro to determine the pool buffer size based on the GKI POOL ID at compile time.
115 **  Pool IDs index from 0 to GKI_NUM_FIXED_BUF_POOLS - 1
116 */
117 
118 #if (GKI_NUM_FIXED_BUF_POOLS < 1)
119 
120 #ifndef GKI_POOL_ID_0
121 #define GKI_POOL_ID_0                0
122 #endif /* ifndef GKI_POOL_ID_0 */
123 
124 #ifndef GKI_BUF0_SIZE
125 #define GKI_BUF0_SIZE                0
126 #endif /* ifndef GKI_BUF0_SIZE */
127 
128 #endif /* GKI_NUM_FIXED_BUF_POOLS < 1 */
129 
130 
131 #if (GKI_NUM_FIXED_BUF_POOLS < 2)
132 
133 #ifndef GKI_POOL_ID_1
134 #define GKI_POOL_ID_1                0
135 #endif /* ifndef GKI_POOL_ID_1 */
136 
137 #ifndef GKI_BUF1_SIZE
138 #define GKI_BUF1_SIZE                0
139 #endif /* ifndef GKI_BUF1_SIZE */
140 
141 #endif /* GKI_NUM_FIXED_BUF_POOLS < 2 */
142 
143 
144 #if (GKI_NUM_FIXED_BUF_POOLS < 3)
145 
146 #ifndef GKI_POOL_ID_2
147 #define GKI_POOL_ID_2                0
148 #endif /* ifndef GKI_POOL_ID_2 */
149 
150 #ifndef GKI_BUF2_SIZE
151 #define GKI_BUF2_SIZE                0
152 #endif /* ifndef GKI_BUF2_SIZE */
153 
154 #endif /* GKI_NUM_FIXED_BUF_POOLS < 3 */
155 
156 
157 #if (GKI_NUM_FIXED_BUF_POOLS < 4)
158 
159 #ifndef GKI_POOL_ID_3
160 #define GKI_POOL_ID_3                0
161 #endif /* ifndef GKI_POOL_ID_4 */
162 
163 #ifndef GKI_BUF3_SIZE
164 #define GKI_BUF3_SIZE                0
165 #endif /* ifndef GKI_BUF3_SIZE */
166 
167 #endif /* GKI_NUM_FIXED_BUF_POOLS < 4 */
168 
169 
170 #if (GKI_NUM_FIXED_BUF_POOLS < 5)
171 
172 #ifndef GKI_POOL_ID_4
173 #define GKI_POOL_ID_4                0
174 #endif /* ifndef GKI_POOL_ID_4 */
175 
176 #ifndef GKI_BUF4_SIZE
177 #define GKI_BUF4_SIZE                0
178 #endif /* ifndef GKI_BUF4_SIZE */
179 
180 #endif /* GKI_NUM_FIXED_BUF_POOLS < 5 */
181 
182 
183 #if (GKI_NUM_FIXED_BUF_POOLS < 6)
184 
185 #ifndef GKI_POOL_ID_5
186 #define GKI_POOL_ID_5                0
187 #endif /* ifndef GKI_POOL_ID_5 */
188 
189 #ifndef GKI_BUF5_SIZE
190 #define GKI_BUF5_SIZE                0
191 #endif /* ifndef GKI_BUF5_SIZE */
192 
193 #endif /* GKI_NUM_FIXED_BUF_POOLS < 6 */
194 
195 
196 #if (GKI_NUM_FIXED_BUF_POOLS < 7)
197 
198 #ifndef GKI_POOL_ID_6
199 #define GKI_POOL_ID_6                0
200 #endif /* ifndef GKI_POOL_ID_6 */
201 
202 #ifndef GKI_BUF6_SIZE
203 #define GKI_BUF6_SIZE                0
204 #endif /* ifndef GKI_BUF6_SIZE */
205 
206 #endif /* GKI_NUM_FIXED_BUF_POOLS < 7 */
207 
208 
209 #if (GKI_NUM_FIXED_BUF_POOLS < 8)
210 
211 #ifndef GKI_POOL_ID_7
212 #define GKI_POOL_ID_7                0
213 #endif /* ifndef GKI_POOL_ID_7 */
214 
215 #ifndef GKI_BUF7_SIZE
216 #define GKI_BUF7_SIZE                0
217 #endif /* ifndef GKI_BUF7_SIZE */
218 
219 #endif /* GKI_NUM_FIXED_BUF_POOLS < 8 */
220 
221 
222 #if (GKI_NUM_FIXED_BUF_POOLS < 9)
223 
224 #ifndef GKI_POOL_ID_8
225 #define GKI_POOL_ID_8                0
226 #endif /* ifndef GKI_POOL_ID_8 */
227 
228 #ifndef GKI_BUF8_SIZE
229 #define GKI_BUF8_SIZE                0
230 #endif /* ifndef GKI_BUF8_SIZE */
231 
232 #endif /* GKI_NUM_FIXED_BUF_POOLS < 9 */
233 
234 
235 #if (GKI_NUM_FIXED_BUF_POOLS < 10)
236 
237 #ifndef GKI_POOL_ID_9
238 #define GKI_POOL_ID_9                0
239 #endif /* ifndef GKI_POOL_ID_9 */
240 
241 #ifndef GKI_BUF9_SIZE
242 #define GKI_BUF9_SIZE                0
243 #endif /* ifndef GKI_BUF9_SIZE */
244 
245 #endif /* GKI_NUM_FIXED_BUF_POOLS < 10 */
246 
247 
248 #if (GKI_NUM_FIXED_BUF_POOLS < 11)
249 
250 #ifndef GKI_POOL_ID_10
251 #define GKI_POOL_ID_10                0
252 #endif /* ifndef GKI_POOL_ID_10 */
253 
254 #ifndef GKI_BUF10_SIZE
255 #define GKI_BUF10_SIZE                0
256 #endif /* ifndef GKI_BUF10_SIZE */
257 
258 #endif /* GKI_NUM_FIXED_BUF_POOLS < 11 */
259 
260 
261 #if (GKI_NUM_FIXED_BUF_POOLS < 12)
262 
263 #ifndef GKI_POOL_ID_11
264 #define GKI_POOL_ID_11                0
265 #endif /* ifndef GKI_POOL_ID_11 */
266 
267 #ifndef GKI_BUF11_SIZE
268 #define GKI_BUF11_SIZE                0
269 #endif /* ifndef GKI_BUF11_SIZE */
270 
271 #endif /* GKI_NUM_FIXED_BUF_POOLS < 12 */
272 
273 
274 #if (GKI_NUM_FIXED_BUF_POOLS < 13)
275 
276 #ifndef GKI_POOL_ID_12
277 #define GKI_POOL_ID_12                0
278 #endif /* ifndef GKI_POOL_ID_12 */
279 
280 #ifndef GKI_BUF12_SIZE
281 #define GKI_BUF12_SIZE                0
282 #endif /* ifndef GKI_BUF12_SIZE */
283 
284 #endif /* GKI_NUM_FIXED_BUF_POOLS < 13 */
285 
286 
287 #if (GKI_NUM_FIXED_BUF_POOLS < 14)
288 
289 #ifndef GKI_POOL_ID_13
290 #define GKI_POOL_ID_13                0
291 #endif /* ifndef GKI_POOL_ID_13 */
292 
293 #ifndef GKI_BUF13_SIZE
294 #define GKI_BUF13_SIZE                0
295 #endif /* ifndef GKI_BUF13_SIZE */
296 
297 #endif /* GKI_NUM_FIXED_BUF_POOLS < 14 */
298 
299 
300 #if (GKI_NUM_FIXED_BUF_POOLS < 15)
301 
302 #ifndef GKI_POOL_ID_14
303 #define GKI_POOL_ID_14                0
304 #endif /* ifndef GKI_POOL_ID_14 */
305 
306 #ifndef GKI_BUF14_SIZE
307 #define GKI_BUF14_SIZE                0
308 #endif /* ifndef GKI_BUF14_SIZE */
309 
310 #endif /* GKI_NUM_FIXED_BUF_POOLS < 15 */
311 
312 
313 #if (GKI_NUM_FIXED_BUF_POOLS < 16)
314 
315 #ifndef GKI_POOL_ID_15
316 #define GKI_POOL_ID_15                0
317 #endif /* ifndef GKI_POOL_ID_15 */
318 
319 #ifndef GKI_BUF15_SIZE
320 #define GKI_BUF15_SIZE                0
321 #endif /* ifndef GKI_BUF15_SIZE */
322 
323 #endif /* GKI_NUM_FIXED_BUF_POOLS < 16 */
324 
325 
326 /* Timer list entry callback type
327 */
328 typedef void (TIMER_CBACK)(void *p_tle);
329 #ifndef TIMER_PARAM_TYPE
330 #ifdef  WIN2000
331 #define TIMER_PARAM_TYPE    void *
332 #else
333 #define TIMER_PARAM_TYPE    UINT32
334 #endif
335 #endif
336 /* Define a timer list entry
337 */
338 typedef struct _tle
339 {
340     struct _tle  *p_next;
341     struct _tle  *p_prev;
342     TIMER_CBACK  *p_cback;
343     INT32         ticks;
344     TIMER_PARAM_TYPE   param;
345     UINT16        event;
346     UINT8         in_use;
347 } TIMER_LIST_ENT;
348 
349 /* Define a timer list queue
350 */
351 typedef struct
352 {
353     TIMER_LIST_ENT   *p_first;
354     TIMER_LIST_ENT   *p_last;
355     INT32             last_ticks;
356 } TIMER_LIST_Q;
357 
358 
359 /***********************************************************************
360 ** This queue is a general purpose buffer queue, for application use.
361 */
362 typedef struct
363 {
364     void    *p_first;
365     void    *p_last;
366     UINT16   count;
367 } BUFFER_Q;
368 
369 #define GKI_IS_QUEUE_EMPTY(p_q) ((p_q)->count == 0)
370 
371 /* Task constants
372 */
373 #ifndef TASKPTR
374 typedef void (*TASKPTR)(UINT32);
375 #endif
376 
377 
378 #define GKI_PUBLIC_POOL         0       /* General pool accessible to GKI_getbuf() */
379 #define GKI_RESTRICTED_POOL     1       /* Inaccessible pool to GKI_getbuf() */
380 
381 /***********************************************************************
382 ** Function prototypes
383 */
384 
385 #ifdef __cplusplus
386 extern "C" {
387 #endif
388 
389 /* Task management
390 */
391 GKI_API extern UINT8   GKI_create_task (TASKPTR, UINT8, INT8 *, UINT16 *, UINT16, void*, void*);
392 GKI_API extern void    GKI_exit_task(UINT8);
393 GKI_API extern UINT8   GKI_get_taskid(void);
394 GKI_API extern void    GKI_init(void);
395 GKI_API extern INT8   *GKI_map_taskname(UINT8);
396 GKI_API extern UINT8   GKI_resume_task(UINT8);
397 GKI_API extern void    GKI_run(void *);
398 GKI_API extern void    GKI_stop(void);
399 GKI_API extern UINT8   GKI_suspend_task(UINT8);
400 GKI_API extern UINT8   GKI_is_task_running(UINT8);
401 
402 /* memory management
403 */
404 GKI_API extern void GKI_shiftdown (UINT8 *p_mem, UINT32 len, UINT32 shift_amount);
405 GKI_API extern void GKI_shiftup (UINT8 *p_dest, UINT8 *p_src, UINT32 len);
406 
407 /* To send buffers and events between tasks
408 */
409 GKI_API extern UINT8   GKI_isend_event (UINT8, UINT16);
410 GKI_API extern void    GKI_isend_msg (UINT8, UINT8, void *);
411 GKI_API extern void   *GKI_read_mbox  (UINT8);
412 GKI_API extern void    GKI_send_msg   (UINT8, UINT8, void *);
413 GKI_API extern UINT8   GKI_send_event (UINT8, UINT16);
414 
415 
416 /* To get and release buffers, change owner and get size
417 */
418 GKI_API extern void    GKI_change_buf_owner (void *, UINT8);
419 GKI_API extern UINT8   GKI_create_pool (UINT16, UINT16, UINT8, void *);
420 GKI_API extern void    GKI_delete_pool (UINT8);
421 GKI_API extern void   *GKI_find_buf_start (void *);
422 GKI_API extern void    GKI_freebuf (void *);
423 #if GKI_BUFFER_DEBUG
424 #define GKI_getbuf(size)    GKI_getbuf_debug(size, __FUNCTION__, __LINE__)
425 GKI_API extern void   *GKI_getbuf_debug (UINT16, const char *, int);
426 #else
427 GKI_API extern void   *GKI_getbuf (UINT16);
428 #endif
429 GKI_API extern UINT16  GKI_get_buf_size (void *);
430 #if GKI_BUFFER_DEBUG
431 #define GKI_getpoolbuf(id)    GKI_getpoolbuf_debug(id, __FUNCTION__, __LINE__)
432 GKI_API extern void   *GKI_getpoolbuf_debug (UINT8, const char *, int);
433 #else
434 GKI_API extern void   *GKI_getpoolbuf (UINT8);
435 #endif
436 
437 GKI_API extern UINT16  GKI_poolcount (UINT8);
438 GKI_API extern UINT16  GKI_poolfreecount (UINT8);
439 GKI_API extern UINT16  GKI_poolutilization (UINT8);
440 GKI_API extern void    GKI_register_mempool (void *p_mem);
441 GKI_API extern UINT8   GKI_set_pool_permission(UINT8, UINT8);
442 
443 
444 /* User buffer queue management
445 */
446 GKI_API extern void   *GKI_dequeue  (BUFFER_Q *);
447 GKI_API extern void    GKI_enqueue (BUFFER_Q *, void *);
448 GKI_API extern void    GKI_enqueue_head (BUFFER_Q *, void *);
449 GKI_API extern void   *GKI_getfirst (BUFFER_Q *);
450 GKI_API extern void   *GKI_getlast (BUFFER_Q *);
451 GKI_API extern void   *GKI_getnext (void *);
452 GKI_API extern void    GKI_init_q (BUFFER_Q *);
453 GKI_API extern BOOLEAN GKI_queue_is_empty(BUFFER_Q *);
454 GKI_API extern void   *GKI_remove_from_queue (BUFFER_Q *, void *);
455 GKI_API extern UINT16  GKI_get_pool_bufsize (UINT8);
456 
457 /* Timer management
458 */
459 GKI_API extern void    GKI_add_to_timer_list (TIMER_LIST_Q *, TIMER_LIST_ENT  *);
460 GKI_API extern void    GKI_delay(UINT32);
461 GKI_API extern UINT32  GKI_get_tick_count(void);
462 GKI_API extern INT8   *GKI_get_time_stamp(INT8 *);
463 GKI_API extern void    GKI_init_timer_list (TIMER_LIST_Q *);
464 GKI_API extern void    GKI_init_timer_list_entry (TIMER_LIST_ENT  *);
465 GKI_API extern INT32   GKI_ready_to_sleep (void);
466 GKI_API extern void    GKI_remove_from_timer_list (TIMER_LIST_Q *, TIMER_LIST_ENT  *);
467 GKI_API extern void    GKI_start_timer(UINT8, INT32, BOOLEAN);
468 GKI_API extern void    GKI_stop_timer (UINT8);
469 GKI_API extern void    GKI_timer_update(INT32);
470 GKI_API extern UINT16  GKI_update_timer_list (TIMER_LIST_Q *, INT32);
471 GKI_API extern UINT32  GKI_get_remaining_ticks (TIMER_LIST_Q *, TIMER_LIST_ENT  *);
472 GKI_API extern UINT16  GKI_wait(UINT16, UINT32);
473 
474 /* Start and Stop system time tick callback
475  * true for start system tick if time queue is not empty
476  * false to stop system tick if time queue is empty
477 */
478 typedef void (SYSTEM_TICK_CBACK)(BOOLEAN);
479 
480 /* Time queue management for system ticks
481 */
482 GKI_API extern BOOLEAN GKI_timer_queue_empty (void);
483 GKI_API extern void    GKI_timer_queue_register_callback(SYSTEM_TICK_CBACK *);
484 
485 /* Disable Interrupts, Enable Interrupts
486 */
487 GKI_API extern void    GKI_enable(void);
488 GKI_API extern void    GKI_disable(void);
489 GKI_API extern void    GKI_sched_lock(void);
490 GKI_API extern void    GKI_sched_unlock(void);
491 
492 /* Allocate (Free) memory from an OS
493 */
494 GKI_API extern void     *GKI_os_malloc (UINT32);
495 GKI_API extern void      GKI_os_free (void *);
496 
497 /* os timer operation */
498 GKI_API extern UINT32 GKI_get_os_tick_count(void);
499 
500 /* Exception handling
501 */
502 GKI_API extern void    GKI_exception (UINT16, char *);
503 
504 #if GKI_DEBUG == TRUE
505 GKI_API extern void    GKI_PrintBufferUsage(UINT8 *p_num_pools, UINT16 *p_cur_used);
506 GKI_API extern void    GKI_PrintBuffer(void);
507 GKI_API extern void    GKI_print_task(void);
508 #else
509 #undef GKI_PrintBufferUsage
510 #define GKI_PrintBuffer() NULL
511 #endif
512 
513 #ifdef __cplusplus
514 }
515 #endif
516 
517 
518 #endif
519 
520