summaryrefslogtreecommitdiff
path: root/qdf/linux/src/qdf_mc_timer.c
blob: d9c9092ab676903e81ed3dd28f87cde657291d62 (plain)
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/*
 * Copyright (c) 2014-2017 The Linux Foundation. All rights reserved.
 *
 * Previously licensed under the ISC license by Qualcomm Atheros, Inc.
 *
 *
 * Permission to use, copy, modify, and/or distribute this software for
 * any purpose with or without fee is hereby granted, provided that the
 * above copyright notice and this permission notice appear in all
 * copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * This file was originally distributed by Qualcomm Atheros, Inc.
 * under proprietary terms before Copyright ownership was assigned
 * to the Linux Foundation.
 */

/**
 * DOC: qdf_mc_timer
 * QCA driver framework timer APIs serialized to MC thread
 */

/* Include Files */
#include <qdf_mc_timer.h>
#include <qdf_lock.h>
#include "qdf_lock.h"
#include "qdf_list.h"
#include "qdf_mem.h"
#include <qdf_module.h>
#include <linux/export.h>
#ifdef CONFIG_MCL
#include <cds_mc_timer.h>
#endif
/* Preprocessor definitions and constants */

#define LINUX_TIMER_COOKIE 0x12341234
#define LINUX_INVALID_TIMER_COOKIE 0xfeedface
#define TMR_INVALID_ID (0)

/* Type declarations */

/* Static Variable Definitions */
static unsigned int persistent_timer_count;
static qdf_mutex_t persistent_timer_count_lock;

/* Function declarations and documenation */

/**
 * qdf_try_allowing_sleep() - clean up timer states after it has been deactivated
 * @type: timer type
 *
 * Clean up timer states after it has been deactivated check and try to allow
 * sleep after a timer has been stopped or expired.
 *
 * Return: none
 */
void qdf_try_allowing_sleep(QDF_TIMER_TYPE type)
{
	if (QDF_TIMER_TYPE_WAKE_APPS == type) {

		persistent_timer_count--;
		if (0 == persistent_timer_count) {
			/* since the number of persistent timers has
			   decreased from 1 to 0, the timer should allow
			   sleep
			  */
		}
	}
}
qdf_export_symbol(qdf_try_allowing_sleep);

/**
 * qdf_mc_timer_get_current_state() - get the current state of the timer
 * @timer: Pointer to timer object
 *
 * Return:
 * QDF_TIMER_STATE - qdf timer state
 */
QDF_TIMER_STATE qdf_mc_timer_get_current_state(qdf_mc_timer_t *timer)
{
	if (NULL == timer) {
		QDF_ASSERT(0);
		return QDF_TIMER_STATE_UNUSED;
	}

	switch (timer->state) {
	case QDF_TIMER_STATE_STOPPED:
	case QDF_TIMER_STATE_STARTING:
	case QDF_TIMER_STATE_RUNNING:
	case QDF_TIMER_STATE_UNUSED:
		return timer->state;
	default:
		QDF_ASSERT(0);
		return QDF_TIMER_STATE_UNUSED;
	}
}
qdf_export_symbol(qdf_mc_timer_get_current_state);

/**
 * qdf_timer_module_init() - initializes a QDF timer module.
 *
 * This API initializes the QDF timer module. This needs to be called
 * exactly once prior to using any QDF timers.
 *
 * Return: none
 */
void qdf_timer_module_init(void)
{
	QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_INFO_HIGH,
		  "Initializing the QDF MC timer module");
	qdf_mutex_create(&persistent_timer_count_lock);
}
qdf_export_symbol(qdf_timer_module_init);

#ifdef TIMER_MANAGER

qdf_list_t qdf_timer_list;
qdf_spinlock_t qdf_timer_list_lock;

static void qdf_timer_clean(void);

/**
 * qdf_mc_timer_manager_init() - initialize QDF debug timer manager
 *
 * This API initializes QDF timer debug functionality.
 *
 * Return: none
 */
void qdf_mc_timer_manager_init(void)
{
	qdf_list_create(&qdf_timer_list, 1000);
	qdf_spinlock_create(&qdf_timer_list_lock);
	return;
}
qdf_export_symbol(qdf_mc_timer_manager_init);

/**
 * qdf_timer_clean() - clean up QDF timer debug functionality
 *
 * This API cleans up QDF timer debug functionality and prints which QDF timers
 * are leaked. This is called during driver unload.
 *
 * Return: none
 */
static void qdf_timer_clean(void)
{
	uint32_t list_size;
	qdf_list_node_t *node;
	QDF_STATUS qdf_status;

	qdf_mc_timer_node_t *timer_node;

	list_size = qdf_list_size(&qdf_timer_list);

	if (!list_size)
		return;
	QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_INFO_HIGH,
		  "%s: List is not Empty. list_size %d ",
		  __func__, (int)list_size);

	do {
		qdf_spin_lock_irqsave(&qdf_timer_list_lock);
		qdf_status = qdf_list_remove_front(&qdf_timer_list, &node);
		qdf_spin_unlock_irqrestore(&qdf_timer_list_lock);
		if (QDF_STATUS_SUCCESS == qdf_status) {
			timer_node = (qdf_mc_timer_node_t *) node;
			QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_FATAL,
			"timer Leak@ File %s, @Line %d",
				  timer_node->file_name,
				  (int)timer_node->line_num);
			qdf_mem_free(timer_node);
		}
	} while (qdf_status == QDF_STATUS_SUCCESS);
}
qdf_export_symbol(qdf_timer_clean);

/**
 * qdf_mc_timer_manager_exit() - exit QDF timer debug functionality
 *
 * This API exists QDF timer debug functionality
 *
 * Return: none
 */
void qdf_mc_timer_manager_exit(void)
{
	qdf_timer_clean();
	qdf_list_destroy(&qdf_timer_list);
}
qdf_export_symbol(qdf_mc_timer_manager_exit);
#endif

/**
 * qdf_mc_timer_init() - initialize a QDF timer
 * @timer: Pointer to timer object
 * @timer_type: Type of timer
 * @callback: Callback to be called after timer expiry
 * @ser_data: User data which will be passed to callback function
 *
 * This API initializes a QDF timer object.
 *
 * qdf_mc_timer_init() initializes a QDF timer object. A timer must be
 * initialized by calling qdf_mc_timer_initialize() before it may be used in
 * any other timer functions.
 *
 * Attempting to initialize timer that is already initialized results in
 * a failure. A destroyed timer object can be re-initialized with a call to
 * qdf_mc_timer_init(). The results of otherwise referencing the object
 * after it has been destroyed are undefined.
 *
 *  Calls to QDF timer functions to manipulate the timer such
 *  as qdf_mc_timer_set() will fail if the timer is not initialized or has
 *  been destroyed. Therefore, don't use the timer after it has been
 *  destroyed until it has been re-initialized.
 *
 *  All callback will be executed within the CDS main thread unless it is
 *  initialized from the Tx thread flow, in which case it will be executed
 *  within the tx thread flow.
 *
 * Return:
 * QDF_STATUS_SUCCESS: timer is initialized successfully
 * QDF failure status: timer initialization failed
 */
#ifdef TIMER_MANAGER
QDF_STATUS qdf_mc_timer_init_debug(qdf_mc_timer_t *timer,
				   QDF_TIMER_TYPE timer_type,
				   qdf_mc_timer_callback_t callback,
				   void *user_data, char *file_name,
				   uint32_t line_num)
{
	QDF_STATUS qdf_status;

	/* check for invalid pointer */
	if ((timer == NULL) || (callback == NULL)) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "%s: Null params being passed", __func__);
		QDF_ASSERT(0);
		return QDF_STATUS_E_FAULT;
	}

	timer->timer_node = qdf_mem_malloc(sizeof(qdf_mc_timer_node_t));

	if (timer->timer_node == NULL) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "%s: Not able to allocate memory for time_node",
			  __func__);
		QDF_ASSERT(0);
		return QDF_STATUS_E_NOMEM;
	}

	qdf_mem_set(timer->timer_node, sizeof(qdf_mc_timer_node_t), 0);

	timer->timer_node->file_name = file_name;
	timer->timer_node->line_num = line_num;
	timer->timer_node->qdf_timer = timer;

	qdf_spin_lock_irqsave(&qdf_timer_list_lock);
	qdf_status = qdf_list_insert_front(&qdf_timer_list,
					   &timer->timer_node->node);
	qdf_spin_unlock_irqrestore(&qdf_timer_list_lock);
	if (QDF_STATUS_SUCCESS != qdf_status) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "%s: Unable to insert node into List qdf_status %d",
			  __func__, qdf_status);
	}

	/* set the various members of the timer structure
	 * with arguments passed or with default values
	 */
	qdf_spinlock_create(&timer->platform_info.spinlock);
	if (QDF_TIMER_TYPE_SW == timer_type)
		init_timer_deferrable(&(timer->platform_info.timer));
	else
		init_timer(&(timer->platform_info.timer));
#ifdef CONFIG_MCL
	timer->platform_info.timer.function = cds_linux_timer_callback;
#else
	timer->platform_info.timer.function = NULL;
#endif
	timer->platform_info.timer.data = (unsigned long)timer;
	timer->callback = callback;
	timer->user_data = user_data;
	timer->type = timer_type;
	timer->platform_info.cookie = LINUX_TIMER_COOKIE;
	timer->platform_info.thread_id = 0;
	timer->state = QDF_TIMER_STATE_STOPPED;

	return QDF_STATUS_SUCCESS;
}
#else
QDF_STATUS qdf_mc_timer_init(qdf_mc_timer_t *timer, QDF_TIMER_TYPE timer_type,
			     qdf_mc_timer_callback_t callback,
			     void *user_data)
{
	/* check for invalid pointer */
	if ((timer == NULL) || (callback == NULL)) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "%s: Null params being passed", __func__);
		QDF_ASSERT(0);
		return QDF_STATUS_E_FAULT;
	}

	/* set the various members of the timer structure
	 * with arguments passed or with default values
	 */
	qdf_spinlock_create(&timer->platform_info.spinlock);
	if (QDF_TIMER_TYPE_SW == timer_type)
		init_timer_deferrable(&(timer->platform_info.timer));
	else
		init_timer(&(timer->platform_info.timer));
#ifdef CONFIG_MCL
	timer->platform_info.timer.function = cds_linux_timer_callback;
#else
	timer->platform_info.timer.function = NULL;
#endif
	timer->platform_info.timer.data = (unsigned long)timer;
	timer->callback = callback;
	timer->user_data = user_data;
	timer->type = timer_type;
	timer->platform_info.cookie = LINUX_TIMER_COOKIE;
	timer->platform_info.thread_id = 0;
	timer->state = QDF_TIMER_STATE_STOPPED;

	return QDF_STATUS_SUCCESS;
}
#endif

/**
 * qdf_mc_timer_destroy() - destroy QDF timer
 * @timer: Pointer to timer object
 *
 * qdf_mc_timer_destroy() function shall destroy the timer object.
 * After a successful return from \a qdf_mc_timer_destroy() the timer
 * object becomes, in effect, uninitialized.
 *
 * A destroyed timer object can be re-initialized by calling
 * qdf_mc_timer_init().  The results of otherwise referencing the object
 * after it has been destroyed are undefined.
 *
 * Calls to QDF timer functions to manipulate the timer, such
 * as qdf_mc_timer_set() will fail if the lock is destroyed.  Therefore,
 * don't use the timer after it has been destroyed until it has
 * been re-initialized.
 *
 * Return:
 * QDF_STATUS_SUCCESS - timer is initialized successfully
 * QDF failure status - timer initialization failed
 */
#ifdef TIMER_MANAGER
QDF_STATUS qdf_mc_timer_destroy(qdf_mc_timer_t *timer)
{
	QDF_STATUS v_status = QDF_STATUS_SUCCESS;

	/* check for invalid pointer */
	if (NULL == timer) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "%s: Null timer pointer being passed", __func__);
		QDF_ASSERT(0);
		return QDF_STATUS_E_FAULT;
	}

	/* Check if timer refers to an uninitialized object */
	if (LINUX_TIMER_COOKIE != timer->platform_info.cookie) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "%s: Cannot destroy uninitialized timer", __func__);
		QDF_ASSERT(0);
		return QDF_STATUS_E_INVAL;
	}

	qdf_spin_lock_irqsave(&qdf_timer_list_lock);
	v_status = qdf_list_remove_node(&qdf_timer_list,
					&timer->timer_node->node);
	qdf_spin_unlock_irqrestore(&qdf_timer_list_lock);
	if (v_status != QDF_STATUS_SUCCESS) {
		QDF_ASSERT(0);
		return QDF_STATUS_E_INVAL;
	}
	qdf_mem_free(timer->timer_node);

	qdf_spin_lock_irqsave(&timer->platform_info.spinlock);

	switch (timer->state) {

	case QDF_TIMER_STATE_STARTING:
		v_status = QDF_STATUS_E_BUSY;
		break;

	case QDF_TIMER_STATE_RUNNING:
		/* Stop the timer first */
		del_timer(&(timer->platform_info.timer));
		v_status = QDF_STATUS_SUCCESS;
		break;
	case QDF_TIMER_STATE_STOPPED:
		v_status = QDF_STATUS_SUCCESS;
		break;

	case QDF_TIMER_STATE_UNUSED:
		v_status = QDF_STATUS_E_ALREADY;
		break;

	default:
		v_status = QDF_STATUS_E_FAULT;
		break;
	}

	if (QDF_STATUS_SUCCESS == v_status) {
		timer->platform_info.cookie = LINUX_INVALID_TIMER_COOKIE;
		timer->state = QDF_TIMER_STATE_UNUSED;
		qdf_spin_unlock_irqrestore(&timer->platform_info.spinlock);
		qdf_spinlock_destroy(&timer->platform_info.spinlock);
		return v_status;
	}

	qdf_spin_unlock_irqrestore(&timer->platform_info.spinlock);

	QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_INFO_HIGH,
		  "%s: Cannot destroy timer in state = %d", __func__,
		  timer->state);
	QDF_ASSERT(0);

	return v_status;
}
qdf_export_symbol(qdf_mc_timer_destroy);

#else

/**
 * qdf_mc_timer_destroy() - destroy QDF timer
 * @timer: Pointer to timer object
 *
 * qdf_mc_timer_destroy() function shall destroy the timer object.
 * After a successful return from \a qdf_mc_timer_destroy() the timer
 * object becomes, in effect, uninitialized.
 *
 * A destroyed timer object can be re-initialized by calling
 * qdf_mc_timer_init(). The results of otherwise referencing the object
 * after it has been destroyed are undefined.
 *
 * Calls to QDF timer functions to manipulate the timer, such
 * as qdf_mc_timer_set() will fail if the lock is destroyed. Therefore,
 * don't use the timer after it has been destroyed until it has
 * been re-initialized.
 *
 * Return:
 * QDF_STATUS_SUCCESS - timer is initialized successfully
 * QDF failure status - timer initialization failed
 */
QDF_STATUS qdf_mc_timer_destroy(qdf_mc_timer_t *timer)
{
	QDF_STATUS v_status = QDF_STATUS_SUCCESS;

	/* check for invalid pointer */
	if (NULL == timer) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "%s: Null timer pointer being passed", __func__);
		QDF_ASSERT(0);
		return QDF_STATUS_E_FAULT;
	}

	/* check if timer refers to an uninitialized object */
	if (LINUX_TIMER_COOKIE != timer->platform_info.cookie) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "%s: Cannot destroy uninitialized timer", __func__);
		QDF_ASSERT(0);
		return QDF_STATUS_E_INVAL;
	}
	qdf_spin_lock_irqsave(&timer->platform_info.spinlock);

	switch (timer->state) {

	case QDF_TIMER_STATE_STARTING:
		v_status = QDF_STATUS_E_BUSY;
		break;

	case QDF_TIMER_STATE_RUNNING:
		/* Stop the timer first */
		del_timer(&(timer->platform_info.timer));
		v_status = QDF_STATUS_SUCCESS;
		break;

	case QDF_TIMER_STATE_STOPPED:
		v_status = QDF_STATUS_SUCCESS;
		break;

	case QDF_TIMER_STATE_UNUSED:
		v_status = QDF_STATUS_E_ALREADY;
		break;

	default:
		v_status = QDF_STATUS_E_FAULT;
		break;
	}

	if (QDF_STATUS_SUCCESS == v_status) {
		timer->platform_info.cookie = LINUX_INVALID_TIMER_COOKIE;
		timer->state = QDF_TIMER_STATE_UNUSED;
		qdf_spin_unlock_irqrestore(&timer->platform_info.spinlock);
		return v_status;
	}

	qdf_spin_unlock_irqrestore(&timer->platform_info.spinlock);

	QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_INFO_HIGH,
		  "%s: Cannot destroy timer in state = %d", __func__,
		  timer->state);
	QDF_ASSERT(0);

	return v_status;
}
qdf_export_symbol(qdf_mc_timer_destroy);
#endif

/**
 * qdf_mc_timer_start() - start a QDF timer object
 * @timer: Pointer to timer object
 * @expiration_time: Time to expire
 *
 * qdf_mc_timer_start() function starts a timer to expire after the
 * specified interval, thus running the timer callback function when
 * the interval expires.
 *
 * A timer only runs once (a one-shot timer). To re-start the
 * timer, qdf_mc_timer_start() has to be called after the timer runs
 * or has been cancelled.
 *
 * Return:
 * QDF_STATUS_SUCCESS: timer is initialized successfully
 * QDF failure status: timer initialization failed
 */
QDF_STATUS qdf_mc_timer_start(qdf_mc_timer_t *timer, uint32_t expiration_time)
{
	/* check for invalid pointer */
	if (NULL == timer) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "%s Null timer pointer being passed", __func__);
		QDF_ASSERT(0);
		return QDF_STATUS_E_INVAL;
	}

	/* check if timer refers to an uninitialized object */
	if (LINUX_TIMER_COOKIE != timer->platform_info.cookie) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "%s: Cannot start uninitialized timer", __func__);
		QDF_ASSERT(0);

		return QDF_STATUS_E_INVAL;
	}

	/* check if timer has expiration time less than 10 ms */
	if (expiration_time < 10) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "%s: Cannot start a timer with expiration less than 10 ms",
			  __func__);
		QDF_ASSERT(0);
		return QDF_STATUS_E_INVAL;
	}

	/* make sure the remainer of the logic isn't interrupted */
	qdf_spin_lock_irqsave(&timer->platform_info.spinlock);

	/* ensure if the timer can be started */
	if (QDF_TIMER_STATE_STOPPED != timer->state) {
		qdf_spin_unlock_irqrestore(&timer->platform_info.spinlock);
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "%s: Cannot start timer in state = %d ", __func__,
			  timer->state);
		return QDF_STATUS_E_ALREADY;
	}

	/* start the timer */
	mod_timer(&(timer->platform_info.timer),
		  jiffies + msecs_to_jiffies(expiration_time));

	timer->state = QDF_TIMER_STATE_RUNNING;

	/* get the thread ID on which the timer is being started */
	timer->platform_info.thread_id = current->pid;

	if (QDF_TIMER_TYPE_WAKE_APPS == timer->type) {
		persistent_timer_count++;
		if (1 == persistent_timer_count) {
			/* since we now have one persistent timer,
			 * we need to disallow sleep
			 * sleep_negate_okts(sleep_client_handle);
			 */
		}
	}

	qdf_spin_unlock_irqrestore(&timer->platform_info.spinlock);

	return QDF_STATUS_SUCCESS;
}
qdf_export_symbol(qdf_mc_timer_start);

/**
 * qdf_mc_timer_stop() - stop a QDF timer
 * @timer: Pointer to timer object
 * qdf_mc_timer_stop() function stops a timer that has been started but
 * has not expired, essentially cancelling the 'start' request.
 *
 * After a timer is stopped, it goes back to the state it was in after it
 * was created and can be started again via a call to qdf_mc_timer_start().
 *
 * Return:
 * QDF_STATUS_SUCCESS: timer is initialized successfully
 * QDF failure status: timer initialization failed
 */
QDF_STATUS qdf_mc_timer_stop(qdf_mc_timer_t *timer)
{
	/* check for invalid pointer */
	if (NULL == timer) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "%s Null timer pointer being passed", __func__);
		QDF_ASSERT(0);
		return QDF_STATUS_E_INVAL;
	}

	/* check if timer refers to an uninitialized object */
	if (LINUX_TIMER_COOKIE != timer->platform_info.cookie) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "%s: Cannot stop uninitialized timer", __func__);
		QDF_ASSERT(0);

		return QDF_STATUS_E_INVAL;
	}

	/* ensure the timer state is correct */
	qdf_spin_lock_irqsave(&timer->platform_info.spinlock);

	if (QDF_TIMER_STATE_RUNNING != timer->state) {
		qdf_spin_unlock_irqrestore(&timer->platform_info.spinlock);
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_INFO_HIGH,
			  "%s: Cannot stop timer in state = %d",
			  __func__, timer->state);
		return QDF_STATUS_SUCCESS;
	}

	timer->state = QDF_TIMER_STATE_STOPPED;

	del_timer(&(timer->platform_info.timer));

	qdf_spin_unlock_irqrestore(&timer->platform_info.spinlock);

	qdf_try_allowing_sleep(timer->type);

	return QDF_STATUS_SUCCESS;
}
qdf_export_symbol(qdf_mc_timer_stop);

/**
 * qdf_mc_timer_get_system_ticks() - get the system time in 10ms ticks

 * qdf_mc_timer_get_system_ticks() function returns the current number
 * of timer ticks in 10msec intervals. This function is suitable timestamping
 * and calculating time intervals by calculating the difference between two
 * timestamps.
 *
 * Return:
 * The current system tick count (in 10msec intervals).  This
 * function cannot fail.
 */
unsigned long qdf_mc_timer_get_system_ticks(void)
{
	return jiffies_to_msecs(jiffies) / 10;
}
qdf_export_symbol(qdf_mc_timer_get_system_ticks);

/**
 * qdf_mc_timer_get_system_time() - Get the system time in milliseconds
 *
 * qdf_mc_timer_get_system_time() function returns the number of milliseconds
 * that have elapsed since the system was started
 *
 * Return:
 * The current system time in milliseconds
 */
unsigned long qdf_mc_timer_get_system_time(void)
{
	struct timeval tv;
	do_gettimeofday(&tv);
	return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
qdf_export_symbol(qdf_mc_timer_get_system_time);

s64 qdf_get_monotonic_boottime_ns(void)
{
	struct timespec ts;

	ktime_get_ts(&ts);
	return timespec_to_ns(&ts);
}
qdf_export_symbol(qdf_get_monotonic_boottime_ns);

/**
 * qdf_timer_module_deinit() - Deinitializes a QDF timer module.
 *
 * This API deinitializes the QDF timer module.
 * Return: none
 */
void qdf_timer_module_deinit(void)
{
	QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_INFO_HIGH,
		  "De-Initializing the QDF MC timer module");
	qdf_mutex_destroy(&persistent_timer_count_lock);
}
qdf_export_symbol(qdf_timer_module_deinit);

void qdf_get_time_of_the_day_in_hr_min_sec_usec(char *tbuf, int len)
{
	struct timeval tv;
	struct rtc_time tm;
	unsigned long local_time;

	/* Format the Log time R#: [hr:min:sec.microsec] */
	do_gettimeofday(&tv);
	/* Convert rtc to local time */
	local_time = (u32)(tv.tv_sec - (sys_tz.tz_minuteswest * 60));
	rtc_time_to_tm(local_time, &tm);
	scnprintf(tbuf, len,
		"[%02d:%02d:%02d.%06lu]",
		tm.tm_hour, tm.tm_min, tm.tm_sec, tv.tv_usec);
}
qdf_export_symbol(qdf_get_time_of_the_day_in_hr_min_sec_usec);