db5c1159a6b669da45402bb9982fb02efcefa3db
[deliverable/linux.git] / drivers / staging / vt6655 / power.c
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 *
22 * Purpose: Handles 802.11 power management functions
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
40
41
42 #if !defined(__TTYPE_H__)
43 #include "ttype.h"
44 #endif
45 #if !defined(__TBIT_H__)
46 #include "tbit.h"
47 #endif
48 #if !defined(__MAC_H__)
49 #include "mac.h"
50 #endif
51 #if !defined(__DEVICE_H__)
52 #include "device.h"
53 #endif
54 #if !defined(__WMGR_H__)
55 #include "wmgr.h"
56 #endif
57 #if !defined(__POWER_H__)
58 #include "power.h"
59 #endif
60 #if !defined(__WCMD_H__)
61 #include "wcmd.h"
62 #endif
63 #if !defined(__TBIT_H__)
64 #include "tbit.h"
65 #endif
66 #if !defined(__UMEM_H__)
67 #include "umem.h"
68 #endif
69 #if !defined(__RXTX_H__)
70 #include "rxtx.h"
71 #endif
72 #if !defined(__CARD_H__)
73 #include "card.h"
74 #endif
75
76
77
78
79 /*--------------------- Static Definitions -------------------------*/
80
81
82
83
84 /*--------------------- Static Classes ----------------------------*/
85
86 /*--------------------- Static Variables --------------------------*/
87 static int msglevel =MSG_LEVEL_INFO;
88 /*--------------------- Static Functions --------------------------*/
89
90
91 /*--------------------- Export Variables --------------------------*/
92
93
94 /*--------------------- Export Functions --------------------------*/
95
96 /*+
97 *
98 * Routine Description:
99 * Enable hw power saving functions
100 *
101 * Return Value:
102 * None.
103 *
104 -*/
105
106
107 VOID
108 PSvEnablePowerSaving(
109 IN HANDLE hDeviceContext,
110 IN WORD wListenInterval
111 )
112 {
113 PSDevice pDevice = (PSDevice)hDeviceContext;
114 PSMgmtObject pMgmt = pDevice->pMgmt;
115 WORD wAID = pMgmt->wCurrAID | BIT14 | BIT15;
116
117 // set period of power up before TBTT
118 VNSvOutPortW(pDevice->PortOffset + MAC_REG_PWBT, C_PWBT);
119 if (pDevice->eOPMode != OP_MODE_ADHOC) {
120 // set AID
121 VNSvOutPortW(pDevice->PortOffset + MAC_REG_AIDATIM, wAID);
122 } else {
123 // set ATIM Window
124 MACvWriteATIMW(pDevice->PortOffset, pMgmt->wCurrATIMWindow);
125 }
126 // Set AutoSleep
127 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCFG, PSCFG_AUTOSLEEP);
128 // Set HWUTSF
129 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_TFTCTL, TFTCTL_HWUTSF);
130
131 if (wListenInterval >= 2) {
132 // clear always listen beacon
133 MACvRegBitsOff(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_ALBCN);
134 //pDevice->wCFG &= ~CFG_ALB;
135 // first time set listen next beacon
136 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_LNBCN);
137 pMgmt->wCountToWakeUp = wListenInterval;
138 }
139 else {
140 // always listen beacon
141 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_ALBCN);
142 //pDevice->wCFG |= CFG_ALB;
143 pMgmt->wCountToWakeUp = 0;
144 }
145
146 // enable power saving hw function
147 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PSEN);
148 pDevice->bEnablePSMode = TRUE;
149
150 if (pDevice->eOPMode == OP_MODE_ADHOC) {
151 // bMgrPrepareBeaconToSend((HANDLE)pDevice, pMgmt);
152 }
153 // We don't send null pkt in ad hoc mode since beacon will handle this.
154 else if (pDevice->eOPMode == OP_MODE_INFRASTRUCTURE) {
155 PSbSendNullPacket(pDevice);
156 }
157 pDevice->bPWBitOn = TRUE;
158 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "PS:Power Saving Mode Enable... \n");
159 return;
160 }
161
162
163
164
165
166
167 /*+
168 *
169 * Routine Description:
170 * Disable hw power saving functions
171 *
172 * Return Value:
173 * None.
174 *
175 -*/
176
177 VOID
178 PSvDisablePowerSaving(
179 IN HANDLE hDeviceContext
180 )
181 {
182 PSDevice pDevice = (PSDevice)hDeviceContext;
183 // PSMgmtObject pMgmt = pDevice->pMgmt;
184
185 // disable power saving hw function
186 MACbPSWakeup(pDevice->PortOffset);
187 //clear AutoSleep
188 MACvRegBitsOff(pDevice->PortOffset, MAC_REG_PSCFG, PSCFG_AUTOSLEEP);
189 //clear HWUTSF
190 MACvRegBitsOff(pDevice->PortOffset, MAC_REG_TFTCTL, TFTCTL_HWUTSF);
191 // set always listen beacon
192 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_ALBCN);
193
194 pDevice->bEnablePSMode = FALSE;
195
196 if (pDevice->eOPMode == OP_MODE_INFRASTRUCTURE) {
197 PSbSendNullPacket(pDevice);
198 }
199 pDevice->bPWBitOn = FALSE;
200 return;
201 }
202
203
204 /*+
205 *
206 * Routine Description:
207 * Consider to power down when no more packets to tx or rx.
208 *
209 * Return Value:
210 * TRUE, if power down success
211 * FALSE, if fail
212 -*/
213
214
215 BOOL
216 PSbConsiderPowerDown(
217 IN HANDLE hDeviceContext,
218 IN BOOL bCheckRxDMA,
219 IN BOOL bCheckCountToWakeUp
220 )
221 {
222 PSDevice pDevice = (PSDevice)hDeviceContext;
223 PSMgmtObject pMgmt = pDevice->pMgmt;
224 UINT uIdx;
225
226 // check if already in Doze mode
227 if (MACbIsRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PS))
228 return TRUE;
229
230 if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
231 // check if in TIM wake period
232 if (pMgmt->bInTIMWake)
233 return FALSE;
234 }
235
236 // check scan state
237 if (pDevice->bCmdRunning)
238 return FALSE;
239
240 // Froce PSEN on
241 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PSEN);
242
243 // check if all TD are empty,
244 for (uIdx = 0; uIdx < TYPE_MAXTD; uIdx ++) {
245 if (pDevice->iTDUsed[uIdx] != 0)
246 return FALSE;
247 }
248
249 // check if rx isr is clear
250 if (bCheckRxDMA &&
251 ((pDevice->dwIsr& ISR_RXDMA0) != 0) &&
252 ((pDevice->dwIsr & ISR_RXDMA1) != 0)){
253 return FALSE;
254 };
255
256 if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
257 if (bCheckCountToWakeUp &&
258 (pMgmt->wCountToWakeUp == 0 || pMgmt->wCountToWakeUp == 1)) {
259 return FALSE;
260 }
261 }
262
263 // no Tx, no Rx isr, now go to Doze
264 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_GO2DOZE);
265 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Go to Doze ZZZZZZZZZZZZZZZ\n");
266 return TRUE;
267 }
268
269
270
271 /*+
272 *
273 * Routine Description:
274 * Send PS-POLL packet
275 *
276 * Return Value:
277 * None.
278 *
279 -*/
280
281
282
283 VOID
284 PSvSendPSPOLL(
285 IN HANDLE hDeviceContext
286 )
287 {
288 PSDevice pDevice = (PSDevice)hDeviceContext;
289 PSMgmtObject pMgmt = pDevice->pMgmt;
290 PSTxMgmtPacket pTxPacket = NULL;
291
292
293 memset(pMgmt->pbyPSPacketPool, 0, sizeof(STxMgmtPacket) + WLAN_HDR_ADDR2_LEN);
294 pTxPacket = (PSTxMgmtPacket)pMgmt->pbyPSPacketPool;
295 pTxPacket->p80211Header = (PUWLAN_80211HDR)((PBYTE)pTxPacket + sizeof(STxMgmtPacket));
296 pTxPacket->p80211Header->sA2.wFrameCtl = cpu_to_le16(
297 (
298 WLAN_SET_FC_FTYPE(WLAN_TYPE_CTL) |
299 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_PSPOLL) |
300 WLAN_SET_FC_PWRMGT(0)
301 ));
302 pTxPacket->p80211Header->sA2.wDurationID = pMgmt->wCurrAID | BIT14 | BIT15;
303 memcpy(pTxPacket->p80211Header->sA2.abyAddr1, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN);
304 memcpy(pTxPacket->p80211Header->sA2.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
305 pTxPacket->cbMPDULen = WLAN_HDR_ADDR2_LEN;
306 pTxPacket->cbPayloadLen = 0;
307 // send the frame
308 if (csMgmt_xmit(pDevice, pTxPacket) != CMD_STATUS_PENDING) {
309 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send PS-Poll packet failed..\n");
310 }
311 else {
312 // DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send PS-Poll packet success..\n");
313 };
314
315 return;
316 }
317
318
319
320 /*+
321 *
322 * Routine Description:
323 * Send NULL packet to AP for notification power state of STA
324 *
325 * Return Value:
326 * None.
327 *
328 -*/
329 BOOL
330 PSbSendNullPacket(
331 IN HANDLE hDeviceContext
332 )
333 {
334 PSDevice pDevice = (PSDevice)hDeviceContext;
335 PSTxMgmtPacket pTxPacket = NULL;
336 PSMgmtObject pMgmt = pDevice->pMgmt;
337 UINT uIdx;
338
339
340 if (pDevice->bLinkPass == FALSE) {
341 return FALSE;
342 }
343 #ifdef TxInSleep
344 if ((pDevice->bEnablePSMode == FALSE) &&
345 (pDevice->fTxDataInSleep == FALSE)){
346 return FALSE;
347 }
348 #else
349 if (pDevice->bEnablePSMode == FALSE) {
350 return FALSE;
351 }
352 #endif
353 if (pDevice->bEnablePSMode) {
354 for (uIdx = 0; uIdx < TYPE_MAXTD; uIdx ++) {
355 if (pDevice->iTDUsed[uIdx] != 0)
356 return FALSE;
357 }
358 }
359
360 memset(pMgmt->pbyPSPacketPool, 0, sizeof(STxMgmtPacket) + WLAN_NULLDATA_FR_MAXLEN);
361 pTxPacket = (PSTxMgmtPacket)pMgmt->pbyPSPacketPool;
362 pTxPacket->p80211Header = (PUWLAN_80211HDR)((PBYTE)pTxPacket + sizeof(STxMgmtPacket));
363
364 if (pDevice->bEnablePSMode) {
365
366 pTxPacket->p80211Header->sA3.wFrameCtl = cpu_to_le16(
367 (
368 WLAN_SET_FC_FTYPE(WLAN_TYPE_DATA) |
369 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_NULL) |
370 WLAN_SET_FC_PWRMGT(1)
371 ));
372 }
373 else {
374 pTxPacket->p80211Header->sA3.wFrameCtl = cpu_to_le16(
375 (
376 WLAN_SET_FC_FTYPE(WLAN_TYPE_DATA) |
377 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_NULL) |
378 WLAN_SET_FC_PWRMGT(0)
379 ));
380 }
381
382 if(pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
383 pTxPacket->p80211Header->sA3.wFrameCtl |= cpu_to_le16((WORD)WLAN_SET_FC_TODS(1));
384 }
385
386 memcpy(pTxPacket->p80211Header->sA3.abyAddr1, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN);
387 memcpy(pTxPacket->p80211Header->sA3.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
388 memcpy(pTxPacket->p80211Header->sA3.abyAddr3, pMgmt->abyCurrBSSID, WLAN_BSSID_LEN);
389 pTxPacket->cbMPDULen = WLAN_HDR_ADDR3_LEN;
390 pTxPacket->cbPayloadLen = 0;
391 // send the frame
392 if (csMgmt_xmit(pDevice, pTxPacket) != CMD_STATUS_PENDING) {
393 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send Null Packet failed !\n");
394 return FALSE;
395 }
396 else {
397
398 // DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send Null Packet success....\n");
399 }
400
401
402 return TRUE ;
403 }
404
405 /*+
406 *
407 * Routine Description:
408 * Check if Next TBTT must wake up
409 *
410 * Return Value:
411 * None.
412 *
413 -*/
414
415 BOOL
416 PSbIsNextTBTTWakeUp(
417 IN HANDLE hDeviceContext
418 )
419 {
420
421 PSDevice pDevice = (PSDevice)hDeviceContext;
422 PSMgmtObject pMgmt = pDevice->pMgmt;
423 BOOL bWakeUp = FALSE;
424
425 if (pMgmt->wListenInterval >= 2) {
426 if (pMgmt->wCountToWakeUp == 0) {
427 pMgmt->wCountToWakeUp = pMgmt->wListenInterval;
428 }
429
430 pMgmt->wCountToWakeUp --;
431
432 if (pMgmt->wCountToWakeUp == 1) {
433 // Turn on wake up to listen next beacon
434 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_LNBCN);
435 bWakeUp = TRUE;
436 }
437
438 }
439
440 return bWakeUp;
441 }
442
This page took 0.03841 seconds and 4 git commands to generate.