9ac48b7e419007d00bc58cad2ebb01c5f5f9e8bd
[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 struct os_lock  {
162         spinlock_t              lock;
163         unsigned long   flags;
164 };
165
166
167 struct os_cookie {
168 #ifdef RT2860
169         struct pci_dev                  *pci_dev;
170         struct pci_dev                  *parent_pci_dev;
171         dma_addr_t                              pAd_pa;
172 #endif
173 #ifdef RT2870
174         struct usb_device               *pUsb_Dev;
175
176         struct pid      *MLMEThr_pid;
177         struct pid      *RTUSBCmdThr_pid;
178         struct pid      *TimerQThr_pid;
179 #endif // RT2870 //
180
181         struct tasklet_struct   rx_done_task;
182         struct tasklet_struct   mgmt_dma_done_task;
183         struct tasklet_struct   ac0_dma_done_task;
184         struct tasklet_struct   ac1_dma_done_task;
185         struct tasklet_struct   ac2_dma_done_task;
186         struct tasklet_struct   ac3_dma_done_task;
187         struct tasklet_struct   hcca_dma_done_task;
188         struct tasklet_struct   tbtt_task;
189 #ifdef RT2860
190         struct tasklet_struct   fifo_statistic_full_task;
191 #endif
192 #ifdef RT2870
193         struct tasklet_struct   null_frame_complete_task;
194         struct tasklet_struct   rts_frame_complete_task;
195         struct tasklet_struct   pspoll_frame_complete_task;
196 #endif // RT2870 //
197
198         unsigned long                   apd_pid; //802.1x daemon pid
199         INT                                             ioctl_if_type;
200         INT                                     ioctl_if;
201 };
202
203 typedef struct _VIRTUAL_ADAPTER
204 {
205         struct net_device               *RtmpDev;
206         struct net_device               *VirtualDev;
207 } VIRTUAL_ADAPTER, PVIRTUAL_ADAPTER;
208
209 #undef  ASSERT
210 #define ASSERT(x)
211
212 typedef struct os_cookie        * POS_COOKIE;
213 typedef struct pci_dev          * PPCI_DEV;
214 typedef struct net_device       * PNET_DEV;
215 typedef void                            * PNDIS_PACKET;
216 typedef char                            NDIS_PACKET;
217 typedef PNDIS_PACKET            * PPNDIS_PACKET;
218 typedef dma_addr_t                      NDIS_PHYSICAL_ADDRESS;
219 typedef dma_addr_t                      * PNDIS_PHYSICAL_ADDRESS;
220 typedef spinlock_t                      NDIS_SPIN_LOCK;
221 typedef struct timer_list       NDIS_MINIPORT_TIMER;
222 typedef void                            * NDIS_HANDLE;
223 typedef char                            * PNDIS_BUFFER;
224
225
226
227 void hex_dump(char *str, unsigned char *pSrcBufVA, unsigned int SrcBufLen);
228
229 dma_addr_t linux_pci_map_single(void *handle, void *ptr, size_t size, int sd_idx, int direction);
230 void linux_pci_unmap_single(void *handle, dma_addr_t dma_addr, size_t size, int direction);
231
232
233 ////////////////////////////////////////
234 // MOVE TO rtmp.h ?
235 /////////////////////////////////////////
236 #define PKTSRC_NDIS             0x7f
237 #define PKTSRC_DRIVER           0x0f
238 #define PRINT_MAC(addr) \
239         addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]
240
241
242 #define RT2860_PCI_DEVICE_ID            0x0601
243
244 #ifdef RT2860
245 #define PCI_MAP_SINGLE(_handle, _ptr, _size, _sd_idx, _dir) \
246         linux_pci_map_single(_handle, _ptr, _size, _sd_idx, _dir)
247
248 #define PCI_UNMAP_SINGLE(_handle, _ptr, _size, _dir) \
249         linux_pci_unmap_single(_handle, _ptr, _size, _dir)
250
251 #define PCI_ALLOC_CONSISTENT(_pci_dev, _size, _ptr) \
252         pci_alloc_consistent(_pci_dev, _size, _ptr)
253
254 #define PCI_FREE_CONSISTENT(_pci_dev, _size, _virtual_addr, _physical_addr) \
255         pci_free_consistent(_pci_dev, _size, _virtual_addr, _physical_addr)
256
257 #define DEV_ALLOC_SKB(_length) \
258         dev_alloc_skb(_length)
259 #endif
260 #ifdef RT2870
261 #define PCI_MAP_SINGLE(_handle, _ptr, _size, _dir) (ULONG)0
262
263 #define PCI_UNMAP_SINGLE(_handle, _ptr, _size, _dir)
264 #endif // RT2870 //
265
266
267 #define BEACON_FRAME_DMA_CACHE_WBACK(_ptr, _size)       \
268         dma_cache_wback(_ptr, _size)
269
270
271 //////////////////////////////////////////
272 //
273 //////////////////////////////////////////
274
275
276 #define NdisMIndicateStatus(_w, _x, _y, _z)
277
278 typedef struct timer_list       RTMP_OS_TIMER;
279
280 #ifdef RT2870
281 /* ----------------- Timer Related MARCO ---------------*/
282 // In RT2870, we have a lot of timer functions and will read/write register, it's
283 //      not allowed in Linux USB sub-system to do it ( because of sleep issue when submit
284 //  to ctrl pipe). So we need a wrapper function to take care it.
285
286 typedef VOID (*RT2870_TIMER_HANDLE)(
287         IN  PVOID   SystemSpecific1,
288         IN  PVOID   FunctionContext,
289         IN  PVOID   SystemSpecific2,
290         IN  PVOID   SystemSpecific3);
291 #endif // RT2870 //
292
293
294 typedef struct  _RALINK_TIMER_STRUCT    {
295     RTMP_OS_TIMER               TimerObj;       // Ndis Timer object
296         BOOLEAN                         Valid;                  // Set to True when call RTMPInitTimer
297     BOOLEAN             State;          // True if timer cancelled
298     BOOLEAN                     PeriodicType;   // True if timer is periodic timer
299     BOOLEAN             Repeat;         // True if periodic timer
300     ULONG               TimerValue;     // Timer value in milliseconds
301         ULONG                           cookie;                 // os specific object
302 #ifdef RT2870
303         RT2870_TIMER_HANDLE     handle;
304         void                            *pAd;
305 #endif // RT2870 //
306 }   RALINK_TIMER_STRUCT, *PRALINK_TIMER_STRUCT;
307
308
309 #ifdef RT2870
310
311 typedef enum _RT2870_KERNEL_THREAD_STATUS_
312 {
313         RT2870_THREAD_UNKNOWN = 0,
314         RT2870_THREAD_INITED = 1,
315         RT2870_THREAD_RUNNING = 2,
316         RT2870_THREAD_STOPED = 4,
317 }RT2870_KERNEL_THREAD_STATUS;
318
319 #define RT2870_THREAD_CAN_DO_INSERT             (RT2870_THREAD_INITED |RT2870_THREAD_RUNNING)
320
321 typedef struct _RT2870_TIMER_ENTRY_
322 {
323         RALINK_TIMER_STRUCT                     *pRaTimer;
324         struct _RT2870_TIMER_ENTRY_     *pNext;
325 }RT2870_TIMER_ENTRY;
326
327
328 #define TIMER_QUEUE_SIZE_MAX    128
329 typedef struct _RT2870_TIMER_QUEUE_
330 {
331         unsigned int            status;
332         UCHAR                           *pTimerQPoll;
333         RT2870_TIMER_ENTRY      *pQPollFreeList;
334         RT2870_TIMER_ENTRY      *pQHead;
335         RT2870_TIMER_ENTRY      *pQTail;
336 }RT2870_TIMER_QUEUE;
337 #endif // RT2870 //
338
339
340 //#define DBG   1
341
342 //
343 //  MACRO for debugging information
344 //
345
346 #ifdef DBG
347 extern ULONG    RTDebugLevel;
348
349 #define DBGPRINT_RAW(Level, Fmt)    \
350 {                                   \
351     if (Level <= RTDebugLevel)      \
352     {                               \
353         printk Fmt;               \
354     }                               \
355 }
356
357 #define DBGPRINT(Level, Fmt)    DBGPRINT_RAW(Level, Fmt)
358
359
360 #define DBGPRINT_ERR(Fmt)           \
361 {                                   \
362     printk("ERROR!!! ");          \
363     printk Fmt;                  \
364 }
365
366 #define DBGPRINT_S(Status, Fmt)         \
367 {                                                                       \
368         printk Fmt;                                     \
369 }
370
371
372 #else
373 #define DBGPRINT(Level, Fmt)
374 #define DBGPRINT_RAW(Level, Fmt)
375 #define DBGPRINT_S(Status, Fmt)
376 #define DBGPRINT_ERR(Fmt)
377 #endif
378
379
380 //
381 //  spin_lock enhanced for Nested spin lock
382 //
383 #define NdisAllocateSpinLock(__lock)      \
384 {                                       \
385     spin_lock_init((spinlock_t *)(__lock));               \
386 }
387
388 #define NdisFreeSpinLock(lock)          \
389 {                                       \
390 }
391
392
393 #define RTMP_SEM_LOCK(__lock)                                   \
394 {                                                                                               \
395         spin_lock_bh((spinlock_t *)(__lock));                           \
396 }
397
398 #define RTMP_SEM_UNLOCK(__lock)                                 \
399 {                                                                                               \
400         spin_unlock_bh((spinlock_t *)(__lock));                         \
401 }
402
403 // sample, use semaphore lock to replace IRQ lock, 2007/11/15
404 #define RTMP_IRQ_LOCK(__lock, __irqflags)                       \
405 {                                                                                                       \
406         __irqflags = 0;                                                                 \
407         spin_lock_bh((spinlock_t *)(__lock));                   \
408         pAd->irq_disabled |= 1; \
409 }
410
411 #define RTMP_IRQ_UNLOCK(__lock, __irqflag)                      \
412 {                                                                                                       \
413         pAd->irq_disabled &= 0; \
414         spin_unlock_bh((spinlock_t *)(__lock));                 \
415 }
416
417 #define RTMP_INT_LOCK(__lock, __irqflags)                       \
418 {                                                                                                       \
419         spin_lock_irqsave((spinlock_t *)__lock, __irqflags);    \
420 }
421
422 #define RTMP_INT_UNLOCK(__lock, __irqflag)                      \
423 {                                                                                                       \
424         spin_unlock_irqrestore((spinlock_t *)(__lock), ((unsigned long)__irqflag));     \
425 }
426
427 #ifdef RT2860
428 //Patch for ASIC turst read/write bug, needs to remove after metel fix
429 #define RTMP_IO_READ32(_A, _R, _pV)                                                             \
430 {                                                                                                                               \
431     if ((_A)->bPCIclkOff == FALSE)                                  \
432     {                                                               \
433                 (*_pV = readl((void *)((_A)->CSRBaseAddress + MAC_CSR0)));              \
434                 (*_pV = readl((void *)((_A)->CSRBaseAddress + (_R))));                  \
435     }                                                               \
436     else                                                                                                                        \
437                 *_pV = 0;                                                                                                       \
438 }
439 #define RTMP_IO_FORCE_READ32(_A, _R, _pV)                                                       \
440 {                                                                                                                                       \
441         (*_pV = readl((void *)((_A)->CSRBaseAddress + MAC_CSR0)));              \
442         (*_pV = readl((void *)((_A)->CSRBaseAddress + (_R))));                  \
443 }
444 #define RTMP_IO_READ8(_A, _R, _pV)                                                              \
445 {                                                                                                                               \
446         (*_pV = readl((void *)((_A)->CSRBaseAddress + MAC_CSR0)));                      \
447         (*_pV = readb((void *)((_A)->CSRBaseAddress + (_R))));                          \
448 }
449 #define RTMP_IO_WRITE32(_A, _R, _V)                                                                                             \
450 {                                                                                                                                                               \
451     if ((_A)->bPCIclkOff == FALSE)                                  \
452     {                                                               \
453         UINT    Val;                                                                                                                            \
454         Val = readl((void *)((_A)->CSRBaseAddress + MAC_CSR0));                 \
455         writel(_V, (void *)((_A)->CSRBaseAddress + (_R)));                                                              \
456     }                                                               \
457 }
458 #define RTMP_IO_WRITE8(_A, _R, _V)                                                                                              \
459 {                                                                                                                                                               \
460         UINT    Val;                                                                                                                            \
461         Val = readl((void *)((_A)->CSRBaseAddress + MAC_CSR0));                 \
462         writeb((_V), (PUCHAR)((_A)->CSRBaseAddress + (_R)));            \
463 }
464 #define RTMP_IO_WRITE16(_A, _R, _V)                                                                                             \
465 {                                                                                                                                                               \
466         UINT    Val;                                                                                                                            \
467         Val = readl((void *)((_A)->CSRBaseAddress + MAC_CSR0));                 \
468         writew((_V), (PUSHORT)((_A)->CSRBaseAddress + (_R)));   \
469 }
470 #endif /* RT2860 */
471 #ifdef RT2870
472 //Patch for ASIC turst read/write bug, needs to remove after metel fix
473 #define RTMP_IO_READ32(_A, _R, _pV)                                                             \
474         RTUSBReadMACRegister(_A, _R, _pV)
475
476 #define RTMP_IO_READ8(_A, _R, _pV)                                                              \
477 {                                                                                                                               \
478 }
479
480 #define RTMP_IO_WRITE32(_A, _R, _V)                                                             \
481         RTUSBWriteMACRegister(_A, _R, _V)
482
483
484 #define RTMP_IO_WRITE8(_A, _R, _V)                                                              \
485 {                                                                                                                               \
486         USHORT  _Val = _V;                                                                                      \
487         RTUSBSingleWrite(_A, _R, _Val);                                                         \
488 }
489
490
491 #define RTMP_IO_WRITE16(_A, _R, _V)                                                             \
492 {                                                                                                                               \
493         RTUSBSingleWrite(_A, _R, _V);                                                           \
494 }
495 #endif // RT2870 //
496
497 #ifndef wait_event_interruptible_timeout
498 #define __wait_event_interruptible_timeout(wq, condition, ret) \
499 do { \
500         wait_queue_t __wait; \
501         init_waitqueue_entry(&__wait, current); \
502         add_wait_queue(&wq, &__wait); \
503         for (;;) { \
504                 set_current_state(TASK_INTERRUPTIBLE); \
505                 if (condition) \
506                         break; \
507                 if (!signal_pending(current)) { \
508                         ret = schedule_timeout(ret); \
509                         if (!ret) \
510                                 break; \
511                         continue; \
512                 } \
513                 ret = -ERESTARTSYS; \
514                 break; \
515         } \
516         current->state = TASK_RUNNING; \
517         remove_wait_queue(&wq, &__wait); \
518 } while (0)
519
520 #define wait_event_interruptible_timeout(wq, condition, timeout) \
521 ({ \
522         long __ret = timeout; \
523         if (!(condition)) \
524                 __wait_event_interruptible_timeout(wq, condition, __ret); \
525         __ret; \
526 })
527 #endif
528 #define ONE_TICK 1
529 #define OS_WAIT(_time) \
530 {       int _i; \
531         long _loop = ((_time)/(1000/OS_HZ)) > 0 ? ((_time)/(1000/OS_HZ)) : 1;\
532         wait_queue_head_t _wait; \
533         init_waitqueue_head(&_wait); \
534         for (_i=0; _i<(_loop); _i++) \
535                 wait_event_interruptible_timeout(_wait, 0, ONE_TICK); }
536
537
538 typedef void (*TIMER_FUNCTION)(unsigned long);
539
540 #define COPY_MAC_ADDR(Addr1, Addr2)             memcpy((Addr1), (Addr2), MAC_ADDR_LEN)
541
542 #define MlmeAllocateMemory(_pAd, _ppVA) os_alloc_mem(_pAd, _ppVA, MGMT_DMA_BUFFER_SIZE)
543 #define MlmeFreeMemory(_pAd, _pVA)     os_free_mem(_pAd, _pVA)
544
545 #ifdef RT2860
546 #define BUILD_TIMER_FUNCTION(_func)                                                                                             \
547 void linux_##_func(unsigned long data)                                                                                  \
548 {                                                                                                                                                               \
549         PRALINK_TIMER_STRUCT    pTimer = (PRALINK_TIMER_STRUCT) data;                           \
550                                                                                                                                                                 \
551         _func(NULL, (PVOID) pTimer->cookie, NULL, pTimer);                                                      \
552         if (pTimer->Repeat)                                                                                                                     \
553                 RTMP_OS_Add_Timer(&pTimer->TimerObj, pTimer->TimerValue);                               \
554 }
555 #endif
556 #ifdef RT2870
557 #define BUILD_TIMER_FUNCTION(_func)                                                                                                     \
558 void linux_##_func(unsigned long data)                                                                                          \
559 {                                                                                                                                                                       \
560         PRALINK_TIMER_STRUCT    _pTimer = (PRALINK_TIMER_STRUCT)data;                                   \
561         RT2870_TIMER_ENTRY              *_pQNode;                                                                                               \
562         RTMP_ADAPTER                    *_pAd;                                                                                                  \
563                                                                                                                                                                 \
564         _pTimer->handle = _func;                                                                                                                        \
565         _pAd = (RTMP_ADAPTER *)_pTimer->pAd;                                                                                            \
566         _pQNode = RT2870_TimerQ_Insert(_pAd, _pTimer);                                                                  \
567         if ((_pQNode == NULL) && (_pAd->TimerQ.status & RT2870_THREAD_CAN_DO_INSERT))   \
568                 RTMP_OS_Add_Timer(&_pTimer->TimerObj, HZ);                                                      \
569 }
570 #endif // RT2870 //
571
572
573 #define DECLARE_TIMER_FUNCTION(_func)                   \
574 void linux_##_func(unsigned long data)
575
576 #define GET_TIMER_FUNCTION(_func)                               \
577                 linux_##_func
578
579 DECLARE_TIMER_FUNCTION(MlmePeriodicExec);
580 DECLARE_TIMER_FUNCTION(MlmeRssiReportExec);
581 DECLARE_TIMER_FUNCTION(AsicRxAntEvalTimeout);
582 DECLARE_TIMER_FUNCTION(APSDPeriodicExec);
583 DECLARE_TIMER_FUNCTION(AsicRfTuningExec);
584 #ifdef RT2870
585 DECLARE_TIMER_FUNCTION(BeaconUpdateExec);
586 #endif // RT2870 //
587
588 DECLARE_TIMER_FUNCTION(BeaconTimeout);
589 DECLARE_TIMER_FUNCTION(ScanTimeout);
590 DECLARE_TIMER_FUNCTION(AuthTimeout);
591 DECLARE_TIMER_FUNCTION(AssocTimeout);
592 DECLARE_TIMER_FUNCTION(ReassocTimeout);
593 DECLARE_TIMER_FUNCTION(DisassocTimeout);
594 DECLARE_TIMER_FUNCTION(LinkDownExec);
595 DECLARE_TIMER_FUNCTION(StaQuickResponeForRateUpExec);
596 DECLARE_TIMER_FUNCTION(WpaDisassocApAndBlockAssoc);
597 DECLARE_TIMER_FUNCTION(PsPollWakeExec);
598 DECLARE_TIMER_FUNCTION(RadioOnExec);
599
600 void RTMP_GetCurrentSystemTime(LARGE_INTEGER *time);
601
602
603 /*
604  * packet helper
605  *      - convert internal rt packet to os packet or
606  *             os packet to rt packet
607  */
608 #define RTPKT_TO_OSPKT(_p)              ((struct sk_buff *)(_p))
609 #define OSPKT_TO_RTPKT(_p)              ((PNDIS_PACKET)(_p))
610
611 #define GET_OS_PKT_DATAPTR(_pkt) \
612                 (RTPKT_TO_OSPKT(_pkt)->data)
613
614 #define GET_OS_PKT_LEN(_pkt) \
615                 (RTPKT_TO_OSPKT(_pkt)->len)
616
617 #define GET_OS_PKT_DATATAIL(_pkt) \
618                 (RTPKT_TO_OSPKT(_pkt)->tail)
619
620 #define GET_OS_PKT_HEAD(_pkt) \
621                 (RTPKT_TO_OSPKT(_pkt)->head)
622
623 #define GET_OS_PKT_END(_pkt) \
624                 (RTPKT_TO_OSPKT(_pkt)->end)
625
626 #define GET_OS_PKT_NETDEV(_pkt) \
627                 (RTPKT_TO_OSPKT(_pkt)->dev)
628
629 #define GET_OS_PKT_TYPE(_pkt) \
630                 (RTPKT_TO_OSPKT(_pkt))
631
632 #define GET_OS_PKT_NEXT(_pkt) \
633                 (RTPKT_TO_OSPKT(_pkt)->next)
634
635
636 #define OS_NTOHS(_Val) \
637                 (ntohs(_Val))
638 #define OS_HTONS(_Val) \
639                 (htons(_Val))
640 #define OS_NTOHL(_Val) \
641                 (ntohl(_Val))
642 #define OS_HTONL(_Val) \
643                 (htonl(_Val))
644
645 /* statistics counter */
646 #define STATS_INC_RX_PACKETS(_pAd, _dev)
647 #define STATS_INC_TX_PACKETS(_pAd, _dev)
648
649 #define STATS_INC_RX_BYTESS(_pAd, _dev, len)
650 #define STATS_INC_TX_BYTESS(_pAd, _dev, len)
651
652 #define STATS_INC_RX_ERRORS(_pAd, _dev)
653 #define STATS_INC_TX_ERRORS(_pAd, _dev)
654
655 #define STATS_INC_RX_DROPPED(_pAd, _dev)
656 #define STATS_INC_TX_DROPPED(_pAd, _dev)
657
658
659 #define CB_OFF  10
660
661
662 //   check DDK NDIS_PACKET data structure and find out only MiniportReservedEx[0..7] can be used by our driver without
663 //   ambiguity. Fields after pPacket->MiniportReservedEx[8] may be used by other wrapper layer thus crashes the driver
664 //
665
666 // User Priority
667 #define RTMP_SET_PACKET_UP(_p, _prio)                   (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+0] = _prio)
668 #define RTMP_GET_PACKET_UP(_p)                                  (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+0])
669
670 // Fragment #
671 #define RTMP_SET_PACKET_FRAGMENTS(_p, _num)             (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+1] = _num)
672 #define RTMP_GET_PACKET_FRAGMENTS(_p)                   (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+1])
673
674 // 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.
675 //(this value also as MAC(on-chip WCID) table index)
676 // 0x80~0xff: TX to a WDS link. b0~6: WDS index
677 #define RTMP_SET_PACKET_WCID(_p, _wdsidx)               (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+2] = _wdsidx)
678 #define RTMP_GET_PACKET_WCID(_p)                        ((UCHAR)(RTPKT_TO_OSPKT(_p)->cb[CB_OFF+2]))
679
680 // 0xff: PKTSRC_NDIS, others: local TX buffer index. This value affects how to a packet
681 #define RTMP_SET_PACKET_SOURCE(_p, _pktsrc)             (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+3] = _pktsrc)
682 #define RTMP_GET_PACKET_SOURCE(_p)                      (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+3])
683
684 // RTS/CTS-to-self protection method
685 #define RTMP_SET_PACKET_RTS(_p, _num)                   (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+4] = _num)
686 #define RTMP_GET_PACKET_RTS(_p)                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+4])
687 // see RTMP_S(G)ET_PACKET_EMACTAB
688
689 // TX rate index
690 #define RTMP_SET_PACKET_TXRATE(_p, _rate)               (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+5] = _rate)
691 #define RTMP_GET_PACKET_TXRATE(_p)                              (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+5])
692
693 // From which Interface
694 #define RTMP_SET_PACKET_IF(_p, _ifdx)           (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+6] = _ifdx)
695 #define RTMP_GET_PACKET_IF(_p)                          (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+6])
696 #define RTMP_SET_PACKET_NET_DEVICE_MBSSID(_p, _bss)             RTMP_SET_PACKET_IF((_p), (_bss))
697 #define RTMP_SET_PACKET_NET_DEVICE_WDS(_p, _bss)                RTMP_SET_PACKET_IF((_p), ((_bss) + MIN_NET_DEVICE_FOR_WDS))
698 #define RTMP_SET_PACKET_NET_DEVICE_APCLI(_p, _idx)      RTMP_SET_PACKET_IF((_p), ((_idx) + MIN_NET_DEVICE_FOR_APCLI))
699 #define RTMP_SET_PACKET_NET_DEVICE_MESH(_p, _idx)       RTMP_SET_PACKET_IF((_p), ((_idx) + MIN_NET_DEVICE_FOR_MESH))
700 #define RTMP_GET_PACKET_NET_DEVICE_MBSSID(_p)                   RTMP_GET_PACKET_IF((_p))
701 #define RTMP_GET_PACKET_NET_DEVICE(_p)                                  RTMP_GET_PACKET_IF((_p))
702
703 #define RTMP_SET_PACKET_MOREDATA(_p, _morebit)          (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+7] = _morebit)
704 #define RTMP_GET_PACKET_MOREDATA(_p)                            (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+7])
705
706
707 //
708 //      Sepcific Pakcet Type definition
709 //
710 #define RTMP_PACKET_SPECIFIC_CB_OFFSET  11
711
712 #define RTMP_PACKET_SPECIFIC_DHCP               0x01
713 #define RTMP_PACKET_SPECIFIC_EAPOL              0x02
714 #define RTMP_PACKET_SPECIFIC_IPV4               0x04
715 #define RTMP_PACKET_SPECIFIC_WAI                0x08
716 #define RTMP_PACKET_SPECIFIC_VLAN               0x10
717 #define RTMP_PACKET_SPECIFIC_LLCSNAP    0x20
718
719 //Specific
720 #define RTMP_SET_PACKET_SPECIFIC(_p, _flg)              (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11] = _flg)
721
722 //DHCP
723 #define RTMP_SET_PACKET_DHCP(_p, _flg)                                                                                                          \
724                         do{                                                                                                                                                             \
725                                 if (_flg)                                                                                                                                       \
726                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) |= (RTMP_PACKET_SPECIFIC_DHCP);             \
727                                 else                                                                                                                                            \
728                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) &= (!RTMP_PACKET_SPECIFIC_DHCP);    \
729                         }while(0)
730 #define RTMP_GET_PACKET_DHCP(_p)                (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11] & RTMP_PACKET_SPECIFIC_DHCP)
731
732 //EAPOL
733 #define RTMP_SET_PACKET_EAPOL(_p, _flg)                                                                                                         \
734                         do{                                                                                                                                                             \
735                                 if (_flg)                                                                                                                                       \
736                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) |= (RTMP_PACKET_SPECIFIC_EAPOL);            \
737                                 else                                                                                                                                            \
738                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) &= (!RTMP_PACKET_SPECIFIC_EAPOL);   \
739                         }while(0)
740 #define RTMP_GET_PACKET_EAPOL(_p)               (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11] & RTMP_PACKET_SPECIFIC_EAPOL)
741
742 //WAI
743 #define RTMP_SET_PACKET_WAI(_p, _flg)                                                                                                           \
744                         do{                                                                                                                                                             \
745                                 if (_flg)                                                                                                                                       \
746                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) |= (RTMP_PACKET_SPECIFIC_WAI);              \
747                                 else                                                                                                                                            \
748                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) &= (!RTMP_PACKET_SPECIFIC_WAI);     \
749                         }while(0)
750 #define RTMP_GET_PACKET_WAI(_p)         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11] & RTMP_PACKET_SPECIFIC_WAI)
751
752 #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))
753
754 //VLAN
755 #define RTMP_SET_PACKET_VLAN(_p, _flg)                                                                                                          \
756                         do{                                                                                                                                                             \
757                                 if (_flg)                                                                                                                                       \
758                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) |= (RTMP_PACKET_SPECIFIC_VLAN);             \
759                                 else                                                                                                                                            \
760                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) &= (!RTMP_PACKET_SPECIFIC_VLAN);    \
761                         }while(0)
762 #define RTMP_GET_PACKET_VLAN(_p)                (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11] & RTMP_PACKET_SPECIFIC_VLAN)
763
764 //LLC/SNAP
765 #define RTMP_SET_PACKET_LLCSNAP(_p, _flg)                                                                                                       \
766                         do{                                                                                                                                                             \
767                                 if (_flg)                                                                                                                                       \
768                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) |= (RTMP_PACKET_SPECIFIC_LLCSNAP);          \
769                                 else                                                                                                                                            \
770                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) &= (!RTMP_PACKET_SPECIFIC_LLCSNAP);         \
771                         }while(0)
772
773 #define RTMP_GET_PACKET_LLCSNAP(_p)             (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11] & RTMP_PACKET_SPECIFIC_LLCSNAP)
774
775 // IP
776 #define RTMP_SET_PACKET_IPV4(_p, _flg)                                                                                                          \
777                         do{                                                                                                                                                             \
778                                 if (_flg)                                                                                                                                       \
779                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) |= (RTMP_PACKET_SPECIFIC_IPV4);             \
780                                 else                                                                                                                                            \
781                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) &= (!RTMP_PACKET_SPECIFIC_IPV4);    \
782                         }while(0)
783
784 #define RTMP_GET_PACKET_IPV4(_p)                (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11] & RTMP_PACKET_SPECIFIC_IPV4)
785
786
787 // If this flag is set, it indicates that this EAPoL frame MUST be clear.
788 #define RTMP_SET_PACKET_CLEAR_EAP_FRAME(_p, _flg)   (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+12] = _flg)
789 #define RTMP_GET_PACKET_CLEAR_EAP_FRAME(_p)         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+12])
790
791 #define RTMP_SET_PACKET_5VT(_p, _flg)   (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+22] = _flg)
792 #define RTMP_GET_PACKET_5VT(_p)         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+22])
793
794 #ifdef CONFIG_5VT_ENHANCE
795 #define BRIDGE_TAG 0x35564252    // depends on 5VT define in br_input.c
796 #endif
797
798
799 #define NDIS_SET_PACKET_STATUS(_p, _status)
800
801
802 #define GET_SG_LIST_FROM_PACKET(_p, _sc)        \
803     rt_get_sg_list_from_packet(_p, _sc)
804
805 #define NdisMoveMemory(Destination, Source, Length) memmove(Destination, Source, Length)
806 #define NdisZeroMemory(Destination, Length)         memset(Destination, 0, Length)
807 #define NdisFillMemory(Destination, Length, Fill)   memset(Destination, Fill, Length)
808 #define NdisEqualMemory(Source1, Source2, Length)   (!memcmp(Source1, Source2, Length))
809 #define RTMPEqualMemory(Source1, Source2, Length)       (!memcmp(Source1, Source2, Length))
810
811
812 #define RTMP_INC_REF(_A)                0
813 #define RTMP_DEC_REF(_A)                0
814 #define RTMP_GET_REF(_A)                0
815
816
817
818 /*
819  * ULONG
820  * RTMP_GetPhysicalAddressLow(
821  *   IN NDIS_PHYSICAL_ADDRESS  PhysicalAddress);
822  */
823 #define RTMP_GetPhysicalAddressLow(PhysicalAddress)             (PhysicalAddress)
824
825 /*
826  * ULONG
827  * RTMP_GetPhysicalAddressHigh(
828  *   IN NDIS_PHYSICAL_ADDRESS  PhysicalAddress);
829  */
830 #define RTMP_GetPhysicalAddressHigh(PhysicalAddress)            (0)
831
832 /*
833  * VOID
834  * RTMP_SetPhysicalAddressLow(
835  *   IN NDIS_PHYSICAL_ADDRESS  PhysicalAddress,
836  *   IN ULONG  Value);
837  */
838 #define RTMP_SetPhysicalAddressLow(PhysicalAddress, Value)      \
839                         PhysicalAddress = Value;
840
841 /*
842  * VOID
843  * RTMP_SetPhysicalAddressHigh(
844  *   IN NDIS_PHYSICAL_ADDRESS  PhysicalAddress,
845  *   IN ULONG  Value);
846  */
847 #define RTMP_SetPhysicalAddressHigh(PhysicalAddress, Value)
848
849
850 //CONTAINING_RECORD(pEntry, NDIS_PACKET, MiniportReservedEx);
851 #define QUEUE_ENTRY_TO_PACKET(pEntry) \
852         (PNDIS_PACKET)(pEntry)
853
854 #define PACKET_TO_QUEUE_ENTRY(pPacket) \
855         (PQUEUE_ENTRY)(pPacket)
856
857
858 #ifndef CONTAINING_RECORD
859 #define CONTAINING_RECORD(address, type, field)                 \
860 ((type *)((PCHAR)(address) - offsetof(type, field)))
861 #endif
862
863
864 #define RELEASE_NDIS_PACKET(_pAd, _pPacket, _Status)                    \
865 {                                                                       \
866         RTMPFreeNdisPacket(_pAd, _pPacket);                             \
867 }
868
869
870 #define SWITCH_PhyAB(_pAA, _pBB)    \
871 {                                                                           \
872     ULONG       AABasePaHigh;                           \
873     ULONG       AABasePaLow;                           \
874     ULONG       BBBasePaHigh;                           \
875     ULONG       BBBasePaLow;                           \
876     BBBasePaHigh = RTMP_GetPhysicalAddressHigh(_pBB);                                                 \
877     BBBasePaLow = RTMP_GetPhysicalAddressLow(_pBB);                                                 \
878     AABasePaHigh = RTMP_GetPhysicalAddressHigh(_pAA);                                                 \
879     AABasePaLow = RTMP_GetPhysicalAddressLow(_pAA);                                                 \
880     RTMP_SetPhysicalAddressHigh(_pAA, BBBasePaHigh);                                                 \
881     RTMP_SetPhysicalAddressLow(_pAA, BBBasePaLow);                                                 \
882     RTMP_SetPhysicalAddressHigh(_pBB, AABasePaHigh);                                                 \
883     RTMP_SetPhysicalAddressLow(_pBB, AABasePaLow);                                                 \
884 }
885
886
887 #define NdisWriteErrorLogEntry(_a, _b, _c, _d)
888 #define NdisMAllocateMapRegisters(_a, _b, _c, _d, _e)           NDIS_STATUS_SUCCESS
889
890
891 #define NdisAcquireSpinLock             RTMP_SEM_LOCK
892 #define NdisReleaseSpinLock             RTMP_SEM_UNLOCK
893
894 static inline void NdisGetSystemUpTime(ULONG *time)
895 {
896         *time = jiffies;
897 }
898
899 //pPacket = CONTAINING_RECORD(pEntry, NDIS_PACKET, MiniportReservedEx);
900 #define QUEUE_ENTRY_TO_PKT(pEntry) \
901                 ((PNDIS_PACKET) (pEntry))
902
903 int rt28xx_packet_xmit(struct sk_buff *skb);
904
905
906
907 void rtmp_os_thread_init(PUCHAR pThreadName, PVOID pNotify);
908
909 #ifdef RT2860
910 #if !defined(PCI_CAP_ID_EXP)
911 #define PCI_CAP_ID_EXP                      0x10
912 #endif
913
914 #if !defined(PCI_EXP_LNKCTL)
915 #define PCI_EXP_LNKCTL                      0x10
916 #endif
917
918 #if !defined(PCI_CLASS_BRIDGE_PCI)
919 #define PCI_CLASS_BRIDGE_PCI            0x0604
920 #endif
921
922 #define PCIBUS_INTEL_VENDOR         0x8086
923 #endif
924