2 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
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.
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.
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.
22 * Purpose: Handles 802.11 power management 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
49 /*--------------------- Static Definitions -------------------------*/
51 /*--------------------- Static Classes ----------------------------*/
53 /*--------------------- Static Variables --------------------------*/
54 static int msglevel = MSG_LEVEL_INFO;
55 /*--------------------- Static Functions --------------------------*/
57 /*--------------------- Export Variables --------------------------*/
59 /*--------------------- Export Functions --------------------------*/
63 * Routine Description:
64 * Enable hw power saving functions
74 unsigned short wListenInterval
77 PSDevice pDevice = (PSDevice)hDeviceContext;
78 PSMgmtObject pMgmt = pDevice->pMgmt;
79 unsigned short wAID = pMgmt->wCurrAID | BIT14 | BIT15;
81 // set period of power up before TBTT
82 VNSvOutPortW(pDevice->PortOffset + MAC_REG_PWBT, C_PWBT);
83 if (pDevice->eOPMode != OP_MODE_ADHOC) {
85 VNSvOutPortW(pDevice->PortOffset + MAC_REG_AIDATIM, wAID);
88 MACvWriteATIMW(pDevice->PortOffset, pMgmt->wCurrATIMWindow);
91 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCFG, PSCFG_AUTOSLEEP);
93 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_TFTCTL, TFTCTL_HWUTSF);
95 if (wListenInterval >= 2) {
96 // clear always listen beacon
97 MACvRegBitsOff(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_ALBCN);
98 // first time set listen next beacon
99 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_LNBCN);
100 pMgmt->wCountToWakeUp = wListenInterval;
102 // always listen beacon
103 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_ALBCN);
104 pMgmt->wCountToWakeUp = 0;
107 // enable power saving hw function
108 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PSEN);
109 pDevice->bEnablePSMode = true;
111 /* We don't send null pkt in ad hoc mode since beacon will handle this. */
112 if (pDevice->eOPMode != OP_MODE_ADHOC && pDevice->eOPMode == OP_MODE_INFRASTRUCTURE)
113 PSbSendNullPacket(pDevice);
115 pDevice->bPWBitOn = true;
116 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "PS:Power Saving Mode Enable... \n");
122 * Routine Description:
123 * Disable hw power saving functions
131 PSvDisablePowerSaving(
135 PSDevice pDevice = (PSDevice)hDeviceContext;
137 // disable power saving hw function
138 MACbPSWakeup(pDevice->PortOffset);
140 MACvRegBitsOff(pDevice->PortOffset, MAC_REG_PSCFG, PSCFG_AUTOSLEEP);
142 MACvRegBitsOff(pDevice->PortOffset, MAC_REG_TFTCTL, TFTCTL_HWUTSF);
143 // set always listen beacon
144 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_ALBCN);
146 pDevice->bEnablePSMode = false;
148 if (pDevice->eOPMode == OP_MODE_INFRASTRUCTURE) {
149 PSbSendNullPacket(pDevice);
151 pDevice->bPWBitOn = false;
157 * Routine Description:
158 * Consider to power down when no more packets to tx or rx.
161 * true, if power down success
166 PSbConsiderPowerDown(
167 void *hDeviceContext,
169 bool bCheckCountToWakeUp
172 PSDevice pDevice = (PSDevice)hDeviceContext;
173 PSMgmtObject pMgmt = pDevice->pMgmt;
176 // check if already in Doze mode
177 if (MACbIsRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PS))
180 if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
181 // check if in TIM wake period
182 if (pMgmt->bInTIMWake)
187 if (pDevice->bCmdRunning)
191 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PSEN);
193 // check if all TD are empty,
194 for (uIdx = 0; uIdx < TYPE_MAXTD; uIdx++) {
195 if (pDevice->iTDUsed[uIdx] != 0)
199 // check if rx isr is clear
201 ((pDevice->dwIsr & ISR_RXDMA0) != 0) &&
202 ((pDevice->dwIsr & ISR_RXDMA1) != 0)) {
206 if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
207 if (bCheckCountToWakeUp &&
208 (pMgmt->wCountToWakeUp == 0 || pMgmt->wCountToWakeUp == 1)) {
213 // no Tx, no Rx isr, now go to Doze
214 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_GO2DOZE);
215 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Go to Doze ZZZZZZZZZZZZZZZ\n");
221 * Routine Description:
222 * Send PS-POLL packet
234 PSDevice pDevice = (PSDevice)hDeviceContext;
235 PSMgmtObject pMgmt = pDevice->pMgmt;
236 PSTxMgmtPacket pTxPacket = NULL;
238 memset(pMgmt->pbyPSPacketPool, 0, sizeof(STxMgmtPacket) + WLAN_HDR_ADDR2_LEN);
239 pTxPacket = (PSTxMgmtPacket)pMgmt->pbyPSPacketPool;
240 pTxPacket->p80211Header = (PUWLAN_80211HDR)((unsigned char *)pTxPacket + sizeof(STxMgmtPacket));
241 pTxPacket->p80211Header->sA2.wFrameCtl = cpu_to_le16(
243 WLAN_SET_FC_FTYPE(WLAN_TYPE_CTL) |
244 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_PSPOLL) |
245 WLAN_SET_FC_PWRMGT(0)
247 pTxPacket->p80211Header->sA2.wDurationID = pMgmt->wCurrAID | BIT14 | BIT15;
248 memcpy(pTxPacket->p80211Header->sA2.abyAddr1, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN);
249 memcpy(pTxPacket->p80211Header->sA2.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
250 pTxPacket->cbMPDULen = WLAN_HDR_ADDR2_LEN;
251 pTxPacket->cbPayloadLen = 0;
253 if (csMgmt_xmit(pDevice, pTxPacket) != CMD_STATUS_PENDING) {
254 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send PS-Poll packet failed..\n");
262 * Routine Description:
263 * Send NULL packet to AP for notification power state of STA
274 PSDevice pDevice = (PSDevice)hDeviceContext;
275 PSTxMgmtPacket pTxPacket = NULL;
276 PSMgmtObject pMgmt = pDevice->pMgmt;
279 if (!pDevice->bLinkPass) {
283 if (!pDevice->bEnablePSMode && !pDevice->fTxDataInSleep) {
287 if (!pDevice->bEnablePSMode) {
291 if (pDevice->bEnablePSMode) {
292 for (uIdx = 0; uIdx < TYPE_MAXTD; uIdx++) {
293 if (pDevice->iTDUsed[uIdx] != 0)
298 memset(pMgmt->pbyPSPacketPool, 0, sizeof(STxMgmtPacket) + WLAN_NULLDATA_FR_MAXLEN);
299 pTxPacket = (PSTxMgmtPacket)pMgmt->pbyPSPacketPool;
300 pTxPacket->p80211Header = (PUWLAN_80211HDR)((unsigned char *)pTxPacket + sizeof(STxMgmtPacket));
302 if (pDevice->bEnablePSMode) {
303 pTxPacket->p80211Header->sA3.wFrameCtl = cpu_to_le16(
305 WLAN_SET_FC_FTYPE(WLAN_TYPE_DATA) |
306 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_NULL) |
307 WLAN_SET_FC_PWRMGT(1)
310 pTxPacket->p80211Header->sA3.wFrameCtl = cpu_to_le16(
312 WLAN_SET_FC_FTYPE(WLAN_TYPE_DATA) |
313 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_NULL) |
314 WLAN_SET_FC_PWRMGT(0)
318 if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
319 pTxPacket->p80211Header->sA3.wFrameCtl |= cpu_to_le16((unsigned short)WLAN_SET_FC_TODS(1));
322 memcpy(pTxPacket->p80211Header->sA3.abyAddr1, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN);
323 memcpy(pTxPacket->p80211Header->sA3.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
324 memcpy(pTxPacket->p80211Header->sA3.abyAddr3, pMgmt->abyCurrBSSID, WLAN_BSSID_LEN);
325 pTxPacket->cbMPDULen = WLAN_HDR_ADDR3_LEN;
326 pTxPacket->cbPayloadLen = 0;
328 if (csMgmt_xmit(pDevice, pTxPacket) != CMD_STATUS_PENDING) {
329 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send Null Packet failed !\n");
338 * Routine Description:
339 * Check if Next TBTT must wake up
351 PSDevice pDevice = (PSDevice)hDeviceContext;
352 PSMgmtObject pMgmt = pDevice->pMgmt;
353 bool bWakeUp = false;
355 if (pMgmt->wListenInterval >= 2) {
356 if (pMgmt->wCountToWakeUp == 0) {
357 pMgmt->wCountToWakeUp = pMgmt->wListenInterval;
360 pMgmt->wCountToWakeUp--;
362 if (pMgmt->wCountToWakeUp == 1) {
363 // Turn on wake up to listen next beacon
364 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_LNBCN);