blob: 2340d2f0399b20da7db606837e53c64807986c55 [file] [log] [blame]
Forest Bond5449c682009-04-25 10:30:44 -04001/*
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 *
Jim Lieb612822f2009-08-12 14:54:03 -070019 *
Forest Bond5449c682009-04-25 10:30:44 -040020 * File: power.c
21 *
Uwe Kleine-König658ce9d2009-07-23 08:33:56 +020022 * Purpose: Handles 802.11 power management functions
Forest Bond5449c682009-04-25 10:30:44 -040023 *
24 * Author: Lyndon Chen
25 *
26 * Date: July 17, 2002
27 *
28 * Functions:
29 * PSvEnablePowerSaving - Enable Power Saving Mode
30 * PSvDiasblePowerSaving - Disable Power Saving Mode
31 * PSbConsiderPowerDown - Decide if we can Power Down
32 * PSvSendPSPOLL - Send PS-POLL packet
33 * PSbSendNullPacket - Send Null packet
34 * PSbIsNextTBTTWakeUp - Decide if we need to wake up at next Beacon
35 *
36 * Revision History:
37 *
38 */
39
Forest Bond5449c682009-04-25 10:30:44 -040040#include "ttype.h"
Forest Bond5449c682009-04-25 10:30:44 -040041#include "mac.h"
Forest Bond5449c682009-04-25 10:30:44 -040042#include "device.h"
Forest Bond5449c682009-04-25 10:30:44 -040043#include "wmgr.h"
Forest Bond5449c682009-04-25 10:30:44 -040044#include "power.h"
Forest Bond5449c682009-04-25 10:30:44 -040045#include "wcmd.h"
Forest Bond5449c682009-04-25 10:30:44 -040046#include "rxtx.h"
Forest Bond5449c682009-04-25 10:30:44 -040047#include "card.h"
Forest Bond5449c682009-04-25 10:30:44 -040048
49/*--------------------- Static Definitions -------------------------*/
50
Forest Bond5449c682009-04-25 10:30:44 -040051/*--------------------- Static Classes ----------------------------*/
52
53/*--------------------- Static Variables --------------------------*/
Joe Perches474f0f82013-03-18 10:44:58 -070054static int msglevel = MSG_LEVEL_INFO;
Forest Bond5449c682009-04-25 10:30:44 -040055/*--------------------- Static Functions --------------------------*/
56
Forest Bond5449c682009-04-25 10:30:44 -040057/*--------------------- Export Variables --------------------------*/
58
Forest Bond5449c682009-04-25 10:30:44 -040059/*--------------------- Export Functions --------------------------*/
60
61/*+
62 *
63 * Routine Description:
64 * Enable hw power saving functions
65 *
66 * Return Value:
67 * None.
68 *
Joe Perches474f0f82013-03-18 10:44:58 -070069 -*/
Forest Bond5449c682009-04-25 10:30:44 -040070
Charles Clément6b35b7b2010-05-07 12:30:19 -070071void
Forest Bond5449c682009-04-25 10:30:44 -040072PSvEnablePowerSaving(
Joe Perches474f0f82013-03-18 10:44:58 -070073 void *hDeviceContext,
74 unsigned short wListenInterval
75)
Forest Bond5449c682009-04-25 10:30:44 -040076{
Joe Perches474f0f82013-03-18 10:44:58 -070077 PSDevice pDevice = (PSDevice)hDeviceContext;
78 PSMgmtObject pMgmt = pDevice->pMgmt;
79 unsigned short wAID = pMgmt->wCurrAID | BIT14 | BIT15;
Forest Bond5449c682009-04-25 10:30:44 -040080
Joe Perches474f0f82013-03-18 10:44:58 -070081 // set period of power up before TBTT
82 VNSvOutPortW(pDevice->PortOffset + MAC_REG_PWBT, C_PWBT);
83 if (pDevice->eOPMode != OP_MODE_ADHOC) {
84 // set AID
85 VNSvOutPortW(pDevice->PortOffset + MAC_REG_AIDATIM, wAID);
86 } else {
87 // set ATIM Window
88 MACvWriteATIMW(pDevice->PortOffset, pMgmt->wCurrATIMWindow);
89 }
90 // Set AutoSleep
91 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCFG, PSCFG_AUTOSLEEP);
92 // Set HWUTSF
93 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_TFTCTL, TFTCTL_HWUTSF);
Forest Bond5449c682009-04-25 10:30:44 -040094
Joe Perches474f0f82013-03-18 10:44:58 -070095 if (wListenInterval >= 2) {
96 // clear always listen beacon
97 MACvRegBitsOff(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_ALBCN);
98 //pDevice->wCFG &= ~CFG_ALB;
99 // first time set listen next beacon
100 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_LNBCN);
101 pMgmt->wCountToWakeUp = wListenInterval;
Joe Perches5e0cc8a2013-03-18 20:55:37 -0700102 } else {
Joe Perches474f0f82013-03-18 10:44:58 -0700103 // always listen beacon
104 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_ALBCN);
105 //pDevice->wCFG |= CFG_ALB;
106 pMgmt->wCountToWakeUp = 0;
107 }
Forest Bond5449c682009-04-25 10:30:44 -0400108
Joe Perches474f0f82013-03-18 10:44:58 -0700109 // enable power saving hw function
110 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PSEN);
111 pDevice->bEnablePSMode = true;
Forest Bond5449c682009-04-25 10:30:44 -0400112
Joe Perches474f0f82013-03-18 10:44:58 -0700113 if (pDevice->eOPMode == OP_MODE_ADHOC) {
Charles Clément3a215e02010-05-12 20:54:39 -0700114// bMgrPrepareBeaconToSend((void *)pDevice, pMgmt);
Joe Perches474f0f82013-03-18 10:44:58 -0700115 }
116 // We don't send null pkt in ad hoc mode since beacon will handle this.
117 else if (pDevice->eOPMode == OP_MODE_INFRASTRUCTURE) {
118 PSbSendNullPacket(pDevice);
119 }
120 pDevice->bPWBitOn = true;
121 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "PS:Power Saving Mode Enable... \n");
122 return;
Forest Bond5449c682009-04-25 10:30:44 -0400123}
124
Forest Bond5449c682009-04-25 10:30:44 -0400125/*+
126 *
127 * Routine Description:
128 * Disable hw power saving functions
129 *
130 * Return Value:
131 * None.
132 *
Joe Perches474f0f82013-03-18 10:44:58 -0700133 -*/
Forest Bond5449c682009-04-25 10:30:44 -0400134
Charles Clément6b35b7b2010-05-07 12:30:19 -0700135void
Forest Bond5449c682009-04-25 10:30:44 -0400136PSvDisablePowerSaving(
Joe Perches474f0f82013-03-18 10:44:58 -0700137 void *hDeviceContext
138)
Forest Bond5449c682009-04-25 10:30:44 -0400139{
Joe Perches474f0f82013-03-18 10:44:58 -0700140 PSDevice pDevice = (PSDevice)hDeviceContext;
Forest Bond5449c682009-04-25 10:30:44 -0400141// PSMgmtObject pMgmt = pDevice->pMgmt;
142
Joe Perches474f0f82013-03-18 10:44:58 -0700143 // disable power saving hw function
144 MACbPSWakeup(pDevice->PortOffset);
145 //clear AutoSleep
146 MACvRegBitsOff(pDevice->PortOffset, MAC_REG_PSCFG, PSCFG_AUTOSLEEP);
147 //clear HWUTSF
148 MACvRegBitsOff(pDevice->PortOffset, MAC_REG_TFTCTL, TFTCTL_HWUTSF);
149 // set always listen beacon
150 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_ALBCN);
Forest Bond5449c682009-04-25 10:30:44 -0400151
Joe Perches474f0f82013-03-18 10:44:58 -0700152 pDevice->bEnablePSMode = false;
Forest Bond5449c682009-04-25 10:30:44 -0400153
Joe Perches474f0f82013-03-18 10:44:58 -0700154 if (pDevice->eOPMode == OP_MODE_INFRASTRUCTURE) {
155 PSbSendNullPacket(pDevice);
156 }
157 pDevice->bPWBitOn = false;
158 return;
Forest Bond5449c682009-04-25 10:30:44 -0400159}
160
Forest Bond5449c682009-04-25 10:30:44 -0400161/*+
162 *
163 * Routine Description:
164 * Consider to power down when no more packets to tx or rx.
165 *
166 * Return Value:
Charles Clément1b120682010-08-01 17:15:48 +0200167 * true, if power down success
Charles Clément5a5a2a62010-08-01 17:15:49 +0200168 * false, if fail
Joe Perches474f0f82013-03-18 10:44:58 -0700169 -*/
Forest Bond5449c682009-04-25 10:30:44 -0400170
Charles Clément7b6a0012010-08-01 17:15:50 +0200171bool
Forest Bond5449c682009-04-25 10:30:44 -0400172PSbConsiderPowerDown(
Joe Perches474f0f82013-03-18 10:44:58 -0700173 void *hDeviceContext,
174 bool bCheckRxDMA,
175 bool bCheckCountToWakeUp
176)
Forest Bond5449c682009-04-25 10:30:44 -0400177{
Joe Perches474f0f82013-03-18 10:44:58 -0700178 PSDevice pDevice = (PSDevice)hDeviceContext;
179 PSMgmtObject pMgmt = pDevice->pMgmt;
180 unsigned int uIdx;
Forest Bond5449c682009-04-25 10:30:44 -0400181
Joe Perches474f0f82013-03-18 10:44:58 -0700182 // check if already in Doze mode
183 if (MACbIsRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PS))
184 return true;
Forest Bond5449c682009-04-25 10:30:44 -0400185
Joe Perches474f0f82013-03-18 10:44:58 -0700186 if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
187 // check if in TIM wake period
188 if (pMgmt->bInTIMWake)
189 return false;
190 }
Forest Bond5449c682009-04-25 10:30:44 -0400191
Joe Perches474f0f82013-03-18 10:44:58 -0700192 // check scan state
193 if (pDevice->bCmdRunning)
194 return false;
Forest Bond5449c682009-04-25 10:30:44 -0400195
Joe Perches474f0f82013-03-18 10:44:58 -0700196 // Force PSEN on
197 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PSEN);
Forest Bond5449c682009-04-25 10:30:44 -0400198
Joe Perches474f0f82013-03-18 10:44:58 -0700199 // check if all TD are empty,
200 for (uIdx = 0; uIdx < TYPE_MAXTD; uIdx++) {
201 if (pDevice->iTDUsed[uIdx] != 0)
202 return false;
203 }
Forest Bond5449c682009-04-25 10:30:44 -0400204
Joe Perches474f0f82013-03-18 10:44:58 -0700205 // check if rx isr is clear
206 if (bCheckRxDMA &&
207 ((pDevice->dwIsr & ISR_RXDMA0) != 0) &&
208 ((pDevice->dwIsr & ISR_RXDMA1) != 0)) {
209 return false;
210 }
Forest Bond5449c682009-04-25 10:30:44 -0400211
Joe Perches474f0f82013-03-18 10:44:58 -0700212 if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
213 if (bCheckCountToWakeUp &&
214 (pMgmt->wCountToWakeUp == 0 || pMgmt->wCountToWakeUp == 1)) {
215 return false;
216 }
217 }
Forest Bond5449c682009-04-25 10:30:44 -0400218
Joe Perches474f0f82013-03-18 10:44:58 -0700219 // no Tx, no Rx isr, now go to Doze
220 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_GO2DOZE);
221 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Go to Doze ZZZZZZZZZZZZZZZ\n");
222 return true;
Forest Bond5449c682009-04-25 10:30:44 -0400223}
224
Forest Bond5449c682009-04-25 10:30:44 -0400225/*+
226 *
227 * Routine Description:
228 * Send PS-POLL packet
229 *
230 * Return Value:
231 * None.
232 *
Joe Perches474f0f82013-03-18 10:44:58 -0700233 -*/
Forest Bond5449c682009-04-25 10:30:44 -0400234
Charles Clément6b35b7b2010-05-07 12:30:19 -0700235void
Forest Bond5449c682009-04-25 10:30:44 -0400236PSvSendPSPOLL(
Joe Perches474f0f82013-03-18 10:44:58 -0700237 void *hDeviceContext
238)
Forest Bond5449c682009-04-25 10:30:44 -0400239{
Joe Perches474f0f82013-03-18 10:44:58 -0700240 PSDevice pDevice = (PSDevice)hDeviceContext;
241 PSMgmtObject pMgmt = pDevice->pMgmt;
242 PSTxMgmtPacket pTxPacket = NULL;
Forest Bond5449c682009-04-25 10:30:44 -0400243
Joe Perches474f0f82013-03-18 10:44:58 -0700244 memset(pMgmt->pbyPSPacketPool, 0, sizeof(STxMgmtPacket) + WLAN_HDR_ADDR2_LEN);
245 pTxPacket = (PSTxMgmtPacket)pMgmt->pbyPSPacketPool;
246 pTxPacket->p80211Header = (PUWLAN_80211HDR)((unsigned char *)pTxPacket + sizeof(STxMgmtPacket));
247 pTxPacket->p80211Header->sA2.wFrameCtl = cpu_to_le16(
248 (
249 WLAN_SET_FC_FTYPE(WLAN_TYPE_CTL) |
250 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_PSPOLL) |
251 WLAN_SET_FC_PWRMGT(0)
252));
253 pTxPacket->p80211Header->sA2.wDurationID = pMgmt->wCurrAID | BIT14 | BIT15;
254 memcpy(pTxPacket->p80211Header->sA2.abyAddr1, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN);
255 memcpy(pTxPacket->p80211Header->sA2.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
256 pTxPacket->cbMPDULen = WLAN_HDR_ADDR2_LEN;
257 pTxPacket->cbPayloadLen = 0;
258 // send the frame
259 if (csMgmt_xmit(pDevice, pTxPacket) != CMD_STATUS_PENDING) {
260 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send PS-Poll packet failed..\n");
Joe Perches5e0cc8a2013-03-18 20:55:37 -0700261 } else {
Jim Lieb7e809a92009-07-30 10:27:21 -0700262// DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send PS-Poll packet success..\n");
Joe Perches474f0f82013-03-18 10:44:58 -0700263 };
Forest Bond5449c682009-04-25 10:30:44 -0400264
Joe Perches474f0f82013-03-18 10:44:58 -0700265 return;
Forest Bond5449c682009-04-25 10:30:44 -0400266}
267
Forest Bond5449c682009-04-25 10:30:44 -0400268/*+
269 *
270 * Routine Description:
271 * Send NULL packet to AP for notification power state of STA
272 *
273 * Return Value:
274 * None.
275 *
Joe Perches474f0f82013-03-18 10:44:58 -0700276 -*/
Charles Clément7b6a0012010-08-01 17:15:50 +0200277bool
Forest Bond5449c682009-04-25 10:30:44 -0400278PSbSendNullPacket(
Joe Perches474f0f82013-03-18 10:44:58 -0700279 void *hDeviceContext
280)
Forest Bond5449c682009-04-25 10:30:44 -0400281{
Joe Perches474f0f82013-03-18 10:44:58 -0700282 PSDevice pDevice = (PSDevice)hDeviceContext;
283 PSTxMgmtPacket pTxPacket = NULL;
284 PSMgmtObject pMgmt = pDevice->pMgmt;
285 unsigned int uIdx;
Forest Bond5449c682009-04-25 10:30:44 -0400286
Joe Perches474f0f82013-03-18 10:44:58 -0700287 if (pDevice->bLinkPass == false) {
288 return false;
289 }
290#ifdef TxInSleep
291 if ((pDevice->bEnablePSMode == false) &&
292 (pDevice->fTxDataInSleep == false)) {
293 return false;
294 }
Forest Bond5449c682009-04-25 10:30:44 -0400295#else
Joe Perches474f0f82013-03-18 10:44:58 -0700296 if (pDevice->bEnablePSMode == false) {
297 return false;
298 }
Forest Bond5449c682009-04-25 10:30:44 -0400299#endif
Joe Perches474f0f82013-03-18 10:44:58 -0700300 if (pDevice->bEnablePSMode) {
301 for (uIdx = 0; uIdx < TYPE_MAXTD; uIdx++) {
302 if (pDevice->iTDUsed[uIdx] != 0)
303 return false;
304 }
305 }
Forest Bond5449c682009-04-25 10:30:44 -0400306
Joe Perches474f0f82013-03-18 10:44:58 -0700307 memset(pMgmt->pbyPSPacketPool, 0, sizeof(STxMgmtPacket) + WLAN_NULLDATA_FR_MAXLEN);
308 pTxPacket = (PSTxMgmtPacket)pMgmt->pbyPSPacketPool;
309 pTxPacket->p80211Header = (PUWLAN_80211HDR)((unsigned char *)pTxPacket + sizeof(STxMgmtPacket));
Forest Bond5449c682009-04-25 10:30:44 -0400310
Joe Perches474f0f82013-03-18 10:44:58 -0700311 if (pDevice->bEnablePSMode) {
Joe Perches474f0f82013-03-18 10:44:58 -0700312 pTxPacket->p80211Header->sA3.wFrameCtl = cpu_to_le16(
313 (
314 WLAN_SET_FC_FTYPE(WLAN_TYPE_DATA) |
315 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_NULL) |
316 WLAN_SET_FC_PWRMGT(1)
317));
Joe Perches5e0cc8a2013-03-18 20:55:37 -0700318 } else {
Joe Perches474f0f82013-03-18 10:44:58 -0700319 pTxPacket->p80211Header->sA3.wFrameCtl = cpu_to_le16(
320 (
321 WLAN_SET_FC_FTYPE(WLAN_TYPE_DATA) |
322 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_NULL) |
323 WLAN_SET_FC_PWRMGT(0)
324));
325 }
Forest Bond5449c682009-04-25 10:30:44 -0400326
Joe Perches474f0f82013-03-18 10:44:58 -0700327 if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
328 pTxPacket->p80211Header->sA3.wFrameCtl |= cpu_to_le16((unsigned short)WLAN_SET_FC_TODS(1));
329 }
Forest Bond5449c682009-04-25 10:30:44 -0400330
Joe Perches474f0f82013-03-18 10:44:58 -0700331 memcpy(pTxPacket->p80211Header->sA3.abyAddr1, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN);
332 memcpy(pTxPacket->p80211Header->sA3.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
333 memcpy(pTxPacket->p80211Header->sA3.abyAddr3, pMgmt->abyCurrBSSID, WLAN_BSSID_LEN);
334 pTxPacket->cbMPDULen = WLAN_HDR_ADDR3_LEN;
335 pTxPacket->cbPayloadLen = 0;
336 // send the frame
337 if (csMgmt_xmit(pDevice, pTxPacket) != CMD_STATUS_PENDING) {
338 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send Null Packet failed !\n");
339 return false;
Joe Perches5e0cc8a2013-03-18 20:55:37 -0700340 } else {
Jim Lieb7e809a92009-07-30 10:27:21 -0700341// DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send Null Packet success....\n");
Joe Perches474f0f82013-03-18 10:44:58 -0700342 }
Forest Bond5449c682009-04-25 10:30:44 -0400343
Joe Perches474f0f82013-03-18 10:44:58 -0700344 return true;
Forest Bond5449c682009-04-25 10:30:44 -0400345}
346
347/*+
348 *
349 * Routine Description:
350 * Check if Next TBTT must wake up
351 *
352 * Return Value:
353 * None.
354 *
Joe Perches474f0f82013-03-18 10:44:58 -0700355 -*/
Forest Bond5449c682009-04-25 10:30:44 -0400356
Charles Clément7b6a0012010-08-01 17:15:50 +0200357bool
Forest Bond5449c682009-04-25 10:30:44 -0400358PSbIsNextTBTTWakeUp(
Joe Perches474f0f82013-03-18 10:44:58 -0700359 void *hDeviceContext
360)
Forest Bond5449c682009-04-25 10:30:44 -0400361{
Joe Perches474f0f82013-03-18 10:44:58 -0700362 PSDevice pDevice = (PSDevice)hDeviceContext;
363 PSMgmtObject pMgmt = pDevice->pMgmt;
364 bool bWakeUp = false;
Forest Bond5449c682009-04-25 10:30:44 -0400365
Joe Perches474f0f82013-03-18 10:44:58 -0700366 if (pMgmt->wListenInterval >= 2) {
367 if (pMgmt->wCountToWakeUp == 0) {
368 pMgmt->wCountToWakeUp = pMgmt->wListenInterval;
369 }
Forest Bond5449c682009-04-25 10:30:44 -0400370
Joe Perches474f0f82013-03-18 10:44:58 -0700371 pMgmt->wCountToWakeUp--;
Forest Bond5449c682009-04-25 10:30:44 -0400372
Joe Perches474f0f82013-03-18 10:44:58 -0700373 if (pMgmt->wCountToWakeUp == 1) {
374 // Turn on wake up to listen next beacon
375 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_LNBCN);
376 bWakeUp = true;
377 }
Forest Bond5449c682009-04-25 10:30:44 -0400378
Joe Perches474f0f82013-03-18 10:44:58 -0700379 }
Forest Bond5449c682009-04-25 10:30:44 -0400380
Joe Perches474f0f82013-03-18 10:44:58 -0700381 return bWakeUp;
Forest Bond5449c682009-04-25 10:30:44 -0400382}