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