hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
/******************************************************************************
 *
 * Copyright(c) 2007 - 2019 Realtek Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 *****************************************************************************/
#ifndef __OSDEP_BSD_SERVICE_H_
#define __OSDEP_BSD_SERVICE_H_
 
 
#include <sys/cdefs.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/param.h>
#include <sys/sockio.h>
#include <sys/sysctl.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/mbuf.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/endian.h>
#include <sys/kdb.h>
#include <sys/kthread.h>
#include <sys/malloc.h>
#include <sys/time.h>
#include <machine/atomic.h>
#include <machine/bus.h>
#include <machine/resource.h>
#include <sys/rman.h>
 
#include <net/bpf.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <net/ethernet.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_types.h>
#include <net/route.h>
 
 
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/in_var.h>
#include <netinet/if_ether.h>
#include <if_ether.h>
 
#include <net80211/ieee80211_var.h>
#include <net80211/ieee80211_regdomain.h>
#include <net80211/ieee80211_radiotap.h>
#include <net80211/ieee80211_ratectl.h>
 
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include "usbdevs.h"
 
#define    USB_DEBUG_VAR rum_debug
#include <dev/usb/usb_debug.h>
 
#if 1 //Baron porting from linux, it's all temp solution, needs to check again
#include <sys/sema.h>
#include <sys/pcpu.h> /* XXX for PCPU_GET */
//    typedef struct     semaphore _sema;
   typedef struct     sema _sema;
//    typedef    spinlock_t    _lock;
   typedef    struct mtx    _lock;
   typedef struct mtx         _mutex;
   typedef struct rtw_timer_list _timer;
   struct list_head {
   struct list_head *next, *prev;
   };
   struct    __queue    {
       struct    list_head    queue;    
       _lock    lock;
   };
 
   typedef struct mbuf    _buffer;
   
   typedef struct    __queue    _queue;
   typedef struct    list_head    _list;
 
   typedef    struct    ifnet * _nic_hdl;
   
   typedef pid_t        _thread_hdl_;
//    typedef struct thread        _thread_hdl_;
   typedef void        thread_return;
   typedef void*    thread_context;
 
   typedef void timer_hdl_return;
   typedef void* timer_hdl_context;
   typedef struct work_struct _workitem;
   typedef struct task _tasklet;
 
#define   KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
/* emulate a modern version */
#define LINUX_VERSION_CODE KERNEL_VERSION(2, 6, 35)
 
#define WIRELESS_EXT -1
#define HZ hz
 
//#define IFT_RTW    0xf9 //ifnet allocate type for RTW
#define free_netdev if_free
#define LIST_CONTAINOR(ptr, type, member) \
        ((type *)((char *)(ptr)-(SIZE_T)(&((type *)0)->member)))
#define container_of(p,t,n) (t*)((p)-&(((t*)0)->n))
 
/*lock - spinlock*/
static inline void _rtw_spinlock_init(_lock *plock)
{
   mtx_init(plock, "", NULL, MTX_DEF | MTX_RECURSE);
}
 
static inline void _rtw_spinlock_free(_lock *plock)
{
   mtx_destroy(plock);
}
 
static inline void _rtw_spinlock(_lock    *plock)
{
   mtx_lock(plock);
}
 
static inline void _rtw_spinunlock(_lock *plock)
{
   mtx_unlock(plock);
}
 
#if 0
static inline void _rtw_spinlock_ex(_lock *plock)
{
   mtx_lock(plock);
}
 
static inline void _rtw_spinunlock_ex(_lock *plock)
{
   mtx_unlock(plock);
}
#endif
 
__inline static void _rtw_spinlock_irq(_lock *plock, unsigned long *flags)
{
   mtx_lock(plock); /*{local_irq_save((x)); mtx_lock_spin((lock));}*/
}
 
__inline static void _rtw_spinunlock_irq(_lock *plock, unsigned long *flags)
{
   mtx_unlock(plock);
}
 
__inline static void _rtw_spinlock_bh(_lock *plock)
{
   mtx_lock(plock);/*{local_irq_save((x)); mtx_lock_spin((lock));}*/
}
 
__inline static void _rtw_spinunlock_bh(_lock *plock)
{
   mtx_unlock(plock);
}
 
/*lock - semaphore*/
static inline void _rtw_init_sema(_sema *sema, int init_val)
{
   sema_init(sema, init_val, "rtw_drv");
}
 
static inline void _rtw_free_sema(_sema *sema)
{
   sema_destroy(sema);
}
 
static inline void _rtw_up_sema(_sema *sema)
{
   sema_post(sema);
}
 
static inline u32 _rtw_down_sema(_sema *sema)
{
   sema_wait(sema);
   return  _SUCCESS;
}
 
/*lock - mutex*/
static inline void _rtw_mutex_init(_mutex *pmutex)
{
   mtx_init(pmutex, "", NULL, MTX_DEF | MTX_RECURSE);
}
 
static inline void _rtw_mutex_free(_mutex *pmutex)
{
   sema_destroy(pmutex);
}
 
__inline static void _rtw_mutex_lock_interruptible(_mutex *pmutex)
{
 
   mtx_lock(pmutex);
 
}
__inline static void _rtw_mutex_lock(_mutex *pmutex)
{
 
   mtx_lock(pmutex);
 
}
 
__inline static void _rtw_mutex_unlock(_mutex *pmutex)
{
   mtx_unlock(pmutex);
}
 
static inline void *_rtw_vmalloc(u32 sz)
{
   void *pbuf;
   pbuf = malloc(sz, M_DEVBUF, M_NOWAIT);
 
   return pbuf;
}
 
static inline void *_rtw_zvmalloc(u32 sz)
{
   void *pbuf;
 
   pbuf = malloc(sz, M_DEVBUF, M_ZERO | M_NOWAIT);
   return pbuf;
}
 
static inline void _rtw_vmfree(void *pbuf, u32 sz)
{
   free(pbuf, M_DEVBUF);
}
 
static inline void *_rtw_malloc(u32 sz)
{
   void *pbuf = NULL;
 
   pbuf = malloc(sz, M_DEVBUF, M_NOWAIT);
   return pbuf;
}
static inline void *_rtw_zmalloc(u32 sz)
{
   return malloc(sz, M_DEVBUF, M_ZERO | M_NOWAIT);
}
static inline void _rtw_mfree(void *pbuf, u32 sz)
{
   free(pbuf, M_DEVBUF);
}
#ifdef CONFIG_USB_HCI
static inline void *_rtw_usb_buffer_alloc(struct usb_device *dev, size_t size, dma_addr_t *dma)
{
   return malloc(size, M_USBDEV, M_NOWAIT | M_ZERO);
}
static inline void _rtw_usb_buffer_free(struct usb_device *dev, size_t size, void *addr, dma_addr_t dma)
{
   free(addr, M_USBDEV);
}
#endif /* CONFIG_USB_HCI */
 
struct sk_buff *_rtw_skb_alloc(u32 sz);
void _rtw_skb_free(struct sk_buff *skb);
static inline struct sk_buff *_rtw_skb_copy(const struct sk_buff *skb)
{
   return NULL;
}
 
static inline struct sk_buff *_rtw_skb_clone(struct sk_buff *skb)
{
   return skb_clone(skb);
}
 
static inline int _rtw_netif_rx(_nic_hdl ndev, struct sk_buff *skb)
{
   return (*ndev->if_input)(ndev, skb);
}
 
#ifdef CONFIG_RTW_NAPI
static inline int _rtw_netif_receive_skb(_nic_hdl ndev, struct sk_buff *skb)
{
   rtw_warn_on(1);
   return -1;
}
 
#ifdef CONFIG_RTW_GRO
static inline gro_result_t _rtw_napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
{
   rtw_warn_on(1);
   return -1;
}
#endif /* CONFIG_RTW_GRO */
#endif /* CONFIG_RTW_NAPI */
 
 
/* 
 * Linux timers are emulated using FreeBSD callout functions
 * (and taskqueue functionality).
 *
 * Currently no timer stats functionality.
 *
 * See (linux_compat) processes.c
 *
 */
struct rtw_timer_list {
   struct callout callout;
   void (*function)(void *);
   void *arg;
};
 
struct workqueue_struct;
struct work_struct;
typedef void (*work_func_t)(struct work_struct *work);
/* Values for the state of an item of work (work_struct) */
typedef enum work_state {
        WORK_STATE_UNSET = 0,
        WORK_STATE_CALLOUT_PENDING = 1,
        WORK_STATE_TASK_PENDING = 2,
        WORK_STATE_WORK_CANCELLED = 3        
} work_state_t;
 
struct work_struct {
        struct task task; /* FreeBSD task */
        work_state_t state; /* the pending or otherwise state of work. */
        work_func_t func;       
};
 
//modify private structure to match freebsd
#define BITS_PER_LONG 32
union ktime {
   s64    tv64;
#if BITS_PER_LONG != 64 && !defined(CONFIG_KTIME_SCALAR)
   struct {
#ifdef __BIG_ENDIAN
   s32    sec, nsec;
#else
   s32    nsec, sec;
#endif
   } tv;
#endif
};
#define kmemcheck_bitfield_begin(name)
#define kmemcheck_bitfield_end(name)
#define CHECKSUM_NONE 0
typedef unsigned char *sk_buff_data_t;
typedef union ktime ktime_t;        /* Kill this */
 
void rtw_mtx_lock(_lock *plock);
   
void rtw_mtx_unlock(_lock *plock);
 
/** 
 *    struct sk_buff - socket buffer
 *    @next: Next buffer in list
 *    @prev: Previous buffer in list
 *    @sk: Socket we are owned by
 *    @tstamp: Time we arrived
 *    @dev: Device we arrived on/are leaving by
 *    @transport_header: Transport layer header
 *    @network_header: Network layer header
 *    @mac_header: Link layer header
 *    @_skb_refdst: destination entry (with norefcount bit)
 *    @sp: the security path, used for xfrm
 *    @cb: Control buffer. Free for use by every layer. Put private vars here
 *    @len: Length of actual data
 *    @data_len: Data length
 *    @mac_len: Length of link layer header
 *    @hdr_len: writable header length of cloned skb
 *    @csum: Checksum (must include start/offset pair)
 *    @csum_start: Offset from skb->head where checksumming should start
 *    @csum_offset: Offset from csum_start where checksum should be stored
 *    @local_df: allow local fragmentation
 *    @cloned: Head may be cloned (check refcnt to be sure)
 *    @nohdr: Payload reference only, must not modify header
 *    @pkt_type: Packet class
 *    @fclone: skbuff clone status
 *    @ip_summed: Driver fed us an IP checksum
 *    @priority: Packet queueing priority
 *    @users: User count - see {datagram,tcp}.c
 *    @protocol: Packet protocol from driver
 *    @truesize: Buffer size 
 *    @head: Head of buffer
 *    @data: Data head pointer
 *    @tail: Tail pointer
 *    @end: End pointer
 *    @destructor: Destruct function
 *    @mark: Generic packet mark
 *    @nfct: Associated connection, if any
 *    @ipvs_property: skbuff is owned by ipvs
 *    @peeked: this packet has been seen already, so stats have been
 *        done for it, don't do them again
 *    @nf_trace: netfilter packet trace flag
 *    @nfctinfo: Relationship of this skb to the connection
 *    @nfct_reasm: netfilter conntrack re-assembly pointer
 *    @nf_bridge: Saved data about a bridged frame - see br_netfilter.c
 *    @skb_iif: ifindex of device we arrived on
 *    @rxhash: the packet hash computed on receive
 *    @queue_mapping: Queue mapping for multiqueue devices
 *    @tc_index: Traffic control index
 *    @tc_verd: traffic control verdict
 *    @ndisc_nodetype: router type (from link layer)
 *    @dma_cookie: a cookie to one of several possible DMA operations
 *        done by skb DMA functions
 *    @secmark: security marking
 *    @vlan_tci: vlan tag control information
 */
 
struct sk_buff {
   /* These two members must be first. */
   struct sk_buff        *next;
   struct sk_buff        *prev;
 
   ktime_t            tstamp;
 
   struct sock        *sk;
   //struct net_device    *dev;
   struct ifnet *dev;
 
   /*
    * This is the control buffer. It is free to use for every
    * layer. Please put your private variables there. If you
    * want to keep them across layers you have to do a skb_clone()
    * first. This is owned by whoever has the skb queued ATM.
    */
   char            cb[48] __aligned(8);
 
   unsigned long        _skb_refdst;
#ifdef CONFIG_XFRM
   struct    sec_path    *sp;
#endif
   unsigned int        len,
               data_len;
   u16            mac_len,
               hdr_len;
   union {
       u32        csum;
       struct {
           u16    csum_start;
           u16    csum_offset;
       }smbol2;
   }smbol1;
   u32            priority;
   kmemcheck_bitfield_begin(flags1);
   u8            local_df:1,
               cloned:1,
               ip_summed:2,
               nohdr:1,
               nfctinfo:3;
   u8            pkt_type:3,
               fclone:2,
               ipvs_property:1,
               peeked:1,
               nf_trace:1;
   kmemcheck_bitfield_end(flags1);
   u16            protocol;
 
   void            (*destructor)(struct sk_buff *skb);
#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
   struct nf_conntrack    *nfct;
   struct sk_buff        *nfct_reasm;
#endif
#ifdef CONFIG_BRIDGE_NETFILTER
   struct nf_bridge_info    *nf_bridge;
#endif
 
   int            skb_iif;
#ifdef CONFIG_NET_SCHED
   u16            tc_index;    /* traffic control index */
#ifdef CONFIG_NET_CLS_ACT
   u16            tc_verd;    /* traffic control verdict */
#endif
#endif
 
   u32            rxhash;
 
   kmemcheck_bitfield_begin(flags2);
   u16            queue_mapping:16;
#ifdef CONFIG_IPV6_NDISC_NODETYPE
   u8            ndisc_nodetype:2,
               deliver_no_wcard:1;
#else
   u8            deliver_no_wcard:1;
#endif
   kmemcheck_bitfield_end(flags2);
 
   /* 0/14 bit hole */
 
#ifdef CONFIG_NET_DMA
   dma_cookie_t        dma_cookie;
#endif
#ifdef CONFIG_NETWORK_SECMARK
   u32            secmark;
#endif
   union {
       u32        mark;
       u32        dropcount;
   }symbol3;
 
   u16            vlan_tci;
 
   sk_buff_data_t        transport_header;
   sk_buff_data_t        network_header;
   sk_buff_data_t        mac_header;
   /* These elements must be at the end, see alloc_skb() for details.  */
   sk_buff_data_t        tail;
   sk_buff_data_t        end;
   unsigned char        *head,
               *data;
   unsigned int        truesize;
   ATOMIC_T        users;
};
struct sk_buff_head {
   /* These two members must be first. */
   struct sk_buff    *next;
   struct sk_buff    *prev;
 
   u32        qlen;
   _lock    lock;
};
#define skb_tail_pointer(skb)    skb->tail
static inline unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
{
   unsigned char *tmp = skb_tail_pointer(skb);
   //SKB_LINEAR_ASSERT(skb);
   skb->tail += len;
   skb->len  += len;
   return tmp;
}
 
static inline unsigned char *__skb_pull(struct sk_buff *skb, unsigned int len)
{
   skb->len -= len;
   if(skb->len < skb->data_len)
       printf("%s(),%d,error!\n",__FUNCTION__,__LINE__);
   return skb->data += len;
}
static inline unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
{
   return __skb_pull(skb, len);
}
static inline u32 skb_queue_len(const struct sk_buff_head *list_)
{
   return list_->qlen;
}
static inline void __skb_insert(struct sk_buff *newsk,
               struct sk_buff *prev, struct sk_buff *next,
               struct sk_buff_head *list)
{
   newsk->next = next;
   newsk->prev = prev;
   next->prev  = prev->next = newsk;
   list->qlen++;
}
static inline void __skb_queue_before(struct sk_buff_head *list,
                     struct sk_buff *next,
                     struct sk_buff *newsk)
{
   __skb_insert(newsk, next->prev, next, list);
}
static inline void skb_queue_tail(struct sk_buff_head *list,
                  struct sk_buff *newsk)
{
   mtx_lock(&list->lock);
   __skb_queue_before(list, (struct sk_buff *)list, newsk);
   mtx_unlock(&list->lock);
}
static inline struct sk_buff *skb_peek(struct sk_buff_head *list_)
{
   struct sk_buff *list = ((struct sk_buff *)list_)->next;
   if (list == (struct sk_buff *)list_)
       list = NULL;
   return list;
}
static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
{
   struct sk_buff *next, *prev;
 
   list->qlen--;
   next       = skb->next;
   prev       = skb->prev;
   skb->next  = skb->prev = NULL;
   next->prev = prev;
   prev->next = next;
}
 
static inline struct sk_buff *skb_dequeue(struct sk_buff_head *list)
{
   mtx_lock(&list->lock);
 
   struct sk_buff *skb = skb_peek(list);
   if (skb)
       __skb_unlink(skb, list);
 
   mtx_unlock(&list->lock);
 
   return skb;
}
static inline void skb_reserve(struct sk_buff *skb, int len)
{
   skb->data += len;
   skb->tail += len;
}
static inline void __skb_queue_head_init(struct sk_buff_head *list)
{
   list->prev = list->next = (struct sk_buff *)list;
   list->qlen = 0;
}
/*
 * This function creates a split out lock class for each invocation;
 * this is needed for now since a whole lot of users of the skb-queue
 * infrastructure in drivers have different locking usage (in hardirq)
 * than the networking core (in softirq only). In the long run either the
 * network layer or drivers should need annotation to consolidate the
 * main types of usage into 3 classes.
 */
static inline void skb_queue_head_init(struct sk_buff_head *list)
{
   _rtw_spinlock_init(&list->lock);
   __skb_queue_head_init(list);
}
static inline u8 *rtw_skb_data(struct sk_buff *pkt)
{
   return pkt->data;
}
 
static inline u32 rtw_skb_len(struct sk_buff *pkt)
{
   return pkt->len;
}
 
unsigned long copy_from_user(void *to, const void *from, unsigned long n);
unsigned long copy_to_user(void *to, const void *from, unsigned long n);
struct sk_buff * dev_alloc_skb(unsigned int size);
struct sk_buff *skb_clone(const struct sk_buff *skb);
void dev_kfree_skb_any(struct sk_buff *skb);
#endif //Baron porting from linux, it's all temp solution, needs to check again
 
 
#if 1 // kenny add Linux compatibility code for Linux USB driver
#include <dev/usb/usb_compat_linux.h>
 
#define __init        // __attribute ((constructor))
#define __exit        // __attribute ((destructor))
 
/*
 * Definitions for module_init and module_exit macros.
 *
 * These macros will use the SYSINIT framework to call a specified
 * function (with no arguments) on module loading or unloading.
 * 
 */
 
void module_init_exit_wrapper(void *arg);
 
#define module_init(initfn)                             \
        SYSINIT(mod_init_ ## initfn,                    \
                SI_SUB_KLD, SI_ORDER_FIRST,             \
                module_init_exit_wrapper, initfn)
 
#define module_exit(exitfn)                             \
        SYSUNINIT(mod_exit_ ## exitfn,                  \
                  SI_SUB_KLD, SI_ORDER_ANY,             \
                  module_init_exit_wrapper, exitfn)
 
/*
 * The usb_register and usb_deregister functions are used to register
 * usb drivers with the usb subsystem. 
 */
int usb_register(struct usb_driver *driver);
int usb_deregister(struct usb_driver *driver);
 
/*
 * usb_get_dev and usb_put_dev - increment/decrement the reference count 
 * of the usb device structure.
 *
 * Original body of usb_get_dev:
 *
 *       if (dev)
 *               get_device(&dev->dev);
 *       return dev;
 *
 * Reference counts are not currently used in this compatibility
 * layer. So these functions will do nothing.
 */
static inline struct usb_device *
usb_get_dev(struct usb_device *dev)
{
        return dev;
}
 
static inline void 
usb_put_dev(struct usb_device *dev)
{
        return;
}
 
 
// rtw_usb_compat_linux
int rtw_usb_submit_urb(struct urb *urb, uint16_t mem_flags);
int rtw_usb_unlink_urb(struct urb *urb);
int rtw_usb_clear_halt(struct usb_device *dev, struct usb_host_endpoint *uhe);
int rtw_usb_control_msg(struct usb_device *dev, struct usb_host_endpoint *uhe,
    uint8_t request, uint8_t requesttype,
    uint16_t value, uint16_t index, void *data,
    uint16_t size, usb_timeout_t timeout);
int rtw_usb_set_interface(struct usb_device *dev, uint8_t iface_no, uint8_t alt_index);
int rtw_usb_setup_endpoint(struct usb_device *dev,
    struct usb_host_endpoint *uhe, usb_size_t bufsize);
struct urb *rtw_usb_alloc_urb(uint16_t iso_packets, uint16_t mem_flags);
struct usb_host_endpoint *rtw_usb_find_host_endpoint(struct usb_device *dev, uint8_t type, uint8_t ep);
struct usb_host_interface *rtw_usb_altnum_to_altsetting(const struct usb_interface *intf, uint8_t alt_index);
struct usb_interface *rtw_usb_ifnum_to_if(struct usb_device *dev, uint8_t iface_no);
void *rtw_usb_get_intfdata(struct usb_interface *intf);
void rtw_usb_linux_register(void *arg);
void rtw_usb_linux_deregister(void *arg);
void rtw_usb_linux_free_device(struct usb_device *dev);
void rtw_usb_free_urb(struct urb *urb);
void rtw_usb_init_urb(struct urb *urb);
void rtw_usb_kill_urb(struct urb *urb);
void rtw_usb_set_intfdata(struct usb_interface *intf, void *data);
void rtw_usb_fill_bulk_urb(struct urb *urb, struct usb_device *udev,
    struct usb_host_endpoint *uhe, void *buf,
    int length, usb_complete_t callback, void *arg);
int rtw_usb_bulk_msg(struct usb_device *udev, struct usb_host_endpoint *uhe,
    void *data, int len, uint16_t *pactlen, usb_timeout_t timeout);
void *usb_get_intfdata(struct usb_interface *intf);
int usb_linux_init_endpoints(struct usb_device *udev);
 
 
 
typedef struct urb *  PURB;
 
typedef unsigned gfp_t;
#define __GFP_WAIT      ((gfp_t)0x10u)  /* Can wait and reschedule? */
#define __GFP_HIGH      ((gfp_t)0x20u)  /* Should access emergency pools? */
#define __GFP_IO        ((gfp_t)0x40u)  /* Can start physical IO? */
#define __GFP_FS        ((gfp_t)0x80u)  /* Can call down to low-level FS? */
#define __GFP_COLD      ((gfp_t)0x100u) /* Cache-cold page required */
#define __GFP_NOWARN    ((gfp_t)0x200u) /* Suppress page allocation failure warning */
#define __GFP_REPEAT    ((gfp_t)0x400u) /* Retry the allocation.  Might fail */
#define __GFP_NOFAIL    ((gfp_t)0x800u) /* Retry for ever.  Cannot fail */
#define __GFP_NORETRY   ((gfp_t)0x1000u)/* Do not retry.  Might fail */
#define __GFP_NO_GROW   ((gfp_t)0x2000u)/* Slab internal usage */
#define __GFP_COMP      ((gfp_t)0x4000u)/* Add compound page metadata */
#define __GFP_ZERO      ((gfp_t)0x8000u)/* Return zeroed page on success */
#define __GFP_NOMEMALLOC ((gfp_t)0x10000u) /* Don't use emergency reserves */
#define __GFP_HARDWALL   ((gfp_t)0x20000u) /* Enforce hardwall cpuset memory allocs */
 
/* This equals 0, but use constants in case they ever change */
#define GFP_NOWAIT      (GFP_ATOMIC & ~__GFP_HIGH)
/* GFP_ATOMIC means both !wait (__GFP_WAIT not set) and use emergency pool */
#define GFP_ATOMIC      (__GFP_HIGH)
#define GFP_NOIO        (__GFP_WAIT)
#define GFP_NOFS        (__GFP_WAIT | __GFP_IO)
#define GFP_KERNEL      (__GFP_WAIT | __GFP_IO | __GFP_FS)
#define GFP_USER        (__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_HARDWALL)
#define GFP_HIGHUSER    (__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_HARDWALL | \
                         __GFP_HIGHMEM)
 
 
#endif // kenny add Linux compatibility code for Linux USB
 
__inline static _list *get_next(_list    *list)
{
   return list->next;
}    
 
__inline static _list    *get_list_head(_queue    *queue)
{
   return (&(queue->queue));
}
 
   
#define LIST_CONTAINOR(ptr, type, member) \
        ((type *)((char *)(ptr)-(SIZE_T)(&((type *)0)->member)))    
 
        
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
   next->prev = prev;
   prev->next = next;
}
static inline void INIT_LIST_HEAD(struct list_head *list)
{
   list->next = list;
   list->prev = list;
}
__inline static void rtw_list_delete(_list *plist)
{
   __list_del(plist->prev, plist->next);
   INIT_LIST_HEAD(plist);
}
 
static inline void timer_hdl(void *ctx)
{
   _timer *timer = (_timer *)ctx;
 
   rtw_mtx_lock(NULL);
   if (callout_pending(&timer->callout)) {
       /* callout was reset */
       rtw_mtx_unlock(NULL);
       return;
   }
 
   if (!callout_active(&timer->callout)) {
       /* callout was stopped */
       rtw_mtx_unlock(NULL);
       return;
   }
 
   callout_deactivate(&timer->callout);
 
   timer->function(timer->arg);
 
   rtw_mtx_unlock(NULL);
}
 
static inline void _init_timer(_timer *ptimer, void *pfunc, void *cntx)
{
   ptimer->function = pfunc;
   ptimer->arg = cntx;
   callout_init(&ptimer->callout, CALLOUT_MPSAFE);
}
 
__inline static int _check_timer_is_active(_timer *ptimer)
{
   rtw_mtx_lock(NULL);
   if (callout_pending(&ptimer->callout)) {
       /* callout was reset */
       rtw_mtx_unlock(NULL);
       return _TRUE;
   }
   rtw_mtx_unlock(NULL);
       return _FALSE;
}
 
__inline static void _set_timer(_timer *ptimer,u32 delay_time)
{    
   if (ptimer->function && ptimer->arg) {
       rtw_mtx_lock(NULL);
       callout_reset(&ptimer->callout, delay_time, timer_hdl, ptimer);
       rtw_mtx_unlock(NULL);
   }
}
 
__inline static void _cancel_timer(_timer *ptimer,u8 *bcancelled)
{
   rtw_mtx_lock(NULL);
   callout_drain(&ptimer->callout);
   rtw_mtx_unlock(NULL);
   *bcancelled = 1; /* assume an pending timer to be canceled */
}
 
__inline static void _init_workitem(_workitem *pwork, void *pfunc, PVOID cntx)
{
   printf("%s Not implement yet! \n",__FUNCTION__);
}
 
__inline static void _set_workitem(_workitem *pwork)
{
   printf("%s Not implement yet! \n",__FUNCTION__);
//    schedule_work(pwork);
}
 
//
// Global Mutex: can only be used at PASSIVE level.
//
 
#define ACQUIRE_GLOBAL_MUTEX(_MutexCounter)                              \
{                                                               \
}
 
#define RELEASE_GLOBAL_MUTEX(_MutexCounter)                              \
{                                                               \
}
 
/* Atomic integer operations */
#define ATOMIC_T atomic_t
 
#define ATOMIC_INIT(i)  { (i) }
static inline void ATOMIC_SET(ATOMIC_T *v, int i)
{
   atomic_set_int(v, i);
}
 
static inline int ATOMIC_READ(ATOMIC_T *v)
{
   return atomic_load_acq_32(v);
}
 
static inline void ATOMIC_ADD(ATOMIC_T *v, int i)
{
   atomic_add_int(v, i);
}
static inline void ATOMIC_SUB(ATOMIC_T *v, int i)
{
   atomic_subtract_int(v, i);
}
 
static inline void ATOMIC_INC(ATOMIC_T *v)
{
   atomic_add_int(v, 1);
}
 
static inline void ATOMIC_DEC(ATOMIC_T *v)
{
   atomic_subtract_int(v, 1);
}
 
static inline int ATOMIC_ADD_RETURN(ATOMIC_T *v, int i)
{
   atomic_add_int(v, i);
   return atomic_load_acq_32(v);
}
 
static inline int ATOMIC_SUB_RETURN(ATOMIC_T *v, int i)
{
   atomic_subtract_int(v, i);
   return atomic_load_acq_32(v);
}
 
static inline int ATOMIC_INC_RETURN(ATOMIC_T *v)
{
   atomic_add_int(v, 1);
   return atomic_load_acq_32(v);
}
 
static inline int ATOMIC_DEC_RETURN(ATOMIC_T *v)
{
   atomic_subtract_int(v, 1);
   return atomic_load_acq_32(v);
}
 
static inline bool ATOMIC_INC_UNLESS(ATOMIC_T *v, int u)
{
   #error "TBD\n"
}
 
/*task*/
typedef void (*task_fn_t)(void *context, int pending);
#if 0 /*taskqueue -- asynchronous task execution*/
 
TASK_INIT(struct task *task, int priority, task_fn_t func,
    void *context);
 
TASK_INITIALIZER(int priority, task_fn_t func, void *context);
 
TASKQUEUE_DECLARE(name);
 
TASKQUEUE_DEFINE(name, taskqueue_enqueue_fn enqueue, void *context,
    init);
 
#endif
static inline void rtw_tasklet_init(_tasklet *t,task_fn_t func,
                           unsigned long data)
{
   TASK_INIT(t, 0, func, padapter);
}
static inline void rtw_tasklet_kill(_tasklet *t)
 
 
}
 
static inline void rtw_tasklet_schedule(_tasklet *t)
{
 
}
static inline void rtw_tasklet_hi_schedule(_tasklet *t)
{
 
}
 
 
/*thread*/
static inline void rtw_thread_enter(char *name)
{
   printf("%s", "RTKTHREAD_enter");
}
 
static inline void rtw_thread_exit(_completion *comp)
{
   printf("%s", "RTKTHREAD_exit");
}
 
#include <sys/unistd.h>        /* for RFHIGHPID */
static inline _thread_hdl_ rtw_thread_start(int (*threadfn)(void *data),
           void *data, const char namefmt[])
{
   _thread_hdl_ _rtw_thread = NULL;
   struct proc *p;
   struct thread *td;
 
   _rtw_thread = kproc_kthread_add(mp_xmit_packet_thread, data,
           &p, &td, RFHIGHPID, 0, namefmt, namefmt);
 
   if (_rtw_thread < 0)
       _rtw_thread = NULL;
   return _rtw_thread;
}
 
static inline bool rtw_thread_stop(_thread_hdl_ th)
{
   return _FALSE;
}
static inline void rtw_thread_wait_stop(void)
{
 
}
__inline static void flush_signals_thread(void)
{
 
}
 
#define rtw_dump_stack(void) do {} while (0)
#define rtw_bug_on(condition) do {} while (0)
#define rtw_warn_on(condition) do {} while (0)
#define rtw_sprintf(buf, size, format, arg...) do {} while (0)
 
#define rtw_netdev_priv(netdev) (((struct ifnet *)netdev)->if_softc)
#define rtw_free_netdev(netdev) if_free((netdev))
 
#define RTW_DIV_ROUND_UP(n, d)    (((n) + (d - 1)) / d)
 
#define NDEV_FMT "%s"
#define NDEV_ARG(ndev) ""
#define ADPT_FMT "%s"
#define ADPT_ARG(adapter) ""
#define FUNC_NDEV_FMT "%s"
#define FUNC_NDEV_ARG(ndev) __func__
#define FUNC_ADPT_FMT "%s"
#define FUNC_ADPT_ARG(adapter) __func__
 
#define STRUCT_PACKED
 
#endif