staging: tidspbridge: ioremap dsp sync addr
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / vt6656 / power.c
CommitLineData
92b96797
FB
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 *
20 * File: power.c
21 *
a0a1f61a 22 * Purpose: Handles 802.11 power management functions
92b96797
FB
23 *
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
92b96797 40#include "ttype.h"
92b96797 41#include "mac.h"
92b96797 42#include "device.h"
92b96797 43#include "wmgr.h"
92b96797 44#include "power.h"
92b96797 45#include "wcmd.h"
92b96797 46#include "rxtx.h"
92b96797 47#include "card.h"
92b96797 48#include "control.h"
92b96797 49#include "rndis.h"
92b96797
FB
50
51/*--------------------- Static Definitions -------------------------*/
52
92b96797
FB
53/*--------------------- Static Classes ----------------------------*/
54
55/*--------------------- Static Variables --------------------------*/
f9cfbe94 56static int msglevel = MSG_LEVEL_INFO;
92b96797
FB
57/*--------------------- Static Functions --------------------------*/
58
92b96797
FB
59/*--------------------- Export Variables --------------------------*/
60
92b96797
FB
61/*--------------------- Export Functions --------------------------*/
62
7404eab2 63/*
92b96797
FB
64 *
65 * Routine Description:
66 * Enable hw power saving functions
67 *
68 * Return Value:
69 * None.
70 *
7404eab2 71 */
92b96797 72
0cbd8d98
AM
73void PSvEnablePowerSaving(void *hDeviceContext,
74 WORD wListenInterval)
92b96797 75{
f9cfbe94
PW
76 PSDevice pDevice = (PSDevice)hDeviceContext;
77 PSMgmtObject pMgmt = &(pDevice->sMgmtObj);
78 WORD wAID = pMgmt->wCurrAID | BIT14 | BIT15;
92b96797 79
f9cfbe94
PW
80 /* set period of power up before TBTT */
81 MACvWriteWord(pDevice, MAC_REG_PWBT, C_PWBT);
92b96797 82
f9cfbe94
PW
83 if (pDevice->eOPMode != OP_MODE_ADHOC) {
84 /* set AID */
85 MACvWriteWord(pDevice, MAC_REG_AIDATIM, wAID);
86 } else {
7404eab2
PW
87 /* set ATIM Window */
88 /* MACvWriteATIMW(pDevice->PortOffset, pMgmt->wCurrATIMWindow); */
f9cfbe94 89 }
92b96797 90
7404eab2
PW
91 /* Warren:06-18-2004,the sequence must follow PSEN->AUTOSLEEP->GO2DOZE */
92 /* enable power saving hw function */
f9cfbe94 93 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_PSEN);
92b96797 94
7404eab2 95 /* Set AutoSleep */
f9cfbe94 96 MACvRegBitsOn(pDevice, MAC_REG_PSCFG, PSCFG_AUTOSLEEP);
92b96797 97
7404eab2 98 /* Warren:MUST turn on this once before turn on AUTOSLEEP ,or the AUTOSLEEP doesn't work */
f9cfbe94 99 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_GO2DOZE);
92b96797 100
f9cfbe94 101 if (wListenInterval >= 2) {
92b96797 102
7404eab2 103 /* clear always listen beacon */
f9cfbe94 104 MACvRegBitsOff(pDevice, MAC_REG_PSCTL, PSCTL_ALBCN);
92b96797 105
7404eab2 106 /* first time set listen next beacon */
f9cfbe94 107 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_LNBCN);
92b96797 108
f9cfbe94 109 pMgmt->wCountToWakeUp = wListenInterval;
92b96797 110
f9cfbe94 111 } else {
92b96797 112
7404eab2 113 /* always listen beacon */
f9cfbe94 114 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_ALBCN);
92b96797 115
f9cfbe94
PW
116 pMgmt->wCountToWakeUp = 0;
117 }
118
119 pDevice->bEnablePSMode = TRUE;
120
7404eab2 121 /* We don't send null pkt in ad hoc mode since beacon will handle this. */
8c811616 122 if (pDevice->eOPMode == OP_MODE_INFRASTRUCTURE)
f9cfbe94 123 PSbSendNullPacket(pDevice);
f9cfbe94
PW
124
125 pDevice->bPWBitOn = TRUE;
c5b0b5fc 126 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "PS:Power Saving Mode Enable...\n");
92b96797
FB
127}
128
7404eab2 129/*
92b96797
FB
130 *
131 * Routine Description:
132 * Disable hw power saving functions
133 *
134 * Return Value:
135 * None.
136 *
7404eab2 137 */
92b96797 138
0cbd8d98 139void PSvDisablePowerSaving(void *hDeviceContext)
92b96797 140{
f9cfbe94 141 PSDevice pDevice = (PSDevice)hDeviceContext;
7404eab2 142 /* PSMgmtObject pMgmt = &(pDevice->sMgmtObj); */
92b96797 143
7404eab2 144 /* disable power saving hw function */
f9cfbe94
PW
145 CONTROLnsRequestOut(pDevice, MESSAGE_TYPE_DISABLE_PS, 0,
146 0, 0, NULL);
92b96797 147
7404eab2 148 /* clear AutoSleep */
f9cfbe94 149 MACvRegBitsOff(pDevice, MAC_REG_PSCFG, PSCFG_AUTOSLEEP);
92b96797 150
7404eab2 151 /* set always listen beacon */
f9cfbe94
PW
152 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_ALBCN);
153 pDevice->bEnablePSMode = FALSE;
92b96797 154
036dba14 155 if (pDevice->eOPMode == OP_MODE_INFRASTRUCTURE)
f9cfbe94 156 PSbSendNullPacket(pDevice);
92b96797 157
f9cfbe94 158 pDevice->bPWBitOn = FALSE;
92b96797
FB
159}
160
7404eab2 161/*
92b96797
FB
162 *
163 * Routine Description:
164 * Consider to power down when no more packets to tx or rx.
165 *
166 * Return Value:
167 * TRUE, if power down success
168 * FALSE, if fail
7404eab2 169 */
92b96797 170
0cbd8d98
AM
171BOOL PSbConsiderPowerDown(void *hDeviceContext,
172 BOOL bCheckRxDMA,
173 BOOL bCheckCountToWakeUp)
92b96797 174{
f9cfbe94
PW
175 PSDevice pDevice = (PSDevice)hDeviceContext;
176 PSMgmtObject pMgmt = &(pDevice->sMgmtObj);
177 BYTE byData;
178
7404eab2 179 /* check if already in Doze mode */
f9cfbe94
PW
180 ControlvReadByte(pDevice, MESSAGE_REQUEST_MACREG,
181 MAC_REG_PSCTL, &byData);
182
c5b0b5fc 183 if ((byData & PSCTL_PS) != 0)
f9cfbe94
PW
184 return TRUE;
185
186 if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
7404eab2 187 /* check if in TIM wake period */
f9cfbe94
PW
188 if (pMgmt->bInTIMWake)
189 return FALSE;
190 }
191
7404eab2 192 /* check scan state */
f9cfbe94
PW
193 if (pDevice->bCmdRunning)
194 return FALSE;
195
7404eab2 196 /* Tx Burst */
c5b0b5fc 197 if (pDevice->bPSModeTxBurst)
f9cfbe94
PW
198 return FALSE;
199
7404eab2 200 /* Froce PSEN on */
f9cfbe94
PW
201 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_PSEN);
202
203 if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
204 if (bCheckCountToWakeUp && (pMgmt->wCountToWakeUp == 0
205 || pMgmt->wCountToWakeUp == 1)) {
206 return FALSE;
207 }
208 }
209
210 pDevice->bPSRxBeacon = TRUE;
211
7404eab2 212 /* no Tx, no Rx isr, now go to Doze */
f9cfbe94
PW
213 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_GO2DOZE);
214 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Go to Doze ZZZZZZZZZZZZZZZ\n");
215 return TRUE;
92b96797
FB
216}
217
7404eab2 218/*
92b96797
FB
219 *
220 * Routine Description:
221 * Send PS-POLL packet
222 *
223 * Return Value:
224 * None.
225 *
7404eab2 226 */
92b96797 227
0cbd8d98 228void PSvSendPSPOLL(void *hDeviceContext)
92b96797 229{
f9cfbe94
PW
230 PSDevice pDevice = (PSDevice)hDeviceContext;
231 PSMgmtObject pMgmt = &(pDevice->sMgmtObj);
232 PSTxMgmtPacket pTxPacket = NULL;
233
234 memset(pMgmt->pbyPSPacketPool, 0, sizeof(STxMgmtPacket) + WLAN_HDR_ADDR2_LEN);
235 pTxPacket = (PSTxMgmtPacket)pMgmt->pbyPSPacketPool;
236 pTxPacket->p80211Header = (PUWLAN_80211HDR)((PBYTE)pTxPacket + sizeof(STxMgmtPacket));
237 pTxPacket->p80211Header->sA2.wFrameCtl = cpu_to_le16(
238 (
239 WLAN_SET_FC_FTYPE(WLAN_TYPE_CTL) |
240 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_PSPOLL) |
241 WLAN_SET_FC_PWRMGT(0)
242 ));
243
244 pTxPacket->p80211Header->sA2.wDurationID = pMgmt->wCurrAID | BIT14 | BIT15;
245 memcpy(pTxPacket->p80211Header->sA2.abyAddr1, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN);
246 memcpy(pTxPacket->p80211Header->sA2.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
247 pTxPacket->cbMPDULen = WLAN_HDR_ADDR2_LEN;
248 pTxPacket->cbPayloadLen = 0;
249
e25b75ec 250 /* log failure if sending failed */
f9cfbe94
PW
251 if (csMgmt_xmit(pDevice, pTxPacket) != CMD_STATUS_PENDING) {
252 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send PS-Poll packet failed..\n");
e25b75ec 253 }
92b96797
FB
254}
255
7404eab2 256/*
92b96797
FB
257 *
258 * Routine Description:
259 * Send NULL packet to AP for notification power state of STA
260 *
261 * Return Value:
262 * None.
263 *
7404eab2 264 */
0cbd8d98
AM
265
266BOOL PSbSendNullPacket(void *hDeviceContext)
92b96797 267{
f9cfbe94
PW
268 PSDevice pDevice = (PSDevice)hDeviceContext;
269 PSTxMgmtPacket pTxPacket = NULL;
270 PSMgmtObject pMgmt = &(pDevice->sMgmtObj);
b898cf21 271 u16 flags = 0;
f9cfbe94 272
036dba14 273 if (pDevice->bLinkPass == FALSE)
f9cfbe94 274 return FALSE;
f9cfbe94
PW
275
276 if ((pDevice->bEnablePSMode == FALSE) &&
036dba14 277 (pDevice->fTxDataInSleep == FALSE)) {
f9cfbe94
PW
278 return FALSE;
279 }
280
281 memset(pMgmt->pbyPSPacketPool, 0, sizeof(STxMgmtPacket) + WLAN_NULLDATA_FR_MAXLEN);
282 pTxPacket = (PSTxMgmtPacket)pMgmt->pbyPSPacketPool;
283 pTxPacket->p80211Header = (PUWLAN_80211HDR)((PBYTE)pTxPacket + sizeof(STxMgmtPacket));
284
b898cf21
PW
285 flags = WLAN_SET_FC_FTYPE(WLAN_TYPE_DATA) |
286 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_NULL);
287
288 if (pDevice->bEnablePSMode)
289 flags |= WLAN_SET_FC_PWRMGT(1);
290 else
291 flags |= WLAN_SET_FC_PWRMGT(0);
292
293 pTxPacket->p80211Header->sA3.wFrameCtl = cpu_to_le16(flags);
f9cfbe94 294
c5b0b5fc 295 if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA)
f9cfbe94 296 pTxPacket->p80211Header->sA3.wFrameCtl |= cpu_to_le16((WORD)WLAN_SET_FC_TODS(1));
f9cfbe94
PW
297
298 memcpy(pTxPacket->p80211Header->sA3.abyAddr1, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN);
299 memcpy(pTxPacket->p80211Header->sA3.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
300 memcpy(pTxPacket->p80211Header->sA3.abyAddr3, pMgmt->abyCurrBSSID, WLAN_BSSID_LEN);
301 pTxPacket->cbMPDULen = WLAN_HDR_ADDR3_LEN;
302 pTxPacket->cbPayloadLen = 0;
e25b75ec 303 /* log error if sending failed */
f9cfbe94
PW
304 if (csMgmt_xmit(pDevice, pTxPacket) != CMD_STATUS_PENDING) {
305 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send Null Packet failed !\n");
306 return FALSE;
f9cfbe94 307 }
e25b75ec 308 return TRUE;
92b96797
FB
309}
310
7404eab2 311/*
92b96797
FB
312 *
313 * Routine Description:
314 * Check if Next TBTT must wake up
315 *
316 * Return Value:
317 * None.
318 *
7404eab2 319 */
92b96797 320
0cbd8d98 321BOOL PSbIsNextTBTTWakeUp(void *hDeviceContext)
92b96797 322{
f9cfbe94
PW
323 PSDevice pDevice = (PSDevice)hDeviceContext;
324 PSMgmtObject pMgmt = &(pDevice->sMgmtObj);
325 BOOL bWakeUp = FALSE;
326
327 if (pMgmt->wListenInterval >= 2) {
036dba14 328 if (pMgmt->wCountToWakeUp == 0)
f9cfbe94 329 pMgmt->wCountToWakeUp = pMgmt->wListenInterval;
f9cfbe94 330
c5b0b5fc 331 pMgmt->wCountToWakeUp--;
f9cfbe94
PW
332
333 if (pMgmt->wCountToWakeUp == 1) {
7404eab2 334 /* Turn on wake up to listen next beacon */
f9cfbe94
PW
335 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_LNBCN);
336 pDevice->bPSRxBeacon = FALSE;
337 bWakeUp = TRUE;
c5b0b5fc 338 } else if (!pDevice->bPSRxBeacon) {
7404eab2 339 /* Listen until RxBeacon */
f9cfbe94
PW
340 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_LNBCN);
341 }
342 }
343 return bWakeUp;
92b96797
FB
344}
345