add rk3288 pinctrl dts code
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / rtl8192cu / include / osdep_service.h
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2011 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
23 #include <drv_conf.h>
24 #include <basic_types.h>
25 //#include <rtl871x_byteorder.h>
26
27 #define _FAIL           0
28 #define _SUCCESS        1
29 #define RTW_RX_HANDLED 2
30 //#define RTW_STATUS_TIMEDOUT -110
31
32 #undef _TRUE
33 #define _TRUE           1
34
35 #undef _FALSE
36 #define _FALSE          0
37         
38
39 #ifdef PLATFORM_FREEBSD
40 #include <sys/cdefs.h>
41 #include <sys/types.h>
42 #include <sys/systm.h>
43 #include <sys/param.h>
44 #include <sys/sockio.h>
45 #include <sys/sysctl.h>
46 #include <sys/lock.h>
47 #include <sys/mutex.h>
48 #include <sys/mbuf.h>
49 #include <sys/kernel.h>
50 #include <sys/socket.h>
51 #include <sys/systm.h>
52 #include <sys/malloc.h>
53 #include <sys/module.h>
54 #include <sys/bus.h>
55 #include <sys/endian.h>
56 #include <sys/kdb.h>
57 #include <sys/kthread.h>
58 #include <machine/atomic.h>
59
60 #include <machine/bus.h>
61 #include <machine/resource.h>
62 #include <sys/rman.h>
63
64 #include <net/bpf.h>
65 #include <net/if.h>
66 #include <net/if_arp.h>
67 #include <net/ethernet.h>
68 #include <net/if_dl.h>
69 #include <net/if_media.h>
70 #include <net/if_types.h>
71 #include <net/route.h>
72
73
74 #include <netinet/in.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/in_var.h>
77 #include <netinet/if_ether.h>
78
79 #include <net80211/ieee80211_var.h>
80 #include <net80211/ieee80211_regdomain.h>
81 #include <net80211/ieee80211_radiotap.h>
82 #include <net80211/ieee80211_ratectl.h>
83
84 #include <dev/usb/usb.h>
85 #include <dev/usb/usbdi.h>
86 #include "usbdevs.h"
87
88 #define USB_DEBUG_VAR rum_debug
89 #include <dev/usb/usb_debug.h>
90
91 #if 1 //Baron porting from linux, it's all temp solution, needs to check again
92 #include <sys/sema.h>
93 #include <sys/pcpu.h> /* XXX for PCPU_GET */
94 //      typedef struct  semaphore _sema;
95         typedef struct  sema _sema;
96 //      typedef spinlock_t      _lock;
97         typedef struct mtx      _lock;
98         typedef struct mtx              _mutex;
99         typedef struct timer_list _timer;
100         struct list_head {
101         struct list_head *next, *prev;
102         };
103         struct  __queue {
104                 struct  list_head       queue;  
105                 _lock   lock;
106         };
107
108         //typedef       struct sk_buff  _pkt;
109         typedef struct mbuf     _pkt;
110         typedef struct mbuf     _buffer;
111         
112         typedef struct  __queue _queue;
113         typedef struct  list_head       _list;
114         typedef int     _OS_STATUS;
115         //typedef u32   _irqL;
116         typedef unsigned long _irqL;
117         typedef struct  ifnet * _nic_hdl;
118         
119         typedef pid_t           _thread_hdl_;
120 //      typedef struct thread           _thread_hdl_;
121         typedef void            thread_return;
122         typedef void*   thread_context;
123
124         //#define thread_exit() complete_and_exit(NULL, 0)
125
126         typedef void timer_hdl_return;
127         typedef void* timer_hdl_context;
128         typedef struct work_struct _workitem;
129
130 #define   KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
131 /* emulate a modern version */
132 #define LINUX_VERSION_CODE KERNEL_VERSION(2, 6, 35)
133
134 #define WIRELESS_EXT -1
135 #define HZ hz
136 #define spin_lock_irqsave mtx_lock_irqsave
137 #define spin_lock_bh mtx_lock_irqsave
138 #define mtx_lock_irqsave(lock, x) mtx_lock(lock)//{local_irq_save((x)); mtx_lock_spin((lock));}
139 //#define IFT_RTW       0xf9 //ifnet allocate type for RTW
140 #define free_netdev if_free
141 #define LIST_CONTAINOR(ptr, type, member) \
142         ((type *)((char *)(ptr)-(SIZE_T)(&((type *)0)->member)))
143 #define container_of(p,t,n) (t*)((p)-&(((t*)0)->n))
144 /* 
145  * Linux timers are emulated using FreeBSD callout functions
146  * (and taskqueue functionality).
147  *
148  * Currently no timer stats functionality.
149  *
150  * See (linux_compat) processes.c
151  *
152  */
153 struct timer_list {
154
155         /* FreeBSD callout related fields */
156         struct callout callout;
157
158         //timeout function
159         void (*function)(void*);
160         //argument
161          void *arg;
162         
163 };
164 struct workqueue_struct;
165 struct work_struct;
166 typedef void (*work_func_t)(struct work_struct *work);
167 /* Values for the state of an item of work (work_struct) */
168 typedef enum work_state {
169         WORK_STATE_UNSET = 0,
170         WORK_STATE_CALLOUT_PENDING = 1,
171         WORK_STATE_TASK_PENDING = 2,
172         WORK_STATE_WORK_CANCELLED = 3        
173 } work_state_t;
174
175 struct work_struct {
176         struct task task; /* FreeBSD task */
177         work_state_t state; /* the pending or otherwise state of work. */
178         work_func_t func;       
179 };
180 #define spin_unlock_irqrestore mtx_unlock_irqrestore
181 #define spin_unlock_bh mtx_unlock_irqrestore
182 #define mtx_unlock_irqrestore(lock,x)    mtx_unlock(lock);
183 extern void     _rtw_spinlock_init(_lock *plock);
184
185 //modify private structure to match freebsd
186 #define BITS_PER_LONG 32
187 union ktime {
188         s64     tv64;
189 #if BITS_PER_LONG != 64 && !defined(CONFIG_KTIME_SCALAR)
190         struct {
191 #ifdef __BIG_ENDIAN
192         s32     sec, nsec;
193 #else
194         s32     nsec, sec;
195 #endif
196         } tv;
197 #endif
198 };
199 #define kmemcheck_bitfield_begin(name)
200 #define kmemcheck_bitfield_end(name)
201 #define CHECKSUM_NONE 0
202 typedef unsigned char *sk_buff_data_t;
203 typedef union ktime ktime_t;            /* Kill this */
204
205 void rtw_mtx_lock(_lock *plock);
206         
207 void rtw_mtx_unlock(_lock *plock);
208
209 /** 
210  *      struct sk_buff - socket buffer
211  *      @next: Next buffer in list
212  *      @prev: Previous buffer in list
213  *      @sk: Socket we are owned by
214  *      @tstamp: Time we arrived
215  *      @dev: Device we arrived on/are leaving by
216  *      @transport_header: Transport layer header
217  *      @network_header: Network layer header
218  *      @mac_header: Link layer header
219  *      @_skb_refdst: destination entry (with norefcount bit)
220  *      @sp: the security path, used for xfrm
221  *      @cb: Control buffer. Free for use by every layer. Put private vars here
222  *      @len: Length of actual data
223  *      @data_len: Data length
224  *      @mac_len: Length of link layer header
225  *      @hdr_len: writable header length of cloned skb
226  *      @csum: Checksum (must include start/offset pair)
227  *      @csum_start: Offset from skb->head where checksumming should start
228  *      @csum_offset: Offset from csum_start where checksum should be stored
229  *      @local_df: allow local fragmentation
230  *      @cloned: Head may be cloned (check refcnt to be sure)
231  *      @nohdr: Payload reference only, must not modify header
232  *      @pkt_type: Packet class
233  *      @fclone: skbuff clone status
234  *      @ip_summed: Driver fed us an IP checksum
235  *      @priority: Packet queueing priority
236  *      @users: User count - see {datagram,tcp}.c
237  *      @protocol: Packet protocol from driver
238  *      @truesize: Buffer size 
239  *      @head: Head of buffer
240  *      @data: Data head pointer
241  *      @tail: Tail pointer
242  *      @end: End pointer
243  *      @destructor: Destruct function
244  *      @mark: Generic packet mark
245  *      @nfct: Associated connection, if any
246  *      @ipvs_property: skbuff is owned by ipvs
247  *      @peeked: this packet has been seen already, so stats have been
248  *              done for it, don't do them again
249  *      @nf_trace: netfilter packet trace flag
250  *      @nfctinfo: Relationship of this skb to the connection
251  *      @nfct_reasm: netfilter conntrack re-assembly pointer
252  *      @nf_bridge: Saved data about a bridged frame - see br_netfilter.c
253  *      @skb_iif: ifindex of device we arrived on
254  *      @rxhash: the packet hash computed on receive
255  *      @queue_mapping: Queue mapping for multiqueue devices
256  *      @tc_index: Traffic control index
257  *      @tc_verd: traffic control verdict
258  *      @ndisc_nodetype: router type (from link layer)
259  *      @dma_cookie: a cookie to one of several possible DMA operations
260  *              done by skb DMA functions
261  *      @secmark: security marking
262  *      @vlan_tci: vlan tag control information
263  */
264
265 struct sk_buff {
266         /* These two members must be first. */
267         struct sk_buff          *next;
268         struct sk_buff          *prev;
269
270         ktime_t                 tstamp;
271
272         struct sock             *sk;
273         //struct net_device     *dev;
274         struct ifnet *dev;
275
276         /*
277          * This is the control buffer. It is free to use for every
278          * layer. Please put your private variables there. If you
279          * want to keep them across layers you have to do a skb_clone()
280          * first. This is owned by whoever has the skb queued ATM.
281          */
282         char                    cb[48] __aligned(8);
283
284         unsigned long           _skb_refdst;
285 #ifdef CONFIG_XFRM
286         struct  sec_path        *sp;
287 #endif
288         unsigned int            len,
289                                 data_len;
290         u16                     mac_len,
291                                 hdr_len;
292         union {
293                 u32             csum;
294                 struct {
295                         u16     csum_start;
296                         u16     csum_offset;
297                 }smbol2;
298         }smbol1;
299         u32                     priority;
300         kmemcheck_bitfield_begin(flags1);
301         u8                      local_df:1,
302                                 cloned:1,
303                                 ip_summed:2,
304                                 nohdr:1,
305                                 nfctinfo:3;
306         u8                      pkt_type:3,
307                                 fclone:2,
308                                 ipvs_property:1,
309                                 peeked:1,
310                                 nf_trace:1;
311         kmemcheck_bitfield_end(flags1);
312         u16                     protocol;
313
314         void                    (*destructor)(struct sk_buff *skb);
315 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
316         struct nf_conntrack     *nfct;
317         struct sk_buff          *nfct_reasm;
318 #endif
319 #ifdef CONFIG_BRIDGE_NETFILTER
320         struct nf_bridge_info   *nf_bridge;
321 #endif
322
323         int                     skb_iif;
324 #ifdef CONFIG_NET_SCHED
325         u16                     tc_index;       /* traffic control index */
326 #ifdef CONFIG_NET_CLS_ACT
327         u16                     tc_verd;        /* traffic control verdict */
328 #endif
329 #endif
330
331         u32                     rxhash;
332
333         kmemcheck_bitfield_begin(flags2);
334         u16                     queue_mapping:16;
335 #ifdef CONFIG_IPV6_NDISC_NODETYPE
336         u8                      ndisc_nodetype:2,
337                                 deliver_no_wcard:1;
338 #else
339         u8                      deliver_no_wcard:1;
340 #endif
341         kmemcheck_bitfield_end(flags2);
342
343         /* 0/14 bit hole */
344
345 #ifdef CONFIG_NET_DMA
346         dma_cookie_t            dma_cookie;
347 #endif
348 #ifdef CONFIG_NETWORK_SECMARK
349         u32                     secmark;
350 #endif
351         union {
352                 u32             mark;
353                 u32             dropcount;
354         }symbol3;
355
356         u16                     vlan_tci;
357
358         sk_buff_data_t          transport_header;
359         sk_buff_data_t          network_header;
360         sk_buff_data_t          mac_header;
361         /* These elements must be at the end, see alloc_skb() for details.  */
362         sk_buff_data_t          tail;
363         sk_buff_data_t          end;
364         unsigned char           *head,
365                                 *data;
366         unsigned int            truesize;
367         atomic_t                users;
368 };
369 struct sk_buff_head {
370         /* These two members must be first. */
371         struct sk_buff  *next;
372         struct sk_buff  *prev;
373
374         u32             qlen;
375         _lock   lock;
376 };
377 #define skb_tail_pointer(skb)   skb->tail
378 static inline unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
379 {
380         unsigned char *tmp = skb_tail_pointer(skb);
381         //SKB_LINEAR_ASSERT(skb);
382         skb->tail += len;
383         skb->len  += len;
384         return tmp;
385 }
386
387 static inline unsigned char *__skb_pull(struct sk_buff *skb, unsigned int len)
388 {
389         skb->len -= len;
390         if(skb->len < skb->data_len)
391                 printf("%s(),%d,error!\n",__FUNCTION__,__LINE__);
392         return skb->data += len;
393 }
394 static inline unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
395 {
396         #ifdef PLATFORM_FREEBSD
397         return __skb_pull(skb, len);
398         #else
399         return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
400         #endif //PLATFORM_FREEBSD
401 }
402 static inline u32 skb_queue_len(const struct sk_buff_head *list_)
403 {
404         return list_->qlen;
405 }
406 static inline void __skb_insert(struct sk_buff *newsk,
407                                 struct sk_buff *prev, struct sk_buff *next,
408                                 struct sk_buff_head *list)
409 {
410         newsk->next = next;
411         newsk->prev = prev;
412         next->prev  = prev->next = newsk;
413         list->qlen++;
414 }
415 static inline void __skb_queue_before(struct sk_buff_head *list,
416                                       struct sk_buff *next,
417                                       struct sk_buff *newsk)
418 {
419         __skb_insert(newsk, next->prev, next, list);
420 }
421 static inline void skb_queue_tail(struct sk_buff_head *list,
422                                    struct sk_buff *newsk)
423 {
424         mtx_lock(&list->lock);
425         __skb_queue_before(list, (struct sk_buff *)list, newsk);
426         mtx_unlock(&list->lock);
427 }
428 static inline struct sk_buff *skb_peek(struct sk_buff_head *list_)
429 {
430         struct sk_buff *list = ((struct sk_buff *)list_)->next;
431         if (list == (struct sk_buff *)list_)
432                 list = NULL;
433         return list;
434 }
435 static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
436 {
437         struct sk_buff *next, *prev;
438
439         list->qlen--;
440         next       = skb->next;
441         prev       = skb->prev;
442         skb->next  = skb->prev = NULL;
443         next->prev = prev;
444         prev->next = next;
445 }
446
447 static inline struct sk_buff *skb_dequeue(struct sk_buff_head *list)
448 {
449         mtx_lock(&list->lock);
450
451         struct sk_buff *skb = skb_peek(list);
452         if (skb)
453                 __skb_unlink(skb, list);
454
455         mtx_unlock(&list->lock);
456
457         return skb;
458 }
459 static inline void skb_reserve(struct sk_buff *skb, int len)
460 {
461         skb->data += len;
462         skb->tail += len;
463 }
464 static inline void __skb_queue_head_init(struct sk_buff_head *list)
465 {
466         list->prev = list->next = (struct sk_buff *)list;
467         list->qlen = 0;
468 }
469 /*
470  * This function creates a split out lock class for each invocation;
471  * this is needed for now since a whole lot of users of the skb-queue
472  * infrastructure in drivers have different locking usage (in hardirq)
473  * than the networking core (in softirq only). In the long run either the
474  * network layer or drivers should need annotation to consolidate the
475  * main types of usage into 3 classes.
476  */
477 static inline void skb_queue_head_init(struct sk_buff_head *list)
478 {
479         _rtw_spinlock_init(&list->lock);
480         __skb_queue_head_init(list);
481 }
482 unsigned long copy_from_user(void *to, const void *from, unsigned long n);
483 unsigned long copy_to_user(void *to, const void *from, unsigned long n);
484 struct sk_buff * dev_alloc_skb(unsigned int size);
485 struct sk_buff *skb_clone(const struct sk_buff *skb);
486 void dev_kfree_skb_any(struct sk_buff *skb);
487 #endif //Baron porting from linux, it's all temp solution, needs to check again
488
489
490 #if 1 // kenny add Linux compatibility code for Linux USB driver
491 #include <dev/usb/usb_compat_linux.h>
492
493 #define __init          // __attribute ((constructor))
494 #define __exit          // __attribute ((destructor))
495
496 /*
497  * Definitions for module_init and module_exit macros.
498  *
499  * These macros will use the SYSINIT framework to call a specified
500  * function (with no arguments) on module loading or unloading.
501  * 
502  */
503
504 void module_init_exit_wrapper(void *arg);
505
506 #define module_init(initfn)                             \
507         SYSINIT(mod_init_ ## initfn,                    \
508                 SI_SUB_KLD, SI_ORDER_FIRST,             \
509                 module_init_exit_wrapper, initfn)
510
511 #define module_exit(exitfn)                             \
512         SYSUNINIT(mod_exit_ ## exitfn,                  \
513                   SI_SUB_KLD, SI_ORDER_ANY,             \
514                   module_init_exit_wrapper, exitfn)
515
516 /*
517  * The usb_register and usb_deregister functions are used to register
518  * usb drivers with the usb subsystem. 
519  */
520 int usb_register(struct usb_driver *driver);
521 int usb_deregister(struct usb_driver *driver);
522
523 /*
524  * usb_get_dev and usb_put_dev - increment/decrement the reference count 
525  * of the usb device structure.
526  *
527  * Original body of usb_get_dev:
528  *
529  *       if (dev)
530  *               get_device(&dev->dev);
531  *       return dev;
532  *
533  * Reference counts are not currently used in this compatibility
534  * layer. So these functions will do nothing.
535  */
536 static inline struct usb_device *
537 usb_get_dev(struct usb_device *dev)
538 {
539         return dev;
540 }
541
542 static inline void 
543 usb_put_dev(struct usb_device *dev)
544 {
545         return;
546 }
547
548
549 // rtw_usb_compat_linux
550 int rtw_usb_submit_urb(struct urb *urb, uint16_t mem_flags);
551 int rtw_usb_unlink_urb(struct urb *urb);
552 int rtw_usb_clear_halt(struct usb_device *dev, struct usb_host_endpoint *uhe);
553 int rtw_usb_control_msg(struct usb_device *dev, struct usb_host_endpoint *uhe,
554     uint8_t request, uint8_t requesttype,
555     uint16_t value, uint16_t index, void *data,
556     uint16_t size, usb_timeout_t timeout);
557 int rtw_usb_set_interface(struct usb_device *dev, uint8_t iface_no, uint8_t alt_index);
558 int rtw_usb_setup_endpoint(struct usb_device *dev,
559     struct usb_host_endpoint *uhe, usb_size_t bufsize);
560 struct urb *rtw_usb_alloc_urb(uint16_t iso_packets, uint16_t mem_flags);
561 struct usb_host_endpoint *rtw_usb_find_host_endpoint(struct usb_device *dev, uint8_t type, uint8_t ep);
562 struct usb_host_interface *rtw_usb_altnum_to_altsetting(const struct usb_interface *intf, uint8_t alt_index);
563 struct usb_interface *rtw_usb_ifnum_to_if(struct usb_device *dev, uint8_t iface_no);
564 void *rtw_usb_buffer_alloc(struct usb_device *dev, usb_size_t size, uint8_t *dma_addr);
565 void *rtw_usbd_get_intfdata(struct usb_interface *intf);
566 void rtw_usb_linux_register(void *arg);
567 void rtw_usb_linux_deregister(void *arg);
568 void rtw_usb_linux_free_device(struct usb_device *dev);
569 void rtw_usb_buffer_free(struct usb_device *dev, usb_size_t size,
570     void *addr, uint8_t dma_addr);
571 void rtw_usb_free_urb(struct urb *urb);
572 void rtw_usb_init_urb(struct urb *urb);
573 void rtw_usb_kill_urb(struct urb *urb);
574 void rtw_usb_set_intfdata(struct usb_interface *intf, void *data);
575 void rtw_usb_fill_bulk_urb(struct urb *urb, struct usb_device *udev,
576     struct usb_host_endpoint *uhe, void *buf,
577     int length, usb_complete_t callback, void *arg);
578 int rtw_usb_bulk_msg(struct usb_device *udev, struct usb_host_endpoint *uhe,
579     void *data, int len, uint16_t *pactlen, usb_timeout_t timeout);
580 void *usb_get_intfdata(struct usb_interface *intf);
581 int usb_linux_init_endpoints(struct usb_device *udev);
582
583
584
585 typedef struct urb *  PURB;
586
587 typedef unsigned gfp_t;
588 #define __GFP_WAIT      ((gfp_t)0x10u)  /* Can wait and reschedule? */
589 #define __GFP_HIGH      ((gfp_t)0x20u)  /* Should access emergency pools? */
590 #define __GFP_IO        ((gfp_t)0x40u)  /* Can start physical IO? */
591 #define __GFP_FS        ((gfp_t)0x80u)  /* Can call down to low-level FS? */
592 #define __GFP_COLD      ((gfp_t)0x100u) /* Cache-cold page required */
593 #define __GFP_NOWARN    ((gfp_t)0x200u) /* Suppress page allocation failure warning */
594 #define __GFP_REPEAT    ((gfp_t)0x400u) /* Retry the allocation.  Might fail */
595 #define __GFP_NOFAIL    ((gfp_t)0x800u) /* Retry for ever.  Cannot fail */
596 #define __GFP_NORETRY   ((gfp_t)0x1000u)/* Do not retry.  Might fail */
597 #define __GFP_NO_GROW   ((gfp_t)0x2000u)/* Slab internal usage */
598 #define __GFP_COMP      ((gfp_t)0x4000u)/* Add compound page metadata */
599 #define __GFP_ZERO      ((gfp_t)0x8000u)/* Return zeroed page on success */
600 #define __GFP_NOMEMALLOC ((gfp_t)0x10000u) /* Don't use emergency reserves */
601 #define __GFP_HARDWALL   ((gfp_t)0x20000u) /* Enforce hardwall cpuset memory allocs */
602
603 /* This equals 0, but use constants in case they ever change */
604 #define GFP_NOWAIT      (GFP_ATOMIC & ~__GFP_HIGH)
605 /* GFP_ATOMIC means both !wait (__GFP_WAIT not set) and use emergency pool */
606 #define GFP_ATOMIC      (__GFP_HIGH)
607 #define GFP_NOIO        (__GFP_WAIT)
608 #define GFP_NOFS        (__GFP_WAIT | __GFP_IO)
609 #define GFP_KERNEL      (__GFP_WAIT | __GFP_IO | __GFP_FS)
610 #define GFP_USER        (__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_HARDWALL)
611 #define GFP_HIGHUSER    (__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_HARDWALL | \
612                          __GFP_HIGHMEM)
613
614
615 #endif // kenny add Linux compatibility code for Linux USB
616
617
618
619 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24))
620         #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
621 #endif
622
623 __inline static _list *get_next(_list   *list)
624 {
625         return list->next;
626 }       
627
628 __inline static _list   *get_list_head(_queue   *queue)
629 {
630         return (&(queue->queue));
631 }
632
633         
634 #define LIST_CONTAINOR(ptr, type, member) \
635         ((type *)((char *)(ptr)-(SIZE_T)(&((type *)0)->member)))        
636
637         
638 __inline static void _enter_critical(_lock *plock, _irqL *pirqL)
639 {
640         spin_lock_irqsave(plock, *pirqL);
641 }
642
643 __inline static void _exit_critical(_lock *plock, _irqL *pirqL)
644 {
645         spin_unlock_irqrestore(plock, *pirqL);
646 }
647
648 __inline static void _enter_critical_ex(_lock *plock, _irqL *pirqL)
649 {
650         spin_lock_irqsave(plock, *pirqL);
651 }
652
653 __inline static void _exit_critical_ex(_lock *plock, _irqL *pirqL)
654 {
655         spin_unlock_irqrestore(plock, *pirqL);
656 }
657
658 __inline static void _enter_critical_bh(_lock *plock, _irqL *pirqL)
659 {
660         spin_lock_bh(plock, *pirqL);
661 }
662
663 __inline static void _exit_critical_bh(_lock *plock, _irqL *pirqL)
664 {
665         spin_unlock_bh(plock, *pirqL);
666 }
667
668 __inline static void _enter_critical_mutex(_mutex *pmutex, _irqL *pirqL)
669 {
670
671                 mtx_lock(pmutex);
672
673 }
674
675
676 __inline static void _exit_critical_mutex(_mutex *pmutex, _irqL *pirqL)
677 {
678
679                 mtx_unlock(pmutex);
680
681 }
682 static inline void __list_del(struct list_head * prev, struct list_head * next)
683 {
684         next->prev = prev;
685         prev->next = next;
686 }
687 static inline void INIT_LIST_HEAD(struct list_head *list)
688 {
689         list->next = list;
690         list->prev = list;
691 }
692 __inline static void rtw_list_delete(_list *plist)
693 {
694         __list_del(plist->prev, plist->next);
695         INIT_LIST_HEAD(plist);
696 }
697
698 __inline static void _init_timer(_timer *ptimer,_nic_hdl padapter,void *pfunc,void* cntx)
699 {
700         ptimer->function = pfunc;
701         ptimer->arg = cntx;
702         callout_init(&ptimer->callout, CALLOUT_MPSAFE);
703 }
704
705 __inline static void _set_timer(_timer *ptimer,u32 delay_time)
706 {       
707         //      mod_timer(ptimer , (jiffies+(delay_time*HZ/1000)));
708         if(ptimer->function && ptimer->arg){
709                 rtw_mtx_lock(NULL);
710                 callout_reset(&ptimer->callout, delay_time,ptimer->function, ptimer->arg);
711                 rtw_mtx_unlock(NULL);
712         }
713 }
714
715 __inline static void _cancel_timer(_timer *ptimer,u8 *bcancelled)
716 {
717         //      del_timer_sync(ptimer);         
718         //      *bcancelled=  _TRUE;//TRUE ==1; FALSE==0        
719         rtw_mtx_lock(NULL);
720         callout_drain(&ptimer->callout);
721         rtw_mtx_unlock(NULL);
722 }
723
724 __inline static void _init_workitem(_workitem *pwork, void *pfunc, PVOID cntx)
725 {
726         printf("%s Not implement yet! \n",__FUNCTION__);
727 }
728
729 __inline static void _set_workitem(_workitem *pwork)
730 {
731         printf("%s Not implement yet! \n",__FUNCTION__);
732 //      schedule_work(pwork);
733 }
734
735 //
736 // Global Mutex: can only be used at PASSIVE level.
737 //
738
739 #define ACQUIRE_GLOBAL_MUTEX(_MutexCounter)                              \
740 {                                                               \
741 }
742
743 #define RELEASE_GLOBAL_MUTEX(_MutexCounter)                              \
744 {                                                               \
745 }
746
747 #define ATOMIC_INIT(i)  { (i) }
748
749 #endif //PLATFORM_FREEBSD
750
751
752 #ifdef PLATFORM_LINUX
753         #include <linux/version.h>
754         #include <linux/spinlock.h>
755         #include <linux/compiler.h>
756         #include <linux/kernel.h>
757         #include <linux/errno.h>
758         #include <linux/init.h>
759         #include <linux/slab.h>
760         #include <linux/module.h>
761 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,5))
762         #include <linux/kref.h>
763 #endif
764         //#include <linux/smp_lock.h>
765         #include <linux/netdevice.h>
766         #include <linux/skbuff.h>
767         #include <linux/circ_buf.h>
768         #include <asm/uaccess.h>
769         #include <asm/byteorder.h>
770         #include <asm/atomic.h>
771         #include <asm/io.h>
772 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26))
773         #include <asm/semaphore.h>
774 #else
775         #include <linux/semaphore.h>
776 #endif
777         #include <linux/sem.h>
778         #include <linux/sched.h>
779         #include <linux/etherdevice.h>
780         #include <linux/wireless.h>
781         #include <net/iw_handler.h>
782         #include <linux/if_arp.h>
783         #include <linux/rtnetlink.h>
784         #include <linux/delay.h>
785         #include <linux/proc_fs.h>      // Necessary because we use the proc fs
786         #include <linux/interrupt.h>    // for struct tasklet_struct
787         #include <linux/ip.h>
788         #include <linux/kthread.h>
789
790 #ifdef CONFIG_IOCTL_CFG80211    
791 //      #include <linux/ieee80211.h>        
792         #include <net/ieee80211_radiotap.h>
793         #include <net/cfg80211.h>       
794 #endif //CONFIG_IOCTL_CFG80211
795
796 #ifdef CONFIG_TCP_CSUM_OFFLOAD_TX
797         #include <linux/in.h>
798         #include <linux/udp.h>
799 #endif
800
801 #ifdef CONFIG_USB_HCI
802         #include <linux/usb.h>
803 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,21))
804         #include <linux/usb_ch9.h>
805 #else
806         #include <linux/usb/ch9.h>
807 #endif
808 #endif
809
810 #ifdef CONFIG_PCI_HCI
811         #include <linux/pci.h>
812 #endif
813
814         
815 #ifdef CONFIG_USB_HCI
816         typedef struct urb *  PURB;
817 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,22))
818 #ifdef CONFIG_USB_SUSPEND
819 #define CONFIG_AUTOSUSPEND      1
820 #endif
821 #endif
822 #endif
823
824         typedef struct  semaphore _sema;
825         typedef spinlock_t      _lock;
826 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37))
827         typedef struct mutex            _mutex;
828 #else
829         typedef struct semaphore        _mutex;
830 #endif
831         typedef struct timer_list _timer;
832
833         struct  __queue {
834                 struct  list_head       queue;  
835                 _lock   lock;
836         };
837
838         typedef struct sk_buff  _pkt;
839         typedef unsigned char   _buffer;
840         
841         typedef struct  __queue _queue;
842         typedef struct  list_head       _list;
843         typedef int     _OS_STATUS;
844         //typedef u32   _irqL;
845         typedef unsigned long _irqL;
846         typedef struct  net_device * _nic_hdl;
847         
848         typedef void*           _thread_hdl_;
849         typedef int             thread_return;
850         typedef void*   thread_context;
851
852         #define thread_exit() complete_and_exit(NULL, 0)
853
854         typedef void timer_hdl_return;
855         typedef void* timer_hdl_context;
856         typedef struct work_struct _workitem;
857
858 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24))
859         #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
860 #endif
861
862 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22))
863 // Porting from linux kernel, for compatible with old kernel.
864 static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb)
865 {
866         return skb->tail;
867 }
868
869 static inline void skb_reset_tail_pointer(struct sk_buff *skb)
870 {
871         skb->tail = skb->data;
872 }
873
874 static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset)
875 {
876         skb->tail = skb->data + offset;
877 }
878
879 static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
880 {
881         return skb->end;
882 }
883 #endif
884
885 __inline static _list *get_next(_list   *list)
886 {
887         return list->next;
888 }       
889
890 __inline static _list   *get_list_head(_queue   *queue)
891 {
892         return (&(queue->queue));
893 }
894
895         
896 #define LIST_CONTAINOR(ptr, type, member) \
897         ((type *)((char *)(ptr)-(SIZE_T)(&((type *)0)->member)))        
898
899         
900 __inline static void _enter_critical(_lock *plock, _irqL *pirqL)
901 {
902         spin_lock_irqsave(plock, *pirqL);
903 }
904
905 __inline static void _exit_critical(_lock *plock, _irqL *pirqL)
906 {
907         spin_unlock_irqrestore(plock, *pirqL);
908 }
909
910 __inline static void _enter_critical_ex(_lock *plock, _irqL *pirqL)
911 {
912         spin_lock_irqsave(plock, *pirqL);
913 }
914
915 __inline static void _exit_critical_ex(_lock *plock, _irqL *pirqL)
916 {
917         spin_unlock_irqrestore(plock, *pirqL);
918 }
919
920 __inline static void _enter_critical_bh(_lock *plock, _irqL *pirqL)
921 {
922         spin_lock_bh(plock);
923 }
924
925 __inline static void _exit_critical_bh(_lock *plock, _irqL *pirqL)
926 {
927         spin_unlock_bh(plock);
928 }
929
930 __inline static void _enter_critical_mutex(_mutex *pmutex, _irqL *pirqL)
931 {
932 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37))
933                 mutex_lock(pmutex);
934 #else
935                 down(pmutex);
936 #endif
937 }
938
939
940 __inline static void _exit_critical_mutex(_mutex *pmutex, _irqL *pirqL)
941 {
942 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37))
943                 mutex_unlock(pmutex);
944 #else
945                 up(pmutex);
946 #endif
947 }
948
949 __inline static void rtw_list_delete(_list *plist)
950 {
951         list_del_init(plist);
952 }
953
954 __inline static void _init_timer(_timer *ptimer,_nic_hdl nic_hdl,void *pfunc,void* cntx)
955 {
956         //setup_timer(ptimer, pfunc,(u32)cntx); 
957         ptimer->function = pfunc;
958         ptimer->data = (unsigned long)cntx;
959         init_timer(ptimer);
960 }
961
962 __inline static void _set_timer(_timer *ptimer,u32 delay_time)
963 {       
964         mod_timer(ptimer , (jiffies+(delay_time*HZ/1000)));     
965 }
966
967 __inline static void _cancel_timer(_timer *ptimer,u8 *bcancelled)
968 {
969         del_timer_sync(ptimer);         
970         *bcancelled=  _TRUE;//TRUE ==1; FALSE==0
971 }
972
973 #ifdef PLATFORM_LINUX
974 #define RTW_TIMER_HDL_ARGS void *FunctionContext
975 #elif defined(PLATFORM_OS_CE) || defined(PLATFORM_WINDOWS)
976 #define RTW_TIMER_HDL_ARGS IN PVOID SystemSpecific1, IN PVOID FunctionContext, IN PVOID SystemSpecific2, IN PVOID SystemSpecific3
977 #endif
978
979 #define RTW_TIMER_HDL_NAME(name) rtw_##name##_timer_hdl
980 #define RTW_DECLARE_TIMER_HDL(name) void RTW_TIMER_HDL_NAME(name)(RTW_TIMER_HDL_ARGS)
981
982
983 __inline static void _init_workitem(_workitem *pwork, void *pfunc, PVOID cntx)
984 {
985 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
986         INIT_WORK(pwork, pfunc);
987 #else
988         INIT_WORK(pwork, pfunc,pwork);
989 #endif
990 }
991
992 __inline static void _set_workitem(_workitem *pwork)
993 {
994         schedule_work(pwork);
995 }
996
997 __inline static void _cancel_workitem_sync(_workitem *pwork)
998 {
999 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,22))
1000         cancel_work_sync(pwork);
1001 #else
1002         flush_scheduled_work();
1003 #endif
1004 }
1005
1006 //
1007 // Global Mutex: can only be used at PASSIVE level.
1008 //
1009
1010 #define ACQUIRE_GLOBAL_MUTEX(_MutexCounter)                              \
1011 {                                                               \
1012         while (atomic_inc_return((atomic_t *)&(_MutexCounter)) != 1)\
1013         {                                                           \
1014                 atomic_dec((atomic_t *)&(_MutexCounter));        \
1015                 msleep(10);                          \
1016         }                                                           \
1017 }
1018
1019 #define RELEASE_GLOBAL_MUTEX(_MutexCounter)                              \
1020 {                                                               \
1021         atomic_dec((atomic_t *)&(_MutexCounter));        \
1022 }
1023
1024 static inline int rtw_netif_queue_stopped(struct net_device *pnetdev)
1025 {
1026 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
1027         return (netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 0)) &&
1028                 netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 1)) &&
1029                 netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 2)) &&
1030                 netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 3)) );
1031 #else
1032         return netif_queue_stopped(pnetdev);
1033 #endif
1034 }
1035
1036 static inline void rtw_netif_wake_queue(struct net_device *pnetdev)
1037 {
1038 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
1039         netif_tx_wake_all_queues(pnetdev);
1040 #else
1041         netif_wake_queue(pnetdev);
1042 #endif
1043 }
1044
1045 static inline void rtw_netif_start_queue(struct net_device *pnetdev)
1046 {
1047 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
1048         netif_tx_start_all_queues(pnetdev);
1049 #else
1050         netif_start_queue(pnetdev);
1051 #endif
1052 }
1053
1054 static inline void rtw_netif_stop_queue(struct net_device *pnetdev)
1055 {
1056 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
1057         netif_tx_stop_all_queues(pnetdev);
1058 #else
1059         netif_stop_queue(pnetdev);
1060 #endif
1061 }
1062
1063 #endif  // PLATFORM_LINUX
1064
1065
1066 #ifdef PLATFORM_OS_XP
1067
1068         #include <ndis.h>
1069         #include <ntddk.h>
1070         #include <ntddndis.h>
1071         #include <ntdef.h>
1072
1073 #ifdef CONFIG_USB_HCI
1074         #include <usb.h>
1075         #include <usbioctl.h>
1076         #include <usbdlib.h>
1077 #endif
1078
1079         typedef KSEMAPHORE      _sema;
1080         typedef LIST_ENTRY      _list;
1081         typedef NDIS_STATUS _OS_STATUS;
1082         
1083
1084         typedef NDIS_SPIN_LOCK  _lock;
1085
1086         typedef KMUTEX                  _mutex;
1087
1088         typedef KIRQL   _irqL;
1089
1090         // USB_PIPE for WINCE , but handle can be use just integer under windows
1091         typedef NDIS_HANDLE  _nic_hdl;
1092
1093
1094         typedef NDIS_MINIPORT_TIMER    _timer;
1095
1096         struct  __queue {
1097                 LIST_ENTRY      queue;  
1098                 _lock   lock;
1099         };
1100
1101         typedef NDIS_PACKET     _pkt;
1102         typedef NDIS_BUFFER     _buffer;
1103         typedef struct  __queue _queue;
1104         
1105         typedef PKTHREAD _thread_hdl_;
1106         typedef void    thread_return;
1107         typedef void* thread_context;
1108
1109         typedef NDIS_WORK_ITEM _workitem;
1110
1111         #define thread_exit() PsTerminateSystemThread(STATUS_SUCCESS);
1112
1113         #define HZ                      10000000
1114         #define SEMA_UPBND      (0x7FFFFFFF)   //8192
1115         
1116 __inline static _list *get_next(_list   *list)
1117 {
1118         return list->Flink;
1119 }       
1120
1121 __inline static _list   *get_list_head(_queue   *queue)
1122 {
1123         return (&(queue->queue));
1124 }
1125         
1126
1127 #define LIST_CONTAINOR(ptr, type, member) CONTAINING_RECORD(ptr, type, member)
1128      
1129
1130 __inline static _enter_critical(_lock *plock, _irqL *pirqL)
1131 {
1132         NdisAcquireSpinLock(plock);     
1133 }
1134
1135 __inline static _exit_critical(_lock *plock, _irqL *pirqL)
1136 {
1137         NdisReleaseSpinLock(plock);     
1138 }
1139
1140
1141 __inline static _enter_critical_ex(_lock *plock, _irqL *pirqL)
1142 {
1143         NdisDprAcquireSpinLock(plock);  
1144 }
1145
1146 __inline static _exit_critical_ex(_lock *plock, _irqL *pirqL)
1147 {
1148         NdisDprReleaseSpinLock(plock);  
1149 }
1150
1151 __inline static void _enter_critical_bh(_lock *plock, _irqL *pirqL)
1152 {
1153         NdisDprAcquireSpinLock(plock);
1154 }
1155
1156 __inline static void _exit_critical_bh(_lock *plock, _irqL *pirqL)
1157 {
1158         NdisDprReleaseSpinLock(plock);
1159 }
1160
1161 __inline static _enter_critical_mutex(_mutex *pmutex, _irqL *pirqL)
1162 {
1163         KeWaitForSingleObject(pmutex, Executive, KernelMode, FALSE, NULL);
1164 }
1165
1166
1167 __inline static _exit_critical_mutex(_mutex *pmutex, _irqL *pirqL)
1168 {
1169         KeReleaseMutex(pmutex, FALSE);
1170 }
1171
1172
1173 __inline static void rtw_list_delete(_list *plist)
1174 {
1175         RemoveEntryList(plist);
1176         InitializeListHead(plist);      
1177 }
1178
1179 __inline static void _init_timer(_timer *ptimer,_nic_hdl nic_hdl,void *pfunc,PVOID cntx)
1180 {
1181         NdisMInitializeTimer(ptimer, nic_hdl, pfunc, cntx);
1182 }
1183
1184 __inline static void _set_timer(_timer *ptimer,u32 delay_time)
1185 {       
1186         NdisMSetTimer(ptimer,delay_time);       
1187 }
1188
1189 __inline static void _cancel_timer(_timer *ptimer,u8 *bcancelled)
1190 {
1191         NdisMCancelTimer(ptimer,bcancelled);
1192 }
1193
1194 __inline static void _init_workitem(_workitem *pwork, void *pfunc, PVOID cntx)
1195 {
1196
1197         NdisInitializeWorkItem(pwork, pfunc, cntx);
1198 }
1199
1200 __inline static void _set_workitem(_workitem *pwork)
1201 {
1202         NdisScheduleWorkItem(pwork);
1203 }
1204
1205
1206 #define ATOMIC_INIT(i)  { (i) }
1207
1208 //
1209 // Global Mutex: can only be used at PASSIVE level.
1210 //
1211
1212 #define ACQUIRE_GLOBAL_MUTEX(_MutexCounter)                              \
1213 {                                                               \
1214     while (NdisInterlockedIncrement((PULONG)&(_MutexCounter)) != 1)\
1215     {                                                           \
1216         NdisInterlockedDecrement((PULONG)&(_MutexCounter));        \
1217         NdisMSleep(10000);                          \
1218     }                                                           \
1219 }
1220
1221 #define RELEASE_GLOBAL_MUTEX(_MutexCounter)                              \
1222 {                                                               \
1223     NdisInterlockedDecrement((PULONG)&(_MutexCounter));              \
1224 }
1225
1226 #endif // PLATFORM_OS_XP
1227
1228
1229 #ifdef PLATFORM_OS_CE
1230 #include <osdep_ce_service.h>
1231 #endif
1232
1233 #include <rtw_byteorder.h>
1234
1235 #ifndef BIT
1236         #define BIT(x)  ( 1 << (x))
1237 #endif
1238
1239 extern int RTW_STATUS_CODE(int error_code);
1240
1241 #define CONFIG_USE_VMALLOC
1242
1243 /* flags used for rtw_mstat_update() */
1244 enum mstat_f {
1245         /* type: 0x00ff */
1246         MSTAT_TYPE_VIR = 0x00,
1247         MSTAT_TYPE_PHY= 0x01,
1248         MSTAT_TYPE_SKB = 0x02,
1249         MSTAT_TYPE_USB = 0x03,
1250         MSTAT_TYPE_MAX = 0x04,
1251
1252         /* func: 0xff00 */
1253         MSTAT_FUNC_UNSPECIFIED = 0x00<<8,
1254         MSTAT_FUNC_IO = 0x01<<8,
1255         MSTAT_FUNC_TX_IO = 0x02<<8,
1256         MSTAT_FUNC_RX_IO = 0x03<<8,
1257         MSTAT_FUNC_TX = 0x04<<8,
1258         MSTAT_FUNC_RX = 0x05<<8,
1259         MSTAT_FUNC_MAX = 0x06<<8,
1260 };
1261
1262 #define mstat_tf_idx(flags) ((flags)&0xff)
1263 #define mstat_ff_idx(flags) (((flags)&0xff00) >> 8)
1264
1265 typedef enum mstat_status{
1266         MSTAT_ALLOC_SUCCESS = 0,
1267         MSTAT_ALLOC_FAIL,
1268         MSTAT_FREE
1269 } MSTAT_STATUS;
1270
1271 #ifdef DBG_MEM_ALLOC
1272 void rtw_mstat_update(const enum mstat_f flags, const MSTAT_STATUS status, u32 sz);
1273 int _rtw_mstat_dump(char *buf, int len);
1274 void rtw_mstat_dump (void);
1275 u8* dbg_rtw_vmalloc(u32 sz, const enum mstat_f flags, const char *func, const int line);
1276 u8* dbg_rtw_zvmalloc(u32 sz, const enum mstat_f flags, const char *func, const int line);
1277 void dbg_rtw_vmfree(u8 *pbuf, const enum mstat_f flags, u32 sz, const char *func, const int line);
1278 u8* dbg_rtw_malloc(u32 sz, const enum mstat_f flags, const char *func, const int line);
1279 u8* dbg_rtw_zmalloc(u32 sz, const enum mstat_f flags, const char *func, const int line);
1280 void dbg_rtw_mfree(u8 *pbuf, const enum mstat_f flags, u32 sz, const char *func, const int line);
1281
1282 struct sk_buff * dbg_rtw_skb_alloc(unsigned int size, const enum mstat_f flags, const char *func, const int line);
1283 void dbg_rtw_skb_free(struct sk_buff *skb, const enum mstat_f flags, const char *func, const int line);
1284 struct sk_buff *dbg_rtw_skb_copy(const struct sk_buff *skb, const enum mstat_f flags, const char *func, const int line);
1285 struct sk_buff *dbg_rtw_skb_clone(struct sk_buff *skb, const enum mstat_f flags, const char *func, const int line);
1286 int dbg_rtw_netif_rx(_nic_hdl ndev, struct sk_buff *skb, const enum mstat_f flags, const char *func, int line);
1287 void dbg_rtw_skb_queue_purge(struct sk_buff_head *list, enum mstat_f flags, const char *func, int line);
1288
1289 #ifdef CONFIG_USB_HCI
1290 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);
1291 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);
1292 #endif /* CONFIG_USB_HCI */
1293
1294 #ifdef CONFIG_USE_VMALLOC
1295 #define rtw_vmalloc(sz)                 dbg_rtw_vmalloc((sz), MSTAT_TYPE_VIR, __FUNCTION__, __LINE__)
1296 #define rtw_zvmalloc(sz)                        dbg_rtw_zvmalloc((sz), MSTAT_TYPE_VIR, __FUNCTION__, __LINE__)
1297 #define rtw_vmfree(pbuf, sz)            dbg_rtw_vmfree((pbuf), (sz), MSTAT_TYPE_VIR, __FUNCTION__, __LINE__)
1298 #define rtw_vmalloc_f(sz, mstat_f)                      dbg_rtw_vmalloc((sz), ((mstat_f)&0xff00)|MSTAT_TYPE_VIR, __FUNCTION__, __LINE__)
1299 #define rtw_zvmalloc_f(sz, mstat_f)             dbg_rtw_zvmalloc((sz), ((mstat_f)&0xff00)|MSTAT_TYPE_VIR, __FUNCTION__, __LINE__)
1300 #define rtw_vmfree_f(pbuf, sz, mstat_f) dbg_rtw_vmfree((pbuf), (sz), ((mstat_f)&0xff00)|MSTAT_TYPE_VIR, __FUNCTION__, __LINE__)
1301 #else /* CONFIG_USE_VMALLOC */
1302 #define rtw_vmalloc(sz)                 dbg_rtw_malloc((sz), MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
1303 #define rtw_zvmalloc(sz)                        dbg_rtw_zmalloc((sz), MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
1304 #define rtw_vmfree(pbuf, sz)            dbg_rtw_mfree((pbuf), (sz), MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
1305 #define rtw_vmalloc_f(sz, mstat_f)                      dbg_rtw_malloc((sz), ((mstat_f)&0xff00)|MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
1306 #define rtw_zvmalloc_f(sz, mstat_f)             dbg_rtw_zmalloc((sz), ((mstat_f)&0xff00)|MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
1307 #define rtw_vmfree_f(pbuf, sz, mstat_f) dbg_rtw_mfree((pbuf), (sz), ((mstat_f)&0xff00)|MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
1308 #endif /* CONFIG_USE_VMALLOC */
1309 #define rtw_malloc(sz)                  dbg_rtw_malloc((sz), MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
1310 #define rtw_zmalloc(sz)                 dbg_rtw_zmalloc((sz), MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
1311 #define rtw_mfree(pbuf, sz)             dbg_rtw_mfree((pbuf), (sz), MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
1312 #define rtw_malloc_f(sz, mstat_f)                       dbg_rtw_malloc((sz), ((mstat_f)&0xff00)|MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
1313 #define rtw_zmalloc_f(sz, mstat_f)                      dbg_rtw_zmalloc((sz), ((mstat_f)&0xff00)|MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
1314 #define rtw_mfree_f(pbuf, sz, mstat_f)          dbg_rtw_mfree((pbuf), (sz), ((mstat_f)&0xff00)|MSTAT_TYPE_PHY, __FUNCTION__, __LINE__)
1315
1316 #define rtw_skb_alloc(size)     dbg_rtw_skb_alloc((size), MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
1317 #define rtw_skb_free(skb)       dbg_rtw_skb_free((skb), MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
1318 #define rtw_skb_alloc_f(size, mstat_f)  dbg_rtw_skb_alloc((size), ((mstat_f)&0xff00)|MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
1319 #define rtw_skb_free_f(skb, mstat_f)    dbg_rtw_skb_free((skb), ((mstat_f)&0xff00)|MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
1320 #define rtw_skb_copy(skb)       dbg_rtw_skb_copy((skb), MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
1321 #define rtw_skb_clone(skb)      dbg_rtw_skb_clone((skb), MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
1322 #define rtw_skb_copy_f(skb, mstat_f)    dbg_rtw_skb_copy((skb), ((mstat_f)&0xff00)|MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
1323 #define rtw_skb_clone_f(skb, mstat_f)   dbg_rtw_skb_clone((skb), ((mstat_f)&0xff00)|MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
1324 #define rtw_netif_rx(ndev, skb) dbg_rtw_netif_rx(ndev, skb, MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
1325 #define rtw_skb_queue_purge(sk_buff_head) dbg_rtw_skb_queue_purge(sk_buff_head, MSTAT_TYPE_SKB, __FUNCTION__, __LINE__)
1326 #ifdef CONFIG_USB_HCI
1327 #define rtw_usb_buffer_alloc(dev, size, dma)            dbg_rtw_usb_buffer_alloc((dev), (size), (dma), MSTAT_TYPE_USB, __FUNCTION__, __LINE__)
1328 #define rtw_usb_buffer_free(dev, size, addr, dma)       dbg_rtw_usb_buffer_free((dev), (size), (addr), (dma), MSTAT_TYPE_USB, __FUNCTION__, __LINE__)
1329 #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__)
1330 #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__)
1331 #endif /* CONFIG_USB_HCI */
1332
1333 #else /* DBG_MEM_ALLOC */
1334 #define rtw_mstat_update(flag, status, sz) do {} while(0)
1335 #define rtw_mstat_dump() do {} while(0)
1336 u8*     _rtw_vmalloc(u32 sz);
1337 u8*     _rtw_zvmalloc(u32 sz);
1338 void    _rtw_vmfree(u8 *pbuf, u32 sz);
1339 u8*     _rtw_zmalloc(u32 sz);
1340 u8*     _rtw_malloc(u32 sz);
1341 void    _rtw_mfree(u8 *pbuf, u32 sz);
1342
1343 struct sk_buff *_rtw_skb_alloc(u32 sz);
1344 void _rtw_skb_free(struct sk_buff *skb);
1345 struct sk_buff *_rtw_skb_copy(const struct sk_buff *skb);
1346 struct sk_buff *_rtw_skb_clone(struct sk_buff *skb);
1347 int _rtw_netif_rx(_nic_hdl ndev, struct sk_buff *skb);
1348 void _rtw_skb_queue_purge(struct sk_buff_head *list);
1349
1350 #ifdef CONFIG_USB_HCI
1351 void *_rtw_usb_buffer_alloc(struct usb_device *dev, size_t size, dma_addr_t *dma);
1352 void _rtw_usb_buffer_free(struct usb_device *dev, size_t size, void *addr, dma_addr_t dma);
1353 #endif /* CONFIG_USB_HCI */
1354
1355 #ifdef CONFIG_USE_VMALLOC
1356 #define rtw_vmalloc(sz)                 _rtw_vmalloc((sz))
1357 #define rtw_zvmalloc(sz)                        _rtw_zvmalloc((sz))
1358 #define rtw_vmfree(pbuf, sz)            _rtw_vmfree((pbuf), (sz))
1359 #define rtw_vmalloc_f(sz, mstat_f)                      _rtw_vmalloc((sz))
1360 #define rtw_zvmalloc_f(sz, mstat_f)             _rtw_zvmalloc((sz))
1361 #define rtw_vmfree_f(pbuf, sz, mstat_f) _rtw_vmfree((pbuf), (sz))
1362 #else /* CONFIG_USE_VMALLOC */
1363 #define rtw_vmalloc(sz)                 _rtw_malloc((sz))
1364 #define rtw_zvmalloc(sz)                        _rtw_zmalloc((sz))
1365 #define rtw_vmfree(pbuf, sz)            _rtw_mfree((pbuf), (sz))
1366 #define rtw_vmalloc_f(sz, mstat_f)                      _rtw_malloc((sz))
1367 #define rtw_zvmalloc_f(sz, mstat_f)             _rtw_zmalloc((sz))
1368 #define rtw_vmfree_f(pbuf, sz, mstat_f) _rtw_mfree((pbuf), (sz))
1369 #endif /* CONFIG_USE_VMALLOC */
1370 #define rtw_malloc(sz)                  _rtw_malloc((sz))
1371 #define rtw_zmalloc(sz)                 _rtw_zmalloc((sz))
1372 #define rtw_mfree(pbuf, sz)             _rtw_mfree((pbuf), (sz))
1373 #define rtw_malloc_f(sz, mstat_f)                       _rtw_malloc((sz))
1374 #define rtw_zmalloc_f(sz, mstat_f)                      _rtw_zmalloc((sz))
1375 #define rtw_mfree_f(pbuf, sz, mstat_f)          _rtw_mfree((pbuf), (sz))
1376
1377 #define rtw_skb_alloc(size) _rtw_skb_alloc((size))
1378 #define rtw_skb_free(skb) _rtw_skb_free((skb))
1379 #define rtw_skb_alloc_f(size, mstat_f)  _rtw_skb_alloc((size))
1380 #define rtw_skb_free_f(skb, mstat_f)    _rtw_skb_free((skb))
1381 #define rtw_skb_copy(skb)       _rtw_skb_copy((skb))
1382 #define rtw_skb_clone(skb)      _rtw_skb_clone((skb))
1383 #define rtw_skb_copy_f(skb, mstat_f)    _rtw_skb_copy((skb))
1384 #define rtw_skb_clone_f(skb, mstat_f)   _rtw_skb_clone((skb))
1385 #define rtw_netif_rx(ndev, skb) _rtw_netif_rx(ndev, skb)
1386 #define rtw_skb_queue_purge(sk_buff_head) _rtw_skb_queue_purge(sk_buff_head)
1387 #ifdef CONFIG_USB_HCI
1388 #define rtw_usb_buffer_alloc(dev, size, dma) _rtw_usb_buffer_alloc((dev), (size), (dma))
1389 #define rtw_usb_buffer_free(dev, size, addr, dma) _rtw_usb_buffer_free((dev), (size), (addr), (dma))
1390 #define rtw_usb_buffer_alloc_f(dev, size, dma, mstat_f) _rtw_usb_buffer_alloc((dev), (size), (dma))
1391 #define rtw_usb_buffer_free_f(dev, size, addr, dma, mstat_f) _rtw_usb_buffer_free((dev), (size), (addr), (dma))
1392 #endif /* CONFIG_USB_HCI */
1393 #endif /* DBG_MEM_ALLOC */
1394
1395 extern void*    rtw_malloc2d(int h, int w, int size);
1396 extern void     rtw_mfree2d(void *pbuf, int h, int w, int size);
1397
1398 extern void     _rtw_memcpy(void* dec, void* sour, u32 sz);
1399 extern int      _rtw_memcmp(void *dst, void *src, u32 sz);
1400 extern void     _rtw_memset(void *pbuf, int c, u32 sz);
1401
1402 extern void     _rtw_init_listhead(_list *list);
1403 extern u32      rtw_is_list_empty(_list *phead);
1404 extern void     rtw_list_insert_head(_list *plist, _list *phead);
1405 extern void     rtw_list_insert_tail(_list *plist, _list *phead);
1406 #ifndef PLATFORM_FREEBSD
1407 extern void     rtw_list_delete(_list *plist);
1408 #endif //PLATFORM_FREEBSD
1409
1410 extern void     _rtw_init_sema(_sema *sema, int init_val);
1411 extern void     _rtw_free_sema(_sema    *sema);
1412 extern void     _rtw_up_sema(_sema      *sema);
1413 extern u32      _rtw_down_sema(_sema *sema);
1414 extern void     _rtw_mutex_init(_mutex *pmutex);
1415 extern void     _rtw_mutex_free(_mutex *pmutex);
1416 #ifndef PLATFORM_FREEBSD
1417 extern void     _rtw_spinlock_init(_lock *plock);
1418 #endif //PLATFORM_FREEBSD
1419 extern void     _rtw_spinlock_free(_lock *plock);
1420 extern void     _rtw_spinlock(_lock     *plock);
1421 extern void     _rtw_spinunlock(_lock   *plock);
1422 extern void     _rtw_spinlock_ex(_lock  *plock);
1423 extern void     _rtw_spinunlock_ex(_lock        *plock);
1424
1425 extern void     _rtw_init_queue(_queue  *pqueue);
1426 extern u32      _rtw_queue_empty(_queue *pqueue);
1427 extern u32      rtw_end_of_queue_search(_list *queue, _list *pelement);
1428
1429 extern u32      rtw_get_current_time(void);
1430 extern u32      rtw_systime_to_ms(u32 systime);
1431 extern u32      rtw_ms_to_systime(u32 ms);
1432 extern s32      rtw_get_passing_time_ms(u32 start);
1433 extern s32      rtw_get_time_interval_ms(u32 start, u32 end);
1434
1435 extern void     rtw_sleep_schedulable(int ms);
1436
1437 extern void     rtw_msleep_os(int ms);
1438 extern void     rtw_usleep_os(int us);
1439
1440 extern u32      rtw_atoi(u8* s);
1441
1442 #ifdef DBG_DELAY_OS
1443 #define rtw_mdelay_os(ms) _rtw_mdelay_os((ms), __FUNCTION__, __LINE__)
1444 #define rtw_udelay_os(ms) _rtw_udelay_os((ms), __FUNCTION__, __LINE__)
1445 extern void _rtw_mdelay_os(int ms, const char *func, const int line);
1446 extern void _rtw_udelay_os(int us, const char *func, const int line);
1447 #else
1448 extern void     rtw_mdelay_os(int ms);
1449 extern void     rtw_udelay_os(int us);
1450 #endif
1451
1452 extern void rtw_yield_os(void);
1453
1454
1455 __inline static unsigned char _cancel_timer_ex(_timer *ptimer)
1456 {
1457 #ifdef PLATFORM_LINUX
1458         return del_timer_sync(ptimer);
1459 #endif
1460 #ifdef PLATFORM_FREEBSD
1461         _cancel_timer(ptimer,0);
1462         return 0;
1463 #endif
1464 #ifdef PLATFORM_WINDOWS
1465         u8 bcancelled;
1466         
1467         _cancel_timer(ptimer, &bcancelled);
1468         
1469         return bcancelled;
1470 #endif
1471 }
1472
1473 #ifdef PLATFORM_FREEBSD
1474 static __inline void thread_enter(void *context);
1475 #endif //PLATFORM_FREEBSD
1476 static __inline void thread_enter(char *name)
1477 {
1478 #ifdef PLATFORM_LINUX
1479         #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 8, 0))
1480         daemonize("%s", name);
1481         #endif
1482         allow_signal(SIGTERM);
1483 #endif
1484 #ifdef PLATFORM_FREEBSD
1485         printf("%s", "RTKTHREAD_enter");
1486 #endif
1487 }
1488
1489 #ifdef PLATFORM_FREEBSD
1490 #define thread_exit() do{printf("%s", "RTKTHREAD_exit");}while(0)
1491 #endif //PLATFORM_FREEBSD
1492 __inline static void flush_signals_thread(void) 
1493 {
1494 #ifdef PLATFORM_LINUX
1495         if (signal_pending (current)) 
1496         {
1497                 flush_signals(current);
1498         }
1499 #endif
1500 }
1501
1502 __inline static _OS_STATUS res_to_status(sint res)
1503 {
1504
1505
1506 #if defined (PLATFORM_LINUX) || defined (PLATFORM_MPIXEL) || defined (PLATFORM_FREEBSD)
1507         return res;
1508 #endif
1509
1510 #ifdef PLATFORM_WINDOWS
1511
1512         if (res == _SUCCESS)
1513                 return NDIS_STATUS_SUCCESS;
1514         else
1515                 return NDIS_STATUS_FAILURE;
1516
1517 #endif  
1518         
1519 }
1520
1521 __inline static void rtw_dump_stack(void)
1522 {
1523 #ifdef PLATFORM_LINUX
1524         dump_stack();
1525 #endif
1526 }
1527
1528 #ifdef PLATFORM_LINUX
1529 #define rtw_warn_on(condition) WARN_ON(condition)
1530 #else
1531 #define rtw_warn_on(condition) do {} while (0)
1532 #endif
1533
1534 #define _RND(sz, r) ((((sz)+((r)-1))/(r))*(r))
1535 #define RND4(x) (((x >> 2) + (((x & 3) == 0) ?  0: 1)) << 2)
1536
1537 __inline static u32 _RND4(u32 sz)
1538 {
1539
1540         u32     val;
1541
1542         val = ((sz >> 2) + ((sz & 3) ? 1: 0)) << 2;
1543         
1544         return val;
1545
1546 }
1547
1548 __inline static u32 _RND8(u32 sz)
1549 {
1550
1551         u32     val;
1552
1553         val = ((sz >> 3) + ((sz & 7) ? 1: 0)) << 3;
1554         
1555         return val;
1556
1557 }
1558
1559 __inline static u32 _RND128(u32 sz)
1560 {
1561
1562         u32     val;
1563
1564         val = ((sz >> 7) + ((sz & 127) ? 1: 0)) << 7;
1565         
1566         return val;
1567
1568 }
1569
1570 __inline static u32 _RND256(u32 sz)
1571 {
1572
1573         u32     val;
1574
1575         val = ((sz >> 8) + ((sz & 255) ? 1: 0)) << 8;
1576         
1577         return val;
1578
1579 }
1580
1581 __inline static u32 _RND512(u32 sz)
1582 {
1583
1584         u32     val;
1585
1586         val = ((sz >> 9) + ((sz & 511) ? 1: 0)) << 9;
1587         
1588         return val;
1589
1590 }
1591
1592 __inline static u32 bitshift(u32 bitmask)
1593 {
1594         u32 i;
1595
1596         for (i = 0; i <= 31; i++)
1597                 if (((bitmask>>i) &  0x1) == 1) break;
1598
1599         return i;
1600 }
1601
1602 #ifndef MAC_FMT
1603 #define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x"
1604 #endif
1605 #ifndef MAC_ARG
1606 #define MAC_ARG(x) ((u8*)(x))[0],((u8*)(x))[1],((u8*)(x))[2],((u8*)(x))[3],((u8*)(x))[4],((u8*)(x))[5]
1607 #endif
1608
1609 //#ifdef __GNUC__
1610 #ifdef PLATFORM_LINUX
1611 #define STRUCT_PACKED __attribute__ ((packed))
1612 #else
1613 #define STRUCT_PACKED
1614 #endif
1615
1616
1617 // limitation of path length
1618 #ifdef PLATFORM_LINUX
1619         #define PATH_LENGTH_MAX PATH_MAX
1620 #elif defined(PLATFORM_WINDOWS)
1621         #define PATH_LENGTH_MAX MAX_PATH
1622 #endif
1623
1624
1625 // Suspend lock prevent system from going suspend
1626 #ifdef CONFIG_WAKELOCK
1627 #include <linux/wakelock.h>
1628 #elif defined(CONFIG_ANDROID_POWER)
1629 #include <linux/android_power.h>
1630 #endif
1631
1632 extern void rtw_suspend_lock_init(void);
1633 extern void rtw_suspend_lock_uninit(void);
1634 extern void rtw_lock_suspend(void);
1635 extern void rtw_unlock_suspend(void);
1636 extern void rtw_lock_suspend_timeout(u32 timeout_ms);
1637
1638
1639 //Atomic integer operations
1640 #ifdef PLATFORM_LINUX
1641         #define ATOMIC_T atomic_t
1642 #elif defined(PLATFORM_WINDOWS)
1643         #define ATOMIC_T LONG
1644 #elif defined(PLATFORM_FREEBSD)
1645         typedef uint32_t ATOMIC_T ;
1646 #endif
1647
1648 extern void ATOMIC_SET(ATOMIC_T *v, int i);
1649 extern int ATOMIC_READ(ATOMIC_T *v);
1650 extern void ATOMIC_ADD(ATOMIC_T *v, int i);
1651 extern void ATOMIC_SUB(ATOMIC_T *v, int i);
1652 extern void ATOMIC_INC(ATOMIC_T *v);
1653 extern void ATOMIC_DEC(ATOMIC_T *v);
1654 extern int ATOMIC_ADD_RETURN(ATOMIC_T *v, int i);
1655 extern int ATOMIC_SUB_RETURN(ATOMIC_T *v, int i);
1656 extern int ATOMIC_INC_RETURN(ATOMIC_T *v);
1657 extern int ATOMIC_DEC_RETURN(ATOMIC_T *v);
1658
1659 //File operation APIs, just for linux now
1660 extern int rtw_is_file_readable(char *path);
1661 extern int rtw_retrive_from_file(char *path, u8* buf, u32 sz);
1662 extern int rtw_store_to_file(char *path, u8* buf, u32 sz);
1663
1664
1665 #if 1 //#ifdef MEM_ALLOC_REFINE_ADAPTOR
1666 struct rtw_netdev_priv_indicator {
1667         void *priv;
1668         u32 sizeof_priv;
1669 };
1670 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv, void *old_priv);
1671 extern struct net_device * rtw_alloc_etherdev(int sizeof_priv);
1672
1673 #ifndef PLATFORM_FREEBSD
1674 #define rtw_netdev_priv(netdev) ( ((struct rtw_netdev_priv_indicator *)netdev_priv(netdev))->priv )
1675 #else //PLATFORM_FREEBSD
1676 #define rtw_netdev_priv(netdev) (((struct ifnet *)netdev)->if_softc)
1677 #endif //PLATFORM_FREEBSD
1678
1679 #ifndef PLATFORM_FREEBSD
1680 extern void rtw_free_netdev(struct net_device * netdev);
1681 #else //PLATFORM_FREEBSD
1682 #define rtw_free_netdev(netdev) if_free((netdev))
1683 #endif //PLATFORM_FREEBSD
1684
1685 #else //MEM_ALLOC_REFINE_ADAPTOR
1686
1687 #define rtw_alloc_etherdev(sizeof_priv) alloc_etherdev((sizeof_priv))
1688
1689 #ifndef PLATFORM_FREEBSD
1690 #define rtw_netdev_priv(netdev) netdev_priv((netdev))
1691 #define rtw_free_netdev(netdev) free_netdev((netdev))
1692 #else //PLATFORM_FREEBSD
1693 #define rtw_netdev_priv(netdev) (((struct ifnet *)netdev)->if_softc)
1694 #define rtw_free_netdev(netdev) if_free((netdev))
1695 #endif //PLATFORM_FREEBSD
1696 #endif
1697
1698 #ifdef PLATFORM_LINUX
1699 #define NDEV_FMT "%s"
1700 #define NDEV_ARG(ndev) ndev->name
1701 #define ADPT_FMT "%s"
1702 #define ADPT_ARG(adapter) adapter->pnetdev->name
1703 #define FUNC_NDEV_FMT "%s(%s)"
1704 #define FUNC_NDEV_ARG(ndev) __func__, ndev->name
1705 #define FUNC_ADPT_FMT "%s(%s)"
1706 #define FUNC_ADPT_ARG(adapter) __func__, adapter->pnetdev->name
1707 #else
1708 #define NDEV_FMT "%s"
1709 #define NDEV_ARG(ndev) ""
1710 #define ADPT_FMT "%s"
1711 #define ADPT_ARG(adapter) ""
1712 #define FUNC_NDEV_FMT "%s"
1713 #define FUNC_NDEV_ARG(ndev) __func__
1714 #define FUNC_ADPT_FMT "%s"
1715 #define FUNC_ADPT_ARG(adapter) __func__
1716 #endif
1717
1718 #ifdef PLATFORM_LINUX
1719 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27))
1720 #define rtw_signal_process(pid, sig) kill_pid(find_vpid((pid)),(sig), 1)
1721 #else //(LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27))
1722 #define rtw_signal_process(pid, sig) kill_proc((pid), (sig), 1)
1723 #endif //(LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27))
1724 #endif //PLATFORM_LINUX
1725
1726 extern u64 rtw_modular64(u64 x, u64 y);
1727 extern u64 rtw_division64(u64 x, u64 y);
1728
1729
1730 /* Macros for handling unaligned memory accesses */
1731
1732 #define RTW_GET_BE16(a) ((u16) (((a)[0] << 8) | (a)[1]))
1733 #define RTW_PUT_BE16(a, val)                    \
1734         do {                                    \
1735                 (a)[0] = ((u16) (val)) >> 8;    \
1736                 (a)[1] = ((u16) (val)) & 0xff;  \
1737         } while (0)
1738
1739 #define RTW_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0]))
1740 #define RTW_PUT_LE16(a, val)                    \
1741         do {                                    \
1742                 (a)[1] = ((u16) (val)) >> 8;    \
1743                 (a)[0] = ((u16) (val)) & 0xff;  \
1744         } while (0)
1745
1746 #define RTW_GET_BE24(a) ((((u32) (a)[0]) << 16) | (((u32) (a)[1]) << 8) | \
1747                          ((u32) (a)[2]))                         
1748 #define RTW_PUT_BE24(a, val)                                    \
1749         do {                                                    \
1750                 (a)[0] = (u8) ((((u32) (val)) >> 16) & 0xff);   \
1751                 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff);    \
1752                 (a)[2] = (u8) (((u32) (val)) & 0xff);           \
1753         } while (0)
1754
1755 #define RTW_GET_BE32(a) ((((u32) (a)[0]) << 24) | (((u32) (a)[1]) << 16) | \
1756                          (((u32) (a)[2]) << 8) | ((u32) (a)[3]))                         
1757 #define RTW_PUT_BE32(a, val)                                    \
1758         do {                                                    \
1759                 (a)[0] = (u8) ((((u32) (val)) >> 24) & 0xff);   \
1760                 (a)[1] = (u8) ((((u32) (val)) >> 16) & 0xff);   \
1761                 (a)[2] = (u8) ((((u32) (val)) >> 8) & 0xff);    \
1762                 (a)[3] = (u8) (((u32) (val)) & 0xff);           \
1763         } while (0)
1764
1765 #define RTW_GET_LE32(a) ((((u32) (a)[3]) << 24) | (((u32) (a)[2]) << 16) | \
1766                          (((u32) (a)[1]) << 8) | ((u32) (a)[0]))                         
1767 #define RTW_PUT_LE32(a, val)                                    \
1768         do {                                                    \
1769                 (a)[3] = (u8) ((((u32) (val)) >> 24) & 0xff);   \
1770                 (a)[2] = (u8) ((((u32) (val)) >> 16) & 0xff);   \
1771                 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff);    \
1772                 (a)[0] = (u8) (((u32) (val)) & 0xff);           \
1773         } while (0)
1774
1775 #define RTW_GET_BE64(a) ((((u64) (a)[0]) << 56) | (((u64) (a)[1]) << 48) | \
1776                          (((u64) (a)[2]) << 40) | (((u64) (a)[3]) << 32) | \
1777                          (((u64) (a)[4]) << 24) | (((u64) (a)[5]) << 16) | \
1778                          (((u64) (a)[6]) << 8) | ((u64) (a)[7]))                         
1779 #define RTW_PUT_BE64(a, val)                            \
1780         do {                                            \
1781                 (a)[0] = (u8) (((u64) (val)) >> 56);    \
1782                 (a)[1] = (u8) (((u64) (val)) >> 48);    \
1783                 (a)[2] = (u8) (((u64) (val)) >> 40);    \
1784                 (a)[3] = (u8) (((u64) (val)) >> 32);    \
1785                 (a)[4] = (u8) (((u64) (val)) >> 24);    \
1786                 (a)[5] = (u8) (((u64) (val)) >> 16);    \
1787                 (a)[6] = (u8) (((u64) (val)) >> 8);     \
1788                 (a)[7] = (u8) (((u64) (val)) & 0xff);   \
1789         } while (0)
1790
1791 #define RTW_GET_LE64(a) ((((u64) (a)[7]) << 56) | (((u64) (a)[6]) << 48) | \
1792                          (((u64) (a)[5]) << 40) | (((u64) (a)[4]) << 32) | \
1793                          (((u64) (a)[3]) << 24) | (((u64) (a)[2]) << 16) | \
1794                          (((u64) (a)[1]) << 8) | ((u64) (a)[0]))
1795
1796 void rtw_buf_free(u8 **buf, u32 *buf_len);
1797 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len);
1798
1799 struct rtw_cbuf {
1800         u32 write;
1801         u32 read;
1802         u32 size;
1803         void *bufs[0];
1804 };
1805
1806 bool rtw_cbuf_full(struct rtw_cbuf *cbuf);
1807 bool rtw_cbuf_empty(struct rtw_cbuf *cbuf);
1808 bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf);
1809 void *rtw_cbuf_pop(struct rtw_cbuf *cbuf);
1810 struct rtw_cbuf *rtw_cbuf_alloc(u32 size);
1811 void rtw_cbuf_free(struct rtw_cbuf *cbuf);
1812
1813 #endif
1814
1815