1/* 2 * This file is part of wl1271 3 * 4 * Copyright (C) 2008-2009 Nokia Corporation 5 * 6 * Contact: Luciano Coelho <luciano.coelho@nokia.com> 7 * 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. 11 * 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. 16 * 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 21 * 22 */ 23 24#include "wl1271.h" 25#include "wl1271_reg.h" 26#include "wl1271_io.h" 27#include "wl1271_event.h" 28#include "wl1271_ps.h" 29#include "wl1271_scan.h" 30#include "wl12xx_80211.h" 31 32void wl1271_pspoll_work(struct work_struct *work) 33{ 34 struct delayed_work *dwork; 35 struct wl1271 *wl; 36 37 dwork = container_of(work, struct delayed_work, work); 38 wl = container_of(dwork, struct wl1271, pspoll_work); 39 40 wl1271_debug(DEBUG_EVENT, "pspoll work"); 41 42 mutex_lock(&wl->mutex); 43 44 if (!test_and_clear_bit(WL1271_FLAG_PSPOLL_FAILURE, &wl->flags)) 45 goto out; 46 47 if (!test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags)) 48 goto out; 49 50 /* 51 * if we end up here, then we were in powersave when the pspoll 52 * delivery failure occurred, and no-one changed state since, so 53 * we should go back to powersave. 54 */ 55 wl1271_ps_set_mode(wl, STATION_POWER_SAVE_MODE, true); 56 57out: 58 mutex_unlock(&wl->mutex); 59}; 60 61static void wl1271_event_pspoll_delivery_fail(struct wl1271 *wl) 62{ 63 int delay = wl->conf.conn.ps_poll_recovery_period; 64 int ret; 65 66 wl->ps_poll_failures++; 67 if (wl->ps_poll_failures == 1) 68 wl1271_info("AP with dysfunctional ps-poll, " 69 "trying to work around it."); 70 71 /* force active mode receive data from the AP */ 72 if (test_bit(WL1271_FLAG_PSM, &wl->flags)) { 73 ret = wl1271_ps_set_mode(wl, STATION_ACTIVE_MODE, true); 74 if (ret < 0) 75 return; 76 set_bit(WL1271_FLAG_PSPOLL_FAILURE, &wl->flags); 77 ieee80211_queue_delayed_work(wl->hw, &wl->pspoll_work, 78 msecs_to_jiffies(delay)); 79 } 80 81 /* 82 * If already in active mode, lets we should be getting data from 83 * the AP right away. If we enter PSM too fast after this, and data 84 * remains on the AP, we will get another event like this, and we'll 85 * go into active once more. 86 */ 87} 88 89static int wl1271_event_ps_report(struct wl1271 *wl, 90 struct event_mailbox *mbox, 91 bool *beacon_loss) 92{ 93 int ret = 0; 94 95 wl1271_debug(DEBUG_EVENT, "ps_status: 0x%x", mbox->ps_status); 96 97 switch (mbox->ps_status) { 98 case EVENT_ENTER_POWER_SAVE_FAIL: 99 wl1271_debug(DEBUG_PSM, "PSM entry failed"); 100 101 if (!test_bit(WL1271_FLAG_PSM, &wl->flags)) { 102 /* remain in active mode */ 103 wl->psm_entry_retry = 0; 104 break; 105 } 106 107 if (wl->psm_entry_retry < wl->conf.conn.psm_entry_retries) { 108 wl->psm_entry_retry++; 109 ret = wl1271_ps_set_mode(wl, STATION_POWER_SAVE_MODE, 110 true); 111 } else { 112 wl1271_info("No ack to nullfunc from AP."); 113 wl->psm_entry_retry = 0; 114 *beacon_loss = true; 115 } 116 break; 117 case EVENT_ENTER_POWER_SAVE_SUCCESS: 118 wl->psm_entry_retry = 0; 119 120 /* enable beacon filtering */ 121 ret = wl1271_acx_beacon_filter_opt(wl, true); 122 if (ret < 0) 123 break; 124 125 /* enable beacon early termination */ 126 ret = wl1271_acx_bet_enable(wl, true); 127 if (ret < 0) 128 break; 129 130 /* go to extremely low power mode */ 131 wl1271_ps_elp_sleep(wl); 132 if (ret < 0) 133 break; 134 break; 135 case EVENT_EXIT_POWER_SAVE_FAIL: 136 wl1271_debug(DEBUG_PSM, "PSM exit failed"); 137 138 if (test_bit(WL1271_FLAG_PSM, &wl->flags)) { 139 wl->psm_entry_retry = 0; 140 break; 141 } 142 143 /* make sure the firmware goes to active mode - the frame to 144 be sent next will indicate to the AP, that we are active. */ 145 ret = wl1271_ps_set_mode(wl, STATION_ACTIVE_MODE, 146 false); 147 break; 148 case EVENT_EXIT_POWER_SAVE_SUCCESS: 149 default: 150 break; 151 } 152 153 return ret; 154} 155 156static void wl1271_event_rssi_trigger(struct wl1271 *wl, 157 struct event_mailbox *mbox) 158{ 159 enum nl80211_cqm_rssi_threshold_event event; 160 s8 metric = mbox->rssi_snr_trigger_metric[0]; 161 162 wl1271_debug(DEBUG_EVENT, "RSSI trigger metric: %d", metric); 163 164 if (metric <= wl->rssi_thold) 165 event = NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW; 166 else 167 event = NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH; 168 169 if (event != wl->last_rssi_event) 170 ieee80211_cqm_rssi_notify(wl->vif, event, GFP_KERNEL); 171 wl->last_rssi_event = event; 172} 173 174static void wl1271_event_mbox_dump(struct event_mailbox *mbox) 175{ 176 wl1271_debug(DEBUG_EVENT, "MBOX DUMP:"); 177 wl1271_debug(DEBUG_EVENT, "\tvector: 0x%x", mbox->events_vector); 178 wl1271_debug(DEBUG_EVENT, "\tmask: 0x%x", mbox->events_mask); 179} 180 181static int wl1271_event_process(struct wl1271 *wl, struct event_mailbox *mbox) 182{ 183 int ret; 184 u32 vector; 185 bool beacon_loss = false; 186 187 wl1271_event_mbox_dump(mbox); 188 189 vector = le32_to_cpu(mbox->events_vector); 190 vector &= ~(le32_to_cpu(mbox->events_mask)); 191 wl1271_debug(DEBUG_EVENT, "vector: 0x%x", vector); 192 193 if (vector & SCAN_COMPLETE_EVENT_ID) { 194 wl1271_debug(DEBUG_EVENT, "status: 0x%x", 195 mbox->scheduled_scan_status); 196 197 wl1271_scan_stm(wl); 198 } 199 200 /* disable dynamic PS when requested by the firmware */ 201 if (vector & SOFT_GEMINI_SENSE_EVENT_ID && 202 wl->bss_type == BSS_TYPE_STA_BSS) { 203 if (mbox->soft_gemini_sense_info) 204 ieee80211_disable_dyn_ps(wl->vif); 205 else 206 ieee80211_enable_dyn_ps(wl->vif); 207 } 208 209 /* 210 * The BSS_LOSE_EVENT_ID is only needed while psm (and hence beacon 211 * filtering) is enabled. Without PSM, the stack will receive all 212 * beacons and can detect beacon loss by itself. 213 * 214 * As there's possibility that the driver disables PSM before receiving 215 * BSS_LOSE_EVENT, beacon loss has to be reported to the stack. 216 * 217 */ 218 if (vector & BSS_LOSE_EVENT_ID) { 219 wl1271_info("Beacon loss detected."); 220 221 /* indicate to the stack, that beacons have been lost */ 222 beacon_loss = true; 223 } 224 225 if (vector & PS_REPORT_EVENT_ID) { 226 wl1271_debug(DEBUG_EVENT, "PS_REPORT_EVENT"); 227 ret = wl1271_event_ps_report(wl, mbox, &beacon_loss); 228 if (ret < 0) 229 return ret; 230 } 231 232 if (vector & PSPOLL_DELIVERY_FAILURE_EVENT_ID) 233 wl1271_event_pspoll_delivery_fail(wl); 234 235 if (vector & RSSI_SNR_TRIGGER_0_EVENT_ID) { 236 wl1271_debug(DEBUG_EVENT, "RSSI_SNR_TRIGGER_0_EVENT"); 237 if (wl->vif) 238 wl1271_event_rssi_trigger(wl, mbox); 239 } 240 241 if (wl->vif && beacon_loss) 242 ieee80211_connection_loss(wl->vif); 243 244 return 0; 245} 246 247int wl1271_event_unmask(struct wl1271 *wl) 248{ 249 int ret; 250 251 ret = wl1271_acx_event_mbox_mask(wl, ~(wl->event_mask)); 252 if (ret < 0) 253 return ret; 254 255 return 0; 256} 257 258void wl1271_event_mbox_config(struct wl1271 *wl) 259{ 260 wl->mbox_ptr[0] = wl1271_read32(wl, REG_EVENT_MAILBOX_PTR); 261 wl->mbox_ptr[1] = wl->mbox_ptr[0] + sizeof(struct event_mailbox); 262 263 wl1271_debug(DEBUG_EVENT, "MBOX ptrs: 0x%x 0x%x", 264 wl->mbox_ptr[0], wl->mbox_ptr[1]); 265} 266 267int wl1271_event_handle(struct wl1271 *wl, u8 mbox_num) 268{ 269 struct event_mailbox mbox; 270 int ret; 271 272 wl1271_debug(DEBUG_EVENT, "EVENT on mbox %d", mbox_num); 273 274 if (mbox_num > 1) 275 return -EINVAL; 276 277 /* first we read the mbox descriptor */ 278 wl1271_read(wl, wl->mbox_ptr[mbox_num], &mbox, 279 sizeof(struct event_mailbox), false); 280 281 /* process the descriptor */ 282 ret = wl1271_event_process(wl, &mbox); 283 if (ret < 0) 284 return ret; 285 286 /* then we let the firmware know it can go on...*/ 287 wl1271_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_EVENT_ACK); 288 289 return 0; 290} 291