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