1 /******************************************************************************
3 * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
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.
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
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
19 ******************************************************************************/
22 #define _OSDEP_SERVICE_C_
25 #include <osdep_service.h>
26 #include <drv_types.h>
27 #include <recv_osdep.h>
29 #include <linux/vmalloc.h>
31 #ifdef PLATFORM_FREEBSD
32 #include <sys/malloc.h>
34 #endif /* PLATFORM_FREEBSD */
35 #ifdef RTK_DMP_PLATFORM
36 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,12))
37 #include <linux/pageremap.h>
43 #ifdef DBG_MEMORY_LEAK
45 #include <asm/atomic.h>
46 atomic_t _malloc_cnt = ATOMIC_INIT(0);
47 atomic_t _malloc_size = ATOMIC_INIT(0);
49 #endif /* DBG_MEMORY_LEAK */
52 #if defined(PLATFORM_LINUX)
54 * Translate the OS dependent @param error_code to OS independent RTW_STATUS_CODE
55 * @return: one of RTW_STATUS_CODE
57 inline int RTW_STATUS_CODE(int error_code){
63 // return RTW_STATUS_TIMEDOUT;
69 inline int RTW_STATUS_CODE(int error_code){
79 for(i=0;i<=strlen(s);i++)
81 if(s[i] >= '0' && s[i] <= '9')
82 num = num * 10 + s[i] -'0';
83 else if(s[0] == '-' && i==0)
96 inline u8* _rtw_vmalloc(u32 sz)
102 #ifdef PLATFORM_FREEBSD
103 pbuf = malloc(sz,M_DEVBUF,M_NOWAIT);
106 #ifdef PLATFORM_WINDOWS
107 NdisAllocateMemoryWithTag(&pbuf,sz, RT_TAG);
110 #ifdef DBG_MEMORY_LEAK
111 #ifdef PLATFORM_LINUX
113 atomic_inc(&_malloc_cnt);
114 atomic_add(sz, &_malloc_size);
117 #endif /* DBG_MEMORY_LEAK */
122 inline u8* _rtw_zvmalloc(u32 sz)
125 #ifdef PLATFORM_LINUX
126 pbuf = _rtw_vmalloc(sz);
130 #ifdef PLATFORM_FREEBSD
131 pbuf = malloc(sz,M_DEVBUF,M_ZERO|M_NOWAIT);
133 #ifdef PLATFORM_WINDOWS
134 NdisAllocateMemoryWithTag(&pbuf,sz, RT_TAG);
136 NdisFillMemory(pbuf, sz, 0);
142 inline void _rtw_vmfree(u8 *pbuf, u32 sz)
144 #ifdef PLATFORM_LINUX
147 #ifdef PLATFORM_FREEBSD
150 #ifdef PLATFORM_WINDOWS
151 NdisFreeMemory(pbuf,sz, 0);
154 #ifdef DBG_MEMORY_LEAK
155 #ifdef PLATFORM_LINUX
156 atomic_dec(&_malloc_cnt);
157 atomic_sub(sz, &_malloc_size);
159 #endif /* DBG_MEMORY_LEAK */
162 u8* _rtw_malloc(u32 sz)
167 #ifdef PLATFORM_LINUX
168 #ifdef RTK_DMP_PLATFORM
170 pbuf = (u8 *)dvr_malloc(sz);
173 pbuf = kmalloc(sz,in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
176 #ifdef PLATFORM_FREEBSD
177 pbuf = malloc(sz,M_DEVBUF,M_NOWAIT);
179 #ifdef PLATFORM_WINDOWS
181 NdisAllocateMemoryWithTag(&pbuf,sz, RT_TAG);
185 #ifdef DBG_MEMORY_LEAK
186 #ifdef PLATFORM_LINUX
188 atomic_inc(&_malloc_cnt);
189 atomic_add(sz, &_malloc_size);
192 #endif /* DBG_MEMORY_LEAK */
199 u8* _rtw_zmalloc(u32 sz)
201 #ifdef PLATFORM_FREEBSD
202 return malloc(sz,M_DEVBUF,M_ZERO|M_NOWAIT);
203 #else // PLATFORM_FREEBSD
204 u8 *pbuf = _rtw_malloc(sz);
208 #ifdef PLATFORM_LINUX
212 #ifdef PLATFORM_WINDOWS
213 NdisFillMemory(pbuf, sz, 0);
219 #endif // PLATFORM_FREEBSD
222 void _rtw_mfree(u8 *pbuf, u32 sz)
225 #ifdef PLATFORM_LINUX
226 #ifdef RTK_DMP_PLATFORM
234 #ifdef PLATFORM_FREEBSD
237 #ifdef PLATFORM_WINDOWS
239 NdisFreeMemory(pbuf,sz, 0);
243 #ifdef DBG_MEMORY_LEAK
244 #ifdef PLATFORM_LINUX
245 atomic_dec(&_malloc_cnt);
246 atomic_sub(sz, &_malloc_size);
248 #endif /* DBG_MEMORY_LEAK */
254 struct rtw_dbg_mem_stat {
255 ATOMIC_T vir_alloc; // the memory bytes we allocate now
256 ATOMIC_T vir_peak; // the peak memory bytes we allocate
257 ATOMIC_T vir_alloc_err; // the error times we fail to allocate memory
261 ATOMIC_T phy_alloc_err;
265 ATOMIC_T tx_alloc_err;
269 ATOMIC_T rx_alloc_err;
272 void rtw_dump_mem_stat (void)
274 int vir_alloc, vir_peak, vir_alloc_err, phy_alloc, phy_peak, phy_alloc_err;
275 int tx_alloc, tx_peak, tx_alloc_err, rx_alloc, rx_peak, rx_alloc_err;
277 vir_alloc=ATOMIC_READ(&rtw_dbg_mem_stat.vir_alloc);
278 vir_peak=ATOMIC_READ(&rtw_dbg_mem_stat.vir_peak);
279 vir_alloc_err=ATOMIC_READ(&rtw_dbg_mem_stat.vir_alloc_err);
281 phy_alloc=ATOMIC_READ(&rtw_dbg_mem_stat.phy_alloc);
282 phy_peak=ATOMIC_READ(&rtw_dbg_mem_stat.phy_peak);
283 phy_alloc_err=ATOMIC_READ(&rtw_dbg_mem_stat.phy_alloc_err);
285 tx_alloc=ATOMIC_READ(&rtw_dbg_mem_stat.tx_alloc);
286 tx_peak=ATOMIC_READ(&rtw_dbg_mem_stat.tx_peak);
287 tx_alloc_err=ATOMIC_READ(&rtw_dbg_mem_stat.tx_alloc_err);
289 rx_alloc=ATOMIC_READ(&rtw_dbg_mem_stat.rx_alloc);
290 rx_peak=ATOMIC_READ(&rtw_dbg_mem_stat.rx_peak);
291 rx_alloc_err=ATOMIC_READ(&rtw_dbg_mem_stat.rx_alloc_err);
293 DBG_8723A( "vir_alloc:%d, vir_peak:%d, vir_alloc_err:%d\n"
294 "phy_alloc:%d, phy_peak:%d, phy_alloc_err:%d\n"
295 "tx_alloc:%d, tx_peak:%d, tx_alloc_err:%d\n"
296 "rx_alloc:%d, rx_peak:%d, rx_alloc_err:%d\n"
297 , vir_alloc, vir_peak, vir_alloc_err
298 , phy_alloc, phy_peak, phy_alloc_err
299 , tx_alloc, tx_peak, tx_alloc_err
300 , rx_alloc, rx_peak, rx_alloc_err
304 void rtw_update_mem_stat(u8 flag, u32 sz)
306 static u32 update_time = 0;
310 ATOMIC_SET(&rtw_dbg_mem_stat.vir_alloc,0);
311 ATOMIC_SET(&rtw_dbg_mem_stat.vir_peak,0);
312 ATOMIC_SET(&rtw_dbg_mem_stat.vir_alloc_err,0);
313 ATOMIC_SET(&rtw_dbg_mem_stat.phy_alloc,0);
314 ATOMIC_SET(&rtw_dbg_mem_stat.phy_peak,0);
315 ATOMIC_SET(&rtw_dbg_mem_stat.phy_alloc_err,0);
319 case MEM_STAT_VIR_ALLOC_SUCCESS:
320 alloc = ATOMIC_ADD_RETURN(&rtw_dbg_mem_stat.vir_alloc, sz);
321 peak=ATOMIC_READ(&rtw_dbg_mem_stat.vir_peak);
323 ATOMIC_SET(&rtw_dbg_mem_stat.vir_peak, alloc);
326 case MEM_STAT_VIR_ALLOC_FAIL:
327 ATOMIC_INC(&rtw_dbg_mem_stat.vir_alloc_err);
330 case MEM_STAT_VIR_FREE:
331 alloc = ATOMIC_SUB_RETURN(&rtw_dbg_mem_stat.vir_alloc, sz);
334 case MEM_STAT_PHY_ALLOC_SUCCESS:
335 alloc = ATOMIC_ADD_RETURN(&rtw_dbg_mem_stat.phy_alloc, sz);
336 peak=ATOMIC_READ(&rtw_dbg_mem_stat.phy_peak);
338 ATOMIC_SET(&rtw_dbg_mem_stat.phy_peak, alloc);
341 case MEM_STAT_PHY_ALLOC_FAIL:
342 ATOMIC_INC(&rtw_dbg_mem_stat.phy_alloc_err);
345 case MEM_STAT_PHY_FREE:
346 alloc = ATOMIC_SUB_RETURN(&rtw_dbg_mem_stat.phy_alloc, sz);
349 case MEM_STAT_TX_ALLOC_SUCCESS:
350 alloc = ATOMIC_ADD_RETURN(&rtw_dbg_mem_stat.tx_alloc, sz);
351 peak=ATOMIC_READ(&rtw_dbg_mem_stat.tx_peak);
353 ATOMIC_SET(&rtw_dbg_mem_stat.tx_peak, alloc);
356 case MEM_STAT_TX_ALLOC_FAIL:
357 ATOMIC_INC(&rtw_dbg_mem_stat.tx_alloc_err);
360 case MEM_STAT_TX_FREE:
361 alloc = ATOMIC_SUB_RETURN(&rtw_dbg_mem_stat.tx_alloc, sz);
364 case MEM_STAT_RX_ALLOC_SUCCESS:
365 alloc = ATOMIC_ADD_RETURN(&rtw_dbg_mem_stat.rx_alloc, sz);
366 peak=ATOMIC_READ(&rtw_dbg_mem_stat.rx_peak);
368 ATOMIC_SET(&rtw_dbg_mem_stat.rx_peak, alloc);
371 case MEM_STAT_RX_ALLOC_FAIL:
372 ATOMIC_INC(&rtw_dbg_mem_stat.rx_alloc_err);
375 case MEM_STAT_RX_FREE:
376 alloc = ATOMIC_SUB_RETURN(&rtw_dbg_mem_stat.rx_alloc, sz);
381 if (rtw_get_passing_time_ms(update_time) > 5000) {
383 update_time=rtw_get_current_time();
390 inline u8* dbg_rtw_vmalloc(u32 sz, const char *func, int line)
393 //DBG_8723A("DBG_MEM_ALLOC %s:%d %s(%d)\n", func, line, __FUNCTION__, (sz));
395 p=_rtw_vmalloc((sz));
398 p ? MEM_STAT_VIR_ALLOC_SUCCESS : MEM_STAT_VIR_ALLOC_FAIL
405 inline u8* dbg_rtw_zvmalloc(u32 sz, const char *func, int line)
408 //DBG_8723A("DBG_MEM_ALLOC %s:%d %s(%d)\n", func, line, __FUNCTION__, (sz));
410 p=_rtw_zvmalloc((sz));
413 p ? MEM_STAT_VIR_ALLOC_SUCCESS : MEM_STAT_VIR_ALLOC_FAIL
420 inline void dbg_rtw_vmfree(u8 *pbuf, u32 sz, const char *func, int line)
422 //DBG_8723A("DBG_MEM_ALLOC %s:%d %s(%p,%d)\n", func, line, __FUNCTION__, (pbuf), (sz));
424 _rtw_vmfree((pbuf), (sz));
433 inline u8* dbg_rtw_malloc(u32 sz, const char *func, int line)
438 DBG_8723A("DBG_MEM_ALLOC !!!!!!!!!!!!!! %s:%d %s(%d)\n", func, line, __FUNCTION__, (sz));
443 p ? MEM_STAT_PHY_ALLOC_SUCCESS : MEM_STAT_PHY_ALLOC_FAIL
450 inline u8* dbg_rtw_zmalloc(u32 sz, const char *func, int line)
455 DBG_8723A("DBG_MEM_ALLOC !!!!!!!!!!!!!! %s:%d %s(%d)\n", func, line, __FUNCTION__, (sz));
457 p = _rtw_zmalloc((sz));
460 p ? MEM_STAT_PHY_ALLOC_SUCCESS : MEM_STAT_PHY_ALLOC_FAIL
468 inline void dbg_rtw_mfree(u8 *pbuf, u32 sz, const char *func, int line)
471 DBG_8723A("DBG_MEM_ALLOC !!!!!!!!!!!!!! %s:%d %s(%p,%d)\n", func, line, __FUNCTION__, (pbuf), (sz));
473 _rtw_mfree((pbuf), (sz));
482 void _rtw_memcpy(void* dst, void* src, u32 sz)
485 #if defined (PLATFORM_LINUX)|| defined (PLATFORM_FREEBSD)
487 memcpy(dst, src, sz);
491 #ifdef PLATFORM_WINDOWS
493 NdisMoveMemory(dst, src, sz);
499 int _rtw_memcmp(void *dst, void *src, u32 sz)
502 #if defined (PLATFORM_LINUX)|| defined (PLATFORM_FREEBSD)
503 //under Linux/GNU/GLibc, the return value of memcmp for two same mem. chunk is 0
505 if (!(memcmp(dst, src, sz)))
512 #ifdef PLATFORM_WINDOWS
513 //under Windows, the return value of NdisEqualMemory for two same mem. chunk is 1
515 if (NdisEqualMemory (dst, src, sz))
526 void _rtw_memset(void *pbuf, int c, u32 sz)
529 #if defined (PLATFORM_LINUX)|| defined (PLATFORM_FREEBSD)
535 #ifdef PLATFORM_WINDOWS
537 NdisZeroMemory(pbuf, sz);
538 if (c != 0) memset(pbuf, c, sz);
540 NdisFillMemory(pbuf, sz, c);
546 #ifdef PLATFORM_FREEBSD
548 static inline void __list_add(_list *pnew, _list *pprev, _list *pnext)
557 struct sk_buff * dev_alloc_skb(unsigned int size)
559 struct sk_buff *skb=NULL;
562 //skb = (struct sk_buff *)_rtw_zmalloc(sizeof(struct sk_buff)); // for skb->len, etc.
563 skb = (struct sk_buff *)_rtw_malloc(sizeof(struct sk_buff));
566 data = _rtw_malloc(size);
570 skb->head = (unsigned char*)data;
571 skb->data = (unsigned char*)data;
572 skb->tail = (unsigned char*)data;
573 skb->end = (unsigned char*)data + size;
575 //printf("%s()-%d: skb=%p, skb->head = %p\n", __FUNCTION__, __LINE__, skb, skb->head);
580 _rtw_mfree((u8 *)skb, sizeof(struct sk_buff));
586 void dev_kfree_skb_any(struct sk_buff *skb)
588 //printf("%s()-%d: skb->head = %p\n", __FUNCTION__, __LINE__, skb->head);
590 _rtw_mfree(skb->head, 0);
591 //printf("%s()-%d: skb = %p\n", __FUNCTION__, __LINE__, skb);
593 _rtw_mfree((u8 *)skb, 0);
595 struct sk_buff *skb_clone(const struct sk_buff *skb)
603 void _rtw_init_listhead(_list *list)
606 #ifdef PLATFORM_LINUX
608 INIT_LIST_HEAD(list);
612 #ifdef PLATFORM_FREEBSD
616 #ifdef PLATFORM_WINDOWS
618 NdisInitializeListHead(list);
626 For the following list_xxx operations,
627 caller must guarantee the atomic context.
628 Otherwise, there will be racing condition.
630 u32 rtw_is_list_empty(_list *phead)
633 #ifdef PLATFORM_LINUX
635 if (list_empty(phead))
641 #ifdef PLATFORM_FREEBSD
643 if (phead->next == phead)
651 #ifdef PLATFORM_WINDOWS
653 if (IsListEmpty(phead))
663 void rtw_list_insert_head(_list *plist, _list *phead)
666 #ifdef PLATFORM_LINUX
667 list_add(plist, phead);
670 #ifdef PLATFORM_FREEBSD
671 __list_add(plist, phead, phead->next);
674 #ifdef PLATFORM_WINDOWS
675 InsertHeadList(phead, plist);
679 void rtw_list_insert_tail(_list *plist, _list *phead)
682 #ifdef PLATFORM_LINUX
684 list_add_tail(plist, phead);
687 #ifdef PLATFORM_FREEBSD
689 __list_add(plist, phead->prev, phead);
692 #ifdef PLATFORM_WINDOWS
694 InsertTailList(phead, plist);
703 Caller must check if the list is empty before calling rtw_list_delete
708 void _rtw_init_sema(_sema *sema, int init_val)
711 #ifdef PLATFORM_LINUX
713 sema_init(sema, init_val);
716 #ifdef PLATFORM_FREEBSD
717 sema_init(sema, init_val, "rtw_drv");
719 #ifdef PLATFORM_OS_XP
721 KeInitializeSemaphore(sema, init_val, SEMA_UPBND); // count=0;
725 #ifdef PLATFORM_OS_CE
727 *sema = CreateSemaphore(NULL, init_val, SEMA_UPBND, NULL);
732 void _rtw_free_sema(_sema *sema)
734 #ifdef PLATFORM_FREEBSD
737 #ifdef PLATFORM_OS_CE
743 void _rtw_up_sema(_sema *sema)
746 #ifdef PLATFORM_LINUX
751 #ifdef PLATFORM_FREEBSD
754 #ifdef PLATFORM_OS_XP
756 KeReleaseSemaphore(sema, IO_NETWORK_INCREMENT, 1, FALSE );
760 #ifdef PLATFORM_OS_CE
761 ReleaseSemaphore(*sema, 1, NULL );
765 u32 _rtw_down_sema(_sema *sema)
768 #ifdef PLATFORM_LINUX
770 if (down_interruptible(sema))
776 #ifdef PLATFORM_FREEBSD
780 #ifdef PLATFORM_OS_XP
782 if(STATUS_SUCCESS == KeWaitForSingleObject(sema, Executive, KernelMode, TRUE, NULL))
788 #ifdef PLATFORM_OS_CE
789 if(WAIT_OBJECT_0 == WaitForSingleObject(*sema, INFINITE ))
798 void _rtw_mutex_init(_mutex *pmutex)
800 #ifdef PLATFORM_LINUX
802 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37))
809 #ifdef PLATFORM_FREEBSD
810 mtx_init(pmutex, "", NULL, MTX_DEF|MTX_RECURSE);
812 #ifdef PLATFORM_OS_XP
814 KeInitializeMutex(pmutex, 0);
818 #ifdef PLATFORM_OS_CE
819 *pmutex = CreateMutex( NULL, _FALSE, NULL);
823 void _rtw_mutex_free(_mutex *pmutex);
824 void _rtw_mutex_free(_mutex *pmutex)
826 #ifdef PLATFORM_LINUX
828 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37))
829 mutex_destroy(pmutex);
833 #ifdef PLATFORM_FREEBSD
834 sema_destroy(pmutex);
839 #ifdef PLATFORM_OS_XP
843 #ifdef PLATFORM_OS_CE
848 void _rtw_spinlock_init(_lock *plock)
851 #ifdef PLATFORM_LINUX
853 spin_lock_init(plock);
856 #ifdef PLATFORM_FREEBSD
857 mtx_init(plock, "", NULL, MTX_DEF|MTX_RECURSE);
859 #ifdef PLATFORM_WINDOWS
861 NdisAllocateSpinLock(plock);
867 void _rtw_spinlock_free(_lock *plock)
869 #ifdef PLATFORM_FREEBSD
873 #ifdef PLATFORM_WINDOWS
875 NdisFreeSpinLock(plock);
880 #ifdef PLATFORM_FREEBSD
881 extern PADAPTER prtw_lock;
883 void rtw_mtx_lock(_lock *plock){
885 mtx_lock(&prtw_lock->glock);
888 printf("%s prtw_lock==NULL",__FUNCTION__);
891 void rtw_mtx_unlock(_lock *plock){
893 mtx_unlock(&prtw_lock->glock);
896 printf("%s prtw_lock==NULL",__FUNCTION__);
900 #endif //PLATFORM_FREEBSD
903 void _rtw_spinlock(_lock *plock)
906 #ifdef PLATFORM_LINUX
911 #ifdef PLATFORM_FREEBSD
914 #ifdef PLATFORM_WINDOWS
916 NdisAcquireSpinLock(plock);
922 void _rtw_spinunlock(_lock *plock)
925 #ifdef PLATFORM_LINUX
930 #ifdef PLATFORM_FREEBSD
933 #ifdef PLATFORM_WINDOWS
935 NdisReleaseSpinLock(plock);
941 void _rtw_spinlock_ex(_lock *plock)
944 #ifdef PLATFORM_LINUX
949 #ifdef PLATFORM_FREEBSD
952 #ifdef PLATFORM_WINDOWS
954 NdisDprAcquireSpinLock(plock);
960 void _rtw_spinunlock_ex(_lock *plock)
963 #ifdef PLATFORM_LINUX
968 #ifdef PLATFORM_FREEBSD
971 #ifdef PLATFORM_WINDOWS
973 NdisDprReleaseSpinLock(plock);
980 void _rtw_init_queue(_queue *pqueue)
983 _rtw_init_listhead(&(pqueue->queue));
985 _rtw_spinlock_init(&(pqueue->lock));
989 u32 _rtw_queue_empty(_queue *pqueue)
991 return (rtw_is_list_empty(&(pqueue->queue)));
995 u32 rtw_end_of_queue_search(_list *head, _list *plist)
1004 u32 rtw_get_current_time(void)
1007 #ifdef PLATFORM_LINUX
1010 #ifdef PLATFORM_FREEBSD
1015 #ifdef PLATFORM_WINDOWS
1016 LARGE_INTEGER SystemTime;
1017 NdisGetCurrentSystemTime(&SystemTime);
1018 return (u32)(SystemTime.LowPart);// count of 100-nanosecond intervals
1022 inline u32 rtw_systime_to_ms(u32 systime)
1024 #ifdef PLATFORM_LINUX
1025 return systime * 1000 / HZ;
1027 #ifdef PLATFORM_FREEBSD
1028 return systime * 1000;
1030 #ifdef PLATFORM_WINDOWS
1031 return systime / 10000 ;
1035 inline u32 rtw_ms_to_systime(u32 ms)
1037 #ifdef PLATFORM_LINUX
1038 return ms * HZ / 1000;
1040 #ifdef PLATFORM_FREEBSD
1043 #ifdef PLATFORM_WINDOWS
1048 // the input parameter start use the same unit as returned by rtw_get_current_time
1049 inline s32 rtw_get_passing_time_ms(u32 start)
1051 #ifdef PLATFORM_LINUX
1052 return rtw_systime_to_ms(jiffies-start);
1054 #ifdef PLATFORM_FREEBSD
1055 return rtw_systime_to_ms(rtw_get_current_time());
1057 #ifdef PLATFORM_WINDOWS
1058 LARGE_INTEGER SystemTime;
1059 NdisGetCurrentSystemTime(&SystemTime);
1060 return rtw_systime_to_ms((u32)(SystemTime.LowPart) - start) ;
1064 inline s32 rtw_get_time_interval_ms(u32 start, u32 end)
1066 #ifdef PLATFORM_LINUX
1067 return rtw_systime_to_ms(end-start);
1069 #ifdef PLATFORM_FREEBSD
1070 return rtw_systime_to_ms(rtw_get_current_time());
1072 #ifdef PLATFORM_WINDOWS
1073 return rtw_systime_to_ms(end-start);
1078 void rtw_sleep_schedulable(int ms)
1081 #ifdef PLATFORM_LINUX
1085 delta = (ms * HZ)/1000;//(ms)
1089 set_current_state(TASK_INTERRUPTIBLE);
1090 if (schedule_timeout(delta) != 0) {
1096 #ifdef PLATFORM_FREEBSD
1101 #ifdef PLATFORM_WINDOWS
1103 NdisMSleep(ms*1000); //(us)*1000=(ms)
1110 void rtw_msleep_os(int ms)
1113 #ifdef PLATFORM_LINUX
1115 msleep((unsigned int)ms);
1118 #ifdef PLATFORM_FREEBSD
1119 //Delay for delay microseconds
1123 #ifdef PLATFORM_WINDOWS
1125 NdisMSleep(ms*1000); //(us)*1000=(ms)
1131 void rtw_usleep_os(int us)
1134 #ifdef PLATFORM_LINUX
1136 // msleep((unsigned int)us);
1137 if ( 1 < (us/1000) )
1140 msleep( (us/1000) + 1);
1143 #ifdef PLATFORM_FREEBSD
1144 //Delay for delay microseconds
1149 #ifdef PLATFORM_WINDOWS
1151 NdisMSleep(us); //(us)
1160 void _rtw_mdelay_os(int ms, const char *func, const int line)
1164 DBG_8723A("%s:%d %s(%d)\n", func, line, __FUNCTION__, ms);
1170 DBG_8723A("%s:%d %s(%d)\n", func, line, __FUNCTION__, ms);
1172 #if defined(PLATFORM_LINUX)
1174 mdelay((unsigned long)ms);
1176 #elif defined(PLATFORM_WINDOWS)
1178 NdisStallExecution(ms*1000); //(us)*1000=(ms)
1184 void _rtw_udelay_os(int us, const char *func, const int line)
1189 DBG_8723A("%s:%d %s(%d)\n", func, line, __FUNCTION__, us);
1196 DBG_8723A("%s:%d %s(%d)\n", func, line, __FUNCTION__, us);
1199 #if defined(PLATFORM_LINUX)
1201 udelay((unsigned long)us);
1203 #elif defined(PLATFORM_WINDOWS)
1205 NdisStallExecution(us); //(us)
1211 void rtw_mdelay_os(int ms)
1214 #ifdef PLATFORM_LINUX
1216 mdelay((unsigned long)ms);
1219 #ifdef PLATFORM_FREEBSD
1223 #ifdef PLATFORM_WINDOWS
1225 NdisStallExecution(ms*1000); //(us)*1000=(ms)
1231 void rtw_udelay_os(int us)
1234 #ifdef PLATFORM_LINUX
1236 udelay((unsigned long)us);
1239 #ifdef PLATFORM_FREEBSD
1240 //Delay for delay microseconds
1244 #ifdef PLATFORM_WINDOWS
1246 NdisStallExecution(us); //(us)
1253 void rtw_yield_os(void)
1258 #define RTW_SUSPEND_LOCK_NAME "rtw_wifi"
1260 #ifdef CONFIG_WAKELOCK
1261 static struct wake_lock rtw_suspend_lock;
1262 #elif defined(CONFIG_ANDROID_POWER)
1263 static android_suspend_lock_t rtw_suspend_lock ={
1264 .name = RTW_SUSPEND_LOCK_NAME
1268 inline void rtw_suspend_lock_init(void)
1270 #ifdef CONFIG_WAKELOCK
1271 wake_lock_init(&rtw_suspend_lock, WAKE_LOCK_SUSPEND, RTW_SUSPEND_LOCK_NAME);
1272 #elif defined(CONFIG_ANDROID_POWER)
1273 android_init_suspend_lock(&rtw_suspend_lock);
1277 inline void rtw_suspend_lock_uninit(void)
1279 #ifdef CONFIG_WAKELOCK
1280 wake_lock_destroy(&rtw_suspend_lock);
1281 #elif defined(CONFIG_ANDROID_POWER)
1282 android_uninit_suspend_lock(&rtw_suspend_lock);
1286 inline void rtw_lock_suspend(void)
1288 #ifdef CONFIG_WAKELOCK
1289 wake_lock(&rtw_suspend_lock);
1290 #elif defined(CONFIG_ANDROID_POWER)
1291 android_lock_suspend(&rtw_suspend_lock);
1294 #if defined(CONFIG_WAKELOCK) || defined(CONFIG_ANDROID_POWER)
1295 //DBG_8723A("####%s: suspend_lock_count:%d####\n", __FUNCTION__, rtw_suspend_lock.stat.count);
1299 inline void rtw_unlock_suspend(void)
1301 #ifdef CONFIG_WAKELOCK
1302 wake_unlock(&rtw_suspend_lock);
1303 #elif defined(CONFIG_ANDROID_POWER)
1304 android_unlock_suspend(&rtw_suspend_lock);
1307 #if defined(CONFIG_WAKELOCK) || defined(CONFIG_ANDROID_POWER)
1308 //DBG_8723A("####%s: suspend_lock_count:%d####\n", __FUNCTION__, rtw_suspend_lock.stat.count);
1312 #ifdef CONFIG_WOWLAN
1313 inline void rtw_lock_suspend_timeout(long timeout)
1315 #ifdef CONFIG_WAKELOCK
1316 wake_lock_timeout(&rtw_suspend_lock, timeout);
1317 #elif defined(CONFIG_ANDROID_POWER)
1318 android_lock_suspend_auto_expire(&rtw_suspend_lock, timeout);
1321 #endif //CONFIG_WOWLAN
1323 inline void ATOMIC_SET(ATOMIC_T *v, int i)
1325 #ifdef PLATFORM_LINUX
1327 #elif defined(PLATFORM_WINDOWS)
1328 *v=i;// other choice????
1329 #elif defined(PLATFORM_FREEBSD)
1330 atomic_set_int(v,i);
1334 inline int ATOMIC_READ(ATOMIC_T *v)
1336 #ifdef PLATFORM_LINUX
1337 return atomic_read(v);
1338 #elif defined(PLATFORM_WINDOWS)
1339 return *v; // other choice????
1340 #elif defined(PLATFORM_FREEBSD)
1341 return atomic_load_acq_32(v);
1345 inline void ATOMIC_ADD(ATOMIC_T *v, int i)
1347 #ifdef PLATFORM_LINUX
1349 #elif defined(PLATFORM_WINDOWS)
1350 InterlockedAdd(v,i);
1351 #elif defined(PLATFORM_FREEBSD)
1352 atomic_add_int(v,i);
1355 inline void ATOMIC_SUB(ATOMIC_T *v, int i)
1357 #ifdef PLATFORM_LINUX
1359 #elif defined(PLATFORM_WINDOWS)
1360 InterlockedAdd(v,-i);
1361 #elif defined(PLATFORM_FREEBSD)
1362 atomic_subtract_int(v,i);
1366 inline void ATOMIC_INC(ATOMIC_T *v)
1368 #ifdef PLATFORM_LINUX
1370 #elif defined(PLATFORM_WINDOWS)
1371 InterlockedIncrement(v);
1372 #elif defined(PLATFORM_FREEBSD)
1373 atomic_add_int(v,1);
1377 inline void ATOMIC_DEC(ATOMIC_T *v)
1379 #ifdef PLATFORM_LINUX
1381 #elif defined(PLATFORM_WINDOWS)
1382 InterlockedDecrement(v);
1383 #elif defined(PLATFORM_FREEBSD)
1384 atomic_subtract_int(v,1);
1388 inline int ATOMIC_ADD_RETURN(ATOMIC_T *v, int i)
1390 #ifdef PLATFORM_LINUX
1391 return atomic_add_return(i,v);
1392 #elif defined(PLATFORM_WINDOWS)
1393 return InterlockedAdd(v,i);
1394 #elif defined(PLATFORM_FREEBSD)
1395 atomic_add_int(v,i);
1396 return atomic_load_acq_32(v);
1400 inline int ATOMIC_SUB_RETURN(ATOMIC_T *v, int i)
1402 #ifdef PLATFORM_LINUX
1403 return atomic_sub_return(i,v);
1404 #elif defined(PLATFORM_WINDOWS)
1405 return InterlockedAdd(v,-i);
1406 #elif defined(PLATFORM_FREEBSD)
1407 atomic_subtract_int(v,i);
1408 return atomic_load_acq_32(v);
1412 inline int ATOMIC_INC_RETURN(ATOMIC_T *v)
1414 #ifdef PLATFORM_LINUX
1415 return atomic_inc_return(v);
1416 #elif defined(PLATFORM_WINDOWS)
1417 return InterlockedIncrement(v);
1418 #elif defined(PLATFORM_FREEBSD)
1419 atomic_add_int(v,1);
1420 return atomic_load_acq_32(v);
1424 inline int ATOMIC_DEC_RETURN(ATOMIC_T *v)
1426 #ifdef PLATFORM_LINUX
1427 return atomic_dec_return(v);
1428 #elif defined(PLATFORM_WINDOWS)
1429 return InterlockedDecrement(v);
1430 #elif defined(PLATFORM_FREEBSD)
1431 atomic_subtract_int(v,1);
1432 return atomic_load_acq_32(v);
1437 #ifdef PLATFORM_LINUX
1439 * Open a file with the specific @param path, @param flag, @param mode
1440 * @param fpp the pointer of struct file pointer to get struct file pointer while file opening is success
1441 * @param path the path of the file to open
1442 * @param flag file operation flags, please refer to linux document
1443 * @param mode please refer to linux document
1444 * @return Linux specific error code
1446 static int openFile(struct file **fpp, char *path, int flag, int mode)
1450 fp=filp_open(path, flag, mode);
1462 * Close the file with the specific @param fp
1463 * @param fp the pointer of struct file to close
1466 static int closeFile(struct file *fp)
1468 filp_close(fp,NULL);
1472 static int readFile(struct file *fp,char *buf,int len)
1476 if (!fp->f_op || !fp->f_op->read)
1480 rlen=fp->f_op->read(fp,buf+sum,len-sum, &fp->f_pos);
1493 static int writeFile(struct file *fp,char *buf,int len)
1497 if (!fp->f_op || !fp->f_op->write)
1501 wlen=fp->f_op->write(fp,buf+sum,len-sum, &fp->f_pos);
1515 * Test if the specifi @param path is a file and readable
1516 * @param path the path of the file to test
1517 * @return Linux specific error code
1519 static int isFileReadable(char *path)
1526 fp=filp_open(path, O_RDONLY, 0);
1531 oldfs = get_fs(); set_fs(get_ds());
1533 if(1!=readFile(fp, &buf, 1))
1537 filp_close(fp,NULL);
1543 * Open the file with @param path and retrive the file content into memory starting from @param buf for @param sz at most
1544 * @param path the path of the file to open and read
1545 * @param buf the starting address of the buffer to store file content
1546 * @param sz how many bytes to read at most
1547 * @return the byte we've read, or Linux specific error code
1549 static int retriveFromFile(char *path, u8* buf, u32 sz)
1556 if( 0 == (ret=openFile(&fp,path, O_RDONLY, 0)) ){
1557 DBG_8723A("%s openFile path:%s fp=%p\n",__FUNCTION__, path ,fp);
1559 oldfs = get_fs(); set_fs(get_ds());
1560 ret=readFile(fp, buf, sz);
1564 DBG_8723A("%s readFile, ret:%d\n",__FUNCTION__, ret);
1567 DBG_8723A("%s openFile path:%s Fail, ret:%d\n",__FUNCTION__, path, ret);
1570 DBG_8723A("%s NULL pointer\n",__FUNCTION__);
1577 * Open the file with @param path and wirte @param sz byte of data starting from @param buf into the file
1578 * @param path the path of the file to open and write
1579 * @param buf the starting address of the data to write into file
1580 * @param sz how many bytes to write at most
1581 * @return the byte we've written, or Linux specific error code
1583 static int storeToFile(char *path, u8* buf, u32 sz)
1590 if( 0 == (ret=openFile(&fp, path, O_CREAT|O_WRONLY, 0666)) ) {
1591 DBG_8723A("%s openFile path:%s fp=%p\n",__FUNCTION__, path ,fp);
1593 oldfs = get_fs(); set_fs(get_ds());
1594 ret=writeFile(fp, buf, sz);
1598 DBG_8723A("%s writeFile, ret:%d\n",__FUNCTION__, ret);
1601 DBG_8723A("%s openFile path:%s Fail, ret:%d\n",__FUNCTION__, path, ret);
1604 DBG_8723A("%s NULL pointer\n",__FUNCTION__);
1609 #endif //PLATFORM_LINUX
1612 * Test if the specifi @param path is a file and readable
1613 * @param path the path of the file to test
1614 * @return _TRUE or _FALSE
1616 int rtw_is_file_readable(char *path)
1618 #ifdef PLATFORM_LINUX
1619 if(isFileReadable(path) == 0)
1630 * Open the file with @param path and retrive the file content into memory starting from @param buf for @param sz at most
1631 * @param path the path of the file to open and read
1632 * @param buf the starting address of the buffer to store file content
1633 * @param sz how many bytes to read at most
1634 * @return the byte we've read
1636 int rtw_retrive_from_file(char *path, u8* buf, u32 sz)
1638 #ifdef PLATFORM_LINUX
1639 int ret =retriveFromFile(path, buf, sz);
1640 return ret>=0?ret:0;
1648 * Open the file with @param path and wirte @param sz byte of data starting from @param buf into the file
1649 * @param path the path of the file to open and write
1650 * @param buf the starting address of the data to write into file
1651 * @param sz how many bytes to write at most
1652 * @return the byte we've written
1654 int rtw_store_to_file(char *path, u8* buf, u32 sz)
1656 #ifdef PLATFORM_LINUX
1657 int ret =storeToFile(path, buf, sz);
1658 return ret>=0?ret:0;
1665 #if 1 //#ifdef MEM_ALLOC_REFINE_ADAPTOR
1666 #ifdef PLATFORM_LINUX
1667 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv, void *old_priv)
1669 struct net_device *pnetdev;
1670 struct rtw_netdev_priv_indicator *pnpi;
1672 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
1673 pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
1675 pnetdev = alloc_etherdev(sizeof(struct rtw_netdev_priv_indicator));
1680 pnpi = netdev_priv(pnetdev);
1681 pnpi->priv=old_priv;
1682 pnpi->sizeof_priv=sizeof_priv;
1688 struct net_device *rtw_alloc_etherdev(int sizeof_priv)
1690 struct net_device *pnetdev;
1691 struct rtw_netdev_priv_indicator *pnpi;
1693 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
1694 pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
1696 pnetdev = alloc_etherdev(sizeof(struct rtw_netdev_priv_indicator));
1701 pnpi = netdev_priv(pnetdev);
1703 pnpi->priv = rtw_zvmalloc(sizeof_priv);
1705 free_netdev(pnetdev);
1710 pnpi->sizeof_priv=sizeof_priv;
1715 void rtw_free_netdev(struct net_device * netdev)
1717 struct rtw_netdev_priv_indicator *pnpi;
1722 pnpi = netdev_priv(netdev);
1727 rtw_vmfree(pnpi->priv, pnpi->sizeof_priv);
1728 free_netdev(netdev);
1735 * Jeff: this function should be called under ioctl (rtnl_lock is accquired) while
1736 * LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
1738 int rtw_change_ifname(_adapter *padapter, const char *ifname)
1740 struct net_device *pnetdev;
1741 struct net_device *cur_pnetdev;
1742 struct rereg_nd_name_data *rereg_priv;
1748 cur_pnetdev = padapter->pnetdev;
1749 rereg_priv = &padapter->rereg_nd_name_priv;
1751 //free the old_pnetdev
1752 if(rereg_priv->old_pnetdev) {
1753 free_netdev(rereg_priv->old_pnetdev);
1754 rereg_priv->old_pnetdev = NULL;
1757 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26))
1758 if(!rtnl_is_locked())
1759 unregister_netdev(cur_pnetdev);
1762 unregister_netdevice(cur_pnetdev);
1764 rtw_proc_remove_one(cur_pnetdev);
1766 rereg_priv->old_pnetdev=cur_pnetdev;
1768 pnetdev = rtw_init_netdev(padapter);
1774 SET_NETDEV_DEV(pnetdev, dvobj_to_dev(adapter_to_dvobj(padapter)));
1776 rtw_init_netdev_name(pnetdev, ifname);
1778 _rtw_memcpy(pnetdev->dev_addr, padapter->eeprompriv.mac_addr, ETH_ALEN);
1780 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26))
1781 if(!rtnl_is_locked())
1782 ret = register_netdev(pnetdev);
1785 ret = register_netdevice(pnetdev);
1788 RT_TRACE(_module_hci_intfs_c_,_drv_err_,("register_netdev() failed\n"));
1792 rtw_proc_init_one(pnetdev);
1802 #endif //MEM_ALLOC_REFINE_ADAPTOR
1804 #ifdef PLATFORM_FREEBSD
1806 * Copy a buffer from userspace and write into kernel address
1809 * This emulation just calls the FreeBSD copyin function (to
1810 * copy data from user space buffer into a kernel space buffer)
1811 * and is designed to be used with the above io_write_wrapper.
1813 * This function should return the number of bytes not copied.
1814 * I.e. success results in a zero value.
1815 * Negative error values are not returned.
1818 copy_from_user(void *to, const void *from, unsigned long n)
1820 if ( copyin(from, to, n) != 0 ) {
1821 /* Any errors will be treated as a failure
1822 to copy any of the requested bytes */
1830 copy_to_user(void *to, const void *from, unsigned long n)
1832 if ( copyout(from, to, n) != 0 ) {
1833 /* Any errors will be treated as a failure
1834 to copy any of the requested bytes */
1843 * The usb_register and usb_deregister functions are used to register
1844 * usb drivers with the usb subsystem. In this compatibility layer
1845 * emulation a list of drivers (struct usb_driver) is maintained
1846 * and is used for probing/attaching etc.
1848 * usb_register and usb_deregister simply call these functions.
1851 usb_register(struct usb_driver *driver)
1853 rtw_usb_linux_register(driver);
1859 usb_deregister(struct usb_driver *driver)
1861 rtw_usb_linux_deregister(driver);
1865 void module_init_exit_wrapper(void *arg)
1867 int (*func)(void) = arg;
1872 #endif //PLATFORM_FREEBSD
1874 #ifdef CONFIG_PLATFORM_SPRD
1878 #include <asm-generic/div64.h>
1881 u64 rtw_modular64(u64 x, u64 y)
1883 #ifdef PLATFORM_LINUX
1884 return do_div(x, y);
1885 #elif defined(PLATFORM_WINDOWS)
1887 #elif defined(PLATFORM_FREEBSD)
1892 u64 rtw_division64(u64 x, u64 y)
1894 #ifdef PLATFORM_LINUX
1897 #elif defined(PLATFORM_WINDOWS)
1899 #elif defined(PLATFORM_FREEBSD)
1904 void rtw_buf_free(u8 **buf, u32 *buf_len)
1908 if (!buf || !buf_len)
1915 _rtw_mfree(*buf, *buf_len);
1920 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len)
1922 u32 ori_len = 0, dup_len = 0;
1926 if (!buf || !buf_len)
1929 if (!src || !src_len)
1933 dup = rtw_malloc(src_len);
1936 _rtw_memcpy(dup, src, dup_len);
1943 /* replace buf with dup */
1949 if (ori && ori_len > 0)
1950 _rtw_mfree(ori, ori_len);
1955 * rtw_cbuf_full - test if cbuf is full
1956 * @cbuf: pointer of struct rtw_cbuf
1958 * Returns: _TRUE if cbuf is full
1960 inline bool rtw_cbuf_full(struct rtw_cbuf *cbuf)
1962 return (cbuf->write == cbuf->read-1)? _TRUE : _FALSE;
1966 * rtw_cbuf_empty - test if cbuf is empty
1967 * @cbuf: pointer of struct rtw_cbuf
1969 * Returns: _TRUE if cbuf is empty
1971 inline bool rtw_cbuf_empty(struct rtw_cbuf *cbuf)
1973 return (cbuf->write == cbuf->read)? _TRUE : _FALSE;
1977 * rtw_cbuf_push - push a pointer into cbuf
1978 * @cbuf: pointer of struct rtw_cbuf
1979 * @buf: pointer to push in
1981 * Lock free operation, be careful of the use scheme
1982 * Returns: _TRUE push success
1984 bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf)
1986 if (rtw_cbuf_full(cbuf))
1990 DBG_8723A("%s on %u\n", __func__, cbuf->write);
1991 cbuf->bufs[cbuf->write] = buf;
1992 cbuf->write = (cbuf->write+1)%cbuf->size;
1998 * rtw_cbuf_pop - pop a pointer from cbuf
1999 * @cbuf: pointer of struct rtw_cbuf
2001 * Lock free operation, be careful of the use scheme
2002 * Returns: pointer popped out
2004 void *rtw_cbuf_pop(struct rtw_cbuf *cbuf)
2007 if (rtw_cbuf_empty(cbuf))
2011 DBG_8723A("%s on %u\n", __func__, cbuf->read);
2012 buf = cbuf->bufs[cbuf->read];
2013 cbuf->read = (cbuf->read+1)%cbuf->size;
2019 * rtw_cbuf_alloc - allocte a rtw_cbuf with given size and do initialization
2020 * @size: size of pointer
2022 * Returns: pointer of srtuct rtw_cbuf, NULL for allocation failure
2024 struct rtw_cbuf *rtw_cbuf_alloc(u32 size)
2026 struct rtw_cbuf *cbuf;
2028 cbuf = (struct rtw_cbuf *)rtw_malloc(sizeof(*cbuf) + sizeof(void*)*size);
2031 cbuf->write = cbuf->read = 0;
2039 * rtw_cbuf_free - free the given rtw_cbuf
2040 * @cbuf: pointer of struct rtw_cbuf to free
2042 void rtw_cbuf_free(struct rtw_cbuf *cbuf)
2044 rtw_mfree((u8*)cbuf, sizeof(*cbuf) + sizeof(void*)*cbuf->size);