linux/drivers/staging/rtl8723bs/core/rtw_pwrctrl.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/******************************************************************************
   3 *
   4 * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
   5 *
   6 ******************************************************************************/
   7#include <drv_types.h>
   8#include <rtw_debug.h>
   9#include <hal_data.h>
  10#include <linux/jiffies.h>
  11
  12
  13void _ips_enter(struct adapter *padapter)
  14{
  15        struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
  16
  17        pwrpriv->bips_processing = true;
  18
  19        /*  syn ips_mode with request */
  20        pwrpriv->ips_mode = pwrpriv->ips_mode_req;
  21
  22        pwrpriv->ips_enter_cnts++;
  23
  24        if (rf_off == pwrpriv->change_rfpwrstate) {
  25                pwrpriv->bpower_saving = true;
  26
  27                if (pwrpriv->ips_mode == IPS_LEVEL_2)
  28                        pwrpriv->bkeepfwalive = true;
  29
  30                rtw_ips_pwr_down(padapter);
  31                pwrpriv->rf_pwrstate = rf_off;
  32        }
  33        pwrpriv->bips_processing = false;
  34
  35}
  36
  37void ips_enter(struct adapter *padapter)
  38{
  39        struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
  40
  41
  42        hal_btcoex_IpsNotify(padapter, pwrpriv->ips_mode_req);
  43
  44        mutex_lock(&pwrpriv->lock);
  45        _ips_enter(padapter);
  46        mutex_unlock(&pwrpriv->lock);
  47}
  48
  49int _ips_leave(struct adapter *padapter)
  50{
  51        struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
  52        int result = _SUCCESS;
  53
  54        if ((pwrpriv->rf_pwrstate == rf_off) && (!pwrpriv->bips_processing)) {
  55                pwrpriv->bips_processing = true;
  56                pwrpriv->change_rfpwrstate = rf_on;
  57                pwrpriv->ips_leave_cnts++;
  58
  59                result = rtw_ips_pwr_up(padapter);
  60                if (result == _SUCCESS) {
  61                        pwrpriv->rf_pwrstate = rf_on;
  62                }
  63                pwrpriv->bips_processing = false;
  64
  65                pwrpriv->bkeepfwalive = false;
  66                pwrpriv->bpower_saving = false;
  67        }
  68
  69        return result;
  70}
  71
  72int ips_leave(struct adapter *padapter)
  73{
  74        struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
  75        int ret;
  76
  77        if (!is_primary_adapter(padapter))
  78                return _SUCCESS;
  79
  80        mutex_lock(&pwrpriv->lock);
  81        ret = _ips_leave(padapter);
  82        mutex_unlock(&pwrpriv->lock);
  83
  84        if (ret == _SUCCESS)
  85                hal_btcoex_IpsNotify(padapter, IPS_NONE);
  86
  87        return ret;
  88}
  89
  90static bool rtw_pwr_unassociated_idle(struct adapter *adapter)
  91{
  92        struct adapter *buddy = adapter->pbuddy_adapter;
  93        struct mlme_priv *pmlmepriv = &(adapter->mlmepriv);
  94        struct xmit_priv *pxmit_priv = &adapter->xmitpriv;
  95
  96        bool ret = false;
  97
  98        if (adapter_to_pwrctl(adapter)->bpower_saving)
  99                goto exit;
 100
 101        if (time_before(jiffies, adapter_to_pwrctl(adapter)->ips_deny_time))
 102                goto exit;
 103
 104        if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE|WIFI_SITE_MONITOR)
 105                || check_fwstate(pmlmepriv, WIFI_UNDER_LINKING|WIFI_UNDER_WPS)
 106                || check_fwstate(pmlmepriv, WIFI_AP_STATE)
 107                || check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE|WIFI_ADHOC_STATE)
 108        )
 109                goto exit;
 110
 111        /* consider buddy, if exist */
 112        if (buddy) {
 113                struct mlme_priv *b_pmlmepriv = &(buddy->mlmepriv);
 114
 115                if (check_fwstate(b_pmlmepriv, WIFI_ASOC_STATE|WIFI_SITE_MONITOR)
 116                        || check_fwstate(b_pmlmepriv, WIFI_UNDER_LINKING|WIFI_UNDER_WPS)
 117                        || check_fwstate(b_pmlmepriv, WIFI_AP_STATE)
 118                        || check_fwstate(b_pmlmepriv, WIFI_ADHOC_MASTER_STATE|WIFI_ADHOC_STATE)
 119                )
 120                        goto exit;
 121        }
 122
 123        if (pxmit_priv->free_xmitbuf_cnt != NR_XMITBUFF ||
 124                pxmit_priv->free_xmit_extbuf_cnt != NR_XMIT_EXTBUFF) {
 125                netdev_dbg(adapter->pnetdev,
 126                           "There are some pkts to transmit\n");
 127                netdev_dbg(adapter->pnetdev,
 128                           "free_xmitbuf_cnt: %d, free_xmit_extbuf_cnt: %d\n",
 129                           pxmit_priv->free_xmitbuf_cnt,
 130                           pxmit_priv->free_xmit_extbuf_cnt);
 131                goto exit;
 132        }
 133
 134        ret = true;
 135
 136exit:
 137        return ret;
 138}
 139
 140
 141/*
 142 * ATTENTION:
 143 *rtw_ps_processor() doesn't handle LPS.
 144 */
 145void rtw_ps_processor(struct adapter *padapter)
 146{
 147        struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
 148        struct dvobj_priv *psdpriv = padapter->dvobj;
 149        struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
 150        u32 ps_deny = 0;
 151
 152        mutex_lock(&adapter_to_pwrctl(padapter)->lock);
 153        ps_deny = rtw_ps_deny_get(padapter);
 154        mutex_unlock(&adapter_to_pwrctl(padapter)->lock);
 155        if (ps_deny != 0)
 156                goto exit;
 157
 158        if (pwrpriv->bInSuspend) {/* system suspend or autosuspend */
 159                pdbgpriv->dbg_ps_insuspend_cnt++;
 160                return;
 161        }
 162
 163        pwrpriv->ps_processing = true;
 164
 165        if (pwrpriv->ips_mode_req == IPS_NONE)
 166                goto exit;
 167
 168        if (!rtw_pwr_unassociated_idle(padapter))
 169                goto exit;
 170
 171        if ((pwrpriv->rf_pwrstate == rf_on) && ((pwrpriv->pwr_state_check_cnts%4) == 0)) {
 172                pwrpriv->change_rfpwrstate = rf_off;
 173                {
 174                        ips_enter(padapter);
 175                }
 176        }
 177exit:
 178        pwrpriv->ps_processing = false;
 179}
 180
 181static void pwr_state_check_handler(struct timer_list *t)
 182{
 183        struct pwrctrl_priv *pwrctrlpriv =
 184                from_timer(pwrctrlpriv, t, pwr_state_check_timer);
 185        struct adapter *padapter = pwrctrlpriv->adapter;
 186
 187        rtw_ps_cmd(padapter);
 188}
 189
 190void traffic_check_for_leave_lps(struct adapter *padapter, u8 tx, u32 tx_packets)
 191{
 192        static unsigned long start_time;
 193        static u32 xmit_cnt;
 194        u8 bLeaveLPS = false;
 195        struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
 196
 197
 198
 199        if (tx) { /* from tx */
 200                xmit_cnt += tx_packets;
 201
 202                if (start_time == 0)
 203                        start_time = jiffies;
 204
 205                if (jiffies_to_msecs(jiffies - start_time) > 2000) { /*  2 sec == watch dog timer */
 206                        if (xmit_cnt > 8) {
 207                                if (adapter_to_pwrctl(padapter)->bLeisurePs
 208                                    && (adapter_to_pwrctl(padapter)->pwr_mode != PS_MODE_ACTIVE)
 209                                    && !(hal_btcoex_IsBtControlLps(padapter))) {
 210                                        bLeaveLPS = true;
 211                                }
 212                        }
 213
 214                        start_time = jiffies;
 215                        xmit_cnt = 0;
 216                }
 217
 218        } else { /*  from rx path */
 219                if (pmlmepriv->LinkDetectInfo.NumRxUnicastOkInPeriod > 4/*2*/) {
 220                        if (adapter_to_pwrctl(padapter)->bLeisurePs
 221                            && (adapter_to_pwrctl(padapter)->pwr_mode != PS_MODE_ACTIVE)
 222                            && !(hal_btcoex_IsBtControlLps(padapter)))
 223                                bLeaveLPS = true;
 224                }
 225        }
 226
 227        if (bLeaveLPS)
 228                /* rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_LEAVE, 1); */
 229                rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_LEAVE, tx?0:1);
 230}
 231
 232/*
 233 * Description:
 234 *This function MUST be called under power lock protect
 235 *
 236 * Parameters
 237 *padapter
 238 *pslv                  power state level, only could be PS_STATE_S0 ~ PS_STATE_S4
 239 *
 240 */
 241void rtw_set_rpwm(struct adapter *padapter, u8 pslv)
 242{
 243        u8 rpwm;
 244        struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
 245        u8 cpwm_orig;
 246
 247        pslv = PS_STATE(pslv);
 248
 249        if (!pwrpriv->brpwmtimeout) {
 250                if (pwrpriv->rpwm == pslv ||
 251                    (pwrpriv->rpwm >= PS_STATE_S2 && pslv >= PS_STATE_S2))
 252                        return;
 253
 254        }
 255
 256        if ((padapter->bSurpriseRemoved) || !(padapter->hw_init_completed)) {
 257                pwrpriv->cpwm = PS_STATE_S4;
 258
 259                return;
 260        }
 261
 262        if (padapter->bDriverStopped) {
 263                if (pslv < PS_STATE_S2)
 264                        return;
 265        }
 266
 267        rpwm = pslv | pwrpriv->tog;
 268        /*  only when from PS_STATE S0/S1 to S2 and higher needs ACK */
 269        if ((pwrpriv->cpwm < PS_STATE_S2) && (pslv >= PS_STATE_S2))
 270                rpwm |= PS_ACK;
 271
 272        pwrpriv->rpwm = pslv;
 273
 274        cpwm_orig = 0;
 275        if (rpwm & PS_ACK)
 276                rtw_hal_get_hwreg(padapter, HW_VAR_CPWM, &cpwm_orig);
 277
 278        if (rpwm & PS_ACK)
 279                _set_timer(&pwrpriv->pwr_rpwm_timer, LPS_RPWM_WAIT_MS);
 280        rtw_hal_set_hwreg(padapter, HW_VAR_SET_RPWM, (u8 *)(&rpwm));
 281
 282        pwrpriv->tog += 0x80;
 283
 284        /*  No LPS 32K, No Ack */
 285        if (rpwm & PS_ACK) {
 286                unsigned long start_time;
 287                u8 cpwm_now;
 288                u8 poll_cnt = 0;
 289
 290                start_time = jiffies;
 291
 292                /*  polling cpwm */
 293                do {
 294                        mdelay(1);
 295                        poll_cnt++;
 296                        rtw_hal_get_hwreg(padapter, HW_VAR_CPWM, &cpwm_now);
 297                        if ((cpwm_orig ^ cpwm_now) & 0x80) {
 298                                pwrpriv->cpwm = PS_STATE_S4;
 299                                pwrpriv->cpwm_tog = cpwm_now & PS_TOGGLE;
 300                                break;
 301                        }
 302
 303                        if (jiffies_to_msecs(jiffies - start_time) > LPS_RPWM_WAIT_MS) {
 304                                _set_timer(&pwrpriv->pwr_rpwm_timer, 1);
 305                                break;
 306                        }
 307                } while (1);
 308        } else
 309                pwrpriv->cpwm = pslv;
 310}
 311
 312static u8 PS_RDY_CHECK(struct adapter *padapter)
 313{
 314        unsigned long curr_time, delta_time;
 315        struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
 316        struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
 317
 318        if (pwrpriv->bInSuspend)
 319                return false;
 320
 321        curr_time = jiffies;
 322
 323        delta_time = curr_time - pwrpriv->DelayLPSLastTimeStamp;
 324
 325        if (delta_time < LPS_DELAY_TIME)
 326                return false;
 327
 328        if (check_fwstate(pmlmepriv, WIFI_SITE_MONITOR)
 329                || check_fwstate(pmlmepriv, WIFI_UNDER_LINKING|WIFI_UNDER_WPS)
 330                || check_fwstate(pmlmepriv, WIFI_AP_STATE)
 331                || check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE|WIFI_ADHOC_STATE)
 332                || rtw_is_scan_deny(padapter)
 333        )
 334                return false;
 335
 336        if (padapter->securitypriv.dot11AuthAlgrthm == dot11AuthAlgrthm_8021X &&
 337            !padapter->securitypriv.binstallGrpkey)
 338                return false;
 339
 340        if (!rtw_cfg80211_pwr_mgmt(padapter))
 341                return false;
 342
 343        return true;
 344}
 345
 346void rtw_set_ps_mode(struct adapter *padapter, u8 ps_mode, u8 smart_ps, u8 bcn_ant_mode, const char *msg)
 347{
 348        struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
 349
 350        if (ps_mode > PM_Card_Disable)
 351                return;
 352
 353        if (pwrpriv->pwr_mode == ps_mode)
 354                if (ps_mode == PS_MODE_ACTIVE)
 355                        return;
 356
 357
 358        mutex_lock(&pwrpriv->lock);
 359
 360        /* if (pwrpriv->pwr_mode == PS_MODE_ACTIVE) */
 361        if (ps_mode == PS_MODE_ACTIVE) {
 362                if (!(hal_btcoex_IsBtControlLps(padapter))
 363                                || (hal_btcoex_IsBtControlLps(padapter)
 364                                        && !(hal_btcoex_IsLpsOn(padapter)))) {
 365                        pwrpriv->pwr_mode = ps_mode;
 366                        rtw_set_rpwm(padapter, PS_STATE_S4);
 367
 368                        rtw_hal_set_hwreg(padapter, HW_VAR_H2C_FW_PWRMODE, (u8 *)(&ps_mode));
 369                        pwrpriv->fw_current_in_ps_mode = false;
 370
 371                        hal_btcoex_LpsNotify(padapter, ps_mode);
 372                }
 373        } else {
 374                if ((PS_RDY_CHECK(padapter) && check_fwstate(&padapter->mlmepriv, WIFI_ASOC_STATE)) ||
 375                    ((hal_btcoex_IsBtControlLps(padapter)) && (hal_btcoex_IsLpsOn(padapter)))
 376                        ) {
 377                        u8 pslv;
 378
 379                        hal_btcoex_LpsNotify(padapter, ps_mode);
 380
 381                        pwrpriv->fw_current_in_ps_mode = true;
 382                        pwrpriv->pwr_mode = ps_mode;
 383                        pwrpriv->smart_ps = smart_ps;
 384                        pwrpriv->bcn_ant_mode = bcn_ant_mode;
 385                        rtw_hal_set_hwreg(padapter, HW_VAR_H2C_FW_PWRMODE, (u8 *)(&ps_mode));
 386
 387                        pslv = PS_STATE_S2;
 388                        if (pwrpriv->alives == 0)
 389                                pslv = PS_STATE_S0;
 390
 391                        if (!(hal_btcoex_IsBtDisabled(padapter)) &&
 392                            (hal_btcoex_IsBtControlLps(padapter))) {
 393                                u8 val8;
 394
 395                                val8 = hal_btcoex_LpsVal(padapter);
 396                                if (val8 & BIT(4))
 397                                        pslv = PS_STATE_S2;
 398                        }
 399
 400                        rtw_set_rpwm(padapter, pslv);
 401                }
 402        }
 403
 404        mutex_unlock(&pwrpriv->lock);
 405}
 406
 407/*
 408 * Return:
 409 *0:    Leave OK
 410 *-1:   Timeout
 411 *-2:   Other error
 412 */
 413s32 LPS_RF_ON_check(struct adapter *padapter, u32 delay_ms)
 414{
 415        unsigned long start_time;
 416        u8 bAwake = false;
 417        s32 err = 0;
 418
 419
 420        start_time = jiffies;
 421        while (1) {
 422                rtw_hal_get_hwreg(padapter, HW_VAR_FWLPS_RF_ON, &bAwake);
 423                if (bAwake)
 424                        break;
 425
 426                if (padapter->bSurpriseRemoved) {
 427                        err = -2;
 428                        break;
 429                }
 430
 431                if (jiffies_to_msecs(jiffies - start_time) > delay_ms) {
 432                        err = -1;
 433                        break;
 434                }
 435                msleep(1);
 436        }
 437
 438        return err;
 439}
 440
 441/*  */
 442/*      Description: */
 443/*              Enter the leisure power save mode. */
 444/*  */
 445void LPS_Enter(struct adapter *padapter, const char *msg)
 446{
 447        struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
 448        struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(dvobj);
 449        int n_assoc_iface = 0;
 450        char buf[32] = {0};
 451
 452        if (hal_btcoex_IsBtControlLps(padapter))
 453                return;
 454
 455        /* Skip lps enter request if number of assocated adapters is not 1 */
 456        if (check_fwstate(&(dvobj->padapters->mlmepriv), WIFI_ASOC_STATE))
 457                n_assoc_iface++;
 458        if (n_assoc_iface != 1)
 459                return;
 460
 461        /* Skip lps enter request for adapter not port0 */
 462        if (get_iface_type(padapter) != IFACE_PORT0)
 463                return;
 464
 465        if (!PS_RDY_CHECK(dvobj->padapters))
 466                return;
 467
 468        if (pwrpriv->bLeisurePs) {
 469                /*  Idle for a while if we connect to AP a while ago. */
 470                if (pwrpriv->LpsIdleCount >= 2) { /*   4 Sec */
 471                        if (pwrpriv->pwr_mode == PS_MODE_ACTIVE) {
 472                                scnprintf(buf, sizeof(buf), "WIFI-%s", msg);
 473                                pwrpriv->bpower_saving = true;
 474                                rtw_set_ps_mode(padapter, pwrpriv->power_mgnt, padapter->registrypriv.smart_ps, 0, buf);
 475                        }
 476                } else
 477                        pwrpriv->LpsIdleCount++;
 478        }
 479}
 480
 481/*  */
 482/*      Description: */
 483/*              Leave the leisure power save mode. */
 484/*  */
 485void LPS_Leave(struct adapter *padapter, const char *msg)
 486{
 487#define LPS_LEAVE_TIMEOUT_MS 100
 488
 489        struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
 490        struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(dvobj);
 491        char buf[32] = {0};
 492
 493        if (hal_btcoex_IsBtControlLps(padapter))
 494                return;
 495
 496        if (pwrpriv->bLeisurePs) {
 497                if (pwrpriv->pwr_mode != PS_MODE_ACTIVE) {
 498                        scnprintf(buf, sizeof(buf), "WIFI-%s", msg);
 499                        rtw_set_ps_mode(padapter, PS_MODE_ACTIVE, 0, 0, buf);
 500
 501                        if (pwrpriv->pwr_mode == PS_MODE_ACTIVE)
 502                                LPS_RF_ON_check(padapter, LPS_LEAVE_TIMEOUT_MS);
 503                }
 504        }
 505
 506        pwrpriv->bpower_saving = false;
 507}
 508
 509void LeaveAllPowerSaveModeDirect(struct adapter *Adapter)
 510{
 511        struct adapter *pri_padapter = GET_PRIMARY_ADAPTER(Adapter);
 512        struct mlme_priv *pmlmepriv = &(Adapter->mlmepriv);
 513        struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(Adapter);
 514
 515        if (Adapter->bSurpriseRemoved)
 516                return;
 517
 518        if (check_fwstate(pmlmepriv, _FW_LINKED)) { /* connect */
 519
 520                if (pwrpriv->pwr_mode == PS_MODE_ACTIVE)
 521                        return;
 522
 523                mutex_lock(&pwrpriv->lock);
 524
 525                rtw_set_rpwm(Adapter, PS_STATE_S4);
 526
 527                mutex_unlock(&pwrpriv->lock);
 528
 529                rtw_lps_ctrl_wk_cmd(pri_padapter, LPS_CTRL_LEAVE, 0);
 530        } else {
 531                if (pwrpriv->rf_pwrstate == rf_off)
 532                        ips_leave(pri_padapter);
 533        }
 534}
 535
 536/*  */
 537/*  Description: Leave all power save mode: LPS, FwLPS, IPS if needed. */
 538/*  Move code to function by tynli. 2010.03.26. */
 539/*  */
 540void LeaveAllPowerSaveMode(struct adapter *Adapter)
 541{
 542        struct dvobj_priv *dvobj = adapter_to_dvobj(Adapter);
 543        u8 enqueue = 0;
 544        int n_assoc_iface = 0;
 545
 546        if (!Adapter->bup)
 547                return;
 548
 549        if (Adapter->bSurpriseRemoved)
 550                return;
 551
 552        if (check_fwstate(&(dvobj->padapters->mlmepriv), WIFI_ASOC_STATE))
 553                n_assoc_iface++;
 554
 555        if (n_assoc_iface) { /* connect */
 556                enqueue = 1;
 557
 558                rtw_lps_ctrl_wk_cmd(Adapter, LPS_CTRL_LEAVE, enqueue);
 559
 560                LPS_Leave_check(Adapter);
 561        } else {
 562                if (adapter_to_pwrctl(Adapter)->rf_pwrstate == rf_off) {
 563                        ips_leave(Adapter);
 564                }
 565        }
 566}
 567
 568void LPS_Leave_check(struct adapter *padapter)
 569{
 570        struct pwrctrl_priv *pwrpriv;
 571        unsigned long   start_time;
 572        u8 bReady;
 573
 574        pwrpriv = adapter_to_pwrctl(padapter);
 575
 576        bReady = false;
 577        start_time = jiffies;
 578
 579        cond_resched();
 580
 581        while (1) {
 582                mutex_lock(&pwrpriv->lock);
 583
 584                if (padapter->bSurpriseRemoved ||
 585                    !(padapter->hw_init_completed) ||
 586                    (pwrpriv->pwr_mode == PS_MODE_ACTIVE))
 587                        bReady = true;
 588
 589                mutex_unlock(&pwrpriv->lock);
 590
 591                if (bReady)
 592                        break;
 593
 594                if (jiffies_to_msecs(jiffies - start_time) > 100)
 595                        break;
 596
 597                msleep(1);
 598        }
 599}
 600
 601/*
 602 * Caller:ISR handler...
 603 *
 604 * This will be called when CPWM interrupt is up.
 605 *
 606 * using to update cpwn of drv; and drv willl make a decision to up or down pwr level
 607 */
 608void cpwm_int_hdl(struct adapter *padapter, struct reportpwrstate_parm *preportpwrstate)
 609{
 610        struct pwrctrl_priv *pwrpriv;
 611
 612        pwrpriv = adapter_to_pwrctl(padapter);
 613
 614        mutex_lock(&pwrpriv->lock);
 615
 616        if (pwrpriv->rpwm < PS_STATE_S2)
 617                goto exit;
 618
 619        pwrpriv->cpwm = PS_STATE(preportpwrstate->state);
 620        pwrpriv->cpwm_tog = preportpwrstate->state & PS_TOGGLE;
 621
 622        if (pwrpriv->cpwm >= PS_STATE_S2) {
 623                if (pwrpriv->alives & CMD_ALIVE)
 624                        complete(&padapter->cmdpriv.cmd_queue_comp);
 625
 626                if (pwrpriv->alives & XMIT_ALIVE)
 627                        complete(&padapter->xmitpriv.xmit_comp);
 628        }
 629
 630exit:
 631        mutex_unlock(&pwrpriv->lock);
 632
 633}
 634
 635static void cpwm_event_callback(struct work_struct *work)
 636{
 637        struct pwrctrl_priv *pwrpriv = container_of(work, struct pwrctrl_priv, cpwm_event);
 638        struct dvobj_priv *dvobj = pwrctl_to_dvobj(pwrpriv);
 639        struct adapter *adapter = dvobj->if1;
 640        struct reportpwrstate_parm report;
 641
 642        report.state = PS_STATE_S2;
 643        cpwm_int_hdl(adapter, &report);
 644}
 645
 646static void rpwmtimeout_workitem_callback(struct work_struct *work)
 647{
 648        struct adapter *padapter;
 649        struct dvobj_priv *dvobj;
 650        struct pwrctrl_priv *pwrpriv;
 651
 652
 653        pwrpriv = container_of(work, struct pwrctrl_priv, rpwmtimeoutwi);
 654        dvobj = pwrctl_to_dvobj(pwrpriv);
 655        padapter = dvobj->if1;
 656
 657        mutex_lock(&pwrpriv->lock);
 658        if ((pwrpriv->rpwm == pwrpriv->cpwm) || (pwrpriv->cpwm >= PS_STATE_S2))
 659                goto exit;
 660
 661        mutex_unlock(&pwrpriv->lock);
 662
 663        if (rtw_read8(padapter, 0x100) != 0xEA) {
 664                struct reportpwrstate_parm report;
 665
 666                report.state = PS_STATE_S2;
 667                cpwm_int_hdl(padapter, &report);
 668
 669                return;
 670        }
 671
 672        mutex_lock(&pwrpriv->lock);
 673
 674        if ((pwrpriv->rpwm == pwrpriv->cpwm) || (pwrpriv->cpwm >= PS_STATE_S2))
 675                goto exit;
 676
 677        pwrpriv->brpwmtimeout = true;
 678        rtw_set_rpwm(padapter, pwrpriv->rpwm);
 679        pwrpriv->brpwmtimeout = false;
 680
 681exit:
 682        mutex_unlock(&pwrpriv->lock);
 683}
 684
 685/*
 686 * This function is a timer handler, can't do any IO in it.
 687 */
 688static void pwr_rpwm_timeout_handler(struct timer_list *t)
 689{
 690        struct pwrctrl_priv *pwrpriv = from_timer(pwrpriv, t, pwr_rpwm_timer);
 691
 692        if ((pwrpriv->rpwm == pwrpriv->cpwm) || (pwrpriv->cpwm >= PS_STATE_S2))
 693                return;
 694
 695        _set_workitem(&pwrpriv->rpwmtimeoutwi);
 696}
 697
 698static inline void register_task_alive(struct pwrctrl_priv *pwrctrl, u32 tag)
 699{
 700        pwrctrl->alives |= tag;
 701}
 702
 703static inline void unregister_task_alive(struct pwrctrl_priv *pwrctrl, u32 tag)
 704{
 705        pwrctrl->alives &= ~tag;
 706}
 707
 708
 709/*
 710 * Description:
 711 *Check if the fw_pwrstate is okay for I/O.
 712 *If not (cpwm is less than S2), then the sub-routine
 713 *will raise the cpwm to be greater than or equal to S2.
 714 *
 715 *Calling Context: Passive
 716 *
 717 *Constraint:
 718 *      1. this function will request pwrctrl->lock
 719 *
 720 * Return Value:
 721 *_SUCCESS      hardware is ready for I/O
 722 *_FAIL         can't I/O right now
 723 */
 724s32 rtw_register_task_alive(struct adapter *padapter, u32 task)
 725{
 726        s32 res;
 727        struct pwrctrl_priv *pwrctrl;
 728        u8 pslv;
 729
 730        res = _SUCCESS;
 731        pwrctrl = adapter_to_pwrctl(padapter);
 732        pslv = PS_STATE_S2;
 733
 734        mutex_lock(&pwrctrl->lock);
 735
 736        register_task_alive(pwrctrl, task);
 737
 738        if (pwrctrl->fw_current_in_ps_mode) {
 739                if (pwrctrl->cpwm < pslv) {
 740                        if (pwrctrl->cpwm < PS_STATE_S2)
 741                                res = _FAIL;
 742                        if (pwrctrl->rpwm < pslv)
 743                                rtw_set_rpwm(padapter, pslv);
 744                }
 745        }
 746
 747        mutex_unlock(&pwrctrl->lock);
 748
 749        if (res == _FAIL)
 750                if (pwrctrl->cpwm >= PS_STATE_S2)
 751                        res = _SUCCESS;
 752
 753        return res;
 754}
 755
 756/*
 757 * Description:
 758 *If task is done, call this func. to power down firmware again.
 759 *
 760 *Constraint:
 761 *      1. this function will request pwrctrl->lock
 762 *
 763 * Return Value:
 764 *none
 765 */
 766void rtw_unregister_task_alive(struct adapter *padapter, u32 task)
 767{
 768        struct pwrctrl_priv *pwrctrl;
 769        u8 pslv;
 770
 771        pwrctrl = adapter_to_pwrctl(padapter);
 772        pslv = PS_STATE_S0;
 773
 774        if (!(hal_btcoex_IsBtDisabled(padapter)) && hal_btcoex_IsBtControlLps(padapter)) {
 775                u8 val8;
 776
 777                val8 = hal_btcoex_LpsVal(padapter);
 778                if (val8 & BIT(4))
 779                        pslv = PS_STATE_S2;
 780        }
 781
 782        mutex_lock(&pwrctrl->lock);
 783
 784        unregister_task_alive(pwrctrl, task);
 785
 786        if ((pwrctrl->pwr_mode != PS_MODE_ACTIVE) && pwrctrl->fw_current_in_ps_mode) {
 787                if (pwrctrl->cpwm > pslv)
 788                        if ((pslv >= PS_STATE_S2) || (pwrctrl->alives == 0))
 789                                rtw_set_rpwm(padapter, pslv);
 790
 791        }
 792
 793        mutex_unlock(&pwrctrl->lock);
 794}
 795
 796/*
 797 * Caller: rtw_xmit_thread
 798 *
 799 * Check if the fw_pwrstate is okay for xmit.
 800 * If not (cpwm is less than S3), then the sub-routine
 801 * will raise the cpwm to be greater than or equal to S3.
 802 *
 803 * Calling Context: Passive
 804 *
 805 * Return Value:
 806 * _SUCCESS     rtw_xmit_thread can write fifo/txcmd afterwards.
 807 * _FAIL                rtw_xmit_thread can not do anything.
 808 */
 809s32 rtw_register_tx_alive(struct adapter *padapter)
 810{
 811        s32 res;
 812        struct pwrctrl_priv *pwrctrl;
 813        u8 pslv;
 814
 815        res = _SUCCESS;
 816        pwrctrl = adapter_to_pwrctl(padapter);
 817        pslv = PS_STATE_S2;
 818
 819        mutex_lock(&pwrctrl->lock);
 820
 821        register_task_alive(pwrctrl, XMIT_ALIVE);
 822
 823        if (pwrctrl->fw_current_in_ps_mode) {
 824                if (pwrctrl->cpwm < pslv) {
 825                        if (pwrctrl->cpwm < PS_STATE_S2)
 826                                res = _FAIL;
 827                        if (pwrctrl->rpwm < pslv)
 828                                rtw_set_rpwm(padapter, pslv);
 829                }
 830        }
 831
 832        mutex_unlock(&pwrctrl->lock);
 833
 834        if (res == _FAIL)
 835                if (pwrctrl->cpwm >= PS_STATE_S2)
 836                        res = _SUCCESS;
 837
 838        return res;
 839}
 840
 841/*
 842 * Caller: rtw_cmd_thread
 843 *
 844 * Check if the fw_pwrstate is okay for issuing cmd.
 845 * If not (cpwm should be is less than S2), then the sub-routine
 846 * will raise the cpwm to be greater than or equal to S2.
 847 *
 848 * Calling Context: Passive
 849 *
 850 * Return Value:
 851 *_SUCCESS      rtw_cmd_thread can issue cmds to firmware afterwards.
 852 *_FAIL         rtw_cmd_thread can not do anything.
 853 */
 854s32 rtw_register_cmd_alive(struct adapter *padapter)
 855{
 856        s32 res;
 857        struct pwrctrl_priv *pwrctrl;
 858        u8 pslv;
 859
 860        res = _SUCCESS;
 861        pwrctrl = adapter_to_pwrctl(padapter);
 862        pslv = PS_STATE_S2;
 863
 864        mutex_lock(&pwrctrl->lock);
 865
 866        register_task_alive(pwrctrl, CMD_ALIVE);
 867
 868        if (pwrctrl->fw_current_in_ps_mode) {
 869                if (pwrctrl->cpwm < pslv) {
 870                        if (pwrctrl->cpwm < PS_STATE_S2)
 871                                res = _FAIL;
 872                        if (pwrctrl->rpwm < pslv)
 873                                rtw_set_rpwm(padapter, pslv);
 874                }
 875        }
 876
 877        mutex_unlock(&pwrctrl->lock);
 878
 879        if (res == _FAIL)
 880                if (pwrctrl->cpwm >= PS_STATE_S2)
 881                        res = _SUCCESS;
 882
 883        return res;
 884}
 885
 886/*
 887 * Caller: ISR
 888 *
 889 * If ISR's txdone,
 890 * No more pkts for TX,
 891 * Then driver shall call this fun. to power down firmware again.
 892 */
 893void rtw_unregister_tx_alive(struct adapter *padapter)
 894{
 895        struct pwrctrl_priv *pwrctrl;
 896        u8 pslv;
 897
 898        pwrctrl = adapter_to_pwrctl(padapter);
 899        pslv = PS_STATE_S0;
 900
 901        if (!(hal_btcoex_IsBtDisabled(padapter)) && hal_btcoex_IsBtControlLps(padapter)) {
 902                u8 val8;
 903
 904                val8 = hal_btcoex_LpsVal(padapter);
 905                if (val8 & BIT(4))
 906                        pslv = PS_STATE_S2;
 907        }
 908
 909        mutex_lock(&pwrctrl->lock);
 910
 911        unregister_task_alive(pwrctrl, XMIT_ALIVE);
 912
 913        if ((pwrctrl->pwr_mode != PS_MODE_ACTIVE) && pwrctrl->fw_current_in_ps_mode) {
 914                if (pwrctrl->cpwm > pslv)
 915                        if ((pslv >= PS_STATE_S2) || (pwrctrl->alives == 0))
 916                                rtw_set_rpwm(padapter, pslv);
 917        }
 918
 919        mutex_unlock(&pwrctrl->lock);
 920}
 921
 922/*
 923 * Caller: ISR
 924 *
 925 * If all commands have been done,
 926 * and no more command to do,
 927 * then driver shall call this fun. to power down firmware again.
 928 */
 929void rtw_unregister_cmd_alive(struct adapter *padapter)
 930{
 931        struct pwrctrl_priv *pwrctrl;
 932        u8 pslv;
 933
 934        pwrctrl = adapter_to_pwrctl(padapter);
 935        pslv = PS_STATE_S0;
 936
 937        if (!(hal_btcoex_IsBtDisabled(padapter)) && hal_btcoex_IsBtControlLps(padapter)) {
 938                u8 val8;
 939
 940                val8 = hal_btcoex_LpsVal(padapter);
 941                if (val8 & BIT(4))
 942                        pslv = PS_STATE_S2;
 943        }
 944
 945        mutex_lock(&pwrctrl->lock);
 946
 947        unregister_task_alive(pwrctrl, CMD_ALIVE);
 948
 949        if ((pwrctrl->pwr_mode != PS_MODE_ACTIVE) && pwrctrl->fw_current_in_ps_mode) {
 950                if (pwrctrl->cpwm > pslv) {
 951                        if ((pslv >= PS_STATE_S2) || (pwrctrl->alives == 0))
 952                                rtw_set_rpwm(padapter, pslv);
 953                }
 954        }
 955
 956        mutex_unlock(&pwrctrl->lock);
 957}
 958
 959void rtw_init_pwrctrl_priv(struct adapter *padapter)
 960{
 961        struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
 962
 963        mutex_init(&pwrctrlpriv->lock);
 964        pwrctrlpriv->rf_pwrstate = rf_on;
 965        pwrctrlpriv->ips_enter_cnts = 0;
 966        pwrctrlpriv->ips_leave_cnts = 0;
 967        pwrctrlpriv->bips_processing = false;
 968
 969        pwrctrlpriv->ips_mode = padapter->registrypriv.ips_mode;
 970        pwrctrlpriv->ips_mode_req = padapter->registrypriv.ips_mode;
 971
 972        pwrctrlpriv->pwr_state_check_interval = RTW_PWR_STATE_CHK_INTERVAL;
 973        pwrctrlpriv->pwr_state_check_cnts = 0;
 974        pwrctrlpriv->bInternalAutoSuspend = false;
 975        pwrctrlpriv->bInSuspend = false;
 976        pwrctrlpriv->bkeepfwalive = false;
 977
 978        pwrctrlpriv->LpsIdleCount = 0;
 979        pwrctrlpriv->power_mgnt = padapter->registrypriv.power_mgnt;/*  PS_MODE_MIN; */
 980        pwrctrlpriv->bLeisurePs = pwrctrlpriv->power_mgnt != PS_MODE_ACTIVE;
 981
 982        pwrctrlpriv->fw_current_in_ps_mode = false;
 983
 984        pwrctrlpriv->rpwm = 0;
 985        pwrctrlpriv->cpwm = PS_STATE_S4;
 986
 987        pwrctrlpriv->pwr_mode = PS_MODE_ACTIVE;
 988        pwrctrlpriv->smart_ps = padapter->registrypriv.smart_ps;
 989        pwrctrlpriv->bcn_ant_mode = 0;
 990        pwrctrlpriv->dtim = 0;
 991
 992        pwrctrlpriv->tog = 0x80;
 993
 994        rtw_hal_set_hwreg(padapter, HW_VAR_SET_RPWM, (u8 *)(&pwrctrlpriv->rpwm));
 995
 996        _init_workitem(&pwrctrlpriv->cpwm_event, cpwm_event_callback, NULL);
 997
 998        pwrctrlpriv->brpwmtimeout = false;
 999        pwrctrlpriv->adapter = padapter;
1000        _init_workitem(&pwrctrlpriv->rpwmtimeoutwi, rpwmtimeout_workitem_callback, NULL);
1001        timer_setup(&pwrctrlpriv->pwr_rpwm_timer, pwr_rpwm_timeout_handler, 0);
1002        timer_setup(&pwrctrlpriv->pwr_state_check_timer,
1003                    pwr_state_check_handler, 0);
1004
1005        pwrctrlpriv->wowlan_mode = false;
1006        pwrctrlpriv->wowlan_ap_mode = false;
1007}
1008
1009
1010void rtw_free_pwrctrl_priv(struct adapter *adapter)
1011{
1012}
1013
1014inline void rtw_set_ips_deny(struct adapter *padapter, u32 ms)
1015{
1016        struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
1017        pwrpriv->ips_deny_time = jiffies + msecs_to_jiffies(ms);
1018}
1019
1020/*
1021* rtw_pwr_wakeup - Wake the NIC up from: 1)IPS. 2)USB autosuspend
1022* @adapter: pointer to struct adapter structure
1023* @ips_deffer_ms: the ms will prevent from falling into IPS after wakeup
1024* Return _SUCCESS or _FAIL
1025*/
1026
1027int _rtw_pwr_wakeup(struct adapter *padapter, u32 ips_deffer_ms, const char *caller)
1028{
1029        struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
1030        struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(dvobj);
1031        struct mlme_priv *pmlmepriv;
1032        int ret = _SUCCESS;
1033        unsigned long start = jiffies;
1034        unsigned long deny_time = jiffies + msecs_to_jiffies(ips_deffer_ms);
1035
1036        /* for LPS */
1037        LeaveAllPowerSaveMode(padapter);
1038
1039        /* IPS still bound with primary adapter */
1040        padapter = GET_PRIMARY_ADAPTER(padapter);
1041        pmlmepriv = &padapter->mlmepriv;
1042
1043        if (time_before(pwrpriv->ips_deny_time, deny_time))
1044                pwrpriv->ips_deny_time = deny_time;
1045
1046
1047        if (pwrpriv->ps_processing)
1048                while (pwrpriv->ps_processing && jiffies_to_msecs(jiffies - start) <= 3000)
1049                        mdelay(10);
1050
1051        if (!(pwrpriv->bInternalAutoSuspend) && pwrpriv->bInSuspend)
1052                while (pwrpriv->bInSuspend && jiffies_to_msecs(jiffies - start) <= 3000
1053                )
1054                        mdelay(10);
1055
1056        /* System suspend is not allowed to wakeup */
1057        if (!(pwrpriv->bInternalAutoSuspend) && pwrpriv->bInSuspend) {
1058                ret = _FAIL;
1059                goto exit;
1060        }
1061
1062        /* block??? */
1063        if (pwrpriv->bInternalAutoSuspend  && padapter->net_closed) {
1064                ret = _FAIL;
1065                goto exit;
1066        }
1067
1068        /* I think this should be check in IPS, LPS, autosuspend functions... */
1069        if (check_fwstate(pmlmepriv, _FW_LINKED)) {
1070                ret = _SUCCESS;
1071                goto exit;
1072        }
1073
1074        if (rf_off == pwrpriv->rf_pwrstate) {
1075                {
1076                        if (ips_leave(padapter) == _FAIL) {
1077                                ret = _FAIL;
1078                                goto exit;
1079                        }
1080                }
1081        }
1082
1083        /* TODO: the following checking need to be merged... */
1084        if (padapter->bDriverStopped || !padapter->bup || !padapter->hw_init_completed) {
1085                ret = false;
1086                goto exit;
1087        }
1088
1089exit:
1090        deny_time = jiffies + msecs_to_jiffies(ips_deffer_ms);
1091        if (time_before(pwrpriv->ips_deny_time, deny_time))
1092                pwrpriv->ips_deny_time = deny_time;
1093        return ret;
1094
1095}
1096
1097int rtw_pm_set_lps(struct adapter *padapter, u8 mode)
1098{
1099        int     ret = 0;
1100        struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
1101
1102        if (mode < PS_MODE_NUM) {
1103                if (pwrctrlpriv->power_mgnt != mode) {
1104                        if (mode == PS_MODE_ACTIVE)
1105                                LeaveAllPowerSaveMode(padapter);
1106                        else
1107                                pwrctrlpriv->LpsIdleCount = 2;
1108
1109                        pwrctrlpriv->power_mgnt = mode;
1110                        pwrctrlpriv->bLeisurePs =
1111                                pwrctrlpriv->power_mgnt != PS_MODE_ACTIVE;
1112                }
1113        } else
1114                ret = -EINVAL;
1115
1116        return ret;
1117}
1118
1119int rtw_pm_set_ips(struct adapter *padapter, u8 mode)
1120{
1121        struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
1122
1123        if (mode == IPS_NORMAL || mode == IPS_LEVEL_2) {
1124                rtw_ips_mode_req(pwrctrlpriv, mode);
1125                return 0;
1126        } else if (mode == IPS_NONE) {
1127                rtw_ips_mode_req(pwrctrlpriv, mode);
1128                if ((padapter->bSurpriseRemoved == 0) && (rtw_pwr_wakeup(padapter) == _FAIL))
1129                        return -EFAULT;
1130        } else
1131                return -EINVAL;
1132
1133        return 0;
1134}
1135
1136/*
1137 * ATTENTION:
1138 *This function will request pwrctrl LOCK!
1139 */
1140void rtw_ps_deny(struct adapter *padapter, enum ps_deny_reason reason)
1141{
1142        struct pwrctrl_priv *pwrpriv;
1143
1144        pwrpriv = adapter_to_pwrctl(padapter);
1145
1146        mutex_lock(&pwrpriv->lock);
1147        pwrpriv->ps_deny |= BIT(reason);
1148        mutex_unlock(&pwrpriv->lock);
1149}
1150
1151/*
1152 * ATTENTION:
1153 *This function will request pwrctrl LOCK!
1154 */
1155void rtw_ps_deny_cancel(struct adapter *padapter, enum ps_deny_reason reason)
1156{
1157        struct pwrctrl_priv *pwrpriv;
1158
1159        pwrpriv = adapter_to_pwrctl(padapter);
1160
1161        mutex_lock(&pwrpriv->lock);
1162        pwrpriv->ps_deny &= ~BIT(reason);
1163        mutex_unlock(&pwrpriv->lock);
1164}
1165
1166/*
1167 * ATTENTION:
1168 *Before calling this function pwrctrl lock should be occupied already,
1169 *otherwise it may return incorrect value.
1170 */
1171u32 rtw_ps_deny_get(struct adapter *padapter)
1172{
1173        return adapter_to_pwrctl(padapter)->ps_deny;
1174}
1175