rfkill: fix miscdev ops
[linux-2.6/btrfs-unstable.git] / drivers / staging / vt6656 / power.c
blobb5702b098e182dc17502074ee03abdddef1e403c
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 managment 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"
48 #include "control.h"
49 #include "rndis.h"
51 /*--------------------- Static Definitions -------------------------*/
56 /*--------------------- Static Classes ----------------------------*/
58 /*--------------------- Static Variables --------------------------*/
59 static int msglevel =MSG_LEVEL_INFO;
60 /*--------------------- Static Functions --------------------------*/
63 /*--------------------- Export Variables --------------------------*/
66 /*--------------------- Export Functions --------------------------*/
68 /*+
70 * Routine Description:
71 * Enable hw power saving functions
73 * Return Value:
74 * None.
76 -*/
79 VOID
80 PSvEnablePowerSaving(
81 IN HANDLE hDeviceContext,
82 IN WORD wListenInterval
85 PSDevice pDevice = (PSDevice)hDeviceContext;
86 PSMgmtObject pMgmt = &(pDevice->sMgmtObj);
87 WORD wAID = pMgmt->wCurrAID | BIT14 | BIT15;
89 // set period of power up before TBTT
90 MACvWriteWord(pDevice, MAC_REG_PWBT, C_PWBT);
92 if (pDevice->eOPMode != OP_MODE_ADHOC) {
93 // set AID
94 MACvWriteWord(pDevice, MAC_REG_AIDATIM, wAID);
95 } else {
96 // set ATIM Window
97 //MACvWriteATIMW(pDevice->PortOffset, pMgmt->wCurrATIMWindow);
100 //Warren:06-18-2004,the sequence must follow PSEN->AUTOSLEEP->GO2DOZE
101 // enable power saving hw function
102 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_PSEN);
103 // Set AutoSleep
104 MACvRegBitsOn(pDevice, MAC_REG_PSCFG, PSCFG_AUTOSLEEP);
106 //Warren:MUST turn on this once before turn on AUTOSLEEP ,or the AUTOSLEEP doesn't work
107 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_GO2DOZE);
110 if (wListenInterval >= 2) {
112 // clear always listen beacon
113 MACvRegBitsOff(pDevice, MAC_REG_PSCTL, PSCTL_ALBCN);
114 // first time set listen next beacon
115 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_LNBCN);
117 pMgmt->wCountToWakeUp = wListenInterval;
120 else {
122 // always listen beacon
123 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_ALBCN);
124 pMgmt->wCountToWakeUp = 0;
128 pDevice->bEnablePSMode = TRUE;
130 if (pDevice->eOPMode == OP_MODE_ADHOC) {
131 // bMgrPrepareBeaconToSend((HANDLE)pDevice, pMgmt);
133 // We don't send null pkt in ad hoc mode since beacon will handle this.
134 else if (pDevice->eOPMode == OP_MODE_INFRASTRUCTURE) {
135 PSbSendNullPacket(pDevice);
137 pDevice->bPWBitOn = TRUE;
138 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "PS:Power Saving Mode Enable... \n");
139 return;
149 * Routine Description:
150 * Disable hw power saving functions
152 * Return Value:
153 * None.
157 VOID
158 PSvDisablePowerSaving(
159 IN HANDLE hDeviceContext
162 PSDevice pDevice = (PSDevice)hDeviceContext;
163 // PSMgmtObject pMgmt = &(pDevice->sMgmtObj);
166 // disable power saving hw function
167 CONTROLnsRequestOut(pDevice,
168 MESSAGE_TYPE_DISABLE_PS,
172 NULL
175 //clear AutoSleep
176 MACvRegBitsOff(pDevice, MAC_REG_PSCFG, PSCFG_AUTOSLEEP);
178 // set always listen beacon
179 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_ALBCN);
181 pDevice->bEnablePSMode = FALSE;
183 if (pDevice->eOPMode == OP_MODE_INFRASTRUCTURE) {
184 PSbSendNullPacket(pDevice);
186 pDevice->bPWBitOn = FALSE;
187 return;
193 * Routine Description:
194 * Consider to power down when no more packets to tx or rx.
196 * Return Value:
197 * TRUE, if power down success
198 * FALSE, if fail
202 BOOL
203 PSbConsiderPowerDown(
204 IN HANDLE hDeviceContext,
205 IN BOOL bCheckRxDMA,
206 IN BOOL bCheckCountToWakeUp
209 PSDevice pDevice = (PSDevice)hDeviceContext;
210 PSMgmtObject pMgmt = &(pDevice->sMgmtObj);
211 BYTE byData;
214 // check if already in Doze mode
215 ControlvReadByte(pDevice, MESSAGE_REQUEST_MACREG, MAC_REG_PSCTL, &byData);
216 if ( (byData & PSCTL_PS) != 0 )
217 return TRUE;;
219 if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
220 // check if in TIM wake period
221 if (pMgmt->bInTIMWake)
222 return FALSE;
225 // check scan state
226 if (pDevice->bCmdRunning)
227 return FALSE;
229 //Tx Burst
230 if ( pDevice->bPSModeTxBurst )
231 return FALSE;
233 // Froce PSEN on
234 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_PSEN);
236 if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
237 if (bCheckCountToWakeUp &&
238 (pMgmt->wCountToWakeUp == 0 || pMgmt->wCountToWakeUp == 1)) {
239 return FALSE;
243 pDevice->bPSRxBeacon = TRUE;
244 // no Tx, no Rx isr, now go to Doze
245 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_GO2DOZE);
247 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Go to Doze ZZZZZZZZZZZZZZZ\n");
248 return TRUE;
255 * Routine Description:
256 * Send PS-POLL packet
258 * Return Value:
259 * None.
265 VOID
266 PSvSendPSPOLL(
267 IN HANDLE hDeviceContext
270 PSDevice pDevice = (PSDevice)hDeviceContext;
271 PSMgmtObject pMgmt = &(pDevice->sMgmtObj);
272 PSTxMgmtPacket pTxPacket = NULL;
275 memset(pMgmt->pbyPSPacketPool, 0, sizeof(STxMgmtPacket) + WLAN_HDR_ADDR2_LEN);
276 pTxPacket = (PSTxMgmtPacket)pMgmt->pbyPSPacketPool;
277 pTxPacket->p80211Header = (PUWLAN_80211HDR)((PBYTE)pTxPacket + sizeof(STxMgmtPacket));
278 pTxPacket->p80211Header->sA2.wFrameCtl = cpu_to_le16(
280 WLAN_SET_FC_FTYPE(WLAN_TYPE_CTL) |
281 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_PSPOLL) |
282 WLAN_SET_FC_PWRMGT(0)
284 pTxPacket->p80211Header->sA2.wDurationID = pMgmt->wCurrAID | BIT14 | BIT15;
285 memcpy(pTxPacket->p80211Header->sA2.abyAddr1, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN);
286 memcpy(pTxPacket->p80211Header->sA2.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
287 pTxPacket->cbMPDULen = WLAN_HDR_ADDR2_LEN;
288 pTxPacket->cbPayloadLen = 0;
289 // send the frame
290 if (csMgmt_xmit(pDevice, pTxPacket) != CMD_STATUS_PENDING) {
291 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send PS-Poll packet failed..\n");
293 else {
294 // DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send PS-Poll packet success..\n");
297 return;
304 * Routine Description:
305 * Send NULL packet to AP for notification power state of STA
307 * Return Value:
308 * None.
311 BOOL
312 PSbSendNullPacket(
313 IN HANDLE hDeviceContext
316 PSDevice pDevice = (PSDevice)hDeviceContext;
317 PSTxMgmtPacket pTxPacket = NULL;
318 PSMgmtObject pMgmt = &(pDevice->sMgmtObj);
322 if (pDevice->bLinkPass == FALSE) {
323 return FALSE;
326 //2007-0115-03<Add>by MikeLiu
327 #ifdef TxInSleep
328 if ((pDevice->bEnablePSMode == FALSE) &&
329 (pDevice->fTxDataInSleep == FALSE)){
330 return FALSE;
332 #else
333 if (pDevice->bEnablePSMode == FALSE) {
334 return FALSE;
336 #endif
337 memset(pMgmt->pbyPSPacketPool, 0, sizeof(STxMgmtPacket) + WLAN_NULLDATA_FR_MAXLEN);
338 pTxPacket = (PSTxMgmtPacket)pMgmt->pbyPSPacketPool;
339 pTxPacket->p80211Header = (PUWLAN_80211HDR)((PBYTE)pTxPacket + sizeof(STxMgmtPacket));
341 if (pDevice->bEnablePSMode) {
343 pTxPacket->p80211Header->sA3.wFrameCtl = cpu_to_le16(
345 WLAN_SET_FC_FTYPE(WLAN_TYPE_DATA) |
346 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_NULL) |
347 WLAN_SET_FC_PWRMGT(1)
350 else {
351 pTxPacket->p80211Header->sA3.wFrameCtl = cpu_to_le16(
353 WLAN_SET_FC_FTYPE(WLAN_TYPE_DATA) |
354 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_NULL) |
355 WLAN_SET_FC_PWRMGT(0)
359 if(pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
360 pTxPacket->p80211Header->sA3.wFrameCtl |= cpu_to_le16((WORD)WLAN_SET_FC_TODS(1));
363 memcpy(pTxPacket->p80211Header->sA3.abyAddr1, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN);
364 memcpy(pTxPacket->p80211Header->sA3.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
365 memcpy(pTxPacket->p80211Header->sA3.abyAddr3, pMgmt->abyCurrBSSID, WLAN_BSSID_LEN);
366 pTxPacket->cbMPDULen = WLAN_HDR_ADDR3_LEN;
367 pTxPacket->cbPayloadLen = 0;
368 // send the frame
369 if (csMgmt_xmit(pDevice, pTxPacket) != CMD_STATUS_PENDING) {
370 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send Null Packet failed !\n");
371 return FALSE;
373 else {
374 // DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send Null Packet success....\n");
378 return TRUE ;
383 * Routine Description:
384 * Check if Next TBTT must wake up
386 * Return Value:
387 * None.
391 BOOL
392 PSbIsNextTBTTWakeUp(
393 IN HANDLE hDeviceContext
397 PSDevice pDevice = (PSDevice)hDeviceContext;
398 PSMgmtObject pMgmt = &(pDevice->sMgmtObj);
399 BOOL bWakeUp = FALSE;
401 if (pMgmt->wListenInterval >= 2) {
402 if (pMgmt->wCountToWakeUp == 0) {
403 pMgmt->wCountToWakeUp = pMgmt->wListenInterval;
406 pMgmt->wCountToWakeUp --;
408 if (pMgmt->wCountToWakeUp == 1) {
410 // Turn on wake up to listen next beacon
411 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_LNBCN);
412 pDevice->bPSRxBeacon = FALSE;
413 bWakeUp = TRUE;
415 } else if ( !pDevice->bPSRxBeacon ) {
416 //Listen until RxBeacon
417 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_LNBCN);
422 return bWakeUp;