xref: /wlan-dirver/qca-wifi-host-cmn/qdf/linux/src/i_qdf_timer.h (revision 503663c6daafffe652fa360bde17243568cd6d2a)
1 /*
2  * Copyright (c) 2014-2019 The Linux Foundation. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all
7  * copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /**
20  * DOC: i_qdf_timer
21  * This file provides OS dependent timer API's.
22  */
23 
24 #ifndef _I_QDF_TIMER_H
25 #define _I_QDF_TIMER_H
26 
27 #include <linux/version.h>
28 #include <linux/delay.h>
29 #include <linux/timer.h>
30 #include <linux/jiffies.h>
31 #include "qdf_mc_timer.h"
32 #include <qdf_types.h>
33 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)
34 #include <linux/sched/task_stack.h>
35 #endif
36 
37 typedef void (*qdf_timer_func_t)(void *);
38 
39 
40 struct __qdf_timer_t {
41 	struct timer_list os_timer;
42 	qdf_timer_func_t callback;
43 	void *context;
44 };
45 
46 #ifdef QDF_TIMER_MULTIPLIER_FRAC
47 #define __qdf_scaled_msecs_to_jiffies(msec) \
48 	(QDF_TIMER_MULTIPLIER_FRAC * msecs_to_jiffies(msec))
49 #else
50 #define __qdf_scaled_msecs_to_jiffies(msec) \
51 	(qdf_timer_get_multiplier() * msecs_to_jiffies(msec))
52 #endif
53 
54 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 15, 0)
55 static inline void __os_timer_shim(struct timer_list *os_timer)
56 {
57 	struct __qdf_timer_t *timer = from_timer(timer, os_timer, os_timer);
58 
59 	timer->callback(timer->context);
60 }
61 
62 static inline QDF_STATUS __qdf_timer_init(struct __qdf_timer_t *timer,
63 					  qdf_timer_func_t func, void *arg,
64 					  QDF_TIMER_TYPE type)
65 {
66 	struct timer_list *os_timer = &timer->os_timer;
67 	uint32_t flags = 0;
68 
69 	timer->callback = func;
70 	timer->context = arg;
71 
72 	if (type == QDF_TIMER_TYPE_SW)
73 		flags |= TIMER_DEFERRABLE;
74 
75 	if (object_is_on_stack(os_timer))
76 		timer_setup_on_stack(os_timer, __os_timer_shim, flags);
77 	else
78 		timer_setup(os_timer, __os_timer_shim, flags);
79 
80 	return QDF_STATUS_SUCCESS;
81 }
82 
83 #else
84 
85 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0)
86 #define setup_deferrable_timer(timer, fn, data) \
87 	__setup_timer((timer), (fn), (data), TIMER_DEFERRABLE)
88 #endif
89 
90 static inline void __os_timer_shim(unsigned long addr)
91 {
92 	struct __qdf_timer_t *timer = (void *)addr;
93 
94 	timer->callback(timer->context);
95 }
96 
97 static inline QDF_STATUS __qdf_timer_init(struct __qdf_timer_t *timer,
98 					  qdf_timer_func_t func, void *arg,
99 					  QDF_TIMER_TYPE type)
100 {
101 	struct timer_list *os_timer = &timer->os_timer;
102 	bool is_on_stack = object_is_on_stack(os_timer);
103 	unsigned long addr = (unsigned long)timer;
104 
105 	timer->callback = func;
106 	timer->context = arg;
107 
108 	if (type == QDF_TIMER_TYPE_SW) {
109 		if (is_on_stack)
110 			setup_deferrable_timer_on_stack(os_timer,
111 							__os_timer_shim,
112 							addr);
113 		else
114 			setup_deferrable_timer(os_timer, __os_timer_shim, addr);
115 	} else {
116 		if (is_on_stack)
117 			setup_timer_on_stack(os_timer, __os_timer_shim, addr);
118 		else
119 			setup_timer(os_timer, __os_timer_shim, addr);
120 	}
121 
122 	return QDF_STATUS_SUCCESS;
123 }
124 #endif /* KERNEL_VERSION(4, 15, 0)*/
125 
126 static inline void __qdf_timer_start(struct __qdf_timer_t *timer, uint32_t msec)
127 {
128 	struct timer_list *os_timer = &timer->os_timer;
129 
130 	os_timer->expires = jiffies + __qdf_scaled_msecs_to_jiffies(msec);
131 	add_timer(os_timer);
132 }
133 
134 static inline void __qdf_timer_mod(struct __qdf_timer_t *timer, uint32_t msec)
135 {
136 	mod_timer(&timer->os_timer,
137 		  jiffies + __qdf_scaled_msecs_to_jiffies(msec));
138 }
139 
140 static inline bool __qdf_timer_stop(struct __qdf_timer_t *timer)
141 {
142 	return !!del_timer(&timer->os_timer);
143 }
144 
145 static inline void __qdf_timer_free(struct __qdf_timer_t *timer)
146 {
147 	struct timer_list *os_timer = &timer->os_timer;
148 
149 	del_timer_sync(os_timer);
150 
151 	if (object_is_on_stack(os_timer))
152 		destroy_timer_on_stack(os_timer);
153 }
154 
155 static inline bool __qdf_timer_sync_cancel(struct __qdf_timer_t *timer)
156 {
157 	return del_timer_sync(&timer->os_timer);
158 }
159 
160 #endif /* _I_QDF_TIMER_H */
161