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
29 #define WL1271_WAKEUP_TIMEOUT 500
31 void wl1271_elp_work(struct work_struct
*work
)
33 struct delayed_work
*dwork
;
35 struct wl12xx_vif
*wlvif
;
37 dwork
= container_of(work
, struct delayed_work
, work
);
38 wl
= container_of(dwork
, struct wl1271
, elp_work
);
40 wl1271_debug(DEBUG_PSM
, "elp work");
42 mutex_lock(&wl
->mutex
);
44 if (unlikely(wl
->state
== WL1271_STATE_OFF
))
47 /* our work might have been already cancelled */
48 if (unlikely(!test_bit(WL1271_FLAG_ELP_REQUESTED
, &wl
->flags
)))
51 if (test_bit(WL1271_FLAG_IN_ELP
, &wl
->flags
))
54 wl12xx_for_each_wlvif(wl
, wlvif
) {
55 if (wlvif
->bss_type
== BSS_TYPE_AP_BSS
)
58 if (!test_bit(WLVIF_FLAG_IN_PS
, &wlvif
->flags
) &&
59 test_bit(WLVIF_FLAG_IN_USE
, &wlvif
->flags
))
63 wl1271_debug(DEBUG_PSM
, "chip to elp");
64 wl1271_raw_write32(wl
, HW_ACCESS_ELP_CTRL_REG
, ELPCTRL_SLEEP
);
65 set_bit(WL1271_FLAG_IN_ELP
, &wl
->flags
);
68 mutex_unlock(&wl
->mutex
);
71 /* Routines to toggle sleep mode while in ELP */
72 void wl1271_ps_elp_sleep(struct wl1271
*wl
)
74 struct wl12xx_vif
*wlvif
;
76 if (wl
->quirks
& WLCORE_QUIRK_NO_ELP
)
79 /* we shouldn't get consecutive sleep requests */
80 if (WARN_ON(test_and_set_bit(WL1271_FLAG_ELP_REQUESTED
, &wl
->flags
)))
83 wl12xx_for_each_wlvif(wl
, wlvif
) {
84 if (wlvif
->bss_type
== BSS_TYPE_AP_BSS
)
87 if (!test_bit(WLVIF_FLAG_IN_PS
, &wlvif
->flags
) &&
88 test_bit(WLVIF_FLAG_IN_USE
, &wlvif
->flags
))
92 ieee80211_queue_delayed_work(wl
->hw
, &wl
->elp_work
,
93 msecs_to_jiffies(wl
->conf
.conn
.dynamic_ps_timeout
));
96 int wl1271_ps_elp_wakeup(struct wl1271
*wl
)
98 DECLARE_COMPLETION_ONSTACK(compl);
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
))
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
))
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
))
127 wl
->elp_compl
= &compl;
128 spin_unlock_irqrestore(&wl
->wl_lock
, flags
);
130 wl1271_raw_write32(wl
, HW_ACCESS_ELP_CTRL_REG
, ELPCTRL_WAKE_UP
);
133 ret
= wait_for_completion_timeout(
134 &compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT
));
136 wl1271_error("ELP wakeup timeout!");
137 wl12xx_queue_recovery_work(wl
);
140 } else if (ret
< 0) {
141 wl1271_error("ELP wakeup completion error.");
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
));
153 spin_lock_irqsave(&wl
->wl_lock
, flags
);
154 wl
->elp_compl
= NULL
;
155 spin_unlock_irqrestore(&wl
->wl_lock
, flags
);
162 int wl1271_ps_set_mode(struct wl1271
*wl
, struct wl12xx_vif
*wlvif
,
163 enum wl1271_cmd_ps_mode mode
)
166 u16 timeout
= wl
->conf
.conn
.dynamic_ps_timeout
;
169 case STATION_AUTO_PS_MODE
:
170 case STATION_POWER_SAVE_MODE
:
171 wl1271_debug(DEBUG_PSM
, "entering psm (mode=%d,timeout=%u)",
174 ret
= wl1271_acx_wake_up_conditions(wl
, wlvif
,
175 wl
->conf
.conn
.wake_up_event
,
176 wl
->conf
.conn
.listen_interval
);
178 wl1271_error("couldn't set wake up conditions");
182 ret
= wl1271_cmd_ps_mode(wl
, wlvif
, mode
, timeout
);
186 set_bit(WLVIF_FLAG_IN_PS
, &wlvif
->flags
);
188 /* enable beacon early termination. Not relevant for 5GHz */
189 if (wlvif
->band
== IEEE80211_BAND_2GHZ
) {
190 ret
= wl1271_acx_bet_enable(wl
, wlvif
, true);
195 case STATION_ACTIVE_MODE
:
196 wl1271_debug(DEBUG_PSM
, "leaving psm");
198 /* disable beacon early termination */
199 if (wlvif
->band
== IEEE80211_BAND_2GHZ
) {
200 ret
= wl1271_acx_bet_enable(wl
, wlvif
, false);
205 ret
= wl1271_cmd_ps_mode(wl
, wlvif
, mode
, 0);
209 clear_bit(WLVIF_FLAG_IN_PS
, &wlvif
->flags
);
212 wl1271_warning("trying to set ps to unsupported mode %d", mode
);
219 static void wl1271_ps_filter_frames(struct wl1271
*wl
, u8 hlid
)
223 struct ieee80211_tx_info
*info
;
225 int filtered
[NUM_TX_QUEUES
];
227 /* filter all frames currently in the low level queues for this hlid */
228 for (i
= 0; i
< NUM_TX_QUEUES
; i
++) {
230 while ((skb
= skb_dequeue(&wl
->links
[hlid
].tx_queue
[i
]))) {
233 if (WARN_ON(wl12xx_is_dummy_packet(wl
, skb
)))
236 info
= IEEE80211_SKB_CB(skb
);
237 info
->flags
|= IEEE80211_TX_STAT_TX_FILTERED
;
238 info
->status
.rates
[0].idx
= -1;
239 ieee80211_tx_status_ni(wl
->hw
, skb
);
243 spin_lock_irqsave(&wl
->wl_lock
, flags
);
244 for (i
= 0; i
< NUM_TX_QUEUES
; i
++)
245 wl
->tx_queue_count
[i
] -= filtered
[i
];
246 spin_unlock_irqrestore(&wl
->wl_lock
, flags
);
248 wl1271_handle_tx_low_watermark(wl
);
251 void wl12xx_ps_link_start(struct wl1271
*wl
, struct wl12xx_vif
*wlvif
,
252 u8 hlid
, bool clean_queues
)
254 struct ieee80211_sta
*sta
;
255 struct ieee80211_vif
*vif
= wl12xx_wlvif_to_vif(wlvif
);
257 if (test_bit(hlid
, &wl
->ap_ps_map
))
260 wl1271_debug(DEBUG_PSM
, "start mac80211 PSM on hlid %d pkts %d "
261 "clean_queues %d", hlid
, wl
->links
[hlid
].allocated_pkts
,
265 sta
= ieee80211_find_sta(vif
, wl
->links
[hlid
].addr
);
267 wl1271_error("could not find sta %pM for starting ps",
268 wl
->links
[hlid
].addr
);
273 ieee80211_sta_ps_transition_ni(sta
, true);
276 /* do we want to filter all frames from this link's queues? */
278 wl1271_ps_filter_frames(wl
, hlid
);
280 __set_bit(hlid
, &wl
->ap_ps_map
);
283 void wl12xx_ps_link_end(struct wl1271
*wl
, struct wl12xx_vif
*wlvif
, u8 hlid
)
285 struct ieee80211_sta
*sta
;
286 struct ieee80211_vif
*vif
= wl12xx_wlvif_to_vif(wlvif
);
288 if (!test_bit(hlid
, &wl
->ap_ps_map
))
291 wl1271_debug(DEBUG_PSM
, "end mac80211 PSM on hlid %d", hlid
);
293 __clear_bit(hlid
, &wl
->ap_ps_map
);
296 sta
= ieee80211_find_sta(vif
, wl
->links
[hlid
].addr
);
298 wl1271_error("could not find sta %pM for ending ps",
299 wl
->links
[hlid
].addr
);
303 ieee80211_sta_ps_transition_ni(sta
, false);