Staging: rtxxx0: PID checking fixes
[firefly-linux-kernel-4.4.55.git] / drivers / staging / rt2860 / rt_linux.h
1 /*
2  *************************************************************************
3  * Ralink Tech Inc.
4  * 5F., No.36, Taiyuan St., Jhubei City,
5  * Hsinchu County 302,
6  * Taiwan, R.O.C.
7  *
8  * (c) Copyright 2002-2007, Ralink Technology, Inc.
9  *
10  * This program is free software; you can redistribute it and/or modify  *
11  * it under the terms of the GNU General Public License as published by  *
12  * the Free Software Foundation; either version 2 of the License, or     *
13  * (at your option) any later version.                                   *
14  *                                                                       *
15  * This program is distributed in the hope that it will be useful,       *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  * GNU General Public License for more details.                          *
19  *                                                                       *
20  * You should have received a copy of the GNU General Public License     *
21  * along with this program; if not, write to the                         *
22  * Free Software Foundation, Inc.,                                       *
23  * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
24  *                                                                       *
25  *************************************************************************
26  */
27
28 /***********************************************************************/
29 /*                                                                     */
30 /*   Program:    rt_linux.c                                            */
31 /*   Created:    4/21/2006 1:17:38 PM                                  */
32 /*   Author:     Wu Xi-Kun                                             */
33 /*   Comments:   `description`                                         */
34 /*                                                                     */
35 /*---------------------------------------------------------------------*/
36 /*                                                                     */
37 /* History:                                                            */
38 /*    Revision 1.1 4/21/2006 1:17:38 PM  xsikun                        */
39 /*    Initial revision                                                 */
40 /*                                                                     */
41 /***********************************************************************/
42
43 #include "rtmp_type.h"
44 #include <linux/module.h>
45 #include <linux/kernel.h>
46
47 #include <linux/spinlock.h>
48 #include <linux/init.h>
49 #include <linux/string.h>
50 #include <linux/timer.h>
51 #include <linux/errno.h>
52 #include <linux/slab.h>
53 #include <linux/interrupt.h>
54 #include <linux/pci.h>
55 #include <linux/netdevice.h>
56 #include <linux/etherdevice.h>
57 #include <linux/skbuff.h>
58 #include <linux/ethtool.h>
59 #include <linux/wireless.h>
60 #include <linux/proc_fs.h>
61 #include <linux/delay.h>
62 #include <linux/if_arp.h>
63 #include <linux/ctype.h>
64 #include <linux/vmalloc.h>
65
66 #include <net/iw_handler.h>
67
68 // load firmware
69 #define __KERNEL_SYSCALLS__
70 #include <linux/unistd.h>
71 #include <asm/uaccess.h>
72
73
74 #define MEM_ALLOC_FLAG      (GFP_ATOMIC) //(GFP_DMA | GFP_ATOMIC)
75
76 #ifndef IFNAMSIZ
77 #define IFNAMSIZ 16
78 #endif
79
80 //#define CONFIG_CKIP_SUPPORT
81
82 #undef __inline
83 #define __inline           static inline
84
85 typedef int (*HARD_START_XMIT_FUNC)(struct sk_buff *skb, struct net_device *net_dev);
86
87 // add by kathy
88
89 /* order of "if defined()" is important, because for 3070 driver
90    both RT2870 and RT3070 are defined */
91 #if defined(RT2860)
92  #define STA_PROFILE_PATH                       "/etc/Wireless/RT2860STA/RT2860STA.dat"
93  #define STA_RTMP_FIRMWARE_FILE_NAME "/etc/Wireless/RT2860STA/RT2860STA.bin"
94  #define STA_NIC_DEVICE_NAME                    "RT2860STA"
95  #define STA_DRIVER_VERSION                     "1.8.1.1"
96 #elif defined(RT3070)
97  #define STA_PROFILE_PATH                       "/etc/Wireless/RT3070STA/RT3070STA.dat"
98  #define STA_RT2870_IMAGE_FILE_NAME  "/etc/Wireless/RT3070STA/rt2870.bin"
99  #define STA_NIC_DEVICE_NAME                    "RT3070STA"
100  #define STA_DRIVER_VERSION                     "2.0.1.0"
101 #elif defined(RT2870)
102  #define STA_PROFILE_PATH                       "/etc/Wireless/RT2870STA/RT2870STA.dat"
103  #define STA_RT2870_IMAGE_FILE_NAME  "/etc/Wireless/RT2870STA/rt2870.bin"
104  #define STA_NIC_DEVICE_NAME                    "RT2870STA"
105  #define STA_DRIVER_VERSION                     "1.4.0.0"
106 #endif
107
108 #ifdef RT2860
109 #ifndef PCI_DEVICE
110 #define PCI_DEVICE(vend,dev) \
111         .vendor = (vend), .device = (dev), \
112         .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID
113 #endif // PCI_DEVICE //
114 #endif
115
116 #define RTMP_TIME_AFTER(a,b)            \
117         (typecheck(unsigned long, (unsigned long)a) && \
118          typecheck(unsigned long, (unsigned long)b) && \
119          ((long)(b) - (long)(a) < 0))
120
121 #define RTMP_TIME_AFTER_EQ(a,b) \
122         (typecheck(unsigned long, (unsigned long)a) && \
123          typecheck(unsigned long, (unsigned long)b) && \
124          ((long)(a) - (long)(b) >= 0))
125 #define RTMP_TIME_BEFORE(a,b)   RTMP_TIME_AFTER_EQ(b,a)
126
127 #define RT_MOD_INC_USE_COUNT() \
128         if (!try_module_get(THIS_MODULE)) \
129         { \
130                 DBGPRINT(RT_DEBUG_ERROR, ("%s: cannot reserve module\n", __func__)); \
131                 return -1; \
132         }
133
134 #define RT_MOD_DEC_USE_COUNT() module_put(THIS_MODULE);
135
136 #define OS_HZ                   HZ
137
138 #define ETH_LENGTH_OF_ADDRESS   6
139
140 #define IN
141 #define OUT
142
143 #define NDIS_STATUS                             INT
144 #define NDIS_STATUS_SUCCESS                     0x00
145 #define NDIS_STATUS_FAILURE                     0x01
146 #define NDIS_STATUS_INVALID_DATA                                0x02
147 #define NDIS_STATUS_RESOURCES                   0x03
148
149 #define MIN_NET_DEVICE_FOR_AID                  0x00            //0x00~0x3f
150 #define MIN_NET_DEVICE_FOR_MBSSID               0x00            //0x00,0x10,0x20,0x30
151 #define MIN_NET_DEVICE_FOR_WDS                  0x10            //0x40,0x50,0x60,0x70
152 #define MIN_NET_DEVICE_FOR_APCLI                0x20
153 #define MIN_NET_DEVICE_FOR_MESH                 0x30
154 #define MIN_NET_DEVICE_FOR_DLS                  0x40
155
156 #define NDIS_PACKET_TYPE_DIRECTED               0
157 #define NDIS_PACKET_TYPE_MULTICAST              1
158 #define NDIS_PACKET_TYPE_BROADCAST              2
159 #define NDIS_PACKET_TYPE_ALL_MULTICAST  3
160
161 #ifndef RT30xx
162 typedef struct pid *    THREAD_PID;
163 #define THREAD_PID_INIT_VALUE   NULL
164 #define GET_PID(_v)     find_get_pid(_v)
165 #define GET_PID_NUMBER(_v)      pid_nr(_v)
166 #define CHECK_PID_LEGALITY(_pid)        if (pid_nr(_pid) > 0)
167 #define KILL_THREAD_PID(_A, _B, _C)     kill_pid(_A, _B, _C)
168 #endif
169
170 struct os_lock  {
171         spinlock_t              lock;
172         unsigned long   flags;
173 };
174
175
176 struct os_cookie {
177 #ifdef RT2860
178         struct pci_dev                  *pci_dev;
179         struct pci_dev                  *parent_pci_dev;
180         dma_addr_t                              pAd_pa;
181 #endif
182 #ifdef RT2870
183         struct usb_device               *pUsb_Dev;
184
185 #ifndef RT30xx
186         THREAD_PID                              MLMEThr_pid;
187         THREAD_PID                              RTUSBCmdThr_pid;
188         THREAD_PID                              TimerQThr_pid;
189 #endif
190 #ifdef RT30xx
191         struct pid      *MLMEThr_pid;
192         struct pid      *RTUSBCmdThr_pid;
193         struct pid      *TimerQThr_pid;
194 #endif
195 #endif // RT2870 //
196
197         struct tasklet_struct   rx_done_task;
198         struct tasklet_struct   mgmt_dma_done_task;
199         struct tasklet_struct   ac0_dma_done_task;
200         struct tasklet_struct   ac1_dma_done_task;
201         struct tasklet_struct   ac2_dma_done_task;
202         struct tasklet_struct   ac3_dma_done_task;
203         struct tasklet_struct   hcca_dma_done_task;
204         struct tasklet_struct   tbtt_task;
205 #ifdef RT2860
206         struct tasklet_struct   fifo_statistic_full_task;
207 #endif
208 #ifdef RT2870
209         struct tasklet_struct   null_frame_complete_task;
210         struct tasklet_struct   rts_frame_complete_task;
211         struct tasklet_struct   pspoll_frame_complete_task;
212 #endif // RT2870 //
213
214         unsigned long                   apd_pid; //802.1x daemon pid
215         INT                                             ioctl_if_type;
216         INT                                     ioctl_if;
217 };
218
219 typedef struct _VIRTUAL_ADAPTER
220 {
221         struct net_device               *RtmpDev;
222         struct net_device               *VirtualDev;
223 } VIRTUAL_ADAPTER, PVIRTUAL_ADAPTER;
224
225 #undef  ASSERT
226 #define ASSERT(x)
227
228 typedef struct os_cookie        * POS_COOKIE;
229 typedef struct pci_dev          * PPCI_DEV;
230 typedef struct net_device       * PNET_DEV;
231 typedef void                            * PNDIS_PACKET;
232 typedef char                            NDIS_PACKET;
233 typedef PNDIS_PACKET            * PPNDIS_PACKET;
234 typedef dma_addr_t                      NDIS_PHYSICAL_ADDRESS;
235 typedef dma_addr_t                      * PNDIS_PHYSICAL_ADDRESS;
236 typedef spinlock_t                      NDIS_SPIN_LOCK;
237 typedef struct timer_list       NDIS_MINIPORT_TIMER;
238 typedef void                            * NDIS_HANDLE;
239 typedef char                            * PNDIS_BUFFER;
240
241
242
243 void hex_dump(char *str, unsigned char *pSrcBufVA, unsigned int SrcBufLen);
244
245 dma_addr_t linux_pci_map_single(void *handle, void *ptr, size_t size, int sd_idx, int direction);
246 void linux_pci_unmap_single(void *handle, dma_addr_t dma_addr, size_t size, int direction);
247
248
249 ////////////////////////////////////////
250 // MOVE TO rtmp.h ?
251 /////////////////////////////////////////
252 #define PKTSRC_NDIS             0x7f
253 #define PKTSRC_DRIVER           0x0f
254 #define PRINT_MAC(addr) \
255         addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]
256
257
258 #define RT2860_PCI_DEVICE_ID            0x0601
259
260 #ifdef RT2860
261 #define PCI_MAP_SINGLE(_handle, _ptr, _size, _sd_idx, _dir) \
262         linux_pci_map_single(_handle, _ptr, _size, _sd_idx, _dir)
263
264 #define PCI_UNMAP_SINGLE(_handle, _ptr, _size, _dir) \
265         linux_pci_unmap_single(_handle, _ptr, _size, _dir)
266
267 #define PCI_ALLOC_CONSISTENT(_pci_dev, _size, _ptr) \
268         pci_alloc_consistent(_pci_dev, _size, _ptr)
269
270 #define PCI_FREE_CONSISTENT(_pci_dev, _size, _virtual_addr, _physical_addr) \
271         pci_free_consistent(_pci_dev, _size, _virtual_addr, _physical_addr)
272
273 #define DEV_ALLOC_SKB(_length) \
274         dev_alloc_skb(_length)
275 #endif
276 #ifdef RT2870
277 #define PCI_MAP_SINGLE(_handle, _ptr, _size, _dir) (ULONG)0
278
279 #define PCI_UNMAP_SINGLE(_handle, _ptr, _size, _dir)
280 #endif // RT2870 //
281
282
283 #define BEACON_FRAME_DMA_CACHE_WBACK(_ptr, _size)       \
284         dma_cache_wback(_ptr, _size)
285
286
287 //////////////////////////////////////////
288 //
289 //////////////////////////////////////////
290
291
292 #define NdisMIndicateStatus(_w, _x, _y, _z)
293
294 typedef struct timer_list       RTMP_OS_TIMER;
295
296 #ifdef RT2870
297 /* ----------------- Timer Related MARCO ---------------*/
298 // In RT2870, we have a lot of timer functions and will read/write register, it's
299 //      not allowed in Linux USB sub-system to do it ( because of sleep issue when submit
300 //  to ctrl pipe). So we need a wrapper function to take care it.
301
302 typedef VOID (*RT2870_TIMER_HANDLE)(
303         IN  PVOID   SystemSpecific1,
304         IN  PVOID   FunctionContext,
305         IN  PVOID   SystemSpecific2,
306         IN  PVOID   SystemSpecific3);
307 #endif // RT2870 //
308
309
310 typedef struct  _RALINK_TIMER_STRUCT    {
311     RTMP_OS_TIMER               TimerObj;       // Ndis Timer object
312         BOOLEAN                         Valid;                  // Set to True when call RTMPInitTimer
313     BOOLEAN             State;          // True if timer cancelled
314     BOOLEAN                     PeriodicType;   // True if timer is periodic timer
315     BOOLEAN             Repeat;         // True if periodic timer
316     ULONG               TimerValue;     // Timer value in milliseconds
317         ULONG                           cookie;                 // os specific object
318 #ifdef RT2870
319         RT2870_TIMER_HANDLE     handle;
320         void                            *pAd;
321 #endif // RT2870 //
322 }   RALINK_TIMER_STRUCT, *PRALINK_TIMER_STRUCT;
323
324
325 #ifdef RT2870
326
327 typedef enum _RT2870_KERNEL_THREAD_STATUS_
328 {
329         RT2870_THREAD_UNKNOWN = 0,
330         RT2870_THREAD_INITED = 1,
331         RT2870_THREAD_RUNNING = 2,
332         RT2870_THREAD_STOPED = 4,
333 }RT2870_KERNEL_THREAD_STATUS;
334
335 #define RT2870_THREAD_CAN_DO_INSERT             (RT2870_THREAD_INITED |RT2870_THREAD_RUNNING)
336
337 typedef struct _RT2870_TIMER_ENTRY_
338 {
339         RALINK_TIMER_STRUCT                     *pRaTimer;
340         struct _RT2870_TIMER_ENTRY_     *pNext;
341 }RT2870_TIMER_ENTRY;
342
343
344 #define TIMER_QUEUE_SIZE_MAX    128
345 typedef struct _RT2870_TIMER_QUEUE_
346 {
347         unsigned int            status;
348         UCHAR                           *pTimerQPoll;
349         RT2870_TIMER_ENTRY      *pQPollFreeList;
350         RT2870_TIMER_ENTRY      *pQHead;
351         RT2870_TIMER_ENTRY      *pQTail;
352 }RT2870_TIMER_QUEUE;
353 #endif // RT2870 //
354
355
356 //#define DBG   1
357
358 //
359 //  MACRO for debugging information
360 //
361
362 #ifdef DBG
363 extern ULONG    RTDebugLevel;
364
365 #define DBGPRINT_RAW(Level, Fmt)    \
366 {                                   \
367     if (Level <= RTDebugLevel)      \
368     {                               \
369         printk Fmt;               \
370     }                               \
371 }
372
373 #define DBGPRINT(Level, Fmt)    DBGPRINT_RAW(Level, Fmt)
374
375
376 #define DBGPRINT_ERR(Fmt)           \
377 {                                   \
378     printk("ERROR!!! ");          \
379     printk Fmt;                  \
380 }
381
382 #define DBGPRINT_S(Status, Fmt)         \
383 {                                                                       \
384         printk Fmt;                                     \
385 }
386
387
388 #else
389 #define DBGPRINT(Level, Fmt)
390 #define DBGPRINT_RAW(Level, Fmt)
391 #define DBGPRINT_S(Status, Fmt)
392 #define DBGPRINT_ERR(Fmt)
393 #endif
394
395
396 //
397 //  spin_lock enhanced for Nested spin lock
398 //
399 #define NdisAllocateSpinLock(__lock)      \
400 {                                       \
401     spin_lock_init((spinlock_t *)(__lock));               \
402 }
403
404 #define NdisFreeSpinLock(lock)          \
405 {                                       \
406 }
407
408
409 #define RTMP_SEM_LOCK(__lock)                                   \
410 {                                                                                               \
411         spin_lock_bh((spinlock_t *)(__lock));                           \
412 }
413
414 #define RTMP_SEM_UNLOCK(__lock)                                 \
415 {                                                                                               \
416         spin_unlock_bh((spinlock_t *)(__lock));                         \
417 }
418
419 // sample, use semaphore lock to replace IRQ lock, 2007/11/15
420 #define RTMP_IRQ_LOCK(__lock, __irqflags)                       \
421 {                                                                                                       \
422         __irqflags = 0;                                                                 \
423         spin_lock_bh((spinlock_t *)(__lock));                   \
424         pAd->irq_disabled |= 1; \
425 }
426
427 #define RTMP_IRQ_UNLOCK(__lock, __irqflag)                      \
428 {                                                                                                       \
429         pAd->irq_disabled &= 0; \
430         spin_unlock_bh((spinlock_t *)(__lock));                 \
431 }
432
433 #define RTMP_INT_LOCK(__lock, __irqflags)                       \
434 {                                                                                                       \
435         spin_lock_irqsave((spinlock_t *)__lock, __irqflags);    \
436 }
437
438 #define RTMP_INT_UNLOCK(__lock, __irqflag)                      \
439 {                                                                                                       \
440         spin_unlock_irqrestore((spinlock_t *)(__lock), ((unsigned long)__irqflag));     \
441 }
442
443 #ifdef RT2860
444 //Patch for ASIC turst read/write bug, needs to remove after metel fix
445 #define RTMP_IO_READ32(_A, _R, _pV)                                                             \
446 {                                                                                                                               \
447     if ((_A)->bPCIclkOff == FALSE)                                  \
448     {                                                               \
449                 (*_pV = readl((void *)((_A)->CSRBaseAddress + MAC_CSR0)));              \
450                 (*_pV = readl((void *)((_A)->CSRBaseAddress + (_R))));                  \
451     }                                                               \
452     else                                                                                                                        \
453                 *_pV = 0;                                                                                                       \
454 }
455 #define RTMP_IO_FORCE_READ32(_A, _R, _pV)                                                       \
456 {                                                                                                                                       \
457         (*_pV = readl((void *)((_A)->CSRBaseAddress + MAC_CSR0)));              \
458         (*_pV = readl((void *)((_A)->CSRBaseAddress + (_R))));                  \
459 }
460 #define RTMP_IO_READ8(_A, _R, _pV)                                                              \
461 {                                                                                                                               \
462         (*_pV = readl((void *)((_A)->CSRBaseAddress + MAC_CSR0)));                      \
463         (*_pV = readb((void *)((_A)->CSRBaseAddress + (_R))));                          \
464 }
465 #define RTMP_IO_WRITE32(_A, _R, _V)                                                                                             \
466 {                                                                                                                                                               \
467     if ((_A)->bPCIclkOff == FALSE)                                  \
468     {                                                               \
469         UINT    Val;                                                                                                                            \
470         Val = readl((void *)((_A)->CSRBaseAddress + MAC_CSR0));                 \
471         writel(_V, (void *)((_A)->CSRBaseAddress + (_R)));                                                              \
472     }                                                               \
473 }
474 #define RTMP_IO_WRITE8(_A, _R, _V)                                                                                              \
475 {                                                                                                                                                               \
476         UINT    Val;                                                                                                                            \
477         Val = readl((void *)((_A)->CSRBaseAddress + MAC_CSR0));                 \
478         writeb((_V), (PUCHAR)((_A)->CSRBaseAddress + (_R)));            \
479 }
480 #define RTMP_IO_WRITE16(_A, _R, _V)                                                                                             \
481 {                                                                                                                                                               \
482         UINT    Val;                                                                                                                            \
483         Val = readl((void *)((_A)->CSRBaseAddress + MAC_CSR0));                 \
484         writew((_V), (PUSHORT)((_A)->CSRBaseAddress + (_R)));   \
485 }
486 #endif /* RT2860 */
487 #ifdef RT2870
488 //Patch for ASIC turst read/write bug, needs to remove after metel fix
489 #define RTMP_IO_READ32(_A, _R, _pV)                                                             \
490         RTUSBReadMACRegister(_A, _R, _pV)
491
492 #define RTMP_IO_READ8(_A, _R, _pV)                                                              \
493 {                                                                                                                               \
494 }
495
496 #define RTMP_IO_WRITE32(_A, _R, _V)                                                             \
497         RTUSBWriteMACRegister(_A, _R, _V)
498
499
500 #define RTMP_IO_WRITE8(_A, _R, _V)                                                              \
501 {                                                                                                                               \
502         USHORT  _Val = _V;                                                                                      \
503         RTUSBSingleWrite(_A, _R, _Val);                                                         \
504 }
505
506
507 #define RTMP_IO_WRITE16(_A, _R, _V)                                                             \
508 {                                                                                                                               \
509         RTUSBSingleWrite(_A, _R, _V);                                                           \
510 }
511 #endif // RT2870 //
512
513 #ifndef wait_event_interruptible_timeout
514 #define __wait_event_interruptible_timeout(wq, condition, ret) \
515 do { \
516         wait_queue_t __wait; \
517         init_waitqueue_entry(&__wait, current); \
518         add_wait_queue(&wq, &__wait); \
519         for (;;) { \
520                 set_current_state(TASK_INTERRUPTIBLE); \
521                 if (condition) \
522                         break; \
523                 if (!signal_pending(current)) { \
524                         ret = schedule_timeout(ret); \
525                         if (!ret) \
526                                 break; \
527                         continue; \
528                 } \
529                 ret = -ERESTARTSYS; \
530                 break; \
531         } \
532         current->state = TASK_RUNNING; \
533         remove_wait_queue(&wq, &__wait); \
534 } while (0)
535
536 #define wait_event_interruptible_timeout(wq, condition, timeout) \
537 ({ \
538         long __ret = timeout; \
539         if (!(condition)) \
540                 __wait_event_interruptible_timeout(wq, condition, __ret); \
541         __ret; \
542 })
543 #endif
544 #define ONE_TICK 1
545 #define OS_WAIT(_time) \
546 {       int _i; \
547         long _loop = ((_time)/(1000/OS_HZ)) > 0 ? ((_time)/(1000/OS_HZ)) : 1;\
548         wait_queue_head_t _wait; \
549         init_waitqueue_head(&_wait); \
550         for (_i=0; _i<(_loop); _i++) \
551                 wait_event_interruptible_timeout(_wait, 0, ONE_TICK); }
552
553
554 typedef void (*TIMER_FUNCTION)(unsigned long);
555
556 #define COPY_MAC_ADDR(Addr1, Addr2)             memcpy((Addr1), (Addr2), MAC_ADDR_LEN)
557
558 #define MlmeAllocateMemory(_pAd, _ppVA) os_alloc_mem(_pAd, _ppVA, MGMT_DMA_BUFFER_SIZE)
559 #define MlmeFreeMemory(_pAd, _pVA)     os_free_mem(_pAd, _pVA)
560
561 #ifdef RT2860
562 #define BUILD_TIMER_FUNCTION(_func)                                                                                             \
563 void linux_##_func(unsigned long data)                                                                                  \
564 {                                                                                                                                                               \
565         PRALINK_TIMER_STRUCT    pTimer = (PRALINK_TIMER_STRUCT) data;                           \
566                                                                                                                                                                 \
567         _func(NULL, (PVOID) pTimer->cookie, NULL, pTimer);                                                      \
568         if (pTimer->Repeat)                                                                                                                     \
569                 RTMP_OS_Add_Timer(&pTimer->TimerObj, pTimer->TimerValue);                               \
570 }
571 #endif
572 #ifdef RT2870
573 #define BUILD_TIMER_FUNCTION(_func)                                                                                                     \
574 void linux_##_func(unsigned long data)                                                                                          \
575 {                                                                                                                                                                       \
576         PRALINK_TIMER_STRUCT    _pTimer = (PRALINK_TIMER_STRUCT)data;                                   \
577         RT2870_TIMER_ENTRY              *_pQNode;                                                                                               \
578         RTMP_ADAPTER                    *_pAd;                                                                                                  \
579                                                                                                                                                                 \
580         _pTimer->handle = _func;                                                                                                                        \
581         _pAd = (RTMP_ADAPTER *)_pTimer->pAd;                                                                                            \
582         _pQNode = RT2870_TimerQ_Insert(_pAd, _pTimer);                                                                  \
583         if ((_pQNode == NULL) && (_pAd->TimerQ.status & RT2870_THREAD_CAN_DO_INSERT))   \
584                 RTMP_OS_Add_Timer(&_pTimer->TimerObj, HZ);                                                      \
585 }
586 #endif // RT2870 //
587
588
589 #define DECLARE_TIMER_FUNCTION(_func)                   \
590 void linux_##_func(unsigned long data)
591
592 #define GET_TIMER_FUNCTION(_func)                               \
593                 linux_##_func
594
595 DECLARE_TIMER_FUNCTION(MlmePeriodicExec);
596 DECLARE_TIMER_FUNCTION(MlmeRssiReportExec);
597 DECLARE_TIMER_FUNCTION(AsicRxAntEvalTimeout);
598 DECLARE_TIMER_FUNCTION(APSDPeriodicExec);
599 DECLARE_TIMER_FUNCTION(AsicRfTuningExec);
600 #ifdef RT2870
601 DECLARE_TIMER_FUNCTION(BeaconUpdateExec);
602 #endif // RT2870 //
603
604 DECLARE_TIMER_FUNCTION(BeaconTimeout);
605 DECLARE_TIMER_FUNCTION(ScanTimeout);
606 DECLARE_TIMER_FUNCTION(AuthTimeout);
607 DECLARE_TIMER_FUNCTION(AssocTimeout);
608 DECLARE_TIMER_FUNCTION(ReassocTimeout);
609 DECLARE_TIMER_FUNCTION(DisassocTimeout);
610 DECLARE_TIMER_FUNCTION(LinkDownExec);
611 DECLARE_TIMER_FUNCTION(StaQuickResponeForRateUpExec);
612 DECLARE_TIMER_FUNCTION(WpaDisassocApAndBlockAssoc);
613 DECLARE_TIMER_FUNCTION(PsPollWakeExec);
614 DECLARE_TIMER_FUNCTION(RadioOnExec);
615
616 void RTMP_GetCurrentSystemTime(LARGE_INTEGER *time);
617
618
619 /*
620  * packet helper
621  *      - convert internal rt packet to os packet or
622  *             os packet to rt packet
623  */
624 #define RTPKT_TO_OSPKT(_p)              ((struct sk_buff *)(_p))
625 #define OSPKT_TO_RTPKT(_p)              ((PNDIS_PACKET)(_p))
626
627 #define GET_OS_PKT_DATAPTR(_pkt) \
628                 (RTPKT_TO_OSPKT(_pkt)->data)
629
630 #define GET_OS_PKT_LEN(_pkt) \
631                 (RTPKT_TO_OSPKT(_pkt)->len)
632
633 #define GET_OS_PKT_DATATAIL(_pkt) \
634                 (RTPKT_TO_OSPKT(_pkt)->tail)
635
636 #define GET_OS_PKT_HEAD(_pkt) \
637                 (RTPKT_TO_OSPKT(_pkt)->head)
638
639 #define GET_OS_PKT_END(_pkt) \
640                 (RTPKT_TO_OSPKT(_pkt)->end)
641
642 #define GET_OS_PKT_NETDEV(_pkt) \
643                 (RTPKT_TO_OSPKT(_pkt)->dev)
644
645 #define GET_OS_PKT_TYPE(_pkt) \
646                 (RTPKT_TO_OSPKT(_pkt))
647
648 #define GET_OS_PKT_NEXT(_pkt) \
649                 (RTPKT_TO_OSPKT(_pkt)->next)
650
651
652 #define OS_NTOHS(_Val) \
653                 (ntohs(_Val))
654 #define OS_HTONS(_Val) \
655                 (htons(_Val))
656 #define OS_NTOHL(_Val) \
657                 (ntohl(_Val))
658 #define OS_HTONL(_Val) \
659                 (htonl(_Val))
660
661 /* statistics counter */
662 #define STATS_INC_RX_PACKETS(_pAd, _dev)
663 #define STATS_INC_TX_PACKETS(_pAd, _dev)
664
665 #define STATS_INC_RX_BYTESS(_pAd, _dev, len)
666 #define STATS_INC_TX_BYTESS(_pAd, _dev, len)
667
668 #define STATS_INC_RX_ERRORS(_pAd, _dev)
669 #define STATS_INC_TX_ERRORS(_pAd, _dev)
670
671 #define STATS_INC_RX_DROPPED(_pAd, _dev)
672 #define STATS_INC_TX_DROPPED(_pAd, _dev)
673
674
675 #define CB_OFF  10
676
677
678 //   check DDK NDIS_PACKET data structure and find out only MiniportReservedEx[0..7] can be used by our driver without
679 //   ambiguity. Fields after pPacket->MiniportReservedEx[8] may be used by other wrapper layer thus crashes the driver
680 //
681
682 // User Priority
683 #define RTMP_SET_PACKET_UP(_p, _prio)                   (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+0] = _prio)
684 #define RTMP_GET_PACKET_UP(_p)                                  (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+0])
685
686 // Fragment #
687 #define RTMP_SET_PACKET_FRAGMENTS(_p, _num)             (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+1] = _num)
688 #define RTMP_GET_PACKET_FRAGMENTS(_p)                   (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+1])
689
690 // 0x0 ~0x7f: TX to AP's own BSS which has the specified AID. if AID>127, set bit 7 in RTMP_SET_PACKET_EMACTAB too.
691 //(this value also as MAC(on-chip WCID) table index)
692 // 0x80~0xff: TX to a WDS link. b0~6: WDS index
693 #define RTMP_SET_PACKET_WCID(_p, _wdsidx)               (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+2] = _wdsidx)
694 #define RTMP_GET_PACKET_WCID(_p)                        ((UCHAR)(RTPKT_TO_OSPKT(_p)->cb[CB_OFF+2]))
695
696 // 0xff: PKTSRC_NDIS, others: local TX buffer index. This value affects how to a packet
697 #define RTMP_SET_PACKET_SOURCE(_p, _pktsrc)             (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+3] = _pktsrc)
698 #define RTMP_GET_PACKET_SOURCE(_p)                      (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+3])
699
700 // RTS/CTS-to-self protection method
701 #define RTMP_SET_PACKET_RTS(_p, _num)                   (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+4] = _num)
702 #define RTMP_GET_PACKET_RTS(_p)                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+4])
703 // see RTMP_S(G)ET_PACKET_EMACTAB
704
705 // TX rate index
706 #define RTMP_SET_PACKET_TXRATE(_p, _rate)               (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+5] = _rate)
707 #define RTMP_GET_PACKET_TXRATE(_p)                              (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+5])
708
709 // From which Interface
710 #define RTMP_SET_PACKET_IF(_p, _ifdx)           (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+6] = _ifdx)
711 #define RTMP_GET_PACKET_IF(_p)                          (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+6])
712 #define RTMP_SET_PACKET_NET_DEVICE_MBSSID(_p, _bss)             RTMP_SET_PACKET_IF((_p), (_bss))
713 #define RTMP_SET_PACKET_NET_DEVICE_WDS(_p, _bss)                RTMP_SET_PACKET_IF((_p), ((_bss) + MIN_NET_DEVICE_FOR_WDS))
714 #define RTMP_SET_PACKET_NET_DEVICE_APCLI(_p, _idx)      RTMP_SET_PACKET_IF((_p), ((_idx) + MIN_NET_DEVICE_FOR_APCLI))
715 #define RTMP_SET_PACKET_NET_DEVICE_MESH(_p, _idx)       RTMP_SET_PACKET_IF((_p), ((_idx) + MIN_NET_DEVICE_FOR_MESH))
716 #define RTMP_GET_PACKET_NET_DEVICE_MBSSID(_p)                   RTMP_GET_PACKET_IF((_p))
717 #define RTMP_GET_PACKET_NET_DEVICE(_p)                                  RTMP_GET_PACKET_IF((_p))
718
719 #define RTMP_SET_PACKET_MOREDATA(_p, _morebit)          (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+7] = _morebit)
720 #define RTMP_GET_PACKET_MOREDATA(_p)                            (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+7])
721
722
723 //
724 //      Sepcific Pakcet Type definition
725 //
726 #define RTMP_PACKET_SPECIFIC_CB_OFFSET  11
727
728 #define RTMP_PACKET_SPECIFIC_DHCP               0x01
729 #define RTMP_PACKET_SPECIFIC_EAPOL              0x02
730 #define RTMP_PACKET_SPECIFIC_IPV4               0x04
731 #define RTMP_PACKET_SPECIFIC_WAI                0x08
732 #define RTMP_PACKET_SPECIFIC_VLAN               0x10
733 #define RTMP_PACKET_SPECIFIC_LLCSNAP    0x20
734
735 //Specific
736 #define RTMP_SET_PACKET_SPECIFIC(_p, _flg)              (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11] = _flg)
737
738 //DHCP
739 #define RTMP_SET_PACKET_DHCP(_p, _flg)                                                                                                          \
740                         do{                                                                                                                                                             \
741                                 if (_flg)                                                                                                                                       \
742                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) |= (RTMP_PACKET_SPECIFIC_DHCP);             \
743                                 else                                                                                                                                            \
744                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) &= (!RTMP_PACKET_SPECIFIC_DHCP);    \
745                         }while(0)
746 #define RTMP_GET_PACKET_DHCP(_p)                (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11] & RTMP_PACKET_SPECIFIC_DHCP)
747
748 //EAPOL
749 #define RTMP_SET_PACKET_EAPOL(_p, _flg)                                                                                                         \
750                         do{                                                                                                                                                             \
751                                 if (_flg)                                                                                                                                       \
752                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) |= (RTMP_PACKET_SPECIFIC_EAPOL);            \
753                                 else                                                                                                                                            \
754                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) &= (!RTMP_PACKET_SPECIFIC_EAPOL);   \
755                         }while(0)
756 #define RTMP_GET_PACKET_EAPOL(_p)               (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11] & RTMP_PACKET_SPECIFIC_EAPOL)
757
758 //WAI
759 #define RTMP_SET_PACKET_WAI(_p, _flg)                                                                                                           \
760                         do{                                                                                                                                                             \
761                                 if (_flg)                                                                                                                                       \
762                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) |= (RTMP_PACKET_SPECIFIC_WAI);              \
763                                 else                                                                                                                                            \
764                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) &= (!RTMP_PACKET_SPECIFIC_WAI);     \
765                         }while(0)
766 #define RTMP_GET_PACKET_WAI(_p)         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11] & RTMP_PACKET_SPECIFIC_WAI)
767
768 #define RTMP_GET_PACKET_LOWRATE(_p)             (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11] & (RTMP_PACKET_SPECIFIC_EAPOL | RTMP_PACKET_SPECIFIC_DHCP | RTMP_PACKET_SPECIFIC_WAI))
769
770 //VLAN
771 #define RTMP_SET_PACKET_VLAN(_p, _flg)                                                                                                          \
772                         do{                                                                                                                                                             \
773                                 if (_flg)                                                                                                                                       \
774                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) |= (RTMP_PACKET_SPECIFIC_VLAN);             \
775                                 else                                                                                                                                            \
776                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) &= (!RTMP_PACKET_SPECIFIC_VLAN);    \
777                         }while(0)
778 #define RTMP_GET_PACKET_VLAN(_p)                (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11] & RTMP_PACKET_SPECIFIC_VLAN)
779
780 //LLC/SNAP
781 #define RTMP_SET_PACKET_LLCSNAP(_p, _flg)                                                                                                       \
782                         do{                                                                                                                                                             \
783                                 if (_flg)                                                                                                                                       \
784                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) |= (RTMP_PACKET_SPECIFIC_LLCSNAP);          \
785                                 else                                                                                                                                            \
786                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) &= (!RTMP_PACKET_SPECIFIC_LLCSNAP);         \
787                         }while(0)
788
789 #define RTMP_GET_PACKET_LLCSNAP(_p)             (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11] & RTMP_PACKET_SPECIFIC_LLCSNAP)
790
791 // IP
792 #define RTMP_SET_PACKET_IPV4(_p, _flg)                                                                                                          \
793                         do{                                                                                                                                                             \
794                                 if (_flg)                                                                                                                                       \
795                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) |= (RTMP_PACKET_SPECIFIC_IPV4);             \
796                                 else                                                                                                                                            \
797                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) &= (!RTMP_PACKET_SPECIFIC_IPV4);    \
798                         }while(0)
799
800 #define RTMP_GET_PACKET_IPV4(_p)                (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11] & RTMP_PACKET_SPECIFIC_IPV4)
801
802
803 // If this flag is set, it indicates that this EAPoL frame MUST be clear.
804 #define RTMP_SET_PACKET_CLEAR_EAP_FRAME(_p, _flg)   (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+12] = _flg)
805 #define RTMP_GET_PACKET_CLEAR_EAP_FRAME(_p)         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+12])
806
807 #define RTMP_SET_PACKET_5VT(_p, _flg)   (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+22] = _flg)
808 #define RTMP_GET_PACKET_5VT(_p)         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+22])
809
810 #ifdef CONFIG_5VT_ENHANCE
811 #define BRIDGE_TAG 0x35564252    // depends on 5VT define in br_input.c
812 #endif
813
814
815 #define NDIS_SET_PACKET_STATUS(_p, _status)
816
817
818 #define GET_SG_LIST_FROM_PACKET(_p, _sc)        \
819     rt_get_sg_list_from_packet(_p, _sc)
820
821 #define NdisMoveMemory(Destination, Source, Length) memmove(Destination, Source, Length)
822 #define NdisZeroMemory(Destination, Length)         memset(Destination, 0, Length)
823 #define NdisFillMemory(Destination, Length, Fill)   memset(Destination, Fill, Length)
824 #define NdisEqualMemory(Source1, Source2, Length)   (!memcmp(Source1, Source2, Length))
825 #define RTMPEqualMemory(Source1, Source2, Length)       (!memcmp(Source1, Source2, Length))
826
827
828 #define RTMP_INC_REF(_A)                0
829 #define RTMP_DEC_REF(_A)                0
830 #define RTMP_GET_REF(_A)                0
831
832
833
834 /*
835  * ULONG
836  * RTMP_GetPhysicalAddressLow(
837  *   IN NDIS_PHYSICAL_ADDRESS  PhysicalAddress);
838  */
839 #define RTMP_GetPhysicalAddressLow(PhysicalAddress)             (PhysicalAddress)
840
841 /*
842  * ULONG
843  * RTMP_GetPhysicalAddressHigh(
844  *   IN NDIS_PHYSICAL_ADDRESS  PhysicalAddress);
845  */
846 #define RTMP_GetPhysicalAddressHigh(PhysicalAddress)            (0)
847
848 /*
849  * VOID
850  * RTMP_SetPhysicalAddressLow(
851  *   IN NDIS_PHYSICAL_ADDRESS  PhysicalAddress,
852  *   IN ULONG  Value);
853  */
854 #define RTMP_SetPhysicalAddressLow(PhysicalAddress, Value)      \
855                         PhysicalAddress = Value;
856
857 /*
858  * VOID
859  * RTMP_SetPhysicalAddressHigh(
860  *   IN NDIS_PHYSICAL_ADDRESS  PhysicalAddress,
861  *   IN ULONG  Value);
862  */
863 #define RTMP_SetPhysicalAddressHigh(PhysicalAddress, Value)
864
865
866 //CONTAINING_RECORD(pEntry, NDIS_PACKET, MiniportReservedEx);
867 #define QUEUE_ENTRY_TO_PACKET(pEntry) \
868         (PNDIS_PACKET)(pEntry)
869
870 #define PACKET_TO_QUEUE_ENTRY(pPacket) \
871         (PQUEUE_ENTRY)(pPacket)
872
873
874 #ifndef CONTAINING_RECORD
875 #define CONTAINING_RECORD(address, type, field)                 \
876 ((type *)((PCHAR)(address) - offsetof(type, field)))
877 #endif
878
879
880 #define RELEASE_NDIS_PACKET(_pAd, _pPacket, _Status)                    \
881 {                                                                       \
882         RTMPFreeNdisPacket(_pAd, _pPacket);                             \
883 }
884
885
886 #define SWITCH_PhyAB(_pAA, _pBB)    \
887 {                                                                           \
888     ULONG       AABasePaHigh;                           \
889     ULONG       AABasePaLow;                           \
890     ULONG       BBBasePaHigh;                           \
891     ULONG       BBBasePaLow;                           \
892     BBBasePaHigh = RTMP_GetPhysicalAddressHigh(_pBB);                                                 \
893     BBBasePaLow = RTMP_GetPhysicalAddressLow(_pBB);                                                 \
894     AABasePaHigh = RTMP_GetPhysicalAddressHigh(_pAA);                                                 \
895     AABasePaLow = RTMP_GetPhysicalAddressLow(_pAA);                                                 \
896     RTMP_SetPhysicalAddressHigh(_pAA, BBBasePaHigh);                                                 \
897     RTMP_SetPhysicalAddressLow(_pAA, BBBasePaLow);                                                 \
898     RTMP_SetPhysicalAddressHigh(_pBB, AABasePaHigh);                                                 \
899     RTMP_SetPhysicalAddressLow(_pBB, AABasePaLow);                                                 \
900 }
901
902
903 #define NdisWriteErrorLogEntry(_a, _b, _c, _d)
904 #define NdisMAllocateMapRegisters(_a, _b, _c, _d, _e)           NDIS_STATUS_SUCCESS
905
906
907 #define NdisAcquireSpinLock             RTMP_SEM_LOCK
908 #define NdisReleaseSpinLock             RTMP_SEM_UNLOCK
909
910 static inline void NdisGetSystemUpTime(ULONG *time)
911 {
912         *time = jiffies;
913 }
914
915 //pPacket = CONTAINING_RECORD(pEntry, NDIS_PACKET, MiniportReservedEx);
916 #define QUEUE_ENTRY_TO_PKT(pEntry) \
917                 ((PNDIS_PACKET) (pEntry))
918
919 int rt28xx_packet_xmit(struct sk_buff *skb);
920
921
922
923 void rtmp_os_thread_init(PUCHAR pThreadName, PVOID pNotify);
924
925 #ifdef RT2860
926 #if !defined(PCI_CAP_ID_EXP)
927 #define PCI_CAP_ID_EXP                      0x10
928 #endif
929
930 #if !defined(PCI_EXP_LNKCTL)
931 #define PCI_EXP_LNKCTL                      0x10
932 #endif
933
934 #if !defined(PCI_CLASS_BRIDGE_PCI)
935 #define PCI_CLASS_BRIDGE_PCI            0x0604
936 #endif
937
938 #define PCIBUS_INTEL_VENDOR         0x8086
939 #endif
940