Btrfs: device_list_add() should not update list when mounted
[linux/fpc-iii.git] / drivers / staging / vt6655 / power.c
blob5dfa911c6f49d0be36257c588c46abd15480efe9
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 "ttype.h"
41 #include "mac.h"
42 #include "device.h"
43 #include "wmgr.h"
44 #include "power.h"
45 #include "wcmd.h"
46 #include "rxtx.h"
47 #include "card.h"
49 /*--------------------- Static Definitions -------------------------*/
51 /*--------------------- Static Classes ----------------------------*/
53 /*--------------------- Static Variables --------------------------*/
54 static int msglevel = MSG_LEVEL_INFO;
55 /*--------------------- Static Functions --------------------------*/
57 /*--------------------- Export Variables --------------------------*/
59 /*--------------------- Export Functions --------------------------*/
61 /*+
63 * Routine Description:
64 * Enable hw power saving functions
66 * Return Value:
67 * None.
69 -*/
71 void
72 PSvEnablePowerSaving(
73 void *hDeviceContext,
74 unsigned short wListenInterval
77 PSDevice pDevice = (PSDevice)hDeviceContext;
78 PSMgmtObject pMgmt = pDevice->pMgmt;
79 unsigned short wAID = pMgmt->wCurrAID | BIT14 | BIT15;
81 // set period of power up before TBTT
82 VNSvOutPortW(pDevice->PortOffset + MAC_REG_PWBT, C_PWBT);
83 if (pDevice->eOPMode != OP_MODE_ADHOC) {
84 // set AID
85 VNSvOutPortW(pDevice->PortOffset + MAC_REG_AIDATIM, wAID);
86 } else {
87 // set ATIM Window
88 MACvWriteATIMW(pDevice->PortOffset, pMgmt->wCurrATIMWindow);
90 // Set AutoSleep
91 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCFG, PSCFG_AUTOSLEEP);
92 // Set HWUTSF
93 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_TFTCTL, TFTCTL_HWUTSF);
95 if (wListenInterval >= 2) {
96 // clear always listen beacon
97 MACvRegBitsOff(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_ALBCN);
98 // first time set listen next beacon
99 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_LNBCN);
100 pMgmt->wCountToWakeUp = wListenInterval;
101 } else {
102 // always listen beacon
103 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_ALBCN);
104 pMgmt->wCountToWakeUp = 0;
107 // enable power saving hw function
108 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PSEN);
109 pDevice->bEnablePSMode = true;
111 /* We don't send null pkt in ad hoc mode since beacon will handle this. */
112 if (pDevice->eOPMode != OP_MODE_ADHOC && pDevice->eOPMode == OP_MODE_INFRASTRUCTURE)
113 PSbSendNullPacket(pDevice);
115 pDevice->bPWBitOn = true;
116 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "PS:Power Saving Mode Enable... \n");
117 return;
122 * Routine Description:
123 * Disable hw power saving functions
125 * Return Value:
126 * None.
130 void
131 PSvDisablePowerSaving(
132 void *hDeviceContext
135 PSDevice pDevice = (PSDevice)hDeviceContext;
137 // disable power saving hw function
138 MACbPSWakeup(pDevice->PortOffset);
139 //clear AutoSleep
140 MACvRegBitsOff(pDevice->PortOffset, MAC_REG_PSCFG, PSCFG_AUTOSLEEP);
141 //clear HWUTSF
142 MACvRegBitsOff(pDevice->PortOffset, MAC_REG_TFTCTL, TFTCTL_HWUTSF);
143 // set always listen beacon
144 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_ALBCN);
146 pDevice->bEnablePSMode = false;
148 if (pDevice->eOPMode == OP_MODE_INFRASTRUCTURE)
149 PSbSendNullPacket(pDevice);
151 pDevice->bPWBitOn = false;
152 return;
157 * Routine Description:
158 * Consider to power down when no more packets to tx or rx.
160 * Return Value:
161 * true, if power down success
162 * false, if fail
165 bool
166 PSbConsiderPowerDown(
167 void *hDeviceContext,
168 bool bCheckRxDMA,
169 bool bCheckCountToWakeUp
172 PSDevice pDevice = (PSDevice)hDeviceContext;
173 PSMgmtObject pMgmt = pDevice->pMgmt;
174 unsigned int uIdx;
176 // check if already in Doze mode
177 if (MACbIsRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PS))
178 return true;
180 if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
181 // check if in TIM wake period
182 if (pMgmt->bInTIMWake)
183 return false;
186 // check scan state
187 if (pDevice->bCmdRunning)
188 return false;
190 // Force PSEN on
191 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PSEN);
193 // check if all TD are empty,
194 for (uIdx = 0; uIdx < TYPE_MAXTD; uIdx++) {
195 if (pDevice->iTDUsed[uIdx] != 0)
196 return false;
199 // check if rx isr is clear
200 if (bCheckRxDMA &&
201 ((pDevice->dwIsr & ISR_RXDMA0) != 0) &&
202 ((pDevice->dwIsr & ISR_RXDMA1) != 0)) {
203 return false;
206 if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
207 if (bCheckCountToWakeUp &&
208 (pMgmt->wCountToWakeUp == 0 || pMgmt->wCountToWakeUp == 1)) {
209 return false;
213 // no Tx, no Rx isr, now go to Doze
214 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_GO2DOZE);
215 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Go to Doze ZZZZZZZZZZZZZZZ\n");
216 return true;
221 * Routine Description:
222 * Send PS-POLL packet
224 * Return Value:
225 * None.
229 void
230 PSvSendPSPOLL(
231 void *hDeviceContext
234 PSDevice pDevice = (PSDevice)hDeviceContext;
235 PSMgmtObject pMgmt = pDevice->pMgmt;
236 PSTxMgmtPacket pTxPacket = NULL;
238 memset(pMgmt->pbyPSPacketPool, 0, sizeof(STxMgmtPacket) + WLAN_HDR_ADDR2_LEN);
239 pTxPacket = (PSTxMgmtPacket)pMgmt->pbyPSPacketPool;
240 pTxPacket->p80211Header = (PUWLAN_80211HDR)((unsigned char *)pTxPacket + sizeof(STxMgmtPacket));
241 pTxPacket->p80211Header->sA2.wFrameCtl = cpu_to_le16(
243 WLAN_SET_FC_FTYPE(WLAN_TYPE_CTL) |
244 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_PSPOLL) |
245 WLAN_SET_FC_PWRMGT(0)
247 pTxPacket->p80211Header->sA2.wDurationID = pMgmt->wCurrAID | BIT14 | BIT15;
248 memcpy(pTxPacket->p80211Header->sA2.abyAddr1, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN);
249 memcpy(pTxPacket->p80211Header->sA2.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
250 pTxPacket->cbMPDULen = WLAN_HDR_ADDR2_LEN;
251 pTxPacket->cbPayloadLen = 0;
252 // send the frame
253 if (csMgmt_xmit(pDevice, pTxPacket) != CMD_STATUS_PENDING) {
254 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send PS-Poll packet failed..\n");
257 return;
262 * Routine Description:
263 * Send NULL packet to AP for notification power state of STA
265 * Return Value:
266 * None.
269 bool
270 PSbSendNullPacket(
271 void *hDeviceContext
274 PSDevice pDevice = (PSDevice)hDeviceContext;
275 PSTxMgmtPacket pTxPacket = NULL;
276 PSMgmtObject pMgmt = pDevice->pMgmt;
277 unsigned int uIdx;
279 if (!pDevice->bLinkPass)
280 return false;
282 #ifdef TxInSleep
283 if (!pDevice->bEnablePSMode && !pDevice->fTxDataInSleep)
284 return false;
285 #else
286 if (!pDevice->bEnablePSMode)
287 return false;
288 #endif
289 if (pDevice->bEnablePSMode) {
290 for (uIdx = 0; uIdx < TYPE_MAXTD; uIdx++) {
291 if (pDevice->iTDUsed[uIdx] != 0)
292 return false;
296 memset(pMgmt->pbyPSPacketPool, 0, sizeof(STxMgmtPacket) + WLAN_NULLDATA_FR_MAXLEN);
297 pTxPacket = (PSTxMgmtPacket)pMgmt->pbyPSPacketPool;
298 pTxPacket->p80211Header = (PUWLAN_80211HDR)((unsigned char *)pTxPacket + sizeof(STxMgmtPacket));
300 if (pDevice->bEnablePSMode) {
301 pTxPacket->p80211Header->sA3.wFrameCtl = cpu_to_le16(
303 WLAN_SET_FC_FTYPE(WLAN_TYPE_DATA) |
304 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_NULL) |
305 WLAN_SET_FC_PWRMGT(1)
307 } else {
308 pTxPacket->p80211Header->sA3.wFrameCtl = cpu_to_le16(
310 WLAN_SET_FC_FTYPE(WLAN_TYPE_DATA) |
311 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_NULL) |
312 WLAN_SET_FC_PWRMGT(0)
316 if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA)
317 pTxPacket->p80211Header->sA3.wFrameCtl |= cpu_to_le16((unsigned short)WLAN_SET_FC_TODS(1));
319 memcpy(pTxPacket->p80211Header->sA3.abyAddr1, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN);
320 memcpy(pTxPacket->p80211Header->sA3.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
321 memcpy(pTxPacket->p80211Header->sA3.abyAddr3, pMgmt->abyCurrBSSID, WLAN_BSSID_LEN);
322 pTxPacket->cbMPDULen = WLAN_HDR_ADDR3_LEN;
323 pTxPacket->cbPayloadLen = 0;
324 // send the frame
325 if (csMgmt_xmit(pDevice, pTxPacket) != CMD_STATUS_PENDING) {
326 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send Null Packet failed !\n");
327 return false;
330 return true;
335 * Routine Description:
336 * Check if Next TBTT must wake up
338 * Return Value:
339 * None.
343 bool
344 PSbIsNextTBTTWakeUp(
345 void *hDeviceContext
348 PSDevice pDevice = (PSDevice)hDeviceContext;
349 PSMgmtObject pMgmt = pDevice->pMgmt;
350 bool bWakeUp = false;
352 if (pMgmt->wListenInterval >= 2) {
353 if (pMgmt->wCountToWakeUp == 0)
354 pMgmt->wCountToWakeUp = pMgmt->wListenInterval;
356 pMgmt->wCountToWakeUp--;
358 if (pMgmt->wCountToWakeUp == 1) {
359 // Turn on wake up to listen next beacon
360 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_LNBCN);
361 bWakeUp = true;
366 return bWakeUp;