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