PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
timer.c
1/*
2# _____ ___ ____ ___ ____
3# ____| | ____| | | |____|
4# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5#-----------------------------------------------------------------------
6# Copyright ps2dev - http://www.ps2dev.org
7# Licenced under Academic Free License version 2.0
8# Review ps2sdk README & LICENSE files for further details.
9*/
10
11#include "usbdpriv.h"
12
13int addTimerCallback(UsbdTimerCbStruct_t *arg, TimerCallback func, void *cbArg, int delay)
14{
16
17 if ( arg->m_isActive )
18 return -1;
19 arg->m_isActive = 1;
20 if ( delay > 0 )
21 delay -= 1;
22 arg->m_callbackProc = func;
23 arg->m_callbackArg = cbArg;
24 for ( pos = memPool->m_timerListStart; pos && delay >= (int)pos->m_delayCount;
25 delay -= pos->m_delayCount, pos = pos->m_prev )
26 {
27 }
28 if ( pos )
29 {
30 arg->m_next = pos->m_next;
31 if ( pos->m_next )
32 pos->m_next->m_prev = arg;
33 else
34 memPool->m_timerListStart = arg;
35 arg->m_prev = pos;
36 pos->m_next = arg;
37 pos->m_delayCount -= delay;
38 }
39 else
40 {
41 arg->m_next = memPool->m_timerListEnd;
42 if ( memPool->m_timerListEnd )
43 memPool->m_timerListEnd->m_prev = arg;
44 else
45 memPool->m_timerListStart = arg;
46 memPool->m_timerListEnd = arg;
47 arg->m_prev = NULL;
48 }
49 arg->m_delayCount = delay;
50 memPool->m_ohciRegs->HcInterruptEnable = OHCI_INT_SF;
51 return 0;
52}
53
54int cancelTimerCallback(UsbdTimerCbStruct_t *arg)
55{
56 if ( !arg->m_isActive )
57 {
58 return -1;
59 }
60 if ( arg->m_prev )
61 arg->m_prev->m_next = arg->m_next;
62 else
63 memPool->m_timerListEnd = arg->m_next;
64 if ( arg->m_next )
65 arg->m_next->m_prev = arg->m_prev;
66 else
67 memPool->m_timerListStart = arg->m_prev;
68 arg->m_isActive = 0;
69 arg->m_next = NULL;
70 arg->m_prev = NULL;
71 return 0;
72}
73
74void handleTimerList(void)
75{
77
78 timer = memPool->m_timerListStart;
79 if ( timer )
80 {
81 if ( timer->m_delayCount > 0 )
82 timer->m_delayCount -= 1;
83 while ( 1 )
84 {
85 timer = memPool->m_timerListStart;
86 if ( !timer || (int)timer->m_delayCount > 0 )
87 break;
88 dbg_printf("timer expired\n");
89 memPool->m_timerListStart = timer->m_prev;
90 if ( timer->m_prev )
91 timer->m_prev->m_next = NULL;
92 else
93 memPool->m_timerListEnd = NULL;
94 timer->m_next = NULL;
95 timer->m_prev = NULL;
96 timer->m_isActive = 0;
97 timer->m_callbackProc(timer->m_callbackArg);
98 }
99 }
100 // disable SOF interrupts if there are no timers left
101 if ( !memPool->m_timerListStart )
102 memPool->m_ohciRegs->HcInterruptDisable = OHCI_INT_SF;
103}