Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[cascardo/linux.git] / drivers / staging / rtl8192ee / rc.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2009-2010  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 "rc.h"
29
30 /*
31  *Finds the highest rate index we can use
32  *if skb is special data like DHCP/EAPOL, we set should
33  *it to lowest rate CCK_1M, otherwise we set rate to
34  *highest rate based on wireless mode used for iwconfig
35  *show Tx rate.
36  */
37 static u8 _rtl_rc_get_highest_rix(struct rtl_priv *rtlpriv,
38                                   struct ieee80211_sta *sta,
39                                   struct sk_buff *skb, bool not_data)
40 {
41         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
42         struct rtl_phy *rtlphy = &(rtlpriv->phy);
43         struct rtl_sta_info *sta_entry = NULL;
44         u8 wireless_mode = 0;
45
46         /*
47          *this rate is no use for true rate, firmware
48          *will control rate at all it just used for
49          *1.show in iwconfig in B/G mode
50          *2.in stg_rtl_get_tcb_desc when we check rate is
51          *      1M we will not use FW rate but user rate.
52          */
53
54         if (sta) {
55                 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
56                 wireless_mode = sta_entry->wireless_mode;
57         }
58
59         if (rtl92e_is_special_data(rtlpriv->mac80211.hw, skb, true) ||
60             not_data) {
61                 return 0;
62         } else {
63                 if (rtlhal->current_bandtype == BAND_ON_2_4G) {
64                         if (wireless_mode == WIRELESS_MODE_B) {
65                                 return B_MODE_MAX_RIX;
66                         } else if (wireless_mode == WIRELESS_MODE_G) {
67                                 return G_MODE_MAX_RIX;
68                         } else if (wireless_mode == WIRELESS_MODE_N_24G) {
69                                 if (get_rf_type(rtlphy) != RF_2T2R)
70                                         return N_MODE_MCS7_RIX;
71                                 else
72                                         return N_MODE_MCS15_RIX;
73                         } else if (wireless_mode == WIRELESS_MODE_AC_24G) {
74                                 return AC_MODE_MCS9_RIX;
75                         } else {
76                                 return 0;
77                         }
78                 } else {
79                         if (wireless_mode == WIRELESS_MODE_A) {
80                                 return A_MODE_MAX_RIX;
81                         } else if (wireless_mode == WIRELESS_MODE_N_5G) {
82                                 if (get_rf_type(rtlphy) != RF_2T2R)
83                                         return N_MODE_MCS7_RIX;
84                                 else
85                                         return N_MODE_MCS15_RIX;
86                         } else if (wireless_mode == WIRELESS_MODE_AC_5G) {
87                                 return AC_MODE_MCS9_RIX;
88                         } else {
89                                 return 0;
90                         }
91                 }
92         }
93 }
94
95 static void _rtl_rc_rate_set_series(struct rtl_priv *rtlpriv,
96                                     struct ieee80211_sta *sta,
97                                     struct ieee80211_tx_rate *rate,
98                                     struct ieee80211_tx_rate_control *txrc,
99                                     u8 tries, char rix, int rtsctsenable,
100                                     bool not_data)
101 {
102         struct rtl_mac *mac = rtl_mac(rtlpriv);
103         u8 sgi_20 = 0, sgi_40 = 0, sgi_80 = 0;
104
105         if (sta) {
106                 sgi_20 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
107                 sgi_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
108                 sgi_80 = sta->vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80;
109         }
110         rate->count = tries;
111         rate->idx = rix >= 0x00 ? rix : 0x00;
112
113         if (!not_data) {
114                 if (txrc->short_preamble)
115                         rate->flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
116                 if (mac->opmode == NL80211_IFTYPE_AP ||
117                     mac->opmode == NL80211_IFTYPE_ADHOC) {
118                         if (sta && (sta->ht_cap.cap &
119                                     IEEE80211_HT_CAP_SUP_WIDTH_20_40))
120                                 rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
121                         if (sta && (sta->vht_cap.vht_supported))
122                                 rate->flags |= IEEE80211_TX_RC_80_MHZ_WIDTH;
123                 } else {
124                         if (mac->bw_40)
125                                 rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
126                         if (mac->bw_80)
127                                 rate->flags |= IEEE80211_TX_RC_80_MHZ_WIDTH;
128                 }
129
130                 if (sgi_20 || sgi_40 || sgi_80)
131                         rate->flags |= IEEE80211_TX_RC_SHORT_GI;
132                 if (sta && sta->ht_cap.ht_supported)
133                         rate->flags |= IEEE80211_TX_RC_MCS;
134                 if (sta && sta->vht_cap.vht_supported)
135                         rate->flags |= IEEE80211_TX_RC_VHT_MCS;
136         }
137 }
138
139 static void rtl_get_rate(void *ppriv, struct ieee80211_sta *sta,
140                          void *priv_sta,
141                          struct ieee80211_tx_rate_control *txrc)
142 {
143         struct rtl_priv *rtlpriv = ppriv;
144         struct sk_buff *skb = txrc->skb;
145         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
146         struct ieee80211_tx_rate *rates = tx_info->control.rates;
147         __le16 fc = rtl_get_fc(skb);
148         u8 try_per_rate, i, rix;
149         bool not_data = !ieee80211_is_data(fc);
150
151         if (rate_control_send_low(sta, priv_sta, txrc))
152                 return;
153
154         rix = _rtl_rc_get_highest_rix(rtlpriv, sta, skb, not_data);
155         try_per_rate = 1;
156         _rtl_rc_rate_set_series(rtlpriv, sta, &rates[0], txrc,
157                                 try_per_rate, rix, 1, not_data);
158
159         if (!not_data) {
160                 for (i = 1; i < 4; i++)
161                         _rtl_rc_rate_set_series(rtlpriv, sta, &rates[i],
162                                                 txrc, i, (rix - i), 1,
163                                                 not_data);
164         }
165 }
166
167 static bool _rtl_tx_aggr_check(struct rtl_priv *rtlpriv,
168                                struct rtl_sta_info *sta_entry, u16 tid)
169 {
170         struct rtl_mac *mac = rtl_mac(rtlpriv);
171
172         if (mac->act_scanning)
173                 return false;
174
175         if (mac->opmode == NL80211_IFTYPE_STATION &&
176             mac->cnt_after_linked < 3)
177                 return false;
178
179         if (sta_entry->tids[tid].agg.agg_state == RTL_AGG_STOP)
180                 return true;
181
182         return false;
183 }
184
185 /*mac80211 Rate Control callbacks*/
186 static void rtl_tx_status(void *ppriv,
187                           struct ieee80211_supported_band *sband,
188                           struct ieee80211_sta *sta, void *priv_sta,
189                           struct sk_buff *skb)
190 {
191         struct rtl_priv *rtlpriv = ppriv;
192         struct rtl_mac *mac = rtl_mac(rtlpriv);
193         struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
194         __le16 fc = rtl_get_fc(skb);
195         struct rtl_sta_info *sta_entry;
196
197         if (!priv_sta || !ieee80211_is_data(fc))
198                 return;
199
200         if (rtl92e_is_special_data(mac->hw, skb, true))
201                 return;
202
203         if (is_multicast_ether_addr(ieee80211_get_DA(hdr)) ||
204             is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
205                 return;
206
207         if (sta) {
208                 /* Check if aggregation has to be enabled for this tid */
209                 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
210                 if ((sta->ht_cap.ht_supported) &&
211                     !(skb->protocol == cpu_to_be16(ETH_P_PAE))) {
212                         if (ieee80211_is_data_qos(fc)) {
213                                 u8 tid = rtl_get_tid(skb);
214                                 if (_rtl_tx_aggr_check(rtlpriv, sta_entry,
215                                                        tid)) {
216                                         sta_entry->tids[tid].agg.agg_state =
217                                                 RTL_AGG_PROGRESS;
218                                         ieee80211_start_tx_ba_session(sta, tid,
219                                                                       5000);
220                                 }
221                         }
222                 }
223         }
224 }
225
226 static void rtl_rate_init(void *ppriv,
227                           struct ieee80211_supported_band *sband,
228                           struct cfg80211_chan_def *chandef,
229                           struct ieee80211_sta *sta, void *priv_sta)
230 {
231 }
232
233 static void *rtl_rate_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
234 {
235         struct rtl_priv *rtlpriv = rtl_priv(hw);
236         return rtlpriv;
237 }
238
239 static void rtl_rate_free(void *rtlpriv)
240 {
241         return;
242 }
243
244 static void *rtl_rate_alloc_sta(void *ppriv,
245                                 struct ieee80211_sta *sta, gfp_t gfp)
246 {
247         struct rtl_priv *rtlpriv = ppriv;
248         struct rtl_rate_priv *rate_priv;
249
250         rate_priv = kzalloc(sizeof(*rate_priv), gfp);
251         if (!rate_priv) {
252                 RT_TRACE(COMP_ERR, DBG_EMERG,
253                          ("Unable to allocate private rc structure\n"));
254                 return NULL;
255         }
256
257         rtlpriv->rate_priv = rate_priv;
258
259         return rate_priv;
260 }
261
262 static void rtl_rate_free_sta(void *rtlpriv,
263                               struct ieee80211_sta *sta, void *priv_sta)
264 {
265         struct rtl_rate_priv *rate_priv = priv_sta;
266         kfree(rate_priv);
267 }
268
269 static struct rate_control_ops rtl_rate_ops = {
270         .name = "rtl_rc_92e",
271         .alloc = rtl_rate_alloc,
272         .free = rtl_rate_free,
273         .alloc_sta = rtl_rate_alloc_sta,
274         .free_sta = rtl_rate_free_sta,
275         .rate_init = rtl_rate_init,
276         .tx_status = rtl_tx_status,
277         .get_rate = rtl_get_rate,
278 };
279
280 int rtl92e_rate_control_register(void)
281 {
282         return ieee80211_rate_control_register(&rtl_rate_ops);
283 }
284
285 void rtl92e_rate_control_unregister(void)
286 {
287         ieee80211_rate_control_unregister(&rtl_rate_ops);
288 }