Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/signal
[cascardo/linux.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 failure
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         union iwreq_data wrqu;
346
347
348     if (pDevice->dwDiagRefCount != 0)
349         return;
350     if (pDevice->bCmdRunning != TRUE)
351         return;
352
353     spin_lock_irq(&pDevice->lock);
354
355     switch ( pDevice->eCommandState ) {
356
357         case WLAN_CMD_SCAN_START:
358
359                 pDevice->byReAssocCount = 0;
360             if (pDevice->bRadioOff == TRUE) {
361                 s_bCommandComplete(pDevice);
362                 spin_unlock_irq(&pDevice->lock);
363                 return;
364             }
365
366             if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
367                 s_bCommandComplete(pDevice);
368                 spin_unlock_irq(&pDevice->lock);
369                 return;
370             }
371
372             pItemSSID = (PWLAN_IE_SSID)pMgmt->abyScanSSID;
373
374             if (pMgmt->uScanChannel == 0 ) {
375                 pMgmt->uScanChannel = pDevice->byMinChannel;
376             }
377             if (pMgmt->uScanChannel > pDevice->byMaxChannel) {
378                 pMgmt->eScanState = WMAC_NO_SCANNING;
379
380                 if (pDevice->byBBType != pDevice->byScanBBType) {
381                     pDevice->byBBType = pDevice->byScanBBType;
382                     CARDvSetBSSMode(pDevice);
383                 }
384
385                 if (pDevice->bUpdateBBVGA) {
386                     BBvSetShortSlotTime(pDevice);
387                     BBvSetVGAGainOffset(pDevice, pDevice->byBBVGACurrent);
388                     BBvUpdatePreEDThreshold(pDevice, FALSE);
389                 }
390                 // Set channel back
391                 vAdHocBeaconRestart(pDevice);
392                 // Set channel back
393                 CARDbSetMediaChannel(pDevice, pMgmt->uCurrChannel);
394                 // Set Filter
395                 if (pMgmt->bCurrBSSIDFilterOn) {
396                     MACvRegBitsOn(pDevice, MAC_REG_RCR, RCR_BSSID);
397                     pDevice->byRxMode |= RCR_BSSID;
398                 }
399                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Scanning, set back to channel: [%d]\n", pMgmt->uCurrChannel);
400                 pDevice->bStopDataPkt = FALSE;
401                 s_bCommandComplete(pDevice);
402                 spin_unlock_irq(&pDevice->lock);
403                 return;
404
405             } else {
406                 if (!ChannelValid(pDevice->byZoneType, pMgmt->uScanChannel)) {
407                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Invalid channel pMgmt->uScanChannel = %d \n",pMgmt->uScanChannel);
408                     s_bCommandComplete(pDevice);
409                     spin_unlock_irq(&pDevice->lock);
410                     return;
411                 }
412                 if (pMgmt->uScanChannel == pDevice->byMinChannel) {
413                    // pMgmt->eScanType = WMAC_SCAN_ACTIVE;          //mike mark
414                     pMgmt->abyScanBSSID[0] = 0xFF;
415                     pMgmt->abyScanBSSID[1] = 0xFF;
416                     pMgmt->abyScanBSSID[2] = 0xFF;
417                     pMgmt->abyScanBSSID[3] = 0xFF;
418                     pMgmt->abyScanBSSID[4] = 0xFF;
419                     pMgmt->abyScanBSSID[5] = 0xFF;
420                     pItemSSID->byElementID = WLAN_EID_SSID;
421                     // clear bssid list
422                     /* BSSvClearBSSList((void *) pDevice,
423                        pDevice->bLinkPass); */
424                     pMgmt->eScanState = WMAC_IS_SCANNING;
425                     pDevice->byScanBBType = pDevice->byBBType;  //lucas
426                     pDevice->bStopDataPkt = TRUE;
427                     // Turn off RCR_BSSID filter every time
428                     MACvRegBitsOff(pDevice, MAC_REG_RCR, RCR_BSSID);
429                     pDevice->byRxMode &= ~RCR_BSSID;
430
431                 }
432                 //lucas
433                 vAdHocBeaconStop(pDevice);
434                 if ((pDevice->byBBType != BB_TYPE_11A) && (pMgmt->uScanChannel > CB_MAX_CHANNEL_24G)) {
435                     pDevice->byBBType = BB_TYPE_11A;
436                     CARDvSetBSSMode(pDevice);
437                 }
438                 else if ((pDevice->byBBType == BB_TYPE_11A) && (pMgmt->uScanChannel <= CB_MAX_CHANNEL_24G)) {
439                     pDevice->byBBType = BB_TYPE_11G;
440                     CARDvSetBSSMode(pDevice);
441                 }
442                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Scanning....  channel: [%d]\n", pMgmt->uScanChannel);
443                 // Set channel
444                 CARDbSetMediaChannel(pDevice, pMgmt->uScanChannel);
445                 // Set Baseband to be more sensitive.
446
447                 if (pDevice->bUpdateBBVGA) {
448                     BBvSetShortSlotTime(pDevice);
449                     BBvSetVGAGainOffset(pDevice, pDevice->abyBBVGA[0]);
450                     BBvUpdatePreEDThreshold(pDevice, TRUE);
451                 }
452                 pMgmt->uScanChannel++;
453
454                 while (!ChannelValid(pDevice->byZoneType, pMgmt->uScanChannel) &&
455                         pMgmt->uScanChannel <= pDevice->byMaxChannel ){
456                     pMgmt->uScanChannel++;
457                 }
458
459                 if (pMgmt->uScanChannel > pDevice->byMaxChannel) {
460                     // Set Baseband to be not sensitive and rescan
461                     pDevice->eCommandState = WLAN_CMD_SCAN_END;
462
463                 }
464                 if ((pMgmt->b11hEnable == FALSE) ||
465                     (pMgmt->uScanChannel < CB_MAX_CHANNEL_24G)) {
466                     s_vProbeChannel(pDevice);
467                     spin_unlock_irq(&pDevice->lock);
468                      vCommandTimerWait((void *) pDevice, 100);
469                     return;
470                 } else {
471                     spin_unlock_irq(&pDevice->lock);
472                     vCommandTimerWait((void *) pDevice, WCMD_PASSIVE_SCAN_TIME);
473                     return;
474                 }
475
476             }
477
478             break;
479
480         case WLAN_CMD_SCAN_END:
481
482             // Set Baseband's sensitivity back.
483             if (pDevice->byBBType != pDevice->byScanBBType) {
484                 pDevice->byBBType = pDevice->byScanBBType;
485                 CARDvSetBSSMode(pDevice);
486             }
487
488             if (pDevice->bUpdateBBVGA) {
489                 BBvSetShortSlotTime(pDevice);
490                 BBvSetVGAGainOffset(pDevice, pDevice->byBBVGACurrent);
491                 BBvUpdatePreEDThreshold(pDevice, FALSE);
492             }
493
494             // Set channel back
495             vAdHocBeaconRestart(pDevice);
496             // Set channel back
497             CARDbSetMediaChannel(pDevice, pMgmt->uCurrChannel);
498             // Set Filter
499             if (pMgmt->bCurrBSSIDFilterOn) {
500                 MACvRegBitsOn(pDevice, MAC_REG_RCR, RCR_BSSID);
501                 pDevice->byRxMode |= RCR_BSSID;
502             }
503             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Scanning, set back to channel: [%d]\n", pMgmt->uCurrChannel);
504             pMgmt->eScanState = WMAC_NO_SCANNING;
505             pDevice->bStopDataPkt = FALSE;
506
507                 /*send scan event to wpa_Supplicant*/
508                 PRINT_K("wireless_send_event--->SIOCGIWSCAN(scan done)\n");
509                 memset(&wrqu, 0, sizeof(wrqu));
510                 wireless_send_event(pDevice->dev, SIOCGIWSCAN, &wrqu, NULL);
511
512             s_bCommandComplete(pDevice);
513             break;
514
515         case WLAN_CMD_DISASSOCIATE_START :
516                 pDevice->byReAssocCount = 0;
517             if ((pMgmt->eCurrMode == WMAC_MODE_ESS_STA) &&
518                 (pMgmt->eCurrState != WMAC_STATE_ASSOC)) {
519                 s_bCommandComplete(pDevice);
520                 spin_unlock_irq(&pDevice->lock);
521                 return;
522             } else {
523
524                       pDevice->bwextstep0 = FALSE;
525                         pDevice->bwextstep1 = FALSE;
526                         pDevice->bwextstep2 = FALSE;
527                         pDevice->bwextstep3 = FALSE;
528                    pDevice->bWPASuppWextEnabled = FALSE;
529                    pDevice->fWPA_Authened = FALSE;
530
531                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Send Disassociation Packet..\n");
532                 // reason = 8 : disassoc because sta has left
533                 vMgrDisassocBeginSta((void *) pDevice,
534                                      pMgmt,
535                                      pMgmt->abyCurrBSSID,
536                                      (8),
537                                      &Status);
538                 pDevice->bLinkPass = FALSE;
539                 ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_SLOW);
540                 // unlock command busy
541                 pItemSSID = (PWLAN_IE_SSID)pMgmt->abyCurrSSID;
542                 pItemSSID->len = 0;
543                 memset(pItemSSID->abySSID, 0, WLAN_SSID_MAXLEN);
544                 pMgmt->eCurrState = WMAC_STATE_IDLE;
545                 pMgmt->sNodeDBTable[0].bActive = FALSE;
546 //                pDevice->bBeaconBufReady = FALSE;
547             }
548             netif_stop_queue(pDevice->dev);
549             if (pDevice->bNeedRadioOFF == TRUE)
550                 CARDbRadioPowerOff(pDevice);
551             s_bCommandComplete(pDevice);
552             break;
553
554
555         case WLAN_CMD_SSID_START:
556
557                 pDevice->byReAssocCount = 0;
558             if (pDevice->bRadioOff == TRUE) {
559                 s_bCommandComplete(pDevice);
560                 spin_unlock_irq(&pDevice->lock);
561                 return;
562             }
563
564             memcpy(pMgmt->abyAdHocSSID,pMgmt->abyDesireSSID,
565                               ((PWLAN_IE_SSID)pMgmt->abyDesireSSID)->len + WLAN_IEHDR_LEN);
566
567             pItemSSID = (PWLAN_IE_SSID)pMgmt->abyDesireSSID;
568             pItemSSIDCurr = (PWLAN_IE_SSID)pMgmt->abyCurrSSID;
569             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" cmd: desire ssid = %s\n", pItemSSID->abySSID);
570             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" cmd: curr ssid = %s\n", pItemSSIDCurr->abySSID);
571
572             if (pMgmt->eCurrState == WMAC_STATE_ASSOC) {
573                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" Cmd pMgmt->eCurrState == WMAC_STATE_ASSOC\n");
574                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" pItemSSID->len =%d\n",pItemSSID->len);
575                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" pItemSSIDCurr->len = %d\n",pItemSSIDCurr->len);
576                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" desire ssid = %s\n", pItemSSID->abySSID);
577                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" curr ssid = %s\n", pItemSSIDCurr->abySSID);
578             }
579
580             if ((pMgmt->eCurrState == WMAC_STATE_ASSOC) ||
581                 ((pMgmt->eCurrMode == WMAC_MODE_IBSS_STA)&& (pMgmt->eCurrState == WMAC_STATE_JOINTED))) {
582
583                 if (pItemSSID->len == pItemSSIDCurr->len) {
584                     if (memcmp(pItemSSID->abySSID, pItemSSIDCurr->abySSID, pItemSSID->len) == 0) {
585                         s_bCommandComplete(pDevice);
586                         spin_unlock_irq(&pDevice->lock);
587                         return;
588                     }
589                 }
590                 netif_stop_queue(pDevice->dev);
591                 pDevice->bLinkPass = FALSE;
592                 ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_SLOW);
593             }
594             // set initial state
595             pMgmt->eCurrState = WMAC_STATE_IDLE;
596             pMgmt->eCurrMode = WMAC_MODE_STANDBY;
597             PSvDisablePowerSaving((void *) pDevice);
598             BSSvClearNodeDBTable(pDevice, 0);
599             vMgrJoinBSSBegin((void *) pDevice, &Status);
600             // if Infra mode
601             if ((pMgmt->eCurrMode == WMAC_MODE_ESS_STA) && (pMgmt->eCurrState == WMAC_STATE_JOINTED)) {
602                 // Call mgr to begin the deauthentication
603                 // reason = (3) because sta has left ESS
604               if (pMgmt->eCurrState >= WMAC_STATE_AUTH) {
605                 vMgrDeAuthenBeginSta((void *)pDevice,
606                                      pMgmt,
607                                      pMgmt->abyCurrBSSID,
608                                      (3),
609                                      &Status);
610               }
611                 // Call mgr to begin the authentication
612                 vMgrAuthenBeginSta((void *) pDevice, pMgmt, &Status);
613                 if (Status == CMD_STATUS_SUCCESS) {
614                    pDevice->byLinkWaitCount = 0;
615                     pDevice->eCommandState = WLAN_AUTHENTICATE_WAIT;
616                     vCommandTimerWait((void *) pDevice, AUTHENTICATE_TIMEOUT);
617                     spin_unlock_irq(&pDevice->lock);
618                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" Set eCommandState = WLAN_AUTHENTICATE_WAIT\n");
619                     return;
620                 }
621             }
622             // if Adhoc mode
623             else if (pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) {
624                 if (pMgmt->eCurrState == WMAC_STATE_JOINTED) {
625                     if (netif_queue_stopped(pDevice->dev)){
626                         netif_wake_queue(pDevice->dev);
627                     }
628                     pDevice->bLinkPass = TRUE;
629                     ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_INTER);
630                     pMgmt->sNodeDBTable[0].bActive = TRUE;
631                     pMgmt->sNodeDBTable[0].uInActiveCount = 0;
632                 }
633                 else {
634                     // start own IBSS
635                     DBG_PRT(MSG_LEVEL_DEBUG,
636                             KERN_INFO "CreateOwn IBSS by CurrMode = IBSS_STA\n");
637                     vMgrCreateOwnIBSS((void *) pDevice, &Status);
638                     if (Status != CMD_STATUS_SUCCESS){
639                         DBG_PRT(MSG_LEVEL_DEBUG,
640                                 KERN_INFO "WLAN_CMD_IBSS_CREATE fail!\n");
641                     }
642                     BSSvAddMulticastNode(pDevice);
643                 }
644                 s_bClearBSSID_SCAN(pDevice);
645             }
646             // if SSID not found
647             else if (pMgmt->eCurrMode == WMAC_MODE_STANDBY) {
648                 if (pMgmt->eConfigMode == WMAC_CONFIG_IBSS_STA ||
649                     pMgmt->eConfigMode == WMAC_CONFIG_AUTO) {
650                     // start own IBSS
651                         DBG_PRT(MSG_LEVEL_DEBUG,
652                                 KERN_INFO "CreateOwn IBSS by CurrMode = STANDBY\n");
653                     vMgrCreateOwnIBSS((void *) pDevice, &Status);
654                     if (Status != CMD_STATUS_SUCCESS){
655                         DBG_PRT(MSG_LEVEL_DEBUG,
656                                 KERN_INFO "WLAN_CMD_IBSS_CREATE fail!\n");
657                     }
658                     BSSvAddMulticastNode(pDevice);
659                     s_bClearBSSID_SCAN(pDevice);
660 /*
661                     pDevice->bLinkPass = TRUE;
662                     ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_INTER);
663                     if (netif_queue_stopped(pDevice->dev)){
664                         netif_wake_queue(pDevice->dev);
665                     }
666                     s_bClearBSSID_SCAN(pDevice);
667 */
668                 }
669                 else {
670                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Disconnect SSID none\n");
671                     // if(pDevice->bWPASuppWextEnabled == TRUE)
672                         {
673                         union iwreq_data  wrqu;
674                         memset(&wrqu, 0, sizeof (wrqu));
675                           wrqu.ap_addr.sa_family = ARPHRD_ETHER;
676                         PRINT_K("wireless_send_event--->SIOCGIWAP(disassociated:vMgrJoinBSSBegin Fail !!)\n");
677                         wireless_send_event(pDevice->dev, SIOCGIWAP, &wrqu, NULL);
678                        }
679                 }
680             }
681             s_bCommandComplete(pDevice);
682             break;
683
684         case WLAN_AUTHENTICATE_WAIT :
685             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"eCommandState == WLAN_AUTHENTICATE_WAIT\n");
686             if (pMgmt->eCurrState == WMAC_STATE_AUTH) {
687                 pDevice->byLinkWaitCount = 0;
688                 // Call mgr to begin the association
689                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"eCurrState == WMAC_STATE_AUTH\n");
690                 vMgrAssocBeginSta((void *) pDevice, pMgmt, &Status);
691                 if (Status == CMD_STATUS_SUCCESS) {
692                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"eCommandState = WLAN_ASSOCIATE_WAIT\n");
693                   pDevice->byLinkWaitCount = 0;
694                     pDevice->eCommandState = WLAN_ASSOCIATE_WAIT;
695                     vCommandTimerWait((void *) pDevice, ASSOCIATE_TIMEOUT);
696                     spin_unlock_irq(&pDevice->lock);
697                     return;
698                 }
699             }
700            else if(pMgmt->eCurrState < WMAC_STATE_AUTHPENDING) {
701                printk("WLAN_AUTHENTICATE_WAIT:Authen Fail???\n");
702            }
703            else  if(pDevice->byLinkWaitCount <= 4){    //mike add:wait another 2 sec if authenticated_frame delay!
704                 pDevice->byLinkWaitCount ++;
705                printk("WLAN_AUTHENTICATE_WAIT:wait %d times!!\n",pDevice->byLinkWaitCount);
706                spin_unlock_irq(&pDevice->lock);
707                vCommandTimerWait((void *) pDevice, AUTHENTICATE_TIMEOUT/2);
708                return;
709            }
710                   pDevice->byLinkWaitCount = 0;
711
712             s_bCommandComplete(pDevice);
713             break;
714
715         case WLAN_ASSOCIATE_WAIT :
716             if (pMgmt->eCurrState == WMAC_STATE_ASSOC) {
717                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"eCurrState == WMAC_STATE_ASSOC\n");
718                 if (pDevice->ePSMode != WMAC_POWER_CAM) {
719                         PSvEnablePowerSaving((void *) pDevice,
720                                              pMgmt->wListenInterval);
721                 }
722 /*
723                 if (pMgmt->eAuthenMode >= WMAC_AUTH_WPA) {
724                     KeybRemoveAllKey(pDevice, &(pDevice->sKey), pDevice->abyBSSID);
725                 }
726 */
727                 pDevice->byLinkWaitCount = 0;
728                 pDevice->byReAssocCount = 0;
729                 pDevice->bLinkPass = TRUE;
730                 ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_INTER);
731                 s_bClearBSSID_SCAN(pDevice);
732
733                 if (netif_queue_stopped(pDevice->dev)){
734                     netif_wake_queue(pDevice->dev);
735                 }
736
737                  if(pDevice->IsTxDataTrigger != FALSE)   {    //TxDataTimer is not triggered at the first time
738                      // printk("Re-initial TxDataTimer****\n");
739                     del_timer(&pDevice->sTimerTxData);
740                       init_timer(&pDevice->sTimerTxData);
741                         pDevice->sTimerTxData.data = (unsigned long) pDevice;
742                       pDevice->sTimerTxData.function = (TimerFunction)BSSvSecondTxData;
743                       pDevice->sTimerTxData.expires = RUN_AT(10*HZ);      //10s callback
744                       pDevice->fTxDataInSleep = FALSE;
745                       pDevice->nTxDataTimeCout = 0;
746                  }
747                  else {
748                    // printk("mike:-->First time trigger TimerTxData InSleep\n");
749                  }
750                 pDevice->IsTxDataTrigger = TRUE;
751                 add_timer(&pDevice->sTimerTxData);
752
753             }
754            else if(pMgmt->eCurrState < WMAC_STATE_ASSOCPENDING) {
755                printk("WLAN_ASSOCIATE_WAIT:Association Fail???\n");
756            }
757            else  if(pDevice->byLinkWaitCount <= 4){    //mike add:wait another 2 sec if associated_frame delay!
758                 pDevice->byLinkWaitCount ++;
759                printk("WLAN_ASSOCIATE_WAIT:wait %d times!!\n",pDevice->byLinkWaitCount);
760                spin_unlock_irq(&pDevice->lock);
761                vCommandTimerWait((void *) pDevice, ASSOCIATE_TIMEOUT/2);
762                return;
763            }
764                   pDevice->byLinkWaitCount = 0;
765
766             s_bCommandComplete(pDevice);
767             break;
768
769         case WLAN_CMD_AP_MODE_START :
770             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"eCommandState == WLAN_CMD_AP_MODE_START\n");
771
772             if (pMgmt->eConfigMode == WMAC_CONFIG_AP) {
773                 del_timer(&pMgmt->sTimerSecondCallback);
774                 pMgmt->eCurrState = WMAC_STATE_IDLE;
775                 pMgmt->eCurrMode = WMAC_MODE_STANDBY;
776                 pDevice->bLinkPass = FALSE;
777                 ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_SLOW);
778                 if (pDevice->bEnableHostWEP == TRUE)
779                     BSSvClearNodeDBTable(pDevice, 1);
780                 else
781                     BSSvClearNodeDBTable(pDevice, 0);
782                 pDevice->uAssocCount = 0;
783                 pMgmt->eCurrState = WMAC_STATE_IDLE;
784                 pDevice->bFixRate = FALSE;
785
786                 vMgrCreateOwnIBSS((void *) pDevice, &Status);
787                 if (Status != CMD_STATUS_SUCCESS) {
788                         DBG_PRT(MSG_LEVEL_DEBUG,
789                                 KERN_INFO "vMgrCreateOwnIBSS fail!\n");
790                 }
791                 // always turn off unicast bit
792                 MACvRegBitsOff(pDevice, MAC_REG_RCR, RCR_UNICAST);
793                 pDevice->byRxMode &= ~RCR_UNICAST;
794                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "wcmd: rx_mode = %x\n", pDevice->byRxMode );
795                 BSSvAddMulticastNode(pDevice);
796                 if (netif_queue_stopped(pDevice->dev)){
797                     netif_wake_queue(pDevice->dev);
798                 }
799                 pDevice->bLinkPass = TRUE;
800                 ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_INTER);
801                 add_timer(&pMgmt->sTimerSecondCallback);
802             }
803             s_bCommandComplete(pDevice);
804             break;
805
806         case WLAN_CMD_TX_PSPACKET_START :
807             // DTIM Multicast tx
808             if (pMgmt->sNodeDBTable[0].bRxPSPoll) {
809                 while ((skb = skb_dequeue(&pMgmt->sNodeDBTable[0].sTxPSQueue)) != NULL) {
810                     if (skb_queue_empty(&pMgmt->sNodeDBTable[0].sTxPSQueue)) {
811                         pMgmt->abyPSTxMap[0] &= ~byMask[0];
812                         pDevice->bMoreData = FALSE;
813                     }
814                     else {
815                         pDevice->bMoreData = TRUE;
816                     }
817
818                     if (nsDMA_tx_packet(pDevice, TYPE_AC0DMA, skb) != 0) {
819                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Multicast ps tx fail \n");
820                     }
821
822                     pMgmt->sNodeDBTable[0].wEnQueueCnt--;
823                 }
824             }
825
826             // PS nodes tx
827             for (ii = 1; ii < (MAX_NODE_NUM + 1); ii++) {
828                 if (pMgmt->sNodeDBTable[ii].bActive &&
829                     pMgmt->sNodeDBTable[ii].bRxPSPoll) {
830                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Index=%d Enqueu Cnt= %d\n",
831                                ii, pMgmt->sNodeDBTable[ii].wEnQueueCnt);
832                     while ((skb = skb_dequeue(&pMgmt->sNodeDBTable[ii].sTxPSQueue)) != NULL) {
833                         if (skb_queue_empty(&pMgmt->sNodeDBTable[ii].sTxPSQueue)) {
834                             // clear tx map
835                             pMgmt->abyPSTxMap[pMgmt->sNodeDBTable[ii].wAID >> 3] &=
836                                     ~byMask[pMgmt->sNodeDBTable[ii].wAID & 7];
837                             pDevice->bMoreData = FALSE;
838                         }
839                         else {
840                             pDevice->bMoreData = TRUE;
841                         }
842
843                         if (nsDMA_tx_packet(pDevice, TYPE_AC0DMA, skb) != 0) {
844                             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "sta ps tx fail \n");
845                         }
846
847                         pMgmt->sNodeDBTable[ii].wEnQueueCnt--;
848                         // check if sta ps enable, wait next pspoll
849                         // if sta ps disable, send all pending buffers.
850                         if (pMgmt->sNodeDBTable[ii].bPSEnable)
851                             break;
852                     }
853                     if (skb_queue_empty(&pMgmt->sNodeDBTable[ii].sTxPSQueue)) {
854                         // clear tx map
855                         pMgmt->abyPSTxMap[pMgmt->sNodeDBTable[ii].wAID >> 3] &=
856                                     ~byMask[pMgmt->sNodeDBTable[ii].wAID & 7];
857                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Index=%d PS queue clear \n", ii);
858                     }
859                     pMgmt->sNodeDBTable[ii].bRxPSPoll = FALSE;
860                 }
861             }
862
863             s_bCommandComplete(pDevice);
864             break;
865
866         case WLAN_CMD_RADIO_START:
867
868             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"eCommandState == WLAN_CMD_RADIO_START\n");
869        //     if (pDevice->bRadioCmd == TRUE)
870        //         CARDbRadioPowerOn(pDevice);
871        //     else
872        //         CARDbRadioPowerOff(pDevice);
873
874        {
875                int ntStatus = STATUS_SUCCESS;
876         BYTE            byTmp;
877
878         ntStatus = CONTROLnsRequestIn(pDevice,
879                                     MESSAGE_TYPE_READ,
880                                     MAC_REG_GPIOCTL1,
881                                     MESSAGE_REQUEST_MACREG,
882                                     1,
883                                     &byTmp);
884
885         if ( ntStatus != STATUS_SUCCESS ) {
886                 s_bCommandComplete(pDevice);
887                 spin_unlock_irq(&pDevice->lock);
888                 return;
889         }
890         if ( (byTmp & GPIO3_DATA) == 0 ) {
891             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" WLAN_CMD_RADIO_START_OFF........................\n");
892                 // Old commands are useless.
893                 // empty command Q
894                pDevice->cbFreeCmdQueue = CMD_Q_SIZE;
895                 pDevice->uCmdDequeueIdx = 0;
896                 pDevice->uCmdEnqueueIdx = 0;
897                 //0415pDevice->bCmdRunning = FALSE;
898                 pDevice->bCmdClear = TRUE;
899                 pDevice->bStopTx0Pkt = FALSE;
900                 pDevice->bStopDataPkt = TRUE;
901
902                 pDevice->byKeyIndex = 0;
903                 pDevice->bTransmitKey = FALSE;
904             spin_unlock_irq(&pDevice->lock);
905             KeyvInitTable(pDevice,&pDevice->sKey);
906             spin_lock_irq(&pDevice->lock);
907                pMgmt->byCSSPK = KEY_CTL_NONE;
908                 pMgmt->byCSSGK = KEY_CTL_NONE;
909
910           if (pDevice->bLinkPass == TRUE) {
911                 // reason = 8 : disassoc because sta has left
912                 vMgrDisassocBeginSta((void *) pDevice,
913                                      pMgmt,
914                                      pMgmt->abyCurrBSSID,
915                                      (8),
916                                      &Status);
917                        pDevice->bLinkPass = FALSE;
918                 // unlock command busy
919                         pMgmt->eCurrState = WMAC_STATE_IDLE;
920                         pMgmt->sNodeDBTable[0].bActive = FALSE;
921                     // if(pDevice->bWPASuppWextEnabled == TRUE)
922                         {
923                         union iwreq_data  wrqu;
924                         memset(&wrqu, 0, sizeof (wrqu));
925                           wrqu.ap_addr.sa_family = ARPHRD_ETHER;
926                         PRINT_K("wireless_send_event--->SIOCGIWAP(disassociated)\n");
927                         wireless_send_event(pDevice->dev, SIOCGIWAP, &wrqu, NULL);
928                        }
929                 }
930                        pDevice->bwextstep0 = FALSE;
931                         pDevice->bwextstep1 = FALSE;
932                         pDevice->bwextstep2 = FALSE;
933                         pDevice->bwextstep3 = FALSE;
934                       pDevice->bWPASuppWextEnabled = FALSE;
935                           //clear current SSID
936                   pItemSSID = (PWLAN_IE_SSID)pMgmt->abyCurrSSID;
937                   pItemSSID->len = 0;
938                   memset(pItemSSID->abySSID, 0, WLAN_SSID_MAXLEN);
939                 //clear desired SSID
940                 pItemSSID = (PWLAN_IE_SSID)pMgmt->abyDesireSSID;
941                 pItemSSID->len = 0;
942                 memset(pItemSSID->abySSID, 0, WLAN_SSID_MAXLEN);
943
944             netif_stop_queue(pDevice->dev);
945             CARDbRadioPowerOff(pDevice);
946              MACvRegBitsOn(pDevice,MAC_REG_GPIOCTL1,GPIO3_INTMD);
947             ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_OFF);
948             pDevice->bHWRadioOff = TRUE;
949         } else {
950             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" WLAN_CMD_RADIO_START_ON........................\n");
951             pDevice->bHWRadioOff = FALSE;
952                 CARDbRadioPowerOn(pDevice);
953             MACvRegBitsOff(pDevice,MAC_REG_GPIOCTL1,GPIO3_INTMD);
954             ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_ON);
955         }
956       }
957
958             s_bCommandComplete(pDevice);
959             break;
960
961
962         case WLAN_CMD_CHANGE_BBSENSITIVITY_START:
963
964             pDevice->bStopDataPkt = TRUE;
965             pDevice->byBBVGACurrent = pDevice->byBBVGANew;
966             BBvSetVGAGainOffset(pDevice, pDevice->byBBVGACurrent);
967             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Change sensitivity pDevice->byBBVGACurrent = %x\n", pDevice->byBBVGACurrent);
968             pDevice->bStopDataPkt = FALSE;
969             s_bCommandComplete(pDevice);
970             break;
971
972         case WLAN_CMD_TBTT_WAKEUP_START:
973             PSbIsNextTBTTWakeUp(pDevice);
974             s_bCommandComplete(pDevice);
975             break;
976
977         case WLAN_CMD_BECON_SEND_START:
978             bMgrPrepareBeaconToSend(pDevice, pMgmt);
979             s_bCommandComplete(pDevice);
980             break;
981
982         case WLAN_CMD_SETPOWER_START:
983
984             RFbSetPower(pDevice, pDevice->wCurrentRate, pMgmt->uCurrChannel);
985
986             s_bCommandComplete(pDevice);
987             break;
988
989         case WLAN_CMD_CHANGE_ANTENNA_START:
990             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Change from Antenna%d to", (int)pDevice->dwRxAntennaSel);
991             if ( pDevice->dwRxAntennaSel == 0) {
992                 pDevice->dwRxAntennaSel=1;
993                 if (pDevice->bTxRxAntInv == TRUE)
994                     BBvSetAntennaMode(pDevice, ANT_RXA);
995                 else
996                     BBvSetAntennaMode(pDevice, ANT_RXB);
997             } else {
998                 pDevice->dwRxAntennaSel=0;
999                 if (pDevice->bTxRxAntInv == TRUE)
1000                     BBvSetAntennaMode(pDevice, ANT_RXB);
1001                 else
1002                     BBvSetAntennaMode(pDevice, ANT_RXA);
1003             }
1004             s_bCommandComplete(pDevice);
1005             break;
1006
1007         case WLAN_CMD_REMOVE_ALLKEY_START:
1008             KeybRemoveAllKey(pDevice, &(pDevice->sKey), pDevice->abyBSSID);
1009             s_bCommandComplete(pDevice);
1010             break;
1011
1012
1013         case WLAN_CMD_MAC_DISPOWERSAVING_START:
1014             ControlvReadByte (pDevice, MESSAGE_REQUEST_MACREG, MAC_REG_PSCTL, &byData);
1015             if ( (byData & PSCTL_PS) != 0 ) {
1016                 // disable power saving hw function
1017                 CONTROLnsRequestOut(pDevice,
1018                                 MESSAGE_TYPE_DISABLE_PS,
1019                                 0,
1020                                 0,
1021                                 0,
1022                                 NULL
1023                                 );
1024             }
1025             s_bCommandComplete(pDevice);
1026             break;
1027
1028         case WLAN_CMD_11H_CHSW_START:
1029             CARDbSetMediaChannel(pDevice, pDevice->byNewChannel);
1030             pDevice->bChannelSwitch = FALSE;
1031             pMgmt->uCurrChannel = pDevice->byNewChannel;
1032             pDevice->bStopDataPkt = FALSE;
1033             s_bCommandComplete(pDevice);
1034             break;
1035
1036         default:
1037             s_bCommandComplete(pDevice);
1038             break;
1039     } //switch
1040
1041     spin_unlock_irq(&pDevice->lock);
1042     return;
1043 }
1044
1045
1046 static
1047 BOOL
1048 s_bCommandComplete (
1049     PSDevice pDevice
1050     )
1051 {
1052     PWLAN_IE_SSID pSSID;
1053     BOOL          bRadioCmd = FALSE;
1054     //WORD          wDeAuthenReason = 0;
1055     BOOL          bForceSCAN = TRUE;
1056     PSMgmtObject  pMgmt = &(pDevice->sMgmtObj);
1057
1058
1059     pDevice->eCommandState = WLAN_CMD_IDLE;
1060     if (pDevice->cbFreeCmdQueue == CMD_Q_SIZE) {
1061         //Command Queue Empty
1062         pDevice->bCmdRunning = FALSE;
1063         return TRUE;
1064     }
1065     else {
1066         pDevice->eCommand = pDevice->eCmdQueue[pDevice->uCmdDequeueIdx].eCmd;
1067         pSSID = (PWLAN_IE_SSID)pDevice->eCmdQueue[pDevice->uCmdDequeueIdx].abyCmdDesireSSID;
1068         bRadioCmd = pDevice->eCmdQueue[pDevice->uCmdDequeueIdx].bRadioCmd;
1069         bForceSCAN = pDevice->eCmdQueue[pDevice->uCmdDequeueIdx].bForceSCAN;
1070         ADD_ONE_WITH_WRAP_AROUND(pDevice->uCmdDequeueIdx, CMD_Q_SIZE);
1071         pDevice->cbFreeCmdQueue++;
1072         pDevice->bCmdRunning = TRUE;
1073         switch ( pDevice->eCommand ) {
1074             case WLAN_CMD_BSSID_SCAN:
1075                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"eCommandState= WLAN_CMD_BSSID_SCAN\n");
1076                 pDevice->eCommandState = WLAN_CMD_SCAN_START;
1077                 pMgmt->uScanChannel = 0;
1078                 if (pSSID->len != 0) {
1079                     memcpy(pMgmt->abyScanSSID, pSSID, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
1080                 } else {
1081                     memset(pMgmt->abyScanSSID, 0, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
1082                 }
1083 /*
1084                 if ((bForceSCAN == FALSE) && (pDevice->bLinkPass == TRUE)) {
1085                     if ((pSSID->len == ((PWLAN_IE_SSID)pMgmt->abyCurrSSID)->len) &&
1086                         ( !memcmp(pSSID->abySSID, ((PWLAN_IE_SSID)pMgmt->abyCurrSSID)->abySSID, pSSID->len))) {
1087                         pDevice->eCommandState = WLAN_CMD_IDLE;
1088                     }
1089                 }
1090 */
1091                 break;
1092             case WLAN_CMD_SSID:
1093                 pDevice->eCommandState = WLAN_CMD_SSID_START;
1094                 if (pSSID->len > WLAN_SSID_MAXLEN)
1095                     pSSID->len = WLAN_SSID_MAXLEN;
1096                 if (pSSID->len != 0)
1097                     memcpy(pMgmt->abyDesireSSID, pSSID, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
1098                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"eCommandState= WLAN_CMD_SSID_START\n");
1099                 break;
1100             case WLAN_CMD_DISASSOCIATE:
1101                 pDevice->eCommandState = WLAN_CMD_DISASSOCIATE_START;
1102                 break;
1103             case WLAN_CMD_RX_PSPOLL:
1104                 pDevice->eCommandState = WLAN_CMD_TX_PSPACKET_START;
1105                 break;
1106             case WLAN_CMD_RUN_AP:
1107                 pDevice->eCommandState = WLAN_CMD_AP_MODE_START;
1108                 break;
1109             case WLAN_CMD_RADIO:
1110                 pDevice->eCommandState = WLAN_CMD_RADIO_START;
1111                 pDevice->bRadioCmd = bRadioCmd;
1112                 break;
1113             case WLAN_CMD_CHANGE_BBSENSITIVITY:
1114                 pDevice->eCommandState = WLAN_CMD_CHANGE_BBSENSITIVITY_START;
1115                 break;
1116
1117             case WLAN_CMD_TBTT_WAKEUP:
1118                 pDevice->eCommandState = WLAN_CMD_TBTT_WAKEUP_START;
1119                 break;
1120
1121             case WLAN_CMD_BECON_SEND:
1122                 pDevice->eCommandState = WLAN_CMD_BECON_SEND_START;
1123                 break;
1124
1125             case WLAN_CMD_SETPOWER:
1126                 pDevice->eCommandState = WLAN_CMD_SETPOWER_START;
1127                 break;
1128
1129             case WLAN_CMD_CHANGE_ANTENNA:
1130                 pDevice->eCommandState = WLAN_CMD_CHANGE_ANTENNA_START;
1131                 break;
1132
1133             case WLAN_CMD_REMOVE_ALLKEY:
1134                 pDevice->eCommandState = WLAN_CMD_REMOVE_ALLKEY_START;
1135                 break;
1136
1137             case WLAN_CMD_MAC_DISPOWERSAVING:
1138                 pDevice->eCommandState = WLAN_CMD_MAC_DISPOWERSAVING_START;
1139                 break;
1140
1141             case WLAN_CMD_11H_CHSW:
1142                 pDevice->eCommandState = WLAN_CMD_11H_CHSW_START;
1143                 break;
1144
1145             default:
1146                 break;
1147
1148         }
1149         vCommandTimerWait((void *) pDevice, 0);
1150     }
1151
1152     return TRUE;
1153 }
1154
1155 BOOL bScheduleCommand(void *hDeviceContext,
1156                       CMD_CODE eCommand,
1157                       PBYTE pbyItem0)
1158 {
1159     PSDevice        pDevice = (PSDevice)hDeviceContext;
1160
1161
1162     if (pDevice->cbFreeCmdQueue == 0) {
1163         return (FALSE);
1164     }
1165     pDevice->eCmdQueue[pDevice->uCmdEnqueueIdx].eCmd = eCommand;
1166     pDevice->eCmdQueue[pDevice->uCmdEnqueueIdx].bForceSCAN = TRUE;
1167     memset(pDevice->eCmdQueue[pDevice->uCmdEnqueueIdx].abyCmdDesireSSID, 0 , WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
1168     if (pbyItem0 != NULL) {
1169         switch (eCommand) {
1170             case WLAN_CMD_BSSID_SCAN:
1171                 pDevice->eCmdQueue[pDevice->uCmdEnqueueIdx].bForceSCAN = FALSE;
1172                 memcpy(pDevice->eCmdQueue[pDevice->uCmdEnqueueIdx].abyCmdDesireSSID,
1173                          pbyItem0, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
1174                 break;
1175
1176             case WLAN_CMD_SSID:
1177                 memcpy(pDevice->eCmdQueue[pDevice->uCmdEnqueueIdx].abyCmdDesireSSID,
1178                          pbyItem0, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
1179                 break;
1180
1181             case WLAN_CMD_DISASSOCIATE:
1182                 pDevice->eCmdQueue[pDevice->uCmdEnqueueIdx].bNeedRadioOFF = *((int *)pbyItem0);
1183                 break;
1184 /*
1185             case WLAN_CMD_DEAUTH:
1186                 pDevice->eCmdQueue[pDevice->uCmdEnqueueIdx].wDeAuthenReason = *((PWORD)pbyItem0);
1187                 break;
1188 */
1189
1190             case WLAN_CMD_RADIO:
1191                 pDevice->eCmdQueue[pDevice->uCmdEnqueueIdx].bRadioCmd = *((int *)pbyItem0);
1192                 break;
1193
1194             default:
1195                 break;
1196         }
1197     }
1198
1199     ADD_ONE_WITH_WRAP_AROUND(pDevice->uCmdEnqueueIdx, CMD_Q_SIZE);
1200     pDevice->cbFreeCmdQueue--;
1201
1202     if (pDevice->bCmdRunning == FALSE) {
1203         s_bCommandComplete(pDevice);
1204     }
1205     else {
1206     }
1207     return (TRUE);
1208
1209 }
1210
1211 /*
1212  * Description:
1213  *      Clear BSSID_SCAN cmd in CMD Queue
1214  *
1215  * Parameters:
1216  *  In:
1217  *      hDeviceContext  - Pointer to the adapter
1218  *      eCommand        - Command
1219  *  Out:
1220  *      none
1221  *
1222  * Return Value: TRUE if success; otherwise FALSE
1223  *
1224  */
1225 static BOOL s_bClearBSSID_SCAN(void *hDeviceContext)
1226 {
1227     PSDevice        pDevice = (PSDevice)hDeviceContext;
1228     unsigned int            uCmdDequeueIdx = pDevice->uCmdDequeueIdx;
1229     unsigned int            ii;
1230
1231     if ((pDevice->cbFreeCmdQueue < CMD_Q_SIZE) && (uCmdDequeueIdx != pDevice->uCmdEnqueueIdx)) {
1232         for (ii = 0; ii < (CMD_Q_SIZE - pDevice->cbFreeCmdQueue); ii ++) {
1233             if (pDevice->eCmdQueue[uCmdDequeueIdx].eCmd == WLAN_CMD_BSSID_SCAN)
1234                 pDevice->eCmdQueue[uCmdDequeueIdx].eCmd = WLAN_CMD_IDLE;
1235             ADD_ONE_WITH_WRAP_AROUND(uCmdDequeueIdx, CMD_Q_SIZE);
1236             if (uCmdDequeueIdx == pDevice->uCmdEnqueueIdx)
1237                 break;
1238         }
1239     }
1240     return TRUE;
1241 }
1242
1243
1244 //mike add:reset command timer
1245 void vResetCommandTimer(void *hDeviceContext)
1246 {
1247         PSDevice pDevice = (PSDevice)hDeviceContext;
1248
1249         //delete timer
1250         del_timer(&pDevice->sTimerCommand);
1251         //init timer
1252         init_timer(&pDevice->sTimerCommand);
1253         pDevice->sTimerCommand.data = (unsigned long)pDevice;
1254         pDevice->sTimerCommand.function = (TimerFunction)vRunCommand;
1255         pDevice->sTimerCommand.expires = RUN_AT(HZ);
1256         pDevice->cbFreeCmdQueue = CMD_Q_SIZE;
1257         pDevice->uCmdDequeueIdx = 0;
1258         pDevice->uCmdEnqueueIdx = 0;
1259         pDevice->eCommandState = WLAN_CMD_IDLE;
1260         pDevice->bCmdRunning = FALSE;
1261         pDevice->bCmdClear = FALSE;
1262 }
1263
1264 void BSSvSecondTxData(void *hDeviceContext)
1265 {
1266         PSDevice pDevice = (PSDevice)hDeviceContext;
1267         PSMgmtObject pMgmt = &(pDevice->sMgmtObj);
1268
1269         pDevice->nTxDataTimeCout++;
1270
1271         if (pDevice->nTxDataTimeCout < 4) {   //don't tx data if timer less than 40s
1272                 // printk("mike:%s-->no data Tx not exceed the desired Time as %d\n",__FUNCTION__,
1273                 //      (int)pDevice->nTxDataTimeCout);
1274                 pDevice->sTimerTxData.expires = RUN_AT(10 * HZ);      //10s callback
1275                 add_timer(&pDevice->sTimerTxData);
1276                 return;
1277         }
1278
1279         spin_lock_irq(&pDevice->lock);
1280         //is wap_supplicant running successful OR only open && sharekey mode!
1281         if (((pDevice->bLinkPass == TRUE) &&
1282                 (pMgmt->eAuthenMode < WMAC_AUTH_WPA)) ||  //open && sharekey linking
1283                 (pDevice->fWPA_Authened == TRUE)) {   //wpa linking
1284                 //   printk("mike:%s-->InSleep Tx Data Procedure\n",__FUNCTION__);
1285                 pDevice->fTxDataInSleep = TRUE;
1286                 PSbSendNullPacket(pDevice);      //send null packet
1287                 pDevice->fTxDataInSleep = FALSE;
1288         }
1289         spin_unlock_irq(&pDevice->lock);
1290
1291         pDevice->sTimerTxData.expires = RUN_AT(10 * HZ);      //10s callback
1292         add_timer(&pDevice->sTimerTxData);
1293 }