staging: vt6655: remove dead code
[firefly-linux-kernel-4.4.55.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 #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
49 /*---------------------  Static Definitions -------------------------*/
50
51 /*---------------------  Static Classes  ----------------------------*/
52
53 /*---------------------  Static Variables  --------------------------*/
54 static int msglevel = MSG_LEVEL_INFO;
55 /*---------------------  Static Functions  --------------------------*/
56
57 /*---------------------  Export Variables  --------------------------*/
58
59 /*---------------------  Export Functions  --------------------------*/
60
61 /*+
62  *
63  * Routine Description:
64  * Enable hw power saving functions
65  *
66  * Return Value:
67  *    None.
68  *
69  -*/
70
71 void
72 PSvEnablePowerSaving(
73         void *hDeviceContext,
74         unsigned short wListenInterval
75 )
76 {
77         PSDevice        pDevice = (PSDevice)hDeviceContext;
78         PSMgmtObject    pMgmt = pDevice->pMgmt;
79         unsigned short wAID = pMgmt->wCurrAID | BIT14 | BIT15;
80
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);
89         }
90         // Set AutoSleep
91         MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCFG, PSCFG_AUTOSLEEP);
92         // Set HWUTSF
93         MACvRegBitsOn(pDevice->PortOffset, MAC_REG_TFTCTL, TFTCTL_HWUTSF);
94
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;
105         }
106
107         // enable power saving hw function
108         MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PSEN);
109         pDevice->bEnablePSMode = true;
110
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);
114
115         pDevice->bPWBitOn = true;
116         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "PS:Power Saving Mode Enable... \n");
117         return;
118 }
119
120 /*+
121  *
122  * Routine Description:
123  * Disable hw power saving functions
124  *
125  * Return Value:
126  *    None.
127  *
128  -*/
129
130 void
131 PSvDisablePowerSaving(
132         void *hDeviceContext
133 )
134 {
135         PSDevice        pDevice = (PSDevice)hDeviceContext;
136
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);
145
146         pDevice->bEnablePSMode = false;
147
148         if (pDevice->eOPMode == OP_MODE_INFRASTRUCTURE) {
149                 PSbSendNullPacket(pDevice);
150         }
151         pDevice->bPWBitOn = false;
152         return;
153 }
154
155 /*+
156  *
157  * Routine Description:
158  * Consider to power down when no more packets to tx or rx.
159  *
160  * Return Value:
161  *    true, if power down success
162  *    false, if fail
163  -*/
164
165 bool
166 PSbConsiderPowerDown(
167         void *hDeviceContext,
168         bool bCheckRxDMA,
169         bool bCheckCountToWakeUp
170 )
171 {
172         PSDevice        pDevice = (PSDevice)hDeviceContext;
173         PSMgmtObject    pMgmt = pDevice->pMgmt;
174         unsigned int uIdx;
175
176         // check if already in Doze mode
177         if (MACbIsRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PS))
178                 return true;
179
180         if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
181                 // check if in TIM wake period
182                 if (pMgmt->bInTIMWake)
183                         return false;
184         }
185
186         // check scan state
187         if (pDevice->bCmdRunning)
188                 return false;
189
190         // Force PSEN on
191         MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PSEN);
192
193         // check if all TD are empty,
194         for (uIdx = 0; uIdx < TYPE_MAXTD; uIdx++) {
195                 if (pDevice->iTDUsed[uIdx] != 0)
196                         return false;
197         }
198
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;
204         }
205
206         if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
207                 if (bCheckCountToWakeUp &&
208                     (pMgmt->wCountToWakeUp == 0 || pMgmt->wCountToWakeUp == 1)) {
209                         return false;
210                 }
211         }
212
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;
217 }
218
219 /*+
220  *
221  * Routine Description:
222  * Send PS-POLL packet
223  *
224  * Return Value:
225  *    None.
226  *
227  -*/
228
229 void
230 PSvSendPSPOLL(
231         void *hDeviceContext
232 )
233 {
234         PSDevice            pDevice = (PSDevice)hDeviceContext;
235         PSMgmtObject        pMgmt = pDevice->pMgmt;
236         PSTxMgmtPacket      pTxPacket = NULL;
237
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(
242                 (
243                         WLAN_SET_FC_FTYPE(WLAN_TYPE_CTL) |
244                         WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_PSPOLL) |
245                         WLAN_SET_FC_PWRMGT(0)
246 ));
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");
255         }
256
257         return;
258 }
259
260 /*+
261  *
262  * Routine Description:
263  * Send NULL packet to AP for notification power state of STA
264  *
265  * Return Value:
266  *    None.
267  *
268  -*/
269 bool
270 PSbSendNullPacket(
271         void *hDeviceContext
272 )
273 {
274         PSDevice            pDevice = (PSDevice)hDeviceContext;
275         PSTxMgmtPacket      pTxPacket = NULL;
276         PSMgmtObject        pMgmt = pDevice->pMgmt;
277         unsigned int uIdx;
278
279         if (!pDevice->bLinkPass) {
280                 return false;
281         }
282 #ifdef TxInSleep
283         if (!pDevice->bEnablePSMode && !pDevice->fTxDataInSleep) {
284                 return false;
285         }
286 #else
287         if (!pDevice->bEnablePSMode) {
288                 return false;
289         }
290 #endif
291         if (pDevice->bEnablePSMode) {
292                 for (uIdx = 0; uIdx < TYPE_MAXTD; uIdx++) {
293                         if (pDevice->iTDUsed[uIdx] != 0)
294                                 return false;
295                 }
296         }
297
298         memset(pMgmt->pbyPSPacketPool, 0, sizeof(STxMgmtPacket) + WLAN_NULLDATA_FR_MAXLEN);
299         pTxPacket = (PSTxMgmtPacket)pMgmt->pbyPSPacketPool;
300         pTxPacket->p80211Header = (PUWLAN_80211HDR)((unsigned char *)pTxPacket + sizeof(STxMgmtPacket));
301
302         if (pDevice->bEnablePSMode) {
303                 pTxPacket->p80211Header->sA3.wFrameCtl = cpu_to_le16(
304                         (
305                                 WLAN_SET_FC_FTYPE(WLAN_TYPE_DATA) |
306                                 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_NULL) |
307                                 WLAN_SET_FC_PWRMGT(1)
308 ));
309         } else {
310                 pTxPacket->p80211Header->sA3.wFrameCtl = cpu_to_le16(
311                         (
312                                 WLAN_SET_FC_FTYPE(WLAN_TYPE_DATA) |
313                                 WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_NULL) |
314                                 WLAN_SET_FC_PWRMGT(0)
315 ));
316         }
317
318         if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
319                 pTxPacket->p80211Header->sA3.wFrameCtl |= cpu_to_le16((unsigned short)WLAN_SET_FC_TODS(1));
320         }
321
322         memcpy(pTxPacket->p80211Header->sA3.abyAddr1, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN);
323         memcpy(pTxPacket->p80211Header->sA3.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
324         memcpy(pTxPacket->p80211Header->sA3.abyAddr3, pMgmt->abyCurrBSSID, WLAN_BSSID_LEN);
325         pTxPacket->cbMPDULen = WLAN_HDR_ADDR3_LEN;
326         pTxPacket->cbPayloadLen = 0;
327         // send the frame
328         if (csMgmt_xmit(pDevice, pTxPacket) != CMD_STATUS_PENDING) {
329                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send Null Packet failed !\n");
330                 return false;
331         }
332
333         return true;
334 }
335
336 /*+
337  *
338  * Routine Description:
339  * Check if Next TBTT must wake up
340  *
341  * Return Value:
342  *    None.
343  *
344  -*/
345
346 bool
347 PSbIsNextTBTTWakeUp(
348         void *hDeviceContext
349 )
350 {
351         PSDevice         pDevice = (PSDevice)hDeviceContext;
352         PSMgmtObject        pMgmt = pDevice->pMgmt;
353         bool bWakeUp = false;
354
355         if (pMgmt->wListenInterval >= 2) {
356                 if (pMgmt->wCountToWakeUp == 0) {
357                         pMgmt->wCountToWakeUp = pMgmt->wListenInterval;
358                 }
359
360                 pMgmt->wCountToWakeUp--;
361
362                 if (pMgmt->wCountToWakeUp == 1) {
363                         // Turn on wake up to listen next beacon
364                         MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_LNBCN);
365                         bWakeUp = true;
366                 }
367
368         }
369
370         return bWakeUp;
371 }