Merge commit 'alsa/master' into for-linus
[pandora-kernel.git] / drivers / staging / rt2870 / rt2870.h
1 /*
2  *************************************************************************
3  * Ralink Tech Inc.
4  * 5F., No.36, Taiyuan St., Jhubei City,
5  * Hsinchu County 302,
6  * Taiwan, R.O.C.
7  *
8  * (c) Copyright 2002-2007, Ralink Technology, Inc.
9  *
10  * This program is free software; you can redistribute it and/or modify  *
11  * it under the terms of the GNU General Public License as published by  *
12  * the Free Software Foundation; either version 2 of the License, or     *
13  * (at your option) any later version.                                   *
14  *                                                                       *
15  * This program is distributed in the hope that it will be useful,       *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  * GNU General Public License for more details.                          *
19  *                                                                       *
20  * You should have received a copy of the GNU General Public License     *
21  * along with this program; if not, write to the                         *
22  * Free Software Foundation, Inc.,                                       *
23  * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
24  *                                                                       *
25  *************************************************************************
26  */
27
28 #ifndef __RT2870_H__
29 #define __RT2870_H__
30
31 //usb header files
32 #include <linux/usb.h>
33
34 /* rtmp_def.h */
35 //
36 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
37 #define BULKAGGRE_ZISE          100
38 #define RT28XX_DRVDATA_SET(_a)                                             usb_set_intfdata(_a, pAd);
39 #define RT28XX_PUT_DEVICE                                                  usb_put_dev
40 #define RTUSB_ALLOC_URB(iso)                                               usb_alloc_urb(iso, GFP_ATOMIC)
41 #define RTUSB_SUBMIT_URB(pUrb)                                             usb_submit_urb(pUrb, GFP_ATOMIC)
42 #define RTUSB_URB_ALLOC_BUFFER(pUsb_Dev, BufSize, pDma_addr)               usb_buffer_alloc(pUsb_Dev, BufSize, GFP_ATOMIC, pDma_addr)
43 #define RTUSB_URB_FREE_BUFFER(pUsb_Dev, BufSize, pTransferBuf, Dma_addr)   usb_buffer_free(pUsb_Dev, BufSize, pTransferBuf, Dma_addr)
44 #else
45 #define BULKAGGRE_ZISE          60
46 #define RT28XX_DRVDATA_SET(_a)
47 #define RT28XX_PUT_DEVICE(dev_p)
48 #define RTUSB_ALLOC_URB(iso)                                               usb_alloc_urb(iso)
49 #define RTUSB_SUBMIT_URB(pUrb)                                             usb_submit_urb(pUrb)
50 #define RTUSB_URB_ALLOC_BUFFER(pUsb_Dev, BufSize, pDma_addr)               kmalloc(BufSize, GFP_ATOMIC)
51 #define RTUSB_URB_FREE_BUFFER(pUsb_Dev, BufSize, pTransferBuf, Dma_addr)   kfree(pTransferBuf)
52 #endif
53
54 #define RXBULKAGGRE_ZISE        12
55 #define MAX_TXBULK_LIMIT        (LOCAL_TXBUF_SIZE*(BULKAGGRE_ZISE-1))
56 #define MAX_TXBULK_SIZE         (LOCAL_TXBUF_SIZE*BULKAGGRE_ZISE)
57 #define MAX_RXBULK_SIZE         (LOCAL_TXBUF_SIZE*RXBULKAGGRE_ZISE)
58 #define MAX_MLME_HANDLER_MEMORY 20
59 #define RETRY_LIMIT             10
60 #define BUFFER_SIZE                             2400    //2048
61 #define TX_RING                                 0xa
62 #define PRIO_RING                               0xc
63
64
65 // Flags for Bulkflags control for bulk out data
66 //
67 #define fRTUSB_BULK_OUT_DATA_NULL                               0x00000001
68 #define fRTUSB_BULK_OUT_RTS                                             0x00000002
69 #define fRTUSB_BULK_OUT_MLME                                    0x00000004
70
71 #define fRTUSB_BULK_OUT_DATA_NORMAL                             0x00010000
72 #define fRTUSB_BULK_OUT_DATA_NORMAL_2                   0x00020000
73 #define fRTUSB_BULK_OUT_DATA_NORMAL_3                   0x00040000
74 #define fRTUSB_BULK_OUT_DATA_NORMAL_4                   0x00080000
75
76 #define fRTUSB_BULK_OUT_PSPOLL                                  0x00000020
77 #define fRTUSB_BULK_OUT_DATA_FRAG                               0x00000040
78 #define fRTUSB_BULK_OUT_DATA_FRAG_2                             0x00000080
79 #define fRTUSB_BULK_OUT_DATA_FRAG_3                             0x00000100
80 #define fRTUSB_BULK_OUT_DATA_FRAG_4                             0x00000200
81
82 #ifdef RALINK_ATE
83 #define fRTUSB_BULK_OUT_DATA_ATE                                0x00100000
84 #endif // RALINK_ATE //
85
86 #define RT2870_USB_DEVICES      \
87 {       \
88         {USB_DEVICE(0x148F,0x2770)}, /* Ralink */               \
89         {USB_DEVICE(0x1737,0x0071)}, /* Linksys WUSB600N */     \
90         {USB_DEVICE(0x148F,0x2870)}, /* Ralink */               \
91         {USB_DEVICE(0x148F,0x3070)}, /* Ralink */               \
92         {USB_DEVICE(0x0B05,0x1731)}, /* Asus */                 \
93         {USB_DEVICE(0x0B05,0x1732)}, /* Asus */                 \
94         {USB_DEVICE(0x0B05,0x1742)}, /* Asus */                 \
95         {USB_DEVICE(0x0DF6,0x0017)}, /* Sitecom */              \
96         {USB_DEVICE(0x0DF6,0x002B)}, /* Sitecom */              \
97         {USB_DEVICE(0x0DF6,0x002C)}, /* Sitecom */              \
98         {USB_DEVICE(0x0DF6,0x002D)}, /* Sitecom */              \
99         {USB_DEVICE(0x0DF6,0x0039)}, /* Sitecom */              \
100         {USB_DEVICE(0x14B2,0x3C06)}, /* Conceptronic */         \
101         {USB_DEVICE(0x14B2,0x3C28)}, /* Conceptronic */         \
102         {USB_DEVICE(0x2019,0xED06)}, /* Planex Communications, Inc. */          \
103         {USB_DEVICE(0x2019,0xAB25)}, /* Planex Communications, Inc. RT3070 */           \
104         {USB_DEVICE(0x07D1,0x3C09)}, /* D-Link */               \
105         {USB_DEVICE(0x07D1,0x3C11)}, /* D-Link */               \
106         {USB_DEVICE(0x14B2,0x3C07)}, /* AL */                   \
107         {USB_DEVICE(0x14B2,0x3C12)}, /* AL */           \
108         {USB_DEVICE(0x050D,0x8053)}, /* Belkin */               \
109         {USB_DEVICE(0x14B2,0x3C23)}, /* Airlink */              \
110         {USB_DEVICE(0x14B2,0x3C27)}, /* Airlink */              \
111         {USB_DEVICE(0x07AA,0x002F)}, /* Corega */               \
112         {USB_DEVICE(0x07AA,0x003C)}, /* Corega */               \
113         {USB_DEVICE(0x07AA,0x003F)}, /* Corega */               \
114         {USB_DEVICE(0x18C5,0x0012)}, /* Corega */               \
115         {USB_DEVICE(0x1044,0x800B)}, /* Gigabyte */             \
116         {USB_DEVICE(0x15A9,0x0006)}, /* Sparklan */             \
117         {USB_DEVICE(0x083A,0xB522)}, /* SMC */                  \
118         {USB_DEVICE(0x083A,0xA618)}, /* SMC */                  \
119         {USB_DEVICE(0x083A,0x7522)}, /* Arcadyan */             \
120         {USB_DEVICE(0x0CDE,0x0022)}, /* ZCOM */                 \
121         {USB_DEVICE(0x0586,0x3416)}, /* Zyxel */                \
122         {USB_DEVICE(0x0CDE,0x0025)}, /* Zyxel */                \
123         {USB_DEVICE(0x1740,0x9701)}, /* EnGenius */             \
124         {USB_DEVICE(0x1740,0x9702)}, /* EnGenius */             \
125         {USB_DEVICE(0x0471,0x200f)}, /* Philips */              \
126         {USB_DEVICE(0x14B2,0x3C25)}, /* Draytek */              \
127         {USB_DEVICE(0x13D3,0x3247)}, /* AzureWave */    \
128         {USB_DEVICE(0x083A,0x6618)}, /* Accton */               \
129         {USB_DEVICE(0x15c5,0x0008)}, /* Amit */                 \
130         {USB_DEVICE(0x0E66,0x0001)}, /* Hawking */              \
131         {USB_DEVICE(0x0E66,0x0003)}, /* Hawking */              \
132         {USB_DEVICE(0x129B,0x1828)}, /* Siemens */              \
133         {USB_DEVICE(0x157E,0x300E)},    /* U-Media */   \
134         {USB_DEVICE(0x050d,0x805c)},                                    \
135         {USB_DEVICE(0x1482,0x3C09)}, /* Abocom*/                \
136         {USB_DEVICE(0x14B2,0x3C09)}, /* Alpha */                \
137         {USB_DEVICE(0x04E8,0x2018)}, /* samsung */      \
138         {USB_DEVICE(0x07B8,0x3070)}, /* AboCom */               \
139         {USB_DEVICE(0x07B8,0x3071)}, /* AboCom */               \
140         {USB_DEVICE(0x07B8,0x2870)}, /* AboCom */               \
141         {USB_DEVICE(0x07B8,0x2770)}, /* AboCom */               \
142         {USB_DEVICE(0x7392,0x7711)}, /* Edimax */               \
143         {USB_DEVICE(0x5A57,0x0280)}, /* Zinwell */              \
144         {USB_DEVICE(0x5A57,0x0282)}, /* Zinwell */              \
145         {USB_DEVICE(0x0789,0x0162)}, /* Logitec */              \
146         {USB_DEVICE(0x0789,0x0163)}, /* Logitec */              \
147         {USB_DEVICE(0x0789,0x0164)}, /* Logitec */              \
148         { }/* Terminating entry */                      \
149 }
150
151 #define FREE_HTTX_RING(_p, _b, _t)                      \
152 {                                                                               \
153         if ((_t)->ENextBulkOutPosition == (_t)->CurWritePosition)                               \
154         {                                                                                                                                       \
155                 (_t)->bRingEmpty = TRUE;                        \
156         }                                                                                                                                       \
157         /*NdisInterlockedDecrement(&(_p)->TxCount); */\
158 }
159
160 //
161 // RXINFO appends at the end of each rx packet.
162 //
163 #ifdef RT_BIG_ENDIAN
164 typedef struct  PACKED _RXINFO_STRUC {
165         UINT32          PlcpSignal:12;
166         UINT32          LastAMSDU:1;
167         UINT32          CipherAlg:1;
168         UINT32          PlcpRssil:1;
169         UINT32          Decrypted:1;
170         UINT32          AMPDU:1;                // To be moved
171         UINT32          L2PAD:1;
172         UINT32          RSSI:1;
173         UINT32          HTC:1;
174         UINT32          AMSDU:1;                // rx with 802.3 header, not 802.11 header.
175         UINT32          CipherErr:2;        // 0: decryption okay, 1:ICV error, 2:MIC error, 3:KEY not valid
176         UINT32          Crc:1;              // 1: CRC error
177         UINT32          MyBss:1;        // 1: this frame belongs to the same BSSID
178         UINT32          Bcast:1;            // 1: this is a broadcast frame
179         UINT32          Mcast:1;            // 1: this is a multicast frame
180         UINT32          U2M:1;              // 1: this RX frame is unicast to me
181         UINT32          FRAG:1;
182         UINT32          NULLDATA:1;
183         UINT32          DATA:1;
184         UINT32          BA:1;
185 }       RXINFO_STRUC, *PRXINFO_STRUC, RT28XX_RXD_STRUC, *PRT28XX_RXD_STRUC;
186 #else
187 typedef struct  PACKED _RXINFO_STRUC {
188         UINT32          BA:1;
189         UINT32          DATA:1;
190         UINT32          NULLDATA:1;
191         UINT32          FRAG:1;
192         UINT32          U2M:1;              // 1: this RX frame is unicast to me
193         UINT32          Mcast:1;            // 1: this is a multicast frame
194         UINT32          Bcast:1;            // 1: this is a broadcast frame
195         UINT32          MyBss:1;        // 1: this frame belongs to the same BSSID
196         UINT32          Crc:1;              // 1: CRC error
197         UINT32          CipherErr:2;        // 0: decryption okay, 1:ICV error, 2:MIC error, 3:KEY not valid
198         UINT32          AMSDU:1;                // rx with 802.3 header, not 802.11 header.
199         UINT32          HTC:1;
200         UINT32          RSSI:1;
201         UINT32          L2PAD:1;
202         UINT32          AMPDU:1;                // To be moved
203         UINT32          Decrypted:1;
204         UINT32          PlcpRssil:1;
205         UINT32          CipherAlg:1;
206         UINT32          LastAMSDU:1;
207         UINT32          PlcpSignal:12;
208 }       RXINFO_STRUC, *PRXINFO_STRUC, RT28XX_RXD_STRUC, *PRT28XX_RXD_STRUC;
209 #endif
210
211
212 //
213 // TXINFO
214 //
215 #ifdef RT_BIG_ENDIAN
216 typedef struct  _TXINFO_STRUC {
217         // Word 0
218         UINT32          USBDMATxburst:1;//used ONLY in USB bulk Aggre. Force USB DMA transmit frame from current selected endpoint
219         UINT32          USBDMANextVLD:1;        //used ONLY in USB bulk Aggregation, NextValid
220         UINT32          rsv2:2;  // Software use.
221         UINT32          SwUseLastRound:1; // Software use.
222         UINT32          QSEL:2; // select on-chip FIFO ID for 2nd-stage output scheduler.0:MGMT, 1:HCCA 2:EDCA
223         UINT32          WIV:1;  // Wireless Info Valid. 1 if Driver already fill WI,  o if DMA needs to copy WI to correctposition
224         UINT32          rsv:8;
225         UINT32          USBDMATxPktLen:16;      //used ONLY in USB bulk Aggregation,  Total byte counts of all sub-frame.
226 }       TXINFO_STRUC, *PTXINFO_STRUC;
227 #else
228 typedef struct  _TXINFO_STRUC {
229         // Word 0
230         UINT32          USBDMATxPktLen:16;      //used ONLY in USB bulk Aggregation,  Total byte counts of all sub-frame.
231         UINT32          rsv:8;
232         UINT32          WIV:1;  // Wireless Info Valid. 1 if Driver already fill WI,  o if DMA needs to copy WI to correctposition
233         UINT32          QSEL:2; // select on-chip FIFO ID for 2nd-stage output scheduler.0:MGMT, 1:HCCA 2:EDCA
234         UINT32          SwUseLastRound:1; // Software use.
235         UINT32          rsv2:2;  // Software use.
236         UINT32          USBDMANextVLD:1;        //used ONLY in USB bulk Aggregation, NextValid
237         UINT32          USBDMATxburst:1;//used ONLY in USB bulk Aggre. Force USB DMA transmit frame from current selected endpoint
238 }       TXINFO_STRUC, *PTXINFO_STRUC;
239 #endif
240
241 #define TXINFO_SIZE                             4
242 #define RXINFO_SIZE                             4
243 #define TXPADDING_SIZE                  11
244
245 //
246 // Management ring buffer format
247 //
248 typedef struct  _MGMT_STRUC     {
249         BOOLEAN         Valid;
250         PUCHAR          pBuffer;
251         ULONG           Length;
252 }       MGMT_STRUC, *PMGMT_STRUC;
253
254
255 /* ----------------- EEPROM Related MACRO ----------------- */
256 #define RT28xx_EEPROM_READ16(pAd, offset, var)                                  \
257         do {                                                                                                            \
258                 RTUSBReadEEPROM(pAd, offset, (PUCHAR)&(var), 2);                \
259                 var = le2cpu16(var);                                                                    \
260         }while(0)
261
262 #define RT28xx_EEPROM_WRITE16(pAd, offset, var)                                 \
263         do{                                                                                                                     \
264                 USHORT _tmpVar;                                                                                 \
265                 _tmpVar = cpu2le16(var);                                                                \
266                 RTUSBWriteEEPROM(pAd, offset, (PUCHAR)&(_tmpVar), 2);   \
267         }while(0)
268
269 /* ----------------- TASK/THREAD Related MACRO ----------------- */
270 #define RT28XX_TASK_THREAD_INIT(pAd, Status)            \
271         Status = CreateThreads(net_dev);
272
273
274 /* ----------------- Frimware Related MACRO ----------------- */
275 #if 0
276 #define RT28XX_FIRMUD_INIT(pAd)         \
277         {       UINT32  MacReg;                         \
278                 RTUSBReadMACRegister(pAd, MAC_CSR0, &MacReg); }
279
280 #define RT28XX_FIRMUD_END(pAd)  \
281         RTUSBWriteMACRegister(pAd, 0x7014, 0xffffffff); \
282         RTUSBWriteMACRegister(pAd, 0x701c, 0xffffffff); \
283         RTUSBFirmwareRun(pAd);
284 #else
285 #define RT28XX_WRITE_FIRMWARE(_pAd, _pFwImage, _FwLen)          \
286         RTUSBFirmwareWrite(_pAd, _pFwImage, _FwLen)
287 #endif
288
289 /* ----------------- TX Related MACRO ----------------- */
290 #define RT28XX_START_DEQUEUE(pAd, QueIdx, irqFlags)                             \
291                         {                                                                                                       \
292                                 RTMP_IRQ_LOCK(&pAd->DeQueueLock[QueIdx], irqFlags);             \
293                                 if (pAd->DeQueueRunning[QueIdx])                                                \
294                                 {                                                                                                               \
295                                         RTMP_IRQ_UNLOCK(&pAd->DeQueueLock[QueIdx], irqFlags);\
296                                         printk("DeQueueRunning[%d]= TRUE!\n", QueIdx);          \
297                                         continue;                                                                                       \
298                                 }                                                                                                               \
299                                 else                                                                                                    \
300                                 {                                                                                                               \
301                                         pAd->DeQueueRunning[QueIdx] = TRUE;                                     \
302                                         RTMP_IRQ_UNLOCK(&pAd->DeQueueLock[QueIdx], irqFlags);\
303                                 }                                                                                                               \
304                         }
305 #define RT28XX_STOP_DEQUEUE(pAd, QueIdx, irqFlags)                                              \
306                         do{                                                                                                                     \
307                                 RTMP_IRQ_LOCK(&pAd->DeQueueLock[QueIdx], irqFlags);             \
308                                 pAd->DeQueueRunning[QueIdx] = FALSE;                                    \
309                                 RTMP_IRQ_UNLOCK(&pAd->DeQueueLock[QueIdx], irqFlags);   \
310                         }while(0)
311
312
313 #define RT28XX_HAS_ENOUGH_FREE_DESC(pAd, pTxBlk, freeNum, pPacket) \
314                 (RTUSBFreeDescriptorRequest(pAd, pTxBlk->QueIdx, (pTxBlk->TotalFrameLen + GET_OS_PKT_LEN(pPacket))) == NDIS_STATUS_SUCCESS)
315
316 #define RT28XX_RELEASE_DESC_RESOURCE(pAd, QueIdx)                       \
317                 do{}while(0)
318
319 #define NEED_QUEUE_BACK_FOR_AGG(_pAd, _QueIdx, _freeNum, _TxFrameType)          \
320                 ((_TxFrameType == TX_RALINK_FRAME) && (RTUSBNeedQueueBackForAgg(_pAd, _QueIdx)))
321
322
323
324 #define fRTMP_ADAPTER_NEED_STOP_TX              \
325                 (fRTMP_ADAPTER_NIC_NOT_EXIST | fRTMP_ADAPTER_HALT_IN_PROGRESS | \
326                  fRTMP_ADAPTER_RESET_IN_PROGRESS | fRTMP_ADAPTER_BULKOUT_RESET | \
327                  fRTMP_ADAPTER_RADIO_OFF | fRTMP_ADAPTER_REMOVE_IN_PROGRESS)
328
329
330 #define HAL_WriteSubTxResource(pAd, pTxBlk, bIsLast, pFreeNumber)       \
331                         RtmpUSB_WriteSubTxResource(pAd, pTxBlk, bIsLast, pFreeNumber)
332
333 #define HAL_WriteTxResource(pAd, pTxBlk,bIsLast, pFreeNumber)   \
334                         RtmpUSB_WriteSingleTxResource(pAd, pTxBlk,bIsLast, pFreeNumber)
335
336 #define HAL_WriteFragTxResource(pAd, pTxBlk, fragNum, pFreeNumber) \
337                         RtmpUSB_WriteFragTxResource(pAd, pTxBlk, fragNum, pFreeNumber)
338
339 #define HAL_WriteMultiTxResource(pAd, pTxBlk,frameNum, pFreeNumber)     \
340                         RtmpUSB_WriteMultiTxResource(pAd, pTxBlk,frameNum, pFreeNumber)
341
342 #define HAL_FinalWriteTxResource(pAd, pTxBlk, totalMPDUSize, TxIdx)     \
343                         RtmpUSB_FinalWriteTxResource(pAd, pTxBlk, totalMPDUSize, TxIdx)
344
345 #define HAL_LastTxIdx(pAd, QueIdx,TxIdx) \
346                         /*RtmpUSBDataLastTxIdx(pAd, QueIdx,TxIdx)*/
347
348 #define HAL_KickOutTx(pAd, pTxBlk, QueIdx)      \
349                         RtmpUSBDataKickOut(pAd, pTxBlk, QueIdx)
350
351
352 #define HAL_KickOutMgmtTx(pAd, QueIdx, pPacket, pSrcBufVA, SrcBufLen)   \
353                         RtmpUSBMgmtKickOut(pAd, QueIdx, pPacket, pSrcBufVA, SrcBufLen)
354
355 #define HAL_KickOutNullFrameTx(_pAd, _QueIdx, _pNullFrame, _frameLen)   \
356                         RtmpUSBNullFrameKickOut(_pAd, _QueIdx, _pNullFrame, _frameLen)
357
358 #define RTMP_PKT_TAIL_PADDING   11 // 3(max 4 byte padding) + 4 (last packet padding) + 4 (MaxBulkOutsize align padding)
359
360 extern UCHAR EpToQueue[6];
361
362
363 #ifdef RT2870
364 #define GET_TXRING_FREENO(_pAd, _QueIdx)        (_QueIdx) //(_pAd->TxRing[_QueIdx].TxSwFreeIdx)
365 #define GET_MGMTRING_FREENO(_pAd)                       (_pAd->MgmtRing.TxSwFreeIdx)
366 #endif // RT2870 //
367
368
369 /* ----------------- RX Related MACRO ----------------- */
370 //#define RT28XX_RX_ERROR_CHECK                         RTMPCheckRxWI
371
372 #if 0
373 #define RT28XX_RCV_INIT(pAd)                                    \
374         pAd->TransferBufferLength = 0;                          \
375         pAd->ReadPosition = 0;                                          \
376         pAd->pCurrRxContext = NULL;
377 #endif
378
379 #define RT28XX_RV_ALL_BUF_END(bBulkReceive)             \
380         /* We return STATUS_MORE_PROCESSING_REQUIRED so that the completion */  \
381         /* routine (IofCompleteRequest) will stop working on the irp. */                \
382         if (bBulkReceive == TRUE)       RTUSBBulkReceive(pAd);
383
384
385 /* ----------------- ASIC Related MACRO ----------------- */
386 #if 0
387 #define RT28XX_DMA_WRITE_INIT(GloCfg)                   \
388         {       GloCfg.field.EnTXWriteBackDDONE = 1;    \
389                 GloCfg.field.EnableRxDMA = 1;                   \
390                 GloCfg.field.EnableTxDMA = 1; }
391
392 #define RT28XX_DMA_POST_WRITE(_pAd)                             \
393         do{     USB_DMA_CFG_STRUC       UsbCfg;                         \
394                 UsbCfg.word = 0;                                                \
395                 /* for last packet, PBF might use more than limited, so minus 2 to prevent from error */ \
396                 UsbCfg.field.RxBulkAggLmt = (MAX_RXBULK_SIZE /1024)-3;  \
397                 UsbCfg.field.phyclear = 0;                                                              \
398                 /* usb version is 1.1,do not use bulk in aggregation */ \
399                 if (_pAd->BulkInMaxPacketSize == 512)                                   \
400                         UsbCfg.field.RxBulkAggEn = 1;                                           \
401                 UsbCfg.field.RxBulkEn = 1;                                                              \
402                 UsbCfg.field.TxBulkEn = 1;                                                              \
403                 UsbCfg.field.RxBulkAggTOut = 0x80; /* 2006-10-18 */             \
404                 RTUSBWriteMACRegister(_pAd, USB_DMA_CFG, UsbCfg.word);  \
405         }while(0)
406 #endif
407
408 // reset MAC of a station entry to 0xFFFFFFFFFFFF
409 #define RT28XX_STA_ENTRY_MAC_RESET(pAd, Wcid)                                   \
410         {       RT_SET_ASIC_WCID        SetAsicWcid;                                            \
411                 SetAsicWcid.WCID = Wcid;                                                                \
412                 SetAsicWcid.SetTid = 0xffffffff;                                                \
413                 SetAsicWcid.DeleteTid = 0xffffffff;                                             \
414                 RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_SET_ASIC_WCID,   \
415                                 &SetAsicWcid, sizeof(RT_SET_ASIC_WCID));        }
416
417 // add this entry into ASIC RX WCID search table
418 #define RT28XX_STA_ENTRY_ADD(pAd, pEntry)                                                       \
419         RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_SET_CLIENT_MAC_ENTRY,    \
420                                                         pEntry, sizeof(MAC_TABLE_ENTRY));
421
422 // remove Pair-wise key material from ASIC
423 // yet implement
424 #define RT28XX_STA_ENTRY_KEY_DEL(pAd, BssIdx, Wcid)
425
426 // add Client security information into ASIC WCID table and IVEIV table
427 #define RT28XX_STA_SECURITY_INFO_ADD(pAd, apidx, KeyID, pEntry)                                         \
428         {       RT28XX_STA_ENTRY_MAC_RESET(pAd, pEntry->Aid);                                                           \
429                 if (pEntry->Aid >= 1) {                                                                                                         \
430                         RT_SET_ASIC_WCID_ATTRI  SetAsicWcidAttri;                                                               \
431                         SetAsicWcidAttri.WCID = pEntry->Aid;                                                                    \
432                         if ((pEntry->AuthMode <= Ndis802_11AuthModeAutoSwitch) &&                               \
433                                 (pEntry->WepStatus == Ndis802_11Encryption1Enabled))                            \
434                         {                                                                                                                                               \
435                                 SetAsicWcidAttri.Cipher = pAd->SharedKey[apidx][KeyID].CipherAlg;       \
436                         }                                                                                                                                               \
437                         else if (pEntry->AuthMode == Ndis802_11AuthModeWPANone)                                 \
438                         {                                                                                                                                               \
439                                 SetAsicWcidAttri.Cipher = pAd->SharedKey[apidx][KeyID].CipherAlg;       \
440                         }                                                                                                                                               \
441                         else SetAsicWcidAttri.Cipher = 0;                                                                               \
442             DBGPRINT(RT_DEBUG_TRACE, ("aid cipher = %ld\n",SetAsicWcidAttri.Cipher));       \
443                         RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_SET_ASIC_WCID_CIPHER,                    \
444                                                         &SetAsicWcidAttri, sizeof(RT_SET_ASIC_WCID_ATTRI)); } }
445
446 // Insert the BA bitmap to ASIC for the Wcid entry
447 #define RT28XX_ADD_BA_SESSION_TO_ASIC(_pAd, _Aid, _TID)                                 \
448                 do{                                                                                                                             \
449                         RT_SET_ASIC_WCID        SetAsicWcid;                                                    \
450                         SetAsicWcid.WCID = (_Aid);                                                                      \
451                         SetAsicWcid.SetTid = (0x10000<<(_TID));                                         \
452                         SetAsicWcid.DeleteTid = 0xffffffff;                                                     \
453                         RTUSBEnqueueInternalCmd((_pAd), CMDTHREAD_SET_ASIC_WCID, &SetAsicWcid, sizeof(RT_SET_ASIC_WCID));       \
454                 }while(0)
455
456 // Remove the BA bitmap from ASIC for the Wcid entry
457 #define RT28XX_DEL_BA_SESSION_FROM_ASIC(_pAd, _Wcid, _TID)                              \
458                 do{                                                                                                                             \
459                         RT_SET_ASIC_WCID        SetAsicWcid;                                                    \
460                         SetAsicWcid.WCID = (_Wcid);                                                                     \
461                         SetAsicWcid.SetTid = (0xffffffff);                                                      \
462                         SetAsicWcid.DeleteTid = (0x10000<<(_TID) );                                     \
463                         RTUSBEnqueueInternalCmd((_pAd), CMDTHREAD_SET_ASIC_WCID, &SetAsicWcid, sizeof(RT_SET_ASIC_WCID));       \
464                 }while(0)
465
466
467 /* ----------------- PCI/USB Related MACRO ----------------- */
468 #define RT28XX_HANDLE_DEV_ASSIGN(handle, dev_p)                 \
469         ((POS_COOKIE)handle)->pUsb_Dev = dev_p;
470
471 // no use
472 #define RT28XX_UNMAP()
473 #define RT28XX_IRQ_REQUEST(net_dev)
474 #define RT28XX_IRQ_RELEASE(net_dev)
475 #define RT28XX_IRQ_INIT(pAd)
476 #define RT28XX_IRQ_ENABLE(pAd)
477
478
479 /* ----------------- MLME Related MACRO ----------------- */
480 #define RT28XX_MLME_HANDLER(pAd)                        RTUSBMlmeUp(pAd)
481
482 #define RT28XX_MLME_PRE_SANITY_CHECK(pAd)                                                               \
483         {       if ((pAd->CommonCfg.bHardwareRadio == TRUE) &&                                  \
484                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)) &&          \
485                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS))) {       \
486                         RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_CHECK_GPIO, NULL, 0); } }
487
488 #define RT28XX_MLME_STA_QUICK_RSP_WAKE_UP(pAd)  \
489         {       RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_QKERIODIC_EXECUT, NULL, 0);      \
490                 RTUSBMlmeUp(pAd); }
491
492 #define RT28XX_MLME_RESET_STATE_MACHINE(pAd)    \
493                         MlmeEnqueue(pAd, MLME_CNTL_STATE_MACHINE, MT2_RESET_CONF, 0, NULL);     \
494                         RTUSBMlmeUp(pAd);
495
496 #define RT28XX_HANDLE_COUNTER_MEASURE(_pAd, _pEntry)            \
497         {       RTUSBEnqueueInternalCmd(_pAd, CMDTHREAD_802_11_COUNTER_MEASURE, _pEntry, sizeof(MAC_TABLE_ENTRY));      \
498                 RTUSBMlmeUp(_pAd);                                                                      \
499         }
500
501
502 /* ----------------- Power Save Related MACRO ----------------- */
503 #define RT28XX_PS_POLL_ENQUEUE(pAd)                                             \
504         {       RTUSB_SET_BULK_FLAG(pAd, fRTUSB_BULK_OUT_PSPOLL);       \
505                 RTUSBKickBulkOut(pAd); }
506
507 #define RT28xx_CHIP_NAME            "RT2870"
508 #define USB_CYC_CFG                 0x02a4
509 #define STATUS_SUCCESS                          0x00
510 #define STATUS_UNSUCCESSFUL             0x01
511 #define NT_SUCCESS(status)                      (((status) > 0) ? (1):(0))
512 #define InterlockedIncrement            atomic_inc
513 #define NdisInterlockedIncrement        atomic_inc
514 #define InterlockedDecrement            atomic_dec
515 #define NdisInterlockedDecrement        atomic_dec
516 #define InterlockedExchange                     atomic_set
517 //#define NdisMSendComplete                     RTMP_SendComplete
518 #define NdisMCancelTimer                        RTMPCancelTimer
519 #define NdisAllocMemory(_ptr, _size, _flag)     \
520                                                                         do{_ptr = kmalloc((_size),(_flag));}while(0)
521 #define NdisFreeMemory(a, b, c)         kfree((a))
522 #define NdisMSleep                                      RTMPusecDelay           /* unit: microsecond */
523
524
525 #define USBD_TRANSFER_DIRECTION_OUT             0
526 #define USBD_TRANSFER_DIRECTION_IN              0
527 #define USBD_SHORT_TRANSFER_OK                  0
528 #define PURB                    purbb_t
529
530 #define RTUSB_FREE_URB(pUrb)    usb_free_urb(pUrb)
531
532 //#undef MlmeAllocateMemory
533 //#undef MlmeFreeMemory
534
535 typedef int                             NTSTATUS;
536 typedef struct usb_device       * PUSB_DEV;
537
538 /* MACRO for linux usb */
539 typedef struct urb *purbb_t;
540 typedef struct usb_ctrlrequest devctrlrequest;
541 #define PIRP            PVOID
542 #define PMDL            PVOID
543 #define NDIS_OID        UINT
544 #ifndef USB_ST_NOERROR
545 #define USB_ST_NOERROR     0
546 #endif
547
548 // vendor-specific control operations
549 #define CONTROL_TIMEOUT_JIFFIES ( (100 * HZ) / 1000)
550 #define UNLINK_TIMEOUT_MS               3
551
552 /* unlink urb   */
553 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,7)
554 #define RTUSB_UNLINK_URB(pUrb)          usb_kill_urb(pUrb)
555 #else
556 #define RTUSB_UNLINK_URB(pUrb)          usb_unlink_urb(pUrb)
557 #endif
558
559 // Prototypes of completion funuc.
560 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
561 #define RTUSBBulkOutDataPacketComplete(purb, pt_regs)    RTUSBBulkOutDataPacketComplete(purb)
562 #define RTUSBBulkOutMLMEPacketComplete(pUrb, pt_regs)    RTUSBBulkOutMLMEPacketComplete(pUrb)
563 #define RTUSBBulkOutNullFrameComplete(pUrb, pt_regs)     RTUSBBulkOutNullFrameComplete(pUrb)
564 #define RTUSBBulkOutRTSFrameComplete(pUrb, pt_regs)      RTUSBBulkOutRTSFrameComplete(pUrb)
565 #define RTUSBBulkOutPsPollComplete(pUrb, pt_regs)        RTUSBBulkOutPsPollComplete(pUrb)
566 #define RTUSBBulkRxComplete(pUrb, pt_regs)               RTUSBBulkRxComplete(pUrb)
567 #endif
568
569
570 VOID RTUSBBulkOutDataPacketComplete(purbb_t purb, struct pt_regs *pt_regs);
571 VOID RTUSBBulkOutMLMEPacketComplete(purbb_t pUrb, struct pt_regs *pt_regs);
572 VOID RTUSBBulkOutNullFrameComplete(purbb_t pUrb, struct pt_regs *pt_regs);
573 VOID RTUSBBulkOutRTSFrameComplete(purbb_t pUrb, struct pt_regs *pt_regs);
574 VOID RTUSBBulkOutPsPollComplete(purbb_t pUrb, struct pt_regs *pt_regs);
575 VOID RTUSBBulkRxComplete(purbb_t pUrb, struct pt_regs *pt_regs);
576
577
578 #define RTUSBMlmeUp(pAd)                \
579 {                                                                   \
580         POS_COOKIE pObj = (POS_COOKIE) pAd->OS_Cookie;  \
581         BUG_ON(pObj->MLMEThr_task == NULL);                 \
582         CHECK_PID_LEGALITY(task_pid(pObj->MLMEThr_task))                    \
583         up(&(pAd->mlme_semaphore)); \
584 }
585
586 #define RTUSBCMDUp(pAd)                 \
587 {                                                                           \
588         POS_COOKIE pObj = (POS_COOKIE) pAd->OS_Cookie;  \
589         BUG_ON(pObj->RTUSBCmdThr_task == NULL);     \
590         CHECK_PID_LEGALITY(task_pid(pObj->RTUSBCmdThr_task))        \
591             up(&(pAd->RTUSBCmd_semaphore)); \
592 }
593
594
595 static inline NDIS_STATUS RTMPAllocateMemory(
596         OUT PVOID *ptr,
597         IN size_t size)
598 {
599         *ptr = kmalloc(size, GFP_ATOMIC);
600         if(*ptr)
601                 return NDIS_STATUS_SUCCESS;
602         else
603                 return NDIS_STATUS_RESOURCES;
604 }
605
606 /* rtmp.h */
607 #define BEACON_RING_SIZE                2
608 #define DEVICE_VENDOR_REQUEST_OUT       0x40
609 #define DEVICE_VENDOR_REQUEST_IN        0xc0
610 #define INTERFACE_VENDOR_REQUEST_OUT    0x41
611 #define INTERFACE_VENDOR_REQUEST_IN     0xc1
612 #define MGMTPIPEIDX                                             0       // EP6 is highest priority
613
614 #define BULKOUT_MGMT_RESET_FLAG                         0x80
615
616 #define RTUSB_SET_BULK_FLAG(_M, _F)                             ((_M)->BulkFlags |= (_F))
617 #define RTUSB_CLEAR_BULK_FLAG(_M, _F)                   ((_M)->BulkFlags &= ~(_F))
618 #define RTUSB_TEST_BULK_FLAG(_M, _F)                    (((_M)->BulkFlags & (_F)) != 0)
619
620 #define EnqueueCmd(cmdq, cmdqelmt)              \
621 {                                                                               \
622         if (cmdq->size == 0)                            \
623                 cmdq->head = cmdqelmt;                  \
624         else                                                            \
625                 cmdq->tail->next = cmdqelmt;    \
626         cmdq->tail = cmdqelmt;                          \
627         cmdqelmt->next = NULL;                          \
628         cmdq->size++;                                           \
629 }
630
631 typedef struct   _RT_SET_ASIC_WCID {
632         ULONG WCID;          // mechanism for rekeying: 0:disable, 1: time-based, 2: packet-based
633         ULONG SetTid;        // time-based: seconds, packet-based: kilo-packets
634         ULONG DeleteTid;        // time-based: seconds, packet-based: kilo-packets
635         UCHAR Addr[MAC_ADDR_LEN];       // avoid in interrupt when write key
636 } RT_SET_ASIC_WCID,*PRT_SET_ASIC_WCID;
637
638 typedef struct   _RT_SET_ASIC_WCID_ATTRI {
639         ULONG   WCID;          // mechanism for rekeying: 0:disable, 1: time-based, 2: packet-based
640         ULONG   Cipher;        // ASIC Cipher definition
641         UCHAR   Addr[ETH_LENGTH_OF_ADDRESS];
642 } RT_SET_ASIC_WCID_ATTRI,*PRT_SET_ASIC_WCID_ATTRI;
643
644 typedef struct _MLME_MEMORY_STRUCT {
645         PVOID                           AllocVa;    //Pointer to the base virtual address of the allocated memory
646         struct _MLME_MEMORY_STRUCT      *Next;      //Pointer to the next virtual address of the allocated memory
647 }   MLME_MEMORY_STRUCT, *PMLME_MEMORY_STRUCT;
648
649 typedef struct  _MLME_MEMORY_HANDLER {
650         BOOLEAN                 MemRunning;         //The flag of the Mlme memory handler's status
651         UINT                    MemoryCount;        //Total nonpaged system-space memory not size
652         UINT                    InUseCount;         //Nonpaged system-space memory in used counts
653         UINT                    UnUseCount;         //Nonpaged system-space memory available counts
654         INT                    PendingCount;       //Nonpaged system-space memory for free counts
655         PMLME_MEMORY_STRUCT     pInUseHead;         //Pointer to the first nonpaed memory not used
656         PMLME_MEMORY_STRUCT     pInUseTail;         //Pointer to the last nonpaged memory not used
657         PMLME_MEMORY_STRUCT     pUnUseHead;         //Pointer to the first nonpaged memory in used
658         PMLME_MEMORY_STRUCT     pUnUseTail;         //Pointer to the last nonpaged memory in used
659         PULONG                  MemFreePending[MAX_MLME_HANDLER_MEMORY];   //an array to keep pending free-memory's pointer (32bits)
660 }   MLME_MEMORY_HANDLER, *PMLME_MEMORY_HANDLER;
661
662 typedef struct _CmdQElmt        {
663         UINT                            command;
664         PVOID                           buffer;
665         ULONG                           bufferlength;
666         BOOLEAN                         CmdFromNdis;
667         BOOLEAN                         SetOperation;
668         struct _CmdQElmt        *next;
669 }       CmdQElmt, *PCmdQElmt;
670
671 typedef struct  _CmdQ   {
672         UINT            size;
673         CmdQElmt        *head;
674         CmdQElmt        *tail;
675         UINT32          CmdQState;
676 }CmdQ, *PCmdQ;
677
678 //
679 // For WPA SUPPLICANT: WIRELESS EXT support wireless events: v14 or newer
680 //
681 #if WIRELESS_EXT >= 14
682 //#define WPA_SUPPLICANT_SUPPORT  1
683 #endif
684
685 /* oid.h */
686 // Cipher suite type for mixed mode group cipher, P802.11i-2004
687 typedef enum _RT_802_11_CIPHER_SUITE_TYPE {
688         Cipher_Type_NONE,
689         Cipher_Type_WEP40,
690         Cipher_Type_TKIP,
691         Cipher_Type_RSVD,
692         Cipher_Type_CCMP,
693         Cipher_Type_WEP104
694 } RT_802_11_CIPHER_SUITE_TYPE, *PRT_802_11_CIPHER_SUITE_TYPE;
695
696 //CMDTHREAD_MULTI_READ_MAC
697 //CMDTHREAD_MULTI_WRITE_MAC
698 //CMDTHREAD_VENDOR_EEPROM_READ
699 //CMDTHREAD_VENDOR_EEPROM_WRITE
700 typedef struct  _CMDHandler_TLV {
701         USHORT          Offset;
702         USHORT          Length;
703         UCHAR           DataFirst;
704 }       CMDHandler_TLV, *PCMDHandler_TLV;
705
706 // New for MeetingHouse Api support
707 #define CMDTHREAD_VENDOR_RESET                      0x0D730101  // cmd
708 #define CMDTHREAD_VENDOR_UNPLUG                     0x0D730102  // cmd
709 #define CMDTHREAD_VENDOR_SWITCH_FUNCTION            0x0D730103  // cmd
710 #define CMDTHREAD_MULTI_WRITE_MAC                   0x0D730107  // cmd
711 #define CMDTHREAD_MULTI_READ_MAC                    0x0D730108  // cmd
712 #define CMDTHREAD_VENDOR_EEPROM_WRITE               0x0D73010A  // cmd
713 #define CMDTHREAD_VENDOR_EEPROM_READ                0x0D73010B  // cmd
714 #define CMDTHREAD_VENDOR_ENTER_TESTMODE             0x0D73010C  // cmd
715 #define CMDTHREAD_VENDOR_EXIT_TESTMODE              0x0D73010D  // cmd
716 #define CMDTHREAD_VENDOR_WRITE_BBP                  0x0D730119  // cmd
717 #define CMDTHREAD_VENDOR_READ_BBP                   0x0D730118  // cmd
718 #define CMDTHREAD_VENDOR_WRITE_RF                   0x0D73011A  // cmd
719 #define CMDTHREAD_VENDOR_FLIP_IQ                    0x0D73011D  // cmd
720 #define CMDTHREAD_RESET_BULK_OUT                    0x0D730210  // cmd
721 #define CMDTHREAD_RESET_BULK_IN                     0x0D730211  // cmd
722 #define CMDTHREAD_SET_PSM_BIT_SAVE                  0x0D730212  // cmd
723 #define CMDTHREAD_SET_RADIO                         0x0D730214  // cmd
724 #define CMDTHREAD_UPDATE_TX_RATE                    0x0D730216  // cmd
725 #define CMDTHREAD_802_11_ADD_KEY_WEP                0x0D730218  // cmd
726 #define CMDTHREAD_RESET_FROM_ERROR                  0x0D73021A  // cmd
727 #define CMDTHREAD_LINK_DOWN                         0x0D73021B  // cmd
728 #define CMDTHREAD_RESET_FROM_NDIS                   0x0D73021C  // cmd
729 #define CMDTHREAD_CHECK_GPIO                        0x0D730215  // cmd
730 #define CMDTHREAD_FORCE_WAKE_UP                     0x0D730222  // cmd
731 #define CMDTHREAD_SET_BW                            0x0D730225  // cmd
732 #define CMDTHREAD_SET_ASIC_WCID                     0x0D730226  // cmd
733 #define CMDTHREAD_SET_ASIC_WCID_CIPHER              0x0D730227  // cmd
734 #define CMDTHREAD_QKERIODIC_EXECUT                  0x0D73023D  // cmd
735 #define RT_CMD_SET_KEY_TABLE                        0x0D730228  // cmd
736 #define RT_CMD_SET_RX_WCID_TABLE                    0x0D730229  // cmd
737 #define CMDTHREAD_SET_CLIENT_MAC_ENTRY              0x0D73023E  // cmd
738 #define CMDTHREAD_802_11_QUERY_HARDWARE_REGISTER    0x0D710105  // cmd
739 #define CMDTHREAD_802_11_SET_PHY_MODE               0x0D79010C  // cmd
740 #define CMDTHREAD_802_11_SET_STA_CONFIG             0x0D790111  // cmd
741 #define CMDTHREAD_802_11_SET_PREAMBLE               0x0D790101  // cmd
742 #define CMDTHREAD_802_11_COUNTER_MEASURE                        0x0D790102      // cmd
743
744
745 #define WPA1AKMBIT          0x01
746 #define WPA2AKMBIT          0x02
747 #define WPA1PSKAKMBIT   0x04
748 #define WPA2PSKAKMBIT   0x08
749 #define TKIPBIT         0x01
750 #define CCMPBIT         0x02
751
752
753 #define RT28XX_STA_FORCE_WAKEUP(pAd, bFromTx) \
754     RT28xxUsbStaAsicForceWakeup(pAd, bFromTx);
755
756 #define RT28XX_STA_SLEEP_THEN_AUTO_WAKEUP(pAd, TbttNumToNextWakeUp) \
757     RT28xxUsbStaAsicSleepThenAutoWakeup(pAd, TbttNumToNextWakeUp);
758
759 #define RT28XX_MLME_RADIO_ON(pAd) \
760     RT28xxUsbMlmeRadioOn(pAd);
761
762 #define RT28XX_MLME_RADIO_OFF(pAd) \
763     RT28xxUsbMlmeRadioOFF(pAd);
764
765 #endif //__RT2870_H__