Staging: rt2860: remove dead DFS_SUPPORT code
[pandora-kernel.git] / 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                         }
958
959                         DBGPRINT(RT_DEBUG_TRACE, ("CNTL - start a new IBSS = %02x:%02x:%02x:%02x:%02x:%02x ...\n",
960                                 pAd->CommonCfg.Bssid[0],pAd->CommonCfg.Bssid[1],pAd->CommonCfg.Bssid[2],
961                                 pAd->CommonCfg.Bssid[3],pAd->CommonCfg.Bssid[4],pAd->CommonCfg.Bssid[5]));
962                 }
963                 else
964                 {
965                         DBGPRINT(RT_DEBUG_TRACE, ("CNTL - Start IBSS fail. BUG!!!!!\n"));
966                         pAd->Mlme.CntlMachine.CurrState = CNTL_IDLE;
967                 }
968         }
969 }
970
971 /*
972         ==========================================================================
973         Description:
974
975         IRQL = DISPATCH_LEVEL
976
977         ==========================================================================
978 */
979 VOID CntlWaitAuthProc(
980         IN PRTMP_ADAPTER pAd,
981         IN MLME_QUEUE_ELEM *Elem)
982 {
983         USHORT                       Reason;
984         MLME_ASSOC_REQ_STRUCT        AssocReq;
985         MLME_AUTH_REQ_STRUCT         AuthReq;
986
987         if (Elem->MsgType == MT2_AUTH_CONF)
988         {
989                 NdisMoveMemory(&Reason, Elem->Msg, sizeof(USHORT));
990                 if (Reason == MLME_SUCCESS)
991                 {
992                         DBGPRINT(RT_DEBUG_TRACE, ("CNTL - AUTH OK\n"));
993                         AssocParmFill(pAd, &AssocReq, pAd->MlmeAux.Bssid, pAd->MlmeAux.CapabilityInfo,
994                                                   ASSOC_TIMEOUT, pAd->StaCfg.DefaultListenCount);
995
996 #ifdef LEAP_SUPPORT
997                         //
998                         // Cisco Leap CCKM supported Re-association.
999                         //
1000                         if (LEAP_CCKM_ON(pAd) && (pAd->StaCfg.CCKMLinkUpFlag == TRUE))
1001                         {
1002                                 //if CCKM is turn on , that's mean Fast Reauthentication
1003                                 //Use CCKM Reassociation instead of normal association for Fast Roaming.
1004                                 MlmeEnqueue(pAd, ASSOC_STATE_MACHINE, MT2_MLME_REASSOC_REQ,
1005                                                         sizeof(MLME_ASSOC_REQ_STRUCT), &AssocReq);
1006
1007                                 pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_REASSOC;
1008                         }
1009                         else
1010 #endif // LEAP_SUPPORT //
1011                         {
1012                                 MlmeEnqueue(pAd, ASSOC_STATE_MACHINE, MT2_MLME_ASSOC_REQ,
1013                                                         sizeof(MLME_ASSOC_REQ_STRUCT), &AssocReq);
1014
1015                                 pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_ASSOC;
1016                         }
1017                 }
1018                 else
1019                 {
1020                         // This fail may because of the AP already keep us in its MAC table without
1021                         // ageing-out. The previous authentication attempt must have let it remove us.
1022                         // so try Authentication again may help. For D-Link DWL-900AP+ compatibility.
1023                         DBGPRINT(RT_DEBUG_TRACE, ("CNTL - AUTH FAIL, try again...\n"));
1024 #ifdef LEAP_SUPPORT
1025                         //Add AuthMode "LEAP" for CCX 1.X
1026                         if (pAd->StaCfg.LeapAuthMode == CISCO_AuthModeLEAP)
1027                         {
1028                                 AuthParmFill(pAd, &AuthReq, pAd->MlmeAux.Bssid, CISCO_AuthModeLEAP);
1029                         }
1030                         else
1031 #endif // LEAP_SUPPORT //
1032                         {
1033                                 if ((pAd->StaCfg.AuthMode == Ndis802_11AuthModeShared) ||
1034                                         (pAd->StaCfg.AuthMode == Ndis802_11AuthModeAutoSwitch))
1035                                 {
1036                                         // either Ndis802_11AuthModeShared or Ndis802_11AuthModeAutoSwitch, try shared key first
1037                                         AuthParmFill(pAd, &AuthReq, pAd->MlmeAux.Bssid, Ndis802_11AuthModeShared);
1038                                 }
1039                                 else
1040                                 {
1041                                         AuthParmFill(pAd, &AuthReq, pAd->MlmeAux.Bssid, Ndis802_11AuthModeOpen);
1042                                 }
1043                         }
1044                         MlmeEnqueue(pAd, AUTH_STATE_MACHINE, MT2_MLME_AUTH_REQ,
1045                                                 sizeof(MLME_AUTH_REQ_STRUCT), &AuthReq);
1046
1047                         pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_AUTH2;
1048                 }
1049         }
1050 }
1051
1052 /*
1053         ==========================================================================
1054         Description:
1055
1056         IRQL = DISPATCH_LEVEL
1057
1058         ==========================================================================
1059 */
1060 VOID CntlWaitAuthProc2(
1061         IN PRTMP_ADAPTER pAd,
1062         IN MLME_QUEUE_ELEM *Elem)
1063 {
1064         USHORT                       Reason;
1065         MLME_ASSOC_REQ_STRUCT        AssocReq;
1066         MLME_AUTH_REQ_STRUCT         AuthReq;
1067
1068         if (Elem->MsgType == MT2_AUTH_CONF)
1069         {
1070                 NdisMoveMemory(&Reason, Elem->Msg, sizeof(USHORT));
1071                 if (Reason == MLME_SUCCESS)
1072                 {
1073                         DBGPRINT(RT_DEBUG_TRACE, ("CNTL - AUTH OK\n"));
1074                         AssocParmFill(pAd, &AssocReq, pAd->MlmeAux.Bssid, pAd->MlmeAux.CapabilityInfo,
1075                                                   ASSOC_TIMEOUT, pAd->StaCfg.DefaultListenCount);
1076                         MlmeEnqueue(pAd, ASSOC_STATE_MACHINE, MT2_MLME_ASSOC_REQ,
1077                                                 sizeof(MLME_ASSOC_REQ_STRUCT), &AssocReq);
1078
1079                         pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_ASSOC;
1080                 }
1081                 else
1082                 {
1083 #ifdef LEAP_SUPPORT
1084                         // Process LEAP first, since it use different control variable
1085                         // We don't want to affect other poven operation
1086                         if (pAd->StaCfg.LeapAuthMode == CISCO_AuthModeLEAP)
1087                         {
1088                                 // LEAP Auth not success, try next BSS
1089                                 DBGPRINT(RT_DEBUG_TRACE, ("CNTL - *LEAP* AUTH FAIL, give up; try next BSS\n"));
1090                                 DBGPRINT(RT_DEBUG_TRACE, ("Total match BSSID [=%d]\n", pAd->MlmeAux.SsidBssTab.BssNr));
1091                                 pAd->Mlme.CntlMachine.CurrState = CNTL_IDLE;
1092                                 pAd->MlmeAux.BssIdx++;
1093                                 IterateOnBssTab(pAd);
1094                         }
1095                         else
1096 #endif // LEAP_SUPPORT //
1097                         if ((pAd->StaCfg.AuthMode == Ndis802_11AuthModeAutoSwitch) &&
1098                                  (pAd->MlmeAux.Alg == Ndis802_11AuthModeShared))
1099                         {
1100                                 DBGPRINT(RT_DEBUG_TRACE, ("CNTL - AUTH FAIL, try OPEN system...\n"));
1101                                 AuthParmFill(pAd, &AuthReq, pAd->MlmeAux.Bssid, Ndis802_11AuthModeOpen);
1102                                 MlmeEnqueue(pAd, AUTH_STATE_MACHINE, MT2_MLME_AUTH_REQ,
1103                                                         sizeof(MLME_AUTH_REQ_STRUCT), &AuthReq);
1104
1105                                 pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_AUTH2;
1106                         }
1107                         else
1108                         {
1109                                 // not success, try next BSS
1110                                 DBGPRINT(RT_DEBUG_TRACE, ("CNTL - AUTH FAIL, give up; try next BSS\n"));
1111                                 pAd->Mlme.CntlMachine.CurrState = CNTL_IDLE; //???????
1112                                 pAd->MlmeAux.BssIdx++;
1113                                 IterateOnBssTab(pAd);
1114                         }
1115                 }
1116         }
1117 }
1118
1119 /*
1120         ==========================================================================
1121         Description:
1122
1123         IRQL = DISPATCH_LEVEL
1124
1125         ==========================================================================
1126 */
1127 VOID CntlWaitAssocProc(
1128         IN PRTMP_ADAPTER pAd,
1129         IN MLME_QUEUE_ELEM *Elem)
1130 {
1131         USHORT      Reason;
1132
1133         if (Elem->MsgType == MT2_ASSOC_CONF)
1134         {
1135                 NdisMoveMemory(&Reason, Elem->Msg, sizeof(USHORT));
1136                 if (Reason == MLME_SUCCESS)
1137                 {
1138                         LinkUp(pAd, BSS_INFRA);
1139                         pAd->Mlme.CntlMachine.CurrState = CNTL_IDLE;
1140                         DBGPRINT(RT_DEBUG_TRACE, ("CNTL - Association successful on BSS #%ld\n",pAd->MlmeAux.BssIdx));
1141
1142                         if (pAd->CommonCfg.bWirelessEvent)
1143                         {
1144                                 RTMPSendWirelessEvent(pAd, IW_ASSOC_EVENT_FLAG, pAd->MacTab.Content[BSSID_WCID].Addr, BSS0, 0);
1145                         }
1146                 }
1147                 else
1148                 {
1149                         // not success, try next BSS
1150                         DBGPRINT(RT_DEBUG_TRACE, ("CNTL - Association fails on BSS #%ld\n",pAd->MlmeAux.BssIdx));
1151                         pAd->MlmeAux.BssIdx++;
1152                         IterateOnBssTab(pAd);
1153                 }
1154         }
1155 }
1156
1157 /*
1158         ==========================================================================
1159         Description:
1160
1161         IRQL = DISPATCH_LEVEL
1162
1163         ==========================================================================
1164 */
1165 VOID CntlWaitReassocProc(
1166         IN PRTMP_ADAPTER pAd,
1167         IN MLME_QUEUE_ELEM *Elem)
1168 {
1169         USHORT      Result;
1170
1171         if (Elem->MsgType == MT2_REASSOC_CONF)
1172         {
1173                 NdisMoveMemory(&Result, Elem->Msg, sizeof(USHORT));
1174                 if (Result == MLME_SUCCESS)
1175                 {
1176                         //
1177                         // NDIS requires a new Link UP indication but no Link Down for RE-ASSOC
1178                         //
1179                         LinkUp(pAd, BSS_INFRA);
1180
1181                         // send wireless event - for association
1182                         if (pAd->CommonCfg.bWirelessEvent)
1183                                 RTMPSendWirelessEvent(pAd, IW_ASSOC_EVENT_FLAG, pAd->MacTab.Content[BSSID_WCID].Addr, BSS0, 0);
1184
1185
1186 #ifdef LEAP_SUPPORT
1187                         if (LEAP_CCKM_ON(pAd))
1188                         {
1189                                 STA_PORT_SECURED(pAd);
1190                                 pAd->StaCfg.WpaState = SS_FINISH;
1191                         }
1192 #endif // LEAP_SUPPORT //
1193                         pAd->Mlme.CntlMachine.CurrState = CNTL_IDLE;
1194                         DBGPRINT(RT_DEBUG_TRACE, ("CNTL - Re-assocition successful on BSS #%ld\n", pAd->MlmeAux.RoamIdx));
1195                 }
1196                 else
1197                 {
1198                         // reassoc failed, try to pick next BSS in the BSS Table
1199                         DBGPRINT(RT_DEBUG_TRACE, ("CNTL - Re-assocition fails on BSS #%ld\n", pAd->MlmeAux.RoamIdx));
1200                         pAd->MlmeAux.RoamIdx++;
1201                         IterateOnBssTab2(pAd);
1202                 }
1203         }
1204 }
1205
1206 /*
1207         ==========================================================================
1208         Description:
1209
1210         IRQL = DISPATCH_LEVEL
1211
1212         ==========================================================================
1213 */
1214 VOID LinkUp(
1215         IN PRTMP_ADAPTER pAd,
1216         IN UCHAR BssType)
1217 {
1218         ULONG   Now;
1219         UINT32  Data;
1220         BOOLEAN Cancelled;
1221         UCHAR   Value = 0, idx;
1222         MAC_TABLE_ENTRY *pEntry = NULL, *pCurrEntry;
1223
1224         if (RTMP_TEST_PSFLAG(pAd, fRTMP_PS_SET_PCI_CLK_OFF_COMMAND))
1225         {
1226                 RTMPPCIeLinkCtrlValueRestore(pAd, RESTORE_HALT);
1227                 RTMPusecDelay(6000);
1228                 pAd->bPCIclkOff = FALSE;
1229         }
1230
1231         pEntry = &pAd->MacTab.Content[BSSID_WCID];
1232
1233         //
1234         // ASSOC - DisassocTimeoutAction
1235         // CNTL - Dis-associate successful
1236         // !!! LINK DOWN !!!
1237         // [88888] OID_802_11_SSID should have returned NDTEST_WEP_AP2(Returned: )
1238         //
1239         // To prevent DisassocTimeoutAction to call Link down after we link up,
1240         // cancel the DisassocTimer no matter what it start or not.
1241         //
1242         RTMPCancelTimer(&pAd->MlmeAux.DisassocTimer,  &Cancelled);
1243
1244         COPY_SETTINGS_FROM_MLME_AUX_TO_ACTIVE_CFG(pAd);
1245
1246 #ifdef DOT11_N_SUPPORT
1247         COPY_HTSETTINGS_FROM_MLME_AUX_TO_ACTIVE_CFG(pAd);
1248 #endif // DOT11_N_SUPPORT //
1249         // It's quite difficult to tell if a newly added KEY is WEP or CKIP until a new BSS
1250         // is formed (either ASSOC/RE-ASSOC done or IBSS started. LinkUP should be a safe place
1251         // to examine if cipher algorithm switching is required.
1252         //rt2860b. Don't know why need this
1253         SwitchBetweenWepAndCkip(pAd);
1254
1255         // Before power save before link up function, We will force use 1R.
1256         // So after link up, check Rx antenna # again.
1257         RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &Value);
1258         if(pAd->Antenna.field.RxPath == 3)
1259         {
1260                 Value |= (0x10);
1261         }
1262         else if(pAd->Antenna.field.RxPath == 2)
1263         {
1264                 Value |= (0x8);
1265         }
1266         else if(pAd->Antenna.field.RxPath == 1)
1267         {
1268                 Value |= (0x0);
1269         }
1270         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, Value);
1271         pAd->StaCfg.BBPR3 = Value;
1272
1273         if (BssType == BSS_ADHOC)
1274         {
1275                 OPSTATUS_SET_FLAG(pAd, fOP_STATUS_ADHOC_ON);
1276                 OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_INFRA_ON);
1277
1278 #ifdef CARRIER_DETECTION_SUPPORT // Roger sync Carrier
1279                 // No carrier detection when adhoc
1280                 // CarrierDetectionStop(pAd);
1281                 pAd->CommonCfg.CarrierDetect.CD_State = CD_NORMAL;
1282 #endif // CARRIER_DETECTION_SUPPORT //
1283
1284                 DBGPRINT(RT_DEBUG_TRACE, ("!!!Adhoc LINK UP !!! \n" ));
1285         }
1286         else
1287         {
1288                 OPSTATUS_SET_FLAG(pAd, fOP_STATUS_INFRA_ON);
1289                 OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_ADHOC_ON);
1290
1291                 DBGPRINT(RT_DEBUG_TRACE, ("!!!Infra LINK UP !!! \n" ));
1292         }
1293
1294         // 3*3
1295         // reset Tx beamforming bit
1296         RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R4, &Value);
1297         Value &= (~0x01);
1298         Value |= pAd->CommonCfg.RegTransmitSetting.field.TxBF;
1299         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R4, Value);
1300
1301 #ifdef DOT11_N_SUPPORT
1302         // Change to AP channel
1303     if ((pAd->CommonCfg.CentralChannel > pAd->CommonCfg.Channel) && (pAd->MlmeAux.HtCapability.HtCapInfo.ChannelWidth == BW_40))
1304         {
1305                 // Must using 40MHz.
1306                 pAd->CommonCfg.BBPCurrentBW = BW_40;
1307                 AsicSwitchChannel(pAd, pAd->CommonCfg.CentralChannel, FALSE);
1308                 AsicLockChannel(pAd, pAd->CommonCfg.CentralChannel);
1309
1310                 RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R4, &Value);
1311                 Value &= (~0x18);
1312                 Value |= 0x10;
1313                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R4, Value);
1314
1315                 //  RX : control channel at lower
1316                 RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &Value);
1317                 Value &= (~0x20);
1318                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, Value);
1319         pAd->StaCfg.BBPR3 = Value;
1320
1321                 RTMP_IO_READ32(pAd, TX_BAND_CFG, &Data);
1322                 Data &= 0xfffffffe;
1323                 RTMP_IO_WRITE32(pAd, TX_BAND_CFG, Data);
1324
1325                 if (pAd->MACVersion == 0x28600100)
1326                 {
1327                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R69, 0x1A);
1328                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R70, 0x0A);
1329                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R73, 0x16);
1330             DBGPRINT(RT_DEBUG_TRACE, ("!!!rt2860C !!! \n" ));
1331                 }
1332
1333                 DBGPRINT(RT_DEBUG_TRACE, ("!!!40MHz Lower LINK UP !!! Control Channel at Below. Central = %d \n", pAd->CommonCfg.CentralChannel ));
1334         }
1335         else if ((pAd->CommonCfg.CentralChannel < pAd->CommonCfg.Channel) && (pAd->MlmeAux.HtCapability.HtCapInfo.ChannelWidth == BW_40))
1336     {
1337             // Must using 40MHz.
1338                 pAd->CommonCfg.BBPCurrentBW = BW_40;
1339                 AsicSwitchChannel(pAd, pAd->CommonCfg.CentralChannel, FALSE);
1340             AsicLockChannel(pAd, pAd->CommonCfg.CentralChannel);
1341
1342                 RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R4, &Value);
1343                 Value &= (~0x18);
1344                 Value |= 0x10;
1345                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R4, Value);
1346
1347                 RTMP_IO_READ32(pAd, TX_BAND_CFG, &Data);
1348                 Data |= 0x1;
1349                 RTMP_IO_WRITE32(pAd, TX_BAND_CFG, Data);
1350
1351                 RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &Value);
1352             Value |= (0x20);
1353                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, Value);
1354         pAd->StaCfg.BBPR3 = Value;
1355
1356                 if (pAd->MACVersion == 0x28600100)
1357                 {
1358                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R69, 0x1A);
1359                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R70, 0x0A);
1360                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R73, 0x16);
1361                             DBGPRINT(RT_DEBUG_TRACE, ("!!!rt2860C !!! \n" ));
1362                 }
1363
1364             DBGPRINT(RT_DEBUG_TRACE, ("!!! 40MHz Upper LINK UP !!! Control Channel at UpperCentral = %d \n", pAd->CommonCfg.CentralChannel ));
1365     }
1366     else
1367 #endif // DOT11_N_SUPPORT //
1368     {
1369             pAd->CommonCfg.BBPCurrentBW = BW_20;
1370                 pAd->CommonCfg.CentralChannel = pAd->CommonCfg.Channel;
1371                 AsicSwitchChannel(pAd, pAd->CommonCfg.Channel, FALSE);
1372                 AsicLockChannel(pAd, pAd->CommonCfg.Channel);
1373
1374                 RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R4, &Value);
1375                 Value &= (~0x18);
1376                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R4, Value);
1377
1378                 RTMP_IO_READ32(pAd, TX_BAND_CFG, &Data);
1379                 Data &= 0xfffffffe;
1380                 RTMP_IO_WRITE32(pAd, TX_BAND_CFG, Data);
1381
1382                 RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &Value);
1383                 Value &= (~0x20);
1384                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, Value);
1385         pAd->StaCfg.BBPR3 = Value;
1386
1387                 if (pAd->MACVersion == 0x28600100)
1388                 {
1389                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R69, 0x16);
1390                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R70, 0x08);
1391                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R73, 0x11);
1392             DBGPRINT(RT_DEBUG_TRACE, ("!!!rt2860C !!! \n" ));
1393                 }
1394
1395             DBGPRINT(RT_DEBUG_TRACE, ("!!! 20MHz LINK UP !!! \n" ));
1396     }
1397
1398         RTMPSetAGCInitValue(pAd, pAd->CommonCfg.BBPCurrentBW);
1399         //
1400         // Save BBP_R66 value, it will be used in RTUSBResumeMsduTransmission
1401         //
1402         RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R66, &pAd->BbpTuning.R66CurrentValue);
1403
1404         DBGPRINT(RT_DEBUG_TRACE, ("!!! LINK UP !!! (BssType=%d, AID=%d, ssid=%s, Channel=%d, CentralChannel = %d)\n",
1405                 BssType, pAd->StaActive.Aid, pAd->CommonCfg.Ssid, pAd->CommonCfg.Channel, pAd->CommonCfg.CentralChannel));
1406
1407 #ifdef DOT11_N_SUPPORT
1408         DBGPRINT(RT_DEBUG_TRACE, ("!!! LINK UP !!! (Density =%d, )\n", pAd->MacTab.Content[BSSID_WCID].MpduDensity));
1409 #endif // DOT11_N_SUPPORT //
1410
1411                 AsicSetBssid(pAd, pAd->CommonCfg.Bssid);
1412
1413         AsicSetSlotTime(pAd, TRUE);
1414         AsicSetEdcaParm(pAd, &pAd->CommonCfg.APEdcaParm);
1415
1416         // Call this for RTS protectionfor legacy rate, we will always enable RTS threshold, but normally it will not hit
1417         AsicUpdateProtect(pAd, 0, (OFDMSETPROTECT | CCKSETPROTECT), TRUE, FALSE);
1418
1419 #ifdef DOT11_N_SUPPORT
1420         if ((pAd->StaActive.SupportedPhyInfo.bHtEnable == TRUE))
1421         {
1422                 // Update HT protectionfor based on AP's operating mode.
1423         if (pAd->MlmeAux.AddHtInfo.AddHtInfo2.NonGfPresent == 1)
1424         {
1425                 AsicUpdateProtect(pAd, pAd->MlmeAux.AddHtInfo.AddHtInfo2.OperaionMode,  ALLN_SETPROTECT, FALSE, TRUE);
1426         }
1427         else
1428                         AsicUpdateProtect(pAd, pAd->MlmeAux.AddHtInfo.AddHtInfo2.OperaionMode,  ALLN_SETPROTECT, FALSE, FALSE);
1429         }
1430 #endif // DOT11_N_SUPPORT //
1431
1432         NdisZeroMemory(&pAd->DrsCounters, sizeof(COUNTER_DRS));
1433
1434         NdisGetSystemUpTime(&Now);
1435         pAd->StaCfg.LastBeaconRxTime = Now;   // last RX timestamp
1436
1437         if ((pAd->CommonCfg.TxPreamble != Rt802_11PreambleLong) &&
1438                 CAP_IS_SHORT_PREAMBLE_ON(pAd->StaActive.CapabilityInfo))
1439         {
1440                 MlmeSetTxPreamble(pAd, Rt802_11PreambleShort);
1441         }
1442
1443         OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_AGGREGATION_INUSED);
1444
1445         if (pAd->CommonCfg.RadarDetect.RDMode == RD_SILENCE_MODE)
1446         {
1447         }
1448         pAd->CommonCfg.RadarDetect.RDMode = RD_NORMAL_MODE;
1449
1450         if (BssType == BSS_ADHOC)
1451         {
1452                 MakeIbssBeacon(pAd);
1453                 if ((pAd->CommonCfg.Channel > 14)
1454                         && (pAd->CommonCfg.bIEEE80211H == 1)
1455                         && RadarChannelCheck(pAd, pAd->CommonCfg.Channel))
1456                 {
1457                         ; //Do nothing
1458                 }
1459                 else
1460                 {
1461                         AsicEnableIbssSync(pAd);
1462                 }
1463
1464                 // In ad hoc mode, use MAC table from index 1.
1465                 // 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.
1466                 RTMP_IO_WRITE32(pAd, MAC_WCID_BASE, 0x00);
1467                 RTMP_IO_WRITE32(pAd, 0x1808, 0x00);
1468
1469                 // If WEP is enabled, add key material and cipherAlg into Asic
1470                 // Fill in Shared Key Table(offset: 0x6c00) and Shared Key Mode(offset: 0x7000)
1471
1472                 if (pAd->StaCfg.WepStatus == Ndis802_11WEPEnabled)
1473                 {
1474                         PUCHAR  Key;
1475                         UCHAR   CipherAlg;
1476
1477                         for (idx=0; idx < SHARE_KEY_NUM; idx++)
1478                 {
1479                                 CipherAlg = pAd->SharedKey[BSS0][idx].CipherAlg;
1480                         Key = pAd->SharedKey[BSS0][idx].Key;
1481
1482                                 if (pAd->SharedKey[BSS0][idx].KeyLen > 0)
1483                                 {
1484                                         // Set key material and cipherAlg to Asic
1485                                 AsicAddSharedKeyEntry(pAd, BSS0, idx, CipherAlg, Key, NULL, NULL);
1486
1487                     if (idx == pAd->StaCfg.DefaultKeyId)
1488                                         {
1489                                                 // Update WCID attribute table and IVEIV table for this group key table
1490                                                 RTMPAddWcidAttributeEntry(pAd, BSS0, idx, CipherAlg, NULL);
1491                                         }
1492                                 }
1493
1494
1495                         }
1496                 }
1497                 // If WPANone is enabled, add key material and cipherAlg into Asic
1498                 // Fill in Shared Key Table(offset: 0x6c00) and Shared Key Mode(offset: 0x7000)
1499                 else if (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPANone)
1500                 {
1501                         pAd->StaCfg.DefaultKeyId = 0;   // always be zero
1502
1503             NdisZeroMemory(&pAd->SharedKey[BSS0][0], sizeof(CIPHER_KEY));
1504                                                         pAd->SharedKey[BSS0][0].KeyLen = LEN_TKIP_EK;
1505                         NdisMoveMemory(pAd->SharedKey[BSS0][0].Key, pAd->StaCfg.PMK, LEN_TKIP_EK);
1506
1507             if (pAd->StaCfg.PairCipher == Ndis802_11Encryption2Enabled)
1508             {
1509                         NdisMoveMemory(pAd->SharedKey[BSS0][0].RxMic, &pAd->StaCfg.PMK[16], LEN_TKIP_RXMICK);
1510                         NdisMoveMemory(pAd->SharedKey[BSS0][0].TxMic, &pAd->StaCfg.PMK[16], LEN_TKIP_TXMICK);
1511             }
1512
1513                         // Decide its ChiperAlg
1514                         if (pAd->StaCfg.PairCipher == Ndis802_11Encryption2Enabled)
1515                                 pAd->SharedKey[BSS0][0].CipherAlg = CIPHER_TKIP;
1516                         else if (pAd->StaCfg.PairCipher == Ndis802_11Encryption3Enabled)
1517                                 pAd->SharedKey[BSS0][0].CipherAlg = CIPHER_AES;
1518                         else
1519             {
1520                 DBGPRINT(RT_DEBUG_TRACE, ("Unknow Cipher (=%d), set Cipher to AES\n", pAd->StaCfg.PairCipher));
1521                                 pAd->SharedKey[BSS0][0].CipherAlg = CIPHER_AES;
1522             }
1523
1524                         // Set key material and cipherAlg to Asic
1525                         AsicAddSharedKeyEntry(pAd,
1526                                                                   BSS0,
1527                                                                   0,
1528                                                                   pAd->SharedKey[BSS0][0].CipherAlg,
1529                                                                   pAd->SharedKey[BSS0][0].Key,
1530                                                                   pAd->SharedKey[BSS0][0].TxMic,
1531                                                                   pAd->SharedKey[BSS0][0].RxMic);
1532
1533             // Update WCID attribute table and IVEIV table for this group key table
1534                         RTMPAddWcidAttributeEntry(pAd, BSS0, 0, pAd->SharedKey[BSS0][0].CipherAlg, NULL);
1535
1536                 }
1537
1538         }
1539         else // BSS_INFRA
1540         {
1541                 // Check the new SSID with last SSID
1542                 while (Cancelled == TRUE)
1543                 {
1544                         if (pAd->CommonCfg.LastSsidLen == pAd->CommonCfg.SsidLen)
1545                         {
1546                                 if (RTMPCompareMemory(pAd->CommonCfg.LastSsid, pAd->CommonCfg.Ssid, pAd->CommonCfg.LastSsidLen) == 0)
1547                                 {
1548                                         // Link to the old one no linkdown is required.
1549                                         break;
1550                                 }
1551                         }
1552                         // Send link down event before set to link up
1553                         pAd->IndicateMediaState = NdisMediaStateDisconnected;
1554                         RTMP_IndicateMediaState(pAd);
1555             pAd->ExtraInfo = GENERAL_LINK_DOWN;
1556                         DBGPRINT(RT_DEBUG_TRACE, ("NDIS_STATUS_MEDIA_DISCONNECT Event AA!\n"));
1557                         break;
1558                 }
1559
1560                 //
1561                 // On WPA mode, Remove All Keys if not connect to the last BSSID
1562                 // Key will be set after 4-way handshake.
1563                 //
1564                 if ((pAd->StaCfg.AuthMode >= Ndis802_11AuthModeWPA))
1565                 {
1566                         ULONG           IV;
1567
1568                         // Remove all WPA keys
1569                         RTMPWPARemoveAllKeys(pAd);
1570                         pAd->StaCfg.PortSecured = WPA_802_1X_PORT_NOT_SECURED;
1571                         pAd->StaCfg.PrivacyFilter = Ndis802_11PrivFilter8021xWEP;
1572
1573                         // Fixed connection failed with Range Maximizer - 515 AP (Marvell Chip) when security is WPAPSK/TKIP
1574                         // If IV related values are too large in GroupMsg2, AP would ignore this message.
1575                         IV = 0;
1576                         IV |= (pAd->StaCfg.DefaultKeyId << 30);
1577                         AsicUpdateWCIDIVEIV(pAd, BSSID_WCID, IV, 0);
1578
1579                         RTMP_CLEAR_PSFLAG(pAd, fRTMP_PS_CAN_GO_SLEEP);
1580                 }
1581                 // NOTE:
1582                 // the decision of using "short slot time" or not may change dynamically due to
1583                 // new STA association to the AP. so we have to decide that upon parsing BEACON, not here
1584
1585                 // NOTE:
1586                 // the decision to use "RTC/CTS" or "CTS-to-self" protection or not may change dynamically
1587                 // due to new STA association to the AP. so we have to decide that upon parsing BEACON, not here
1588
1589                 ComposePsPoll(pAd);
1590                 ComposeNullFrame(pAd);
1591
1592                         AsicEnableBssSync(pAd);
1593
1594                 // Add BSSID to WCID search table
1595                 AsicUpdateRxWCIDTable(pAd, BSSID_WCID, pAd->CommonCfg.Bssid);
1596
1597                 NdisAcquireSpinLock(&pAd->MacTabLock);
1598                 // add this BSSID entry into HASH table
1599                 {
1600                         UCHAR HashIdx;
1601
1602                         //pEntry = &pAd->MacTab.Content[BSSID_WCID];
1603                         HashIdx = MAC_ADDR_HASH_INDEX(pAd->CommonCfg.Bssid);
1604                         if (pAd->MacTab.Hash[HashIdx] == NULL)
1605                         {
1606                                 pAd->MacTab.Hash[HashIdx] = pEntry;
1607                         }
1608                         else
1609                         {
1610                                 pCurrEntry = pAd->MacTab.Hash[HashIdx];
1611                                 while (pCurrEntry->pNext != NULL)
1612                                         pCurrEntry = pCurrEntry->pNext;
1613                                 pCurrEntry->pNext = pEntry;
1614                         }
1615                 }
1616                 NdisReleaseSpinLock(&pAd->MacTabLock);
1617
1618
1619                 // If WEP is enabled, add paiewise and shared key
1620 #ifdef WPA_SUPPLICANT_SUPPORT
1621         if (((pAd->StaCfg.WpaSupplicantUP)&&
1622              (pAd->StaCfg.WepStatus == Ndis802_11WEPEnabled)&&
1623              (pAd->StaCfg.PortSecured == WPA_802_1X_PORT_SECURED)) ||
1624             ((pAd->StaCfg.WpaSupplicantUP == WPA_SUPPLICANT_DISABLE)&&
1625               (pAd->StaCfg.WepStatus == Ndis802_11WEPEnabled)))
1626 #else
1627                 if (pAd->StaCfg.WepStatus == Ndis802_11WEPEnabled)
1628 #endif // WPA_SUPPLICANT_SUPPORT //
1629                 {
1630                         PUCHAR  Key;
1631                         UCHAR   CipherAlg;
1632
1633                         for (idx=0; idx < SHARE_KEY_NUM; idx++)
1634                 {
1635                                 CipherAlg = pAd->SharedKey[BSS0][idx].CipherAlg;
1636                         Key = pAd->SharedKey[BSS0][idx].Key;
1637
1638                                 if (pAd->SharedKey[BSS0][idx].KeyLen > 0)
1639                                 {
1640                                         // Set key material and cipherAlg to Asic
1641                                 AsicAddSharedKeyEntry(pAd, BSS0, idx, CipherAlg, Key, NULL, NULL);
1642
1643                                         if (idx == pAd->StaCfg.DefaultKeyId)
1644                                         {
1645                                                 // Assign group key info
1646                                                 RTMPAddWcidAttributeEntry(pAd, BSS0, idx, CipherAlg, NULL);
1647
1648                                                 // Assign pairwise key info
1649                                                 RTMPAddWcidAttributeEntry(pAd, BSS0, idx, CipherAlg, pEntry);
1650                                         }
1651                                 }
1652                         }
1653                 }
1654
1655                 // only INFRASTRUCTURE mode need to indicate connectivity immediately; ADHOC mode
1656                 // should wait until at least 2 active nodes in this BSSID.
1657                 OPSTATUS_SET_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED);
1658
1659         // For GUI ++
1660                 if (pAd->StaCfg.AuthMode < Ndis802_11AuthModeWPA)
1661                 {
1662                         pAd->IndicateMediaState = NdisMediaStateConnected;
1663                         pAd->ExtraInfo = GENERAL_LINK_UP;
1664                 }
1665         // --
1666                 RTMP_IndicateMediaState(pAd);
1667
1668                 // Add BSSID in my MAC Table.
1669         NdisAcquireSpinLock(&pAd->MacTabLock);
1670                 RTMPMoveMemory(pAd->MacTab.Content[BSSID_WCID].Addr, pAd->CommonCfg.Bssid, MAC_ADDR_LEN);
1671                 pAd->MacTab.Content[BSSID_WCID].Aid = BSSID_WCID;
1672                 pAd->MacTab.Content[BSSID_WCID].pAd = pAd;
1673                 pAd->MacTab.Content[BSSID_WCID].ValidAsCLI = TRUE;      //Although this is bssid..still set ValidAsCl
1674                 pAd->MacTab.Size = 1;   // infra mode always set MACtab size =1.
1675                 pAd->MacTab.Content[BSSID_WCID].Sst = SST_ASSOC;
1676                 pAd->MacTab.Content[BSSID_WCID].AuthState = SST_ASSOC;
1677                 pAd->MacTab.Content[BSSID_WCID].WepStatus = pAd->StaCfg.WepStatus;
1678         NdisReleaseSpinLock(&pAd->MacTabLock);
1679
1680                 DBGPRINT(RT_DEBUG_TRACE, ("!!! LINK UP !!!  ClientStatusFlags=%lx)\n",
1681                         pAd->MacTab.Content[BSSID_WCID].ClientStatusFlags));
1682
1683                 MlmeUpdateTxRates(pAd, TRUE, BSS0);
1684 #ifdef DOT11_N_SUPPORT
1685                 MlmeUpdateHtTxRates(pAd, BSS0);
1686                 DBGPRINT(RT_DEBUG_TRACE, ("!!! LINK UP !! (StaActive.bHtEnable =%d, )\n", pAd->StaActive.SupportedPhyInfo.bHtEnable));
1687 #endif // DOT11_N_SUPPORT //
1688
1689                 //
1690                 // Report Adjacent AP report.
1691                 //
1692 #ifdef LEAP_SUPPORT
1693                 CCXAdjacentAPReport(pAd);
1694 #endif // LEAP_SUPPORT //
1695
1696                 if (pAd->CommonCfg.bAggregationCapable)
1697                 {
1698                         if ((pAd->CommonCfg.bPiggyBackCapable) && (pAd->MlmeAux.APRalinkIe & 0x00000003) == 3)
1699                         {
1700
1701                                 OPSTATUS_SET_FLAG(pAd, fOP_STATUS_PIGGYBACK_INUSED);
1702                                 OPSTATUS_SET_FLAG(pAd, fOP_STATUS_AGGREGATION_INUSED);
1703                 RTMPSetPiggyBack(pAd, TRUE);
1704                                 DBGPRINT(RT_DEBUG_TRACE, ("Turn on Piggy-Back\n"));
1705                         }
1706                         else if (pAd->MlmeAux.APRalinkIe & 0x00000001)
1707                         {
1708                                 OPSTATUS_SET_FLAG(pAd, fOP_STATUS_AGGREGATION_INUSED);
1709                         }
1710                 }
1711
1712                 if (pAd->MlmeAux.APRalinkIe != 0x0)
1713                 {
1714 #ifdef DOT11_N_SUPPORT
1715                         if (CLIENT_STATUS_TEST_FLAG(&pAd->MacTab.Content[BSSID_WCID], fCLIENT_STATUS_RDG_CAPABLE))
1716                         {
1717                                 AsicEnableRDG(pAd);
1718                         }
1719 #endif // DOT11_N_SUPPORT //
1720                         OPSTATUS_SET_FLAG(pAd, fCLIENT_STATUS_RALINK_CHIPSET);
1721                         CLIENT_STATUS_SET_FLAG(&pAd->MacTab.Content[BSSID_WCID], fCLIENT_STATUS_RALINK_CHIPSET);
1722                 }
1723                 else
1724                 {
1725                         OPSTATUS_CLEAR_FLAG(pAd, fCLIENT_STATUS_RALINK_CHIPSET);
1726                         CLIENT_STATUS_CLEAR_FLAG(&pAd->MacTab.Content[BSSID_WCID], fCLIENT_STATUS_RALINK_CHIPSET);
1727                 }
1728         }
1729
1730 #ifdef DOT11_N_SUPPORT
1731         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));
1732 #endif // DOT11_N_SUPPORT //
1733
1734         // Set LED
1735         RTMPSetLED(pAd, LED_LINK_UP);
1736
1737         pAd->Mlme.PeriodicRound = 0;
1738         pAd->Mlme.OneSecPeriodicRound = 0;
1739         pAd->bConfigChanged = FALSE;        // Reset config flag
1740         pAd->ExtraInfo = GENERAL_LINK_UP;   // Update extra information to link is up
1741
1742         // Set asic auto fall back
1743         {
1744                 PUCHAR                                  pTable;
1745                 UCHAR                                   TableSize = 0;
1746
1747                 MlmeSelectTxRateTable(pAd, &pAd->MacTab.Content[BSSID_WCID], &pTable, &TableSize, &pAd->CommonCfg.TxRateIndex);
1748                 AsicUpdateAutoFallBackTable(pAd, pTable);
1749         }
1750
1751         NdisAcquireSpinLock(&pAd->MacTabLock);
1752     pEntry->HTPhyMode.word = pAd->StaCfg.HTPhyMode.word;
1753     pEntry->MaxHTPhyMode.word = pAd->StaCfg.HTPhyMode.word;
1754         if (pAd->StaCfg.bAutoTxRateSwitch == FALSE)
1755         {
1756                 pEntry->bAutoTxRateSwitch = FALSE;
1757 #ifdef DOT11_N_SUPPORT
1758                 if (pEntry->HTPhyMode.field.MCS == 32)
1759                         pEntry->HTPhyMode.field.ShortGI = GI_800;
1760
1761                 if ((pEntry->HTPhyMode.field.MCS > MCS_7) || (pEntry->HTPhyMode.field.MCS == 32))
1762                         pEntry->HTPhyMode.field.STBC = STBC_NONE;
1763 #endif // DOT11_N_SUPPORT //
1764                 // If the legacy mode is set, overwrite the transmit setting of this entry.
1765                 if (pEntry->HTPhyMode.field.MODE <= MODE_OFDM)
1766                         RTMPUpdateLegacyTxSetting((UCHAR)pAd->StaCfg.DesiredTransmitSetting.field.FixedTxMode, pEntry);
1767         }
1768         else
1769                 pEntry->bAutoTxRateSwitch = TRUE;
1770         NdisReleaseSpinLock(&pAd->MacTabLock);
1771
1772         //  Let Link Status Page display first initial rate.
1773         pAd->LastTxRate = (USHORT)(pEntry->HTPhyMode.word);
1774         // Select DAC according to HT or Legacy
1775         if (pAd->StaActive.SupportedPhyInfo.MCSSet[0] != 0x00)
1776         {
1777                 RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R1, &Value);
1778                 Value &= (~0x18);
1779                 if (pAd->Antenna.field.TxPath == 2)
1780                 {
1781                     Value |= 0x10;
1782                 }
1783                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R1, Value);
1784         }
1785         else
1786         {
1787                 RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R1, &Value);
1788                 Value &= (~0x18);
1789                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R1, Value);
1790         }
1791
1792 #ifdef DOT11_N_SUPPORT
1793         if (pAd->StaActive.SupportedPhyInfo.bHtEnable == FALSE)
1794         {
1795         }
1796         else if (pEntry->MaxRAmpduFactor == 0)
1797         {
1798             // If HT AP doesn't support MaxRAmpduFactor = 1, we need to set max PSDU to 0.
1799             // Because our Init value is 1 at MACRegTable.
1800                 RTMP_IO_WRITE32(pAd, MAX_LEN_CFG, 0x0fff);
1801         }
1802 #endif // DOT11_N_SUPPORT //
1803
1804         // Patch for Marvel AP to gain high throughput
1805         // Need to set as following,
1806         // 1. Set txop in register-EDCA_AC0_CFG as 0x60
1807         // 2. Set EnTXWriteBackDDONE in register-WPDMA_GLO_CFG as zero
1808         // 3. PBF_MAX_PCNT as 0x1F3FBF9F
1809         // 4. kick per two packets when dequeue
1810         //
1811         // Txop can only be modified when RDG is off, WMM is disable and TxBurst is enable
1812         //
1813         // if 1. Legacy AP WMM on,  or 2. 11n AP, AMPDU disable.  Force turn off burst no matter what bEnableTxBurst is.
1814 #ifdef DOT11_N_SUPPORT
1815         if (((pAd->StaActive.SupportedPhyInfo.bHtEnable == FALSE) && (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_WMM_INUSED)))
1816                 || ((pAd->StaActive.SupportedPhyInfo.bHtEnable == TRUE) && (pAd->CommonCfg.BACapability.field.Policy == BA_NOTUSE)))
1817         {
1818                 RTMP_IO_READ32(pAd, EDCA_AC0_CFG, &Data);
1819                 Data  &= 0xFFFFFF00;
1820                 RTMP_IO_WRITE32(pAd, EDCA_AC0_CFG, Data);
1821
1822                 RTMP_IO_WRITE32(pAd, PBF_MAX_PCNT, 0x1F3F7F9F);
1823                 DBGPRINT(RT_DEBUG_TRACE, ("Txburst 1\n"));
1824         }
1825         else
1826 #endif // DOT11_N_SUPPORT //
1827         if (pAd->CommonCfg.bEnableTxBurst)
1828         {
1829                 RTMP_IO_READ32(pAd, EDCA_AC0_CFG, &Data);
1830                 Data  &= 0xFFFFFF00;
1831                 Data  |= 0x60;
1832                 RTMP_IO_WRITE32(pAd, EDCA_AC0_CFG, Data);
1833                 pAd->CommonCfg.IOTestParm.bNowAtherosBurstOn = TRUE;
1834
1835                 RTMP_IO_WRITE32(pAd, PBF_MAX_PCNT, 0x1F3FBF9F);
1836                 DBGPRINT(RT_DEBUG_TRACE, ("Txburst 2\n"));
1837         }
1838         else
1839         {
1840                 RTMP_IO_READ32(pAd, EDCA_AC0_CFG, &Data);
1841                 Data  &= 0xFFFFFF00;
1842                 RTMP_IO_WRITE32(pAd, EDCA_AC0_CFG, Data);
1843
1844                 RTMP_IO_WRITE32(pAd, PBF_MAX_PCNT, 0x1F3F7F9F);
1845                 DBGPRINT(RT_DEBUG_TRACE, ("Txburst 3\n"));
1846         }
1847
1848 #ifdef DOT11_N_SUPPORT
1849         // Re-check to turn on TX burst or not.
1850         if ((pAd->CommonCfg.IOTestParm.bLastAtheros == TRUE) && ((STA_WEP_ON(pAd))||(STA_TKIP_ON(pAd))))
1851         {
1852                 pAd->CommonCfg.IOTestParm.bNextDisableRxBA = TRUE;
1853                 if (pAd->CommonCfg.bEnableTxBurst)
1854                 {
1855                     UINT32 MACValue = 0;
1856                         // Force disable  TXOP value in this case. The same action in MLMEUpdateProtect too.
1857                         // I didn't change PBF_MAX_PCNT setting.
1858                         RTMP_IO_READ32(pAd, EDCA_AC0_CFG, &MACValue);
1859                         MACValue  &= 0xFFFFFF00;
1860                         RTMP_IO_WRITE32(pAd, EDCA_AC0_CFG, MACValue);
1861                         pAd->CommonCfg.IOTestParm.bNowAtherosBurstOn = FALSE;
1862                 }
1863         }
1864         else
1865         {
1866                 pAd->CommonCfg.IOTestParm.bNextDisableRxBA = FALSE;
1867         }
1868 #endif // DOT11_N_SUPPORT //
1869
1870         pAd->CommonCfg.IOTestParm.bLastAtheros = FALSE;
1871         COPY_MAC_ADDR(pAd->CommonCfg.LastBssid, pAd->CommonCfg.Bssid);
1872         DBGPRINT(RT_DEBUG_TRACE, ("!!!pAd->bNextDisableRxBA= %d \n", pAd->CommonCfg.IOTestParm.bNextDisableRxBA));
1873         // BSSID add in one MAC entry too.  Because in Tx, ASIC need to check Cipher and IV/EIV, BAbitmap
1874         // Pther information in MACTab.Content[BSSID_WCID] is not necessary for driver.
1875         // Note: As STA, The MACTab.Content[BSSID_WCID]. PairwiseKey and Shared Key for BSS0 are the same.
1876
1877     if (pAd->StaCfg.WepStatus <= Ndis802_11WEPDisabled)
1878     {
1879         pAd->StaCfg.PortSecured = WPA_802_1X_PORT_SECURED;
1880                 pAd->StaCfg.PrivacyFilter = Ndis802_11PrivFilterAcceptAll;
1881         }
1882
1883         NdisAcquireSpinLock(&pAd->MacTabLock);
1884         pEntry->PortSecured = pAd->StaCfg.PortSecured;
1885         NdisReleaseSpinLock(&pAd->MacTabLock);
1886
1887     //
1888         // Patch Atheros AP TX will breakdown issue.
1889         // AP Model: DLink DWL-8200AP
1890         //
1891         if (INFRA_ON(pAd) && OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_WMM_INUSED) && STA_TKIP_ON(pAd))
1892         {
1893                 RTMP_IO_WRITE32(pAd, RX_PARSER_CFG, 0x01);
1894         }
1895         else
1896         {
1897                 RTMP_IO_WRITE32(pAd, RX_PARSER_CFG, 0x00);
1898         }
1899
1900         RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS);
1901         RTMP_CLEAR_PSFLAG(pAd, fRTMP_PS_GO_TO_SLEEP_NOW);
1902
1903 #ifdef DOT11_N_SUPPORT
1904 #ifdef DOT11N_DRAFT3
1905         if ((pAd->CommonCfg.BACapability.field.b2040CoexistScanSup) && (pAd->CommonCfg.Channel <= 11))
1906         {
1907                 OPSTATUS_SET_FLAG(pAd, fOP_STATUS_SCAN_2040);
1908                 BuildEffectedChannelList(pAd);
1909         }
1910 #endif // DOT11N_DRAFT3 //
1911 #endif // DOT11_N_SUPPORT //
1912 }
1913
1914 /*
1915         ==========================================================================
1916
1917         Routine Description:
1918                 Disconnect current BSSID
1919
1920         Arguments:
1921                 pAd                             - Pointer to our adapter
1922                 IsReqFromAP             - Request from AP
1923
1924         Return Value:
1925                 None
1926
1927         IRQL = DISPATCH_LEVEL
1928
1929         Note:
1930                 We need more information to know it's this requst from AP.
1931                 If yes! we need to do extra handling, for example, remove the WPA key.
1932                 Otherwise on 4-way handshaking will faied, since the WPA key didn't be
1933                 remove while auto reconnect.
1934                 Disconnect request from AP, it means we will start afresh 4-way handshaking
1935                 on WPA mode.
1936
1937         ==========================================================================
1938 */
1939 VOID LinkDown(
1940         IN PRTMP_ADAPTER pAd,
1941         IN  BOOLEAN      IsReqFromAP)
1942 {
1943         UCHAR                       i, ByteValue = 0;
1944         BOOLEAN         Cancelled;
1945
1946         // Do nothing if monitor mode is on
1947         if (MONITOR_ON(pAd))
1948                 return;
1949
1950         RTMP_CLEAR_PSFLAG(pAd, fRTMP_PS_GO_TO_SLEEP_NOW);
1951         RTMPCancelTimer(&pAd->Mlme.PsPollTimer,         &Cancelled);
1952
1953         // Not allow go to sleep within linkdown function.
1954         RTMP_CLEAR_PSFLAG(pAd, fRTMP_PS_CAN_GO_SLEEP);
1955
1956     if (pAd->CommonCfg.bWirelessEvent)
1957         {
1958                 RTMPSendWirelessEvent(pAd, IW_STA_LINKDOWN_EVENT_FLAG, pAd->MacTab.Content[BSSID_WCID].Addr, BSS0, 0);
1959         }
1960
1961         DBGPRINT(RT_DEBUG_TRACE, ("!!! LINK DOWN !!!\n"));
1962         OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_AGGREGATION_INUSED);
1963
1964     if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_ADVANCE_POWER_SAVE_PCIE_DEVICE))
1965     {
1966             BOOLEAN Cancelled;
1967         pAd->Mlme.bPsPollTimerRunning = FALSE;
1968         RTMPCancelTimer(&pAd->Mlme.PsPollTimer, &Cancelled);
1969     }
1970
1971     if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_DOZE) ||
1972                 RTMP_TEST_PSFLAG(pAd, fRTMP_PS_SET_PCI_CLK_OFF_COMMAND) ||
1973                 RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_IDLE_RADIO_OFF))
1974     {
1975                 AsicForceWakeup(pAd, RTMP_HALT);
1976         OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_DOZE);
1977     }
1978
1979     pAd->bPCIclkOff = FALSE;
1980         if (ADHOC_ON(pAd))              // Adhoc mode link down
1981         {
1982                 DBGPRINT(RT_DEBUG_TRACE, ("!!! LINK DOWN 1!!!\n"));
1983
1984                 OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_ADHOC_ON);
1985                 OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED);
1986                 pAd->IndicateMediaState = NdisMediaStateDisconnected;
1987                 RTMP_IndicateMediaState(pAd);
1988         pAd->ExtraInfo = GENERAL_LINK_DOWN;
1989                 BssTableDeleteEntry(&pAd->ScanTab, pAd->CommonCfg.Bssid, pAd->CommonCfg.Channel);
1990                 DBGPRINT(RT_DEBUG_TRACE, ("!!! MacTab.Size=%d !!!\n", pAd->MacTab.Size));
1991         }
1992         else                                    // Infra structure mode
1993         {
1994                 DBGPRINT(RT_DEBUG_TRACE, ("!!! LINK DOWN 2!!!\n"));
1995
1996 #ifdef QOS_DLS_SUPPORT
1997                 // DLS tear down frame must be sent before link down
1998                 // send DLS-TEAR_DOWN message
1999                 if (pAd->CommonCfg.bDLSCapable)
2000                 {
2001                         // tear down local dls table entry
2002                         for (i=0; i<MAX_NUM_OF_INIT_DLS_ENTRY; i++)
2003                         {
2004                                 if (pAd->StaCfg.DLSEntry[i].Valid && (pAd->StaCfg.DLSEntry[i].Status == DLS_FINISH))
2005                                 {
2006                                         pAd->StaCfg.DLSEntry[i].Status = DLS_NONE;
2007                                         RTMPSendDLSTearDownFrame(pAd, pAd->StaCfg.DLSEntry[i].MacAddr);
2008                                 }
2009                         }
2010
2011                         // tear down peer dls table entry
2012                         for (i=MAX_NUM_OF_INIT_DLS_ENTRY; i<MAX_NUM_OF_DLS_ENTRY; i++)
2013                         {
2014                                 if (pAd->StaCfg.DLSEntry[i].Valid && (pAd->StaCfg.DLSEntry[i].Status ==  DLS_FINISH))
2015                                 {
2016                                         pAd->StaCfg.DLSEntry[i].Status = DLS_NONE;
2017                                         RTMPSendDLSTearDownFrame(pAd, pAd->StaCfg.DLSEntry[i].MacAddr);
2018                                 }
2019                         }
2020                 }
2021 #endif // QOS_DLS_SUPPORT //
2022
2023                 OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_INFRA_ON);
2024                 OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED);
2025
2026                 // Saved last SSID for linkup comparison
2027                 pAd->CommonCfg.LastSsidLen = pAd->CommonCfg.SsidLen;
2028                 NdisMoveMemory(pAd->CommonCfg.LastSsid, pAd->CommonCfg.Ssid, pAd->CommonCfg.LastSsidLen);
2029                 COPY_MAC_ADDR(pAd->CommonCfg.LastBssid, pAd->CommonCfg.Bssid);
2030                 if (pAd->MlmeAux.CurrReqIsFromNdis == TRUE)
2031                 {
2032                         pAd->IndicateMediaState = NdisMediaStateDisconnected;
2033                         RTMP_IndicateMediaState(pAd);
2034             pAd->ExtraInfo = GENERAL_LINK_DOWN;
2035                         DBGPRINT(RT_DEBUG_TRACE, ("NDIS_STATUS_MEDIA_DISCONNECT Event A!\n"));
2036                         pAd->MlmeAux.CurrReqIsFromNdis = FALSE;
2037                 }
2038                 else
2039                 {
2040             //
2041                         // If disassociation request is from NDIS, then we don't need to delete BSSID from entry.
2042                         // Otherwise lost beacon or receive De-Authentication from AP,
2043                         // then we should delete BSSID from BssTable.
2044                         // If we don't delete from entry, roaming will fail.
2045                         //
2046                         BssTableDeleteEntry(&pAd->ScanTab, pAd->CommonCfg.Bssid, pAd->CommonCfg.Channel);
2047                 }
2048
2049                 // restore back to -
2050                 //      1. long slot (20 us) or short slot (9 us) time
2051                 //      2. turn on/off RTS/CTS and/or CTS-to-self protection
2052                 //      3. short preamble
2053                 OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_BG_PROTECTION_INUSED);
2054
2055                 if (pAd->StaCfg.CCXAdjacentAPReportFlag == TRUE)
2056                 {
2057                         //
2058                         // Record current AP's information.
2059                         // for later used reporting Adjacent AP report.
2060                         //
2061                         pAd->StaCfg.CCXAdjacentAPChannel = pAd->CommonCfg.Channel;
2062                         pAd->StaCfg.CCXAdjacentAPSsidLen = pAd->CommonCfg.SsidLen;
2063                         NdisMoveMemory(pAd->StaCfg.CCXAdjacentAPSsid, pAd->CommonCfg.Ssid, pAd->StaCfg.CCXAdjacentAPSsidLen);
2064                         COPY_MAC_ADDR(pAd->StaCfg.CCXAdjacentAPBssid, pAd->CommonCfg.Bssid);
2065                 }
2066
2067 #ifdef EXT_BUILD_CHANNEL_LIST
2068                 // Country IE of the AP will be evaluated and will be used.
2069                 if (pAd->StaCfg.IEEE80211dClientMode != Rt802_11_D_None)
2070                 {
2071                         NdisMoveMemory(&pAd->CommonCfg.CountryCode[0], &pAd->StaCfg.StaOriCountryCode[0], 2);
2072                         pAd->CommonCfg.Geography = pAd->StaCfg.StaOriGeography;
2073                         BuildChannelListEx(pAd);
2074                 }
2075 #endif // EXT_BUILD_CHANNEL_LIST //
2076
2077         }
2078
2079         for (i=1; i<MAX_LEN_OF_MAC_TABLE; i++)
2080         {
2081                 if (pAd->MacTab.Content[i].ValidAsCLI == TRUE)
2082                         MacTableDeleteEntry(pAd, pAd->MacTab.Content[i].Aid, pAd->MacTab.Content[i].Addr);
2083         }
2084
2085         pAd->StaCfg.CCXQosECWMin        = 4;
2086         pAd->StaCfg.CCXQosECWMax        = 10;
2087
2088         AsicSetSlotTime(pAd, TRUE); //FALSE);
2089         AsicSetEdcaParm(pAd, NULL);
2090
2091         // Set LED
2092         RTMPSetLED(pAd, LED_LINK_DOWN);
2093     pAd->LedIndicatorStregth = 0xF0;
2094     RTMPSetSignalLED(pAd, -100);        // Force signal strength Led to be turned off, firmware is not done it.
2095
2096                 AsicDisableSync(pAd);
2097
2098         pAd->Mlme.PeriodicRound = 0;
2099         pAd->Mlme.OneSecPeriodicRound = 0;
2100
2101         if (pAd->StaCfg.BssType == BSS_INFRA)
2102         {
2103                 // Remove StaCfg Information after link down
2104                 NdisZeroMemory(pAd->CommonCfg.Bssid, MAC_ADDR_LEN);
2105                 NdisZeroMemory(pAd->CommonCfg.Ssid, MAX_LEN_OF_SSID);
2106                 pAd->CommonCfg.SsidLen = 0;
2107         }
2108 #ifdef DOT11_N_SUPPORT
2109         NdisZeroMemory(&pAd->MlmeAux.HtCapability, sizeof(HT_CAPABILITY_IE));
2110         NdisZeroMemory(&pAd->MlmeAux.AddHtInfo, sizeof(ADD_HT_INFO_IE));
2111         pAd->MlmeAux.HtCapabilityLen = 0;
2112         pAd->MlmeAux.NewExtChannelOffset = 0xff;
2113 #endif // DOT11_N_SUPPORT //
2114
2115         // Reset WPA-PSK state. Only reset when supplicant enabled
2116         if (pAd->StaCfg.WpaState != SS_NOTUSE)
2117         {
2118                 pAd->StaCfg.WpaState = SS_START;
2119                 // Clear Replay counter
2120                 NdisZeroMemory(pAd->StaCfg.ReplayCounter, 8);
2121
2122 #ifdef QOS_DLS_SUPPORT
2123                 if (pAd->CommonCfg.bDLSCapable)
2124                         NdisZeroMemory(pAd->StaCfg.DlsReplayCounter, 8);
2125 #endif // QOS_DLS_SUPPORT //
2126         }
2127
2128
2129         //
2130         // if link down come from AP, we need to remove all WPA keys on WPA mode.
2131         // otherwise will cause 4-way handshaking failed, since the WPA key not empty.
2132         //
2133         if ((IsReqFromAP) && (pAd->StaCfg.AuthMode >= Ndis802_11AuthModeWPA))
2134         {
2135                 // Remove all WPA keys
2136                 RTMPWPARemoveAllKeys(pAd);
2137         }
2138
2139         // 802.1x port control
2140 #ifdef WPA_SUPPLICANT_SUPPORT
2141         // Prevent clear PortSecured here with static WEP
2142         // NetworkManger set security policy first then set SSID to connect AP.
2143         if (pAd->StaCfg.WpaSupplicantUP &&
2144                 (pAd->StaCfg.WepStatus == Ndis802_11WEPEnabled) &&
2145                 (pAd->StaCfg.IEEE8021X == FALSE))
2146         {
2147                 pAd->StaCfg.PortSecured = WPA_802_1X_PORT_SECURED;
2148         }
2149         else
2150 #endif // WPA_SUPPLICANT_SUPPORT //
2151         {
2152                 pAd->StaCfg.PortSecured = WPA_802_1X_PORT_NOT_SECURED;
2153                 pAd->StaCfg.PrivacyFilter = Ndis802_11PrivFilter8021xWEP;
2154         }
2155
2156         NdisAcquireSpinLock(&pAd->MacTabLock);
2157         pAd->MacTab.Content[BSSID_WCID].PortSecured = pAd->StaCfg.PortSecured;
2158         NdisReleaseSpinLock(&pAd->MacTabLock);
2159
2160         pAd->StaCfg.MicErrCnt = 0;
2161
2162         // Turn off Ckip control flag
2163         pAd->StaCfg.bCkipOn = FALSE;
2164         pAd->StaCfg.CCXEnable = FALSE;
2165
2166     pAd->IndicateMediaState = NdisMediaStateDisconnected;
2167         // Update extra information to link is up
2168         pAd->ExtraInfo = GENERAL_LINK_DOWN;
2169
2170     pAd->StaCfg.AdhocBOnlyJoined = FALSE;
2171         pAd->StaCfg.AdhocBGJoined = FALSE;
2172         pAd->StaCfg.Adhoc20NJoined = FALSE;
2173     pAd->StaActive.SupportedPhyInfo.bHtEnable = FALSE;
2174
2175         // Reset the Current AP's IP address
2176         NdisZeroMemory(pAd->StaCfg.AironetIPAddress, 4);
2177
2178         // Clean association information
2179         NdisZeroMemory(&pAd->StaCfg.AssocInfo, sizeof(NDIS_802_11_ASSOCIATION_INFORMATION));
2180         pAd->StaCfg.AssocInfo.Length = sizeof(NDIS_802_11_ASSOCIATION_INFORMATION);
2181         pAd->StaCfg.ReqVarIELen = 0;
2182         pAd->StaCfg.ResVarIELen = 0;
2183
2184         //
2185         // Reset RSSI value after link down
2186         //
2187         pAd->StaCfg.RssiSample.AvgRssi0 = 0;
2188         pAd->StaCfg.RssiSample.AvgRssi0X8 = 0;
2189         pAd->StaCfg.RssiSample.AvgRssi1 = 0;
2190         pAd->StaCfg.RssiSample.AvgRssi1X8 = 0;
2191         pAd->StaCfg.RssiSample.AvgRssi2 = 0;
2192         pAd->StaCfg.RssiSample.AvgRssi2X8 = 0;
2193
2194         // Restore MlmeRate
2195         pAd->CommonCfg.MlmeRate = pAd->CommonCfg.BasicMlmeRate;
2196         pAd->CommonCfg.RtsRate = pAd->CommonCfg.BasicMlmeRate;
2197
2198 #ifdef DOT11_N_SUPPORT
2199         //
2200         // After Link down, reset piggy-back setting in ASIC. Disable RDG.
2201         //
2202         if (pAd->CommonCfg.BBPCurrentBW == BW_40)
2203         {
2204                 pAd->CommonCfg.BBPCurrentBW = BW_20;
2205                 RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R4, &ByteValue);
2206                 ByteValue &= (~0x18);
2207                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R4, ByteValue);
2208         }
2209 #endif // DOT11_N_SUPPORT //
2210         // Reset DAC
2211         RTMP_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R1, &ByteValue);
2212         ByteValue &= (~0x18);
2213         if (pAd->Antenna.field.TxPath == 2)
2214         {
2215                 ByteValue |= 0x10;
2216         }
2217         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R1, ByteValue);
2218
2219         RTMPSetPiggyBack(pAd,FALSE);
2220         OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_PIGGYBACK_INUSED);
2221
2222 #ifdef DOT11_N_SUPPORT
2223         pAd->CommonCfg.BACapability.word = pAd->CommonCfg.REGBACapability.word;
2224 #endif // DOT11_N_SUPPORT //
2225
2226         // Restore all settings in the following.
2227         AsicUpdateProtect(pAd, 0, (ALLN_SETPROTECT|CCKSETPROTECT|OFDMSETPROTECT), TRUE, FALSE);
2228         AsicDisableRDG(pAd);
2229         pAd->CommonCfg.IOTestParm.bCurrentAtheros = FALSE;
2230         pAd->CommonCfg.IOTestParm.bNowAtherosBurstOn = FALSE;
2231
2232 #ifdef DOT11_N_SUPPORT
2233 #ifdef DOT11N_DRAFT3
2234         OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_SCAN_2040);
2235         pAd->CommonCfg.BSSCoexist2040.word = 0;
2236         TriEventInit(pAd);
2237         for (i = 0; i < (pAd->ChannelListNum - 1); i++)
2238         {
2239                 pAd->ChannelList[i].bEffectedChannel = FALSE;
2240         }
2241 #endif // DOT11N_DRAFT3 //
2242 #endif // DOT11_N_SUPPORT //
2243
2244         RTMP_IO_WRITE32(pAd, MAX_LEN_CFG, 0x1fff);
2245         RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS);
2246
2247         // Allow go to sleep after linkdown steps.
2248         RTMP_SET_PSFLAG(pAd, fRTMP_PS_CAN_GO_SLEEP);
2249
2250 #ifdef WPA_SUPPLICANT_SUPPORT
2251 #ifndef NATIVE_WPA_SUPPLICANT_SUPPORT
2252         if (pAd->StaCfg.WpaSupplicantUP) {
2253                 union iwreq_data    wrqu;
2254                 //send disassociate event to wpa_supplicant
2255                 memset(&wrqu, 0, sizeof(wrqu));
2256                 wrqu.data.flags = RT_DISASSOC_EVENT_FLAG;
2257                 wireless_send_event(pAd->net_dev, IWEVCUSTOM, &wrqu, NULL);
2258         }
2259 #endif // NATIVE_WPA_SUPPLICANT_SUPPORT //
2260 #endif // WPA_SUPPLICANT_SUPPORT //
2261
2262 #ifdef NATIVE_WPA_SUPPLICANT_SUPPORT
2263         {
2264                 union iwreq_data    wrqu;
2265                 memset(wrqu.ap_addr.sa_data, 0, MAC_ADDR_LEN);
2266                 wireless_send_event(pAd->net_dev, SIOCGIWAP, &wrqu, NULL);
2267         }
2268 #endif // NATIVE_WPA_SUPPLICANT_SUPPORT //
2269 }
2270
2271 /*
2272         ==========================================================================
2273         Description:
2274
2275         IRQL = DISPATCH_LEVEL
2276
2277         ==========================================================================
2278 */
2279 VOID IterateOnBssTab(
2280         IN PRTMP_ADAPTER pAd)
2281 {
2282         MLME_START_REQ_STRUCT   StartReq;
2283         MLME_JOIN_REQ_STRUCT    JoinReq;
2284         ULONG                   BssIdx;
2285
2286         // Change the wepstatus to original wepstatus
2287         pAd->StaCfg.WepStatus   = pAd->StaCfg.OrigWepStatus;
2288         pAd->StaCfg.PairCipher  = pAd->StaCfg.OrigWepStatus;
2289         pAd->StaCfg.GroupCipher = pAd->StaCfg.OrigWepStatus;
2290
2291         BssIdx = pAd->MlmeAux.BssIdx;
2292         if (BssIdx < pAd->MlmeAux.SsidBssTab.BssNr)
2293         {
2294                 // Check cipher suite, AP must have more secured cipher than station setting
2295                 // Set the Pairwise and Group cipher to match the intended AP setting
2296                 // We can only connect to AP with less secured cipher setting
2297                 if ((pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA) || (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPAPSK))
2298                 {
2299                         pAd->StaCfg.GroupCipher = pAd->MlmeAux.SsidBssTab.BssEntry[BssIdx].WPA.GroupCipher;
2300
2301                         if (pAd->StaCfg.WepStatus == pAd->MlmeAux.SsidBssTab.BssEntry[BssIdx].WPA.PairCipher)
2302                                 pAd->StaCfg.PairCipher = pAd->MlmeAux.SsidBssTab.BssEntry[BssIdx].WPA.PairCipher;
2303                         else if (pAd->MlmeAux.SsidBssTab.BssEntry[BssIdx].WPA.PairCipherAux != Ndis802_11WEPDisabled)
2304                                 pAd->StaCfg.PairCipher = pAd->MlmeAux.SsidBssTab.BssEntry[BssIdx].WPA.PairCipherAux;
2305                         else    // There is no PairCipher Aux, downgrade our capability to TKIP
2306                                 pAd->StaCfg.PairCipher = Ndis802_11Encryption2Enabled;
2307                 }
2308                 else if ((pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA2) || (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA2PSK))
2309                 {
2310                         pAd->StaCfg.GroupCipher = pAd->MlmeAux.SsidBssTab.BssEntry[BssIdx].WPA2.GroupCipher;
2311
2312                         if (pAd->StaCfg.WepStatus == pAd->MlmeAux.SsidBssTab.BssEntry[BssIdx].WPA2.PairCipher)
2313                                 pAd->StaCfg.PairCipher = pAd->MlmeAux.SsidBssTab.BssEntry[BssIdx].WPA2.PairCipher;
2314                         else if (pAd->MlmeAux.SsidBssTab.BssEntry[BssIdx].WPA2.PairCipherAux != Ndis802_11WEPDisabled)
2315                                 pAd->StaCfg.PairCipher = pAd->MlmeAux.SsidBssTab.BssEntry[BssIdx].WPA2.PairCipherAux;
2316                         else    // There is no PairCipher Aux, downgrade our capability to TKIP
2317                                 pAd->StaCfg.PairCipher = Ndis802_11Encryption2Enabled;
2318
2319                         // RSN capability
2320                         pAd->StaCfg.RsnCapability = pAd->MlmeAux.SsidBssTab.BssEntry[BssIdx].WPA2.RsnCapability;
2321                 }
2322
2323                 // Set Mix cipher flag
2324                 pAd->StaCfg.bMixCipher = (pAd->StaCfg.PairCipher == pAd->StaCfg.GroupCipher) ? FALSE : TRUE;
2325                 if (pAd->StaCfg.bMixCipher == TRUE)
2326                 {
2327                         // If mix cipher, re-build RSNIE
2328                         RTMPMakeRSNIE(pAd, pAd->StaCfg.AuthMode, pAd->StaCfg.WepStatus, 0);
2329                 }
2330
2331                 DBGPRINT(RT_DEBUG_TRACE, ("CNTL - iterate BSS %ld of %d\n", BssIdx, pAd->MlmeAux.SsidBssTab.BssNr));
2332                 JoinParmFill(pAd, &JoinReq, BssIdx);
2333                 MlmeEnqueue(pAd, SYNC_STATE_MACHINE, MT2_MLME_JOIN_REQ, sizeof(MLME_JOIN_REQ_STRUCT),
2334                                         &JoinReq);
2335                 pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_JOIN;
2336         }
2337         else if (pAd->StaCfg.BssType == BSS_ADHOC)
2338         {
2339                 DBGPRINT(RT_DEBUG_TRACE, ("CNTL - All BSS fail; start a new ADHOC (Ssid=%s)...\n",pAd->MlmeAux.Ssid));
2340                 StartParmFill(pAd, &StartReq, pAd->MlmeAux.Ssid, pAd->MlmeAux.SsidLen);
2341                 MlmeEnqueue(pAd, SYNC_STATE_MACHINE, MT2_MLME_START_REQ, sizeof(MLME_START_REQ_STRUCT), &StartReq);
2342                 pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_START;
2343         }
2344         else // no more BSS
2345         {
2346                 DBGPRINT(RT_DEBUG_TRACE, ("CNTL - All roaming failed, stay @ ch #%d\n", pAd->CommonCfg.Channel));
2347                 AsicSwitchChannel(pAd, pAd->CommonCfg.Channel, FALSE);
2348                 AsicLockChannel(pAd, pAd->CommonCfg.Channel);
2349                 pAd->Mlme.CntlMachine.CurrState = CNTL_IDLE;
2350         }
2351 }
2352
2353 // for re-association only
2354 // IRQL = DISPATCH_LEVEL
2355 VOID IterateOnBssTab2(
2356         IN PRTMP_ADAPTER pAd)
2357 {
2358         MLME_REASSOC_REQ_STRUCT ReassocReq;
2359         ULONG                   BssIdx;
2360         BSS_ENTRY               *pBss;
2361
2362         BssIdx = pAd->MlmeAux.RoamIdx;
2363         pBss = &pAd->MlmeAux.RoamTab.BssEntry[BssIdx];
2364
2365         if (BssIdx < pAd->MlmeAux.RoamTab.BssNr)
2366         {
2367                 DBGPRINT(RT_DEBUG_TRACE, ("CNTL - iterate BSS %ld of %d\n", BssIdx, pAd->MlmeAux.RoamTab.BssNr));
2368
2369                 AsicSwitchChannel(pAd, pBss->Channel, FALSE);
2370                 AsicLockChannel(pAd, pBss->Channel);
2371
2372                 // reassociate message has the same structure as associate message
2373                 AssocParmFill(pAd, &ReassocReq, pBss->Bssid, pBss->CapabilityInfo,
2374                                           ASSOC_TIMEOUT, pAd->StaCfg.DefaultListenCount);
2375                 MlmeEnqueue(pAd, ASSOC_STATE_MACHINE, MT2_MLME_REASSOC_REQ,
2376                                         sizeof(MLME_REASSOC_REQ_STRUCT), &ReassocReq);
2377
2378                 pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_REASSOC;
2379         }
2380         else // no more BSS
2381         {
2382                 DBGPRINT(RT_DEBUG_TRACE, ("CNTL - All fast roaming failed, back to ch #%d\n",pAd->CommonCfg.Channel));
2383                 AsicSwitchChannel(pAd, pAd->CommonCfg.Channel, FALSE);
2384                 AsicLockChannel(pAd, pAd->CommonCfg.Channel);
2385                 pAd->Mlme.CntlMachine.CurrState = CNTL_IDLE;
2386         }
2387 }
2388
2389 /*
2390         ==========================================================================
2391         Description:
2392
2393         IRQL = DISPATCH_LEVEL
2394
2395         ==========================================================================
2396 */
2397 VOID JoinParmFill(
2398         IN PRTMP_ADAPTER pAd,
2399         IN OUT MLME_JOIN_REQ_STRUCT *JoinReq,
2400         IN ULONG BssIdx)
2401 {
2402         JoinReq->BssIdx = BssIdx;
2403 }
2404
2405 /*
2406         ==========================================================================
2407         Description:
2408
2409         IRQL = DISPATCH_LEVEL
2410
2411         ==========================================================================
2412 */
2413 VOID ScanParmFill(
2414         IN PRTMP_ADAPTER pAd,
2415         IN OUT MLME_SCAN_REQ_STRUCT *ScanReq,
2416         IN CHAR Ssid[],
2417         IN UCHAR SsidLen,
2418         IN UCHAR BssType,
2419         IN UCHAR ScanType)
2420 {
2421     NdisZeroMemory(ScanReq->Ssid, MAX_LEN_OF_SSID);
2422         ScanReq->SsidLen = SsidLen;
2423         NdisMoveMemory(ScanReq->Ssid, Ssid, SsidLen);
2424         ScanReq->BssType = BssType;
2425         ScanReq->ScanType = ScanType;
2426 }
2427
2428 #ifdef QOS_DLS_SUPPORT
2429 /*
2430         ==========================================================================
2431         Description:
2432
2433         IRQL = DISPATCH_LEVEL
2434
2435         ==========================================================================
2436 */
2437 VOID DlsParmFill(
2438         IN PRTMP_ADAPTER pAd,
2439         IN OUT MLME_DLS_REQ_STRUCT *pDlsReq,
2440         IN PRT_802_11_DLS pDls,
2441         IN USHORT reason)
2442 {
2443         pDlsReq->pDLS = pDls;
2444         pDlsReq->Reason = reason;
2445 }
2446 #endif // QOS_DLS_SUPPORT //
2447
2448 /*
2449         ==========================================================================
2450         Description:
2451
2452         IRQL = DISPATCH_LEVEL
2453
2454         ==========================================================================
2455 */
2456 VOID StartParmFill(
2457         IN PRTMP_ADAPTER pAd,
2458         IN OUT MLME_START_REQ_STRUCT *StartReq,
2459         IN CHAR Ssid[],
2460         IN UCHAR SsidLen)
2461 {
2462         ASSERT(SsidLen <= MAX_LEN_OF_SSID);
2463         NdisMoveMemory(StartReq->Ssid, Ssid, SsidLen);
2464         StartReq->SsidLen = SsidLen;
2465 }
2466
2467 /*
2468         ==========================================================================
2469         Description:
2470
2471         IRQL = DISPATCH_LEVEL
2472
2473         ==========================================================================
2474 */
2475 VOID AuthParmFill(
2476         IN PRTMP_ADAPTER pAd,
2477         IN OUT MLME_AUTH_REQ_STRUCT *AuthReq,
2478         IN PUCHAR pAddr,
2479         IN USHORT Alg)
2480 {
2481         COPY_MAC_ADDR(AuthReq->Addr, pAddr);
2482         AuthReq->Alg = Alg;
2483         AuthReq->Timeout = AUTH_TIMEOUT;
2484 }
2485
2486 /*
2487         ==========================================================================
2488         Description:
2489
2490         IRQL = DISPATCH_LEVEL
2491
2492         ==========================================================================
2493  */
2494 VOID ComposePsPoll(
2495         IN PRTMP_ADAPTER pAd)
2496 {
2497         NdisZeroMemory(&pAd->PsPollFrame, sizeof(PSPOLL_FRAME));
2498         pAd->PsPollFrame.FC.Type = BTYPE_CNTL;
2499         pAd->PsPollFrame.FC.SubType = SUBTYPE_PS_POLL;
2500         pAd->PsPollFrame.Aid = pAd->StaActive.Aid | 0xC000;
2501         COPY_MAC_ADDR(pAd->PsPollFrame.Bssid, pAd->CommonCfg.Bssid);
2502         COPY_MAC_ADDR(pAd->PsPollFrame.Ta, pAd->CurrentAddress);
2503 }
2504
2505 // IRQL = DISPATCH_LEVEL
2506 VOID ComposeNullFrame(
2507         IN PRTMP_ADAPTER pAd)
2508 {
2509         NdisZeroMemory(&pAd->NullFrame, sizeof(HEADER_802_11));
2510         pAd->NullFrame.FC.Type = BTYPE_DATA;
2511         pAd->NullFrame.FC.SubType = SUBTYPE_NULL_FUNC;
2512         pAd->NullFrame.FC.ToDs = 1;
2513         COPY_MAC_ADDR(pAd->NullFrame.Addr1, pAd->CommonCfg.Bssid);
2514         COPY_MAC_ADDR(pAd->NullFrame.Addr2, pAd->CurrentAddress);
2515         COPY_MAC_ADDR(pAd->NullFrame.Addr3, pAd->CommonCfg.Bssid);
2516 }
2517
2518
2519
2520
2521 /*
2522         ==========================================================================
2523         Description:
2524                 Pre-build a BEACON frame in the shared memory
2525
2526         IRQL = PASSIVE_LEVEL
2527         IRQL = DISPATCH_LEVEL
2528
2529         ==========================================================================
2530 */
2531 ULONG MakeIbssBeacon(
2532         IN PRTMP_ADAPTER pAd)
2533 {
2534         UCHAR         DsLen = 1, IbssLen = 2;
2535         UCHAR         LocalErpIe[3] = {IE_ERP, 1, 0x04};
2536         HEADER_802_11 BcnHdr;
2537         USHORT        CapabilityInfo;
2538         LARGE_INTEGER FakeTimestamp;
2539         ULONG         FrameLen = 0;
2540         PTXWI_STRUC       pTxWI = &pAd->BeaconTxWI;
2541         CHAR         *pBeaconFrame = pAd->BeaconBuf;
2542         BOOLEAN       Privacy;
2543         UCHAR         SupRate[MAX_LEN_OF_SUPPORTED_RATES];
2544         UCHAR         SupRateLen = 0;
2545         UCHAR         ExtRate[MAX_LEN_OF_SUPPORTED_RATES];
2546         UCHAR         ExtRateLen = 0;
2547         UCHAR         RSNIe = IE_WPA;
2548
2549         if ((pAd->CommonCfg.PhyMode == PHY_11B) && (pAd->CommonCfg.Channel <= 14))
2550         {
2551                 SupRate[0] = 0x82; // 1 mbps
2552                 SupRate[1] = 0x84; // 2 mbps
2553                 SupRate[2] = 0x8b; // 5.5 mbps
2554                 SupRate[3] = 0x96; // 11 mbps
2555                 SupRateLen = 4;
2556                 ExtRateLen = 0;
2557         }
2558         else if (pAd->CommonCfg.Channel > 14)
2559         {
2560                 SupRate[0]  = 0x8C;    // 6 mbps, in units of 0.5 Mbps, basic rate
2561                 SupRate[1]  = 0x12;    // 9 mbps, in units of 0.5 Mbps
2562                 SupRate[2]  = 0x98;    // 12 mbps, in units of 0.5 Mbps, basic rate
2563                 SupRate[3]  = 0x24;    // 18 mbps, in units of 0.5 Mbps
2564                 SupRate[4]  = 0xb0;    // 24 mbps, in units of 0.5 Mbps, basic rate
2565                 SupRate[5]  = 0x48;    // 36 mbps, in units of 0.5 Mbps
2566                 SupRate[6]  = 0x60;    // 48 mbps, in units of 0.5 Mbps
2567                 SupRate[7]  = 0x6c;    // 54 mbps, in units of 0.5 Mbps
2568                 SupRateLen  = 8;
2569                 ExtRateLen  = 0;
2570
2571                 //
2572                 // Also Update MlmeRate & RtsRate for G only & A only
2573                 //
2574                 pAd->CommonCfg.MlmeRate = RATE_6;
2575                 pAd->CommonCfg.RtsRate = RATE_6;
2576                 pAd->CommonCfg.MlmeTransmit.field.MODE = MODE_OFDM;
2577                 pAd->CommonCfg.MlmeTransmit.field.MCS = OfdmRateToRxwiMCS[pAd->CommonCfg.MlmeRate];
2578                 pAd->MacTab.Content[BSS0Mcast_WCID].HTPhyMode.field.MODE = MODE_OFDM;
2579                 pAd->MacTab.Content[BSS0Mcast_WCID].HTPhyMode.field.MCS = OfdmRateToRxwiMCS[pAd->CommonCfg.MlmeRate];
2580         }
2581         else
2582         {
2583                 SupRate[0] = 0x82; // 1 mbps
2584                 SupRate[1] = 0x84; // 2 mbps
2585                 SupRate[2] = 0x8b; // 5.5 mbps
2586                 SupRate[3] = 0x96; // 11 mbps
2587                 SupRateLen = 4;
2588
2589                 ExtRate[0]  = 0x0C;    // 6 mbps, in units of 0.5 Mbps,
2590                 ExtRate[1]  = 0x12;    // 9 mbps, in units of 0.5 Mbps
2591                 ExtRate[2]  = 0x18;    // 12 mbps, in units of 0.5 Mbps,
2592                 ExtRate[3]  = 0x24;    // 18 mbps, in units of 0.5 Mbps
2593                 ExtRate[4]  = 0x30;    // 24 mbps, in units of 0.5 Mbps,
2594                 ExtRate[5]  = 0x48;    // 36 mbps, in units of 0.5 Mbps
2595                 ExtRate[6]  = 0x60;    // 48 mbps, in units of 0.5 Mbps
2596                 ExtRate[7]  = 0x6c;    // 54 mbps, in units of 0.5 Mbps
2597                 ExtRateLen  = 8;
2598         }
2599
2600         pAd->StaActive.SupRateLen = SupRateLen;
2601         NdisMoveMemory(pAd->StaActive.SupRate, SupRate, SupRateLen);
2602         pAd->StaActive.ExtRateLen = ExtRateLen;
2603         NdisMoveMemory(pAd->StaActive.ExtRate, ExtRate, ExtRateLen);
2604
2605         // compose IBSS beacon frame
2606         MgtMacHeaderInit(pAd, &BcnHdr, SUBTYPE_BEACON, 0, BROADCAST_ADDR, pAd->CommonCfg.Bssid);
2607         Privacy = (pAd->StaCfg.WepStatus == Ndis802_11Encryption1Enabled) ||
2608                           (pAd->StaCfg.WepStatus == Ndis802_11Encryption2Enabled) ||
2609                           (pAd->StaCfg.WepStatus == Ndis802_11Encryption3Enabled);
2610         CapabilityInfo = CAP_GENERATE(0, 1, Privacy, (pAd->CommonCfg.TxPreamble == Rt802_11PreambleShort), 0, 0);
2611
2612         MakeOutgoingFrame(pBeaconFrame,                &FrameLen,
2613                                           sizeof(HEADER_802_11),           &BcnHdr,
2614                                           TIMESTAMP_LEN,                   &FakeTimestamp,
2615                                           2,                               &pAd->CommonCfg.BeaconPeriod,
2616                                           2,                               &CapabilityInfo,
2617                                           1,                               &SsidIe,
2618                                           1,                               &pAd->CommonCfg.SsidLen,
2619                                           pAd->CommonCfg.SsidLen,          pAd->CommonCfg.Ssid,
2620                                           1,                               &SupRateIe,
2621                                           1,                               &SupRateLen,
2622                                           SupRateLen,                      SupRate,
2623                                           1,                               &DsIe,
2624                                           1,                               &DsLen,
2625                                           1,                               &pAd->CommonCfg.Channel,
2626                                           1,                               &IbssIe,
2627                                           1,                               &IbssLen,
2628                                           2,                               &pAd->StaActive.AtimWin,
2629                                           END_OF_ARGS);
2630
2631         // add ERP_IE and EXT_RAE IE of in 802.11g
2632         if (ExtRateLen)
2633         {
2634                 ULONG   tmp;
2635
2636                 MakeOutgoingFrame(pBeaconFrame + FrameLen,         &tmp,
2637                                                   3,                               LocalErpIe,
2638                                                   1,                               &ExtRateIe,
2639                                                   1,                               &ExtRateLen,
2640                                                   ExtRateLen,                      ExtRate,
2641                                                   END_OF_ARGS);
2642                 FrameLen += tmp;
2643         }
2644
2645         // If adhoc secruity is set for WPA-None, append the cipher suite IE
2646         if (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPANone)
2647         {
2648                 ULONG tmp;
2649         RTMPMakeRSNIE(pAd, pAd->StaCfg.AuthMode, pAd->StaCfg.WepStatus, BSS0);
2650
2651                 MakeOutgoingFrame(pBeaconFrame + FrameLen,              &tmp,
2652                                                   1,                                    &RSNIe,
2653                                                   1,                                    &pAd->StaCfg.RSNIE_Len,
2654                                                   pAd->StaCfg.RSNIE_Len,                pAd->StaCfg.RSN_IE,
2655                                                   END_OF_ARGS);
2656                 FrameLen += tmp;
2657         }
2658
2659 #ifdef DOT11_N_SUPPORT
2660         if ((pAd->CommonCfg.PhyMode >= PHY_11ABGN_MIXED))
2661         {
2662                 ULONG TmpLen;
2663                 UCHAR HtLen, HtLen1;
2664
2665 #ifdef RT_BIG_ENDIAN
2666                 HT_CAPABILITY_IE HtCapabilityTmp;
2667                 ADD_HT_INFO_IE  addHTInfoTmp;
2668                 USHORT  b2lTmp, b2lTmp2;
2669 #endif
2670
2671                 // add HT Capability IE
2672                 HtLen = sizeof(pAd->CommonCfg.HtCapability);
2673                 HtLen1 = sizeof(pAd->CommonCfg.AddHTInfo);
2674 #ifndef RT_BIG_ENDIAN
2675                 MakeOutgoingFrame(pBeaconFrame+FrameLen,        &TmpLen,
2676                                                   1,                                            &HtCapIe,
2677                                                   1,                                            &HtLen,
2678                                                   HtLen,                                        &pAd->CommonCfg.HtCapability,
2679                                                   1,                                            &AddHtInfoIe,
2680                                                   1,                                            &HtLen1,
2681                                                   HtLen1,                                       &pAd->CommonCfg.AddHTInfo,
2682                                                   END_OF_ARGS);
2683 #else
2684                 NdisMoveMemory(&HtCapabilityTmp, &pAd->CommonCfg.HtCapability, HtLen);
2685                 *(USHORT *)(&HtCapabilityTmp.HtCapInfo) = SWAP16(*(USHORT *)(&HtCapabilityTmp.HtCapInfo));
2686                 *(USHORT *)(&HtCapabilityTmp.ExtHtCapInfo) = SWAP16(*(USHORT *)(&HtCapabilityTmp.ExtHtCapInfo));
2687
2688                 NdisMoveMemory(&addHTInfoTmp, &pAd->CommonCfg.AddHTInfo, HtLen1);
2689                 *(USHORT *)(&addHTInfoTmp.AddHtInfo2) = SWAP16(*(USHORT *)(&addHTInfoTmp.AddHtInfo2));
2690                 *(USHORT *)(&addHTInfoTmp.AddHtInfo3) = SWAP16(*(USHORT *)(&addHTInfoTmp.AddHtInfo3));
2691
2692                 MakeOutgoingFrame(pBeaconFrame+FrameLen,        &TmpLen,
2693                                                   1,                                            &HtCapIe,
2694                                                   1,                                            &HtLen,
2695                                                   HtLen,                                        &HtCapabilityTmp,
2696                                                   1,                                            &AddHtInfoIe,
2697                                                   1,                                            &HtLen1,
2698                                                   HtLen1,                                       &addHTInfoTmp,
2699                                                   END_OF_ARGS);
2700 #endif
2701                 FrameLen += TmpLen;
2702         }
2703 #endif // DOT11_N_SUPPORT //
2704
2705         //beacon use reserved WCID 0xff
2706     if (pAd->CommonCfg.Channel > 14)
2707     {
2708         RTMPWriteTxWI(pAd, pTxWI, FALSE, FALSE,  TRUE, FALSE, FALSE, TRUE, 0, 0xff, FrameLen,
2709                 PID_MGMT, PID_BEACON, RATE_1, IFS_HTTXOP, FALSE, &pAd->CommonCfg.MlmeTransmit);
2710     }
2711     else
2712     {
2713         // Set to use 1Mbps for Adhoc beacon.
2714                 HTTRANSMIT_SETTING Transmit;
2715         Transmit.word = 0;
2716         RTMPWriteTxWI(pAd, pTxWI, FALSE, FALSE,  TRUE, FALSE, FALSE, TRUE, 0, 0xff, FrameLen,
2717                 PID_MGMT, PID_BEACON, RATE_1, IFS_HTTXOP, FALSE, &Transmit);
2718     }
2719
2720 #ifdef RT_BIG_ENDIAN
2721         RTMPFrameEndianChange(pAd, pBeaconFrame, DIR_WRITE, FALSE);
2722         RTMPWIEndianChange((PUCHAR)pTxWI, TYPE_TXWI);
2723 #endif
2724
2725     DBGPRINT(RT_DEBUG_TRACE, ("MakeIbssBeacon (len=%ld), SupRateLen=%d, ExtRateLen=%d, Channel=%d, PhyMode=%d\n",
2726                                         FrameLen, SupRateLen, ExtRateLen, pAd->CommonCfg.Channel, pAd->CommonCfg.PhyMode));
2727         return FrameLen;
2728 }
2729
2730