xenbus_client.c: correct exit path for xenbus_map_ring_valloc_hvm
[linux/fpc-iii.git] / drivers / staging / vt6656 / power.c
blobedc8975b2e2a0ceae17aabea15813285cd8c3846
1 /*
2 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3 * All rights reserved.
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.
20 * File: power.c
22 * Purpose: Handles 802.11 power management functions
24 * Author: Lyndon Chen
26 * Date: July 17, 2002
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
36 * Revision History:
40 #include "mac.h"
41 #include "device.h"
42 #include "wmgr.h"
43 #include "power.h"
44 #include "wcmd.h"
45 #include "rxtx.h"
46 #include "card.h"
47 #include "control.h"
48 #include "rndis.h"
50 static int msglevel = MSG_LEVEL_INFO;
54 * Routine Description:
55 * Enable hw power saving functions
57 * Return Value:
58 * None.
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->eOPMode != OP_MODE_ADHOC) {
71 /* set AID */
72 MACvWriteWord(pDevice, MAC_REG_AIDATIM, wAID);
73 } else {
74 /* set ATIM Window */
75 /* MACvWriteATIMW(pDevice->PortOffset, pMgmt->wCurrATIMWindow); */
78 /* Warren:06-18-2004,the sequence must follow PSEN->AUTOSLEEP->GO2DOZE */
79 /* enable power saving hw function */
80 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_PSEN);
82 /* Set AutoSleep */
83 MACvRegBitsOn(pDevice, MAC_REG_PSCFG, PSCFG_AUTOSLEEP);
85 /* Warren:MUST turn on this once before turn on AUTOSLEEP ,or the AUTOSLEEP doesn't work */
86 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_GO2DOZE);
88 if (wListenInterval >= 2) {
90 /* clear always listen beacon */
91 MACvRegBitsOff(pDevice, MAC_REG_PSCTL, PSCTL_ALBCN);
93 /* first time set listen next beacon */
94 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_LNBCN);
96 pMgmt->wCountToWakeUp = wListenInterval;
98 } else {
100 /* always listen beacon */
101 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_ALBCN);
103 pMgmt->wCountToWakeUp = 0;
106 pDevice->bEnablePSMode = true;
108 /* We don't send null pkt in ad hoc mode since beacon will handle this. */
109 if (pDevice->eOPMode == OP_MODE_INFRASTRUCTURE)
110 PSbSendNullPacket(pDevice);
112 pDevice->bPWBitOn = true;
113 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "PS:Power Saving Mode Enable...\n");
118 * Routine Description:
119 * Disable hw power saving functions
121 * Return Value:
122 * None.
126 void PSvDisablePowerSaving(struct vnt_private *pDevice)
129 /* disable power saving hw function */
130 CONTROLnsRequestOut(pDevice, MESSAGE_TYPE_DISABLE_PS, 0,
131 0, 0, NULL);
133 /* clear AutoSleep */
134 MACvRegBitsOff(pDevice, MAC_REG_PSCFG, PSCFG_AUTOSLEEP);
136 /* set always listen beacon */
137 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_ALBCN);
138 pDevice->bEnablePSMode = false;
140 if (pDevice->eOPMode == OP_MODE_INFRASTRUCTURE)
141 PSbSendNullPacket(pDevice);
143 pDevice->bPWBitOn = false;
148 * Routine Description:
149 * Consider to power down when no more packets to tx or rx.
151 * Return Value:
152 * true, if power down success
153 * false, if fail
156 int PSbConsiderPowerDown(struct vnt_private *pDevice, int bCheckRxDMA,
157 int bCheckCountToWakeUp)
159 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
160 u8 byData;
162 /* check if already in Doze mode */
163 ControlvReadByte(pDevice, MESSAGE_REQUEST_MACREG,
164 MAC_REG_PSCTL, &byData);
166 if ((byData & PSCTL_PS) != 0)
167 return true;
169 if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
170 /* check if in TIM wake period */
171 if (pMgmt->bInTIMWake)
172 return false;
175 /* check scan state */
176 if (pDevice->bCmdRunning)
177 return false;
179 /* Tx Burst */
180 if (pDevice->bPSModeTxBurst)
181 return false;
183 /* Froce PSEN on */
184 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_PSEN);
186 if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
187 if (bCheckCountToWakeUp && (pMgmt->wCountToWakeUp == 0
188 || pMgmt->wCountToWakeUp == 1)) {
189 return false;
193 pDevice->bPSRxBeacon = true;
195 /* no Tx, no Rx isr, now go to Doze */
196 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_GO2DOZE);
197 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Go to Doze ZZZZZZZZZZZZZZZ\n");
198 return true;
203 * Routine Description:
204 * Send PS-POLL packet
206 * Return Value:
207 * None.
211 void PSvSendPSPOLL(struct vnt_private *pDevice)
213 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
214 struct vnt_tx_mgmt *pTxPacket = NULL;
216 memset(pMgmt->pbyPSPacketPool, 0, sizeof(struct vnt_tx_mgmt)
217 + WLAN_HDR_ADDR2_LEN);
218 pTxPacket = (struct vnt_tx_mgmt *)pMgmt->pbyPSPacketPool;
219 pTxPacket->p80211Header = (PUWLAN_80211HDR)((u8 *)pTxPacket
220 + sizeof(struct vnt_tx_mgmt));
222 pTxPacket->p80211Header->sA2.wFrameCtl = cpu_to_le16(
224 WLAN_SET_FC_FTYPE(WLAN_TYPE_CTL) |
225 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_PSPOLL) |
226 WLAN_SET_FC_PWRMGT(0)
229 pTxPacket->p80211Header->sA2.wDurationID = pMgmt->wCurrAID | BIT14 | BIT15;
230 memcpy(pTxPacket->p80211Header->sA2.abyAddr1, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN);
231 memcpy(pTxPacket->p80211Header->sA2.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
232 pTxPacket->cbMPDULen = WLAN_HDR_ADDR2_LEN;
233 pTxPacket->cbPayloadLen = 0;
235 /* log failure if sending failed */
236 if (csMgmt_xmit(pDevice, pTxPacket) != CMD_STATUS_PENDING) {
237 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send PS-Poll packet failed..\n");
243 * Routine Description:
244 * Send NULL packet to AP for notification power state of STA
246 * Return Value:
247 * None.
251 int PSbSendNullPacket(struct vnt_private *pDevice)
253 struct vnt_tx_mgmt *pTxPacket = NULL;
254 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
255 u16 flags = 0;
257 if (pDevice->bLinkPass == false)
258 return false;
260 if ((pDevice->bEnablePSMode == false) &&
261 (pDevice->fTxDataInSleep == false)) {
262 return false;
265 memset(pMgmt->pbyPSPacketPool, 0, sizeof(struct vnt_tx_mgmt)
266 + WLAN_NULLDATA_FR_MAXLEN);
267 pTxPacket = (struct vnt_tx_mgmt *)pMgmt->pbyPSPacketPool;
268 pTxPacket->p80211Header = (PUWLAN_80211HDR)((u8 *)pTxPacket
269 + sizeof(struct vnt_tx_mgmt));
271 flags = WLAN_SET_FC_FTYPE(WLAN_TYPE_DATA) |
272 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_NULL);
274 if (pDevice->bEnablePSMode)
275 flags |= WLAN_SET_FC_PWRMGT(1);
276 else
277 flags |= WLAN_SET_FC_PWRMGT(0);
279 pTxPacket->p80211Header->sA3.wFrameCtl = cpu_to_le16(flags);
281 if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA)
282 pTxPacket->p80211Header->sA3.wFrameCtl |= cpu_to_le16((u16)WLAN_SET_FC_TODS(1));
284 memcpy(pTxPacket->p80211Header->sA3.abyAddr1, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN);
285 memcpy(pTxPacket->p80211Header->sA3.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
286 memcpy(pTxPacket->p80211Header->sA3.abyAddr3, pMgmt->abyCurrBSSID, WLAN_BSSID_LEN);
287 pTxPacket->cbMPDULen = WLAN_HDR_ADDR3_LEN;
288 pTxPacket->cbPayloadLen = 0;
289 /* log error if sending failed */
290 if (csMgmt_xmit(pDevice, pTxPacket) != CMD_STATUS_PENDING) {
291 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send Null Packet failed !\n");
292 return false;
294 return true;
299 * Routine Description:
300 * Check if Next TBTT must wake up
302 * Return Value:
303 * None.
307 int PSbIsNextTBTTWakeUp(struct vnt_private *pDevice)
309 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
310 int bWakeUp = false;
312 if (pMgmt->wListenInterval >= 2) {
313 if (pMgmt->wCountToWakeUp == 0)
314 pMgmt->wCountToWakeUp = pMgmt->wListenInterval;
316 pMgmt->wCountToWakeUp--;
318 if (pMgmt->wCountToWakeUp == 1) {
319 /* Turn on wake up to listen next beacon */
320 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_LNBCN);
321 pDevice->bPSRxBeacon = false;
322 bWakeUp = true;
323 } else if (!pDevice->bPSRxBeacon) {
324 /* Listen until RxBeacon */
325 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_LNBCN);
328 return bWakeUp;