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
;
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
))
46 /* our work might have been already cancelled */
47 if (unlikely(!test_bit(WL1271_FLAG_ELP_REQUESTED
, &wl
->flags
)))
50 if (test_bit(WL1271_FLAG_IN_ELP
, &wl
->flags
) ||
51 (!test_bit(WL1271_FLAG_PSM
, &wl
->flags
) &&
52 !test_bit(WL1271_FLAG_IDLE
, &wl
->flags
)))
55 wl1271_debug(DEBUG_PSM
, "chip to elp");
56 wl1271_raw_write32(wl
, HW_ACCESS_ELP_CTRL_REG_ADDR
, ELPCTRL_SLEEP
);
57 set_bit(WL1271_FLAG_IN_ELP
, &wl
->flags
);
60 mutex_unlock(&wl
->mutex
);
63 #define ELP_ENTRY_DELAY 5
65 /* Routines to toggle sleep mode while in ELP */
66 void wl1271_ps_elp_sleep(struct wl1271
*wl
)
68 /* we shouldn't get consecutive sleep requests */
69 if (WARN_ON(test_and_set_bit(WL1271_FLAG_ELP_REQUESTED
, &wl
->flags
)))
72 if (!test_bit(WL1271_FLAG_PSM
, &wl
->flags
) &&
73 !test_bit(WL1271_FLAG_IDLE
, &wl
->flags
))
76 ieee80211_queue_delayed_work(wl
->hw
, &wl
->elp_work
,
77 msecs_to_jiffies(ELP_ENTRY_DELAY
));
80 int wl1271_ps_elp_wakeup(struct wl1271
*wl
)
82 DECLARE_COMPLETION_ONSTACK(compl);
85 u32 start_time
= jiffies
;
89 * we might try to wake up even if we didn't go to sleep
90 * before (e.g. on boot)
92 if (!test_and_clear_bit(WL1271_FLAG_ELP_REQUESTED
, &wl
->flags
))
95 /* don't cancel_sync as it might contend for a mutex and deadlock */
96 cancel_delayed_work(&wl
->elp_work
);
98 if (!test_bit(WL1271_FLAG_IN_ELP
, &wl
->flags
))
101 wl1271_debug(DEBUG_PSM
, "waking up chip from elp");
104 * The spinlock is required here to synchronize both the work and
105 * the completion variable in one entity.
107 spin_lock_irqsave(&wl
->wl_lock
, flags
);
108 if (test_bit(WL1271_FLAG_IRQ_RUNNING
, &wl
->flags
))
111 wl
->elp_compl
= &compl;
112 spin_unlock_irqrestore(&wl
->wl_lock
, flags
);
114 wl1271_raw_write32(wl
, HW_ACCESS_ELP_CTRL_REG_ADDR
, ELPCTRL_WAKE_UP
);
117 ret
= wait_for_completion_timeout(
118 &compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT
));
120 wl1271_error("ELP wakeup timeout!");
121 wl12xx_queue_recovery_work(wl
);
124 } else if (ret
< 0) {
125 wl1271_error("ELP wakeup completion error.");
130 clear_bit(WL1271_FLAG_IN_ELP
, &wl
->flags
);
132 wl1271_debug(DEBUG_PSM
, "wakeup time: %u ms",
133 jiffies_to_msecs(jiffies
- start_time
));
137 spin_lock_irqsave(&wl
->wl_lock
, flags
);
138 wl
->elp_compl
= NULL
;
139 spin_unlock_irqrestore(&wl
->wl_lock
, flags
);
146 int wl1271_ps_set_mode(struct wl1271
*wl
, enum wl1271_cmd_ps_mode mode
,
147 u32 rates
, bool send
)
152 case STATION_POWER_SAVE_MODE
:
153 wl1271_debug(DEBUG_PSM
, "entering psm");
155 ret
= wl1271_acx_wake_up_conditions(wl
);
157 wl1271_error("couldn't set wake up conditions");
161 ret
= wl1271_cmd_ps_mode(wl
, STATION_POWER_SAVE_MODE
);
165 set_bit(WL1271_FLAG_PSM
, &wl
->flags
);
167 case STATION_ACTIVE_MODE
:
169 wl1271_debug(DEBUG_PSM
, "leaving psm");
171 /* disable beacon early termination */
172 if (wl
->band
== IEEE80211_BAND_2GHZ
) {
173 ret
= wl1271_acx_bet_enable(wl
, false);
178 /* disable beacon filtering */
179 ret
= wl1271_acx_beacon_filter_opt(wl
, false);
183 ret
= wl1271_cmd_ps_mode(wl
, STATION_ACTIVE_MODE
);
187 clear_bit(WL1271_FLAG_PSM
, &wl
->flags
);
194 static void wl1271_ps_filter_frames(struct wl1271
*wl
, u8 hlid
)
198 struct ieee80211_tx_info
*info
;
200 int filtered
[NUM_TX_QUEUES
];
202 /* filter all frames currently the low level queus for this hlid */
203 for (i
= 0; i
< NUM_TX_QUEUES
; i
++) {
205 while ((skb
= skb_dequeue(&wl
->links
[hlid
].tx_queue
[i
]))) {
206 info
= IEEE80211_SKB_CB(skb
);
207 info
->flags
|= IEEE80211_TX_STAT_TX_FILTERED
;
208 info
->status
.rates
[0].idx
= -1;
209 ieee80211_tx_status_ni(wl
->hw
, skb
);
214 spin_lock_irqsave(&wl
->wl_lock
, flags
);
215 for (i
= 0; i
< NUM_TX_QUEUES
; i
++)
216 wl
->tx_queue_count
[i
] -= filtered
[i
];
217 spin_unlock_irqrestore(&wl
->wl_lock
, flags
);
219 wl1271_handle_tx_low_watermark(wl
);
222 void wl1271_ps_link_start(struct wl1271
*wl
, u8 hlid
, bool clean_queues
)
224 struct ieee80211_sta
*sta
;
226 if (test_bit(hlid
, &wl
->ap_ps_map
))
229 wl1271_debug(DEBUG_PSM
, "start mac80211 PSM on hlid %d blks %d "
230 "clean_queues %d", hlid
, wl
->links
[hlid
].allocated_blks
,
234 sta
= ieee80211_find_sta(wl
->vif
, wl
->links
[hlid
].addr
);
236 wl1271_error("could not find sta %pM for starting ps",
237 wl
->links
[hlid
].addr
);
242 ieee80211_sta_ps_transition_ni(sta
, true);
245 /* do we want to filter all frames from this link's queues? */
247 wl1271_ps_filter_frames(wl
, hlid
);
249 __set_bit(hlid
, &wl
->ap_ps_map
);
252 void wl1271_ps_link_end(struct wl1271
*wl
, u8 hlid
)
254 struct ieee80211_sta
*sta
;
256 if (!test_bit(hlid
, &wl
->ap_ps_map
))
259 wl1271_debug(DEBUG_PSM
, "end mac80211 PSM on hlid %d", hlid
);
261 __clear_bit(hlid
, &wl
->ap_ps_map
);
264 sta
= ieee80211_find_sta(wl
->vif
, wl
->links
[hlid
].addr
);
266 wl1271_error("could not find sta %pM for ending ps",
267 wl
->links
[hlid
].addr
);
271 ieee80211_sta_ps_transition_ni(sta
, false);