Forest Bond | 5449c68 | 2009-04-25 10:30:44 -0400 | [diff] [blame] | 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: power.c |
| 20 | * |
| 21 | * Purpose: Handles 802.11 power managment functions |
| 22 | * |
| 23 | * Author: Lyndon Chen |
| 24 | * |
| 25 | * Date: July 17, 2002 |
| 26 | * |
| 27 | * Functions: |
| 28 | * PSvEnablePowerSaving - Enable Power Saving Mode |
| 29 | * PSvDiasblePowerSaving - Disable Power Saving Mode |
| 30 | * PSbConsiderPowerDown - Decide if we can Power Down |
| 31 | * PSvSendPSPOLL - Send PS-POLL packet |
| 32 | * PSbSendNullPacket - Send Null packet |
| 33 | * PSbIsNextTBTTWakeUp - Decide if we need to wake up at next Beacon |
| 34 | * |
| 35 | * Revision History: |
| 36 | * |
| 37 | */ |
| 38 | |
| 39 | |
| 40 | |
| 41 | #if !defined(__TTYPE_H__) |
| 42 | #include "ttype.h" |
| 43 | #endif |
| 44 | #if !defined(__TBIT_H__) |
| 45 | #include "tbit.h" |
| 46 | #endif |
| 47 | #if !defined(__MAC_H__) |
| 48 | #include "mac.h" |
| 49 | #endif |
| 50 | #if !defined(__DEVICE_H__) |
| 51 | #include "device.h" |
| 52 | #endif |
| 53 | #if !defined(__WMGR_H__) |
| 54 | #include "wmgr.h" |
| 55 | #endif |
| 56 | #if !defined(__POWER_H__) |
| 57 | #include "power.h" |
| 58 | #endif |
| 59 | #if !defined(__WCMD_H__) |
| 60 | #include "wcmd.h" |
| 61 | #endif |
| 62 | #if !defined(__TBIT_H__) |
| 63 | #include "tbit.h" |
| 64 | #endif |
| 65 | #if !defined(__UMEM_H__) |
| 66 | #include "umem.h" |
| 67 | #endif |
| 68 | #if !defined(__RXTX_H__) |
| 69 | #include "rxtx.h" |
| 70 | #endif |
| 71 | #if !defined(__CARD_H__) |
| 72 | #include "card.h" |
| 73 | #endif |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | /*--------------------- Static Definitions -------------------------*/ |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | /*--------------------- Static Classes ----------------------------*/ |
| 84 | |
| 85 | /*--------------------- Static Variables --------------------------*/ |
| 86 | static int msglevel =MSG_LEVEL_INFO; |
| 87 | /*--------------------- Static Functions --------------------------*/ |
| 88 | |
| 89 | |
| 90 | /*--------------------- Export Variables --------------------------*/ |
| 91 | |
| 92 | |
| 93 | /*--------------------- Export Functions --------------------------*/ |
| 94 | |
| 95 | /*+ |
| 96 | * |
| 97 | * Routine Description: |
| 98 | * Enable hw power saving functions |
| 99 | * |
| 100 | * Return Value: |
| 101 | * None. |
| 102 | * |
| 103 | -*/ |
| 104 | |
| 105 | |
| 106 | VOID |
| 107 | PSvEnablePowerSaving( |
| 108 | IN HANDLE hDeviceContext, |
| 109 | IN WORD wListenInterval |
| 110 | ) |
| 111 | { |
| 112 | PSDevice pDevice = (PSDevice)hDeviceContext; |
| 113 | PSMgmtObject pMgmt = pDevice->pMgmt; |
| 114 | WORD wAID = pMgmt->wCurrAID | BIT14 | BIT15; |
| 115 | |
| 116 | // set period of power up before TBTT |
| 117 | VNSvOutPortW(pDevice->PortOffset + MAC_REG_PWBT, C_PWBT); |
| 118 | if (pDevice->eOPMode != OP_MODE_ADHOC) { |
| 119 | // set AID |
| 120 | VNSvOutPortW(pDevice->PortOffset + MAC_REG_AIDATIM, wAID); |
| 121 | } else { |
| 122 | // set ATIM Window |
| 123 | MACvWriteATIMW(pDevice->PortOffset, pMgmt->wCurrATIMWindow); |
| 124 | } |
| 125 | // Set AutoSleep |
| 126 | MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCFG, PSCFG_AUTOSLEEP); |
| 127 | // Set HWUTSF |
| 128 | MACvRegBitsOn(pDevice->PortOffset, MAC_REG_TFTCTL, TFTCTL_HWUTSF); |
| 129 | |
| 130 | if (wListenInterval >= 2) { |
| 131 | // clear always listen beacon |
| 132 | MACvRegBitsOff(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_ALBCN); |
| 133 | //pDevice->wCFG &= ~CFG_ALB; |
| 134 | // first time set listen next beacon |
| 135 | MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_LNBCN); |
| 136 | pMgmt->wCountToWakeUp = wListenInterval; |
| 137 | } |
| 138 | else { |
| 139 | // always listen beacon |
| 140 | MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_ALBCN); |
| 141 | //pDevice->wCFG |= CFG_ALB; |
| 142 | pMgmt->wCountToWakeUp = 0; |
| 143 | } |
| 144 | |
| 145 | // enable power saving hw function |
| 146 | MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PSEN); |
| 147 | pDevice->bEnablePSMode = TRUE; |
| 148 | |
| 149 | if (pDevice->eOPMode == OP_MODE_ADHOC) { |
| 150 | // bMgrPrepareBeaconToSend((HANDLE)pDevice, pMgmt); |
| 151 | } |
| 152 | // We don't send null pkt in ad hoc mode since beacon will handle this. |
| 153 | else if (pDevice->eOPMode == OP_MODE_INFRASTRUCTURE) { |
| 154 | PSbSendNullPacket(pDevice); |
| 155 | } |
| 156 | pDevice->bPWBitOn = TRUE; |
| 157 | DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "PS:Power Saving Mode Enable... \n"); |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | /*+ |
| 167 | * |
| 168 | * Routine Description: |
| 169 | * Disable hw power saving functions |
| 170 | * |
| 171 | * Return Value: |
| 172 | * None. |
| 173 | * |
| 174 | -*/ |
| 175 | |
| 176 | VOID |
| 177 | PSvDisablePowerSaving( |
| 178 | IN HANDLE hDeviceContext |
| 179 | ) |
| 180 | { |
| 181 | PSDevice pDevice = (PSDevice)hDeviceContext; |
| 182 | // PSMgmtObject pMgmt = pDevice->pMgmt; |
| 183 | |
| 184 | // disable power saving hw function |
| 185 | MACbPSWakeup(pDevice->PortOffset); |
| 186 | //clear AutoSleep |
| 187 | MACvRegBitsOff(pDevice->PortOffset, MAC_REG_PSCFG, PSCFG_AUTOSLEEP); |
| 188 | //clear HWUTSF |
| 189 | MACvRegBitsOff(pDevice->PortOffset, MAC_REG_TFTCTL, TFTCTL_HWUTSF); |
| 190 | // set always listen beacon |
| 191 | MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_ALBCN); |
| 192 | |
| 193 | pDevice->bEnablePSMode = FALSE; |
| 194 | |
| 195 | if (pDevice->eOPMode == OP_MODE_INFRASTRUCTURE) { |
| 196 | PSbSendNullPacket(pDevice); |
| 197 | } |
| 198 | pDevice->bPWBitOn = FALSE; |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | |
| 203 | /*+ |
| 204 | * |
| 205 | * Routine Description: |
| 206 | * Consider to power down when no more packets to tx or rx. |
| 207 | * |
| 208 | * Return Value: |
| 209 | * TRUE, if power down success |
| 210 | * FALSE, if fail |
| 211 | -*/ |
| 212 | |
| 213 | |
| 214 | BOOL |
| 215 | PSbConsiderPowerDown( |
| 216 | IN HANDLE hDeviceContext, |
| 217 | IN BOOL bCheckRxDMA, |
| 218 | IN BOOL bCheckCountToWakeUp |
| 219 | ) |
| 220 | { |
| 221 | PSDevice pDevice = (PSDevice)hDeviceContext; |
| 222 | PSMgmtObject pMgmt = pDevice->pMgmt; |
| 223 | UINT uIdx; |
| 224 | |
| 225 | // check if already in Doze mode |
| 226 | if (MACbIsRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PS)) |
| 227 | return TRUE; |
| 228 | |
| 229 | if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) { |
| 230 | // check if in TIM wake period |
| 231 | if (pMgmt->bInTIMWake) |
| 232 | return FALSE; |
| 233 | } |
| 234 | |
| 235 | // check scan state |
| 236 | if (pDevice->bCmdRunning) |
| 237 | return FALSE; |
| 238 | |
| 239 | // Froce PSEN on |
| 240 | MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PSEN); |
| 241 | |
| 242 | // check if all TD are empty, |
| 243 | for (uIdx = 0; uIdx < TYPE_MAXTD; uIdx ++) { |
| 244 | if (pDevice->iTDUsed[uIdx] != 0) |
| 245 | return FALSE; |
| 246 | } |
| 247 | |
| 248 | // check if rx isr is clear |
| 249 | if (bCheckRxDMA && |
| 250 | ((pDevice->dwIsr& ISR_RXDMA0) != 0) && |
| 251 | ((pDevice->dwIsr & ISR_RXDMA1) != 0)){ |
| 252 | return FALSE; |
| 253 | }; |
| 254 | |
| 255 | if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) { |
| 256 | if (bCheckCountToWakeUp && |
| 257 | (pMgmt->wCountToWakeUp == 0 || pMgmt->wCountToWakeUp == 1)) { |
| 258 | return FALSE; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | // no Tx, no Rx isr, now go to Doze |
| 263 | MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_GO2DOZE); |
| 264 | DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Go to Doze ZZZZZZZZZZZZZZZ\n"); |
| 265 | return TRUE; |
| 266 | } |
| 267 | |
| 268 | |
| 269 | |
| 270 | /*+ |
| 271 | * |
| 272 | * Routine Description: |
| 273 | * Send PS-POLL packet |
| 274 | * |
| 275 | * Return Value: |
| 276 | * None. |
| 277 | * |
| 278 | -*/ |
| 279 | |
| 280 | |
| 281 | |
| 282 | VOID |
| 283 | PSvSendPSPOLL( |
| 284 | IN HANDLE hDeviceContext |
| 285 | ) |
| 286 | { |
| 287 | PSDevice pDevice = (PSDevice)hDeviceContext; |
| 288 | PSMgmtObject pMgmt = pDevice->pMgmt; |
| 289 | PSTxMgmtPacket pTxPacket = NULL; |
| 290 | |
| 291 | |
| 292 | memset(pMgmt->pbyPSPacketPool, 0, sizeof(STxMgmtPacket) + WLAN_HDR_ADDR2_LEN); |
| 293 | pTxPacket = (PSTxMgmtPacket)pMgmt->pbyPSPacketPool; |
| 294 | pTxPacket->p80211Header = (PUWLAN_80211HDR)((PBYTE)pTxPacket + sizeof(STxMgmtPacket)); |
| 295 | pTxPacket->p80211Header->sA2.wFrameCtl = cpu_to_le16( |
| 296 | ( |
| 297 | WLAN_SET_FC_FTYPE(WLAN_TYPE_CTL) | |
| 298 | WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_PSPOLL) | |
| 299 | WLAN_SET_FC_PWRMGT(0) |
| 300 | )); |
| 301 | pTxPacket->p80211Header->sA2.wDurationID = pMgmt->wCurrAID | BIT14 | BIT15; |
| 302 | memcpy(pTxPacket->p80211Header->sA2.abyAddr1, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN); |
| 303 | memcpy(pTxPacket->p80211Header->sA2.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN); |
| 304 | pTxPacket->cbMPDULen = WLAN_HDR_ADDR2_LEN; |
| 305 | pTxPacket->cbPayloadLen = 0; |
| 306 | // send the frame |
| 307 | if (csMgmt_xmit(pDevice, pTxPacket) != CMD_STATUS_PENDING) { |
| 308 | DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send PS-Poll packet failed..\n"); |
| 309 | } |
| 310 | else { |
| 311 | // DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send PS-Poll packet success..\n"); |
| 312 | }; |
| 313 | |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | |
| 318 | |
| 319 | /*+ |
| 320 | * |
| 321 | * Routine Description: |
| 322 | * Send NULL packet to AP for notification power state of STA |
| 323 | * |
| 324 | * Return Value: |
| 325 | * None. |
| 326 | * |
| 327 | -*/ |
| 328 | BOOL |
| 329 | PSbSendNullPacket( |
| 330 | IN HANDLE hDeviceContext |
| 331 | ) |
| 332 | { |
| 333 | PSDevice pDevice = (PSDevice)hDeviceContext; |
| 334 | PSTxMgmtPacket pTxPacket = NULL; |
| 335 | PSMgmtObject pMgmt = pDevice->pMgmt; |
| 336 | UINT uIdx; |
| 337 | |
| 338 | |
| 339 | if (pDevice->bLinkPass == FALSE) { |
| 340 | return FALSE; |
| 341 | } |
| 342 | #ifdef TxInSleep |
| 343 | if ((pDevice->bEnablePSMode == FALSE) && |
| 344 | (pDevice->fTxDataInSleep == FALSE)){ |
| 345 | return FALSE; |
| 346 | } |
| 347 | #else |
| 348 | if (pDevice->bEnablePSMode == FALSE) { |
| 349 | return FALSE; |
| 350 | } |
| 351 | #endif |
| 352 | if (pDevice->bEnablePSMode) { |
| 353 | for (uIdx = 0; uIdx < TYPE_MAXTD; uIdx ++) { |
| 354 | if (pDevice->iTDUsed[uIdx] != 0) |
| 355 | return FALSE; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | memset(pMgmt->pbyPSPacketPool, 0, sizeof(STxMgmtPacket) + WLAN_NULLDATA_FR_MAXLEN); |
| 360 | pTxPacket = (PSTxMgmtPacket)pMgmt->pbyPSPacketPool; |
| 361 | pTxPacket->p80211Header = (PUWLAN_80211HDR)((PBYTE)pTxPacket + sizeof(STxMgmtPacket)); |
| 362 | |
| 363 | if (pDevice->bEnablePSMode) { |
| 364 | |
| 365 | pTxPacket->p80211Header->sA3.wFrameCtl = cpu_to_le16( |
| 366 | ( |
| 367 | WLAN_SET_FC_FTYPE(WLAN_TYPE_DATA) | |
| 368 | WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_NULL) | |
| 369 | WLAN_SET_FC_PWRMGT(1) |
| 370 | )); |
| 371 | } |
| 372 | else { |
| 373 | pTxPacket->p80211Header->sA3.wFrameCtl = cpu_to_le16( |
| 374 | ( |
| 375 | WLAN_SET_FC_FTYPE(WLAN_TYPE_DATA) | |
| 376 | WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_NULL) | |
| 377 | WLAN_SET_FC_PWRMGT(0) |
| 378 | )); |
| 379 | } |
| 380 | |
| 381 | if(pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) { |
| 382 | pTxPacket->p80211Header->sA3.wFrameCtl |= cpu_to_le16((WORD)WLAN_SET_FC_TODS(1)); |
| 383 | } |
| 384 | |
| 385 | memcpy(pTxPacket->p80211Header->sA3.abyAddr1, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN); |
| 386 | memcpy(pTxPacket->p80211Header->sA3.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN); |
| 387 | memcpy(pTxPacket->p80211Header->sA3.abyAddr3, pMgmt->abyCurrBSSID, WLAN_BSSID_LEN); |
| 388 | pTxPacket->cbMPDULen = WLAN_HDR_ADDR3_LEN; |
| 389 | pTxPacket->cbPayloadLen = 0; |
| 390 | // send the frame |
| 391 | if (csMgmt_xmit(pDevice, pTxPacket) != CMD_STATUS_PENDING) { |
| 392 | DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send Null Packet failed !\n"); |
| 393 | return FALSE; |
| 394 | } |
| 395 | else { |
| 396 | |
| 397 | // DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send Null Packet success....\n"); |
| 398 | } |
| 399 | |
| 400 | |
| 401 | return TRUE ; |
| 402 | } |
| 403 | |
| 404 | /*+ |
| 405 | * |
| 406 | * Routine Description: |
| 407 | * Check if Next TBTT must wake up |
| 408 | * |
| 409 | * Return Value: |
| 410 | * None. |
| 411 | * |
| 412 | -*/ |
| 413 | |
| 414 | BOOL |
| 415 | PSbIsNextTBTTWakeUp( |
| 416 | IN HANDLE hDeviceContext |
| 417 | ) |
| 418 | { |
| 419 | |
| 420 | PSDevice pDevice = (PSDevice)hDeviceContext; |
| 421 | PSMgmtObject pMgmt = pDevice->pMgmt; |
| 422 | BOOL bWakeUp = FALSE; |
| 423 | |
| 424 | if (pMgmt->wListenInterval >= 2) { |
| 425 | if (pMgmt->wCountToWakeUp == 0) { |
| 426 | pMgmt->wCountToWakeUp = pMgmt->wListenInterval; |
| 427 | } |
| 428 | |
| 429 | pMgmt->wCountToWakeUp --; |
| 430 | |
| 431 | if (pMgmt->wCountToWakeUp == 1) { |
| 432 | // Turn on wake up to listen next beacon |
| 433 | MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_LNBCN); |
| 434 | bWakeUp = TRUE; |
| 435 | } |
| 436 | |
| 437 | } |
| 438 | |
| 439 | return bWakeUp; |
| 440 | } |
| 441 | |