staging: vt6656: clean up BBvUpdatePreEDThreshold
[cascardo/linux.git] / drivers / staging / vt6656 / dpc.c
1 /*
2  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * File: dpc.c
20  *
21  * Purpose: handle dpc rx functions
22  *
23  * Author: Lyndon Chen
24  *
25  * Date: May 20, 2003
26  *
27  * Functions:
28  *      device_receive_frame - Rcv 802.11 frame function
29  *      s_bHandleRxEncryption- Rcv decrypted data via on-fly
30  *      s_byGetRateIdx- get rate index
31  *      s_vGetDASA- get data offset
32  *      s_vProcessRxMACHeader- Rcv 802.11 and translate to 802.3
33  *
34  * Revision History:
35  *
36  */
37
38 #include "dpc.h"
39 #include "device.h"
40 #include "rxtx.h"
41 #include "tether.h"
42 #include "card.h"
43 #include "bssdb.h"
44 #include "mac.h"
45 #include "baseband.h"
46 #include "michael.h"
47 #include "tkip.h"
48 #include "tcrc.h"
49 #include "wctl.h"
50 #include "rf.h"
51 #include "iowpa.h"
52 #include "datarate.h"
53 #include "usbpipe.h"
54
55 //static int          msglevel                =MSG_LEVEL_DEBUG;
56 static int          msglevel                =MSG_LEVEL_INFO;
57
58 static const u8 acbyRxRate[MAX_RATE] =
59 {2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108};
60
61 static u8 s_byGetRateIdx(u8 byRate);
62
63 static
64 void
65 s_vGetDASA(
66       u8 * pbyRxBufferAddr,
67      unsigned int *pcbHeaderSize,
68      struct ethhdr *psEthHeader
69     );
70
71 static void s_vProcessRxMACHeader(struct vnt_private *pDevice,
72         u8 *pbyRxBufferAddr, u32 cbPacketSize, int bIsWEP, int bExtIV,
73         u32 *pcbHeadSize);
74
75 static int s_bHandleRxEncryption(struct vnt_private *pDevice, u8 *pbyFrame,
76         u32 FrameSize, u8 *pbyRsr, u8 *pbyNewRsr, PSKeyItem *pKeyOut,
77         s32 *pbExtIV, u16 *pwRxTSC15_0, u32 *pdwRxTSC47_16);
78
79 /*+
80  *
81  * Description:
82  *    Translate Rcv 802.11 header to 802.3 header with Rx buffer
83  *
84  * Parameters:
85  *  In:
86  *      pDevice
87  *      dwRxBufferAddr  - Address of Rcv Buffer
88  *      cbPacketSize    - Rcv Packet size
89  *      bIsWEP          - If Rcv with WEP
90  *  Out:
91  *      pcbHeaderSize   - 802.11 header size
92  *
93  * Return Value: None
94  *
95 -*/
96
97 static void s_vProcessRxMACHeader(struct vnt_private *pDevice,
98         u8 *pbyRxBufferAddr, u32 cbPacketSize, int bIsWEP, int bExtIV,
99         u32 *pcbHeadSize)
100 {
101         u8 *pbyRxBuffer;
102         u32 cbHeaderSize = 0;
103         u16 *pwType;
104         struct ieee80211_hdr *pMACHeader;
105         int ii;
106
107     pMACHeader = (struct ieee80211_hdr *) (pbyRxBufferAddr + cbHeaderSize);
108
109     s_vGetDASA((u8 *)pMACHeader, &cbHeaderSize, &pDevice->sRxEthHeader);
110
111     if (bIsWEP) {
112         if (bExtIV) {
113             // strip IV&ExtIV , add 8 byte
114             cbHeaderSize += (WLAN_HDR_ADDR3_LEN + 8);
115         } else {
116             // strip IV , add 4 byte
117             cbHeaderSize += (WLAN_HDR_ADDR3_LEN + 4);
118         }
119     }
120     else {
121         cbHeaderSize += WLAN_HDR_ADDR3_LEN;
122     };
123
124     pbyRxBuffer = (u8 *) (pbyRxBufferAddr + cbHeaderSize);
125     if (ether_addr_equal(pbyRxBuffer, pDevice->abySNAP_Bridgetunnel)) {
126         cbHeaderSize += 6;
127     } else if (ether_addr_equal(pbyRxBuffer, pDevice->abySNAP_RFC1042)) {
128         cbHeaderSize += 6;
129         pwType = (u16 *) (pbyRxBufferAddr + cbHeaderSize);
130         if ((*pwType == cpu_to_be16(ETH_P_IPX)) ||
131             (*pwType == cpu_to_le16(0xF380))) {
132                 cbHeaderSize -= 8;
133             pwType = (u16 *) (pbyRxBufferAddr + cbHeaderSize);
134             if (bIsWEP) {
135                 if (bExtIV) {
136                     *pwType = htons(cbPacketSize - WLAN_HDR_ADDR3_LEN - 8);    // 8 is IV&ExtIV
137                 } else {
138                     *pwType = htons(cbPacketSize - WLAN_HDR_ADDR3_LEN - 4);    // 4 is IV
139                 }
140             }
141             else {
142                 *pwType = htons(cbPacketSize - WLAN_HDR_ADDR3_LEN);
143             }
144         }
145     }
146     else {
147         cbHeaderSize -= 2;
148         pwType = (u16 *) (pbyRxBufferAddr + cbHeaderSize);
149         if (bIsWEP) {
150             if (bExtIV) {
151                 *pwType = htons(cbPacketSize - WLAN_HDR_ADDR3_LEN - 8);    // 8 is IV&ExtIV
152             } else {
153                 *pwType = htons(cbPacketSize - WLAN_HDR_ADDR3_LEN - 4);    // 4 is IV
154             }
155         }
156         else {
157             *pwType = htons(cbPacketSize - WLAN_HDR_ADDR3_LEN);
158         }
159     }
160
161     cbHeaderSize -= (ETH_ALEN * 2);
162     pbyRxBuffer = (u8 *) (pbyRxBufferAddr + cbHeaderSize);
163     for (ii = 0; ii < ETH_ALEN; ii++)
164         *pbyRxBuffer++ = pDevice->sRxEthHeader.h_dest[ii];
165     for (ii = 0; ii < ETH_ALEN; ii++)
166         *pbyRxBuffer++ = pDevice->sRxEthHeader.h_source[ii];
167
168     *pcbHeadSize = cbHeaderSize;
169 }
170
171 static u8 s_byGetRateIdx(u8 byRate)
172 {
173     u8    byRateIdx;
174
175     for (byRateIdx = 0; byRateIdx <MAX_RATE ; byRateIdx++) {
176         if (acbyRxRate[byRateIdx%MAX_RATE] == byRate)
177             return byRateIdx;
178     }
179     return 0;
180 }
181
182 static
183 void
184 s_vGetDASA (
185       u8 * pbyRxBufferAddr,
186      unsigned int *pcbHeaderSize,
187      struct ethhdr *psEthHeader
188     )
189 {
190         unsigned int            cbHeaderSize = 0;
191         struct ieee80211_hdr *pMACHeader;
192         int             ii;
193
194         pMACHeader = (struct ieee80211_hdr *) (pbyRxBufferAddr + cbHeaderSize);
195
196         if ((pMACHeader->frame_control & FC_TODS) == 0) {
197                 if (pMACHeader->frame_control & FC_FROMDS) {
198                         for (ii = 0; ii < ETH_ALEN; ii++) {
199                                 psEthHeader->h_dest[ii] =
200                                         pMACHeader->addr1[ii];
201                                 psEthHeader->h_source[ii] =
202                                         pMACHeader->addr3[ii];
203                         }
204                 } else {
205                         /* IBSS mode */
206                         for (ii = 0; ii < ETH_ALEN; ii++) {
207                                 psEthHeader->h_dest[ii] =
208                                         pMACHeader->addr1[ii];
209                                 psEthHeader->h_source[ii] =
210                                         pMACHeader->addr2[ii];
211                         }
212                 }
213         } else {
214                 /* Is AP mode.. */
215                 if (pMACHeader->frame_control & FC_FROMDS) {
216                         for (ii = 0; ii < ETH_ALEN; ii++) {
217                                 psEthHeader->h_dest[ii] =
218                                         pMACHeader->addr3[ii];
219                                 psEthHeader->h_source[ii] =
220                                         pMACHeader->addr4[ii];
221                                 cbHeaderSize += 6;
222                         }
223                 } else {
224                         for (ii = 0; ii < ETH_ALEN; ii++) {
225                                 psEthHeader->h_dest[ii] =
226                                         pMACHeader->addr3[ii];
227                                 psEthHeader->h_source[ii] =
228                                         pMACHeader->addr2[ii];
229                         }
230                 }
231         };
232     *pcbHeaderSize = cbHeaderSize;
233 }
234
235 int RXbBulkInProcessData(struct vnt_private *pDevice, struct vnt_rcb *pRCB,
236         unsigned long BytesToIndicate)
237 {
238         struct net_device_stats *pStats = &pDevice->stats;
239         struct sk_buff *skb;
240         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
241         struct vnt_rx_mgmt *pRxPacket = &pMgmt->sRxPacket;
242         struct ieee80211_hdr *p802_11Header;
243         u8 *pbyRsr, *pbyNewRsr, *pbyRSSI, *pbyFrame;
244         u64 *pqwTSFTime;
245         u32 bDeFragRx = false;
246         u32 cbHeaderOffset, cbIVOffset;
247         u32 FrameSize;
248         u16 wEtherType = 0;
249         s32 iSANodeIndex = -1;
250         int ii;
251         u8 *pbyRxSts, *pbyRxRate, *pbySQ, *pby3SQ;
252         u32 cbHeaderSize;
253         PSKeyItem pKey = NULL;
254         u16 wRxTSC15_0 = 0;
255         u32 dwRxTSC47_16 = 0;
256         /* signed long ldBm = 0; */
257         int bIsWEP = false; int bExtIV = false;
258         u32 dwWbkStatus;
259         struct vnt_rcb *pRCBIndicate = pRCB;
260         u8 *pbyDAddress;
261         u16 *pwPLCP_Length;
262         u8 abyVaildRate[MAX_RATE]
263                 = {2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108};
264         u16 wPLCPwithPadding;
265         struct ieee80211_hdr *pMACHeader;
266         int bRxeapol_key = false;
267
268     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---------- RXbBulkInProcessData---\n");
269
270     skb = pRCB->skb;
271
272         /* [31:16]RcvByteCount ( not include 4-byte Status ) */
273         dwWbkStatus = *((u32 *)(skb->data));
274         FrameSize = dwWbkStatus >> 16;
275         FrameSize += 4;
276
277         if (BytesToIndicate != FrameSize) {
278                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"------- WRONG Length 1\n");
279                 pStats->rx_frame_errors++;
280                 return false;
281         }
282
283     if ((BytesToIndicate > 2372) || (BytesToIndicate <= 40)) {
284         // Frame Size error drop this packet.
285         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "---------- WRONG Length 2\n");
286         pStats->rx_frame_errors++;
287         return false;
288     }
289
290     pbyDAddress = (u8 *)(skb->data);
291     pbyRxSts = pbyDAddress+4;
292     pbyRxRate = pbyDAddress+5;
293
294     //real Frame Size = USBFrameSize -4WbkStatus - 4RxStatus - 8TSF - 4RSR - 4SQ3 - ?Padding
295     //if SQ3 the range is 24~27, if no SQ3 the range is 20~23
296     //real Frame size in PLCPLength field.
297     pwPLCP_Length = (u16 *) (pbyDAddress + 6);
298     //Fix hardware bug => PLCP_Length error
299     if ( ((BytesToIndicate - (*pwPLCP_Length)) > 27) ||
300          ((BytesToIndicate - (*pwPLCP_Length)) < 24) ||
301          (BytesToIndicate < (*pwPLCP_Length)) ) {
302
303         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Wrong PLCP Length %x\n", (int) *pwPLCP_Length);
304         pStats->rx_frame_errors++;
305         return false;
306     }
307     for ( ii=RATE_1M;ii<MAX_RATE;ii++) {
308         if ( *pbyRxRate == abyVaildRate[ii] ) {
309             break;
310         }
311     }
312     if ( ii==MAX_RATE ) {
313         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Wrong RxRate %x\n",(int) *pbyRxRate);
314         return false;
315     }
316
317     wPLCPwithPadding = ( (*pwPLCP_Length / 4) + ( (*pwPLCP_Length % 4) ? 1:0 ) ) *4;
318
319         pqwTSFTime = (u64 *)(pbyDAddress + 8 + wPLCPwithPadding);
320   if(pDevice->byBBType == BB_TYPE_11G)  {
321       pby3SQ = pbyDAddress + 8 + wPLCPwithPadding + 12;
322       pbySQ = pby3SQ;
323     }
324   else {
325    pbySQ = pbyDAddress + 8 + wPLCPwithPadding + 8;
326    pby3SQ = pbySQ;
327   }
328     pbyNewRsr = pbyDAddress + 8 + wPLCPwithPadding + 9;
329     pbyRSSI = pbyDAddress + 8 + wPLCPwithPadding + 10;
330     pbyRsr = pbyDAddress + 8 + wPLCPwithPadding + 11;
331
332     FrameSize = *pwPLCP_Length;
333
334     pbyFrame = pbyDAddress + 8;
335
336     pMACHeader = (struct ieee80211_hdr *) pbyFrame;
337
338 //mike add: to judge if current AP is activated?
339     if ((pMgmt->eCurrMode == WMAC_MODE_STANDBY) ||
340         (pMgmt->eCurrMode == WMAC_MODE_ESS_STA)) {
341        if (pMgmt->sNodeDBTable[0].bActive) {
342          if (ether_addr_equal(pMgmt->abyCurrBSSID, pMACHeader->addr2)) {
343             if (pMgmt->sNodeDBTable[0].uInActiveCount != 0)
344                   pMgmt->sNodeDBTable[0].uInActiveCount = 0;
345            }
346        }
347     }
348
349     if (!is_multicast_ether_addr(pMACHeader->addr1)) {
350         if (WCTLbIsDuplicate(&(pDevice->sDupRxCache), (struct ieee80211_hdr *) pbyFrame)) {
351             return false;
352         }
353
354         if (!ether_addr_equal(pDevice->abyCurrentNetAddr, pMACHeader->addr1)) {
355                 return false;
356         }
357     }
358
359     // Use for TKIP MIC
360     s_vGetDASA(pbyFrame, &cbHeaderSize, &pDevice->sRxEthHeader);
361
362     if (ether_addr_equal((u8 *)pDevice->sRxEthHeader.h_source,
363                          pDevice->abyCurrentNetAddr))
364         return false;
365
366     if ((pMgmt->eCurrMode == WMAC_MODE_ESS_AP) || (pMgmt->eCurrMode == WMAC_MODE_IBSS_STA)) {
367         if (IS_CTL_PSPOLL(pbyFrame) || !IS_TYPE_CONTROL(pbyFrame)) {
368             p802_11Header = (struct ieee80211_hdr *) (pbyFrame);
369             // get SA NodeIndex
370             if (BSSbIsSTAInNodeDB(pDevice, (u8 *)(p802_11Header->addr2), &iSANodeIndex)) {
371                 pMgmt->sNodeDBTable[iSANodeIndex].ulLastRxJiffer = jiffies;
372                 pMgmt->sNodeDBTable[iSANodeIndex].uInActiveCount = 0;
373             }
374         }
375     }
376
377     if (IS_FC_WEP(pbyFrame)) {
378         bool     bRxDecryOK = false;
379
380         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"rx WEP pkt\n");
381         bIsWEP = true;
382
383         bRxDecryOK = s_bHandleRxEncryption(pDevice, pbyFrame, FrameSize,
384                 pbyRsr, pbyNewRsr, &pKey, &bExtIV, &wRxTSC15_0, &dwRxTSC47_16);
385
386         if (bRxDecryOK) {
387             if ((*pbyNewRsr & NEWRSR_DECRYPTOK) == 0) {
388                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ICV Fail\n");
389                 if ( (pMgmt->eAuthenMode == WMAC_AUTH_WPA) ||
390                     (pMgmt->eAuthenMode == WMAC_AUTH_WPAPSK) ||
391                     (pMgmt->eAuthenMode == WMAC_AUTH_WPANONE) ||
392                     (pMgmt->eAuthenMode == WMAC_AUTH_WPA2) ||
393                     (pMgmt->eAuthenMode == WMAC_AUTH_WPA2PSK)) {
394                 }
395                 return false;
396             }
397         } else {
398             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"WEP Func Fail\n");
399             return false;
400         }
401         if ((pKey != NULL) && (pKey->byCipherSuite == KEY_CTL_CCMP))
402             FrameSize -= 8;         // Message Integrity Code
403         else
404             FrameSize -= 4;         // 4 is ICV
405     }
406
407     //
408     // RX OK
409     //
410     /* remove the FCS/CRC length */
411     FrameSize -= ETH_FCS_LEN;
412
413     if ( !(*pbyRsr & (RSR_ADDRBROAD | RSR_ADDRMULTI)) && // unicast address
414         (IS_FRAGMENT_PKT((pbyFrame)))
415         ) {
416         // defragment
417         bDeFragRx = WCTLbHandleFragment(pDevice, (struct ieee80211_hdr *) (pbyFrame), FrameSize, bIsWEP, bExtIV);
418         if (bDeFragRx) {
419             // defrag complete
420             // TODO skb, pbyFrame
421             skb = pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].skb;
422             FrameSize = pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx].cbFrameLength;
423             pbyFrame = skb->data + 8;
424         }
425         else {
426             return false;
427         }
428     }
429
430     //
431     // Management & Control frame Handle
432     //
433     if ((IS_TYPE_DATA((pbyFrame))) == false) {
434         // Handle Control & Manage Frame
435
436         if (IS_TYPE_MGMT((pbyFrame))) {
437             u8 * pbyData1;
438             u8 * pbyData2;
439
440             pRxPacket = &(pRCB->sMngPacket);
441             pRxPacket->p80211Header = (PUWLAN_80211HDR)(pbyFrame);
442             pRxPacket->cbMPDULen = FrameSize;
443             pRxPacket->uRSSI = *pbyRSSI;
444             pRxPacket->bySQ = *pbySQ;
445                 pRxPacket->qwLocalTSF = cpu_to_le64(*pqwTSFTime);
446             if (bIsWEP) {
447                 // strip IV
448                 pbyData1 = WLAN_HDR_A3_DATA_PTR(pbyFrame);
449                 pbyData2 = WLAN_HDR_A3_DATA_PTR(pbyFrame) + 4;
450                 for (ii = 0; ii < (FrameSize - 4); ii++) {
451                     *pbyData1 = *pbyData2;
452                      pbyData1++;
453                      pbyData2++;
454                 }
455             }
456
457             pRxPacket->byRxRate = s_byGetRateIdx(*pbyRxRate);
458
459             if ( *pbyRxSts == 0 ) {
460                 //Discard beacon packet which channel is 0
461                 if ( (WLAN_GET_FC_FSTYPE((pRxPacket->p80211Header->sA3.wFrameCtl)) == WLAN_FSTYPE_BEACON) ||
462                      (WLAN_GET_FC_FSTYPE((pRxPacket->p80211Header->sA3.wFrameCtl)) == WLAN_FSTYPE_PROBERESP) ) {
463                         return false;
464                 }
465             }
466             pRxPacket->byRxChannel = (*pbyRxSts) >> 2;
467
468             //
469             // Insert the RCB in the Recv Mng list
470             //
471             EnqueueRCB(pDevice->FirstRecvMngList, pDevice->LastRecvMngList, pRCBIndicate);
472             pDevice->NumRecvMngList++;
473             if ( bDeFragRx == false) {
474                 pRCB->Ref++;
475             }
476             if (pDevice->bIsRxMngWorkItemQueued == false) {
477                 pDevice->bIsRxMngWorkItemQueued = true;
478                 schedule_work(&pDevice->rx_mng_work_item);
479             }
480
481         }
482         else {
483             // Control Frame
484         };
485         return false;
486     }
487     else {
488         if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
489             //In AP mode, hw only check addr1(BSSID or RA) if equal to local MAC.
490             if ( !(*pbyRsr & RSR_BSSIDOK)) {
491                 if (bDeFragRx) {
492                     if (!device_alloc_frag_buf(pDevice, &pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx])) {
493                         DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc more frag bufs\n",
494                         pDevice->dev->name);
495                     }
496                 }
497                 return false;
498             }
499         }
500         else {
501             // discard DATA packet while not associate || BSSID error
502             if ((pDevice->bLinkPass == false) ||
503                 !(*pbyRsr & RSR_BSSIDOK)) {
504                 if (bDeFragRx) {
505                     if (!device_alloc_frag_buf(pDevice, &pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx])) {
506                         DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc more frag bufs\n",
507                         pDevice->dev->name);
508                     }
509                 }
510                 return false;
511             }
512    //mike add:station mode check eapol-key challenge--->
513           {
514             u8  Protocol_Version;    //802.1x Authentication
515             u8  Packet_Type;           //802.1x Authentication
516             u8  Descriptor_type;
517              u16 Key_info;
518               if (bIsWEP)
519                   cbIVOffset = 8;
520               else
521                   cbIVOffset = 0;
522               wEtherType = (skb->data[cbIVOffset + 8 + 24 + 6] << 8) |
523                           skb->data[cbIVOffset + 8 + 24 + 6 + 1];
524               Protocol_Version = skb->data[cbIVOffset + 8 + 24 + 6 + 1 +1];
525               Packet_Type = skb->data[cbIVOffset + 8 + 24 + 6 + 1 +1+1];
526              if (wEtherType == ETH_P_PAE) {         //Protocol Type in LLC-Header
527                   if(((Protocol_Version==1) ||(Protocol_Version==2)) &&
528                      (Packet_Type==3)) {  //802.1x OR eapol-key challenge frame receive
529                         bRxeapol_key = true;
530                       Descriptor_type = skb->data[cbIVOffset + 8 + 24 + 6 + 1 +1+1+1+2];
531                       Key_info = (skb->data[cbIVOffset + 8 + 24 + 6 + 1 +1+1+1+2+1]<<8) |skb->data[cbIVOffset + 8 + 24 + 6 + 1 +1+1+1+2+2] ;
532                       if(Descriptor_type==2) {    //RSN
533                          //  printk("WPA2_Rx_eapol-key_info<-----:%x\n",Key_info);
534                       }
535                      else  if(Descriptor_type==254) {
536                         //  printk("WPA_Rx_eapol-key_info<-----:%x\n",Key_info);
537                      }
538                   }
539               }
540           }
541     //mike add:station mode check eapol-key challenge<---
542         }
543     }
544
545 // Data frame Handle
546
547     if (pDevice->bEnablePSMode) {
548         if (IS_FC_MOREDATA((pbyFrame))) {
549             if (*pbyRsr & RSR_ADDROK) {
550                 //PSbSendPSPOLL((PSDevice)pDevice);
551             }
552         }
553         else {
554             if (pMgmt->bInTIMWake == true) {
555                 pMgmt->bInTIMWake = false;
556             }
557         }
558     }
559
560     // Now it only supports 802.11g Infrastructure Mode, and support rate must up to 54 Mbps
561     if (pDevice->bDiversityEnable && (FrameSize>50) &&
562         pDevice->op_mode == NL80211_IFTYPE_STATION &&
563        (pDevice->bLinkPass == true)) {
564         BBvAntennaDiversity(pDevice, s_byGetRateIdx(*pbyRxRate), 0);
565     }
566
567     // ++++++++ For BaseBand Algorithm +++++++++++++++
568     pDevice->uCurrRSSI = *pbyRSSI;
569     pDevice->byCurrSQ = *pbySQ;
570
571     // todo
572 /*
573     if ((*pbyRSSI != 0) &&
574         (pMgmt->pCurrBSS!=NULL)) {
575         RFvRSSITodBm(pDevice, *pbyRSSI, &ldBm);
576         // Monitor if RSSI is too strong.
577         pMgmt->pCurrBSS->byRSSIStatCnt++;
578         pMgmt->pCurrBSS->byRSSIStatCnt %= RSSI_STAT_COUNT;
579         pMgmt->pCurrBSS->ldBmAverage[pMgmt->pCurrBSS->byRSSIStatCnt] = ldBm;
580         for (ii = 0; ii < RSSI_STAT_COUNT; ii++) {
581                 if (pMgmt->pCurrBSS->ldBmAverage[ii] != 0) {
582                         pMgmt->pCurrBSS->ldBmMAX =
583                                 max(pMgmt->pCurrBSS->ldBmAverage[ii], ldBm);
584                 }
585         }
586     }
587 */
588
589     if ((pKey != NULL) && (pKey->byCipherSuite == KEY_CTL_TKIP)) {
590         if (bIsWEP) {
591             FrameSize -= 8;  //MIC
592         }
593     }
594
595     //--------------------------------------------------------------------------------
596     // Soft MIC
597     if ((pKey != NULL) && (pKey->byCipherSuite == KEY_CTL_TKIP)) {
598         if (bIsWEP) {
599             u32 *          pdwMIC_L;
600             u32 *          pdwMIC_R;
601             u32           dwMIC_Priority;
602             u32           dwMICKey0 = 0, dwMICKey1 = 0;
603             u32           dwLocalMIC_L = 0;
604             u32           dwLocalMIC_R = 0;
605
606             if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
607                 dwMICKey0 = cpu_to_le32(*(u32 *)(&pKey->abyKey[24]));
608                 dwMICKey1 = cpu_to_le32(*(u32 *)(&pKey->abyKey[28]));
609             }
610             else {
611                 if (pMgmt->eAuthenMode == WMAC_AUTH_WPANONE) {
612                     dwMICKey0 = cpu_to_le32(*(u32 *)(&pKey->abyKey[16]));
613                     dwMICKey1 = cpu_to_le32(*(u32 *)(&pKey->abyKey[20]));
614                 } else if ((pKey->dwKeyIndex & BIT28) == 0) {
615                     dwMICKey0 = cpu_to_le32(*(u32 *)(&pKey->abyKey[16]));
616                     dwMICKey1 = cpu_to_le32(*(u32 *)(&pKey->abyKey[20]));
617                 } else {
618                     dwMICKey0 = cpu_to_le32(*(u32 *)(&pKey->abyKey[24]));
619                     dwMICKey1 = cpu_to_le32(*(u32 *)(&pKey->abyKey[28]));
620                 }
621             }
622
623             MIC_vInit(dwMICKey0, dwMICKey1);
624             MIC_vAppend((u8 *)&(pDevice->sRxEthHeader.h_dest[0]), 12);
625             dwMIC_Priority = 0;
626             MIC_vAppend((u8 *)&dwMIC_Priority, 4);
627             // 4 is Rcv buffer header, 24 is MAC Header, and 8 is IV and Ext IV.
628             MIC_vAppend((u8 *)(skb->data + 8 + WLAN_HDR_ADDR3_LEN + 8),
629                         FrameSize - WLAN_HDR_ADDR3_LEN - 8);
630             MIC_vGetMIC(&dwLocalMIC_L, &dwLocalMIC_R);
631             MIC_vUnInit();
632
633             pdwMIC_L = (u32 *)(skb->data + 8 + FrameSize);
634             pdwMIC_R = (u32 *)(skb->data + 8 + FrameSize + 4);
635
636             if ((cpu_to_le32(*pdwMIC_L) != dwLocalMIC_L) || (cpu_to_le32(*pdwMIC_R) != dwLocalMIC_R) ||
637                 (pDevice->bRxMICFail == true)) {
638                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"MIC comparison is fail!\n");
639                 pDevice->bRxMICFail = false;
640                 if (bDeFragRx) {
641                     if (!device_alloc_frag_buf(pDevice, &pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx])) {
642                         DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc more frag bufs\n",
643                             pDevice->dev->name);
644                     }
645                 }
646                                 //send event to wpa_supplicant
647                                 //if(pDevice->bWPASuppWextEnabled == true)
648                                 {
649                                         union iwreq_data wrqu;
650                                         struct iw_michaelmicfailure ev;
651                                         int keyidx = pbyFrame[cbHeaderSize+3] >> 6; //top two-bits
652                                         memset(&ev, 0, sizeof(ev));
653                                         ev.flags = keyidx & IW_MICFAILURE_KEY_ID;
654                                         if ((pMgmt->eCurrMode == WMAC_MODE_ESS_STA) &&
655                                                         (pMgmt->eCurrState == WMAC_STATE_ASSOC) &&
656                                                                 (*pbyRsr & (RSR_ADDRBROAD | RSR_ADDRMULTI)) == 0) {
657                                                 ev.flags |= IW_MICFAILURE_PAIRWISE;
658                                         } else {
659                                                 ev.flags |= IW_MICFAILURE_GROUP;
660                                         }
661
662                                         ev.src_addr.sa_family = ARPHRD_ETHER;
663                                         memcpy(ev.src_addr.sa_data, pMACHeader->addr2, ETH_ALEN);
664                                         memset(&wrqu, 0, sizeof(wrqu));
665                                         wrqu.data.length = sizeof(ev);
666                         PRINT_K("wireless_send_event--->IWEVMICHAELMICFAILURE\n");
667                                         wireless_send_event(pDevice->dev, IWEVMICHAELMICFAILURE, &wrqu, (char *)&ev);
668
669                                 }
670
671                 return false;
672
673             }
674         }
675     } //---end of SOFT MIC-----------------------------------------------------------------------
676
677     // ++++++++++ Reply Counter Check +++++++++++++
678
679     if ((pKey != NULL) && ((pKey->byCipherSuite == KEY_CTL_TKIP) ||
680                            (pKey->byCipherSuite == KEY_CTL_CCMP))) {
681         if (bIsWEP) {
682             u16        wLocalTSC15_0 = 0;
683             u32       dwLocalTSC47_16 = 0;
684             unsigned long long       RSC = 0;
685             // endian issues
686             RSC = *((unsigned long long *) &(pKey->KeyRSC));
687             wLocalTSC15_0 = (u16) RSC;
688             dwLocalTSC47_16 = (u32) (RSC>>16);
689
690             RSC = dwRxTSC47_16;
691             RSC <<= 16;
692             RSC += wRxTSC15_0;
693                 memcpy(&(pKey->KeyRSC), &RSC,  sizeof(u64));
694
695                 if (pDevice->vnt_mgmt.eCurrMode == WMAC_MODE_ESS_STA &&
696                         pDevice->vnt_mgmt.eCurrState == WMAC_STATE_ASSOC) {
697                         /* check RSC */
698                 if ( (wRxTSC15_0 < wLocalTSC15_0) &&
699                      (dwRxTSC47_16 <= dwLocalTSC47_16) &&
700                      !((dwRxTSC47_16 == 0) && (dwLocalTSC47_16 == 0xFFFFFFFF))) {
701                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"TSC is illegal~~!\n ");
702
703                     if (bDeFragRx) {
704                         if (!device_alloc_frag_buf(pDevice, &pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx])) {
705                             DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc more frag bufs\n",
706                                 pDevice->dev->name);
707                         }
708                     }
709                     return false;
710                 }
711             }
712         }
713     } // ----- End of Reply Counter Check --------------------------
714
715     s_vProcessRxMACHeader(pDevice, (u8 *)(skb->data+8), FrameSize, bIsWEP, bExtIV, &cbHeaderOffset);
716     FrameSize -= cbHeaderOffset;
717     cbHeaderOffset += 8;        // 8 is Rcv buffer header
718
719     // Null data, framesize = 12
720     if (FrameSize < 12)
721         return false;
722
723         skb->data += cbHeaderOffset;
724         skb->tail += cbHeaderOffset;
725     skb_put(skb, FrameSize);
726     skb->protocol=eth_type_trans(skb, skb->dev);
727     skb->ip_summed=CHECKSUM_NONE;
728     pStats->rx_bytes +=skb->len;
729     pStats->rx_packets++;
730     netif_rx(skb);
731     if (bDeFragRx) {
732         if (!device_alloc_frag_buf(pDevice, &pDevice->sRxDFCB[pDevice->uCurrentDFCBIdx])) {
733             DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc more frag bufs\n",
734                 pDevice->dev->name);
735         }
736         return false;
737     }
738
739     return true;
740 }
741
742 static int s_bHandleRxEncryption(struct vnt_private *pDevice, u8 *pbyFrame,
743         u32 FrameSize, u8 *pbyRsr, u8 *pbyNewRsr, PSKeyItem *pKeyOut,
744         s32 *pbExtIV, u16 *pwRxTSC15_0, u32 *pdwRxTSC47_16)
745 {
746         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
747         u32 PayloadLen = FrameSize;
748         u8 *pbyIV;
749         u8 byKeyIdx;
750         PSKeyItem pKey = NULL;
751         u8 byDecMode = KEY_CTL_WEP;
752
753     *pwRxTSC15_0 = 0;
754     *pdwRxTSC47_16 = 0;
755
756     pbyIV = pbyFrame + WLAN_HDR_ADDR3_LEN;
757     if ( WLAN_GET_FC_TODS(*(u16 *)pbyFrame) &&
758          WLAN_GET_FC_FROMDS(*(u16 *)pbyFrame) ) {
759          pbyIV += 6;             // 6 is 802.11 address4
760          PayloadLen -= 6;
761     }
762     byKeyIdx = (*(pbyIV+3) & 0xc0);
763     byKeyIdx >>= 6;
764     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"\nKeyIdx: %d\n", byKeyIdx);
765
766     if ((pMgmt->eAuthenMode == WMAC_AUTH_WPA) ||
767         (pMgmt->eAuthenMode == WMAC_AUTH_WPAPSK) ||
768         (pMgmt->eAuthenMode == WMAC_AUTH_WPANONE) ||
769         (pMgmt->eAuthenMode == WMAC_AUTH_WPA2) ||
770         (pMgmt->eAuthenMode == WMAC_AUTH_WPA2PSK)) {
771         if (((*pbyRsr & (RSR_ADDRBROAD | RSR_ADDRMULTI)) == 0) &&
772             (pMgmt->byCSSPK != KEY_CTL_NONE)) {
773             // unicast pkt use pairwise key
774             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"unicast pkt\n");
775             if (KeybGetKey(&(pDevice->sKey), pDevice->abyBSSID, 0xFFFFFFFF, &pKey) == true) {
776                 if (pMgmt->byCSSPK == KEY_CTL_TKIP)
777                     byDecMode = KEY_CTL_TKIP;
778                 else if (pMgmt->byCSSPK == KEY_CTL_CCMP)
779                     byDecMode = KEY_CTL_CCMP;
780             }
781             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"unicast pkt: %d, %p\n", byDecMode, pKey);
782         } else {
783             // use group key
784             KeybGetKey(&(pDevice->sKey), pDevice->abyBSSID, byKeyIdx, &pKey);
785             if (pMgmt->byCSSGK == KEY_CTL_TKIP)
786                 byDecMode = KEY_CTL_TKIP;
787             else if (pMgmt->byCSSGK == KEY_CTL_CCMP)
788                 byDecMode = KEY_CTL_CCMP;
789             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"group pkt: %d, %d, %p\n", byKeyIdx, byDecMode, pKey);
790         }
791     }
792     // our WEP only support Default Key
793     if (pKey == NULL) {
794         // use default group key
795         KeybGetKey(&(pDevice->sKey), pDevice->abyBroadcastAddr, byKeyIdx, &pKey);
796         if (pMgmt->byCSSGK == KEY_CTL_TKIP)
797             byDecMode = KEY_CTL_TKIP;
798         else if (pMgmt->byCSSGK == KEY_CTL_CCMP)
799             byDecMode = KEY_CTL_CCMP;
800     }
801     *pKeyOut = pKey;
802
803     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"AES:%d %d %d\n", pMgmt->byCSSPK, pMgmt->byCSSGK, byDecMode);
804
805     if (pKey == NULL) {
806         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pKey == NULL\n");
807         return false;
808     }
809     if (byDecMode != pKey->byCipherSuite) {
810         *pKeyOut = NULL;
811         return false;
812     }
813     if (byDecMode == KEY_CTL_WEP) {
814         // handle WEP
815         if ((pDevice->byLocalID <= REV_ID_VT3253_A1) ||
816                 (((PSKeyTable)(pKey->pvKeyTable))->bSoftWEP == true)) {
817             // Software WEP
818             // 1. 3253A
819             // 2. WEP 256
820
821             PayloadLen -= (WLAN_HDR_ADDR3_LEN + 4 + 4); // 24 is 802.11 header,4 is IV, 4 is crc
822             memcpy(pDevice->abyPRNG, pbyIV, 3);
823             memcpy(pDevice->abyPRNG + 3, pKey->abyKey, pKey->uKeyLength);
824             rc4_init(&pDevice->SBox, pDevice->abyPRNG, pKey->uKeyLength + 3);
825             rc4_encrypt(&pDevice->SBox, pbyIV+4, pbyIV+4, PayloadLen);
826
827             if (ETHbIsBufferCrc32Ok(pbyIV+4, PayloadLen)) {
828                 *pbyNewRsr |= NEWRSR_DECRYPTOK;
829             }
830         }
831     } else if ((byDecMode == KEY_CTL_TKIP) ||
832                (byDecMode == KEY_CTL_CCMP)) {
833         // TKIP/AES
834
835         PayloadLen -= (WLAN_HDR_ADDR3_LEN + 8 + 4); // 24 is 802.11 header, 8 is IV&ExtIV, 4 is crc
836         *pdwRxTSC47_16 = cpu_to_le32(*(u32 *)(pbyIV + 4));
837         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ExtIV: %x\n", *pdwRxTSC47_16);
838         if (byDecMode == KEY_CTL_TKIP) {
839             *pwRxTSC15_0 = cpu_to_le16(MAKEWORD(*(pbyIV+2), *pbyIV));
840         } else {
841             *pwRxTSC15_0 = cpu_to_le16(*(u16 *)pbyIV);
842         }
843         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"TSC0_15: %x\n", *pwRxTSC15_0);
844
845         if ((byDecMode == KEY_CTL_TKIP) &&
846             (pDevice->byLocalID <= REV_ID_VT3253_A1)) {
847             // Software TKIP
848             // 1. 3253 A
849             struct ieee80211_hdr *pMACHeader = (struct ieee80211_hdr *) (pbyFrame);
850             TKIPvMixKey(pKey->abyKey, pMACHeader->addr2, *pwRxTSC15_0, *pdwRxTSC47_16, pDevice->abyPRNG);
851             rc4_init(&pDevice->SBox, pDevice->abyPRNG, TKIP_KEY_LEN);
852             rc4_encrypt(&pDevice->SBox, pbyIV+8, pbyIV+8, PayloadLen);
853             if (ETHbIsBufferCrc32Ok(pbyIV+8, PayloadLen)) {
854                 *pbyNewRsr |= NEWRSR_DECRYPTOK;
855                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ICV OK!\n");
856             } else {
857                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ICV FAIL!!!\n");
858                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"PayloadLen = %d\n", PayloadLen);
859             }
860         }
861     }// end of TKIP/AES
862
863     if ((*(pbyIV+3) & 0x20) != 0)
864         *pbExtIV = true;
865     return true;
866 }
867
868 void RXvWorkItem(struct work_struct *work)
869 {
870         struct vnt_private *priv =
871                 container_of(work, struct vnt_private, read_work_item);
872         int status;
873         struct vnt_rcb *rcb = NULL;
874
875         if (priv->Flags & fMP_DISCONNECTED)
876                 return;
877
878         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->Rx Polling Thread\n");
879
880         spin_lock_irq(&priv->lock);
881
882         while ((priv->Flags & fMP_POST_READS) && MP_IS_READY(priv) &&
883                         (priv->NumRecvFreeList != 0)) {
884                 rcb = priv->FirstRecvFreeList;
885
886                 priv->NumRecvFreeList--;
887
888                 DequeueRCB(priv->FirstRecvFreeList, priv->LastRecvFreeList);
889
890                 status = PIPEnsBulkInUsbRead(priv, rcb);
891         }
892
893         priv->bIsRxWorkItemQueued = false;
894
895         spin_unlock_irq(&priv->lock);
896 }
897
898 void RXvFreeRCB(struct vnt_rcb *rcb, int re_alloc_skb)
899 {
900         struct vnt_private *priv = rcb->pDevice;
901
902         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->RXvFreeRCB\n");
903
904         if (re_alloc_skb == false) {
905                 kfree_skb(rcb->skb);
906                 re_alloc_skb = true;
907         }
908
909         if (re_alloc_skb == true) {
910                 rcb->skb = netdev_alloc_skb(priv->dev, priv->rx_buf_sz);
911                 /* TODO error handling */
912                 if (!rcb->skb) {
913                         DBG_PRT(MSG_LEVEL_ERR, KERN_ERR
914                                 " Failed to re-alloc rx skb\n");
915                 }
916         }
917
918         /* Insert the RCB back in the Recv free list */
919         EnqueueRCB(priv->FirstRecvFreeList, priv->LastRecvFreeList, rcb);
920         priv->NumRecvFreeList++;
921
922         if ((priv->Flags & fMP_POST_READS) && MP_IS_READY(priv) &&
923                         (priv->bIsRxWorkItemQueued == false)) {
924                 priv->bIsRxWorkItemQueued = true;
925                 schedule_work(&priv->read_work_item);
926         }
927
928         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"<----RXFreeRCB %d %d\n",
929                         priv->NumRecvFreeList, priv->NumRecvMngList);
930 }
931
932 void RXvMngWorkItem(struct work_struct *work)
933 {
934         struct vnt_private *pDevice =
935                 container_of(work, struct vnt_private, rx_mng_work_item);
936         struct vnt_rcb *pRCB = NULL;
937         struct vnt_rx_mgmt *pRxPacket;
938         int bReAllocSkb = false;
939         unsigned long flags;
940
941         if (pDevice->Flags & fMP_DISCONNECTED)
942                 return;
943
944     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->Rx Mng Thread\n");
945
946     while (pDevice->NumRecvMngList!=0)
947     {
948         spin_lock_irqsave(&pDevice->lock, flags);
949
950         pRCB = pDevice->FirstRecvMngList;
951         pDevice->NumRecvMngList--;
952         DequeueRCB(pDevice->FirstRecvMngList, pDevice->LastRecvMngList);
953
954         spin_unlock_irqrestore(&pDevice->lock, flags);
955
956         if(!pRCB){
957             break;
958         }
959         pRxPacket = &(pRCB->sMngPacket);
960         vMgrRxManagePacket(pDevice, &pDevice->vnt_mgmt, pRxPacket);
961         pRCB->Ref--;
962         if (pRCB->Ref == 0) {
963                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"RxvFreeMng %d %d\n",
964                         pDevice->NumRecvFreeList, pDevice->NumRecvMngList);
965
966                 spin_lock_irqsave(&pDevice->lock, flags);
967
968                 RXvFreeRCB(pRCB, bReAllocSkb);
969
970                 spin_unlock_irqrestore(&pDevice->lock, flags);
971         } else {
972             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Rx Mng Only we have the right to free RCB\n");
973         }
974     }
975
976         pDevice->bIsRxMngWorkItemQueued = false;
977 }
978