spi-topcliff-pch: supports a spi mode setup and bit order setup by IO control
[zen-stable.git] / drivers / net / wireless / wl12xx / ps.c
bloba2bdacdd7e1dd742ae9398ef70c1d2eb1c8037d2
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"
28 #include "debug.h"
30 #define WL1271_WAKEUP_TIMEOUT 500
32 void wl1271_elp_work(struct work_struct *work)
34 struct delayed_work *dwork;
35 struct wl1271 *wl;
36 struct wl12xx_vif *wlvif;
38 dwork = container_of(work, struct delayed_work, work);
39 wl = container_of(dwork, struct wl1271, elp_work);
41 wl1271_debug(DEBUG_PSM, "elp work");
43 mutex_lock(&wl->mutex);
45 if (unlikely(wl->state == WL1271_STATE_OFF))
46 goto out;
48 /* our work might have been already cancelled */
49 if (unlikely(!test_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags)))
50 goto out;
52 if (test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
53 goto out;
55 wl12xx_for_each_wlvif(wl, wlvif) {
56 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
57 goto out;
59 if (!test_bit(WLVIF_FLAG_PSM, &wlvif->flags) &&
60 test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags))
61 goto out;
64 wl1271_debug(DEBUG_PSM, "chip to elp");
65 wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
66 set_bit(WL1271_FLAG_IN_ELP, &wl->flags);
68 out:
69 mutex_unlock(&wl->mutex);
72 #define ELP_ENTRY_DELAY 5
74 /* Routines to toggle sleep mode while in ELP */
75 void wl1271_ps_elp_sleep(struct wl1271 *wl)
77 struct wl12xx_vif *wlvif;
79 /* we shouldn't get consecutive sleep requests */
80 if (WARN_ON(test_and_set_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags)))
81 return;
83 wl12xx_for_each_wlvif(wl, wlvif) {
84 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
85 return;
87 if (!test_bit(WLVIF_FLAG_PSM, &wlvif->flags) &&
88 test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags))
89 return;
92 ieee80211_queue_delayed_work(wl->hw, &wl->elp_work,
93 msecs_to_jiffies(ELP_ENTRY_DELAY));
96 int wl1271_ps_elp_wakeup(struct wl1271 *wl)
98 DECLARE_COMPLETION_ONSTACK(compl);
99 unsigned long flags;
100 int ret;
101 u32 start_time = jiffies;
102 bool pending = false;
105 * we might try to wake up even if we didn't go to sleep
106 * before (e.g. on boot)
108 if (!test_and_clear_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags))
109 return 0;
111 /* don't cancel_sync as it might contend for a mutex and deadlock */
112 cancel_delayed_work(&wl->elp_work);
114 if (!test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
115 return 0;
117 wl1271_debug(DEBUG_PSM, "waking up chip from elp");
120 * The spinlock is required here to synchronize both the work and
121 * the completion variable in one entity.
123 spin_lock_irqsave(&wl->wl_lock, flags);
124 if (test_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags))
125 pending = true;
126 else
127 wl->elp_compl = &compl;
128 spin_unlock_irqrestore(&wl->wl_lock, flags);
130 wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
132 if (!pending) {
133 ret = wait_for_completion_timeout(
134 &compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT));
135 if (ret == 0) {
136 wl1271_error("ELP wakeup timeout!");
137 wl12xx_queue_recovery_work(wl);
138 ret = -ETIMEDOUT;
139 goto err;
140 } else if (ret < 0) {
141 wl1271_error("ELP wakeup completion error.");
142 goto err;
146 clear_bit(WL1271_FLAG_IN_ELP, &wl->flags);
148 wl1271_debug(DEBUG_PSM, "wakeup time: %u ms",
149 jiffies_to_msecs(jiffies - start_time));
150 goto out;
152 err:
153 spin_lock_irqsave(&wl->wl_lock, flags);
154 wl->elp_compl = NULL;
155 spin_unlock_irqrestore(&wl->wl_lock, flags);
156 return ret;
158 out:
159 return 0;
162 int wl1271_ps_set_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
163 enum wl1271_cmd_ps_mode mode, u32 rates, bool send)
165 int ret;
167 switch (mode) {
168 case STATION_POWER_SAVE_MODE:
169 wl1271_debug(DEBUG_PSM, "entering psm");
171 ret = wl1271_acx_wake_up_conditions(wl, wlvif);
172 if (ret < 0) {
173 wl1271_error("couldn't set wake up conditions");
174 return ret;
177 ret = wl1271_cmd_ps_mode(wl, wlvif, STATION_POWER_SAVE_MODE);
178 if (ret < 0)
179 return ret;
181 set_bit(WLVIF_FLAG_PSM, &wlvif->flags);
182 break;
183 case STATION_ACTIVE_MODE:
184 default:
185 wl1271_debug(DEBUG_PSM, "leaving psm");
187 /* disable beacon early termination */
188 if (wlvif->band == IEEE80211_BAND_2GHZ) {
189 ret = wl1271_acx_bet_enable(wl, wlvif, false);
190 if (ret < 0)
191 return ret;
194 ret = wl1271_cmd_ps_mode(wl, wlvif, STATION_ACTIVE_MODE);
195 if (ret < 0)
196 return ret;
198 clear_bit(WLVIF_FLAG_PSM, &wlvif->flags);
199 break;
202 return ret;
205 static void wl1271_ps_filter_frames(struct wl1271 *wl, u8 hlid)
207 int i;
208 struct sk_buff *skb;
209 struct ieee80211_tx_info *info;
210 unsigned long flags;
211 int filtered[NUM_TX_QUEUES];
213 /* filter all frames currently in the low level queues for this hlid */
214 for (i = 0; i < NUM_TX_QUEUES; i++) {
215 filtered[i] = 0;
216 while ((skb = skb_dequeue(&wl->links[hlid].tx_queue[i]))) {
217 filtered[i]++;
219 if (WARN_ON(wl12xx_is_dummy_packet(wl, skb)))
220 continue;
222 info = IEEE80211_SKB_CB(skb);
223 info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
224 info->status.rates[0].idx = -1;
225 ieee80211_tx_status_ni(wl->hw, skb);
229 spin_lock_irqsave(&wl->wl_lock, flags);
230 for (i = 0; i < NUM_TX_QUEUES; i++)
231 wl->tx_queue_count[i] -= filtered[i];
232 spin_unlock_irqrestore(&wl->wl_lock, flags);
234 wl1271_handle_tx_low_watermark(wl);
237 void wl12xx_ps_link_start(struct wl1271 *wl, struct wl12xx_vif *wlvif,
238 u8 hlid, bool clean_queues)
240 struct ieee80211_sta *sta;
241 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
243 if (test_bit(hlid, &wl->ap_ps_map))
244 return;
246 wl1271_debug(DEBUG_PSM, "start mac80211 PSM on hlid %d pkts %d "
247 "clean_queues %d", hlid, wl->links[hlid].allocated_pkts,
248 clean_queues);
250 rcu_read_lock();
251 sta = ieee80211_find_sta(vif, wl->links[hlid].addr);
252 if (!sta) {
253 wl1271_error("could not find sta %pM for starting ps",
254 wl->links[hlid].addr);
255 rcu_read_unlock();
256 return;
259 ieee80211_sta_ps_transition_ni(sta, true);
260 rcu_read_unlock();
262 /* do we want to filter all frames from this link's queues? */
263 if (clean_queues)
264 wl1271_ps_filter_frames(wl, hlid);
266 __set_bit(hlid, &wl->ap_ps_map);
269 void wl12xx_ps_link_end(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 hlid)
271 struct ieee80211_sta *sta;
272 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
274 if (!test_bit(hlid, &wl->ap_ps_map))
275 return;
277 wl1271_debug(DEBUG_PSM, "end mac80211 PSM on hlid %d", hlid);
279 __clear_bit(hlid, &wl->ap_ps_map);
281 rcu_read_lock();
282 sta = ieee80211_find_sta(vif, wl->links[hlid].addr);
283 if (!sta) {
284 wl1271_error("could not find sta %pM for ending ps",
285 wl->links[hlid].addr);
286 goto end;
289 ieee80211_sta_ps_transition_ni(sta, false);
290 end:
291 rcu_read_unlock();