1 /******************************************************************************
2 *
3 * Copyright(c) 2007 - 2017 Realtek Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 *****************************************************************************/
15 #ifndef __OSDEP_SERVICE_H_
16 #define __OSDEP_SERVICE_H_
17
18
19 #define _FAIL 0
20 #define _SUCCESS 1
21 #define RTW_RX_HANDLED 2
22 #define RTW_RFRAME_UNAVAIL 3
23 #define RTW_RFRAME_PKT_UNAVAIL 4
24 #define RTW_RBUF_UNAVAIL 5
25 #define RTW_RBUF_PKT_UNAVAIL 6
26 #define RTW_SDIO_READ_PORT_FAIL 7
27 #define RTW_ALREADY 8
28 #define RTW_RA_RESOLVING 9
29 #define RTW_ORI_NO_NEED 10
30 #define RTW_XBUF_UNAVAIL 11
31 #define RTW_TX_BALANCE 12
32 #define RTW_TX_WAIT_MORE_FRAME 13
33 #define RTW_QUEUE_MGMT 14
34 #define RTW_NOT_SUPPORT 15
35 #define RTW_BUSY 16
36
37 /* #define RTW_STATUS_TIMEDOUT -110 */
38
39 #undef _TRUE
40 #define _TRUE 1
41
42 #undef _FALSE
43 #define _FALSE 0
44
45
46 #ifdef PLATFORM_FREEBSD
47 #include <osdep_service_bsd.h>
48 #endif
49
50 #ifdef PLATFORM_LINUX
51 #include <linux/version.h>
52 #if defined(CONFIG_RTW_ANDROID_GKI)
53 #include <linux/firmware.h>
54 #endif
55 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0))
56 #include <linux/sched/signal.h>
57 #include <linux/sched/types.h>
58 #endif
59 #include <osdep_service_linux.h>
60 #include <drv_types_linux.h>
61 #endif
62
63 #ifdef PLATFORM_OS_XP
64 #include <osdep_service_xp.h>
65 #include <drv_types_xp.h>
66 #endif
67
68 #ifdef PLATFORM_OS_CE
69 #include <osdep_service_ce.h>
70 #include <drv_types_ce.h>
71 #endif
72
73 /* #include <rtw_byteorder.h> */
74
75 #ifndef BIT
76 #define BIT(x) (1 << (x))
77 #endif
78 #ifndef BIT_ULL
79 #define BIT_ULL(x) (1ULL << (x))
80 #endif
81
82 #define CHECK_BIT(a, b) (!!((a) & (b)))
83
84 #define BIT0 0x00000001
85 #define BIT1 0x00000002
86 #define BIT2 0x00000004
87 #define BIT3 0x00000008
88 #define BIT4 0x00000010
89 #define BIT5 0x00000020
90 #define BIT6 0x00000040
91 #define BIT7 0x00000080
92 #define BIT8 0x00000100
93 #define BIT9 0x00000200
94 #define BIT10 0x00000400
95 #define BIT11 0x00000800
96 #define BIT12 0x00001000
97 #define BIT13 0x00002000
98 #define BIT14 0x00004000
99 #define BIT15 0x00008000
100 #define BIT16 0x00010000
101 #define BIT17 0x00020000
102 #define BIT18 0x00040000
103 #define BIT19 0x00080000
104 #define BIT20 0x00100000
105 #define BIT21 0x00200000
106 #define BIT22 0x00400000
107 #define BIT23 0x00800000
108 #define BIT24 0x01000000
109 #define BIT25 0x02000000
110 #define BIT26 0x04000000
111 #define BIT27 0x08000000
112 #define BIT28 0x10000000
113 #define BIT29 0x20000000
114 #define BIT30 0x40000000
115 #define BIT31 0x80000000
116 #define BIT32 0x0100000000
117 #define BIT33 0x0200000000
118 #define BIT34 0x0400000000
119 #define BIT35 0x0800000000
120 #define BIT36 0x1000000000
121
122 #ifndef GENMASK
123 #define GENMASK(h, l) \
124 (((~0UL) - (1UL << (l)) + 1) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
125 #endif
126
127 extern int RTW_STATUS_CODE(int error_code);
128
129 #ifndef RTK_DMP_PLATFORM
130 #define CONFIG_USE_VMALLOC
131 #endif
132
133 /* flags used for rtw_mstat_update() */
134 enum mstat_f {
135 /* type: 0x00ff */
136 MSTAT_TYPE_VIR = 0x00,
137 MSTAT_TYPE_PHY = 0x01,
138 MSTAT_TYPE_SKB = 0x02,
139 MSTAT_TYPE_USB = 0x03,
140 MSTAT_TYPE_MAX = 0x04,
141
142 /* func: 0xff00 */
143 MSTAT_FUNC_UNSPECIFIED = 0x00 << 8,
144 MSTAT_FUNC_IO = 0x01 << 8,
145 MSTAT_FUNC_TX_IO = 0x02 << 8,
146 MSTAT_FUNC_RX_IO = 0x03 << 8,
147 MSTAT_FUNC_TX = 0x04 << 8,
148 MSTAT_FUNC_RX = 0x05 << 8,
149 MSTAT_FUNC_CFG_VENDOR = 0x06 << 8,
150 MSTAT_FUNC_MAX = 0x07 << 8,
151 };
152
153 #define mstat_tf_idx(flags) ((flags) & 0xff)
154 #define mstat_ff_idx(flags) (((flags) & 0xff00) >> 8)
155
156 typedef enum mstat_status {
157 MSTAT_ALLOC_SUCCESS = 0,
158 MSTAT_ALLOC_FAIL,
159 MSTAT_FREE
160 } MSTAT_STATUS;
161
162 #ifdef DBG_MEM_ALLOC
163 void rtw_mstat_update(const enum mstat_f flags, const MSTAT_STATUS status, u32 sz);
164 void rtw_mstat_dump(void *sel);
165 bool match_mstat_sniff_rules(const enum mstat_f flags, const size_t size);
166 void *dbg_rtw_vmalloc(u32 sz, const enum mstat_f flags, const char *func, const int line);
167 void *dbg_rtw_zvmalloc(u32 sz, const enum mstat_f flags, const char *func, const int line);
168 void dbg_rtw_vmfree(void *pbuf, const enum mstat_f flags, u32 sz, const char *func, const int line);
169 void *dbg_rtw_malloc(u32 sz, const enum mstat_f flags, const char *func, const int line);
170 void *dbg_rtw_zmalloc(u32 sz, const enum mstat_f flags, const char *func, const int line);
171 void dbg_rtw_mfree(void *pbuf, const enum mstat_f flags, u32 sz, const char *func, const int line);
172
173 struct sk_buff *dbg_rtw_skb_alloc(unsigned int size, const enum mstat_f flags, const char *func, const int line);
174 void dbg_rtw_skb_free(struct sk_buff *skb, const enum mstat_f flags, const char *func, const int line);
175 struct sk_buff *dbg_rtw_skb_copy(const struct sk_buff *skb, const enum mstat_f flags, const char *func, const int line);
176 struct sk_buff *dbg_rtw_skb_clone(struct sk_buff *skb, const enum mstat_f flags, const char *func, const int line);
177 int dbg_rtw_netif_rx(_nic_hdl ndev, struct sk_buff *skb, const enum mstat_f flags, const char *func, int line);
178 #ifdef CONFIG_RTW_NAPI
179 int dbg_rtw_netif_receive_skb(_nic_hdl ndev, struct sk_buff *skb, const enum mstat_f flags, const char *func, int line);
180 #ifdef CONFIG_RTW_GRO
181 gro_result_t dbg_rtw_napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb, const enum mstat_f flags, const char *func, int line);
182 #endif
183 #endif /* CONFIG_RTW_NAPI */
184 void dbg_rtw_skb_queue_purge(struct sk_buff_head *list, enum mstat_f flags, const char *func, int line);
185 #ifdef CONFIG_USB_HCI
186 void *dbg_rtw_usb_buffer_alloc(struct usb_device *dev, size_t size, dma_addr_t *dma, const enum mstat_f flags, const char *func, const int line);
187 void dbg_rtw_usb_buffer_free(struct usb_device *dev, size_t size, void *addr, dma_addr_t dma, const enum mstat_f flags, const char *func, const int line);
188 #endif /* CONFIG_USB_HCI */
189
190 #ifdef CONFIG_USE_VMALLOC
191 #define rtw_vmalloc(sz) dbg_rtw_vmalloc((sz), MSTAT_TYPE_VIR, __FUNCTION__, __LINE__)
192 #define rtw_zvmalloc(sz) dbg_rtw_zvmalloc((sz), MSTAT_TYPE_VIR, __FUNCTION__, __LINE__)
193 #define rtw_vmfree(pbuf, sz) dbg_rtw_vmfree((pbuf), (sz), MSTAT_TYPE_VIR, __FUNCTION__, __LINE__)
194 #define rtw_vmalloc_f(sz, mstat_f) dbg_rtw_vmalloc((sz), ((mstat_f) & 0xff00) | MSTAT_TYPE_VIR, __FUNCTION__, __LINE__)
195 #define rtw_zvmalloc_f(sz, mstat_f) dbg_rtw_zvmalloc((sz), ((mstat_f) & 0xff00) | MSTAT_TYPE_VIR, __FUNCTION__, __LINE__)
196 #define rtw_vmfree_f(pbuf, sz, mstat_f) dbg_rtw_vmfree((pbuf), (sz), ((mstat_f) & 0xff00) | MSTAT_TYPE_VIR, __FUNCTION__, __LINE__)
197 #else /* CONFIG_USE_VMALLOC */
198 #define rtw_vmalloc(sz) dbg_rtw_malloc((sz), MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
199 #define rtw_zvmalloc(sz) dbg_rtw_zmalloc((sz), MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
200 #define rtw_vmfree(pbuf, sz) dbg_rtw_mfree((pbuf), (sz), MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
201 #define rtw_vmalloc_f(sz, mstat_f) dbg_rtw_malloc((sz), ((mstat_f) & 0xff00) | MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
202 #define rtw_zvmalloc_f(sz, mstat_f) dbg_rtw_zmalloc((sz), ((mstat_f) & 0xff00) | MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
203 #define rtw_vmfree_f(pbuf, sz, mstat_f) dbg_rtw_mfree((pbuf), (sz), ((mstat_f) & 0xff00) | MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
204 #endif /* CONFIG_USE_VMALLOC */
205 #define rtw_malloc(sz) dbg_rtw_malloc((sz), MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
206 #define rtw_zmalloc(sz) dbg_rtw_zmalloc((sz), MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
207 #define rtw_mfree(pbuf, sz) dbg_rtw_mfree((pbuf), (sz), MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
208 #define rtw_malloc_f(sz, mstat_f) dbg_rtw_malloc((sz), ((mstat_f) & 0xff00) | MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
209 #define rtw_zmalloc_f(sz, mstat_f) dbg_rtw_zmalloc((sz), ((mstat_f) & 0xff00) | MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
210 #define rtw_mfree_f(pbuf, sz, mstat_f) dbg_rtw_mfree((pbuf), (sz), ((mstat_f) & 0xff00) | MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
211
212 #define rtw_skb_alloc(size) dbg_rtw_skb_alloc((size), MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
213 #define rtw_skb_free(skb) dbg_rtw_skb_free((skb), MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
214 #define rtw_skb_alloc_f(size, mstat_f) dbg_rtw_skb_alloc((size), ((mstat_f) & 0xff00) | MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
215 #define rtw_skb_free_f(skb, mstat_f) dbg_rtw_skb_free((skb), ((mstat_f) & 0xff00) | MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
216 #define rtw_skb_copy(skb) dbg_rtw_skb_copy((skb), MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
217 #define rtw_skb_clone(skb) dbg_rtw_skb_clone((skb), MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
218 #define rtw_skb_copy_f(skb, mstat_f) dbg_rtw_skb_copy((skb), ((mstat_f) & 0xff00) | MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
219 #define rtw_skb_clone_f(skb, mstat_f) dbg_rtw_skb_clone((skb), ((mstat_f) & 0xff00) | MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
220 #define rtw_netif_rx(ndev, skb) dbg_rtw_netif_rx(ndev, skb, MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
221 #ifdef CONFIG_RTW_NAPI
222 #define rtw_netif_receive_skb(ndev, skb) dbg_rtw_netif_receive_skb(ndev, skb, MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
223 #ifdef CONFIG_RTW_GRO
224 #define rtw_napi_gro_receive(napi, skb) dbg_rtw_napi_gro_receive(napi, skb, MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
225 #endif
226 #endif /* CONFIG_RTW_NAPI */
227 #define rtw_skb_queue_purge(sk_buff_head) dbg_rtw_skb_queue_purge(sk_buff_head, MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
228 #ifdef CONFIG_USB_HCI
229 #define rtw_usb_buffer_alloc(dev, size, dma) dbg_rtw_usb_buffer_alloc((dev), (size), (dma), MSTAT_TYPE_USB, __FUNCTION__, __LINE__)
230 #define rtw_usb_buffer_free(dev, size, addr, dma) dbg_rtw_usb_buffer_free((dev), (size), (addr), (dma), MSTAT_TYPE_USB, __FUNCTION__, __LINE__)
231 #define rtw_usb_buffer_alloc_f(dev, size, dma, mstat_f) dbg_rtw_usb_buffer_alloc((dev), (size), (dma), ((mstat_f) & 0xff00) | MSTAT_TYPE_USB, __FUNCTION__, __LINE__)
232 #define rtw_usb_buffer_free_f(dev, size, addr, dma, mstat_f) dbg_rtw_usb_buffer_free((dev), (size), (addr), (dma), ((mstat_f) & 0xff00) | MSTAT_TYPE_USB, __FUNCTION__, __LINE__)
233 #endif /* CONFIG_USB_HCI */
234
235 #else /* DBG_MEM_ALLOC */
236 #define rtw_mstat_update(flag, status, sz) do {} while (0)
237 #define rtw_mstat_dump(sel) do {} while (0)
238 #define match_mstat_sniff_rules(flags, size) _FALSE
239 void *_rtw_vmalloc(u32 sz);
240 void *_rtw_zvmalloc(u32 sz);
241 void _rtw_vmfree(void *pbuf, u32 sz);
242 void *_rtw_zmalloc(u32 sz);
243 void *_rtw_malloc(u32 sz);
244 void _rtw_mfree(void *pbuf, u32 sz);
245
246 #ifdef CONFIG_PCIE_DMA_COHERENT
247 struct sk_buff *dev_alloc_skb_coherent(struct pci_dev *pdev, unsigned int size);
248 #endif
249 struct sk_buff *_rtw_skb_alloc(u32 sz);
250 void _rtw_skb_free(struct sk_buff *skb);
251 struct sk_buff *_rtw_skb_copy(const struct sk_buff *skb);
252 struct sk_buff *_rtw_skb_clone(struct sk_buff *skb);
253 int _rtw_netif_rx(_nic_hdl ndev, struct sk_buff *skb);
254 #ifdef CONFIG_RTW_NAPI
255 int _rtw_netif_receive_skb(_nic_hdl ndev, struct sk_buff *skb);
256 #ifdef CONFIG_RTW_GRO
257 gro_result_t _rtw_napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb);
258 #endif
259 #endif /* CONFIG_RTW_NAPI */
260 void _rtw_skb_queue_purge(struct sk_buff_head *list);
261
262 #ifdef CONFIG_USB_HCI
263 void *_rtw_usb_buffer_alloc(struct usb_device *dev, size_t size, dma_addr_t *dma);
264 void _rtw_usb_buffer_free(struct usb_device *dev, size_t size, void *addr, dma_addr_t dma);
265 #endif /* CONFIG_USB_HCI */
266
267 #ifdef CONFIG_USE_VMALLOC
268 #define rtw_vmalloc(sz) _rtw_vmalloc((sz))
269 #define rtw_zvmalloc(sz) _rtw_zvmalloc((sz))
270 #define rtw_vmfree(pbuf, sz) _rtw_vmfree((pbuf), (sz))
271 #define rtw_vmalloc_f(sz, mstat_f) _rtw_vmalloc((sz))
272 #define rtw_zvmalloc_f(sz, mstat_f) _rtw_zvmalloc((sz))
273 #define rtw_vmfree_f(pbuf, sz, mstat_f) _rtw_vmfree((pbuf), (sz))
274 #else /* CONFIG_USE_VMALLOC */
275 #define rtw_vmalloc(sz) _rtw_malloc((sz))
276 #define rtw_zvmalloc(sz) _rtw_zmalloc((sz))
277 #define rtw_vmfree(pbuf, sz) _rtw_mfree((pbuf), (sz))
278 #define rtw_vmalloc_f(sz, mstat_f) _rtw_malloc((sz))
279 #define rtw_zvmalloc_f(sz, mstat_f) _rtw_zmalloc((sz))
280 #define rtw_vmfree_f(pbuf, sz, mstat_f) _rtw_mfree((pbuf), (sz))
281 #endif /* CONFIG_USE_VMALLOC */
282 #define rtw_malloc(sz) _rtw_malloc((sz))
283 #define rtw_zmalloc(sz) _rtw_zmalloc((sz))
284 #define rtw_mfree(pbuf, sz) _rtw_mfree((pbuf), (sz))
285 #define rtw_malloc_f(sz, mstat_f) _rtw_malloc((sz))
286 #define rtw_zmalloc_f(sz, mstat_f) _rtw_zmalloc((sz))
287 #define rtw_mfree_f(pbuf, sz, mstat_f) _rtw_mfree((pbuf), (sz))
288
289 #define rtw_skb_alloc(size) _rtw_skb_alloc((size))
290 #define rtw_skb_free(skb) _rtw_skb_free((skb))
291 #define rtw_skb_alloc_f(size, mstat_f) _rtw_skb_alloc((size))
292 #define rtw_skb_free_f(skb, mstat_f) _rtw_skb_free((skb))
293 #define rtw_skb_copy(skb) _rtw_skb_copy((skb))
294 #define rtw_skb_clone(skb) _rtw_skb_clone((skb))
295 #define rtw_skb_copy_f(skb, mstat_f) _rtw_skb_copy((skb))
296 #define rtw_skb_clone_f(skb, mstat_f) _rtw_skb_clone((skb))
297 #define rtw_netif_rx(ndev, skb) _rtw_netif_rx(ndev, skb)
298 #ifdef CONFIG_RTW_NAPI
299 #define rtw_netif_receive_skb(ndev, skb) _rtw_netif_receive_skb(ndev, skb)
300 #ifdef CONFIG_RTW_GRO
301 #define rtw_napi_gro_receive(napi, skb) _rtw_napi_gro_receive(napi, skb)
302 #endif
303 #endif /* CONFIG_RTW_NAPI */
304 #define rtw_skb_queue_purge(sk_buff_head) _rtw_skb_queue_purge(sk_buff_head)
305 #ifdef CONFIG_USB_HCI
306 #define rtw_usb_buffer_alloc(dev, size, dma) _rtw_usb_buffer_alloc((dev), (size), (dma))
307 #define rtw_usb_buffer_free(dev, size, addr, dma) _rtw_usb_buffer_free((dev), (size), (addr), (dma))
308 #define rtw_usb_buffer_alloc_f(dev, size, dma, mstat_f) _rtw_usb_buffer_alloc((dev), (size), (dma))
309 #define rtw_usb_buffer_free_f(dev, size, addr, dma, mstat_f) _rtw_usb_buffer_free((dev), (size), (addr), (dma))
310 #endif /* CONFIG_USB_HCI */
311 #endif /* DBG_MEM_ALLOC */
312
313 extern void *rtw_malloc2d(int h, int w, size_t size);
314 extern void rtw_mfree2d(void *pbuf, int h, int w, int size);
315
316 void rtw_os_pkt_free(_pkt *pkt);
317 _pkt *rtw_os_pkt_copy(_pkt *pkt);
318 void *rtw_os_pkt_data(_pkt *pkt);
319 u32 rtw_os_pkt_len(_pkt *pkt);
320
321 extern void _rtw_memcpy(void *dec, const void *sour, u32 sz);
322 extern void _rtw_memmove(void *dst, const void *src, u32 sz);
323 extern int _rtw_memcmp(const void *dst, const void *src, u32 sz);
324 extern int _rtw_memcmp2(const void *dst, const void *src, u32 sz);
325 extern void _rtw_memset(void *pbuf, int c, u32 sz);
326
327 extern void _rtw_init_listhead(_list *list);
328 extern u32 rtw_is_list_empty(_list *phead);
329 extern void rtw_list_insert_head(_list *plist, _list *phead);
330 extern void rtw_list_insert_tail(_list *plist, _list *phead);
331 void rtw_list_splice(_list *list, _list *head);
332 void rtw_list_splice_init(_list *list, _list *head);
333 void rtw_list_splice_tail(_list *list, _list *head);
334
335 #ifndef PLATFORM_FREEBSD
336 extern void rtw_list_delete(_list *plist);
337 #endif /* PLATFORM_FREEBSD */
338
339 void rtw_hlist_head_init(rtw_hlist_head *h);
340 void rtw_hlist_add_head(rtw_hlist_node *n, rtw_hlist_head *h);
341 void rtw_hlist_del(rtw_hlist_node *n);
342 void rtw_hlist_add_head_rcu(rtw_hlist_node *n, rtw_hlist_head *h);
343 void rtw_hlist_del_rcu(rtw_hlist_node *n);
344
345 extern void _rtw_init_sema(_sema *sema, int init_val);
346 extern void _rtw_free_sema(_sema *sema);
347 extern void _rtw_up_sema(_sema *sema);
348 extern u32 _rtw_down_sema(_sema *sema);
349 extern void _rtw_mutex_init(_mutex *pmutex);
350 extern void _rtw_mutex_free(_mutex *pmutex);
351 #ifndef PLATFORM_FREEBSD
352 extern void _rtw_spinlock_init(_lock *plock);
353 #endif /* PLATFORM_FREEBSD */
354 extern void _rtw_spinlock_free(_lock *plock);
355 extern void _rtw_spinlock(_lock *plock);
356 extern void _rtw_spinunlock(_lock *plock);
357 extern void _rtw_spinlock_ex(_lock *plock);
358 extern void _rtw_spinunlock_ex(_lock *plock);
359
360 extern void _rtw_init_queue(_queue *pqueue);
361 extern void _rtw_deinit_queue(_queue *pqueue);
362 extern u32 _rtw_queue_empty(_queue *pqueue);
363 extern u32 rtw_end_of_queue_search(_list *queue, _list *pelement);
364
365 extern systime _rtw_get_current_time(void);
366 extern u32 _rtw_systime_to_ms(systime stime);
367 extern systime _rtw_ms_to_systime(u32 ms);
368 extern systime _rtw_us_to_systime(u32 us);
369 extern s32 _rtw_get_passing_time_ms(systime start);
370 extern s32 _rtw_get_remaining_time_ms(systime end);
371 extern s32 _rtw_get_time_interval_ms(systime start, systime end);
372 extern bool _rtw_time_after(systime a, systime b);
373
374 #ifdef DBG_SYSTIME
375 #define rtw_get_current_time() ({systime __stime = _rtw_get_current_time(); __stime;})
376 #define rtw_systime_to_ms(stime) ({u32 __ms = _rtw_systime_to_ms(stime); typecheck(systime, stime); __ms;})
377 #define rtw_ms_to_systime(ms) ({systime __stime = _rtw_ms_to_systime(ms); __stime;})
378 #define rtw_us_to_systime(us) ({systime __stime = _rtw_us_to_systime(us); __stime;})
379 #define rtw_get_passing_time_ms(start) ({u32 __ms = _rtw_get_passing_time_ms(start); typecheck(systime, start); __ms;})
380 #define rtw_get_remaining_time_ms(end) ({u32 __ms = _rtw_get_remaining_time_ms(end); typecheck(systime, end); __ms;})
381 #define rtw_get_time_interval_ms(start, end) ({u32 __ms = _rtw_get_time_interval_ms(start, end); typecheck(systime, start); typecheck(systime, end); __ms;})
382 #define rtw_time_after(a,b) ({bool __r = _rtw_time_after(a,b); typecheck(systime, a); typecheck(systime, b); __r;})
383 #define rtw_time_before(a,b) ({bool __r = _rtw_time_after(b, a); typecheck(systime, a); typecheck(systime, b); __r;})
384 #else
385 #define rtw_get_current_time() _rtw_get_current_time()
386 #define rtw_systime_to_ms(stime) _rtw_systime_to_ms(stime)
387 #define rtw_ms_to_systime(ms) _rtw_ms_to_systime(ms)
388 #define rtw_us_to_systime(us) _rtw_us_to_systime(us)
389 #define rtw_get_passing_time_ms(start) _rtw_get_passing_time_ms(start)
390 #define rtw_get_remaining_time_ms(end) _rtw_get_remaining_time_ms(end)
391 #define rtw_get_time_interval_ms(start, end) _rtw_get_time_interval_ms(start, end)
392 #define rtw_time_after(a,b) _rtw_time_after(a,b)
393 #define rtw_time_before(a,b) _rtw_time_after(b,a)
394 #endif
395
396 sysptime rtw_sptime_get(void);
397 sysptime rtw_sptime_set(s64 secs, const u32 nsecs);
398 sysptime rtw_sptime_zero(void);
399
400 int rtw_sptime_cmp(const sysptime cmp1, const sysptime cmp2);
401 bool rtw_sptime_eql(const sysptime cmp1, const sysptime cmp2);
402 bool rtw_sptime_is_zero(const sysptime sptime);
403 sysptime rtw_sptime_sub(const sysptime lhs, const sysptime rhs);
404 sysptime rtw_sptime_add(const sysptime lhs, const sysptime rhs);
405
406 s64 rtw_sptime_to_ms(const sysptime sptime);
407 sysptime rtw_ms_to_sptime(u64 ms);
408 s64 rtw_sptime_to_us(const sysptime sptime);
409 sysptime rtw_us_to_sptime(u64 us);
410 s64 rtw_sptime_to_ns(const sysptime sptime);
411 sysptime rtw_ns_to_sptime(u64 ns);
412
413 s64 rtw_sptime_diff_ms(const sysptime start, const sysptime end);
414 s64 rtw_sptime_pass_ms(const sysptime start);
415 s64 rtw_sptime_diff_us(const sysptime start, const sysptime end);
416 s64 rtw_sptime_pass_us(const sysptime start);
417 s64 rtw_sptime_diff_ns(const sysptime start, const sysptime end);
418 s64 rtw_sptime_pass_ns(const sysptime start);
419
420 extern void rtw_sleep_schedulable(int ms);
421
422 extern void rtw_msleep_os(int ms);
423 extern void rtw_usleep_os(int us);
424
425 extern u32 rtw_atoi(u8 *s);
426
427 #ifdef DBG_DELAY_OS
428 #define rtw_mdelay_os(ms) _rtw_mdelay_os((ms), __FUNCTION__, __LINE__)
429 #define rtw_udelay_os(ms) _rtw_udelay_os((ms), __FUNCTION__, __LINE__)
430 extern void _rtw_mdelay_os(int ms, const char *func, const int line);
431 extern void _rtw_udelay_os(int us, const char *func, const int line);
432 #else
433 extern void rtw_mdelay_os(int ms);
434 extern void rtw_udelay_os(int us);
435 #endif
436
437 extern void rtw_yield_os(void);
438
439 enum rtw_pwait_type {
440 RTW_PWAIT_TYPE_MSLEEP,
441 RTW_PWAIT_TYPE_USLEEP,
442 RTW_PWAIT_TYPE_YIELD,
443 RTW_PWAIT_TYPE_MDELAY,
444 RTW_PWAIT_TYPE_UDELAY,
445
446 RTW_PWAIT_TYPE_NUM,
447 };
448
449 #define RTW_PWAIT_TYPE_VALID(type) (type < RTW_PWAIT_TYPE_NUM)
450
451 struct rtw_pwait_conf {
452 enum rtw_pwait_type type;
453 s32 wait_time;
454 s32 wait_cnt_lmt;
455 };
456
457 struct rtw_pwait_ctx {
458 struct rtw_pwait_conf conf;
459 s32 wait_cnt;
460 void (*wait_hdl)(int us);
461 };
462
463 extern const char *_rtw_pwait_type_str[];
464 #define rtw_pwait_type_str(type) (RTW_PWAIT_TYPE_VALID(type) ? _rtw_pwait_type_str[type] : _rtw_pwait_type_str[RTW_PWAIT_TYPE_NUM])
465
466 #define rtw_pwctx_reset(pwctx) (pwctx)->wait_cnt = 0
467 #define rtw_pwctx_wait(pwctx) do { (pwctx)->wait_hdl((pwctx)->conf.wait_time); (pwctx)->wait_cnt++; } while(0)
468 #define rtw_pwctx_waited(pwctx) ((pwctx)->wait_cnt)
469 #define rtw_pwctx_exceed(pwctx) ((pwctx)->conf.wait_cnt_lmt >= 0 && (pwctx)->wait_cnt >= (pwctx)->conf.wait_cnt_lmt)
470
471 int rtw_pwctx_config(struct rtw_pwait_ctx *pwctx, enum rtw_pwait_type type, s32 time, s32 cnt_lmt);
472
473 extern void rtw_init_timer(_timer *ptimer, void *padapter, void *pfunc, void *ctx);
474
475
_cancel_timer_ex(_timer * ptimer)476 __inline static unsigned char _cancel_timer_ex(_timer *ptimer)
477 {
478 u8 bcancelled;
479
480 _cancel_timer(ptimer, &bcancelled);
481
482 return bcancelled;
483 }
484
thread_enter(char * name)485 static __inline void thread_enter(char *name)
486 {
487 #ifdef PLATFORM_LINUX
488 allow_signal(SIGTERM);
489 #endif
490 #ifdef PLATFORM_FREEBSD
491 printf("%s", "RTKTHREAD_enter");
492 #endif
493 }
494 void thread_exit(_completion *comp);
495 void _rtw_init_completion(_completion *comp);
496 void _rtw_wait_for_comp_timeout(_completion *comp);
497 void _rtw_wait_for_comp(_completion *comp);
498
rtw_thread_stop(_thread_hdl_ th)499 static inline bool rtw_thread_stop(_thread_hdl_ th)
500 {
501 #ifdef PLATFORM_LINUX
502 return kthread_stop(th);
503 #endif
504 }
rtw_thread_wait_stop(void)505 static inline void rtw_thread_wait_stop(void)
506 {
507 #ifdef PLATFORM_LINUX
508 #if 0
509 while (!kthread_should_stop())
510 rtw_msleep_os(10);
511 #else
512 set_current_state(TASK_INTERRUPTIBLE);
513 while (!kthread_should_stop()) {
514 schedule();
515 set_current_state(TASK_INTERRUPTIBLE);
516 }
517 __set_current_state(TASK_RUNNING);
518 #endif
519 #endif
520 }
521
flush_signals_thread(void)522 __inline static void flush_signals_thread(void)
523 {
524 #ifdef PLATFORM_LINUX
525 if (signal_pending(current))
526 flush_signals(current);
527 #endif
528 }
529
res_to_status(sint res)530 __inline static _OS_STATUS res_to_status(sint res)
531 {
532
533 #if defined(PLATFORM_LINUX) || defined (PLATFORM_MPIXEL) || defined (PLATFORM_FREEBSD)
534 return res;
535 #endif
536
537 #ifdef PLATFORM_WINDOWS
538
539 if (res == _SUCCESS)
540 return NDIS_STATUS_SUCCESS;
541 else
542 return NDIS_STATUS_FAILURE;
543
544 #endif
545
546 }
547
rtw_dump_stack(void)548 __inline static void rtw_dump_stack(void)
549 {
550 #ifdef PLATFORM_LINUX
551 dump_stack();
552 #endif
553 }
554
555 #ifdef PLATFORM_LINUX
556 #define rtw_warn_on(condition) WARN_ON(condition)
557 #else
558 #define rtw_warn_on(condition) do {} while (0)
559 #endif
560
rtw_bug_check(void * parg1,void * parg2,void * parg3,void * parg4)561 __inline static int rtw_bug_check(void *parg1, void *parg2, void *parg3, void *parg4)
562 {
563 int ret = _TRUE;
564
565 #ifdef PLATFORM_WINDOWS
566 if (((uint)parg1) <= 0x7fffffff ||
567 ((uint)parg2) <= 0x7fffffff ||
568 ((uint)parg3) <= 0x7fffffff ||
569 ((uint)parg4) <= 0x7fffffff) {
570 ret = _FALSE;
571 KeBugCheckEx(0x87110000, (ULONG_PTR)parg1, (ULONG_PTR)parg2, (ULONG_PTR)parg3, (ULONG_PTR)parg4);
572 }
573 #endif
574
575 return ret;
576
577 }
578 #ifdef PLATFORM_LINUX
579 #define RTW_DIV_ROUND_UP(n, d) DIV_ROUND_UP(n, d)
580 #else /* !PLATFORM_LINUX */
581 #define RTW_DIV_ROUND_UP(n, d) (((n) + (d - 1)) / d)
582 #endif /* !PLATFORM_LINUX */
583
584 #define _RND(sz, r) ((((sz)+((r)-1))/(r))*(r))
585 #define RND4(x) (((x >> 2) + (((x & 3) == 0) ? 0 : 1)) << 2)
586
_RND4(u32 sz)587 __inline static u32 _RND4(u32 sz)
588 {
589
590 u32 val;
591
592 val = ((sz >> 2) + ((sz & 3) ? 1 : 0)) << 2;
593
594 return val;
595
596 }
597
_RND8(u32 sz)598 __inline static u32 _RND8(u32 sz)
599 {
600
601 u32 val;
602
603 val = ((sz >> 3) + ((sz & 7) ? 1 : 0)) << 3;
604
605 return val;
606
607 }
608
_RND128(u32 sz)609 __inline static u32 _RND128(u32 sz)
610 {
611
612 u32 val;
613
614 val = ((sz >> 7) + ((sz & 127) ? 1 : 0)) << 7;
615
616 return val;
617
618 }
619
_RND256(u32 sz)620 __inline static u32 _RND256(u32 sz)
621 {
622
623 u32 val;
624
625 val = ((sz >> 8) + ((sz & 255) ? 1 : 0)) << 8;
626
627 return val;
628
629 }
630
_RND512(u32 sz)631 __inline static u32 _RND512(u32 sz)
632 {
633
634 u32 val;
635
636 val = ((sz >> 9) + ((sz & 511) ? 1 : 0)) << 9;
637
638 return val;
639
640 }
641
bitshift(u32 bitmask)642 __inline static u32 bitshift(u32 bitmask)
643 {
644 u32 i;
645
646 for (i = 0; i <= 31; i++)
647 if (((bitmask >> i) & 0x1) == 1)
648 break;
649
650 return i;
651 }
652
largest_bit(u32 bitmask)653 static inline int largest_bit(u32 bitmask)
654 {
655 int i;
656
657 for (i = 31; i >= 0; i--)
658 if (bitmask & BIT(i))
659 break;
660
661 return i;
662 }
663
largest_bit_64(u64 bitmask)664 static inline int largest_bit_64(u64 bitmask)
665 {
666 int i;
667
668 for (i = 63; i >= 0; i--)
669 if (bitmask & BIT_ULL(i))
670 break;
671
672 return i;
673 }
674
675 #define rtw_abs(a) ((a) < 0 ? -(a) : (a))
676 #define rtw_min(a, b) (((a) > (b)) ? (b) : (a))
677 #define rtw_max(a, b) (((a) > (b)) ? (a) : (b))
678 #define rtw_is_range_a_in_b(hi_a, lo_a, hi_b, lo_b) (((hi_a) <= (hi_b)) && ((lo_a) >= (lo_b)))
679 #define rtw_is_range_overlap(hi_a, lo_a, hi_b, lo_b) (((hi_a) > (lo_b)) && ((lo_a) < (hi_b)))
680
681 #ifndef MAC_FMT
682 #define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x"
683 #endif
684 #ifndef MAC_ARG
685 #define MAC_ARG(x) ((u8 *)(x))[0], ((u8 *)(x))[1], ((u8 *)(x))[2], ((u8 *)(x))[3], ((u8 *)(x))[4], ((u8 *)(x))[5]
686 #endif
687
688 bool rtw_macaddr_is_larger(const u8 *a, const u8 *b);
689
690 extern void rtw_suspend_lock_init(void);
691 extern void rtw_suspend_lock_uninit(void);
692 extern void rtw_lock_suspend(void);
693 extern void rtw_unlock_suspend(void);
694 extern void rtw_lock_suspend_timeout(u32 timeout_ms);
695 extern void rtw_lock_traffic_suspend_timeout(u32 timeout_ms);
696 extern void rtw_resume_lock_suspend(void);
697 extern void rtw_resume_unlock_suspend(void);
698 #ifdef CONFIG_AP_WOWLAN
699 extern void rtw_softap_lock_suspend(void);
700 extern void rtw_softap_unlock_suspend(void);
701 #endif
702
703 extern void rtw_set_bit(int nr, unsigned long *addr);
704 extern void rtw_clear_bit(int nr, unsigned long *addr);
705 extern int rtw_test_and_clear_bit(int nr, unsigned long *addr);
706
707 extern void ATOMIC_SET(ATOMIC_T *v, int i);
708 extern int ATOMIC_READ(ATOMIC_T *v);
709 extern void ATOMIC_ADD(ATOMIC_T *v, int i);
710 extern void ATOMIC_SUB(ATOMIC_T *v, int i);
711 extern void ATOMIC_INC(ATOMIC_T *v);
712 extern void ATOMIC_DEC(ATOMIC_T *v);
713 extern int ATOMIC_ADD_RETURN(ATOMIC_T *v, int i);
714 extern int ATOMIC_SUB_RETURN(ATOMIC_T *v, int i);
715 extern int ATOMIC_INC_RETURN(ATOMIC_T *v);
716 extern int ATOMIC_DEC_RETURN(ATOMIC_T *v);
717 extern bool ATOMIC_INC_UNLESS(ATOMIC_T *v, int u);
718
719 /* File operation APIs, just for linux now */
720 #if !defined(CONFIG_RTW_ANDROID_GKI)
721 extern int rtw_is_dir_readable(const char *path);
722 extern int rtw_store_to_file(const char *path, u8 *buf, u32 sz);
723 #endif /* !defined(CONFIG_RTW_ANDROID_GKI) */
724 extern int rtw_is_file_readable(const char *path);
725 extern int rtw_is_file_readable_with_size(const char *path, u32 *sz);
726 extern int rtw_readable_file_sz_chk(const char *path, u32 sz);
727 extern int rtw_retrieve_from_file(const char *path, u8 *buf, u32 sz);
728
729
730 #ifndef PLATFORM_FREEBSD
731 extern void rtw_free_netdev(struct net_device *netdev);
732 #endif /* PLATFORM_FREEBSD */
733
734
735 extern u64 rtw_modular64(u64 x, u64 y);
736 extern u64 rtw_division64(u64 x, u64 y);
737 extern u32 rtw_random32(void);
738
739 /* Macros for handling unaligned memory accesses */
740
741 #define RTW_GET_BE16(a) ((u16) (((a)[0] << 8) | (a)[1]))
742 #define RTW_PUT_BE16(a, val) \
743 do { \
744 (a)[0] = ((u16) (val)) >> 8; \
745 (a)[1] = ((u16) (val)) & 0xff; \
746 } while (0)
747
748 #define RTW_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0]))
749 #define RTW_PUT_LE16(a, val) \
750 do { \
751 (a)[1] = ((u16) (val)) >> 8; \
752 (a)[0] = ((u16) (val)) & 0xff; \
753 } while (0)
754
755 #define RTW_GET_BE24(a) ((((u32) (a)[0]) << 16) | (((u32) (a)[1]) << 8) | \
756 ((u32) (a)[2]))
757 #define RTW_PUT_BE24(a, val) \
758 do { \
759 (a)[0] = (u8) ((((u32) (val)) >> 16) & 0xff); \
760 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff); \
761 (a)[2] = (u8) (((u32) (val)) & 0xff); \
762 } while (0)
763
764 #define RTW_GET_BE32(a) ((((u32) (a)[0]) << 24) | (((u32) (a)[1]) << 16) | \
765 (((u32) (a)[2]) << 8) | ((u32) (a)[3]))
766 #define RTW_PUT_BE32(a, val) \
767 do { \
768 (a)[0] = (u8) ((((u32) (val)) >> 24) & 0xff); \
769 (a)[1] = (u8) ((((u32) (val)) >> 16) & 0xff); \
770 (a)[2] = (u8) ((((u32) (val)) >> 8) & 0xff); \
771 (a)[3] = (u8) (((u32) (val)) & 0xff); \
772 } while (0)
773
774 #define RTW_GET_LE32(a) ((((u32) (a)[3]) << 24) | (((u32) (a)[2]) << 16) | \
775 (((u32) (a)[1]) << 8) | ((u32) (a)[0]))
776 #define RTW_PUT_LE32(a, val) \
777 do { \
778 (a)[3] = (u8) ((((u32) (val)) >> 24) & 0xff); \
779 (a)[2] = (u8) ((((u32) (val)) >> 16) & 0xff); \
780 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff); \
781 (a)[0] = (u8) (((u32) (val)) & 0xff); \
782 } while (0)
783
784 #define RTW_GET_BE64(a) ((((u64) (a)[0]) << 56) | (((u64) (a)[1]) << 48) | \
785 (((u64) (a)[2]) << 40) | (((u64) (a)[3]) << 32) | \
786 (((u64) (a)[4]) << 24) | (((u64) (a)[5]) << 16) | \
787 (((u64) (a)[6]) << 8) | ((u64) (a)[7]))
788 #define RTW_PUT_BE64(a, val) \
789 do { \
790 (a)[0] = (u8) (((u64) (val)) >> 56); \
791 (a)[1] = (u8) (((u64) (val)) >> 48); \
792 (a)[2] = (u8) (((u64) (val)) >> 40); \
793 (a)[3] = (u8) (((u64) (val)) >> 32); \
794 (a)[4] = (u8) (((u64) (val)) >> 24); \
795 (a)[5] = (u8) (((u64) (val)) >> 16); \
796 (a)[6] = (u8) (((u64) (val)) >> 8); \
797 (a)[7] = (u8) (((u64) (val)) & 0xff); \
798 } while (0)
799
800 #define RTW_GET_LE64(a) ((((u64) (a)[7]) << 56) | (((u64) (a)[6]) << 48) | \
801 (((u64) (a)[5]) << 40) | (((u64) (a)[4]) << 32) | \
802 (((u64) (a)[3]) << 24) | (((u64) (a)[2]) << 16) | \
803 (((u64) (a)[1]) << 8) | ((u64) (a)[0]))
804 #define RTW_PUT_LE64(a, val) \
805 do { \
806 (a)[7] = (u8) ((((u64) (val)) >> 56) & 0xff); \
807 (a)[6] = (u8) ((((u64) (val)) >> 48) & 0xff); \
808 (a)[5] = (u8) ((((u64) (val)) >> 40) & 0xff); \
809 (a)[4] = (u8) ((((u64) (val)) >> 32) & 0xff); \
810 (a)[3] = (u8) ((((u64) (val)) >> 24) & 0xff); \
811 (a)[2] = (u8) ((((u64) (val)) >> 16) & 0xff); \
812 (a)[1] = (u8) ((((u64) (val)) >> 8) & 0xff); \
813 (a)[0] = (u8) (((u64) (val)) & 0xff); \
814 } while (0)
815
816 void rtw_buf_free(u8 **buf, u32 *buf_len);
817 void rtw_buf_update(u8 **buf, u32 *buf_len, const u8 *src, u32 src_len);
818
819 struct rtw_cbuf {
820 u32 write;
821 u32 read;
822 u32 size;
823 void *bufs[0];
824 };
825
826 bool rtw_cbuf_full(struct rtw_cbuf *cbuf);
827 bool rtw_cbuf_empty(struct rtw_cbuf *cbuf);
828 bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf);
829 void *rtw_cbuf_pop(struct rtw_cbuf *cbuf);
830 struct rtw_cbuf *rtw_cbuf_alloc(u32 size);
831 void rtw_cbuf_free(struct rtw_cbuf *cbuf);
832
833 struct map_seg_t {
834 u16 sa;
835 u16 len;
836 u8 *c;
837 };
838
839 struct map_t {
840 u16 len;
841 u16 seg_num;
842 u8 init_value;
843 struct map_seg_t *segs;
844 };
845
846 #define MAPSEG_ARRAY_ENT(_sa, _len, _c, arg...) \
847 { .sa = _sa, .len = _len, .c = (u8[_len]){ _c, ##arg}, }
848
849 #define MAPSEG_PTR_ENT(_sa, _len, _p) \
850 { .sa = _sa, .len = _len, .c = _p, }
851
852 #define MAP_ENT(_len, _seg_num, _init_v, _seg, arg...) \
853 { .len = _len, .seg_num = _seg_num, .init_value = _init_v, .segs = (struct map_seg_t[_seg_num]){ _seg, ##arg}, }
854
855 int map_readN(const struct map_t *map, u16 offset, u16 len, u8 *buf);
856 u8 map_read8(const struct map_t *map, u16 offset);
857
858 struct blacklist_ent {
859 _list list;
860 u8 addr[ETH_ALEN];
861 systime exp_time;
862 };
863
864 #ifdef CONFIG_RTW_MESH
865 int rtw_blacklist_add(_queue *blist, const u8 *addr, u32 timeout_ms);
866 int rtw_blacklist_del(_queue *blist, const u8 *addr);
867 int rtw_blacklist_search(_queue *blist, const u8 *addr);
868 void rtw_blacklist_flush(_queue *blist);
869 void dump_blacklist(void *sel, _queue *blist, const char *title);
870 #endif
871
872 /* String handler */
873
874 BOOLEAN is_null(char c);
875 BOOLEAN is_all_null(char *c, int len);
876 BOOLEAN is_eol(char c);
877 BOOLEAN is_space(char c);
878 BOOLEAN is_decimal(char chTmp);
879 BOOLEAN IsHexDigit(char chTmp);
880 BOOLEAN is_alpha(char chTmp);
881 char alpha_to_upper(char c);
882
883 int hex2num_i(char c);
884 int hex2byte_i(const char *hex);
885 int hexstr2bin(const char *hex, u8 *buf, size_t len);
886
887 int hwaddr_aton_i(const char *txt, u8 *addr);
888
889 /*
890 * Write formatted output to sized buffer
891 */
892 #ifdef PLATFORM_LINUX
893 #define rtw_sprintf(buf, size, format, arg...) snprintf(buf, size, format, ##arg)
894 #else /* !PLATFORM_LINUX */
895 #error "NOT DEFINE \"rtw_sprintf\"!!"
896 #endif /* !PLATFORM_LINUX */
897
898 #endif
899