1 /****************************************************************************** 2 * 3 * Copyright(c) 2009-2012 Realtek Corporation. 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms of version 2 of the GNU General Public License as 7 * published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 * more details. 13 * 14 * The full GNU General Public License is included in this distribution in the 15 * file called LICENSE. 16 * 17 * Contact Information: 18 * wlanfae <wlanfae@realtek.com> 19 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park, 20 * Hsinchu 300, Taiwan. 21 * 22 * Larry Finger <Larry.Finger@lwfinger.net> 23 * 24 *****************************************************************************/ 25 26 #include "wifi.h" 27 #include "base.h" 28 #include "ps.h" 29 #include <linux/export.h> 30 #include "btcoexist/rtl_btc.h" 31 32 bool rtl_ps_enable_nic(struct ieee80211_hw *hw) 33 { 34 struct rtl_priv *rtlpriv = rtl_priv(hw); 35 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); 36 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw)); 37 38 /*<1> reset trx ring */ 39 if (rtlhal->interface == INTF_PCI) 40 rtlpriv->intf_ops->reset_trx_ring(hw); 41 42 if (is_hal_stop(rtlhal)) 43 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING, 44 "Driver is already down!\n"); 45 46 /*<2> Enable Adapter */ 47 if (rtlpriv->cfg->ops->hw_init(hw)) 48 return false; 49 RT_CLEAR_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC); 50 51 /*<3> Enable Interrupt */ 52 rtlpriv->cfg->ops->enable_interrupt(hw); 53 54 /*<enable timer> */ 55 rtl_watch_dog_timer_callback((unsigned long)hw); 56 57 return true; 58 } 59 EXPORT_SYMBOL(rtl_ps_enable_nic); 60 61 bool rtl_ps_disable_nic(struct ieee80211_hw *hw) 62 { 63 struct rtl_priv *rtlpriv = rtl_priv(hw); 64 65 /*<1> Stop all timer */ 66 rtl_deinit_deferred_work(hw); 67 68 /*<2> Disable Interrupt */ 69 rtlpriv->cfg->ops->disable_interrupt(hw); 70 tasklet_kill(&rtlpriv->works.irq_tasklet); 71 72 /*<3> Disable Adapter */ 73 rtlpriv->cfg->ops->hw_disable(hw); 74 75 return true; 76 } 77 EXPORT_SYMBOL(rtl_ps_disable_nic); 78 79 static bool rtl_ps_set_rf_state(struct ieee80211_hw *hw, 80 enum rf_pwrstate state_toset, 81 u32 changesource) 82 { 83 struct rtl_priv *rtlpriv = rtl_priv(hw); 84 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); 85 enum rf_pwrstate rtstate; 86 bool actionallowed = false; 87 u16 rfwait_cnt = 0; 88 89 /*Only one thread can change 90 *the RF state at one time, and others 91 *should wait to be executed. 92 */ 93 while (true) { 94 spin_lock(&rtlpriv->locks.rf_ps_lock); 95 if (ppsc->rfchange_inprogress) { 96 spin_unlock(&rtlpriv->locks.rf_ps_lock); 97 98 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING, 99 "RF Change in progress! Wait to set..state_toset(%d).\n", 100 state_toset); 101 102 /* Set RF after the previous action is done. */ 103 while (ppsc->rfchange_inprogress) { 104 rfwait_cnt++; 105 mdelay(1); 106 /*Wait too long, return false to avoid 107 *to be stuck here. 108 */ 109 if (rfwait_cnt > 100) 110 return false; 111 } 112 } else { 113 ppsc->rfchange_inprogress = true; 114 spin_unlock(&rtlpriv->locks.rf_ps_lock); 115 break; 116 } 117 } 118 119 rtstate = ppsc->rfpwr_state; 120 121 switch (state_toset) { 122 case ERFON: 123 ppsc->rfoff_reason &= (~changesource); 124 125 if ((changesource == RF_CHANGE_BY_HW) && 126 (ppsc->hwradiooff)) { 127 ppsc->hwradiooff = false; 128 } 129 130 if (!ppsc->rfoff_reason) { 131 ppsc->rfoff_reason = 0; 132 actionallowed = true; 133 } 134 135 break; 136 137 case ERFOFF: 138 139 if ((changesource == RF_CHANGE_BY_HW) && !ppsc->hwradiooff) { 140 ppsc->hwradiooff = true; 141 } 142 143 ppsc->rfoff_reason |= changesource; 144 actionallowed = true; 145 break; 146 147 case ERFSLEEP: 148 ppsc->rfoff_reason |= changesource; 149 actionallowed = true; 150 break; 151 152 default: 153 pr_err("switch case %#x not processed\n", state_toset); 154 break; 155 } 156 157 if (actionallowed) 158 rtlpriv->cfg->ops->set_rf_power_state(hw, state_toset); 159 160 spin_lock(&rtlpriv->locks.rf_ps_lock); 161 ppsc->rfchange_inprogress = false; 162 spin_unlock(&rtlpriv->locks.rf_ps_lock); 163 164 return actionallowed; 165 } 166 167 static void _rtl_ps_inactive_ps(struct ieee80211_hw *hw) 168 { 169 struct rtl_priv *rtlpriv = rtl_priv(hw); 170 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw)); 171 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); 172 173 ppsc->swrf_processing = true; 174 175 if (ppsc->inactive_pwrstate == ERFON && 176 rtlhal->interface == INTF_PCI) { 177 if ((ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM) && 178 RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM) && 179 rtlhal->interface == INTF_PCI) { 180 rtlpriv->intf_ops->disable_aspm(hw); 181 RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM); 182 } 183 } 184 185 rtl_ps_set_rf_state(hw, ppsc->inactive_pwrstate, 186 RF_CHANGE_BY_IPS); 187 188 if (ppsc->inactive_pwrstate == ERFOFF && 189 rtlhal->interface == INTF_PCI) { 190 if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM && 191 !RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) { 192 rtlpriv->intf_ops->enable_aspm(hw); 193 RT_SET_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM); 194 } 195 } 196 197 ppsc->swrf_processing = false; 198 } 199 200 void rtl_ips_nic_off_wq_callback(void *data) 201 { 202 struct rtl_works *rtlworks = 203 container_of_dwork_rtl(data, struct rtl_works, ips_nic_off_wq); 204 struct ieee80211_hw *hw = rtlworks->hw; 205 struct rtl_priv *rtlpriv = rtl_priv(hw); 206 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw)); 207 struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); 208 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); 209 enum rf_pwrstate rtstate; 210 211 if (mac->opmode != NL80211_IFTYPE_STATION) { 212 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING, 213 "not station return\n"); 214 return; 215 } 216 217 if (mac->p2p_in_use) 218 return; 219 220 if (mac->link_state > MAC80211_NOLINK) 221 return; 222 223 if (is_hal_stop(rtlhal)) 224 return; 225 226 if (rtlpriv->sec.being_setkey) 227 return; 228 229 if (rtlpriv->cfg->ops->bt_coex_off_before_lps) 230 rtlpriv->cfg->ops->bt_coex_off_before_lps(hw); 231 232 if (ppsc->inactiveps) { 233 rtstate = ppsc->rfpwr_state; 234 235 /* 236 *Do not enter IPS in the following conditions: 237 *(1) RF is already OFF or Sleep 238 *(2) swrf_processing (indicates the IPS is still under going) 239 *(3) Connectted (only disconnected can trigger IPS) 240 *(4) IBSS (send Beacon) 241 *(5) AP mode (send Beacon) 242 *(6) monitor mode (rcv packet) 243 */ 244 245 if (rtstate == ERFON && 246 !ppsc->swrf_processing && 247 (mac->link_state == MAC80211_NOLINK) && 248 !mac->act_scanning) { 249 RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE, 250 "IPSEnter(): Turn off RF\n"); 251 252 ppsc->inactive_pwrstate = ERFOFF; 253 ppsc->in_powersavemode = true; 254 255 /* call before RF off */ 256 if (rtlpriv->cfg->ops->get_btc_status()) 257 rtlpriv->btcoexist.btc_ops->btc_ips_notify(rtlpriv, 258 ppsc->inactive_pwrstate); 259 260 /*rtl_pci_reset_trx_ring(hw); */ 261 _rtl_ps_inactive_ps(hw); 262 } 263 } 264 } 265 266 void rtl_ips_nic_off(struct ieee80211_hw *hw) 267 { 268 struct rtl_priv *rtlpriv = rtl_priv(hw); 269 270 /* because when link with ap, mac80211 will ask us 271 * to disable nic quickly after scan before linking, 272 * this will cause link failed, so we delay 100ms here 273 */ 274 queue_delayed_work(rtlpriv->works.rtl_wq, 275 &rtlpriv->works.ips_nic_off_wq, MSECS(100)); 276 } 277 278 /* NOTICE: any opmode should exc nic_on, or disable without 279 * nic_on may something wrong, like adhoc TP 280 */ 281 void rtl_ips_nic_on(struct ieee80211_hw *hw) 282 { 283 struct rtl_priv *rtlpriv = rtl_priv(hw); 284 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); 285 enum rf_pwrstate rtstate; 286 287 cancel_delayed_work(&rtlpriv->works.ips_nic_off_wq); 288 289 spin_lock(&rtlpriv->locks.ips_lock); 290 if (ppsc->inactiveps) { 291 rtstate = ppsc->rfpwr_state; 292 293 if (rtstate != ERFON && 294 !ppsc->swrf_processing && 295 ppsc->rfoff_reason <= RF_CHANGE_BY_IPS) { 296 297 ppsc->inactive_pwrstate = ERFON; 298 ppsc->in_powersavemode = false; 299 _rtl_ps_inactive_ps(hw); 300 /* call after RF on */ 301 if (rtlpriv->cfg->ops->get_btc_status()) 302 rtlpriv->btcoexist.btc_ops->btc_ips_notify(rtlpriv, 303 ppsc->inactive_pwrstate); 304 } 305 } 306 spin_unlock(&rtlpriv->locks.ips_lock); 307 } 308 EXPORT_SYMBOL_GPL(rtl_ips_nic_on); 309 310 /*for FW LPS*/ 311 312 /* 313 *Determine if we can set Fw into PS mode 314 *in current condition.Return TRUE if it 315 *can enter PS mode. 316 */ 317 static bool rtl_get_fwlps_doze(struct ieee80211_hw *hw) 318 { 319 struct rtl_priv *rtlpriv = rtl_priv(hw); 320 struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); 321 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); 322 u32 ps_timediff; 323 324 ps_timediff = jiffies_to_msecs(jiffies - 325 ppsc->last_delaylps_stamp_jiffies); 326 327 if (ps_timediff < 2000) { 328 RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD, 329 "Delay enter Fw LPS for DHCP, ARP, or EAPOL exchanging state\n"); 330 return false; 331 } 332 333 if (mac->link_state != MAC80211_LINKED) 334 return false; 335 336 if (mac->opmode == NL80211_IFTYPE_ADHOC) 337 return false; 338 339 return true; 340 } 341 342 /* Change current and default preamble mode.*/ 343 void rtl_lps_set_psmode(struct ieee80211_hw *hw, u8 rt_psmode) 344 { 345 struct rtl_priv *rtlpriv = rtl_priv(hw); 346 struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); 347 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); 348 bool enter_fwlps; 349 350 if (mac->opmode == NL80211_IFTYPE_ADHOC) 351 return; 352 353 if (mac->link_state != MAC80211_LINKED) 354 return; 355 356 if (ppsc->dot11_psmode == rt_psmode) 357 return; 358 359 /* Update power save mode configured. */ 360 ppsc->dot11_psmode = rt_psmode; 361 362 /* 363 *<FW control LPS> 364 *1. Enter PS mode 365 * Set RPWM to Fw to turn RF off and send H2C fw_pwrmode 366 * cmd to set Fw into PS mode. 367 *2. Leave PS mode 368 * Send H2C fw_pwrmode cmd to Fw to set Fw into Active 369 * mode and set RPWM to turn RF on. 370 */ 371 372 if ((ppsc->fwctrl_lps) && ppsc->report_linked) { 373 if (ppsc->dot11_psmode == EACTIVE) { 374 RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG, 375 "FW LPS leave ps_mode:%x\n", 376 FW_PS_ACTIVE_MODE); 377 enter_fwlps = false; 378 ppsc->pwr_mode = FW_PS_ACTIVE_MODE; 379 ppsc->smart_ps = 0; 380 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_FW_LPS_ACTION, 381 (u8 *)(&enter_fwlps)); 382 if (ppsc->p2p_ps_info.opp_ps) 383 rtl_p2p_ps_cmd(hw , P2P_PS_ENABLE); 384 385 if (rtlpriv->cfg->ops->get_btc_status()) 386 rtlpriv->btcoexist.btc_ops->btc_lps_notify(rtlpriv, rt_psmode); 387 } else { 388 if (rtl_get_fwlps_doze(hw)) { 389 RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG, 390 "FW LPS enter ps_mode:%x\n", 391 ppsc->fwctrl_psmode); 392 if (rtlpriv->cfg->ops->get_btc_status()) 393 rtlpriv->btcoexist.btc_ops->btc_lps_notify(rtlpriv, rt_psmode); 394 enter_fwlps = true; 395 ppsc->pwr_mode = ppsc->fwctrl_psmode; 396 ppsc->smart_ps = 2; 397 rtlpriv->cfg->ops->set_hw_reg(hw, 398 HW_VAR_FW_LPS_ACTION, 399 (u8 *)(&enter_fwlps)); 400 401 } else { 402 /* Reset the power save related parameters. */ 403 ppsc->dot11_psmode = EACTIVE; 404 } 405 } 406 } 407 } 408 409 /* Interrupt safe routine to enter the leisure power save mode.*/ 410 static void rtl_lps_enter_core(struct ieee80211_hw *hw) 411 { 412 struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); 413 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); 414 struct rtl_priv *rtlpriv = rtl_priv(hw); 415 unsigned long flag; 416 417 if (!ppsc->fwctrl_lps) 418 return; 419 420 if (rtlpriv->sec.being_setkey) 421 return; 422 423 if (rtlpriv->link_info.busytraffic) 424 return; 425 426 /*sleep after linked 10s, to let DHCP and 4-way handshake ok enough!! */ 427 if (mac->cnt_after_linked < 5) 428 return; 429 430 if (mac->opmode == NL80211_IFTYPE_ADHOC) 431 return; 432 433 if (mac->link_state != MAC80211_LINKED) 434 return; 435 436 spin_lock_irqsave(&rtlpriv->locks.lps_lock, flag); 437 438 if (ppsc->dot11_psmode == EACTIVE) { 439 RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD, 440 "Enter 802.11 power save mode...\n"); 441 rtl_lps_set_psmode(hw, EAUTOPS); 442 } 443 444 spin_unlock_irqrestore(&rtlpriv->locks.lps_lock, flag); 445 } 446 447 /* Interrupt safe routine to leave the leisure power save mode.*/ 448 static void rtl_lps_leave_core(struct ieee80211_hw *hw) 449 { 450 struct rtl_priv *rtlpriv = rtl_priv(hw); 451 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); 452 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw)); 453 unsigned long flag; 454 455 spin_lock_irqsave(&rtlpriv->locks.lps_lock, flag); 456 457 if (ppsc->fwctrl_lps) { 458 if (ppsc->dot11_psmode != EACTIVE) { 459 460 /*FIX ME */ 461 /*rtlpriv->cfg->ops->enable_interrupt(hw); */ 462 463 if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM && 464 RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM) && 465 rtlhal->interface == INTF_PCI) { 466 rtlpriv->intf_ops->disable_aspm(hw); 467 RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM); 468 } 469 470 RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD, 471 "Busy Traffic,Leave 802.11 power save..\n"); 472 473 rtl_lps_set_psmode(hw, EACTIVE); 474 } 475 } 476 spin_unlock_irqrestore(&rtlpriv->locks.lps_lock, flag); 477 } 478 479 /* For sw LPS*/ 480 void rtl_swlps_beacon(struct ieee80211_hw *hw, void *data, unsigned int len) 481 { 482 struct rtl_priv *rtlpriv = rtl_priv(hw); 483 struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); 484 struct ieee80211_hdr *hdr = data; 485 struct ieee80211_tim_ie *tim_ie; 486 u8 *tim; 487 u8 tim_len; 488 bool u_buffed; 489 bool m_buffed; 490 491 if (mac->opmode != NL80211_IFTYPE_STATION) 492 return; 493 494 if (!rtlpriv->psc.swctrl_lps) 495 return; 496 497 if (rtlpriv->mac80211.link_state != MAC80211_LINKED) 498 return; 499 500 if (!rtlpriv->psc.sw_ps_enabled) 501 return; 502 503 if (rtlpriv->psc.fwctrl_lps) 504 return; 505 506 if (likely(!(hw->conf.flags & IEEE80211_CONF_PS))) 507 return; 508 509 /* check if this really is a beacon */ 510 if (!ieee80211_is_beacon(hdr->frame_control)) 511 return; 512 513 /* min. beacon length + FCS_LEN */ 514 if (len <= 40 + FCS_LEN) 515 return; 516 517 /* and only beacons from the associated BSSID, please */ 518 if (!ether_addr_equal_64bits(hdr->addr3, rtlpriv->mac80211.bssid)) 519 return; 520 521 rtlpriv->psc.last_beacon = jiffies; 522 523 tim = rtl_find_ie(data, len - FCS_LEN, WLAN_EID_TIM); 524 if (!tim) 525 return; 526 527 if (tim[1] < sizeof(*tim_ie)) 528 return; 529 530 tim_len = tim[1]; 531 tim_ie = (struct ieee80211_tim_ie *) &tim[2]; 532 533 if (!WARN_ON_ONCE(!hw->conf.ps_dtim_period)) 534 rtlpriv->psc.dtim_counter = tim_ie->dtim_count; 535 536 /* Check whenever the PHY can be turned off again. */ 537 538 /* 1. What about buffered unicast traffic for our AID? */ 539 u_buffed = ieee80211_check_tim(tim_ie, tim_len, 540 rtlpriv->mac80211.assoc_id); 541 542 /* 2. Maybe the AP wants to send multicast/broadcast data? */ 543 m_buffed = tim_ie->bitmap_ctrl & 0x01; 544 rtlpriv->psc.multi_buffered = m_buffed; 545 546 /* unicast will process by mac80211 through 547 * set ~IEEE80211_CONF_PS, So we just check 548 * multicast frames here */ 549 if (!m_buffed) { 550 /* back to low-power land. and delay is 551 * prevent null power save frame tx fail */ 552 queue_delayed_work(rtlpriv->works.rtl_wq, 553 &rtlpriv->works.ps_work, MSECS(5)); 554 } else { 555 RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG, 556 "u_bufferd: %x, m_buffered: %x\n", u_buffed, m_buffed); 557 } 558 } 559 EXPORT_SYMBOL_GPL(rtl_swlps_beacon); 560 561 void rtl_swlps_rf_awake(struct ieee80211_hw *hw) 562 { 563 struct rtl_priv *rtlpriv = rtl_priv(hw); 564 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); 565 struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); 566 unsigned long flag; 567 568 if (!rtlpriv->psc.swctrl_lps) 569 return; 570 if (mac->link_state != MAC80211_LINKED) 571 return; 572 573 if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM && 574 RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) { 575 rtlpriv->intf_ops->disable_aspm(hw); 576 RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM); 577 } 578 579 spin_lock_irqsave(&rtlpriv->locks.lps_lock, flag); 580 rtl_ps_set_rf_state(hw, ERFON, RF_CHANGE_BY_PS); 581 spin_unlock_irqrestore(&rtlpriv->locks.lps_lock, flag); 582 } 583 584 void rtl_swlps_rfon_wq_callback(void *data) 585 { 586 struct rtl_works *rtlworks = 587 container_of_dwork_rtl(data, struct rtl_works, ps_rfon_wq); 588 struct ieee80211_hw *hw = rtlworks->hw; 589 590 rtl_swlps_rf_awake(hw); 591 } 592 593 void rtl_swlps_rf_sleep(struct ieee80211_hw *hw) 594 { 595 struct rtl_priv *rtlpriv = rtl_priv(hw); 596 struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); 597 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw)); 598 unsigned long flag; 599 u8 sleep_intv; 600 601 if (!rtlpriv->psc.sw_ps_enabled) 602 return; 603 604 if ((rtlpriv->sec.being_setkey) || 605 (mac->opmode == NL80211_IFTYPE_ADHOC)) 606 return; 607 608 /*sleep after linked 10s, to let DHCP and 4-way handshake ok enough!! */ 609 if ((mac->link_state != MAC80211_LINKED) || (mac->cnt_after_linked < 5)) 610 return; 611 612 if (rtlpriv->link_info.busytraffic) 613 return; 614 615 spin_lock(&rtlpriv->locks.rf_ps_lock); 616 if (rtlpriv->psc.rfchange_inprogress) { 617 spin_unlock(&rtlpriv->locks.rf_ps_lock); 618 return; 619 } 620 spin_unlock(&rtlpriv->locks.rf_ps_lock); 621 622 spin_lock_irqsave(&rtlpriv->locks.lps_lock, flag); 623 rtl_ps_set_rf_state(hw, ERFSLEEP, RF_CHANGE_BY_PS); 624 spin_unlock_irqrestore(&rtlpriv->locks.lps_lock, flag); 625 626 if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM && 627 !RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) { 628 rtlpriv->intf_ops->enable_aspm(hw); 629 RT_SET_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM); 630 } 631 632 /* here is power save alg, when this beacon is DTIM 633 * we will set sleep time to dtim_period * n; 634 * when this beacon is not DTIM, we will set sleep 635 * time to sleep_intv = rtlpriv->psc.dtim_counter or 636 * MAX_SW_LPS_SLEEP_INTV(default set to 5) */ 637 638 if (rtlpriv->psc.dtim_counter == 0) { 639 if (hw->conf.ps_dtim_period == 1) 640 sleep_intv = hw->conf.ps_dtim_period * 2; 641 else 642 sleep_intv = hw->conf.ps_dtim_period; 643 } else { 644 sleep_intv = rtlpriv->psc.dtim_counter; 645 } 646 647 if (sleep_intv > MAX_SW_LPS_SLEEP_INTV) 648 sleep_intv = MAX_SW_LPS_SLEEP_INTV; 649 650 /* this print should always be dtim_conter = 0 & 651 * sleep = dtim_period, that meaons, we should 652 * awake before every dtim */ 653 RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG, 654 "dtim_counter:%x will sleep :%d beacon_intv\n", 655 rtlpriv->psc.dtim_counter, sleep_intv); 656 657 /* we tested that 40ms is enough for sw & hw sw delay */ 658 queue_delayed_work(rtlpriv->works.rtl_wq, &rtlpriv->works.ps_rfon_wq, 659 MSECS(sleep_intv * mac->vif->bss_conf.beacon_int - 40)); 660 } 661 662 void rtl_lps_change_work_callback(struct work_struct *work) 663 { 664 struct rtl_works *rtlworks = 665 container_of(work, struct rtl_works, lps_change_work); 666 struct ieee80211_hw *hw = rtlworks->hw; 667 struct rtl_priv *rtlpriv = rtl_priv(hw); 668 669 if (rtlpriv->enter_ps) 670 rtl_lps_enter_core(hw); 671 else 672 rtl_lps_leave_core(hw); 673 } 674 EXPORT_SYMBOL_GPL(rtl_lps_change_work_callback); 675 676 void rtl_lps_enter(struct ieee80211_hw *hw) 677 { 678 struct rtl_priv *rtlpriv = rtl_priv(hw); 679 680 if (!in_interrupt()) 681 return rtl_lps_enter_core(hw); 682 rtlpriv->enter_ps = true; 683 schedule_work(&rtlpriv->works.lps_change_work); 684 } 685 EXPORT_SYMBOL_GPL(rtl_lps_enter); 686 687 void rtl_lps_leave(struct ieee80211_hw *hw) 688 { 689 struct rtl_priv *rtlpriv = rtl_priv(hw); 690 691 if (!in_interrupt()) 692 return rtl_lps_leave_core(hw); 693 rtlpriv->enter_ps = false; 694 schedule_work(&rtlpriv->works.lps_change_work); 695 } 696 EXPORT_SYMBOL_GPL(rtl_lps_leave); 697 698 void rtl_swlps_wq_callback(void *data) 699 { 700 struct rtl_works *rtlworks = container_of_dwork_rtl(data, 701 struct rtl_works, 702 ps_work); 703 struct ieee80211_hw *hw = rtlworks->hw; 704 struct rtl_priv *rtlpriv = rtl_priv(hw); 705 bool ps = false; 706 707 ps = (hw->conf.flags & IEEE80211_CONF_PS); 708 709 /* we can sleep after ps null send ok */ 710 if (rtlpriv->psc.state_inap) { 711 rtl_swlps_rf_sleep(hw); 712 713 if (rtlpriv->psc.state && !ps) { 714 rtlpriv->psc.sleep_ms = jiffies_to_msecs(jiffies - 715 rtlpriv->psc.last_action); 716 } 717 718 if (ps) 719 rtlpriv->psc.last_slept = jiffies; 720 721 rtlpriv->psc.last_action = jiffies; 722 rtlpriv->psc.state = ps; 723 } 724 } 725 726 static void rtl_p2p_noa_ie(struct ieee80211_hw *hw, void *data, 727 unsigned int len) 728 { 729 struct rtl_priv *rtlpriv = rtl_priv(hw); 730 struct ieee80211_mgmt *mgmt = data; 731 struct rtl_p2p_ps_info *p2pinfo = &(rtlpriv->psc.p2p_ps_info); 732 u8 *pos, *end, *ie; 733 u16 noa_len; 734 static u8 p2p_oui_ie_type[4] = {0x50, 0x6f, 0x9a, 0x09}; 735 u8 noa_num, index , i, noa_index = 0; 736 bool find_p2p_ie = false , find_p2p_ps_ie = false; 737 pos = (u8 *)mgmt->u.beacon.variable; 738 end = data + len; 739 ie = NULL; 740 741 while (pos + 1 < end) { 742 if (pos + 2 + pos[1] > end) 743 return; 744 745 if (pos[0] == 221 && pos[1] > 4) { 746 if (memcmp(&pos[2], p2p_oui_ie_type, 4) == 0) { 747 ie = pos + 2+4; 748 break; 749 } 750 } 751 pos += 2 + pos[1]; 752 } 753 754 if (ie == NULL) 755 return; 756 find_p2p_ie = true; 757 /*to find noa ie*/ 758 while (ie + 1 < end) { 759 noa_len = READEF2BYTE((__le16 *)&ie[1]); 760 if (ie + 3 + ie[1] > end) 761 return; 762 763 if (ie[0] == 12) { 764 find_p2p_ps_ie = true; 765 if ((noa_len - 2) % 13 != 0) { 766 RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, 767 "P2P notice of absence: invalid length.%d\n", 768 noa_len); 769 return; 770 } else { 771 noa_num = (noa_len - 2) / 13; 772 } 773 noa_index = ie[3]; 774 if (rtlpriv->psc.p2p_ps_info.p2p_ps_mode == 775 P2P_PS_NONE || noa_index != p2pinfo->noa_index) { 776 RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, 777 "update NOA ie.\n"); 778 p2pinfo->noa_index = noa_index; 779 p2pinfo->opp_ps = (ie[4] >> 7); 780 p2pinfo->ctwindow = ie[4] & 0x7F; 781 p2pinfo->noa_num = noa_num; 782 index = 5; 783 for (i = 0; i < noa_num; i++) { 784 p2pinfo->noa_count_type[i] = 785 READEF1BYTE(ie+index); 786 index += 1; 787 p2pinfo->noa_duration[i] = 788 READEF4BYTE((__le32 *)ie+index); 789 index += 4; 790 p2pinfo->noa_interval[i] = 791 READEF4BYTE((__le32 *)ie+index); 792 index += 4; 793 p2pinfo->noa_start_time[i] = 794 READEF4BYTE((__le32 *)ie+index); 795 index += 4; 796 } 797 798 if (p2pinfo->opp_ps == 1) { 799 p2pinfo->p2p_ps_mode = P2P_PS_CTWINDOW; 800 /* Driver should wait LPS entering 801 * CTWindow 802 */ 803 if (rtlpriv->psc.fw_current_inpsmode) 804 rtl_p2p_ps_cmd(hw, 805 P2P_PS_ENABLE); 806 } else if (p2pinfo->noa_num > 0) { 807 p2pinfo->p2p_ps_mode = P2P_PS_NOA; 808 rtl_p2p_ps_cmd(hw, P2P_PS_ENABLE); 809 } else if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) { 810 rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE); 811 } 812 } 813 break; 814 } 815 ie += 3 + noa_len; 816 } 817 818 if (find_p2p_ie == true) { 819 if ((p2pinfo->p2p_ps_mode > P2P_PS_NONE) && 820 (find_p2p_ps_ie == false)) 821 rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE); 822 } 823 } 824 825 static void rtl_p2p_action_ie(struct ieee80211_hw *hw, void *data, 826 unsigned int len) 827 { 828 struct rtl_priv *rtlpriv = rtl_priv(hw); 829 struct ieee80211_mgmt *mgmt = data; 830 struct rtl_p2p_ps_info *p2pinfo = &(rtlpriv->psc.p2p_ps_info); 831 u8 noa_num, index , i , noa_index = 0; 832 u8 *pos, *end, *ie; 833 u16 noa_len; 834 static u8 p2p_oui_ie_type[4] = {0x50, 0x6f, 0x9a, 0x09}; 835 836 pos = (u8 *)&mgmt->u.action.category; 837 end = data + len; 838 ie = NULL; 839 840 if (pos[0] == 0x7f) { 841 if (memcmp(&pos[1], p2p_oui_ie_type, 4) == 0) 842 ie = pos + 3+4; 843 } 844 845 if (ie == NULL) 846 return; 847 848 RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, "action frame find P2P IE.\n"); 849 /*to find noa ie*/ 850 while (ie + 1 < end) { 851 noa_len = READEF2BYTE((__le16 *)&ie[1]); 852 if (ie + 3 + ie[1] > end) 853 return; 854 855 if (ie[0] == 12) { 856 RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, "find NOA IE.\n"); 857 RT_PRINT_DATA(rtlpriv, COMP_FW, DBG_LOUD, "noa ie ", 858 ie, noa_len); 859 if ((noa_len - 2) % 13 != 0) { 860 RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, 861 "P2P notice of absence: invalid length.%d\n", 862 noa_len); 863 return; 864 } else { 865 noa_num = (noa_len - 2) / 13; 866 } 867 noa_index = ie[3]; 868 if (rtlpriv->psc.p2p_ps_info.p2p_ps_mode == 869 P2P_PS_NONE || noa_index != p2pinfo->noa_index) { 870 p2pinfo->noa_index = noa_index; 871 p2pinfo->opp_ps = (ie[4] >> 7); 872 p2pinfo->ctwindow = ie[4] & 0x7F; 873 p2pinfo->noa_num = noa_num; 874 index = 5; 875 for (i = 0; i < noa_num; i++) { 876 p2pinfo->noa_count_type[i] = 877 READEF1BYTE(ie+index); 878 index += 1; 879 p2pinfo->noa_duration[i] = 880 READEF4BYTE((__le32 *)ie+index); 881 index += 4; 882 p2pinfo->noa_interval[i] = 883 READEF4BYTE((__le32 *)ie+index); 884 index += 4; 885 p2pinfo->noa_start_time[i] = 886 READEF4BYTE((__le32 *)ie+index); 887 index += 4; 888 } 889 890 if (p2pinfo->opp_ps == 1) { 891 p2pinfo->p2p_ps_mode = P2P_PS_CTWINDOW; 892 /* Driver should wait LPS entering 893 * CTWindow 894 */ 895 if (rtlpriv->psc.fw_current_inpsmode) 896 rtl_p2p_ps_cmd(hw, 897 P2P_PS_ENABLE); 898 } else if (p2pinfo->noa_num > 0) { 899 p2pinfo->p2p_ps_mode = P2P_PS_NOA; 900 rtl_p2p_ps_cmd(hw, P2P_PS_ENABLE); 901 } else if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) { 902 rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE); 903 } 904 } 905 break; 906 } 907 ie += 3 + noa_len; 908 } 909 } 910 911 void rtl_p2p_ps_cmd(struct ieee80211_hw *hw , u8 p2p_ps_state) 912 { 913 struct rtl_priv *rtlpriv = rtl_priv(hw); 914 struct rtl_ps_ctl *rtlps = rtl_psc(rtl_priv(hw)); 915 struct rtl_p2p_ps_info *p2pinfo = &(rtlpriv->psc.p2p_ps_info); 916 917 RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, " p2p state %x\n" , p2p_ps_state); 918 switch (p2p_ps_state) { 919 case P2P_PS_DISABLE: 920 p2pinfo->p2p_ps_state = p2p_ps_state; 921 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_H2C_FW_P2P_PS_OFFLOAD, 922 &p2p_ps_state); 923 p2pinfo->noa_index = 0; 924 p2pinfo->ctwindow = 0; 925 p2pinfo->opp_ps = 0; 926 p2pinfo->noa_num = 0; 927 p2pinfo->p2p_ps_mode = P2P_PS_NONE; 928 if (rtlps->fw_current_inpsmode) { 929 if (rtlps->smart_ps == 0) { 930 rtlps->smart_ps = 2; 931 rtlpriv->cfg->ops->set_hw_reg(hw, 932 HW_VAR_H2C_FW_PWRMODE, 933 &rtlps->pwr_mode); 934 } 935 936 } 937 break; 938 case P2P_PS_ENABLE: 939 if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) { 940 p2pinfo->p2p_ps_state = p2p_ps_state; 941 942 if (p2pinfo->ctwindow > 0) { 943 if (rtlps->smart_ps != 0) { 944 rtlps->smart_ps = 0; 945 rtlpriv->cfg->ops->set_hw_reg(hw, 946 HW_VAR_H2C_FW_PWRMODE, 947 &rtlps->pwr_mode); 948 } 949 } 950 rtlpriv->cfg->ops->set_hw_reg(hw, 951 HW_VAR_H2C_FW_P2P_PS_OFFLOAD, 952 &p2p_ps_state); 953 954 } 955 break; 956 case P2P_PS_SCAN: 957 case P2P_PS_SCAN_DONE: 958 case P2P_PS_ALLSTASLEEP: 959 if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) { 960 p2pinfo->p2p_ps_state = p2p_ps_state; 961 rtlpriv->cfg->ops->set_hw_reg(hw, 962 HW_VAR_H2C_FW_P2P_PS_OFFLOAD, 963 &p2p_ps_state); 964 } 965 break; 966 default: 967 break; 968 } 969 RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, 970 "ctwindow %x oppps %x\n", 971 p2pinfo->ctwindow , p2pinfo->opp_ps); 972 RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, 973 "count %x duration %x index %x interval %x start time %x noa num %x\n", 974 p2pinfo->noa_count_type[0], 975 p2pinfo->noa_duration[0], 976 p2pinfo->noa_index, 977 p2pinfo->noa_interval[0], 978 p2pinfo->noa_start_time[0], 979 p2pinfo->noa_num); 980 RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, "end\n"); 981 } 982 983 void rtl_p2p_info(struct ieee80211_hw *hw, void *data, unsigned int len) 984 { 985 struct rtl_priv *rtlpriv = rtl_priv(hw); 986 struct rtl_mac *mac = rtl_mac(rtl_priv(hw)); 987 struct ieee80211_hdr *hdr = data; 988 989 if (!mac->p2p) 990 return; 991 if (mac->link_state != MAC80211_LINKED) 992 return; 993 /* min. beacon length + FCS_LEN */ 994 if (len <= 40 + FCS_LEN) 995 return; 996 997 /* and only beacons from the associated BSSID, please */ 998 if (!ether_addr_equal_64bits(hdr->addr3, rtlpriv->mac80211.bssid)) 999 return; 1000 1001 /* check if this really is a beacon */ 1002 if (!(ieee80211_is_beacon(hdr->frame_control) || 1003 ieee80211_is_probe_resp(hdr->frame_control) || 1004 ieee80211_is_action(hdr->frame_control))) 1005 return; 1006 1007 if (ieee80211_is_action(hdr->frame_control)) 1008 rtl_p2p_action_ie(hw , data , len - FCS_LEN); 1009 else 1010 rtl_p2p_noa_ie(hw , data , len - FCS_LEN); 1011 } 1012 EXPORT_SYMBOL_GPL(rtl_p2p_info); 1013