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
50 static int msglevel
= MSG_LEVEL_INFO
;
54 * Routine Description:
55 * Enable hw power saving functions
62 void PSvEnablePowerSaving(struct vnt_private
*pDevice
, u16 wListenInterval
)
64 struct vnt_manager
*pMgmt
= &pDevice
->vnt_mgmt
;
65 u16 wAID
= pMgmt
->wCurrAID
| BIT14
| BIT15
;
67 /* set period of power up before TBTT */
68 MACvWriteWord(pDevice
, MAC_REG_PWBT
, C_PWBT
);
70 if (pDevice
->op_mode
!= NL80211_IFTYPE_ADHOC
) {
72 MACvWriteWord(pDevice
, MAC_REG_AIDATIM
, wAID
);
75 /* Warren:06-18-2004,the sequence must follow
76 * PSEN->AUTOSLEEP->GO2DOZE
78 /* enable power saving hw function */
79 MACvRegBitsOn(pDevice
, MAC_REG_PSCTL
, PSCTL_PSEN
);
82 MACvRegBitsOn(pDevice
, MAC_REG_PSCFG
, PSCFG_AUTOSLEEP
);
84 /* Warren:MUST turn on this once before turn on AUTOSLEEP ,or the
85 * AUTOSLEEP doesn't work
87 MACvRegBitsOn(pDevice
, MAC_REG_PSCTL
, PSCTL_GO2DOZE
);
89 if (wListenInterval
>= 2) {
91 /* clear always listen beacon */
92 MACvRegBitsOff(pDevice
, MAC_REG_PSCTL
, PSCTL_ALBCN
);
94 /* first time set listen next beacon */
95 MACvRegBitsOn(pDevice
, MAC_REG_PSCTL
, PSCTL_LNBCN
);
97 pMgmt
->wCountToWakeUp
= wListenInterval
;
101 /* always listen beacon */
102 MACvRegBitsOn(pDevice
, MAC_REG_PSCTL
, PSCTL_ALBCN
);
104 pMgmt
->wCountToWakeUp
= 0;
107 pDevice
->bEnablePSMode
= true;
109 /* We don't send null pkt in ad hoc mode
110 * since beacon will handle this.
112 if (pDevice
->op_mode
== NL80211_IFTYPE_STATION
)
113 PSbSendNullPacket(pDevice
);
115 pDevice
->bPWBitOn
= true;
116 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"PS:Power Saving Mode Enable...\n");
121 * Routine Description:
122 * Disable hw power saving functions
129 void PSvDisablePowerSaving(struct vnt_private
*pDevice
)
132 /* disable power saving hw function */
133 CONTROLnsRequestOut(pDevice
, MESSAGE_TYPE_DISABLE_PS
, 0,
136 /* clear AutoSleep */
137 MACvRegBitsOff(pDevice
, MAC_REG_PSCFG
, PSCFG_AUTOSLEEP
);
139 /* set always listen beacon */
140 MACvRegBitsOn(pDevice
, MAC_REG_PSCTL
, PSCTL_ALBCN
);
141 pDevice
->bEnablePSMode
= false;
143 if (pDevice
->op_mode
== NL80211_IFTYPE_STATION
)
144 PSbSendNullPacket(pDevice
);
146 pDevice
->bPWBitOn
= false;
151 * Routine Description:
152 * Consider to power down when no more packets to tx or rx.
155 * true, if power down success
159 int PSbConsiderPowerDown(struct vnt_private
*pDevice
, int bCheckRxDMA
,
160 int bCheckCountToWakeUp
)
162 struct vnt_manager
*pMgmt
= &pDevice
->vnt_mgmt
;
165 /* check if already in Doze mode */
166 ControlvReadByte(pDevice
, MESSAGE_REQUEST_MACREG
,
167 MAC_REG_PSCTL
, &byData
);
169 if ((byData
& PSCTL_PS
) != 0)
172 if (pMgmt
->eCurrMode
!= WMAC_MODE_IBSS_STA
) {
173 /* check if in TIM wake period */
174 if (pMgmt
->bInTIMWake
)
178 /* check scan state */
179 if (pDevice
->bCmdRunning
)
183 if (pDevice
->bPSModeTxBurst
)
187 MACvRegBitsOn(pDevice
, MAC_REG_PSCTL
, PSCTL_PSEN
);
189 if (pMgmt
->eCurrMode
!= WMAC_MODE_IBSS_STA
) {
190 if (bCheckCountToWakeUp
&& (pMgmt
->wCountToWakeUp
== 0
191 || pMgmt
->wCountToWakeUp
== 1)) {
196 pDevice
->bPSRxBeacon
= true;
198 /* no Tx, no Rx isr, now go to Doze */
199 MACvRegBitsOn(pDevice
, MAC_REG_PSCTL
, PSCTL_GO2DOZE
);
200 DBG_PRT(MSG_LEVEL_DEBUG
, KERN_INFO
"Go to Doze ZZZZZZZZZZZZZZZ\n");
206 * Routine Description:
207 * Send PS-POLL packet
214 void PSvSendPSPOLL(struct vnt_private
*pDevice
)
216 struct vnt_manager
*pMgmt
= &pDevice
->vnt_mgmt
;
217 struct vnt_tx_mgmt
*pTxPacket
= NULL
;
219 memset(pMgmt
->pbyPSPacketPool
, 0, sizeof(struct vnt_tx_mgmt
)
220 + WLAN_HDR_ADDR2_LEN
);
221 pTxPacket
= (struct vnt_tx_mgmt
*)pMgmt
->pbyPSPacketPool
;
222 pTxPacket
->p80211Header
= (PUWLAN_80211HDR
)((u8
*)pTxPacket
223 + sizeof(struct vnt_tx_mgmt
));
225 pTxPacket
->p80211Header
->sA2
.wFrameCtl
= cpu_to_le16(
227 WLAN_SET_FC_FTYPE(WLAN_TYPE_CTL
) |
228 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_PSPOLL
) |
229 WLAN_SET_FC_PWRMGT(0)
232 pTxPacket
->p80211Header
->sA2
.wDurationID
=
233 pMgmt
->wCurrAID
| BIT14
| BIT15
;
234 memcpy(pTxPacket
->p80211Header
->sA2
.abyAddr1
, pMgmt
->abyCurrBSSID
,
236 memcpy(pTxPacket
->p80211Header
->sA2
.abyAddr2
, pMgmt
->abyMACAddr
,
238 pTxPacket
->cbMPDULen
= WLAN_HDR_ADDR2_LEN
;
239 pTxPacket
->cbPayloadLen
= 0;
241 /* log failure if sending failed */
242 if (csMgmt_xmit(pDevice
, pTxPacket
) != CMD_STATUS_PENDING
)
243 DBG_PRT(MSG_LEVEL_DEBUG
,
244 KERN_INFO
"Send PS-Poll packet failed..\n");
249 * Routine Description:
250 * Send NULL packet to AP for notification power state of STA
257 int PSbSendNullPacket(struct vnt_private
*pDevice
)
259 struct vnt_tx_mgmt
*pTxPacket
= NULL
;
260 struct vnt_manager
*pMgmt
= &pDevice
->vnt_mgmt
;
263 if (pDevice
->bLinkPass
== false)
266 if (pDevice
->bEnablePSMode
== false && pDevice
->tx_trigger
== false)
269 memset(pMgmt
->pbyPSPacketPool
, 0, sizeof(struct vnt_tx_mgmt
)
270 + WLAN_NULLDATA_FR_MAXLEN
);
271 pTxPacket
= (struct vnt_tx_mgmt
*)pMgmt
->pbyPSPacketPool
;
272 pTxPacket
->p80211Header
= (PUWLAN_80211HDR
)((u8
*)pTxPacket
273 + sizeof(struct vnt_tx_mgmt
));
275 flags
= WLAN_SET_FC_FTYPE(WLAN_TYPE_DATA
) |
276 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_NULL
);
278 if (pDevice
->bEnablePSMode
)
279 flags
|= WLAN_SET_FC_PWRMGT(1);
281 flags
|= WLAN_SET_FC_PWRMGT(0);
283 pTxPacket
->p80211Header
->sA3
.wFrameCtl
= cpu_to_le16(flags
);
285 if (pMgmt
->eCurrMode
!= WMAC_MODE_IBSS_STA
)
286 pTxPacket
->p80211Header
->sA3
.wFrameCtl
|=
287 cpu_to_le16((u16
)WLAN_SET_FC_TODS(1));
289 memcpy(pTxPacket
->p80211Header
->sA3
.abyAddr1
, pMgmt
->abyCurrBSSID
,
291 memcpy(pTxPacket
->p80211Header
->sA3
.abyAddr2
, pMgmt
->abyMACAddr
,
293 memcpy(pTxPacket
->p80211Header
->sA3
.abyAddr3
, pMgmt
->abyCurrBSSID
,
295 pTxPacket
->cbMPDULen
= WLAN_HDR_ADDR3_LEN
;
296 pTxPacket
->cbPayloadLen
= 0;
297 /* log error if sending failed */
298 if (csMgmt_xmit(pDevice
, pTxPacket
) != CMD_STATUS_PENDING
) {
299 DBG_PRT(MSG_LEVEL_DEBUG
,
300 KERN_INFO
"Send Null Packet failed !\n");
308 * Routine Description:
309 * Check if Next TBTT must wake up
316 int PSbIsNextTBTTWakeUp(struct vnt_private
*pDevice
)
318 struct vnt_manager
*pMgmt
= &pDevice
->vnt_mgmt
;
321 if (pMgmt
->wListenInterval
>= 2) {
322 if (pMgmt
->wCountToWakeUp
== 0)
323 pMgmt
->wCountToWakeUp
= pMgmt
->wListenInterval
;
325 pMgmt
->wCountToWakeUp
--;
327 if (pMgmt
->wCountToWakeUp
== 1) {
328 /* Turn on wake up to listen next beacon */
329 MACvRegBitsOn(pDevice
, MAC_REG_PSCTL
, PSCTL_LNBCN
);
330 pDevice
->bPSRxBeacon
= false;
332 } else if (!pDevice
->bPSRxBeacon
) {
333 /* Listen until RxBeacon */
334 MACvRegBitsOn(pDevice
, MAC_REG_PSCTL
, PSCTL_LNBCN
);