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