Merge tag 'vfio-v3.9-rc1' of git://github.com/awilliam/linux-vfio
[cascardo/linux.git] / drivers / staging / vt6656 / 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 #include "control.h"
49 #include "rndis.h"
50
51 /*---------------------  Static Definitions -------------------------*/
52
53 /*---------------------  Static Classes  ----------------------------*/
54
55 /*---------------------  Static Variables  --------------------------*/
56 static int msglevel = MSG_LEVEL_INFO;
57 /*---------------------  Static Functions  --------------------------*/
58
59 /*---------------------  Export Variables  --------------------------*/
60
61 /*---------------------  Export Functions  --------------------------*/
62
63 /*
64  *
65  * Routine Description:
66  * Enable hw power saving functions
67  *
68  * Return Value:
69  *    None.
70  *
71  */
72
73 void PSvEnablePowerSaving(struct vnt_private *pDevice, u16 wListenInterval)
74 {
75         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
76         u16 wAID = pMgmt->wCurrAID | BIT14 | BIT15;
77
78         /* set period of power up before TBTT */
79         MACvWriteWord(pDevice, MAC_REG_PWBT, C_PWBT);
80
81         if (pDevice->eOPMode != OP_MODE_ADHOC) {
82                 /* set AID */
83                 MACvWriteWord(pDevice, MAC_REG_AIDATIM, wAID);
84         } else {
85                 /* set ATIM Window */
86                 /* MACvWriteATIMW(pDevice->PortOffset, pMgmt->wCurrATIMWindow); */
87         }
88
89         /* Warren:06-18-2004,the sequence must follow PSEN->AUTOSLEEP->GO2DOZE */
90         /* enable power saving hw function */
91         MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_PSEN);
92
93         /* Set AutoSleep */
94         MACvRegBitsOn(pDevice, MAC_REG_PSCFG, PSCFG_AUTOSLEEP);
95
96         /* Warren:MUST turn on this once before turn on AUTOSLEEP ,or the AUTOSLEEP doesn't work */
97         MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_GO2DOZE);
98
99         if (wListenInterval >= 2) {
100
101                 /* clear always listen beacon */
102                 MACvRegBitsOff(pDevice, MAC_REG_PSCTL, PSCTL_ALBCN);
103
104                 /* first time set listen next beacon */
105                 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_LNBCN);
106
107                 pMgmt->wCountToWakeUp = wListenInterval;
108
109         } else {
110
111                 /* always listen beacon */
112                 MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_ALBCN);
113
114                 pMgmt->wCountToWakeUp = 0;
115         }
116
117         pDevice->bEnablePSMode = true;
118
119         /* We don't send null pkt in ad hoc mode since beacon will handle this. */
120         if (pDevice->eOPMode == OP_MODE_INFRASTRUCTURE)
121                 PSbSendNullPacket(pDevice);
122
123         pDevice->bPWBitOn = true;
124         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "PS:Power Saving Mode Enable...\n");
125 }
126
127 /*
128  *
129  * Routine Description:
130  * Disable hw power saving functions
131  *
132  * Return Value:
133  *    None.
134  *
135  */
136
137 void PSvDisablePowerSaving(struct vnt_private *pDevice)
138 {
139
140         /* disable power saving hw function */
141         CONTROLnsRequestOut(pDevice, MESSAGE_TYPE_DISABLE_PS, 0,
142                                                 0, 0, NULL);
143
144         /* clear AutoSleep */
145         MACvRegBitsOff(pDevice, MAC_REG_PSCFG, PSCFG_AUTOSLEEP);
146
147         /* set always listen beacon */
148         MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_ALBCN);
149         pDevice->bEnablePSMode = false;
150
151         if (pDevice->eOPMode == OP_MODE_INFRASTRUCTURE)
152                 PSbSendNullPacket(pDevice);
153
154         pDevice->bPWBitOn = false;
155 }
156
157 /*
158  *
159  * Routine Description:
160  * Consider to power down when no more packets to tx or rx.
161  *
162  * Return Value:
163  *    true, if power down success
164  *    false, if fail
165  */
166
167 int PSbConsiderPowerDown(struct vnt_private *pDevice, int bCheckRxDMA,
168         int bCheckCountToWakeUp)
169 {
170         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
171         u8 byData;
172
173         /* check if already in Doze mode */
174         ControlvReadByte(pDevice, MESSAGE_REQUEST_MACREG,
175                                         MAC_REG_PSCTL, &byData);
176
177         if ((byData & PSCTL_PS) != 0)
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         /* Tx Burst */
191         if (pDevice->bPSModeTxBurst)
192                 return false;
193
194         /* Froce PSEN on */
195         MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_PSEN);
196
197         if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA) {
198                 if (bCheckCountToWakeUp && (pMgmt->wCountToWakeUp == 0
199                         || pMgmt->wCountToWakeUp == 1)) {
200                                 return false;
201                 }
202         }
203
204         pDevice->bPSRxBeacon = true;
205
206         /* no Tx, no Rx isr, now go to Doze */
207         MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_GO2DOZE);
208         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Go to Doze ZZZZZZZZZZZZZZZ\n");
209         return true;
210 }
211
212 /*
213  *
214  * Routine Description:
215  * Send PS-POLL packet
216  *
217  * Return Value:
218  *    None.
219  *
220  */
221
222 void PSvSendPSPOLL(struct vnt_private *pDevice)
223 {
224         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
225         struct vnt_tx_mgmt *pTxPacket = NULL;
226
227         memset(pMgmt->pbyPSPacketPool, 0, sizeof(struct vnt_tx_mgmt)
228                 + WLAN_HDR_ADDR2_LEN);
229         pTxPacket = (struct vnt_tx_mgmt *)pMgmt->pbyPSPacketPool;
230         pTxPacket->p80211Header = (PUWLAN_80211HDR)((u8 *)pTxPacket
231                 + sizeof(struct vnt_tx_mgmt));
232
233         pTxPacket->p80211Header->sA2.wFrameCtl = cpu_to_le16(
234                 (
235                         WLAN_SET_FC_FTYPE(WLAN_TYPE_CTL) |
236                         WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_PSPOLL) |
237                         WLAN_SET_FC_PWRMGT(0)
238                 ));
239
240         pTxPacket->p80211Header->sA2.wDurationID = pMgmt->wCurrAID | BIT14 | BIT15;
241         memcpy(pTxPacket->p80211Header->sA2.abyAddr1, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN);
242         memcpy(pTxPacket->p80211Header->sA2.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
243         pTxPacket->cbMPDULen = WLAN_HDR_ADDR2_LEN;
244         pTxPacket->cbPayloadLen = 0;
245
246         /* log failure if sending failed */
247         if (csMgmt_xmit(pDevice, pTxPacket) != CMD_STATUS_PENDING) {
248                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send PS-Poll packet failed..\n");
249         }
250 }
251
252 /*
253  *
254  * Routine Description:
255  * Send NULL packet to AP for notification power state of STA
256  *
257  * Return Value:
258  *    None.
259  *
260  */
261
262 int PSbSendNullPacket(struct vnt_private *pDevice)
263 {
264         struct vnt_tx_mgmt *pTxPacket = NULL;
265         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
266         u16 flags = 0;
267
268         if (pDevice->bLinkPass == false)
269                 return false;
270
271         if ((pDevice->bEnablePSMode == false) &&
272                 (pDevice->fTxDataInSleep == false)) {
273                         return false;
274         }
275
276         memset(pMgmt->pbyPSPacketPool, 0, sizeof(struct vnt_tx_mgmt)
277                 + WLAN_NULLDATA_FR_MAXLEN);
278         pTxPacket = (struct vnt_tx_mgmt *)pMgmt->pbyPSPacketPool;
279         pTxPacket->p80211Header = (PUWLAN_80211HDR)((u8 *)pTxPacket
280                 + sizeof(struct vnt_tx_mgmt));
281
282         flags = WLAN_SET_FC_FTYPE(WLAN_TYPE_DATA) |
283                         WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_NULL);
284
285         if (pDevice->bEnablePSMode)
286                 flags |= WLAN_SET_FC_PWRMGT(1);
287         else
288                 flags |= WLAN_SET_FC_PWRMGT(0);
289
290         pTxPacket->p80211Header->sA3.wFrameCtl = cpu_to_le16(flags);
291
292         if (pMgmt->eCurrMode != WMAC_MODE_IBSS_STA)
293                 pTxPacket->p80211Header->sA3.wFrameCtl |= cpu_to_le16((WORD)WLAN_SET_FC_TODS(1));
294
295         memcpy(pTxPacket->p80211Header->sA3.abyAddr1, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN);
296         memcpy(pTxPacket->p80211Header->sA3.abyAddr2, pMgmt->abyMACAddr, WLAN_ADDR_LEN);
297         memcpy(pTxPacket->p80211Header->sA3.abyAddr3, pMgmt->abyCurrBSSID, WLAN_BSSID_LEN);
298         pTxPacket->cbMPDULen = WLAN_HDR_ADDR3_LEN;
299         pTxPacket->cbPayloadLen = 0;
300         /* log error if sending failed */
301         if (csMgmt_xmit(pDevice, pTxPacket) != CMD_STATUS_PENDING) {
302                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Send Null Packet failed !\n");
303                 return false;
304         }
305         return true;
306 }
307
308 /*
309  *
310  * Routine Description:
311  * Check if Next TBTT must wake up
312  *
313  * Return Value:
314  *    None.
315  *
316  */
317
318 int PSbIsNextTBTTWakeUp(struct vnt_private *pDevice)
319 {
320         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
321         int bWakeUp = false;
322
323         if (pMgmt->wListenInterval >= 2) {
324                 if (pMgmt->wCountToWakeUp == 0)
325                         pMgmt->wCountToWakeUp = pMgmt->wListenInterval;
326
327                 pMgmt->wCountToWakeUp--;
328
329                 if (pMgmt->wCountToWakeUp == 1) {
330                         /* Turn on wake up to listen next beacon */
331                         MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_LNBCN);
332                         pDevice->bPSRxBeacon = false;
333                         bWakeUp = true;
334                 } else if (!pDevice->bPSRxBeacon) {
335                         /* Listen until RxBeacon */
336                         MACvRegBitsOn(pDevice, MAC_REG_PSCTL, PSCTL_LNBCN);
337                 }
338         }
339         return bWakeUp;
340 }
341