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