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