jbd: Fix a race between checkpointing code and journal_get_write_access()
[linux/fpc-iii.git] / drivers / staging / vt6655 / power.c
blobedd8336712a2d51d4c7bbd3fec4bb04e829065f7
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.
19 * File: power.c
21 * Purpose: Handles 802.11 power managment functions
23 * Author: Lyndon Chen
25 * Date: July 17, 2002
27 * Functions:
28 * PSvEnablePowerSaving - Enable Power Saving Mode
29 * PSvDiasblePowerSaving - Disable Power Saving Mode
30 * PSbConsiderPowerDown - Decide if we can Power Down
31 * PSvSendPSPOLL - Send PS-POLL packet
32 * PSbSendNullPacket - Send Null packet
33 * PSbIsNextTBTTWakeUp - Decide if we need to wake up at next Beacon
35 * Revision History:
41 #if !defined(__TTYPE_H__)
42 #include "ttype.h"
43 #endif
44 #if !defined(__TBIT_H__)
45 #include "tbit.h"
46 #endif
47 #if !defined(__MAC_H__)
48 #include "mac.h"
49 #endif
50 #if !defined(__DEVICE_H__)
51 #include "device.h"
52 #endif
53 #if !defined(__WMGR_H__)
54 #include "wmgr.h"
55 #endif
56 #if !defined(__POWER_H__)
57 #include "power.h"
58 #endif
59 #if !defined(__WCMD_H__)
60 #include "wcmd.h"
61 #endif
62 #if !defined(__TBIT_H__)
63 #include "tbit.h"
64 #endif
65 #if !defined(__UMEM_H__)
66 #include "umem.h"
67 #endif
68 #if !defined(__RXTX_H__)
69 #include "rxtx.h"
70 #endif
71 #if !defined(__CARD_H__)
72 #include "card.h"
73 #endif
78 /*--------------------- Static Definitions -------------------------*/
83 /*--------------------- Static Classes ----------------------------*/
85 /*--------------------- Static Variables --------------------------*/
86 static int msglevel =MSG_LEVEL_INFO;
87 /*--------------------- Static Functions --------------------------*/
90 /*--------------------- Export Variables --------------------------*/
93 /*--------------------- Export Functions --------------------------*/
95 /*+
97 * Routine Description:
98 * Enable hw power saving functions
100 * Return Value:
101 * None.
106 VOID
107 PSvEnablePowerSaving(
108 IN HANDLE hDeviceContext,
109 IN WORD wListenInterval
112 PSDevice pDevice = (PSDevice)hDeviceContext;
113 PSMgmtObject pMgmt = pDevice->pMgmt;
114 WORD wAID = pMgmt->wCurrAID | BIT14 | BIT15;
116 // set period of power up before TBTT
117 VNSvOutPortW(pDevice->PortOffset + MAC_REG_PWBT, C_PWBT);
118 if (pDevice->eOPMode != OP_MODE_ADHOC) {
119 // set AID
120 VNSvOutPortW(pDevice->PortOffset + MAC_REG_AIDATIM, wAID);
121 } else {
122 // set ATIM Window
123 MACvWriteATIMW(pDevice->PortOffset, pMgmt->wCurrATIMWindow);
125 // Set AutoSleep
126 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCFG, PSCFG_AUTOSLEEP);
127 // Set HWUTSF
128 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_TFTCTL, TFTCTL_HWUTSF);
130 if (wListenInterval >= 2) {
131 // clear always listen beacon
132 MACvRegBitsOff(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_ALBCN);
133 //pDevice->wCFG &= ~CFG_ALB;
134 // first time set listen next beacon
135 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_LNBCN);
136 pMgmt->wCountToWakeUp = wListenInterval;
138 else {
139 // always listen beacon
140 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_ALBCN);
141 //pDevice->wCFG |= CFG_ALB;
142 pMgmt->wCountToWakeUp = 0;
145 // enable power saving hw function
146 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PSEN);
147 pDevice->bEnablePSMode = TRUE;
149 if (pDevice->eOPMode == OP_MODE_ADHOC) {
150 // bMgrPrepareBeaconToSend((HANDLE)pDevice, pMgmt);
152 // We don't send null pkt in ad hoc mode since beacon will handle this.
153 else if (pDevice->eOPMode == OP_MODE_INFRASTRUCTURE) {
154 PSbSendNullPacket(pDevice);
156 pDevice->bPWBitOn = TRUE;
157 DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "PS:Power Saving Mode Enable... \n");
158 return;
168 * Routine Description:
169 * Disable hw power saving functions
171 * Return Value:
172 * None.
176 VOID
177 PSvDisablePowerSaving(
178 IN HANDLE hDeviceContext
181 PSDevice pDevice = (PSDevice)hDeviceContext;
182 // PSMgmtObject pMgmt = pDevice->pMgmt;
184 // disable power saving hw function
185 MACbPSWakeup(pDevice->PortOffset);
186 //clear AutoSleep
187 MACvRegBitsOff(pDevice->PortOffset, MAC_REG_PSCFG, PSCFG_AUTOSLEEP);
188 //clear HWUTSF
189 MACvRegBitsOff(pDevice->PortOffset, MAC_REG_TFTCTL, TFTCTL_HWUTSF);
190 // set always listen beacon
191 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_ALBCN);
193 pDevice->bEnablePSMode = FALSE;
195 if (pDevice->eOPMode == OP_MODE_INFRASTRUCTURE) {
196 PSbSendNullPacket(pDevice);
198 pDevice->bPWBitOn = FALSE;
199 return;
205 * Routine Description:
206 * Consider to power down when no more packets to tx or rx.
208 * Return Value:
209 * TRUE, if power down success
210 * FALSE, if fail
214 BOOL
215 PSbConsiderPowerDown(
216 IN HANDLE hDeviceContext,
217 IN BOOL bCheckRxDMA,
218 IN BOOL bCheckCountToWakeUp
221 PSDevice pDevice = (PSDevice)hDeviceContext;
222 PSMgmtObject pMgmt = pDevice->pMgmt;
223 UINT uIdx;
225 // check if already in Doze mode
226 if (MACbIsRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PS))
227 return TRUE;
229 if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
230 // check if in TIM wake period
231 if (pMgmt->bInTIMWake)
232 return FALSE;
235 // check scan state
236 if (pDevice->bCmdRunning)
237 return FALSE;
239 // Froce PSEN on
240 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PSEN);
242 // check if all TD are empty,
243 for (uIdx = 0; uIdx < TYPE_MAXTD; uIdx ++) {
244 if (pDevice->iTDUsed[uIdx] != 0)
245 return FALSE;
248 // check if rx isr is clear
249 if (bCheckRxDMA &&
250 ((pDevice->dwIsr& ISR_RXDMA0) != 0) &&
251 ((pDevice->dwIsr & ISR_RXDMA1) != 0)){
252 return FALSE;
255 if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
256 if (bCheckCountToWakeUp &&
257 (pMgmt->wCountToWakeUp == 0 || pMgmt->wCountToWakeUp == 1)) {
258 return FALSE;
262 // no Tx, no Rx isr, now go to Doze
263 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_GO2DOZE);
264 DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Go to Doze ZZZZZZZZZZZZZZZ\n");
265 return TRUE;
272 * Routine Description:
273 * Send PS-POLL packet
275 * Return Value:
276 * None.
282 VOID
283 PSvSendPSPOLL(
284 IN HANDLE hDeviceContext
287 PSDevice pDevice = (PSDevice)hDeviceContext;
288 PSMgmtObject pMgmt = pDevice->pMgmt;
289 PSTxMgmtPacket pTxPacket = NULL;
292 memset(pMgmt->pbyPSPacketPool, 0, sizeof(STxMgmtPacket) + WLAN_HDR_ADDR2_LEN);
293 pTxPacket = (PSTxMgmtPacket)pMgmt->pbyPSPacketPool;
294 pTxPacket->p80211Header = (PUWLAN_80211HDR)((PBYTE)pTxPacket + sizeof(STxMgmtPacket));
295 pTxPacket->p80211Header->sA2.wFrameCtl = cpu_to_le16(
297 WLAN_SET_FC_FTYPE(WLAN_TYPE_CTL) |
298 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_PSPOLL) |
299 WLAN_SET_FC_PWRMGT(0)
301 pTxPacket->p80211Header->sA2.wDurationID = pMgmt->wCurrAID | BIT14 | BIT15;
302 memcpy(pTxPacket->p80211Header->sA2.abyAddr1, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN);
303 memcpy(pTxPacket->p80211Header->sA2.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
304 pTxPacket->cbMPDULen = WLAN_HDR_ADDR2_LEN;
305 pTxPacket->cbPayloadLen = 0;
306 // send the frame
307 if (csMgmt_xmit(pDevice, pTxPacket) != CMD_STATUS_PENDING) {
308 DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send PS-Poll packet failed..\n");
310 else {
311 // DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send PS-Poll packet success..\n");
314 return;
321 * Routine Description:
322 * Send NULL packet to AP for notification power state of STA
324 * Return Value:
325 * None.
328 BOOL
329 PSbSendNullPacket(
330 IN HANDLE hDeviceContext
333 PSDevice pDevice = (PSDevice)hDeviceContext;
334 PSTxMgmtPacket pTxPacket = NULL;
335 PSMgmtObject pMgmt = pDevice->pMgmt;
336 UINT uIdx;
339 if (pDevice->bLinkPass == FALSE) {
340 return FALSE;
342 #ifdef TxInSleep
343 if ((pDevice->bEnablePSMode == FALSE) &&
344 (pDevice->fTxDataInSleep == FALSE)){
345 return FALSE;
347 #else
348 if (pDevice->bEnablePSMode == FALSE) {
349 return FALSE;
351 #endif
352 if (pDevice->bEnablePSMode) {
353 for (uIdx = 0; uIdx < TYPE_MAXTD; uIdx ++) {
354 if (pDevice->iTDUsed[uIdx] != 0)
355 return FALSE;
359 memset(pMgmt->pbyPSPacketPool, 0, sizeof(STxMgmtPacket) + WLAN_NULLDATA_FR_MAXLEN);
360 pTxPacket = (PSTxMgmtPacket)pMgmt->pbyPSPacketPool;
361 pTxPacket->p80211Header = (PUWLAN_80211HDR)((PBYTE)pTxPacket + sizeof(STxMgmtPacket));
363 if (pDevice->bEnablePSMode) {
365 pTxPacket->p80211Header->sA3.wFrameCtl = cpu_to_le16(
367 WLAN_SET_FC_FTYPE(WLAN_TYPE_DATA) |
368 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_NULL) |
369 WLAN_SET_FC_PWRMGT(1)
372 else {
373 pTxPacket->p80211Header->sA3.wFrameCtl = cpu_to_le16(
375 WLAN_SET_FC_FTYPE(WLAN_TYPE_DATA) |
376 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_NULL) |
377 WLAN_SET_FC_PWRMGT(0)
381 if(pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
382 pTxPacket->p80211Header->sA3.wFrameCtl |= cpu_to_le16((WORD)WLAN_SET_FC_TODS(1));
385 memcpy(pTxPacket->p80211Header->sA3.abyAddr1, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN);
386 memcpy(pTxPacket->p80211Header->sA3.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
387 memcpy(pTxPacket->p80211Header->sA3.abyAddr3, pMgmt->abyCurrBSSID, WLAN_BSSID_LEN);
388 pTxPacket->cbMPDULen = WLAN_HDR_ADDR3_LEN;
389 pTxPacket->cbPayloadLen = 0;
390 // send the frame
391 if (csMgmt_xmit(pDevice, pTxPacket) != CMD_STATUS_PENDING) {
392 DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send Null Packet failed !\n");
393 return FALSE;
395 else {
397 // DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send Null Packet success....\n");
401 return TRUE ;
406 * Routine Description:
407 * Check if Next TBTT must wake up
409 * Return Value:
410 * None.
414 BOOL
415 PSbIsNextTBTTWakeUp(
416 IN HANDLE hDeviceContext
420 PSDevice pDevice = (PSDevice)hDeviceContext;
421 PSMgmtObject pMgmt = pDevice->pMgmt;
422 BOOL bWakeUp = FALSE;
424 if (pMgmt->wListenInterval >= 2) {
425 if (pMgmt->wCountToWakeUp == 0) {
426 pMgmt->wCountToWakeUp = pMgmt->wListenInterval;
429 pMgmt->wCountToWakeUp --;
431 if (pMgmt->wCountToWakeUp == 1) {
432 // Turn on wake up to listen next beacon
433 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_LNBCN);
434 bWakeUp = TRUE;
439 return bWakeUp;