Staging: rt2860: remove dead RALINK_ATE code
[linux-2.6] / drivers / staging / rt2860 / sta / connect.c
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         connect.c
29
30         Abstract:
31
32         Revision History:
33         Who                     When                    What
34         --------        ----------              ----------------------------------------------
35         John                    2004-08-08                      Major modification from RT2560
36 */
37 #include "../rt_config.h"
38
39 UCHAR   CipherSuiteWpaNoneTkip[] = {
40                 0x00, 0x50, 0xf2, 0x01, // oui
41                 0x01, 0x00,                             // Version
42                 0x00, 0x50, 0xf2, 0x02, // Multicast
43                 0x01, 0x00,                             // Number of unicast
44                 0x00, 0x50, 0xf2, 0x02, // unicast
45                 0x01, 0x00,                             // number of authentication method
46                 0x00, 0x50, 0xf2, 0x00  // authentication
47                 };
48 UCHAR   CipherSuiteWpaNoneTkipLen = (sizeof(CipherSuiteWpaNoneTkip) / sizeof(UCHAR));
49
50 UCHAR   CipherSuiteWpaNoneAes[] = {
51                 0x00, 0x50, 0xf2, 0x01, // oui
52                 0x01, 0x00,                             // Version
53                 0x00, 0x50, 0xf2, 0x04, // Multicast
54                 0x01, 0x00,                             // Number of unicast
55                 0x00, 0x50, 0xf2, 0x04, // unicast
56                 0x01, 0x00,                             // number of authentication method
57                 0x00, 0x50, 0xf2, 0x00  // authentication
58                 };
59 UCHAR   CipherSuiteWpaNoneAesLen = (sizeof(CipherSuiteWpaNoneAes) / sizeof(UCHAR));
60
61 // The following MACRO is called after 1. starting an new IBSS, 2. succesfully JOIN an IBSS,
62 // or 3. succesfully ASSOCIATE to a BSS, 4. successfully RE_ASSOCIATE to a BSS
63 // All settings successfuly negotiated furing MLME state machines become final settings
64 // and are copied to pAd->StaActive
65 #define COPY_SETTINGS_FROM_MLME_AUX_TO_ACTIVE_CFG(_pAd)                                 \
66 {                                                                                       \
67         (_pAd)->CommonCfg.SsidLen = (_pAd)->MlmeAux.SsidLen;                                \
68         NdisMoveMemory((_pAd)->CommonCfg.Ssid, (_pAd)->MlmeAux.Ssid, (_pAd)->MlmeAux.SsidLen); \
69         COPY_MAC_ADDR((_pAd)->CommonCfg.Bssid, (_pAd)->MlmeAux.Bssid);                      \
70         (_pAd)->CommonCfg.Channel = (_pAd)->MlmeAux.Channel;                                \
71         (_pAd)->CommonCfg.CentralChannel = (_pAd)->MlmeAux.CentralChannel;                  \
72         (_pAd)->StaActive.Aid = (_pAd)->MlmeAux.Aid;                                        \
73         (_pAd)->StaActive.AtimWin = (_pAd)->MlmeAux.AtimWin;                                \
74         (_pAd)->StaActive.CapabilityInfo = (_pAd)->MlmeAux.CapabilityInfo;                  \
75         (_pAd)->CommonCfg.BeaconPeriod = (_pAd)->MlmeAux.BeaconPeriod;                      \
76         (_pAd)->StaActive.CfpMaxDuration = (_pAd)->MlmeAux.CfpMaxDuration;                  \
77         (_pAd)->StaActive.CfpPeriod = (_pAd)->MlmeAux.CfpPeriod;                            \
78         (_pAd)->StaActive.SupRateLen = (_pAd)->MlmeAux.SupRateLen;                          \
79         NdisMoveMemory((_pAd)->StaActive.SupRate, (_pAd)->MlmeAux.SupRate, (_pAd)->MlmeAux.SupRateLen);\
80         (_pAd)->StaActive.ExtRateLen = (_pAd)->MlmeAux.ExtRateLen;                          \
81         NdisMoveMemory((_pAd)->StaActive.ExtRate, (_pAd)->MlmeAux.ExtRate, (_pAd)->MlmeAux.ExtRateLen);\
82         NdisMoveMemory(&(_pAd)->CommonCfg.APEdcaParm, &(_pAd)->MlmeAux.APEdcaParm, sizeof(EDCA_PARM));\
83         NdisMoveMemory(&(_pAd)->CommonCfg.APQosCapability, &(_pAd)->MlmeAux.APQosCapability, sizeof(QOS_CAPABILITY_PARM));\
84         NdisMoveMemory(&(_pAd)->CommonCfg.APQbssLoad, &(_pAd)->MlmeAux.APQbssLoad, sizeof(QBSS_LOAD_PARM));\
85         COPY_MAC_ADDR((_pAd)->MacTab.Content[BSSID_WCID].Addr, (_pAd)->MlmeAux.Bssid);      \
86         (_pAd)->MacTab.Content[BSSID_WCID].Aid = (_pAd)->MlmeAux.Aid;                       \
87         (_pAd)->MacTab.Content[BSSID_WCID].PairwiseKey.CipherAlg = (_pAd)->StaCfg.PairCipher;\
88         COPY_MAC_ADDR((_pAd)->MacTab.Content[BSSID_WCID].PairwiseKey.BssId, (_pAd)->MlmeAux.Bssid);\
89         (_pAd)->MacTab.Content[BSSID_WCID].RateLen = (_pAd)->StaActive.SupRateLen + (_pAd)->StaActive.ExtRateLen;\
90 }
91
92 /*
93         ==========================================================================
94         Description:
95
96         IRQL = PASSIVE_LEVEL
97
98         ==========================================================================
99 */
100 VOID MlmeCntlInit(
101         IN PRTMP_ADAPTER pAd,
102         IN STATE_MACHINE *S,
103         OUT STATE_MACHINE_FUNC Trans[])
104 {
105         // Control state machine differs from other state machines, the interface
106         // follows the standard interface
107         pAd->Mlme.CntlMachine.CurrState = CNTL_IDLE;
108 }
109
110 /*
111         ==========================================================================
112         Description:
113
114         IRQL = DISPATCH_LEVEL
115
116         ==========================================================================
117 */
118 VOID MlmeCntlMachinePerformAction(
119         IN PRTMP_ADAPTER pAd,
120         IN STATE_MACHINE *S,
121         IN MLME_QUEUE_ELEM *Elem)
122 {
123         switch(pAd->Mlme.CntlMachine.CurrState)
124         {
125                 case CNTL_IDLE:
126                         {
127                                 CntlIdleProc(pAd, Elem);
128                         }
129                         break;
130                 case CNTL_WAIT_DISASSOC:
131                         CntlWaitDisassocProc(pAd, Elem);
132                         break;
133                 case CNTL_WAIT_JOIN:
134                         CntlWaitJoinProc(pAd, Elem);
135                         break;
136
137                 // CNTL_WAIT_REASSOC is the only state in CNTL machine that does
138                 // not triggered directly or indirectly by "RTMPSetInformation(OID_xxx)".
139                 // Therefore not protected by NDIS's "only one outstanding OID request"
140                 // rule. Which means NDIS may SET OID in the middle of ROAMing attempts.
141                 // Current approach is to block new SET request at RTMPSetInformation()
142                 // when CntlMachine.CurrState is not CNTL_IDLE
143                 case CNTL_WAIT_REASSOC:
144                         CntlWaitReassocProc(pAd, Elem);
145                         break;
146
147                 case CNTL_WAIT_START:
148                         CntlWaitStartProc(pAd, Elem);
149                         break;
150                 case CNTL_WAIT_AUTH:
151                         CntlWaitAuthProc(pAd, Elem);
152                         break;
153                 case CNTL_WAIT_AUTH2:
154                         CntlWaitAuthProc2(pAd, Elem);
155                         break;
156                 case CNTL_WAIT_ASSOC:
157                         CntlWaitAssocProc(pAd, Elem);
158                         break;
159
160                 case CNTL_WAIT_OID_LIST_SCAN:
161                         if(Elem->MsgType == MT2_SCAN_CONF)
162                         {
163                                 // Resume TxRing after SCANING complete. We hope the out-of-service time
164                                 // won't be too long to let upper layer time-out the waiting frames
165                                 RTMPResumeMsduTransmission(pAd);
166                                 if (pAd->StaCfg.CCXReqType != MSRN_TYPE_UNUSED)
167                                 {
168                                         // Cisco scan request is finished, prepare beacon report
169                                         MlmeEnqueue(pAd, AIRONET_STATE_MACHINE, MT2_AIRONET_SCAN_DONE, 0, NULL);
170                                 }
171                                 pAd->Mlme.CntlMachine.CurrState = CNTL_IDLE;
172
173                 //
174                                 // Set LED status to previous status.
175                                 //
176                                 if (pAd->bLedOnScanning)
177                                 {
178                                         pAd->bLedOnScanning = FALSE;
179                                         RTMPSetLED(pAd, pAd->LedStatus);
180                                 }
181 #ifdef DOT11N_DRAFT3
182                                 // AP sent a 2040Coexistence mgmt frame, then station perform a scan, and then send back the respone.
183                                 if (pAd->CommonCfg.BSSCoexist2040.field.InfoReq == 1)
184                                 {
185                                         Update2040CoexistFrameAndNotify(pAd, BSSID_WCID, TRUE);
186                                 }
187 #endif // DOT11N_DRAFT3 //
188                         }
189                         break;
190
191                 case CNTL_WAIT_OID_DISASSOC:
192                         if (Elem->MsgType == MT2_DISASSOC_CONF)
193                         {
194                                 LinkDown(pAd, FALSE);
195                                 pAd->Mlme.CntlMachine.CurrState = CNTL_IDLE;
196                         }
197                         break;
198                 default:
199                         DBGPRINT_ERR(("!ERROR! CNTL - Illegal message type(=%ld)", Elem->MsgType));
200                         break;
201         }
202 }
203
204
205 /*
206         ==========================================================================
207         Description:
208
209         IRQL = DISPATCH_LEVEL
210
211         ==========================================================================
212 */
213 VOID CntlIdleProc(
214         IN PRTMP_ADAPTER pAd,
215         IN MLME_QUEUE_ELEM *Elem)
216 {
217         MLME_DISASSOC_REQ_STRUCT   DisassocReq;
218
219         if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RADIO_OFF))
220                 return;
221
222         switch(Elem->MsgType)
223         {
224                 case OID_802_11_SSID:
225                         CntlOidSsidProc(pAd, Elem);
226                         break;
227
228                 case OID_802_11_BSSID:
229                         CntlOidRTBssidProc(pAd,Elem);
230                         break;
231
232                 case OID_802_11_BSSID_LIST_SCAN:
233                         CntlOidScanProc(pAd,Elem);
234                         break;
235
236                 case OID_802_11_DISASSOCIATE:
237                         DisassocParmFill(pAd, &DisassocReq, pAd->CommonCfg.Bssid, REASON_DISASSOC_STA_LEAVING);
238                         MlmeEnqueue(pAd, ASSOC_STATE_MACHINE, MT2_MLME_DISASSOC_REQ, sizeof(MLME_DISASSOC_REQ_STRUCT), &DisassocReq);
239                         pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_OID_DISASSOC;
240 #ifdef WPA_SUPPLICANT_SUPPORT
241             if (pAd->StaCfg.WpaSupplicantUP != WPA_SUPPLICANT_ENABLE_WITH_WEB_UI)
242 #endif // WPA_SUPPLICANT_SUPPORT //
243             {
244                         // Set the AutoReconnectSsid to prevent it reconnect to old SSID
245                         // Since calling this indicate user don't want to connect to that SSID anymore.
246                         pAd->MlmeAux.AutoReconnectSsidLen= 32;
247                         NdisZeroMemory(pAd->MlmeAux.AutoReconnectSsid, pAd->MlmeAux.AutoReconnectSsidLen);
248             }
249                         break;
250
251                 case MT2_MLME_ROAMING_REQ:
252                         CntlMlmeRoamingProc(pAd, Elem);
253                         break;
254
255         case OID_802_11_MIC_FAILURE_REPORT_FRAME:
256             WpaMicFailureReportFrame(pAd, Elem);
257             break;
258
259 #ifdef QOS_DLS_SUPPORT
260                 case RT_OID_802_11_SET_DLS_PARAM:
261                         CntlOidDLSSetupProc(pAd, Elem);
262                         break;
263 #endif // QOS_DLS_SUPPORT //
264
265                 default:
266                         DBGPRINT(RT_DEBUG_TRACE, ("CNTL - Illegal message in CntlIdleProc(MsgType=%ld)\n",Elem->MsgType));
267                         break;
268         }
269 }
270
271 VOID CntlOidScanProc(
272         IN PRTMP_ADAPTER pAd,
273         IN MLME_QUEUE_ELEM *Elem)
274 {
275         MLME_SCAN_REQ_STRUCT       ScanReq;
276         ULONG                      BssIdx = BSS_NOT_FOUND;
277         BSS_ENTRY                  CurrBss;
278
279         // record current BSS if network is connected.
280         // 2003-2-13 do not include current IBSS if this is the only STA in this IBSS.
281         if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED))
282         {
283                 BssIdx = BssSsidTableSearch(&pAd->ScanTab, pAd->CommonCfg.Bssid, pAd->CommonCfg.Ssid, pAd->CommonCfg.SsidLen, pAd->CommonCfg.Channel);
284                 if (BssIdx != BSS_NOT_FOUND)
285                 {
286                         NdisMoveMemory(&CurrBss, &pAd->ScanTab.BssEntry[BssIdx], sizeof(BSS_ENTRY));
287                 }
288         }
289
290         // clean up previous SCAN result, add current BSS back to table if any
291         BssTableInit(&pAd->ScanTab);
292         if (BssIdx != BSS_NOT_FOUND)
293         {
294                 // DDK Note: If the NIC is associated with a particular BSSID and SSID
295                 //    that are not contained in the list of BSSIDs generated by this scan, the
296                 //    BSSID description of the currently associated BSSID and SSID should be
297                 //    appended to the list of BSSIDs in the NIC's database.
298                 // To ensure this, we append this BSS as the first entry in SCAN result
299                 NdisMoveMemory(&pAd->ScanTab.BssEntry[0], &CurrBss, sizeof(BSS_ENTRY));
300                 pAd->ScanTab.BssNr = 1;
301         }
302
303         ScanParmFill(pAd, &ScanReq, "", 0, BSS_ANY, SCAN_ACTIVE);
304         MlmeEnqueue(pAd, SYNC_STATE_MACHINE, MT2_MLME_SCAN_REQ,
305                 sizeof(MLME_SCAN_REQ_STRUCT), &ScanReq);
306         pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_OID_LIST_SCAN;
307 }
308
309 /*
310         ==========================================================================
311         Description:
312                 Before calling this routine, user desired SSID should already been
313                 recorded in CommonCfg.Ssid[]
314         IRQL = DISPATCH_LEVEL
315
316         ==========================================================================
317 */
318 VOID CntlOidSsidProc(
319         IN PRTMP_ADAPTER pAd,
320         IN MLME_QUEUE_ELEM * Elem)
321 {
322         PNDIS_802_11_SSID          pOidSsid = (NDIS_802_11_SSID *)Elem->Msg;
323         MLME_DISASSOC_REQ_STRUCT   DisassocReq;
324         ULONG                                      Now;
325
326         // BBP and RF are not accessible in PS mode, we has to wake them up first
327         if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_DOZE))
328                 AsicForceWakeup(pAd, RTMP_HALT);
329
330         // Step 1. record the desired user settings to MlmeAux
331         NdisZeroMemory(pAd->MlmeAux.Ssid, MAX_LEN_OF_SSID);
332         NdisMoveMemory(pAd->MlmeAux.Ssid, pOidSsid->Ssid, pOidSsid->SsidLength);
333         pAd->MlmeAux.SsidLen = (UCHAR)pOidSsid->SsidLength;
334         NdisZeroMemory(pAd->MlmeAux.Bssid, MAC_ADDR_LEN);
335         pAd->MlmeAux.BssType = pAd->StaCfg.BssType;
336
337
338         //
339         // Update Reconnect Ssid, that user desired to connect.
340         //
341         NdisZeroMemory(pAd->MlmeAux.AutoReconnectSsid, MAX_LEN_OF_SSID);
342         NdisMoveMemory(pAd->MlmeAux.AutoReconnectSsid, pAd->MlmeAux.Ssid, pAd->MlmeAux.SsidLen);
343         pAd->MlmeAux.AutoReconnectSsidLen = pAd->MlmeAux.SsidLen;
344
345         // step 2. find all matching BSS in the lastest SCAN result (inBssTab)
346         //    & log them into MlmeAux.SsidBssTab for later-on iteration. Sort by RSSI order
347         BssTableSsidSort(pAd, &pAd->MlmeAux.SsidBssTab, pAd->MlmeAux.Ssid, pAd->MlmeAux.SsidLen);
348
349         DBGPRINT(RT_DEBUG_TRACE, ("CntlOidSsidProc():CNTL - %d BSS of %d BSS match the desire (%d)SSID - %s\n",
350                         pAd->MlmeAux.SsidBssTab.BssNr, pAd->ScanTab.BssNr, pAd->MlmeAux.SsidLen, pAd->MlmeAux.Ssid));
351         NdisGetSystemUpTime(&Now);
352
353         if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED) &&
354                 (pAd->CommonCfg.SsidLen == pAd->MlmeAux.SsidBssTab.BssEntry[0].SsidLen) &&
355                 NdisEqualMemory(pAd->CommonCfg.Ssid, pAd->MlmeAux.SsidBssTab.BssEntry[0].Ssid, pAd->CommonCfg.SsidLen) &&
356                 MAC_ADDR_EQUAL(pAd->CommonCfg.Bssid, pAd->MlmeAux.SsidBssTab.BssEntry[0].Bssid))
357         {
358                 // Case 1. already connected with an AP who has the desired SSID
359                 //         with highest RSSI
360
361                 // Add checking Mode "LEAP" for CCX 1.0
362                 if (((pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA) ||
363                          (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPAPSK) ||
364                          (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA2) ||
365                          (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA2PSK)
366 #ifdef LEAP_SUPPORT
367                          || (pAd->StaCfg.LeapAuthMode == CISCO_AuthModeLEAP)
368 #endif // LEAP_SUPPORT //
369                          ) &&
370                         (pAd->StaCfg.PortSecured == WPA_802_1X_PORT_NOT_SECURED))
371                 {
372                         // case 1.1 For WPA, WPA-PSK, if the 1x port is not secured, we have to redo
373                         //          connection process
374                         DBGPRINT(RT_DEBUG_TRACE, ("CntlOidSsidProc():CNTL - disassociate with current AP...\n"));
375                         DisassocParmFill(pAd, &DisassocReq, pAd->CommonCfg.Bssid, REASON_DISASSOC_STA_LEAVING);
376                         MlmeEnqueue(pAd, ASSOC_STATE_MACHINE, MT2_MLME_DISASSOC_REQ,
377                                                 sizeof(MLME_DISASSOC_REQ_STRUCT), &DisassocReq);
378                         pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_DISASSOC;
379                 }
380                 else if (pAd->bConfigChanged == TRUE)
381                 {
382                         // case 1.2 Important Config has changed, we have to reconnect to the same AP
383                         DBGPRINT(RT_DEBUG_TRACE, ("CntlOidSsidProc():CNTL - disassociate with current AP Because config changed...\n"));
384                         DisassocParmFill(pAd, &DisassocReq, pAd->CommonCfg.Bssid, REASON_DISASSOC_STA_LEAVING);
385                         MlmeEnqueue(pAd, ASSOC_STATE_MACHINE, MT2_MLME_DISASSOC_REQ,
386                                                 sizeof(MLME_DISASSOC_REQ_STRUCT), &DisassocReq);
387                         pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_DISASSOC;
388                 }
389                 else
390                 {
391                         // case 1.3. already connected to the SSID with highest RSSI.
392                         DBGPRINT(RT_DEBUG_TRACE, ("CntlOidSsidProc():CNTL - already with this BSSID. ignore this SET_SSID request\n"));
393                         //
394                         // (HCT 12.1) 1c_wlan_mediaevents required
395                         // media connect events are indicated when associating with the same AP
396                         //
397                         if (INFRA_ON(pAd))
398                         {
399                                 //
400                                 // Since MediaState already is NdisMediaStateConnected
401                                 // We just indicate the connect event again to meet the WHQL required.
402                                 //
403                                 pAd->IndicateMediaState = NdisMediaStateConnected;
404                                 RTMP_IndicateMediaState(pAd);
405                 pAd->ExtraInfo = GENERAL_LINK_UP;   // Update extra information to link is up
406                         }
407
408                         pAd->Mlme.CntlMachine.CurrState = CNTL_IDLE;
409 #ifdef NATIVE_WPA_SUPPLICANT_SUPPORT
410             {
411                 union iwreq_data    wrqu;
412
413                 memset(wrqu.ap_addr.sa_data, 0, MAC_ADDR_LEN);
414                 memcpy(wrqu.ap_addr.sa_data, pAd->MlmeAux.Bssid, MAC_ADDR_LEN);
415                 wireless_send_event(pAd->net_dev, SIOCGIWAP, &wrqu, NULL);
416
417             }
418 #endif // NATIVE_WPA_SUPPLICANT_SUPPORT //
419                 }
420         }
421         else if (INFRA_ON(pAd))
422         {
423                 //
424                 // For RT61
425                 // [88888] OID_802_11_SSID should have returned NDTEST_WEP_AP2(Returned: )
426                 // RT61 may lost SSID, and not connect to NDTEST_WEP_AP2 and will connect to NDTEST_WEP_AP2 by Autoreconnect
427                 // But media status is connected, so the SSID not report correctly.
428                 //
429                 if (!SSID_EQUAL(pAd->CommonCfg.Ssid, pAd->CommonCfg.SsidLen, pAd->MlmeAux.Ssid, pAd->MlmeAux.SsidLen))
430                 {
431                         //
432                         // Different SSID means not Roaming case, so we let LinkDown() to Indicate a disconnect event.
433                         //
434                         pAd->MlmeAux.CurrReqIsFromNdis = TRUE;
435                 }
436                 // case 2. active INFRA association existent
437                 //    roaming is done within miniport driver, nothing to do with configuration
438                 //    utility. so upon a new SET(OID_802_11_SSID) is received, we just
439                 //    disassociate with the current associated AP,
440                 //    then perform a new association with this new SSID, no matter the
441                 //    new/old SSID are the same or not.
442                 DBGPRINT(RT_DEBUG_TRACE, ("CntlOidSsidProc():CNTL - disassociate with current AP...\n"));
443                 DisassocParmFill(pAd, &DisassocReq, pAd->CommonCfg.Bssid, REASON_DISASSOC_STA_LEAVING);
444                 MlmeEnqueue(pAd, ASSOC_STATE_MACHINE, MT2_MLME_DISASSOC_REQ,
445                                         sizeof(MLME_DISASSOC_REQ_STRUCT), &DisassocReq);
446                 pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_DISASSOC;
447         }
448         else
449         {
450                 if (ADHOC_ON(pAd))
451                 {
452                         DBGPRINT(RT_DEBUG_TRACE, ("CntlOidSsidProc():CNTL - drop current ADHOC\n"));
453                         LinkDown(pAd, FALSE);
454                         OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED);
455                         pAd->IndicateMediaState = NdisMediaStateDisconnected;
456                         RTMP_IndicateMediaState(pAd);
457             pAd->ExtraInfo = GENERAL_LINK_DOWN;
458                         DBGPRINT(RT_DEBUG_TRACE, ("CntlOidSsidProc():NDIS_STATUS_MEDIA_DISCONNECT Event C!\n"));
459                 }
460
461                 if ((pAd->MlmeAux.SsidBssTab.BssNr == 0) &&
462                         (pAd->StaCfg.bAutoReconnect == TRUE) &&
463                         (pAd->MlmeAux.BssType == BSS_INFRA) &&
464                         (MlmeValidateSSID(pAd->MlmeAux.Ssid, pAd->MlmeAux.SsidLen) == TRUE)
465                         )
466                 {
467                         MLME_SCAN_REQ_STRUCT       ScanReq;
468
469                         DBGPRINT(RT_DEBUG_TRACE, ("CntlOidSsidProc():CNTL - No matching BSS, start a new scan\n"));
470                         ScanParmFill(pAd, &ScanReq, pAd->MlmeAux.Ssid, pAd->MlmeAux.SsidLen, BSS_ANY, SCAN_ACTIVE);
471                         MlmeEnqueue(pAd, SYNC_STATE_MACHINE, MT2_MLME_SCAN_REQ, sizeof(MLME_SCAN_REQ_STRUCT), &ScanReq);
472                         pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_OID_LIST_SCAN;
473                         // Reset Missed scan number
474                         pAd->StaCfg.LastScanTime = Now;
475                 }
476                 else
477                 {
478                         pAd->MlmeAux.BssIdx = 0;
479                         IterateOnBssTab(pAd);
480                 }
481         }
482 }
483
484
485 /*
486         ==========================================================================
487         Description:
488
489         IRQL = DISPATCH_LEVEL
490
491         ==========================================================================
492 */
493 VOID CntlOidRTBssidProc(
494         IN PRTMP_ADAPTER pAd,
495         IN MLME_QUEUE_ELEM * Elem)
496 {
497         ULONG       BssIdx;
498         PUCHAR      pOidBssid = (PUCHAR)Elem->Msg;
499         MLME_DISASSOC_REQ_STRUCT    DisassocReq;
500         MLME_JOIN_REQ_STRUCT        JoinReq;
501
502         // record user desired settings
503         COPY_MAC_ADDR(pAd->MlmeAux.Bssid, pOidBssid);
504         pAd->MlmeAux.BssType = pAd->StaCfg.BssType;
505
506         //
507         // Update Reconnect Ssid, that user desired to connect.
508         //
509         NdisZeroMemory(pAd->MlmeAux.AutoReconnectSsid, MAX_LEN_OF_SSID);
510         pAd->MlmeAux.AutoReconnectSsidLen = pAd->MlmeAux.SsidLen;
511         NdisMoveMemory(pAd->MlmeAux.AutoReconnectSsid, pAd->MlmeAux.Ssid, pAd->MlmeAux.SsidLen);
512
513         // find the desired BSS in the latest SCAN result table
514         BssIdx = BssTableSearch(&pAd->ScanTab, pOidBssid, pAd->MlmeAux.Channel);
515         if (BssIdx == BSS_NOT_FOUND)
516         {
517                 DBGPRINT(RT_DEBUG_TRACE, ("CNTL - BSSID not found. reply NDIS_STATUS_NOT_ACCEPTED\n"));
518                 pAd->Mlme.CntlMachine.CurrState = CNTL_IDLE;
519                 return;
520         }
521
522         // copy the matched BSS entry from ScanTab to MlmeAux.SsidBssTab. Why?
523         // Because we need this entry to become the JOIN target in later on SYNC state machine
524         pAd->MlmeAux.BssIdx = 0;
525         pAd->MlmeAux.SsidBssTab.BssNr = 1;
526         NdisMoveMemory(&pAd->MlmeAux.SsidBssTab.BssEntry[0], &pAd->ScanTab.BssEntry[BssIdx], sizeof(BSS_ENTRY));
527
528         // 2002-11-26 skip the following checking. i.e. if user wants to re-connect to same AP
529         //   we just follow normal procedure. The reason of user doing this may because he/she changed
530         //   AP to another channel, but we still received BEACON from it thus don't claim Link Down.
531         //   Since user knows he's changed AP channel, he'll re-connect again. By skipping the following
532         //   checking, we'll disassociate then re-do normal association with this AP at the new channel.
533         // 2003-1-6 Re-enable this feature based on microsoft requirement which prefer not to re-do
534         //   connection when setting the same BSSID.
535         if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED) &&
536                 MAC_ADDR_EQUAL(pAd->CommonCfg.Bssid, pOidBssid))
537         {
538                 // already connected to the same BSSID, go back to idle state directly
539                 DBGPRINT(RT_DEBUG_TRACE, ("CNTL - already in this BSSID. ignore this SET_BSSID request\n"));
540                 pAd->Mlme.CntlMachine.CurrState = CNTL_IDLE;
541 #ifdef NATIVE_WPA_SUPPLICANT_SUPPORT
542             {
543                 union iwreq_data    wrqu;
544
545                 memset(wrqu.ap_addr.sa_data, 0, MAC_ADDR_LEN);
546                 memcpy(wrqu.ap_addr.sa_data, pAd->MlmeAux.Bssid, MAC_ADDR_LEN);
547                 wireless_send_event(pAd->net_dev, SIOCGIWAP, &wrqu, NULL);
548
549             }
550 #endif // NATIVE_WPA_SUPPLICANT_SUPPORT //
551         }
552         else
553         {
554                 if (INFRA_ON(pAd))
555                 {
556                         // disassoc from current AP first
557                         DBGPRINT(RT_DEBUG_TRACE, ("CNTL - disassociate with current AP ...\n"));
558                         DisassocParmFill(pAd, &DisassocReq, pAd->CommonCfg.Bssid, REASON_DISASSOC_STA_LEAVING);
559                         MlmeEnqueue(pAd, ASSOC_STATE_MACHINE, MT2_MLME_DISASSOC_REQ,
560                                                 sizeof(MLME_DISASSOC_REQ_STRUCT), &DisassocReq);
561
562                         pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_DISASSOC;
563                 }
564                 else
565                 {
566                         if (ADHOC_ON(pAd))
567                         {
568                                 DBGPRINT(RT_DEBUG_TRACE, ("CNTL - drop current ADHOC\n"));
569                                 LinkDown(pAd, FALSE);
570                                 OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED);
571                                 pAd->IndicateMediaState = NdisMediaStateDisconnected;
572                                 RTMP_IndicateMediaState(pAd);
573                 pAd->ExtraInfo = GENERAL_LINK_DOWN;
574                                 DBGPRINT(RT_DEBUG_TRACE, ("NDIS_STATUS_MEDIA_DISCONNECT Event C!\n"));
575                         }
576
577                         // Change the wepstatus to original wepstatus
578                         pAd->StaCfg.WepStatus   = pAd->StaCfg.OrigWepStatus;
579                         pAd->StaCfg.PairCipher  = pAd->StaCfg.OrigWepStatus;
580                         pAd->StaCfg.GroupCipher = pAd->StaCfg.OrigWepStatus;
581
582                         // Check cipher suite, AP must have more secured cipher than station setting
583                         // Set the Pairwise and Group cipher to match the intended AP setting
584                         // We can only connect to AP with less secured cipher setting
585                         if ((pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA) || (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPAPSK))
586                         {
587                                 pAd->StaCfg.GroupCipher = pAd->ScanTab.BssEntry[BssIdx].WPA.GroupCipher;
588
589                                 if (pAd->StaCfg.WepStatus == pAd->ScanTab.BssEntry[BssIdx].WPA.PairCipher)
590                                         pAd->StaCfg.PairCipher = pAd->ScanTab.BssEntry[BssIdx].WPA.PairCipher;
591                                 else if (pAd->ScanTab.BssEntry[BssIdx].WPA.PairCipherAux != Ndis802_11WEPDisabled)
592                                         pAd->StaCfg.PairCipher = pAd->ScanTab.BssEntry[BssIdx].WPA.PairCipherAux;
593                                 else    // There is no PairCipher Aux, downgrade our capability to TKIP
594                                         pAd->StaCfg.PairCipher = Ndis802_11Encryption2Enabled;
595                         }
596                         else if ((pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA2) || (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA2PSK))
597                         {
598                                 pAd->StaCfg.GroupCipher = pAd->ScanTab.BssEntry[BssIdx].WPA2.GroupCipher;
599
600                                 if (pAd->StaCfg.WepStatus == pAd->ScanTab.BssEntry[BssIdx].WPA2.PairCipher)
601                                         pAd->StaCfg.PairCipher = pAd->ScanTab.BssEntry[BssIdx].WPA2.PairCipher;
602                                 else if (pAd->ScanTab.BssEntry[BssIdx].WPA2.PairCipherAux != Ndis802_11WEPDisabled)
603                                         pAd->StaCfg.PairCipher = pAd->ScanTab.BssEntry[BssIdx].WPA2.PairCipherAux;
604                                 else    // There is no PairCipher Aux, downgrade our capability to TKIP
605                                         pAd->StaCfg.PairCipher = Ndis802_11Encryption2Enabled;
606
607                                 // RSN capability
608                                 pAd->StaCfg.RsnCapability = pAd->ScanTab.BssEntry[BssIdx].WPA2.RsnCapability;
609                         }
610
611                         // Set Mix cipher flag
612                         pAd->StaCfg.bMixCipher = (pAd->StaCfg.PairCipher == pAd->StaCfg.GroupCipher) ? FALSE : TRUE;
613                         if (pAd->StaCfg.bMixCipher == TRUE)
614                         {
615                                 // If mix cipher, re-build RSNIE
616                                 RTMPMakeRSNIE(pAd, pAd->StaCfg.AuthMode, pAd->StaCfg.WepStatus, 0);
617                         }
618                         // No active association, join the BSS immediately
619                         DBGPRINT(RT_DEBUG_TRACE, ("CNTL - joining %02x:%02x:%02x:%02x:%02x:%02x ...\n",
620                                 pOidBssid[0],pOidBssid[1],pOidBssid[2],pOidBssid[3],pOidBssid[4],pOidBssid[5]));
621
622                         JoinParmFill(pAd, &JoinReq, pAd->MlmeAux.BssIdx);
623                         MlmeEnqueue(pAd, SYNC_STATE_MACHINE, MT2_MLME_JOIN_REQ, sizeof(MLME_JOIN_REQ_STRUCT), &JoinReq);
624
625                         pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_JOIN;
626                 }
627         }
628 }
629
630 // Roaming is the only external request triggering CNTL state machine
631 // despite of other "SET OID" operation. All "SET OID" related oerations
632 // happen in sequence, because no other SET OID will be sent to this device
633 // until the the previous SET operation is complete (successful o failed).
634 // So, how do we quarantee this ROAMING request won't corrupt other "SET OID"?
635 // or been corrupted by other "SET OID"?
636 //
637 // IRQL = DISPATCH_LEVEL
638 VOID CntlMlmeRoamingProc(
639         IN PRTMP_ADAPTER pAd,
640         IN MLME_QUEUE_ELEM *Elem)
641 {
642         // TODO:
643         // AP in different channel may show lower RSSI than actual value??
644         // should we add a weighting factor to compensate it?
645         DBGPRINT(RT_DEBUG_TRACE,("CNTL - Roaming in MlmeAux.RoamTab...\n"));
646
647         NdisMoveMemory(&pAd->MlmeAux.SsidBssTab, &pAd->MlmeAux.RoamTab, sizeof(pAd->MlmeAux.RoamTab));
648         pAd->MlmeAux.SsidBssTab.BssNr = pAd->MlmeAux.RoamTab.BssNr;
649
650         BssTableSortByRssi(&pAd->MlmeAux.SsidBssTab);
651         pAd->MlmeAux.BssIdx = 0;
652         IterateOnBssTab(pAd);
653 }
654
655 #ifdef QOS_DLS_SUPPORT
656 /*
657         ==========================================================================
658         Description:
659
660         IRQL = DISPATCH_LEVEL
661
662         ==========================================================================
663 */
664 VOID CntlOidDLSSetupProc(
665         IN PRTMP_ADAPTER pAd,
666         IN MLME_QUEUE_ELEM *Elem)
667 {
668         PRT_802_11_DLS          pDLS = (PRT_802_11_DLS)Elem->Msg;
669         MLME_DLS_REQ_STRUCT     MlmeDlsReq;
670         INT                                     i;
671         USHORT                          reason = REASON_UNSPECIFY;
672
673         DBGPRINT(RT_DEBUG_TRACE,("CNTL - (OID set %02x:%02x:%02x:%02x:%02x:%02x with Valid=%d, Status=%d, TimeOut=%d, CountDownTimer=%d)\n",
674                 pDLS->MacAddr[0], pDLS->MacAddr[1], pDLS->MacAddr[2], pDLS->MacAddr[3], pDLS->MacAddr[4], pDLS->MacAddr[5],
675                 pDLS->Valid, pDLS->Status, pDLS->TimeOut, pDLS->CountDownTimer));
676
677         if (!pAd->CommonCfg.bDLSCapable)
678                 return;
679
680         // DLS will not be supported when Adhoc mode
681         if (INFRA_ON(pAd))
682         {
683                 for (i = 0; i < MAX_NUM_OF_DLS_ENTRY; i++)
684                 {
685                         if (pDLS->Valid && pAd->StaCfg.DLSEntry[i].Valid && (pAd->StaCfg.DLSEntry[i].Status == DLS_FINISH) &&
686                                 (pDLS->TimeOut == pAd->StaCfg.DLSEntry[i].TimeOut) && MAC_ADDR_EQUAL(pDLS->MacAddr, pAd->StaCfg.DLSEntry[i].MacAddr))
687                         {
688                                 // 1. Same setting, just drop it
689                                 DBGPRINT(RT_DEBUG_TRACE,("CNTL - setting unchanged\n"));
690                                 break;
691                         }
692                         else if (!pDLS->Valid && pAd->StaCfg.DLSEntry[i].Valid && (pAd->StaCfg.DLSEntry[i].Status == DLS_FINISH) &&
693                                 MAC_ADDR_EQUAL(pDLS->MacAddr, pAd->StaCfg.DLSEntry[i].MacAddr))
694                         {
695                                 // 2. Disable DLS link case, just tear down DLS link
696                                 reason = REASON_QOS_UNWANTED_MECHANISM;
697                                 pAd->StaCfg.DLSEntry[i].Valid   = FALSE;
698                                 pAd->StaCfg.DLSEntry[i].Status  = DLS_NONE;
699                                 DlsParmFill(pAd, &MlmeDlsReq, &pAd->StaCfg.DLSEntry[i], reason);
700                                 MlmeEnqueue(pAd, DLS_STATE_MACHINE, MT2_MLME_DLS_TEAR_DOWN, sizeof(MLME_DLS_REQ_STRUCT), &MlmeDlsReq);
701                                 DBGPRINT(RT_DEBUG_TRACE,("CNTL - start tear down procedure\n"));
702                                 break;
703                         }
704                         else if ((i < MAX_NUM_OF_DLS_ENTRY) && pDLS->Valid && !pAd->StaCfg.DLSEntry[i].Valid)
705                         {
706                                 // 3. Enable case, start DLS setup procedure
707                                 NdisMoveMemory(&pAd->StaCfg.DLSEntry[i], pDLS, sizeof(RT_802_11_DLS_UI));
708
709                                 //Update countdown timer
710                                 pAd->StaCfg.DLSEntry[i].CountDownTimer = pAd->StaCfg.DLSEntry[i].TimeOut;
711                                 DlsParmFill(pAd, &MlmeDlsReq, &pAd->StaCfg.DLSEntry[i], reason);
712                                 MlmeEnqueue(pAd, DLS_STATE_MACHINE, MT2_MLME_DLS_REQ, sizeof(MLME_DLS_REQ_STRUCT), &MlmeDlsReq);
713                                 DBGPRINT(RT_DEBUG_TRACE,("CNTL - DLS setup case\n"));
714                                 break;
715                         }
716                         else if ((i < MAX_NUM_OF_DLS_ENTRY) && pDLS->Valid && pAd->StaCfg.DLSEntry[i].Valid &&
717                                 (pAd->StaCfg.DLSEntry[i].Status == DLS_FINISH) && !MAC_ADDR_EQUAL(pDLS->MacAddr, pAd->StaCfg.DLSEntry[i].MacAddr))
718                         {
719                                 // 4. update mac case, tear down old DLS and setup new DLS
720                                 reason = REASON_QOS_UNWANTED_MECHANISM;
721                                 pAd->StaCfg.DLSEntry[i].Valid   = FALSE;
722                                 pAd->StaCfg.DLSEntry[i].Status  = DLS_NONE;
723                                 DlsParmFill(pAd, &MlmeDlsReq, &pAd->StaCfg.DLSEntry[i], reason);
724                                 MlmeEnqueue(pAd, DLS_STATE_MACHINE, MT2_MLME_DLS_TEAR_DOWN, sizeof(MLME_DLS_REQ_STRUCT), &MlmeDlsReq);
725                                 NdisMoveMemory(&pAd->StaCfg.DLSEntry[i], pDLS, sizeof(RT_802_11_DLS_UI));
726                                 DlsParmFill(pAd, &MlmeDlsReq, &pAd->StaCfg.DLSEntry[i], reason);
727                                 MlmeEnqueue(pAd, DLS_STATE_MACHINE, MT2_MLME_DLS_REQ, sizeof(MLME_DLS_REQ_STRUCT), &MlmeDlsReq);
728                                 DBGPRINT(RT_DEBUG_TRACE,("CNTL - DLS tear down and restart case\n"));
729                                 break;
730                         }
731                         else if (pDLS->Valid && pAd->StaCfg.DLSEntry[i].Valid &&
732                                 MAC_ADDR_EQUAL(pDLS->MacAddr, pAd->StaCfg.DLSEntry[i].MacAddr) && (pAd->StaCfg.DLSEntry[i].TimeOut != pDLS->TimeOut))
733                         {
734                                 // 5. update timeout case, start DLS setup procedure (no tear down)
735                                 pAd->StaCfg.DLSEntry[i].TimeOut = pDLS->TimeOut;
736                                 //Update countdown timer
737                                 pAd->StaCfg.DLSEntry[i].CountDownTimer = pAd->StaCfg.DLSEntry[i].TimeOut;
738                                 DlsParmFill(pAd, &MlmeDlsReq, &pAd->StaCfg.DLSEntry[i], reason);
739                                 MlmeEnqueue(pAd, DLS_STATE_MACHINE, MT2_MLME_DLS_REQ, sizeof(MLME_DLS_REQ_STRUCT), &MlmeDlsReq);
740                                 DBGPRINT(RT_DEBUG_TRACE,("CNTL - DLS update timeout case\n"));
741                                 break;
742                         }
743                         else if (pDLS->Valid && pAd->StaCfg.DLSEntry[i].Valid &&
744                                 (pAd->StaCfg.DLSEntry[i].Status != DLS_FINISH) && MAC_ADDR_EQUAL(pDLS->MacAddr, pAd->StaCfg.DLSEntry[i].MacAddr))
745                         {
746                                 // 6. re-setup case, start DLS setup procedure (no tear down)
747                                 DlsParmFill(pAd, &MlmeDlsReq, &pAd->StaCfg.DLSEntry[i], reason);
748                                 MlmeEnqueue(pAd, DLS_STATE_MACHINE, MT2_MLME_DLS_REQ, sizeof(MLME_DLS_REQ_STRUCT), &MlmeDlsReq);
749                                 DBGPRINT(RT_DEBUG_TRACE,("CNTL - DLS retry setup procedure\n"));
750                                 break;
751                         }
752                         else
753                         {
754                                 DBGPRINT(RT_DEBUG_WARN,("CNTL - DLS not changed in entry - %d - Valid=%d, Status=%d, TimeOut=%d\n",
755                                         i, pAd->StaCfg.DLSEntry[i].Valid, pAd->StaCfg.DLSEntry[i].Status, pAd->StaCfg.DLSEntry[i].TimeOut));
756                         }
757                 }
758         }
759 }
760 #endif // QOS_DLS_SUPPORT //
761
762 /*
763         ==========================================================================
764         Description:
765
766         IRQL = DISPATCH_LEVEL
767
768         ==========================================================================
769 */
770 VOID CntlWaitDisassocProc(
771         IN PRTMP_ADAPTER pAd,
772         IN MLME_QUEUE_ELEM *Elem)
773 {
774         MLME_START_REQ_STRUCT     StartReq;
775
776         if (Elem->MsgType == MT2_DISASSOC_CONF)
777         {
778                 DBGPRINT(RT_DEBUG_TRACE, ("CNTL - Dis-associate successful\n"));
779
780             if (pAd->CommonCfg.bWirelessEvent)
781                 {
782                         RTMPSendWirelessEvent(pAd, IW_DISASSOC_EVENT_FLAG, pAd->MacTab.Content[BSSID_WCID].Addr, BSS0, 0);
783                 }
784
785                 LinkDown(pAd, FALSE);
786
787                 // case 1. no matching BSS, and user wants ADHOC, so we just start a new one
788                 if ((pAd->MlmeAux.SsidBssTab.BssNr==0) && (pAd->StaCfg.BssType == BSS_ADHOC))
789                 {
790                         DBGPRINT(RT_DEBUG_TRACE, ("CNTL - No matching BSS, start a new ADHOC (Ssid=%s)...\n",pAd->MlmeAux.Ssid));
791                         StartParmFill(pAd, &StartReq, pAd->MlmeAux.Ssid, pAd->MlmeAux.SsidLen);
792                         MlmeEnqueue(pAd, SYNC_STATE_MACHINE, MT2_MLME_START_REQ, sizeof(MLME_START_REQ_STRUCT), &StartReq);
793                         pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_START;
794                 }
795                 // case 2. try each matched BSS
796                 else
797                 {
798                         pAd->MlmeAux.BssIdx = 0;
799
800                         IterateOnBssTab(pAd);
801                 }
802         }
803 }
804
805 /*
806         ==========================================================================
807         Description:
808
809         IRQL = DISPATCH_LEVEL
810
811         ==========================================================================
812 */
813 VOID CntlWaitJoinProc(
814         IN PRTMP_ADAPTER pAd,
815         IN MLME_QUEUE_ELEM *Elem)
816 {
817         USHORT                      Reason;
818         MLME_AUTH_REQ_STRUCT        AuthReq;
819
820         if (Elem->MsgType == MT2_JOIN_CONF)
821         {
822                 NdisMoveMemory(&Reason, Elem->Msg, sizeof(USHORT));
823                 if (Reason == MLME_SUCCESS)
824                 {
825                         // 1. joined an IBSS, we are pretty much done here
826                         if (pAd->MlmeAux.BssType == BSS_ADHOC)
827                         {
828                             //
829                                 // 5G bands rules of Japan:
830                                 // Ad hoc must be disabled in W53(ch52,56,60,64) channels.
831                                 //
832                                 if ( (pAd->CommonCfg.bIEEE80211H == 1) &&
833                       RadarChannelCheck(pAd, pAd->CommonCfg.Channel)
834                                    )
835                                 {
836                                         pAd->Mlme.CntlMachine.CurrState = CNTL_IDLE;
837                                         DBGPRINT(RT_DEBUG_TRACE, ("CNTL - Channel=%d, Join adhoc on W53(52,56,60,64) Channels are not accepted\n", pAd->CommonCfg.Channel));
838                                         return;
839                                 }
840
841                                 LinkUp(pAd, BSS_ADHOC);
842                                 pAd->Mlme.CntlMachine.CurrState = CNTL_IDLE;
843                                 DBGPRINT(RT_DEBUG_TRACE, ("CNTL - join the IBSS = %02x:%02x:%02x:%02x:%02x:%02x ...\n",
844                                 pAd->CommonCfg.Bssid[0],pAd->CommonCfg.Bssid[1],pAd->CommonCfg.Bssid[2],
845                                 pAd->CommonCfg.Bssid[3],pAd->CommonCfg.Bssid[4],pAd->CommonCfg.Bssid[5]));
846
847                 pAd->IndicateMediaState = NdisMediaStateConnected;
848                 pAd->ExtraInfo = GENERAL_LINK_UP;
849                         }
850                         // 2. joined a new INFRA network, start from authentication
851                         else
852                         {
853 #ifdef LEAP_SUPPORT
854                                 // Add AuthMode "LEAP" for CCX 1.X
855                                 if (pAd->StaCfg.LeapAuthMode == CISCO_AuthModeLEAP)
856                                 {
857                                         AuthParmFill(pAd, &AuthReq, pAd->MlmeAux.Bssid, CISCO_AuthModeLEAP);
858                                 }
859                                 else
860 #endif // LEAP_SUPPORT //
861                                 {
862                                         // either Ndis802_11AuthModeShared or Ndis802_11AuthModeAutoSwitch, try shared key first
863                                         if ((pAd->StaCfg.AuthMode == Ndis802_11AuthModeShared) ||
864                                                 (pAd->StaCfg.AuthMode == Ndis802_11AuthModeAutoSwitch))
865                                         {
866                                                 AuthParmFill(pAd, &AuthReq, pAd->MlmeAux.Bssid, Ndis802_11AuthModeShared);
867                                         }
868                                         else
869                                         {
870                                                 AuthParmFill(pAd, &AuthReq, pAd->MlmeAux.Bssid, Ndis802_11AuthModeOpen);
871                                         }
872                                 }
873                                 MlmeEnqueue(pAd, AUTH_STATE_MACHINE, MT2_MLME_AUTH_REQ,
874                                                         sizeof(MLME_AUTH_REQ_STRUCT), &AuthReq);
875
876                                 pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_AUTH;
877                         }
878                 }
879                 else
880                 {
881                         // 3. failed, try next BSS
882                         pAd->MlmeAux.BssIdx++;
883                         IterateOnBssTab(pAd);
884                 }
885         }
886 }
887
888
889 /*
890         ==========================================================================
891         Description:
892
893         IRQL = DISPATCH_LEVEL
894
895         ==========================================================================
896 */
897 VOID CntlWaitStartProc(
898         IN PRTMP_ADAPTER pAd,
899         IN MLME_QUEUE_ELEM *Elem)
900 {
901         USHORT      Result;
902
903         if (Elem->MsgType == MT2_START_CONF)
904         {
905                 NdisMoveMemory(&Result, Elem->Msg, sizeof(USHORT));
906                 if (Result == MLME_SUCCESS)
907                 {
908                     //
909                         // 5G bands rules of Japan:
910                         // Ad hoc must be disabled in W53(ch52,56,60,64) channels.
911                         //
912                         if ( (pAd->CommonCfg.bIEEE80211H == 1) &&
913                   RadarChannelCheck(pAd, pAd->CommonCfg.Channel)
914                            )
915                         {
916                                 pAd->Mlme.CntlMachine.CurrState = CNTL_IDLE;
917                                 DBGPRINT(RT_DEBUG_TRACE, ("CNTL - Channel=%d, Start adhoc on W53(52,56,60,64) Channels are not accepted\n", pAd->CommonCfg.Channel));
918                                 return;
919                         }
920 #ifdef DOT11_N_SUPPORT
921                         if (pAd->CommonCfg.PhyMode >= PHY_11ABGN_MIXED)
922                         {
923                                 N_ChannelCheck(pAd);
924                                 SetCommonHT(pAd);
925                                 NdisMoveMemory(&pAd->MlmeAux.AddHtInfo, &pAd->CommonCfg.AddHTInfo, sizeof(ADD_HT_INFO_IE));
926                                 RTMPCheckHt(pAd, BSSID_WCID, &pAd->CommonCfg.HtCapability, &pAd->CommonCfg.AddHTInfo);
927                                 pAd->StaActive.SupportedPhyInfo.bHtEnable = TRUE;
928                                 NdisZeroMemory(&pAd->StaActive.SupportedPhyInfo.MCSSet[0], 16);
929                                 NdisMoveMemory(&pAd->StaActive.SupportedPhyInfo.MCSSet[0], &pAd->CommonCfg.HtCapability.MCSSet[0], 16);
930                                 COPY_HTSETTINGS_FROM_MLME_AUX_TO_ACTIVE_CFG(pAd);
931
932                                 if ((pAd->CommonCfg.HtCapability.HtCapInfo.ChannelWidth  == BW_40) &&
933                                         (pAd->CommonCfg.AddHTInfo.AddHtInfo.ExtChanOffset == EXTCHA_ABOVE))
934                                 {
935                                         pAd->MlmeAux.CentralChannel = pAd->CommonCfg.Channel + 2;
936                                 }
937                                 else if ((pAd->CommonCfg.HtCapability.HtCapInfo.ChannelWidth  == BW_40) &&
938                                                  (pAd->CommonCfg.AddHTInfo.AddHtInfo.ExtChanOffset == EXTCHA_BELOW))
939                                 {
940                                         pAd->MlmeAux.CentralChannel = pAd->CommonCfg.Channel - 2;
941                                 }
942                         }
943                         else
944 #endif // DOT11_N_SUPPORT //
945                         {
946                                 pAd->StaActive.SupportedPhyInfo.bHtEnable = FALSE;
947                         }
948                         LinkUp(pAd, BSS_ADHOC);
949                         pAd->Mlme.CntlMachine.CurrState = CNTL_IDLE;
950                         // Before send beacon, driver need do radar detection
951                         if ((pAd->CommonCfg.Channel > 14 )
952                                 && (pAd->CommonCfg.bIEEE80211H == 1)
953                                 && RadarChannelCheck(pAd, pAd->CommonCfg.Channel))
954                         {
955                                 pAd->CommonCfg.RadarDetect.RDMode = RD_SILENCE_MODE;
956                                 pAd->CommonCfg.RadarDetect.RDCount = 0;
957 #ifdef DFS_SUPPORT
958                                 BbpRadarDetectionStart(pAd);
959 #endif // DFS_SUPPORT //
960                         }
961
962                         DBGPRINT(RT_DEBUG_TRACE, ("CNTL - start a new IBSS = %02x:%02x:%02x:%02x:%02x:%02x ...\n",
963                                 pAd->CommonCfg.Bssid[0],pAd->CommonCfg.Bssid[1],pAd->CommonCfg.Bssid[2],
964                                 pAd->CommonCfg.Bssid[3],pAd->CommonCfg.Bssid[4],pAd->CommonCfg.Bssid[5]));
965                 }
966                 else
967                 {
968                         DBGPRINT(RT_DEBUG_TRACE, ("CNTL - Start IBSS fail. BUG!!!!!\n"));
969                         pAd->Mlme.CntlMachine.CurrState = CNTL_IDLE;
970                 }
971         }
972 }
973
974 /*
975         ==========================================================================
976         Description:
977
978         IRQL = DISPATCH_LEVEL
979
980         ==========================================================================
981 */
982 VOID CntlWaitAuthProc(
983         IN PRTMP_ADAPTER pAd,
984         IN MLME_QUEUE_ELEM *Elem)
985 {
986         USHORT                       Reason;
987         MLME_ASSOC_REQ_STRUCT        AssocReq;
988         MLME_AUTH_REQ_STRUCT         AuthReq;
989
990         if (Elem->MsgType == MT2_AUTH_CONF)
991         {
992                 NdisMoveMemory(&Reason, Elem->Msg, sizeof(USHORT));
993                 if (Reason == MLME_SUCCESS)
994                 {
995                         DBGPRINT(RT_DEBUG_TRACE, ("CNTL - AUTH OK\n"));
996                         AssocParmFill(pAd, &AssocReq, pAd->MlmeAux.Bssid, pAd->MlmeAux.CapabilityInfo,
997                                                   ASSOC_TIMEOUT, pAd->StaCfg.DefaultListenCount);
998
999 #ifdef LEAP_SUPPORT
1000                         //
1001                         // Cisco Leap CCKM supported Re-association.
1002                         //
1003                         if (LEAP_CCKM_ON(pAd) && (pAd->StaCfg.CCKMLinkUpFlag == TRUE))
1004                         {
1005                                 //if CCKM is turn on , that's mean Fast Reauthentication
1006                                 //Use CCKM Reassociation instead of normal association for Fast Roaming.
1007                                 MlmeEnqueue(pAd, ASSOC_STATE_MACHINE, MT2_MLME_REASSOC_REQ,
1008                                                         sizeof(MLME_ASSOC_REQ_STRUCT), &AssocReq);
1009
1010                                 pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_REASSOC;
1011                         }
1012                         else
1013 #endif // LEAP_SUPPORT //
1014                         {
1015                                 MlmeEnqueue(pAd, ASSOC_STATE_MACHINE, MT2_MLME_ASSOC_REQ,
1016                                                         sizeof(MLME_ASSOC_REQ_STRUCT), &AssocReq);
1017
1018                                 pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_ASSOC;
1019                         }
1020                 }
1021                 else
1022                 {
1023                         // This fail may because of the AP already keep us in its MAC table without
1024                         // ageing-out. The previous authentication attempt must have let it remove us.
1025                         // so try Authentication again may help. For D-Link DWL-900AP+ compatibility.
1026                         DBGPRINT(RT_DEBUG_TRACE, ("CNTL - AUTH FAIL, try again...\n"));
1027 #ifdef LEAP_SUPPORT
1028                         //Add AuthMode "LEAP" for CCX 1.X
1029                         if (pAd->StaCfg.LeapAuthMode == CISCO_AuthModeLEAP)
1030                         {
1031                                 AuthParmFill(pAd, &AuthReq, pAd->MlmeAux.Bssid, CISCO_AuthModeLEAP);
1032                         }
1033                         else
1034 #endif // LEAP_SUPPORT //
1035                         {
1036                                 if ((pAd->StaCfg.AuthMode == Ndis802_11AuthModeShared) ||
1037                                         (pAd->StaCfg.AuthMode == Ndis802_11AuthModeAutoSwitch))
1038                                 {
1039                                         // either Ndis802_11AuthModeShared or Ndis802_11AuthModeAutoSwitch, try shared key first
1040                                         AuthParmFill(pAd, &AuthReq, pAd->MlmeAux.Bssid, Ndis802_11AuthModeShared);
1041                                 }
1042                                 else
1043                                 {
1044                                         AuthParmFill(pAd, &AuthReq, pAd->MlmeAux.Bssid, Ndis802_11AuthModeOpen);
1045                                 }
1046                         }
1047                         MlmeEnqueue(pAd, AUTH_STATE_MACHINE, MT2_MLME_AUTH_REQ,
1048                                                 sizeof(MLME_AUTH_REQ_STRUCT), &AuthReq);
1049
1050                         pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_AUTH2;
1051                 }
1052         }
1053 }
1054
1055 /*
1056         ==========================================================================
1057         Description:
1058
1059         IRQL = DISPATCH_LEVEL
1060
1061         ==========================================================================
1062 */
1063 VOID CntlWaitAuthProc2(
1064         IN PRTMP_ADAPTER pAd,
1065         IN MLME_QUEUE_ELEM *Elem)
1066 {
1067         USHORT                       Reason;
1068         MLME_ASSOC_REQ_STRUCT        AssocReq;
1069         MLME_AUTH_REQ_STRUCT         AuthReq;
1070
1071         if (Elem->MsgType == MT2_AUTH_CONF)
1072         {
1073                 NdisMoveMemory(&Reason, Elem->Msg, sizeof(USHORT));
1074                 if (Reason == MLME_SUCCESS)
1075                 {
1076                         DBGPRINT(RT_DEBUG_TRACE, ("CNTL - AUTH OK\n"));
1077                         AssocParmFill(pAd, &AssocReq, pAd->MlmeAux.Bssid, pAd->MlmeAux.CapabilityInfo,
1078                                                   ASSOC_TIMEOUT, pAd->StaCfg.DefaultListenCount);
1079                         MlmeEnqueue(pAd, ASSOC_STATE_MACHINE, MT2_MLME_ASSOC_REQ,
1080                                                 sizeof(MLME_ASSOC_REQ_STRUCT), &AssocReq);
1081
1082                         pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_ASSOC;
1083                 }
1084                 else
1085                 {
1086 #ifdef LEAP_SUPPORT
1087                         // Process LEAP first, since it use different control variable
1088                         // We don't want to affect other poven operation
1089                         if (pAd->StaCfg.LeapAuthMode == CISCO_AuthModeLEAP)
1090                         {
1091                                 // LEAP Auth not success, try next BSS
1092                                 DBGPRINT(RT_DEBUG_TRACE, ("CNTL - *LEAP* AUTH FAIL, give up; try next BSS\n"));
1093                                 DBGPRINT(RT_DEBUG_TRACE, ("Total match BSSID [=%d]\n", pAd->MlmeAux.SsidBssTab.BssNr));
1094                                 pAd->Mlme.CntlMachine.CurrState = CNTL_IDLE;
1095                                 pAd->MlmeAux.BssIdx++;
1096                                 IterateOnBssTab(pAd);
1097                         }
1098                         else
1099 #endif // LEAP_SUPPORT //
1100                         if ((pAd->StaCfg.AuthMode == Ndis802_11AuthModeAutoSwitch) &&
1101                                  (pAd->MlmeAux.Alg == Ndis802_11AuthModeShared))
1102                         {
1103                                 DBGPRINT(RT_DEBUG_TRACE, ("CNTL - AUTH FAIL, try OPEN system...\n"));
1104                                 AuthParmFill(pAd, &AuthReq, pAd->MlmeAux.Bssid, Ndis802_11AuthModeOpen);
1105                                 MlmeEnqueue(pAd, AUTH_STATE_MACHINE, MT2_MLME_AUTH_REQ,
1106                                                         sizeof(MLME_AUTH_REQ_STRUCT), &AuthReq);
1107
1108                                 pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_AUTH2;
1109                         }
1110                         else
1111                         {
1112                                 // not success, try next BSS
1113                                 DBGPRINT(RT_DEBUG_TRACE, ("CNTL - AUTH FAIL, give up; try next BSS\n"));
1114                                 pAd->Mlme.CntlMachine.CurrState = CNTL_IDLE; //???????
1115                                 pAd->MlmeAux.BssIdx++;
1116                                 IterateOnBssTab(pAd);
1117                         }
1118                 }
1119         }
1120 }
1121
1122 /*
1123         ==========================================================================
1124         Description:
1125
1126         IRQL = DISPATCH_LEVEL
1127
1128         ==========================================================================
1129 */
1130 VOID CntlWaitAssocProc(
1131         IN PRTMP_ADAPTER pAd,
1132         IN MLME_QUEUE_ELEM *Elem)
1133 {
1134         USHORT      Reason;
1135
1136         if (Elem->MsgType == MT2_ASSOC_CONF)
1137         {
1138                 NdisMoveMemory(&Reason, Elem->Msg, sizeof(USHORT));
1139                 if (Reason == MLME_SUCCESS)
1140                 {
1141                         LinkUp(pAd, BSS_INFRA);
1142                         pAd->Mlme.CntlMachine.CurrState = CNTL_IDLE;
1143                         DBGPRINT(RT_DEBUG_TRACE, ("CNTL - Association successful on BSS #%ld\n",pAd->MlmeAux.BssIdx));
1144
1145                         if (pAd->CommonCfg.bWirelessEvent)
1146                         {
1147                                 RTMPSendWirelessEvent(pAd, IW_ASSOC_EVENT_FLAG, pAd->MacTab.Content[BSSID_WCID].Addr, BSS0, 0);
1148                         }
1149                 }
1150                 else
1151                 {
1152                         // not success, try next BSS
1153                         DBGPRINT(RT_DEBUG_TRACE, ("CNTL - Association fails on BSS #%ld\n",pAd->MlmeAux.BssIdx));
1154                         pAd->MlmeAux.BssIdx++;
1155                         IterateOnBssTab(pAd);
1156                 }
1157         }
1158 }
1159
1160 /*
1161         ==========================================================================
1162         Description:
1163
1164         IRQL = DISPATCH_LEVEL
1165
1166         ==========================================================================
1167 */
1168 VOID CntlWaitReassocProc(
1169         IN PRTMP_ADAPTER pAd,
1170         IN MLME_QUEUE_ELEM *Elem)
1171 {
1172         USHORT      Result;
1173
1174         if (Elem->MsgType == MT2_REASSOC_CONF)
1175         {
1176                 NdisMoveMemory(&Result, Elem->Msg, sizeof(USHORT));
1177                 if (Result == MLME_SUCCESS)
1178                 {
1179                         //
1180                         // NDIS requires a new Link UP indication but no Link Down for RE-ASSOC
1181                         //
1182                         LinkUp(pAd, BSS_INFRA);
1183
1184                         // send wireless event - for association
1185                         if (pAd->CommonCfg.bWirelessEvent)
1186                                 RTMPSendWirelessEvent(pAd, IW_ASSOC_EVENT_FLAG, pAd->MacTab.Content[BSSID_WCID].Addr, BSS0, 0);
1187
1188
1189 #ifdef LEAP_SUPPORT
1190                         if (LEAP_CCKM_ON(pAd))
1191                         {
1192                                 STA_PORT_SECURED(pAd);
1193                                 pAd->StaCfg.WpaState = SS_FINISH;
1194                         }
1195 #endif // LEAP_SUPPORT //
1196                         pAd->Mlme.CntlMachine.CurrState = CNTL_IDLE;
1197                         DBGPRINT(RT_DEBUG_TRACE, ("CNTL - Re-assocition successful on BSS #%ld\n", pAd->MlmeAux.RoamIdx));
1198                 }
1199                 else
1200                 {
1201                         // reassoc failed, try to pick next BSS in the BSS Table
1202                         DBGPRINT(RT_DEBUG_TRACE, ("CNTL - Re-assocition fails on BSS #%ld\n", pAd->MlmeAux.RoamIdx));
1203                         pAd->MlmeAux.RoamIdx++;
1204                         IterateOnBssTab2(pAd);
1205                 }
1206         }
1207 }
1208
1209 /*
1210         ==========================================================================
1211         Description:
1212
1213         IRQL = DISPATCH_LEVEL
1214
1215         ==========================================================================
1216 */
1217 VOID LinkUp(
1218         IN PRTMP_ADAPTER pAd,
1219         IN UCHAR BssType)
1220 {
1221         ULONG   Now;
1222         UINT32  Data;
1223         BOOLEAN Cancelled;
1224         UCHAR   Value = 0, idx;
1225         MAC_TABLE_ENTRY *pEntry = NULL, *pCurrEntry;
1226
1227         if (RTMP_TEST_PSFLAG(pAd, fRTMP_PS_SET_PCI_CLK_OFF_COMMAND))
1228         {
1229                 RTMPPCIeLinkCtrlValueRestore(pAd, RESTORE_HALT);
1230                 RTMPusecDelay(6000);
1231                 pAd->bPCIclkOff = FALSE;
1232         }
1233
1234         pEntry = &pAd->MacTab.Content[BSSID_WCID];
1235
1236         //
1237         // ASSOC - DisassocTimeoutAction
1238         // CNTL - Dis-associate successful
1239         // !!! LINK DOWN !!!
1240         // [88888] OID_802_11_SSID should have returned NDTEST_WEP_AP2(Returned: )
1241         //
1242         // To prevent DisassocTimeoutAction to call Link down after we link up,
1243         // cancel the DisassocTimer no matter what it start or not.
1244         //
1245         RTMPCancelTimer(&pAd->MlmeAux.DisassocTimer,  &Cancelled);
1246
1247         COPY_SETTINGS_FROM_MLME_AUX_TO_ACTIVE_CFG(pAd);
1248
1249 #ifdef DOT11_N_SUPPORT
1250         COPY_HTSETTINGS_FROM_MLME_AUX_TO_ACTIVE_CFG(pAd);
1251 #endif // DOT11_N_SUPPORT //
1252         // It's quite difficult to tell if a newly added KEY is WEP or CKIP until a new BSS
1253         // is formed (either ASSOC/RE-ASSOC done or IBSS started. LinkUP should be a safe place
1254         // to examine if cipher algorithm switching is required.
1255         //rt2860b. Don't know why need this
1256         SwitchBetweenWepAndCkip(pAd);
1257
1258         // Before power save before link up function, We will force use 1R.
1259         // So after link up, check Rx antenna # again.
1260         RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &Value);
1261         if(pAd->Antenna.field.RxPath == 3)
1262         {
1263                 Value |= (0x10);
1264         }
1265         else if(pAd->Antenna.field.RxPath == 2)
1266         {
1267                 Value |= (0x8);
1268         }
1269         else if(pAd->Antenna.field.RxPath == 1)
1270         {
1271                 Value |= (0x0);
1272         }
1273         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, Value);
1274         pAd->StaCfg.BBPR3 = Value;
1275
1276         if (BssType == BSS_ADHOC)
1277         {
1278                 OPSTATUS_SET_FLAG(pAd, fOP_STATUS_ADHOC_ON);
1279                 OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_INFRA_ON);
1280
1281 #ifdef CARRIER_DETECTION_SUPPORT // Roger sync Carrier
1282                 // No carrier detection when adhoc
1283                 // CarrierDetectionStop(pAd);
1284                 pAd->CommonCfg.CarrierDetect.CD_State = CD_NORMAL;
1285 #endif // CARRIER_DETECTION_SUPPORT //
1286
1287                 DBGPRINT(RT_DEBUG_TRACE, ("!!!Adhoc LINK UP !!! \n" ));
1288         }
1289         else
1290         {
1291                 OPSTATUS_SET_FLAG(pAd, fOP_STATUS_INFRA_ON);
1292                 OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_ADHOC_ON);
1293
1294                 DBGPRINT(RT_DEBUG_TRACE, ("!!!Infra LINK UP !!! \n" ));
1295         }
1296
1297         // 3*3
1298         // reset Tx beamforming bit
1299         RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R4, &Value);
1300         Value &= (~0x01);
1301         Value |= pAd->CommonCfg.RegTransmitSetting.field.TxBF;
1302         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R4, Value);
1303
1304 #ifdef DOT11_N_SUPPORT
1305         // Change to AP channel
1306     if ((pAd->CommonCfg.CentralChannel > pAd->CommonCfg.Channel) && (pAd->MlmeAux.HtCapability.HtCapInfo.ChannelWidth == BW_40))
1307         {
1308                 // Must using 40MHz.
1309                 pAd->CommonCfg.BBPCurrentBW = BW_40;
1310                 AsicSwitchChannel(pAd, pAd->CommonCfg.CentralChannel, FALSE);
1311                 AsicLockChannel(pAd, pAd->CommonCfg.CentralChannel);
1312
1313                 RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R4, &Value);
1314                 Value &= (~0x18);
1315                 Value |= 0x10;
1316                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R4, Value);
1317
1318                 //  RX : control channel at lower
1319                 RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &Value);
1320                 Value &= (~0x20);
1321                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, Value);
1322         pAd->StaCfg.BBPR3 = Value;
1323
1324                 RTMP_IO_READ32(pAd, TX_BAND_CFG, &Data);
1325                 Data &= 0xfffffffe;
1326                 RTMP_IO_WRITE32(pAd, TX_BAND_CFG, Data);
1327
1328                 if (pAd->MACVersion == 0x28600100)
1329                 {
1330                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R69, 0x1A);
1331                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R70, 0x0A);
1332                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R73, 0x16);
1333             DBGPRINT(RT_DEBUG_TRACE, ("!!!rt2860C !!! \n" ));
1334                 }
1335
1336                 DBGPRINT(RT_DEBUG_TRACE, ("!!!40MHz Lower LINK UP !!! Control Channel at Below. Central = %d \n", pAd->CommonCfg.CentralChannel ));
1337         }
1338         else if ((pAd->CommonCfg.CentralChannel < pAd->CommonCfg.Channel) && (pAd->MlmeAux.HtCapability.HtCapInfo.ChannelWidth == BW_40))
1339     {
1340             // Must using 40MHz.
1341                 pAd->CommonCfg.BBPCurrentBW = BW_40;
1342                 AsicSwitchChannel(pAd, pAd->CommonCfg.CentralChannel, FALSE);
1343             AsicLockChannel(pAd, pAd->CommonCfg.CentralChannel);
1344
1345                 RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R4, &Value);
1346                 Value &= (~0x18);
1347                 Value |= 0x10;
1348                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R4, Value);
1349
1350                 RTMP_IO_READ32(pAd, TX_BAND_CFG, &Data);
1351                 Data |= 0x1;
1352                 RTMP_IO_WRITE32(pAd, TX_BAND_CFG, Data);
1353
1354                 RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &Value);
1355             Value |= (0x20);
1356                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, Value);
1357         pAd->StaCfg.BBPR3 = Value;
1358
1359                 if (pAd->MACVersion == 0x28600100)
1360                 {
1361                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R69, 0x1A);
1362                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R70, 0x0A);
1363                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R73, 0x16);
1364                             DBGPRINT(RT_DEBUG_TRACE, ("!!!rt2860C !!! \n" ));
1365                 }
1366
1367             DBGPRINT(RT_DEBUG_TRACE, ("!!! 40MHz Upper LINK UP !!! Control Channel at UpperCentral = %d \n", pAd->CommonCfg.CentralChannel ));
1368     }
1369     else
1370 #endif // DOT11_N_SUPPORT //
1371     {
1372             pAd->CommonCfg.BBPCurrentBW = BW_20;
1373                 pAd->CommonCfg.CentralChannel = pAd->CommonCfg.Channel;
1374                 AsicSwitchChannel(pAd, pAd->CommonCfg.Channel, FALSE);
1375                 AsicLockChannel(pAd, pAd->CommonCfg.Channel);
1376
1377                 RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R4, &Value);
1378                 Value &= (~0x18);
1379                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R4, Value);
1380
1381                 RTMP_IO_READ32(pAd, TX_BAND_CFG, &Data);
1382                 Data &= 0xfffffffe;
1383                 RTMP_IO_WRITE32(pAd, TX_BAND_CFG, Data);
1384
1385                 RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &Value);
1386                 Value &= (~0x20);
1387                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, Value);
1388         pAd->StaCfg.BBPR3 = Value;
1389
1390                 if (pAd->MACVersion == 0x28600100)
1391                 {
1392                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R69, 0x16);
1393                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R70, 0x08);
1394                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R73, 0x11);
1395             DBGPRINT(RT_DEBUG_TRACE, ("!!!rt2860C !!! \n" ));
1396                 }
1397
1398             DBGPRINT(RT_DEBUG_TRACE, ("!!! 20MHz LINK UP !!! \n" ));
1399     }
1400
1401         RTMPSetAGCInitValue(pAd, pAd->CommonCfg.BBPCurrentBW);
1402         //
1403         // Save BBP_R66 value, it will be used in RTUSBResumeMsduTransmission
1404         //
1405         RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R66, &pAd->BbpTuning.R66CurrentValue);
1406
1407         DBGPRINT(RT_DEBUG_TRACE, ("!!! LINK UP !!! (BssType=%d, AID=%d, ssid=%s, Channel=%d, CentralChannel = %d)\n",
1408                 BssType, pAd->StaActive.Aid, pAd->CommonCfg.Ssid, pAd->CommonCfg.Channel, pAd->CommonCfg.CentralChannel));
1409
1410 #ifdef DOT11_N_SUPPORT
1411         DBGPRINT(RT_DEBUG_TRACE, ("!!! LINK UP !!! (Density =%d, )\n", pAd->MacTab.Content[BSSID_WCID].MpduDensity));
1412 #endif // DOT11_N_SUPPORT //
1413
1414                 AsicSetBssid(pAd, pAd->CommonCfg.Bssid);
1415
1416         AsicSetSlotTime(pAd, TRUE);
1417         AsicSetEdcaParm(pAd, &pAd->CommonCfg.APEdcaParm);
1418
1419         // Call this for RTS protectionfor legacy rate, we will always enable RTS threshold, but normally it will not hit
1420         AsicUpdateProtect(pAd, 0, (OFDMSETPROTECT | CCKSETPROTECT), TRUE, FALSE);
1421
1422 #ifdef DOT11_N_SUPPORT
1423         if ((pAd->StaActive.SupportedPhyInfo.bHtEnable == TRUE))
1424         {
1425                 // Update HT protectionfor based on AP's operating mode.
1426         if (pAd->MlmeAux.AddHtInfo.AddHtInfo2.NonGfPresent == 1)
1427         {
1428                 AsicUpdateProtect(pAd, pAd->MlmeAux.AddHtInfo.AddHtInfo2.OperaionMode,  ALLN_SETPROTECT, FALSE, TRUE);
1429         }
1430         else
1431                         AsicUpdateProtect(pAd, pAd->MlmeAux.AddHtInfo.AddHtInfo2.OperaionMode,  ALLN_SETPROTECT, FALSE, FALSE);
1432         }
1433 #endif // DOT11_N_SUPPORT //
1434
1435         NdisZeroMemory(&pAd->DrsCounters, sizeof(COUNTER_DRS));
1436
1437         NdisGetSystemUpTime(&Now);
1438         pAd->StaCfg.LastBeaconRxTime = Now;   // last RX timestamp
1439
1440         if ((pAd->CommonCfg.TxPreamble != Rt802_11PreambleLong) &&
1441                 CAP_IS_SHORT_PREAMBLE_ON(pAd->StaActive.CapabilityInfo))
1442         {
1443                 MlmeSetTxPreamble(pAd, Rt802_11PreambleShort);
1444         }
1445
1446         OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_AGGREGATION_INUSED);
1447
1448         if (pAd->CommonCfg.RadarDetect.RDMode == RD_SILENCE_MODE)
1449         {
1450 #ifdef DFS_SUPPORT
1451                 RadarDetectionStop(pAd);
1452 #endif // DFS_SUPPORT //
1453         }
1454         pAd->CommonCfg.RadarDetect.RDMode = RD_NORMAL_MODE;
1455
1456         if (BssType == BSS_ADHOC)
1457         {
1458                 MakeIbssBeacon(pAd);
1459                 if ((pAd->CommonCfg.Channel > 14)
1460                         && (pAd->CommonCfg.bIEEE80211H == 1)
1461                         && RadarChannelCheck(pAd, pAd->CommonCfg.Channel))
1462                 {
1463                         ; //Do nothing
1464                 }
1465                 else
1466                 {
1467                         AsicEnableIbssSync(pAd);
1468                 }
1469
1470                 // In ad hoc mode, use MAC table from index 1.
1471                 // p.s ASIC use all 0xff as termination of WCID table search.To prevent it's 0xff-ff-ff-ff-ff-ff, Write 0 here.
1472                 RTMP_IO_WRITE32(pAd, MAC_WCID_BASE, 0x00);
1473                 RTMP_IO_WRITE32(pAd, 0x1808, 0x00);
1474
1475                 // If WEP is enabled, add key material and cipherAlg into Asic
1476                 // Fill in Shared Key Table(offset: 0x6c00) and Shared Key Mode(offset: 0x7000)
1477
1478                 if (pAd->StaCfg.WepStatus == Ndis802_11WEPEnabled)
1479                 {
1480                         PUCHAR  Key;
1481                         UCHAR   CipherAlg;
1482
1483                         for (idx=0; idx < SHARE_KEY_NUM; idx++)
1484                 {
1485                                 CipherAlg = pAd->SharedKey[BSS0][idx].CipherAlg;
1486                         Key = pAd->SharedKey[BSS0][idx].Key;
1487
1488                                 if (pAd->SharedKey[BSS0][idx].KeyLen > 0)
1489                                 {
1490                                         // Set key material and cipherAlg to Asic
1491                                 AsicAddSharedKeyEntry(pAd, BSS0, idx, CipherAlg, Key, NULL, NULL);
1492
1493                     if (idx == pAd->StaCfg.DefaultKeyId)
1494                                         {
1495                                                 // Update WCID attribute table and IVEIV table for this group key table
1496                                                 RTMPAddWcidAttributeEntry(pAd, BSS0, idx, CipherAlg, NULL);
1497                                         }
1498                                 }
1499
1500
1501                         }
1502                 }
1503                 // If WPANone is enabled, add key material and cipherAlg into Asic
1504                 // Fill in Shared Key Table(offset: 0x6c00) and Shared Key Mode(offset: 0x7000)
1505                 else if (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPANone)
1506                 {
1507                         pAd->StaCfg.DefaultKeyId = 0;   // always be zero
1508
1509             NdisZeroMemory(&pAd->SharedKey[BSS0][0], sizeof(CIPHER_KEY));
1510                                                         pAd->SharedKey[BSS0][0].KeyLen = LEN_TKIP_EK;
1511                         NdisMoveMemory(pAd->SharedKey[BSS0][0].Key, pAd->StaCfg.PMK, LEN_TKIP_EK);
1512
1513             if (pAd->StaCfg.PairCipher == Ndis802_11Encryption2Enabled)
1514             {
1515                         NdisMoveMemory(pAd->SharedKey[BSS0][0].RxMic, &pAd->StaCfg.PMK[16], LEN_TKIP_RXMICK);
1516                         NdisMoveMemory(pAd->SharedKey[BSS0][0].TxMic, &pAd->StaCfg.PMK[16], LEN_TKIP_TXMICK);
1517             }
1518
1519                         // Decide its ChiperAlg
1520                         if (pAd->StaCfg.PairCipher == Ndis802_11Encryption2Enabled)
1521                                 pAd->SharedKey[BSS0][0].CipherAlg = CIPHER_TKIP;
1522                         else if (pAd->StaCfg.PairCipher == Ndis802_11Encryption3Enabled)
1523                                 pAd->SharedKey[BSS0][0].CipherAlg = CIPHER_AES;
1524                         else
1525             {
1526                 DBGPRINT(RT_DEBUG_TRACE, ("Unknow Cipher (=%d), set Cipher to AES\n", pAd->StaCfg.PairCipher));
1527                                 pAd->SharedKey[BSS0][0].CipherAlg = CIPHER_AES;
1528             }
1529
1530                         // Set key material and cipherAlg to Asic
1531                         AsicAddSharedKeyEntry(pAd,
1532                                                                   BSS0,
1533                                                                   0,
1534                                                                   pAd->SharedKey[BSS0][0].CipherAlg,
1535                                                                   pAd->SharedKey[BSS0][0].Key,
1536                                                                   pAd->SharedKey[BSS0][0].TxMic,
1537                                                                   pAd->SharedKey[BSS0][0].RxMic);
1538
1539             // Update WCID attribute table and IVEIV table for this group key table
1540                         RTMPAddWcidAttributeEntry(pAd, BSS0, 0, pAd->SharedKey[BSS0][0].CipherAlg, NULL);
1541
1542                 }
1543
1544         }
1545         else // BSS_INFRA
1546         {
1547                 // Check the new SSID with last SSID
1548                 while (Cancelled == TRUE)
1549                 {
1550                         if (pAd->CommonCfg.LastSsidLen == pAd->CommonCfg.SsidLen)
1551                         {
1552                                 if (RTMPCompareMemory(pAd->CommonCfg.LastSsid, pAd->CommonCfg.Ssid, pAd->CommonCfg.LastSsidLen) == 0)
1553                                 {
1554                                         // Link to the old one no linkdown is required.
1555                                         break;
1556                                 }
1557                         }
1558                         // Send link down event before set to link up
1559                         pAd->IndicateMediaState = NdisMediaStateDisconnected;
1560                         RTMP_IndicateMediaState(pAd);
1561             pAd->ExtraInfo = GENERAL_LINK_DOWN;
1562                         DBGPRINT(RT_DEBUG_TRACE, ("NDIS_STATUS_MEDIA_DISCONNECT Event AA!\n"));
1563                         break;
1564                 }
1565
1566                 //
1567                 // On WPA mode, Remove All Keys if not connect to the last BSSID
1568                 // Key will be set after 4-way handshake.
1569                 //
1570                 if ((pAd->StaCfg.AuthMode >= Ndis802_11AuthModeWPA))
1571                 {
1572                         ULONG           IV;
1573
1574                         // Remove all WPA keys
1575                         RTMPWPARemoveAllKeys(pAd);
1576                         pAd->StaCfg.PortSecured = WPA_802_1X_PORT_NOT_SECURED;
1577                         pAd->StaCfg.PrivacyFilter = Ndis802_11PrivFilter8021xWEP;
1578
1579                         // Fixed connection failed with Range Maximizer - 515 AP (Marvell Chip) when security is WPAPSK/TKIP
1580                         // If IV related values are too large in GroupMsg2, AP would ignore this message.
1581                         IV = 0;
1582                         IV |= (pAd->StaCfg.DefaultKeyId << 30);
1583                         AsicUpdateWCIDIVEIV(pAd, BSSID_WCID, IV, 0);
1584
1585                         RTMP_CLEAR_PSFLAG(pAd, fRTMP_PS_CAN_GO_SLEEP);
1586                 }
1587                 // NOTE:
1588                 // the decision of using "short slot time" or not may change dynamically due to
1589                 // new STA association to the AP. so we have to decide that upon parsing BEACON, not here
1590
1591                 // NOTE:
1592                 // the decision to use "RTC/CTS" or "CTS-to-self" protection or not may change dynamically
1593                 // due to new STA association to the AP. so we have to decide that upon parsing BEACON, not here
1594
1595                 ComposePsPoll(pAd);
1596                 ComposeNullFrame(pAd);
1597
1598                         AsicEnableBssSync(pAd);
1599
1600                 // Add BSSID to WCID search table
1601                 AsicUpdateRxWCIDTable(pAd, BSSID_WCID, pAd->CommonCfg.Bssid);
1602
1603                 NdisAcquireSpinLock(&pAd->MacTabLock);
1604                 // add this BSSID entry into HASH table
1605                 {
1606                         UCHAR HashIdx;
1607
1608                         //pEntry = &pAd->MacTab.Content[BSSID_WCID];
1609                         HashIdx = MAC_ADDR_HASH_INDEX(pAd->CommonCfg.Bssid);
1610                         if (pAd->MacTab.Hash[HashIdx] == NULL)
1611                         {
1612                                 pAd->MacTab.Hash[HashIdx] = pEntry;
1613                         }
1614                         else
1615                         {
1616                                 pCurrEntry = pAd->MacTab.Hash[HashIdx];
1617                                 while (pCurrEntry->pNext != NULL)
1618                                         pCurrEntry = pCurrEntry->pNext;
1619                                 pCurrEntry->pNext = pEntry;
1620                         }
1621                 }
1622                 NdisReleaseSpinLock(&pAd->MacTabLock);
1623
1624
1625                 // If WEP is enabled, add paiewise and shared key
1626 #ifdef WPA_SUPPLICANT_SUPPORT
1627         if (((pAd->StaCfg.WpaSupplicantUP)&&
1628              (pAd->StaCfg.WepStatus == Ndis802_11WEPEnabled)&&
1629              (pAd->StaCfg.PortSecured == WPA_802_1X_PORT_SECURED)) ||
1630             ((pAd->StaCfg.WpaSupplicantUP == WPA_SUPPLICANT_DISABLE)&&
1631               (pAd->StaCfg.WepStatus == Ndis802_11WEPEnabled)))
1632 #else
1633                 if (pAd->StaCfg.WepStatus == Ndis802_11WEPEnabled)
1634 #endif // WPA_SUPPLICANT_SUPPORT //
1635                 {
1636                         PUCHAR  Key;
1637                         UCHAR   CipherAlg;
1638
1639                         for (idx=0; idx < SHARE_KEY_NUM; idx++)
1640                 {
1641                                 CipherAlg = pAd->SharedKey[BSS0][idx].CipherAlg;
1642                         Key = pAd->SharedKey[BSS0][idx].Key;
1643
1644                                 if (pAd->SharedKey[BSS0][idx].KeyLen > 0)
1645                                 {
1646                                         // Set key material and cipherAlg to Asic
1647                                 AsicAddSharedKeyEntry(pAd, BSS0, idx, CipherAlg, Key, NULL, NULL);
1648
1649                                         if (idx == pAd->StaCfg.DefaultKeyId)
1650                                         {
1651                                                 // Assign group key info
1652                                                 RTMPAddWcidAttributeEntry(pAd, BSS0, idx, CipherAlg, NULL);
1653
1654                                                 // Assign pairwise key info
1655                                                 RTMPAddWcidAttributeEntry(pAd, BSS0, idx, CipherAlg, pEntry);
1656                                         }
1657                                 }
1658                         }
1659                 }
1660
1661                 // only INFRASTRUCTURE mode need to indicate connectivity immediately; ADHOC mode
1662                 // should wait until at least 2 active nodes in this BSSID.
1663                 OPSTATUS_SET_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED);
1664
1665         // For GUI ++
1666                 if (pAd->StaCfg.AuthMode < Ndis802_11AuthModeWPA)
1667                 {
1668                         pAd->IndicateMediaState = NdisMediaStateConnected;
1669                         pAd->ExtraInfo = GENERAL_LINK_UP;
1670                 }
1671         // --
1672                 RTMP_IndicateMediaState(pAd);
1673
1674                 // Add BSSID in my MAC Table.
1675         NdisAcquireSpinLock(&pAd->MacTabLock);
1676                 RTMPMoveMemory(pAd->MacTab.Content[BSSID_WCID].Addr, pAd->CommonCfg.Bssid, MAC_ADDR_LEN);
1677                 pAd->MacTab.Content[BSSID_WCID].Aid = BSSID_WCID;
1678                 pAd->MacTab.Content[BSSID_WCID].pAd = pAd;
1679                 pAd->MacTab.Content[BSSID_WCID].ValidAsCLI = TRUE;      //Although this is bssid..still set ValidAsCl
1680                 pAd->MacTab.Size = 1;   // infra mode always set MACtab size =1.
1681                 pAd->MacTab.Content[BSSID_WCID].Sst = SST_ASSOC;
1682                 pAd->MacTab.Content[BSSID_WCID].AuthState = SST_ASSOC;
1683                 pAd->MacTab.Content[BSSID_WCID].WepStatus = pAd->StaCfg.WepStatus;
1684         NdisReleaseSpinLock(&pAd->MacTabLock);
1685
1686                 DBGPRINT(RT_DEBUG_TRACE, ("!!! LINK UP !!!  ClientStatusFlags=%lx)\n",
1687                         pAd->MacTab.Content[BSSID_WCID].ClientStatusFlags));
1688
1689                 MlmeUpdateTxRates(pAd, TRUE, BSS0);
1690 #ifdef DOT11_N_SUPPORT
1691                 MlmeUpdateHtTxRates(pAd, BSS0);
1692                 DBGPRINT(RT_DEBUG_TRACE, ("!!! LINK UP !! (StaActive.bHtEnable =%d, )\n", pAd->StaActive.SupportedPhyInfo.bHtEnable));
1693 #endif // DOT11_N_SUPPORT //
1694
1695                 //
1696                 // Report Adjacent AP report.
1697                 //
1698 #ifdef LEAP_SUPPORT
1699                 CCXAdjacentAPReport(pAd);
1700 #endif // LEAP_SUPPORT //
1701
1702                 if (pAd->CommonCfg.bAggregationCapable)
1703                 {
1704                         if ((pAd->CommonCfg.bPiggyBackCapable) && (pAd->MlmeAux.APRalinkIe & 0x00000003) == 3)
1705                         {
1706
1707                                 OPSTATUS_SET_FLAG(pAd, fOP_STATUS_PIGGYBACK_INUSED);
1708                                 OPSTATUS_SET_FLAG(pAd, fOP_STATUS_AGGREGATION_INUSED);
1709                 RTMPSetPiggyBack(pAd, TRUE);
1710                                 DBGPRINT(RT_DEBUG_TRACE, ("Turn on Piggy-Back\n"));
1711                         }
1712                         else if (pAd->MlmeAux.APRalinkIe & 0x00000001)
1713                         {
1714                                 OPSTATUS_SET_FLAG(pAd, fOP_STATUS_AGGREGATION_INUSED);
1715                         }
1716                 }
1717
1718                 if (pAd->MlmeAux.APRalinkIe != 0x0)
1719                 {
1720 #ifdef DOT11_N_SUPPORT
1721                         if (CLIENT_STATUS_TEST_FLAG(&pAd->MacTab.Content[BSSID_WCID], fCLIENT_STATUS_RDG_CAPABLE))
1722                         {
1723                                 AsicEnableRDG(pAd);
1724                         }
1725 #endif // DOT11_N_SUPPORT //
1726                         OPSTATUS_SET_FLAG(pAd, fCLIENT_STATUS_RALINK_CHIPSET);
1727                         CLIENT_STATUS_SET_FLAG(&pAd->MacTab.Content[BSSID_WCID], fCLIENT_STATUS_RALINK_CHIPSET);
1728                 }
1729                 else
1730                 {
1731                         OPSTATUS_CLEAR_FLAG(pAd, fCLIENT_STATUS_RALINK_CHIPSET);
1732                         CLIENT_STATUS_CLEAR_FLAG(&pAd->MacTab.Content[BSSID_WCID], fCLIENT_STATUS_RALINK_CHIPSET);
1733                 }
1734         }
1735
1736 #ifdef DOT11_N_SUPPORT
1737         DBGPRINT(RT_DEBUG_TRACE, ("NDIS_STATUS_MEDIA_CONNECT Event B!.BACapability = %x. ClientStatusFlags = %lx\n", pAd->CommonCfg.BACapability.word, pAd->MacTab.Content[BSSID_WCID].ClientStatusFlags));
1738 #endif // DOT11_N_SUPPORT //
1739
1740         // Set LED
1741         RTMPSetLED(pAd, LED_LINK_UP);
1742
1743         pAd->Mlme.PeriodicRound = 0;
1744         pAd->Mlme.OneSecPeriodicRound = 0;
1745         pAd->bConfigChanged = FALSE;        // Reset config flag
1746         pAd->ExtraInfo = GENERAL_LINK_UP;   // Update extra information to link is up
1747
1748         // Set asic auto fall back
1749         {
1750                 PUCHAR                                  pTable;
1751                 UCHAR                                   TableSize = 0;
1752
1753                 MlmeSelectTxRateTable(pAd, &pAd->MacTab.Content[BSSID_WCID], &pTable, &TableSize, &pAd->CommonCfg.TxRateIndex);
1754                 AsicUpdateAutoFallBackTable(pAd, pTable);
1755         }
1756
1757         NdisAcquireSpinLock(&pAd->MacTabLock);
1758     pEntry->HTPhyMode.word = pAd->StaCfg.HTPhyMode.word;
1759     pEntry->MaxHTPhyMode.word = pAd->StaCfg.HTPhyMode.word;
1760         if (pAd->StaCfg.bAutoTxRateSwitch == FALSE)
1761         {
1762                 pEntry->bAutoTxRateSwitch = FALSE;
1763 #ifdef DOT11_N_SUPPORT
1764                 if (pEntry->HTPhyMode.field.MCS == 32)
1765                         pEntry->HTPhyMode.field.ShortGI = GI_800;
1766
1767                 if ((pEntry->HTPhyMode.field.MCS > MCS_7) || (pEntry->HTPhyMode.field.MCS == 32))
1768                         pEntry->HTPhyMode.field.STBC = STBC_NONE;
1769 #endif // DOT11_N_SUPPORT //
1770                 // If the legacy mode is set, overwrite the transmit setting of this entry.
1771                 if (pEntry->HTPhyMode.field.MODE <= MODE_OFDM)
1772                         RTMPUpdateLegacyTxSetting((UCHAR)pAd->StaCfg.DesiredTransmitSetting.field.FixedTxMode, pEntry);
1773         }
1774         else
1775                 pEntry->bAutoTxRateSwitch = TRUE;
1776         NdisReleaseSpinLock(&pAd->MacTabLock);
1777
1778         //  Let Link Status Page display first initial rate.
1779         pAd->LastTxRate = (USHORT)(pEntry->HTPhyMode.word);
1780         // Select DAC according to HT or Legacy
1781         if (pAd->StaActive.SupportedPhyInfo.MCSSet[0] != 0x00)
1782         {
1783                 RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R1, &Value);
1784                 Value &= (~0x18);
1785                 if (pAd->Antenna.field.TxPath == 2)
1786                 {
1787                     Value |= 0x10;
1788                 }
1789                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R1, Value);
1790         }
1791         else
1792         {
1793                 RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R1, &Value);
1794                 Value &= (~0x18);
1795                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R1, Value);
1796         }
1797
1798 #ifdef DOT11_N_SUPPORT
1799         if (pAd->StaActive.SupportedPhyInfo.bHtEnable == FALSE)
1800         {
1801         }
1802         else if (pEntry->MaxRAmpduFactor == 0)
1803         {
1804             // If HT AP doesn't support MaxRAmpduFactor = 1, we need to set max PSDU to 0.
1805             // Because our Init value is 1 at MACRegTable.
1806                 RTMP_IO_WRITE32(pAd, MAX_LEN_CFG, 0x0fff);
1807         }
1808 #endif // DOT11_N_SUPPORT //
1809
1810         // Patch for Marvel AP to gain high throughput
1811         // Need to set as following,
1812         // 1. Set txop in register-EDCA_AC0_CFG as 0x60
1813         // 2. Set EnTXWriteBackDDONE in register-WPDMA_GLO_CFG as zero
1814         // 3. PBF_MAX_PCNT as 0x1F3FBF9F
1815         // 4. kick per two packets when dequeue
1816         //
1817         // Txop can only be modified when RDG is off, WMM is disable and TxBurst is enable
1818         //
1819         // if 1. Legacy AP WMM on,  or 2. 11n AP, AMPDU disable.  Force turn off burst no matter what bEnableTxBurst is.
1820 #ifdef DOT11_N_SUPPORT
1821         if (((pAd->StaActive.SupportedPhyInfo.bHtEnable == FALSE) && (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_WMM_INUSED)))
1822                 || ((pAd->StaActive.SupportedPhyInfo.bHtEnable == TRUE) && (pAd->CommonCfg.BACapability.field.Policy == BA_NOTUSE)))
1823         {
1824                 RTMP_IO_READ32(pAd, EDCA_AC0_CFG, &Data);
1825                 Data  &= 0xFFFFFF00;
1826                 RTMP_IO_WRITE32(pAd, EDCA_AC0_CFG, Data);
1827
1828                 RTMP_IO_WRITE32(pAd, PBF_MAX_PCNT, 0x1F3F7F9F);
1829                 DBGPRINT(RT_DEBUG_TRACE, ("Txburst 1\n"));
1830         }
1831         else
1832 #endif // DOT11_N_SUPPORT //
1833         if (pAd->CommonCfg.bEnableTxBurst)
1834         {
1835                 RTMP_IO_READ32(pAd, EDCA_AC0_CFG, &Data);
1836                 Data  &= 0xFFFFFF00;
1837                 Data  |= 0x60;
1838                 RTMP_IO_WRITE32(pAd, EDCA_AC0_CFG, Data);
1839                 pAd->CommonCfg.IOTestParm.bNowAtherosBurstOn = TRUE;
1840
1841                 RTMP_IO_WRITE32(pAd, PBF_MAX_PCNT, 0x1F3FBF9F);
1842                 DBGPRINT(RT_DEBUG_TRACE, ("Txburst 2\n"));
1843         }
1844         else
1845         {
1846                 RTMP_IO_READ32(pAd, EDCA_AC0_CFG, &Data);
1847                 Data  &= 0xFFFFFF00;
1848                 RTMP_IO_WRITE32(pAd, EDCA_AC0_CFG, Data);
1849
1850                 RTMP_IO_WRITE32(pAd, PBF_MAX_PCNT, 0x1F3F7F9F);
1851                 DBGPRINT(RT_DEBUG_TRACE, ("Txburst 3\n"));
1852         }
1853
1854 #ifdef DOT11_N_SUPPORT
1855         // Re-check to turn on TX burst or not.
1856         if ((pAd->CommonCfg.IOTestParm.bLastAtheros == TRUE) && ((STA_WEP_ON(pAd))||(STA_TKIP_ON(pAd))))
1857         {
1858                 pAd->CommonCfg.IOTestParm.bNextDisableRxBA = TRUE;
1859                 if (pAd->CommonCfg.bEnableTxBurst)
1860                 {
1861                     UINT32 MACValue = 0;
1862                         // Force disable  TXOP value in this case. The same action in MLMEUpdateProtect too.
1863                         // I didn't change PBF_MAX_PCNT setting.
1864                         RTMP_IO_READ32(pAd, EDCA_AC0_CFG, &MACValue);
1865                         MACValue  &= 0xFFFFFF00;
1866                         RTMP_IO_WRITE32(pAd, EDCA_AC0_CFG, MACValue);
1867                         pAd->CommonCfg.IOTestParm.bNowAtherosBurstOn = FALSE;
1868                 }
1869         }
1870         else
1871         {
1872                 pAd->CommonCfg.IOTestParm.bNextDisableRxBA = FALSE;
1873         }
1874 #endif // DOT11_N_SUPPORT //
1875
1876         pAd->CommonCfg.IOTestParm.bLastAtheros = FALSE;
1877         COPY_MAC_ADDR(pAd->CommonCfg.LastBssid, pAd->CommonCfg.Bssid);
1878         DBGPRINT(RT_DEBUG_TRACE, ("!!!pAd->bNextDisableRxBA= %d \n", pAd->CommonCfg.IOTestParm.bNextDisableRxBA));
1879         // BSSID add in one MAC entry too.  Because in Tx, ASIC need to check Cipher and IV/EIV, BAbitmap
1880         // Pther information in MACTab.Content[BSSID_WCID] is not necessary for driver.
1881         // Note: As STA, The MACTab.Content[BSSID_WCID]. PairwiseKey and Shared Key for BSS0 are the same.
1882
1883     if (pAd->StaCfg.WepStatus <= Ndis802_11WEPDisabled)
1884     {
1885         pAd->StaCfg.PortSecured = WPA_802_1X_PORT_SECURED;
1886                 pAd->StaCfg.PrivacyFilter = Ndis802_11PrivFilterAcceptAll;
1887         }
1888
1889         NdisAcquireSpinLock(&pAd->MacTabLock);
1890         pEntry->PortSecured = pAd->StaCfg.PortSecured;
1891         NdisReleaseSpinLock(&pAd->MacTabLock);
1892
1893     //
1894         // Patch Atheros AP TX will breakdown issue.
1895         // AP Model: DLink DWL-8200AP
1896         //
1897         if (INFRA_ON(pAd) && OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_WMM_INUSED) && STA_TKIP_ON(pAd))
1898         {
1899                 RTMP_IO_WRITE32(pAd, RX_PARSER_CFG, 0x01);
1900         }
1901         else
1902         {
1903                 RTMP_IO_WRITE32(pAd, RX_PARSER_CFG, 0x00);
1904         }
1905
1906         RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS);
1907         RTMP_CLEAR_PSFLAG(pAd, fRTMP_PS_GO_TO_SLEEP_NOW);
1908
1909 #ifdef DOT11_N_SUPPORT
1910 #ifdef DOT11N_DRAFT3
1911         if ((pAd->CommonCfg.BACapability.field.b2040CoexistScanSup) && (pAd->CommonCfg.Channel <= 11))
1912         {
1913                 OPSTATUS_SET_FLAG(pAd, fOP_STATUS_SCAN_2040);
1914                 BuildEffectedChannelList(pAd);
1915         }
1916 #endif // DOT11N_DRAFT3 //
1917 #endif // DOT11_N_SUPPORT //
1918 }
1919
1920 /*
1921         ==========================================================================
1922
1923         Routine Description:
1924                 Disconnect current BSSID
1925
1926         Arguments:
1927                 pAd                             - Pointer to our adapter
1928                 IsReqFromAP             - Request from AP
1929
1930         Return Value:
1931                 None
1932
1933         IRQL = DISPATCH_LEVEL
1934
1935         Note:
1936                 We need more information to know it's this requst from AP.
1937                 If yes! we need to do extra handling, for example, remove the WPA key.
1938                 Otherwise on 4-way handshaking will faied, since the WPA key didn't be
1939                 remove while auto reconnect.
1940                 Disconnect request from AP, it means we will start afresh 4-way handshaking
1941                 on WPA mode.
1942
1943         ==========================================================================
1944 */
1945 VOID LinkDown(
1946         IN PRTMP_ADAPTER pAd,
1947         IN  BOOLEAN      IsReqFromAP)
1948 {
1949         UCHAR                       i, ByteValue = 0;
1950         BOOLEAN         Cancelled;
1951
1952         // Do nothing if monitor mode is on
1953         if (MONITOR_ON(pAd))
1954                 return;
1955
1956         RTMP_CLEAR_PSFLAG(pAd, fRTMP_PS_GO_TO_SLEEP_NOW);
1957         RTMPCancelTimer(&pAd->Mlme.PsPollTimer,         &Cancelled);
1958
1959         // Not allow go to sleep within linkdown function.
1960         RTMP_CLEAR_PSFLAG(pAd, fRTMP_PS_CAN_GO_SLEEP);
1961
1962     if (pAd->CommonCfg.bWirelessEvent)
1963         {
1964                 RTMPSendWirelessEvent(pAd, IW_STA_LINKDOWN_EVENT_FLAG, pAd->MacTab.Content[BSSID_WCID].Addr, BSS0, 0);
1965         }
1966
1967         DBGPRINT(RT_DEBUG_TRACE, ("!!! LINK DOWN !!!\n"));
1968         OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_AGGREGATION_INUSED);
1969
1970     if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_ADVANCE_POWER_SAVE_PCIE_DEVICE))
1971     {
1972             BOOLEAN Cancelled;
1973         pAd->Mlme.bPsPollTimerRunning = FALSE;
1974         RTMPCancelTimer(&pAd->Mlme.PsPollTimer, &Cancelled);
1975     }
1976
1977     if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_DOZE) ||
1978                 RTMP_TEST_PSFLAG(pAd, fRTMP_PS_SET_PCI_CLK_OFF_COMMAND) ||
1979                 RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_IDLE_RADIO_OFF))
1980     {
1981                 AsicForceWakeup(pAd, RTMP_HALT);
1982         OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_DOZE);
1983     }
1984
1985     pAd->bPCIclkOff = FALSE;
1986         if (ADHOC_ON(pAd))              // Adhoc mode link down
1987         {
1988                 DBGPRINT(RT_DEBUG_TRACE, ("!!! LINK DOWN 1!!!\n"));
1989
1990                 OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_ADHOC_ON);
1991                 OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED);
1992                 pAd->IndicateMediaState = NdisMediaStateDisconnected;
1993                 RTMP_IndicateMediaState(pAd);
1994         pAd->ExtraInfo = GENERAL_LINK_DOWN;
1995                 BssTableDeleteEntry(&pAd->ScanTab, pAd->CommonCfg.Bssid, pAd->CommonCfg.Channel);
1996                 DBGPRINT(RT_DEBUG_TRACE, ("!!! MacTab.Size=%d !!!\n", pAd->MacTab.Size));
1997         }
1998         else                                    // Infra structure mode
1999         {
2000                 DBGPRINT(RT_DEBUG_TRACE, ("!!! LINK DOWN 2!!!\n"));
2001
2002 #ifdef QOS_DLS_SUPPORT
2003                 // DLS tear down frame must be sent before link down
2004                 // send DLS-TEAR_DOWN message
2005                 if (pAd->CommonCfg.bDLSCapable)
2006                 {
2007                         // tear down local dls table entry
2008                         for (i=0; i<MAX_NUM_OF_INIT_DLS_ENTRY; i++)
2009                         {
2010                                 if (pAd->StaCfg.DLSEntry[i].Valid && (pAd->StaCfg.DLSEntry[i].Status == DLS_FINISH))
2011                                 {
2012                                         pAd->StaCfg.DLSEntry[i].Status = DLS_NONE;
2013                                         RTMPSendDLSTearDownFrame(pAd, pAd->StaCfg.DLSEntry[i].MacAddr);
2014                                 }
2015                         }
2016
2017                         // tear down peer dls table entry
2018                         for (i=MAX_NUM_OF_INIT_DLS_ENTRY; i<MAX_NUM_OF_DLS_ENTRY; i++)
2019                         {
2020                                 if (pAd->StaCfg.DLSEntry[i].Valid && (pAd->StaCfg.DLSEntry[i].Status ==  DLS_FINISH))
2021                                 {
2022                                         pAd->StaCfg.DLSEntry[i].Status = DLS_NONE;
2023                                         RTMPSendDLSTearDownFrame(pAd, pAd->StaCfg.DLSEntry[i].MacAddr);
2024                                 }
2025                         }
2026                 }
2027 #endif // QOS_DLS_SUPPORT //
2028
2029                 OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_INFRA_ON);
2030                 OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED);
2031
2032                 // Saved last SSID for linkup comparison
2033                 pAd->CommonCfg.LastSsidLen = pAd->CommonCfg.SsidLen;
2034                 NdisMoveMemory(pAd->CommonCfg.LastSsid, pAd->CommonCfg.Ssid, pAd->CommonCfg.LastSsidLen);
2035                 COPY_MAC_ADDR(pAd->CommonCfg.LastBssid, pAd->CommonCfg.Bssid);
2036                 if (pAd->MlmeAux.CurrReqIsFromNdis == TRUE)
2037                 {
2038                         pAd->IndicateMediaState = NdisMediaStateDisconnected;
2039                         RTMP_IndicateMediaState(pAd);
2040             pAd->ExtraInfo = GENERAL_LINK_DOWN;
2041                         DBGPRINT(RT_DEBUG_TRACE, ("NDIS_STATUS_MEDIA_DISCONNECT Event A!\n"));
2042                         pAd->MlmeAux.CurrReqIsFromNdis = FALSE;
2043                 }
2044                 else
2045                 {
2046             //
2047                         // If disassociation request is from NDIS, then we don't need to delete BSSID from entry.
2048                         // Otherwise lost beacon or receive De-Authentication from AP,
2049                         // then we should delete BSSID from BssTable.
2050                         // If we don't delete from entry, roaming will fail.
2051                         //
2052                         BssTableDeleteEntry(&pAd->ScanTab, pAd->CommonCfg.Bssid, pAd->CommonCfg.Channel);
2053                 }
2054
2055                 // restore back to -
2056                 //      1. long slot (20 us) or short slot (9 us) time
2057                 //      2. turn on/off RTS/CTS and/or CTS-to-self protection
2058                 //      3. short preamble
2059                 OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_BG_PROTECTION_INUSED);
2060
2061                 if (pAd->StaCfg.CCXAdjacentAPReportFlag == TRUE)
2062                 {
2063                         //
2064                         // Record current AP's information.
2065                         // for later used reporting Adjacent AP report.
2066                         //
2067                         pAd->StaCfg.CCXAdjacentAPChannel = pAd->CommonCfg.Channel;
2068                         pAd->StaCfg.CCXAdjacentAPSsidLen = pAd->CommonCfg.SsidLen;
2069                         NdisMoveMemory(pAd->StaCfg.CCXAdjacentAPSsid, pAd->CommonCfg.Ssid, pAd->StaCfg.CCXAdjacentAPSsidLen);
2070                         COPY_MAC_ADDR(pAd->StaCfg.CCXAdjacentAPBssid, pAd->CommonCfg.Bssid);
2071                 }
2072
2073 #ifdef EXT_BUILD_CHANNEL_LIST
2074                 // Country IE of the AP will be evaluated and will be used.
2075                 if (pAd->StaCfg.IEEE80211dClientMode != Rt802_11_D_None)
2076                 {
2077                         NdisMoveMemory(&pAd->CommonCfg.CountryCode[0], &pAd->StaCfg.StaOriCountryCode[0], 2);
2078                         pAd->CommonCfg.Geography = pAd->StaCfg.StaOriGeography;
2079                         BuildChannelListEx(pAd);
2080                 }
2081 #endif // EXT_BUILD_CHANNEL_LIST //
2082
2083         }
2084
2085         for (i=1; i<MAX_LEN_OF_MAC_TABLE; i++)
2086         {
2087                 if (pAd->MacTab.Content[i].ValidAsCLI == TRUE)
2088                         MacTableDeleteEntry(pAd, pAd->MacTab.Content[i].Aid, pAd->MacTab.Content[i].Addr);
2089         }
2090
2091         pAd->StaCfg.CCXQosECWMin        = 4;
2092         pAd->StaCfg.CCXQosECWMax        = 10;
2093
2094         AsicSetSlotTime(pAd, TRUE); //FALSE);
2095         AsicSetEdcaParm(pAd, NULL);
2096
2097         // Set LED
2098         RTMPSetLED(pAd, LED_LINK_DOWN);
2099     pAd->LedIndicatorStregth = 0xF0;
2100     RTMPSetSignalLED(pAd, -100);        // Force signal strength Led to be turned off, firmware is not done it.
2101
2102                 AsicDisableSync(pAd);
2103
2104         pAd->Mlme.PeriodicRound = 0;
2105         pAd->Mlme.OneSecPeriodicRound = 0;
2106
2107         if (pAd->StaCfg.BssType == BSS_INFRA)
2108         {
2109                 // Remove StaCfg Information after link down
2110                 NdisZeroMemory(pAd->CommonCfg.Bssid, MAC_ADDR_LEN);
2111                 NdisZeroMemory(pAd->CommonCfg.Ssid, MAX_LEN_OF_SSID);
2112                 pAd->CommonCfg.SsidLen = 0;
2113         }
2114 #ifdef DOT11_N_SUPPORT
2115         NdisZeroMemory(&pAd->MlmeAux.HtCapability, sizeof(HT_CAPABILITY_IE));
2116         NdisZeroMemory(&pAd->MlmeAux.AddHtInfo, sizeof(ADD_HT_INFO_IE));
2117         pAd->MlmeAux.HtCapabilityLen = 0;
2118         pAd->MlmeAux.NewExtChannelOffset = 0xff;
2119 #endif // DOT11_N_SUPPORT //
2120
2121         // Reset WPA-PSK state. Only reset when supplicant enabled
2122         if (pAd->StaCfg.WpaState != SS_NOTUSE)
2123         {
2124                 pAd->StaCfg.WpaState = SS_START;
2125                 // Clear Replay counter
2126                 NdisZeroMemory(pAd->StaCfg.ReplayCounter, 8);
2127
2128 #ifdef QOS_DLS_SUPPORT
2129                 if (pAd->CommonCfg.bDLSCapable)
2130                         NdisZeroMemory(pAd->StaCfg.DlsReplayCounter, 8);
2131 #endif // QOS_DLS_SUPPORT //
2132         }
2133
2134
2135         //
2136         // if link down come from AP, we need to remove all WPA keys on WPA mode.
2137         // otherwise will cause 4-way handshaking failed, since the WPA key not empty.
2138         //
2139         if ((IsReqFromAP) && (pAd->StaCfg.AuthMode >= Ndis802_11AuthModeWPA))
2140         {
2141                 // Remove all WPA keys
2142                 RTMPWPARemoveAllKeys(pAd);
2143         }
2144
2145         // 802.1x port control
2146 #ifdef WPA_SUPPLICANT_SUPPORT
2147         // Prevent clear PortSecured here with static WEP
2148         // NetworkManger set security policy first then set SSID to connect AP.
2149         if (pAd->StaCfg.WpaSupplicantUP &&
2150                 (pAd->StaCfg.WepStatus == Ndis802_11WEPEnabled) &&
2151                 (pAd->StaCfg.IEEE8021X == FALSE))
2152         {
2153                 pAd->StaCfg.PortSecured = WPA_802_1X_PORT_SECURED;
2154         }
2155         else
2156 #endif // WPA_SUPPLICANT_SUPPORT //
2157         {
2158                 pAd->StaCfg.PortSecured = WPA_802_1X_PORT_NOT_SECURED;
2159                 pAd->StaCfg.PrivacyFilter = Ndis802_11PrivFilter8021xWEP;
2160         }
2161
2162         NdisAcquireSpinLock(&pAd->MacTabLock);
2163         pAd->MacTab.Content[BSSID_WCID].PortSecured = pAd->StaCfg.PortSecured;
2164         NdisReleaseSpinLock(&pAd->MacTabLock);
2165
2166         pAd->StaCfg.MicErrCnt = 0;
2167
2168         // Turn off Ckip control flag
2169         pAd->StaCfg.bCkipOn = FALSE;
2170         pAd->StaCfg.CCXEnable = FALSE;
2171
2172     pAd->IndicateMediaState = NdisMediaStateDisconnected;
2173         // Update extra information to link is up
2174         pAd->ExtraInfo = GENERAL_LINK_DOWN;
2175
2176     pAd->StaCfg.AdhocBOnlyJoined = FALSE;
2177         pAd->StaCfg.AdhocBGJoined = FALSE;
2178         pAd->StaCfg.Adhoc20NJoined = FALSE;
2179     pAd->StaActive.SupportedPhyInfo.bHtEnable = FALSE;
2180
2181         // Reset the Current AP's IP address
2182         NdisZeroMemory(pAd->StaCfg.AironetIPAddress, 4);
2183
2184         // Clean association information
2185         NdisZeroMemory(&pAd->StaCfg.AssocInfo, sizeof(NDIS_802_11_ASSOCIATION_INFORMATION));
2186         pAd->StaCfg.AssocInfo.Length = sizeof(NDIS_802_11_ASSOCIATION_INFORMATION);
2187         pAd->StaCfg.ReqVarIELen = 0;
2188         pAd->StaCfg.ResVarIELen = 0;
2189
2190         //
2191         // Reset RSSI value after link down
2192         //
2193         pAd->StaCfg.RssiSample.AvgRssi0 = 0;
2194         pAd->StaCfg.RssiSample.AvgRssi0X8 = 0;
2195         pAd->StaCfg.RssiSample.AvgRssi1 = 0;
2196         pAd->StaCfg.RssiSample.AvgRssi1X8 = 0;
2197         pAd->StaCfg.RssiSample.AvgRssi2 = 0;
2198         pAd->StaCfg.RssiSample.AvgRssi2X8 = 0;
2199
2200         // Restore MlmeRate
2201         pAd->CommonCfg.MlmeRate = pAd->CommonCfg.BasicMlmeRate;
2202         pAd->CommonCfg.RtsRate = pAd->CommonCfg.BasicMlmeRate;
2203
2204 #ifdef DOT11_N_SUPPORT
2205         //
2206         // After Link down, reset piggy-back setting in ASIC. Disable RDG.
2207         //
2208         if (pAd->CommonCfg.BBPCurrentBW == BW_40)
2209         {
2210                 pAd->CommonCfg.BBPCurrentBW = BW_20;
2211                 RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R4, &ByteValue);
2212                 ByteValue &= (~0x18);
2213                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R4, ByteValue);
2214         }
2215 #endif // DOT11_N_SUPPORT //
2216         // Reset DAC
2217         RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R1, &ByteValue);
2218         ByteValue &= (~0x18);
2219         if (pAd->Antenna.field.TxPath == 2)
2220         {
2221                 ByteValue |= 0x10;
2222         }
2223         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R1, ByteValue);
2224
2225         RTMPSetPiggyBack(pAd,FALSE);
2226         OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_PIGGYBACK_INUSED);
2227
2228 #ifdef DOT11_N_SUPPORT
2229         pAd->CommonCfg.BACapability.word = pAd->CommonCfg.REGBACapability.word;
2230 #endif // DOT11_N_SUPPORT //
2231
2232         // Restore all settings in the following.
2233         AsicUpdateProtect(pAd, 0, (ALLN_SETPROTECT|CCKSETPROTECT|OFDMSETPROTECT), TRUE, FALSE);
2234         AsicDisableRDG(pAd);
2235         pAd->CommonCfg.IOTestParm.bCurrentAtheros = FALSE;
2236         pAd->CommonCfg.IOTestParm.bNowAtherosBurstOn = FALSE;
2237
2238 #ifdef DOT11_N_SUPPORT
2239 #ifdef DOT11N_DRAFT3
2240         OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_SCAN_2040);
2241         pAd->CommonCfg.BSSCoexist2040.word = 0;
2242         TriEventInit(pAd);
2243         for (i = 0; i < (pAd->ChannelListNum - 1); i++)
2244         {
2245                 pAd->ChannelList[i].bEffectedChannel = FALSE;
2246         }
2247 #endif // DOT11N_DRAFT3 //
2248 #endif // DOT11_N_SUPPORT //
2249
2250         RTMP_IO_WRITE32(pAd, MAX_LEN_CFG, 0x1fff);
2251         RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS);
2252
2253         // Allow go to sleep after linkdown steps.
2254         RTMP_SET_PSFLAG(pAd, fRTMP_PS_CAN_GO_SLEEP);
2255
2256 #ifdef WPA_SUPPLICANT_SUPPORT
2257 #ifndef NATIVE_WPA_SUPPLICANT_SUPPORT
2258         if (pAd->StaCfg.WpaSupplicantUP) {
2259                 union iwreq_data    wrqu;
2260                 //send disassociate event to wpa_supplicant
2261                 memset(&wrqu, 0, sizeof(wrqu));
2262                 wrqu.data.flags = RT_DISASSOC_EVENT_FLAG;
2263                 wireless_send_event(pAd->net_dev, IWEVCUSTOM, &wrqu, NULL);
2264         }
2265 #endif // NATIVE_WPA_SUPPLICANT_SUPPORT //
2266 #endif // WPA_SUPPLICANT_SUPPORT //
2267
2268 #ifdef NATIVE_WPA_SUPPLICANT_SUPPORT
2269         {
2270                 union iwreq_data    wrqu;
2271                 memset(wrqu.ap_addr.sa_data, 0, MAC_ADDR_LEN);
2272                 wireless_send_event(pAd->net_dev, SIOCGIWAP, &wrqu, NULL);
2273         }
2274 #endif // NATIVE_WPA_SUPPLICANT_SUPPORT //
2275 }
2276
2277 /*
2278         ==========================================================================
2279         Description:
2280
2281         IRQL = DISPATCH_LEVEL
2282
2283         ==========================================================================
2284 */
2285 VOID IterateOnBssTab(
2286         IN PRTMP_ADAPTER pAd)
2287 {
2288         MLME_START_REQ_STRUCT   StartReq;
2289         MLME_JOIN_REQ_STRUCT    JoinReq;
2290         ULONG                   BssIdx;
2291
2292         // Change the wepstatus to original wepstatus
2293         pAd->StaCfg.WepStatus   = pAd->StaCfg.OrigWepStatus;
2294         pAd->StaCfg.PairCipher  = pAd->StaCfg.OrigWepStatus;
2295         pAd->StaCfg.GroupCipher = pAd->StaCfg.OrigWepStatus;
2296
2297         BssIdx = pAd->MlmeAux.BssIdx;
2298         if (BssIdx < pAd->MlmeAux.SsidBssTab.BssNr)
2299         {
2300                 // Check cipher suite, AP must have more secured cipher than station setting
2301                 // Set the Pairwise and Group cipher to match the intended AP setting
2302                 // We can only connect to AP with less secured cipher setting
2303                 if ((pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA) || (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPAPSK))
2304                 {
2305                         pAd->StaCfg.GroupCipher = pAd->MlmeAux.SsidBssTab.BssEntry[BssIdx].WPA.GroupCipher;
2306
2307                         if (pAd->StaCfg.WepStatus == pAd->MlmeAux.SsidBssTab.BssEntry[BssIdx].WPA.PairCipher)
2308                                 pAd->StaCfg.PairCipher = pAd->MlmeAux.SsidBssTab.BssEntry[BssIdx].WPA.PairCipher;
2309                         else if (pAd->MlmeAux.SsidBssTab.BssEntry[BssIdx].WPA.PairCipherAux != Ndis802_11WEPDisabled)
2310                                 pAd->StaCfg.PairCipher = pAd->MlmeAux.SsidBssTab.BssEntry[BssIdx].WPA.PairCipherAux;
2311                         else    // There is no PairCipher Aux, downgrade our capability to TKIP
2312                                 pAd->StaCfg.PairCipher = Ndis802_11Encryption2Enabled;
2313                 }
2314                 else if ((pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA2) || (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA2PSK))
2315                 {
2316                         pAd->StaCfg.GroupCipher = pAd->MlmeAux.SsidBssTab.BssEntry[BssIdx].WPA2.GroupCipher;
2317
2318                         if (pAd->StaCfg.WepStatus == pAd->MlmeAux.SsidBssTab.BssEntry[BssIdx].WPA2.PairCipher)
2319                                 pAd->StaCfg.PairCipher = pAd->MlmeAux.SsidBssTab.BssEntry[BssIdx].WPA2.PairCipher;
2320                         else if (pAd->MlmeAux.SsidBssTab.BssEntry[BssIdx].WPA2.PairCipherAux != Ndis802_11WEPDisabled)
2321                                 pAd->StaCfg.PairCipher = pAd->MlmeAux.SsidBssTab.BssEntry[BssIdx].WPA2.PairCipherAux;
2322                         else    // There is no PairCipher Aux, downgrade our capability to TKIP
2323                                 pAd->StaCfg.PairCipher = Ndis802_11Encryption2Enabled;
2324
2325                         // RSN capability
2326                         pAd->StaCfg.RsnCapability = pAd->MlmeAux.SsidBssTab.BssEntry[BssIdx].WPA2.RsnCapability;
2327                 }
2328
2329                 // Set Mix cipher flag
2330                 pAd->StaCfg.bMixCipher = (pAd->StaCfg.PairCipher == pAd->StaCfg.GroupCipher) ? FALSE : TRUE;
2331                 if (pAd->StaCfg.bMixCipher == TRUE)
2332                 {
2333                         // If mix cipher, re-build RSNIE
2334                         RTMPMakeRSNIE(pAd, pAd->StaCfg.AuthMode, pAd->StaCfg.WepStatus, 0);
2335                 }
2336
2337                 DBGPRINT(RT_DEBUG_TRACE, ("CNTL - iterate BSS %ld of %d\n", BssIdx, pAd->MlmeAux.SsidBssTab.BssNr));
2338                 JoinParmFill(pAd, &JoinReq, BssIdx);
2339                 MlmeEnqueue(pAd, SYNC_STATE_MACHINE, MT2_MLME_JOIN_REQ, sizeof(MLME_JOIN_REQ_STRUCT),
2340                                         &JoinReq);
2341                 pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_JOIN;
2342         }
2343         else if (pAd->StaCfg.BssType == BSS_ADHOC)
2344         {
2345                 DBGPRINT(RT_DEBUG_TRACE, ("CNTL - All BSS fail; start a new ADHOC (Ssid=%s)...\n",pAd->MlmeAux.Ssid));
2346                 StartParmFill(pAd, &StartReq, pAd->MlmeAux.Ssid, pAd->MlmeAux.SsidLen);
2347                 MlmeEnqueue(pAd, SYNC_STATE_MACHINE, MT2_MLME_START_REQ, sizeof(MLME_START_REQ_STRUCT), &StartReq);
2348                 pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_START;
2349         }
2350         else // no more BSS
2351         {
2352                 DBGPRINT(RT_DEBUG_TRACE, ("CNTL - All roaming failed, stay @ ch #%d\n", pAd->CommonCfg.Channel));
2353                 AsicSwitchChannel(pAd, pAd->CommonCfg.Channel, FALSE);
2354                 AsicLockChannel(pAd, pAd->CommonCfg.Channel);
2355                 pAd->Mlme.CntlMachine.CurrState = CNTL_IDLE;
2356         }
2357 }
2358
2359 // for re-association only
2360 // IRQL = DISPATCH_LEVEL
2361 VOID IterateOnBssTab2(
2362         IN PRTMP_ADAPTER pAd)
2363 {
2364         MLME_REASSOC_REQ_STRUCT ReassocReq;
2365         ULONG                   BssIdx;
2366         BSS_ENTRY               *pBss;
2367
2368         BssIdx = pAd->MlmeAux.RoamIdx;
2369         pBss = &pAd->MlmeAux.RoamTab.BssEntry[BssIdx];
2370
2371         if (BssIdx < pAd->MlmeAux.RoamTab.BssNr)
2372         {
2373                 DBGPRINT(RT_DEBUG_TRACE, ("CNTL - iterate BSS %ld of %d\n", BssIdx, pAd->MlmeAux.RoamTab.BssNr));
2374
2375                 AsicSwitchChannel(pAd, pBss->Channel, FALSE);
2376                 AsicLockChannel(pAd, pBss->Channel);
2377
2378                 // reassociate message has the same structure as associate message
2379                 AssocParmFill(pAd, &ReassocReq, pBss->Bssid, pBss->CapabilityInfo,
2380                                           ASSOC_TIMEOUT, pAd->StaCfg.DefaultListenCount);
2381                 MlmeEnqueue(pAd, ASSOC_STATE_MACHINE, MT2_MLME_REASSOC_REQ,
2382                                         sizeof(MLME_REASSOC_REQ_STRUCT), &ReassocReq);
2383
2384                 pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_REASSOC;
2385         }
2386         else // no more BSS
2387         {
2388                 DBGPRINT(RT_DEBUG_TRACE, ("CNTL - All fast roaming failed, back to ch #%d\n",pAd->CommonCfg.Channel));
2389                 AsicSwitchChannel(pAd, pAd->CommonCfg.Channel, FALSE);
2390                 AsicLockChannel(pAd, pAd->CommonCfg.Channel);
2391                 pAd->Mlme.CntlMachine.CurrState = CNTL_IDLE;
2392         }
2393 }
2394
2395 /*
2396         ==========================================================================
2397         Description:
2398
2399         IRQL = DISPATCH_LEVEL
2400
2401         ==========================================================================
2402 */
2403 VOID JoinParmFill(
2404         IN PRTMP_ADAPTER pAd,
2405         IN OUT MLME_JOIN_REQ_STRUCT *JoinReq,
2406         IN ULONG BssIdx)
2407 {
2408         JoinReq->BssIdx = BssIdx;
2409 }
2410
2411 /*
2412         ==========================================================================
2413         Description:
2414
2415         IRQL = DISPATCH_LEVEL
2416
2417         ==========================================================================
2418 */
2419 VOID ScanParmFill(
2420         IN PRTMP_ADAPTER pAd,
2421         IN OUT MLME_SCAN_REQ_STRUCT *ScanReq,
2422         IN CHAR Ssid[],
2423         IN UCHAR SsidLen,
2424         IN UCHAR BssType,
2425         IN UCHAR ScanType)
2426 {
2427     NdisZeroMemory(ScanReq->Ssid, MAX_LEN_OF_SSID);
2428         ScanReq->SsidLen = SsidLen;
2429         NdisMoveMemory(ScanReq->Ssid, Ssid, SsidLen);
2430         ScanReq->BssType = BssType;
2431         ScanReq->ScanType = ScanType;
2432 }
2433
2434 #ifdef QOS_DLS_SUPPORT
2435 /*
2436         ==========================================================================
2437         Description:
2438
2439         IRQL = DISPATCH_LEVEL
2440
2441         ==========================================================================
2442 */
2443 VOID DlsParmFill(
2444         IN PRTMP_ADAPTER pAd,
2445         IN OUT MLME_DLS_REQ_STRUCT *pDlsReq,
2446         IN PRT_802_11_DLS pDls,
2447         IN USHORT reason)
2448 {
2449         pDlsReq->pDLS = pDls;
2450         pDlsReq->Reason = reason;
2451 }
2452 #endif // QOS_DLS_SUPPORT //
2453
2454 /*
2455         ==========================================================================
2456         Description:
2457
2458         IRQL = DISPATCH_LEVEL
2459
2460         ==========================================================================
2461 */
2462 VOID StartParmFill(
2463         IN PRTMP_ADAPTER pAd,
2464         IN OUT MLME_START_REQ_STRUCT *StartReq,
2465         IN CHAR Ssid[],
2466         IN UCHAR SsidLen)
2467 {
2468         ASSERT(SsidLen <= MAX_LEN_OF_SSID);
2469         NdisMoveMemory(StartReq->Ssid, Ssid, SsidLen);
2470         StartReq->SsidLen = SsidLen;
2471 }
2472
2473 /*
2474         ==========================================================================
2475         Description:
2476
2477         IRQL = DISPATCH_LEVEL
2478
2479         ==========================================================================
2480 */
2481 VOID AuthParmFill(
2482         IN PRTMP_ADAPTER pAd,
2483         IN OUT MLME_AUTH_REQ_STRUCT *AuthReq,
2484         IN PUCHAR pAddr,
2485         IN USHORT Alg)
2486 {
2487         COPY_MAC_ADDR(AuthReq->Addr, pAddr);
2488         AuthReq->Alg = Alg;
2489         AuthReq->Timeout = AUTH_TIMEOUT;
2490 }
2491
2492 /*
2493         ==========================================================================
2494         Description:
2495
2496         IRQL = DISPATCH_LEVEL
2497
2498         ==========================================================================
2499  */
2500 VOID ComposePsPoll(
2501         IN PRTMP_ADAPTER pAd)
2502 {
2503         NdisZeroMemory(&pAd->PsPollFrame, sizeof(PSPOLL_FRAME));
2504         pAd->PsPollFrame.FC.Type = BTYPE_CNTL;
2505         pAd->PsPollFrame.FC.SubType = SUBTYPE_PS_POLL;
2506         pAd->PsPollFrame.Aid = pAd->StaActive.Aid | 0xC000;
2507         COPY_MAC_ADDR(pAd->PsPollFrame.Bssid, pAd->CommonCfg.Bssid);
2508         COPY_MAC_ADDR(pAd->PsPollFrame.Ta, pAd->CurrentAddress);
2509 }
2510
2511 // IRQL = DISPATCH_LEVEL
2512 VOID ComposeNullFrame(
2513         IN PRTMP_ADAPTER pAd)
2514 {
2515         NdisZeroMemory(&pAd->NullFrame, sizeof(HEADER_802_11));
2516         pAd->NullFrame.FC.Type = BTYPE_DATA;
2517         pAd->NullFrame.FC.SubType = SUBTYPE_NULL_FUNC;
2518         pAd->NullFrame.FC.ToDs = 1;
2519         COPY_MAC_ADDR(pAd->NullFrame.Addr1, pAd->CommonCfg.Bssid);
2520         COPY_MAC_ADDR(pAd->NullFrame.Addr2, pAd->CurrentAddress);
2521         COPY_MAC_ADDR(pAd->NullFrame.Addr3, pAd->CommonCfg.Bssid);
2522 }
2523
2524
2525
2526
2527 /*
2528         ==========================================================================
2529         Description:
2530                 Pre-build a BEACON frame in the shared memory
2531
2532         IRQL = PASSIVE_LEVEL
2533         IRQL = DISPATCH_LEVEL
2534
2535         ==========================================================================
2536 */
2537 ULONG MakeIbssBeacon(
2538         IN PRTMP_ADAPTER pAd)
2539 {
2540         UCHAR         DsLen = 1, IbssLen = 2;
2541         UCHAR         LocalErpIe[3] = {IE_ERP, 1, 0x04};
2542         HEADER_802_11 BcnHdr;
2543         USHORT        CapabilityInfo;
2544         LARGE_INTEGER FakeTimestamp;
2545         ULONG         FrameLen = 0;
2546         PTXWI_STRUC       pTxWI = &pAd->BeaconTxWI;
2547         CHAR         *pBeaconFrame = pAd->BeaconBuf;
2548         BOOLEAN       Privacy;
2549         UCHAR         SupRate[MAX_LEN_OF_SUPPORTED_RATES];
2550         UCHAR         SupRateLen = 0;
2551         UCHAR         ExtRate[MAX_LEN_OF_SUPPORTED_RATES];
2552         UCHAR         ExtRateLen = 0;
2553         UCHAR         RSNIe = IE_WPA;
2554
2555         if ((pAd->CommonCfg.PhyMode == PHY_11B) && (pAd->CommonCfg.Channel <= 14))
2556         {
2557                 SupRate[0] = 0x82; // 1 mbps
2558                 SupRate[1] = 0x84; // 2 mbps
2559                 SupRate[2] = 0x8b; // 5.5 mbps
2560                 SupRate[3] = 0x96; // 11 mbps
2561                 SupRateLen = 4;
2562                 ExtRateLen = 0;
2563         }
2564         else if (pAd->CommonCfg.Channel > 14)
2565         {
2566                 SupRate[0]  = 0x8C;    // 6 mbps, in units of 0.5 Mbps, basic rate
2567                 SupRate[1]  = 0x12;    // 9 mbps, in units of 0.5 Mbps
2568                 SupRate[2]  = 0x98;    // 12 mbps, in units of 0.5 Mbps, basic rate
2569                 SupRate[3]  = 0x24;    // 18 mbps, in units of 0.5 Mbps
2570                 SupRate[4]  = 0xb0;    // 24 mbps, in units of 0.5 Mbps, basic rate
2571                 SupRate[5]  = 0x48;    // 36 mbps, in units of 0.5 Mbps
2572                 SupRate[6]  = 0x60;    // 48 mbps, in units of 0.5 Mbps
2573                 SupRate[7]  = 0x6c;    // 54 mbps, in units of 0.5 Mbps
2574                 SupRateLen  = 8;
2575                 ExtRateLen  = 0;
2576
2577                 //
2578                 // Also Update MlmeRate & RtsRate for G only & A only
2579                 //
2580                 pAd->CommonCfg.MlmeRate = RATE_6;
2581                 pAd->CommonCfg.RtsRate = RATE_6;
2582                 pAd->CommonCfg.MlmeTransmit.field.MODE = MODE_OFDM;
2583                 pAd->CommonCfg.MlmeTransmit.field.MCS = OfdmRateToRxwiMCS[pAd->CommonCfg.MlmeRate];
2584                 pAd->MacTab.Content[BSS0Mcast_WCID].HTPhyMode.field.MODE = MODE_OFDM;
2585                 pAd->MacTab.Content[BSS0Mcast_WCID].HTPhyMode.field.MCS = OfdmRateToRxwiMCS[pAd->CommonCfg.MlmeRate];
2586         }
2587         else
2588         {
2589                 SupRate[0] = 0x82; // 1 mbps
2590                 SupRate[1] = 0x84; // 2 mbps
2591                 SupRate[2] = 0x8b; // 5.5 mbps
2592                 SupRate[3] = 0x96; // 11 mbps
2593                 SupRateLen = 4;
2594
2595                 ExtRate[0]  = 0x0C;    // 6 mbps, in units of 0.5 Mbps,
2596                 ExtRate[1]  = 0x12;    // 9 mbps, in units of 0.5 Mbps
2597                 ExtRate[2]  = 0x18;    // 12 mbps, in units of 0.5 Mbps,
2598                 ExtRate[3]  = 0x24;    // 18 mbps, in units of 0.5 Mbps
2599                 ExtRate[4]  = 0x30;    // 24 mbps, in units of 0.5 Mbps,
2600                 ExtRate[5]  = 0x48;    // 36 mbps, in units of 0.5 Mbps
2601                 ExtRate[6]  = 0x60;    // 48 mbps, in units of 0.5 Mbps
2602                 ExtRate[7]  = 0x6c;    // 54 mbps, in units of 0.5 Mbps
2603                 ExtRateLen  = 8;
2604         }
2605
2606         pAd->StaActive.SupRateLen = SupRateLen;
2607         NdisMoveMemory(pAd->StaActive.SupRate, SupRate, SupRateLen);
2608         pAd->StaActive.ExtRateLen = ExtRateLen;
2609         NdisMoveMemory(pAd->StaActive.ExtRate, ExtRate, ExtRateLen);
2610
2611         // compose IBSS beacon frame
2612         MgtMacHeaderInit(pAd, &BcnHdr, SUBTYPE_BEACON, 0, BROADCAST_ADDR, pAd->CommonCfg.Bssid);
2613         Privacy = (pAd->StaCfg.WepStatus == Ndis802_11Encryption1Enabled) ||
2614                           (pAd->StaCfg.WepStatus == Ndis802_11Encryption2Enabled) ||
2615                           (pAd->StaCfg.WepStatus == Ndis802_11Encryption3Enabled);
2616         CapabilityInfo = CAP_GENERATE(0, 1, Privacy, (pAd->CommonCfg.TxPreamble == Rt802_11PreambleShort), 0, 0);
2617
2618         MakeOutgoingFrame(pBeaconFrame,                &FrameLen,
2619                                           sizeof(HEADER_802_11),           &BcnHdr,
2620                                           TIMESTAMP_LEN,                   &FakeTimestamp,
2621                                           2,                               &pAd->CommonCfg.BeaconPeriod,
2622                                           2,                               &CapabilityInfo,
2623                                           1,                               &SsidIe,
2624                                           1,                               &pAd->CommonCfg.SsidLen,
2625                                           pAd->CommonCfg.SsidLen,          pAd->CommonCfg.Ssid,
2626                                           1,                               &SupRateIe,
2627                                           1,                               &SupRateLen,
2628                                           SupRateLen,                      SupRate,
2629                                           1,                               &DsIe,
2630                                           1,                               &DsLen,
2631                                           1,                               &pAd->CommonCfg.Channel,
2632                                           1,                               &IbssIe,
2633                                           1,                               &IbssLen,
2634                                           2,                               &pAd->StaActive.AtimWin,
2635                                           END_OF_ARGS);
2636
2637         // add ERP_IE and EXT_RAE IE of in 802.11g
2638         if (ExtRateLen)
2639         {
2640                 ULONG   tmp;
2641
2642                 MakeOutgoingFrame(pBeaconFrame + FrameLen,         &tmp,
2643                                                   3,                               LocalErpIe,
2644                                                   1,                               &ExtRateIe,
2645                                                   1,                               &ExtRateLen,
2646                                                   ExtRateLen,                      ExtRate,
2647                                                   END_OF_ARGS);
2648                 FrameLen += tmp;
2649         }
2650
2651         // If adhoc secruity is set for WPA-None, append the cipher suite IE
2652         if (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPANone)
2653         {
2654                 ULONG tmp;
2655         RTMPMakeRSNIE(pAd, pAd->StaCfg.AuthMode, pAd->StaCfg.WepStatus, BSS0);
2656
2657                 MakeOutgoingFrame(pBeaconFrame + FrameLen,              &tmp,
2658                                                   1,                                    &RSNIe,
2659                                                   1,                                    &pAd->StaCfg.RSNIE_Len,
2660                                                   pAd->StaCfg.RSNIE_Len,                pAd->StaCfg.RSN_IE,
2661                                                   END_OF_ARGS);
2662                 FrameLen += tmp;
2663         }
2664
2665 #ifdef DOT11_N_SUPPORT
2666         if ((pAd->CommonCfg.PhyMode >= PHY_11ABGN_MIXED))
2667         {
2668                 ULONG TmpLen;
2669                 UCHAR HtLen, HtLen1;
2670
2671 #ifdef RT_BIG_ENDIAN
2672                 HT_CAPABILITY_IE HtCapabilityTmp;
2673                 ADD_HT_INFO_IE  addHTInfoTmp;
2674                 USHORT  b2lTmp, b2lTmp2;
2675 #endif
2676
2677                 // add HT Capability IE
2678                 HtLen = sizeof(pAd->CommonCfg.HtCapability);
2679                 HtLen1 = sizeof(pAd->CommonCfg.AddHTInfo);
2680 #ifndef RT_BIG_ENDIAN
2681                 MakeOutgoingFrame(pBeaconFrame+FrameLen,        &TmpLen,
2682                                                   1,                                            &HtCapIe,
2683                                                   1,                                            &HtLen,
2684                                                   HtLen,                                        &pAd->CommonCfg.HtCapability,
2685                                                   1,                                            &AddHtInfoIe,
2686                                                   1,                                            &HtLen1,
2687                                                   HtLen1,                                       &pAd->CommonCfg.AddHTInfo,
2688                                                   END_OF_ARGS);
2689 #else
2690                 NdisMoveMemory(&HtCapabilityTmp, &pAd->CommonCfg.HtCapability, HtLen);
2691                 *(USHORT *)(&HtCapabilityTmp.HtCapInfo) = SWAP16(*(USHORT *)(&HtCapabilityTmp.HtCapInfo));
2692                 *(USHORT *)(&HtCapabilityTmp.ExtHtCapInfo) = SWAP16(*(USHORT *)(&HtCapabilityTmp.ExtHtCapInfo));
2693
2694                 NdisMoveMemory(&addHTInfoTmp, &pAd->CommonCfg.AddHTInfo, HtLen1);
2695                 *(USHORT *)(&addHTInfoTmp.AddHtInfo2) = SWAP16(*(USHORT *)(&addHTInfoTmp.AddHtInfo2));
2696                 *(USHORT *)(&addHTInfoTmp.AddHtInfo3) = SWAP16(*(USHORT *)(&addHTInfoTmp.AddHtInfo3));
2697
2698                 MakeOutgoingFrame(pBeaconFrame+FrameLen,        &TmpLen,
2699                                                   1,                                            &HtCapIe,
2700                                                   1,                                            &HtLen,
2701                                                   HtLen,                                        &HtCapabilityTmp,
2702                                                   1,                                            &AddHtInfoIe,
2703                                                   1,                                            &HtLen1,
2704                                                   HtLen1,                                       &addHTInfoTmp,
2705                                                   END_OF_ARGS);
2706 #endif
2707                 FrameLen += TmpLen;
2708         }
2709 #endif // DOT11_N_SUPPORT //
2710
2711         //beacon use reserved WCID 0xff
2712     if (pAd->CommonCfg.Channel > 14)
2713     {
2714         RTMPWriteTxWI(pAd, pTxWI, FALSE, FALSE,  TRUE, FALSE, FALSE, TRUE, 0, 0xff, FrameLen,
2715                 PID_MGMT, PID_BEACON, RATE_1, IFS_HTTXOP, FALSE, &pAd->CommonCfg.MlmeTransmit);
2716     }
2717     else
2718     {
2719         // Set to use 1Mbps for Adhoc beacon.
2720                 HTTRANSMIT_SETTING Transmit;
2721         Transmit.word = 0;
2722         RTMPWriteTxWI(pAd, pTxWI, FALSE, FALSE,  TRUE, FALSE, FALSE, TRUE, 0, 0xff, FrameLen,
2723                 PID_MGMT, PID_BEACON, RATE_1, IFS_HTTXOP, FALSE, &Transmit);
2724     }
2725
2726 #ifdef RT_BIG_ENDIAN
2727         RTMPFrameEndianChange(pAd, pBeaconFrame, DIR_WRITE, FALSE);
2728         RTMPWIEndianChange((PUCHAR)pTxWI, TYPE_TXWI);
2729 #endif
2730
2731     DBGPRINT(RT_DEBUG_TRACE, ("MakeIbssBeacon (len=%ld), SupRateLen=%d, ExtRateLen=%d, Channel=%d, PhyMode=%d\n",
2732                                         FrameLen, SupRateLen, ExtRateLen, pAd->CommonCfg.Channel, pAd->CommonCfg.PhyMode));
2733         return FrameLen;
2734 }
2735
2736