Merge branch 'linus' into cont_syslog
[safe/jmp/linux-2.6] / 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 int MSecond)
320 {
321     PSDevice        pDevice = (PSDevice)hDeviceContext;
322
323     init_timer(&pDevice->sTimerCommand);
324     pDevice->sTimerCommand.data = (unsigned long)pDevice;
325     pDevice->sTimerCommand.function = (TimerFunction)vRunCommand;
326     // RUN_AT :1 msec ~= (HZ/1024)
327     pDevice->sTimerCommand.expires = (unsigned int)RUN_AT((MSecond * HZ) >> 10);
328     add_timer(&pDevice->sTimerCommand);
329     return;
330 }
331
332 void vRunCommand(void *hDeviceContext)
333 {
334     PSDevice        pDevice = (PSDevice)hDeviceContext;
335     PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
336     PWLAN_IE_SSID   pItemSSID;
337     PWLAN_IE_SSID   pItemSSIDCurr;
338     CMD_STATUS      Status;
339     unsigned int            ii;
340     BYTE            byMask[8] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80};
341     struct sk_buff  *skb;
342     BYTE            byData;
343
344
345     if (pDevice->dwDiagRefCount != 0)
346         return;
347     if (pDevice->bCmdRunning != TRUE)
348         return;
349
350     spin_lock_irq(&pDevice->lock);
351
352     switch ( pDevice->eCommandState ) {
353
354         case WLAN_CMD_SCAN_START:
355
356                 pDevice->byReAssocCount = 0;
357             if (pDevice->bRadioOff == TRUE) {
358                 s_bCommandComplete(pDevice);
359                 spin_unlock_irq(&pDevice->lock);
360                 return;
361             }
362
363             if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
364                 s_bCommandComplete(pDevice);
365                 spin_unlock_irq(&pDevice->lock);
366                 return;
367             }
368
369             pItemSSID = (PWLAN_IE_SSID)pMgmt->abyScanSSID;
370
371             if (pMgmt->uScanChannel == 0 ) {
372                 pMgmt->uScanChannel = pDevice->byMinChannel;
373             }
374             if (pMgmt->uScanChannel > pDevice->byMaxChannel) {
375                 pMgmt->eScanState = WMAC_NO_SCANNING;
376
377                 if (pDevice->byBBType != pDevice->byScanBBType) {
378                     pDevice->byBBType = pDevice->byScanBBType;
379                     CARDvSetBSSMode(pDevice);
380                 }
381
382                 if (pDevice->bUpdateBBVGA) {
383                     BBvSetShortSlotTime(pDevice);
384                     BBvSetVGAGainOffset(pDevice, pDevice->byBBVGACurrent);
385                     BBvUpdatePreEDThreshold(pDevice, FALSE);
386                 }
387                 // Set channel back
388                 vAdHocBeaconRestart(pDevice);
389                 // Set channel back
390                 CARDbSetMediaChannel(pDevice, pMgmt->uCurrChannel);
391                 // Set Filter
392                 if (pMgmt->bCurrBSSIDFilterOn) {
393                     MACvRegBitsOn(pDevice, MAC_REG_RCR, RCR_BSSID);
394                     pDevice->byRxMode |= RCR_BSSID;
395                 }
396                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Scanning, set back to channel: [%d]\n", pMgmt->uCurrChannel);
397                 pDevice->bStopDataPkt = FALSE;
398                 s_bCommandComplete(pDevice);
399                 spin_unlock_irq(&pDevice->lock);
400                 return;
401
402             } else {
403                 if (!ChannelValid(pDevice->byZoneType, pMgmt->uScanChannel)) {
404                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Invalid channel pMgmt->uScanChannel = %d \n",pMgmt->uScanChannel);
405                     s_bCommandComplete(pDevice);
406                     spin_unlock_irq(&pDevice->lock);
407                     return;
408                 }
409                 if (pMgmt->uScanChannel == pDevice->byMinChannel) {
410                    // pMgmt->eScanType = WMAC_SCAN_ACTIVE;          //mike mark
411                     pMgmt->abyScanBSSID[0] = 0xFF;
412                     pMgmt->abyScanBSSID[1] = 0xFF;
413                     pMgmt->abyScanBSSID[2] = 0xFF;
414                     pMgmt->abyScanBSSID[3] = 0xFF;
415                     pMgmt->abyScanBSSID[4] = 0xFF;
416                     pMgmt->abyScanBSSID[5] = 0xFF;
417                     pItemSSID->byElementID = WLAN_EID_SSID;
418                     // clear bssid list
419                     /* BSSvClearBSSList((void *) pDevice,
420                        pDevice->bLinkPass); */
421                     pMgmt->eScanState = WMAC_IS_SCANNING;
422                     pDevice->byScanBBType = pDevice->byBBType;  //lucas
423                     pDevice->bStopDataPkt = TRUE;
424                     // Turn off RCR_BSSID filter everytime
425                     MACvRegBitsOff(pDevice, MAC_REG_RCR, RCR_BSSID);
426                     pDevice->byRxMode &= ~RCR_BSSID;
427
428                 }
429                 //lucas
430                 vAdHocBeaconStop(pDevice);
431                 if ((pDevice->byBBType != BB_TYPE_11A) && (pMgmt->uScanChannel > CB_MAX_CHANNEL_24G)) {
432                     pDevice->byBBType = BB_TYPE_11A;
433                     CARDvSetBSSMode(pDevice);
434                 }
435                 else if ((pDevice->byBBType == BB_TYPE_11A) && (pMgmt->uScanChannel <= CB_MAX_CHANNEL_24G)) {
436                     pDevice->byBBType = BB_TYPE_11G;
437                     CARDvSetBSSMode(pDevice);
438                 }
439                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Scanning....  channel: [%d]\n", pMgmt->uScanChannel);
440                 // Set channel
441                 CARDbSetMediaChannel(pDevice, pMgmt->uScanChannel);
442                 // Set Baseband to be more sensitive.
443
444                 if (pDevice->bUpdateBBVGA) {
445                     BBvSetShortSlotTime(pDevice);
446                     BBvSetVGAGainOffset(pDevice, pDevice->abyBBVGA[0]);
447                     BBvUpdatePreEDThreshold(pDevice, TRUE);
448                 }
449                 pMgmt->uScanChannel++;
450
451                 while (!ChannelValid(pDevice->byZoneType, pMgmt->uScanChannel) &&
452                         pMgmt->uScanChannel <= pDevice->byMaxChannel ){
453                     pMgmt->uScanChannel++;
454                 }
455
456                 if (pMgmt->uScanChannel > pDevice->byMaxChannel) {
457                     // Set Baseband to be not sensitive and rescan
458                     pDevice->eCommandState = WLAN_CMD_SCAN_END;
459
460                 }
461                 if ((pMgmt->b11hEnable == FALSE) ||
462                     (pMgmt->uScanChannel < CB_MAX_CHANNEL_24G)) {
463                     s_vProbeChannel(pDevice);
464                     spin_unlock_irq(&pDevice->lock);
465                      vCommandTimerWait((void *) pDevice, 100);
466                     return;
467                 } else {
468                     spin_unlock_irq(&pDevice->lock);
469                     vCommandTimerWait((void *) pDevice, WCMD_PASSIVE_SCAN_TIME);
470                     return;
471                 }
472
473             }
474
475             break;
476
477         case WLAN_CMD_SCAN_END:
478
479             // Set Baseband's sensitivity back.
480             if (pDevice->byBBType != pDevice->byScanBBType) {
481                 pDevice->byBBType = pDevice->byScanBBType;
482                 CARDvSetBSSMode(pDevice);
483             }
484
485             if (pDevice->bUpdateBBVGA) {
486                 BBvSetShortSlotTime(pDevice);
487                 BBvSetVGAGainOffset(pDevice, pDevice->byBBVGACurrent);
488                 BBvUpdatePreEDThreshold(pDevice, FALSE);
489             }
490
491             // Set channel back
492             vAdHocBeaconRestart(pDevice);
493             // Set channel back
494             CARDbSetMediaChannel(pDevice, pMgmt->uCurrChannel);
495             // Set Filter
496             if (pMgmt->bCurrBSSIDFilterOn) {
497                 MACvRegBitsOn(pDevice, MAC_REG_RCR, RCR_BSSID);
498                 pDevice->byRxMode |= RCR_BSSID;
499             }
500             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Scanning, set back to channel: [%d]\n", pMgmt->uCurrChannel);
501             pMgmt->eScanState = WMAC_NO_SCANNING;
502             pDevice->bStopDataPkt = FALSE;
503 //2008-0409-07, <Add> by Einsn Liu
504 #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
505         if(pMgmt->eScanType == WMAC_SCAN_PASSIVE)
506                 {
507                         //send scan event to wpa_Supplicant
508                                 union iwreq_data wrqu;
509                                 PRINT_K("wireless_send_event--->SIOCGIWSCAN(scan done)\n");
510                                 memset(&wrqu, 0, sizeof(wrqu));
511                                 wireless_send_event(pDevice->dev, SIOCGIWSCAN, &wrqu, NULL);
512                         }
513 #endif
514             s_bCommandComplete(pDevice);
515             break;
516
517         case WLAN_CMD_DISASSOCIATE_START :
518                 pDevice->byReAssocCount = 0;
519             if ((pMgmt->eCurrMode == WMAC_MODE_ESS_STA) &&
520                 (pMgmt->eCurrState != WMAC_STATE_ASSOC)) {
521                 s_bCommandComplete(pDevice);
522                 spin_unlock_irq(&pDevice->lock);
523                 return;
524             } else {
525
526           #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
527                       pDevice->bwextstep0 = FALSE;
528                         pDevice->bwextstep1 = FALSE;
529                         pDevice->bwextstep2 = FALSE;
530                         pDevice->bwextstep3 = FALSE;
531                    pDevice->bWPASuppWextEnabled = FALSE;
532          #endif
533                    pDevice->fWPA_Authened = FALSE;
534
535                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Send Disassociation Packet..\n");
536                 // reason = 8 : disassoc because sta has left
537                 vMgrDisassocBeginSta((void *) pDevice,
538                                      pMgmt,
539                                      pMgmt->abyCurrBSSID,
540                                      (8),
541                                      &Status);
542                 pDevice->bLinkPass = FALSE;
543                 ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_SLOW);
544                 // unlock command busy
545                 pItemSSID = (PWLAN_IE_SSID)pMgmt->abyCurrSSID;
546                 pItemSSID->len = 0;
547                 memset(pItemSSID->abySSID, 0, WLAN_SSID_MAXLEN);
548                 pMgmt->eCurrState = WMAC_STATE_IDLE;
549                 pMgmt->sNodeDBTable[0].bActive = FALSE;
550 //                pDevice->bBeaconBufReady = FALSE;
551             }
552             netif_stop_queue(pDevice->dev);
553             if (pDevice->bNeedRadioOFF == TRUE)
554                 CARDbRadioPowerOff(pDevice);
555             s_bCommandComplete(pDevice);
556             break;
557
558
559         case WLAN_CMD_SSID_START:
560
561                 pDevice->byReAssocCount = 0;
562             if (pDevice->bRadioOff == TRUE) {
563                 s_bCommandComplete(pDevice);
564                 spin_unlock_irq(&pDevice->lock);
565                 return;
566             }
567
568 //20080131-03,<Add> by Mike Liu
569         #ifdef Adhoc_STA
570             memcpy(pMgmt->abyAdHocSSID,pMgmt->abyDesireSSID,
571                               ((PWLAN_IE_SSID)pMgmt->abyDesireSSID)->len + WLAN_IEHDR_LEN);
572         #endif
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) beacuse 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                  #if 0
720                      #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
721                     // if(pDevice->bWPASuppWextEnabled == TRUE)
722                         {
723                         union iwreq_data  wrqu;
724                         memset(&wrqu, 0, sizeof (wrqu));
725                           wrqu.ap_addr.sa_family = ARPHRD_ETHER;
726                         printk("wireless_send_event--->SIOCGIWAP(disassociated:AUTHENTICATE_WAIT_timeout)\n");
727                         wireless_send_event(pDevice->dev, SIOCGIWAP, &wrqu, NULL);
728                        }
729                     #endif
730                  #endif
731
732             s_bCommandComplete(pDevice);
733             break;
734
735         case WLAN_ASSOCIATE_WAIT :
736             if (pMgmt->eCurrState == WMAC_STATE_ASSOC) {
737                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"eCurrState == WMAC_STATE_ASSOC\n");
738                 if (pDevice->ePSMode != WMAC_POWER_CAM) {
739                         PSvEnablePowerSaving((void *) pDevice,
740                                              pMgmt->wListenInterval);
741                 }
742 /*
743                 if (pMgmt->eAuthenMode >= WMAC_AUTH_WPA) {
744                     KeybRemoveAllKey(pDevice, &(pDevice->sKey), pDevice->abyBSSID);
745                 }
746 */
747                 pDevice->byLinkWaitCount = 0;
748                 pDevice->byReAssocCount = 0;
749                 pDevice->bLinkPass = TRUE;
750                 ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_INTER);
751                 s_bClearBSSID_SCAN(pDevice);
752
753                 if (netif_queue_stopped(pDevice->dev)){
754                     netif_wake_queue(pDevice->dev);
755                 }
756
757         //2007-0115-07<Add>by MikeLiu
758              #ifdef TxInSleep
759                  if(pDevice->IsTxDataTrigger != FALSE)   {    //TxDataTimer is not triggered at the first time
760                      // printk("Re-initial TxDataTimer****\n");
761                     del_timer(&pDevice->sTimerTxData);
762                       init_timer(&pDevice->sTimerTxData);
763                         pDevice->sTimerTxData.data = (unsigned long) pDevice;
764                       pDevice->sTimerTxData.function = (TimerFunction)BSSvSecondTxData;
765                       pDevice->sTimerTxData.expires = RUN_AT(10*HZ);      //10s callback
766                       pDevice->fTxDataInSleep = FALSE;
767                       pDevice->nTxDataTimeCout = 0;
768                  }
769                  else {
770                    // printk("mike:-->First time triger TimerTxData InSleep\n");
771                  }
772                 pDevice->IsTxDataTrigger = TRUE;
773                 add_timer(&pDevice->sTimerTxData);
774              #endif
775
776             }
777            else if(pMgmt->eCurrState < WMAC_STATE_ASSOCPENDING) {
778                printk("WLAN_ASSOCIATE_WAIT:Association Fail???\n");
779            }
780            else  if(pDevice->byLinkWaitCount <= 4){    //mike add:wait another 2 sec if associated_frame delay!
781                 pDevice->byLinkWaitCount ++;
782                printk("WLAN_ASSOCIATE_WAIT:wait %d times!!\n",pDevice->byLinkWaitCount);
783                spin_unlock_irq(&pDevice->lock);
784                vCommandTimerWait((void *) pDevice, ASSOCIATE_TIMEOUT/2);
785                return;
786            }
787                   pDevice->byLinkWaitCount = 0;
788                 #if 0
789                      #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
790                     // if(pDevice->bWPASuppWextEnabled == TRUE)
791                         {
792                         union iwreq_data  wrqu;
793                         memset(&wrqu, 0, sizeof (wrqu));
794                           wrqu.ap_addr.sa_family = ARPHRD_ETHER;
795                         printk("wireless_send_event--->SIOCGIWAP(disassociated:ASSOCIATE_WAIT_timeout)\n");
796                         wireless_send_event(pDevice->dev, SIOCGIWAP, &wrqu, NULL);
797                        }
798                     #endif
799                 #endif
800
801             s_bCommandComplete(pDevice);
802             break;
803
804         case WLAN_CMD_AP_MODE_START :
805             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"eCommandState == WLAN_CMD_AP_MODE_START\n");
806
807             if (pMgmt->eConfigMode == WMAC_CONFIG_AP) {
808                 del_timer(&pMgmt->sTimerSecondCallback);
809                 pMgmt->eCurrState = WMAC_STATE_IDLE;
810                 pMgmt->eCurrMode = WMAC_MODE_STANDBY;
811                 pDevice->bLinkPass = FALSE;
812                 ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_SLOW);
813                 if (pDevice->bEnableHostWEP == TRUE)
814                     BSSvClearNodeDBTable(pDevice, 1);
815                 else
816                     BSSvClearNodeDBTable(pDevice, 0);
817                 pDevice->uAssocCount = 0;
818                 pMgmt->eCurrState = WMAC_STATE_IDLE;
819                 pDevice->bFixRate = FALSE;
820
821                 vMgrCreateOwnIBSS((void *) pDevice, &Status);
822                 if (Status != CMD_STATUS_SUCCESS) {
823                         DBG_PRT(MSG_LEVEL_DEBUG,
824                                 KERN_INFO "vMgrCreateOwnIBSS fail!\n");
825                 };
826                 // alway turn off unicast bit
827                 MACvRegBitsOff(pDevice, MAC_REG_RCR, RCR_UNICAST);
828                 pDevice->byRxMode &= ~RCR_UNICAST;
829                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "wcmd: rx_mode = %x\n", pDevice->byRxMode );
830                 BSSvAddMulticastNode(pDevice);
831                 if (netif_queue_stopped(pDevice->dev)){
832                     netif_wake_queue(pDevice->dev);
833                 }
834                 pDevice->bLinkPass = TRUE;
835                 ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_INTER);
836                 add_timer(&pMgmt->sTimerSecondCallback);
837             }
838             s_bCommandComplete(pDevice);
839             break;
840
841         case WLAN_CMD_TX_PSPACKET_START :
842             // DTIM Multicast tx
843             if (pMgmt->sNodeDBTable[0].bRxPSPoll) {
844                 while ((skb = skb_dequeue(&pMgmt->sNodeDBTable[0].sTxPSQueue)) != NULL) {
845                     if (skb_queue_empty(&pMgmt->sNodeDBTable[0].sTxPSQueue)) {
846                         pMgmt->abyPSTxMap[0] &= ~byMask[0];
847                         pDevice->bMoreData = FALSE;
848                     }
849                     else {
850                         pDevice->bMoreData = TRUE;
851                     }
852
853                     if (nsDMA_tx_packet(pDevice, TYPE_AC0DMA, skb) != 0) {
854                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Multicast ps tx fail \n");
855                     }
856
857                     pMgmt->sNodeDBTable[0].wEnQueueCnt--;
858                 }
859             };
860
861             // PS nodes tx
862             for (ii = 1; ii < (MAX_NODE_NUM + 1); ii++) {
863                 if (pMgmt->sNodeDBTable[ii].bActive &&
864                     pMgmt->sNodeDBTable[ii].bRxPSPoll) {
865                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Index=%d Enqueu Cnt= %d\n",
866                                ii, pMgmt->sNodeDBTable[ii].wEnQueueCnt);
867                     while ((skb = skb_dequeue(&pMgmt->sNodeDBTable[ii].sTxPSQueue)) != NULL) {
868                         if (skb_queue_empty(&pMgmt->sNodeDBTable[ii].sTxPSQueue)) {
869                             // clear tx map
870                             pMgmt->abyPSTxMap[pMgmt->sNodeDBTable[ii].wAID >> 3] &=
871                                     ~byMask[pMgmt->sNodeDBTable[ii].wAID & 7];
872                             pDevice->bMoreData = FALSE;
873                         }
874                         else {
875                             pDevice->bMoreData = TRUE;
876                         }
877
878                         if (nsDMA_tx_packet(pDevice, TYPE_AC0DMA, skb) != 0) {
879                             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "sta ps tx fail \n");
880                         }
881
882                         pMgmt->sNodeDBTable[ii].wEnQueueCnt--;
883                         // check if sta ps enable, wait next pspoll
884                         // if sta ps disable, send all pending buffers.
885                         if (pMgmt->sNodeDBTable[ii].bPSEnable)
886                             break;
887                     }
888                     if (skb_queue_empty(&pMgmt->sNodeDBTable[ii].sTxPSQueue)) {
889                         // clear tx map
890                         pMgmt->abyPSTxMap[pMgmt->sNodeDBTable[ii].wAID >> 3] &=
891                                     ~byMask[pMgmt->sNodeDBTable[ii].wAID & 7];
892                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Index=%d PS queue clear \n", ii);
893                     }
894                     pMgmt->sNodeDBTable[ii].bRxPSPoll = FALSE;
895                 }
896             }
897
898             s_bCommandComplete(pDevice);
899             break;
900
901         case WLAN_CMD_RADIO_START:
902
903             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"eCommandState == WLAN_CMD_RADIO_START\n");
904        //     if (pDevice->bRadioCmd == TRUE)
905        //         CARDbRadioPowerOn(pDevice);
906        //     else
907        //         CARDbRadioPowerOff(pDevice);
908        //2008-09-09<Add> BY Mike:Hot Key for Radio On/Off
909        {
910         NTSTATUS        ntStatus = STATUS_SUCCESS;
911         BYTE            byTmp;
912
913         ntStatus = CONTROLnsRequestIn(pDevice,
914                                     MESSAGE_TYPE_READ,
915                                     MAC_REG_GPIOCTL1,
916                                     MESSAGE_REQUEST_MACREG,
917                                     1,
918                                     &byTmp);
919
920         if ( ntStatus != STATUS_SUCCESS ) {
921                 s_bCommandComplete(pDevice);
922                 spin_unlock_irq(&pDevice->lock);
923                 return;
924         }
925         if ( (byTmp & GPIO3_DATA) == 0 ) {
926             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" WLAN_CMD_RADIO_START_OFF........................\n");
927                 // Old commands are useless.
928                 // empty command Q
929                pDevice->cbFreeCmdQueue = CMD_Q_SIZE;
930                 pDevice->uCmdDequeueIdx = 0;
931                 pDevice->uCmdEnqueueIdx = 0;
932                 //0415pDevice->bCmdRunning = FALSE;
933                 pDevice->bCmdClear = TRUE;
934                 pDevice->bStopTx0Pkt = FALSE;
935                 pDevice->bStopDataPkt = TRUE;
936
937                 pDevice->byKeyIndex = 0;
938                 pDevice->bTransmitKey = FALSE;
939             spin_unlock_irq(&pDevice->lock);
940             KeyvInitTable(pDevice,&pDevice->sKey);
941             spin_lock_irq(&pDevice->lock);
942                pMgmt->byCSSPK = KEY_CTL_NONE;
943                 pMgmt->byCSSGK = KEY_CTL_NONE;
944
945           if (pDevice->bLinkPass == TRUE) {
946                 // reason = 8 : disassoc because sta has left
947                 vMgrDisassocBeginSta((void *) pDevice,
948                                      pMgmt,
949                                      pMgmt->abyCurrBSSID,
950                                      (8),
951                                      &Status);
952                        pDevice->bLinkPass = FALSE;
953                 // unlock command busy
954                         pMgmt->eCurrState = WMAC_STATE_IDLE;
955                         pMgmt->sNodeDBTable[0].bActive = FALSE;
956                      #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
957                     // if(pDevice->bWPASuppWextEnabled == TRUE)
958                         {
959                         union iwreq_data  wrqu;
960                         memset(&wrqu, 0, sizeof (wrqu));
961                           wrqu.ap_addr.sa_family = ARPHRD_ETHER;
962                         PRINT_K("wireless_send_event--->SIOCGIWAP(disassociated)\n");
963                         wireless_send_event(pDevice->dev, SIOCGIWAP, &wrqu, NULL);
964                        }
965                     #endif
966                 }
967                #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
968                        pDevice->bwextstep0 = FALSE;
969                         pDevice->bwextstep1 = FALSE;
970                         pDevice->bwextstep2 = FALSE;
971                         pDevice->bwextstep3 = FALSE;
972                       pDevice->bWPASuppWextEnabled = FALSE;
973                 #endif
974                           //clear current SSID
975                   pItemSSID = (PWLAN_IE_SSID)pMgmt->abyCurrSSID;
976                   pItemSSID->len = 0;
977                   memset(pItemSSID->abySSID, 0, WLAN_SSID_MAXLEN);
978                 //clear dessire SSID
979                 pItemSSID = (PWLAN_IE_SSID)pMgmt->abyDesireSSID;
980                 pItemSSID->len = 0;
981                 memset(pItemSSID->abySSID, 0, WLAN_SSID_MAXLEN);
982
983             netif_stop_queue(pDevice->dev);
984             CARDbRadioPowerOff(pDevice);
985              MACvRegBitsOn(pDevice,MAC_REG_GPIOCTL1,GPIO3_INTMD);
986             ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_OFF);
987             pDevice->bHWRadioOff = TRUE;
988         } else {
989             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" WLAN_CMD_RADIO_START_ON........................\n");
990             pDevice->bHWRadioOff = FALSE;
991                 CARDbRadioPowerOn(pDevice);
992             MACvRegBitsOff(pDevice,MAC_REG_GPIOCTL1,GPIO3_INTMD);
993             ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_ON);
994         }
995       }
996
997             s_bCommandComplete(pDevice);
998             break;
999
1000
1001         case WLAN_CMD_CHANGE_BBSENSITIVITY_START:
1002
1003             pDevice->bStopDataPkt = TRUE;
1004             pDevice->byBBVGACurrent = pDevice->byBBVGANew;
1005             BBvSetVGAGainOffset(pDevice, pDevice->byBBVGACurrent);
1006             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Change sensitivity pDevice->byBBVGACurrent = %x\n", pDevice->byBBVGACurrent);
1007             pDevice->bStopDataPkt = FALSE;
1008             s_bCommandComplete(pDevice);
1009             break;
1010
1011         case WLAN_CMD_TBTT_WAKEUP_START:
1012             PSbIsNextTBTTWakeUp(pDevice);
1013             s_bCommandComplete(pDevice);
1014             break;
1015
1016         case WLAN_CMD_BECON_SEND_START:
1017             bMgrPrepareBeaconToSend(pDevice, pMgmt);
1018             s_bCommandComplete(pDevice);
1019             break;
1020
1021         case WLAN_CMD_SETPOWER_START:
1022
1023             RFbSetPower(pDevice, pDevice->wCurrentRate, pMgmt->uCurrChannel);
1024
1025             s_bCommandComplete(pDevice);
1026             break;
1027
1028         case WLAN_CMD_CHANGE_ANTENNA_START:
1029             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Change from Antenna%d to", (int)pDevice->dwRxAntennaSel);
1030             if ( pDevice->dwRxAntennaSel == 0) {
1031                 pDevice->dwRxAntennaSel=1;
1032                 if (pDevice->bTxRxAntInv == TRUE)
1033                     BBvSetAntennaMode(pDevice, ANT_RXA);
1034                 else
1035                     BBvSetAntennaMode(pDevice, ANT_RXB);
1036             } else {
1037                 pDevice->dwRxAntennaSel=0;
1038                 if (pDevice->bTxRxAntInv == TRUE)
1039                     BBvSetAntennaMode(pDevice, ANT_RXB);
1040                 else
1041                     BBvSetAntennaMode(pDevice, ANT_RXA);
1042             }
1043             s_bCommandComplete(pDevice);
1044             break;
1045
1046         case WLAN_CMD_REMOVE_ALLKEY_START:
1047             KeybRemoveAllKey(pDevice, &(pDevice->sKey), pDevice->abyBSSID);
1048             s_bCommandComplete(pDevice);
1049             break;
1050
1051
1052         case WLAN_CMD_MAC_DISPOWERSAVING_START:
1053             ControlvReadByte (pDevice, MESSAGE_REQUEST_MACREG, MAC_REG_PSCTL, &byData);
1054             if ( (byData & PSCTL_PS) != 0 ) {
1055                 // disable power saving hw function
1056                 CONTROLnsRequestOut(pDevice,
1057                                 MESSAGE_TYPE_DISABLE_PS,
1058                                 0,
1059                                 0,
1060                                 0,
1061                                 NULL
1062                                 );
1063             }
1064             s_bCommandComplete(pDevice);
1065             break;
1066
1067         case WLAN_CMD_11H_CHSW_START:
1068             CARDbSetMediaChannel(pDevice, pDevice->byNewChannel);
1069             pDevice->bChannelSwitch = FALSE;
1070             pMgmt->uCurrChannel = pDevice->byNewChannel;
1071             pDevice->bStopDataPkt = FALSE;
1072             s_bCommandComplete(pDevice);
1073             break;
1074
1075         default:
1076             s_bCommandComplete(pDevice);
1077             break;
1078     } //switch
1079
1080     spin_unlock_irq(&pDevice->lock);
1081     return;
1082 }
1083
1084
1085 static
1086 BOOL
1087 s_bCommandComplete (
1088     PSDevice pDevice
1089     )
1090 {
1091     PWLAN_IE_SSID pSSID;
1092     BOOL          bRadioCmd = FALSE;
1093     //WORD          wDeAuthenReason = 0;
1094     BOOL          bForceSCAN = TRUE;
1095     PSMgmtObject  pMgmt = &(pDevice->sMgmtObj);
1096
1097
1098     pDevice->eCommandState = WLAN_CMD_IDLE;
1099     if (pDevice->cbFreeCmdQueue == CMD_Q_SIZE) {
1100         //Command Queue Empty
1101         pDevice->bCmdRunning = FALSE;
1102         return TRUE;
1103     }
1104     else {
1105         pDevice->eCommand = pDevice->eCmdQueue[pDevice->uCmdDequeueIdx].eCmd;
1106         pSSID = (PWLAN_IE_SSID)pDevice->eCmdQueue[pDevice->uCmdDequeueIdx].abyCmdDesireSSID;
1107         bRadioCmd = pDevice->eCmdQueue[pDevice->uCmdDequeueIdx].bRadioCmd;
1108         bForceSCAN = pDevice->eCmdQueue[pDevice->uCmdDequeueIdx].bForceSCAN;
1109         ADD_ONE_WITH_WRAP_AROUND(pDevice->uCmdDequeueIdx, CMD_Q_SIZE);
1110         pDevice->cbFreeCmdQueue++;
1111         pDevice->bCmdRunning = TRUE;
1112         switch ( pDevice->eCommand ) {
1113             case WLAN_CMD_BSSID_SCAN:
1114                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"eCommandState= WLAN_CMD_BSSID_SCAN\n");
1115                 pDevice->eCommandState = WLAN_CMD_SCAN_START;
1116                 pMgmt->uScanChannel = 0;
1117                 if (pSSID->len != 0) {
1118                     memcpy(pMgmt->abyScanSSID, pSSID, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
1119                 } else {
1120                     memset(pMgmt->abyScanSSID, 0, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
1121                 }
1122 /*
1123                 if ((bForceSCAN == FALSE) && (pDevice->bLinkPass == TRUE)) {
1124                     if ((pSSID->len == ((PWLAN_IE_SSID)pMgmt->abyCurrSSID)->len) &&
1125                         ( !memcmp(pSSID->abySSID, ((PWLAN_IE_SSID)pMgmt->abyCurrSSID)->abySSID, pSSID->len))) {
1126                         pDevice->eCommandState = WLAN_CMD_IDLE;
1127                     }
1128                 }
1129 */
1130                 break;
1131             case WLAN_CMD_SSID:
1132                 pDevice->eCommandState = WLAN_CMD_SSID_START;
1133                 if (pSSID->len > WLAN_SSID_MAXLEN)
1134                     pSSID->len = WLAN_SSID_MAXLEN;
1135                 if (pSSID->len != 0)
1136                     memcpy(pMgmt->abyDesireSSID, pSSID, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
1137                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"eCommandState= WLAN_CMD_SSID_START\n");
1138                 break;
1139             case WLAN_CMD_DISASSOCIATE:
1140                 pDevice->eCommandState = WLAN_CMD_DISASSOCIATE_START;
1141                 break;
1142             case WLAN_CMD_RX_PSPOLL:
1143                 pDevice->eCommandState = WLAN_CMD_TX_PSPACKET_START;
1144                 break;
1145             case WLAN_CMD_RUN_AP:
1146                 pDevice->eCommandState = WLAN_CMD_AP_MODE_START;
1147                 break;
1148             case WLAN_CMD_RADIO:
1149                 pDevice->eCommandState = WLAN_CMD_RADIO_START;
1150                 pDevice->bRadioCmd = bRadioCmd;
1151                 break;
1152             case WLAN_CMD_CHANGE_BBSENSITIVITY:
1153                 pDevice->eCommandState = WLAN_CMD_CHANGE_BBSENSITIVITY_START;
1154                 break;
1155
1156             case WLAN_CMD_TBTT_WAKEUP:
1157                 pDevice->eCommandState = WLAN_CMD_TBTT_WAKEUP_START;
1158                 break;
1159
1160             case WLAN_CMD_BECON_SEND:
1161                 pDevice->eCommandState = WLAN_CMD_BECON_SEND_START;
1162                 break;
1163
1164             case WLAN_CMD_SETPOWER:
1165                 pDevice->eCommandState = WLAN_CMD_SETPOWER_START;
1166                 break;
1167
1168             case WLAN_CMD_CHANGE_ANTENNA:
1169                 pDevice->eCommandState = WLAN_CMD_CHANGE_ANTENNA_START;
1170                 break;
1171
1172             case WLAN_CMD_REMOVE_ALLKEY:
1173                 pDevice->eCommandState = WLAN_CMD_REMOVE_ALLKEY_START;
1174                 break;
1175
1176             case WLAN_CMD_MAC_DISPOWERSAVING:
1177                 pDevice->eCommandState = WLAN_CMD_MAC_DISPOWERSAVING_START;
1178                 break;
1179
1180             case WLAN_CMD_11H_CHSW:
1181                 pDevice->eCommandState = WLAN_CMD_11H_CHSW_START;
1182                 break;
1183
1184             default:
1185                 break;
1186
1187         }
1188         vCommandTimerWait((void *) pDevice, 0);
1189     }
1190
1191     return TRUE;
1192 }
1193
1194 BOOL bScheduleCommand(void *hDeviceContext,
1195                       CMD_CODE eCommand,
1196                       PBYTE pbyItem0)
1197 {
1198     PSDevice        pDevice = (PSDevice)hDeviceContext;
1199
1200
1201     if (pDevice->cbFreeCmdQueue == 0) {
1202         return (FALSE);
1203     }
1204     pDevice->eCmdQueue[pDevice->uCmdEnqueueIdx].eCmd = eCommand;
1205     pDevice->eCmdQueue[pDevice->uCmdEnqueueIdx].bForceSCAN = TRUE;
1206     memset(pDevice->eCmdQueue[pDevice->uCmdEnqueueIdx].abyCmdDesireSSID, 0 , WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
1207     if (pbyItem0 != NULL) {
1208         switch (eCommand) {
1209             case WLAN_CMD_BSSID_SCAN:
1210                 pDevice->eCmdQueue[pDevice->uCmdEnqueueIdx].bForceSCAN = FALSE;
1211                 memcpy(pDevice->eCmdQueue[pDevice->uCmdEnqueueIdx].abyCmdDesireSSID,
1212                          pbyItem0, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
1213                 break;
1214
1215             case WLAN_CMD_SSID:
1216                 memcpy(pDevice->eCmdQueue[pDevice->uCmdEnqueueIdx].abyCmdDesireSSID,
1217                          pbyItem0, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
1218                 break;
1219
1220             case WLAN_CMD_DISASSOCIATE:
1221                 pDevice->eCmdQueue[pDevice->uCmdEnqueueIdx].bNeedRadioOFF = *((int *)pbyItem0);
1222                 break;
1223 /*
1224             case WLAN_CMD_DEAUTH:
1225                 pDevice->eCmdQueue[pDevice->uCmdEnqueueIdx].wDeAuthenReason = *((PWORD)pbyItem0);
1226                 break;
1227 */
1228
1229             case WLAN_CMD_RADIO:
1230                 pDevice->eCmdQueue[pDevice->uCmdEnqueueIdx].bRadioCmd = *((int *)pbyItem0);
1231                 break;
1232
1233             default:
1234                 break;
1235         }
1236     }
1237
1238     ADD_ONE_WITH_WRAP_AROUND(pDevice->uCmdEnqueueIdx, CMD_Q_SIZE);
1239     pDevice->cbFreeCmdQueue--;
1240
1241     if (pDevice->bCmdRunning == FALSE) {
1242         s_bCommandComplete(pDevice);
1243     }
1244     else {
1245     }
1246     return (TRUE);
1247
1248 }
1249
1250 /*
1251  * Description:
1252  *      Clear BSSID_SCAN cmd in CMD Queue
1253  *
1254  * Parameters:
1255  *  In:
1256  *      hDeviceContext  - Pointer to the adapter
1257  *      eCommand        - Command
1258  *  Out:
1259  *      none
1260  *
1261  * Return Value: TRUE if success; otherwise FALSE
1262  *
1263  */
1264 static BOOL s_bClearBSSID_SCAN(void *hDeviceContext)
1265 {
1266     PSDevice        pDevice = (PSDevice)hDeviceContext;
1267     unsigned int            uCmdDequeueIdx = pDevice->uCmdDequeueIdx;
1268     unsigned int            ii;
1269
1270     if ((pDevice->cbFreeCmdQueue < CMD_Q_SIZE) && (uCmdDequeueIdx != pDevice->uCmdEnqueueIdx)) {
1271         for (ii = 0; ii < (CMD_Q_SIZE - pDevice->cbFreeCmdQueue); ii ++) {
1272             if (pDevice->eCmdQueue[uCmdDequeueIdx].eCmd == WLAN_CMD_BSSID_SCAN)
1273                 pDevice->eCmdQueue[uCmdDequeueIdx].eCmd = WLAN_CMD_IDLE;
1274             ADD_ONE_WITH_WRAP_AROUND(uCmdDequeueIdx, CMD_Q_SIZE);
1275             if (uCmdDequeueIdx == pDevice->uCmdEnqueueIdx)
1276                 break;
1277         }
1278     }
1279     return TRUE;
1280 }
1281
1282
1283 //mike add:reset command timer
1284 void vResetCommandTimer(void *hDeviceContext)
1285 {
1286   PSDevice        pDevice = (PSDevice)hDeviceContext;
1287
1288   //delete timer
1289       del_timer(&pDevice->sTimerCommand);
1290   //init timer
1291       init_timer(&pDevice->sTimerCommand);
1292     pDevice->sTimerCommand.data = (unsigned long)pDevice;
1293     pDevice->sTimerCommand.function = (TimerFunction)vRunCommand;
1294     pDevice->sTimerCommand.expires = RUN_AT(HZ);
1295     pDevice->cbFreeCmdQueue = CMD_Q_SIZE;
1296     pDevice->uCmdDequeueIdx = 0;
1297     pDevice->uCmdEnqueueIdx = 0;
1298     pDevice->eCommandState = WLAN_CMD_IDLE;
1299     pDevice->bCmdRunning = FALSE;
1300     pDevice->bCmdClear = FALSE;
1301 }
1302
1303 //2007-0115-08<Add>by MikeLiu
1304 #ifdef TxInSleep
1305 void BSSvSecondTxData(void *hDeviceContext)
1306 {
1307   PSDevice        pDevice = (PSDevice)hDeviceContext;
1308   PSMgmtObject  pMgmt = &(pDevice->sMgmtObj);
1309
1310   pDevice->nTxDataTimeCout++;
1311
1312   if(pDevice->nTxDataTimeCout<4)     //don't tx data if timer less than 40s
1313     {
1314      // printk("mike:%s-->no data Tx not exceed the desired Time as %d\n",__FUNCTION__,
1315         //      (int)pDevice->nTxDataTimeCout);
1316      pDevice->sTimerTxData.expires = RUN_AT(10*HZ);      //10s callback
1317      add_timer(&pDevice->sTimerTxData);
1318       return;
1319     }
1320
1321   spin_lock_irq(&pDevice->lock);
1322   //is wap_supplicant running successful OR only open && sharekey mode!
1323   #if 1
1324   if(((pDevice->bLinkPass ==TRUE)&&(pMgmt->eAuthenMode < WMAC_AUTH_WPA)) ||  //open && sharekey linking
1325       (pDevice->fWPA_Authened == TRUE)) {   //wpa linking
1326  #else
1327   if(pDevice->bLinkPass ==TRUE) {
1328  #endif
1329         //   printk("mike:%s-->InSleep Tx Data Procedure\n",__FUNCTION__);
1330           pDevice->fTxDataInSleep = TRUE;
1331           PSbSendNullPacket(pDevice);      //send null packet
1332           pDevice->fTxDataInSleep = FALSE;
1333         }
1334   spin_unlock_irq(&pDevice->lock);
1335
1336   pDevice->sTimerTxData.expires = RUN_AT(10*HZ);      //10s callback
1337   add_timer(&pDevice->sTimerTxData);
1338   return;
1339 }
1340 #endif
1341