osdep_service.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2007 - 2013 Realtek Corporation. All rights reserved.
  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. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  17. *
  18. *
  19. ******************************************************************************/
  20. #ifndef __OSDEP_SERVICE_H_
  21. #define __OSDEP_SERVICE_H_
  22. #define _FAIL 0
  23. #define _SUCCESS 1
  24. #define RTW_RX_HANDLED 2
  25. //#define RTW_STATUS_TIMEDOUT -110
  26. #undef _TRUE
  27. #define _TRUE 1
  28. #undef _FALSE
  29. #define _FALSE 0
  30. #ifdef PLATFORM_FREEBSD
  31. #include <osdep_service_bsd.h>
  32. #endif
  33. #ifdef PLATFORM_LINUX
  34. #include <osdep_service_linux.h>
  35. #endif
  36. #ifdef PLATFORM_OS_XP
  37. #include <osdep_service_xp.h>
  38. #endif
  39. #ifdef PLATFORM_OS_CE
  40. #include <osdep_service_ce.h>
  41. #endif
  42. #define RTW_TIMER_HDL_NAME(name) rtw_##name##_timer_hdl
  43. #define RTW_DECLARE_TIMER_HDL(name) void RTW_TIMER_HDL_NAME(name)(RTW_TIMER_HDL_ARGS)
  44. //#include <rtw_byteorder.h>
  45. #ifndef BIT
  46. #define BIT(x) ( 1 << (x))
  47. #endif
  48. #define BIT0 0x00000001
  49. #define BIT1 0x00000002
  50. #define BIT2 0x00000004
  51. #define BIT3 0x00000008
  52. #define BIT4 0x00000010
  53. #define BIT5 0x00000020
  54. #define BIT6 0x00000040
  55. #define BIT7 0x00000080
  56. #define BIT8 0x00000100
  57. #define BIT9 0x00000200
  58. #define BIT10 0x00000400
  59. #define BIT11 0x00000800
  60. #define BIT12 0x00001000
  61. #define BIT13 0x00002000
  62. #define BIT14 0x00004000
  63. #define BIT15 0x00008000
  64. #define BIT16 0x00010000
  65. #define BIT17 0x00020000
  66. #define BIT18 0x00040000
  67. #define BIT19 0x00080000
  68. #define BIT20 0x00100000
  69. #define BIT21 0x00200000
  70. #define BIT22 0x00400000
  71. #define BIT23 0x00800000
  72. #define BIT24 0x01000000
  73. #define BIT25 0x02000000
  74. #define BIT26 0x04000000
  75. #define BIT27 0x08000000
  76. #define BIT28 0x10000000
  77. #define BIT29 0x20000000
  78. #define BIT30 0x40000000
  79. #define BIT31 0x80000000
  80. #define BIT32 0x0100000000
  81. #define BIT33 0x0200000000
  82. #define BIT34 0x0400000000
  83. #define BIT35 0x0800000000
  84. #define BIT36 0x1000000000
  85. extern int RTW_STATUS_CODE(int error_code);
  86. #define CONFIG_USE_VMALLOC
  87. //flags used for rtw_update_mem_stat()
  88. enum {
  89. MEM_STAT_VIR_ALLOC_SUCCESS,
  90. MEM_STAT_VIR_ALLOC_FAIL,
  91. MEM_STAT_VIR_FREE,
  92. MEM_STAT_PHY_ALLOC_SUCCESS,
  93. MEM_STAT_PHY_ALLOC_FAIL,
  94. MEM_STAT_PHY_FREE,
  95. MEM_STAT_TX, //used to distinguish TX/RX, asigned from caller
  96. MEM_STAT_TX_ALLOC_SUCCESS,
  97. MEM_STAT_TX_ALLOC_FAIL,
  98. MEM_STAT_TX_FREE,
  99. MEM_STAT_RX, //used to distinguish TX/RX, asigned from caller
  100. MEM_STAT_RX_ALLOC_SUCCESS,
  101. MEM_STAT_RX_ALLOC_FAIL,
  102. MEM_STAT_RX_FREE
  103. };
  104. #ifdef DBG_MEM_ALLOC
  105. void rtw_update_mem_stat(u8 flag, u32 sz);
  106. void rtw_dump_mem_stat (void);
  107. extern u8* dbg_rtw_vmalloc(u32 sz, const char *func, int line);
  108. extern u8* dbg_rtw_zvmalloc(u32 sz, const char *func, int line);
  109. extern void dbg_rtw_vmfree(u8 *pbuf, u32 sz, const char *func, int line);
  110. extern u8* dbg_rtw_malloc(u32 sz, const char *func, int line);
  111. extern u8* dbg_rtw_zmalloc(u32 sz, const char *func, int line);
  112. extern void dbg_rtw_mfree(u8 *pbuf, u32 sz, const char *func, int line);
  113. #ifdef CONFIG_USE_VMALLOC
  114. #define rtw_vmalloc(sz) dbg_rtw_vmalloc((sz), __FUNCTION__, __LINE__)
  115. #define rtw_zvmalloc(sz) dbg_rtw_zvmalloc((sz), __FUNCTION__, __LINE__)
  116. #define rtw_vmfree(pbuf, sz) dbg_rtw_vmfree((pbuf), (sz), __FUNCTION__, __LINE__)
  117. #else //CONFIG_USE_VMALLOC
  118. #define rtw_vmalloc(sz) dbg_rtw_malloc((sz), __FUNCTION__, __LINE__)
  119. #define rtw_zvmalloc(sz) dbg_rtw_zmalloc((sz), __FUNCTION__, __LINE__)
  120. #define rtw_vmfree(pbuf, sz) dbg_rtw_mfree((pbuf), (sz), __FUNCTION__, __LINE__)
  121. #endif //CONFIG_USE_VMALLOC
  122. #define rtw_malloc(sz) dbg_rtw_malloc((sz), __FUNCTION__, __LINE__)
  123. #define rtw_zmalloc(sz) dbg_rtw_zmalloc((sz), __FUNCTION__, __LINE__)
  124. #define rtw_mfree(pbuf, sz) dbg_rtw_mfree((pbuf), (sz), __FUNCTION__, __LINE__)
  125. #else
  126. #define rtw_update_mem_stat(flag, sz) do {} while(0)
  127. extern u8* _rtw_vmalloc(u32 sz);
  128. extern u8* _rtw_zvmalloc(u32 sz);
  129. extern void _rtw_vmfree(u8 *pbuf, u32 sz);
  130. extern u8* _rtw_zmalloc(u32 sz);
  131. extern u8* _rtw_malloc(u32 sz);
  132. extern void _rtw_mfree(u8 *pbuf, u32 sz);
  133. #ifdef CONFIG_USE_VMALLOC
  134. #define rtw_vmalloc(sz) _rtw_vmalloc((sz))
  135. #define rtw_zvmalloc(sz) _rtw_zvmalloc((sz))
  136. #define rtw_vmfree(pbuf, sz) _rtw_vmfree((pbuf), (sz))
  137. #else //CONFIG_USE_VMALLOC
  138. #define rtw_vmalloc(sz) _rtw_malloc((sz))
  139. #define rtw_zvmalloc(sz) _rtw_zmalloc((sz))
  140. #define rtw_vmfree(pbuf, sz) _rtw_mfree((pbuf), (sz))
  141. #endif //CONFIG_USE_VMALLOC
  142. #define rtw_malloc(sz) _rtw_malloc((sz))
  143. #define rtw_zmalloc(sz) _rtw_zmalloc((sz))
  144. #define rtw_mfree(pbuf, sz) _rtw_mfree((pbuf), (sz))
  145. #endif
  146. extern void* rtw_malloc2d(int h, int w, int size);
  147. extern void rtw_mfree2d(void *pbuf, int h, int w, int size);
  148. extern void _rtw_memcpy(void* dec, void* sour, u32 sz);
  149. extern int _rtw_memcmp(void *dst, void *src, u32 sz);
  150. extern void _rtw_memset(void *pbuf, int c, u32 sz);
  151. extern void _rtw_init_listhead(_list *list);
  152. extern u32 rtw_is_list_empty(_list *phead);
  153. extern void rtw_list_insert_head(_list *plist, _list *phead);
  154. extern void rtw_list_insert_tail(_list *plist, _list *phead);
  155. #ifndef PLATFORM_FREEBSD
  156. extern void rtw_list_delete(_list *plist);
  157. #endif //PLATFORM_FREEBSD
  158. extern void _rtw_init_sema(_sema *sema, int init_val);
  159. extern void _rtw_free_sema(_sema *sema);
  160. extern void _rtw_up_sema(_sema *sema);
  161. extern u32 _rtw_down_sema(_sema *sema);
  162. extern void _rtw_mutex_init(_mutex *pmutex);
  163. extern void _rtw_mutex_free(_mutex *pmutex);
  164. #ifndef PLATFORM_FREEBSD
  165. extern void _rtw_spinlock_init(_lock *plock);
  166. #endif //PLATFORM_FREEBSD
  167. extern void _rtw_spinlock_free(_lock *plock);
  168. extern void _rtw_spinlock(_lock *plock);
  169. extern void _rtw_spinunlock(_lock *plock);
  170. extern void _rtw_spinlock_ex(_lock *plock);
  171. extern void _rtw_spinunlock_ex(_lock *plock);
  172. extern void _rtw_init_queue(_queue *pqueue);
  173. extern u32 _rtw_queue_empty(_queue *pqueue);
  174. extern u32 rtw_end_of_queue_search(_list *queue, _list *pelement);
  175. extern u32 rtw_get_current_time(void);
  176. extern u32 rtw_systime_to_ms(u32 systime);
  177. extern u32 rtw_ms_to_systime(u32 ms);
  178. extern s32 rtw_get_passing_time_ms(u32 start);
  179. extern s32 rtw_get_time_interval_ms(u32 start, u32 end);
  180. extern void rtw_sleep_schedulable(int ms);
  181. extern void rtw_msleep_os(int ms);
  182. extern void rtw_usleep_os(int us);
  183. extern u32 rtw_atoi(u8* s);
  184. #ifdef DBG_DELAY_OS
  185. #define rtw_mdelay_os(ms) _rtw_mdelay_os((ms), __FUNCTION__, __LINE__)
  186. #define rtw_udelay_os(ms) _rtw_udelay_os((ms), __FUNCTION__, __LINE__)
  187. extern void _rtw_mdelay_os(int ms, const char *func, const int line);
  188. extern void _rtw_udelay_os(int us, const char *func, const int line);
  189. #else
  190. extern void rtw_mdelay_os(int ms);
  191. extern void rtw_udelay_os(int us);
  192. #endif
  193. extern void rtw_yield_os(void);
  194. extern void rtw_init_timer(_timer *ptimer, void *padapter, void *pfunc);
  195. __inline static unsigned char _cancel_timer_ex(_timer *ptimer)
  196. {
  197. #ifdef PLATFORM_LINUX
  198. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 15, 0))
  199. return del_timer_sync(&ptimer->t);
  200. #else
  201. return del_timer_sync(ptimer);
  202. #endif
  203. #endif
  204. #ifdef PLATFORM_FREEBSD
  205. _cancel_timer(ptimer,0);
  206. return 0;
  207. #endif
  208. #ifdef PLATFORM_WINDOWS
  209. u8 bcancelled;
  210. _cancel_timer(ptimer, &bcancelled);
  211. return bcancelled;
  212. #endif
  213. }
  214. static __inline void thread_enter(char *name)
  215. {
  216. #ifdef PLATFORM_LINUX
  217. #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 8, 0))
  218. daemonize("%s", name);
  219. #endif
  220. allow_signal(SIGTERM);
  221. #endif
  222. #ifdef PLATFORM_FREEBSD
  223. printf("%s", "RTKTHREAD_enter");
  224. #endif
  225. }
  226. __inline static void flush_signals_thread(void)
  227. {
  228. #ifdef PLATFORM_LINUX
  229. if (signal_pending (current))
  230. {
  231. flush_signals(current);
  232. }
  233. #endif
  234. }
  235. __inline static _OS_STATUS res_to_status(sint res)
  236. {
  237. #if defined (PLATFORM_LINUX) || defined (PLATFORM_MPIXEL) || defined (PLATFORM_FREEBSD)
  238. return res;
  239. #endif
  240. #ifdef PLATFORM_WINDOWS
  241. if (res == _SUCCESS)
  242. return NDIS_STATUS_SUCCESS;
  243. else
  244. return NDIS_STATUS_FAILURE;
  245. #endif
  246. }
  247. __inline static void rtw_dump_stack(void)
  248. {
  249. #ifdef PLATFORM_LINUX
  250. dump_stack();
  251. #endif
  252. }
  253. __inline static int rtw_bug_check(void *parg1, void *parg2, void *parg3, void *parg4)
  254. {
  255. int ret = _TRUE;
  256. #ifdef PLATFORM_WINDOWS
  257. if ( ((uint)parg1) <= 0x7fffffff ||
  258. ((uint)parg2) <= 0x7fffffff ||
  259. ((uint)parg3) <= 0x7fffffff ||
  260. ((uint)parg4) <= 0x7fffffff)
  261. {
  262. ret = _FALSE;
  263. KeBugCheckEx(0x87110000, (ULONG_PTR)parg1, (ULONG_PTR)parg2, (ULONG_PTR)parg3, (ULONG_PTR)parg4);
  264. }
  265. #endif
  266. return ret;
  267. }
  268. #define _RND(sz, r) ((((sz)+((r)-1))/(r))*(r))
  269. #define RND4(x) (((x >> 2) + (((x & 3) == 0) ? 0: 1)) << 2)
  270. __inline static u32 _RND4(u32 sz)
  271. {
  272. u32 val;
  273. val = ((sz >> 2) + ((sz & 3) ? 1: 0)) << 2;
  274. return val;
  275. }
  276. __inline static u32 _RND8(u32 sz)
  277. {
  278. u32 val;
  279. val = ((sz >> 3) + ((sz & 7) ? 1: 0)) << 3;
  280. return val;
  281. }
  282. __inline static u32 _RND128(u32 sz)
  283. {
  284. u32 val;
  285. val = ((sz >> 7) + ((sz & 127) ? 1: 0)) << 7;
  286. return val;
  287. }
  288. __inline static u32 _RND256(u32 sz)
  289. {
  290. u32 val;
  291. val = ((sz >> 8) + ((sz & 255) ? 1: 0)) << 8;
  292. return val;
  293. }
  294. __inline static u32 _RND512(u32 sz)
  295. {
  296. u32 val;
  297. val = ((sz >> 9) + ((sz & 511) ? 1: 0)) << 9;
  298. return val;
  299. }
  300. __inline static u32 bitshift(u32 bitmask)
  301. {
  302. u32 i;
  303. for (i = 0; i <= 31; i++)
  304. if (((bitmask>>i) & 0x1) == 1) break;
  305. return i;
  306. }
  307. #ifndef MAC_FMT
  308. #define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x"
  309. #endif
  310. #ifndef MAC_ARG
  311. #define MAC_ARG(x) ((u8*)(x))[0],((u8*)(x))[1],((u8*)(x))[2],((u8*)(x))[3],((u8*)(x))[4],((u8*)(x))[5]
  312. #endif
  313. extern void rtw_suspend_lock_init(void);
  314. extern void rtw_suspend_lock_uninit(void);
  315. extern void rtw_lock_suspend(void);
  316. extern void rtw_unlock_suspend(void);
  317. #ifdef CONFIG_WOWLAN
  318. extern void rtw_lock_suspend_timeout(long timeout);
  319. #endif //CONFIG_WOWLAN
  320. extern void ATOMIC_SET(ATOMIC_T *v, int i);
  321. extern int ATOMIC_READ(ATOMIC_T *v);
  322. extern void ATOMIC_ADD(ATOMIC_T *v, int i);
  323. extern void ATOMIC_SUB(ATOMIC_T *v, int i);
  324. extern void ATOMIC_INC(ATOMIC_T *v);
  325. extern void ATOMIC_DEC(ATOMIC_T *v);
  326. extern int ATOMIC_ADD_RETURN(ATOMIC_T *v, int i);
  327. extern int ATOMIC_SUB_RETURN(ATOMIC_T *v, int i);
  328. extern int ATOMIC_INC_RETURN(ATOMIC_T *v);
  329. extern int ATOMIC_DEC_RETURN(ATOMIC_T *v);
  330. //File operation APIs, just for linux now
  331. extern int rtw_is_file_readable(char *path);
  332. extern int rtw_retrive_from_file(char *path, u8* buf, u32 sz);
  333. extern int rtw_store_to_file(char *path, u8* buf, u32 sz);
  334. #ifndef PLATFORM_FREEBSD
  335. extern void rtw_free_netdev(struct net_device * netdev);
  336. #endif //PLATFORM_FREEBSD
  337. extern u64 rtw_modular64(u64 x, u64 y);
  338. extern u64 rtw_division64(u64 x, u64 y);
  339. /* Macros for handling unaligned memory accesses */
  340. #define RTW_GET_BE16(a) ((u16) (((a)[0] << 8) | (a)[1]))
  341. #define RTW_PUT_BE16(a, val) \
  342. do { \
  343. (a)[0] = ((u16) (val)) >> 8; \
  344. (a)[1] = ((u16) (val)) & 0xff; \
  345. } while (0)
  346. #define RTW_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0]))
  347. #define RTW_PUT_LE16(a, val) \
  348. do { \
  349. (a)[1] = ((u16) (val)) >> 8; \
  350. (a)[0] = ((u16) (val)) & 0xff; \
  351. } while (0)
  352. #define RTW_GET_BE24(a) ((((u32) (a)[0]) << 16) | (((u32) (a)[1]) << 8) | \
  353. ((u32) (a)[2]))
  354. #define RTW_PUT_BE24(a, val) \
  355. do { \
  356. (a)[0] = (u8) ((((u32) (val)) >> 16) & 0xff); \
  357. (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff); \
  358. (a)[2] = (u8) (((u32) (val)) & 0xff); \
  359. } while (0)
  360. #define RTW_GET_BE32(a) ((((u32) (a)[0]) << 24) | (((u32) (a)[1]) << 16) | \
  361. (((u32) (a)[2]) << 8) | ((u32) (a)[3]))
  362. #define RTW_PUT_BE32(a, val) \
  363. do { \
  364. (a)[0] = (u8) ((((u32) (val)) >> 24) & 0xff); \
  365. (a)[1] = (u8) ((((u32) (val)) >> 16) & 0xff); \
  366. (a)[2] = (u8) ((((u32) (val)) >> 8) & 0xff); \
  367. (a)[3] = (u8) (((u32) (val)) & 0xff); \
  368. } while (0)
  369. #define RTW_GET_LE32(a) ((((u32) (a)[3]) << 24) | (((u32) (a)[2]) << 16) | \
  370. (((u32) (a)[1]) << 8) | ((u32) (a)[0]))
  371. #define RTW_PUT_LE32(a, val) \
  372. do { \
  373. (a)[3] = (u8) ((((u32) (val)) >> 24) & 0xff); \
  374. (a)[2] = (u8) ((((u32) (val)) >> 16) & 0xff); \
  375. (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff); \
  376. (a)[0] = (u8) (((u32) (val)) & 0xff); \
  377. } while (0)
  378. #define RTW_GET_BE64(a) ((((u64) (a)[0]) << 56) | (((u64) (a)[1]) << 48) | \
  379. (((u64) (a)[2]) << 40) | (((u64) (a)[3]) << 32) | \
  380. (((u64) (a)[4]) << 24) | (((u64) (a)[5]) << 16) | \
  381. (((u64) (a)[6]) << 8) | ((u64) (a)[7]))
  382. #define RTW_PUT_BE64(a, val) \
  383. do { \
  384. (a)[0] = (u8) (((u64) (val)) >> 56); \
  385. (a)[1] = (u8) (((u64) (val)) >> 48); \
  386. (a)[2] = (u8) (((u64) (val)) >> 40); \
  387. (a)[3] = (u8) (((u64) (val)) >> 32); \
  388. (a)[4] = (u8) (((u64) (val)) >> 24); \
  389. (a)[5] = (u8) (((u64) (val)) >> 16); \
  390. (a)[6] = (u8) (((u64) (val)) >> 8); \
  391. (a)[7] = (u8) (((u64) (val)) & 0xff); \
  392. } while (0)
  393. #define RTW_GET_LE64(a) ((((u64) (a)[7]) << 56) | (((u64) (a)[6]) << 48) | \
  394. (((u64) (a)[5]) << 40) | (((u64) (a)[4]) << 32) | \
  395. (((u64) (a)[3]) << 24) | (((u64) (a)[2]) << 16) | \
  396. (((u64) (a)[1]) << 8) | ((u64) (a)[0]))
  397. void rtw_buf_free(u8 **buf, u32 *buf_len);
  398. void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len);
  399. struct rtw_cbuf {
  400. u32 write;
  401. u32 read;
  402. u32 size;
  403. void *bufs[0];
  404. };
  405. bool rtw_cbuf_full(struct rtw_cbuf *cbuf);
  406. bool rtw_cbuf_empty(struct rtw_cbuf *cbuf);
  407. bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf);
  408. void *rtw_cbuf_pop(struct rtw_cbuf *cbuf);
  409. struct rtw_cbuf *rtw_cbuf_alloc(u32 size);
  410. void rtw_cbuf_free(struct rtw_cbuf *cbuf);
  411. #endif