GNU Linux-libre 4.9.337-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 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 = reject_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("Selected firmware is not available\n");
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         release_firmware(firmware);
135 }
136
137 void rtl_fw_cb(const struct firmware *firmware, void *context)
138 {
139         rtl_fw_do_work(firmware, context, false);
140 }
141 EXPORT_SYMBOL(rtl_fw_cb);
142
143 void rtl_wowlan_fw_cb(const struct firmware *firmware, void *context)
144 {
145         rtl_fw_do_work(firmware, context, true);
146 }
147 EXPORT_SYMBOL(rtl_wowlan_fw_cb);
148
149 /*mutex for start & stop is must here. */
150 static int rtl_op_start(struct ieee80211_hw *hw)
151 {
152         int err = 0;
153         struct rtl_priv *rtlpriv = rtl_priv(hw);
154         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
155
156         if (!is_hal_stop(rtlhal))
157                 return 0;
158         if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
159                 return 0;
160         mutex_lock(&rtlpriv->locks.conf_mutex);
161         err = rtlpriv->intf_ops->adapter_start(hw);
162         if (!err)
163                 rtl_watch_dog_timer_callback((unsigned long)hw);
164         mutex_unlock(&rtlpriv->locks.conf_mutex);
165         return err;
166 }
167
168 static void rtl_op_stop(struct ieee80211_hw *hw)
169 {
170         struct rtl_priv *rtlpriv = rtl_priv(hw);
171         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
172         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
173         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
174         bool support_remote_wakeup = false;
175
176         if (is_hal_stop(rtlhal))
177                 return;
178
179         rtlpriv->cfg->ops->get_hw_reg(hw, HAL_DEF_WOWLAN,
180                                       (u8 *)(&support_remote_wakeup));
181         /* here is must, because adhoc do stop and start,
182          * but stop with RFOFF may cause something wrong,
183          * like adhoc TP
184          */
185         if (unlikely(ppsc->rfpwr_state == ERFOFF))
186                 rtl_ips_nic_on(hw);
187
188         mutex_lock(&rtlpriv->locks.conf_mutex);
189         /* if wowlan supported, DON'T clear connected info */
190         if (!(support_remote_wakeup &&
191               rtlhal->enter_pnp_sleep)) {
192                 mac->link_state = MAC80211_NOLINK;
193                 eth_zero_addr(mac->bssid);
194                 mac->vendor = PEER_UNKNOWN;
195
196                 /* reset sec info */
197                 rtl_cam_reset_sec_info(hw);
198
199                 rtl_deinit_deferred_work(hw);
200         }
201         rtlpriv->intf_ops->adapter_stop(hw);
202
203         mutex_unlock(&rtlpriv->locks.conf_mutex);
204 }
205
206 static void rtl_op_tx(struct ieee80211_hw *hw,
207                       struct ieee80211_tx_control *control,
208                       struct sk_buff *skb)
209 {
210         struct rtl_priv *rtlpriv = rtl_priv(hw);
211         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
212         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
213         struct rtl_tcb_desc tcb_desc;
214         memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
215
216         if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
217                 goto err_free;
218
219         if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
220                 goto err_free;
221
222         if (!rtlpriv->intf_ops->waitq_insert(hw, control->sta, skb))
223                 rtlpriv->intf_ops->adapter_tx(hw, control->sta, skb, &tcb_desc);
224         return;
225
226 err_free:
227         dev_kfree_skb_any(skb);
228 }
229
230 static int rtl_op_add_interface(struct ieee80211_hw *hw,
231                 struct ieee80211_vif *vif)
232 {
233         struct rtl_priv *rtlpriv = rtl_priv(hw);
234         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
235         int err = 0;
236
237         if (mac->vif) {
238                 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
239                          "vif has been set!! mac->vif = 0x%p\n", mac->vif);
240                 return -EOPNOTSUPP;
241         }
242
243         vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
244
245         rtl_ips_nic_on(hw);
246
247         mutex_lock(&rtlpriv->locks.conf_mutex);
248         switch (ieee80211_vif_type_p2p(vif)) {
249         case NL80211_IFTYPE_P2P_CLIENT:
250                 mac->p2p = P2P_ROLE_CLIENT;
251                 /*fall through*/
252         case NL80211_IFTYPE_STATION:
253                 if (mac->beacon_enabled == 1) {
254                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
255                                  "NL80211_IFTYPE_STATION\n");
256                         mac->beacon_enabled = 0;
257                         rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
258                                         rtlpriv->cfg->maps[RTL_IBSS_INT_MASKS]);
259                 }
260                 break;
261         case NL80211_IFTYPE_ADHOC:
262                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
263                          "NL80211_IFTYPE_ADHOC\n");
264
265                 mac->link_state = MAC80211_LINKED;
266                 rtlpriv->cfg->ops->set_bcn_reg(hw);
267                 if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
268                         mac->basic_rates = 0xfff;
269                 else
270                         mac->basic_rates = 0xff0;
271                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
272                                 (u8 *)(&mac->basic_rates));
273
274                 break;
275         case NL80211_IFTYPE_P2P_GO:
276                 mac->p2p = P2P_ROLE_GO;
277                 /*fall through*/
278         case NL80211_IFTYPE_AP:
279                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
280                          "NL80211_IFTYPE_AP\n");
281
282                 mac->link_state = MAC80211_LINKED;
283                 rtlpriv->cfg->ops->set_bcn_reg(hw);
284                 if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
285                         mac->basic_rates = 0xfff;
286                 else
287                         mac->basic_rates = 0xff0;
288                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
289                                               (u8 *)(&mac->basic_rates));
290                 break;
291         case NL80211_IFTYPE_MESH_POINT:
292                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
293                          "NL80211_IFTYPE_MESH_POINT\n");
294
295                 mac->link_state = MAC80211_LINKED;
296                 rtlpriv->cfg->ops->set_bcn_reg(hw);
297                 if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
298                         mac->basic_rates = 0xfff;
299                 else
300                         mac->basic_rates = 0xff0;
301                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
302                                 (u8 *)(&mac->basic_rates));
303                 break;
304         default:
305                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
306                          "operation mode %d is not support!\n", vif->type);
307                 err = -EOPNOTSUPP;
308                 goto out;
309         }
310
311         if (mac->p2p) {
312                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
313                          "p2p role %x\n", vif->type);
314                 mac->basic_rates = 0xff0;/*disable cck rate for p2p*/
315                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
316                                 (u8 *)(&mac->basic_rates));
317         }
318         mac->vif = vif;
319         mac->opmode = vif->type;
320         rtlpriv->cfg->ops->set_network_type(hw, vif->type);
321         memcpy(mac->mac_addr, vif->addr, ETH_ALEN);
322         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_ETHER_ADDR, mac->mac_addr);
323
324 out:
325         mutex_unlock(&rtlpriv->locks.conf_mutex);
326         return err;
327 }
328
329 static void rtl_op_remove_interface(struct ieee80211_hw *hw,
330                 struct ieee80211_vif *vif)
331 {
332         struct rtl_priv *rtlpriv = rtl_priv(hw);
333         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
334
335         mutex_lock(&rtlpriv->locks.conf_mutex);
336
337         /* Free beacon resources */
338         if ((vif->type == NL80211_IFTYPE_AP) ||
339             (vif->type == NL80211_IFTYPE_ADHOC) ||
340             (vif->type == NL80211_IFTYPE_MESH_POINT)) {
341                 if (mac->beacon_enabled == 1) {
342                         mac->beacon_enabled = 0;
343                         rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
344                                         rtlpriv->cfg->maps[RTL_IBSS_INT_MASKS]);
345                 }
346         }
347
348         /*
349          *Note: We assume NL80211_IFTYPE_UNSPECIFIED as
350          *NO LINK for our hardware.
351          */
352         mac->p2p = 0;
353         mac->vif = NULL;
354         mac->link_state = MAC80211_NOLINK;
355         eth_zero_addr(mac->bssid);
356         mac->vendor = PEER_UNKNOWN;
357         mac->opmode = NL80211_IFTYPE_UNSPECIFIED;
358         rtlpriv->cfg->ops->set_network_type(hw, mac->opmode);
359
360         mutex_unlock(&rtlpriv->locks.conf_mutex);
361 }
362 static int rtl_op_change_interface(struct ieee80211_hw *hw,
363                                    struct ieee80211_vif *vif,
364                                    enum nl80211_iftype new_type, bool p2p)
365 {
366         struct rtl_priv *rtlpriv = rtl_priv(hw);
367         int ret;
368         rtl_op_remove_interface(hw, vif);
369
370         vif->type = new_type;
371         vif->p2p = p2p;
372         ret = rtl_op_add_interface(hw, vif);
373         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
374                  "p2p  %x\n", p2p);
375         return ret;
376 }
377
378 #ifdef CONFIG_PM
379 static u16 crc16_ccitt(u8 data, u16 crc)
380 {
381         u8 shift_in, data_bit, crc_bit11, crc_bit4, crc_bit15;
382         u8 i;
383         u16 result;
384
385         for (i = 0; i < 8; i++) {
386                 crc_bit15 = ((crc & BIT(15)) ? 1 : 0);
387                 data_bit  = (data & (BIT(0) << i) ? 1 : 0);
388                 shift_in = crc_bit15 ^ data_bit;
389
390                 result = crc << 1;
391                 if (shift_in == 0)
392                         result &= (~BIT(0));
393                 else
394                         result |= BIT(0);
395
396                 crc_bit11 = ((crc & BIT(11)) ? 1 : 0) ^ shift_in;
397                 if (crc_bit11 == 0)
398                         result &= (~BIT(12));
399                 else
400                         result |= BIT(12);
401
402                 crc_bit4 = ((crc & BIT(4)) ? 1 : 0) ^ shift_in;
403                 if (crc_bit4 == 0)
404                         result &= (~BIT(5));
405                 else
406                         result |= BIT(5);
407
408                 crc = result;
409         }
410
411         return crc;
412 }
413
414 static u16 _calculate_wol_pattern_crc(u8 *pattern, u16 len)
415 {
416         u16 crc = 0xffff;
417         u32 i;
418
419         for (i = 0; i < len; i++)
420                 crc = crc16_ccitt(pattern[i], crc);
421
422         crc = ~crc;
423
424         return crc;
425 }
426
427 static void _rtl_add_wowlan_patterns(struct ieee80211_hw *hw,
428                                      struct cfg80211_wowlan *wow)
429 {
430         struct rtl_priv *rtlpriv = rtl_priv(hw);
431         struct rtl_mac *mac = &rtlpriv->mac80211;
432         struct cfg80211_pkt_pattern *patterns = wow->patterns;
433         struct rtl_wow_pattern rtl_pattern;
434         const u8 *pattern_os, *mask_os;
435         u8 mask[MAX_WOL_BIT_MASK_SIZE] = {0};
436         u8 content[MAX_WOL_PATTERN_SIZE] = {0};
437         u8 broadcast_addr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
438         u8 multicast_addr1[2] = {0x33, 0x33};
439         u8 multicast_addr2[3] = {0x01, 0x00, 0x5e};
440         u8 i, mask_len;
441         u16 j, len;
442
443         for (i = 0; i < wow->n_patterns; i++) {
444                 memset(&rtl_pattern, 0, sizeof(struct rtl_wow_pattern));
445                 memset(mask, 0, MAX_WOL_BIT_MASK_SIZE);
446                 if (patterns[i].pattern_len > MAX_WOL_PATTERN_SIZE) {
447                         RT_TRACE(rtlpriv, COMP_POWER, DBG_WARNING,
448                                  "Pattern[%d] is too long\n", i);
449                         continue;
450                 }
451                 pattern_os = patterns[i].pattern;
452                 mask_len = DIV_ROUND_UP(patterns[i].pattern_len, 8);
453                 mask_os = patterns[i].mask;
454                 RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
455                               "pattern content\n", pattern_os,
456                                patterns[i].pattern_len);
457                 RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
458                               "mask content\n", mask_os, mask_len);
459                 /* 1. unicast? multicast? or broadcast? */
460                 if (memcmp(pattern_os, broadcast_addr, 6) == 0)
461                         rtl_pattern.type = BROADCAST_PATTERN;
462                 else if (memcmp(pattern_os, multicast_addr1, 2) == 0 ||
463                          memcmp(pattern_os, multicast_addr2, 3) == 0)
464                         rtl_pattern.type = MULTICAST_PATTERN;
465                 else if  (memcmp(pattern_os, mac->mac_addr, 6) == 0)
466                         rtl_pattern.type = UNICAST_PATTERN;
467                 else
468                         rtl_pattern.type = UNKNOWN_TYPE;
469
470                 /* 2. translate mask_from_os to mask_for_hw */
471
472 /******************************************************************************
473  * pattern from OS uses 'ethenet frame', like this:
474
475                    |    6   |    6   |   2  |     20    |  Variable  |  4  |
476                    |--------+--------+------+-----------+------------+-----|
477                    |    802.3 Mac Header    | IP Header | TCP Packet | FCS |
478                    |   DA   |   SA   | Type |
479
480  * BUT, packet catched by our HW is in '802.11 frame', begin from LLC,
481
482         |     24 or 30      |    6   |   2  |     20    |  Variable  |  4  |
483         |-------------------+--------+------+-----------+------------+-----|
484         | 802.11 MAC Header |       LLC     | IP Header | TCP Packet | FCS |
485                             | Others | Tpye |
486
487  * Therefore, we need translate mask_from_OS to mask_to_hw.
488  * We should left-shift mask by 6 bits, then set the new bit[0~5] = 0,
489  * because new mask[0~5] means 'SA', but our HW packet begins from LLC,
490  * bit[0~5] corresponds to first 6 Bytes in LLC, they just don't match.
491  ******************************************************************************/
492
493                 /* Shift 6 bits */
494                 for (j = 0; j < mask_len - 1; j++) {
495                         mask[j] = mask_os[j] >> 6;
496                         mask[j] |= (mask_os[j + 1] & 0x3F) << 2;
497                 }
498                 mask[j] = (mask_os[j] >> 6) & 0x3F;
499                 /* Set bit 0-5 to zero */
500                 mask[0] &= 0xC0;
501
502                 RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
503                               "mask to hw\n", mask, mask_len);
504                 for (j = 0; j < (MAX_WOL_BIT_MASK_SIZE + 1) / 4; j++) {
505                         rtl_pattern.mask[j] = mask[j * 4];
506                         rtl_pattern.mask[j] |= (mask[j * 4 + 1] << 8);
507                         rtl_pattern.mask[j] |= (mask[j * 4 + 2] << 16);
508                         rtl_pattern.mask[j] |= (mask[j * 4 + 3] << 24);
509                 }
510
511                 /* To get the wake up pattern from the mask.
512                  * We do not count first 12 bits which means
513                  * DA[6] and SA[6] in the pattern to match HW design.
514                  */
515                 len = 0;
516                 for (j = 12; j < patterns[i].pattern_len; j++) {
517                         if ((mask_os[j / 8] >> (j % 8)) & 0x01) {
518                                 content[len] = pattern_os[j];
519                                 len++;
520                         }
521                 }
522
523                 RT_PRINT_DATA(rtlpriv, COMP_POWER, DBG_TRACE,
524                               "pattern to hw\n", content, len);
525                 /* 3. calculate crc */
526                 rtl_pattern.crc = _calculate_wol_pattern_crc(content, len);
527                 RT_TRACE(rtlpriv, COMP_POWER, DBG_TRACE,
528                          "CRC_Remainder = 0x%x\n", rtl_pattern.crc);
529
530                 /* 4. write crc & mask_for_hw to hw */
531                 rtlpriv->cfg->ops->add_wowlan_pattern(hw, &rtl_pattern, i);
532         }
533         rtl_write_byte(rtlpriv, 0x698, wow->n_patterns);
534 }
535
536 static int rtl_op_suspend(struct ieee80211_hw *hw,
537                           struct cfg80211_wowlan *wow)
538 {
539         struct rtl_priv *rtlpriv = rtl_priv(hw);
540         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
541         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
542         struct timeval ts;
543
544         RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG, "\n");
545         if (WARN_ON(!wow))
546                 return -EINVAL;
547
548         /* to resolve s4 can not wake up*/
549         do_gettimeofday(&ts);
550         rtlhal->last_suspend_sec = ts.tv_sec;
551
552         if ((ppsc->wo_wlan_mode & WAKE_ON_PATTERN_MATCH) && wow->n_patterns)
553                 _rtl_add_wowlan_patterns(hw, wow);
554
555         rtlhal->driver_is_goingto_unload = true;
556         rtlhal->enter_pnp_sleep = true;
557
558         rtl_lps_leave(hw);
559         rtl_op_stop(hw);
560         device_set_wakeup_enable(wiphy_dev(hw->wiphy), true);
561         return 0;
562 }
563
564 static int rtl_op_resume(struct ieee80211_hw *hw)
565 {
566         struct rtl_priv *rtlpriv = rtl_priv(hw);
567         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
568         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
569         struct timeval ts;
570
571         RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG, "\n");
572         rtlhal->driver_is_goingto_unload = false;
573         rtlhal->enter_pnp_sleep = false;
574         rtlhal->wake_from_pnp_sleep = true;
575
576         /* to resovle s4 can not wake up*/
577         do_gettimeofday(&ts);
578         if (ts.tv_sec - rtlhal->last_suspend_sec < 5)
579                 return -1;
580
581         rtl_op_start(hw);
582         device_set_wakeup_enable(wiphy_dev(hw->wiphy), false);
583         ieee80211_resume_disconnect(mac->vif);
584         rtlhal->wake_from_pnp_sleep = false;
585         return 0;
586 }
587 #endif
588
589 static int rtl_op_config(struct ieee80211_hw *hw, u32 changed)
590 {
591         struct rtl_priv *rtlpriv = rtl_priv(hw);
592         struct rtl_phy *rtlphy = &(rtlpriv->phy);
593         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
594         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
595         struct ieee80211_conf *conf = &hw->conf;
596
597         if (mac->skip_scan)
598                 return 1;
599
600         mutex_lock(&rtlpriv->locks.conf_mutex);
601         if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {  /* BIT(2)*/
602                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
603                          "IEEE80211_CONF_CHANGE_LISTEN_INTERVAL\n");
604         }
605
606         /*For IPS */
607         if (changed & IEEE80211_CONF_CHANGE_IDLE) {
608                 if (hw->conf.flags & IEEE80211_CONF_IDLE)
609                         rtl_ips_nic_off(hw);
610                 else
611                         rtl_ips_nic_on(hw);
612         } else {
613                 /*
614                  *although rfoff may not cause by ips, but we will
615                  *check the reason in set_rf_power_state function
616                  */
617                 if (unlikely(ppsc->rfpwr_state == ERFOFF))
618                         rtl_ips_nic_on(hw);
619         }
620
621         /*For LPS */
622         if (changed & IEEE80211_CONF_CHANGE_PS) {
623                 cancel_delayed_work(&rtlpriv->works.ps_work);
624                 cancel_delayed_work(&rtlpriv->works.ps_rfon_wq);
625                 if (conf->flags & IEEE80211_CONF_PS) {
626                         rtlpriv->psc.sw_ps_enabled = true;
627                         /* sleep here is must, or we may recv the beacon and
628                          * cause mac80211 into wrong ps state, this will cause
629                          * power save nullfunc send fail, and further cause
630                          * pkt loss, So sleep must quickly but not immediatly
631                          * because that will cause nullfunc send by mac80211
632                          * fail, and cause pkt loss, we have tested that 5mA
633                          * is worked very well */
634                         if (!rtlpriv->psc.multi_buffered)
635                                 queue_delayed_work(rtlpriv->works.rtl_wq,
636                                                    &rtlpriv->works.ps_work,
637                                                    MSECS(5));
638                 } else {
639                         rtl_swlps_rf_awake(hw);
640                         rtlpriv->psc.sw_ps_enabled = false;
641                 }
642         }
643
644         if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
645                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
646                          "IEEE80211_CONF_CHANGE_RETRY_LIMITS %x\n",
647                          hw->conf.long_frame_max_tx_count);
648                 mac->retry_long = hw->conf.long_frame_max_tx_count;
649                 mac->retry_short = hw->conf.long_frame_max_tx_count;
650                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RETRY_LIMIT,
651                                 (u8 *)(&hw->conf.long_frame_max_tx_count));
652         }
653
654         if (changed & IEEE80211_CONF_CHANGE_CHANNEL &&
655             !rtlpriv->proximity.proxim_on) {
656                 struct ieee80211_channel *channel = hw->conf.chandef.chan;
657                 enum nl80211_chan_width width = hw->conf.chandef.width;
658                 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
659                 u8 wide_chan = (u8) channel->hw_value;
660
661                 /* channel_type is for 20&40M */
662                 if (width < NL80211_CHAN_WIDTH_80)
663                         channel_type =
664                                 cfg80211_get_chandef_type(&hw->conf.chandef);
665                 if (mac->act_scanning)
666                         mac->n_channels++;
667
668                 if (rtlpriv->dm.supp_phymode_switch &&
669                         mac->link_state < MAC80211_LINKED &&
670                         !mac->act_scanning) {
671                         if (rtlpriv->cfg->ops->chk_switch_dmdp)
672                                 rtlpriv->cfg->ops->chk_switch_dmdp(hw);
673                 }
674
675                 /*
676                  *because we should back channel to
677                  *current_network.chan in in scanning,
678                  *So if set_chan == current_network.chan
679                  *we should set it.
680                  *because mac80211 tell us wrong bw40
681                  *info for cisco1253 bw20, so we modify
682                  *it here based on UPPER & LOWER
683                  */
684
685                 if (width >= NL80211_CHAN_WIDTH_80) {
686                         if (width == NL80211_CHAN_WIDTH_80) {
687                                 u32 center = hw->conf.chandef.center_freq1;
688                                 u32 primary =
689                                 (u32)hw->conf.chandef.chan->center_freq;
690
691                                 rtlphy->current_chan_bw =
692                                         HT_CHANNEL_WIDTH_80;
693                                 mac->bw_80 = true;
694                                 mac->bw_40 = true;
695                                 if (center > primary) {
696                                         mac->cur_80_prime_sc =
697                                         PRIME_CHNL_OFFSET_LOWER;
698                                         if (center - primary == 10) {
699                                                 mac->cur_40_prime_sc =
700                                                 PRIME_CHNL_OFFSET_UPPER;
701
702                                                 wide_chan += 2;
703                                         } else if (center - primary == 30) {
704                                                 mac->cur_40_prime_sc =
705                                                 PRIME_CHNL_OFFSET_LOWER;
706
707                                                 wide_chan += 6;
708                                         }
709                                 } else {
710                                         mac->cur_80_prime_sc =
711                                         PRIME_CHNL_OFFSET_UPPER;
712                                         if (primary - center == 10) {
713                                                 mac->cur_40_prime_sc =
714                                                 PRIME_CHNL_OFFSET_LOWER;
715
716                                                 wide_chan -= 2;
717                                         } else if (primary - center == 30) {
718                                                 mac->cur_40_prime_sc =
719                                                 PRIME_CHNL_OFFSET_UPPER;
720
721                                                 wide_chan -= 6;
722                                         }
723                                 }
724                         }
725                 } else {
726                         switch (channel_type) {
727                         case NL80211_CHAN_HT20:
728                         case NL80211_CHAN_NO_HT:
729                                         /* SC */
730                                         mac->cur_40_prime_sc =
731                                                 PRIME_CHNL_OFFSET_DONT_CARE;
732                                         rtlphy->current_chan_bw =
733                                                 HT_CHANNEL_WIDTH_20;
734                                         mac->bw_40 = false;
735                                         mac->bw_80 = false;
736                                         break;
737                         case NL80211_CHAN_HT40MINUS:
738                                         /* SC */
739                                         mac->cur_40_prime_sc =
740                                                 PRIME_CHNL_OFFSET_UPPER;
741                                         rtlphy->current_chan_bw =
742                                                 HT_CHANNEL_WIDTH_20_40;
743                                         mac->bw_40 = true;
744                                         mac->bw_80 = false;
745
746                                         /*wide channel */
747                                         wide_chan -= 2;
748
749                                         break;
750                         case NL80211_CHAN_HT40PLUS:
751                                         /* SC */
752                                         mac->cur_40_prime_sc =
753                                                 PRIME_CHNL_OFFSET_LOWER;
754                                         rtlphy->current_chan_bw =
755                                                 HT_CHANNEL_WIDTH_20_40;
756                                         mac->bw_40 = true;
757                                         mac->bw_80 = false;
758
759                                         /*wide channel */
760                                         wide_chan += 2;
761
762                                         break;
763                         default:
764                                         mac->bw_40 = false;
765                                         mac->bw_80 = false;
766                                         RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
767                                                  "switch case %#x not processed\n",
768                                                  channel_type);
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)
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                                 rtl_lps_leave(hw);
1154                         if (ppsc->p2p_ps_info.p2p_ps_mode > P2P_PS_NONE)
1155                                 rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
1156                         mac->link_state = MAC80211_NOLINK;
1157                         eth_zero_addr(mac->bssid);
1158                         mac->vendor = PEER_UNKNOWN;
1159                         mac->mode = 0;
1160
1161                         if (rtlpriv->dm.supp_phymode_switch) {
1162                                 if (rtlpriv->cfg->ops->chk_switch_dmdp)
1163                                         rtlpriv->cfg->ops->chk_switch_dmdp(hw);
1164                         }
1165                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
1166                                  "BSS_CHANGED_UN_ASSOC\n");
1167                 }
1168                 rtlpriv->cfg->ops->set_network_type(hw, vif->type);
1169                 /* For FW LPS:
1170                  * To tell firmware we have connected or disconnected
1171                  */
1172                 rtlpriv->cfg->ops->set_hw_reg(hw,
1173                                               HW_VAR_H2C_FW_JOINBSSRPT,
1174                                               (u8 *)(&mstatus));
1175                 ppsc->report_linked = (mstatus == RT_MEDIA_CONNECT) ?
1176                                       true : false;
1177
1178                 if (rtlpriv->cfg->ops->get_btc_status())
1179                         rtlpriv->btcoexist.btc_ops->btc_mediastatus_notify(
1180                                                         rtlpriv, mstatus);
1181         }
1182
1183         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1184                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1185                          "BSS_CHANGED_ERP_CTS_PROT\n");
1186                 mac->use_cts_protect = bss_conf->use_cts_prot;
1187         }
1188
1189         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1190                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
1191                          "BSS_CHANGED_ERP_PREAMBLE use short preamble:%x\n",
1192                           bss_conf->use_short_preamble);
1193
1194                 mac->short_preamble = bss_conf->use_short_preamble;
1195                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_ACK_PREAMBLE,
1196                                               (u8 *)(&mac->short_preamble));
1197         }
1198
1199         if (changed & BSS_CHANGED_ERP_SLOT) {
1200                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1201                          "BSS_CHANGED_ERP_SLOT\n");
1202
1203                 if (bss_conf->use_short_slot)
1204                         mac->slot_time = RTL_SLOT_TIME_9;
1205                 else
1206                         mac->slot_time = RTL_SLOT_TIME_20;
1207
1208                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SLOT_TIME,
1209                                               (u8 *)(&mac->slot_time));
1210         }
1211
1212         if (changed & BSS_CHANGED_HT) {
1213                 struct ieee80211_sta *sta = NULL;
1214
1215                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1216                          "BSS_CHANGED_HT\n");
1217
1218                 rcu_read_lock();
1219                 sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
1220                 if (sta) {
1221                         if (sta->ht_cap.ampdu_density >
1222                             mac->current_ampdu_density)
1223                                 mac->current_ampdu_density =
1224                                     sta->ht_cap.ampdu_density;
1225                         if (sta->ht_cap.ampdu_factor <
1226                             mac->current_ampdu_factor)
1227                                 mac->current_ampdu_factor =
1228                                     sta->ht_cap.ampdu_factor;
1229                 }
1230                 rcu_read_unlock();
1231
1232                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SHORTGI_DENSITY,
1233                                               (u8 *)(&mac->max_mss_density));
1234                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AMPDU_FACTOR,
1235                                               &mac->current_ampdu_factor);
1236                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AMPDU_MIN_SPACE,
1237                                               &mac->current_ampdu_density);
1238         }
1239
1240         if (changed & BSS_CHANGED_BSSID) {
1241                 u32 basic_rates;
1242                 struct ieee80211_sta *sta = NULL;
1243
1244                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BSSID,
1245                                               (u8 *)bss_conf->bssid);
1246
1247                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
1248                          "bssid: %pM\n", bss_conf->bssid);
1249
1250                 mac->vendor = PEER_UNKNOWN;
1251                 memcpy(mac->bssid, bss_conf->bssid, ETH_ALEN);
1252
1253                 rcu_read_lock();
1254                 sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
1255                 if (!sta) {
1256                         rcu_read_unlock();
1257                         goto out;
1258                 }
1259
1260                 if (rtlhal->current_bandtype == BAND_ON_5G) {
1261                         mac->mode = WIRELESS_MODE_A;
1262                 } else {
1263                         if (sta->supp_rates[0] <= 0xf)
1264                                 mac->mode = WIRELESS_MODE_B;
1265                         else
1266                                 mac->mode = WIRELESS_MODE_G;
1267                 }
1268
1269                 if (sta->ht_cap.ht_supported) {
1270                         if (rtlhal->current_bandtype == BAND_ON_2_4G)
1271                                 mac->mode = WIRELESS_MODE_N_24G;
1272                         else
1273                                 mac->mode = WIRELESS_MODE_N_5G;
1274                 }
1275
1276                 if (sta->vht_cap.vht_supported) {
1277                         if (rtlhal->current_bandtype == BAND_ON_5G)
1278                                 mac->mode = WIRELESS_MODE_AC_5G;
1279                         else
1280                                 mac->mode = WIRELESS_MODE_AC_24G;
1281                 }
1282
1283                 /* just station need it, because ibss & ap mode will
1284                  * set in sta_add, and will be NULL here */
1285                 if (vif->type == NL80211_IFTYPE_STATION) {
1286                         struct rtl_sta_info *sta_entry;
1287                         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1288                         sta_entry->wireless_mode = mac->mode;
1289                 }
1290
1291                 if (sta->ht_cap.ht_supported) {
1292                         mac->ht_enable = true;
1293
1294                         /*
1295                          * for cisco 1252 bw20 it's wrong
1296                          * if (ht_cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
1297                          *      mac->bw_40 = true;
1298                          * }
1299                          * */
1300                 }
1301
1302                 if (sta->vht_cap.vht_supported)
1303                         mac->vht_enable = true;
1304
1305                 if (changed & BSS_CHANGED_BASIC_RATES) {
1306                         /* for 5G must << RATE_6M_INDEX = 4,
1307                          * because 5G have no cck rate*/
1308                         if (rtlhal->current_bandtype == BAND_ON_5G)
1309                                 basic_rates = sta->supp_rates[1] << 4;
1310                         else
1311                                 basic_rates = sta->supp_rates[0];
1312
1313                         mac->basic_rates = basic_rates;
1314                         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
1315                                         (u8 *)(&basic_rates));
1316                 }
1317                 rcu_read_unlock();
1318         }
1319 out:
1320         mutex_unlock(&rtlpriv->locks.conf_mutex);
1321 }
1322
1323 static u64 rtl_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1324 {
1325         struct rtl_priv *rtlpriv = rtl_priv(hw);
1326         u64 tsf;
1327
1328         rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_CORRECT_TSF, (u8 *)(&tsf));
1329         return tsf;
1330 }
1331
1332 static void rtl_op_set_tsf(struct ieee80211_hw *hw,
1333                            struct ieee80211_vif *vif, u64 tsf)
1334 {
1335         struct rtl_priv *rtlpriv = rtl_priv(hw);
1336         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1337         u8 bibss = (mac->opmode == NL80211_IFTYPE_ADHOC) ? 1 : 0;
1338
1339         mac->tsf = tsf;
1340         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_CORRECT_TSF, (u8 *)(&bibss));
1341 }
1342
1343 static void rtl_op_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1344 {
1345         struct rtl_priv *rtlpriv = rtl_priv(hw);
1346         u8 tmp = 0;
1347
1348         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_DUAL_TSF_RST, (u8 *)(&tmp));
1349 }
1350
1351 static void rtl_op_sta_notify(struct ieee80211_hw *hw,
1352                               struct ieee80211_vif *vif,
1353                               enum sta_notify_cmd cmd,
1354                               struct ieee80211_sta *sta)
1355 {
1356         switch (cmd) {
1357         case STA_NOTIFY_SLEEP:
1358                 break;
1359         case STA_NOTIFY_AWAKE:
1360                 break;
1361         default:
1362                 break;
1363         }
1364 }
1365
1366 static int rtl_op_ampdu_action(struct ieee80211_hw *hw,
1367                                struct ieee80211_vif *vif,
1368                                struct ieee80211_ampdu_params *params)
1369 {
1370         struct rtl_priv *rtlpriv = rtl_priv(hw);
1371         struct ieee80211_sta *sta = params->sta;
1372         enum ieee80211_ampdu_mlme_action action = params->action;
1373         u16 tid = params->tid;
1374         u16 *ssn = &params->ssn;
1375
1376         switch (action) {
1377         case IEEE80211_AMPDU_TX_START:
1378                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1379                          "IEEE80211_AMPDU_TX_START: TID:%d\n", tid);
1380                 return rtl_tx_agg_start(hw, vif, sta, tid, ssn);
1381         case IEEE80211_AMPDU_TX_STOP_CONT:
1382         case IEEE80211_AMPDU_TX_STOP_FLUSH:
1383         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1384                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1385                          "IEEE80211_AMPDU_TX_STOP: TID:%d\n", tid);
1386                 return rtl_tx_agg_stop(hw, vif, sta, tid);
1387         case IEEE80211_AMPDU_TX_OPERATIONAL:
1388                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1389                          "IEEE80211_AMPDU_TX_OPERATIONAL:TID:%d\n", tid);
1390                 rtl_tx_agg_oper(hw, sta, tid);
1391                 break;
1392         case IEEE80211_AMPDU_RX_START:
1393                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1394                          "IEEE80211_AMPDU_RX_START:TID:%d\n", tid);
1395                 return rtl_rx_agg_start(hw, sta, tid);
1396         case IEEE80211_AMPDU_RX_STOP:
1397                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1398                          "IEEE80211_AMPDU_RX_STOP:TID:%d\n", tid);
1399                 return rtl_rx_agg_stop(hw, sta, tid);
1400         default:
1401                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
1402                          "IEEE80211_AMPDU_ERR!!!!:\n");
1403                 return -EOPNOTSUPP;
1404         }
1405         return 0;
1406 }
1407
1408 static void rtl_op_sw_scan_start(struct ieee80211_hw *hw,
1409                                  struct ieee80211_vif *vif,
1410                                  const u8 *mac_addr)
1411 {
1412         struct rtl_priv *rtlpriv = rtl_priv(hw);
1413         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1414
1415         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "\n");
1416         mac->act_scanning = true;
1417         if (rtlpriv->link_info.higher_busytraffic) {
1418                 mac->skip_scan = true;
1419                 return;
1420         }
1421
1422         if (rtlpriv->cfg->ops->get_btc_status())
1423                 rtlpriv->btcoexist.btc_ops->btc_scan_notify(rtlpriv, 1);
1424
1425         if (rtlpriv->dm.supp_phymode_switch) {
1426                 if (rtlpriv->cfg->ops->chk_switch_dmdp)
1427                         rtlpriv->cfg->ops->chk_switch_dmdp(hw);
1428         }
1429
1430         if (mac->link_state == MAC80211_LINKED) {
1431                 rtl_lps_leave(hw);
1432                 mac->link_state = MAC80211_LINKED_SCANNING;
1433         } else {
1434                 rtl_ips_nic_on(hw);
1435         }
1436
1437         /* Dul mac */
1438         rtlpriv->rtlhal.load_imrandiqk_setting_for2g = false;
1439
1440         rtlpriv->cfg->ops->led_control(hw, LED_CTL_SITE_SURVEY);
1441         rtlpriv->cfg->ops->scan_operation_backup(hw, SCAN_OPT_BACKUP_BAND0);
1442 }
1443
1444 static void rtl_op_sw_scan_complete(struct ieee80211_hw *hw,
1445                                     struct ieee80211_vif *vif)
1446 {
1447         struct rtl_priv *rtlpriv = rtl_priv(hw);
1448         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1449
1450         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "\n");
1451         mac->act_scanning = false;
1452         mac->skip_scan = false;
1453         if (rtlpriv->link_info.higher_busytraffic)
1454                 return;
1455
1456         /* p2p will use 1/6/11 to scan */
1457         if (mac->n_channels == 3)
1458                 mac->p2p_in_use = true;
1459         else
1460                 mac->p2p_in_use = false;
1461         mac->n_channels = 0;
1462         /* Dul mac */
1463         rtlpriv->rtlhal.load_imrandiqk_setting_for2g = false;
1464
1465         if (mac->link_state == MAC80211_LINKED_SCANNING) {
1466                 mac->link_state = MAC80211_LINKED;
1467                 if (mac->opmode == NL80211_IFTYPE_STATION) {
1468                         /* fix fwlps issue */
1469                         rtlpriv->cfg->ops->set_network_type(hw, mac->opmode);
1470                 }
1471         }
1472
1473         rtlpriv->cfg->ops->scan_operation_backup(hw, SCAN_OPT_RESTORE);
1474         if (rtlpriv->cfg->ops->get_btc_status())
1475                 rtlpriv->btcoexist.btc_ops->btc_scan_notify(rtlpriv, 0);
1476 }
1477
1478 static int rtl_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
1479                           struct ieee80211_vif *vif, struct ieee80211_sta *sta,
1480                           struct ieee80211_key_conf *key)
1481 {
1482         struct rtl_priv *rtlpriv = rtl_priv(hw);
1483         u8 key_type = NO_ENCRYPTION;
1484         u8 key_idx;
1485         bool group_key = false;
1486         bool wep_only = false;
1487         int err = 0;
1488         u8 mac_addr[ETH_ALEN];
1489         u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1490
1491         if (rtlpriv->cfg->mod_params->sw_crypto || rtlpriv->sec.use_sw_sec) {
1492                 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
1493                          "not open hw encryption\n");
1494                 return -ENOSPC; /*User disabled HW-crypto */
1495         }
1496         /* To support IBSS, use sw-crypto for GTK */
1497         if (((vif->type == NL80211_IFTYPE_ADHOC) ||
1498             (vif->type == NL80211_IFTYPE_MESH_POINT)) &&
1499            !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1500                 return -ENOSPC;
1501         RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1502                  "%s hardware based encryption for keyidx: %d, mac: %pM\n",
1503                   cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
1504                   sta ? sta->addr : bcast_addr);
1505         rtlpriv->sec.being_setkey = true;
1506         rtl_ips_nic_on(hw);
1507         mutex_lock(&rtlpriv->locks.conf_mutex);
1508         /* <1> get encryption alg */
1509
1510         switch (key->cipher) {
1511         case WLAN_CIPHER_SUITE_WEP40:
1512                 key_type = WEP40_ENCRYPTION;
1513                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:WEP40\n");
1514                 break;
1515         case WLAN_CIPHER_SUITE_WEP104:
1516                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:WEP104\n");
1517                 key_type = WEP104_ENCRYPTION;
1518                 break;
1519         case WLAN_CIPHER_SUITE_TKIP:
1520                 key_type = TKIP_ENCRYPTION;
1521                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:TKIP\n");
1522                 break;
1523         case WLAN_CIPHER_SUITE_CCMP:
1524                 key_type = AESCCMP_ENCRYPTION;
1525                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:CCMP\n");
1526                 break;
1527         case WLAN_CIPHER_SUITE_AES_CMAC:
1528                 /* HW don't support CMAC encryption,
1529                  * use software CMAC encryption
1530                  */
1531                 key_type = AESCMAC_ENCRYPTION;
1532                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:CMAC\n");
1533                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1534                          "HW don't support CMAC encrypiton, use software CMAC encrypiton\n");
1535                 err = -EOPNOTSUPP;
1536                 goto out_unlock;
1537         default:
1538                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
1539                          "alg_err:%x!!!!:\n", key->cipher);
1540                 goto out_unlock;
1541         }
1542         if (key_type == WEP40_ENCRYPTION ||
1543            key_type == WEP104_ENCRYPTION ||
1544            vif->type == NL80211_IFTYPE_ADHOC)
1545                 rtlpriv->sec.use_defaultkey = true;
1546
1547         /* <2> get key_idx */
1548         key_idx = (u8) (key->keyidx);
1549         if (key_idx > 3)
1550                 goto out_unlock;
1551         /* <3> if pairwise key enable_hw_sec */
1552         group_key = !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE);
1553
1554         /* wep always be group key, but there are two conditions:
1555          * 1) wep only: is just for wep enc, in this condition
1556          * rtlpriv->sec.pairwise_enc_algorithm == NO_ENCRYPTION
1557          * will be true & enable_hw_sec will be set when wep
1558          * ke setting.
1559          * 2) wep(group) + AES(pairwise): some AP like cisco
1560          * may use it, in this condition enable_hw_sec will not
1561          * be set when wep key setting */
1562         /* we must reset sec_info after lingked before set key,
1563          * or some flag will be wrong*/
1564         if (vif->type == NL80211_IFTYPE_AP ||
1565                 vif->type == NL80211_IFTYPE_MESH_POINT) {
1566                 if (!group_key || key_type == WEP40_ENCRYPTION ||
1567                         key_type == WEP104_ENCRYPTION) {
1568                         if (group_key)
1569                                 wep_only = true;
1570                         rtlpriv->cfg->ops->enable_hw_sec(hw);
1571                 }
1572         } else {
1573                 if ((!group_key) || (vif->type == NL80211_IFTYPE_ADHOC) ||
1574                     rtlpriv->sec.pairwise_enc_algorithm == NO_ENCRYPTION) {
1575                         if (rtlpriv->sec.pairwise_enc_algorithm ==
1576                             NO_ENCRYPTION &&
1577                            (key_type == WEP40_ENCRYPTION ||
1578                             key_type == WEP104_ENCRYPTION))
1579                                 wep_only = true;
1580                         rtlpriv->sec.pairwise_enc_algorithm = key_type;
1581                         RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1582                                  "set enable_hw_sec, key_type:%x(OPEN:0 WEP40:1 TKIP:2 AES:4 WEP104:5)\n",
1583                                  key_type);
1584                         rtlpriv->cfg->ops->enable_hw_sec(hw);
1585                 }
1586         }
1587         /* <4> set key based on cmd */
1588         switch (cmd) {
1589         case SET_KEY:
1590                 if (wep_only) {
1591                         RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1592                                  "set WEP(group/pairwise) key\n");
1593                         /* Pairwise key with an assigned MAC address. */
1594                         rtlpriv->sec.pairwise_enc_algorithm = key_type;
1595                         rtlpriv->sec.group_enc_algorithm = key_type;
1596                         /*set local buf about wep key. */
1597                         memcpy(rtlpriv->sec.key_buf[key_idx],
1598                                key->key, key->keylen);
1599                         rtlpriv->sec.key_len[key_idx] = key->keylen;
1600                         eth_zero_addr(mac_addr);
1601                 } else if (group_key) { /* group key */
1602                         RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1603                                  "set group key\n");
1604                         /* group key */
1605                         rtlpriv->sec.group_enc_algorithm = key_type;
1606                         /*set local buf about group key. */
1607                         memcpy(rtlpriv->sec.key_buf[key_idx],
1608                                key->key, key->keylen);
1609                         rtlpriv->sec.key_len[key_idx] = key->keylen;
1610                         memcpy(mac_addr, bcast_addr, ETH_ALEN);
1611                 } else {        /* pairwise key */
1612                         RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1613                                  "set pairwise key\n");
1614                         if (!sta) {
1615                                 RT_ASSERT(false,
1616                                           "pairwise key without mac_addr\n");
1617
1618                                 err = -EOPNOTSUPP;
1619                                 goto out_unlock;
1620                         }
1621                         /* Pairwise key with an assigned MAC address. */
1622                         rtlpriv->sec.pairwise_enc_algorithm = key_type;
1623                         /*set local buf about pairwise key. */
1624                         memcpy(rtlpriv->sec.key_buf[PAIRWISE_KEYIDX],
1625                                key->key, key->keylen);
1626                         rtlpriv->sec.key_len[PAIRWISE_KEYIDX] = key->keylen;
1627                         rtlpriv->sec.pairwise_key =
1628                             rtlpriv->sec.key_buf[PAIRWISE_KEYIDX];
1629                         memcpy(mac_addr, sta->addr, ETH_ALEN);
1630                 }
1631                 rtlpriv->cfg->ops->set_key(hw, key_idx, mac_addr,
1632                                            group_key, key_type, wep_only,
1633                                            false);
1634                 /* <5> tell mac80211 do something: */
1635                 /*must use sw generate IV, or can not work !!!!. */
1636                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1637                 key->hw_key_idx = key_idx;
1638                 if (key_type == TKIP_ENCRYPTION)
1639                         key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1640                 /*use software CCMP encryption for management frames (MFP) */
1641                 if (key_type == AESCCMP_ENCRYPTION)
1642                         key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
1643                 break;
1644         case DISABLE_KEY:
1645                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1646                          "disable key delete one entry\n");
1647                 /*set local buf about wep key. */
1648                 if (vif->type == NL80211_IFTYPE_AP ||
1649                         vif->type == NL80211_IFTYPE_MESH_POINT) {
1650                         if (sta)
1651                                 rtl_cam_del_entry(hw, sta->addr);
1652                 }
1653                 memset(rtlpriv->sec.key_buf[key_idx], 0, key->keylen);
1654                 rtlpriv->sec.key_len[key_idx] = 0;
1655                 eth_zero_addr(mac_addr);
1656                 /*
1657                  *mac80211 will delete entrys one by one,
1658                  *so don't use rtl_cam_reset_all_entry
1659                  *or clear all entry here.
1660                  */
1661                 rtl_cam_delete_one_entry(hw, mac_addr, key_idx);
1662                 break;
1663         default:
1664                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
1665                          "cmd_err:%x!!!!:\n", cmd);
1666         }
1667 out_unlock:
1668         mutex_unlock(&rtlpriv->locks.conf_mutex);
1669         rtlpriv->sec.being_setkey = false;
1670         return err;
1671 }
1672
1673 static void rtl_op_rfkill_poll(struct ieee80211_hw *hw)
1674 {
1675         struct rtl_priv *rtlpriv = rtl_priv(hw);
1676
1677         bool radio_state;
1678         bool blocked;
1679         u8 valid = 0;
1680
1681         if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
1682                 return;
1683
1684         mutex_lock(&rtlpriv->locks.conf_mutex);
1685
1686         /*if Radio On return true here */
1687         radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
1688
1689         if (valid) {
1690                 if (unlikely(radio_state != rtlpriv->rfkill.rfkill_state)) {
1691                         rtlpriv->rfkill.rfkill_state = radio_state;
1692
1693                         RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
1694                                  "wireless radio switch turned %s\n",
1695                                   radio_state ? "on" : "off");
1696
1697                         blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
1698                         wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
1699                 }
1700         }
1701
1702         mutex_unlock(&rtlpriv->locks.conf_mutex);
1703 }
1704
1705 /* this function is called by mac80211 to flush tx buffer
1706  * before switch channle or power save, or tx buffer packet
1707  * maybe send after offchannel or rf sleep, this may cause
1708  * dis-association by AP */
1709 static void rtl_op_flush(struct ieee80211_hw *hw,
1710                          struct ieee80211_vif *vif,
1711                          u32 queues,
1712                          bool drop)
1713 {
1714         struct rtl_priv *rtlpriv = rtl_priv(hw);
1715
1716         if (rtlpriv->intf_ops->flush)
1717                 rtlpriv->intf_ops->flush(hw, queues, drop);
1718 }
1719
1720 /*      Description:
1721  *              This routine deals with the Power Configuration CMD
1722  *               parsing for RTL8723/RTL8188E Series IC.
1723  *      Assumption:
1724  *              We should follow specific format that was released from HW SD.
1725  */
1726 bool rtl_hal_pwrseqcmdparsing(struct rtl_priv *rtlpriv, u8 cut_version,
1727                               u8 faversion, u8 interface_type,
1728                               struct wlan_pwr_cfg pwrcfgcmd[])
1729 {
1730         struct wlan_pwr_cfg cfg_cmd = {0};
1731         bool polling_bit = false;
1732         u32 ary_idx = 0;
1733         u8 value = 0;
1734         u32 offset = 0;
1735         u32 polling_count = 0;
1736         u32 max_polling_cnt = 5000;
1737
1738         do {
1739                 cfg_cmd = pwrcfgcmd[ary_idx];
1740                 RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1741                          "rtl_hal_pwrseqcmdparsing(): offset(%#x),cut_msk(%#x), famsk(%#x), interface_msk(%#x), base(%#x), cmd(%#x), msk(%#x), value(%#x)\n",
1742                          GET_PWR_CFG_OFFSET(cfg_cmd),
1743                                             GET_PWR_CFG_CUT_MASK(cfg_cmd),
1744                          GET_PWR_CFG_FAB_MASK(cfg_cmd),
1745                                               GET_PWR_CFG_INTF_MASK(cfg_cmd),
1746                          GET_PWR_CFG_BASE(cfg_cmd), GET_PWR_CFG_CMD(cfg_cmd),
1747                          GET_PWR_CFG_MASK(cfg_cmd), GET_PWR_CFG_VALUE(cfg_cmd));
1748
1749                 if ((GET_PWR_CFG_FAB_MASK(cfg_cmd)&faversion) &&
1750                     (GET_PWR_CFG_CUT_MASK(cfg_cmd)&cut_version) &&
1751                     (GET_PWR_CFG_INTF_MASK(cfg_cmd)&interface_type)) {
1752                         switch (GET_PWR_CFG_CMD(cfg_cmd)) {
1753                         case PWR_CMD_READ:
1754                                 RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1755                                         "rtl_hal_pwrseqcmdparsing(): PWR_CMD_READ\n");
1756                                 break;
1757                         case PWR_CMD_WRITE:
1758                                 RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1759                                         "rtl_hal_pwrseqcmdparsing(): PWR_CMD_WRITE\n");
1760                                 offset = GET_PWR_CFG_OFFSET(cfg_cmd);
1761
1762                                 /*Read the value from system register*/
1763                                 value = rtl_read_byte(rtlpriv, offset);
1764                                 value &= (~(GET_PWR_CFG_MASK(cfg_cmd)));
1765                                 value |= (GET_PWR_CFG_VALUE(cfg_cmd) &
1766                                           GET_PWR_CFG_MASK(cfg_cmd));
1767
1768                                 /*Write the value back to sytem register*/
1769                                 rtl_write_byte(rtlpriv, offset, value);
1770                                 break;
1771                         case PWR_CMD_POLLING:
1772                                 RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1773                                         "rtl_hal_pwrseqcmdparsing(): PWR_CMD_POLLING\n");
1774                                 polling_bit = false;
1775                                 offset = GET_PWR_CFG_OFFSET(cfg_cmd);
1776
1777                                 do {
1778                                         value = rtl_read_byte(rtlpriv, offset);
1779
1780                                         value &= GET_PWR_CFG_MASK(cfg_cmd);
1781                                         if (value ==
1782                                             (GET_PWR_CFG_VALUE(cfg_cmd) &
1783                                              GET_PWR_CFG_MASK(cfg_cmd)))
1784                                                 polling_bit = true;
1785                                         else
1786                                                 udelay(10);
1787
1788                                         if (polling_count++ > max_polling_cnt)
1789                                                 return false;
1790                                 } while (!polling_bit);
1791                                 break;
1792                         case PWR_CMD_DELAY:
1793                                 RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1794                                          "rtl_hal_pwrseqcmdparsing(): PWR_CMD_DELAY\n");
1795                                 if (GET_PWR_CFG_VALUE(cfg_cmd) ==
1796                                     PWRSEQ_DELAY_US)
1797                                         udelay(GET_PWR_CFG_OFFSET(cfg_cmd));
1798                                 else
1799                                         mdelay(GET_PWR_CFG_OFFSET(cfg_cmd));
1800                                 break;
1801                         case PWR_CMD_END:
1802                                 RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1803                                          "rtl_hal_pwrseqcmdparsing(): PWR_CMD_END\n");
1804                                 return true;
1805                         default:
1806                                 RT_ASSERT(false,
1807                                           "rtl_hal_pwrseqcmdparsing(): Unknown CMD!!\n");
1808                                 break;
1809                         }
1810                 }
1811                 ary_idx++;
1812         } while (1);
1813
1814         return true;
1815 }
1816 EXPORT_SYMBOL(rtl_hal_pwrseqcmdparsing);
1817
1818 bool rtl_cmd_send_packet(struct ieee80211_hw *hw, struct sk_buff *skb)
1819 {
1820         struct rtl_priv *rtlpriv = rtl_priv(hw);
1821         struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1822         struct rtl8192_tx_ring *ring;
1823         struct rtl_tx_desc *pdesc;
1824         unsigned long flags;
1825         struct sk_buff *pskb = NULL;
1826
1827         ring = &rtlpci->tx_ring[BEACON_QUEUE];
1828
1829         spin_lock_irqsave(&rtlpriv->locks.irq_th_lock, flags);
1830         pskb = __skb_dequeue(&ring->queue);
1831         kfree_skb(pskb);
1832
1833         /*this is wrong, fill_tx_cmddesc needs update*/
1834         pdesc = &ring->desc[0];
1835
1836         rtlpriv->cfg->ops->fill_tx_cmddesc(hw, (u8 *)pdesc, 1, 1, skb);
1837
1838         __skb_queue_tail(&ring->queue, skb);
1839
1840         spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);
1841
1842         rtlpriv->cfg->ops->tx_polling(hw, BEACON_QUEUE);
1843
1844         return true;
1845 }
1846 EXPORT_SYMBOL(rtl_cmd_send_packet);
1847 const struct ieee80211_ops rtl_ops = {
1848         .start = rtl_op_start,
1849         .stop = rtl_op_stop,
1850         .tx = rtl_op_tx,
1851         .add_interface = rtl_op_add_interface,
1852         .remove_interface = rtl_op_remove_interface,
1853         .change_interface = rtl_op_change_interface,
1854 #ifdef CONFIG_PM
1855         .suspend = rtl_op_suspend,
1856         .resume = rtl_op_resume,
1857 #endif
1858         .config = rtl_op_config,
1859         .configure_filter = rtl_op_configure_filter,
1860         .set_key = rtl_op_set_key,
1861         .conf_tx = rtl_op_conf_tx,
1862         .bss_info_changed = rtl_op_bss_info_changed,
1863         .get_tsf = rtl_op_get_tsf,
1864         .set_tsf = rtl_op_set_tsf,
1865         .reset_tsf = rtl_op_reset_tsf,
1866         .sta_notify = rtl_op_sta_notify,
1867         .ampdu_action = rtl_op_ampdu_action,
1868         .sw_scan_start = rtl_op_sw_scan_start,
1869         .sw_scan_complete = rtl_op_sw_scan_complete,
1870         .rfkill_poll = rtl_op_rfkill_poll,
1871         .sta_add = rtl_op_sta_add,
1872         .sta_remove = rtl_op_sta_remove,
1873         .flush = rtl_op_flush,
1874 };
1875 EXPORT_SYMBOL_GPL(rtl_ops);
1876
1877 bool rtl_btc_status_false(void)
1878 {
1879         return false;
1880 }
1881 EXPORT_SYMBOL_GPL(rtl_btc_status_false);
1882
1883 void rtl_dm_diginit(struct ieee80211_hw *hw, u32 cur_igvalue)
1884 {
1885         struct rtl_priv *rtlpriv = rtl_priv(hw);
1886         struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
1887
1888         dm_digtable->dig_enable_flag = true;
1889         dm_digtable->dig_ext_port_stage = DIG_EXT_PORT_STAGE_MAX;
1890         dm_digtable->cur_igvalue = cur_igvalue;
1891         dm_digtable->pre_igvalue = 0;
1892         dm_digtable->cur_sta_cstate = DIG_STA_DISCONNECT;
1893         dm_digtable->presta_cstate = DIG_STA_DISCONNECT;
1894         dm_digtable->curmultista_cstate = DIG_MULTISTA_DISCONNECT;
1895         dm_digtable->rssi_lowthresh = DM_DIG_THRESH_LOW;
1896         dm_digtable->rssi_highthresh = DM_DIG_THRESH_HIGH;
1897         dm_digtable->fa_lowthresh = DM_FALSEALARM_THRESH_LOW;
1898         dm_digtable->fa_highthresh = DM_FALSEALARM_THRESH_HIGH;
1899         dm_digtable->rx_gain_max = DM_DIG_MAX;
1900         dm_digtable->rx_gain_min = DM_DIG_MIN;
1901         dm_digtable->back_val = DM_DIG_BACKOFF_DEFAULT;
1902         dm_digtable->back_range_max = DM_DIG_BACKOFF_MAX;
1903         dm_digtable->back_range_min = DM_DIG_BACKOFF_MIN;
1904         dm_digtable->pre_cck_cca_thres = 0xff;
1905         dm_digtable->cur_cck_cca_thres = 0x83;
1906         dm_digtable->forbidden_igi = DM_DIG_MIN;
1907         dm_digtable->large_fa_hit = 0;
1908         dm_digtable->recover_cnt = 0;
1909         dm_digtable->dig_min_0 = 0x25;
1910         dm_digtable->dig_min_1 = 0x25;
1911         dm_digtable->media_connect_0 = false;
1912         dm_digtable->media_connect_1 = false;
1913         rtlpriv->dm.dm_initialgain_enable = true;
1914         dm_digtable->bt30_cur_igi = 0x32;
1915         dm_digtable->pre_cck_pd_state = CCK_PD_STAGE_MAX;
1916         dm_digtable->cur_cck_pd_state = CCK_PD_STAGE_LOWRSSI;
1917 }
1918 EXPORT_SYMBOL(rtl_dm_diginit);