GNU Linux-libre 4.14.257-gnu1
[releases.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  *
16  * File: power.c
17  *
18  * Purpose: Handles 802.11 power management functions
19  *
20  * Author: Lyndon Chen
21  *
22  * Date: July 17, 2002
23  *
24  * Functions:
25  *      vnt_enable_power_saving - Enable Power Saving Mode
26  *      PSvDiasblePowerSaving - Disable Power Saving Mode
27  *      vnt_next_tbtt_wakeup - Decide if we need to wake up at next Beacon
28  *
29  * Revision History:
30  *
31  */
32
33 #include "mac.h"
34 #include "device.h"
35 #include "power.h"
36 #include "wcmd.h"
37 #include "rxtx.h"
38 #include "card.h"
39 #include "usbpipe.h"
40
41 /*
42  *
43  * Routine Description:
44  * Enable hw power saving functions
45  *
46  * Return Value:
47  *    None.
48  *
49  */
50
51 void vnt_enable_power_saving(struct vnt_private *priv, u16 listen_interval)
52 {
53         u16 aid = priv->current_aid | BIT(14) | BIT(15);
54
55         /* set period of power up before TBTT */
56         vnt_mac_write_word(priv, MAC_REG_PWBT, C_PWBT);
57
58         if (priv->op_mode != NL80211_IFTYPE_ADHOC)
59                 /* set AID */
60                 vnt_mac_write_word(priv, MAC_REG_AIDATIM, aid);
61
62         /* Warren:06-18-2004,the sequence must follow
63          * PSEN->AUTOSLEEP->GO2DOZE
64          */
65         /* enable power saving hw function */
66         vnt_mac_reg_bits_on(priv, MAC_REG_PSCTL, PSCTL_PSEN);
67
68         /* Set AutoSleep */
69         vnt_mac_reg_bits_on(priv, MAC_REG_PSCFG, PSCFG_AUTOSLEEP);
70
71         /* Warren:MUST turn on this once before turn on AUTOSLEEP ,or the
72          * AUTOSLEEP doesn't work
73          */
74         vnt_mac_reg_bits_on(priv, MAC_REG_PSCTL, PSCTL_GO2DOZE);
75
76         if (listen_interval >= 2) {
77                 /* clear always listen beacon */
78                 vnt_mac_reg_bits_off(priv, MAC_REG_PSCTL, PSCTL_ALBCN);
79
80                 /* first time set listen next beacon */
81                 vnt_mac_reg_bits_on(priv, MAC_REG_PSCTL, PSCTL_LNBCN);
82         } else {
83                 /* always listen beacon */
84                 vnt_mac_reg_bits_on(priv, MAC_REG_PSCTL, PSCTL_ALBCN);
85         }
86
87         dev_dbg(&priv->usb->dev,  "PS:Power Saving Mode Enable...\n");
88 }
89
90 /*
91  *
92  * Routine Description:
93  * Disable hw power saving functions
94  *
95  * Return Value:
96  *    None.
97  *
98  */
99
100 void vnt_disable_power_saving(struct vnt_private *priv)
101 {
102         /* disable power saving hw function */
103         vnt_control_out(priv, MESSAGE_TYPE_DISABLE_PS, 0,
104                         0, 0, NULL);
105
106         /* clear AutoSleep */
107         vnt_mac_reg_bits_off(priv, MAC_REG_PSCFG, PSCFG_AUTOSLEEP);
108
109         /* set always listen beacon */
110         vnt_mac_reg_bits_on(priv, MAC_REG_PSCTL, PSCTL_ALBCN);
111 }
112
113 /*
114  *
115  * Routine Description:
116  * Check if Next TBTT must wake up
117  *
118  * Return Value:
119  *    None.
120  *
121  */
122
123 int vnt_next_tbtt_wakeup(struct vnt_private *priv)
124 {
125         struct ieee80211_hw *hw = priv->hw;
126         struct ieee80211_conf *conf = &hw->conf;
127         int wake_up = false;
128
129         if (conf->listen_interval > 1) {
130                 /* Turn on wake up to listen next beacon */
131                 vnt_mac_reg_bits_on(priv, MAC_REG_PSCTL, PSCTL_LNBCN);
132                 wake_up = true;
133         }
134
135         return wake_up;
136 }