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