OSDN Git Service

resolved conflicts for merge of 51356020 to master
[android-x86/system-bt.git] / gki / common / gki_time.c
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
19 #include <utils/Log.h>
20 #include "gki_int.h"
21
22 #ifndef BT_ERROR_TRACE_0
23 #define BT_ERROR_TRACE_0(l,m)
24 #endif
25
26 /* Make sure that this has been defined in target.h */
27 #ifndef GKI_NUM_TIMERS
28 #error  NO TIMERS: Must define at least 1 timer in the system!
29 #endif
30
31
32 #define GKI_NO_NEW_TMRS_STARTED (0x7fffffffL)   /* Largest signed positive timer count */
33 #define GKI_UNUSED_LIST_ENTRY   (0x80000000L)   /* Marks an unused timer list entry (initial value) */
34 #define GKI_MAX_INT32           (0x7fffffffL)
35
36 #define GKI_ERROR(fmt, ...)  ALOGE ("ERROR : %s: " fmt, __FUNCTION__, ## __VA_ARGS__)
37
38 // Used for controlling alarms from AlarmService.
39 extern void alarm_service_reschedule(void);
40
41 /*******************************************************************************
42 **
43 ** Function         gki_timers_init
44 **
45 ** Description      This internal function is called once at startup to initialize
46 **                  all the timer structures.
47 **
48 ** Returns          void
49 **
50 *******************************************************************************/
51 void gki_timers_init(void)
52 {
53     UINT8   tt;
54
55     gki_cb.com.OSTicksTilExp = 0;       /* Remaining time (of OSTimeCurTimeout) before next timer expires */
56     gki_cb.com.OSNumOrigTicks = 0;
57 #if (defined(GKI_DELAY_STOP_SYS_TICK) && (GKI_DELAY_STOP_SYS_TICK > 0))
58     gki_cb.com.OSTicksTilStop = 0;      /* clear inactivity delay timer */
59 #endif
60
61     for (tt = 0; tt < GKI_MAX_TASKS; tt++)
62     {
63         gki_cb.com.OSWaitTmr   [tt] = 0;
64
65 #if (GKI_NUM_TIMERS > 0)
66         gki_cb.com.OSTaskTmr0  [tt] = 0;
67         gki_cb.com.OSTaskTmr0R [tt] = 0;
68 #endif
69
70 #if (GKI_NUM_TIMERS > 1)
71         gki_cb.com.OSTaskTmr1  [tt] = 0;
72         gki_cb.com.OSTaskTmr1R [tt] = 0;
73 #endif
74
75 #if (GKI_NUM_TIMERS > 2)
76         gki_cb.com.OSTaskTmr2  [tt] = 0;
77         gki_cb.com.OSTaskTmr2R [tt] = 0;
78 #endif
79
80 #if (GKI_NUM_TIMERS > 3)
81         gki_cb.com.OSTaskTmr3  [tt] = 0;
82         gki_cb.com.OSTaskTmr3R [tt] = 0;
83 #endif
84     }
85
86     for (tt = 0; tt < GKI_MAX_TIMER_QUEUES; tt++)
87     {
88         gki_cb.com.timer_queues[tt] = NULL;
89     }
90
91     gki_cb.com.p_tick_cb = NULL;
92     gki_cb.com.system_tick_running = FALSE;
93
94     return;
95 }
96
97 /*******************************************************************************
98 **
99 ** Function         gki_timers_is_timer_running
100 **
101 ** Description      This internal function is called to test if any gki timer are running
102 **
103 **
104 ** Returns          TRUE if at least one time is running in the system, FALSE else.
105 **
106 *******************************************************************************/
107 BOOLEAN gki_timers_is_timer_running(void)
108 {
109     UINT8   tt;
110     for (tt = 0; tt < GKI_MAX_TASKS; tt++)
111     {
112
113 #if (GKI_NUM_TIMERS > 0)
114         if(gki_cb.com.OSTaskTmr0  [tt])
115         {
116             return TRUE;
117         }
118 #endif
119
120 #if (GKI_NUM_TIMERS > 1)
121         if(gki_cb.com.OSTaskTmr1  [tt] )
122         {
123             return TRUE;
124         }
125 #endif
126
127 #if (GKI_NUM_TIMERS > 2)
128         if(gki_cb.com.OSTaskTmr2  [tt] )
129         {
130             return TRUE;
131         }
132 #endif
133
134 #if (GKI_NUM_TIMERS > 3)
135         if(gki_cb.com.OSTaskTmr3  [tt] )
136         {
137             return TRUE;
138         }
139 #endif
140     }
141
142     return FALSE;
143
144 }
145
146 /*******************************************************************************
147 **
148 ** Function         GKI_get_tick_count
149 **
150 ** Description      This function returns the current system ticks
151 **
152 ** Returns          The current number of system ticks
153 **
154 *******************************************************************************/
155 UINT32  GKI_get_tick_count(void)
156 {
157     return gki_cb.com.OSTicks;
158 }
159
160
161 /*******************************************************************************
162 **
163 ** Function         GKI_ready_to_sleep
164 **
165 ** Description      This function returns the number of system ticks until the
166 **                  next timer will expire.  It is typically called by a power
167 **                  savings manager to find out how long it can have the system
168 **                  sleep before it needs to service the next entry.
169 **
170 ** Parameters:      None
171 **
172 ** Returns          Number of ticks til the next timer expires
173 **                  Note: the value is a signed  value.  This value should be
174 **                      compared to x > 0, to avoid misinterpreting negative tick
175 **                      values.
176 **
177 *******************************************************************************/
178 INT32    GKI_ready_to_sleep (void)
179 {
180     return (gki_cb.com.OSTicksTilExp);
181 }
182
183
184 /*******************************************************************************
185 **
186 ** Function         GKI_start_timer
187 **
188 ** Description      An application can call this function to start one of
189 **                  it's four general purpose timers. Any of the four timers
190 **                  can be 1-shot or continuous. If a timer is already running,
191 **                  it will be reset to the new parameters.
192 **
193 ** Parameters       tnum            - (input) timer number to be started (TIMER_0,
194 **                                              TIMER_1, TIMER_2, or TIMER_3)
195 **                  ticks           - (input) the number of system ticks til the
196 **                                              timer expires.
197 **                  is_continuous   - (input) TRUE if timer restarts automatically,
198 **                                              else FALSE if it is a 'one-shot'.
199 **
200 ** Returns          void
201 **
202 *******************************************************************************/
203 void GKI_start_timer (UINT8 tnum, INT32 ticks, BOOLEAN is_continuous)
204 {
205     INT32   reload;
206     INT32   orig_ticks;
207     UINT8   task_id = GKI_get_taskid();
208     BOOLEAN bad_timer = FALSE;
209
210     if (ticks <= 0)
211         ticks = 1;
212
213     orig_ticks = ticks;     /* save the ticks in case adjustment is necessary */
214
215
216     /* If continuous timer, set reload, else set it to 0 */
217     if (is_continuous)
218         reload = ticks;
219     else
220         reload = 0;
221
222     GKI_disable();
223
224     if(gki_timers_is_timer_running() == FALSE)
225     {
226 #if (defined(GKI_DELAY_STOP_SYS_TICK) && (GKI_DELAY_STOP_SYS_TICK > 0))
227         /* if inactivity delay timer is not running, start system tick */
228         if(gki_cb.com.OSTicksTilStop == 0)
229         {
230 #endif
231             if(gki_cb.com.p_tick_cb)
232             {
233                 /* start system tick */
234                 gki_cb.com.system_tick_running = TRUE;
235                 (gki_cb.com.p_tick_cb) (TRUE);
236             }
237 #if (defined(GKI_DELAY_STOP_SYS_TICK) && (GKI_DELAY_STOP_SYS_TICK > 0))
238         }
239         else
240         {
241             /* clear inactivity delay timer */
242             gki_cb.com.OSTicksTilStop = 0;
243         }
244 #endif
245     }
246     /* Add the time since the last task timer update.
247     ** Note that this works when no timers are active since
248     ** both OSNumOrigTicks and OSTicksTilExp are 0.
249     */
250     if (GKI_MAX_INT32 - (gki_cb.com.OSNumOrigTicks - gki_cb.com.OSTicksTilExp) > ticks)
251     {
252         ticks += gki_cb.com.OSNumOrigTicks - gki_cb.com.OSTicksTilExp;
253     }
254     else
255         ticks = GKI_MAX_INT32;
256
257     switch (tnum)
258     {
259 #if (GKI_NUM_TIMERS > 0)
260         case TIMER_0:
261             gki_cb.com.OSTaskTmr0R[task_id] = reload;
262             gki_cb.com.OSTaskTmr0 [task_id] = ticks;
263             break;
264 #endif
265
266 #if (GKI_NUM_TIMERS > 1)
267         case TIMER_1:
268             gki_cb.com.OSTaskTmr1R[task_id] = reload;
269             gki_cb.com.OSTaskTmr1 [task_id] = ticks;
270             break;
271 #endif
272
273 #if (GKI_NUM_TIMERS > 2)
274         case TIMER_2:
275             gki_cb.com.OSTaskTmr2R[task_id] = reload;
276             gki_cb.com.OSTaskTmr2 [task_id] = ticks;
277             break;
278 #endif
279
280 #if (GKI_NUM_TIMERS > 3)
281         case TIMER_3:
282             gki_cb.com.OSTaskTmr3R[task_id] = reload;
283             gki_cb.com.OSTaskTmr3 [task_id] = ticks;
284             break;
285 #endif
286         default:
287             bad_timer = TRUE;       /* Timer number is bad, so do not use */
288     }
289
290     /* Update the expiration timeout if a legitimate timer */
291     if (!bad_timer)
292     {
293         /* Only update the timeout value if it is less than any other newly started timers */
294         gki_adjust_timer_count (orig_ticks);
295     }
296
297     GKI_enable();
298
299 }
300
301 /*******************************************************************************
302 **
303 ** Function         GKI_stop_timer
304 **
305 ** Description      An application can call this function to stop one of
306 **                  it's four general purpose timers. There is no harm in
307 **                  stopping a timer that is already stopped.
308 **
309 ** Parameters       tnum            - (input) timer number to be started (TIMER_0,
310 **                                              TIMER_1, TIMER_2, or TIMER_3)
311 ** Returns          void
312 **
313 *******************************************************************************/
314 void GKI_stop_timer (UINT8 tnum)
315 {
316     UINT8  task_id = GKI_get_taskid();
317
318     switch (tnum)
319     {
320 #if (GKI_NUM_TIMERS > 0)
321         case TIMER_0:
322             gki_cb.com.OSTaskTmr0R[task_id] = 0;
323             gki_cb.com.OSTaskTmr0 [task_id] = 0;
324             break;
325 #endif
326
327 #if (GKI_NUM_TIMERS > 1)
328         case TIMER_1:
329             gki_cb.com.OSTaskTmr1R[task_id] = 0;
330             gki_cb.com.OSTaskTmr1 [task_id] = 0;
331             break;
332 #endif
333
334 #if (GKI_NUM_TIMERS > 2)
335         case TIMER_2:
336             gki_cb.com.OSTaskTmr2R[task_id] = 0;
337             gki_cb.com.OSTaskTmr2 [task_id] = 0;
338             break;
339 #endif
340
341 #if (GKI_NUM_TIMERS > 3)
342         case TIMER_3:
343             gki_cb.com.OSTaskTmr3R[task_id] = 0;
344             gki_cb.com.OSTaskTmr3 [task_id] = 0;
345             break;
346 #endif
347     }
348
349     GKI_disable();
350
351     if (gki_timers_is_timer_running() == FALSE)
352     {
353         if (gki_cb.com.p_tick_cb)
354         {
355 #if (defined(GKI_DELAY_STOP_SYS_TICK) && (GKI_DELAY_STOP_SYS_TICK > 0))
356             /* if inactivity delay timer is not running */
357             if ((gki_cb.com.system_tick_running)&&(gki_cb.com.OSTicksTilStop == 0))
358             {
359                 /* set inactivity delay timer */
360                 /* when timer expires, system tick will be stopped */
361                 gki_cb.com.OSTicksTilStop = GKI_DELAY_STOP_SYS_TICK;
362             }
363 #else
364             gki_cb.com.system_tick_running = FALSE;
365             gki_cb.com.p_tick_cb(FALSE); /* stop system tick */
366 #endif
367         }
368     }
369
370     GKI_enable();
371
372
373 }
374
375
376 /*******************************************************************************
377 **
378 ** Function         GKI_timer_update
379 **
380 ** Description      This function is called by an OS to drive the GKI's timers.
381 **                  It is typically called at every system tick to
382 **                  update the timers for all tasks, and check for timeouts.
383 **
384 **                  Note: It has been designed to also allow for variable tick updates
385 **                      so that systems with strict power savings requirements can
386 **                      have the update occur at variable intervals.
387 **
388 ** Parameters:      ticks_since_last_update - (input) This is the number of TICKS that have
389 **                          occurred since the last time GKI_timer_update was called.
390 **
391 ** Returns          void
392 **
393 *******************************************************************************/
394 void GKI_timer_update (INT32 ticks_since_last_update)
395 {
396     UINT8   task_id;
397     long    next_expiration;        /* Holds the next soonest expiration time after this update */
398
399     /* Increment the number of ticks used for time stamps */
400     gki_cb.com.OSTicks += ticks_since_last_update;
401
402     /* If any timers are running in any tasks, decrement the remaining time til
403      * the timer updates need to take place (next expiration occurs)
404      */
405     gki_cb.com.OSTicksTilExp -= ticks_since_last_update;
406
407     /* Don't allow timer interrupt nesting */
408     if (gki_cb.com.timer_nesting)
409         return;
410
411     gki_cb.com.timer_nesting = 1;
412
413 #if (defined(GKI_DELAY_STOP_SYS_TICK) && (GKI_DELAY_STOP_SYS_TICK > 0))
414     /* if inactivity delay timer is set and expired */
415     if (gki_cb.com.OSTicksTilStop)
416     {
417         if( gki_cb.com.OSTicksTilStop <= (UINT32)ticks_since_last_update )
418         {
419             if(gki_cb.com.p_tick_cb)
420             {
421                 gki_cb.com.system_tick_running = FALSE;
422                 (gki_cb.com.p_tick_cb) (FALSE); /* stop system tick */
423             }
424             gki_cb.com.OSTicksTilStop = 0;      /* clear inactivity delay timer */
425             gki_cb.com.timer_nesting = 0;
426             return;
427         }
428         else
429             gki_cb.com.OSTicksTilStop -= ticks_since_last_update;
430     }
431 #endif
432
433     /* No need to update the ticks if no timeout has occurred */
434     if (gki_cb.com.OSTicksTilExp > 0)
435     {
436         // When using alarms from AlarmService we should
437         // always have work to be done here.
438         GKI_ERROR("No work to be done when expected work\n");
439         gki_cb.com.timer_nesting = 0;
440         return;
441     }
442
443     next_expiration = GKI_NO_NEW_TMRS_STARTED;
444
445     /* If here then gki_cb.com.OSTicksTilExp <= 0. If negative, then increase gki_cb.com.OSNumOrigTicks
446        to account for the difference so timer updates below are decremented by the full number
447        of ticks. gki_cb.com.OSNumOrigTicks is reset at the bottom of this function so changing this
448        value only affects the timer updates below
449      */
450     gki_cb.com.OSNumOrigTicks -= gki_cb.com.OSTicksTilExp;
451
452 #if GKI_TIMER_LIST_NOPREEMPT == TRUE
453     /* Protect this section because if a GKI_timer_stop happens between:
454      *   - gki_cb.com.OSTaskTmr0[task_id] -= gki_cb.com.OSNumOrigTicks;
455      *   - gki_cb.com.OSTaskTmr0[task_id] = gki_cb.com.OSTaskTmr0R[task_id];
456      * then the timer may appear stopped while it is about to be reloaded.
457      * Note: Not needed if this function cannot be preempted (typical).
458      */
459     GKI_disable();
460 #endif
461
462     /* Check for OS Task Timers */
463     for (task_id = 0; task_id < GKI_MAX_TASKS; task_id++)
464     {
465         if (gki_cb.com.OSWaitTmr[task_id] > 0) /* If timer is running */
466         {
467             gki_cb.com.OSWaitTmr[task_id] -= gki_cb.com.OSNumOrigTicks;
468             if (gki_cb.com.OSWaitTmr[task_id] <= 0)
469             {
470                 /* Timer Expired */
471                 gki_cb.com.OSRdyTbl[task_id] = TASK_READY;
472             }
473         }
474
475 #if (GKI_NUM_TIMERS > 0)
476          /* If any timer is running, decrement */
477         if (gki_cb.com.OSTaskTmr0[task_id] > 0)
478         {
479             gki_cb.com.OSTaskTmr0[task_id] -= gki_cb.com.OSNumOrigTicks;
480
481             if (gki_cb.com.OSTaskTmr0[task_id] <= 0)
482             {
483                 /* Reload timer and set Timer 0 Expired event mask */
484                 gki_cb.com.OSTaskTmr0[task_id] = gki_cb.com.OSTaskTmr0R[task_id];
485
486 #if (defined(GKI_TIMER_UPDATES_FROM_ISR) &&  GKI_TIMER_UPDATES_FROM_ISR == TRUE)
487                 GKI_isend_event (task_id, TIMER_0_EVT_MASK);
488 #else
489                 GKI_send_event (task_id, TIMER_0_EVT_MASK);
490 #endif
491             }
492         }
493
494         /* Check to see if this timer is the next one to expire */
495         if (gki_cb.com.OSTaskTmr0[task_id] > 0 && gki_cb.com.OSTaskTmr0[task_id] < next_expiration)
496             next_expiration = gki_cb.com.OSTaskTmr0[task_id];
497 #endif
498
499 #if (GKI_NUM_TIMERS > 1)
500          /* If any timer is running, decrement */
501         if (gki_cb.com.OSTaskTmr1[task_id] > 0)
502         {
503             gki_cb.com.OSTaskTmr1[task_id] -= gki_cb.com.OSNumOrigTicks;
504
505             if (gki_cb.com.OSTaskTmr1[task_id] <= 0)
506             {
507                 /* Reload timer and set Timer 1 Expired event mask */
508                 gki_cb.com.OSTaskTmr1[task_id] = gki_cb.com.OSTaskTmr1R[task_id];
509
510 #if (defined(GKI_TIMER_UPDATES_FROM_ISR) &&  GKI_TIMER_UPDATES_FROM_ISR == TRUE)
511                 GKI_isend_event (task_id, TIMER_1_EVT_MASK);
512 #else
513                 GKI_send_event (task_id, TIMER_1_EVT_MASK);
514 #endif
515             }
516         }
517
518         /* Check to see if this timer is the next one to expire */
519         if (gki_cb.com.OSTaskTmr1[task_id] > 0 && gki_cb.com.OSTaskTmr1[task_id] < next_expiration)
520             next_expiration = gki_cb.com.OSTaskTmr1[task_id];
521 #endif
522
523 #if (GKI_NUM_TIMERS > 2)
524          /* If any timer is running, decrement */
525         if (gki_cb.com.OSTaskTmr2[task_id] > 0)
526         {
527             gki_cb.com.OSTaskTmr2[task_id] -= gki_cb.com.OSNumOrigTicks;
528
529             if (gki_cb.com.OSTaskTmr2[task_id] <= 0)
530             {
531                 /* Reload timer and set Timer 2 Expired event mask */
532                 gki_cb.com.OSTaskTmr2[task_id] = gki_cb.com.OSTaskTmr2R[task_id];
533
534 #if (defined(GKI_TIMER_UPDATES_FROM_ISR) &&  GKI_TIMER_UPDATES_FROM_ISR == TRUE)
535                 GKI_isend_event (task_id, TIMER_2_EVT_MASK);
536 #else
537                 GKI_send_event (task_id, TIMER_2_EVT_MASK);
538 #endif
539             }
540         }
541
542         /* Check to see if this timer is the next one to expire */
543         if (gki_cb.com.OSTaskTmr2[task_id] > 0 && gki_cb.com.OSTaskTmr2[task_id] < next_expiration)
544             next_expiration = gki_cb.com.OSTaskTmr2[task_id];
545 #endif
546
547 #if (GKI_NUM_TIMERS > 3)
548          /* If any timer is running, decrement */
549         if (gki_cb.com.OSTaskTmr3[task_id] > 0)
550         {
551             gki_cb.com.OSTaskTmr3[task_id] -= gki_cb.com.OSNumOrigTicks;
552
553             if (gki_cb.com.OSTaskTmr3[task_id] <= 0)
554             {
555                 /* Reload timer and set Timer 3 Expired event mask */
556                 gki_cb.com.OSTaskTmr3[task_id] = gki_cb.com.OSTaskTmr3R[task_id];
557
558 #if (defined(GKI_TIMER_UPDATES_FROM_ISR) &&  GKI_TIMER_UPDATES_FROM_ISR == TRUE)
559                 GKI_isend_event (task_id, TIMER_3_EVT_MASK);
560 #else
561                 GKI_send_event (task_id, TIMER_3_EVT_MASK);
562 #endif
563             }
564         }
565
566         /* Check to see if this timer is the next one to expire */
567         if (gki_cb.com.OSTaskTmr3[task_id] > 0 && gki_cb.com.OSTaskTmr3[task_id] < next_expiration)
568             next_expiration = gki_cb.com.OSTaskTmr3[task_id];
569 #endif
570
571     }
572     /* Set the next timer experation value if there is one to start */
573     if (next_expiration < GKI_NO_NEW_TMRS_STARTED)
574     {
575         gki_cb.com.OSTicksTilExp = gki_cb.com.OSNumOrigTicks = next_expiration;
576     }
577     else
578     {
579         gki_cb.com.OSTicksTilExp = gki_cb.com.OSNumOrigTicks = 0;
580     }
581
582     // Set alarm service for next alarm.
583     alarm_service_reschedule();
584
585 #if GKI_TIMER_LIST_NOPREEMPT == TRUE
586     /* End the critical section */
587     GKI_enable();
588 #endif
589
590 //    GKI_ERROR("Timer expired - next expiration ticks:%ld\n", next_expiration);
591
592     gki_cb.com.timer_nesting = 0;
593
594     return;
595 }
596
597
598 /*******************************************************************************
599 **
600 ** Function         GKI_timer_queue_empty
601 **
602 ** Description      This function is called by applications to see whether the timer
603 **                  queue is empty
604 **
605 ** Parameters
606 **
607 ** Returns          BOOLEAN
608 **
609 *******************************************************************************/
610 BOOLEAN GKI_timer_queue_empty (void)
611 {
612     UINT8 tt;
613
614     for (tt = 0; tt < GKI_MAX_TIMER_QUEUES; tt++)
615     {
616         if (gki_cb.com.timer_queues[tt])
617             return FALSE;
618     }
619
620     return TRUE;
621 }
622
623 /*******************************************************************************
624 **
625 ** Function         GKI_timer_queue_register_callback
626 **
627 ** Description      This function is called by applications to register system tick
628 **                  start/stop callback for time queues
629 **
630 **
631 ** Parameters       p_callback - (input) pointer to the system tick callback
632 **
633 ** Returns          BOOLEAN
634 **
635 *******************************************************************************/
636 void GKI_timer_queue_register_callback (SYSTEM_TICK_CBACK *p_callback)
637 {
638     gki_cb.com.p_tick_cb = p_callback;
639
640     return;
641 }
642
643 /*******************************************************************************
644 **
645 ** Function         GKI_init_timer_list
646 **
647 ** Description      This function is called by applications when they
648 **                  want to initialize a timer list.
649 **
650 ** Parameters       p_timer_listq   - (input) pointer to the timer list queue object
651 **
652 ** Returns          void
653 **
654 *******************************************************************************/
655 void GKI_init_timer_list (TIMER_LIST_Q *p_timer_listq)
656 {
657     p_timer_listq->p_first    = NULL;
658     p_timer_listq->p_last     = NULL;
659     p_timer_listq->last_ticks = 0;
660
661     return;
662 }
663
664 /*******************************************************************************
665 **
666 ** Function         GKI_init_timer_list_entry
667 **
668 ** Description      This function is called by the applications when they
669 **                  want to initialize a timer list entry. This must be
670 **                  done prior to first use of the entry.
671 **
672 ** Parameters       p_tle           - (input) pointer to a timer list queue entry
673 **
674 ** Returns          void
675 **
676 *******************************************************************************/
677 void GKI_init_timer_list_entry (TIMER_LIST_ENT  *p_tle)
678 {
679     p_tle->p_next  = NULL;
680     p_tle->p_prev  = NULL;
681     p_tle->ticks   = GKI_UNUSED_LIST_ENTRY;
682     p_tle->in_use  = FALSE;
683 }
684
685
686 /*******************************************************************************
687 **
688 ** Function         GKI_update_timer_list
689 **
690 ** Description      This function is called by the applications when they
691 **                  want to update a timer list. This should be at every
692 **                  timer list unit tick, e.g. once per sec, once per minute etc.
693 **
694 ** Parameters       p_timer_listq   - (input) pointer to the timer list queue object
695 **                  num_units_since_last_update - (input) number of units since the last update
696 **                                  (allows for variable unit update)
697 **
698 **      NOTE: The following timer list update routines should not be used for exact time
699 **            critical purposes.  The timer tasks should be used when exact timing is needed.
700 **
701 ** Returns          the number of timers that have expired
702 **
703 *******************************************************************************/
704 UINT16 GKI_update_timer_list (TIMER_LIST_Q *p_timer_listq, INT32 num_units_since_last_update)
705 {
706     TIMER_LIST_ENT  *p_tle;
707     UINT16           num_time_out = 0;
708     INT32            rem_ticks;
709     INT32            temp_ticks;
710
711     p_tle = p_timer_listq->p_first;
712
713     /* First, get the guys who have previously timed out */
714     /* Note that the tick value of the timers should always be '0' */
715     while ((p_tle) && (p_tle->ticks <= 0))
716     {
717         num_time_out++;
718         p_tle = p_tle->p_next;
719     }
720
721     /* Timer entriy tick values are relative to the preceeding entry */
722     rem_ticks = num_units_since_last_update;
723
724     /* Now, adjust remaining timer entries */
725     while ((p_tle != NULL) && (rem_ticks > 0))
726     {
727         temp_ticks = p_tle->ticks;
728         p_tle->ticks -= rem_ticks;
729
730         /* See if this timer has just timed out */
731         if (p_tle->ticks <= 0)
732         {
733             /* We set the number of ticks to '0' so that the legacy code
734              * that assumes a '0' or nonzero value will still work as coded. */
735             p_tle->ticks = 0;
736
737             num_time_out++;
738         }
739
740         rem_ticks -= temp_ticks;  /* Decrement the remaining ticks to process */
741         p_tle = p_tle->p_next;
742     }
743
744     if (p_timer_listq->last_ticks > 0)
745     {
746         p_timer_listq->last_ticks -= num_units_since_last_update;
747
748         /* If the last timer has expired set last_ticks to 0 so that other list update
749         * functions will calculate correctly
750         */
751         if (p_timer_listq->last_ticks < 0)
752             p_timer_listq->last_ticks = 0;
753     }
754
755     return (num_time_out);
756 }
757
758 /*******************************************************************************
759 **
760 ** Function         GKI_get_remaining_ticks
761 **
762 ** Description      This function is called by an application to get remaining
763 **                  ticks to expire
764 **
765 ** Parameters       p_timer_listq   - (input) pointer to the timer list queue object
766 **                  p_target_tle    - (input) pointer to a timer list queue entry
767 **
768 ** Returns          0 if timer is not used or timer is not in the list
769 **                  remaining ticks if success
770 **
771 *******************************************************************************/
772 UINT32 GKI_get_remaining_ticks (TIMER_LIST_Q *p_timer_listq, TIMER_LIST_ENT  *p_target_tle)
773 {
774     TIMER_LIST_ENT  *p_tle;
775     UINT32           rem_ticks = 0;
776
777     if (p_target_tle->in_use)
778     {
779         p_tle = p_timer_listq->p_first;
780
781         /* adding up all of ticks in previous entries */
782         while ((p_tle)&&(p_tle != p_target_tle))
783         {
784             rem_ticks += p_tle->ticks;
785             p_tle = p_tle->p_next;
786         }
787
788         /* if found target entry */
789         if (p_tle == p_target_tle)
790         {
791             rem_ticks += p_tle->ticks;
792         }
793         else
794         {
795             BT_ERROR_TRACE_0(TRACE_LAYER_GKI, "GKI_get_remaining_ticks: No timer entry in the list");
796             return(0);
797         }
798     }
799     else
800     {
801         BT_ERROR_TRACE_0(TRACE_LAYER_GKI, "GKI_get_remaining_ticks: timer entry is not active");
802     }
803
804     return (rem_ticks);
805 }
806
807 /*******************************************************************************
808 **
809 ** Function         GKI_add_to_timer_list
810 **
811 ** Description      This function is called by an application to add a timer
812 **                  entry to a timer list.
813 **
814 **                  Note: A timer value of '0' will effectively insert an already
815 **                      expired event.  Negative tick values will be ignored.
816 **
817 ** Parameters       p_timer_listq   - (input) pointer to the timer list queue object
818 **                  p_tle           - (input) pointer to a timer list queue entry
819 **
820 ** Returns          void
821 **
822 *******************************************************************************/
823 void GKI_add_to_timer_list (TIMER_LIST_Q *p_timer_listq, TIMER_LIST_ENT  *p_tle)
824 {
825     UINT32           nr_ticks_total;
826     UINT8 tt;
827     TIMER_LIST_ENT  *p_temp;
828
829     /* Only process valid tick values */
830     if (p_tle->ticks >= 0)
831     {
832         /* If this entry is the last in the list */
833         if (p_tle->ticks >= p_timer_listq->last_ticks)
834         {
835             /* If this entry is the only entry in the list */
836             if (p_timer_listq->p_first == NULL)
837                 p_timer_listq->p_first = p_tle;
838             else
839             {
840                 /* Insert the entry onto the end of the list */
841                 if (p_timer_listq->p_last != NULL)
842                     p_timer_listq->p_last->p_next = p_tle;
843
844                 p_tle->p_prev = p_timer_listq->p_last;
845             }
846
847             p_tle->p_next = NULL;
848             p_timer_listq->p_last = p_tle;
849             nr_ticks_total = p_tle->ticks;
850             p_tle->ticks -= p_timer_listq->last_ticks;
851
852             p_timer_listq->last_ticks = nr_ticks_total;
853         }
854         else    /* This entry needs to be inserted before the last entry */
855         {
856             p_temp = p_timer_listq->p_first;
857
858             if (p_temp == NULL)
859             {
860                 /* list is corrupted, exit to avoid crash */
861                 GKI_TRACE_ERROR_0("GKI_add_to_timer_list : Timerlist Q is empty");
862                 GKI_exception(GKI_ERROR_TIMER_LIST_CORRUPTED, "*** "
863                         "GKI_add_to_timer_list(): timer list corrupted! ***");
864                 return;
865             }
866             /* Find the entry that the new one needs to be inserted in front of
867              * as last_ticks is the expiry value of p_last, it should be inserted
868              * BEFORE p_last. otherwise list is probably corrupted! */
869             while (p_tle->ticks > p_temp->ticks)
870             {
871                 /* Update the tick value if looking at an unexpired entry */
872                 if (p_temp->ticks > 0)
873                     p_tle->ticks -= p_temp->ticks;
874
875                 p_temp = p_temp->p_next;
876             }
877
878             /* The new entry is the first in the list */
879             if (p_temp == p_timer_listq->p_first)
880             {
881                 p_tle->p_next = p_timer_listq->p_first;
882                 p_timer_listq->p_first->p_prev = p_tle;
883                 p_timer_listq->p_first = p_tle;
884             }
885             else
886             {
887                 p_temp->p_prev->p_next = p_tle;
888                 p_tle->p_prev = p_temp->p_prev;
889                 p_temp->p_prev = p_tle;
890                 p_tle->p_next = p_temp;
891             }
892             p_temp->ticks -= p_tle->ticks;
893         }
894
895         p_tle->in_use = TRUE;
896
897         /* if we already add this timer queue to the array */
898         for (tt = 0; tt < GKI_MAX_TIMER_QUEUES; tt++)
899         {
900              if (gki_cb.com.timer_queues[tt] == p_timer_listq)
901                  return;
902         }
903         /* add this timer queue to the array */
904         for (tt = 0; tt < GKI_MAX_TIMER_QUEUES; tt++)
905         {
906              if (gki_cb.com.timer_queues[tt] == NULL)
907                  break;
908         }
909         if (tt < GKI_MAX_TIMER_QUEUES)
910         {
911             gki_cb.com.timer_queues[tt] = p_timer_listq;
912         }
913     }
914
915     return;
916 }
917
918
919 /*******************************************************************************
920 **
921 ** Function         GKI_remove_from_timer_list
922 **
923 ** Description      This function is called by an application to remove a timer
924 **                  entry from a timer list.
925 **
926 ** Parameters       p_timer_listq   - (input) pointer to the timer list queue object
927 **                  p_tle           - (input) pointer to a timer list queue entry
928 **
929 ** Returns          void
930 **
931 *******************************************************************************/
932 void GKI_remove_from_timer_list (TIMER_LIST_Q *p_timer_listq, TIMER_LIST_ENT  *p_tle)
933 {
934     UINT8 tt;
935
936     /* Verify that the entry is valid */
937     if (p_tle == NULL || p_tle->in_use == FALSE || p_timer_listq->p_first == NULL)
938     {
939         return;
940     }
941
942     /* Add the ticks remaining in this timer (if any) to the next guy in the list.
943     ** Note: Expired timers have a tick value of '0'.
944     */
945     if (p_tle->p_next != NULL)
946     {
947         p_tle->p_next->ticks += p_tle->ticks;
948     }
949     else
950     {
951         p_timer_listq->last_ticks -= p_tle->ticks;
952     }
953
954     /* Unlink timer from the list.
955     */
956     if (p_timer_listq->p_first == p_tle)
957     {
958         p_timer_listq->p_first = p_tle->p_next;
959
960         if (p_timer_listq->p_first != NULL)
961             p_timer_listq->p_first->p_prev = NULL;
962
963         if (p_timer_listq->p_last == p_tle)
964             p_timer_listq->p_last = NULL;
965     }
966     else
967     {
968         if (p_timer_listq->p_last == p_tle)
969         {
970             p_timer_listq->p_last = p_tle->p_prev;
971
972             if (p_timer_listq->p_last != NULL)
973                 p_timer_listq->p_last->p_next = NULL;
974         }
975         else
976         {
977             if (p_tle->p_next != NULL && p_tle->p_next->p_prev == p_tle)
978                 p_tle->p_next->p_prev = p_tle->p_prev;
979             else
980             {
981                 /* Error case - chain messed up ?? */
982                 return;
983             }
984
985             if (p_tle->p_prev != NULL && p_tle->p_prev->p_next == p_tle)
986                 p_tle->p_prev->p_next = p_tle->p_next;
987             else
988             {
989                 /* Error case - chain messed up ?? */
990                 return;
991             }
992         }
993     }
994
995     p_tle->p_next = p_tle->p_prev = NULL;
996     p_tle->ticks = GKI_UNUSED_LIST_ENTRY;
997     p_tle->in_use = FALSE;
998
999     /* if timer queue is empty */
1000     if (p_timer_listq->p_first == NULL && p_timer_listq->p_last == NULL)
1001     {
1002         for (tt = 0; tt < GKI_MAX_TIMER_QUEUES; tt++)
1003         {
1004             if (gki_cb.com.timer_queues[tt] == p_timer_listq)
1005             {
1006                 gki_cb.com.timer_queues[tt] = NULL;
1007                 break;
1008             }
1009         }
1010     }
1011
1012     return;
1013 }
1014
1015
1016 /*******************************************************************************
1017 **
1018 ** Function         gki_adjust_timer_count
1019 **
1020 ** Description      This function is called whenever a new timer or GKI_wait occurs
1021 **                  to adjust (if necessary) the current time til the first expiration.
1022 **                  This only needs to make an adjustment if the new timer (in ticks) is
1023 **                  less than the number of ticks remaining on the current timer.
1024 **
1025 ** Parameters:      ticks - (input) number of system ticks of the new timer entry
1026 **
1027 **                  NOTE:  This routine MUST be called while interrupts are disabled to
1028 **                          avoid updates while adjusting the timer variables.
1029 **
1030 ** Returns          void
1031 **
1032 *******************************************************************************/
1033 void gki_adjust_timer_count (INT32 ticks)
1034 {
1035     if (ticks > 0)
1036     {
1037         /* See if the new timer expires before the current first expiration */
1038         if (gki_cb.com.OSNumOrigTicks == 0 || (ticks < gki_cb.com.OSTicksTilExp && gki_cb.com.OSTicksTilExp > 0))
1039         {
1040             gki_cb.com.OSNumOrigTicks = (gki_cb.com.OSNumOrigTicks - gki_cb.com.OSTicksTilExp) + ticks;
1041             gki_cb.com.OSTicksTilExp = ticks;
1042             alarm_service_reschedule();
1043         }
1044     }
1045
1046     return;
1047 }