1 /* 2 * Copyright (c) 2014-2018 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_types.h> 32 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0) 33 #include <linux/sched/task_stack.h> 34 #endif 35 36 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0) 37 #define setup_deferrable_timer(timer, fn, data) \ 38 __setup_timer((timer), (fn), (data), TIMER_DEFERRABLE) 39 #endif 40 41 /* timer data type */ 42 typedef struct timer_list __qdf_timer_t; 43 44 typedef void (*__qdf_dummy_timer_func_t)(unsigned long arg); 45 46 /** 47 * __qdf_timer_init() - initialize a softirq timer 48 * @hdl: OS handle 49 * @timer: Pointer to timer object 50 * @func: Function pointer 51 * @arg: Argument 52 * @type: deferrable or non deferrable timer type 53 * 54 * Timer type QDF_TIMER_TYPE_SW means its a deferrable sw timer which will 55 * not cause CPU wake upon expiry 56 * Timer type QDF_TIMER_TYPE_WAKE_APPS means its a non-deferrable timer which 57 * will cause CPU wake up on expiry 58 * 59 * Return: QDF_STATUS 60 */ 61 static inline QDF_STATUS __qdf_timer_init(qdf_handle_t hdl, 62 struct timer_list *timer, 63 qdf_timer_func_t func, void *arg, 64 QDF_TIMER_TYPE type) 65 { 66 if (type == QDF_TIMER_TYPE_SW) { 67 if (object_is_on_stack(timer)) 68 setup_deferrable_timer_on_stack( 69 timer, (__qdf_dummy_timer_func_t)func, 70 (unsigned long)arg); 71 else 72 setup_deferrable_timer(timer, 73 (__qdf_dummy_timer_func_t)func, 74 (unsigned long)arg); 75 } else { 76 if (object_is_on_stack(timer)) 77 setup_timer_on_stack(timer, 78 (__qdf_dummy_timer_func_t)func, 79 (unsigned long)arg); 80 else 81 setup_timer(timer, (__qdf_dummy_timer_func_t)func, 82 (unsigned long)arg); 83 } 84 85 return QDF_STATUS_SUCCESS; 86 } 87 88 /** 89 * __qdf_timer_start() - start a qdf softirq timer 90 * @timer: Pointer to timer object 91 * @delay: Delay in milli seconds 92 * 93 * Return: QDF_STATUS 94 */ 95 static inline QDF_STATUS __qdf_timer_start(struct timer_list *timer, 96 uint32_t delay) 97 { 98 timer->expires = jiffies + msecs_to_jiffies(delay); 99 add_timer(timer); 100 101 return QDF_STATUS_SUCCESS; 102 } 103 104 /** 105 * __qdf_timer_mod() - modify a timer 106 * @timer: Pointer to timer object 107 * @delay: Delay in milli seconds 108 * 109 * Return: QDF_STATUS 110 */ 111 static inline QDF_STATUS __qdf_timer_mod(struct timer_list *timer, 112 uint32_t delay) 113 { 114 mod_timer(timer, jiffies + msecs_to_jiffies(delay)); 115 116 return QDF_STATUS_SUCCESS; 117 } 118 119 /** 120 * __qdf_timer_stop() - cancel a timer 121 * @timer: Pointer to timer object 122 * 123 * Return: true if timer was cancelled and deactived, 124 * false if timer was cancelled but already got fired. 125 */ 126 static inline bool __qdf_timer_stop(struct timer_list *timer) 127 { 128 if (likely(del_timer(timer))) 129 return 1; 130 else 131 return 0; 132 } 133 134 /** 135 * __qdf_timer_free() - free a qdf timer 136 * @timer: Pointer to timer object 137 * 138 * Return: None 139 */ 140 static inline void __qdf_timer_free(struct timer_list *timer) 141 { 142 del_timer_sync(timer); 143 144 if (object_is_on_stack(timer)) 145 destroy_timer_on_stack(timer); 146 } 147 148 /** 149 * __qdf_sostirq_timer_sync_cancel() - Synchronously canel a timer 150 * @timer: Pointer to timer object 151 * 152 * Synchronization Rules: 153 * 1. caller must make sure timer function will not use 154 * qdf_set_timer to add iteself again. 155 * 2. caller must not hold any lock that timer function 156 * is likely to hold as well. 157 * 3. It can't be called from interrupt context. 158 * 159 * Return: true if timer was cancelled and deactived, 160 * false if timer was cancelled but already got fired. 161 */ 162 static inline bool __qdf_timer_sync_cancel(struct timer_list *timer) 163 { 164 return del_timer_sync(timer); 165 } 166 167 #endif /*_QDF_TIMER_PVT_H*/ 168