]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/wireless/realtek/rtlwifi/core.c
Merge branch 'for-4.8/core' of git://git.kernel.dk/linux-block
[karo-tx-linux.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 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 EXPORT_SYMBOL(channel5g);
48
49 u8 channel5g_80m[CHANNEL_MAX_NUMBER_5G_80M] = {
50         42, 58, 106, 122, 138, 155, 171
51 };
52 EXPORT_SYMBOL(channel5g_80m);
53
54 void rtl_addr_delay(u32 addr)
55 {
56         if (addr == 0xfe)
57                 mdelay(50);
58         else if (addr == 0xfd)
59                 msleep(5);
60         else if (addr == 0xfc)
61                 msleep(1);
62         else if (addr == 0xfb)
63                 usleep_range(50, 100);
64         else if (addr == 0xfa)
65                 usleep_range(5, 10);
66         else if (addr == 0xf9)
67                 usleep_range(1, 2);
68 }
69 EXPORT_SYMBOL(rtl_addr_delay);
70
71 void rtl_rfreg_delay(struct ieee80211_hw *hw, enum radio_path rfpath, u32 addr,
72                      u32 mask, u32 data)
73 {
74         if (addr >= 0xf9 && addr <= 0xfe) {
75                 rtl_addr_delay(addr);
76         } else {
77                 rtl_set_rfreg(hw, rfpath, addr, mask, data);
78                 udelay(1);
79         }
80 }
81 EXPORT_SYMBOL(rtl_rfreg_delay);
82
83 void rtl_bb_delay(struct ieee80211_hw *hw, u32 addr, u32 data)
84 {
85         if (addr >= 0xf9 && addr <= 0xfe) {
86                 rtl_addr_delay(addr);
87         } else {
88                 rtl_set_bbreg(hw, addr, MASKDWORD, data);
89                 udelay(1);
90         }
91 }
92 EXPORT_SYMBOL(rtl_bb_delay);
93
94 static void rtl_fw_do_work(const struct firmware *firmware, void *context,
95                            bool is_wow)
96 {
97         struct ieee80211_hw *hw = context;
98         struct rtl_priv *rtlpriv = rtl_priv(hw);
99         int err;
100
101         RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
102                  "Firmware callback routine entered!\n");
103         complete(&rtlpriv->firmware_loading_complete);
104         if (!firmware) {
105                 if (rtlpriv->cfg->alt_fw_name) {
106                         err = request_firmware(&firmware,
107                                                rtlpriv->cfg->alt_fw_name,
108                                                rtlpriv->io.dev);
109                         pr_info("Loading alternative firmware %s\n",
110                                 rtlpriv->cfg->alt_fw_name);
111                         if (!err)
112                                 goto found_alt;
113                 }
114                 pr_err("Firmware %s not available\n", rtlpriv->cfg->fw_name);
115                 rtlpriv->max_fw_size = 0;
116                 return;
117         }
118 found_alt:
119         if (firmware->size > rtlpriv->max_fw_size) {
120                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
121                          "Firmware is too big!\n");
122                 release_firmware(firmware);
123                 return;
124         }
125         if (!is_wow) {
126                 memcpy(rtlpriv->rtlhal.pfirmware, firmware->data,
127                        firmware->size);
128                 rtlpriv->rtlhal.fwsize = firmware->size;
129         } else {
130                 memcpy(rtlpriv->rtlhal.wowlan_firmware, firmware->data,
131                        firmware->size);
132                 rtlpriv->rtlhal.wowlan_fwsize = firmware->size;
133         }
134         rtlpriv->rtlhal.fwsize = firmware->size;
135         release_firmware(firmware);
136 }
137
138 void rtl_fw_cb(const struct firmware *firmware, void *context)
139 {
140         rtl_fw_do_work(firmware, context, false);
141 }
142 EXPORT_SYMBOL(rtl_fw_cb);
143
144 void rtl_wowlan_fw_cb(const struct firmware *firmware, void *context)
145 {
146         rtl_fw_do_work(firmware, context, true);
147 }
148 EXPORT_SYMBOL(rtl_wowlan_fw_cb);
149
150 /*mutex for start & stop is must here. */
151 static int rtl_op_start(struct ieee80211_hw *hw)
152 {
153         int err = 0;
154         struct rtl_priv *rtlpriv = rtl_priv(hw);
155         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
156
157         if (!is_hal_stop(rtlhal))
158                 return 0;
159         if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
160                 return 0;
161         mutex_lock(&rtlpriv->locks.conf_mutex);
162         err = rtlpriv->intf_ops->adapter_start(hw);
163         if (!err)
164                 rtl_watch_dog_timer_callback((unsigned long)hw);
165         mutex_unlock(&rtlpriv->locks.conf_mutex);
166         return err;
167 }
168
169 static void rtl_op_stop(struct ieee80211_hw *hw)
170 {
171         struct rtl_priv *rtlpriv = rtl_priv(hw);
172         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
173         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
174         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
175         bool support_remote_wakeup = false;
176
177         if (is_hal_stop(rtlhal))
178                 return;
179
180         rtlpriv->cfg->ops->get_hw_reg(hw, HAL_DEF_WOWLAN,
181                                       (u8 *)(&support_remote_wakeup));
182         /* here is must, because adhoc do stop and start,
183          * but stop with RFOFF may cause something wrong,
184          * like adhoc TP
185          */
186         if (unlikely(ppsc->rfpwr_state == ERFOFF))
187                 rtl_ips_nic_on(hw);
188
189         mutex_lock(&rtlpriv->locks.conf_mutex);
190         /* if wowlan supported, DON'T clear connected info */
191         if (!(support_remote_wakeup &&
192               rtlhal->enter_pnp_sleep)) {
193                 mac->link_state = MAC80211_NOLINK;
194                 eth_zero_addr(mac->bssid);
195                 mac->vendor = PEER_UNKNOWN;
196
197                 /* reset sec info */
198                 rtl_cam_reset_sec_info(hw);
199
200                 rtl_deinit_deferred_work(hw);
201         }
202         rtlpriv->intf_ops->adapter_stop(hw);
203
204         mutex_unlock(&rtlpriv->locks.conf_mutex);
205 }
206
207 static void rtl_op_tx(struct ieee80211_hw *hw,
208                       struct ieee80211_tx_control *control,
209                       struct sk_buff *skb)
210 {
211         struct rtl_priv *rtlpriv = rtl_priv(hw);
212         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
213         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
214         struct rtl_tcb_desc tcb_desc;
215         memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
216
217         if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
218                 goto err_free;
219
220         if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
221                 goto err_free;
222
223         if (!rtlpriv->intf_ops->waitq_insert(hw, control->sta, skb))
224                 rtlpriv->intf_ops->adapter_tx(hw, control->sta, skb, &tcb_desc);
225         return;
226
227 err_free:
228         dev_kfree_skb_any(skb);
229 }
230
231 static int rtl_op_add_interface(struct ieee80211_hw *hw,
232                 struct ieee80211_vif *vif)
233 {
234         struct rtl_priv *rtlpriv = rtl_priv(hw);
235         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
236         int err = 0;
237
238         if (mac->vif) {
239                 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
240                          "vif has been set!! mac->vif = 0x%p\n", mac->vif);
241                 return -EOPNOTSUPP;
242         }
243
244         vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
245
246         rtl_ips_nic_on(hw);
247
248         mutex_lock(&rtlpriv->locks.conf_mutex);
249         switch (ieee80211_vif_type_p2p(vif)) {
250         case NL80211_IFTYPE_P2P_CLIENT:
251                 mac->p2p = P2P_ROLE_CLIENT;
252                 /*fall through*/
253         case NL80211_IFTYPE_STATION:
254                 if (mac->beacon_enabled == 1) {
255                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
256                                  "NL80211_IFTYPE_STATION\n");
257                         mac->beacon_enabled = 0;
258                         rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
259                                         rtlpriv->cfg->maps[RTL_IBSS_INT_MASKS]);
260                 }
261                 break;
262         case NL80211_IFTYPE_ADHOC:
263                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
264                          "NL80211_IFTYPE_ADHOC\n");
265
266                 mac->link_state = MAC80211_LINKED;
267                 rtlpriv->cfg->ops->set_bcn_reg(hw);
268                 if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
269                         mac->basic_rates = 0xfff;
270                 else
271                         mac->basic_rates = 0xff0;
272                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
273                                 (u8 *)(&mac->basic_rates));
274
275                 break;
276         case NL80211_IFTYPE_P2P_GO:
277                 mac->p2p = P2P_ROLE_GO;
278                 /*fall through*/
279         case NL80211_IFTYPE_AP:
280                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
281                          "NL80211_IFTYPE_AP\n");
282
283                 mac->link_state = MAC80211_LINKED;
284                 rtlpriv->cfg->ops->set_bcn_reg(hw);
285                 if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
286                         mac->basic_rates = 0xfff;
287                 else
288                         mac->basic_rates = 0xff0;
289                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
290                                               (u8 *)(&mac->basic_rates));
291                 break;
292         case NL80211_IFTYPE_MESH_POINT:
293                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
294                          "NL80211_IFTYPE_MESH_POINT\n");
295
296                 mac->link_state = MAC80211_LINKED;
297                 rtlpriv->cfg->ops->set_bcn_reg(hw);
298                 if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
299                         mac->basic_rates = 0xfff;
300                 else
301                         mac->basic_rates = 0xff0;
302                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
303                                 (u8 *)(&mac->basic_rates));
304                 break;
305         default:
306                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
307                          "operation mode %d is not support!\n", vif->type);
308                 err = -EOPNOTSUPP;
309                 goto out;
310         }
311
312         if (mac->p2p) {
313                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
314                          "p2p role %x\n", vif->type);
315                 mac->basic_rates = 0xff0;/*disable cck rate for p2p*/
316                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
317                                 (u8 *)(&mac->basic_rates));
318         }
319         mac->vif = vif;
320         mac->opmode = vif->type;
321         rtlpriv->cfg->ops->set_network_type(hw, vif->type);
322         memcpy(mac->mac_addr, vif->addr, ETH_ALEN);
323         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_ETHER_ADDR, mac->mac_addr);
324
325 out:
326         mutex_unlock(&rtlpriv->locks.conf_mutex);
327         return err;
328 }
329
330 static void rtl_op_remove_interface(struct ieee80211_hw *hw,
331                 struct ieee80211_vif *vif)
332 {
333         struct rtl_priv *rtlpriv = rtl_priv(hw);
334         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
335
336         mutex_lock(&rtlpriv->locks.conf_mutex);
337
338         /* Free beacon resources */
339         if ((vif->type == NL80211_IFTYPE_AP) ||
340             (vif->type == NL80211_IFTYPE_ADHOC) ||
341             (vif->type == NL80211_IFTYPE_MESH_POINT)) {
342                 if (mac->beacon_enabled == 1) {
343                         mac->beacon_enabled = 0;
344                         rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
345                                         rtlpriv->cfg->maps[RTL_IBSS_INT_MASKS]);
346                 }
347         }
348
349         /*
350          *Note: We assume NL80211_IFTYPE_UNSPECIFIED as
351          *NO LINK for our hardware.
352          */
353         mac->p2p = 0;
354         mac->vif = NULL;
355         mac->link_state = MAC80211_NOLINK;
356         eth_zero_addr(mac->bssid);
357         mac->vendor = PEER_UNKNOWN;
358         mac->opmode = NL80211_IFTYPE_UNSPECIFIED;
359         rtlpriv->cfg->ops->set_network_type(hw, mac->opmode);
360
361         mutex_unlock(&rtlpriv->locks.conf_mutex);
362 }
363 static int rtl_op_change_interface(struct ieee80211_hw *hw,
364                                    struct ieee80211_vif *vif,
365                                    enum nl80211_iftype new_type, bool p2p)
366 {
367         struct rtl_priv *rtlpriv = rtl_priv(hw);
368         int ret;
369         rtl_op_remove_interface(hw, vif);
370
371         vif->type = new_type;
372         vif->p2p = p2p;
373         ret = rtl_op_add_interface(hw, vif);
374         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
375                  "p2p  %x\n", p2p);
376         return ret;
377 }
378
379 #ifdef CONFIG_PM
380 static u16 crc16_ccitt(u8 data, u16 crc)
381 {
382         u8 shift_in, data_bit, crc_bit11, crc_bit4, crc_bit15;
383         u8 i;
384         u16 result;
385
386         for (i = 0; i < 8; i++) {
387                 crc_bit15 = ((crc & BIT(15)) ? 1 : 0);
388                 data_bit  = (data & (BIT(0) << i) ? 1 : 0);
389                 shift_in = crc_bit15 ^ data_bit;
390
391                 result = crc << 1;
392                 if (shift_in == 0)
393                         result &= (~BIT(0));
394                 else
395                         result |= BIT(0);
396
397                 crc_bit11 = ((crc & BIT(11)) ? 1 : 0) ^ shift_in;
398                 if (crc_bit11 == 0)
399                         result &= (~BIT(12));
400                 else
401                         result |= BIT(12);
402
403                 crc_bit4 = ((crc & BIT(4)) ? 1 : 0) ^ shift_in;
404                 if (crc_bit4 == 0)
405                         result &= (~BIT(5));
406                 else
407                         result |= BIT(5);
408
409                 crc = result;
410         }
411
412         return crc;
413 }
414
415 static u16 _calculate_wol_pattern_crc(u8 *pattern, u16 len)
416 {
417         u16 crc = 0xffff;
418         u32 i;
419
420         for (i = 0; i < len; i++)
421                 crc = crc16_ccitt(pattern[i], crc);
422
423         crc = ~crc;
424
425         return crc;
426 }
427
428 static void _rtl_add_wowlan_patterns(struct ieee80211_hw *hw,
429                                      struct cfg80211_wowlan *wow)
430 {
431         struct rtl_priv *rtlpriv = rtl_priv(hw);
432         struct rtl_mac *mac = &rtlpriv->mac80211;
433         struct cfg80211_pkt_pattern *patterns = wow->patterns;
434         struct rtl_wow_pattern rtl_pattern;
435         const u8 *pattern_os, *mask_os;
436         u8 mask[MAX_WOL_BIT_MASK_SIZE] = {0};
437         u8 content[MAX_WOL_PATTERN_SIZE] = {0};
438         u8 broadcast_addr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
439         u8 multicast_addr1[2] = {0x33, 0x33};
440         u8 multicast_addr2[3] = {0x01, 0x00, 0x5e};
441         u8 i, mask_len;
442         u16 j, len;
443
444         for (i = 0; i < wow->n_patterns; i++) {
445                 memset(&rtl_pattern, 0, sizeof(struct rtl_wow_pattern));
446                 memset(mask, 0, MAX_WOL_BIT_MASK_SIZE);
447                 if (patterns[i].pattern_len > MAX_WOL_PATTERN_SIZE) {
448                         RT_TRACE(rtlpriv, COMP_POWER, DBG_WARNING,
449                                  "Pattern[%d] is too long\n", i);
450                         continue;
451                 }
452                 pattern_os = patterns[i].pattern;
453                 mask_len = DIV_ROUND_UP(patterns[i].pattern_len, 8);
454                 mask_os = patterns[i].mask;
455                 RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
456                               "pattern content\n", pattern_os,
457                                patterns[i].pattern_len);
458                 RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
459                               "mask content\n", mask_os, mask_len);
460                 /* 1. unicast? multicast? or broadcast? */
461                 if (memcmp(pattern_os, broadcast_addr, 6) == 0)
462                         rtl_pattern.type = BROADCAST_PATTERN;
463                 else if (memcmp(pattern_os, multicast_addr1, 2) == 0 ||
464                          memcmp(pattern_os, multicast_addr2, 3) == 0)
465                         rtl_pattern.type = MULTICAST_PATTERN;
466                 else if  (memcmp(pattern_os, mac->mac_addr, 6) == 0)
467                         rtl_pattern.type = UNICAST_PATTERN;
468                 else
469                         rtl_pattern.type = UNKNOWN_TYPE;
470
471                 /* 2. translate mask_from_os to mask_for_hw */
472
473 /******************************************************************************
474  * pattern from OS uses 'ethenet frame', like this:
475
476                    |    6   |    6   |   2  |     20    |  Variable  |  4  |
477                    |--------+--------+------+-----------+------------+-----|
478                    |    802.3 Mac Header    | IP Header | TCP Packet | FCS |
479                    |   DA   |   SA   | Type |
480
481  * BUT, packet catched by our HW is in '802.11 frame', begin from LLC,
482
483         |     24 or 30      |    6   |   2  |     20    |  Variable  |  4  |
484         |-------------------+--------+------+-----------+------------+-----|
485         | 802.11 MAC Header |       LLC     | IP Header | TCP Packet | FCS |
486                             | Others | Tpye |
487
488  * Therefore, we need translate mask_from_OS to mask_to_hw.
489  * We should left-shift mask by 6 bits, then set the new bit[0~5] = 0,
490  * because new mask[0~5] means 'SA', but our HW packet begins from LLC,
491  * bit[0~5] corresponds to first 6 Bytes in LLC, they just don't match.
492  ******************************************************************************/
493
494                 /* Shift 6 bits */
495                 for (j = 0; j < mask_len - 1; j++) {
496                         mask[j] = mask_os[j] >> 6;
497                         mask[j] |= (mask_os[j + 1] & 0x3F) << 2;
498                 }
499                 mask[j] = (mask_os[j] >> 6) & 0x3F;
500                 /* Set bit 0-5 to zero */
501                 mask[0] &= 0xC0;
502
503                 RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
504                               "mask to hw\n", mask, mask_len);
505                 for (j = 0; j < (MAX_WOL_BIT_MASK_SIZE + 1) / 4; j++) {
506                         rtl_pattern.mask[j] = mask[j * 4];
507                         rtl_pattern.mask[j] |= (mask[j * 4 + 1] << 8);
508                         rtl_pattern.mask[j] |= (mask[j * 4 + 2] << 16);
509                         rtl_pattern.mask[j] |= (mask[j * 4 + 3] << 24);
510                 }
511
512                 /* To get the wake up pattern from the mask.
513                  * We do not count first 12 bits which means
514                  * DA[6] and SA[6] in the pattern to match HW design.
515                  */
516                 len = 0;
517                 for (j = 12; j < patterns[i].pattern_len; j++) {
518                         if ((mask_os[j / 8] >> (j % 8)) & 0x01) {
519                                 content[len] = pattern_os[j];
520                                 len++;
521                         }
522                 }
523
524                 RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
525                               "pattern to hw\n", content, len);
526                 /* 3. calculate crc */
527                 rtl_pattern.crc = _calculate_wol_pattern_crc(content, len);
528                 RT_TRACE(rtlpriv, COMP_POWER, DBG_TRACE,
529                          "CRC_Remainder = 0x%x", rtl_pattern.crc);
530
531                 /* 4. write crc & mask_for_hw to hw */
532                 rtlpriv->cfg->ops->add_wowlan_pattern(hw, &rtl_pattern, i);
533         }
534         rtl_write_byte(rtlpriv, 0x698, wow->n_patterns);
535 }
536
537 static int rtl_op_suspend(struct ieee80211_hw *hw,
538                           struct cfg80211_wowlan *wow)
539 {
540         struct rtl_priv *rtlpriv = rtl_priv(hw);
541         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
542         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
543         struct timeval ts;
544
545         RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG, "\n");
546         if (WARN_ON(!wow))
547                 return -EINVAL;
548
549         /* to resolve s4 can not wake up*/
550         do_gettimeofday(&ts);
551         rtlhal->last_suspend_sec = ts.tv_sec;
552
553         if ((ppsc->wo_wlan_mode & WAKE_ON_PATTERN_MATCH) && wow->n_patterns)
554                 _rtl_add_wowlan_patterns(hw, wow);
555
556         rtlhal->driver_is_goingto_unload = true;
557         rtlhal->enter_pnp_sleep = true;
558
559         rtl_lps_leave(hw);
560         rtl_op_stop(hw);
561         device_set_wakeup_enable(wiphy_dev(hw->wiphy), true);
562         return 0;
563 }
564
565 static int rtl_op_resume(struct ieee80211_hw *hw)
566 {
567         struct rtl_priv *rtlpriv = rtl_priv(hw);
568         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
569         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
570         struct timeval ts;
571
572         RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG, "\n");
573         rtlhal->driver_is_goingto_unload = false;
574         rtlhal->enter_pnp_sleep = false;
575         rtlhal->wake_from_pnp_sleep = true;
576
577         /* to resovle s4 can not wake up*/
578         do_gettimeofday(&ts);
579         if (ts.tv_sec - rtlhal->last_suspend_sec < 5)
580                 return -1;
581
582         rtl_op_start(hw);
583         device_set_wakeup_enable(wiphy_dev(hw->wiphy), false);
584         ieee80211_resume_disconnect(mac->vif);
585         rtlhal->wake_from_pnp_sleep = false;
586         return 0;
587 }
588 #endif
589
590 static int rtl_op_config(struct ieee80211_hw *hw, u32 changed)
591 {
592         struct rtl_priv *rtlpriv = rtl_priv(hw);
593         struct rtl_phy *rtlphy = &(rtlpriv->phy);
594         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
595         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
596         struct ieee80211_conf *conf = &hw->conf;
597
598         if (mac->skip_scan)
599                 return 1;
600
601         mutex_lock(&rtlpriv->locks.conf_mutex);
602         if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {  /* BIT(2)*/
603                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
604                          "IEEE80211_CONF_CHANGE_LISTEN_INTERVAL\n");
605         }
606
607         /*For IPS */
608         if (changed & IEEE80211_CONF_CHANGE_IDLE) {
609                 if (hw->conf.flags & IEEE80211_CONF_IDLE)
610                         rtl_ips_nic_off(hw);
611                 else
612                         rtl_ips_nic_on(hw);
613         } else {
614                 /*
615                  *although rfoff may not cause by ips, but we will
616                  *check the reason in set_rf_power_state function
617                  */
618                 if (unlikely(ppsc->rfpwr_state == ERFOFF))
619                         rtl_ips_nic_on(hw);
620         }
621
622         /*For LPS */
623         if (changed & IEEE80211_CONF_CHANGE_PS) {
624                 cancel_delayed_work(&rtlpriv->works.ps_work);
625                 cancel_delayed_work(&rtlpriv->works.ps_rfon_wq);
626                 if (conf->flags & IEEE80211_CONF_PS) {
627                         rtlpriv->psc.sw_ps_enabled = true;
628                         /* sleep here is must, or we may recv the beacon and
629                          * cause mac80211 into wrong ps state, this will cause
630                          * power save nullfunc send fail, and further cause
631                          * pkt loss, So sleep must quickly but not immediatly
632                          * because that will cause nullfunc send by mac80211
633                          * fail, and cause pkt loss, we have tested that 5mA
634                          * is worked very well */
635                         if (!rtlpriv->psc.multi_buffered)
636                                 queue_delayed_work(rtlpriv->works.rtl_wq,
637                                                    &rtlpriv->works.ps_work,
638                                                    MSECS(5));
639                 } else {
640                         rtl_swlps_rf_awake(hw);
641                         rtlpriv->psc.sw_ps_enabled = false;
642                 }
643         }
644
645         if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
646                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
647                          "IEEE80211_CONF_CHANGE_RETRY_LIMITS %x\n",
648                          hw->conf.long_frame_max_tx_count);
649                 mac->retry_long = hw->conf.long_frame_max_tx_count;
650                 mac->retry_short = hw->conf.long_frame_max_tx_count;
651                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RETRY_LIMIT,
652                                 (u8 *)(&hw->conf.long_frame_max_tx_count));
653         }
654
655         if (changed & IEEE80211_CONF_CHANGE_CHANNEL &&
656             !rtlpriv->proximity.proxim_on) {
657                 struct ieee80211_channel *channel = hw->conf.chandef.chan;
658                 enum nl80211_chan_width width = hw->conf.chandef.width;
659                 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
660                 u8 wide_chan = (u8) channel->hw_value;
661
662                 /* channel_type is for 20&40M */
663                 if (width < NL80211_CHAN_WIDTH_80)
664                         channel_type =
665                                 cfg80211_get_chandef_type(&hw->conf.chandef);
666                 if (mac->act_scanning)
667                         mac->n_channels++;
668
669                 if (rtlpriv->dm.supp_phymode_switch &&
670                         mac->link_state < MAC80211_LINKED &&
671                         !mac->act_scanning) {
672                         if (rtlpriv->cfg->ops->chk_switch_dmdp)
673                                 rtlpriv->cfg->ops->chk_switch_dmdp(hw);
674                 }
675
676                 /*
677                  *because we should back channel to
678                  *current_network.chan in in scanning,
679                  *So if set_chan == current_network.chan
680                  *we should set it.
681                  *because mac80211 tell us wrong bw40
682                  *info for cisco1253 bw20, so we modify
683                  *it here based on UPPER & LOWER
684                  */
685
686                 if (width >= NL80211_CHAN_WIDTH_80) {
687                         if (width == NL80211_CHAN_WIDTH_80) {
688                                 u32 center = hw->conf.chandef.center_freq1;
689                                 u32 primary =
690                                 (u32)hw->conf.chandef.chan->center_freq;
691
692                                 rtlphy->current_chan_bw =
693                                         HT_CHANNEL_WIDTH_80;
694                                 mac->bw_80 = true;
695                                 mac->bw_40 = true;
696                                 if (center > primary) {
697                                         mac->cur_80_prime_sc =
698                                         PRIME_CHNL_OFFSET_LOWER;
699                                         if (center - primary == 10) {
700                                                 mac->cur_40_prime_sc =
701                                                 PRIME_CHNL_OFFSET_UPPER;
702
703                                                 wide_chan += 2;
704                                         } else if (center - primary == 30) {
705                                                 mac->cur_40_prime_sc =
706                                                 PRIME_CHNL_OFFSET_LOWER;
707
708                                                 wide_chan += 6;
709                                         }
710                                 } else {
711                                         mac->cur_80_prime_sc =
712                                         PRIME_CHNL_OFFSET_UPPER;
713                                         if (primary - center == 10) {
714                                                 mac->cur_40_prime_sc =
715                                                 PRIME_CHNL_OFFSET_LOWER;
716
717                                                 wide_chan -= 2;
718                                         } else if (primary - center == 30) {
719                                                 mac->cur_40_prime_sc =
720                                                 PRIME_CHNL_OFFSET_UPPER;
721
722                                                 wide_chan -= 6;
723                                         }
724                                 }
725                         }
726                 } else {
727                         switch (channel_type) {
728                         case NL80211_CHAN_HT20:
729                         case NL80211_CHAN_NO_HT:
730                                         /* SC */
731                                         mac->cur_40_prime_sc =
732                                                 PRIME_CHNL_OFFSET_DONT_CARE;
733                                         rtlphy->current_chan_bw =
734                                                 HT_CHANNEL_WIDTH_20;
735                                         mac->bw_40 = false;
736                                         mac->bw_80 = false;
737                                         break;
738                         case NL80211_CHAN_HT40MINUS:
739                                         /* SC */
740                                         mac->cur_40_prime_sc =
741                                                 PRIME_CHNL_OFFSET_UPPER;
742                                         rtlphy->current_chan_bw =
743                                                 HT_CHANNEL_WIDTH_20_40;
744                                         mac->bw_40 = true;
745                                         mac->bw_80 = false;
746
747                                         /*wide channel */
748                                         wide_chan -= 2;
749
750                                         break;
751                         case NL80211_CHAN_HT40PLUS:
752                                         /* SC */
753                                         mac->cur_40_prime_sc =
754                                                 PRIME_CHNL_OFFSET_LOWER;
755                                         rtlphy->current_chan_bw =
756                                                 HT_CHANNEL_WIDTH_20_40;
757                                         mac->bw_40 = true;
758                                         mac->bw_80 = false;
759
760                                         /*wide channel */
761                                         wide_chan += 2;
762
763                                         break;
764                         default:
765                                         mac->bw_40 = false;
766                                         mac->bw_80 = false;
767                                         RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
768                                                  "switch case not processed\n");
769                                         break;
770                         }
771                 }
772
773                 if (wide_chan <= 0)
774                         wide_chan = 1;
775
776                 /* In scanning, when before we offchannel we may send a ps=1
777                  * null to AP, and then we may send a ps = 0 null to AP quickly,
778                  * but first null may have caused AP to put lots of packet to
779                  * hw tx buffer. These packets must be tx'd before we go off
780                  * channel so we must delay more time to let AP flush these
781                  * packets before going offchannel, or dis-association or
782                  * delete BA will be caused by AP
783                  */
784                 if (rtlpriv->mac80211.offchan_delay) {
785                         rtlpriv->mac80211.offchan_delay = false;
786                         mdelay(50);
787                 }
788
789                 rtlphy->current_channel = wide_chan;
790
791                 rtlpriv->cfg->ops->switch_channel(hw);
792                 rtlpriv->cfg->ops->set_channel_access(hw);
793                 rtlpriv->cfg->ops->set_bw_mode(hw, channel_type);
794         }
795
796         mutex_unlock(&rtlpriv->locks.conf_mutex);
797
798         return 0;
799 }
800
801 static void rtl_op_configure_filter(struct ieee80211_hw *hw,
802                                     unsigned int changed_flags,
803                                     unsigned int *new_flags, u64 multicast)
804 {
805         bool update_rcr = false;
806         struct rtl_priv *rtlpriv = rtl_priv(hw);
807         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
808
809         *new_flags &= RTL_SUPPORTED_FILTERS;
810         if (0 == changed_flags)
811                 return;
812
813         /*TODO: we disable broadcase now, so enable here */
814         if (changed_flags & FIF_ALLMULTI) {
815                 if (*new_flags & FIF_ALLMULTI) {
816                         mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_AM] |
817                             rtlpriv->cfg->maps[MAC_RCR_AB];
818                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
819                                  "Enable receive multicast frame\n");
820                 } else {
821                         mac->rx_conf &= ~(rtlpriv->cfg->maps[MAC_RCR_AM] |
822                                           rtlpriv->cfg->maps[MAC_RCR_AB]);
823                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
824                                  "Disable receive multicast frame\n");
825                 }
826                 update_rcr = true;
827         }
828
829         if (changed_flags & FIF_FCSFAIL) {
830                 if (*new_flags & FIF_FCSFAIL) {
831                         mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_ACRC32];
832                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
833                                  "Enable receive FCS error frame\n");
834                 } else {
835                         mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_ACRC32];
836                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
837                                  "Disable receive FCS error frame\n");
838                 }
839                 if (!update_rcr)
840                         update_rcr = true;
841         }
842
843         /* if ssid not set to hw don't check bssid
844          * here just used for linked scanning, & linked
845          * and nolink check bssid is set in set network_type
846          */
847         if ((changed_flags & FIF_BCN_PRBRESP_PROMISC) &&
848             (mac->link_state >= MAC80211_LINKED)) {
849                 if (mac->opmode != NL80211_IFTYPE_AP &&
850                     mac->opmode != NL80211_IFTYPE_MESH_POINT) {
851                         if (*new_flags & FIF_BCN_PRBRESP_PROMISC)
852                                 rtlpriv->cfg->ops->set_chk_bssid(hw, false);
853                         else
854                                 rtlpriv->cfg->ops->set_chk_bssid(hw, true);
855                         if (update_rcr)
856                                 update_rcr = false;
857                 }
858         }
859
860         if (changed_flags & FIF_CONTROL) {
861                 if (*new_flags & FIF_CONTROL) {
862                         mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_ACF];
863
864                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
865                                  "Enable receive control frame.\n");
866                 } else {
867                         mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_ACF];
868                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
869                                  "Disable receive control frame.\n");
870                 }
871                 if (!update_rcr)
872                         update_rcr = true;
873         }
874
875         if (changed_flags & FIF_OTHER_BSS) {
876                 if (*new_flags & FIF_OTHER_BSS) {
877                         mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_AAP];
878                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
879                                  "Enable receive other BSS's frame.\n");
880                 } else {
881                         mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_AAP];
882                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
883                                  "Disable receive other BSS's frame.\n");
884                 }
885                 if (!update_rcr)
886                         update_rcr = true;
887         }
888
889         if (update_rcr)
890                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RCR,
891                                               (u8 *)(&mac->rx_conf));
892 }
893 static int rtl_op_sta_add(struct ieee80211_hw *hw,
894                          struct ieee80211_vif *vif,
895                          struct ieee80211_sta *sta)
896 {
897         struct rtl_priv *rtlpriv = rtl_priv(hw);
898         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
899         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
900         struct rtl_sta_info *sta_entry;
901
902         if (sta) {
903                 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
904                 spin_lock_bh(&rtlpriv->locks.entry_list_lock);
905                 list_add_tail(&sta_entry->list, &rtlpriv->entry_list);
906                 spin_unlock_bh(&rtlpriv->locks.entry_list_lock);
907                 if (rtlhal->current_bandtype == BAND_ON_2_4G) {
908                         sta_entry->wireless_mode = WIRELESS_MODE_G;
909                         if (sta->supp_rates[0] <= 0xf)
910                                 sta_entry->wireless_mode = WIRELESS_MODE_B;
911                         if (sta->ht_cap.ht_supported)
912                                 sta_entry->wireless_mode = WIRELESS_MODE_N_24G;
913
914                         if (vif->type == NL80211_IFTYPE_ADHOC)
915                                 sta_entry->wireless_mode = WIRELESS_MODE_G;
916                 } else if (rtlhal->current_bandtype == BAND_ON_5G) {
917                         sta_entry->wireless_mode = WIRELESS_MODE_A;
918                         if (sta->ht_cap.ht_supported)
919                                 sta_entry->wireless_mode = WIRELESS_MODE_N_5G;
920                         if (sta->vht_cap.vht_supported)
921                                 sta_entry->wireless_mode = WIRELESS_MODE_AC_5G;
922
923                         if (vif->type == NL80211_IFTYPE_ADHOC)
924                                 sta_entry->wireless_mode = WIRELESS_MODE_A;
925                 }
926                 /*disable cck rate for p2p*/
927                 if (mac->p2p)
928                         sta->supp_rates[0] &= 0xfffffff0;
929
930                 memcpy(sta_entry->mac_addr, sta->addr, ETH_ALEN);
931                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
932                         "Add sta addr is %pM\n", sta->addr);
933                 rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0);
934         }
935
936         return 0;
937 }
938
939 static int rtl_op_sta_remove(struct ieee80211_hw *hw,
940                                 struct ieee80211_vif *vif,
941                                 struct ieee80211_sta *sta)
942 {
943         struct rtl_priv *rtlpriv = rtl_priv(hw);
944         struct rtl_sta_info *sta_entry;
945         if (sta) {
946                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
947                          "Remove sta addr is %pM\n", sta->addr);
948                 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
949                 sta_entry->wireless_mode = 0;
950                 sta_entry->ratr_index = 0;
951                 spin_lock_bh(&rtlpriv->locks.entry_list_lock);
952                 list_del(&sta_entry->list);
953                 spin_unlock_bh(&rtlpriv->locks.entry_list_lock);
954         }
955         return 0;
956 }
957 static int _rtl_get_hal_qnum(u16 queue)
958 {
959         int qnum;
960
961         switch (queue) {
962         case 0:
963                 qnum = AC3_VO;
964                 break;
965         case 1:
966                 qnum = AC2_VI;
967                 break;
968         case 2:
969                 qnum = AC0_BE;
970                 break;
971         case 3:
972                 qnum = AC1_BK;
973                 break;
974         default:
975                 qnum = AC0_BE;
976                 break;
977         }
978         return qnum;
979 }
980
981 /*
982  *for mac80211 VO = 0, VI = 1, BE = 2, BK = 3
983  *for rtl819x  BE = 0, BK = 1, VI = 2, VO = 3
984  */
985 static int rtl_op_conf_tx(struct ieee80211_hw *hw,
986                           struct ieee80211_vif *vif, u16 queue,
987                           const struct ieee80211_tx_queue_params *param)
988 {
989         struct rtl_priv *rtlpriv = rtl_priv(hw);
990         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
991         int aci;
992
993         if (queue >= AC_MAX) {
994                 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
995                          "queue number %d is incorrect!\n", queue);
996                 return -EINVAL;
997         }
998
999         aci = _rtl_get_hal_qnum(queue);
1000         mac->ac[aci].aifs = param->aifs;
1001         mac->ac[aci].cw_min = cpu_to_le16(param->cw_min);
1002         mac->ac[aci].cw_max = cpu_to_le16(param->cw_max);
1003         mac->ac[aci].tx_op = cpu_to_le16(param->txop);
1004         memcpy(&mac->edca_param[aci], param, sizeof(*param));
1005         rtlpriv->cfg->ops->set_qos(hw, aci);
1006         return 0;
1007 }
1008
1009 static void send_beacon_frame(struct ieee80211_hw *hw,
1010                               struct ieee80211_vif *vif)
1011 {
1012         struct rtl_priv *rtlpriv = rtl_priv(hw);
1013         struct sk_buff *skb = ieee80211_beacon_get(hw, vif);
1014         struct rtl_tcb_desc tcb_desc;
1015
1016         if (skb) {
1017                 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
1018                 rtlpriv->intf_ops->adapter_tx(hw, NULL, skb, &tcb_desc);
1019         }
1020 }
1021
1022 static void rtl_op_bss_info_changed(struct ieee80211_hw *hw,
1023                                     struct ieee80211_vif *vif,
1024                                     struct ieee80211_bss_conf *bss_conf,
1025                                     u32 changed)
1026 {
1027         struct rtl_priv *rtlpriv = rtl_priv(hw);
1028         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
1029         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1030         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1031
1032         mutex_lock(&rtlpriv->locks.conf_mutex);
1033         if ((vif->type == NL80211_IFTYPE_ADHOC) ||
1034             (vif->type == NL80211_IFTYPE_AP) ||
1035             (vif->type == NL80211_IFTYPE_MESH_POINT)) {
1036                 if ((changed & BSS_CHANGED_BEACON) ||
1037                     (changed & BSS_CHANGED_BEACON_ENABLED &&
1038                      bss_conf->enable_beacon)) {
1039                         if (mac->beacon_enabled == 0) {
1040                                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
1041                                          "BSS_CHANGED_BEACON_ENABLED\n");
1042
1043                                 /*start hw beacon interrupt. */
1044                                 /*rtlpriv->cfg->ops->set_bcn_reg(hw); */
1045                                 mac->beacon_enabled = 1;
1046                                 rtlpriv->cfg->ops->update_interrupt_mask(hw,
1047                                                 rtlpriv->cfg->maps
1048                                                 [RTL_IBSS_INT_MASKS], 0);
1049
1050                                 if (rtlpriv->cfg->ops->linked_set_reg)
1051                                         rtlpriv->cfg->ops->linked_set_reg(hw);
1052                                 send_beacon_frame(hw, vif);
1053                         }
1054                 }
1055                 if ((changed & BSS_CHANGED_BEACON_ENABLED &&
1056                     !bss_conf->enable_beacon)) {
1057                         if (mac->beacon_enabled == 1) {
1058                                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
1059                                          "ADHOC DISABLE BEACON\n");
1060
1061                                 mac->beacon_enabled = 0;
1062                                 rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
1063                                                 rtlpriv->cfg->maps
1064                                                 [RTL_IBSS_INT_MASKS]);
1065                         }
1066                 }
1067                 if (changed & BSS_CHANGED_BEACON_INT) {
1068                         RT_TRACE(rtlpriv, COMP_BEACON, DBG_TRACE,
1069                                  "BSS_CHANGED_BEACON_INT\n");
1070                         mac->beacon_interval = bss_conf->beacon_int;
1071                         rtlpriv->cfg->ops->set_bcn_intv(hw);
1072                 }
1073         }
1074
1075         /*TODO: reference to enum ieee80211_bss_change */
1076         if (changed & BSS_CHANGED_ASSOC) {
1077                 u8 mstatus;
1078                 if (bss_conf->assoc) {
1079                         struct ieee80211_sta *sta = NULL;
1080                         u8 keep_alive = 10;
1081
1082                         mstatus = RT_MEDIA_CONNECT;
1083                         /* we should reset all sec info & cam
1084                          * before set cam after linked, we should not
1085                          * reset in disassoc, that will cause tkip->wep
1086                          * fail because some flag will be wrong */
1087                         /* reset sec info */
1088                         rtl_cam_reset_sec_info(hw);
1089                         /* reset cam to fix wep fail issue
1090                          * when change from wpa to wep */
1091                         rtl_cam_reset_all_entry(hw);
1092
1093                         mac->link_state = MAC80211_LINKED;
1094                         mac->cnt_after_linked = 0;
1095                         mac->assoc_id = bss_conf->aid;
1096                         memcpy(mac->bssid, bss_conf->bssid, ETH_ALEN);
1097
1098                         if (rtlpriv->cfg->ops->linked_set_reg)
1099                                 rtlpriv->cfg->ops->linked_set_reg(hw);
1100
1101                         rcu_read_lock();
1102                         sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
1103                         if (!sta) {
1104                                 rcu_read_unlock();
1105                                 goto out;
1106                         }
1107                         RT_TRACE(rtlpriv, COMP_EASY_CONCURRENT, DBG_LOUD,
1108                                  "send PS STATIC frame\n");
1109                         if (rtlpriv->dm.supp_phymode_switch) {
1110                                 if (sta->ht_cap.ht_supported)
1111                                         rtl_send_smps_action(hw, sta,
1112                                                         IEEE80211_SMPS_STATIC);
1113                         }
1114
1115                         if (rtlhal->current_bandtype == BAND_ON_5G) {
1116                                 mac->mode = WIRELESS_MODE_A;
1117                         } else {
1118                                 if (sta->supp_rates[0] <= 0xf)
1119                                         mac->mode = WIRELESS_MODE_B;
1120                                 else
1121                                         mac->mode = WIRELESS_MODE_G;
1122                         }
1123
1124                         if (sta->ht_cap.ht_supported) {
1125                                 if (rtlhal->current_bandtype == BAND_ON_2_4G)
1126                                         mac->mode = WIRELESS_MODE_N_24G;
1127                                 else
1128                                         mac->mode = WIRELESS_MODE_N_5G;
1129                         }
1130
1131                         if (sta->vht_cap.vht_supported) {
1132                                 if (rtlhal->current_bandtype == BAND_ON_5G)
1133                                         mac->mode = WIRELESS_MODE_AC_5G;
1134                                 else
1135                                         mac->mode = WIRELESS_MODE_AC_24G;
1136                         }
1137
1138                         if (vif->type == NL80211_IFTYPE_STATION && sta)
1139                                 rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0);
1140                         rcu_read_unlock();
1141
1142                         /* to avoid AP Disassociation caused by inactivity */
1143                         rtlpriv->cfg->ops->set_hw_reg(hw,
1144                                                       HW_VAR_KEEP_ALIVE,
1145                                                       (u8 *)(&keep_alive));
1146
1147                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
1148                                  "BSS_CHANGED_ASSOC\n");
1149                 } else {
1150                         mstatus = RT_MEDIA_DISCONNECT;
1151
1152                         if (mac->link_state == MAC80211_LINKED) {
1153                                 rtlpriv->enter_ps = false;
1154                                 schedule_work(&rtlpriv->works.lps_change_work);
1155                         }
1156                         if (ppsc->p2p_ps_info.p2p_ps_mode > P2P_PS_NONE)
1157                                 rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
1158                         mac->link_state = MAC80211_NOLINK;
1159                         eth_zero_addr(mac->bssid);
1160                         mac->vendor = PEER_UNKNOWN;
1161                         mac->mode = 0;
1162
1163                         if (rtlpriv->dm.supp_phymode_switch) {
1164                                 if (rtlpriv->cfg->ops->chk_switch_dmdp)
1165                                         rtlpriv->cfg->ops->chk_switch_dmdp(hw);
1166                         }
1167                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
1168                                  "BSS_CHANGED_UN_ASSOC\n");
1169                 }
1170                 rtlpriv->cfg->ops->set_network_type(hw, vif->type);
1171                 /* For FW LPS:
1172                  * To tell firmware we have connected or disconnected
1173                  */
1174                 rtlpriv->cfg->ops->set_hw_reg(hw,
1175                                               HW_VAR_H2C_FW_JOINBSSRPT,
1176                                               (u8 *)(&mstatus));
1177                 ppsc->report_linked = (mstatus == RT_MEDIA_CONNECT) ?
1178                                       true : false;
1179
1180                 if (rtlpriv->cfg->ops->get_btc_status())
1181                         rtlpriv->btcoexist.btc_ops->btc_mediastatus_notify(
1182                                                         rtlpriv, mstatus);
1183         }
1184
1185         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1186                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1187                          "BSS_CHANGED_ERP_CTS_PROT\n");
1188                 mac->use_cts_protect = bss_conf->use_cts_prot;
1189         }
1190
1191         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1192                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
1193                          "BSS_CHANGED_ERP_PREAMBLE use short preamble:%x\n",
1194                           bss_conf->use_short_preamble);
1195
1196                 mac->short_preamble = bss_conf->use_short_preamble;
1197                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_ACK_PREAMBLE,
1198                                               (u8 *)(&mac->short_preamble));
1199         }
1200
1201         if (changed & BSS_CHANGED_ERP_SLOT) {
1202                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1203                          "BSS_CHANGED_ERP_SLOT\n");
1204
1205                 if (bss_conf->use_short_slot)
1206                         mac->slot_time = RTL_SLOT_TIME_9;
1207                 else
1208                         mac->slot_time = RTL_SLOT_TIME_20;
1209
1210                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SLOT_TIME,
1211                                               (u8 *)(&mac->slot_time));
1212         }
1213
1214         if (changed & BSS_CHANGED_HT) {
1215                 struct ieee80211_sta *sta = NULL;
1216
1217                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1218                          "BSS_CHANGED_HT\n");
1219
1220                 rcu_read_lock();
1221                 sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
1222                 if (sta) {
1223                         if (sta->ht_cap.ampdu_density >
1224                             mac->current_ampdu_density)
1225                                 mac->current_ampdu_density =
1226                                     sta->ht_cap.ampdu_density;
1227                         if (sta->ht_cap.ampdu_factor <
1228                             mac->current_ampdu_factor)
1229                                 mac->current_ampdu_factor =
1230                                     sta->ht_cap.ampdu_factor;
1231                 }
1232                 rcu_read_unlock();
1233
1234                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SHORTGI_DENSITY,
1235                                               (u8 *)(&mac->max_mss_density));
1236                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AMPDU_FACTOR,
1237                                               &mac->current_ampdu_factor);
1238                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AMPDU_MIN_SPACE,
1239                                               &mac->current_ampdu_density);
1240         }
1241
1242         if (changed & BSS_CHANGED_BSSID) {
1243                 u32 basic_rates;
1244                 struct ieee80211_sta *sta = NULL;
1245
1246                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BSSID,
1247                                               (u8 *)bss_conf->bssid);
1248
1249                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
1250                          "bssid: %pM\n", bss_conf->bssid);
1251
1252                 mac->vendor = PEER_UNKNOWN;
1253                 memcpy(mac->bssid, bss_conf->bssid, ETH_ALEN);
1254
1255                 rcu_read_lock();
1256                 sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
1257                 if (!sta) {
1258                         rcu_read_unlock();
1259                         goto out;
1260                 }
1261
1262                 if (rtlhal->current_bandtype == BAND_ON_5G) {
1263                         mac->mode = WIRELESS_MODE_A;
1264                 } else {
1265                         if (sta->supp_rates[0] <= 0xf)
1266                                 mac->mode = WIRELESS_MODE_B;
1267                         else
1268                                 mac->mode = WIRELESS_MODE_G;
1269                 }
1270
1271                 if (sta->ht_cap.ht_supported) {
1272                         if (rtlhal->current_bandtype == BAND_ON_2_4G)
1273                                 mac->mode = WIRELESS_MODE_N_24G;
1274                         else
1275                                 mac->mode = WIRELESS_MODE_N_5G;
1276                 }
1277
1278                 if (sta->vht_cap.vht_supported) {
1279                         if (rtlhal->current_bandtype == BAND_ON_5G)
1280                                 mac->mode = WIRELESS_MODE_AC_5G;
1281                         else
1282                                 mac->mode = WIRELESS_MODE_AC_24G;
1283                 }
1284
1285                 /* just station need it, because ibss & ap mode will
1286                  * set in sta_add, and will be NULL here */
1287                 if (vif->type == NL80211_IFTYPE_STATION) {
1288                         struct rtl_sta_info *sta_entry;
1289                         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1290                         sta_entry->wireless_mode = mac->mode;
1291                 }
1292
1293                 if (sta->ht_cap.ht_supported) {
1294                         mac->ht_enable = true;
1295
1296                         /*
1297                          * for cisco 1252 bw20 it's wrong
1298                          * if (ht_cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
1299                          *      mac->bw_40 = true;
1300                          * }
1301                          * */
1302                 }
1303
1304                 if (sta->vht_cap.vht_supported)
1305                         mac->vht_enable = true;
1306
1307                 if (changed & BSS_CHANGED_BASIC_RATES) {
1308                         /* for 5G must << RATE_6M_INDEX = 4,
1309                          * because 5G have no cck rate*/
1310                         if (rtlhal->current_bandtype == BAND_ON_5G)
1311                                 basic_rates = sta->supp_rates[1] << 4;
1312                         else
1313                                 basic_rates = sta->supp_rates[0];
1314
1315                         mac->basic_rates = basic_rates;
1316                         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
1317                                         (u8 *)(&basic_rates));
1318                 }
1319                 rcu_read_unlock();
1320         }
1321 out:
1322         mutex_unlock(&rtlpriv->locks.conf_mutex);
1323 }
1324
1325 static u64 rtl_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1326 {
1327         struct rtl_priv *rtlpriv = rtl_priv(hw);
1328         u64 tsf;
1329
1330         rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_CORRECT_TSF, (u8 *)(&tsf));
1331         return tsf;
1332 }
1333
1334 static void rtl_op_set_tsf(struct ieee80211_hw *hw,
1335                            struct ieee80211_vif *vif, u64 tsf)
1336 {
1337         struct rtl_priv *rtlpriv = rtl_priv(hw);
1338         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1339         u8 bibss = (mac->opmode == NL80211_IFTYPE_ADHOC) ? 1 : 0;
1340
1341         mac->tsf = tsf;
1342         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_CORRECT_TSF, (u8 *)(&bibss));
1343 }
1344
1345 static void rtl_op_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1346 {
1347         struct rtl_priv *rtlpriv = rtl_priv(hw);
1348         u8 tmp = 0;
1349
1350         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_DUAL_TSF_RST, (u8 *)(&tmp));
1351 }
1352
1353 static void rtl_op_sta_notify(struct ieee80211_hw *hw,
1354                               struct ieee80211_vif *vif,
1355                               enum sta_notify_cmd cmd,
1356                               struct ieee80211_sta *sta)
1357 {
1358         switch (cmd) {
1359         case STA_NOTIFY_SLEEP:
1360                 break;
1361         case STA_NOTIFY_AWAKE:
1362                 break;
1363         default:
1364                 break;
1365         }
1366 }
1367
1368 static int rtl_op_ampdu_action(struct ieee80211_hw *hw,
1369                                struct ieee80211_vif *vif,
1370                                struct ieee80211_ampdu_params *params)
1371 {
1372         struct rtl_priv *rtlpriv = rtl_priv(hw);
1373         struct ieee80211_sta *sta = params->sta;
1374         enum ieee80211_ampdu_mlme_action action = params->action;
1375         u16 tid = params->tid;
1376         u16 *ssn = &params->ssn;
1377
1378         switch (action) {
1379         case IEEE80211_AMPDU_TX_START:
1380                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1381                          "IEEE80211_AMPDU_TX_START: TID:%d\n", tid);
1382                 return rtl_tx_agg_start(hw, vif, sta, tid, ssn);
1383         case IEEE80211_AMPDU_TX_STOP_CONT:
1384         case IEEE80211_AMPDU_TX_STOP_FLUSH:
1385         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1386                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1387                          "IEEE80211_AMPDU_TX_STOP: TID:%d\n", tid);
1388                 return rtl_tx_agg_stop(hw, vif, sta, tid);
1389         case IEEE80211_AMPDU_TX_OPERATIONAL:
1390                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1391                          "IEEE80211_AMPDU_TX_OPERATIONAL:TID:%d\n", tid);
1392                 rtl_tx_agg_oper(hw, sta, tid);
1393                 break;
1394         case IEEE80211_AMPDU_RX_START:
1395                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1396                          "IEEE80211_AMPDU_RX_START:TID:%d\n", tid);
1397                 return rtl_rx_agg_start(hw, sta, tid);
1398         case IEEE80211_AMPDU_RX_STOP:
1399                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1400                          "IEEE80211_AMPDU_RX_STOP:TID:%d\n", tid);
1401                 return rtl_rx_agg_stop(hw, sta, tid);
1402         default:
1403                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
1404                          "IEEE80211_AMPDU_ERR!!!!:\n");
1405                 return -EOPNOTSUPP;
1406         }
1407         return 0;
1408 }
1409
1410 static void rtl_op_sw_scan_start(struct ieee80211_hw *hw,
1411                                  struct ieee80211_vif *vif,
1412                                  const u8 *mac_addr)
1413 {
1414         struct rtl_priv *rtlpriv = rtl_priv(hw);
1415         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1416
1417         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "\n");
1418         mac->act_scanning = true;
1419         if (rtlpriv->link_info.higher_busytraffic) {
1420                 mac->skip_scan = true;
1421                 return;
1422         }
1423
1424         if (rtlpriv->cfg->ops->get_btc_status())
1425                 rtlpriv->btcoexist.btc_ops->btc_scan_notify(rtlpriv, 1);
1426
1427         if (rtlpriv->dm.supp_phymode_switch) {
1428                 if (rtlpriv->cfg->ops->chk_switch_dmdp)
1429                         rtlpriv->cfg->ops->chk_switch_dmdp(hw);
1430         }
1431
1432         if (mac->link_state == MAC80211_LINKED) {
1433                 rtlpriv->enter_ps = false;
1434                 schedule_work(&rtlpriv->works.lps_change_work);
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         kfree_skb(pskb);
1835
1836         /*this is wrong, fill_tx_cmddesc needs update*/
1837         pdesc = &ring->desc[0];
1838
1839         rtlpriv->cfg->ops->fill_tx_cmddesc(hw, (u8 *)pdesc, 1, 1, skb);
1840
1841         __skb_queue_tail(&ring->queue, skb);
1842
1843         spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);
1844
1845         rtlpriv->cfg->ops->tx_polling(hw, BEACON_QUEUE);
1846
1847         return true;
1848 }
1849 EXPORT_SYMBOL(rtl_cmd_send_packet);
1850 const struct ieee80211_ops rtl_ops = {
1851         .start = rtl_op_start,
1852         .stop = rtl_op_stop,
1853         .tx = rtl_op_tx,
1854         .add_interface = rtl_op_add_interface,
1855         .remove_interface = rtl_op_remove_interface,
1856         .change_interface = rtl_op_change_interface,
1857 #ifdef CONFIG_PM
1858         .suspend = rtl_op_suspend,
1859         .resume = rtl_op_resume,
1860 #endif
1861         .config = rtl_op_config,
1862         .configure_filter = rtl_op_configure_filter,
1863         .set_key = rtl_op_set_key,
1864         .conf_tx = rtl_op_conf_tx,
1865         .bss_info_changed = rtl_op_bss_info_changed,
1866         .get_tsf = rtl_op_get_tsf,
1867         .set_tsf = rtl_op_set_tsf,
1868         .reset_tsf = rtl_op_reset_tsf,
1869         .sta_notify = rtl_op_sta_notify,
1870         .ampdu_action = rtl_op_ampdu_action,
1871         .sw_scan_start = rtl_op_sw_scan_start,
1872         .sw_scan_complete = rtl_op_sw_scan_complete,
1873         .rfkill_poll = rtl_op_rfkill_poll,
1874         .sta_add = rtl_op_sta_add,
1875         .sta_remove = rtl_op_sta_remove,
1876         .flush = rtl_op_flush,
1877 };
1878 EXPORT_SYMBOL_GPL(rtl_ops);
1879
1880 bool rtl_btc_status_false(void)
1881 {
1882         return false;
1883 }
1884 EXPORT_SYMBOL_GPL(rtl_btc_status_false);
1885
1886 void rtl_dm_diginit(struct ieee80211_hw *hw, u32 cur_igvalue)
1887 {
1888         struct rtl_priv *rtlpriv = rtl_priv(hw);
1889         struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
1890
1891         dm_digtable->dig_enable_flag = true;
1892         dm_digtable->dig_ext_port_stage = DIG_EXT_PORT_STAGE_MAX;
1893         dm_digtable->cur_igvalue = cur_igvalue;
1894         dm_digtable->pre_igvalue = 0;
1895         dm_digtable->cur_sta_cstate = DIG_STA_DISCONNECT;
1896         dm_digtable->presta_cstate = DIG_STA_DISCONNECT;
1897         dm_digtable->curmultista_cstate = DIG_MULTISTA_DISCONNECT;
1898         dm_digtable->rssi_lowthresh = DM_DIG_THRESH_LOW;
1899         dm_digtable->rssi_highthresh = DM_DIG_THRESH_HIGH;
1900         dm_digtable->fa_lowthresh = DM_FALSEALARM_THRESH_LOW;
1901         dm_digtable->fa_highthresh = DM_FALSEALARM_THRESH_HIGH;
1902         dm_digtable->rx_gain_max = DM_DIG_MAX;
1903         dm_digtable->rx_gain_min = DM_DIG_MIN;
1904         dm_digtable->back_val = DM_DIG_BACKOFF_DEFAULT;
1905         dm_digtable->back_range_max = DM_DIG_BACKOFF_MAX;
1906         dm_digtable->back_range_min = DM_DIG_BACKOFF_MIN;
1907         dm_digtable->pre_cck_cca_thres = 0xff;
1908         dm_digtable->cur_cck_cca_thres = 0x83;
1909         dm_digtable->forbidden_igi = DM_DIG_MIN;
1910         dm_digtable->large_fa_hit = 0;
1911         dm_digtable->recover_cnt = 0;
1912         dm_digtable->dig_min_0 = 0x25;
1913         dm_digtable->dig_min_1 = 0x25;
1914         dm_digtable->media_connect_0 = false;
1915         dm_digtable->media_connect_1 = false;
1916         rtlpriv->dm.dm_initialgain_enable = true;
1917         dm_digtable->bt30_cur_igi = 0x32;
1918         dm_digtable->pre_cck_pd_state = CCK_PD_STAGE_MAX;
1919         dm_digtable->cur_cck_pd_state = CCK_PD_STAGE_LOWRSSI;
1920 }
1921 EXPORT_SYMBOL(rtl_dm_diginit);