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