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