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