1 /******************************************************************************
3 * Copyright(c) 2007 - 2017 Realtek Corporation.
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 *****************************************************************************/
15 #ifndef __OSDEP_BSD_SERVICE_H_
16 #define __OSDEP_BSD_SERVICE_H_
19 #include <sys/cdefs.h>
20 #include <sys/types.h>
21 #include <sys/systm.h>
22 #include <sys/param.h>
23 #include <sys/sockio.h>
24 #include <sys/sysctl.h>
26 #include <sys/mutex.h>
28 #include <sys/kernel.h>
29 #include <sys/socket.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 #include <sys/module.h>
34 #include <sys/endian.h>
36 #include <sys/kthread.h>
37 #include <sys/malloc.h>
39 #include <machine/atomic.h>
40 #include <machine/bus.h>
41 #include <machine/resource.h>
46 #include <net/if_arp.h>
47 #include <net/ethernet.h>
48 #include <net/if_dl.h>
49 #include <net/if_media.h>
50 #include <net/if_types.h>
51 #include <net/route.h>
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/in_var.h>
57 #include <netinet/if_ether.h>
60 #include <net80211/ieee80211_var.h>
61 #include <net80211/ieee80211_regdomain.h>
62 #include <net80211/ieee80211_radiotap.h>
63 #include <net80211/ieee80211_ratectl.h>
65 #include <dev/usb/usb.h>
66 #include <dev/usb/usbdi.h>
69 #define USB_DEBUG_VAR rum_debug
70 #include <dev/usb/usb_debug.h>
72 #if 1 //Baron porting from linux, it's all temp solution, needs to check again
74 #include <sys/pcpu.h> /* XXX for PCPU_GET */
75 // typedef struct semaphore _sema;
76 typedef struct sema _sema;
77 // typedef spinlock_t _lock;
78 typedef struct mtx _lock;
79 typedef struct mtx _mutex;
80 typedef struct rtw_timer_list _timer;
82 struct list_head *next, *prev;
85 struct list_head queue;
89 typedef struct mbuf _pkt;
90 typedef struct mbuf _buffer;
92 typedef struct __queue _queue;
93 typedef struct list_head _list;
94 typedef int _OS_STATUS;
96 typedef unsigned long _irqL;
97 typedef struct ifnet * _nic_hdl;
99 typedef pid_t _thread_hdl_;
100 // typedef struct thread _thread_hdl_;
101 typedef void thread_return;
102 typedef void* thread_context;
104 typedef void timer_hdl_return;
105 typedef void* timer_hdl_context;
106 typedef struct work_struct _workitem;
108 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
109 /* emulate a modern version */
110 #define LINUX_VERSION_CODE KERNEL_VERSION(2, 6, 35)
112 #define WIRELESS_EXT -1
114 #define spin_lock_irqsave mtx_lock_irqsave
115 #define spin_lock_bh mtx_lock_irqsave
116 #define mtx_lock_irqsave(lock, x) mtx_lock(lock)//{local_irq_save((x)); mtx_lock_spin((lock));}
117 //#define IFT_RTW 0xf9 //ifnet allocate type for RTW
118 #define free_netdev if_free
119 #define LIST_CONTAINOR(ptr, type, member) \
120 ((type *)((char *)(ptr)-(SIZE_T)(&((type *)0)->member)))
121 #define container_of(p,t,n) (t*)((p)-&(((t*)0)->n))
123 * Linux timers are emulated using FreeBSD callout functions
124 * (and taskqueue functionality).
126 * Currently no timer stats functionality.
128 * See (linux_compat) processes.c
131 struct rtw_timer_list {
132 struct callout callout;
133 void (*function)(void *);
137 struct workqueue_struct;
139 typedef void (*work_func_t)(struct work_struct *work);
140 /* Values for the state of an item of work (work_struct) */
141 typedef enum work_state {
142 WORK_STATE_UNSET = 0,
143 WORK_STATE_CALLOUT_PENDING = 1,
144 WORK_STATE_TASK_PENDING = 2,
145 WORK_STATE_WORK_CANCELLED = 3
149 struct task task; /* FreeBSD task */
150 work_state_t state; /* the pending or otherwise state of work. */
153 #define spin_unlock_irqrestore mtx_unlock_irqrestore
154 #define spin_unlock_bh mtx_unlock_irqrestore
155 #define mtx_unlock_irqrestore(lock,x) mtx_unlock(lock);
156 extern void _rtw_spinlock_init(_lock *plock);
158 //modify private structure to match freebsd
159 #define BITS_PER_LONG 32
162 #if BITS_PER_LONG != 64 && !defined(CONFIG_KTIME_SCALAR)
172 #define kmemcheck_bitfield_begin(name)
173 #define kmemcheck_bitfield_end(name)
174 #define CHECKSUM_NONE 0
175 typedef unsigned char *sk_buff_data_t;
176 typedef union ktime ktime_t; /* Kill this */
178 void rtw_mtx_lock(_lock *plock);
180 void rtw_mtx_unlock(_lock *plock);
183 * struct sk_buff - socket buffer
184 * @next: Next buffer in list
185 * @prev: Previous buffer in list
186 * @sk: Socket we are owned by
187 * @tstamp: Time we arrived
188 * @dev: Device we arrived on/are leaving by
189 * @transport_header: Transport layer header
190 * @network_header: Network layer header
191 * @mac_header: Link layer header
192 * @_skb_refdst: destination entry (with norefcount bit)
193 * @sp: the security path, used for xfrm
194 * @cb: Control buffer. Free for use by every layer. Put private vars here
195 * @len: Length of actual data
196 * @data_len: Data length
197 * @mac_len: Length of link layer header
198 * @hdr_len: writable header length of cloned skb
199 * @csum: Checksum (must include start/offset pair)
200 * @csum_start: Offset from skb->head where checksumming should start
201 * @csum_offset: Offset from csum_start where checksum should be stored
202 * @local_df: allow local fragmentation
203 * @cloned: Head may be cloned (check refcnt to be sure)
204 * @nohdr: Payload reference only, must not modify header
205 * @pkt_type: Packet class
206 * @fclone: skbuff clone status
207 * @ip_summed: Driver fed us an IP checksum
208 * @priority: Packet queueing priority
209 * @users: User count - see {datagram,tcp}.c
210 * @protocol: Packet protocol from driver
211 * @truesize: Buffer size
212 * @head: Head of buffer
213 * @data: Data head pointer
214 * @tail: Tail pointer
216 * @destructor: Destruct function
217 * @mark: Generic packet mark
218 * @nfct: Associated connection, if any
219 * @ipvs_property: skbuff is owned by ipvs
220 * @peeked: this packet has been seen already, so stats have been
221 * done for it, don't do them again
222 * @nf_trace: netfilter packet trace flag
223 * @nfctinfo: Relationship of this skb to the connection
224 * @nfct_reasm: netfilter conntrack re-assembly pointer
225 * @nf_bridge: Saved data about a bridged frame - see br_netfilter.c
226 * @skb_iif: ifindex of device we arrived on
227 * @rxhash: the packet hash computed on receive
228 * @queue_mapping: Queue mapping for multiqueue devices
229 * @tc_index: Traffic control index
230 * @tc_verd: traffic control verdict
231 * @ndisc_nodetype: router type (from link layer)
232 * @dma_cookie: a cookie to one of several possible DMA operations
233 * done by skb DMA functions
234 * @secmark: security marking
235 * @vlan_tci: vlan tag control information
239 /* These two members must be first. */
240 struct sk_buff *next;
241 struct sk_buff *prev;
246 //struct net_device *dev;
250 * This is the control buffer. It is free to use for every
251 * layer. Please put your private variables there. If you
252 * want to keep them across layers you have to do a skb_clone()
253 * first. This is owned by whoever has the skb queued ATM.
255 char cb[48] __aligned(8);
257 unsigned long _skb_refdst;
273 kmemcheck_bitfield_begin(flags1);
284 kmemcheck_bitfield_end(flags1);
287 void (*destructor)(struct sk_buff *skb);
288 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
289 struct nf_conntrack *nfct;
290 struct sk_buff *nfct_reasm;
292 #ifdef CONFIG_BRIDGE_NETFILTER
293 struct nf_bridge_info *nf_bridge;
297 #ifdef CONFIG_NET_SCHED
298 u16 tc_index; /* traffic control index */
299 #ifdef CONFIG_NET_CLS_ACT
300 u16 tc_verd; /* traffic control verdict */
306 kmemcheck_bitfield_begin(flags2);
307 u16 queue_mapping:16;
308 #ifdef CONFIG_IPV6_NDISC_NODETYPE
312 u8 deliver_no_wcard:1;
314 kmemcheck_bitfield_end(flags2);
318 #ifdef CONFIG_NET_DMA
319 dma_cookie_t dma_cookie;
321 #ifdef CONFIG_NETWORK_SECMARK
331 sk_buff_data_t transport_header;
332 sk_buff_data_t network_header;
333 sk_buff_data_t mac_header;
334 /* These elements must be at the end, see alloc_skb() for details. */
339 unsigned int truesize;
342 struct sk_buff_head {
343 /* These two members must be first. */
344 struct sk_buff *next;
345 struct sk_buff *prev;
350 #define skb_tail_pointer(skb) skb->tail
351 static inline unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
353 unsigned char *tmp = skb_tail_pointer(skb);
354 //SKB_LINEAR_ASSERT(skb);
360 static inline unsigned char *__skb_pull(struct sk_buff *skb, unsigned int len)
363 if(skb->len < skb->data_len)
364 printf("%s(),%d,error!\n",__FUNCTION__,__LINE__);
365 return skb->data += len;
367 static inline unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
369 #ifdef PLATFORM_FREEBSD
370 return __skb_pull(skb, len);
372 return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
373 #endif //PLATFORM_FREEBSD
375 static inline u32 skb_queue_len(const struct sk_buff_head *list_)
379 static inline void __skb_insert(struct sk_buff *newsk,
380 struct sk_buff *prev, struct sk_buff *next,
381 struct sk_buff_head *list)
385 next->prev = prev->next = newsk;
388 static inline void __skb_queue_before(struct sk_buff_head *list,
389 struct sk_buff *next,
390 struct sk_buff *newsk)
392 __skb_insert(newsk, next->prev, next, list);
394 static inline void skb_queue_tail(struct sk_buff_head *list,
395 struct sk_buff *newsk)
397 mtx_lock(&list->lock);
398 __skb_queue_before(list, (struct sk_buff *)list, newsk);
399 mtx_unlock(&list->lock);
401 static inline struct sk_buff *skb_peek(struct sk_buff_head *list_)
403 struct sk_buff *list = ((struct sk_buff *)list_)->next;
404 if (list == (struct sk_buff *)list_)
408 static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
410 struct sk_buff *next, *prev;
415 skb->next = skb->prev = NULL;
420 static inline struct sk_buff *skb_dequeue(struct sk_buff_head *list)
422 mtx_lock(&list->lock);
424 struct sk_buff *skb = skb_peek(list);
426 __skb_unlink(skb, list);
428 mtx_unlock(&list->lock);
432 static inline void skb_reserve(struct sk_buff *skb, int len)
437 static inline void __skb_queue_head_init(struct sk_buff_head *list)
439 list->prev = list->next = (struct sk_buff *)list;
443 * This function creates a split out lock class for each invocation;
444 * this is needed for now since a whole lot of users of the skb-queue
445 * infrastructure in drivers have different locking usage (in hardirq)
446 * than the networking core (in softirq only). In the long run either the
447 * network layer or drivers should need annotation to consolidate the
448 * main types of usage into 3 classes.
450 static inline void skb_queue_head_init(struct sk_buff_head *list)
452 _rtw_spinlock_init(&list->lock);
453 __skb_queue_head_init(list);
455 unsigned long copy_from_user(void *to, const void *from, unsigned long n);
456 unsigned long copy_to_user(void *to, const void *from, unsigned long n);
457 struct sk_buff * dev_alloc_skb(unsigned int size);
458 struct sk_buff *skb_clone(const struct sk_buff *skb);
459 void dev_kfree_skb_any(struct sk_buff *skb);
460 #endif //Baron porting from linux, it's all temp solution, needs to check again
463 #if 1 // kenny add Linux compatibility code for Linux USB driver
464 #include <dev/usb/usb_compat_linux.h>
466 #define __init // __attribute ((constructor))
467 #define __exit // __attribute ((destructor))
470 * Definitions for module_init and module_exit macros.
472 * These macros will use the SYSINIT framework to call a specified
473 * function (with no arguments) on module loading or unloading.
477 void module_init_exit_wrapper(void *arg);
479 #define module_init(initfn) \
480 SYSINIT(mod_init_ ## initfn, \
481 SI_SUB_KLD, SI_ORDER_FIRST, \
482 module_init_exit_wrapper, initfn)
484 #define module_exit(exitfn) \
485 SYSUNINIT(mod_exit_ ## exitfn, \
486 SI_SUB_KLD, SI_ORDER_ANY, \
487 module_init_exit_wrapper, exitfn)
490 * The usb_register and usb_deregister functions are used to register
491 * usb drivers with the usb subsystem.
493 int usb_register(struct usb_driver *driver);
494 int usb_deregister(struct usb_driver *driver);
497 * usb_get_dev and usb_put_dev - increment/decrement the reference count
498 * of the usb device structure.
500 * Original body of usb_get_dev:
503 * get_device(&dev->dev);
506 * Reference counts are not currently used in this compatibility
507 * layer. So these functions will do nothing.
509 static inline struct usb_device *
510 usb_get_dev(struct usb_device *dev)
516 usb_put_dev(struct usb_device *dev)
522 // rtw_usb_compat_linux
523 int rtw_usb_submit_urb(struct urb *urb, uint16_t mem_flags);
524 int rtw_usb_unlink_urb(struct urb *urb);
525 int rtw_usb_clear_halt(struct usb_device *dev, struct usb_host_endpoint *uhe);
526 int rtw_usb_control_msg(struct usb_device *dev, struct usb_host_endpoint *uhe,
527 uint8_t request, uint8_t requesttype,
528 uint16_t value, uint16_t index, void *data,
529 uint16_t size, usb_timeout_t timeout);
530 int rtw_usb_set_interface(struct usb_device *dev, uint8_t iface_no, uint8_t alt_index);
531 int rtw_usb_setup_endpoint(struct usb_device *dev,
532 struct usb_host_endpoint *uhe, usb_size_t bufsize);
533 struct urb *rtw_usb_alloc_urb(uint16_t iso_packets, uint16_t mem_flags);
534 struct usb_host_endpoint *rtw_usb_find_host_endpoint(struct usb_device *dev, uint8_t type, uint8_t ep);
535 struct usb_host_interface *rtw_usb_altnum_to_altsetting(const struct usb_interface *intf, uint8_t alt_index);
536 struct usb_interface *rtw_usb_ifnum_to_if(struct usb_device *dev, uint8_t iface_no);
537 void *rtw_usbd_get_intfdata(struct usb_interface *intf);
538 void rtw_usb_linux_register(void *arg);
539 void rtw_usb_linux_deregister(void *arg);
540 void rtw_usb_linux_free_device(struct usb_device *dev);
541 void rtw_usb_free_urb(struct urb *urb);
542 void rtw_usb_init_urb(struct urb *urb);
543 void rtw_usb_kill_urb(struct urb *urb);
544 void rtw_usb_set_intfdata(struct usb_interface *intf, void *data);
545 void rtw_usb_fill_bulk_urb(struct urb *urb, struct usb_device *udev,
546 struct usb_host_endpoint *uhe, void *buf,
547 int length, usb_complete_t callback, void *arg);
548 int rtw_usb_bulk_msg(struct usb_device *udev, struct usb_host_endpoint *uhe,
549 void *data, int len, uint16_t *pactlen, usb_timeout_t timeout);
550 void *usb_get_intfdata(struct usb_interface *intf);
551 int usb_linux_init_endpoints(struct usb_device *udev);
555 typedef struct urb * PURB;
557 typedef unsigned gfp_t;
558 #define __GFP_WAIT ((gfp_t)0x10u) /* Can wait and reschedule? */
559 #define __GFP_HIGH ((gfp_t)0x20u) /* Should access emergency pools? */
560 #define __GFP_IO ((gfp_t)0x40u) /* Can start physical IO? */
561 #define __GFP_FS ((gfp_t)0x80u) /* Can call down to low-level FS? */
562 #define __GFP_COLD ((gfp_t)0x100u) /* Cache-cold page required */
563 #define __GFP_NOWARN ((gfp_t)0x200u) /* Suppress page allocation failure warning */
564 #define __GFP_REPEAT ((gfp_t)0x400u) /* Retry the allocation. Might fail */
565 #define __GFP_NOFAIL ((gfp_t)0x800u) /* Retry for ever. Cannot fail */
566 #define __GFP_NORETRY ((gfp_t)0x1000u)/* Do not retry. Might fail */
567 #define __GFP_NO_GROW ((gfp_t)0x2000u)/* Slab internal usage */
568 #define __GFP_COMP ((gfp_t)0x4000u)/* Add compound page metadata */
569 #define __GFP_ZERO ((gfp_t)0x8000u)/* Return zeroed page on success */
570 #define __GFP_NOMEMALLOC ((gfp_t)0x10000u) /* Don't use emergency reserves */
571 #define __GFP_HARDWALL ((gfp_t)0x20000u) /* Enforce hardwall cpuset memory allocs */
573 /* This equals 0, but use constants in case they ever change */
574 #define GFP_NOWAIT (GFP_ATOMIC & ~__GFP_HIGH)
575 /* GFP_ATOMIC means both !wait (__GFP_WAIT not set) and use emergency pool */
576 #define GFP_ATOMIC (__GFP_HIGH)
577 #define GFP_NOIO (__GFP_WAIT)
578 #define GFP_NOFS (__GFP_WAIT | __GFP_IO)
579 #define GFP_KERNEL (__GFP_WAIT | __GFP_IO | __GFP_FS)
580 #define GFP_USER (__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_HARDWALL)
581 #define GFP_HIGHUSER (__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_HARDWALL | \
585 #endif // kenny add Linux compatibility code for Linux USB
587 __inline static _list *get_next(_list *list)
592 __inline static _list *get_list_head(_queue *queue)
594 return (&(queue->queue));
598 #define LIST_CONTAINOR(ptr, type, member) \
599 ((type *)((char *)(ptr)-(SIZE_T)(&((type *)0)->member)))
602 __inline static void _enter_critical(_lock *plock, _irqL *pirqL)
604 spin_lock_irqsave(plock, *pirqL);
607 __inline static void _exit_critical(_lock *plock, _irqL *pirqL)
609 spin_unlock_irqrestore(plock, *pirqL);
612 __inline static void _enter_critical_ex(_lock *plock, _irqL *pirqL)
614 spin_lock_irqsave(plock, *pirqL);
617 __inline static void _exit_critical_ex(_lock *plock, _irqL *pirqL)
619 spin_unlock_irqrestore(plock, *pirqL);
622 __inline static void _enter_critical_bh(_lock *plock, _irqL *pirqL)
624 spin_lock_bh(plock, *pirqL);
627 __inline static void _exit_critical_bh(_lock *plock, _irqL *pirqL)
629 spin_unlock_bh(plock, *pirqL);
632 __inline static void _enter_critical_mutex(_mutex *pmutex, _irqL *pirqL)
640 __inline static void _exit_critical_mutex(_mutex *pmutex, _irqL *pirqL)
646 static inline void __list_del(struct list_head * prev, struct list_head * next)
651 static inline void INIT_LIST_HEAD(struct list_head *list)
656 __inline static void rtw_list_delete(_list *plist)
658 __list_del(plist->prev, plist->next);
659 INIT_LIST_HEAD(plist);
662 static inline void timer_hdl(void *ctx)
664 _timer *timer = (_timer *)ctx;
667 if (callout_pending(&timer->callout)) {
668 /* callout was reset */
669 rtw_mtx_unlock(NULL);
673 if (!callout_active(&timer->callout)) {
674 /* callout was stopped */
675 rtw_mtx_unlock(NULL);
679 callout_deactivate(&timer->callout);
681 timer->function(timer->arg);
683 rtw_mtx_unlock(NULL);
686 static inline void _init_timer(_timer *ptimer, _nic_hdl padapter, void *pfunc, void *cntx)
688 ptimer->function = pfunc;
690 callout_init(&ptimer->callout, CALLOUT_MPSAFE);
693 __inline static void _set_timer(_timer *ptimer,u32 delay_time)
695 if (ptimer->function && ptimer->arg) {
697 callout_reset(&ptimer->callout, delay_time, timer_hdl, ptimer);
698 rtw_mtx_unlock(NULL);
702 __inline static void _cancel_timer(_timer *ptimer,u8 *bcancelled)
705 callout_drain(&ptimer->callout);
706 rtw_mtx_unlock(NULL);
707 *bcancelled = 1; /* assume an pending timer to be canceled */
710 __inline static void _init_workitem(_workitem *pwork, void *pfunc, PVOID cntx)
712 printf("%s Not implement yet! \n",__FUNCTION__);
715 __inline static void _set_workitem(_workitem *pwork)
717 printf("%s Not implement yet! \n",__FUNCTION__);
718 // schedule_work(pwork);
722 // Global Mutex: can only be used at PASSIVE level.
725 #define ACQUIRE_GLOBAL_MUTEX(_MutexCounter) \
729 #define RELEASE_GLOBAL_MUTEX(_MutexCounter) \
733 #define ATOMIC_INIT(i) { (i) }
735 static __inline void thread_enter(char *name);
737 //Atomic integer operations
738 typedef uint32_t ATOMIC_T ;
740 #define rtw_netdev_priv(netdev) (((struct ifnet *)netdev)->if_softc)
742 #define rtw_free_netdev(netdev) if_free((netdev))
744 #define NDEV_FMT "%s"
745 #define NDEV_ARG(ndev) ""
746 #define ADPT_FMT "%s"
747 #define ADPT_ARG(adapter) ""
748 #define FUNC_NDEV_FMT "%s"
749 #define FUNC_NDEV_ARG(ndev) __func__
750 #define FUNC_ADPT_FMT "%s"
751 #define FUNC_ADPT_ARG(adapter) __func__
753 #define STRUCT_PACKED