1 /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
2 /* Copyright(c) 2007 - 2011 Realtek Corporation. */
3
4 #ifndef __OSDEP_SERVICE_H_
5 #define __OSDEP_SERVICE_H_
6
7 #include <linux/sched/signal.h>
8 #include "basic_types.h"
9
10 #define _FAIL 0
11 #define _SUCCESS 1
12 #define RTW_RX_HANDLED 2
13
14 #include <linux/spinlock.h>
15 #include <linux/compiler.h>
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/module.h>
21 #include <linux/kref.h>
22 #include <linux/netdevice.h>
23 #include <linux/skbuff.h>
24 #include <linux/circ_buf.h>
25 #include <linux/uaccess.h>
26 #include <asm/byteorder.h>
27 #include <asm/atomic.h>
28 #include <linux/io.h>
29 #include <linux/semaphore.h>
30 #include <linux/sem.h>
31 #include <linux/sched.h>
32 #include <linux/etherdevice.h>
33 #include <linux/wireless.h>
34 #include <net/iw_handler.h>
35 #include <linux/if_arp.h>
36 #include <linux/rtnetlink.h>
37 #include <linux/delay.h>
38 #include <linux/proc_fs.h> /* Necessary because we use the proc fs */
39 #include <linux/interrupt.h> /* for struct tasklet_struct */
40 #include <linux/ip.h>
41 #include <linux/kthread.h>
42 #include <linux/vmalloc.h>
43
44 #include <linux/usb.h>
45 #include <linux/usb/ch9.h>
46
47 struct __queue {
48 struct list_head queue;
49 spinlock_t lock;
50 };
51
52 #define thread_exit() complete_and_exit(NULL, 0)
53
get_list_head(struct __queue * queue)54 static inline struct list_head *get_list_head(struct __queue *queue)
55 {
56 return (&(queue->queue));
57 }
58
_enter_critical_mutex(struct mutex * pmutex,unsigned long * pirqL)59 static inline int _enter_critical_mutex(struct mutex *pmutex, unsigned long *pirqL)
60 {
61 int ret;
62
63 ret = mutex_lock_interruptible(pmutex);
64 return ret;
65 }
66
_exit_critical_mutex(struct mutex * pmutex,unsigned long * pirqL)67 static inline void _exit_critical_mutex(struct mutex *pmutex, unsigned long *pirqL)
68 {
69 mutex_unlock(pmutex);
70 }
71
rtw_list_delete(struct list_head * plist)72 static inline void rtw_list_delete(struct list_head *plist)
73 {
74 list_del_init(plist);
75 }
76
_set_timer(struct timer_list * ptimer,u32 delay_time)77 static inline void _set_timer(struct timer_list *ptimer,u32 delay_time)
78 {
79 mod_timer(ptimer , (jiffies+(delay_time*HZ/1000)));
80 }
81
_cancel_timer(struct timer_list * ptimer,u8 * bcancelled)82 static inline void _cancel_timer(struct timer_list *ptimer,u8 *bcancelled)
83 {
84 del_timer_sync(ptimer);
85 *bcancelled= true;/* true ==1; false==0 */
86 }
87
88 #define RTW_TIMER_HDL_ARGS void *FunctionContext
89 #define RTW_TIMER_HDL_NAME(name) rtw_##name##_timer_hdl
90 #define RTW_DECLARE_TIMER_HDL(name) void RTW_TIMER_HDL_NAME(name)(RTW_TIMER_HDL_ARGS)
91
_init_workitem(struct work_struct * pwork,void * pfunc,void * cntx)92 static inline void _init_workitem(struct work_struct *pwork, void *pfunc, void * cntx)
93 {
94 INIT_WORK(pwork, pfunc);
95 }
96
_set_workitem(struct work_struct * pwork)97 static inline void _set_workitem(struct work_struct *pwork)
98 {
99 schedule_work(pwork);
100 }
101
_cancel_workitem_sync(struct work_struct * pwork)102 static inline void _cancel_workitem_sync(struct work_struct *pwork)
103 {
104 cancel_work_sync(pwork);
105 }
106 /* */
107 /* Global Mutex: can only be used at PASSIVE level. */
108 /* */
109
110 #define ACQUIRE_GLOBAL_MUTEX(_MutexCounter) \
111 { \
112 while (atomic_inc_return((atomic_t *)&(_MutexCounter)) != 1)\
113 { \
114 atomic_dec((atomic_t *)&(_MutexCounter)); \
115 msleep(10); \
116 } \
117 }
118
119 #define RELEASE_GLOBAL_MUTEX(_MutexCounter) \
120 { \
121 atomic_dec((atomic_t *)&(_MutexCounter)); \
122 }
123
rtw_netif_queue_stopped(struct net_device * pnetdev)124 static inline int rtw_netif_queue_stopped(struct net_device *pnetdev)
125 {
126 return netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 0)) &&
127 netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 1)) &&
128 netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 2)) &&
129 netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 3));
130 }
131
rtw_netif_wake_queue(struct net_device * pnetdev)132 static inline void rtw_netif_wake_queue(struct net_device *pnetdev)
133 {
134 netif_tx_wake_all_queues(pnetdev);
135 }
136
rtw_netif_start_queue(struct net_device * pnetdev)137 static inline void rtw_netif_start_queue(struct net_device *pnetdev)
138 {
139 netif_tx_start_all_queues(pnetdev);
140 }
141
rtw_netif_stop_queue(struct net_device * pnetdev)142 static inline void rtw_netif_stop_queue(struct net_device *pnetdev)
143 {
144 netif_tx_stop_all_queues(pnetdev);
145 }
146
147 extern int RTW_STATUS_CODE(int error_code);
148
149 extern unsigned char MCS_rate_2R[16];
150 extern unsigned char MCS_rate_1R[16];
151 extern unsigned char RTW_WPA_OUI[];
152 extern unsigned char WPA_TKIP_CIPHER[4];
153 extern unsigned char RSN_TKIP_CIPHER[4];
154
155 void *rtw_malloc2d(int h, int w, int size);
156
157 u32 _rtw_down_sema(struct semaphore *sema);
158 void _rtw_mutex_init(struct mutex *pmutex);
159 void _rtw_mutex_free(struct mutex *pmutex);
160
161 void _rtw_init_queue(struct __queue *pqueue);
162
163 u32 rtw_systime_to_ms(u32 systime);
164 u32 rtw_ms_to_systime(u32 ms);
165 s32 rtw_get_passing_time_ms(u32 start);
166
167 void rtw_usleep_os(int us);
168
169 u32 rtw_atoi(u8 *s);
170
_cancel_timer_ex(struct timer_list * ptimer)171 static inline unsigned char _cancel_timer_ex(struct timer_list *ptimer)
172 {
173 return del_timer_sync(ptimer);
174 }
175
thread_enter(char * name)176 static __inline void thread_enter(char *name)
177 {
178 #ifdef daemonize
179 daemonize("%s", name);
180 #endif
181 allow_signal(SIGTERM);
182 }
183
flush_signals_thread(void)184 static inline void flush_signals_thread(void)
185 {
186 if (signal_pending (current))
187 flush_signals(current);
188 }
189
res_to_status(int res)190 static inline int res_to_status(int res)
191 {
192 return res;
193 }
194
195 #define _RND(sz, r) ((((sz)+((r)-1))/(r))*(r))
196 #define RND4(x) (((x >> 2) + (((x & 3) == 0) ? 0: 1)) << 2)
197
_RND4(u32 sz)198 static inline u32 _RND4(u32 sz)
199 {
200 u32 val;
201
202 val = ((sz >> 2) + ((sz & 3) ? 1: 0)) << 2;
203 return val;
204 }
205
_RND8(u32 sz)206 static inline u32 _RND8(u32 sz)
207 {
208 u32 val;
209
210 val = ((sz >> 3) + ((sz & 7) ? 1: 0)) << 3;
211 return val;
212 }
213
_RND128(u32 sz)214 static inline u32 _RND128(u32 sz)
215 {
216 u32 val;
217
218 val = ((sz >> 7) + ((sz & 127) ? 1: 0)) << 7;
219 return val;
220 }
221
_RND256(u32 sz)222 static inline u32 _RND256(u32 sz)
223 {
224 u32 val;
225
226 val = ((sz >> 8) + ((sz & 255) ? 1: 0)) << 8;
227 return val;
228 }
229
_RND512(u32 sz)230 static inline u32 _RND512(u32 sz)
231 {
232 u32 val;
233
234 val = ((sz >> 9) + ((sz & 511) ? 1: 0)) << 9;
235 return val;
236 }
237
bitshift(u32 bitmask)238 static inline u32 bitshift(u32 bitmask)
239 {
240 u32 i;
241
242 for (i = 0; i <= 31; i++)
243 if (((bitmask>>i) & 0x1) == 1) break;
244 return i;
245 }
246
247 /* limitation of path length */
248 #define PATH_LENGTH_MAX PATH_MAX
249
250 struct rtw_netdev_priv_indicator {
251 void *priv;
252 u32 sizeof_priv;
253 };
254 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv,
255 void *old_priv);
256 struct net_device *rtw_alloc_etherdev(int sizeof_priv);
257
258 #define rtw_netdev_priv(netdev) \
259 (((struct rtw_netdev_priv_indicator *)netdev_priv(netdev))->priv)
260 void rtw_free_netdev(struct net_device *netdev);
261
262 #define NDEV_FMT "%s"
263 #define NDEV_ARG(ndev) ndev->name
264 #define ADPT_FMT "%s"
265 #define ADPT_ARG(adapter) adapter->pnetdev->name
266 #define FUNC_NDEV_FMT "%s(%s)"
267 #define FUNC_NDEV_ARG(ndev) __func__, ndev->name
268 #define FUNC_ADPT_FMT "%s(%s)"
269 #define FUNC_ADPT_ARG(adapter) __func__, adapter->pnetdev->name
270
271 #define rtw_signal_process(pid, sig) kill_pid(find_vpid((pid)),(sig), 1)
272
273 /* Macros for handling unaligned memory accesses */
274
275 #define RTW_GET_BE16(a) ((u16) (((a)[0] << 8) | (a)[1]))
276 #define RTW_PUT_BE16(a, val) \
277 do { \
278 (a)[0] = ((u16) (val)) >> 8; \
279 (a)[1] = ((u16) (val)) & 0xff; \
280 } while (0)
281
282 #define RTW_PUT_LE16(a, val) \
283 do { \
284 (a)[1] = ((u16) (val)) >> 8; \
285 (a)[0] = ((u16) (val)) & 0xff; \
286 } while (0)
287
288 #define RTW_GET_BE24(a) ((((u32) (a)[0]) << 16) | (((u32) (a)[1]) << 8) | \
289 ((u32) (a)[2]))
290
291 #define RTW_PUT_BE32(a, val) \
292 do { \
293 (a)[0] = (u8) ((((u32) (val)) >> 24) & 0xff); \
294 (a)[1] = (u8) ((((u32) (val)) >> 16) & 0xff); \
295 (a)[2] = (u8) ((((u32) (val)) >> 8) & 0xff); \
296 (a)[3] = (u8) (((u32) (val)) & 0xff); \
297 } while (0)
298
299 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len);
300
301 struct rtw_cbuf {
302 u32 write;
303 u32 read;
304 u32 size;
305 void *bufs[0];
306 };
307
308 bool rtw_cbuf_full(struct rtw_cbuf *cbuf);
309 bool rtw_cbuf_empty(struct rtw_cbuf *cbuf);
310 bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf);
311 void *rtw_cbuf_pop(struct rtw_cbuf *cbuf);
312 struct rtw_cbuf *rtw_cbuf_alloc(u32 size);
313 int wifirate2_ratetbl_inx(unsigned char rate);
314
315 #endif
316