1*ce86893fSKarun Eagalapati /** 2*ce86893fSKarun Eagalapati * Copyright (c) 2014 Redpine Signals Inc. 3*ce86893fSKarun Eagalapati * 4*ce86893fSKarun Eagalapati * Permission to use, copy, modify, and/or distribute this software for any 5*ce86893fSKarun Eagalapati * purpose with or without fee is hereby granted, provided that the above 6*ce86893fSKarun Eagalapati * copyright notice and this permission notice appear in all copies. 7*ce86893fSKarun Eagalapati * 8*ce86893fSKarun Eagalapati * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9*ce86893fSKarun Eagalapati * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10*ce86893fSKarun Eagalapati * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11*ce86893fSKarun Eagalapati * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12*ce86893fSKarun Eagalapati * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13*ce86893fSKarun Eagalapati * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14*ce86893fSKarun Eagalapati * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15*ce86893fSKarun Eagalapati */ 16*ce86893fSKarun Eagalapati 17*ce86893fSKarun Eagalapati #include <linux/etherdevice.h> 18*ce86893fSKarun Eagalapati #include <linux/if.h> 19*ce86893fSKarun Eagalapati #include <linux/version.h> 20*ce86893fSKarun Eagalapati #include "rsi_debugfs.h" 21*ce86893fSKarun Eagalapati #include "rsi_mgmt.h" 22*ce86893fSKarun Eagalapati #include "rsi_common.h" 23*ce86893fSKarun Eagalapati #include "rsi_ps.h" 24*ce86893fSKarun Eagalapati 25*ce86893fSKarun Eagalapati char *str_psstate(enum ps_state state) 26*ce86893fSKarun Eagalapati { 27*ce86893fSKarun Eagalapati switch (state) { 28*ce86893fSKarun Eagalapati case PS_NONE: 29*ce86893fSKarun Eagalapati return "PS_NONE"; 30*ce86893fSKarun Eagalapati case PS_DISABLE_REQ_SENT: 31*ce86893fSKarun Eagalapati return "PS_DISABLE_REQ_SENT"; 32*ce86893fSKarun Eagalapati case PS_ENABLE_REQ_SENT: 33*ce86893fSKarun Eagalapati return "PS_ENABLE_REQ_SENT"; 34*ce86893fSKarun Eagalapati case PS_ENABLED: 35*ce86893fSKarun Eagalapati return "PS_ENABLED"; 36*ce86893fSKarun Eagalapati default: 37*ce86893fSKarun Eagalapati return "INVALID_STATE"; 38*ce86893fSKarun Eagalapati } 39*ce86893fSKarun Eagalapati return "INVALID_STATE"; 40*ce86893fSKarun Eagalapati } 41*ce86893fSKarun Eagalapati 42*ce86893fSKarun Eagalapati static inline void rsi_modify_ps_state(struct rsi_hw *adapter, 43*ce86893fSKarun Eagalapati enum ps_state nstate) 44*ce86893fSKarun Eagalapati { 45*ce86893fSKarun Eagalapati rsi_dbg(INFO_ZONE, "PS state changed %s => %s\n", 46*ce86893fSKarun Eagalapati str_psstate(adapter->ps_state), 47*ce86893fSKarun Eagalapati str_psstate(nstate)); 48*ce86893fSKarun Eagalapati 49*ce86893fSKarun Eagalapati adapter->ps_state = nstate; 50*ce86893fSKarun Eagalapati } 51*ce86893fSKarun Eagalapati 52*ce86893fSKarun Eagalapati void rsi_default_ps_params(struct rsi_hw *adapter) 53*ce86893fSKarun Eagalapati { 54*ce86893fSKarun Eagalapati struct rsi_ps_info *ps_info = &adapter->ps_info; 55*ce86893fSKarun Eagalapati 56*ce86893fSKarun Eagalapati ps_info->enabled = true; 57*ce86893fSKarun Eagalapati ps_info->sleep_type = RSI_SLEEP_TYPE_LP; 58*ce86893fSKarun Eagalapati ps_info->tx_threshold = 0; 59*ce86893fSKarun Eagalapati ps_info->rx_threshold = 0; 60*ce86893fSKarun Eagalapati ps_info->tx_hysterisis = 0; 61*ce86893fSKarun Eagalapati ps_info->rx_hysterisis = 0; 62*ce86893fSKarun Eagalapati ps_info->monitor_interval = 0; 63*ce86893fSKarun Eagalapati ps_info->listen_interval = RSI_DEF_LISTEN_INTERVAL; 64*ce86893fSKarun Eagalapati ps_info->num_bcns_per_lis_int = 0; 65*ce86893fSKarun Eagalapati ps_info->dtim_interval_duration = 0; 66*ce86893fSKarun Eagalapati ps_info->num_dtims_per_sleep = 0; 67*ce86893fSKarun Eagalapati ps_info->deep_sleep_wakeup_period = RSI_DEF_DS_WAKEUP_PERIOD; 68*ce86893fSKarun Eagalapati } 69*ce86893fSKarun Eagalapati 70*ce86893fSKarun Eagalapati void rsi_enable_ps(struct rsi_hw *adapter) 71*ce86893fSKarun Eagalapati { 72*ce86893fSKarun Eagalapati if (adapter->ps_state != PS_NONE) { 73*ce86893fSKarun Eagalapati rsi_dbg(ERR_ZONE, 74*ce86893fSKarun Eagalapati "%s: Cannot accept enable PS in %s state\n", 75*ce86893fSKarun Eagalapati __func__, str_psstate(adapter->ps_state)); 76*ce86893fSKarun Eagalapati return; 77*ce86893fSKarun Eagalapati } 78*ce86893fSKarun Eagalapati 79*ce86893fSKarun Eagalapati if (rsi_send_ps_request(adapter, true)) { 80*ce86893fSKarun Eagalapati rsi_dbg(ERR_ZONE, 81*ce86893fSKarun Eagalapati "%s: Failed to send PS request to device\n", 82*ce86893fSKarun Eagalapati __func__); 83*ce86893fSKarun Eagalapati return; 84*ce86893fSKarun Eagalapati } 85*ce86893fSKarun Eagalapati 86*ce86893fSKarun Eagalapati rsi_modify_ps_state(adapter, PS_ENABLE_REQ_SENT); 87*ce86893fSKarun Eagalapati } 88*ce86893fSKarun Eagalapati 89*ce86893fSKarun Eagalapati void rsi_disable_ps(struct rsi_hw *adapter) 90*ce86893fSKarun Eagalapati { 91*ce86893fSKarun Eagalapati if (adapter->ps_state != PS_ENABLED) { 92*ce86893fSKarun Eagalapati rsi_dbg(ERR_ZONE, 93*ce86893fSKarun Eagalapati "%s: Cannot accept disable PS in %s state\n", 94*ce86893fSKarun Eagalapati __func__, str_psstate(adapter->ps_state)); 95*ce86893fSKarun Eagalapati return; 96*ce86893fSKarun Eagalapati } 97*ce86893fSKarun Eagalapati 98*ce86893fSKarun Eagalapati if (rsi_send_ps_request(adapter, false)) { 99*ce86893fSKarun Eagalapati rsi_dbg(ERR_ZONE, 100*ce86893fSKarun Eagalapati "%s: Failed to send PS request to device\n", 101*ce86893fSKarun Eagalapati __func__); 102*ce86893fSKarun Eagalapati return; 103*ce86893fSKarun Eagalapati } 104*ce86893fSKarun Eagalapati 105*ce86893fSKarun Eagalapati rsi_modify_ps_state(adapter, PS_DISABLE_REQ_SENT); 106*ce86893fSKarun Eagalapati } 107*ce86893fSKarun Eagalapati 108*ce86893fSKarun Eagalapati int rsi_handle_ps_confirm(struct rsi_hw *adapter, u8 *msg) 109*ce86893fSKarun Eagalapati { 110*ce86893fSKarun Eagalapati u16 cfm_type = get_unaligned_le16(msg + PS_CONFIRM_INDEX); 111*ce86893fSKarun Eagalapati 112*ce86893fSKarun Eagalapati switch (cfm_type) { 113*ce86893fSKarun Eagalapati case RSI_SLEEP_REQUEST: 114*ce86893fSKarun Eagalapati if (adapter->ps_state == PS_ENABLE_REQ_SENT) 115*ce86893fSKarun Eagalapati rsi_modify_ps_state(adapter, PS_ENABLED); 116*ce86893fSKarun Eagalapati break; 117*ce86893fSKarun Eagalapati case RSI_WAKEUP_REQUEST: 118*ce86893fSKarun Eagalapati if (adapter->ps_state == PS_DISABLE_REQ_SENT) 119*ce86893fSKarun Eagalapati rsi_modify_ps_state(adapter, PS_NONE); 120*ce86893fSKarun Eagalapati break; 121*ce86893fSKarun Eagalapati default: 122*ce86893fSKarun Eagalapati rsi_dbg(ERR_ZONE, 123*ce86893fSKarun Eagalapati "Invalid PS confirm type %x in state %s\n", 124*ce86893fSKarun Eagalapati cfm_type, str_psstate(adapter->ps_state)); 125*ce86893fSKarun Eagalapati return -1; 126*ce86893fSKarun Eagalapati } 127*ce86893fSKarun Eagalapati 128*ce86893fSKarun Eagalapati return 0; 129*ce86893fSKarun Eagalapati } 130