1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
|
/*
** Id: //Department/DaVinci/BRANCHES/MT6620_WIFI_DRIVER_V2_3/mgmt/cnm_timer.c#1
*/
/*! \file "cnm_timer.c"
\brief
*/
/*
** Log: cnm_timer.c
**
** 02 21 2013 cm.chang
** [BORA00002149] [MT6630 Wi-Fi] Initial software development
** Refine code to protect zero timeout interval like FW
**
** 09 17 2012 cm.chang
** [BORA00002149] [MT6630 Wi-Fi] Initial software development
** Duplicate source from MT6620 v2.3 driver branch
** (Davinci label: MT6620_WIFI_Driver_V2_3_120913_1942_As_MT6630_Base)
*/
/*******************************************************************************
* C O M P I L E R F L A G S
********************************************************************************
*/
/*******************************************************************************
* E X T E R N A L R E F E R E N C E S
********************************************************************************
*/
#include "precomp.h"
/*******************************************************************************
* C O N S T A N T S
********************************************************************************
*/
/*******************************************************************************
* D A T A T Y P E S
********************************************************************************
*/
/*******************************************************************************
* P U B L I C D A T A
********************************************************************************
*/
/*******************************************************************************
* P R I V A T E D A T A
********************************************************************************
*/
/*******************************************************************************
* M A C R O S
********************************************************************************
*/
/*******************************************************************************
* F U N C T I O N D E C L A R A T I O N S
********************************************************************************
*/
/*******************************************************************************
* F U N C T I O N S
********************************************************************************
*/
/*----------------------------------------------------------------------------*/
/*!
* \brief This routine is called to set the time to do the time out check.
*
* \param[in] rTimeout Time out interval from current time.
*
* \retval TRUE Success.
*
*/
/*----------------------------------------------------------------------------*/
static BOOLEAN cnmTimerSetTimer(IN P_ADAPTER_T prAdapter, IN OS_SYSTIME rTimeout)
{
P_ROOT_TIMER prRootTimer;
BOOLEAN fgNeedWakeLock;
ASSERT(prAdapter);
prRootTimer = &prAdapter->rRootTimer;
kalSetTimer(prAdapter->prGlueInfo, rTimeout);
if (rTimeout <= SEC_TO_SYSTIME(WAKE_LOCK_MAX_TIME)) {
fgNeedWakeLock = TRUE;
if (!prRootTimer->fgWakeLocked) {
KAL_WAKE_LOCK(prAdapter, &prRootTimer->rWakeLock);
prRootTimer->fgWakeLocked = TRUE;
}
} else {
fgNeedWakeLock = FALSE;
}
return fgNeedWakeLock;
}
/*----------------------------------------------------------------------------*/
/*!
* \brief This routines is called to initialize a root timer.
*
* \param[in] prAdapter
*
* \return (none)
*/
/*----------------------------------------------------------------------------*/
VOID cnmTimerInitialize(IN P_ADAPTER_T prAdapter)
{
P_ROOT_TIMER prRootTimer;
KAL_SPIN_LOCK_DECLARATION();
ASSERT(prAdapter);
prRootTimer = &prAdapter->rRootTimer;
/* Note: glue layer have configured timer */
KAL_ACQUIRE_SPIN_LOCK(prAdapter, SPIN_LOCK_TIMER);
LINK_INITIALIZE(&prRootTimer->rLinkHead);
KAL_RELEASE_SPIN_LOCK(prAdapter, SPIN_LOCK_TIMER);
KAL_WAKE_LOCK_INIT(prAdapter, &prRootTimer->rWakeLock, "WLAN Timer");
prRootTimer->fgWakeLocked = FALSE;
}
/*----------------------------------------------------------------------------*/
/*!
* \brief This routines is called to destroy a root timer.
* When WIFI is off, the token shall be returned back to system.
*
* \param[in]
*
* \return (none)
*/
/*----------------------------------------------------------------------------*/
VOID cnmTimerDestroy(IN P_ADAPTER_T prAdapter)
{
P_ROOT_TIMER prRootTimer;
KAL_SPIN_LOCK_DECLARATION();
ASSERT(prAdapter);
prRootTimer = &prAdapter->rRootTimer;
if (prRootTimer->fgWakeLocked) {
KAL_WAKE_UNLOCK(prAdapter, &prRootTimer->rWakeLock);
prRootTimer->fgWakeLocked = FALSE;
}
KAL_WAKE_LOCK_DESTROY(prAdapter, &prRootTimer->rWakeLock);
KAL_ACQUIRE_SPIN_LOCK(prAdapter, SPIN_LOCK_TIMER);
LINK_INITIALIZE(&prRootTimer->rLinkHead);
KAL_RELEASE_SPIN_LOCK(prAdapter, SPIN_LOCK_TIMER);
/* Note: glue layer will be responsible for timer destruction */
}
/*----------------------------------------------------------------------------*/
/*!
* \brief This routines is called to initialize a timer.
*
* \param[in] prTimer Pointer to a timer structure.
* \param[in] pfnFunc Pointer to the call back function.
* \param[in] u4Data Parameter for call back function.
*
* \return (none)
*/
/*----------------------------------------------------------------------------*/
VOID
cnmTimerInitTimer(IN P_ADAPTER_T prAdapter, IN P_TIMER_T prTimer, IN PFN_MGMT_TIMEOUT_FUNC pfFunc, IN ULONG ulDataPtr)
{
ASSERT(prAdapter);
ASSERT(prTimer);
#if DBG
/* Note: NULL function pointer is permitted for HEM POWER */
if (pfFunc == NULL)
DBGLOG(CNM, WARN, "Init timer with NULL callback function!\n");
#endif
#if DBG
ASSERT(prAdapter->rRootTimer.rLinkHead.prNext);
{
P_LINK_T prTimerList;
P_LINK_ENTRY_T prLinkEntry;
P_TIMER_T prPendingTimer;
prTimerList = &(prAdapter->rRootTimer.rLinkHead);
LINK_FOR_EACH(prLinkEntry, prTimerList) {
prPendingTimer = LINK_ENTRY(prLinkEntry, TIMER_T, rLinkEntry);
ASSERT(prPendingTimer);
ASSERT(prPendingTimer != prTimer);
}
}
#endif
LINK_ENTRY_INITIALIZE(&prTimer->rLinkEntry);
prTimer->pfMgmtTimeOutFunc = pfFunc;
prTimer->ulDataPtr = ulDataPtr;
}
/*----------------------------------------------------------------------------*/
/*!
* \brief This routines is called to stop a timer.
*
* \param[in] prTimer Pointer to a timer structure.
*
* \return (none)
*/
/*----------------------------------------------------------------------------*/
static VOID cnmTimerStopTimer_impl(IN P_ADAPTER_T prAdapter, IN P_TIMER_T prTimer, IN BOOLEAN fgAcquireSpinlock)
{
P_ROOT_TIMER prRootTimer;
KAL_SPIN_LOCK_DECLARATION();
ASSERT(prAdapter);
ASSERT(prTimer);
prRootTimer = &prAdapter->rRootTimer;
if (fgAcquireSpinlock)
KAL_ACQUIRE_SPIN_LOCK(prAdapter, SPIN_LOCK_TIMER);
if (timerPendingTimer(prTimer)) {
LINK_REMOVE_KNOWN_ENTRY(&prRootTimer->rLinkHead, &prTimer->rLinkEntry);
/* Reduce dummy timeout for power saving, especially HIF activity.
* If two or more timers exist and being removed timer is smallest,
* this dummy timeout will still happen, but it is OK.
*/
if (LINK_IS_EMPTY(&prRootTimer->rLinkHead)) {
kalCancelTimer(prAdapter->prGlueInfo);
if (fgAcquireSpinlock && prRootTimer->fgWakeLocked) {
KAL_WAKE_UNLOCK(prAdapter, &prRootTimer->rWakeLock);
prRootTimer->fgWakeLocked = FALSE;
}
}
}
if (fgAcquireSpinlock)
KAL_RELEASE_SPIN_LOCK(prAdapter, SPIN_LOCK_TIMER);
}
/*----------------------------------------------------------------------------*/
/*!
* \brief This routines is called to stop a timer.
*
* \param[in] prTimer Pointer to a timer structure.
*
* \return (none)
*/
/*----------------------------------------------------------------------------*/
VOID cnmTimerStopTimer(IN P_ADAPTER_T prAdapter, IN P_TIMER_T prTimer)
{
ASSERT(prAdapter);
ASSERT(prTimer);
cnmTimerStopTimer_impl(prAdapter, prTimer, TRUE);
}
/*----------------------------------------------------------------------------*/
/*!
* \brief This routines is called to start a timer with wake_lock.
*
* \param[in] prTimer Pointer to a timer structure.
* \param[in] u4TimeoutMs Timeout to issue the timer and call back function
* (unit: ms).
*
* \return (none)
*/
/*----------------------------------------------------------------------------*/
VOID cnmTimerStartTimer(IN P_ADAPTER_T prAdapter, IN P_TIMER_T prTimer, IN UINT_32 u4TimeoutMs)
{
P_ROOT_TIMER prRootTimer;
P_LINK_T prTimerList;
OS_SYSTIME rExpiredSysTime, rTimeoutSystime;
KAL_SPIN_LOCK_DECLARATION();
ASSERT(prAdapter);
ASSERT(prTimer);
KAL_ACQUIRE_SPIN_LOCK(prAdapter, SPIN_LOCK_TIMER);
prRootTimer = &prAdapter->rRootTimer;
prTimerList = &prRootTimer->rLinkHead;
/* If timeout interval is larger than 1 minute, the mod value is set
* to the timeout value first, then per minutue.
*/
if (u4TimeoutMs > MSEC_PER_MIN) {
ASSERT(u4TimeoutMs <= ((UINT_32) 0xFFFF * MSEC_PER_MIN));
prTimer->u2Minutes = (UINT_16) (u4TimeoutMs / MSEC_PER_MIN);
u4TimeoutMs -= (prTimer->u2Minutes * MSEC_PER_MIN);
if (u4TimeoutMs == 0) {
u4TimeoutMs = MSEC_PER_MIN;
prTimer->u2Minutes--;
}
} else {
prTimer->u2Minutes = 0;
}
/* The assertion check if MSEC_TO_SYSTIME() may be overflow. */
ASSERT(u4TimeoutMs < (((UINT_32) 0x80000000 - MSEC_PER_SEC) / KAL_HZ));
rTimeoutSystime = MSEC_TO_SYSTIME(u4TimeoutMs);
if (rTimeoutSystime == 0)
rTimeoutSystime = 1;
rExpiredSysTime = kalGetTimeTick() + rTimeoutSystime;
/* If no timer pending or the fast time interval is used. */
if (LINK_IS_EMPTY(prTimerList) || TIME_BEFORE(rExpiredSysTime, prRootTimer->rNextExpiredSysTime)) {
prRootTimer->rNextExpiredSysTime = rExpiredSysTime;
cnmTimerSetTimer(prAdapter, rTimeoutSystime);
}
/* Add this timer to checking list */
prTimer->rExpiredSysTime = rExpiredSysTime;
if (!timerPendingTimer(prTimer))
LINK_INSERT_TAIL(prTimerList, &prTimer->rLinkEntry);
KAL_RELEASE_SPIN_LOCK(prAdapter, SPIN_LOCK_TIMER);
}
/*----------------------------------------------------------------------------*/
/*!
* \brief This routines is called to check the timer list.
*
* \param[in]
*
* \return (none)
*/
/*----------------------------------------------------------------------------*/
VOID cnmTimerDoTimeOutCheck(IN P_ADAPTER_T prAdapter)
{
P_ROOT_TIMER prRootTimer;
P_LINK_T prTimerList;
P_LINK_ENTRY_T prLinkEntry;
P_TIMER_T prTimer;
OS_SYSTIME rCurSysTime;
PFN_MGMT_TIMEOUT_FUNC pfMgmtTimeOutFunc;
ULONG ulTimeoutDataPtr;
BOOLEAN fgNeedWakeLock;
KAL_SPIN_LOCK_DECLARATION();
ASSERT(prAdapter);
/* acquire spin lock */
KAL_ACQUIRE_SPIN_LOCK(prAdapter, SPIN_LOCK_TIMER);
prRootTimer = &prAdapter->rRootTimer;
prTimerList = &prRootTimer->rLinkHead;
rCurSysTime = kalGetTimeTick();
/* Set the permitted max timeout value for new one */
prRootTimer->rNextExpiredSysTime = rCurSysTime + MGMT_MAX_TIMEOUT_INTERVAL;
LINK_FOR_EACH(prLinkEntry, prTimerList) {
prTimer = LINK_ENTRY(prLinkEntry, TIMER_T, rLinkEntry);
ASSERT(prTimer);
/* Check if this entry is timeout. */
if (!TIME_BEFORE(rCurSysTime, prTimer->rExpiredSysTime)) {
cnmTimerStopTimer_impl(prAdapter, prTimer, FALSE);
pfMgmtTimeOutFunc = prTimer->pfMgmtTimeOutFunc;
ulTimeoutDataPtr = prTimer->ulDataPtr;
if (prTimer->u2Minutes > 0) {
prTimer->u2Minutes--;
prTimer->rExpiredSysTime = rCurSysTime + MSEC_TO_SYSTIME(MSEC_PER_MIN);
LINK_INSERT_TAIL(prTimerList, &prTimer->rLinkEntry);
} else if (pfMgmtTimeOutFunc) {
KAL_RELEASE_SPIN_LOCK(prAdapter, SPIN_LOCK_TIMER);
(pfMgmtTimeOutFunc) (prAdapter, ulTimeoutDataPtr);
KAL_ACQUIRE_SPIN_LOCK(prAdapter, SPIN_LOCK_TIMER);
}
/* Search entire list again because of nest del and add timers
* and current MGMT_TIMER could be volatile after stopped
*/
prLinkEntry = (P_LINK_ENTRY_T) prTimerList;
prRootTimer->rNextExpiredSysTime = rCurSysTime + MGMT_MAX_TIMEOUT_INTERVAL;
} else if (TIME_BEFORE(prTimer->rExpiredSysTime, prRootTimer->rNextExpiredSysTime)) {
prRootTimer->rNextExpiredSysTime = prTimer->rExpiredSysTime;
}
} /* end of for loop */
/* Setup the prNext timeout event. It is possible the timer was already
* set in the above timeout callback function.
*/
fgNeedWakeLock = FALSE;
if (!LINK_IS_EMPTY(prTimerList)) {
ASSERT(TIME_AFTER(prRootTimer->rNextExpiredSysTime, rCurSysTime));
fgNeedWakeLock = cnmTimerSetTimer(prAdapter, (OS_SYSTIME)
((INT_32) prRootTimer->rNextExpiredSysTime - (INT_32) rCurSysTime));
}
if (prRootTimer->fgWakeLocked && !fgNeedWakeLock) {
KAL_WAKE_UNLOCK(prAdapter, &prRootTimer->rWakeLock);
prRootTimer->fgWakeLocked = FALSE;
}
/* release spin lock */
KAL_RELEASE_SPIN_LOCK(prAdapter, SPIN_LOCK_TIMER);
}
|