1 /******************************************************************************
3 * Copyright(c) 2007 - 2011 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 */
252 #ifdef PLATFORM_FREEBSD
254 struct sk_buff
* dev_alloc_skb(unsigned int size
)
256 struct sk_buff
*skb
=NULL
;
259 //skb = (struct sk_buff *)_rtw_zmalloc(sizeof(struct sk_buff)); // for skb->len, etc.
260 skb
= (struct sk_buff
*)_rtw_malloc(sizeof(struct sk_buff
));
263 data
= _rtw_malloc(size
);
267 skb
->head
= (unsigned char*)data
;
268 skb
->data
= (unsigned char*)data
;
269 skb
->tail
= (unsigned char*)data
;
270 skb
->end
= (unsigned char*)data
+ size
;
272 //printf("%s()-%d: skb=%p, skb->head = %p\n", __FUNCTION__, __LINE__, skb, skb->head);
277 _rtw_mfree((u8
*)skb
, sizeof(struct sk_buff
));
283 void dev_kfree_skb_any(struct sk_buff
*skb
)
285 //printf("%s()-%d: skb->head = %p\n", __FUNCTION__, __LINE__, skb->head);
287 _rtw_mfree(skb
->head
, 0);
288 //printf("%s()-%d: skb = %p\n", __FUNCTION__, __LINE__, skb);
290 _rtw_mfree((u8
*)skb
, 0);
292 struct sk_buff
*skb_clone(const struct sk_buff
*skb
)
297 #endif /* PLATFORM_FREEBSD */
299 inline struct sk_buff
*_rtw_skb_alloc(u32 sz
)
301 #ifdef PLATFORM_LINUX
302 return __dev_alloc_skb(sz
, in_interrupt() ? GFP_ATOMIC
: GFP_KERNEL
);
303 #endif /* PLATFORM_LINUX */
305 #ifdef PLATFORM_FREEBSD
306 return dev_alloc_skb(sz
);
307 #endif /* PLATFORM_FREEBSD */
310 inline void _rtw_skb_free(struct sk_buff
*skb
)
312 dev_kfree_skb_any(skb
);
315 inline struct sk_buff
*_rtw_skb_copy(const struct sk_buff
*skb
)
317 #ifdef PLATFORM_LINUX
318 return skb_copy(skb
, in_interrupt() ? GFP_ATOMIC
: GFP_KERNEL
);
319 #endif /* PLATFORM_LINUX */
321 #ifdef PLATFORM_FREEBSD
323 #endif /* PLATFORM_FREEBSD */
326 inline struct sk_buff
*_rtw_skb_clone(struct sk_buff
*skb
)
328 #ifdef PLATFORM_LINUX
329 return skb_clone(skb
, in_interrupt() ? GFP_ATOMIC
: GFP_KERNEL
);
330 #endif /* PLATFORM_LINUX */
332 #ifdef PLATFORM_FREEBSD
333 return skb_clone(skb
);
334 #endif /* PLATFORM_FREEBSD */
337 inline int _rtw_netif_rx(_nic_hdl ndev
, struct sk_buff
*skb
)
339 #ifdef PLATFORM_LINUX
341 return netif_rx(skb
);
342 #endif /* PLATFORM_LINUX */
344 #ifdef PLATFORM_FREEBSD
345 return (*ndev
->if_input
)(ndev
, skb
);
346 #endif /* PLATFORM_FREEBSD */
349 void _rtw_skb_queue_purge(struct sk_buff_head
*list
)
353 while ((skb
= skb_dequeue(list
)) != NULL
)
357 #ifdef CONFIG_USB_HCI
358 inline void *_rtw_usb_buffer_alloc(struct usb_device
*dev
, size_t size
, dma_addr_t
*dma
)
360 #ifdef PLATFORM_LINUX
361 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
362 return usb_alloc_coherent(dev
, size
, (in_interrupt() ? GFP_ATOMIC
: GFP_KERNEL
), dma
);
364 return usb_buffer_alloc(dev
, size
, (in_interrupt() ? GFP_ATOMIC
: GFP_KERNEL
), dma
);
366 #endif /* PLATFORM_LINUX */
368 #ifdef PLATFORM_FREEBSD
369 return (malloc(size
, M_USBDEV
, M_NOWAIT
| M_ZERO
));
370 #endif /* PLATFORM_FREEBSD */
372 inline void _rtw_usb_buffer_free(struct usb_device
*dev
, size_t size
, void *addr
, dma_addr_t dma
)
374 #ifdef PLATFORM_LINUX
375 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
376 usb_free_coherent(dev
, size
, addr
, dma
);
378 usb_buffer_free(dev
, size
, addr
, dma
);
380 #endif /* PLATFORM_LINUX */
382 #ifdef PLATFORM_FREEBSD
383 free(addr
, M_USBDEV
);
384 #endif /* PLATFORM_FREEBSD */
386 #endif /* CONFIG_USB_HCI */
390 struct rtw_mem_stat
{
391 ATOMIC_T alloc
; // the memory bytes we allocate currently
392 ATOMIC_T peak
; // the peak memory bytes we allocate
393 ATOMIC_T alloc_cnt
; // the alloc count for alloc currently
394 ATOMIC_T alloc_err_cnt
; // the error times we fail to allocate memory
397 struct rtw_mem_stat rtw_mem_type_stat
[mstat_tf_idx(MSTAT_TYPE_MAX
)];
398 struct rtw_mem_stat rtw_mem_func_stat
[mstat_ff_idx(MSTAT_FUNC_MAX
)];
400 char *MSTAT_TYPE_str
[] = {
407 char *MSTAT_FUNC_str
[] = {
416 int _rtw_mstat_dump(char *buf
, int len
)
420 int value_t
[4][mstat_tf_idx(MSTAT_TYPE_MAX
)];
421 int value_f
[4][mstat_ff_idx(MSTAT_FUNC_MAX
)];
423 int vir_alloc
, vir_peak
, vir_alloc_err
, phy_alloc
, phy_peak
, phy_alloc_err
;
424 int tx_alloc
, tx_peak
, tx_alloc_err
, rx_alloc
, rx_peak
, rx_alloc_err
;
426 for(i
=0;i
<mstat_tf_idx(MSTAT_TYPE_MAX
);i
++) {
427 value_t
[0][i
] = ATOMIC_READ(&(rtw_mem_type_stat
[i
].alloc
));
428 value_t
[1][i
] = ATOMIC_READ(&(rtw_mem_type_stat
[i
].peak
));
429 value_t
[2][i
] = ATOMIC_READ(&(rtw_mem_type_stat
[i
].alloc_cnt
));
430 value_t
[3][i
] = ATOMIC_READ(&(rtw_mem_type_stat
[i
].alloc_err_cnt
));
434 for(i
=0;i
<mstat_ff_idx(MSTAT_FUNC_MAX
);i
++) {
435 value_f
[0][i
] = ATOMIC_READ(&(rtw_mem_func_stat
[i
].alloc
));
436 value_f
[1][i
] = ATOMIC_READ(&(rtw_mem_func_stat
[i
].peak
));
437 value_f
[2][i
] = ATOMIC_READ(&(rtw_mem_func_stat
[i
].alloc_cnt
));
438 value_f
[3][i
] = ATOMIC_READ(&(rtw_mem_func_stat
[i
].alloc_err_cnt
));
442 cnt
+= snprintf(buf
+cnt
, len
-cnt
, "===================== MSTAT =====================\n");
443 cnt
+= snprintf(buf
+cnt
, len
-cnt
, "%4s %10s %10s %10s %10s\n", "TAG", "alloc", "peak", "aloc_cnt", "err_cnt");
444 cnt
+= snprintf(buf
+cnt
, len
-cnt
, "-------------------------------------------------\n");
445 for(i
=0;i
<mstat_tf_idx(MSTAT_TYPE_MAX
);i
++) {
446 cnt
+= snprintf(buf
+cnt
, len
-cnt
, "%4s %10d %10d %10d %10d\n", MSTAT_TYPE_str
[i
], value_t
[0][i
], value_t
[1][i
], value_t
[2][i
], value_t
[3][i
]);
449 cnt
+= snprintf(buf
+cnt
, len
-cnt
, "-------------------------------------------------\n");
450 for(i
=0;i
<mstat_ff_idx(MSTAT_FUNC_MAX
);i
++) {
451 cnt
+= snprintf(buf
+cnt
, len
-cnt
, "%4s %10d %10d %10d %10d\n", MSTAT_FUNC_str
[i
], value_f
[0][i
], value_f
[1][i
], value_f
[2][i
], value_f
[3][i
]);
458 void rtw_mstat_dump(void)
462 _rtw_mstat_dump(buf
, 768);
463 DBG_871X("\n%s", buf
);
466 void rtw_mstat_update(const enum mstat_f flags
, const MSTAT_STATUS status
, u32 sz
)
468 static u32 update_time
= 0;
474 for(i
=0;i
<mstat_tf_idx(MSTAT_TYPE_MAX
);i
++) {
475 ATOMIC_SET(&(rtw_mem_type_stat
[i
].alloc
), 0);
476 ATOMIC_SET(&(rtw_mem_type_stat
[i
].peak
), 0);
477 ATOMIC_SET(&(rtw_mem_type_stat
[i
].alloc_cnt
), 0);
478 ATOMIC_SET(&(rtw_mem_type_stat
[i
].alloc_err_cnt
), 0);
480 for(i
=0;i
<mstat_ff_idx(MSTAT_FUNC_MAX
);i
++) {
481 ATOMIC_SET(&(rtw_mem_func_stat
[i
].alloc
), 0);
482 ATOMIC_SET(&(rtw_mem_func_stat
[i
].peak
), 0);
483 ATOMIC_SET(&(rtw_mem_func_stat
[i
].alloc_cnt
), 0);
484 ATOMIC_SET(&(rtw_mem_func_stat
[i
].alloc_err_cnt
), 0);
489 case MSTAT_ALLOC_SUCCESS
:
490 ATOMIC_INC(&(rtw_mem_type_stat
[mstat_tf_idx(flags
)].alloc_cnt
));
491 alloc
= ATOMIC_ADD_RETURN(&(rtw_mem_type_stat
[mstat_tf_idx(flags
)].alloc
), sz
);
492 peak
=ATOMIC_READ(&(rtw_mem_type_stat
[mstat_tf_idx(flags
)].peak
));
494 ATOMIC_SET(&(rtw_mem_type_stat
[mstat_tf_idx(flags
)].peak
), alloc
);
496 ATOMIC_INC(&(rtw_mem_func_stat
[mstat_ff_idx(flags
)].alloc_cnt
));
497 alloc
= ATOMIC_ADD_RETURN(&(rtw_mem_func_stat
[mstat_ff_idx(flags
)].alloc
), sz
);
498 peak
=ATOMIC_READ(&(rtw_mem_func_stat
[mstat_ff_idx(flags
)].peak
));
500 ATOMIC_SET(&(rtw_mem_func_stat
[mstat_ff_idx(flags
)].peak
), alloc
);
503 case MSTAT_ALLOC_FAIL
:
504 ATOMIC_INC(&(rtw_mem_type_stat
[mstat_tf_idx(flags
)].alloc_err_cnt
));
506 ATOMIC_INC(&(rtw_mem_func_stat
[mstat_ff_idx(flags
)].alloc_err_cnt
));
510 ATOMIC_DEC(&(rtw_mem_type_stat
[mstat_tf_idx(flags
)].alloc_cnt
));
511 ATOMIC_SUB(&(rtw_mem_type_stat
[mstat_tf_idx(flags
)].alloc
), sz
);
513 ATOMIC_DEC(&(rtw_mem_func_stat
[mstat_ff_idx(flags
)].alloc_cnt
));
514 ATOMIC_SUB(&(rtw_mem_func_stat
[mstat_ff_idx(flags
)].alloc
), sz
);
518 //if (rtw_get_passing_time_ms(update_time) > 5000) {
520 update_time
=rtw_get_current_time();
526 inline u8
* dbg_rtw_vmalloc(u32 sz
, const enum mstat_f flags
, const char *func
, const int line
)
529 //DBG_871X("DBG_MEM_ALLOC %s:%d %s(%d)\n", func, line, __FUNCTION__, (sz));
531 p
=_rtw_vmalloc((sz
));
535 , p
? MSTAT_ALLOC_SUCCESS
: MSTAT_ALLOC_FAIL
542 inline u8
* dbg_rtw_zvmalloc(u32 sz
, const enum mstat_f flags
, const char *func
, const int line
)
545 //DBG_871X("DBG_MEM_ALLOC %s:%d %s(%d)\n", func, line, __FUNCTION__, (sz));
547 p
=_rtw_zvmalloc((sz
));
551 , p
? MSTAT_ALLOC_SUCCESS
: MSTAT_ALLOC_FAIL
558 inline void dbg_rtw_vmfree(u8
*pbuf
, u32 sz
, const enum mstat_f flags
, const char *func
, const int line
)
560 //DBG_871X("DBG_MEM_ALLOC %s:%d %s(%p,%d)\n", func, line, __FUNCTION__, (pbuf), (sz));
562 _rtw_vmfree((pbuf
), (sz
));
571 inline u8
* dbg_rtw_malloc(u32 sz
, const enum mstat_f flags
, const char *func
, const int line
)
575 //if(sz>=153 && sz<=306)
576 // DBG_871X("DBG_MEM_ALLOC %s:%d %s(%d)\n", func, line, __FUNCTION__, (sz));
579 // DBG_871X("DBG_MEM_ALLOC %s:%d %s(%d)\n", func, line, __FUNCTION__, (sz));
585 , p
? MSTAT_ALLOC_SUCCESS
: MSTAT_ALLOC_FAIL
592 inline u8
* dbg_rtw_zmalloc(u32 sz
, const enum mstat_f flags
, const char *func
, const int line
)
596 //if(sz>=153 && sz<=306)
597 // DBG_871X("DBG_MEM_ALLOC %s:%d %s(%d)\n", func, line, __FUNCTION__, (sz));
600 // DBG_871X("DBG_MEM_ALLOC %s:%d %s(%d)\n", func, line, __FUNCTION__, (sz));
602 p
= _rtw_zmalloc((sz
));
606 , p
? MSTAT_ALLOC_SUCCESS
: MSTAT_ALLOC_FAIL
613 inline void dbg_rtw_mfree(u8
*pbuf
, u32 sz
, const enum mstat_f flags
, const char *func
, const int line
)
615 //if(sz>=153 && sz<=306)
616 // DBG_871X("DBG_MEM_ALLOC %s:%d %s(%d)\n", func, line, __FUNCTION__, (sz));
619 // DBG_871X("DBG_MEM_ALLOC %s:%d %s(%p,%d)\n", func, line, __FUNCTION__, (pbuf), (sz));
621 _rtw_mfree((pbuf
), (sz
));
630 inline struct sk_buff
* dbg_rtw_skb_alloc(unsigned int size
, const enum mstat_f flags
, const char *func
, int line
)
633 unsigned int truesize
= 0;
635 skb
= _rtw_skb_alloc(size
);
638 truesize
= skb
->truesize
;
640 if(!skb
|| truesize
< size
/*|| size > 4096*/)
641 DBG_871X("DBG_MEM_ALLOC %s:%d %s(%d), skb:%p, truesize=%u\n", func
, line
, __FUNCTION__
, size
, skb
, truesize
);
645 , skb
? MSTAT_ALLOC_SUCCESS
: MSTAT_ALLOC_FAIL
652 inline void dbg_rtw_skb_free(struct sk_buff
*skb
, const enum mstat_f flags
, const char *func
, int line
)
654 unsigned int truesize
= skb
->truesize
;
656 //if(truesize > 4096)
657 // DBG_871X("DBG_MEM_ALLOC %s:%d %s, truesize=%u\n", func, line, __FUNCTION__, truesize);
668 inline struct sk_buff
*dbg_rtw_skb_copy(const struct sk_buff
*skb
, const enum mstat_f flags
, const char *func
, const int line
)
670 struct sk_buff
*skb_cp
;
671 unsigned int truesize
= skb
->truesize
;
672 unsigned int cp_truesize
= 0;
674 skb_cp
= _rtw_skb_copy(skb
);
676 cp_truesize
= skb_cp
->truesize
;
678 if(!skb_cp
|| cp_truesize
!= truesize
/*||cp_truesize > 4096*/)
679 DBG_871X("DBG_MEM_ALLOC %s:%d %s(%u), skb_cp:%p, cp_truesize=%u\n", func
, line
, __FUNCTION__
, truesize
, skb_cp
, cp_truesize
);
683 , skb_cp
? MSTAT_ALLOC_SUCCESS
: MSTAT_ALLOC_FAIL
690 inline struct sk_buff
*dbg_rtw_skb_clone(struct sk_buff
*skb
, const enum mstat_f flags
, const char *func
, const int line
)
692 struct sk_buff
*skb_cl
;
693 unsigned int truesize
= skb
->truesize
;
694 unsigned int cl_truesize
= 0;
696 skb_cl
= _rtw_skb_clone(skb
);
698 cl_truesize
= skb_cl
->truesize
;
700 if(!skb_cl
|| cl_truesize
!= truesize
/*|| cl_truesize > 4096*/)
701 DBG_871X("DBG_MEM_ALLOC %s:%d %s(%u), skb_cl:%p, cl_truesize=%u\n", func
, line
, __FUNCTION__
, truesize
, skb_cl
, cl_truesize
);
705 , skb_cl
? MSTAT_ALLOC_SUCCESS
: MSTAT_ALLOC_FAIL
712 inline int dbg_rtw_netif_rx(_nic_hdl ndev
, struct sk_buff
*skb
, const enum mstat_f flags
, const char *func
, int line
)
715 unsigned int truesize
= skb
->truesize
;
717 //if(truesize > 4096)
718 // DBG_871X("DBG_MEM_ALLOC %s:%d %s, truesize=%u\n", func, line, __FUNCTION__, truesize);
720 ret
= _rtw_netif_rx(ndev
, skb
);
731 inline void dbg_rtw_skb_queue_purge(struct sk_buff_head
*list
, enum mstat_f flags
, const char *func
, int line
)
735 while ((skb
= skb_dequeue(list
)) != NULL
)
736 dbg_rtw_skb_free(skb
, flags
, func
, line
);
739 #ifdef CONFIG_USB_HCI
740 inline 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
, int line
)
743 //DBG_871X("DBG_MEM_ALLOC %s:%d %s(%d)\n", func, line, __FUNCTION__, size);
745 p
= _rtw_usb_buffer_alloc(dev
, size
, dma
);
749 , p
? MSTAT_ALLOC_SUCCESS
: MSTAT_ALLOC_FAIL
756 inline 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
, int line
)
758 //DBG_871X("DBG_MEM_ALLOC %s:%d %s(%d)\n", func, line, __FUNCTION__, size);
760 _rtw_usb_buffer_free(dev
, size
, addr
, dma
);
768 #endif /* CONFIG_USB_HCI */
769 #endif /* DBG_MEM_ALLOC */
771 void* rtw_malloc2d(int h
, int w
, int size
)
775 void **a
= (void **) rtw_zmalloc( h
*sizeof(void *) + h
*w
*size
);
778 DBG_871X("%s: alloc memory fail!\n", __FUNCTION__
);
783 a
[j
] = ((char *)(a
+h
)) + j
*w
*size
;
788 void rtw_mfree2d(void *pbuf
, int h
, int w
, int size
)
790 rtw_mfree((u8
*)pbuf
, h
*sizeof(void*) + w
*h
*size
);
793 void _rtw_memcpy(void* dst
, void* src
, u32 sz
)
796 #if defined (PLATFORM_LINUX)|| defined (PLATFORM_FREEBSD)
798 memcpy(dst
, src
, sz
);
802 #ifdef PLATFORM_WINDOWS
804 NdisMoveMemory(dst
, src
, sz
);
810 int _rtw_memcmp(void *dst
, void *src
, u32 sz
)
813 #if defined (PLATFORM_LINUX)|| defined (PLATFORM_FREEBSD)
814 //under Linux/GNU/GLibc, the return value of memcmp for two same mem. chunk is 0
816 if (!(memcmp(dst
, src
, sz
)))
823 #ifdef PLATFORM_WINDOWS
824 //under Windows, the return value of NdisEqualMemory for two same mem. chunk is 1
826 if (NdisEqualMemory (dst
, src
, sz
))
837 void _rtw_memset(void *pbuf
, int c
, u32 sz
)
840 #if defined (PLATFORM_LINUX)|| defined (PLATFORM_FREEBSD)
846 #ifdef PLATFORM_WINDOWS
848 NdisZeroMemory(pbuf
, sz
);
849 if (c
!= 0) memset(pbuf
, c
, sz
);
851 NdisFillMemory(pbuf
, sz
, c
);
857 #ifdef PLATFORM_FREEBSD
858 static inline void __list_add(_list
*pnew
, _list
*pprev
, _list
*pnext
)
865 #endif /* PLATFORM_FREEBSD */
867 void _rtw_init_listhead(_list
*list
)
870 #ifdef PLATFORM_LINUX
872 INIT_LIST_HEAD(list
);
876 #ifdef PLATFORM_FREEBSD
880 #ifdef PLATFORM_WINDOWS
882 NdisInitializeListHead(list
);
890 For the following list_xxx operations,
891 caller must guarantee the atomic context.
892 Otherwise, there will be racing condition.
894 u32
rtw_is_list_empty(_list
*phead
)
897 #ifdef PLATFORM_LINUX
899 if (list_empty(phead
))
905 #ifdef PLATFORM_FREEBSD
907 if (phead
->next
== phead
)
915 #ifdef PLATFORM_WINDOWS
917 if (IsListEmpty(phead
))
927 void rtw_list_insert_head(_list
*plist
, _list
*phead
)
930 #ifdef PLATFORM_LINUX
931 list_add(plist
, phead
);
934 #ifdef PLATFORM_FREEBSD
935 __list_add(plist
, phead
, phead
->next
);
938 #ifdef PLATFORM_WINDOWS
939 InsertHeadList(phead
, plist
);
943 void rtw_list_insert_tail(_list
*plist
, _list
*phead
)
946 #ifdef PLATFORM_LINUX
948 list_add_tail(plist
, phead
);
951 #ifdef PLATFORM_FREEBSD
953 __list_add(plist
, phead
->prev
, phead
);
956 #ifdef PLATFORM_WINDOWS
958 InsertTailList(phead
, plist
);
967 Caller must check if the list is empty before calling rtw_list_delete
972 void _rtw_init_sema(_sema
*sema
, int init_val
)
975 #ifdef PLATFORM_LINUX
977 sema_init(sema
, init_val
);
980 #ifdef PLATFORM_FREEBSD
981 sema_init(sema
, init_val
, "rtw_drv");
983 #ifdef PLATFORM_OS_XP
985 KeInitializeSemaphore(sema
, init_val
, SEMA_UPBND
); // count=0;
989 #ifdef PLATFORM_OS_CE
991 *sema
= CreateSemaphore(NULL
, init_val
, SEMA_UPBND
, NULL
);
996 void _rtw_free_sema(_sema
*sema
)
998 #ifdef PLATFORM_FREEBSD
1001 #ifdef PLATFORM_OS_CE
1007 void _rtw_up_sema(_sema
*sema
)
1010 #ifdef PLATFORM_LINUX
1015 #ifdef PLATFORM_FREEBSD
1018 #ifdef PLATFORM_OS_XP
1020 KeReleaseSemaphore(sema
, IO_NETWORK_INCREMENT
, 1, FALSE
);
1024 #ifdef PLATFORM_OS_CE
1025 ReleaseSemaphore(*sema
, 1, NULL
);
1029 u32
_rtw_down_sema(_sema
*sema
)
1032 #ifdef PLATFORM_LINUX
1034 if (down_interruptible(sema
))
1040 #ifdef PLATFORM_FREEBSD
1044 #ifdef PLATFORM_OS_XP
1046 if(STATUS_SUCCESS
== KeWaitForSingleObject(sema
, Executive
, KernelMode
, TRUE
, NULL
))
1052 #ifdef PLATFORM_OS_CE
1053 if(WAIT_OBJECT_0
== WaitForSingleObject(*sema
, INFINITE
))
1062 void _rtw_mutex_init(_mutex
*pmutex
)
1064 #ifdef PLATFORM_LINUX
1066 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37))
1073 #ifdef PLATFORM_FREEBSD
1074 mtx_init(pmutex
, "", NULL
, MTX_DEF
|MTX_RECURSE
);
1076 #ifdef PLATFORM_OS_XP
1078 KeInitializeMutex(pmutex
, 0);
1082 #ifdef PLATFORM_OS_CE
1083 *pmutex
= CreateMutex( NULL
, _FALSE
, NULL
);
1087 void _rtw_mutex_free(_mutex
*pmutex
);
1088 void _rtw_mutex_free(_mutex
*pmutex
)
1090 #ifdef PLATFORM_LINUX
1092 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37))
1093 mutex_destroy(pmutex
);
1097 #ifdef PLATFORM_FREEBSD
1098 sema_destroy(pmutex
);
1103 #ifdef PLATFORM_OS_XP
1107 #ifdef PLATFORM_OS_CE
1112 void _rtw_spinlock_init(_lock
*plock
)
1115 #ifdef PLATFORM_LINUX
1117 spin_lock_init(plock
);
1120 #ifdef PLATFORM_FREEBSD
1121 mtx_init(plock
, "", NULL
, MTX_DEF
|MTX_RECURSE
);
1123 #ifdef PLATFORM_WINDOWS
1125 NdisAllocateSpinLock(plock
);
1131 void _rtw_spinlock_free(_lock
*plock
)
1133 #ifdef PLATFORM_FREEBSD
1137 #ifdef PLATFORM_WINDOWS
1139 NdisFreeSpinLock(plock
);
1144 #ifdef PLATFORM_FREEBSD
1145 extern PADAPTER prtw_lock
;
1147 void rtw_mtx_lock(_lock
*plock
){
1149 mtx_lock(&prtw_lock
->glock
);
1152 printf("%s prtw_lock==NULL",__FUNCTION__
);
1155 void rtw_mtx_unlock(_lock
*plock
){
1157 mtx_unlock(&prtw_lock
->glock
);
1160 printf("%s prtw_lock==NULL",__FUNCTION__
);
1164 #endif //PLATFORM_FREEBSD
1167 void _rtw_spinlock(_lock
*plock
)
1170 #ifdef PLATFORM_LINUX
1175 #ifdef PLATFORM_FREEBSD
1178 #ifdef PLATFORM_WINDOWS
1180 NdisAcquireSpinLock(plock
);
1186 void _rtw_spinunlock(_lock
*plock
)
1189 #ifdef PLATFORM_LINUX
1194 #ifdef PLATFORM_FREEBSD
1197 #ifdef PLATFORM_WINDOWS
1199 NdisReleaseSpinLock(plock
);
1205 void _rtw_spinlock_ex(_lock
*plock
)
1208 #ifdef PLATFORM_LINUX
1213 #ifdef PLATFORM_FREEBSD
1216 #ifdef PLATFORM_WINDOWS
1218 NdisDprAcquireSpinLock(plock
);
1224 void _rtw_spinunlock_ex(_lock
*plock
)
1227 #ifdef PLATFORM_LINUX
1232 #ifdef PLATFORM_FREEBSD
1235 #ifdef PLATFORM_WINDOWS
1237 NdisDprReleaseSpinLock(plock
);
1244 void _rtw_init_queue(_queue
*pqueue
)
1247 _rtw_init_listhead(&(pqueue
->queue
));
1249 _rtw_spinlock_init(&(pqueue
->lock
));
1253 u32
_rtw_queue_empty(_queue
*pqueue
)
1255 return (rtw_is_list_empty(&(pqueue
->queue
)));
1259 u32
rtw_end_of_queue_search(_list
*head
, _list
*plist
)
1268 u32
rtw_get_current_time(void)
1271 #ifdef PLATFORM_LINUX
1274 #ifdef PLATFORM_FREEBSD
1279 #ifdef PLATFORM_WINDOWS
1280 LARGE_INTEGER SystemTime
;
1281 NdisGetCurrentSystemTime(&SystemTime
);
1282 return (u32
)(SystemTime
.LowPart
);// count of 100-nanosecond intervals
1286 inline u32
rtw_systime_to_ms(u32 systime
)
1288 #ifdef PLATFORM_LINUX
1289 return systime
* 1000 / HZ
;
1291 #ifdef PLATFORM_FREEBSD
1292 return systime
* 1000;
1294 #ifdef PLATFORM_WINDOWS
1295 return systime
/ 10000 ;
1299 inline u32
rtw_ms_to_systime(u32 ms
)
1301 #ifdef PLATFORM_LINUX
1302 return ms
* HZ
/ 1000;
1304 #ifdef PLATFORM_FREEBSD
1307 #ifdef PLATFORM_WINDOWS
1312 // the input parameter start use the same unit as returned by rtw_get_current_time
1313 inline s32
rtw_get_passing_time_ms(u32 start
)
1315 #ifdef PLATFORM_LINUX
1316 return rtw_systime_to_ms(jiffies
-start
);
1318 #ifdef PLATFORM_FREEBSD
1319 return rtw_systime_to_ms(rtw_get_current_time());
1321 #ifdef PLATFORM_WINDOWS
1322 LARGE_INTEGER SystemTime
;
1323 NdisGetCurrentSystemTime(&SystemTime
);
1324 return rtw_systime_to_ms((u32
)(SystemTime
.LowPart
) - start
) ;
1328 inline s32
rtw_get_time_interval_ms(u32 start
, u32 end
)
1330 #ifdef PLATFORM_LINUX
1331 return rtw_systime_to_ms(end
-start
);
1333 #ifdef PLATFORM_FREEBSD
1334 return rtw_systime_to_ms(rtw_get_current_time());
1336 #ifdef PLATFORM_WINDOWS
1337 return rtw_systime_to_ms(end
-start
);
1342 void rtw_sleep_schedulable(int ms
)
1345 #ifdef PLATFORM_LINUX
1349 delta
= (ms
* HZ
)/1000;//(ms)
1353 set_current_state(TASK_INTERRUPTIBLE
);
1354 if (schedule_timeout(delta
) != 0) {
1360 #ifdef PLATFORM_FREEBSD
1365 #ifdef PLATFORM_WINDOWS
1367 NdisMSleep(ms
*1000); //(us)*1000=(ms)
1374 void rtw_msleep_os(int ms
)
1377 #ifdef PLATFORM_LINUX
1379 msleep((unsigned int)ms
);
1382 #ifdef PLATFORM_FREEBSD
1383 //Delay for delay microseconds
1387 #ifdef PLATFORM_WINDOWS
1389 NdisMSleep(ms
*1000); //(us)*1000=(ms)
1395 void rtw_usleep_os(int us
)
1398 #ifdef PLATFORM_LINUX
1400 // msleep((unsigned int)us);
1401 if ( 1 < (us
/1000) )
1404 msleep( (us
/1000) + 1);
1407 #ifdef PLATFORM_FREEBSD
1408 //Delay for delay microseconds
1413 #ifdef PLATFORM_WINDOWS
1415 NdisMSleep(us
); //(us)
1424 void _rtw_mdelay_os(int ms
, const char *func
, const int line
)
1428 DBG_871X("%s:%d %s(%d)\n", func
, line
, __FUNCTION__
, ms
);
1434 DBG_871X("%s:%d %s(%d)\n", func
, line
, __FUNCTION__
, ms
);
1436 #if defined(PLATFORM_LINUX)
1438 mdelay((unsigned long)ms
);
1440 #elif defined(PLATFORM_WINDOWS)
1442 NdisStallExecution(ms
*1000); //(us)*1000=(ms)
1448 void _rtw_udelay_os(int us
, const char *func
, const int line
)
1453 DBG_871X("%s:%d %s(%d)\n", func
, line
, __FUNCTION__
, us
);
1460 DBG_871X("%s:%d %s(%d)\n", func
, line
, __FUNCTION__
, us
);
1463 #if defined(PLATFORM_LINUX)
1465 udelay((unsigned long)us
);
1467 #elif defined(PLATFORM_WINDOWS)
1469 NdisStallExecution(us
); //(us)
1475 void rtw_mdelay_os(int ms
)
1478 #ifdef PLATFORM_LINUX
1480 mdelay((unsigned long)ms
);
1483 #ifdef PLATFORM_FREEBSD
1487 #ifdef PLATFORM_WINDOWS
1489 NdisStallExecution(ms
*1000); //(us)*1000=(ms)
1495 void rtw_udelay_os(int us
)
1498 #ifdef PLATFORM_LINUX
1500 udelay((unsigned long)us
);
1503 #ifdef PLATFORM_FREEBSD
1504 //Delay for delay microseconds
1508 #ifdef PLATFORM_WINDOWS
1510 NdisStallExecution(us
); //(us)
1519 #ifdef PLATFORM_LINUX
1522 #ifdef PLATFORM_FREEBSD
1525 #ifdef PLATFORM_WINDOWS
1530 #define RTW_SUSPEND_LOCK_NAME "rtw_wifi"
1532 #ifdef CONFIG_WAKELOCK
1533 static struct wake_lock rtw_suspend_lock
;
1534 #elif defined(CONFIG_ANDROID_POWER)
1535 static android_suspend_lock_t rtw_suspend_lock
={
1536 .name
= RTW_SUSPEND_LOCK_NAME
1540 inline void rtw_suspend_lock_init()
1542 #ifdef CONFIG_WAKELOCK
1543 wake_lock_init(&rtw_suspend_lock
, WAKE_LOCK_SUSPEND
, RTW_SUSPEND_LOCK_NAME
);
1544 #elif defined(CONFIG_ANDROID_POWER)
1545 android_init_suspend_lock(&rtw_suspend_lock
);
1549 inline void rtw_suspend_lock_uninit()
1551 #ifdef CONFIG_WAKELOCK
1552 wake_lock_destroy(&rtw_suspend_lock
);
1553 #elif defined(CONFIG_ANDROID_POWER)
1554 android_uninit_suspend_lock(&rtw_suspend_lock
);
1558 inline void rtw_lock_suspend()
1560 #ifdef CONFIG_WAKELOCK
1561 wake_lock(&rtw_suspend_lock
);
1562 #elif defined(CONFIG_ANDROID_POWER)
1563 android_lock_suspend(&rtw_suspend_lock
);
1567 inline void rtw_unlock_suspend()
1569 #ifdef CONFIG_WAKELOCK
1570 wake_unlock(&rtw_suspend_lock
);
1571 #elif defined(CONFIG_ANDROID_POWER)
1572 android_unlock_suspend(&rtw_suspend_lock
);
1576 inline void rtw_lock_suspend_timeout(u32 timeout_ms
)
1578 #ifdef CONFIG_WAKELOCK
1579 wake_lock_timeout(&rtw_suspend_lock
, rtw_ms_to_systime(timeout_ms
));
1580 #elif defined(CONFIG_ANDROID_POWER)
1581 android_lock_suspend_auto_expire(&rtw_suspend_lock
, rtw_ms_to_systime(timeout_ms
));
1585 inline void ATOMIC_SET(ATOMIC_T
*v
, int i
)
1587 #ifdef PLATFORM_LINUX
1589 #elif defined(PLATFORM_WINDOWS)
1590 *v
=i
;// other choice????
1591 #elif defined(PLATFORM_FREEBSD)
1592 atomic_set_int(v
,i
);
1596 inline int ATOMIC_READ(ATOMIC_T
*v
)
1598 #ifdef PLATFORM_LINUX
1599 return atomic_read(v
);
1600 #elif defined(PLATFORM_WINDOWS)
1601 return *v
; // other choice????
1602 #elif defined(PLATFORM_FREEBSD)
1603 return atomic_load_acq_32(v
);
1607 inline void ATOMIC_ADD(ATOMIC_T
*v
, int i
)
1609 #ifdef PLATFORM_LINUX
1611 #elif defined(PLATFORM_WINDOWS)
1612 InterlockedAdd(v
,i
);
1613 #elif defined(PLATFORM_FREEBSD)
1614 atomic_add_int(v
,i
);
1617 inline void ATOMIC_SUB(ATOMIC_T
*v
, int i
)
1619 #ifdef PLATFORM_LINUX
1621 #elif defined(PLATFORM_WINDOWS)
1622 InterlockedAdd(v
,-i
);
1623 #elif defined(PLATFORM_FREEBSD)
1624 atomic_subtract_int(v
,i
);
1628 inline void ATOMIC_INC(ATOMIC_T
*v
)
1630 #ifdef PLATFORM_LINUX
1632 #elif defined(PLATFORM_WINDOWS)
1633 InterlockedIncrement(v
);
1634 #elif defined(PLATFORM_FREEBSD)
1635 atomic_add_int(v
,1);
1639 inline void ATOMIC_DEC(ATOMIC_T
*v
)
1641 #ifdef PLATFORM_LINUX
1643 #elif defined(PLATFORM_WINDOWS)
1644 InterlockedDecrement(v
);
1645 #elif defined(PLATFORM_FREEBSD)
1646 atomic_subtract_int(v
,1);
1650 inline int ATOMIC_ADD_RETURN(ATOMIC_T
*v
, int i
)
1652 #ifdef PLATFORM_LINUX
1653 return atomic_add_return(i
,v
);
1654 #elif defined(PLATFORM_WINDOWS)
1655 return InterlockedAdd(v
,i
);
1656 #elif defined(PLATFORM_FREEBSD)
1657 atomic_add_int(v
,i
);
1658 return atomic_load_acq_32(v
);
1662 inline int ATOMIC_SUB_RETURN(ATOMIC_T
*v
, int i
)
1664 #ifdef PLATFORM_LINUX
1665 return atomic_sub_return(i
,v
);
1666 #elif defined(PLATFORM_WINDOWS)
1667 return InterlockedAdd(v
,-i
);
1668 #elif defined(PLATFORM_FREEBSD)
1669 atomic_subtract_int(v
,i
);
1670 return atomic_load_acq_32(v
);
1674 inline int ATOMIC_INC_RETURN(ATOMIC_T
*v
)
1676 #ifdef PLATFORM_LINUX
1677 return atomic_inc_return(v
);
1678 #elif defined(PLATFORM_WINDOWS)
1679 return InterlockedIncrement(v
);
1680 #elif defined(PLATFORM_FREEBSD)
1681 atomic_add_int(v
,1);
1682 return atomic_load_acq_32(v
);
1686 inline int ATOMIC_DEC_RETURN(ATOMIC_T
*v
)
1688 #ifdef PLATFORM_LINUX
1689 return atomic_dec_return(v
);
1690 #elif defined(PLATFORM_WINDOWS)
1691 return InterlockedDecrement(v
);
1692 #elif defined(PLATFORM_FREEBSD)
1693 atomic_subtract_int(v
,1);
1694 return atomic_load_acq_32(v
);
1699 #ifdef PLATFORM_LINUX
1701 * Open a file with the specific @param path, @param flag, @param mode
1702 * @param fpp the pointer of struct file pointer to get struct file pointer while file opening is success
1703 * @param path the path of the file to open
1704 * @param flag file operation flags, please refer to linux document
1705 * @param mode please refer to linux document
1706 * @return Linux specific error code
1708 static int openFile(struct file
**fpp
, char *path
, int flag
, int mode
)
1712 fp
=filp_open(path
, flag
, mode
);
1724 * Close the file with the specific @param fp
1725 * @param fp the pointer of struct file to close
1728 static int closeFile(struct file
*fp
)
1730 filp_close(fp
,NULL
);
1734 static int readFile(struct file
*fp
,char *buf
,int len
)
1738 if (!fp
->f_op
|| !fp
->f_op
->read
)
1742 rlen
=fp
->f_op
->read(fp
,buf
+sum
,len
-sum
, &fp
->f_pos
);
1755 static int writeFile(struct file
*fp
,char *buf
,int len
)
1759 if (!fp
->f_op
|| !fp
->f_op
->write
)
1763 wlen
=fp
->f_op
->write(fp
,buf
+sum
,len
-sum
, &fp
->f_pos
);
1777 * Test if the specifi @param path is a file and readable
1778 * @param path the path of the file to test
1779 * @return Linux specific error code
1781 static int isFileReadable(char *path
)
1788 fp
=filp_open(path
, O_RDONLY
, 0);
1793 oldfs
= get_fs(); set_fs(get_ds());
1795 if(1!=readFile(fp
, &buf
, 1))
1799 filp_close(fp
,NULL
);
1805 * Open the file with @param path and retrive the file content into memory starting from @param buf for @param sz at most
1806 * @param path the path of the file to open and read
1807 * @param buf the starting address of the buffer to store file content
1808 * @param sz how many bytes to read at most
1809 * @return the byte we've read, or Linux specific error code
1811 static int retriveFromFile(char *path
, u8
* buf
, u32 sz
)
1818 if( 0 == (ret
=openFile(&fp
,path
, O_RDONLY
, 0)) ){
1819 DBG_871X("%s openFile path:%s fp=%p\n",__FUNCTION__
, path
,fp
);
1821 oldfs
= get_fs(); set_fs(get_ds());
1822 ret
=readFile(fp
, buf
, sz
);
1826 DBG_871X("%s readFile, ret:%d\n",__FUNCTION__
, ret
);
1829 DBG_871X("%s openFile path:%s Fail, ret:%d\n",__FUNCTION__
, path
, ret
);
1832 DBG_871X("%s NULL pointer\n",__FUNCTION__
);
1839 * Open the file with @param path and wirte @param sz byte of data starting from @param buf into the file
1840 * @param path the path of the file to open and write
1841 * @param buf the starting address of the data to write into file
1842 * @param sz how many bytes to write at most
1843 * @return the byte we've written, or Linux specific error code
1845 static int storeToFile(char *path
, u8
* buf
, u32 sz
)
1852 if( 0 == (ret
=openFile(&fp
, path
, O_CREAT
|O_WRONLY
, 0666)) ) {
1853 DBG_871X("%s openFile path:%s fp=%p\n",__FUNCTION__
, path
,fp
);
1855 oldfs
= get_fs(); set_fs(get_ds());
1856 ret
=writeFile(fp
, buf
, sz
);
1860 DBG_871X("%s writeFile, ret:%d\n",__FUNCTION__
, ret
);
1863 DBG_871X("%s openFile path:%s Fail, ret:%d\n",__FUNCTION__
, path
, ret
);
1866 DBG_871X("%s NULL pointer\n",__FUNCTION__
);
1871 #endif //PLATFORM_LINUX
1874 * Test if the specifi @param path is a file and readable
1875 * @param path the path of the file to test
1876 * @return _TRUE or _FALSE
1878 int rtw_is_file_readable(char *path
)
1880 #ifdef PLATFORM_LINUX
1881 if(isFileReadable(path
) == 0)
1892 * Open the file with @param path and retrive the file content into memory starting from @param buf for @param sz at most
1893 * @param path the path of the file to open and read
1894 * @param buf the starting address of the buffer to store file content
1895 * @param sz how many bytes to read at most
1896 * @return the byte we've read
1898 int rtw_retrive_from_file(char *path
, u8
* buf
, u32 sz
)
1900 #ifdef PLATFORM_LINUX
1901 int ret
=retriveFromFile(path
, buf
, sz
);
1902 return ret
>=0?ret
:0;
1910 * Open the file with @param path and wirte @param sz byte of data starting from @param buf into the file
1911 * @param path the path of the file to open and write
1912 * @param buf the starting address of the data to write into file
1913 * @param sz how many bytes to write at most
1914 * @return the byte we've written
1916 int rtw_store_to_file(char *path
, u8
* buf
, u32 sz
)
1918 #ifdef PLATFORM_LINUX
1919 int ret
=storeToFile(path
, buf
, sz
);
1920 return ret
>=0?ret
:0;
1927 #if 1 //#ifdef MEM_ALLOC_REFINE_ADAPTOR
1928 #ifdef PLATFORM_LINUX
1929 struct net_device
*rtw_alloc_etherdev_with_old_priv(int sizeof_priv
, void *old_priv
)
1931 struct net_device
*pnetdev
;
1932 struct rtw_netdev_priv_indicator
*pnpi
;
1934 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
1935 pnetdev
= alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator
), 4);
1937 pnetdev
= alloc_etherdev(sizeof(struct rtw_netdev_priv_indicator
));
1942 pnpi
= netdev_priv(pnetdev
);
1943 pnpi
->priv
=old_priv
;
1944 pnpi
->sizeof_priv
=sizeof_priv
;
1950 struct net_device
*rtw_alloc_etherdev(int sizeof_priv
)
1952 struct net_device
*pnetdev
;
1953 struct rtw_netdev_priv_indicator
*pnpi
;
1955 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
1956 pnetdev
= alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator
), 4);
1958 pnetdev
= alloc_etherdev(sizeof(struct rtw_netdev_priv_indicator
));
1963 pnpi
= netdev_priv(pnetdev
);
1965 pnpi
->priv
= rtw_zvmalloc(sizeof_priv
);
1967 free_netdev(pnetdev
);
1972 pnpi
->sizeof_priv
=sizeof_priv
;
1977 void rtw_free_netdev(struct net_device
* netdev
)
1979 struct rtw_netdev_priv_indicator
*pnpi
;
1984 pnpi
= netdev_priv(netdev
);
1989 rtw_vmfree(pnpi
->priv
, pnpi
->sizeof_priv
);
1990 free_netdev(netdev
);
1997 * Jeff: this function should be called under ioctl (rtnl_lock is accquired) while
1998 * LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
2000 int rtw_change_ifname(_adapter
*padapter
, const char *ifname
)
2002 struct net_device
*pnetdev
;
2003 struct net_device
*cur_pnetdev
= padapter
->pnetdev
;
2004 struct rereg_nd_name_data
*rereg_priv
;
2010 rereg_priv
= &padapter
->rereg_nd_name_priv
;
2012 //free the old_pnetdev
2013 if(rereg_priv
->old_pnetdev
) {
2014 free_netdev(rereg_priv
->old_pnetdev
);
2015 rereg_priv
->old_pnetdev
= NULL
;
2018 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26))
2019 if(!rtnl_is_locked())
2020 unregister_netdev(cur_pnetdev
);
2023 unregister_netdevice(cur_pnetdev
);
2025 rtw_proc_remove_one(cur_pnetdev
);
2027 rereg_priv
->old_pnetdev
=cur_pnetdev
;
2029 pnetdev
= rtw_init_netdev(padapter
);
2035 SET_NETDEV_DEV(pnetdev
, dvobj_to_dev(adapter_to_dvobj(padapter
)));
2037 rtw_init_netdev_name(pnetdev
, ifname
);
2039 _rtw_memcpy(pnetdev
->dev_addr
, padapter
->eeprompriv
.mac_addr
, ETH_ALEN
);
2041 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26))
2042 if(!rtnl_is_locked())
2043 ret
= register_netdev(pnetdev
);
2046 ret
= register_netdevice(pnetdev
);
2049 RT_TRACE(_module_hci_intfs_c_
,_drv_err_
,("register_netdev() failed\n"));
2053 rtw_proc_init_one(pnetdev
);
2063 #endif //MEM_ALLOC_REFINE_ADAPTOR
2065 #ifdef PLATFORM_FREEBSD
2067 * Copy a buffer from userspace and write into kernel address
2070 * This emulation just calls the FreeBSD copyin function (to
2071 * copy data from user space buffer into a kernel space buffer)
2072 * and is designed to be used with the above io_write_wrapper.
2074 * This function should return the number of bytes not copied.
2075 * I.e. success results in a zero value.
2076 * Negative error values are not returned.
2079 copy_from_user(void *to
, const void *from
, unsigned long n
)
2081 if ( copyin(from
, to
, n
) != 0 ) {
2082 /* Any errors will be treated as a failure
2083 to copy any of the requested bytes */
2091 copy_to_user(void *to
, const void *from
, unsigned long n
)
2093 if ( copyout(from
, to
, n
) != 0 ) {
2094 /* Any errors will be treated as a failure
2095 to copy any of the requested bytes */
2104 * The usb_register and usb_deregister functions are used to register
2105 * usb drivers with the usb subsystem. In this compatibility layer
2106 * emulation a list of drivers (struct usb_driver) is maintained
2107 * and is used for probing/attaching etc.
2109 * usb_register and usb_deregister simply call these functions.
2112 usb_register(struct usb_driver
*driver
)
2114 rtw_usb_linux_register(driver
);
2120 usb_deregister(struct usb_driver
*driver
)
2122 rtw_usb_linux_deregister(driver
);
2126 void module_init_exit_wrapper(void *arg
)
2128 int (*func
)(void) = arg
;
2133 #endif //PLATFORM_FREEBSD
2134 u64
rtw_modular64(u64 x
, u64 y
)
2136 #ifdef PLATFORM_LINUX
2137 return do_div(x
, y
);
2138 #elif defined(PLATFORM_WINDOWS)
2140 #elif defined(PLATFORM_FREEBSD)
2145 u64
rtw_division64(u64 x
, u64 y
)
2147 #ifdef PLATFORM_LINUX
2150 #elif defined(PLATFORM_WINDOWS)
2152 #elif defined(PLATFORM_FREEBSD)
2157 void rtw_buf_free(u8
**buf
, u32
*buf_len
)
2161 if (!buf
|| !buf_len
)
2167 u32 tmp_buf_len
= *buf_len
;
2169 rtw_mfree(*buf
, tmp_buf_len
);
2174 void rtw_buf_update(u8
**buf
, u32
*buf_len
, u8
*src
, u32 src_len
)
2176 u32 ori_len
= 0, dup_len
= 0;
2180 if (!buf
|| !buf_len
)
2183 if (!src
|| !src_len
)
2187 dup
= rtw_malloc(src_len
);
2190 _rtw_memcpy(dup
, src
, dup_len
);
2197 /* replace buf with dup */
2203 if (ori
&& ori_len
> 0)
2204 rtw_mfree(ori
, ori_len
);
2209 * rtw_cbuf_full - test if cbuf is full
2210 * @cbuf: pointer of struct rtw_cbuf
2212 * Returns: _TRUE if cbuf is full
2214 inline bool rtw_cbuf_full(struct rtw_cbuf
*cbuf
)
2216 return (cbuf
->write
== cbuf
->read
-1)? _TRUE
: _FALSE
;
2220 * rtw_cbuf_empty - test if cbuf is empty
2221 * @cbuf: pointer of struct rtw_cbuf
2223 * Returns: _TRUE if cbuf is empty
2225 inline bool rtw_cbuf_empty(struct rtw_cbuf
*cbuf
)
2227 return (cbuf
->write
== cbuf
->read
)? _TRUE
: _FALSE
;
2231 * rtw_cbuf_push - push a pointer into cbuf
2232 * @cbuf: pointer of struct rtw_cbuf
2233 * @buf: pointer to push in
2235 * Lock free operation, be careful of the use scheme
2236 * Returns: _TRUE push success
2238 bool rtw_cbuf_push(struct rtw_cbuf
*cbuf
, void *buf
)
2240 if (rtw_cbuf_full(cbuf
))
2244 DBG_871X("%s on %u\n", __func__
, cbuf
->write
);
2245 cbuf
->bufs
[cbuf
->write
] = buf
;
2246 cbuf
->write
= (cbuf
->write
+1)%cbuf
->size
;
2252 * rtw_cbuf_pop - pop a pointer from cbuf
2253 * @cbuf: pointer of struct rtw_cbuf
2255 * Lock free operation, be careful of the use scheme
2256 * Returns: pointer popped out
2258 void *rtw_cbuf_pop(struct rtw_cbuf
*cbuf
)
2261 if (rtw_cbuf_empty(cbuf
))
2265 DBG_871X("%s on %u\n", __func__
, cbuf
->read
);
2266 buf
= cbuf
->bufs
[cbuf
->read
];
2267 cbuf
->read
= (cbuf
->read
+1)%cbuf
->size
;
2273 * rtw_cbuf_alloc - allocte a rtw_cbuf with given size and do initialization
2274 * @size: size of pointer
2276 * Returns: pointer of srtuct rtw_cbuf, NULL for allocation failure
2278 struct rtw_cbuf
*rtw_cbuf_alloc(u32 size
)
2280 struct rtw_cbuf
*cbuf
;
2282 cbuf
= (struct rtw_cbuf
*)rtw_malloc(sizeof(*cbuf
) + sizeof(void*)*size
);
2285 cbuf
->write
= cbuf
->read
= 0;
2293 * rtw_cbuf_free - free the given rtw_cbuf
2294 * @cbuf: pointer of struct rtw_cbuf to free
2296 void rtw_cbuf_free(struct rtw_cbuf
*cbuf
)
2298 rtw_mfree((u8
*)cbuf
, sizeof(*cbuf
) + sizeof(void*)*cbuf
->size
);