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