]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/wireless/realtek/rtlwifi/core.c
rtlwifi: fix spelling mistake: "encrypiton" -> "encryption"
[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("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                 pr_err("Firmware is too big!\n");
121                 release_firmware(firmware);
122                 return;
123         }
124         if (!is_wow) {
125                 memcpy(rtlpriv->rtlhal.pfirmware, firmware->data,
126                        firmware->size);
127                 rtlpriv->rtlhal.fwsize = firmware->size;
128         } else {
129                 memcpy(rtlpriv->rtlhal.wowlan_firmware, firmware->data,
130                        firmware->size);
131                 rtlpriv->rtlhal.wowlan_fwsize = firmware->size;
132         }
133         rtlpriv->rtlhal.fwsize = firmware->size;
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                 pr_err("operation mode %d is not supported!\n",
306                        vif->type);
307                 err = -EOPNOTSUPP;
308                 goto out;
309         }
310
311         if (mac->p2p) {
312                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
313                          "p2p role %x\n", vif->type);
314                 mac->basic_rates = 0xff0;/*disable cck rate for p2p*/
315                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
316                                 (u8 *)(&mac->basic_rates));
317         }
318         mac->vif = vif;
319         mac->opmode = vif->type;
320         rtlpriv->cfg->ops->set_network_type(hw, vif->type);
321         memcpy(mac->mac_addr, vif->addr, ETH_ALEN);
322         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_ETHER_ADDR, mac->mac_addr);
323
324 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                                         pr_err("switch case %#x not processed\n",
767                                                channel_type);
768                                         break;
769                         }
770                 }
771
772                 if (wide_chan <= 0)
773                         wide_chan = 1;
774
775                 /* In scanning, when before we offchannel we may send a ps=1
776                  * null to AP, and then we may send a ps = 0 null to AP quickly,
777                  * but first null may have caused AP to put lots of packet to
778                  * hw tx buffer. These packets must be tx'd before we go off
779                  * channel so we must delay more time to let AP flush these
780                  * packets before going offchannel, or dis-association or
781                  * delete BA will be caused by AP
782                  */
783                 if (rtlpriv->mac80211.offchan_delay) {
784                         rtlpriv->mac80211.offchan_delay = false;
785                         mdelay(50);
786                 }
787
788                 rtlphy->current_channel = wide_chan;
789
790                 rtlpriv->cfg->ops->switch_channel(hw);
791                 rtlpriv->cfg->ops->set_channel_access(hw);
792                 rtlpriv->cfg->ops->set_bw_mode(hw, channel_type);
793         }
794
795         mutex_unlock(&rtlpriv->locks.conf_mutex);
796
797         return 0;
798 }
799
800 static void rtl_op_configure_filter(struct ieee80211_hw *hw,
801                                     unsigned int changed_flags,
802                                     unsigned int *new_flags, u64 multicast)
803 {
804         bool update_rcr = false;
805         struct rtl_priv *rtlpriv = rtl_priv(hw);
806         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
807
808         *new_flags &= RTL_SUPPORTED_FILTERS;
809         if (0 == changed_flags)
810                 return;
811
812         /*TODO: we disable broadcase now, so enable here */
813         if (changed_flags & FIF_ALLMULTI) {
814                 if (*new_flags & FIF_ALLMULTI) {
815                         mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_AM] |
816                             rtlpriv->cfg->maps[MAC_RCR_AB];
817                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
818                                  "Enable receive multicast frame\n");
819                 } else {
820                         mac->rx_conf &= ~(rtlpriv->cfg->maps[MAC_RCR_AM] |
821                                           rtlpriv->cfg->maps[MAC_RCR_AB]);
822                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
823                                  "Disable receive multicast frame\n");
824                 }
825                 update_rcr = true;
826         }
827
828         if (changed_flags & FIF_FCSFAIL) {
829                 if (*new_flags & FIF_FCSFAIL) {
830                         mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_ACRC32];
831                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
832                                  "Enable receive FCS error frame\n");
833                 } else {
834                         mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_ACRC32];
835                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
836                                  "Disable receive FCS error frame\n");
837                 }
838                 if (!update_rcr)
839                         update_rcr = true;
840         }
841
842         /* if ssid not set to hw don't check bssid
843          * here just used for linked scanning, & linked
844          * and nolink check bssid is set in set network_type
845          */
846         if ((changed_flags & FIF_BCN_PRBRESP_PROMISC) &&
847             (mac->link_state >= MAC80211_LINKED)) {
848                 if (mac->opmode != NL80211_IFTYPE_AP &&
849                     mac->opmode != NL80211_IFTYPE_MESH_POINT) {
850                         if (*new_flags & FIF_BCN_PRBRESP_PROMISC)
851                                 rtlpriv->cfg->ops->set_chk_bssid(hw, false);
852                         else
853                                 rtlpriv->cfg->ops->set_chk_bssid(hw, true);
854                         if (update_rcr)
855                                 update_rcr = false;
856                 }
857         }
858
859         if (changed_flags & FIF_CONTROL) {
860                 if (*new_flags & FIF_CONTROL) {
861                         mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_ACF];
862
863                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
864                                  "Enable receive control frame.\n");
865                 } else {
866                         mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_ACF];
867                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
868                                  "Disable receive control frame.\n");
869                 }
870                 if (!update_rcr)
871                         update_rcr = true;
872         }
873
874         if (changed_flags & FIF_OTHER_BSS) {
875                 if (*new_flags & FIF_OTHER_BSS) {
876                         mac->rx_conf |= rtlpriv->cfg->maps[MAC_RCR_AAP];
877                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
878                                  "Enable receive other BSS's frame.\n");
879                 } else {
880                         mac->rx_conf &= ~rtlpriv->cfg->maps[MAC_RCR_AAP];
881                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
882                                  "Disable receive other BSS's frame.\n");
883                 }
884                 if (!update_rcr)
885                         update_rcr = true;
886         }
887
888         if (update_rcr)
889                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RCR,
890                                               (u8 *)(&mac->rx_conf));
891 }
892 static int rtl_op_sta_add(struct ieee80211_hw *hw,
893                          struct ieee80211_vif *vif,
894                          struct ieee80211_sta *sta)
895 {
896         struct rtl_priv *rtlpriv = rtl_priv(hw);
897         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
898         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
899         struct rtl_sta_info *sta_entry;
900
901         if (sta) {
902                 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
903                 spin_lock_bh(&rtlpriv->locks.entry_list_lock);
904                 list_add_tail(&sta_entry->list, &rtlpriv->entry_list);
905                 spin_unlock_bh(&rtlpriv->locks.entry_list_lock);
906                 if (rtlhal->current_bandtype == BAND_ON_2_4G) {
907                         sta_entry->wireless_mode = WIRELESS_MODE_G;
908                         if (sta->supp_rates[0] <= 0xf)
909                                 sta_entry->wireless_mode = WIRELESS_MODE_B;
910                         if (sta->ht_cap.ht_supported)
911                                 sta_entry->wireless_mode = WIRELESS_MODE_N_24G;
912
913                         if (vif->type == NL80211_IFTYPE_ADHOC)
914                                 sta_entry->wireless_mode = WIRELESS_MODE_G;
915                 } else if (rtlhal->current_bandtype == BAND_ON_5G) {
916                         sta_entry->wireless_mode = WIRELESS_MODE_A;
917                         if (sta->ht_cap.ht_supported)
918                                 sta_entry->wireless_mode = WIRELESS_MODE_N_5G;
919                         if (sta->vht_cap.vht_supported)
920                                 sta_entry->wireless_mode = WIRELESS_MODE_AC_5G;
921
922                         if (vif->type == NL80211_IFTYPE_ADHOC)
923                                 sta_entry->wireless_mode = WIRELESS_MODE_A;
924                 }
925                 /*disable cck rate for p2p*/
926                 if (mac->p2p)
927                         sta->supp_rates[0] &= 0xfffffff0;
928
929                 memcpy(sta_entry->mac_addr, sta->addr, ETH_ALEN);
930                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
931                         "Add sta addr is %pM\n", sta->addr);
932                 rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0);
933         }
934
935         return 0;
936 }
937
938 static int rtl_op_sta_remove(struct ieee80211_hw *hw,
939                                 struct ieee80211_vif *vif,
940                                 struct ieee80211_sta *sta)
941 {
942         struct rtl_priv *rtlpriv = rtl_priv(hw);
943         struct rtl_sta_info *sta_entry;
944         if (sta) {
945                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
946                          "Remove sta addr is %pM\n", sta->addr);
947                 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
948                 sta_entry->wireless_mode = 0;
949                 sta_entry->ratr_index = 0;
950                 spin_lock_bh(&rtlpriv->locks.entry_list_lock);
951                 list_del(&sta_entry->list);
952                 spin_unlock_bh(&rtlpriv->locks.entry_list_lock);
953         }
954         return 0;
955 }
956 static int _rtl_get_hal_qnum(u16 queue)
957 {
958         int qnum;
959
960         switch (queue) {
961         case 0:
962                 qnum = AC3_VO;
963                 break;
964         case 1:
965                 qnum = AC2_VI;
966                 break;
967         case 2:
968                 qnum = AC0_BE;
969                 break;
970         case 3:
971                 qnum = AC1_BK;
972                 break;
973         default:
974                 qnum = AC0_BE;
975                 break;
976         }
977         return qnum;
978 }
979
980 /*
981  *for mac80211 VO = 0, VI = 1, BE = 2, BK = 3
982  *for rtl819x  BE = 0, BK = 1, VI = 2, VO = 3
983  */
984 static int rtl_op_conf_tx(struct ieee80211_hw *hw,
985                           struct ieee80211_vif *vif, u16 queue,
986                           const struct ieee80211_tx_queue_params *param)
987 {
988         struct rtl_priv *rtlpriv = rtl_priv(hw);
989         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
990         int aci;
991
992         if (queue >= AC_MAX) {
993                 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
994                          "queue number %d is incorrect!\n", queue);
995                 return -EINVAL;
996         }
997
998         aci = _rtl_get_hal_qnum(queue);
999         mac->ac[aci].aifs = param->aifs;
1000         mac->ac[aci].cw_min = cpu_to_le16(param->cw_min);
1001         mac->ac[aci].cw_max = cpu_to_le16(param->cw_max);
1002         mac->ac[aci].tx_op = cpu_to_le16(param->txop);
1003         memcpy(&mac->edca_param[aci], param, sizeof(*param));
1004         rtlpriv->cfg->ops->set_qos(hw, aci);
1005         return 0;
1006 }
1007
1008 static void send_beacon_frame(struct ieee80211_hw *hw,
1009                               struct ieee80211_vif *vif)
1010 {
1011         struct rtl_priv *rtlpriv = rtl_priv(hw);
1012         struct sk_buff *skb = ieee80211_beacon_get(hw, vif);
1013         struct rtl_tcb_desc tcb_desc;
1014
1015         if (skb) {
1016                 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
1017                 rtlpriv->intf_ops->adapter_tx(hw, NULL, skb, &tcb_desc);
1018         }
1019 }
1020
1021 static void rtl_op_bss_info_changed(struct ieee80211_hw *hw,
1022                                     struct ieee80211_vif *vif,
1023                                     struct ieee80211_bss_conf *bss_conf,
1024                                     u32 changed)
1025 {
1026         struct rtl_priv *rtlpriv = rtl_priv(hw);
1027         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
1028         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1029         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1030
1031         mutex_lock(&rtlpriv->locks.conf_mutex);
1032         if ((vif->type == NL80211_IFTYPE_ADHOC) ||
1033             (vif->type == NL80211_IFTYPE_AP) ||
1034             (vif->type == NL80211_IFTYPE_MESH_POINT)) {
1035                 if ((changed & BSS_CHANGED_BEACON) ||
1036                     (changed & BSS_CHANGED_BEACON_ENABLED &&
1037                      bss_conf->enable_beacon)) {
1038                         if (mac->beacon_enabled == 0) {
1039                                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
1040                                          "BSS_CHANGED_BEACON_ENABLED\n");
1041
1042                                 /*start hw beacon interrupt. */
1043                                 /*rtlpriv->cfg->ops->set_bcn_reg(hw); */
1044                                 mac->beacon_enabled = 1;
1045                                 rtlpriv->cfg->ops->update_interrupt_mask(hw,
1046                                                 rtlpriv->cfg->maps
1047                                                 [RTL_IBSS_INT_MASKS], 0);
1048
1049                                 if (rtlpriv->cfg->ops->linked_set_reg)
1050                                         rtlpriv->cfg->ops->linked_set_reg(hw);
1051                                 send_beacon_frame(hw, vif);
1052                         }
1053                 }
1054                 if ((changed & BSS_CHANGED_BEACON_ENABLED &&
1055                     !bss_conf->enable_beacon)) {
1056                         if (mac->beacon_enabled == 1) {
1057                                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
1058                                          "ADHOC DISABLE BEACON\n");
1059
1060                                 mac->beacon_enabled = 0;
1061                                 rtlpriv->cfg->ops->update_interrupt_mask(hw, 0,
1062                                                 rtlpriv->cfg->maps
1063                                                 [RTL_IBSS_INT_MASKS]);
1064                         }
1065                 }
1066                 if (changed & BSS_CHANGED_BEACON_INT) {
1067                         RT_TRACE(rtlpriv, COMP_BEACON, DBG_TRACE,
1068                                  "BSS_CHANGED_BEACON_INT\n");
1069                         mac->beacon_interval = bss_conf->beacon_int;
1070                         rtlpriv->cfg->ops->set_bcn_intv(hw);
1071                 }
1072         }
1073
1074         /*TODO: reference to enum ieee80211_bss_change */
1075         if (changed & BSS_CHANGED_ASSOC) {
1076                 u8 mstatus;
1077                 if (bss_conf->assoc) {
1078                         struct ieee80211_sta *sta = NULL;
1079                         u8 keep_alive = 10;
1080
1081                         mstatus = RT_MEDIA_CONNECT;
1082                         /* we should reset all sec info & cam
1083                          * before set cam after linked, we should not
1084                          * reset in disassoc, that will cause tkip->wep
1085                          * fail because some flag will be wrong */
1086                         /* reset sec info */
1087                         rtl_cam_reset_sec_info(hw);
1088                         /* reset cam to fix wep fail issue
1089                          * when change from wpa to wep */
1090                         rtl_cam_reset_all_entry(hw);
1091
1092                         mac->link_state = MAC80211_LINKED;
1093                         mac->cnt_after_linked = 0;
1094                         mac->assoc_id = bss_conf->aid;
1095                         memcpy(mac->bssid, bss_conf->bssid, ETH_ALEN);
1096
1097                         if (rtlpriv->cfg->ops->linked_set_reg)
1098                                 rtlpriv->cfg->ops->linked_set_reg(hw);
1099
1100                         rcu_read_lock();
1101                         sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
1102                         if (!sta) {
1103                                 rcu_read_unlock();
1104                                 goto out;
1105                         }
1106                         RT_TRACE(rtlpriv, COMP_EASY_CONCURRENT, DBG_LOUD,
1107                                  "send PS STATIC frame\n");
1108                         if (rtlpriv->dm.supp_phymode_switch) {
1109                                 if (sta->ht_cap.ht_supported)
1110                                         rtl_send_smps_action(hw, sta,
1111                                                         IEEE80211_SMPS_STATIC);
1112                         }
1113
1114                         if (rtlhal->current_bandtype == BAND_ON_5G) {
1115                                 mac->mode = WIRELESS_MODE_A;
1116                         } else {
1117                                 if (sta->supp_rates[0] <= 0xf)
1118                                         mac->mode = WIRELESS_MODE_B;
1119                                 else
1120                                         mac->mode = WIRELESS_MODE_G;
1121                         }
1122
1123                         if (sta->ht_cap.ht_supported) {
1124                                 if (rtlhal->current_bandtype == BAND_ON_2_4G)
1125                                         mac->mode = WIRELESS_MODE_N_24G;
1126                                 else
1127                                         mac->mode = WIRELESS_MODE_N_5G;
1128                         }
1129
1130                         if (sta->vht_cap.vht_supported) {
1131                                 if (rtlhal->current_bandtype == BAND_ON_5G)
1132                                         mac->mode = WIRELESS_MODE_AC_5G;
1133                                 else
1134                                         mac->mode = WIRELESS_MODE_AC_24G;
1135                         }
1136
1137                         if (vif->type == NL80211_IFTYPE_STATION)
1138                                 rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0);
1139                         rcu_read_unlock();
1140
1141                         /* to avoid AP Disassociation caused by inactivity */
1142                         rtlpriv->cfg->ops->set_hw_reg(hw,
1143                                                       HW_VAR_KEEP_ALIVE,
1144                                                       (u8 *)(&keep_alive));
1145
1146                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
1147                                  "BSS_CHANGED_ASSOC\n");
1148                 } else {
1149                         mstatus = RT_MEDIA_DISCONNECT;
1150
1151                         if (mac->link_state == MAC80211_LINKED)
1152                                 rtl_lps_leave(hw);
1153                         if (ppsc->p2p_ps_info.p2p_ps_mode > P2P_PS_NONE)
1154                                 rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
1155                         mac->link_state = MAC80211_NOLINK;
1156                         eth_zero_addr(mac->bssid);
1157                         mac->vendor = PEER_UNKNOWN;
1158                         mac->mode = 0;
1159
1160                         if (rtlpriv->dm.supp_phymode_switch) {
1161                                 if (rtlpriv->cfg->ops->chk_switch_dmdp)
1162                                         rtlpriv->cfg->ops->chk_switch_dmdp(hw);
1163                         }
1164                         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
1165                                  "BSS_CHANGED_UN_ASSOC\n");
1166                 }
1167                 rtlpriv->cfg->ops->set_network_type(hw, vif->type);
1168                 /* For FW LPS:
1169                  * To tell firmware we have connected or disconnected
1170                  */
1171                 rtlpriv->cfg->ops->set_hw_reg(hw,
1172                                               HW_VAR_H2C_FW_JOINBSSRPT,
1173                                               (u8 *)(&mstatus));
1174                 ppsc->report_linked = (mstatus == RT_MEDIA_CONNECT) ?
1175                                       true : false;
1176
1177                 if (rtlpriv->cfg->ops->get_btc_status())
1178                         rtlpriv->btcoexist.btc_ops->btc_mediastatus_notify(
1179                                                         rtlpriv, mstatus);
1180         }
1181
1182         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1183                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1184                          "BSS_CHANGED_ERP_CTS_PROT\n");
1185                 mac->use_cts_protect = bss_conf->use_cts_prot;
1186         }
1187
1188         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1189                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD,
1190                          "BSS_CHANGED_ERP_PREAMBLE use short preamble:%x\n",
1191                           bss_conf->use_short_preamble);
1192
1193                 mac->short_preamble = bss_conf->use_short_preamble;
1194                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_ACK_PREAMBLE,
1195                                               (u8 *)(&mac->short_preamble));
1196         }
1197
1198         if (changed & BSS_CHANGED_ERP_SLOT) {
1199                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1200                          "BSS_CHANGED_ERP_SLOT\n");
1201
1202                 if (bss_conf->use_short_slot)
1203                         mac->slot_time = RTL_SLOT_TIME_9;
1204                 else
1205                         mac->slot_time = RTL_SLOT_TIME_20;
1206
1207                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SLOT_TIME,
1208                                               (u8 *)(&mac->slot_time));
1209         }
1210
1211         if (changed & BSS_CHANGED_HT) {
1212                 struct ieee80211_sta *sta = NULL;
1213
1214                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1215                          "BSS_CHANGED_HT\n");
1216
1217                 rcu_read_lock();
1218                 sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
1219                 if (sta) {
1220                         if (sta->ht_cap.ampdu_density >
1221                             mac->current_ampdu_density)
1222                                 mac->current_ampdu_density =
1223                                     sta->ht_cap.ampdu_density;
1224                         if (sta->ht_cap.ampdu_factor <
1225                             mac->current_ampdu_factor)
1226                                 mac->current_ampdu_factor =
1227                                     sta->ht_cap.ampdu_factor;
1228                 }
1229                 rcu_read_unlock();
1230
1231                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SHORTGI_DENSITY,
1232                                               (u8 *)(&mac->max_mss_density));
1233                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AMPDU_FACTOR,
1234                                               &mac->current_ampdu_factor);
1235                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AMPDU_MIN_SPACE,
1236                                               &mac->current_ampdu_density);
1237         }
1238
1239         if (changed & BSS_CHANGED_BSSID) {
1240                 u32 basic_rates;
1241                 struct ieee80211_sta *sta = NULL;
1242
1243                 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BSSID,
1244                                               (u8 *)bss_conf->bssid);
1245
1246                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_DMESG,
1247                          "bssid: %pM\n", bss_conf->bssid);
1248
1249                 mac->vendor = PEER_UNKNOWN;
1250                 memcpy(mac->bssid, bss_conf->bssid, ETH_ALEN);
1251
1252                 rcu_read_lock();
1253                 sta = ieee80211_find_sta(vif, (u8 *)bss_conf->bssid);
1254                 if (!sta) {
1255                         rcu_read_unlock();
1256                         goto out;
1257                 }
1258
1259                 if (rtlhal->current_bandtype == BAND_ON_5G) {
1260                         mac->mode = WIRELESS_MODE_A;
1261                 } else {
1262                         if (sta->supp_rates[0] <= 0xf)
1263                                 mac->mode = WIRELESS_MODE_B;
1264                         else
1265                                 mac->mode = WIRELESS_MODE_G;
1266                 }
1267
1268                 if (sta->ht_cap.ht_supported) {
1269                         if (rtlhal->current_bandtype == BAND_ON_2_4G)
1270                                 mac->mode = WIRELESS_MODE_N_24G;
1271                         else
1272                                 mac->mode = WIRELESS_MODE_N_5G;
1273                 }
1274
1275                 if (sta->vht_cap.vht_supported) {
1276                         if (rtlhal->current_bandtype == BAND_ON_5G)
1277                                 mac->mode = WIRELESS_MODE_AC_5G;
1278                         else
1279                                 mac->mode = WIRELESS_MODE_AC_24G;
1280                 }
1281
1282                 /* just station need it, because ibss & ap mode will
1283                  * set in sta_add, and will be NULL here */
1284                 if (vif->type == NL80211_IFTYPE_STATION) {
1285                         struct rtl_sta_info *sta_entry;
1286                         sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1287                         sta_entry->wireless_mode = mac->mode;
1288                 }
1289
1290                 if (sta->ht_cap.ht_supported) {
1291                         mac->ht_enable = true;
1292
1293                         /*
1294                          * for cisco 1252 bw20 it's wrong
1295                          * if (ht_cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
1296                          *      mac->bw_40 = true;
1297                          * }
1298                          * */
1299                 }
1300
1301                 if (sta->vht_cap.vht_supported)
1302                         mac->vht_enable = true;
1303
1304                 if (changed & BSS_CHANGED_BASIC_RATES) {
1305                         /* for 5G must << RATE_6M_INDEX = 4,
1306                          * because 5G have no cck rate*/
1307                         if (rtlhal->current_bandtype == BAND_ON_5G)
1308                                 basic_rates = sta->supp_rates[1] << 4;
1309                         else
1310                                 basic_rates = sta->supp_rates[0];
1311
1312                         mac->basic_rates = basic_rates;
1313                         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_BASIC_RATE,
1314                                         (u8 *)(&basic_rates));
1315                 }
1316                 rcu_read_unlock();
1317         }
1318 out:
1319         mutex_unlock(&rtlpriv->locks.conf_mutex);
1320 }
1321
1322 static u64 rtl_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1323 {
1324         struct rtl_priv *rtlpriv = rtl_priv(hw);
1325         u64 tsf;
1326
1327         rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_CORRECT_TSF, (u8 *)(&tsf));
1328         return tsf;
1329 }
1330
1331 static void rtl_op_set_tsf(struct ieee80211_hw *hw,
1332                            struct ieee80211_vif *vif, u64 tsf)
1333 {
1334         struct rtl_priv *rtlpriv = rtl_priv(hw);
1335         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1336         u8 bibss = (mac->opmode == NL80211_IFTYPE_ADHOC) ? 1 : 0;
1337
1338         mac->tsf = tsf;
1339         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_CORRECT_TSF, (u8 *)(&bibss));
1340 }
1341
1342 static void rtl_op_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1343 {
1344         struct rtl_priv *rtlpriv = rtl_priv(hw);
1345         u8 tmp = 0;
1346
1347         rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_DUAL_TSF_RST, (u8 *)(&tmp));
1348 }
1349
1350 static void rtl_op_sta_notify(struct ieee80211_hw *hw,
1351                               struct ieee80211_vif *vif,
1352                               enum sta_notify_cmd cmd,
1353                               struct ieee80211_sta *sta)
1354 {
1355         switch (cmd) {
1356         case STA_NOTIFY_SLEEP:
1357                 break;
1358         case STA_NOTIFY_AWAKE:
1359                 break;
1360         default:
1361                 break;
1362         }
1363 }
1364
1365 static int rtl_op_ampdu_action(struct ieee80211_hw *hw,
1366                                struct ieee80211_vif *vif,
1367                                struct ieee80211_ampdu_params *params)
1368 {
1369         struct rtl_priv *rtlpriv = rtl_priv(hw);
1370         struct ieee80211_sta *sta = params->sta;
1371         enum ieee80211_ampdu_mlme_action action = params->action;
1372         u16 tid = params->tid;
1373         u16 *ssn = &params->ssn;
1374
1375         switch (action) {
1376         case IEEE80211_AMPDU_TX_START:
1377                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1378                          "IEEE80211_AMPDU_TX_START: TID:%d\n", tid);
1379                 return rtl_tx_agg_start(hw, vif, sta, tid, ssn);
1380         case IEEE80211_AMPDU_TX_STOP_CONT:
1381         case IEEE80211_AMPDU_TX_STOP_FLUSH:
1382         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1383                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1384                          "IEEE80211_AMPDU_TX_STOP: TID:%d\n", tid);
1385                 return rtl_tx_agg_stop(hw, vif, sta, tid);
1386         case IEEE80211_AMPDU_TX_OPERATIONAL:
1387                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1388                          "IEEE80211_AMPDU_TX_OPERATIONAL:TID:%d\n", tid);
1389                 rtl_tx_agg_oper(hw, sta, tid);
1390                 break;
1391         case IEEE80211_AMPDU_RX_START:
1392                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1393                          "IEEE80211_AMPDU_RX_START:TID:%d\n", tid);
1394                 return rtl_rx_agg_start(hw, sta, tid);
1395         case IEEE80211_AMPDU_RX_STOP:
1396                 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_TRACE,
1397                          "IEEE80211_AMPDU_RX_STOP:TID:%d\n", tid);
1398                 return rtl_rx_agg_stop(hw, sta, tid);
1399         default:
1400                 pr_err("IEEE80211_AMPDU_ERR!!!!:\n");
1401                 return -EOPNOTSUPP;
1402         }
1403         return 0;
1404 }
1405
1406 static void rtl_op_sw_scan_start(struct ieee80211_hw *hw,
1407                                  struct ieee80211_vif *vif,
1408                                  const u8 *mac_addr)
1409 {
1410         struct rtl_priv *rtlpriv = rtl_priv(hw);
1411         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1412
1413         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "\n");
1414         mac->act_scanning = true;
1415         if (rtlpriv->link_info.higher_busytraffic) {
1416                 mac->skip_scan = true;
1417                 return;
1418         }
1419
1420         if (rtlpriv->cfg->ops->get_btc_status())
1421                 rtlpriv->btcoexist.btc_ops->btc_scan_notify(rtlpriv, 1);
1422
1423         if (rtlpriv->dm.supp_phymode_switch) {
1424                 if (rtlpriv->cfg->ops->chk_switch_dmdp)
1425                         rtlpriv->cfg->ops->chk_switch_dmdp(hw);
1426         }
1427
1428         if (mac->link_state == MAC80211_LINKED) {
1429                 rtl_lps_leave(hw);
1430                 mac->link_state = MAC80211_LINKED_SCANNING;
1431         } else {
1432                 rtl_ips_nic_on(hw);
1433         }
1434
1435         /* Dul mac */
1436         rtlpriv->rtlhal.load_imrandiqk_setting_for2g = false;
1437
1438         rtlpriv->cfg->ops->led_control(hw, LED_CTL_SITE_SURVEY);
1439         rtlpriv->cfg->ops->scan_operation_backup(hw, SCAN_OPT_BACKUP_BAND0);
1440 }
1441
1442 static void rtl_op_sw_scan_complete(struct ieee80211_hw *hw,
1443                                     struct ieee80211_vif *vif)
1444 {
1445         struct rtl_priv *rtlpriv = rtl_priv(hw);
1446         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1447
1448         RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "\n");
1449         mac->act_scanning = false;
1450         mac->skip_scan = false;
1451         if (rtlpriv->link_info.higher_busytraffic)
1452                 return;
1453
1454         /* p2p will use 1/6/11 to scan */
1455         if (mac->n_channels == 3)
1456                 mac->p2p_in_use = true;
1457         else
1458                 mac->p2p_in_use = false;
1459         mac->n_channels = 0;
1460         /* Dul mac */
1461         rtlpriv->rtlhal.load_imrandiqk_setting_for2g = false;
1462
1463         if (mac->link_state == MAC80211_LINKED_SCANNING) {
1464                 mac->link_state = MAC80211_LINKED;
1465                 if (mac->opmode == NL80211_IFTYPE_STATION) {
1466                         /* fix fwlps issue */
1467                         rtlpriv->cfg->ops->set_network_type(hw, mac->opmode);
1468                 }
1469         }
1470
1471         rtlpriv->cfg->ops->scan_operation_backup(hw, SCAN_OPT_RESTORE);
1472         if (rtlpriv->cfg->ops->get_btc_status())
1473                 rtlpriv->btcoexist.btc_ops->btc_scan_notify(rtlpriv, 0);
1474 }
1475
1476 static int rtl_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
1477                           struct ieee80211_vif *vif, struct ieee80211_sta *sta,
1478                           struct ieee80211_key_conf *key)
1479 {
1480         struct rtl_priv *rtlpriv = rtl_priv(hw);
1481         u8 key_type = NO_ENCRYPTION;
1482         u8 key_idx;
1483         bool group_key = false;
1484         bool wep_only = false;
1485         int err = 0;
1486         u8 mac_addr[ETH_ALEN];
1487         u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1488
1489         if (rtlpriv->cfg->mod_params->sw_crypto || rtlpriv->sec.use_sw_sec) {
1490                 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
1491                          "not open hw encryption\n");
1492                 return -ENOSPC; /*User disabled HW-crypto */
1493         }
1494         /* To support IBSS, use sw-crypto for GTK */
1495         if (((vif->type == NL80211_IFTYPE_ADHOC) ||
1496             (vif->type == NL80211_IFTYPE_MESH_POINT)) &&
1497            !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1498                 return -ENOSPC;
1499         RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1500                  "%s hardware based encryption for keyidx: %d, mac: %pM\n",
1501                   cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
1502                   sta ? sta->addr : bcast_addr);
1503         rtlpriv->sec.being_setkey = true;
1504         rtl_ips_nic_on(hw);
1505         mutex_lock(&rtlpriv->locks.conf_mutex);
1506         /* <1> get encryption alg */
1507
1508         switch (key->cipher) {
1509         case WLAN_CIPHER_SUITE_WEP40:
1510                 key_type = WEP40_ENCRYPTION;
1511                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:WEP40\n");
1512                 break;
1513         case WLAN_CIPHER_SUITE_WEP104:
1514                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:WEP104\n");
1515                 key_type = WEP104_ENCRYPTION;
1516                 break;
1517         case WLAN_CIPHER_SUITE_TKIP:
1518                 key_type = TKIP_ENCRYPTION;
1519                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:TKIP\n");
1520                 break;
1521         case WLAN_CIPHER_SUITE_CCMP:
1522                 key_type = AESCCMP_ENCRYPTION;
1523                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:CCMP\n");
1524                 break;
1525         case WLAN_CIPHER_SUITE_AES_CMAC:
1526                 /* HW don't support CMAC encryption,
1527                  * use software CMAC encryption
1528                  */
1529                 key_type = AESCMAC_ENCRYPTION;
1530                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "alg:CMAC\n");
1531                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1532                          "HW don't support CMAC encryption, use software CMAC encryption\n");
1533                 err = -EOPNOTSUPP;
1534                 goto out_unlock;
1535         default:
1536                 pr_err("alg_err:%x!!!!:\n", key->cipher);
1537                 goto out_unlock;
1538         }
1539         if (key_type == WEP40_ENCRYPTION ||
1540            key_type == WEP104_ENCRYPTION ||
1541            vif->type == NL80211_IFTYPE_ADHOC)
1542                 rtlpriv->sec.use_defaultkey = true;
1543
1544         /* <2> get key_idx */
1545         key_idx = (u8) (key->keyidx);
1546         if (key_idx > 3)
1547                 goto out_unlock;
1548         /* <3> if pairwise key enable_hw_sec */
1549         group_key = !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE);
1550
1551         /* wep always be group key, but there are two conditions:
1552          * 1) wep only: is just for wep enc, in this condition
1553          * rtlpriv->sec.pairwise_enc_algorithm == NO_ENCRYPTION
1554          * will be true & enable_hw_sec will be set when wep
1555          * ke setting.
1556          * 2) wep(group) + AES(pairwise): some AP like cisco
1557          * may use it, in this condition enable_hw_sec will not
1558          * be set when wep key setting */
1559         /* we must reset sec_info after lingked before set key,
1560          * or some flag will be wrong*/
1561         if (vif->type == NL80211_IFTYPE_AP ||
1562                 vif->type == NL80211_IFTYPE_MESH_POINT) {
1563                 if (!group_key || key_type == WEP40_ENCRYPTION ||
1564                         key_type == WEP104_ENCRYPTION) {
1565                         if (group_key)
1566                                 wep_only = true;
1567                         rtlpriv->cfg->ops->enable_hw_sec(hw);
1568                 }
1569         } else {
1570                 if ((!group_key) || (vif->type == NL80211_IFTYPE_ADHOC) ||
1571                     rtlpriv->sec.pairwise_enc_algorithm == NO_ENCRYPTION) {
1572                         if (rtlpriv->sec.pairwise_enc_algorithm ==
1573                             NO_ENCRYPTION &&
1574                            (key_type == WEP40_ENCRYPTION ||
1575                             key_type == WEP104_ENCRYPTION))
1576                                 wep_only = true;
1577                         rtlpriv->sec.pairwise_enc_algorithm = key_type;
1578                         RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1579                                  "set enable_hw_sec, key_type:%x(OPEN:0 WEP40:1 TKIP:2 AES:4 WEP104:5)\n",
1580                                  key_type);
1581                         rtlpriv->cfg->ops->enable_hw_sec(hw);
1582                 }
1583         }
1584         /* <4> set key based on cmd */
1585         switch (cmd) {
1586         case SET_KEY:
1587                 if (wep_only) {
1588                         RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1589                                  "set WEP(group/pairwise) key\n");
1590                         /* Pairwise key with an assigned MAC address. */
1591                         rtlpriv->sec.pairwise_enc_algorithm = key_type;
1592                         rtlpriv->sec.group_enc_algorithm = key_type;
1593                         /*set local buf about wep key. */
1594                         memcpy(rtlpriv->sec.key_buf[key_idx],
1595                                key->key, key->keylen);
1596                         rtlpriv->sec.key_len[key_idx] = key->keylen;
1597                         eth_zero_addr(mac_addr);
1598                 } else if (group_key) { /* group key */
1599                         RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1600                                  "set group key\n");
1601                         /* group key */
1602                         rtlpriv->sec.group_enc_algorithm = key_type;
1603                         /*set local buf about group key. */
1604                         memcpy(rtlpriv->sec.key_buf[key_idx],
1605                                key->key, key->keylen);
1606                         rtlpriv->sec.key_len[key_idx] = key->keylen;
1607                         memcpy(mac_addr, bcast_addr, ETH_ALEN);
1608                 } else {        /* pairwise key */
1609                         RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1610                                  "set pairwise key\n");
1611                         if (!sta) {
1612                                 WARN_ONCE(true,
1613                                           "rtlwifi: pairwise key without mac_addr\n");
1614
1615                                 err = -EOPNOTSUPP;
1616                                 goto out_unlock;
1617                         }
1618                         /* Pairwise key with an assigned MAC address. */
1619                         rtlpriv->sec.pairwise_enc_algorithm = key_type;
1620                         /*set local buf about pairwise key. */
1621                         memcpy(rtlpriv->sec.key_buf[PAIRWISE_KEYIDX],
1622                                key->key, key->keylen);
1623                         rtlpriv->sec.key_len[PAIRWISE_KEYIDX] = key->keylen;
1624                         rtlpriv->sec.pairwise_key =
1625                             rtlpriv->sec.key_buf[PAIRWISE_KEYIDX];
1626                         memcpy(mac_addr, sta->addr, ETH_ALEN);
1627                 }
1628                 rtlpriv->cfg->ops->set_key(hw, key_idx, mac_addr,
1629                                            group_key, key_type, wep_only,
1630                                            false);
1631                 /* <5> tell mac80211 do something: */
1632                 /*must use sw generate IV, or can not work !!!!. */
1633                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1634                 key->hw_key_idx = key_idx;
1635                 if (key_type == TKIP_ENCRYPTION)
1636                         key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1637                 /*use software CCMP encryption for management frames (MFP) */
1638                 if (key_type == AESCCMP_ENCRYPTION)
1639                         key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
1640                 break;
1641         case DISABLE_KEY:
1642                 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1643                          "disable key delete one entry\n");
1644                 /*set local buf about wep key. */
1645                 if (vif->type == NL80211_IFTYPE_AP ||
1646                         vif->type == NL80211_IFTYPE_MESH_POINT) {
1647                         if (sta)
1648                                 rtl_cam_del_entry(hw, sta->addr);
1649                 }
1650                 memset(rtlpriv->sec.key_buf[key_idx], 0, key->keylen);
1651                 rtlpriv->sec.key_len[key_idx] = 0;
1652                 eth_zero_addr(mac_addr);
1653                 /*
1654                  *mac80211 will delete entrys one by one,
1655                  *so don't use rtl_cam_reset_all_entry
1656                  *or clear all entry here.
1657                  */
1658                 rtl_cam_delete_one_entry(hw, mac_addr, key_idx);
1659                 break;
1660         default:
1661                 pr_err("cmd_err:%x!!!!:\n", cmd);
1662         }
1663 out_unlock:
1664         mutex_unlock(&rtlpriv->locks.conf_mutex);
1665         rtlpriv->sec.being_setkey = false;
1666         return err;
1667 }
1668
1669 static void rtl_op_rfkill_poll(struct ieee80211_hw *hw)
1670 {
1671         struct rtl_priv *rtlpriv = rtl_priv(hw);
1672
1673         bool radio_state;
1674         bool blocked;
1675         u8 valid = 0;
1676
1677         if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
1678                 return;
1679
1680         mutex_lock(&rtlpriv->locks.conf_mutex);
1681
1682         /*if Radio On return true here */
1683         radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
1684
1685         if (valid) {
1686                 if (unlikely(radio_state != rtlpriv->rfkill.rfkill_state)) {
1687                         rtlpriv->rfkill.rfkill_state = radio_state;
1688
1689                         RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
1690                                  "wireless radio switch turned %s\n",
1691                                   radio_state ? "on" : "off");
1692
1693                         blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
1694                         wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
1695                 }
1696         }
1697
1698         mutex_unlock(&rtlpriv->locks.conf_mutex);
1699 }
1700
1701 /* this function is called by mac80211 to flush tx buffer
1702  * before switch channle or power save, or tx buffer packet
1703  * maybe send after offchannel or rf sleep, this may cause
1704  * dis-association by AP */
1705 static void rtl_op_flush(struct ieee80211_hw *hw,
1706                          struct ieee80211_vif *vif,
1707                          u32 queues,
1708                          bool drop)
1709 {
1710         struct rtl_priv *rtlpriv = rtl_priv(hw);
1711
1712         if (rtlpriv->intf_ops->flush)
1713                 rtlpriv->intf_ops->flush(hw, queues, drop);
1714 }
1715
1716 /*      Description:
1717  *              This routine deals with the Power Configuration CMD
1718  *               parsing for RTL8723/RTL8188E Series IC.
1719  *      Assumption:
1720  *              We should follow specific format that was released from HW SD.
1721  */
1722 bool rtl_hal_pwrseqcmdparsing(struct rtl_priv *rtlpriv, u8 cut_version,
1723                               u8 faversion, u8 interface_type,
1724                               struct wlan_pwr_cfg pwrcfgcmd[])
1725 {
1726         struct wlan_pwr_cfg cfg_cmd = {0};
1727         bool polling_bit = false;
1728         u32 ary_idx = 0;
1729         u8 value = 0;
1730         u32 offset = 0;
1731         u32 polling_count = 0;
1732         u32 max_polling_cnt = 5000;
1733
1734         do {
1735                 cfg_cmd = pwrcfgcmd[ary_idx];
1736                 RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1737                          "rtl_hal_pwrseqcmdparsing(): offset(%#x),cut_msk(%#x), famsk(%#x), interface_msk(%#x), base(%#x), cmd(%#x), msk(%#x), value(%#x)\n",
1738                          GET_PWR_CFG_OFFSET(cfg_cmd),
1739                                             GET_PWR_CFG_CUT_MASK(cfg_cmd),
1740                          GET_PWR_CFG_FAB_MASK(cfg_cmd),
1741                                               GET_PWR_CFG_INTF_MASK(cfg_cmd),
1742                          GET_PWR_CFG_BASE(cfg_cmd), GET_PWR_CFG_CMD(cfg_cmd),
1743                          GET_PWR_CFG_MASK(cfg_cmd), GET_PWR_CFG_VALUE(cfg_cmd));
1744
1745                 if ((GET_PWR_CFG_FAB_MASK(cfg_cmd)&faversion) &&
1746                     (GET_PWR_CFG_CUT_MASK(cfg_cmd)&cut_version) &&
1747                     (GET_PWR_CFG_INTF_MASK(cfg_cmd)&interface_type)) {
1748                         switch (GET_PWR_CFG_CMD(cfg_cmd)) {
1749                         case PWR_CMD_READ:
1750                                 RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1751                                         "rtl_hal_pwrseqcmdparsing(): PWR_CMD_READ\n");
1752                                 break;
1753                         case PWR_CMD_WRITE:
1754                                 RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1755                                         "rtl_hal_pwrseqcmdparsing(): PWR_CMD_WRITE\n");
1756                                 offset = GET_PWR_CFG_OFFSET(cfg_cmd);
1757
1758                                 /*Read the value from system register*/
1759                                 value = rtl_read_byte(rtlpriv, offset);
1760                                 value &= (~(GET_PWR_CFG_MASK(cfg_cmd)));
1761                                 value |= (GET_PWR_CFG_VALUE(cfg_cmd) &
1762                                           GET_PWR_CFG_MASK(cfg_cmd));
1763
1764                                 /*Write the value back to sytem register*/
1765                                 rtl_write_byte(rtlpriv, offset, value);
1766                                 break;
1767                         case PWR_CMD_POLLING:
1768                                 RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1769                                         "rtl_hal_pwrseqcmdparsing(): PWR_CMD_POLLING\n");
1770                                 polling_bit = false;
1771                                 offset = GET_PWR_CFG_OFFSET(cfg_cmd);
1772
1773                                 do {
1774                                         value = rtl_read_byte(rtlpriv, offset);
1775
1776                                         value &= GET_PWR_CFG_MASK(cfg_cmd);
1777                                         if (value ==
1778                                             (GET_PWR_CFG_VALUE(cfg_cmd) &
1779                                              GET_PWR_CFG_MASK(cfg_cmd)))
1780                                                 polling_bit = true;
1781                                         else
1782                                                 udelay(10);
1783
1784                                         if (polling_count++ > max_polling_cnt)
1785                                                 return false;
1786                                 } while (!polling_bit);
1787                                 break;
1788                         case PWR_CMD_DELAY:
1789                                 RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1790                                          "rtl_hal_pwrseqcmdparsing(): PWR_CMD_DELAY\n");
1791                                 if (GET_PWR_CFG_VALUE(cfg_cmd) ==
1792                                     PWRSEQ_DELAY_US)
1793                                         udelay(GET_PWR_CFG_OFFSET(cfg_cmd));
1794                                 else
1795                                         mdelay(GET_PWR_CFG_OFFSET(cfg_cmd));
1796                                 break;
1797                         case PWR_CMD_END:
1798                                 RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1799                                          "rtl_hal_pwrseqcmdparsing(): PWR_CMD_END\n");
1800                                 return true;
1801                         default:
1802                                 WARN_ONCE(true,
1803                                           "rtlwifi: rtl_hal_pwrseqcmdparsing(): Unknown CMD!!\n");
1804                                 break;
1805                         }
1806                 }
1807                 ary_idx++;
1808         } while (1);
1809
1810         return true;
1811 }
1812 EXPORT_SYMBOL(rtl_hal_pwrseqcmdparsing);
1813
1814 bool rtl_cmd_send_packet(struct ieee80211_hw *hw, struct sk_buff *skb)
1815 {
1816         struct rtl_priv *rtlpriv = rtl_priv(hw);
1817         struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1818         struct rtl8192_tx_ring *ring;
1819         struct rtl_tx_desc *pdesc;
1820         unsigned long flags;
1821         struct sk_buff *pskb = NULL;
1822
1823         ring = &rtlpci->tx_ring[BEACON_QUEUE];
1824
1825         spin_lock_irqsave(&rtlpriv->locks.irq_th_lock, flags);
1826         pskb = __skb_dequeue(&ring->queue);
1827         dev_kfree_skb_irq(pskb);
1828
1829         /*this is wrong, fill_tx_cmddesc needs update*/
1830         pdesc = &ring->desc[0];
1831
1832         rtlpriv->cfg->ops->fill_tx_cmddesc(hw, (u8 *)pdesc, 1, 1, skb);
1833
1834         __skb_queue_tail(&ring->queue, skb);
1835
1836         spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);
1837
1838         rtlpriv->cfg->ops->tx_polling(hw, BEACON_QUEUE);
1839
1840         return true;
1841 }
1842 EXPORT_SYMBOL(rtl_cmd_send_packet);
1843 const struct ieee80211_ops rtl_ops = {
1844         .start = rtl_op_start,
1845         .stop = rtl_op_stop,
1846         .tx = rtl_op_tx,
1847         .add_interface = rtl_op_add_interface,
1848         .remove_interface = rtl_op_remove_interface,
1849         .change_interface = rtl_op_change_interface,
1850 #ifdef CONFIG_PM
1851         .suspend = rtl_op_suspend,
1852         .resume = rtl_op_resume,
1853 #endif
1854         .config = rtl_op_config,
1855         .configure_filter = rtl_op_configure_filter,
1856         .set_key = rtl_op_set_key,
1857         .conf_tx = rtl_op_conf_tx,
1858         .bss_info_changed = rtl_op_bss_info_changed,
1859         .get_tsf = rtl_op_get_tsf,
1860         .set_tsf = rtl_op_set_tsf,
1861         .reset_tsf = rtl_op_reset_tsf,
1862         .sta_notify = rtl_op_sta_notify,
1863         .ampdu_action = rtl_op_ampdu_action,
1864         .sw_scan_start = rtl_op_sw_scan_start,
1865         .sw_scan_complete = rtl_op_sw_scan_complete,
1866         .rfkill_poll = rtl_op_rfkill_poll,
1867         .sta_add = rtl_op_sta_add,
1868         .sta_remove = rtl_op_sta_remove,
1869         .flush = rtl_op_flush,
1870 };
1871 EXPORT_SYMBOL_GPL(rtl_ops);
1872
1873 bool rtl_btc_status_false(void)
1874 {
1875         return false;
1876 }
1877 EXPORT_SYMBOL_GPL(rtl_btc_status_false);
1878
1879 void rtl_dm_diginit(struct ieee80211_hw *hw, u32 cur_igvalue)
1880 {
1881         struct rtl_priv *rtlpriv = rtl_priv(hw);
1882         struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
1883
1884         dm_digtable->dig_enable_flag = true;
1885         dm_digtable->dig_ext_port_stage = DIG_EXT_PORT_STAGE_MAX;
1886         dm_digtable->cur_igvalue = cur_igvalue;
1887         dm_digtable->pre_igvalue = 0;
1888         dm_digtable->cur_sta_cstate = DIG_STA_DISCONNECT;
1889         dm_digtable->presta_cstate = DIG_STA_DISCONNECT;
1890         dm_digtable->curmultista_cstate = DIG_MULTISTA_DISCONNECT;
1891         dm_digtable->rssi_lowthresh = DM_DIG_THRESH_LOW;
1892         dm_digtable->rssi_highthresh = DM_DIG_THRESH_HIGH;
1893         dm_digtable->fa_lowthresh = DM_FALSEALARM_THRESH_LOW;
1894         dm_digtable->fa_highthresh = DM_FALSEALARM_THRESH_HIGH;
1895         dm_digtable->rx_gain_max = DM_DIG_MAX;
1896         dm_digtable->rx_gain_min = DM_DIG_MIN;
1897         dm_digtable->back_val = DM_DIG_BACKOFF_DEFAULT;
1898         dm_digtable->back_range_max = DM_DIG_BACKOFF_MAX;
1899         dm_digtable->back_range_min = DM_DIG_BACKOFF_MIN;
1900         dm_digtable->pre_cck_cca_thres = 0xff;
1901         dm_digtable->cur_cck_cca_thres = 0x83;
1902         dm_digtable->forbidden_igi = DM_DIG_MIN;
1903         dm_digtable->large_fa_hit = 0;
1904         dm_digtable->recover_cnt = 0;
1905         dm_digtable->dig_min_0 = 0x25;
1906         dm_digtable->dig_min_1 = 0x25;
1907         dm_digtable->media_connect_0 = false;
1908         dm_digtable->media_connect_1 = false;
1909         rtlpriv->dm.dm_initialgain_enable = true;
1910         dm_digtable->bt30_cur_igi = 0x32;
1911         dm_digtable->pre_cck_pd_state = CCK_PD_STAGE_MAX;
1912         dm_digtable->cur_cck_pd_state = CCK_PD_STAGE_LOWRSSI;
1913 }
1914 EXPORT_SYMBOL(rtl_dm_diginit);