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