nl80211: check netlink protocol in socket release notification
[pandora-kernel.git] / drivers / staging / vt6656 / wcmd.c
1 /*
2  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * File: wcmd.c
20  *
21  * Purpose: Handles the management command interface functions
22  *
23  * Author: Lyndon Chen
24  *
25  * Date: May 8, 2003
26  *
27  * Functions:
28  *      s_vProbeChannel - Active scan channel
29  *      s_MgrMakeProbeRequest - Make ProbeRequest packet
30  *      CommandTimer - Timer function to handle command
31  *      s_bCommandComplete - Command Complete function
32  *      bScheduleCommand - Push Command and wait Command Scheduler to do
33  *      vCommandTimer- Command call back functions
34  *      vCommandTimerWait- Call back timer
35  *      s_bClearBSSID_SCAN- Clear BSSID_SCAN cmd in CMD Queue
36  *
37  * Revision History:
38  *
39  */
40
41 #include "ttype.h"
42 #include "tmacro.h"
43 #include "device.h"
44 #include "mac.h"
45 #include "card.h"
46 #include "80211hdr.h"
47 #include "wcmd.h"
48 #include "wmgr.h"
49 #include "power.h"
50 #include "wctl.h"
51 #include "baseband.h"
52 #include "control.h"
53 #include "rxtx.h"
54 #include "rf.h"
55 #include "rndis.h"
56 #include "channel.h"
57 #include "iowpa.h"
58
59 /*---------------------  Static Definitions -------------------------*/
60
61
62
63
64 /*---------------------  Static Classes  ----------------------------*/
65
66 /*---------------------  Static Variables  --------------------------*/
67 static int          msglevel                =MSG_LEVEL_INFO;
68 //static int          msglevel                =MSG_LEVEL_DEBUG;
69 /*---------------------  Static Functions  --------------------------*/
70
71 static
72 void
73 s_vProbeChannel(
74      PSDevice pDevice
75     );
76
77
78 static
79 PSTxMgmtPacket
80 s_MgrMakeProbeRequest(
81      PSDevice pDevice,
82      PSMgmtObject pMgmt,
83      PBYTE pScanBSSID,
84      PWLAN_IE_SSID pSSID,
85      PWLAN_IE_SUPP_RATES pCurrRates,
86      PWLAN_IE_SUPP_RATES pCurrExtSuppRates
87     );
88
89
90 static
91 BOOL
92 s_bCommandComplete (
93     PSDevice pDevice
94     );
95
96
97 static BOOL s_bClearBSSID_SCAN(void *hDeviceContext);
98
99 /*---------------------  Export Variables  --------------------------*/
100
101 /*---------------------  Export Functions  --------------------------*/
102
103 /*
104  * Description:
105  *      Stop AdHoc beacon during scan process
106  *
107  * Parameters:
108  *  In:
109  *      pDevice     - Pointer to the adapter
110  *  Out:
111  *      none
112  *
113  * Return Value: none
114  *
115  */
116
117 static
118 void
119 vAdHocBeaconStop(PSDevice  pDevice)
120 {
121
122     PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
123     BOOL            bStop;
124
125     /*
126      * temporarily stop Beacon packet for AdHoc Server
127      * if all of the following coditions are met:
128      *  (1) STA is in AdHoc mode
129      *  (2) VT3253 is programmed as automatic Beacon Transmitting
130      *  (3) One of the following conditions is met
131      *      (3.1) AdHoc channel is in B/G band and the
132      *      current scan channel is in A band
133      *      or
134      *      (3.2) AdHoc channel is in A mode
135      */
136     bStop = FALSE;
137     if ((pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) &&
138     (pMgmt->eCurrState >= WMAC_STATE_STARTED))
139     {
140         if ((pMgmt->uIBSSChannel <=  CB_MAX_CHANNEL_24G) &&
141              (pMgmt->uScanChannel > CB_MAX_CHANNEL_24G))
142         {
143             bStop = TRUE;
144         }
145         if (pMgmt->uIBSSChannel >  CB_MAX_CHANNEL_24G)
146         {
147             bStop = TRUE;
148         }
149     }
150
151     if (bStop)
152     {
153         //PMESG(("STOP_BEACON: IBSSChannel = %u, ScanChannel = %u\n",
154         //        pMgmt->uIBSSChannel, pMgmt->uScanChannel));
155         MACvRegBitsOff(pDevice, MAC_REG_TCR, TCR_AUTOBCNTX);
156     }
157
158 } /* vAdHocBeaconStop */
159
160
161 /*
162  * Description:
163  *      Restart AdHoc beacon after scan process complete
164  *
165  * Parameters:
166  *  In:
167  *      pDevice     - Pointer to the adapter
168  *  Out:
169  *      none
170  *
171  * Return Value: none
172  *
173  */
174 static
175 void
176 vAdHocBeaconRestart(PSDevice pDevice)
177 {
178     PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
179
180     /*
181      * Restart Beacon packet for AdHoc Server
182      * if all of the following coditions are met:
183      *  (1) STA is in AdHoc mode
184      *  (2) VT3253 is programmed as automatic Beacon Transmitting
185      */
186     if ((pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) &&
187     (pMgmt->eCurrState >= WMAC_STATE_STARTED))
188     {
189         //PMESG(("RESTART_BEACON\n"));
190         MACvRegBitsOn(pDevice, MAC_REG_TCR, TCR_AUTOBCNTX);
191     }
192
193 }
194
195
196 /*+
197  *
198  * Routine Description:
199  *   Prepare and send probe request management frames.
200  *
201  *
202  * Return Value:
203  *    none.
204  *
205 -*/
206
207 static
208 void
209 s_vProbeChannel(
210      PSDevice pDevice
211     )
212 {
213                                                      //1M,   2M,   5M,   11M,  18M,  24M,  36M,  54M
214     BYTE abyCurrSuppRatesG[] = {WLAN_EID_SUPP_RATES, 8, 0x02, 0x04, 0x0B, 0x16, 0x24, 0x30, 0x48, 0x6C};
215     BYTE abyCurrExtSuppRatesG[] = {WLAN_EID_EXTSUPP_RATES, 4, 0x0C, 0x12, 0x18, 0x60};
216                                                            //6M,   9M,   12M,  48M
217     BYTE abyCurrSuppRatesA[] = {WLAN_EID_SUPP_RATES, 8, 0x0C, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C};
218     BYTE abyCurrSuppRatesB[] = {WLAN_EID_SUPP_RATES, 4, 0x02, 0x04, 0x0B, 0x16};
219     PBYTE           pbyRate;
220     PSTxMgmtPacket  pTxPacket;
221     PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
222     unsigned int            ii;
223
224
225     if (pDevice->byBBType == BB_TYPE_11A) {
226         pbyRate = &abyCurrSuppRatesA[0];
227     } else if (pDevice->byBBType == BB_TYPE_11B) {
228         pbyRate = &abyCurrSuppRatesB[0];
229     } else {
230         pbyRate = &abyCurrSuppRatesG[0];
231     }
232     // build an assocreq frame and send it
233     pTxPacket = s_MgrMakeProbeRequest
234                 (
235                   pDevice,
236                   pMgmt,
237                   pMgmt->abyScanBSSID,
238                   (PWLAN_IE_SSID)pMgmt->abyScanSSID,
239                   (PWLAN_IE_SUPP_RATES)pbyRate,
240                   (PWLAN_IE_SUPP_RATES)abyCurrExtSuppRatesG
241                 );
242
243     if (pTxPacket != NULL ){
244         for (ii = 0; ii < 1 ; ii++) {
245             if (csMgmt_xmit(pDevice, pTxPacket) != CMD_STATUS_PENDING) {
246                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Probe request sending fail.. \n");
247             }
248             else {
249                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Probe request is sending.. \n");
250             }
251         }
252     }
253
254 }
255
256
257
258
259 /*+
260  *
261  * Routine Description:
262  *  Constructs an probe request frame
263  *
264  *
265  * Return Value:
266  *    A ptr to Tx frame or NULL on allocation failue
267  *
268 -*/
269
270
271 PSTxMgmtPacket
272 s_MgrMakeProbeRequest(
273      PSDevice pDevice,
274      PSMgmtObject pMgmt,
275      PBYTE pScanBSSID,
276      PWLAN_IE_SSID pSSID,
277      PWLAN_IE_SUPP_RATES pCurrRates,
278      PWLAN_IE_SUPP_RATES pCurrExtSuppRates
279
280     )
281 {
282     PSTxMgmtPacket      pTxPacket = NULL;
283     WLAN_FR_PROBEREQ    sFrame;
284
285
286     pTxPacket = (PSTxMgmtPacket)pMgmt->pbyMgmtPacketPool;
287     memset(pTxPacket, 0, sizeof(STxMgmtPacket) + WLAN_PROBEREQ_FR_MAXLEN);
288     pTxPacket->p80211Header = (PUWLAN_80211HDR)((PBYTE)pTxPacket + sizeof(STxMgmtPacket));
289     sFrame.pBuf = (PBYTE)pTxPacket->p80211Header;
290     sFrame.len = WLAN_PROBEREQ_FR_MAXLEN;
291     vMgrEncodeProbeRequest(&sFrame);
292     sFrame.pHdr->sA3.wFrameCtl = cpu_to_le16(
293         (
294         WLAN_SET_FC_FTYPE(WLAN_TYPE_MGR) |
295         WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_PROBEREQ)
296         ));
297     memcpy( sFrame.pHdr->sA3.abyAddr1, pScanBSSID, WLAN_ADDR_LEN);
298     memcpy( sFrame.pHdr->sA3.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
299     memcpy( sFrame.pHdr->sA3.abyAddr3, pScanBSSID, WLAN_BSSID_LEN);
300     // Copy the SSID, pSSID->len=0 indicate broadcast SSID
301     sFrame.pSSID = (PWLAN_IE_SSID)(sFrame.pBuf + sFrame.len);
302     sFrame.len += pSSID->len + WLAN_IEHDR_LEN;
303     memcpy(sFrame.pSSID, pSSID, pSSID->len + WLAN_IEHDR_LEN);
304     sFrame.pSuppRates = (PWLAN_IE_SUPP_RATES)(sFrame.pBuf + sFrame.len);
305     sFrame.len += pCurrRates->len + WLAN_IEHDR_LEN;
306     memcpy(sFrame.pSuppRates, pCurrRates, pCurrRates->len + WLAN_IEHDR_LEN);
307     // Copy the extension rate set
308     if (pDevice->byBBType == BB_TYPE_11G) {
309         sFrame.pExtSuppRates = (PWLAN_IE_SUPP_RATES)(sFrame.pBuf + sFrame.len);
310         sFrame.len += pCurrExtSuppRates->len + WLAN_IEHDR_LEN;
311         memcpy(sFrame.pExtSuppRates, pCurrExtSuppRates, pCurrExtSuppRates->len + WLAN_IEHDR_LEN);
312     }
313     pTxPacket->cbMPDULen = sFrame.len;
314     pTxPacket->cbPayloadLen = sFrame.len - WLAN_HDR_ADDR3_LEN;
315
316     return pTxPacket;
317 }
318
319 void vCommandTimerWait(void *hDeviceContext, unsigned long MSecond)
320 {
321         PSDevice pDevice = (PSDevice)hDeviceContext;
322
323         init_timer(&pDevice->sTimerCommand);
324
325         pDevice->sTimerCommand.data = (unsigned long)pDevice;
326         pDevice->sTimerCommand.function = (TimerFunction)vRunCommand;
327         pDevice->sTimerCommand.expires = RUN_AT((MSecond * HZ) / 1000);
328
329         add_timer(&pDevice->sTimerCommand);
330
331         return;
332 }
333
334 void vRunCommand(void *hDeviceContext)
335 {
336     PSDevice        pDevice = (PSDevice)hDeviceContext;
337     PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
338     PWLAN_IE_SSID   pItemSSID;
339     PWLAN_IE_SSID   pItemSSIDCurr;
340     CMD_STATUS      Status;
341     unsigned int            ii;
342     BYTE            byMask[8] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80};
343     struct sk_buff  *skb;
344     BYTE            byData;
345
346
347     if (pDevice->dwDiagRefCount != 0)
348         return;
349     if (pDevice->bCmdRunning != TRUE)
350         return;
351
352     spin_lock_irq(&pDevice->lock);
353
354     switch ( pDevice->eCommandState ) {
355
356         case WLAN_CMD_SCAN_START:
357
358                 pDevice->byReAssocCount = 0;
359             if (pDevice->bRadioOff == TRUE) {
360                 s_bCommandComplete(pDevice);
361                 spin_unlock_irq(&pDevice->lock);
362                 return;
363             }
364
365             if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
366                 s_bCommandComplete(pDevice);
367                 spin_unlock_irq(&pDevice->lock);
368                 return;
369             }
370
371             pItemSSID = (PWLAN_IE_SSID)pMgmt->abyScanSSID;
372
373             if (pMgmt->uScanChannel == 0 ) {
374                 pMgmt->uScanChannel = pDevice->byMinChannel;
375             }
376             if (pMgmt->uScanChannel > pDevice->byMaxChannel) {
377                 pMgmt->eScanState = WMAC_NO_SCANNING;
378
379                 if (pDevice->byBBType != pDevice->byScanBBType) {
380                     pDevice->byBBType = pDevice->byScanBBType;
381                     CARDvSetBSSMode(pDevice);
382                 }
383
384                 if (pDevice->bUpdateBBVGA) {
385                     BBvSetShortSlotTime(pDevice);
386                     BBvSetVGAGainOffset(pDevice, pDevice->byBBVGACurrent);
387                     BBvUpdatePreEDThreshold(pDevice, FALSE);
388                 }
389                 // Set channel back
390                 vAdHocBeaconRestart(pDevice);
391                 // Set channel back
392                 CARDbSetMediaChannel(pDevice, pMgmt->uCurrChannel);
393                 // Set Filter
394                 if (pMgmt->bCurrBSSIDFilterOn) {
395                     MACvRegBitsOn(pDevice, MAC_REG_RCR, RCR_BSSID);
396                     pDevice->byRxMode |= RCR_BSSID;
397                 }
398                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Scanning, set back to channel: [%d]\n", pMgmt->uCurrChannel);
399                 pDevice->bStopDataPkt = FALSE;
400                 s_bCommandComplete(pDevice);
401                 spin_unlock_irq(&pDevice->lock);
402                 return;
403
404             } else {
405                 if (!ChannelValid(pDevice->byZoneType, pMgmt->uScanChannel)) {
406                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Invalid channel pMgmt->uScanChannel = %d \n",pMgmt->uScanChannel);
407                     s_bCommandComplete(pDevice);
408                     spin_unlock_irq(&pDevice->lock);
409                     return;
410                 }
411                 if (pMgmt->uScanChannel == pDevice->byMinChannel) {
412                    // pMgmt->eScanType = WMAC_SCAN_ACTIVE;          //mike mark
413                     pMgmt->abyScanBSSID[0] = 0xFF;
414                     pMgmt->abyScanBSSID[1] = 0xFF;
415                     pMgmt->abyScanBSSID[2] = 0xFF;
416                     pMgmt->abyScanBSSID[3] = 0xFF;
417                     pMgmt->abyScanBSSID[4] = 0xFF;
418                     pMgmt->abyScanBSSID[5] = 0xFF;
419                     pItemSSID->byElementID = WLAN_EID_SSID;
420                     // clear bssid list
421                     /* BSSvClearBSSList((void *) pDevice,
422                        pDevice->bLinkPass); */
423                     pMgmt->eScanState = WMAC_IS_SCANNING;
424                     pDevice->byScanBBType = pDevice->byBBType;  //lucas
425                     pDevice->bStopDataPkt = TRUE;
426                     // Turn off RCR_BSSID filter every time
427                     MACvRegBitsOff(pDevice, MAC_REG_RCR, RCR_BSSID);
428                     pDevice->byRxMode &= ~RCR_BSSID;
429
430                 }
431                 //lucas
432                 vAdHocBeaconStop(pDevice);
433                 if ((pDevice->byBBType != BB_TYPE_11A) && (pMgmt->uScanChannel > CB_MAX_CHANNEL_24G)) {
434                     pDevice->byBBType = BB_TYPE_11A;
435                     CARDvSetBSSMode(pDevice);
436                 }
437                 else if ((pDevice->byBBType == BB_TYPE_11A) && (pMgmt->uScanChannel <= CB_MAX_CHANNEL_24G)) {
438                     pDevice->byBBType = BB_TYPE_11G;
439                     CARDvSetBSSMode(pDevice);
440                 }
441                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Scanning....  channel: [%d]\n", pMgmt->uScanChannel);
442                 // Set channel
443                 CARDbSetMediaChannel(pDevice, pMgmt->uScanChannel);
444                 // Set Baseband to be more sensitive.
445
446                 if (pDevice->bUpdateBBVGA) {
447                     BBvSetShortSlotTime(pDevice);
448                     BBvSetVGAGainOffset(pDevice, pDevice->abyBBVGA[0]);
449                     BBvUpdatePreEDThreshold(pDevice, TRUE);
450                 }
451                 pMgmt->uScanChannel++;
452
453                 while (!ChannelValid(pDevice->byZoneType, pMgmt->uScanChannel) &&
454                         pMgmt->uScanChannel <= pDevice->byMaxChannel ){
455                     pMgmt->uScanChannel++;
456                 }
457
458                 if (pMgmt->uScanChannel > pDevice->byMaxChannel) {
459                     // Set Baseband to be not sensitive and rescan
460                     pDevice->eCommandState = WLAN_CMD_SCAN_END;
461
462                 }
463                 if ((pMgmt->b11hEnable == FALSE) ||
464                     (pMgmt->uScanChannel < CB_MAX_CHANNEL_24G)) {
465                     s_vProbeChannel(pDevice);
466                     spin_unlock_irq(&pDevice->lock);
467                      vCommandTimerWait((void *) pDevice, 100);
468                     return;
469                 } else {
470                     spin_unlock_irq(&pDevice->lock);
471                     vCommandTimerWait((void *) pDevice, WCMD_PASSIVE_SCAN_TIME);
472                     return;
473                 }
474
475             }
476
477             break;
478
479         case WLAN_CMD_SCAN_END:
480
481             // Set Baseband's sensitivity back.
482             if (pDevice->byBBType != pDevice->byScanBBType) {
483                 pDevice->byBBType = pDevice->byScanBBType;
484                 CARDvSetBSSMode(pDevice);
485             }
486
487             if (pDevice->bUpdateBBVGA) {
488                 BBvSetShortSlotTime(pDevice);
489                 BBvSetVGAGainOffset(pDevice, pDevice->byBBVGACurrent);
490                 BBvUpdatePreEDThreshold(pDevice, FALSE);
491             }
492
493             // Set channel back
494             vAdHocBeaconRestart(pDevice);
495             // Set channel back
496             CARDbSetMediaChannel(pDevice, pMgmt->uCurrChannel);
497             // Set Filter
498             if (pMgmt->bCurrBSSIDFilterOn) {
499                 MACvRegBitsOn(pDevice, MAC_REG_RCR, RCR_BSSID);
500                 pDevice->byRxMode |= RCR_BSSID;
501             }
502             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Scanning, set back to channel: [%d]\n", pMgmt->uCurrChannel);
503             pMgmt->eScanState = WMAC_NO_SCANNING;
504             pDevice->bStopDataPkt = FALSE;
505
506 #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
507         if(pMgmt->eScanType == WMAC_SCAN_PASSIVE)
508                 {
509                         //send scan event to wpa_Supplicant
510                                 union iwreq_data wrqu;
511                                 PRINT_K("wireless_send_event--->SIOCGIWSCAN(scan done)\n");
512                                 memset(&wrqu, 0, sizeof(wrqu));
513                                 wireless_send_event(pDevice->dev, SIOCGIWSCAN, &wrqu, NULL);
514                         }
515 #endif
516             s_bCommandComplete(pDevice);
517             break;
518
519         case WLAN_CMD_DISASSOCIATE_START :
520                 pDevice->byReAssocCount = 0;
521             if ((pMgmt->eCurrMode == WMAC_MODE_ESS_STA) &&
522                 (pMgmt->eCurrState != WMAC_STATE_ASSOC)) {
523                 s_bCommandComplete(pDevice);
524                 spin_unlock_irq(&pDevice->lock);
525                 return;
526             } else {
527
528           #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
529                       pDevice->bwextstep0 = FALSE;
530                         pDevice->bwextstep1 = FALSE;
531                         pDevice->bwextstep2 = FALSE;
532                         pDevice->bwextstep3 = FALSE;
533                    pDevice->bWPASuppWextEnabled = FALSE;
534          #endif
535                    pDevice->fWPA_Authened = FALSE;
536
537                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Send Disassociation Packet..\n");
538                 // reason = 8 : disassoc because sta has left
539                 vMgrDisassocBeginSta((void *) pDevice,
540                                      pMgmt,
541                                      pMgmt->abyCurrBSSID,
542                                      (8),
543                                      &Status);
544                 pDevice->bLinkPass = FALSE;
545                 ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_SLOW);
546                 // unlock command busy
547                 pItemSSID = (PWLAN_IE_SSID)pMgmt->abyCurrSSID;
548                 pItemSSID->len = 0;
549                 memset(pItemSSID->abySSID, 0, WLAN_SSID_MAXLEN);
550                 pMgmt->eCurrState = WMAC_STATE_IDLE;
551                 pMgmt->sNodeDBTable[0].bActive = FALSE;
552 //                pDevice->bBeaconBufReady = FALSE;
553             }
554             netif_stop_queue(pDevice->dev);
555             if (pDevice->bNeedRadioOFF == TRUE)
556                 CARDbRadioPowerOff(pDevice);
557             s_bCommandComplete(pDevice);
558             break;
559
560
561         case WLAN_CMD_SSID_START:
562
563                 pDevice->byReAssocCount = 0;
564             if (pDevice->bRadioOff == TRUE) {
565                 s_bCommandComplete(pDevice);
566                 spin_unlock_irq(&pDevice->lock);
567                 return;
568             }
569
570             memcpy(pMgmt->abyAdHocSSID,pMgmt->abyDesireSSID,
571                               ((PWLAN_IE_SSID)pMgmt->abyDesireSSID)->len + WLAN_IEHDR_LEN);
572
573             pItemSSID = (PWLAN_IE_SSID)pMgmt->abyDesireSSID;
574             pItemSSIDCurr = (PWLAN_IE_SSID)pMgmt->abyCurrSSID;
575             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" cmd: desire ssid = %s\n", pItemSSID->abySSID);
576             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" cmd: curr ssid = %s\n", pItemSSIDCurr->abySSID);
577
578             if (pMgmt->eCurrState == WMAC_STATE_ASSOC) {
579                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" Cmd pMgmt->eCurrState == WMAC_STATE_ASSOC\n");
580                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" pItemSSID->len =%d\n",pItemSSID->len);
581                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" pItemSSIDCurr->len = %d\n",pItemSSIDCurr->len);
582                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" desire ssid = %s\n", pItemSSID->abySSID);
583                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" curr ssid = %s\n", pItemSSIDCurr->abySSID);
584             }
585
586             if ((pMgmt->eCurrState == WMAC_STATE_ASSOC) ||
587                 ((pMgmt->eCurrMode == WMAC_MODE_IBSS_STA)&& (pMgmt->eCurrState == WMAC_STATE_JOINTED))) {
588
589                 if (pItemSSID->len == pItemSSIDCurr->len) {
590                     if (memcmp(pItemSSID->abySSID, pItemSSIDCurr->abySSID, pItemSSID->len) == 0) {
591                         s_bCommandComplete(pDevice);
592                         spin_unlock_irq(&pDevice->lock);
593                         return;
594                     }
595                 }
596                 netif_stop_queue(pDevice->dev);
597                 pDevice->bLinkPass = FALSE;
598                 ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_SLOW);
599             }
600             // set initial state
601             pMgmt->eCurrState = WMAC_STATE_IDLE;
602             pMgmt->eCurrMode = WMAC_MODE_STANDBY;
603             PSvDisablePowerSaving((void *) pDevice);
604             BSSvClearNodeDBTable(pDevice, 0);
605             vMgrJoinBSSBegin((void *) pDevice, &Status);
606             // if Infra mode
607             if ((pMgmt->eCurrMode == WMAC_MODE_ESS_STA) && (pMgmt->eCurrState == WMAC_STATE_JOINTED)) {
608                 // Call mgr to begin the deauthentication
609                 // reason = (3) because sta has left ESS
610               if (pMgmt->eCurrState >= WMAC_STATE_AUTH) {
611                 vMgrDeAuthenBeginSta((void *)pDevice,
612                                      pMgmt,
613                                      pMgmt->abyCurrBSSID,
614                                      (3),
615                                      &Status);
616               }
617                 // Call mgr to begin the authentication
618                 vMgrAuthenBeginSta((void *) pDevice, pMgmt, &Status);
619                 if (Status == CMD_STATUS_SUCCESS) {
620                    pDevice->byLinkWaitCount = 0;
621                     pDevice->eCommandState = WLAN_AUTHENTICATE_WAIT;
622                     vCommandTimerWait((void *) pDevice, AUTHENTICATE_TIMEOUT);
623                     spin_unlock_irq(&pDevice->lock);
624                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" Set eCommandState = WLAN_AUTHENTICATE_WAIT\n");
625                     return;
626                 }
627             }
628             // if Adhoc mode
629             else if (pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) {
630                 if (pMgmt->eCurrState == WMAC_STATE_JOINTED) {
631                     if (netif_queue_stopped(pDevice->dev)){
632                         netif_wake_queue(pDevice->dev);
633                     }
634                     pDevice->bLinkPass = TRUE;
635                     ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_INTER);
636                     pMgmt->sNodeDBTable[0].bActive = TRUE;
637                     pMgmt->sNodeDBTable[0].uInActiveCount = 0;
638                 }
639                 else {
640                     // start own IBSS
641                     DBG_PRT(MSG_LEVEL_DEBUG,
642                             KERN_INFO "CreateOwn IBSS by CurrMode = IBSS_STA\n");
643                     vMgrCreateOwnIBSS((void *) pDevice, &Status);
644                     if (Status != CMD_STATUS_SUCCESS){
645                         DBG_PRT(MSG_LEVEL_DEBUG,
646                                 KERN_INFO "WLAN_CMD_IBSS_CREATE fail!\n");
647                     }
648                     BSSvAddMulticastNode(pDevice);
649                 }
650                 s_bClearBSSID_SCAN(pDevice);
651             }
652             // if SSID not found
653             else if (pMgmt->eCurrMode == WMAC_MODE_STANDBY) {
654                 if (pMgmt->eConfigMode == WMAC_CONFIG_IBSS_STA ||
655                     pMgmt->eConfigMode == WMAC_CONFIG_AUTO) {
656                     // start own IBSS
657                         DBG_PRT(MSG_LEVEL_DEBUG,
658                                 KERN_INFO "CreateOwn IBSS by CurrMode = STANDBY\n");
659                     vMgrCreateOwnIBSS((void *) pDevice, &Status);
660                     if (Status != CMD_STATUS_SUCCESS){
661                         DBG_PRT(MSG_LEVEL_DEBUG,
662                                 KERN_INFO "WLAN_CMD_IBSS_CREATE fail!\n");
663                     }
664                     BSSvAddMulticastNode(pDevice);
665                     s_bClearBSSID_SCAN(pDevice);
666 /*
667                     pDevice->bLinkPass = TRUE;
668                     ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_INTER);
669                     if (netif_queue_stopped(pDevice->dev)){
670                         netif_wake_queue(pDevice->dev);
671                     }
672                     s_bClearBSSID_SCAN(pDevice);
673 */
674                 }
675                 else {
676                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Disconnect SSID none\n");
677                      #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
678                     // if(pDevice->bWPASuppWextEnabled == TRUE)
679                         {
680                         union iwreq_data  wrqu;
681                         memset(&wrqu, 0, sizeof (wrqu));
682                           wrqu.ap_addr.sa_family = ARPHRD_ETHER;
683                         PRINT_K("wireless_send_event--->SIOCGIWAP(disassociated:vMgrJoinBSSBegin Fail !!)\n");
684                         wireless_send_event(pDevice->dev, SIOCGIWAP, &wrqu, NULL);
685                        }
686                     #endif
687                 }
688             }
689             s_bCommandComplete(pDevice);
690             break;
691
692         case WLAN_AUTHENTICATE_WAIT :
693             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"eCommandState == WLAN_AUTHENTICATE_WAIT\n");
694             if (pMgmt->eCurrState == WMAC_STATE_AUTH) {
695                 pDevice->byLinkWaitCount = 0;
696                 // Call mgr to begin the association
697                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"eCurrState == WMAC_STATE_AUTH\n");
698                 vMgrAssocBeginSta((void *) pDevice, pMgmt, &Status);
699                 if (Status == CMD_STATUS_SUCCESS) {
700                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"eCommandState = WLAN_ASSOCIATE_WAIT\n");
701                   pDevice->byLinkWaitCount = 0;
702                     pDevice->eCommandState = WLAN_ASSOCIATE_WAIT;
703                     vCommandTimerWait((void *) pDevice, ASSOCIATE_TIMEOUT);
704                     spin_unlock_irq(&pDevice->lock);
705                     return;
706                 }
707             }
708            else if(pMgmt->eCurrState < WMAC_STATE_AUTHPENDING) {
709                printk("WLAN_AUTHENTICATE_WAIT:Authen Fail???\n");
710            }
711            else  if(pDevice->byLinkWaitCount <= 4){    //mike add:wait another 2 sec if authenticated_frame delay!
712                 pDevice->byLinkWaitCount ++;
713                printk("WLAN_AUTHENTICATE_WAIT:wait %d times!!\n",pDevice->byLinkWaitCount);
714                spin_unlock_irq(&pDevice->lock);
715                vCommandTimerWait((void *) pDevice, AUTHENTICATE_TIMEOUT/2);
716                return;
717            }
718                   pDevice->byLinkWaitCount = 0;
719
720             s_bCommandComplete(pDevice);
721             break;
722
723         case WLAN_ASSOCIATE_WAIT :
724             if (pMgmt->eCurrState == WMAC_STATE_ASSOC) {
725                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"eCurrState == WMAC_STATE_ASSOC\n");
726                 if (pDevice->ePSMode != WMAC_POWER_CAM) {
727                         PSvEnablePowerSaving((void *) pDevice,
728                                              pMgmt->wListenInterval);
729                 }
730 /*
731                 if (pMgmt->eAuthenMode >= WMAC_AUTH_WPA) {
732                     KeybRemoveAllKey(pDevice, &(pDevice->sKey), pDevice->abyBSSID);
733                 }
734 */
735                 pDevice->byLinkWaitCount = 0;
736                 pDevice->byReAssocCount = 0;
737                 pDevice->bLinkPass = TRUE;
738                 ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_INTER);
739                 s_bClearBSSID_SCAN(pDevice);
740
741                 if (netif_queue_stopped(pDevice->dev)){
742                     netif_wake_queue(pDevice->dev);
743                 }
744
745                  if(pDevice->IsTxDataTrigger != FALSE)   {    //TxDataTimer is not triggered at the first time
746                      // printk("Re-initial TxDataTimer****\n");
747                     del_timer(&pDevice->sTimerTxData);
748                       init_timer(&pDevice->sTimerTxData);
749                         pDevice->sTimerTxData.data = (unsigned long) pDevice;
750                       pDevice->sTimerTxData.function = (TimerFunction)BSSvSecondTxData;
751                       pDevice->sTimerTxData.expires = RUN_AT(10*HZ);      //10s callback
752                       pDevice->fTxDataInSleep = FALSE;
753                       pDevice->nTxDataTimeCout = 0;
754                  }
755                  else {
756                    // printk("mike:-->First time triger TimerTxData InSleep\n");
757                  }
758                 pDevice->IsTxDataTrigger = TRUE;
759                 add_timer(&pDevice->sTimerTxData);
760
761             }
762            else if(pMgmt->eCurrState < WMAC_STATE_ASSOCPENDING) {
763                printk("WLAN_ASSOCIATE_WAIT:Association Fail???\n");
764            }
765            else  if(pDevice->byLinkWaitCount <= 4){    //mike add:wait another 2 sec if associated_frame delay!
766                 pDevice->byLinkWaitCount ++;
767                printk("WLAN_ASSOCIATE_WAIT:wait %d times!!\n",pDevice->byLinkWaitCount);
768                spin_unlock_irq(&pDevice->lock);
769                vCommandTimerWait((void *) pDevice, ASSOCIATE_TIMEOUT/2);
770                return;
771            }
772                   pDevice->byLinkWaitCount = 0;
773
774             s_bCommandComplete(pDevice);
775             break;
776
777         case WLAN_CMD_AP_MODE_START :
778             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"eCommandState == WLAN_CMD_AP_MODE_START\n");
779
780             if (pMgmt->eConfigMode == WMAC_CONFIG_AP) {
781                 del_timer(&pMgmt->sTimerSecondCallback);
782                 pMgmt->eCurrState = WMAC_STATE_IDLE;
783                 pMgmt->eCurrMode = WMAC_MODE_STANDBY;
784                 pDevice->bLinkPass = FALSE;
785                 ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_SLOW);
786                 if (pDevice->bEnableHostWEP == TRUE)
787                     BSSvClearNodeDBTable(pDevice, 1);
788                 else
789                     BSSvClearNodeDBTable(pDevice, 0);
790                 pDevice->uAssocCount = 0;
791                 pMgmt->eCurrState = WMAC_STATE_IDLE;
792                 pDevice->bFixRate = FALSE;
793
794                 vMgrCreateOwnIBSS((void *) pDevice, &Status);
795                 if (Status != CMD_STATUS_SUCCESS) {
796                         DBG_PRT(MSG_LEVEL_DEBUG,
797                                 KERN_INFO "vMgrCreateOwnIBSS fail!\n");
798                 }
799                 // alway turn off unicast bit
800                 MACvRegBitsOff(pDevice, MAC_REG_RCR, RCR_UNICAST);
801                 pDevice->byRxMode &= ~RCR_UNICAST;
802                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "wcmd: rx_mode = %x\n", pDevice->byRxMode );
803                 BSSvAddMulticastNode(pDevice);
804                 if (netif_queue_stopped(pDevice->dev)){
805                     netif_wake_queue(pDevice->dev);
806                 }
807                 pDevice->bLinkPass = TRUE;
808                 ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_INTER);
809                 add_timer(&pMgmt->sTimerSecondCallback);
810             }
811             s_bCommandComplete(pDevice);
812             break;
813
814         case WLAN_CMD_TX_PSPACKET_START :
815             // DTIM Multicast tx
816             if (pMgmt->sNodeDBTable[0].bRxPSPoll) {
817                 while ((skb = skb_dequeue(&pMgmt->sNodeDBTable[0].sTxPSQueue)) != NULL) {
818                     if (skb_queue_empty(&pMgmt->sNodeDBTable[0].sTxPSQueue)) {
819                         pMgmt->abyPSTxMap[0] &= ~byMask[0];
820                         pDevice->bMoreData = FALSE;
821                     }
822                     else {
823                         pDevice->bMoreData = TRUE;
824                     }
825
826                     if (nsDMA_tx_packet(pDevice, TYPE_AC0DMA, skb) != 0) {
827                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Multicast ps tx fail \n");
828                     }
829
830                     pMgmt->sNodeDBTable[0].wEnQueueCnt--;
831                 }
832             }
833
834             // PS nodes tx
835             for (ii = 1; ii < (MAX_NODE_NUM + 1); ii++) {
836                 if (pMgmt->sNodeDBTable[ii].bActive &&
837                     pMgmt->sNodeDBTable[ii].bRxPSPoll) {
838                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Index=%d Enqueu Cnt= %d\n",
839                                ii, pMgmt->sNodeDBTable[ii].wEnQueueCnt);
840                     while ((skb = skb_dequeue(&pMgmt->sNodeDBTable[ii].sTxPSQueue)) != NULL) {
841                         if (skb_queue_empty(&pMgmt->sNodeDBTable[ii].sTxPSQueue)) {
842                             // clear tx map
843                             pMgmt->abyPSTxMap[pMgmt->sNodeDBTable[ii].wAID >> 3] &=
844                                     ~byMask[pMgmt->sNodeDBTable[ii].wAID & 7];
845                             pDevice->bMoreData = FALSE;
846                         }
847                         else {
848                             pDevice->bMoreData = TRUE;
849                         }
850
851                         if (nsDMA_tx_packet(pDevice, TYPE_AC0DMA, skb) != 0) {
852                             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "sta ps tx fail \n");
853                         }
854
855                         pMgmt->sNodeDBTable[ii].wEnQueueCnt--;
856                         // check if sta ps enable, wait next pspoll
857                         // if sta ps disable, send all pending buffers.
858                         if (pMgmt->sNodeDBTable[ii].bPSEnable)
859                             break;
860                     }
861                     if (skb_queue_empty(&pMgmt->sNodeDBTable[ii].sTxPSQueue)) {
862                         // clear tx map
863                         pMgmt->abyPSTxMap[pMgmt->sNodeDBTable[ii].wAID >> 3] &=
864                                     ~byMask[pMgmt->sNodeDBTable[ii].wAID & 7];
865                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Index=%d PS queue clear \n", ii);
866                     }
867                     pMgmt->sNodeDBTable[ii].bRxPSPoll = FALSE;
868                 }
869             }
870
871             s_bCommandComplete(pDevice);
872             break;
873
874         case WLAN_CMD_RADIO_START:
875
876             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"eCommandState == WLAN_CMD_RADIO_START\n");
877        //     if (pDevice->bRadioCmd == TRUE)
878        //         CARDbRadioPowerOn(pDevice);
879        //     else
880        //         CARDbRadioPowerOff(pDevice);
881
882        {
883                int ntStatus = STATUS_SUCCESS;
884         BYTE            byTmp;
885
886         ntStatus = CONTROLnsRequestIn(pDevice,
887                                     MESSAGE_TYPE_READ,
888                                     MAC_REG_GPIOCTL1,
889                                     MESSAGE_REQUEST_MACREG,
890                                     1,
891                                     &byTmp);
892
893         if ( ntStatus != STATUS_SUCCESS ) {
894                 s_bCommandComplete(pDevice);
895                 spin_unlock_irq(&pDevice->lock);
896                 return;
897         }
898         if ( (byTmp & GPIO3_DATA) == 0 ) {
899             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" WLAN_CMD_RADIO_START_OFF........................\n");
900                 // Old commands are useless.
901                 // empty command Q
902                pDevice->cbFreeCmdQueue = CMD_Q_SIZE;
903                 pDevice->uCmdDequeueIdx = 0;
904                 pDevice->uCmdEnqueueIdx = 0;
905                 //0415pDevice->bCmdRunning = FALSE;
906                 pDevice->bCmdClear = TRUE;
907                 pDevice->bStopTx0Pkt = FALSE;
908                 pDevice->bStopDataPkt = TRUE;
909
910                 pDevice->byKeyIndex = 0;
911                 pDevice->bTransmitKey = FALSE;
912             spin_unlock_irq(&pDevice->lock);
913             KeyvInitTable(pDevice,&pDevice->sKey);
914             spin_lock_irq(&pDevice->lock);
915                pMgmt->byCSSPK = KEY_CTL_NONE;
916                 pMgmt->byCSSGK = KEY_CTL_NONE;
917
918           if (pDevice->bLinkPass == TRUE) {
919                 // reason = 8 : disassoc because sta has left
920                 vMgrDisassocBeginSta((void *) pDevice,
921                                      pMgmt,
922                                      pMgmt->abyCurrBSSID,
923                                      (8),
924                                      &Status);
925                        pDevice->bLinkPass = FALSE;
926                 // unlock command busy
927                         pMgmt->eCurrState = WMAC_STATE_IDLE;
928                         pMgmt->sNodeDBTable[0].bActive = FALSE;
929                      #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
930                     // if(pDevice->bWPASuppWextEnabled == TRUE)
931                         {
932                         union iwreq_data  wrqu;
933                         memset(&wrqu, 0, sizeof (wrqu));
934                           wrqu.ap_addr.sa_family = ARPHRD_ETHER;
935                         PRINT_K("wireless_send_event--->SIOCGIWAP(disassociated)\n");
936                         wireless_send_event(pDevice->dev, SIOCGIWAP, &wrqu, NULL);
937                        }
938                     #endif
939                 }
940                #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
941                        pDevice->bwextstep0 = FALSE;
942                         pDevice->bwextstep1 = FALSE;
943                         pDevice->bwextstep2 = FALSE;
944                         pDevice->bwextstep3 = FALSE;
945                       pDevice->bWPASuppWextEnabled = FALSE;
946                 #endif
947                           //clear current SSID
948                   pItemSSID = (PWLAN_IE_SSID)pMgmt->abyCurrSSID;
949                   pItemSSID->len = 0;
950                   memset(pItemSSID->abySSID, 0, WLAN_SSID_MAXLEN);
951                 //clear dessire SSID
952                 pItemSSID = (PWLAN_IE_SSID)pMgmt->abyDesireSSID;
953                 pItemSSID->len = 0;
954                 memset(pItemSSID->abySSID, 0, WLAN_SSID_MAXLEN);
955
956             netif_stop_queue(pDevice->dev);
957             CARDbRadioPowerOff(pDevice);
958              MACvRegBitsOn(pDevice,MAC_REG_GPIOCTL1,GPIO3_INTMD);
959             ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_OFF);
960             pDevice->bHWRadioOff = TRUE;
961         } else {
962             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" WLAN_CMD_RADIO_START_ON........................\n");
963             pDevice->bHWRadioOff = FALSE;
964                 CARDbRadioPowerOn(pDevice);
965             MACvRegBitsOff(pDevice,MAC_REG_GPIOCTL1,GPIO3_INTMD);
966             ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_ON);
967         }
968       }
969
970             s_bCommandComplete(pDevice);
971             break;
972
973
974         case WLAN_CMD_CHANGE_BBSENSITIVITY_START:
975
976             pDevice->bStopDataPkt = TRUE;
977             pDevice->byBBVGACurrent = pDevice->byBBVGANew;
978             BBvSetVGAGainOffset(pDevice, pDevice->byBBVGACurrent);
979             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Change sensitivity pDevice->byBBVGACurrent = %x\n", pDevice->byBBVGACurrent);
980             pDevice->bStopDataPkt = FALSE;
981             s_bCommandComplete(pDevice);
982             break;
983
984         case WLAN_CMD_TBTT_WAKEUP_START:
985             PSbIsNextTBTTWakeUp(pDevice);
986             s_bCommandComplete(pDevice);
987             break;
988
989         case WLAN_CMD_BECON_SEND_START:
990             bMgrPrepareBeaconToSend(pDevice, pMgmt);
991             s_bCommandComplete(pDevice);
992             break;
993
994         case WLAN_CMD_SETPOWER_START:
995
996             RFbSetPower(pDevice, pDevice->wCurrentRate, pMgmt->uCurrChannel);
997
998             s_bCommandComplete(pDevice);
999             break;
1000
1001         case WLAN_CMD_CHANGE_ANTENNA_START:
1002             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Change from Antenna%d to", (int)pDevice->dwRxAntennaSel);
1003             if ( pDevice->dwRxAntennaSel == 0) {
1004                 pDevice->dwRxAntennaSel=1;
1005                 if (pDevice->bTxRxAntInv == TRUE)
1006                     BBvSetAntennaMode(pDevice, ANT_RXA);
1007                 else
1008                     BBvSetAntennaMode(pDevice, ANT_RXB);
1009             } else {
1010                 pDevice->dwRxAntennaSel=0;
1011                 if (pDevice->bTxRxAntInv == TRUE)
1012                     BBvSetAntennaMode(pDevice, ANT_RXB);
1013                 else
1014                     BBvSetAntennaMode(pDevice, ANT_RXA);
1015             }
1016             s_bCommandComplete(pDevice);
1017             break;
1018
1019         case WLAN_CMD_REMOVE_ALLKEY_START:
1020             KeybRemoveAllKey(pDevice, &(pDevice->sKey), pDevice->abyBSSID);
1021             s_bCommandComplete(pDevice);
1022             break;
1023
1024
1025         case WLAN_CMD_MAC_DISPOWERSAVING_START:
1026             ControlvReadByte (pDevice, MESSAGE_REQUEST_MACREG, MAC_REG_PSCTL, &byData);
1027             if ( (byData & PSCTL_PS) != 0 ) {
1028                 // disable power saving hw function
1029                 CONTROLnsRequestOut(pDevice,
1030                                 MESSAGE_TYPE_DISABLE_PS,
1031                                 0,
1032                                 0,
1033                                 0,
1034                                 NULL
1035                                 );
1036             }
1037             s_bCommandComplete(pDevice);
1038             break;
1039
1040         case WLAN_CMD_11H_CHSW_START:
1041             CARDbSetMediaChannel(pDevice, pDevice->byNewChannel);
1042             pDevice->bChannelSwitch = FALSE;
1043             pMgmt->uCurrChannel = pDevice->byNewChannel;
1044             pDevice->bStopDataPkt = FALSE;
1045             s_bCommandComplete(pDevice);
1046             break;
1047
1048         default:
1049             s_bCommandComplete(pDevice);
1050             break;
1051     } //switch
1052
1053     spin_unlock_irq(&pDevice->lock);
1054     return;
1055 }
1056
1057
1058 static
1059 BOOL
1060 s_bCommandComplete (
1061     PSDevice pDevice
1062     )
1063 {
1064     PWLAN_IE_SSID pSSID;
1065     BOOL          bRadioCmd = FALSE;
1066     //WORD          wDeAuthenReason = 0;
1067     BOOL          bForceSCAN = TRUE;
1068     PSMgmtObject  pMgmt = &(pDevice->sMgmtObj);
1069
1070
1071     pDevice->eCommandState = WLAN_CMD_IDLE;
1072     if (pDevice->cbFreeCmdQueue == CMD_Q_SIZE) {
1073         //Command Queue Empty
1074         pDevice->bCmdRunning = FALSE;
1075         return TRUE;
1076     }
1077     else {
1078         pDevice->eCommand = pDevice->eCmdQueue[pDevice->uCmdDequeueIdx].eCmd;
1079         pSSID = (PWLAN_IE_SSID)pDevice->eCmdQueue[pDevice->uCmdDequeueIdx].abyCmdDesireSSID;
1080         bRadioCmd = pDevice->eCmdQueue[pDevice->uCmdDequeueIdx].bRadioCmd;
1081         bForceSCAN = pDevice->eCmdQueue[pDevice->uCmdDequeueIdx].bForceSCAN;
1082         ADD_ONE_WITH_WRAP_AROUND(pDevice->uCmdDequeueIdx, CMD_Q_SIZE);
1083         pDevice->cbFreeCmdQueue++;
1084         pDevice->bCmdRunning = TRUE;
1085         switch ( pDevice->eCommand ) {
1086             case WLAN_CMD_BSSID_SCAN:
1087                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"eCommandState= WLAN_CMD_BSSID_SCAN\n");
1088                 pDevice->eCommandState = WLAN_CMD_SCAN_START;
1089                 pMgmt->uScanChannel = 0;
1090                 if (pSSID->len != 0) {
1091                     memcpy(pMgmt->abyScanSSID, pSSID, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
1092                 } else {
1093                     memset(pMgmt->abyScanSSID, 0, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
1094                 }
1095 /*
1096                 if ((bForceSCAN == FALSE) && (pDevice->bLinkPass == TRUE)) {
1097                     if ((pSSID->len == ((PWLAN_IE_SSID)pMgmt->abyCurrSSID)->len) &&
1098                         ( !memcmp(pSSID->abySSID, ((PWLAN_IE_SSID)pMgmt->abyCurrSSID)->abySSID, pSSID->len))) {
1099                         pDevice->eCommandState = WLAN_CMD_IDLE;
1100                     }
1101                 }
1102 */
1103                 break;
1104             case WLAN_CMD_SSID:
1105                 pDevice->eCommandState = WLAN_CMD_SSID_START;
1106                 if (pSSID->len > WLAN_SSID_MAXLEN)
1107                     pSSID->len = WLAN_SSID_MAXLEN;
1108                 if (pSSID->len != 0)
1109                     memcpy(pMgmt->abyDesireSSID, pSSID, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
1110                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"eCommandState= WLAN_CMD_SSID_START\n");
1111                 break;
1112             case WLAN_CMD_DISASSOCIATE:
1113                 pDevice->eCommandState = WLAN_CMD_DISASSOCIATE_START;
1114                 break;
1115             case WLAN_CMD_RX_PSPOLL:
1116                 pDevice->eCommandState = WLAN_CMD_TX_PSPACKET_START;
1117                 break;
1118             case WLAN_CMD_RUN_AP:
1119                 pDevice->eCommandState = WLAN_CMD_AP_MODE_START;
1120                 break;
1121             case WLAN_CMD_RADIO:
1122                 pDevice->eCommandState = WLAN_CMD_RADIO_START;
1123                 pDevice->bRadioCmd = bRadioCmd;
1124                 break;
1125             case WLAN_CMD_CHANGE_BBSENSITIVITY:
1126                 pDevice->eCommandState = WLAN_CMD_CHANGE_BBSENSITIVITY_START;
1127                 break;
1128
1129             case WLAN_CMD_TBTT_WAKEUP:
1130                 pDevice->eCommandState = WLAN_CMD_TBTT_WAKEUP_START;
1131                 break;
1132
1133             case WLAN_CMD_BECON_SEND:
1134                 pDevice->eCommandState = WLAN_CMD_BECON_SEND_START;
1135                 break;
1136
1137             case WLAN_CMD_SETPOWER:
1138                 pDevice->eCommandState = WLAN_CMD_SETPOWER_START;
1139                 break;
1140
1141             case WLAN_CMD_CHANGE_ANTENNA:
1142                 pDevice->eCommandState = WLAN_CMD_CHANGE_ANTENNA_START;
1143                 break;
1144
1145             case WLAN_CMD_REMOVE_ALLKEY:
1146                 pDevice->eCommandState = WLAN_CMD_REMOVE_ALLKEY_START;
1147                 break;
1148
1149             case WLAN_CMD_MAC_DISPOWERSAVING:
1150                 pDevice->eCommandState = WLAN_CMD_MAC_DISPOWERSAVING_START;
1151                 break;
1152
1153             case WLAN_CMD_11H_CHSW:
1154                 pDevice->eCommandState = WLAN_CMD_11H_CHSW_START;
1155                 break;
1156
1157             default:
1158                 break;
1159
1160         }
1161         vCommandTimerWait((void *) pDevice, 0);
1162     }
1163
1164     return TRUE;
1165 }
1166
1167 BOOL bScheduleCommand(void *hDeviceContext,
1168                       CMD_CODE eCommand,
1169                       PBYTE pbyItem0)
1170 {
1171     PSDevice        pDevice = (PSDevice)hDeviceContext;
1172
1173
1174     if (pDevice->cbFreeCmdQueue == 0) {
1175         return (FALSE);
1176     }
1177     pDevice->eCmdQueue[pDevice->uCmdEnqueueIdx].eCmd = eCommand;
1178     pDevice->eCmdQueue[pDevice->uCmdEnqueueIdx].bForceSCAN = TRUE;
1179     memset(pDevice->eCmdQueue[pDevice->uCmdEnqueueIdx].abyCmdDesireSSID, 0 , WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
1180     if (pbyItem0 != NULL) {
1181         switch (eCommand) {
1182             case WLAN_CMD_BSSID_SCAN:
1183                 pDevice->eCmdQueue[pDevice->uCmdEnqueueIdx].bForceSCAN = FALSE;
1184                 memcpy(pDevice->eCmdQueue[pDevice->uCmdEnqueueIdx].abyCmdDesireSSID,
1185                          pbyItem0, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
1186                 break;
1187
1188             case WLAN_CMD_SSID:
1189                 memcpy(pDevice->eCmdQueue[pDevice->uCmdEnqueueIdx].abyCmdDesireSSID,
1190                          pbyItem0, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
1191                 break;
1192
1193             case WLAN_CMD_DISASSOCIATE:
1194                 pDevice->eCmdQueue[pDevice->uCmdEnqueueIdx].bNeedRadioOFF = *((int *)pbyItem0);
1195                 break;
1196 /*
1197             case WLAN_CMD_DEAUTH:
1198                 pDevice->eCmdQueue[pDevice->uCmdEnqueueIdx].wDeAuthenReason = *((PWORD)pbyItem0);
1199                 break;
1200 */
1201
1202             case WLAN_CMD_RADIO:
1203                 pDevice->eCmdQueue[pDevice->uCmdEnqueueIdx].bRadioCmd = *((int *)pbyItem0);
1204                 break;
1205
1206             default:
1207                 break;
1208         }
1209     }
1210
1211     ADD_ONE_WITH_WRAP_AROUND(pDevice->uCmdEnqueueIdx, CMD_Q_SIZE);
1212     pDevice->cbFreeCmdQueue--;
1213
1214     if (pDevice->bCmdRunning == FALSE) {
1215         s_bCommandComplete(pDevice);
1216     }
1217     else {
1218     }
1219     return (TRUE);
1220
1221 }
1222
1223 /*
1224  * Description:
1225  *      Clear BSSID_SCAN cmd in CMD Queue
1226  *
1227  * Parameters:
1228  *  In:
1229  *      hDeviceContext  - Pointer to the adapter
1230  *      eCommand        - Command
1231  *  Out:
1232  *      none
1233  *
1234  * Return Value: TRUE if success; otherwise FALSE
1235  *
1236  */
1237 static BOOL s_bClearBSSID_SCAN(void *hDeviceContext)
1238 {
1239     PSDevice        pDevice = (PSDevice)hDeviceContext;
1240     unsigned int            uCmdDequeueIdx = pDevice->uCmdDequeueIdx;
1241     unsigned int            ii;
1242
1243     if ((pDevice->cbFreeCmdQueue < CMD_Q_SIZE) && (uCmdDequeueIdx != pDevice->uCmdEnqueueIdx)) {
1244         for (ii = 0; ii < (CMD_Q_SIZE - pDevice->cbFreeCmdQueue); ii ++) {
1245             if (pDevice->eCmdQueue[uCmdDequeueIdx].eCmd == WLAN_CMD_BSSID_SCAN)
1246                 pDevice->eCmdQueue[uCmdDequeueIdx].eCmd = WLAN_CMD_IDLE;
1247             ADD_ONE_WITH_WRAP_AROUND(uCmdDequeueIdx, CMD_Q_SIZE);
1248             if (uCmdDequeueIdx == pDevice->uCmdEnqueueIdx)
1249                 break;
1250         }
1251     }
1252     return TRUE;
1253 }
1254
1255
1256 //mike add:reset command timer
1257 void vResetCommandTimer(void *hDeviceContext)
1258 {
1259   PSDevice        pDevice = (PSDevice)hDeviceContext;
1260
1261   //delete timer
1262       del_timer(&pDevice->sTimerCommand);
1263   //init timer
1264       init_timer(&pDevice->sTimerCommand);
1265     pDevice->sTimerCommand.data = (unsigned long)pDevice;
1266     pDevice->sTimerCommand.function = (TimerFunction)vRunCommand;
1267     pDevice->sTimerCommand.expires = RUN_AT(HZ);
1268     pDevice->cbFreeCmdQueue = CMD_Q_SIZE;
1269     pDevice->uCmdDequeueIdx = 0;
1270     pDevice->uCmdEnqueueIdx = 0;
1271     pDevice->eCommandState = WLAN_CMD_IDLE;
1272     pDevice->bCmdRunning = FALSE;
1273     pDevice->bCmdClear = FALSE;
1274 }
1275
1276 void BSSvSecondTxData(void *hDeviceContext)
1277 {
1278   PSDevice        pDevice = (PSDevice)hDeviceContext;
1279   PSMgmtObject  pMgmt = &(pDevice->sMgmtObj);
1280
1281   pDevice->nTxDataTimeCout++;
1282
1283   if(pDevice->nTxDataTimeCout<4)     //don't tx data if timer less than 40s
1284     {
1285      // printk("mike:%s-->no data Tx not exceed the desired Time as %d\n",__FUNCTION__,
1286         //      (int)pDevice->nTxDataTimeCout);
1287      pDevice->sTimerTxData.expires = RUN_AT(10*HZ);      //10s callback
1288      add_timer(&pDevice->sTimerTxData);
1289       return;
1290     }
1291
1292   spin_lock_irq(&pDevice->lock);
1293   //is wap_supplicant running successful OR only open && sharekey mode!
1294   if(((pDevice->bLinkPass ==TRUE)&&(pMgmt->eAuthenMode < WMAC_AUTH_WPA)) ||  //open && sharekey linking
1295       (pDevice->fWPA_Authened == TRUE)) {   //wpa linking
1296         //   printk("mike:%s-->InSleep Tx Data Procedure\n",__FUNCTION__);
1297           pDevice->fTxDataInSleep = TRUE;
1298           PSbSendNullPacket(pDevice);      //send null packet
1299           pDevice->fTxDataInSleep = FALSE;
1300         }
1301   spin_unlock_irq(&pDevice->lock);
1302
1303   pDevice->sTimerTxData.expires = RUN_AT(10*HZ);      //10s callback
1304   add_timer(&pDevice->sTimerTxData);
1305   return;
1306 }