wl12xx: AP-mode - management of links in PS-mode
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / wl12xx / ps.c
blob5c347b1bd17faae15f9150fb3a50ecfec370e264
1 /*
2 * This file is part of wl1271
4 * Copyright (C) 2008-2009 Nokia Corporation
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
24 #include "reg.h"
25 #include "ps.h"
26 #include "io.h"
27 #include "tx.h"
29 #define WL1271_WAKEUP_TIMEOUT 500
31 void wl1271_elp_work(struct work_struct *work)
33 struct delayed_work *dwork;
34 struct wl1271 *wl;
36 dwork = container_of(work, struct delayed_work, work);
37 wl = container_of(dwork, struct wl1271, elp_work);
39 wl1271_debug(DEBUG_PSM, "elp work");
41 mutex_lock(&wl->mutex);
43 if (unlikely(wl->state == WL1271_STATE_OFF))
44 goto out;
46 if (test_bit(WL1271_FLAG_IN_ELP, &wl->flags) ||
47 (!test_bit(WL1271_FLAG_PSM, &wl->flags) &&
48 !test_bit(WL1271_FLAG_IDLE, &wl->flags)))
49 goto out;
51 wl1271_debug(DEBUG_PSM, "chip to elp");
52 wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
53 set_bit(WL1271_FLAG_IN_ELP, &wl->flags);
55 out:
56 mutex_unlock(&wl->mutex);
59 #define ELP_ENTRY_DELAY 5
61 /* Routines to toggle sleep mode while in ELP */
62 void wl1271_ps_elp_sleep(struct wl1271 *wl)
64 if (test_bit(WL1271_FLAG_PSM, &wl->flags) ||
65 test_bit(WL1271_FLAG_IDLE, &wl->flags)) {
66 cancel_delayed_work(&wl->elp_work);
67 ieee80211_queue_delayed_work(wl->hw, &wl->elp_work,
68 msecs_to_jiffies(ELP_ENTRY_DELAY));
72 int wl1271_ps_elp_wakeup(struct wl1271 *wl, bool chip_awake)
74 DECLARE_COMPLETION_ONSTACK(compl);
75 unsigned long flags;
76 int ret;
77 u32 start_time = jiffies;
78 bool pending = false;
80 if (!test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
81 return 0;
83 wl1271_debug(DEBUG_PSM, "waking up chip from elp");
86 * The spinlock is required here to synchronize both the work and
87 * the completion variable in one entity.
89 spin_lock_irqsave(&wl->wl_lock, flags);
90 if (work_pending(&wl->irq_work) || chip_awake)
91 pending = true;
92 else
93 wl->elp_compl = &compl;
94 spin_unlock_irqrestore(&wl->wl_lock, flags);
96 wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
98 if (!pending) {
99 ret = wait_for_completion_timeout(
100 &compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT));
101 if (ret == 0) {
102 wl1271_error("ELP wakeup timeout!");
103 ieee80211_queue_work(wl->hw, &wl->recovery_work);
104 ret = -ETIMEDOUT;
105 goto err;
106 } else if (ret < 0) {
107 wl1271_error("ELP wakeup completion error.");
108 goto err;
112 clear_bit(WL1271_FLAG_IN_ELP, &wl->flags);
114 wl1271_debug(DEBUG_PSM, "wakeup time: %u ms",
115 jiffies_to_msecs(jiffies - start_time));
116 goto out;
118 err:
119 spin_lock_irqsave(&wl->wl_lock, flags);
120 wl->elp_compl = NULL;
121 spin_unlock_irqrestore(&wl->wl_lock, flags);
122 return ret;
124 out:
125 return 0;
128 int wl1271_ps_set_mode(struct wl1271 *wl, enum wl1271_cmd_ps_mode mode,
129 u32 rates, bool send)
131 int ret;
133 switch (mode) {
134 case STATION_POWER_SAVE_MODE:
135 wl1271_debug(DEBUG_PSM, "entering psm");
137 ret = wl1271_acx_wake_up_conditions(wl);
138 if (ret < 0) {
139 wl1271_error("couldn't set wake up conditions");
140 return ret;
143 ret = wl1271_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE);
144 if (ret < 0)
145 return ret;
147 set_bit(WL1271_FLAG_PSM, &wl->flags);
148 break;
149 case STATION_ACTIVE_MODE:
150 default:
151 wl1271_debug(DEBUG_PSM, "leaving psm");
152 ret = wl1271_ps_elp_wakeup(wl, false);
153 if (ret < 0)
154 return ret;
156 /* disable beacon early termination */
157 ret = wl1271_acx_bet_enable(wl, false);
158 if (ret < 0)
159 return ret;
161 /* disable beacon filtering */
162 ret = wl1271_acx_beacon_filter_opt(wl, false);
163 if (ret < 0)
164 return ret;
166 ret = wl1271_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
167 if (ret < 0)
168 return ret;
170 clear_bit(WL1271_FLAG_PSM, &wl->flags);
171 break;
174 return ret;
177 static void wl1271_ps_filter_frames(struct wl1271 *wl, u8 hlid)
179 int i, filtered = 0;
180 struct sk_buff *skb;
181 struct ieee80211_tx_info *info;
182 unsigned long flags;
184 /* filter all frames currently the low level queus for this hlid */
185 for (i = 0; i < NUM_TX_QUEUES; i++) {
186 while ((skb = skb_dequeue(&wl->links[hlid].tx_queue[i]))) {
187 info = IEEE80211_SKB_CB(skb);
188 info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
189 info->status.rates[0].idx = -1;
190 ieee80211_tx_status(wl->hw, skb);
191 filtered++;
195 spin_lock_irqsave(&wl->wl_lock, flags);
196 wl->tx_queue_count -= filtered;
197 spin_unlock_irqrestore(&wl->wl_lock, flags);
199 wl1271_handle_tx_low_watermark(wl);
202 void wl1271_ps_link_start(struct wl1271 *wl, u8 hlid, bool clean_queues)
204 struct ieee80211_sta *sta;
206 if (test_bit(hlid, &wl->ap_ps_map))
207 return;
209 wl1271_debug(DEBUG_PSM, "start mac80211 PSM on hlid %d blks %d "
210 "clean_queues %d", hlid, wl->links[hlid].allocated_blks,
211 clean_queues);
213 rcu_read_lock();
214 sta = ieee80211_find_sta(wl->vif, wl->links[hlid].addr);
215 if (!sta) {
216 wl1271_error("could not find sta %pM for starting ps",
217 wl->links[hlid].addr);
218 rcu_read_unlock();
219 return;
222 ieee80211_sta_ps_transition_ni(sta, true);
223 rcu_read_unlock();
225 /* do we want to filter all frames from this link's queues? */
226 if (clean_queues)
227 wl1271_ps_filter_frames(wl, hlid);
229 __set_bit(hlid, &wl->ap_ps_map);
232 void wl1271_ps_link_end(struct wl1271 *wl, u8 hlid)
234 struct ieee80211_sta *sta;
236 if (!test_bit(hlid, &wl->ap_ps_map))
237 return;
239 wl1271_debug(DEBUG_PSM, "end mac80211 PSM on hlid %d", hlid);
241 __clear_bit(hlid, &wl->ap_ps_map);
243 rcu_read_lock();
244 sta = ieee80211_find_sta(wl->vif, wl->links[hlid].addr);
245 if (!sta) {
246 wl1271_error("could not find sta %pM for ending ps",
247 wl->links[hlid].addr);
248 goto end;
251 ieee80211_sta_ps_transition_ni(sta, false);
252 end:
253 rcu_read_unlock();