blob: 661d534304cce99f26a275ccedaed34210b7edbf [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
51
52
53
54/*--------------------- Static Classes ----------------------------*/
55
56/*--------------------- Static Variables --------------------------*/
57static int msglevel =MSG_LEVEL_INFO;
58/*--------------------- Static Functions --------------------------*/
59
60
61/*--------------------- Export Variables --------------------------*/
62
63
64/*--------------------- Export Functions --------------------------*/
65
66/*+
67 *
68 * Routine Description:
69 * Enable hw power saving functions
70 *
71 * Return Value:
72 * None.
73 *
74-*/
75
76
Charles Clément6b35b7b2010-05-07 12:30:19 -070077void
Forest Bond5449c682009-04-25 10:30:44 -040078PSvEnablePowerSaving(
Charles Clément3a215e02010-05-12 20:54:39 -070079 void *hDeviceContext,
Charles Clément2986db52010-06-24 11:02:26 -070080 unsigned short wListenInterval
Forest Bond5449c682009-04-25 10:30:44 -040081 )
82{
83 PSDevice pDevice = (PSDevice)hDeviceContext;
84 PSMgmtObject pMgmt = pDevice->pMgmt;
Charles Clément2986db52010-06-24 11:02:26 -070085 unsigned short wAID = pMgmt->wCurrAID | BIT14 | BIT15;
Forest Bond5449c682009-04-25 10:30:44 -040086
87 // set period of power up before TBTT
88 VNSvOutPortW(pDevice->PortOffset + MAC_REG_PWBT, C_PWBT);
89 if (pDevice->eOPMode != OP_MODE_ADHOC) {
90 // set AID
91 VNSvOutPortW(pDevice->PortOffset + MAC_REG_AIDATIM, wAID);
92 } else {
93 // set ATIM Window
94 MACvWriteATIMW(pDevice->PortOffset, pMgmt->wCurrATIMWindow);
95 }
96 // Set AutoSleep
97 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCFG, PSCFG_AUTOSLEEP);
98 // Set HWUTSF
99 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_TFTCTL, TFTCTL_HWUTSF);
100
101 if (wListenInterval >= 2) {
102 // clear always listen beacon
103 MACvRegBitsOff(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_ALBCN);
104 //pDevice->wCFG &= ~CFG_ALB;
105 // first time set listen next beacon
106 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_LNBCN);
107 pMgmt->wCountToWakeUp = wListenInterval;
108 }
109 else {
110 // always listen beacon
111 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_ALBCN);
112 //pDevice->wCFG |= CFG_ALB;
113 pMgmt->wCountToWakeUp = 0;
114 }
115
116 // enable power saving hw function
117 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PSEN);
Charles Clément1b120682010-08-01 17:15:48 +0200118 pDevice->bEnablePSMode = true;
Forest Bond5449c682009-04-25 10:30:44 -0400119
120 if (pDevice->eOPMode == OP_MODE_ADHOC) {
Charles Clément3a215e02010-05-12 20:54:39 -0700121// bMgrPrepareBeaconToSend((void *)pDevice, pMgmt);
Forest Bond5449c682009-04-25 10:30:44 -0400122 }
123 // We don't send null pkt in ad hoc mode since beacon will handle this.
124 else if (pDevice->eOPMode == OP_MODE_INFRASTRUCTURE) {
125 PSbSendNullPacket(pDevice);
126 }
Charles Clément1b120682010-08-01 17:15:48 +0200127 pDevice->bPWBitOn = true;
Jim Lieb7e809a92009-07-30 10:27:21 -0700128 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "PS:Power Saving Mode Enable... \n");
Forest Bond5449c682009-04-25 10:30:44 -0400129 return;
130}
131
132
133
134
135
136
137/*+
138 *
139 * Routine Description:
140 * Disable hw power saving functions
141 *
142 * Return Value:
143 * None.
144 *
145-*/
146
Charles Clément6b35b7b2010-05-07 12:30:19 -0700147void
Forest Bond5449c682009-04-25 10:30:44 -0400148PSvDisablePowerSaving(
Charles Clément3a215e02010-05-12 20:54:39 -0700149 void *hDeviceContext
Forest Bond5449c682009-04-25 10:30:44 -0400150 )
151{
152 PSDevice pDevice = (PSDevice)hDeviceContext;
153// PSMgmtObject pMgmt = pDevice->pMgmt;
154
155 // disable power saving hw function
156 MACbPSWakeup(pDevice->PortOffset);
157 //clear AutoSleep
158 MACvRegBitsOff(pDevice->PortOffset, MAC_REG_PSCFG, PSCFG_AUTOSLEEP);
159 //clear HWUTSF
160 MACvRegBitsOff(pDevice->PortOffset, MAC_REG_TFTCTL, TFTCTL_HWUTSF);
161 // set always listen beacon
162 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_ALBCN);
163
Charles Clément5a5a2a62010-08-01 17:15:49 +0200164 pDevice->bEnablePSMode = false;
Forest Bond5449c682009-04-25 10:30:44 -0400165
166 if (pDevice->eOPMode == OP_MODE_INFRASTRUCTURE) {
167 PSbSendNullPacket(pDevice);
168 }
Charles Clément5a5a2a62010-08-01 17:15:49 +0200169 pDevice->bPWBitOn = false;
Forest Bond5449c682009-04-25 10:30:44 -0400170 return;
171}
172
173
174/*+
175 *
176 * Routine Description:
177 * Consider to power down when no more packets to tx or rx.
178 *
179 * Return Value:
Charles Clément1b120682010-08-01 17:15:48 +0200180 * true, if power down success
Charles Clément5a5a2a62010-08-01 17:15:49 +0200181 * false, if fail
Forest Bond5449c682009-04-25 10:30:44 -0400182-*/
183
184
Charles Clément7b6a0012010-08-01 17:15:50 +0200185bool
Forest Bond5449c682009-04-25 10:30:44 -0400186PSbConsiderPowerDown(
Charles Clément3a215e02010-05-12 20:54:39 -0700187 void *hDeviceContext,
Charles Clément7b6a0012010-08-01 17:15:50 +0200188 bool bCheckRxDMA,
189 bool bCheckCountToWakeUp
Forest Bond5449c682009-04-25 10:30:44 -0400190 )
191{
192 PSDevice pDevice = (PSDevice)hDeviceContext;
193 PSMgmtObject pMgmt = pDevice->pMgmt;
Charles Clémentb6e95cd2010-06-02 09:52:01 -0700194 unsigned int uIdx;
Forest Bond5449c682009-04-25 10:30:44 -0400195
196 // check if already in Doze mode
197 if (MACbIsRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PS))
Charles Clément1b120682010-08-01 17:15:48 +0200198 return true;
Forest Bond5449c682009-04-25 10:30:44 -0400199
200 if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
201 // check if in TIM wake period
202 if (pMgmt->bInTIMWake)
Charles Clément5a5a2a62010-08-01 17:15:49 +0200203 return false;
Forest Bond5449c682009-04-25 10:30:44 -0400204 }
205
206 // check scan state
207 if (pDevice->bCmdRunning)
Charles Clément5a5a2a62010-08-01 17:15:49 +0200208 return false;
Forest Bond5449c682009-04-25 10:30:44 -0400209
Justin P. Mattock789d1ae2012-08-20 08:43:13 -0700210 // Force PSEN on
Forest Bond5449c682009-04-25 10:30:44 -0400211 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PSEN);
212
213 // check if all TD are empty,
214 for (uIdx = 0; uIdx < TYPE_MAXTD; uIdx ++) {
215 if (pDevice->iTDUsed[uIdx] != 0)
Charles Clément5a5a2a62010-08-01 17:15:49 +0200216 return false;
Forest Bond5449c682009-04-25 10:30:44 -0400217 }
218
219 // check if rx isr is clear
220 if (bCheckRxDMA &&
221 ((pDevice->dwIsr& ISR_RXDMA0) != 0) &&
222 ((pDevice->dwIsr & ISR_RXDMA1) != 0)){
Charles Clément5a5a2a62010-08-01 17:15:49 +0200223 return false;
Joe Perches9fc86022011-04-10 14:31:32 -0700224 }
Forest Bond5449c682009-04-25 10:30:44 -0400225
226 if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
227 if (bCheckCountToWakeUp &&
228 (pMgmt->wCountToWakeUp == 0 || pMgmt->wCountToWakeUp == 1)) {
Charles Clément5a5a2a62010-08-01 17:15:49 +0200229 return false;
Forest Bond5449c682009-04-25 10:30:44 -0400230 }
231 }
232
233 // no Tx, no Rx isr, now go to Doze
234 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_GO2DOZE);
Jim Lieb7e809a92009-07-30 10:27:21 -0700235 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Go to Doze ZZZZZZZZZZZZZZZ\n");
Charles Clément1b120682010-08-01 17:15:48 +0200236 return true;
Forest Bond5449c682009-04-25 10:30:44 -0400237}
238
239
240
241/*+
242 *
243 * Routine Description:
244 * Send PS-POLL packet
245 *
246 * Return Value:
247 * None.
248 *
249-*/
250
251
252
Charles Clément6b35b7b2010-05-07 12:30:19 -0700253void
Forest Bond5449c682009-04-25 10:30:44 -0400254PSvSendPSPOLL(
Charles Clément3a215e02010-05-12 20:54:39 -0700255 void *hDeviceContext
Forest Bond5449c682009-04-25 10:30:44 -0400256 )
257{
258 PSDevice pDevice = (PSDevice)hDeviceContext;
259 PSMgmtObject pMgmt = pDevice->pMgmt;
260 PSTxMgmtPacket pTxPacket = NULL;
261
262
263 memset(pMgmt->pbyPSPacketPool, 0, sizeof(STxMgmtPacket) + WLAN_HDR_ADDR2_LEN);
264 pTxPacket = (PSTxMgmtPacket)pMgmt->pbyPSPacketPool;
Charles Clément2989e962010-06-05 15:13:47 -0700265 pTxPacket->p80211Header = (PUWLAN_80211HDR)((unsigned char *)pTxPacket + sizeof(STxMgmtPacket));
Forest Bond5449c682009-04-25 10:30:44 -0400266 pTxPacket->p80211Header->sA2.wFrameCtl = cpu_to_le16(
267 (
268 WLAN_SET_FC_FTYPE(WLAN_TYPE_CTL) |
269 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_PSPOLL) |
270 WLAN_SET_FC_PWRMGT(0)
271 ));
272 pTxPacket->p80211Header->sA2.wDurationID = pMgmt->wCurrAID | BIT14 | BIT15;
273 memcpy(pTxPacket->p80211Header->sA2.abyAddr1, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN);
274 memcpy(pTxPacket->p80211Header->sA2.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
275 pTxPacket->cbMPDULen = WLAN_HDR_ADDR2_LEN;
276 pTxPacket->cbPayloadLen = 0;
277 // send the frame
278 if (csMgmt_xmit(pDevice, pTxPacket) != CMD_STATUS_PENDING) {
Jim Lieb7e809a92009-07-30 10:27:21 -0700279 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send PS-Poll packet failed..\n");
Forest Bond5449c682009-04-25 10:30:44 -0400280 }
281 else {
Jim Lieb7e809a92009-07-30 10:27:21 -0700282// DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send PS-Poll packet success..\n");
Forest Bond5449c682009-04-25 10:30:44 -0400283 };
284
285 return;
286}
287
288
289
290/*+
291 *
292 * Routine Description:
293 * Send NULL packet to AP for notification power state of STA
294 *
295 * Return Value:
296 * None.
297 *
298-*/
Charles Clément7b6a0012010-08-01 17:15:50 +0200299bool
Forest Bond5449c682009-04-25 10:30:44 -0400300PSbSendNullPacket(
Charles Clément3a215e02010-05-12 20:54:39 -0700301 void *hDeviceContext
Forest Bond5449c682009-04-25 10:30:44 -0400302 )
303{
304 PSDevice pDevice = (PSDevice)hDeviceContext;
305 PSTxMgmtPacket pTxPacket = NULL;
306 PSMgmtObject pMgmt = pDevice->pMgmt;
Charles Clémentb6e95cd2010-06-02 09:52:01 -0700307 unsigned int uIdx;
Forest Bond5449c682009-04-25 10:30:44 -0400308
309
Charles Clément5a5a2a62010-08-01 17:15:49 +0200310 if (pDevice->bLinkPass == false) {
311 return false;
Forest Bond5449c682009-04-25 10:30:44 -0400312 }
313 #ifdef TxInSleep
Charles Clément5a5a2a62010-08-01 17:15:49 +0200314 if ((pDevice->bEnablePSMode == false) &&
315 (pDevice->fTxDataInSleep == false)){
316 return false;
Forest Bond5449c682009-04-25 10:30:44 -0400317 }
318#else
Charles Clément5a5a2a62010-08-01 17:15:49 +0200319 if (pDevice->bEnablePSMode == false) {
320 return false;
Forest Bond5449c682009-04-25 10:30:44 -0400321 }
322#endif
323 if (pDevice->bEnablePSMode) {
324 for (uIdx = 0; uIdx < TYPE_MAXTD; uIdx ++) {
325 if (pDevice->iTDUsed[uIdx] != 0)
Charles Clément5a5a2a62010-08-01 17:15:49 +0200326 return false;
Forest Bond5449c682009-04-25 10:30:44 -0400327 }
328 }
329
330 memset(pMgmt->pbyPSPacketPool, 0, sizeof(STxMgmtPacket) + WLAN_NULLDATA_FR_MAXLEN);
331 pTxPacket = (PSTxMgmtPacket)pMgmt->pbyPSPacketPool;
Charles Clément2989e962010-06-05 15:13:47 -0700332 pTxPacket->p80211Header = (PUWLAN_80211HDR)((unsigned char *)pTxPacket + sizeof(STxMgmtPacket));
Forest Bond5449c682009-04-25 10:30:44 -0400333
334 if (pDevice->bEnablePSMode) {
335
336 pTxPacket->p80211Header->sA3.wFrameCtl = cpu_to_le16(
337 (
338 WLAN_SET_FC_FTYPE(WLAN_TYPE_DATA) |
339 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_NULL) |
340 WLAN_SET_FC_PWRMGT(1)
341 ));
342 }
343 else {
344 pTxPacket->p80211Header->sA3.wFrameCtl = cpu_to_le16(
345 (
346 WLAN_SET_FC_FTYPE(WLAN_TYPE_DATA) |
347 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_NULL) |
348 WLAN_SET_FC_PWRMGT(0)
349 ));
350 }
351
352 if(pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
Charles Clément2986db52010-06-24 11:02:26 -0700353 pTxPacket->p80211Header->sA3.wFrameCtl |= cpu_to_le16((unsigned short)WLAN_SET_FC_TODS(1));
Forest Bond5449c682009-04-25 10:30:44 -0400354 }
355
356 memcpy(pTxPacket->p80211Header->sA3.abyAddr1, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN);
357 memcpy(pTxPacket->p80211Header->sA3.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
358 memcpy(pTxPacket->p80211Header->sA3.abyAddr3, pMgmt->abyCurrBSSID, WLAN_BSSID_LEN);
359 pTxPacket->cbMPDULen = WLAN_HDR_ADDR3_LEN;
360 pTxPacket->cbPayloadLen = 0;
361 // send the frame
362 if (csMgmt_xmit(pDevice, pTxPacket) != CMD_STATUS_PENDING) {
Jim Lieb7e809a92009-07-30 10:27:21 -0700363 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send Null Packet failed !\n");
Charles Clément5a5a2a62010-08-01 17:15:49 +0200364 return false;
Forest Bond5449c682009-04-25 10:30:44 -0400365 }
366 else {
367
Jim Lieb7e809a92009-07-30 10:27:21 -0700368// DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send Null Packet success....\n");
Forest Bond5449c682009-04-25 10:30:44 -0400369 }
370
371
Charles Clément1b120682010-08-01 17:15:48 +0200372 return true ;
Forest Bond5449c682009-04-25 10:30:44 -0400373}
374
375/*+
376 *
377 * Routine Description:
378 * Check if Next TBTT must wake up
379 *
380 * Return Value:
381 * None.
382 *
383-*/
384
Charles Clément7b6a0012010-08-01 17:15:50 +0200385bool
Forest Bond5449c682009-04-25 10:30:44 -0400386PSbIsNextTBTTWakeUp(
Charles Clément3a215e02010-05-12 20:54:39 -0700387 void *hDeviceContext
Forest Bond5449c682009-04-25 10:30:44 -0400388 )
389{
390
391 PSDevice pDevice = (PSDevice)hDeviceContext;
392 PSMgmtObject pMgmt = pDevice->pMgmt;
Charles Clément7b6a0012010-08-01 17:15:50 +0200393 bool bWakeUp = false;
Forest Bond5449c682009-04-25 10:30:44 -0400394
395 if (pMgmt->wListenInterval >= 2) {
396 if (pMgmt->wCountToWakeUp == 0) {
397 pMgmt->wCountToWakeUp = pMgmt->wListenInterval;
398 }
399
400 pMgmt->wCountToWakeUp --;
401
402 if (pMgmt->wCountToWakeUp == 1) {
403 // Turn on wake up to listen next beacon
404 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_LNBCN);
Charles Clément1b120682010-08-01 17:15:48 +0200405 bWakeUp = true;
Forest Bond5449c682009-04-25 10:30:44 -0400406 }
407
408 }
409
410 return bWakeUp;
411}
412