Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[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     Module Name:
28         rt_linux.h
29
30     Abstract:
31
32     Revision History:
33     Who          When          What
34     ---------    ----------    ----------------------------------------------
35 */
36
37 #ifndef __RT_LINUX_H__
38 #define __RT_LINUX_H__
39
40 #include <linux/module.h>
41 #include <linux/kernel.h>
42 #include <linux/spinlock.h>
43 #include <linux/init.h>
44 #include <linux/string.h>
45 #include <linux/timer.h>
46 #include <linux/errno.h>
47 #include <linux/slab.h>
48 #include <linux/interrupt.h>
49 #include <linux/pci.h>
50 #include <linux/netdevice.h>
51 #include <linux/etherdevice.h>
52 #include <linux/skbuff.h>
53 #include <linux/ethtool.h>
54 #include <linux/wireless.h>
55 #include <linux/proc_fs.h>
56 #include <linux/delay.h>
57 #include <linux/if_arp.h>
58 #include <linux/ctype.h>
59 #include <linux/vmalloc.h>
60
61 #include <net/iw_handler.h>
62
63 /* load firmware */
64 #define __KERNEL_SYSCALLS__
65 #include <linux/unistd.h>
66 #include <asm/uaccess.h>
67 #include <asm/types.h>
68 #include <asm/unaligned.h>      /* for get_unaligned() */
69
70 #define KTHREAD_SUPPORT 1
71 /* RT2870 2.1.0.0 has it disabled */
72
73 #ifdef KTHREAD_SUPPORT
74 #include <linux/err.h>
75 #include <linux/kthread.h>
76 #endif /* KTHREAD_SUPPORT // */
77
78 /***********************************************************************************
79  *      Profile related sections
80  ***********************************************************************************/
81
82 #ifdef RTMP_MAC_PCI
83 #define STA_DRIVER_VERSION                      "2.1.0.0"
84 #endif /* RTMP_MAC_PCI // */
85 #ifdef RTMP_MAC_USB
86 #define STA_DRIVER_VERSION                      "2.1.0.0"
87 /* RT3070 version: 2.1.1.0 */
88 #endif /* RTMP_MAC_USB // */
89
90 extern const struct iw_handler_def rt28xx_iw_handler_def;
91
92 /***********************************************************************************
93  *      Compiler related definitions
94  ***********************************************************************************/
95 #undef __inline
96 #define __inline           static inline
97 #define IN
98 #define OUT
99 #define INOUT
100
101 /***********************************************************************************
102  *      OS Specific definitions and data structures
103  ***********************************************************************************/
104 typedef int (*HARD_START_XMIT_FUNC) (struct sk_buff * skb,
105                                      struct net_device * net_dev);
106
107 #ifdef RTMP_MAC_PCI
108 #ifndef PCI_DEVICE
109 #define PCI_DEVICE(vend,dev) \
110         .vendor = (vend), .device = (dev), \
111         .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID
112 #endif /* PCI_DEVICE // */
113 #endif /* RTMP_MAC_PCI // */
114
115 #define RT_MOD_INC_USE_COUNT() \
116         if (!try_module_get(THIS_MODULE)) \
117         { \
118                 DBGPRINT(RT_DEBUG_ERROR, ("%s: cannot reserve module\n", __func__)); \
119                 return -1; \
120         }
121
122 #define RT_MOD_DEC_USE_COUNT() module_put(THIS_MODULE);
123
124 #define RTMP_INC_REF(_A)                0
125 #define RTMP_DEC_REF(_A)                0
126 #define RTMP_GET_REF(_A)                0
127
128 /* This function will be called when query /proc */
129 struct iw_statistics *rt28xx_get_wireless_stats(IN struct net_device *net_dev);
130
131 /***********************************************************************************
132  *      Network related constant definitions
133  ***********************************************************************************/
134 #ifndef IFNAMSIZ
135 #define IFNAMSIZ 16
136 #endif
137
138 #define ETH_LENGTH_OF_ADDRESS   6
139
140 #define NDIS_STATUS_SUCCESS                     0x00
141 #define NDIS_STATUS_FAILURE                     0x01
142 #define NDIS_STATUS_INVALID_DATA                                0x02
143 #define NDIS_STATUS_RESOURCES                   0x03
144
145 #define NDIS_SET_PACKET_STATUS(_p, _status)                     do{} while(0)
146 #define NdisWriteErrorLogEntry(_a, _b, _c, _d)          do{} while(0)
147
148 /* statistics counter */
149 #define STATS_INC_RX_PACKETS(_pAd, _dev)
150 #define STATS_INC_TX_PACKETS(_pAd, _dev)
151
152 #define STATS_INC_RX_BYTESS(_pAd, _dev, len)
153 #define STATS_INC_TX_BYTESS(_pAd, _dev, len)
154
155 #define STATS_INC_RX_ERRORS(_pAd, _dev)
156 #define STATS_INC_TX_ERRORS(_pAd, _dev)
157
158 #define STATS_INC_RX_DROPPED(_pAd, _dev)
159 #define STATS_INC_TX_DROPPED(_pAd, _dev)
160
161 /***********************************************************************************
162  *      Ralink Specific network related constant definitions
163  ***********************************************************************************/
164 #define MIN_NET_DEVICE_FOR_AID                  0x00    /*0x00~0x3f */
165 #define MIN_NET_DEVICE_FOR_MBSSID               0x00    /*0x00,0x10,0x20,0x30 */
166 #define MIN_NET_DEVICE_FOR_WDS                  0x10    /*0x40,0x50,0x60,0x70 */
167 #define MIN_NET_DEVICE_FOR_APCLI                0x20
168 #define MIN_NET_DEVICE_FOR_MESH                 0x30
169 #define MIN_NET_DEVICE_FOR_DLS                  0x40
170 #define NET_DEVICE_REAL_IDX_MASK                0x0f    /* for each operation mode, we maximum support 15 entities. */
171
172 #define NDIS_PACKET_TYPE_DIRECTED               0
173 #define NDIS_PACKET_TYPE_MULTICAST              1
174 #define NDIS_PACKET_TYPE_BROADCAST              2
175 #define NDIS_PACKET_TYPE_ALL_MULTICAST  3
176 #define NDIS_PACKET_TYPE_PROMISCUOUS    4
177
178 /***********************************************************************************
179  *      OS signaling related constant definitions
180  ***********************************************************************************/
181
182 /***********************************************************************************
183  *      OS file operation related data structure definitions
184  ***********************************************************************************/
185 struct rt_rtmp_os_fs_info {
186         int fsuid;
187         int fsgid;
188         mm_segment_t fs;
189 };
190
191 #define IS_FILE_OPEN_ERR(_fd)   IS_ERR((_fd))
192
193 /***********************************************************************************
194  *      OS semaphore related data structure and definitions
195  ***********************************************************************************/
196 struct os_lock {
197         spinlock_t lock;
198         unsigned long flags;
199 };
200
201 /* */
202 /*  spin_lock enhanced for Nested spin lock */
203 /* */
204 #define NdisAllocateSpinLock(__lock)      \
205 {                                       \
206     spin_lock_init((spinlock_t *)(__lock));               \
207 }
208
209 #define NdisFreeSpinLock(lock)          \
210         do{}while(0)
211
212 #define RTMP_SEM_LOCK(__lock)                                   \
213 {                                                                                               \
214         spin_lock_bh((spinlock_t *)(__lock));           \
215 }
216
217 #define RTMP_SEM_UNLOCK(__lock)                                 \
218 {                                                                                               \
219         spin_unlock_bh((spinlock_t *)(__lock));         \
220 }
221
222 /* sample, use semaphore lock to replace IRQ lock, 2007/11/15 */
223 #define RTMP_IRQ_LOCK(__lock, __irqflags)                       \
224 {                                                                                                       \
225         __irqflags = 0;                                                                 \
226         spin_lock_bh((spinlock_t *)(__lock));                   \
227         pAd->irq_disabled |= 1; \
228 }
229
230 #define RTMP_IRQ_UNLOCK(__lock, __irqflag)                      \
231 {                                                                                                       \
232         pAd->irq_disabled &= 0;                                                 \
233         spin_unlock_bh((spinlock_t *)(__lock));                 \
234 }
235
236 #define RTMP_INT_LOCK(__lock, __irqflags)                       \
237 {                                                                                                       \
238         spin_lock_irqsave((spinlock_t *)__lock, __irqflags);    \
239 }
240
241 #define RTMP_INT_UNLOCK(__lock, __irqflag)                      \
242 {                                                                                                       \
243         spin_unlock_irqrestore((spinlock_t *)(__lock), ((unsigned long)__irqflag));     \
244 }
245
246 #define NdisAcquireSpinLock             RTMP_SEM_LOCK
247 #define NdisReleaseSpinLock             RTMP_SEM_UNLOCK
248
249 #ifndef wait_event_interruptible_timeout
250 #define __wait_event_interruptible_timeout(wq, condition, ret) \
251 do { \
252         wait_queue_t __wait; \
253         init_waitqueue_entry(&__wait, current); \
254         add_wait_queue(&wq, &__wait); \
255         for (;;) { \
256                 set_current_state(TASK_INTERRUPTIBLE); \
257                 if (condition) \
258                         break; \
259                 if (!signal_pending(current)) { \
260                         ret = schedule_timeout(ret); \
261                         if (!ret) \
262                                 break; \
263                         continue; \
264                 } \
265                 ret = -ERESTARTSYS; \
266                 break; \
267         } \
268         current->state = TASK_RUNNING; \
269         remove_wait_queue(&wq, &__wait); \
270 } while (0)
271
272 #define wait_event_interruptible_timeout(wq, condition, timeout) \
273 ({ \
274         long __ret = timeout; \
275         if (!(condition)) \
276                 __wait_event_interruptible_timeout(wq, condition, __ret); \
277         __ret; \
278 })
279 #endif
280
281 #define RTMP_SEM_EVENT_INIT_LOCKED(_pSema)      sema_init((_pSema), 0)
282 #define RTMP_SEM_EVENT_INIT(_pSema)                     sema_init((_pSema), 1)
283 #define RTMP_SEM_EVENT_WAIT(_pSema, _status)    ((_status) = down_interruptible((_pSema)))
284 #define RTMP_SEM_EVENT_UP(_pSema)                       up(_pSema)
285
286 #ifdef KTHREAD_SUPPORT
287 #define RTMP_WAIT_EVENT_INTERRUPTIBLE(_pAd, _pTask) \
288 { \
289                 wait_event_interruptible(_pTask->kthread_q, \
290                                                                  _pTask->kthread_running || kthread_should_stop()); \
291                 _pTask->kthread_running = FALSE; \
292                 if (kthread_should_stop()) \
293                 { \
294                         RTMP_SET_FLAG(_pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS); \
295                         break; \
296                 } \
297 }
298 #endif
299
300 #ifdef KTHREAD_SUPPORT
301 #define WAKE_UP(_pTask) \
302         do{ \
303                 if ((_pTask)->kthread_task) \
304         { \
305                         (_pTask)->kthread_running = TRUE; \
306                 wake_up(&(_pTask)->kthread_q); \
307                 } \
308         }while(0)
309 #endif
310
311 /***********************************************************************************
312  *      OS Memory Access related data structure and definitions
313  ***********************************************************************************/
314 #define MEM_ALLOC_FLAG      (GFP_ATOMIC)        /*(GFP_DMA | GFP_ATOMIC) */
315
316 #define NdisMoveMemory(Destination, Source, Length) memmove(Destination, Source, Length)
317 #define NdisCopyMemory(Destination, Source, Length) memcpy(Destination, Source, Length)
318 #define NdisZeroMemory(Destination, Length)         memset(Destination, 0, Length)
319 #define NdisFillMemory(Destination, Length, Fill)   memset(Destination, Fill, Length)
320 #define NdisCmpMemory(Destination, Source, Length)  memcmp(Destination, Source, Length)
321 #define NdisEqualMemory(Source1, Source2, Length)   (!memcmp(Source1, Source2, Length))
322 #define RTMPEqualMemory(Source1, Source2, Length)       (!memcmp(Source1, Source2, Length))
323
324 #define MlmeAllocateMemory(_pAd, _ppVA)         os_alloc_mem(_pAd, _ppVA, MGMT_DMA_BUFFER_SIZE)
325 #define MlmeFreeMemory(_pAd, _pVA)                      os_free_mem(_pAd, _pVA)
326
327 #define COPY_MAC_ADDR(Addr1, Addr2)             memcpy((Addr1), (Addr2), MAC_ADDR_LEN)
328
329 /***********************************************************************************
330  *      OS task related data structure and definitions
331  ***********************************************************************************/
332 #define RTMP_OS_MGMT_TASK_FLAGS CLONE_VM
333
334 #define THREAD_PID_INIT_VALUE   NULL
335 #define GET_PID(_v)     find_get_pid((_v))
336 #define GET_PID_NUMBER(_v)      pid_nr((_v))
337 #define CHECK_PID_LEGALITY(_pid)        if (pid_nr((_pid)) > 0)
338 #define KILL_THREAD_PID(_A, _B, _C)     kill_pid((_A), (_B), (_C))
339
340 /***********************************************************************************
341  * Timer related definitions and data structures.
342  **********************************************************************************/
343 #define OS_HZ                   HZ
344
345 typedef void (*TIMER_FUNCTION) (unsigned long);
346
347 #define OS_WAIT(_time) \
348 {       int _i; \
349         long _loop = ((_time)/(1000/OS_HZ)) > 0 ? ((_time)/(1000/OS_HZ)) : 1;\
350         wait_queue_head_t _wait; \
351         init_waitqueue_head(&_wait); \
352         for (_i=0; _i<(_loop); _i++) \
353                 wait_event_interruptible_timeout(_wait, 0, ONE_TICK); }
354
355 #define RTMP_TIME_AFTER(a,b)            \
356         (typecheck(unsigned long, (unsigned long)a) && \
357          typecheck(unsigned long, (unsigned long)b) && \
358          ((long)(b) - (long)(a) < 0))
359
360 #define RTMP_TIME_AFTER_EQ(a,b) \
361         (typecheck(unsigned long, (unsigned long)a) && \
362          typecheck(unsigned long, (unsigned long)b) && \
363          ((long)(a) - (long)(b) >= 0))
364 #define RTMP_TIME_BEFORE(a,b)   RTMP_TIME_AFTER_EQ(b,a)
365
366 #define ONE_TICK 1
367
368 static inline void NdisGetSystemUpTime(unsigned long * time)
369 {
370         *time = jiffies;
371 }
372
373 /***********************************************************************************
374  *      OS specific cookie data structure binding to struct rt_rtmp_adapter
375  ***********************************************************************************/
376
377 struct os_cookie {
378 #ifdef RTMP_MAC_PCI
379         struct pci_dev *pci_dev;
380         struct pci_dev *parent_pci_dev;
381         u16 DeviceID;
382         dma_addr_t pAd_pa;
383 #endif                          /* RTMP_MAC_PCI // */
384 #ifdef RTMP_MAC_USB
385         struct usb_device *pUsb_Dev;
386 #endif                          /* RTMP_MAC_USB // */
387
388         struct tasklet_struct rx_done_task;
389         struct tasklet_struct mgmt_dma_done_task;
390         struct tasklet_struct ac0_dma_done_task;
391         struct tasklet_struct ac1_dma_done_task;
392         struct tasklet_struct ac2_dma_done_task;
393         struct tasklet_struct ac3_dma_done_task;
394         struct tasklet_struct tbtt_task;
395 #ifdef RTMP_MAC_PCI
396         struct tasklet_struct fifo_statistic_full_task;
397 #endif                          /* RTMP_MAC_PCI // */
398 #ifdef RTMP_MAC_USB
399         struct tasklet_struct null_frame_complete_task;
400         struct tasklet_struct rts_frame_complete_task;
401         struct tasklet_struct pspoll_frame_complete_task;
402 #endif                          /* RTMP_MAC_USB // */
403
404         unsigned long apd_pid;  /*802.1x daemon pid */
405         int ioctl_if_type;
406         int ioctl_if;
407 };
408
409 /***********************************************************************************
410  *      OS debugging and printing related definitions and data structure
411  ***********************************************************************************/
412 #define PRINT_MAC(addr) \
413         addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]
414
415 #ifdef DBG
416 extern unsigned long RTDebugLevel;
417
418 #define DBGPRINT_RAW(Level, Fmt)    \
419 do{                                   \
420     if (Level <= RTDebugLevel)      \
421     {                               \
422         printk Fmt;               \
423     }                               \
424 }while(0)
425
426 #define DBGPRINT(Level, Fmt)    DBGPRINT_RAW(Level, Fmt)
427
428 #define DBGPRINT_ERR(Fmt)           \
429 {                                   \
430     printk("ERROR! ");          \
431     printk Fmt;                  \
432 }
433
434 #define DBGPRINT_S(Status, Fmt)         \
435 {                                                                       \
436         printk Fmt;                                     \
437 }
438
439 #else
440 #define DBGPRINT(Level, Fmt)
441 #define DBGPRINT_RAW(Level, Fmt)
442 #define DBGPRINT_S(Status, Fmt)
443 #define DBGPRINT_ERR(Fmt)
444 #endif
445
446 #define ASSERT(x)
447
448 void hex_dump(char *str, unsigned char *pSrcBufVA, unsigned int SrcBufLen);
449
450 /*********************************************************************************************************
451         The following code are not revised, temporary put it here.
452   *********************************************************************************************************/
453
454 /***********************************************************************************
455  * Device DMA Access related definitions and data structures.
456  **********************************************************************************/
457 #ifdef RTMP_MAC_PCI
458 struct rt_rtmp_adapter;
459 dma_addr_t linux_pci_map_single(struct rt_rtmp_adapter *pAd, void *ptr,
460                                 size_t size, int sd_idx, int direction);
461 void linux_pci_unmap_single(struct rt_rtmp_adapter *pAd, dma_addr_t dma_addr,
462                             size_t size, int direction);
463
464 #define PCI_MAP_SINGLE(_handle, _ptr, _size, _sd_idx, _dir) \
465         linux_pci_map_single(_handle, _ptr, _size, _sd_idx, _dir)
466
467 #define PCI_UNMAP_SINGLE(_handle, _ptr, _size, _dir) \
468         linux_pci_unmap_single(_handle, _ptr, _size, _dir)
469
470 #define PCI_ALLOC_CONSISTENT(_pci_dev, _size, _ptr) \
471         pci_alloc_consistent(_pci_dev, _size, _ptr)
472
473 #define PCI_FREE_CONSISTENT(_pci_dev, _size, _virtual_addr, _physical_addr) \
474         pci_free_consistent(_pci_dev, _size, _virtual_addr, _physical_addr)
475
476 #define DEV_ALLOC_SKB(_length) \
477         dev_alloc_skb(_length)
478 #endif /* RTMP_MAC_PCI // */
479
480 /*
481  * unsigned long
482  * RTMP_GetPhysicalAddressLow(
483  *   dma_addr_t  PhysicalAddress);
484  */
485 #define RTMP_GetPhysicalAddressLow(PhysicalAddress)             (PhysicalAddress)
486
487 /*
488  * unsigned long
489  * RTMP_GetPhysicalAddressHigh(
490  *   dma_addr_t  PhysicalAddress);
491  */
492 #define RTMP_GetPhysicalAddressHigh(PhysicalAddress)            (0)
493
494 /*
495  * void
496  * RTMP_SetPhysicalAddressLow(
497  *   dma_addr_t  PhysicalAddress,
498  *   unsigned long  Value);
499  */
500 #define RTMP_SetPhysicalAddressLow(PhysicalAddress, Value)      \
501                         PhysicalAddress = Value;
502
503 /*
504  * void
505  * RTMP_SetPhysicalAddressHigh(
506  *   dma_addr_t  PhysicalAddress,
507  *   unsigned long  Value);
508  */
509 #define RTMP_SetPhysicalAddressHigh(PhysicalAddress, Value)
510
511 #define NdisMIndicateStatus(_w, _x, _y, _z)
512
513 /***********************************************************************************
514  * Device Register I/O Access related definitions and data structures.
515  **********************************************************************************/
516 #ifdef RTMP_MAC_PCI
517 /*Patch for ASIC turst read/write bug, needs to remove after metel fix */
518 #define RTMP_IO_READ32(_A, _R, _pV)                                                             \
519 {                                                                                                                               \
520     if ((_A)->bPCIclkOff == FALSE)                                  \
521     {                                                               \
522                 (*_pV = readl((void *)((_A)->CSRBaseAddress + MAC_CSR0)));              \
523                 (*_pV = readl((void *)((_A)->CSRBaseAddress + (_R))));                  \
524     }                                                               \
525     else                                                                                                                        \
526                 *_pV = 0;                                                                                                       \
527 }
528
529 #define RTMP_IO_FORCE_READ32(_A, _R, _pV)                                                       \
530 {                                                                                                                                       \
531         (*_pV = readl((void *)((_A)->CSRBaseAddress + MAC_CSR0)));              \
532         (*_pV = readl((void *)((_A)->CSRBaseAddress + (_R))));                  \
533 }
534
535 #define RTMP_IO_READ8(_A, _R, _pV)                                                              \
536 {                                                                                                                               \
537         (*_pV = readl((void *)((_A)->CSRBaseAddress + MAC_CSR0)));                      \
538         (*_pV = readb((void *)((_A)->CSRBaseAddress + (_R))));                          \
539 }
540 #define RTMP_IO_WRITE32(_A, _R, _V)                                                                                             \
541 {                                                                                                                                                               \
542     if ((_A)->bPCIclkOff == FALSE)                                  \
543     {                                                               \
544         u32 Val;                                                                                                                                \
545         Val = readl((void *)((_A)->CSRBaseAddress + MAC_CSR0));                 \
546         writel((_V), (void *)((_A)->CSRBaseAddress + (_R)));                                                            \
547     }                                                               \
548 }
549
550 #define RTMP_IO_FORCE_WRITE32(_A, _R, _V)                                                                                               \
551 {                                                                                                                                                               \
552         u32 Val;                                                                                                                                \
553         Val = readl((void *)((_A)->CSRBaseAddress + MAC_CSR0));                 \
554         writel(_V, (void *)((_A)->CSRBaseAddress + (_R)));                                                              \
555 }
556
557 #if defined(RALINK_2880) || defined(RALINK_3052)
558 #define RTMP_IO_WRITE8(_A, _R, _V)            \
559 {                    \
560         unsigned long Val;                \
561         u8 _i;                \
562         _i = ((_R) & 0x3);             \
563         Val = readl((void *)((_A)->CSRBaseAddress + ((_R) - _i)));   \
564         Val = Val & (~(0x000000ff << ((_i)*8)));         \
565         Val = Val | ((unsigned long)(_V) << ((_i)*8));         \
566         writel((Val), (void *)((_A)->CSRBaseAddress + ((_R) - _i)));    \
567 }
568 #else
569 #define RTMP_IO_WRITE8(_A, _R, _V)                                                                                              \
570 {                                                                                                                                                               \
571         u32 Val;                                                                                                                                \
572         Val = readl((void *)((_A)->CSRBaseAddress + MAC_CSR0));                 \
573         writeb((_V), (u8 *)((_A)->CSRBaseAddress + (_R)));              \
574 }
575 #endif /* #if defined(BRCM_6358) || defined(RALINK_2880) // */
576
577 #define RTMP_IO_WRITE16(_A, _R, _V)                                                                                             \
578 {                                                                                                                                                               \
579         u32 Val;                                                                                                                                \
580         Val = readl((void *)((_A)->CSRBaseAddress + MAC_CSR0));                 \
581         writew((_V), (u16 *)((_A)->CSRBaseAddress + (_R)));     \
582 }
583 #endif /* RTMP_MAC_PCI // */
584 #ifdef RTMP_MAC_USB
585 /*Patch for ASIC turst read/write bug, needs to remove after metel fix */
586 #define RTMP_IO_READ32(_A, _R, _pV)                                                             \
587         RTUSBReadMACRegister((_A), (_R), (u32 *)(_pV))
588
589 #define RTMP_IO_READ8(_A, _R, _pV)                                                              \
590 {                                                                                                                               \
591 }
592
593 #define RTMP_IO_WRITE32(_A, _R, _V)                                                             \
594         RTUSBWriteMACRegister((_A), (_R), (u32)(_V))
595
596 #define RTMP_IO_WRITE8(_A, _R, _V)                                                              \
597 {                                                                                                                               \
598         u16     _Val = _V;                                                                                      \
599         RTUSBSingleWrite((_A), (_R), (u16)(_Val));                                                              \
600 }
601
602 #define RTMP_IO_WRITE16(_A, _R, _V)                                                             \
603 {                                                                                                                               \
604         RTUSBSingleWrite((_A), (_R), (u16)(_V));                                                                \
605 }
606 #endif /* RTMP_MAC_USB // */
607
608 /***********************************************************************************
609  *      Network Related data structure and marco definitions
610  ***********************************************************************************/
611 #define PKTSRC_NDIS             0x7f
612 #define PKTSRC_DRIVER           0x0f
613
614 #define RTMP_OS_NETDEV_SET_PRIV(_pNetDev, _pPriv)       ((_pNetDev)->ml_priv = (_pPriv))
615 #define RTMP_OS_NETDEV_GET_PRIV(_pNetDev)               ((_pNetDev)->ml_priv)
616 #define RTMP_OS_NETDEV_GET_DEVNAME(_pNetDev)    ((_pNetDev)->name)
617 #define RTMP_OS_NETDEV_GET_PHYADDR(_PNETDEV)    ((_PNETDEV)->dev_addr)
618
619 #define RTMP_OS_NETDEV_START_QUEUE(_pNetDev)    netif_start_queue((_pNetDev))
620 #define RTMP_OS_NETDEV_STOP_QUEUE(_pNetDev)     netif_stop_queue((_pNetDev))
621 #define RTMP_OS_NETDEV_WAKE_QUEUE(_pNetDev)     netif_wake_queue((_pNetDev))
622 #define RTMP_OS_NETDEV_CARRIER_OFF(_pNetDev)    netif_carrier_off((_pNetDev))
623
624 #define QUEUE_ENTRY_TO_PACKET(pEntry) \
625         (void *)(pEntry)
626
627 #define PACKET_TO_QUEUE_ENTRY(pPacket) \
628         (struct rt_queue_entry *)(pPacket)
629
630 #define GET_SG_LIST_FROM_PACKET(_p, _sc)        \
631     rt_get_sg_list_from_packet(_p, _sc)
632
633 #define RELEASE_NDIS_PACKET(_pAd, _pPacket, _Status)                    \
634 {                                                                       \
635         RTMPFreeNdisPacket(_pAd, _pPacket);                             \
636 }
637
638 /*
639  * packet helper
640  *      - convert internal rt packet to os packet or
641  *             os packet to rt packet
642  */
643 #define RTPKT_TO_OSPKT(_p)              ((struct sk_buff *)(_p))
644 #define OSPKT_TO_RTPKT(_p)              ((void *)(_p))
645
646 #define GET_OS_PKT_DATAPTR(_pkt) \
647                 (RTPKT_TO_OSPKT(_pkt)->data)
648 #define SET_OS_PKT_DATAPTR(_pkt, _dataPtr)      \
649                 (RTPKT_TO_OSPKT(_pkt)->data) = (_dataPtr)
650
651 #define GET_OS_PKT_LEN(_pkt) \
652                 (RTPKT_TO_OSPKT(_pkt)->len)
653 #define SET_OS_PKT_LEN(_pkt, _len)      \
654                 (RTPKT_TO_OSPKT(_pkt)->len) = (_len)
655
656 #define GET_OS_PKT_DATATAIL(_pkt) \
657                 (skb_tail_pointer(RTPKT_TO_OSPKT(_pkt))
658 #define SET_OS_PKT_DATATAIL(_pkt, _start, _len) \
659                 (skb_set_tail_pointer(RTPKT_TO_OSPKT(_pkt), _len))
660
661 #define GET_OS_PKT_HEAD(_pkt) \
662                 (RTPKT_TO_OSPKT(_pkt)->head)
663
664 #define GET_OS_PKT_END(_pkt) \
665                 (RTPKT_TO_OSPKT(_pkt)->end)
666
667 #define GET_OS_PKT_NETDEV(_pkt) \
668                 (RTPKT_TO_OSPKT(_pkt)->dev)
669 #define SET_OS_PKT_NETDEV(_pkt, _pNetDev)       \
670                 (RTPKT_TO_OSPKT(_pkt)->dev) = (_pNetDev)
671
672 #define GET_OS_PKT_TYPE(_pkt) \
673                 (RTPKT_TO_OSPKT(_pkt))
674
675 #define GET_OS_PKT_NEXT(_pkt) \
676                 (RTPKT_TO_OSPKT(_pkt)->next)
677
678 #define OS_PKT_CLONED(_pkt)             skb_cloned(RTPKT_TO_OSPKT(_pkt))
679
680 #define OS_NTOHS(_Val) \
681                 (ntohs(_Val))
682 #define OS_HTONS(_Val) \
683                 (htons(_Val))
684 #define OS_NTOHL(_Val) \
685                 (ntohl(_Val))
686 #define OS_HTONL(_Val) \
687                 (htonl(_Val))
688
689 #define CB_OFF  10
690
691 /* User Priority */
692 #define RTMP_SET_PACKET_UP(_p, _prio)                   (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+0] = _prio)
693 #define RTMP_GET_PACKET_UP(_p)                                  (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+0])
694
695 /* Fragment # */
696 #define RTMP_SET_PACKET_FRAGMENTS(_p, _num)             (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+1] = _num)
697 #define RTMP_GET_PACKET_FRAGMENTS(_p)                   (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+1])
698
699 /* 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. */
700 /*(this value also as MAC(on-chip WCID) table index) */
701 /* 0x80~0xff: TX to a WDS link. b0~6: WDS index */
702 #define RTMP_SET_PACKET_WCID(_p, _wdsidx)               (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+2] = _wdsidx)
703 #define RTMP_GET_PACKET_WCID(_p)                        ((u8)(RTPKT_TO_OSPKT(_p)->cb[CB_OFF+2]))
704
705 /* 0xff: PKTSRC_NDIS, others: local TX buffer index. This value affects how to a packet */
706 #define RTMP_SET_PACKET_SOURCE(_p, _pktsrc)             (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+3] = _pktsrc)
707 #define RTMP_GET_PACKET_SOURCE(_p)                      (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+3])
708
709 /* RTS/CTS-to-self protection method */
710 #define RTMP_SET_PACKET_RTS(_p, _num)                   (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+4] = _num)
711 #define RTMP_GET_PACKET_RTS(_p)                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+4])
712 /* see RTMP_S(G)ET_PACKET_EMACTAB */
713
714 /* TX rate index */
715 #define RTMP_SET_PACKET_TXRATE(_p, _rate)               (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+5] = _rate)
716 #define RTMP_GET_PACKET_TXRATE(_p)                              (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+5])
717
718 /* From which Interface */
719 #define RTMP_SET_PACKET_IF(_p, _ifdx)           (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+6] = _ifdx)
720 #define RTMP_GET_PACKET_IF(_p)                          (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+6])
721 #define RTMP_SET_PACKET_NET_DEVICE_MBSSID(_p, _bss)             RTMP_SET_PACKET_IF((_p), (_bss))
722 #define RTMP_SET_PACKET_NET_DEVICE_WDS(_p, _bss)                RTMP_SET_PACKET_IF((_p), ((_bss) + MIN_NET_DEVICE_FOR_WDS))
723 #define RTMP_SET_PACKET_NET_DEVICE_APCLI(_p, _idx)      RTMP_SET_PACKET_IF((_p), ((_idx) + MIN_NET_DEVICE_FOR_APCLI))
724 #define RTMP_SET_PACKET_NET_DEVICE_MESH(_p, _idx)       RTMP_SET_PACKET_IF((_p), ((_idx) + MIN_NET_DEVICE_FOR_MESH))
725 #define RTMP_GET_PACKET_NET_DEVICE_MBSSID(_p)                   RTMP_GET_PACKET_IF((_p))
726 #define RTMP_GET_PACKET_NET_DEVICE(_p)                                  RTMP_GET_PACKET_IF((_p))
727
728 #define RTMP_SET_PACKET_MOREDATA(_p, _morebit)          (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+7] = _morebit)
729 #define RTMP_GET_PACKET_MOREDATA(_p)                            (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+7])
730
731 /* */
732 /*      Sepcific Pakcet Type definition */
733 /* */
734 #define RTMP_PACKET_SPECIFIC_CB_OFFSET  11
735
736 #define RTMP_PACKET_SPECIFIC_DHCP               0x01
737 #define RTMP_PACKET_SPECIFIC_EAPOL              0x02
738 #define RTMP_PACKET_SPECIFIC_IPV4               0x04
739 #define RTMP_PACKET_SPECIFIC_WAI                0x08
740 #define RTMP_PACKET_SPECIFIC_VLAN               0x10
741 #define RTMP_PACKET_SPECIFIC_LLCSNAP    0x20
742
743 /*Specific */
744 #define RTMP_SET_PACKET_SPECIFIC(_p, _flg)              (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11] = _flg)
745
746 /*DHCP */
747 #define RTMP_SET_PACKET_DHCP(_p, _flg)                                                                                                          \
748                         do{                                                                                                                                                             \
749                                 if (_flg)                                                                                                                                       \
750                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) |= (RTMP_PACKET_SPECIFIC_DHCP);             \
751                                 else                                                                                                                                            \
752                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) &= (!RTMP_PACKET_SPECIFIC_DHCP);    \
753                         }while(0)
754 #define RTMP_GET_PACKET_DHCP(_p)                (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11] & RTMP_PACKET_SPECIFIC_DHCP)
755
756 /*EAPOL */
757 #define RTMP_SET_PACKET_EAPOL(_p, _flg)                                                                                                         \
758                         do{                                                                                                                                                             \
759                                 if (_flg)                                                                                                                                       \
760                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) |= (RTMP_PACKET_SPECIFIC_EAPOL);            \
761                                 else                                                                                                                                            \
762                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) &= (!RTMP_PACKET_SPECIFIC_EAPOL);   \
763                         }while(0)
764 #define RTMP_GET_PACKET_EAPOL(_p)               (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11] & RTMP_PACKET_SPECIFIC_EAPOL)
765
766 /*WAI */
767 #define RTMP_SET_PACKET_WAI(_p, _flg)                                                                                                           \
768                         do{                                                                                                                                                             \
769                                 if (_flg)                                                                                                                                       \
770                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) |= (RTMP_PACKET_SPECIFIC_WAI);              \
771                                 else                                                                                                                                            \
772                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) &= (!RTMP_PACKET_SPECIFIC_WAI);     \
773                         }while(0)
774 #define RTMP_GET_PACKET_WAI(_p)         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11] & RTMP_PACKET_SPECIFIC_WAI)
775
776 #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))
777
778 /*VLAN */
779 #define RTMP_SET_PACKET_VLAN(_p, _flg)                                                                                                          \
780                         do{                                                                                                                                                             \
781                                 if (_flg)                                                                                                                                       \
782                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) |= (RTMP_PACKET_SPECIFIC_VLAN);             \
783                                 else                                                                                                                                            \
784                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) &= (!RTMP_PACKET_SPECIFIC_VLAN);    \
785                         }while(0)
786 #define RTMP_GET_PACKET_VLAN(_p)                (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11] & RTMP_PACKET_SPECIFIC_VLAN)
787
788 /*LLC/SNAP */
789 #define RTMP_SET_PACKET_LLCSNAP(_p, _flg)                                                                                                       \
790                         do{                                                                                                                                                             \
791                                 if (_flg)                                                                                                                                       \
792                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) |= (RTMP_PACKET_SPECIFIC_LLCSNAP);          \
793                                 else                                                                                                                                            \
794                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) &= (!RTMP_PACKET_SPECIFIC_LLCSNAP);         \
795                         }while(0)
796
797 #define RTMP_GET_PACKET_LLCSNAP(_p)             (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11] & RTMP_PACKET_SPECIFIC_LLCSNAP)
798
799 /* IP */
800 #define RTMP_SET_PACKET_IPV4(_p, _flg)                                                                                                          \
801                         do{                                                                                                                                                             \
802                                 if (_flg)                                                                                                                                       \
803                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) |= (RTMP_PACKET_SPECIFIC_IPV4);             \
804                                 else                                                                                                                                            \
805                                         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11]) &= (!RTMP_PACKET_SPECIFIC_IPV4);    \
806                         }while(0)
807
808 #define RTMP_GET_PACKET_IPV4(_p)                (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+11] & RTMP_PACKET_SPECIFIC_IPV4)
809
810 /* If this flag is set, it indicates that this EAPoL frame MUST be clear. */
811 #define RTMP_SET_PACKET_CLEAR_EAP_FRAME(_p, _flg)   (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+12] = _flg)
812 #define RTMP_GET_PACKET_CLEAR_EAP_FRAME(_p)         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+12])
813
814 /* use bit3 of cb[CB_OFF+16] */
815
816 #define RTMP_SET_PACKET_5VT(_p, _flg)   (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+22] = _flg)
817 #define RTMP_GET_PACKET_5VT(_p)         (RTPKT_TO_OSPKT(_p)->cb[CB_OFF+22])
818
819 /* Max skb->cb = 48B = [CB_OFF+38] */
820
821 /***********************************************************************************
822  *      Other function prototypes definitions
823  ***********************************************************************************/
824 void RTMP_GetCurrentSystemTime(LARGE_INTEGER * time);
825 int rt28xx_packet_xmit(struct sk_buff *skb);
826
827 #ifdef RTMP_MAC_PCI
828 /* function declarations */
829 #define IRQ_HANDLE_TYPE  irqreturn_t
830
831 IRQ_HANDLE_TYPE rt2860_interrupt(int irq, void *dev_instance);
832 #endif /* RTMP_MAC_PCI // */
833
834 int rt28xx_sta_ioctl(struct net_device *net_dev, IN OUT struct ifreq *rq, int cmd);
835
836 extern int ra_mtd_write(int num, loff_t to, size_t len, const u_char * buf);
837 extern int ra_mtd_read(int num, loff_t from, size_t len, u_char * buf);
838
839 #define GET_PAD_FROM_NET_DEV(_pAd, _net_dev)    (_pAd) = (struct rt_rtmp_adapter *)(_net_dev)->ml_priv;
840
841 #endif /* __RT_LINUX_H__ // */