GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / staging / rtlwifi / core.c
1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2009-2012  Realtek Corporation.
5  *
6  * Contact Information:
7  * wlanfae <wlanfae@realtek.com>
8  * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
9  * Hsinchu 300, Taiwan.
10  *
11  * Larry Finger <Larry.Finger@lwfinger.net>
12  *
13  *****************************************************************************/
14
15 #include "wifi.h"
16 #include "core.h"
17 #include "cam.h"
18 #include "base.h"
19 #include "ps.h"
20 #include "pwrseqcmd.h"
21
22 #include "btcoexist/rtl_btc.h"
23 #include <linux/firmware.h>
24 #include <linux/export.h>
25 #include <net/cfg80211.h>
26
27 u8 channel5g[CHANNEL_MAX_NUMBER_5G] = {
28         36, 38, 40, 42, 44, 46, 48,             /* Band 1 */
29         52, 54, 56, 58, 60, 62, 64,             /* Band 2 */
30         100, 102, 104, 106, 108, 110, 112,      /* Band 3 */
31         116, 118, 120, 122, 124, 126, 128,      /* Band 3 */
32         132, 134, 136, 138, 140, 142, 144,      /* Band 3 */
33         149, 151, 153, 155, 157, 159, 161,      /* Band 4 */
34         165, 167, 169, 171, 173, 175, 177       /* Band 4 */
35 };
36
37 u8 channel5g_80m[CHANNEL_MAX_NUMBER_5G_80M] = {
38         42, 58, 106, 122, 138, 155, 171
39 };
40
41 static void rtl_fw_do_work(const struct firmware *firmware, void *context,
42                            bool is_wow)
43 {
44         struct ieee80211_hw *hw = context;
45         struct rtl_priv *rtlpriv = rtl_priv(hw);
46         int err;
47
48         RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
49                  "Firmware callback routine entered!\n");
50         complete(&rtlpriv->firmware_loading_complete);
51         if (!firmware) {
52                 if (rtlpriv->cfg->alt_fw_name) {
53                         err = reject_firmware(&firmware,
54                                                rtlpriv->cfg->alt_fw_name,
55                                                rtlpriv->io.dev);
56                         pr_info("Loading alternative firmware %s\n",
57                                 rtlpriv->cfg->alt_fw_name);
58                         if (!err)
59                                 goto found_alt;
60                 }
61                 pr_err("Selected firmware is not available\n");
62                 rtlpriv->max_fw_size = 0;
63                 return;
64         }
65 found_alt:
66         if (firmware->size > rtlpriv->max_fw_size) {
67                 pr_err("Firmware is too big!\n");
68                 release_firmware(firmware);
69                 return;
70         }
71         if (!is_wow) {
72                 memcpy(rtlpriv->rtlhal.pfirmware, firmware->data,
73                        firmware->size);
74                 rtlpriv->rtlhal.fwsize = firmware->size;
75         } else {
76                 memcpy(rtlpriv->rtlhal.wowlan_firmware, firmware->data,
77                        firmware->size);
78                 rtlpriv->rtlhal.wowlan_fwsize = firmware->size;
79         }
80         rtlpriv->rtlhal.fwsize = firmware->size;
81         release_firmware(firmware);
82 }
83
84 void rtl_fw_cb(const struct firmware *firmware, void *context)
85 {
86         rtl_fw_do_work(firmware, context, false);
87 }
88
89 void rtl_wowlan_fw_cb(const struct firmware *firmware, void *context)
90 {
91         rtl_fw_do_work(firmware, context, true);
92 }
93
94 /*mutex for start & stop is must here. */
95 static int rtl_op_start(struct ieee80211_hw *hw)
96 {
97         int err = 0;
98         struct rtl_priv *rtlpriv = rtl_priv(hw);
99         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
100
101         if (!is_hal_stop(rtlhal))
102                 return 0;
103         if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
104                 return 0;
105         mutex_lock(&rtlpriv->locks.conf_mutex);
106         err = rtlpriv->intf_ops->adapter_start(hw);
107         if (!err)
108                 rtl_watch_dog_timer_callback(&rtlpriv->works.watchdog_timer);
109         mutex_unlock(&rtlpriv->locks.conf_mutex);
110         return err;
111 }
112
113 static void rtl_op_stop(struct ieee80211_hw *hw)
114 {
115         struct rtl_priv *rtlpriv = rtl_priv(hw);
116         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
117         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
118         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
119         bool support_remote_wakeup = false;
120
121         if (is_hal_stop(rtlhal))
122                 return;
123
124         rtlpriv->cfg->ops->get_hw_reg(hw, HAL_DEF_WOWLAN,
125                                       (u8 *)(&support_remote_wakeup));
126         /* here is must, because adhoc do stop and start,
127          * but stop with RFOFF may cause something wrong,
128          * like adhoc TP
129          */
130         if (unlikely(ppsc->rfpwr_state == ERFOFF))
131                 rtl_ips_nic_on(hw);
132
133         mutex_lock(&rtlpriv->locks.conf_mutex);
134         /* if wowlan supported, DON'T clear connected info */
135         if (!(support_remote_wakeup &&
136               rtlhal->enter_pnp_sleep)) {
137                 mac->link_state = MAC80211_NOLINK;
138                 eth_zero_addr(mac->bssid);
139                 mac->vendor = PEER_UNKNOWN;
140
141                 /* reset sec info */
142                 rtl_cam_reset_sec_info(hw);
143
144                 rtl_deinit_deferred_work(hw);
145         }
146         rtlpriv->intf_ops->adapter_stop(hw);
147
148         mutex_unlock(&rtlpriv->locks.conf_mutex);
149 }
150
151 static void rtl_op_tx(struct ieee80211_hw *hw,
152                       struct ieee80211_tx_control *control,
153                       struct sk_buff *skb)
154 {
155         struct rtl_priv *rtlpriv = rtl_priv(hw);
156         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
157         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
158         struct rtl_tcb_desc tcb_desc;
159
160         memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
161
162         if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
163                 goto err_free;
164
165         if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
166                 goto err_free;
167
168         if (!rtlpriv->intf_ops->waitq_insert(hw, control->sta, skb))
169                 rtlpriv->intf_ops->adapter_tx(hw, control->sta, skb, &tcb_desc);
170         return;
171
172 err_free:
173         dev_kfree_skb_any(skb);
174 }
175
176 static int rtl_op_add_interface(struct ieee80211_hw *hw,
177                                 struct ieee80211_vif *vif)
178 {
179         struct rtl_priv *rtlpriv = rtl_priv(hw);
180         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
181         int err = 0;
182         u8 retry_limit = 0x30;
183
184         if (mac->vif) {
185                 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
186                          "vif has been set!! mac->vif = 0x%p\n", mac->vif);
187                 return -EOPNOTSUPP;
188         }
189
190         vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
191
192         rtl_ips_nic_on(hw);
193
194         mutex_lock(&rtlpriv->locks.conf_mutex);
195         switch (ieee80211_vif_type_p2p(vif)) {
196         case NL80211_IFTYPE_P2P_CLIENT:
197                 mac->p2p = P2P_ROLE_CLIENT;
198                 /*fall through*/
199         case NL80211_IFTYPE_STATION:
200                 if (mac->beacon_enabled == 1) {
201                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
202                                  "NL80211_IFTYPE_STATION\n");
203                         mac->beacon_enabled = 0;
204                         rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
205                                         rtlpriv->cfg->maps[RTL_IBSS_INT_MASKS]);
206                 }
207                 break;
208         case NL80211_IFTYPE_ADHOC:
209                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
210                          "NL80211_IFTYPE_ADHOC\n");
211
212                 mac->link_state = MAC80211_LINKED;
213                 rtlpriv->cfg->ops->set_bcn_reg(hw);
214                 if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
215                         mac->basic_rates = 0xfff;
216                 else
217                         mac->basic_rates = 0xff0;
218                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
219                                 (u8 *)(&mac->basic_rates));
220
221                 retry_limit = 0x07;
222                 break;
223         case NL80211_IFTYPE_P2P_GO:
224                 mac->p2p = P2P_ROLE_GO;
225                 /*fall through*/
226         case NL80211_IFTYPE_AP:
227                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
228                          "NL80211_IFTYPE_AP\n");
229
230                 mac->link_state = MAC80211_LINKED;
231                 rtlpriv->cfg->ops->set_bcn_reg(hw);
232                 if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
233                         mac->basic_rates = 0xfff;
234                 else
235                         mac->basic_rates = 0xff0;
236                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
237                                               (u8 *)(&mac->basic_rates));
238
239                 retry_limit = 0x07;
240                 break;
241         case NL80211_IFTYPE_MESH_POINT:
242                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
243                          "NL80211_IFTYPE_MESH_POINT\n");
244
245                 mac->link_state = MAC80211_LINKED;
246                 rtlpriv->cfg->ops->set_bcn_reg(hw);
247                 if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
248                         mac->basic_rates = 0xfff;
249                 else
250                         mac->basic_rates = 0xff0;
251                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
252                                 (u8 *)(&mac->basic_rates));
253
254                 retry_limit = 0x07;
255                 break;
256         default:
257                 pr_err("operation mode %d is not supported!\n",
258                        vif->type);
259                 err = -EOPNOTSUPP;
260                 goto out;
261         }
262
263         if (mac->p2p) {
264                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
265                          "p2p role %x\n", vif->type);
266                 mac->basic_rates = 0xff0;/*disable cck rate for p2p*/
267                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
268                                 (u8 *)(&mac->basic_rates));
269         }
270         mac->vif = vif;
271         mac->opmode = vif->type;
272         rtlpriv->cfg->ops->set_network_type(hw, vif->type);
273         memcpy(mac->mac_addr, vif->addr, ETH_ALEN);
274         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_ETHER_ADDR, mac->mac_addr);
275
276         mac->retry_long = retry_limit;
277         mac->retry_short = retry_limit;
278         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RETRY_LIMIT,
279                         (u8 *)(&retry_limit));
280 out:
281         mutex_unlock(&rtlpriv->locks.conf_mutex);
282         return err;
283 }
284
285 static void rtl_op_remove_interface(struct ieee80211_hw *hw,
286                                     struct ieee80211_vif *vif)
287 {
288         struct rtl_priv *rtlpriv = rtl_priv(hw);
289         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
290
291         mutex_lock(&rtlpriv->locks.conf_mutex);
292
293         /* Free beacon resources */
294         if (vif->type == NL80211_IFTYPE_AP ||
295             vif->type == NL80211_IFTYPE_ADHOC ||
296             vif->type == NL80211_IFTYPE_MESH_POINT) {
297                 if (mac->beacon_enabled == 1) {
298                         mac->beacon_enabled = 0;
299                         rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
300                                         rtlpriv->cfg->maps[RTL_IBSS_INT_MASKS]);
301                 }
302         }
303
304         /*
305          *Note: We assume NL80211_IFTYPE_UNSPECIFIED as
306          *NO LINK for our hardware.
307          */
308         mac->p2p = 0;
309         mac->vif = NULL;
310         mac->link_state = MAC80211_NOLINK;
311         eth_zero_addr(mac->bssid);
312         mac->vendor = PEER_UNKNOWN;
313         mac->opmode = NL80211_IFTYPE_UNSPECIFIED;
314         rtlpriv->cfg->ops->set_network_type(hw, mac->opmode);
315
316         mutex_unlock(&rtlpriv->locks.conf_mutex);
317 }
318
319 static int rtl_op_change_interface(struct ieee80211_hw *hw,
320                                    struct ieee80211_vif *vif,
321                                    enum nl80211_iftype new_type, bool p2p)
322 {
323         struct rtl_priv *rtlpriv = rtl_priv(hw);
324         int ret;
325
326         rtl_op_remove_interface(hw, vif);
327
328         vif->type = new_type;
329         vif->p2p = p2p;
330         ret = rtl_op_add_interface(hw, vif);
331         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
332                  "p2p  %x\n", p2p);
333         return ret;
334 }
335
336 #ifdef CONFIG_PM
337 static u16 crc16_ccitt(u8 data, u16 crc)
338 {
339         u8 shift_in, data_bit, crc_bit11, crc_bit4, crc_bit15;
340         u8 i;
341         u16 result;
342
343         for (i = 0; i < 8; i++) {
344                 crc_bit15 = ((crc & BIT(15)) ? 1 : 0);
345                 data_bit  = (data & (BIT(0) << i) ? 1 : 0);
346                 shift_in = crc_bit15 ^ data_bit;
347
348                 result = crc << 1;
349                 if (shift_in == 0)
350                         result &= (~BIT(0));
351                 else
352                         result |= BIT(0);
353
354                 crc_bit11 = ((crc & BIT(11)) ? 1 : 0) ^ shift_in;
355                 if (crc_bit11 == 0)
356                         result &= (~BIT(12));
357                 else
358                         result |= BIT(12);
359
360                 crc_bit4 = ((crc & BIT(4)) ? 1 : 0) ^ shift_in;
361                 if (crc_bit4 == 0)
362                         result &= (~BIT(5));
363                 else
364                         result |= BIT(5);
365
366                 crc = result;
367         }
368
369         return crc;
370 }
371
372 static u16 _calculate_wol_pattern_crc(u8 *pattern, u16 len)
373 {
374         u16 crc = 0xffff;
375         u32 i;
376
377         for (i = 0; i < len; i++)
378                 crc = crc16_ccitt(pattern[i], crc);
379
380         crc = ~crc;
381
382         return crc;
383 }
384
385 static void _rtl_add_wowlan_patterns(struct ieee80211_hw *hw,
386                                      struct cfg80211_wowlan *wow)
387 {
388         struct rtl_priv *rtlpriv = rtl_priv(hw);
389         struct rtl_mac *mac = &rtlpriv->mac80211;
390         struct cfg80211_pkt_pattern *patterns = wow->patterns;
391         struct rtl_wow_pattern rtl_pattern;
392         const u8 *pattern_os, *mask_os;
393         u8 mask[MAX_WOL_BIT_MASK_SIZE] = {0};
394         u8 content[MAX_WOL_PATTERN_SIZE] = {0};
395         u8 broadcast_addr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
396         u8 multicast_addr1[2] = {0x33, 0x33};
397         u8 multicast_addr2[3] = {0x01, 0x00, 0x5e};
398         u8 i, mask_len;
399         u16 j, len;
400
401         for (i = 0; i < wow->n_patterns; i++) {
402                 memset(&rtl_pattern, 0, sizeof(struct rtl_wow_pattern));
403                 memset(mask, 0, MAX_WOL_BIT_MASK_SIZE);
404                 if (patterns[i].pattern_len < 0 ||
405                     patterns[i].pattern_len > MAX_WOL_PATTERN_SIZE) {
406                         RT_TRACE(rtlpriv, COMP_POWER, DBG_WARNING,
407                                  "Pattern[%d] is too long\n", i);
408                         continue;
409                 }
410                 pattern_os = patterns[i].pattern;
411                 mask_len = DIV_ROUND_UP(patterns[i].pattern_len, 8);
412                 mask_os = patterns[i].mask;
413                 RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
414                               "pattern content\n", pattern_os,
415                                patterns[i].pattern_len);
416                 RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
417                               "mask content\n", mask_os, mask_len);
418                 /* 1. unicast? multicast? or broadcast? */
419                 if (memcmp(pattern_os, broadcast_addr, 6) == 0)
420                         rtl_pattern.type = BROADCAST_PATTERN;
421                 else if (memcmp(pattern_os, multicast_addr1, 2) == 0 ||
422                          memcmp(pattern_os, multicast_addr2, 3) == 0)
423                         rtl_pattern.type = MULTICAST_PATTERN;
424                 else if  (memcmp(pattern_os, mac->mac_addr, 6) == 0)
425                         rtl_pattern.type = UNICAST_PATTERN;
426                 else
427                         rtl_pattern.type = UNKNOWN_TYPE;
428
429                 /* 2. translate mask_from_os to mask_for_hw */
430
431 /******************************************************************************
432  * pattern from OS uses 'ethenet frame', like this:
433
434                    |    6   |    6   |   2  |     20    |  Variable  |  4  |
435                    |--------+--------+------+-----------+------------+-----|
436                    |    802.3 Mac Header    | IP Header | TCP Packet | FCS |
437                    |   DA   |   SA   | Type |
438
439  * BUT, packet catched by our HW is in '802.11 frame', begin from LLC,
440
441         |     24 or 30      |    6   |   2  |     20    |  Variable  |  4  |
442         |-------------------+--------+------+-----------+------------+-----|
443         | 802.11 MAC Header |       LLC     | IP Header | TCP Packet | FCS |
444                             | Others | Tpye |
445
446  * Therefore, we need translate mask_from_OS to mask_to_hw.
447  * We should left-shift mask by 6 bits, then set the new bit[0~5] = 0,
448  * because new mask[0~5] means 'SA', but our HW packet begins from LLC,
449  * bit[0~5] corresponds to first 6 Bytes in LLC, they just don't match.
450  ******************************************************************************/
451
452                 /* Shift 6 bits */
453                 for (j = 0; j < mask_len - 1; j++) {
454                         mask[j] = mask_os[j] >> 6;
455                         mask[j] |= (mask_os[j + 1] & 0x3F) << 2;
456                 }
457                 mask[j] = (mask_os[j] >> 6) & 0x3F;
458                 /* Set bit 0-5 to zero */
459                 mask[0] &= 0xC0;
460
461                 RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
462                               "mask to hw\n", mask, mask_len);
463                 for (j = 0; j < (MAX_WOL_BIT_MASK_SIZE + 1) / 4; j++) {
464                         rtl_pattern.mask[j] = mask[j * 4];
465                         rtl_pattern.mask[j] |= (mask[j * 4 + 1] << 8);
466                         rtl_pattern.mask[j] |= (mask[j * 4 + 2] << 16);
467                         rtl_pattern.mask[j] |= (mask[j * 4 + 3] << 24);
468                 }
469
470                 /* To get the wake up pattern from the mask.
471                  * We do not count first 12 bits which means
472                  * DA[6] and SA[6] in the pattern to match HW design.
473                  */
474                 len = 0;
475                 for (j = 12; j < patterns[i].pattern_len; j++) {
476                         if ((mask_os[j / 8] >> (j % 8)) & 0x01) {
477                                 content[len] = pattern_os[j];
478                                 len++;
479                         }
480                 }
481
482                 RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
483                               "pattern to hw\n", content, len);
484                 /* 3. calculate crc */
485                 rtl_pattern.crc = _calculate_wol_pattern_crc(content, len);
486                 RT_TRACE(rtlpriv, COMP_POWER, DBG_TRACE,
487                          "CRC_Remainder = 0x%x\n", rtl_pattern.crc);
488
489                 /* 4. write crc & mask_for_hw to hw */
490                 rtlpriv->cfg->ops->add_wowlan_pattern(hw, &rtl_pattern, i);
491         }
492         rtl_write_byte(rtlpriv, 0x698, wow->n_patterns);
493 }
494
495 static int rtl_op_suspend(struct ieee80211_hw *hw,
496                           struct cfg80211_wowlan *wow)
497 {
498         struct rtl_priv *rtlpriv = rtl_priv(hw);
499         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
500         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
501
502         RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG, "\n");
503         if (WARN_ON(!wow))
504                 return -EINVAL;
505
506         /* to resolve s4 can not wake up*/
507         rtlhal->last_suspend_sec = ktime_get_real_seconds();
508
509         if ((ppsc->wo_wlan_mode & WAKE_ON_PATTERN_MATCH) && wow->n_patterns)
510                 _rtl_add_wowlan_patterns(hw, wow);
511
512         rtlhal->driver_is_goingto_unload = true;
513         rtlhal->enter_pnp_sleep = true;
514
515         rtl_lps_leave(hw);
516         rtl_op_stop(hw);
517         device_set_wakeup_enable(wiphy_dev(hw->wiphy), true);
518         return 0;
519 }
520
521 static int rtl_op_resume(struct ieee80211_hw *hw)
522 {
523         struct rtl_priv *rtlpriv = rtl_priv(hw);
524         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
525         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
526
527         RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG, "\n");
528         rtlhal->driver_is_goingto_unload = false;
529         rtlhal->enter_pnp_sleep = false;
530         rtlhal->wake_from_pnp_sleep = true;
531
532         /* to resovle s4 can not wake up*/
533         if (ktime_get_real_seconds() - rtlhal->last_suspend_sec < 5)
534                 return -1;
535
536         rtl_op_start(hw);
537         device_set_wakeup_enable(wiphy_dev(hw->wiphy), false);
538         ieee80211_resume_disconnect(mac->vif);
539         rtlhal->wake_from_pnp_sleep = false;
540         return 0;
541 }
542 #endif
543
544 static int rtl_op_config(struct ieee80211_hw *hw, u32 changed)
545 {
546         struct rtl_priv *rtlpriv = rtl_priv(hw);
547         struct rtl_phy *rtlphy = &rtlpriv->phy;
548         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
549         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
550         struct ieee80211_conf *conf = &hw->conf;
551
552         if (mac->skip_scan)
553                 return 1;
554
555         mutex_lock(&rtlpriv->locks.conf_mutex);
556         if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {  /* BIT(2)*/
557                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
558                          "IEEE80211_CONF_CHANGE_LISTEN_INTERVAL\n");
559         }
560
561         /*For IPS */
562         if (changed & IEEE80211_CONF_CHANGE_IDLE) {
563                 if (hw->conf.flags & IEEE80211_CONF_IDLE)
564                         rtl_ips_nic_off(hw);
565                 else
566                         rtl_ips_nic_on(hw);
567         } else {
568                 /*
569                  *although rfoff may not cause by ips, but we will
570                  *check the reason in set_rf_power_state function
571                  */
572                 if (unlikely(ppsc->rfpwr_state == ERFOFF))
573                         rtl_ips_nic_on(hw);
574         }
575
576         /*For LPS */
577         if ((changed & IEEE80211_CONF_CHANGE_PS) &&
578             rtlpriv->psc.swctrl_lps && !rtlpriv->psc.fwctrl_lps) {
579                 cancel_delayed_work(&rtlpriv->works.ps_work);
580                 cancel_delayed_work(&rtlpriv->works.ps_rfon_wq);
581                 if (conf->flags & IEEE80211_CONF_PS) {
582                         rtlpriv->psc.sw_ps_enabled = true;
583                         /* sleep here is must, or we may recv the beacon and
584                          * cause mac80211 into wrong ps state, this will cause
585                          * power save nullfunc send fail, and further cause
586                          * pkt loss, So sleep must quickly but not immediately
587                          * because that will cause nullfunc send by mac80211
588                          * fail, and cause pkt loss, we have tested that 5mA
589                          * works very well
590                          */
591                         if (!rtlpriv->psc.multi_buffered)
592                                 queue_delayed_work(rtlpriv->works.rtl_wq,
593                                                    &rtlpriv->works.ps_work,
594                                                    MSECS(5));
595                 } else {
596                         rtl_swlps_rf_awake(hw);
597                         rtlpriv->psc.sw_ps_enabled = false;
598                 }
599         }
600
601         if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
602                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
603                          "IEEE80211_CONF_CHANGE_RETRY_LIMITS %x\n",
604                          hw->conf.long_frame_max_tx_count);
605                 /* brought up everything changes (changed == ~0) indicates first
606                  * open, so use our default value instead of that of wiphy.
607                  */
608                 if (changed != ~0) {
609                         mac->retry_long = hw->conf.long_frame_max_tx_count;
610                         mac->retry_short = hw->conf.long_frame_max_tx_count;
611                         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RETRY_LIMIT,
612                                 (u8 *)(&hw->conf.long_frame_max_tx_count));
613                 }
614         }
615
616         if (changed & IEEE80211_CONF_CHANGE_CHANNEL &&
617             !rtlpriv->proximity.proxim_on) {
618                 struct ieee80211_channel *channel = hw->conf.chandef.chan;
619                 enum nl80211_chan_width width = hw->conf.chandef.width;
620                 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
621                 u8 wide_chan = (u8)channel->hw_value;
622
623                 /* channel_type is for 20&40M */
624                 if (width < NL80211_CHAN_WIDTH_80)
625                         channel_type =
626                                 cfg80211_get_chandef_type(&hw->conf.chandef);
627                 if (mac->act_scanning)
628                         mac->n_channels++;
629
630                 if (rtlpriv->dm.supp_phymode_switch &&
631                     mac->link_state < MAC80211_LINKED &&
632                     !mac->act_scanning) {
633                         if (rtlpriv->cfg->ops->chk_switch_dmdp)
634                                 rtlpriv->cfg->ops->chk_switch_dmdp(hw);
635                 }
636
637                 /*
638                  *because we should back channel to
639                  *current_network.chan in in scanning,
640                  *So if set_chan == current_network.chan
641                  *we should set it.
642                  *because mac80211 tell us wrong bw40
643                  *info for cisco1253 bw20, so we modify
644                  *it here based on UPPER & LOWER
645                  */
646
647                 if (width >= NL80211_CHAN_WIDTH_80) {
648                         if (width == NL80211_CHAN_WIDTH_80) {
649                                 u32 center = hw->conf.chandef.center_freq1;
650                                 u32 primary =
651                                 (u32)hw->conf.chandef.chan->center_freq;
652
653                                 rtlphy->current_chan_bw =
654                                         HT_CHANNEL_WIDTH_80;
655                                 mac->bw_80 = true;
656                                 mac->bw_40 = true;
657                                 if (center > primary) {
658                                         mac->cur_80_prime_sc =
659                                         PRIME_CHNL_OFFSET_LOWER;
660                                         if (center - primary == 10) {
661                                                 mac->cur_40_prime_sc =
662                                                 PRIME_CHNL_OFFSET_UPPER;
663
664                                                 wide_chan += 2;
665                                         } else if (center - primary == 30) {
666                                                 mac->cur_40_prime_sc =
667                                                 PRIME_CHNL_OFFSET_LOWER;
668
669                                                 wide_chan += 6;
670                                         }
671                                 } else {
672                                         mac->cur_80_prime_sc =
673                                         PRIME_CHNL_OFFSET_UPPER;
674                                         if (primary - center == 10) {
675                                                 mac->cur_40_prime_sc =
676                                                 PRIME_CHNL_OFFSET_LOWER;
677
678                                                 wide_chan -= 2;
679                                         } else if (primary - center == 30) {
680                                                 mac->cur_40_prime_sc =
681                                                 PRIME_CHNL_OFFSET_UPPER;
682
683                                                 wide_chan -= 6;
684                                         }
685                                 }
686                         }
687                 } else {
688                         switch (channel_type) {
689                         case NL80211_CHAN_HT20:
690                         case NL80211_CHAN_NO_HT:
691                                         /* SC */
692                                         mac->cur_40_prime_sc =
693                                                 PRIME_CHNL_OFFSET_DONT_CARE;
694                                         rtlphy->current_chan_bw =
695                                                 HT_CHANNEL_WIDTH_20;
696                                         mac->bw_40 = false;
697                                         mac->bw_80 = false;
698                                         break;
699                         case NL80211_CHAN_HT40MINUS:
700                                         /* SC */
701                                         mac->cur_40_prime_sc =
702                                                 PRIME_CHNL_OFFSET_UPPER;
703                                         rtlphy->current_chan_bw =
704                                                 HT_CHANNEL_WIDTH_20_40;
705                                         mac->bw_40 = true;
706                                         mac->bw_80 = false;
707
708                                         /*wide channel */
709                                         wide_chan -= 2;
710
711                                         break;
712                         case NL80211_CHAN_HT40PLUS:
713                                         /* SC */
714                                         mac->cur_40_prime_sc =
715                                                 PRIME_CHNL_OFFSET_LOWER;
716                                         rtlphy->current_chan_bw =
717                                                 HT_CHANNEL_WIDTH_20_40;
718                                         mac->bw_40 = true;
719                                         mac->bw_80 = false;
720
721                                         /*wide channel */
722                                         wide_chan += 2;
723
724                                         break;
725                         default:
726                                         mac->bw_40 = false;
727                                         mac->bw_80 = false;
728                                         pr_err("switch case %#x not processed\n",
729                                                channel_type);
730                                         break;
731                         }
732                 }
733
734                 if (wide_chan <= 0)
735                         wide_chan = 1;
736
737                 /* In scanning, when before we offchannel we may send a ps=1
738                  * null to AP, and then we may send a ps = 0 null to AP quickly,
739                  * but first null may have caused AP to put lots of packet to
740                  * hw tx buffer. These packets must be tx'd before we go off
741                  * channel so we must delay more time to let AP flush these
742                  * packets before going offchannel, or dis-association or
743                  * delete BA will be caused by AP
744                  */
745                 if (rtlpriv->mac80211.offchan_delay) {
746                         rtlpriv->mac80211.offchan_delay = false;
747                         mdelay(50);
748                 }
749
750                 rtlphy->current_channel = wide_chan;
751
752                 rtlpriv->cfg->ops->switch_channel(hw);
753                 rtlpriv->cfg->ops->set_channel_access(hw);
754                 rtlpriv->cfg->ops->set_bw_mode(hw, channel_type);
755         }
756
757         mutex_unlock(&rtlpriv->locks.conf_mutex);
758
759         return 0;
760 }
761
762 static void rtl_op_configure_filter(struct ieee80211_hw *hw,
763                                     unsigned int changed_flags,
764                                     unsigned int *new_flags, u64 multicast)
765 {
766         bool update_rcr = false;
767         struct rtl_priv *rtlpriv = rtl_priv(hw);
768         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
769
770         *new_flags &= RTL_SUPPORTED_FILTERS;
771         if (changed_flags == 0)
772                 return;
773
774         /*TODO: we disable broadcase now, so enable here */
775         if (changed_flags & FIF_ALLMULTI) {
776                 if (*new_flags & FIF_ALLMULTI) {
777                         mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_AM] |
778                             rtlpriv->cfg->maps[MAC_RCR_AB];
779                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
780                                  "Enable receive multicast frame\n");
781                 } else {
782                         mac->rx_conf &= ~(rtlpriv->cfg->maps[MAC_RCR_AM] |
783                                           rtlpriv->cfg->maps[MAC_RCR_AB]);
784                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
785                                  "Disable receive multicast frame\n");
786                 }
787                 update_rcr = true;
788         }
789
790         if (changed_flags & FIF_FCSFAIL) {
791                 if (*new_flags & FIF_FCSFAIL) {
792                         mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_ACRC32];
793                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
794                                  "Enable receive FCS error frame\n");
795                 } else {
796                         mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_ACRC32];
797                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
798                                  "Disable receive FCS error frame\n");
799                 }
800                 if (!update_rcr)
801                         update_rcr = true;
802         }
803
804         /* if ssid not set to hw don't check bssid
805          * here just used for linked scanning, & linked
806          * and nolink check bssid is set in set network_type
807          */
808         if (changed_flags & FIF_BCN_PRBRESP_PROMISC &&
809             mac->link_state >= MAC80211_LINKED) {
810                 if (mac->opmode != NL80211_IFTYPE_AP &&
811                     mac->opmode != NL80211_IFTYPE_MESH_POINT) {
812                         if (*new_flags & FIF_BCN_PRBRESP_PROMISC)
813                                 rtlpriv->cfg->ops->set_chk_bssid(hw, false);
814                         else
815                                 rtlpriv->cfg->ops->set_chk_bssid(hw, true);
816                         if (update_rcr)
817                                 update_rcr = false;
818                 }
819         }
820
821         if (changed_flags & FIF_CONTROL) {
822                 if (*new_flags & FIF_CONTROL) {
823                         mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_ACF];
824
825                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
826                                  "Enable receive control frame.\n");
827                 } else {
828                         mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_ACF];
829                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
830                                  "Disable receive control frame.\n");
831                 }
832                 if (!update_rcr)
833                         update_rcr = true;
834         }
835
836         if (changed_flags & FIF_OTHER_BSS) {
837                 if (*new_flags & FIF_OTHER_BSS) {
838                         mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_AAP];
839                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
840                                  "Enable receive other BSS's frame.\n");
841                 } else {
842                         mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_AAP];
843                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
844                                  "Disable receive other BSS's frame.\n");
845                 }
846                 if (!update_rcr)
847                         update_rcr = true;
848         }
849
850         if (update_rcr)
851                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RCR,
852                                               (u8 *)(&mac->rx_conf));
853 }
854
855 static int rtl_op_sta_add(struct ieee80211_hw *hw,
856                           struct ieee80211_vif *vif,
857                           struct ieee80211_sta *sta)
858 {
859         struct rtl_priv *rtlpriv = rtl_priv(hw);
860         struct rtl_phy *rtlphy = &rtlpriv->phy;
861         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
862         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
863         struct rtl_sta_info *sta_entry;
864
865         if (sta) {
866                 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
867                 spin_lock_bh(&rtlpriv->locks.entry_list_lock);
868                 list_add_tail(&sta_entry->list, &rtlpriv->entry_list);
869                 spin_unlock_bh(&rtlpriv->locks.entry_list_lock);
870                 if (rtlhal->current_bandtype == BAND_ON_2_4G) {
871                         sta_entry->wireless_mode = WIRELESS_MODE_G;
872                         if (sta->supp_rates[0] <= 0xf)
873                                 sta_entry->wireless_mode = WIRELESS_MODE_B;
874                         if (sta->ht_cap.ht_supported)
875                                 sta_entry->wireless_mode = WIRELESS_MODE_N_24G;
876
877                         if (vif->type == NL80211_IFTYPE_ADHOC)
878                                 sta_entry->wireless_mode = WIRELESS_MODE_G;
879                 } else if (rtlhal->current_bandtype == BAND_ON_5G) {
880                         sta_entry->wireless_mode = WIRELESS_MODE_A;
881                         if (sta->ht_cap.ht_supported)
882                                 sta_entry->wireless_mode = WIRELESS_MODE_N_5G;
883                         if (sta->vht_cap.vht_supported)
884                                 sta_entry->wireless_mode = WIRELESS_MODE_AC_5G;
885
886                         if (vif->type == NL80211_IFTYPE_ADHOC)
887                                 sta_entry->wireless_mode = WIRELESS_MODE_A;
888                 }
889                 /*disable cck rate for p2p*/
890                 if (mac->p2p)
891                         sta->supp_rates[0] &= 0xfffffff0;
892
893                 if (sta->ht_cap.ht_supported) {
894                         if (sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40)
895                                 rtlphy->max_ht_chan_bw = HT_CHANNEL_WIDTH_20_40;
896                         else
897                                 rtlphy->max_ht_chan_bw = HT_CHANNEL_WIDTH_20;
898                 }
899
900                 if (sta->vht_cap.vht_supported)
901                         rtlphy->max_vht_chan_bw = HT_CHANNEL_WIDTH_80;
902
903                 memcpy(sta_entry->mac_addr, sta->addr, ETH_ALEN);
904                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
905                          "Add sta addr is %pM\n", sta->addr);
906                 rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0, true);
907
908                 if (rtlpriv->phydm.ops)
909                         rtlpriv->phydm.ops->phydm_add_sta(rtlpriv, sta);
910         }
911
912         return 0;
913 }
914
915 static int rtl_op_sta_remove(struct ieee80211_hw *hw,
916                              struct ieee80211_vif *vif,
917                              struct ieee80211_sta *sta)
918 {
919         struct rtl_priv *rtlpriv = rtl_priv(hw);
920         struct rtl_sta_info *sta_entry;
921
922         if (sta) {
923                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
924                          "Remove sta addr is %pM\n", sta->addr);
925
926                 if (rtlpriv->phydm.ops)
927                         rtlpriv->phydm.ops->phydm_del_sta(rtlpriv, sta);
928
929                 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
930                 sta_entry->wireless_mode = 0;
931                 sta_entry->ratr_index = 0;
932                 spin_lock_bh(&rtlpriv->locks.entry_list_lock);
933                 list_del(&sta_entry->list);
934                 spin_unlock_bh(&rtlpriv->locks.entry_list_lock);
935         }
936         return 0;
937 }
938
939 static int _rtl_get_hal_qnum(u16 queue)
940 {
941         int qnum;
942
943         switch (queue) {
944         case 0:
945                 qnum = AC3_VO;
946                 break;
947         case 1:
948                 qnum = AC2_VI;
949                 break;
950         case 2:
951                 qnum = AC0_BE;
952                 break;
953         case 3:
954                 qnum = AC1_BK;
955                 break;
956         default:
957                 qnum = AC0_BE;
958                 break;
959         }
960         return qnum;
961 }
962
963 static void rtl_op_sta_statistics(struct ieee80211_hw *hw,
964                                   struct ieee80211_vif *vif,
965                                   struct ieee80211_sta *sta,
966                                   struct station_info *sinfo)
967 {
968         /* nothing filled by driver, so mac80211 will update all info */
969         sinfo->filled = 0;
970 }
971
972 static int rtl_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
973 {
974         return -EOPNOTSUPP;
975 }
976
977 /*
978  *for mac80211 VO = 0, VI = 1, BE = 2, BK = 3
979  *for rtl819x  BE = 0, BK = 1, VI = 2, VO = 3
980  */
981 static int rtl_op_conf_tx(struct ieee80211_hw *hw,
982                           struct ieee80211_vif *vif, u16 queue,
983                           const struct ieee80211_tx_queue_params *param)
984 {
985         struct rtl_priv *rtlpriv = rtl_priv(hw);
986         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
987         int aci;
988
989         if (queue >= AC_MAX) {
990                 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
991                          "queue number %d is incorrect!\n", queue);
992                 return -EINVAL;
993         }
994
995         aci = _rtl_get_hal_qnum(queue);
996         mac->ac[aci].aifs = param->aifs;
997         mac->ac[aci].cw_min = cpu_to_le16(param->cw_min);
998         mac->ac[aci].cw_max = cpu_to_le16(param->cw_max);
999         mac->ac[aci].tx_op = cpu_to_le16(param->txop);
1000         memcpy(&mac->edca_param[aci], param, sizeof(*param));
1001         rtlpriv->cfg->ops->set_qos(hw, aci);
1002         return 0;
1003 }
1004
1005 static void send_beacon_frame(struct ieee80211_hw *hw,
1006                               struct ieee80211_vif *vif)
1007 {
1008         struct rtl_priv *rtlpriv = rtl_priv(hw);
1009         struct sk_buff *skb = ieee80211_beacon_get(hw, vif);
1010         struct rtl_tcb_desc tcb_desc;
1011
1012         if (skb) {
1013                 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
1014                 rtlpriv->intf_ops->adapter_tx(hw, NULL, skb, &tcb_desc);
1015         }
1016 }
1017
1018 static void rtl_op_bss_info_changed(struct ieee80211_hw *hw,
1019                                     struct ieee80211_vif *vif,
1020                                     struct ieee80211_bss_conf *bss_conf,
1021                                     u32 changed)
1022 {
1023         struct rtl_priv *rtlpriv = rtl_priv(hw);
1024         struct rtl_phy *rtlphy = &rtlpriv->phy;
1025         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
1026         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1027         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1028
1029         mutex_lock(&rtlpriv->locks.conf_mutex);
1030         if (vif->type == NL80211_IFTYPE_ADHOC ||
1031             vif->type == NL80211_IFTYPE_AP ||
1032             vif->type == NL80211_IFTYPE_MESH_POINT) {
1033                 if (changed & BSS_CHANGED_BEACON ||
1034                     (changed & BSS_CHANGED_BEACON_ENABLED &&
1035                      bss_conf->enable_beacon)) {
1036                         if (mac->beacon_enabled == 0) {
1037                                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
1038                                          "BSS_CHANGED_BEACON_ENABLED\n");
1039
1040                                 /*start hw beacon interrupt. */
1041                                 /*rtlpriv->cfg->ops->set_bcn_reg(hw); */
1042                                 mac->beacon_enabled = 1;
1043                                 rtlpriv->cfg->ops->update_interrupt_mask(hw,
1044                                                 rtlpriv->cfg->maps
1045                                                 [RTL_IBSS_INT_MASKS], 0);
1046
1047                                 if (rtlpriv->cfg->ops->linked_set_reg)
1048                                         rtlpriv->cfg->ops->linked_set_reg(hw);
1049                                 send_beacon_frame(hw, vif);
1050                         }
1051                 }
1052                 if ((changed & BSS_CHANGED_BEACON_ENABLED &&
1053                      !bss_conf->enable_beacon)) {
1054                         if (mac->beacon_enabled == 1) {
1055                                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
1056                                          "ADHOC DISABLE BEACON\n");
1057
1058                                 mac->beacon_enabled = 0;
1059                                 rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
1060                                                 rtlpriv->cfg->maps
1061                                                 [RTL_IBSS_INT_MASKS]);
1062                         }
1063                 }
1064                 if (changed & BSS_CHANGED_BEACON_INT) {
1065                         RT_TRACE(rtlpriv, COMP_BEACON, DBG_TRACE,
1066                                  "BSS_CHANGED_BEACON_INT\n");
1067                         mac->beacon_interval = bss_conf->beacon_int;
1068                         rtlpriv->cfg->ops->set_bcn_intv(hw);
1069                 }
1070         }
1071
1072         /*TODO: reference to enum ieee80211_bss_change */
1073         if (changed & BSS_CHANGED_ASSOC) {
1074                 u8 mstatus;
1075
1076                 if (bss_conf->assoc) {
1077                         struct ieee80211_sta *sta = NULL;
1078                         u8 keep_alive = 10;
1079
1080                         mstatus = RT_MEDIA_CONNECT;
1081                         /* we should reset all sec info & cam
1082                          * before set cam after linked, we should not
1083                          * reset in disassoc, that will cause tkip->wep
1084                          * fail because some flag will be wrong
1085                          * reset sec info
1086                          */
1087                         rtl_cam_reset_sec_info(hw);
1088                         /* reset cam to fix wep fail issue
1089                          * when change from wpa to wep
1090                          */
1091                         rtl_cam_reset_all_entry(hw);
1092
1093                         mac->link_state = MAC80211_LINKED;
1094                         mac->cnt_after_linked = 0;
1095                         mac->assoc_id = bss_conf->aid;
1096                         memcpy(mac->bssid, bss_conf->bssid, ETH_ALEN);
1097
1098                         if (rtlpriv->cfg->ops->linked_set_reg)
1099                                 rtlpriv->cfg->ops->linked_set_reg(hw);
1100
1101                         rcu_read_lock();
1102                         sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
1103                         if (!sta) {
1104                                 rcu_read_unlock();
1105                                 goto out;
1106                         }
1107                         RT_TRACE(rtlpriv, COMP_EASY_CONCURRENT, DBG_LOUD,
1108                                  "send PS STATIC frame\n");
1109                         if (rtlpriv->dm.supp_phymode_switch) {
1110                                 if (sta->ht_cap.ht_supported)
1111                                         rtl_send_smps_action(hw, sta,
1112                                                              IEEE80211_SMPS_STATIC);
1113                         }
1114
1115                         if (rtlhal->current_bandtype == BAND_ON_5G) {
1116                                 mac->mode = WIRELESS_MODE_A;
1117                         } else {
1118                                 if (sta->supp_rates[0] <= 0xf)
1119                                         mac->mode = WIRELESS_MODE_B;
1120                                 else
1121                                         mac->mode = WIRELESS_MODE_G;
1122                         }
1123
1124                         if (sta->ht_cap.ht_supported) {
1125                                 if (rtlhal->current_bandtype == BAND_ON_2_4G)
1126                                         mac->mode = WIRELESS_MODE_N_24G;
1127                                 else
1128                                         mac->mode = WIRELESS_MODE_N_5G;
1129                         }
1130
1131                         if (sta->vht_cap.vht_supported) {
1132                                 if (rtlhal->current_bandtype == BAND_ON_5G)
1133                                         mac->mode = WIRELESS_MODE_AC_5G;
1134                                 else
1135                                         mac->mode = WIRELESS_MODE_AC_24G;
1136                         }
1137
1138                         if (vif->type == NL80211_IFTYPE_STATION)
1139                                 rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0,
1140                                                                    true);
1141                         rcu_read_unlock();
1142
1143                         /* to avoid AP Disassociation caused by inactivity */
1144                         rtlpriv->cfg->ops->set_hw_reg(hw,
1145                                                       HW_VAR_KEEP_ALIVE,
1146                                                       (u8 *)(&keep_alive));
1147
1148                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
1149                                  "BSS_CHANGED_ASSOC\n");
1150                 } else {
1151                         struct cfg80211_bss *bss = NULL;
1152
1153                         mstatus = RT_MEDIA_DISCONNECT;
1154
1155                         if (mac->link_state == MAC80211_LINKED)
1156                                 rtl_lps_leave(hw);
1157                         if (ppsc->p2p_ps_info.p2p_ps_mode > P2P_PS_NONE)
1158                                 rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
1159                         mac->link_state = MAC80211_NOLINK;
1160
1161                         bss = cfg80211_get_bss(hw->wiphy, NULL,
1162                                                (u8 *)mac->bssid, NULL, 0,
1163                                                IEEE80211_BSS_TYPE_ESS,
1164                                                IEEE80211_PRIVACY_OFF);
1165
1166                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
1167                                  "bssid = %x-%x-%x-%x-%x-%x\n",
1168                                  mac->bssid[0], mac->bssid[1],
1169                                  mac->bssid[2], mac->bssid[3],
1170                                  mac->bssid[4], mac->bssid[5]);
1171
1172                         if (bss) {
1173                                 cfg80211_unlink_bss(hw->wiphy, bss);
1174                                 cfg80211_put_bss(hw->wiphy, bss);
1175                                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
1176                                          "cfg80211_unlink !!\n");
1177                         }
1178
1179                         eth_zero_addr(mac->bssid);
1180                         mac->vendor = PEER_UNKNOWN;
1181                         mac->mode = 0;
1182
1183                         if (rtlpriv->dm.supp_phymode_switch) {
1184                                 if (rtlpriv->cfg->ops->chk_switch_dmdp)
1185                                         rtlpriv->cfg->ops->chk_switch_dmdp(hw);
1186                         }
1187                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
1188                                  "BSS_CHANGED_UN_ASSOC\n");
1189                 }
1190                 rtlpriv->cfg->ops->set_network_type(hw, vif->type);
1191                 /* For FW LPS:
1192                  * To tell firmware we have connected or disconnected
1193                  */
1194                 rtlpriv->cfg->ops->set_hw_reg(hw,
1195                                               HW_VAR_H2C_FW_JOINBSSRPT,
1196                                               (u8 *)(&mstatus));
1197                 ppsc->report_linked = (mstatus == RT_MEDIA_CONNECT) ?
1198                                       true : false;
1199
1200                 if (rtlpriv->cfg->ops->get_btc_status())
1201                         rtlpriv->btcoexist.btc_ops->btc_mediastatus_notify(
1202                                                         rtlpriv, mstatus);
1203         }
1204
1205         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1206                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1207                          "BSS_CHANGED_ERP_CTS_PROT\n");
1208                 mac->use_cts_protect = bss_conf->use_cts_prot;
1209         }
1210
1211         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1212                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
1213                          "BSS_CHANGED_ERP_PREAMBLE use short preamble:%x\n",
1214                           bss_conf->use_short_preamble);
1215
1216                 mac->short_preamble = bss_conf->use_short_preamble;
1217                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_ACK_PREAMBLE,
1218                                               (u8 *)(&mac->short_preamble));
1219         }
1220
1221         if (changed & BSS_CHANGED_ERP_SLOT) {
1222                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1223                          "BSS_CHANGED_ERP_SLOT\n");
1224
1225                 if (bss_conf->use_short_slot)
1226                         mac->slot_time = RTL_SLOT_TIME_9;
1227                 else
1228                         mac->slot_time = RTL_SLOT_TIME_20;
1229
1230                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SLOT_TIME,
1231                                               (u8 *)(&mac->slot_time));
1232         }
1233
1234         if (changed & BSS_CHANGED_HT) {
1235                 struct ieee80211_sta *sta = NULL;
1236
1237                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1238                          "BSS_CHANGED_HT\n");
1239
1240                 rcu_read_lock();
1241                 sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
1242                 if (sta) {
1243                         if (sta->ht_cap.ampdu_density >
1244                             mac->current_ampdu_density)
1245                                 mac->current_ampdu_density =
1246                                     sta->ht_cap.ampdu_density;
1247                         if (sta->ht_cap.ampdu_factor <
1248                             mac->current_ampdu_factor)
1249                                 mac->current_ampdu_factor =
1250                                     sta->ht_cap.ampdu_factor;
1251
1252                         if (sta->ht_cap.ht_supported) {
1253                                 if (sta->ht_cap.cap &
1254                                     IEEE80211_HT_CAP_SUP_WIDTH_20_40)
1255                                         rtlphy->max_ht_chan_bw =
1256                                                         HT_CHANNEL_WIDTH_20_40;
1257                                 else
1258                                         rtlphy->max_ht_chan_bw =
1259                                                         HT_CHANNEL_WIDTH_20;
1260                         }
1261                 }
1262                 rcu_read_unlock();
1263
1264                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SHORTGI_DENSITY,
1265                                               (u8 *)(&mac->max_mss_density));
1266                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AMPDU_FACTOR,
1267                                               &mac->current_ampdu_factor);
1268                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AMPDU_MIN_SPACE,
1269                                               &mac->current_ampdu_density);
1270         }
1271
1272         if (changed & BSS_CHANGED_BANDWIDTH) {
1273                 struct ieee80211_sta *sta = NULL;
1274
1275                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1276                          "BSS_CHANGED_BANDWIDTH\n");
1277
1278                 rcu_read_lock();
1279                 sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
1280
1281                 if (sta) {
1282                         if (sta->ht_cap.ht_supported) {
1283                                 if (sta->ht_cap.cap &
1284                                     IEEE80211_HT_CAP_SUP_WIDTH_20_40)
1285                                         rtlphy->max_ht_chan_bw =
1286                                                         HT_CHANNEL_WIDTH_20_40;
1287                                 else
1288                                         rtlphy->max_ht_chan_bw =
1289                                                         HT_CHANNEL_WIDTH_20;
1290                         }
1291
1292                         if (sta->vht_cap.vht_supported)
1293                                 rtlphy->max_vht_chan_bw = HT_CHANNEL_WIDTH_80;
1294                 }
1295                 rcu_read_unlock();
1296         }
1297
1298         if (changed & BSS_CHANGED_BSSID) {
1299                 u32 basic_rates;
1300                 struct ieee80211_sta *sta = NULL;
1301
1302                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BSSID,
1303                                               (u8 *)bss_conf->bssid);
1304
1305                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
1306                          "bssid: %pM\n", bss_conf->bssid);
1307
1308                 mac->vendor = PEER_UNKNOWN;
1309                 memcpy(mac->bssid, bss_conf->bssid, ETH_ALEN);
1310
1311                 rcu_read_lock();
1312                 sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
1313                 if (!sta) {
1314                         rcu_read_unlock();
1315                         goto out;
1316                 }
1317
1318                 if (rtlhal->current_bandtype == BAND_ON_5G) {
1319                         mac->mode = WIRELESS_MODE_A;
1320                 } else {
1321                         if (sta->supp_rates[0] <= 0xf)
1322                                 mac->mode = WIRELESS_MODE_B;
1323                         else
1324                                 mac->mode = WIRELESS_MODE_G;
1325                 }
1326
1327                 if (sta->ht_cap.ht_supported) {
1328                         if (rtlhal->current_bandtype == BAND_ON_2_4G)
1329                                 mac->mode = WIRELESS_MODE_N_24G;
1330                         else
1331                                 mac->mode = WIRELESS_MODE_N_5G;
1332                 }
1333
1334                 if (sta->vht_cap.vht_supported) {
1335                         if (rtlhal->current_bandtype == BAND_ON_5G)
1336                                 mac->mode = WIRELESS_MODE_AC_5G;
1337                         else
1338                                 mac->mode = WIRELESS_MODE_AC_24G;
1339                 }
1340
1341                 /* just station need it, because ibss & ap mode will
1342                  * set in sta_add, and will be NULL here
1343                  */
1344                 if (vif->type == NL80211_IFTYPE_STATION) {
1345                         struct rtl_sta_info *sta_entry;
1346
1347                         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1348                         sta_entry->wireless_mode = mac->mode;
1349                 }
1350
1351                 if (sta->ht_cap.ht_supported) {
1352                         mac->ht_enable = true;
1353
1354                         /* for cisco 1252 bw20 it's wrong
1355                          * if (ht_cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
1356                          *      mac->bw_40 = true;
1357                          * }
1358                          */
1359                 }
1360
1361                 if (sta->vht_cap.vht_supported)
1362                         mac->vht_enable = true;
1363
1364                 if (changed & BSS_CHANGED_BASIC_RATES) {
1365                         /* for 5G must << RATE_6M_INDEX = 4,
1366                          * because 5G have no cck rate
1367                          */
1368                         if (rtlhal->current_bandtype == BAND_ON_5G)
1369                                 basic_rates = sta->supp_rates[1] << 4;
1370                         else
1371                                 basic_rates = sta->supp_rates[0];
1372
1373                         mac->basic_rates = basic_rates;
1374                         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
1375                                         (u8 *)(&basic_rates));
1376                 }
1377                 rcu_read_unlock();
1378         }
1379 out:
1380         mutex_unlock(&rtlpriv->locks.conf_mutex);
1381 }
1382
1383 static u64 rtl_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1384 {
1385         struct rtl_priv *rtlpriv = rtl_priv(hw);
1386         u64 tsf;
1387
1388         rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_CORRECT_TSF, (u8 *)(&tsf));
1389         return tsf;
1390 }
1391
1392 static void rtl_op_set_tsf(struct ieee80211_hw *hw,
1393                            struct ieee80211_vif *vif, u64 tsf)
1394 {
1395         struct rtl_priv *rtlpriv = rtl_priv(hw);
1396         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1397         u8 bibss = (mac->opmode == NL80211_IFTYPE_ADHOC) ? 1 : 0;
1398
1399         mac->tsf = tsf;
1400         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_CORRECT_TSF, (u8 *)(&bibss));
1401 }
1402
1403 static void rtl_op_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1404 {
1405         struct rtl_priv *rtlpriv = rtl_priv(hw);
1406         u8 tmp = 0;
1407
1408         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_DUAL_TSF_RST, (u8 *)(&tmp));
1409 }
1410
1411 static void rtl_op_sta_notify(struct ieee80211_hw *hw,
1412                               struct ieee80211_vif *vif,
1413                               enum sta_notify_cmd cmd,
1414                               struct ieee80211_sta *sta)
1415 {
1416         switch (cmd) {
1417         case STA_NOTIFY_SLEEP:
1418                 break;
1419         case STA_NOTIFY_AWAKE:
1420                 break;
1421         default:
1422                 break;
1423         }
1424 }
1425
1426 static int rtl_op_ampdu_action(struct ieee80211_hw *hw,
1427                                struct ieee80211_vif *vif,
1428                                struct ieee80211_ampdu_params *params)
1429 {
1430         struct rtl_priv *rtlpriv = rtl_priv(hw);
1431         struct ieee80211_sta *sta = params->sta;
1432         enum ieee80211_ampdu_mlme_action action = params->action;
1433         u16 tid = params->tid;
1434         u16 *ssn = &params->ssn;
1435
1436         switch (action) {
1437         case IEEE80211_AMPDU_TX_START:
1438                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1439                          "IEEE80211_AMPDU_TX_START: TID:%d\n", tid);
1440                 return rtl_tx_agg_start(hw, vif, sta, tid, ssn);
1441         case IEEE80211_AMPDU_TX_STOP_CONT:
1442         case IEEE80211_AMPDU_TX_STOP_FLUSH:
1443         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1444                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1445                          "IEEE80211_AMPDU_TX_STOP: TID:%d\n", tid);
1446                 return rtl_tx_agg_stop(hw, vif, sta, tid);
1447         case IEEE80211_AMPDU_TX_OPERATIONAL:
1448                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1449                          "IEEE80211_AMPDU_TX_OPERATIONAL:TID:%d\n", tid);
1450                 rtl_tx_agg_oper(hw, sta, tid);
1451                 break;
1452         case IEEE80211_AMPDU_RX_START:
1453                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1454                          "IEEE80211_AMPDU_RX_START:TID:%d\n", tid);
1455                 return rtl_rx_agg_start(hw, sta, tid);
1456         case IEEE80211_AMPDU_RX_STOP:
1457                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1458                          "IEEE80211_AMPDU_RX_STOP:TID:%d\n", tid);
1459                 return rtl_rx_agg_stop(hw, sta, tid);
1460         default:
1461                 pr_err("IEEE80211_AMPDU_ERR!!!!:\n");
1462                 return -EOPNOTSUPP;
1463         }
1464         return 0;
1465 }
1466
1467 static void rtl_op_sw_scan_start(struct ieee80211_hw *hw,
1468                                  struct ieee80211_vif *vif,
1469                                  const u8 *mac_addr)
1470 {
1471         struct rtl_priv *rtlpriv = rtl_priv(hw);
1472         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1473
1474         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "\n");
1475         mac->act_scanning = true;
1476         if (rtlpriv->link_info.higher_busytraffic) {
1477                 mac->skip_scan = true;
1478                 return;
1479         }
1480
1481         if (rtlpriv->phydm.ops)
1482                 rtlpriv->phydm.ops->phydm_pause_dig(rtlpriv, 1);
1483
1484         if (rtlpriv->cfg->ops->get_btc_status())
1485                 rtlpriv->btcoexist.btc_ops->btc_scan_notify(rtlpriv, 1);
1486         else if (rtlpriv->btcoexist.btc_ops)
1487                 rtlpriv->btcoexist.btc_ops->btc_scan_notify_wifi_only(rtlpriv,
1488                                                                       1);
1489
1490         if (rtlpriv->dm.supp_phymode_switch) {
1491                 if (rtlpriv->cfg->ops->chk_switch_dmdp)
1492                         rtlpriv->cfg->ops->chk_switch_dmdp(hw);
1493         }
1494
1495         if (mac->link_state == MAC80211_LINKED) {
1496                 rtl_lps_leave(hw);
1497                 mac->link_state = MAC80211_LINKED_SCANNING;
1498         } else {
1499                 rtl_ips_nic_on(hw);
1500         }
1501
1502         /* Dul mac */
1503         rtlpriv->rtlhal.load_imrandiqk_setting_for2g = false;
1504
1505         rtlpriv->cfg->ops->led_control(hw, LED_CTL_SITE_SURVEY);
1506         rtlpriv->cfg->ops->scan_operation_backup(hw, SCAN_OPT_BACKUP_BAND0);
1507 }
1508
1509 static void rtl_op_sw_scan_complete(struct ieee80211_hw *hw,
1510                                     struct ieee80211_vif *vif)
1511 {
1512         struct rtl_priv *rtlpriv = rtl_priv(hw);
1513         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1514
1515         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "\n");
1516         mac->act_scanning = false;
1517         mac->skip_scan = false;
1518
1519         rtlpriv->btcoexist.btc_info.ap_num = rtlpriv->scan_list.num;
1520
1521         if (rtlpriv->link_info.higher_busytraffic)
1522                 return;
1523
1524         /* p2p will use 1/6/11 to scan */
1525         if (mac->n_channels == 3)
1526                 mac->p2p_in_use = true;
1527         else
1528                 mac->p2p_in_use = false;
1529         mac->n_channels = 0;
1530         /* Dul mac */
1531         rtlpriv->rtlhal.load_imrandiqk_setting_for2g = false;
1532
1533         if (mac->link_state == MAC80211_LINKED_SCANNING) {
1534                 mac->link_state = MAC80211_LINKED;
1535                 if (mac->opmode == NL80211_IFTYPE_STATION) {
1536                         /* fix fwlps issue */
1537                         rtlpriv->cfg->ops->set_network_type(hw, mac->opmode);
1538                 }
1539         }
1540
1541         rtlpriv->cfg->ops->scan_operation_backup(hw, SCAN_OPT_RESTORE);
1542         if (rtlpriv->cfg->ops->get_btc_status())
1543                 rtlpriv->btcoexist.btc_ops->btc_scan_notify(rtlpriv, 0);
1544         else if (rtlpriv->btcoexist.btc_ops)
1545                 rtlpriv->btcoexist.btc_ops->btc_scan_notify_wifi_only(rtlpriv,
1546                                                                       0);
1547
1548         if (rtlpriv->phydm.ops)
1549                 rtlpriv->phydm.ops->phydm_pause_dig(rtlpriv, 0);
1550 }
1551
1552 static int rtl_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
1553                           struct ieee80211_vif *vif, struct ieee80211_sta *sta,
1554                           struct ieee80211_key_conf *key)
1555 {
1556         struct rtl_priv *rtlpriv = rtl_priv(hw);
1557         u8 key_type = NO_ENCRYPTION;
1558         u8 key_idx;
1559         bool group_key = false;
1560         bool wep_only = false;
1561         int err = 0;
1562         u8 mac_addr[ETH_ALEN];
1563         u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1564
1565         rtlpriv->btcoexist.btc_info.in_4way = false;
1566
1567         if (rtlpriv->cfg->mod_params->sw_crypto || rtlpriv->sec.use_sw_sec) {
1568                 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
1569                          "not open hw encryption\n");
1570                 return -ENOSPC; /*User disabled HW-crypto */
1571         }
1572         /* To support IBSS, use sw-crypto for GTK */
1573         if ((vif->type == NL80211_IFTYPE_ADHOC ||
1574              vif->type == NL80211_IFTYPE_MESH_POINT) &&
1575             !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1576                 return -ENOSPC;
1577         RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1578                  "%s hardware based encryption for keyidx: %d, mac: %pM\n",
1579                   cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
1580                   sta ? sta->addr : bcast_addr);
1581         rtlpriv->sec.being_setkey = true;
1582         rtl_ips_nic_on(hw);
1583         mutex_lock(&rtlpriv->locks.conf_mutex);
1584         /* <1> get encryption alg */
1585
1586         switch (key->cipher) {
1587         case WLAN_CIPHER_SUITE_WEP40:
1588                 key_type = WEP40_ENCRYPTION;
1589                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:WEP40\n");
1590                 break;
1591         case WLAN_CIPHER_SUITE_WEP104:
1592                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:WEP104\n");
1593                 key_type = WEP104_ENCRYPTION;
1594                 break;
1595         case WLAN_CIPHER_SUITE_TKIP:
1596                 key_type = TKIP_ENCRYPTION;
1597                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:TKIP\n");
1598                 break;
1599         case WLAN_CIPHER_SUITE_CCMP:
1600                 key_type = AESCCMP_ENCRYPTION;
1601                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:CCMP\n");
1602                 break;
1603         case WLAN_CIPHER_SUITE_AES_CMAC:
1604                 /* HW don't support CMAC encryption,
1605                  * use software CMAC encryption
1606                  */
1607                 key_type = AESCMAC_ENCRYPTION;
1608                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:CMAC\n");
1609                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1610                          "HW don't support CMAC encryption, use software CMAC encryption\n");
1611                 err = -EOPNOTSUPP;
1612                 goto out_unlock;
1613         default:
1614                 pr_err("alg_err:%x!!!!:\n", key->cipher);
1615                 goto out_unlock;
1616         }
1617         if (key_type == WEP40_ENCRYPTION ||
1618             key_type == WEP104_ENCRYPTION ||
1619             vif->type == NL80211_IFTYPE_ADHOC)
1620                 rtlpriv->sec.use_defaultkey = true;
1621
1622         /* <2> get key_idx */
1623         key_idx = (u8)(key->keyidx);
1624         if (key_idx > 3)
1625                 goto out_unlock;
1626         /* <3> if pairwise key enable_hw_sec */
1627         group_key = !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE);
1628
1629         /* wep always be group key, but there are two conditions:
1630          * 1) wep only: is just for wep enc, in this condition
1631          * rtlpriv->sec.pairwise_enc_algorithm == NO_ENCRYPTION
1632          * will be true & enable_hw_sec will be set when wep
1633          * ke setting.
1634          * 2) wep(group) + AES(pairwise): some AP like cisco
1635          * may use it, in this condition enable_hw_sec will not
1636          * be set when wep key setting.
1637          * we must reset sec_info after lingked before set key,
1638          * or some flag will be wrong
1639          */
1640         if (vif->type == NL80211_IFTYPE_AP ||
1641             vif->type == NL80211_IFTYPE_MESH_POINT) {
1642                 if (!group_key || key_type == WEP40_ENCRYPTION ||
1643                     key_type == WEP104_ENCRYPTION) {
1644                         if (group_key)
1645                                 wep_only = true;
1646                         rtlpriv->cfg->ops->enable_hw_sec(hw);
1647                 }
1648         } else {
1649                 if (!group_key || vif->type == NL80211_IFTYPE_ADHOC ||
1650                     rtlpriv->sec.pairwise_enc_algorithm == NO_ENCRYPTION) {
1651                         if (rtlpriv->sec.pairwise_enc_algorithm ==
1652                             NO_ENCRYPTION &&
1653                            (key_type == WEP40_ENCRYPTION ||
1654                             key_type == WEP104_ENCRYPTION))
1655                                 wep_only = true;
1656                         rtlpriv->sec.pairwise_enc_algorithm = key_type;
1657                         RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1658                                  "set enable_hw_sec, key_type:%x(OPEN:0 WEP40:1 TKIP:2 AES:4 WEP104:5)\n",
1659                                  key_type);
1660                         rtlpriv->cfg->ops->enable_hw_sec(hw);
1661                 }
1662         }
1663         /* <4> set key based on cmd */
1664         switch (cmd) {
1665         case SET_KEY:
1666                 if (wep_only) {
1667                         RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1668                                  "set WEP(group/pairwise) key\n");
1669                         /* Pairwise key with an assigned MAC address. */
1670                         rtlpriv->sec.pairwise_enc_algorithm = key_type;
1671                         rtlpriv->sec.group_enc_algorithm = key_type;
1672                         /*set local buf about wep key. */
1673                         memcpy(rtlpriv->sec.key_buf[key_idx],
1674                                key->key, key->keylen);
1675                         rtlpriv->sec.key_len[key_idx] = key->keylen;
1676                         eth_zero_addr(mac_addr);
1677                 } else if (group_key) { /* group key */
1678                         RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1679                                  "set group key\n");
1680                         /* group key */
1681                         rtlpriv->sec.group_enc_algorithm = key_type;
1682                         /*set local buf about group key. */
1683                         memcpy(rtlpriv->sec.key_buf[key_idx],
1684                                key->key, key->keylen);
1685                         rtlpriv->sec.key_len[key_idx] = key->keylen;
1686                         memcpy(mac_addr, bcast_addr, ETH_ALEN);
1687                 } else {        /* pairwise key */
1688                         RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1689                                  "set pairwise key\n");
1690                         if (!sta) {
1691                                 WARN_ONCE(true,
1692                                           "rtlwifi: pairwise key without mac_addr\n");
1693
1694                                 err = -EOPNOTSUPP;
1695                                 goto out_unlock;
1696                         }
1697                         /* Pairwise key with an assigned MAC address. */
1698                         rtlpriv->sec.pairwise_enc_algorithm = key_type;
1699                         /*set local buf about pairwise key. */
1700                         memcpy(rtlpriv->sec.key_buf[PAIRWISE_KEYIDX],
1701                                key->key, key->keylen);
1702                         rtlpriv->sec.key_len[PAIRWISE_KEYIDX] = key->keylen;
1703                         rtlpriv->sec.pairwise_key =
1704                             rtlpriv->sec.key_buf[PAIRWISE_KEYIDX];
1705                         memcpy(mac_addr, sta->addr, ETH_ALEN);
1706                 }
1707                 rtlpriv->cfg->ops->set_key(hw, key_idx, mac_addr,
1708                                            group_key, key_type, wep_only,
1709                                            false);
1710                 /* <5> tell mac80211 do something: */
1711                 /*must use sw generate IV, or can not work !!!!. */
1712                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1713                 key->hw_key_idx = key_idx;
1714                 if (key_type == TKIP_ENCRYPTION)
1715                         key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1716                 /*use software CCMP encryption for management frames (MFP) */
1717                 if (key_type == AESCCMP_ENCRYPTION)
1718                         key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
1719                 break;
1720         case DISABLE_KEY:
1721                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1722                          "disable key delete one entry\n");
1723                 /*set local buf about wep key. */
1724                 if (vif->type == NL80211_IFTYPE_AP ||
1725                     vif->type == NL80211_IFTYPE_MESH_POINT) {
1726                         if (sta)
1727                                 rtl_cam_del_entry(hw, sta->addr);
1728                 }
1729                 memset(rtlpriv->sec.key_buf[key_idx], 0, key->keylen);
1730                 rtlpriv->sec.key_len[key_idx] = 0;
1731                 eth_zero_addr(mac_addr);
1732                 /*
1733                  *mac80211 will delete entries one by one,
1734                  *so don't use rtl_cam_reset_all_entry
1735                  *or clear all entries here.
1736                  */
1737                 rtl_wait_tx_report_acked(hw, 500); /* wait 500ms for TX ack */
1738
1739                 rtl_cam_delete_one_entry(hw, mac_addr, key_idx);
1740                 break;
1741         default:
1742                 pr_err("cmd_err:%x!!!!:\n", cmd);
1743         }
1744 out_unlock:
1745         mutex_unlock(&rtlpriv->locks.conf_mutex);
1746         rtlpriv->sec.being_setkey = false;
1747         return err;
1748 }
1749
1750 static void rtl_op_rfkill_poll(struct ieee80211_hw *hw)
1751 {
1752         struct rtl_priv *rtlpriv = rtl_priv(hw);
1753
1754         bool radio_state;
1755         bool blocked;
1756         u8 valid = 0;
1757
1758         if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
1759                 return;
1760
1761         mutex_lock(&rtlpriv->locks.conf_mutex);
1762
1763         /*if Radio On return true here */
1764         radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
1765
1766         if (valid) {
1767                 if (unlikely(radio_state != rtlpriv->rfkill.rfkill_state)) {
1768                         rtlpriv->rfkill.rfkill_state = radio_state;
1769
1770                         RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
1771                                  "wireless radio switch turned %s\n",
1772                                   radio_state ? "on" : "off");
1773
1774                         blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
1775                         wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
1776                 }
1777         }
1778
1779         mutex_unlock(&rtlpriv->locks.conf_mutex);
1780 }
1781
1782 /* this function is called by mac80211 to flush tx buffer
1783  * before switch channel or power save, or tx buffer packet
1784  * maybe send after offchannel or rf sleep, this may cause
1785  * dis-association by AP
1786  */
1787 static void rtl_op_flush(struct ieee80211_hw *hw,
1788                          struct ieee80211_vif *vif,
1789                          u32 queues,
1790                          bool drop)
1791 {
1792         struct rtl_priv *rtlpriv = rtl_priv(hw);
1793
1794         if (rtlpriv->intf_ops->flush)
1795                 rtlpriv->intf_ops->flush(hw, queues, drop);
1796 }
1797
1798 /*      Description:
1799  *              This routine deals with the Power Configuration CMD
1800  *               parsing for RTL8723/RTL8188E Series IC.
1801  *      Assumption:
1802  *              We should follow specific format that was released from HW SD.
1803  */
1804 bool rtl_hal_pwrseqcmdparsing(struct rtl_priv *rtlpriv, u8 cut_version,
1805                               u8 faversion, u8 interface_type,
1806                               struct wlan_pwr_cfg pwrcfgcmd[])
1807 {
1808         struct wlan_pwr_cfg cfg_cmd;
1809         bool polling_bit = false;
1810         u32 ary_idx = 0;
1811         u8 value = 0;
1812         u32 offset = 0;
1813         u32 polling_count = 0;
1814         u32 max_polling_cnt = 5000;
1815
1816         do {
1817                 cfg_cmd = pwrcfgcmd[ary_idx];
1818                 RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1819                          "%s(): offset(%#x),cut_msk(%#x), famsk(%#x), interface_msk(%#x), base(%#x), cmd(%#x), msk(%#x), value(%#x)\n",
1820                          __func__, GET_PWR_CFG_OFFSET(cfg_cmd),
1821                                             GET_PWR_CFG_CUT_MASK(cfg_cmd),
1822                          GET_PWR_CFG_FAB_MASK(cfg_cmd),
1823                                               GET_PWR_CFG_INTF_MASK(cfg_cmd),
1824                          GET_PWR_CFG_BASE(cfg_cmd), GET_PWR_CFG_CMD(cfg_cmd),
1825                          GET_PWR_CFG_MASK(cfg_cmd), GET_PWR_CFG_VALUE(cfg_cmd));
1826
1827                 if ((GET_PWR_CFG_FAB_MASK(cfg_cmd) & faversion) &&
1828                     (GET_PWR_CFG_CUT_MASK(cfg_cmd) & cut_version) &&
1829                     (GET_PWR_CFG_INTF_MASK(cfg_cmd) & interface_type)) {
1830                         switch (GET_PWR_CFG_CMD(cfg_cmd)) {
1831                         case PWR_CMD_READ:
1832                                 RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1833                                          "%s(): PWR_CMD_READ\n", __func__);
1834                                 break;
1835                         case PWR_CMD_WRITE:
1836                                 RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1837                                          "%s(): PWR_CMD_WRITE\n", __func__);
1838                                 offset = GET_PWR_CFG_OFFSET(cfg_cmd);
1839
1840                                 /*Read the value from system register*/
1841                                 value = rtl_read_byte(rtlpriv, offset);
1842                                 value &= (~(GET_PWR_CFG_MASK(cfg_cmd)));
1843                                 value |= (GET_PWR_CFG_VALUE(cfg_cmd) &
1844                                           GET_PWR_CFG_MASK(cfg_cmd));
1845
1846                                 /*Write the value back to system register*/
1847                                 rtl_write_byte(rtlpriv, offset, value);
1848                                 break;
1849                         case PWR_CMD_POLLING:
1850                                 RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1851                                          "%s(): PWR_CMD_POLLING\n", __func__);
1852                                 polling_bit = false;
1853                                 offset = GET_PWR_CFG_OFFSET(cfg_cmd);
1854
1855                                 do {
1856                                         value = rtl_read_byte(rtlpriv, offset);
1857
1858                                         value &= GET_PWR_CFG_MASK(cfg_cmd);
1859                                         if (value ==
1860                                             (GET_PWR_CFG_VALUE(cfg_cmd) &
1861                                              GET_PWR_CFG_MASK(cfg_cmd)))
1862                                                 polling_bit = true;
1863                                         else
1864                                                 udelay(10);
1865
1866                                         if (polling_count++ > max_polling_cnt)
1867                                                 return false;
1868                                 } while (!polling_bit);
1869                                 break;
1870                         case PWR_CMD_DELAY:
1871                                 RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1872                                          "%s(): PWR_CMD_DELAY\n", __func__);
1873                                 if (GET_PWR_CFG_VALUE(cfg_cmd) ==
1874                                     PWRSEQ_DELAY_US)
1875                                         udelay(GET_PWR_CFG_OFFSET(cfg_cmd));
1876                                 else
1877                                         mdelay(GET_PWR_CFG_OFFSET(cfg_cmd));
1878                                 break;
1879                         case PWR_CMD_END:
1880                                 RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1881                                          "%s(): PWR_CMD_END\n", __func__);
1882                                 return true;
1883                         default:
1884                                 WARN_ONCE(true,
1885                                           "rtlwifi: %s(): Unknown CMD!!\n", __func__);
1886                                 break;
1887                         }
1888                 }
1889                 ary_idx++;
1890         } while (1);
1891
1892         return true;
1893 }
1894
1895 bool rtl_cmd_send_packet(struct ieee80211_hw *hw, struct sk_buff *skb)
1896 {
1897         struct rtl_priv *rtlpriv = rtl_priv(hw);
1898         struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1899         struct rtl8192_tx_ring *ring;
1900         struct rtl_tx_desc *pdesc;
1901         unsigned long flags;
1902         struct sk_buff *pskb = NULL;
1903
1904         ring = &rtlpci->tx_ring[BEACON_QUEUE];
1905
1906         spin_lock_irqsave(&rtlpriv->locks.irq_th_lock, flags);
1907         pskb = __skb_dequeue(&ring->queue);
1908         if (pskb)
1909                 dev_kfree_skb_irq(pskb);
1910
1911         /*this is wrong, fill_tx_cmddesc needs update*/
1912         pdesc = &ring->desc[0];
1913
1914         rtlpriv->cfg->ops->fill_tx_cmddesc(hw, (u8 *)pdesc, 1, 1, skb);
1915
1916         __skb_queue_tail(&ring->queue, skb);
1917
1918         spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);
1919
1920         rtlpriv->cfg->ops->tx_polling(hw, BEACON_QUEUE);
1921
1922         return true;
1923 }
1924
1925 const struct ieee80211_ops rtl_ops = {
1926         .start = rtl_op_start,
1927         .stop = rtl_op_stop,
1928         .tx = rtl_op_tx,
1929         .add_interface = rtl_op_add_interface,
1930         .remove_interface = rtl_op_remove_interface,
1931         .change_interface = rtl_op_change_interface,
1932 #ifdef CONFIG_PM
1933         .suspend = rtl_op_suspend,
1934         .resume = rtl_op_resume,
1935 #endif
1936         .config = rtl_op_config,
1937         .configure_filter = rtl_op_configure_filter,
1938         .set_key = rtl_op_set_key,
1939         .sta_statistics = rtl_op_sta_statistics,
1940         .set_frag_threshold = rtl_op_set_frag_threshold,
1941         .conf_tx = rtl_op_conf_tx,
1942         .bss_info_changed = rtl_op_bss_info_changed,
1943         .get_tsf = rtl_op_get_tsf,
1944         .set_tsf = rtl_op_set_tsf,
1945         .reset_tsf = rtl_op_reset_tsf,
1946         .sta_notify = rtl_op_sta_notify,
1947         .ampdu_action = rtl_op_ampdu_action,
1948         .sw_scan_start = rtl_op_sw_scan_start,
1949         .sw_scan_complete = rtl_op_sw_scan_complete,
1950         .rfkill_poll = rtl_op_rfkill_poll,
1951         .sta_add = rtl_op_sta_add,
1952         .sta_remove = rtl_op_sta_remove,
1953         .flush = rtl_op_flush,
1954 };
1955
1956 bool rtl_btc_status_false(void)
1957 {
1958         return false;
1959 }
1960
1961 void rtl_dm_diginit(struct ieee80211_hw *hw, u32 cur_igvalue)
1962 {
1963         struct rtl_priv *rtlpriv = rtl_priv(hw);
1964         struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
1965
1966         dm_digtable->dig_enable_flag = true;
1967         dm_digtable->dig_ext_port_stage = DIG_EXT_PORT_STAGE_MAX;
1968         dm_digtable->cur_igvalue = cur_igvalue;
1969         dm_digtable->pre_igvalue = 0;
1970         dm_digtable->cur_sta_cstate = DIG_STA_DISCONNECT;
1971         dm_digtable->presta_cstate = DIG_STA_DISCONNECT;
1972         dm_digtable->curmultista_cstate = DIG_MULTISTA_DISCONNECT;
1973         dm_digtable->rssi_lowthresh = DM_DIG_THRESH_LOW;
1974         dm_digtable->rssi_highthresh = DM_DIG_THRESH_HIGH;
1975         dm_digtable->fa_lowthresh = DM_FALSEALARM_THRESH_LOW;
1976         dm_digtable->fa_highthresh = DM_FALSEALARM_THRESH_HIGH;
1977         dm_digtable->rx_gain_max = DM_DIG_MAX;
1978         dm_digtable->rx_gain_min = DM_DIG_MIN;
1979         dm_digtable->back_val = DM_DIG_BACKOFF_DEFAULT;
1980         dm_digtable->back_range_max = DM_DIG_BACKOFF_MAX;
1981         dm_digtable->back_range_min = DM_DIG_BACKOFF_MIN;
1982         dm_digtable->pre_cck_cca_thres = 0xff;
1983         dm_digtable->cur_cck_cca_thres = 0x83;
1984         dm_digtable->forbidden_igi = DM_DIG_MIN;
1985         dm_digtable->large_fa_hit = 0;
1986         dm_digtable->recover_cnt = 0;
1987         dm_digtable->dig_min_0 = 0x25;
1988         dm_digtable->dig_min_1 = 0x25;
1989         dm_digtable->media_connect_0 = false;
1990         dm_digtable->media_connect_1 = false;
1991         rtlpriv->dm.dm_initialgain_enable = true;
1992         dm_digtable->bt30_cur_igi = 0x32;
1993         dm_digtable->pre_cck_pd_state = CCK_PD_STAGE_MAX;
1994         dm_digtable->cur_cck_pd_state = CCK_PD_STAGE_LOWRSSI;
1995 }